caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: mattias.waldau@abc.se
Cc: caml-list@inria.fr
Subject: [Caml-list] Re: Phantom types (again) [Novice compilation problem]
Date: Thu, 03 Jan 2002 09:17:14 +0900	[thread overview]
Message-ID: <20020103091714J.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <AAEBJHFJOIPMMIILCEPBKEGPDGAA.mattias.waldau@abc.se>

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


      parent reply	other threads:[~2002-01-03  0:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-01 21:21 [Caml-list] ML GMP new version David Monniaux
     [not found] ` <AAEBJHFJOIPMMIILCEPBKEGPDGAA.mattias.waldau@abc.se>
2002-01-03  0:17   ` Jacques Garrigue [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020103091714J.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=mattias.waldau@abc.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).