caml-list  

Re: [Caml-list] Private types in 3.11, again

Dario Teixeira
Tue, 20 Jan 2009 08:34:00 -0800

Hi again,

I ditched the regular variants in favour of polymorphic variants, hoping
the coercion trick described by Jacques Garrigue would help [1].  I've also
simplified the formulation as much as possible.  Anyway, I'm still stuck
with the same problem: while function 'sprint' works alright inside the
Node module, its clone 'sprint2' fails to compile when placed outside.
Is this a case where no amount of explicit annotations and coercions will
work or am I missing something obvious?

Thanks in advance!
Regards,
Dario Teixeira

[1] http://groups.google.com/group/fa.caml/msg/855011402f1ca1b5


module Node:
sig
        type elem_t = [ `Text of string | `Bold of elem_t list ]
        type +'a t = private elem_t

        val text: string -> [> `Basic ] t
        val bold: 'a t list -> [> `Complex ] t
        val sprint: 'a t -> string
end =
struct
        type elem_t = [ `Text of string | `Bold of elem_t list ]
        type +'a t = elem_t

        let text str = `Text str
        let bold seq = `Bold seq
        let rec sprint = function
                | `Text str     -> Printf.sprintf "(Text %s)" str
                | `Bold seq     -> Printf.sprintf "(Bold %s)" (List.fold_left 
(^) "" (List.map sprint seq))
end


module Foobar:
sig
        val sprint2: 'a Node.t -> string
end =
struct
        let rec sprint2 node = match (node : _ Node.t :> [> ]) with
                | `Text str     -> Printf.sprintf "(Text %s)" str
                | `Bold seq     -> Printf.sprintf "(Bold %s)" (List.fold_left 
(^) "" (List.map sprint2 (seq : _ Node.t list :> [>] list)))
end





_______________________________________________
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