Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Joseph Young <ocaml@optimojoe.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Conditionals based on phantom types
Date: Tue, 03 Aug 2010 17:39:31 +0200	[thread overview]
Message-ID: <87vd7r4sek.fsf@frosties.localdomain> (raw)
In-Reply-To: <Pine.LNX.4.64.1008020903440.30209@myhome> (Joseph Young's message of "Mon, 2 Aug 2010 09:07:04 +0200 (CEST)")

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.

MfG
        Goswin


      parent reply	other threads:[~2010-08-03 15:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-02  7:07 Joseph Young
2010-08-02  7:49 ` [Caml-list] " Lukasz Stafiniak
2010-08-02  8:02   ` bluestorm
2010-08-03  2:46     ` Joseph Young
2010-08-03  6:15       ` Joseph Young
2010-08-03  6:57         ` bluestorm
2010-08-04  2:41           ` Joseph Young
2010-08-03 15:47     ` Goswin von Brederlow
2010-08-03 15:39 ` Goswin von Brederlow [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=87vd7r4sek.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    --cc=ocaml@optimojoe.com \
    /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