diff options
Diffstat (limited to 'src/nbc/promise.sml')
-rw-r--r-- | src/nbc/promise.sml | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/nbc/promise.sml b/src/nbc/promise.sml new file mode 100644 index 0000000..6bb2655 --- /dev/null +++ b/src/nbc/promise.sml @@ -0,0 +1,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 |