Version française
Home     About     Download     Resources     Contact us    
Browse thread
Troublesome nodes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Carette <carette@m...>
Subject: Re: [Caml-list] Troublesome nodes
The ingenious use of a recursive module signature seems to be the source 
of the problem here.  If one defines 'bar' inside the module Node, it 
works fine [but is clearly useless].

What surprises me is that the code (below) also gives the exact same 
error.  In other contexts, I have managed to get such annotations (see 
the definition of the bold function) to 'work', but not here.  I am 
quite puzzled by this, so if you do get an answer to your question 
offl-ist, I would appreciate if you could forward it on.

Jacques

PS: because I am actually using metaocaml for other projects, I am using 
ocaml 3.09 for my tests, so it is possible that something is different 
in 3.10.

module Node :
sig
    type nonlink_node_t
    type link_node_t
    type super_node_t

    val text: string -> nonlink_node_t
    val bold: super_node_t list -> nonlink_node_t
    val see: string -> link_node_t
    val mref: string -> nonlink_node_t list -> link_node_t
end =
struct
    type 'a p1 = [ `Text of string | `Bold of 'a list ]
    type 'a p2 = [ `See of string | `Mref of string * 'a p1 list ]
    type 'a p3 = [ 'a p1 | 'a p2 ]

    type super_node_t = super_node_t p3
    type nonlink_node_t = super_node_t p1
    type link_node_t = super_node_t p2

    let text txt = `Text txt
    let bold seq = `Bold (seq :> super_node_t list)
    let see ref = `See ref
    let mref ref seq = `Mref (ref, seq)
end;;

Dario Teixeira wrote:
> Hi,
>
> Thanks Jeremy, that's quite ingenious.  However, I've hit a problem in the
> definition of the constructor functions (the full code + compiler error
> is below).  Now, I know that nonlink_node_t is a subset of super_node_t,
> and therefore any variant valid for nonlink_node_t is also acceptable for
> super_node_t.  But how do I tell this to the compiler?  (Note that having
> users of the module manually casting types with :> is something I would
> rather avoid).
>
> Thanks again,
> Dario
>
>
> module rec Node:
> sig
> 	type nonlink_node_t = [ `Text of string | `Bold of Node.super_node_t list ]
> 	type link_node_t = [ `See of string | `Mref of string * nonlink_node_t list ]
> 	type super_node_t = [ nonlink_node_t | link_node_t ]
>
> 	val text: string -> nonlink_node_t
> 	val bold: super_node_t list -> nonlink_node_t
> 	val see: string -> link_node_t
> 	val mref: string -> nonlink_node_t list -> link_node_t
> end =
> struct
> 	type nonlink_node_t = [ `Text of string | `Bold of Node.super_node_t list ]
> 	type link_node_t = [ `See of string | `Mref of string * nonlink_node_t list ]
> 	type super_node_t = [ nonlink_node_t | link_node_t ]
>
> 	let text txt = `Text txt
> 	let bold seq = `Bold seq
> 	let see ref = `See ref
> 	let mref ref seq = `Mref (ref, seq)
> end
>
> open Node
> let foo = text "foo"
> let bar = bold [text "bar"]
>
>
> Error: This expression has type Node.nonlink_node_t
>        but is here used with type Node.super_node_t
>        The first variant type does not allow tag(s) `Mref, `See
>
>
>
>
>       __________________________________________________________
> Not happy with your email address?.
> Get the one you really want - millions of new email addresses available now at Yahoo! http://uk.docs.yahoo.com/ymail/new.html
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>