src/Tools/Metis/src/Lazy.sml
changeset 23442 028e39e5e8f3
child 23510 4521fead5609
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/Tools/Metis/src/Lazy.sml	Wed Jun 20 22:07:52 2007 +0200
@@ -0,0 +1,33 @@
+(* ========================================================================= *)
+(* SUPPORT FOR LAZY EVALUATION                                               *)
+(* Copyright (c) 2007 Joe Hurd, distributed under the GNU GPL version 2      *)
+(* ========================================================================= *)
+
+structure Lazy :> Lazy =
+struct
+
+datatype 'a thunk =
+    Value of 'a
+  | Thunk of unit -> 'a;
+
+datatype 'a lazy = Lazy of 'a thunk ref;
+
+fun delay f = Lazy (ref (Thunk f));
+
+fun force (Lazy (ref (Value v))) = v
+  | force (Lazy (s as ref (Thunk f))) =
+    let
+      val v = f ()
+      val () = s := Value v
+    in
+      v
+    end;
+
+fun memoize f =
+    let
+      val t = delay f
+    in
+      fn () => force t
+    end;
+
+end