* phantom type thing question
@ 2007-03-01 9:46 micha
[not found] ` <95513600703010547t1f098d80p7bbff2f2c85d79a5@mail.gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: micha @ 2007-03-01 9:46 UTC (permalink / raw)
To: ocaml
I'm getting confused while trying to implement some subtyping hierachy
with polymorphic variants.
say I have the hierachy:
symbol -> image -> xpmimage
and types like that:
type 'a sym;;
make_symbol : params -> [`Symbol] sym;;
make_image: params -> [`Image] sym;;
or should it be:
make_image: params -> [`Symbol | `Image] sym;;
make_xpm; params -> [`Symbol | `Image | `Xpm ] sym;;
so that some functions work only on some symbols:
val get_image_width: [< `Image | `XpmImage] sym -> int;;
I think I mix up the [< ..] and [> ... ] type constructs, allthough I
thought I understood it :-)
Michael
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] phantom type thing question
[not found] ` <95513600703010547t1f098d80p7bbff2f2c85d79a5@mail.gmail.com>
@ 2007-03-01 13:49 ` Olivier Andrieu
2007-03-01 16:40 ` micha
1 sibling, 0 replies; 5+ messages in thread
From: Olivier Andrieu @ 2007-03-01 13:49 UTC (permalink / raw)
To: caml-list
On 3/1/07, micha <micha-1@fantasymail.de> wrote:
>
> I'm getting confused while trying to implement some subtyping hierachy
> with polymorphic variants.
> say I have the hierachy:
>
> symbol -> image -> xpmimage
>
> and types like that:
>
> type 'a sym;;
if you want subtyping, you'll need to add a variance annotation to
your type parameter. Otherwise since your type sym is abstract, you'll
get no subtyping at all. In this case you probably want a
contravariant type parameter:
type -'a sym
> make_image: params -> [`Symbol | `Image] sym;;
> make_xpm; params -> [`Symbol | `Image | `Xpm ] sym;;
you can define type names, this gives shorter definitions for deep hierarchies:
type symbol = [`Symbol]
type image = [symbol | `Image]
type xpm = [image | `Xpm]
make_xpm : params -> xpm sym
> so that some functions work only on some symbols:
>
> val get_image_width: [< `Image | `XpmImage] sym -> int;;
>
> I think I mix up the [< ..] and [> ... ] type constructs, allthough I
> thought I understood it :-)
that should be:
val get_image_width : [> xpm] sym -> int
--
Olivier
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] phantom type thing question
[not found] ` <95513600703010547t1f098d80p7bbff2f2c85d79a5@mail.gmail.com>
2007-03-01 13:49 ` [Caml-list] " Olivier Andrieu
@ 2007-03-01 16:40 ` micha
2007-03-01 17:03 ` Olivier Andrieu
1 sibling, 1 reply; 5+ messages in thread
From: micha @ 2007-03-01 16:40 UTC (permalink / raw)
To: Olivier Andrieu, caml-list
Am Thu, 1 Mar 2007 14:47:47 +0100
schrieb "Olivier Andrieu" <oandrieu@gmail.com>:
> On 3/1/07, micha <micha-1@fantasymail.de> wrote:
> >
> if you want subtyping, you'll need to add a variance annotation to
> your type parameter. Otherwise since your type sym is abstract, you'll
> get no subtyping at all. In this case you probably want a
> contravariant type parameter:
>
> type -'a sym
o.k., but after reading section 6.8.1 of the manual I don't see why it
needs to be -'a and not +'a
> that should be:
> val get_image_width : [> xpm] sym -> int
thanks, that helped,
Michael
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] phantom type thing question
2007-03-01 16:40 ` micha
@ 2007-03-01 17:03 ` Olivier Andrieu
2007-03-01 20:01 ` micha
0 siblings, 1 reply; 5+ messages in thread
From: Olivier Andrieu @ 2007-03-01 17:03 UTC (permalink / raw)
To: micha; +Cc: caml-list
On 3/1/07, micha <micha-1@fantasymail.de> wrote:
> Am Thu, 1 Mar 2007 14:47:47 +0100
> schrieb "Olivier Andrieu" <oandrieu@gmail.com>:
>
> > On 3/1/07, micha <micha-1@fantasymail.de> wrote:
> > >
> > if you want subtyping, you'll need to add a variance annotation to
> > your type parameter. Otherwise since your type sym is abstract, you'll
> > get no subtyping at all. In this case you probably want a
> > contravariant type parameter:
> >
> > type -'a sym
>
> o.k., but after reading section 6.8.1 of the manual I don't see why it
> needs to be -'a and not +'a
>
>
> > that should be:
> > val get_image_width : [> xpm] sym -> int
your parameter encodes some "capabilities": you want the function
get_image_width to accept values that have the tag `xpm and possibly
more tags, hence the [> ] in the type of the function.
In other words, you want the type xpm sym to be a subtype of
[xpm|`xpm8] sym (for instance), so that get_image_width accept boths.
Since [xpm|`xpm8] is a subtype of xpm sym (the other way around), that
means the parameter needs to be contravariant.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] phantom type thing question
2007-03-01 17:03 ` Olivier Andrieu
@ 2007-03-01 20:01 ` micha
0 siblings, 0 replies; 5+ messages in thread
From: micha @ 2007-03-01 20:01 UTC (permalink / raw)
To: caml-list
thanks for the examples, I indeed thought the other way round :-)
It's clear now,
Michael
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-03-01 19:56 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-01 9:46 phantom type thing question micha
[not found] ` <95513600703010547t1f098d80p7bbff2f2c85d79a5@mail.gmail.com>
2007-03-01 13:49 ` [Caml-list] " Olivier Andrieu
2007-03-01 16:40 ` micha
2007-03-01 17:03 ` Olivier Andrieu
2007-03-01 20:01 ` micha
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox