caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Typechecking of recursive variants
@ 2000-05-12 14:34 Judicael Courant
  2000-05-15  0:33 ` Jacques Garrigue
  0 siblings, 1 reply; 2+ messages in thread
From: Judicael Courant @ 2000-05-12 14:34 UTC (permalink / raw)
  To: caml-list

Hello,

I do not understand the following behavior of the typechecker :
match x 4 with `X z -> z | `Y -> assert false;;
- : [ `Y | `X of '_a] as 'a = `X (`X (`X `Y))

        Objective Caml version 3.00

# let rec x n = if n = 0 then `Y else `X (x (n-1));;
val x : int -> ([> `Y | `X of 'a] as 'a) = <fun>
# x 3;;
- : _[> `Y | `X of '_a] as 'a = `X (`X (`X `Y))
# match x 4 with `X z -> z | `Y -> assert false;;
- : [ `Y | `X of '_a] as 'a = `X (`X (`X `Y))


What do these underscores mean ? Anything to do with monomorphic type
variables ? Why is not there any ">" at the beginning of the latest
type ?

Judicaël.
-- 
Judicael.Courant@lri.fr, http://www.lri.fr/~jcourant/
(+33) (0)1 69 15 64 85
"Montre moi des morceaux de ton monde, et je te montrerai le mien"
Tim, matricule #929, condamné à mort.
http://rozenn.picard.free.fr/tim.html





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Typechecking of recursive variants
  2000-05-12 14:34 Typechecking of recursive variants Judicael Courant
@ 2000-05-15  0:33 ` Jacques Garrigue
  0 siblings, 0 replies; 2+ messages in thread
From: Jacques Garrigue @ 2000-05-15  0:33 UTC (permalink / raw)
  To: Judicael.Courant; +Cc: caml-list

From: Judicael Courant <Judicael.Courant@lri.fr>

>         Objective Caml version 3.00
> 
> # let rec x n = if n = 0 then `Y else `X (x (n-1));;
> val x : int -> ([> `Y | `X of 'a] as 'a) = <fun>
> # x 3;;
> - : _[> `Y | `X of '_a] as 'a = `X (`X (`X `Y))
> # match x 4 with `X z -> z | `Y -> assert false;;
> - : [ `Y | `X of '_a] as 'a = `X (`X (`X `Y))
> 
> 
> What do these underscores mean ? Anything to do with monomorphic type
> variables ?

Exactly. Since "x 3" is an application, its result must be
monomorphic.
But in fact the _ in '_a is a bug :-)
In recursive variants, the monomorphism is not indicated on the
abbreviation, but on the variant type itself (like for objects).
So the right types would be
# x 3;;
- : _[> `Y | `X of 'a] as 'a = `X (`X (`X `Y))
# match x 4 with `X z -> z | `Y -> assert false;;
- : [ `Y | `X of 'a] as 'a = `X (`X (`X `Y))
Notice that there is no monomorphism annotation in the second type,
since it cannot be refined anymore.

> Why is not there any ">" at the beginning of the latest
> type ?
Your original type was [> `Y | `X of 'a], and you matched it with
cases producing the type [< `Y | `X of 'a]. By unification, the
result is [< `Y | `X of 'a > `Y | `X], or, in abbreviated form,
[`Y | `X of 'a] since all constructors are required.
Since you type was monomorphic, this unification affects the type
of "x 4", which is also the type of the argument of `X.

        Jacques
---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2000-05-15 20:09 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-05-12 14:34 Typechecking of recursive variants Judicael Courant
2000-05-15  0:33 ` Jacques Garrigue

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