caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Phantom types and read-only variables
@ 2005-02-05 16:50 Yaron Minsky
  2005-02-05 17:24 ` Yaron Minsky
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Yaron Minsky @ 2005-02-05 16:50 UTC (permalink / raw)
  To: Caml Mailing List

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?

y


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-02-06  0:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-02-05 16:50 Phantom types and read-only variables Yaron Minsky
2005-02-05 17:24 ` Yaron Minsky
2005-02-05 20:21   ` Ethan Aubin
2005-02-05 18:34 ` [Caml-list] " Remi Vanicat
2005-02-05 19:21 ` Markus Mottl
2005-02-06  0:48   ` Jacques Garrigue

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).