summaryrefslogtreecommitdiff
path: root/src/nbc/promise.sml
blob: 6bb2655e2015fb198cce1af4b75f17dba0c1886b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
structure Promise
:> sig
	type 'fulfillment promise
	val delay: (unit -> 'fulfillment) -> 'fulfillment promise
	val force: 'fulfillment promise -> 'fulfillment
end = struct
	local
                datatype 'expectation lazy =
                        Delayed of unit -> 'expectation
                        | Forced of 'expectation
        in
                type 'expectation promise = 'expectation lazy ref
                fun delay fulfill = ref (Delayed fulfill)
                fun force promise = case !promise of
                        Delayed fulfill =>
                                let
                                        val expectation = fulfill ()
                                in
                                        promise := Forced expectation
                                        ; expectation
                                end
                        | Forced expectation => expectation
        end
end