src/Pure/General/susp.ML
author skalberg
Fri, 05 Dec 2003 19:39:39 +0100
changeset 14278 ae499452700a
child 16179 fa7e70be26b0
permissions -rw-r--r--
Added lazy sequences and parser combinators for same.

signature SUSP =
sig

type 'a susp

val force : 'a susp -> 'a
val delay : (unit -> 'a) -> 'a susp
val value : 'a -> 'a susp

end

structure Susp :> SUSP =
struct

datatype 'a suspVal
  = Value of 'a
  | Delay of unit -> 'a

type 'a susp = 'a suspVal ref

fun force (ref (Value v)) = v
  | force (r as ref (Delay f)) =
    let
	val v = f ()
    in
	r := Value v;
	v
    end

fun delay f = ref (Delay f)

fun value v = ref (Value v)

end