| 1 |
(* |
| 2 |
* Translation from one sort to another |
| 3 |
*) |
| 4 |
functor MDLAstTranslation |
| 5 |
(structure AstPP : MDL_AST_PRETTY_PRINTER |
| 6 |
structure AstRewriter : MDL_AST_REWRITER |
| 7 |
sharing AstRewriter.Ast = AstPP.Ast |
| 8 |
) : MDL_AST_TRANSLATION = |
| 9 |
struct |
| 10 |
|
| 11 |
structure Ast = AstPP.Ast |
| 12 |
structure A = Ast |
| 13 |
structure R = AstRewriter |
| 14 |
structure H = HashTable |
| 15 |
|
| 16 |
fun error msg = MLRiscErrorMsg.error("MDLAstTranslation",msg) |
| 17 |
|
| 18 |
type 'a map = {origName : Ast.id, |
| 19 |
newName : Ast.id, |
| 20 |
ty : Ast.ty |
| 21 |
} -> 'a |
| 22 |
|
| 23 |
type 'a folder = {origName : Ast.id, |
| 24 |
newName : Ast.id, |
| 25 |
ty : Ast.ty} * 'a -> 'a |
| 26 |
|
| 27 |
fun ID x = A.IDexp(A.IDENT([],x)) |
| 28 |
|
| 29 |
exception NoName |
| 30 |
|
| 31 |
(* |
| 32 |
* Treat a type expression as a pattern and compute its set of |
| 33 |
* variable bindings. Duplicates are given unique suffixes. |
| 34 |
*) |
| 35 |
fun bindingsInTy ty = |
| 36 |
let val namesTable = H.mkTable (HashString.hashString,op =)(32,NoName) |
| 37 |
val variables = ref 0 |
| 38 |
|
| 39 |
fun enterName id = |
| 40 |
let val _ = variables := !variables + 1 |
| 41 |
val (_, totalCount) = H.lookup namesTable id |
| 42 |
in totalCount := !totalCount + 1 |
| 43 |
end handle _ => H.insert namesTable (id, (ref 0,ref 1)) |
| 44 |
|
| 45 |
fun enter(A.IDty(A.IDENT(_,id))) = enterName id |
| 46 |
| enter(A.TYVARty(A.VARtv id)) = enterName id |
| 47 |
| enter(A.APPty(A.IDENT(_,id),_)) = enterName id |
| 48 |
| enter(A.CELLty id) = enterName id |
| 49 |
| enter(A.TUPLEty tys) = app enter tys |
| 50 |
| enter(A.RECORDty ltys) = app (fn (id, _) => enterName id) ltys |
| 51 |
| enter t = error("bindingsInTy: "^PP.text(AstPP.ty t)) |
| 52 |
val stripTicks = String.map (fn #"'" => #"t" | c => c) |
| 53 |
fun getName id = |
| 54 |
let val (currentCount, totalCount) = H.lookup namesTable id |
| 55 |
in stripTicks( |
| 56 |
if !totalCount = 1 then id (* use the same name *) |
| 57 |
else |
| 58 |
(currentCount := !currentCount + 1; |
| 59 |
id^Int.toString(!currentCount) |
| 60 |
) |
| 61 |
) |
| 62 |
end |
| 63 |
in enter ty; |
| 64 |
(!variables, getName) |
| 65 |
end |
| 66 |
|
| 67 |
(* |
| 68 |
* Translate a type into a pattern expression |
| 69 |
*) |
| 70 |
fun mapTyToPat f' ty = |
| 71 |
let val (_,getName) = bindingsInTy ty |
| 72 |
fun f(id,ty) = f'{origName=id,newName=getName id,ty=ty} |
| 73 |
fun g(A.IDty(A.IDENT(_,id)), ty) = f(id,ty) |
| 74 |
| g(A.TYVARty(A.VARtv id), ty) = f(id, ty) |
| 75 |
| g(A.APPty(A.IDENT(_,id),_), ty) = f(id, ty) |
| 76 |
| g(A.CELLty id, ty) = f(id, ty) |
| 77 |
| g(A.TUPLEty tys, _) = A.TUPLEpat(map g' tys) |
| 78 |
| g(A.RECORDty ltys, _) = A.RECORDpat(map h ltys,false) |
| 79 |
| g(t, _) = error("tyToPat: "^PP.text(AstPP.ty t)) |
| 80 |
and g' t = g(t,t) |
| 81 |
and h(lab, ty) = (lab, f(lab, ty)) |
| 82 |
in g' ty |
| 83 |
end |
| 84 |
|
| 85 |
fun foldTy f' x ty = |
| 86 |
let val (_,getName) = bindingsInTy ty |
| 87 |
fun f(id,ty,x) = f'({origName=id,newName=getName id,ty=ty},x) |
| 88 |
fun g(A.IDty(A.IDENT(_,id)),ty,x) = f(id,ty,x) |
| 89 |
| g(A.TYVARty(A.VARtv id), ty, x) = f(id,ty,x) |
| 90 |
| g(A.APPty(A.IDENT(_,id),_), ty, x) = f(id,ty, x) |
| 91 |
| g(A.CELLty id,ty,x) = f(id,ty,x) |
| 92 |
| g(A.TUPLEty tys,ty,x) = foldr g' x (rev tys) |
| 93 |
| g(A.RECORDty ltys,ty,x) = foldr h x (rev ltys) |
| 94 |
| g(t, ty, x) = error("foldTyBindings: "^PP.text(AstPP.ty t)) |
| 95 |
and g'(t,x) = g(t,t,x) |
| 96 |
and h((lab, ty),x) = f(lab,ty,x) |
| 97 |
in g'(ty,x) |
| 98 |
end |
| 99 |
|
| 100 |
fun foldCons f x (A.CONSbind{ty=NONE, ...}) = x |
| 101 |
| foldCons f x (A.CONSbind{ty=SOME ty, ...}) = foldTy f x ty |
| 102 |
|
| 103 |
(* |
| 104 |
* Translate a type into an expression |
| 105 |
*) |
| 106 |
fun mapTyToExp f' ty = |
| 107 |
let val (_,getName) = bindingsInTy ty |
| 108 |
fun f(id,ty) = f'{origName=id,newName=getName id,ty=ty} |
| 109 |
fun g(A.IDty(A.IDENT(_,id)), ty) = f(id, ty) |
| 110 |
| g(A.TYVARty(A.VARtv id), ty) = f(id, ty) |
| 111 |
| g(A.APPty(A.IDENT(_,id),_), ty) = f(id, ty) |
| 112 |
| g(A.CELLty id, ty) = f(id, ty) |
| 113 |
| g(A.TUPLEty tys, ty) = A.TUPLEexp(map g' tys) |
| 114 |
| g(A.RECORDty ltys, ty) = A.RECORDexp(map h ltys) |
| 115 |
| g(t, _) = error("tyToPat: "^PP.text(AstPP.ty t)) |
| 116 |
and g' t = g(t,t) |
| 117 |
and h(lab, ty) = (lab, f(lab, ty)) |
| 118 |
in g' ty |
| 119 |
end |
| 120 |
|
| 121 |
(* |
| 122 |
* Translate a constructor into a pattern |
| 123 |
*) |
| 124 |
fun mapConsToPat {prefix, id} (A.CONSbind{id=x, ty, ...}) = |
| 125 |
A.CONSpat(A.IDENT(prefix,x), Option.map (mapTyToPat id) ty) |
| 126 |
(* |
| 127 |
* Translate a constructor into an expression |
| 128 |
*) |
| 129 |
fun mapConsToExp {prefix,id} (A.CONSbind{id=x, ty, ...}) = |
| 130 |
A.CONSexp(A.IDENT(prefix,x), Option.map (mapTyToExp id) ty) |
| 131 |
|
| 132 |
fun mapConsArgToExp id (A.CONSbind{ty=NONE, ...}) = A.TUPLEexp [] |
| 133 |
| mapConsArgToExp id (A.CONSbind{ty=SOME ty, ...}) = mapTyToExp id ty |
| 134 |
|
| 135 |
fun mapConsToClause {prefix, pat, exp} cons = |
| 136 |
A.CLAUSE([pat(mapConsToPat |
| 137 |
{prefix=prefix, id=fn {newName,...} => A.IDpat newName} |
| 138 |
cons)], |
| 139 |
NONE, |
| 140 |
exp) |
| 141 |
|
| 142 |
fun consBindings cons = |
| 143 |
let fun enter({newName,origName,ty},bindings) = (newName, ty)::bindings |
| 144 |
val bindings = foldCons enter [] cons |
| 145 |
fun lookup(id : Ast.id) = |
| 146 |
let fun find((b as (x,t))::bs) = if x = id then (ID x,t) else find bs |
| 147 |
| find [] = raise NoName |
| 148 |
in find bindings end |
| 149 |
in lookup |
| 150 |
end |
| 151 |
|
| 152 |
(* Simplification *) |
| 153 |
local |
| 154 |
val NIL = R.noRewrite |
| 155 |
|
| 156 |
fun hasBindings ps = |
| 157 |
let val bindings = ref false |
| 158 |
fun pat _ (p as A.IDpat x) = (bindings := true; p) |
| 159 |
| pat _ p = p |
| 160 |
in app (fn p => |
| 161 |
(#pat(R.rewrite{pat=pat,decl=NIL,sexp=NIL,exp=NIL,ty=NIL}) p; |
| 162 |
())) ps; |
| 163 |
!bindings |
| 164 |
end |
| 165 |
|
| 166 |
fun allTheSame [] = true |
| 167 |
| allTheSame (x::xs) = List.all (fn x' => x = x') xs |
| 168 |
|
| 169 |
exception Don'tApply |
| 170 |
|
| 171 |
fun reduceExp ==> (exp as A.CASEexp(e,[])) = exp |
| 172 |
| reduceExp ==> (A.SEQexp es) = |
| 173 |
(A.SEQexp(foldr (fn (A.TUPLEexp [],es) => es |
| 174 |
| (A.SEQexp [],es) => es |
| 175 |
| (e,es) => e::es |
| 176 |
) [] es)) |
| 177 |
| reduceExp ==> |
| 178 |
(exp as A.CASEexp(e,allCs as (c as A.CLAUSE(p1,NONE,e1))::cs)) = |
| 179 |
let fun collect(A.CLAUSE([p],NONE,e),Ps) = |
| 180 |
let fun ins [] = [([p],e)] |
| 181 |
| ins((ps,e')::Ps) = |
| 182 |
if e = e' then (p::ps,e)::Ps |
| 183 |
else (ps,e')::ins Ps |
| 184 |
in ins Ps end |
| 185 |
val Ps = foldr collect [] (c::cs) |
| 186 |
fun orPat [p] = p |
| 187 |
| orPat ps = |
| 188 |
if List.all (fn A.WILDpat => true | _ => false) ps then |
| 189 |
A.WILDpat |
| 190 |
else A.ORpat ps |
| 191 |
fun tuplepat [p] = p |
| 192 |
| tuplepat ps = A.TUPLEpat ps |
| 193 |
fun join([p],e) = A.CLAUSE([p],NONE,e) |
| 194 |
| join(ps,e) = |
| 195 |
let val xs = map (fn A.TUPLEpat(p::ps) => (p,ps) |
| 196 |
| _ => raise Don'tApply) ps |
| 197 |
val firstPats = map #1 xs |
| 198 |
val restPats = map #2 xs |
| 199 |
in if allTheSame (map tuplepat restPats) then |
| 200 |
A.CLAUSE([tuplepat(orPat firstPats::hd restPats)], |
| 201 |
NONE,e) |
| 202 |
else raise Don'tApply |
| 203 |
end handle Dont'Apply => A.CLAUSE([orPat ps],NONE,e) |
| 204 |
val cs = map join (rev Ps) |
| 205 |
in case cs of |
| 206 |
[A.CLAUSE([A.TUPLEpat []],NONE,body)] => body |
| 207 |
| [A.CLAUSE([_],NONE,body as A.LISTexp([],NONE))] => body |
| 208 |
| [A.CLAUSE([A.TUPLEpat(ps)],NONE,body)] => |
| 209 |
if hasBindings ps then |
| 210 |
let fun elimOr(pat as A.ORpat p) = |
| 211 |
if hasBindings p then pat else A.WILDpat |
| 212 |
| elimOr pat = pat |
| 213 |
in A.CASEexp(e, |
| 214 |
[A.CLAUSE([A.TUPLEpat(map elimOr ps)],NONE,body)]) |
| 215 |
end |
| 216 |
else body |
| 217 |
| [A.CLAUSE(ps,NONE,body)] => |
| 218 |
if hasBindings ps then A.CASEexp(e,cs) else body |
| 219 |
| _ => A.CASEexp(e,cs) |
| 220 |
end |
| 221 |
| reduceExp ==> (exp as A.IFexp(a,b,c)) = if b = c then b else exp |
| 222 |
| reduceExp ==> e = e |
| 223 |
|
| 224 |
val simplifier = |
| 225 |
R.rewrite{pat=NIL,decl=NIL,exp=reduceExp,sexp=NIL,ty=NIL} |
| 226 |
in |
| 227 |
val simplifyExp = #exp simplifier |
| 228 |
val simplifyDecl = #decl simplifier |
| 229 |
val simplifyPat = #pat simplifier |
| 230 |
val simplifySexp = #sexp simplifier |
| 231 |
val simplifyTy = #ty simplifier |
| 232 |
|
| 233 |
fun stripMarks d = |
| 234 |
let fun decl ==> (A.MARKdecl(_,d)) = d |
| 235 |
| decl ==> d = d |
| 236 |
in #decl (R.rewrite{pat=NIL,decl=decl,sexp=NIL,exp=NIL,ty=NIL}) d end |
| 237 |
end |
| 238 |
|
| 239 |
end |