From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id BAA06573; Thu, 3 Jan 2002 01:18:09 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA06301 for ; Thu, 3 Jan 2002 01:18:08 +0100 (MET) Received: from favie.faith.gr.jp (favie.faith.gr.jp [61.127.175.250]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g030I5512707 for ; Thu, 3 Jan 2002 01:18:07 +0100 (MET) Received: from localhost (dhcp7.faith.gr.jp [192.168.1.17]) by favie.faith.gr.jp (8.9.3/8.9.3) with ESMTP id JAA02204; Thu, 3 Jan 2002 09:17:49 +0900 To: mattias.waldau@abc.se Cc: caml-list@inria.fr Subject: [Caml-list] Re: Phantom types (again) [Novice compilation problem] In-Reply-To: References: X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20020103091714J.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 03 Jan 2002 09:17:14 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hello, You have an interesting point here. When using phantom types, in some cases it is not enough to put type annotations in the signature, since they must appear before any meaningful use of the phantom. Here what happens is that you are using "before" and "happen" as fully polymorphic functions inside "illegal" (which is legal), and only later force them to have more specific types. Note that module M : Sig = Struct is (AFAIK) equivalent to defining first module M0 = Struct and then module M : Sig = M0. That is, constraints in the interface have no effect whatsoever on the type checking done inside the structure. Remark also that if you define your module and interface (without illegal) first, and try later to define illegal outside of the module, then you will properly fail: the constraints are now active. Cheers, Jacques Garrigue From: "Mattias Waldau" > > I was looking at phantom types again and realized that I could use > them to get rid of a lot of ' | _ -> assert false ' in my code. > > However, I have to put the typing into the header of the actual > code in the ML-file to get it to work. > > Why doesn't the type checking of 'illegal' fail when I split it > into sig+struct or into mli+ml? > > Below, you first see my original program (with 'assert false'), > and then 2 phantom-type versions. The last one compiles and complains > as expected. > > /mattias > > Even if I used Ocaml a lot for a year now, I haven't really seen > why I should use signatures or MLI-files. I do application > development, and rarely define new signatures. I think they > are mostly needed when you define general data structures. > Compile times are good enough, so I don't need mli-files for > that. Making types abstract makes it harder to test and debug > since you cannot see them the values in the debugger or on the > toplevel. A pointer to a discussion on "Why you should use > signatures" would be appreciated. > > > (* The old code that I am replacing using phantom types > > type flag_t = Before | After ;; > > type t = > One of string * flag_t > | Two of string * flag_t ;; > > let before x = > match x with > | One( str, Before ) -> () > | Two( str, Before ) -> () > | _ -> assert false ;; > > let transform x = > match x with > | One( str, Before ) -> One( str, After ) > | Two( str, Before ) -> Two( str, After ) > | _ -> assert false ;; > > let after x = > match x with > | One( str, After ) -> () > | Two( str, After ) -> () > | _ -> assert false ;; > > > let main x = > before x; > after (transform x) ;; > > > let _ = main (One ("foo", Before)) ;; > *) > > (* But why doesn't splitting the phantom code > into mli+ml generate a compile error for illegal???? ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr