summaryrefslogtreecommitdiff
path: root/common.sml
diff options
context:
space:
mode:
authorVladimir Azarov <avm@intermediate-node.net>2025-04-11 21:54:16 +0200
committerVladimir Azarov <avm@intermediate-node.net>2025-04-11 21:54:16 +0200
commite99a8dc48ede26696be2ba75a8cb0d5122d94598 (patch)
treec3dcd1d6a9b96aaedd081f13b9dc7e7d6c07e2bd /common.sml
parent8e2dc7712de206b87e1c46df9383c3fa1e18a43a (diff)
#include directive
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