summaryrefslogtreecommitdiff
path: root/common.sml
diff options
context:
space:
mode:
Diffstat (limited to 'common.sml')
-rw-r--r--common.sml32
1 files changed, 32 insertions, 0 deletions
diff --git a/common.sml b/common.sml
new file mode 100644
index 0000000..cb4652a
--- /dev/null
+++ b/common.sml
@@ -0,0 +1,32 @@
+exception Unreachable
+
+fun $ (x, y) = x y
+infixr 0 $
+
+fun printLn s = (print s; print "\n")
+
+(* All global values which computations may raise an exception must be
+ * wrapped in lazy, so that no exception is thrown before custom
+ * top-level handler is set.
+ *)
+fun lazy thunk =
+let
+ datatype 'a value =
+ Unevaluated of unit -> 'a |
+ Evaluated of 'a |
+ Exn of exn
+
+ val value = ref $ Unevaluated thunk
+in
+ fn () =>
+ case !value of
+ Unevaluated th =>
+ let
+ val x = th () handle e => (value := Exn e; raise e)
+ in
+ value := Evaluated x;
+ x
+ end
+ | Evaluated v => v
+ | Exn e => raise e
+end