Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] ML GMP new version
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: [Caml-list] Re: Phantom types (again) [Novice compilation problem]
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" <mattias.waldau@abc.se>
>
> 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