Lazy evaluation with memoing (sequential version).
authorwenzelm
Thu, 01 Oct 2009 18:24:06 +0200
changeset 32817 4ed308181f7f
parent 32816 5db89f8d44f3
child 32827 2729c8033326
Lazy evaluation with memoing (sequential version).
src/Pure/Concurrent/lazy_sequential.ML
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Pure/Concurrent/lazy_sequential.ML	Thu Oct 01 18:24:06 2009 +0200
@@ -0,0 +1,43 @@
+(*  Title:      Pure/Concurrent/lazy_sequential.ML
+    Author:     Florian Haftmann and Makarius, TU Muenchen
+
+Lazy evaluation with memoing (sequential version).
+*)
+
+structure Lazy: LAZY =
+struct
+
+(* datatype *)
+
+datatype 'a expr =
+  Expr of unit -> 'a |
+  Result of 'a Exn.result;
+
+abstype 'a lazy = Lazy of 'a expr Unsynchronized.ref
+with
+
+fun peek (Lazy r) =
+  (case ! r of
+    Expr _ => NONE
+  | Result x => SOME x);
+
+fun lazy e = Lazy (Unsynchronized.ref (Expr e));
+fun value a = Lazy (Unsynchronized.ref (Result (Exn.Result a)));
+
+
+(* force result *)
+
+fun force_result (Lazy r) =
+  (case ! r of
+    Expr e => Exn.capture e ()
+  | Result res => res);
+
+fun force r = Exn.release (force_result r);
+
+fun map_force f = value o f o force;
+
+end;
+end;
+
+type 'a lazy = 'a Lazy.lazy;
+