From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id C1C0FBB81 for ; Sun, 19 Mar 2006 12:20:09 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k2JBK8GP006260 for ; Sun, 19 Mar 2006 12:20:09 +0100 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA21801 for ; Sun, 19 Mar 2006 12:20:08 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k2JBK5ON001116 for ; Sun, 19 Mar 2006 12:20:07 +0100 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id k2JBJwXo002945; Sun, 19 Mar 2006 20:19:59 +0900 (JST) Date: Sun, 19 Mar 2006 20:21:49 +0900 (JST) Message-Id: <20060319.202149.108734866.garrigue@math.nagoya-u.ac.jp> To: daniel.buenzli@epfl.ch Cc: caml-list@inria.fr Subject: Re: [Caml-list] Typing problem From: Jacques Garrigue In-Reply-To: References: X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Miltered: at concorde with ID 441D3E68.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 441D3E65.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; bunzli:01 buenzli:01 epfl:01 statically:01 occurences:01 ocaml's:01 sig:01 val:01 val:01 struct:01 caml-list:01 polymorphic:01 typing:01 typing:01 encode:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 From: Daniel B=FCnzli > I would like to define the following types. > = > > type u =3D [ `U1 | `U2 ] > > > > type 'a t =3D [`A of 'a | `B of ([`U1 ] as 'a) ] constraint 'a =3D = [< u ] > = > t's parameter is used to statically express constraints in other = > parts of the code. My problem is that > the constraint on 'a is downgraded to [`U1] Indeed. Constraints are shared in the whole type, so if 'a has two occurences in the type they represent the same type. > while I would like to be able to write > = > > let param : 'a t -> 'a =3D function (`A v | `B v) -> v > = > and get the following typing behaviour. > = > > let v =3D param (`A `U1) (* should type *) > > let v =3D param (`A `U2) (* should type *) > > let v =3D param (`B `U1) (* should type *) > > let v =3D param (`B `U2) (* should not type *) > = > Is it possible to express that in ocaml's type system ? For the above definition of param, this is clearly impossible: a single variable can have only one (polymorphic) type. Depending on your concrete problem, there may be a way to encode it in the type system, but not along the lines you describe here, which are strongly reminiscent of GADTs. For instance: module M : sig type 'a t =3D private [< `A of 'a | `B of 'a] val a : ([< `U1 | `U2 ] as 'a) -> 'a t val b : [ `U1 ] -> [ `U1 ] t end =3D struct type 'a t =3D [`A of 'a | `B of 'a] let a x =3D `A x let b x =3D `B x end let param : 'a M.t -> 'a =3D function (`A v | `B v) -> v The properties you describe are guaranteed, because all values must be created through M.a and M.b. Hope this helps. Jacques Garrigue