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:47)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Conditionals based on phantom types
bluestorm <bluestorm.dylc@gmail.com> writes:

> Two remarks on Lukasz suggestion :
>>   val add : 'a t -> 'a t -> 'a t
>>   [..]
>>   let add (_,x) (_,y) = x +. y
> This does not typecheck. I suggest the following :
> let add (ux, x) (uy, y) =
>   assert (ux = uy);
>   (ux, x +. y)
> While the assertion does not seem necessary at first (correct units
> are guaranteed by typing !), it may be helpful in case of bug inside
> the Units module or signature, wich breaks the typing invariant. If
> you're planning to do relatively elaborate things inside the Units, I
> strongly recommend to use any kind of dynamic checking available, at
> least during development. This is something is understood late in my
> own phantom-type project (Macaque), and would have been very useful
> for debugging.

I tend to hide the phantom tpyes in a submodule with just the bare
minimum of function (creation and access) included and then use the
submodule in the actual module. That way the phantom types are verified
in the actual module too. Instead of assertion failures at runtie you
get compiler errors.