English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Conditionals based on phantom types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-08-03 (15:39)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Conditionals based on phantom types
Joseph Young <ocaml@optimojoe.com> writes:

> Hi,
> 	Is there any way to write a conditional based on the type
> information of a value?  Specifically, if we use phantom types to
> write a module such as

No, there is no way to write a conditional based on the type
information. In trivial cases the hypothetical conditional could be
evaluated by the compiler but only in trivial cases. The more complex
cases would require a runtime evaluation of the type and the type
information simply isn't there at runtime to do this.

> module Units : sig
>     type 'a t
>     val to_feet : float -> [`Feet ] t
>     val to_meters : float -> [`Meters] t
>     val add : 'a t -> 'a t -> 'a t
>     val print : 'a t -> unit
> end = struct
>     type 'a t = float
>     let to_feet x=x
>     let to_meters x=x
>     let add x y = x +. y
>     let print x = Printf.printf "%f (units)" x
> end;;
> is there anyway to modify the print statement to correctly denote
> which units are used?
> 	Thanks.
> Joe

Instead of a phantom type use a real type. See other mails for examples.