* Not a variant??
@ 2008-08-19 22:53 Jacques Carette
2008-08-20 3:21 ` [Caml-list] " Jacques Garrigue
0 siblings, 1 reply; 4+ messages in thread
From: Jacques Carette @ 2008-08-19 22:53 UTC (permalink / raw)
To: OCaml
The function f1 below works fine -- so type d is seen to be a variant type.
However, f2 does not compile because type c is *not* a variant type.
However, if one looks at O'Caml's output for type c, it visually looks
like a variant, while that of d does not. This is highly confusing.
This looks like a bug to me -- both c and d should be recognized as
variant types.
Jacques
type a = [`A ]
type b = [`B ]
type 'a c = 'a
constraint 'a = [< a | b]
;;
type 'a d = 'a
constraint 'a = b
;;
let f1 = function
| #d -> true
| _ -> false
let f2 = function
| #c -> true
| _ -> false
;;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Not a variant??
2008-08-19 22:53 Not a variant?? Jacques Carette
@ 2008-08-20 3:21 ` Jacques Garrigue
2008-08-20 13:24 ` Jacques Carette
0 siblings, 1 reply; 4+ messages in thread
From: Jacques Garrigue @ 2008-08-20 3:21 UTC (permalink / raw)
To: carette; +Cc: caml-list
From: Jacques Carette <carette@mcmaster.ca>
> The function f1 below works fine -- so type d is seen to be a variant type.
> However, f2 does not compile because type c is *not* a variant type.
>
> However, if one looks at O'Caml's output for type c, it visually looks
> like a variant, while that of d does not. This is highly confusing.
>
> This looks like a bug to me -- both c and d should be recognized as
> variant types.
>
> Jacques
>
> type a = [`A ]
> type b = [`B ]
> type 'a c = 'a
> constraint 'a = [< a | b]
> ;;
The message is maybe not clear enough: for pattern-matching and
inheritance, you need an "exact" variant type, i.e. a type whose lower
and upper bounds are identical. This is clearly not the case for
c. The standard workaround is to write:
type c0 = [a | b]
type 'a c = 'a constraint 'a = [< c0]
Jacques Garrigue
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Not a variant??
2008-08-20 3:21 ` [Caml-list] " Jacques Garrigue
@ 2008-08-20 13:24 ` Jacques Carette
2008-08-21 5:10 ` Jacques Garrigue
0 siblings, 1 reply; 4+ messages in thread
From: Jacques Carette @ 2008-08-20 13:24 UTC (permalink / raw)
To: Jacques Garrigue; +Cc: caml-list
Jacques Garrigue wrote:
> The message is maybe not clear enough: for pattern-matching and
> inheritance, you need an "exact" variant type, i.e. a type whose lower
> and upper bounds are identical.
Indeed - I would suggest that the error include some clearer diagnostics
that indicate how the variant is 'inexact'.
> The standard workaround is to write:
>
> type c0 = [a | b]
> type 'a c = 'a constraint 'a = [< c0]
>
This got me further, thanks. But not far enough...
Here is a simple example of the problems I am now running into:
type 'a a = [`A of 'a ]
type 'a b = [`B of 'a ]
type d = [`D ]
type ('a,'b) e = ['b a | 'a b | d]
type ('a,'b) c = ('a,'b) e
constraint 'a = [< 'b a ]
constraint 'b = [< 'a b ]
type 'd cc = 'd
constraint 'd = [< ('a,'b) c]
let is_cc = function
| #cc -> true
| _ -> false
;;
(* complains that cc is not a variant type when trying to define is_cc *)
Jacques Carette
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] Not a variant??
2008-08-20 13:24 ` Jacques Carette
@ 2008-08-21 5:10 ` Jacques Garrigue
0 siblings, 0 replies; 4+ messages in thread
From: Jacques Garrigue @ 2008-08-21 5:10 UTC (permalink / raw)
To: carette; +Cc: caml-list
From: Jacques Carette <carette@mcmaster.ca>
> Jacques Garrigue wrote:
> > The message is maybe not clear enough: for pattern-matching and
> > inheritance, you need an "exact" variant type, i.e. a type whose lower
> > and upper bounds are identical.
> Indeed - I would suggest that the error include some clearer diagnostics
> that indicate how the variant is 'inexact'.
>
> > The standard workaround is to write:
> >
> > type c0 = [a | b]
> > type 'a c = 'a constraint 'a = [< c0]
> >
> This got me further, thanks. But not far enough...
> Here is a simple example of the problems I am now running into:
>
> type 'a a = [`A of 'a ]
> type 'a b = [`B of 'a ]
> type d = [`D ]
> type ('a,'b) e = ['b a | 'a b | d]
>
> type ('a,'b) c = ('a,'b) e
> constraint 'a = [< 'b a ]
> constraint 'b = [< 'a b ]
>
> type 'd cc = 'd
> constraint 'd = [< ('a,'b) c]
>
> let is_cc = function
> | #cc -> true
> | _ -> false
> ;;
>
> (* complains that cc is not a variant type when trying to define is_cc *)
Sure, you must use #c as pattern, not #cc.
Or are you thinking of another problem?
Jacques Garrigue
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2008-08-21 7:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-19 22:53 Not a variant?? Jacques Carette
2008-08-20 3:21 ` [Caml-list] " Jacques Garrigue
2008-08-20 13:24 ` Jacques Carette
2008-08-21 5:10 ` Jacques Garrigue
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox