Version française
Home     About     Download     Resources     Contact us    
Browse thread
private types - suggestion
[ 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: private types - suggestion
[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"
;;