| 1 |
(* |
| 2 |
* Machine description environment. |
| 3 |
*) |
| 4 |
functor MDLEnv(TypeUtils : MDL_TYPE_UTILS) : MDL_ENV = |
| 5 |
struct |
| 6 |
|
| 7 |
structure Ast = TypeUtils.Ast |
| 8 |
structure AstPP = TypeUtils.AstPP |
| 9 |
structure Error = MDLError |
| 10 |
|
| 11 |
datatype env = |
| 12 |
ENV of |
| 13 |
{ TE : Ast.ty Env.env, (* type environment *) |
| 14 |
VE : (Ast.exp * Ast.ty) Env.env, (* value environment *) |
| 15 |
EE : (Ast.decl list * env) Env.env, (* structure environment *) |
| 16 |
DE : Ast.decl list, (* declarations environment *) |
| 17 |
SE : Ast.decl list (* signature environment *) |
| 18 |
} |
| 19 |
|
| 20 |
open Ast |
| 21 |
|
| 22 |
infix ++ |
| 23 |
infix $$ |
| 24 |
infix ==> |
| 25 |
|
| 26 |
val op $$ = Env.union |
| 27 |
val op ==> = Env.bind |
| 28 |
val O = Env.empty |
| 29 |
|
| 30 |
val empty = ENV{TE=O, VE=O, EE=O, DE=[], SE=[]} |
| 31 |
fun (ENV{TE=TE1, VE=VE1, EE=EE1, DE=DE1, SE=SE1}) ++ |
| 32 |
(ENV{TE=TE2, VE=VE2, EE=EE2, DE=DE2, SE=SE2}) = |
| 33 |
ENV{TE=TE1 $$ TE2, VE=VE1 $$ VE2, EE=EE1 $$ EE2, DE=DE1@DE2, SE=SE1@SE2} |
| 34 |
|
| 35 |
fun mkDECL d = ENV{TE=O, VE=O, EE=O, DE=[d], SE=[]} |
| 36 |
fun mkSIG d = ENV{TE=O, VE=O, EE=O, DE=[], SE=[d]} |
| 37 |
fun mkVALs vbs = ENV{TE=O, VE=vbs, EE=O, DE=[], SE=[]} |
| 38 |
|
| 39 |
fun VALbind(id,e,t)= mkVALs(id ==> (e,t)) |
| 40 |
fun TYPEbind(id,t) = ENV{TE=id ==> t, VE=O, EE=O, DE=[], SE=[]} |
| 41 |
fun STRbind(id,args,E) = ENV{TE=O, VE=O, EE= id ==> (args,E), DE=[], SE=[]} |
| 42 |
|
| 43 |
(* Create a new free variable; instantiation and generalization *) |
| 44 |
fun var(ENV _) = TypeUtils.newVar 0 (* XXX *) |
| 45 |
fun inst(ENV _) t = TypeUtils.inst 0 t (* XXX *) |
| 46 |
fun gen(ENV _) t = TypeUtils.gen 0 t (* XXX *) |
| 47 |
fun lambda(ENV _) t = TypeUtils.lambda 0 t (* XXX *) |
| 48 |
|
| 49 |
(* Extract components *) |
| 50 |
fun DE(ENV{DE, ...}) = DE |
| 51 |
fun SE(ENV{SE, ...}) = SE |
| 52 |
fun datatypeDefinitions(ENV{DE,...}) = |
| 53 |
let fun collect(DATATYPEdecl(dbs, _), dbs') = dbs @ dbs' |
| 54 |
| collect(MARKdecl(_, d), dbs') = collect(d, dbs') |
| 55 |
| collect(_, dbs') = dbs' |
| 56 |
in List.foldr collect [] DE |
| 57 |
end |
| 58 |
|
| 59 |
(* Lookup components from the environment *) |
| 60 |
fun lookupTy (E as ENV{TE,EE,...}) (IDENT([],id)) = |
| 61 |
(Env.look TE id |
| 62 |
handle _ => (Error.error("undefined type '"^id^"'"); var E)) |
| 63 |
| lookupTy (ENV{EE,...}) (IDENT(s::ss,id)) = |
| 64 |
lookupTy (lookupStr' EE (IDENT(ss,s))) (IDENT([],id)) |
| 65 |
|
| 66 |
and lookupVal' err (E as ENV{VE,EE,...}) (IDENT([],id)) = |
| 67 |
(inst E (Env.look VE id) |
| 68 |
handle _ => (err id; (LITexp(INTlit 0), var E))) |
| 69 |
| lookupVal' err (ENV{EE,...}) (IDENT(s::ss,id)) = |
| 70 |
lookupVal' err (lookupStr' EE (IDENT(ss,s))) (IDENT([],id)) |
| 71 |
|
| 72 |
and lookupVal E x = lookupVal' |
| 73 |
(fn x => Error.error("undefined value '"^x^"'")) E x |
| 74 |
|
| 75 |
and lookupStr (ENV{EE,...}) id = lookupStr' EE id |
| 76 |
|
| 77 |
and lookupStr' EE (IDENT([],id)) = |
| 78 |
(#2(Env.look EE id) |
| 79 |
handle _ => |
| 80 |
(Error.error("undefined structure '"^id^"'"); empty)) |
| 81 |
| lookupStr' EE (IDENT(s::ss,id)) = |
| 82 |
lookupStr (lookupStr' EE (IDENT(ss,s))) (IDENT([],id)) |
| 83 |
|
| 84 |
(* Interators *) |
| 85 |
fun foldVal f x (ENV{VE, ...}) = |
| 86 |
Env.fold (fn (id,(e,ty),l) => f(id,e,ty,l)) x VE |
| 87 |
|
| 88 |
(* |
| 89 |
* Elaborate a declaration in an environment. |
| 90 |
* Return an environment. |
| 91 |
*) |
| 92 |
fun elab E d = |
| 93 |
let (* elaborate a declaration *) |
| 94 |
val mkDECL = fn(l,d) => mkDECL(MARKdecl(l,d)) |
| 95 |
val mkSIG = fn(l,d) => mkSIG(MARKdecl(l,d)) |
| 96 |
fun D E l (d as DATATYPEdecl(dbs,tys)) = mkDECL(l,d) ++ mkSIG(l,d) |
| 97 |
| D E l (d as INSTRUCTIONdecl cbs) = mkDECL(l,d) |
| 98 |
| D E l (d as FUNdecl _) = mkDECL(l,d) |
| 99 |
| D E l (d as RTLdecl _) = mkDECL(l,d) |
| 100 |
| D E l (d as RTLSIGdecl _) = mkDECL(l,d) |
| 101 |
| D E l (d as VALdecl _) = mkDECL(l,d) |
| 102 |
| D E l (d as TYPESIGdecl _) = mkSIG(l,d) |
| 103 |
| D E l (d as VALSIGdecl _) = mkSIG(l,d) |
| 104 |
| D E l (d as LOCALdecl(d1,d2)) = mkDECL(l,d) |
| 105 |
(* let val E' = Ds E l d1 in Ds (E ++ E') l d2 end *) |
| 106 |
| D E l (d as SEQdecl ds) = Ds E l ds |
| 107 |
| D E l (d as OPENdecl ids) = mkDECL(l,d) ++ openStrs E ids |
| 108 |
| D E l (d as STRUCTUREdecl(id,args,_,DECLsexp ds)) = |
| 109 |
let val E' = Ds E l ds |
| 110 |
in STRbind(id,args,E') ++ mkDECL(l,d) end |
| 111 |
| D E l (STRUCTURESIGdecl _) = empty |
| 112 |
| D E l (d as INFIXdecl _) = mkDECL(l,d) |
| 113 |
| D E l (d as INFIXRdecl _) = mkDECL(l,d) |
| 114 |
| D E l (d as NONFIXdecl _) = mkDECL(l,d) |
| 115 |
| D E _ (MARKdecl(l,d)) = (Error.setLoc l; D E l d) |
| 116 |
| D E l d = Error.fail("illegal declaration: "^ |
| 117 |
(PP.text(AstPP.decl d))) |
| 118 |
|
| 119 |
and Ds E l [] = empty |
| 120 |
| Ds E l (d::ds) = let val E' = D E l d |
| 121 |
in E' ++ Ds (E ++ E') l ds end |
| 122 |
|
| 123 |
(* open up a list of structures *) |
| 124 |
and openStrs E ids = |
| 125 |
List.foldr (fn (id,E') => lookupStr E id ++ E') empty ids |
| 126 |
|
| 127 |
in D E SourceMapping.dummyLoc d |
| 128 |
end |
| 129 |
|
| 130 |
(* |
| 131 |
* Treat a type expression as a pattern and |
| 132 |
* compute its set of bindings. Duplicated names are assigned |
| 133 |
* unique suffixes. |
| 134 |
*) |
| 135 |
fun bindingsInType ty = |
| 136 |
let val names = Env.envir "names" |
| 137 |
fun count id = let val (n,total) = Env.lookup names id |
| 138 |
in total := !total + 1 end |
| 139 |
handle _ => Env.update names (id,(ref 0,ref 1)) |
| 140 |
fun getName id = let val (n,total) = Env.lookup names id |
| 141 |
in if !total = 1 then id else |
| 142 |
(n := !n + 1; id^Int.toString(!n)) |
| 143 |
end |
| 144 |
fun f(IDty(IDENT(_,id))) = count id |
| 145 |
| f(APPty(_,[ty])) = f ty |
| 146 |
| f(CELLty id) = count id |
| 147 |
| f(TUPLEty tys) = app f tys |
| 148 |
| f(RECORDty ltys) = app (fn (id,_) => count id) ltys |
| 149 |
| f _ = () |
| 150 |
in f ty; (!names,getName) end |
| 151 |
|
| 152 |
(* Lookup from nested environment *) |
| 153 |
fun declOf(ENV{EE, ...}) id = |
| 154 |
let val (_, ENV{DE,...}) = Env.look EE id |
| 155 |
in SEQdecl DE |
| 156 |
end handle _ => $ [] |
| 157 |
|
| 158 |
fun fctArgOf(ENV{EE, ...}) id = |
| 159 |
let val (args, _) = Env.look EE id |
| 160 |
in SEQdecl args |
| 161 |
end handle _ => $ [] |
| 162 |
|
| 163 |
fun typeOf(ENV{EE, ...}) id = |
| 164 |
let val (_, ENV{SE,...}) = Env.look EE id |
| 165 |
in SEQdecl SE |
| 166 |
end handle _ => $ [] |
| 167 |
|
| 168 |
end |