From: oleg@okmij.org
To: pedagand@gmail.com, caml-list@inria.fr
Subject: Re: "Ref" and copy of functions
Date: Sat, 15 Dec 2007 04:08:55 -0800 (PST) [thread overview]
Message-ID: <20071215120855.4A826AF01@Adric.metnet.fnmoc.navy.mil> (raw)
If you like using the mutable state, perhaps you might find the
following code helpful. The key idea is packaging the clone function
along with the arrow. There is no longer any need in any unsafe
features. The lesson from our tagless final APLAS paper is that many
things are significantly easier if we do the work at the production
site rather than at the consumption site.
(* The first component is the arrow itself, the second one is the clone
function*)
type ('a,'b) arrow = {arrow: 'a -> 'b; clone: unit -> ('a,'b) arrow};;
let rec arr f = {arrow = f; clone = fun () -> arr f};;
let rec (>>>) f g = {arrow = (fun c -> g.arrow (f.arrow c));
clone = (fun () -> f.clone () >>> g.clone ())};;
(* Here, our clone function uses the initial state rather than the
current state. It is trivial to clone from the current state, if
desired.*)
let rec loop init f =
let state = ref init in
{arrow =
(fun c ->
let new_state , output = f.arrow ( !state , c ) in
state := new_state;
output);
clone = (fun () -> loop init (f.clone ()))};;
let arr_counter = loop 0 (arr (fun (counter, x) -> counter+1 ,counter+x));;
let arr_double = arr (fun x -> 2*x);;
let arr_my_small_arrow = arr_counter >>> arr_double;;
let run f input = let v1 = f.arrow input in
let v2 = f.arrow input in
(v1,v2);;
let test1 = run arr_my_small_arrow 10;;
val test1 : int * int = (20, 22)
let test2 = run arr_my_small_arrow 10;;
val test2 : int * int = (24, 26) (*obviously, counter keeps
counting *)
let test3 = run (arr_my_small_arrow.clone ()) 10;;
val test3 : int * int = (20, 22) (* cloning `resets' the counter *)
next reply other threads:[~2007-12-15 12:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-15 12:08 oleg [this message]
2007-12-15 15:19 ` Pierre-Evariste Dagand
-- strict thread matches above, loose matches on Subject: below --
2007-12-13 17:27 Pierre-Evariste Dagand
2007-12-14 10:49 ` Zheng Li
2007-12-14 14:51 ` [Caml-list] " David Teller
2007-12-14 16:19 ` Zheng Li
2007-12-14 14:54 ` [Caml-list] " Pierre-Evariste Dagand
2007-12-14 16:12 ` Zheng Li
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=20071215120855.4A826AF01@Adric.metnet.fnmoc.navy.mil \
--to=oleg@okmij.org \
--cc=caml-list@inria.fr \
--cc=oleg@pobox.com \
--cc=pedagand@gmail.com \
/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