summaryrefslogtreecommitdiff
path: root/common.sml
blob: cb4652aa7f83e952a7058fa3e121a6b8365b121a (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
25
26
27
28
29
30
31
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