From: Markus Mottl <markus@oefai.at>
To: "Christopher A. Gorski" <cgorski@cgorski.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] const equivalent for mutable types?
Date: Sat, 31 Jul 2004 12:34:12 +0200 [thread overview]
Message-ID: <20040731103412.GA11964@fichte.ai.univie.ac.at> (raw)
In-Reply-To: <410B5EBD.6060800@cgorski.org>
On Sat, 31 Jul 2004, Christopher A. Gorski wrote:
> In my code I find that I'm passing a lot of mutable values to functions.
> Some functions merely read the values. Others modify the values. Is
> there a method in OCaml for reproducing behavior similar in spirit to
> the const declaration in C?
No, you'd need an abstract module for this to hide the concrete
representation. This is actually good SE-practice.
You can do this very conveniently using so-called phantom types. For the
special case of references, here is an example that implements ones,
which can be made constant:
---------------------------------------------------------------------------
module type REF = sig
type ('a, 'rw) t
val ref : 'a -> ('a, [ `R | `W ]) t
val (!) : ('a, [> `R ]) t -> 'a
val (:=) : ('a, [> `W ]) t -> 'a -> unit
val incr : (int, [> `W ]) t -> unit
val decr : (int, [> `W ]) t -> unit
external const : ('a, [> `R ]) t -> ('a, [ `R ]) t = "%identity"
external normal_ref : ('a, [ `R | `W ]) t -> 'a ref = "%identity"
end
module Ref : REF = struct
type ('a, 'rw) t = 'a ref
let ref = ref
let (!) = (!)
let (:=) = (:=)
let incr = incr
let decr = decr
external const : ('a, [> `R ]) t -> ('a, [ `R ]) t = "%identity"
external normal_ref : ('a, [ `R | `W ]) t -> 'a ref = "%identity"
end
---------------------------------------------------------------------------
The phantom variable is 'rw. When creating references, it can be any
of `R (for reading) and `T (for writing). Some functions only require
the reference to be readable (like (!)), others only need to write to
them (e.g. (:=)). References are made constant by simply applying the
(internal) identity function to them, which is actually a no-op. But we
only leave the `R-flag in the type. A "normal" reference can be made
from the upper ones only if they support both reading and writing.
> let result = change (const r)
>
> and have the compiler give me a type error.
Now we try this out with your example:
---------------------------------------------------------------------------
open Ref
let () =
let t = ref 0 in
let change r = incr r in
let nochange r = Printf.printf "test:%d\n" !r in
change (const t)
---------------------------------------------------------------------------
And we get:
This expression has type (int, [ `R ]) Ref.t but is here used with type
(int, [> `W ]) Ref.t
"nochange" will work without a type error as expected.
Regards,
Markus
--
Markus Mottl http://www.oefai.at/~markus markus@oefai.at
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
next prev parent reply other threads:[~2004-07-31 10:34 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-31 8:56 Christopher A. Gorski
2004-07-31 9:24 ` Jean-Marie Gaillourdert
2004-07-31 10:24 ` Jean-Marie Gaillourdert
2004-07-31 10:50 ` Markus Mottl
2004-07-31 14:31 ` Brian Hurt
2004-07-31 15:51 ` Markus Mottl
2004-07-31 17:05 ` skaller
2004-07-31 10:34 ` Markus Mottl [this message]
2004-07-31 13:44 ` Jon Harrop
2004-07-31 16:31 ` [Caml-list] Phantom types Markus Mottl
2004-08-23 9:49 ` Jon Harrop
2004-08-23 12:25 ` [Caml-list] Why does ocaml use custom buffering? Daan Leijen
2004-08-23 15:16 ` [Caml-list] Phantom types Jon Harrop
2004-08-27 9:03 ` Jacques GARRIGUE
2004-08-25 21:03 ` brogoff
2004-07-31 16:35 ` [Caml-list] const equivalent for mutable types? skaller
2004-07-31 17:23 ` [Caml-list] Functional arrays Jon Harrop
2004-07-31 18:45 ` skaller
2004-08-02 5:07 ` brogoff
2004-08-02 7:45 ` Diego Olivier Fernandez Pons
2004-08-05 16:42 ` Daniel Ortmann
2004-08-05 17:02 ` Diego Olivier Fernandez Pons
2004-08-05 17:16 ` Diego Olivier Fernandez Pons
2004-07-31 17:45 ` [Caml-list] const equivalent for mutable types? Chris Gorski
2004-07-31 14:11 ` Brian Hurt
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=20040731103412.GA11964@fichte.ai.univie.ac.at \
--to=markus@oefai.at \
--cc=caml-list@inria.fr \
--cc=cgorski@cgorski.org \
/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