18 val discard : program -> unit |
18 val discard : program -> unit |
19 |
19 |
20 exception Run of string; |
20 exception Run of string; |
21 val run : program -> term -> term |
21 val run : program -> term -> term |
22 |
22 |
|
23 (* Utilities *) |
|
24 |
|
25 val check_freevars : int -> term -> bool |
|
26 val forall_consts : (int -> bool) -> term -> bool |
|
27 val closed : term -> bool |
|
28 val erase_Computed : term -> term |
|
29 |
23 end |
30 end |
24 |
31 |
25 structure AbstractMachine : ABSTRACT_MACHINE = |
32 structure AbstractMachine : ABSTRACT_MACHINE = |
26 struct |
33 struct |
27 |
34 |
28 datatype term = Var of int | Const of int | App of term * term | Abs of term |
35 datatype term = Var of int | Const of int | App of term * term | Abs of term | Computed of term |
29 |
36 |
30 datatype pattern = PVar | PConst of int * (pattern list) |
37 datatype pattern = PVar | PConst of int * (pattern list) |
31 |
38 |
32 datatype guard = Guard of term * term |
39 datatype guard = Guard of term * term |
33 |
40 |
34 type program = unit |
41 type program = unit |
35 |
42 |
36 exception Compile of string; |
43 exception Compile of string; |
|
44 |
|
45 fun erase_Computed (Computed t) = erase_Computed t |
|
46 | erase_Computed (App (t1, t2)) = App (erase_Computed t1, erase_Computed t2) |
|
47 | erase_Computed (Abs t) = Abs (erase_Computed t) |
|
48 | erase_Computed t = t |
|
49 |
|
50 (*Returns true iff at most 0 .. (free-1) occur unbound. therefore |
|
51 check_freevars 0 t iff t is closed*) |
|
52 fun check_freevars free (Var x) = x < free |
|
53 | check_freevars free (Const c) = true |
|
54 | check_freevars free (App (u, v)) = check_freevars free u andalso check_freevars free v |
|
55 | check_freevars free (Abs m) = check_freevars (free+1) m |
|
56 | check_freevars free (Computed t) = check_freevars free t |
|
57 |
|
58 fun forall_consts pred (Const c) = pred c |
|
59 | forall_consts pred (Var x) = true |
|
60 | forall_consts pred (App (u,v)) = forall_consts pred u |
|
61 andalso forall_consts pred v |
|
62 | forall_consts pred (Abs m) = forall_consts pred m |
|
63 | forall_consts pred (Computed t) = forall_consts pred t |
|
64 |
|
65 fun closed t = check_freevars 0 t |
37 |
66 |
38 fun compile _ = raise Compile "abstract machine stub" |
67 fun compile _ = raise Compile "abstract machine stub" |
39 |
68 |
40 fun discard _ = raise Compile "abstract machine stub" |
69 fun discard _ = raise Compile "abstract machine stub" |
41 |
70 |