caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Documentation error - #myvariant
@ 2004-04-29 11:18 Keith Wansbrough
  2004-04-29 12:40 ` Remi Vanicat
  0 siblings, 1 reply; 4+ messages in thread
From: Keith Wansbrough @ 2004-04-29 11:18 UTC (permalink / raw)
  To: caml-list

Hi.. the OCaml documentation, section 4.2 on polymorphic variants,
Advanced use, explains that

  To make this even more confortable, you may use type definitions as
  abbreviations for or-patterns. That is, if you have defined type
  myvariant = [`Tag1 int | `Tag2 bool], then the pattern #myvariant is
  equivalent to writing (`Tag1(_ : int) | `Tag2(_ : bool)).

But this is not correct!  Consider

  type de = [`D | `E of de];;
  
  type def = [`D | `E of def | `F of def];;
  
  let rec deproc2 rfun =
    function
      | `D -> print_string "D"; `D
      | `E(x) -> print_string "E"; `E(rfun x)
  
  let rec deproc3 x = deproc2 deproc3 x
  
  let rec defproc2 rfun =
    function
  (*    | (`D | `E(_)) as x -> deproc2 rfun x *)
      | #de as x -> deproc2 rfun x
      | `F(x) -> print_string "f"; `F(rfun x)
  
  let rec defproc3 : def -> def =
    fun x -> defproc2 defproc3 x
  
This gives the following error (in OCaml 3.07+2):

  File "polyvar2.ml", line 19, characters 29-30:
  This expression has type def = [ `D | `E of def | `F of def ]
  but is here used with type [< `D | `E of de | `F of de ]
  Type def = [ `D | `E of def | `F of def ] is not compatible with type
    de = [ `D | `E of de ] 

(Note that the expression mentioned is the final x on the last line.)
Replacing the #de line with the commented line above it, however,
yields

  val defproc3 : def -> def = <fun>

as expected.  It looks to me like #de means (`D | `E(_:de)), rather
than (`D | `E(_)) as I expected; except that I'm not even sure what
`E(_:de) means in this case - does it do type-directed matching at
runtime?

Could the documentation please be made more accurate at this point?

Thanks.

--KW 8-)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Documentation error - #myvariant
  2004-04-29 11:18 [Caml-list] Documentation error - #myvariant Keith Wansbrough
@ 2004-04-29 12:40 ` Remi Vanicat
  2004-04-29 13:54   ` Keith Wansbrough
  2004-04-29 18:09   ` skaller
  0 siblings, 2 replies; 4+ messages in thread
From: Remi Vanicat @ 2004-04-29 12:40 UTC (permalink / raw)
  To: caml-list

Keith Wansbrough <Keith.Wansbrough@cl.cam.ac.uk> writes:

> Hi.. the OCaml documentation, section 4.2 on polymorphic variants,
> Advanced use, explains that
>
>   To make this even more confortable, you may use type definitions as
>   abbreviations for or-patterns. That is, if you have defined type
>   myvariant = [`Tag1 int | `Tag2 bool], then the pattern #myvariant is
>   equivalent to writing (`Tag1(_ : int) | `Tag2(_ : bool)).
>
> But this is not correct!  Consider
>
>   type de = [`D | `E of de];;
>   
>   type def = [`D | `E of def | `F of def];;
>   
>   let rec deproc2 rfun =
>     function
>       | `D -> print_string "D"; `D
>       | `E(x) -> print_string "E"; `E(rfun x)
>   
>   let rec deproc3 x = deproc2 deproc3 x
>   
>   let rec defproc2 rfun =
>     function
>   (*    | (`D | `E(_)) as x -> deproc2 rfun x *)
>       | #de as x -> deproc2 rfun x
>       | `F(x) -> print_string "f"; `F(rfun x)

If I read the documentation, this is rewrote as 
  let rec defproc2 rfun =
    function
    | (`D | `E(_:de)) as x -> deproc2 rfun x
    | `F(x) -> print_string "f"; `F(rfun x)


that mean that what is in the `E must be a de, not a def. There is
your error.


In fact, your code will do what you want if you don't define your type
as recursive but as polymorphic :

type 'a de = [`D | `E of 'a];;
  
type 'a def = [`D | `E of 'a | `F of 'a];;

then, your code will work as expected. Well, the last definition became :
let rec defproc3 : ('a def as 'a) -> 'a =
    fun x -> defproc2 defproc3 x


There is a very interesting
example about this in the ocaml source : ocaml/testlabl/mixin.ml


[...]


> as expected.  It looks to me like #de means (`D | `E(_:de)), rather
> than (`D | `E(_)) as I expected; except that I'm not even sure what
> `E(_:de) means in this case - does it do type-directed matching at
> runtime?

no, it is a constraint on the type of what is the `E done at compile
time. And it is the source of your error because a de is not a def.

>
> Could the documentation please be made more accurate at this point?
>

The documentation is accurate.

-- 
Rémi Vanicat

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Documentation error - #myvariant
  2004-04-29 12:40 ` Remi Vanicat
@ 2004-04-29 13:54   ` Keith Wansbrough
  2004-04-29 18:09   ` skaller
  1 sibling, 0 replies; 4+ messages in thread
From: Keith Wansbrough @ 2004-04-29 13:54 UTC (permalink / raw)
  To: Remi Vanicat; +Cc: caml-list

Rémi Vanicat writes:
> The documentation is accurate.

I think the documentation is unclear, and should be clarified.  All
the OCaml manual says is:

  "if you have defined type myvariant = [`Tag1 int | `Tag2 bool], then
  the pattern #myvariant is equivalent to writing (`Tag1(_ : int) |
  `Tag2(_ : bool))."

This says nothing about how recursive types are treated when
#myvariant occurs in a supertype context.  Since this is surely a
common use of polymorphic variants, it should at least say "the g1 / g
example is misleading, as this #myvariant style won't work in most
useful cases".

> In fact, your code will do what you want if you don't define your type
> as recursive but as polymorphic :

OK - that type trick corresponds to the term trick already explained
in the manual, with passing the recursive function explicitly.  Shades
of Steele, 1994, _Building Interpreters by Composing Monads_(!).

Thanks..

--KW 8-)

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Caml-list] Documentation error - #myvariant
  2004-04-29 12:40 ` Remi Vanicat
  2004-04-29 13:54   ` Keith Wansbrough
@ 2004-04-29 18:09   ` skaller
  1 sibling, 0 replies; 4+ messages in thread
From: skaller @ 2004-04-29 18:09 UTC (permalink / raw)
  To: Remi Vanicat; +Cc: caml-list

On Thu, 2004-04-29 at 22:40, Remi Vanicat wrote:

> > But this is not correct!  Consider
> >
> >   type de = [`D | `E of de];;
> >   
> >   type def = [`D | `E of def | `F of def];;

> In fact, your code will do what you want if you don't define your type
> as recursive but as polymorphic :
> 
> type 'a de = [`D | `E of 'a];;
>   
> type 'a def = [`D | `E of 'a | `F of 'a];;


type 'a de' = [`D | `E of 'a]
type 'a def' = ['a de' | `F of 'a]

type de = 'a de' as 'a
type def = 'a def' as 'a



-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-04-29 18:09 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-29 11:18 [Caml-list] Documentation error - #myvariant Keith Wansbrough
2004-04-29 12:40 ` Remi Vanicat
2004-04-29 13:54   ` Keith Wansbrough
2004-04-29 18:09   ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).