caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Ethan Aubin <aubineth@ccs.neu.edu>
To: caml-list@inria.fr
Subject: Re: Phantom types and read-only variables
Date: Sat, 5 Feb 2005 20:21:26 +0000 (UTC)	[thread overview]
Message-ID: <cu39s6$t4q$1@sea.gmane.org> (raw)
In-Reply-To: <891bd33905020509243edf074d@mail.gmail.com>

(*
In article <891bd33905020508504e272acf@mail.gmail.com> you 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:

Coincidentally, I was trying to to get a better grasp on phantom types
and wrote something which you might find useful. My goal was to simulate
file useage so that 1) you could not write a readonly file 2) you
could downgrade the permissions on a file from read-write to readonly
and 3) a file must be closed after using it.

Perhaps some more advanced camler could like to tell me how to make
this code more pretty? Also, I don't know if this is a proper arrow or
not... -  ethan.aubin@pobox.com
*)

type ('s1,'s2,'a) stateA  = State of ('s1 -> ('a * 's2))

type filename = string ref
type 'a file = File of filename

type ('s1,'s2,'b) fileSt = ('s1 file,'s2 file, 'b) stateA
let return a : ('a,'b,'c) fileSt = State (fun s -> (a,s))
let modify f : ('a,'b,'c) fileSt = State (fun st -> (), f st);;

let frwopen () : [< `Read | `Write] file = File (ref "empty")
let fropen () : [< `Read] file = File (ref "empty")
let fwopen () : [< `Write] file = File (ref "empty")

let read : (([> `Read] as 'perm), 'perm, string) fileSt = 
    State (fun st -> let File r = st in !r,st)

let write ~str : ([> `Write] as 'perm,'perm,unit) fileSt = 
  State (fun (File r) ->
    r := str;
    (),File r)

let print : (([> `Read] as 'a),'a,unit) fileSt  =
  State (fun (File r) -> Printf.printf "value=%s\n" !r; ((), File r));;

let closef ((File r) : [< `Write | `Read] file) : [`Close] file
    = File r;;
(*
 This is not useful, because infered type is:
 val close : (_[< `Read | `Write ], [ `Close ], unit) fileSt
*)  
(* let close = modify closef;; *)

let to_readonly =
  let f ((File r) : [> `Read] file) : [`Read] file = File r in
  modify f;;

let (>>>)
    ((State f) : ('a,'b,'c) fileSt)
    ((State g)  : ('d,'e,'f) fileSt) : ('g,'h,'i) fileSt =
  State (fun s ->
    let (_,s1) = f s in
    g s1);;

let r = read
and w = write "i";;


let runFileSt
    ~comp:((State f) : ('perm,[`Close],'v) fileSt)
    ~(st : [< `Read | `Write] file) () : 'v =
  let (v,_) = f st in v;;

(* Print a file, update it, print the new value, close it *)
let comp = print >>> w >>> print >>> (modify closef);;
runFileSt ~comp ~st:(frwopen ()) ();;

let permdown = print >>> w >>> to_readonly >>> print >>> (modify closef);;
runFileSt ~comp:permdown ~st:(frwopen ()) ();;

(* This correctly doesn't type *)
(* let write_to_readonly = print >>> w >>> to_readonly >>> w >>> (modify closef);; *)

(* Don't close the file, *)
let bad_file_left_open = print >>> w >>> print;;

(* so we refuse to run it. i.e. This fails to type *)
(* runFileSt ~comp:bad_file_left_open ~st ();; *)



  reply	other threads:[~2005-02-05 20:22 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 [this message]
2005-02-05 18:34 ` [Caml-list] " Remi Vanicat
2005-02-05 19:21 ` 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='cu39s6$t4q$1@sea.gmane.org' \
    --to=aubineth@ccs.neu.edu \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).