| 1 |
|
| 2 |
(* |
| 3 |
* Translation from one sort to another |
| 4 |
*) |
| 5 |
functor MDLAstConstants(Ast : MDL_AST) : MDL_AST_CONSTANTS = |
| 6 |
struct |
| 7 |
|
| 8 |
structure Ast = Ast |
| 9 |
structure A = Ast |
| 10 |
|
| 11 |
fun ID x = A.IDexp(A.IDENT([],x)) |
| 12 |
|
| 13 |
abstype constTable = TABLE of (A.id * A.exp) list ref * int ref |
| 14 |
with fun newConstTable() = TABLE(ref [], ref 0) |
| 15 |
fun const(TABLE(entries, counter)) e = |
| 16 |
let fun lookup [] = |
| 17 |
let val name = "TMP"^ Int.toString(!counter) |
| 18 |
in counter := !counter + 1; |
| 19 |
entries := (name, e) :: !entries; |
| 20 |
ID name |
| 21 |
end |
| 22 |
| lookup((x,e')::rest) = if e = e' then ID x else lookup rest |
| 23 |
in lookup(!entries) end |
| 24 |
fun genConsts(TABLE(entries, _)) = |
| 25 |
map (fn (x,e) => A.VALdecl[A.VALbind(A.IDpat x,e)]) |
| 26 |
(rev(!entries)) |
| 27 |
fun withConsts f = |
| 28 |
let val tbl = newConstTable() |
| 29 |
val decl = f (const tbl) |
| 30 |
val consts = genConsts tbl |
| 31 |
in case consts of |
| 32 |
[] => decl |
| 33 |
| _ => A.LOCALdecl(consts,[decl]) |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
end |