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
Phantom types and read-only variables
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-02-05 (16:50)
From: Yaron Minsky <yminsky@g...>
Subject: Phantom types and read-only variables
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

type ro = Readonly
type rw = Readwrite

module M = 
  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

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

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?