From: Remi Vanicat <remi.vanicat@gmail.com>
To: yminsky@cs.cornell.edu
Cc: Caml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Phantom types and read-only variables
Date: Sat, 5 Feb 2005 19:34:28 +0100 [thread overview]
Message-ID: <6b8a9142050205103422fbc441@mail.gmail.com> (raw)
In-Reply-To: <891bd33905020508504e272acf@mail.gmail.com>
On Sat, 5 Feb 2005 11:50:43 -0500, Yaron Minsky <yminsky@gmail.com> wrote:
> I'm trying to use phantom types to build a "freezable" variable, where
> I can create a version of the variable to which write operations can
> not be applied. Here's my first attempt, which was rejected by the
> compiler:
>
> type ro = Readonly
> type rw = Readwrite
>
> module M =
> struct
> type 'a t = { mutable value: int }
>
> let create i = { value = i }
> let freeze t = t
> let read x = x.value
> let write x i =
> x.value <- i
> end
>
> module N =
> (M : sig
> type 'a t
> val create : int -> rw t
> val freeze : 'a t -> ro t
> val read : 'a t -> int
> val write : rw t -> int -> unit
> end)
>
> I do basically understand why the compiler rejects module N. It
> basically complains that the freeze in M is not compatible with the
> constraints on N. In particular:
>
> Values do not match:
> val freeze : 'a -> 'a
> is not included in
> val freeze : 'a t -> ro t
>
> So, what's the right approach here?
Ocaml don't do any automatic type conversion, you have to explicitly coerce :
let freeze t = (t :> ro t)
will do what you want.
next prev parent reply other threads:[~2005-02-05 18:34 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-02-05 16:50 Yaron Minsky
2005-02-05 17:24 ` Yaron Minsky
2005-02-05 20:21 ` Ethan Aubin
2005-02-05 18:34 ` Remi Vanicat [this message]
2005-02-05 19:21 ` [Caml-list] " Markus Mottl
2005-02-06 0:48 ` Jacques Garrigue
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=6b8a9142050205103422fbc441@mail.gmail.com \
--to=remi.vanicat@gmail.com \
--cc=caml-list@inria.fr \
--cc=yminsky@cs.cornell.edu \
/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