caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* private types - suggestion
@ 2009-01-20 16:43 Jacques Carette
  0 siblings, 0 replies; only message in thread
From: Jacques Carette @ 2009-01-20 16:43 UTC (permalink / raw)
  To: caml-list

[See full code below]

I have encountered a fairly subtle issue with private types which 
doesn't occur otherwise.  The source of the issue, I believe, is 
actually the need to have a 'row variable' available to be able to use 
'private' on a polymorphic variant.

In the code below (a massive simplification of my actual case), f 
compiles, but g does not, with error
Error: This pattern matches values of type
             [> `Operator of 'a * 'b list * 'c S.pk ]
         but is here used to match values of types S.operator
        Types for tag `Operator are incompatible
(pointing to the line that starts with | `Operator ...)

First suggestion: could the error message please include what the 
incompatibility is? 
Second suggestion: could the type S.operator be (also) given in its 
'expanded' form?  It would be useful to know what the compiler thinks 
the type S.operator actually is.
Third suggestion: in this particular case, could the error message 
include some hints that the problem is one of [< ] vs [> ] ? [if my 
understanding of the issue is correct!]

I will add these suggestions to the bug system next, but I figured 
others on this list might benefit from my experience in debugging this 
issue in my code.

Jacques

PS: I actually don't want the intermediate type pk, po, pb and ps to be 
visible at all outside of S, but I do not know how to achieve that 
without adding another layer of indirection.  Any advice along these 
lines would be appreciated.

PPS: I experimented with variance annotations too, thinking that that 
might do the trick, but to no avail. 
---

module S : sig
    type 'a pk = KType | KFormula | Kind of 'a pb
    and  'a po = [`Operator of string * 'a pk list * 'a pk ]
    and  'a pb = [`Thing of string]
    type 'a ps = ['a po | 'a pb]

    type se = se ps
    type operator = private [< se po]
end = struct
    type 'a pk = KType | KFormula | Kind of 'a pb
    and  'a po = [`Operator of string * 'a pk list * 'a pk ]
    and  'a pb = [`Thing of string]
    type 'a ps = ['a po | 'a pb]

    type se = se ps
    type operator = se po
end

open S

(* this one works *)
let f (o:operator) : string =
    match o with
    | `Operator(s, [], S.KType) -> s
    | `Operator(_,_,_)          -> failwith "nope"
(* this one doesn't ! *)
let g (o:operator) : string =
    match o with
    | `Operator(s, [], S.KType) -> s
    | _                         -> failwith "nope"
;;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-01-20 16:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-01-20 16:43 private types - suggestion Jacques Carette

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).