author | paulson |
Tue, 20 Aug 1996 18:53:17 +0200 | |
changeset 1930 | cdf9bcd53749 |
parent 1500 | b2de3b3277b8 |
child 2142 | 20f208ff085d |
permissions | -rw-r--r-- |
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
1 |
(* Title: Pure/envir.ML |
0 | 2 |
ID: $Id$ |
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
0 | 4 |
Copyright 1988 University of Cambridge |
5 |
*) |
|
6 |
||
7 |
(*Environments |
|
8 |
Should take account that typ is part of variable name, |
|
9 |
otherwise two variables with same name and different types |
|
10 |
will cause errors! |
|
11 |
*) |
|
12 |
||
13 |
||
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
14 |
signature ENVIR = |
0 | 15 |
sig |
16 |
type 'a xolist |
|
17 |
exception ENVIR of string * indexname; |
|
18 |
datatype env = Envir of {asol: term xolist, |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
19 |
iTs: (indexname * typ) list, |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
20 |
maxidx: int} |
1460 | 21 |
val alist_of : env -> (indexname * term) list |
22 |
val alist_of_olist : 'a xolist -> (indexname * 'a) list |
|
23 |
val empty : int->env |
|
24 |
val is_empty : env -> bool |
|
25 |
val minidx : env -> int option |
|
26 |
val genvar : string -> env * typ -> env * term |
|
27 |
val genvars : string -> env * typ list -> env * term list |
|
28 |
val lookup : env * indexname -> term option |
|
29 |
val norm_term : env -> term -> term |
|
30 |
val null_olist : 'a xolist |
|
31 |
val olist_of_alist : (indexname * 'a) list -> 'a xolist |
|
32 |
val update : (indexname * term) * env -> env |
|
33 |
val vupdate : (indexname * term) * env -> env |
|
0 | 34 |
end; |
35 |
||
1500 | 36 |
structure Envir : ENVIR = |
0 | 37 |
struct |
38 |
||
39 |
(*association lists with keys in ascending order, no repeated keys*) |
|
40 |
datatype 'a xolist = Olist of (indexname * 'a) list; |
|
41 |
||
42 |
||
43 |
exception ENVIR of string * indexname; |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
44 |
|
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
45 |
(*look up key in ordered list*) |
0 | 46 |
fun xsearch (Olist[], key) = None |
47 |
| xsearch (Olist ((keyi,xi)::pairs), key) = |
|
48 |
if xless(keyi,key) then xsearch (Olist pairs, key) |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
49 |
else if key=keyi then Some xi |
0 | 50 |
else None; |
51 |
||
52 |
(*insert pair in ordered list, reject if key already present*) |
|
53 |
fun xinsert_new (newpr as (key, x), Olist al) = |
|
54 |
let fun insert [] = [newpr] |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
55 |
| insert ((pair as (keyi,xi)) :: pairs) = |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
56 |
if xless(keyi,key) then pair :: insert pairs |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
57 |
else if key=keyi then |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
58 |
raise ENVIR("xinsert_new: already present",key) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
59 |
else newpr::pair::pairs |
0 | 60 |
in Olist (insert al) end; |
61 |
||
62 |
(*insert pair in ordered list, overwrite if key already present*) |
|
63 |
fun xinsert (newpr as (key, x), Olist al) = |
|
64 |
let fun insert [] = [newpr] |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
65 |
| insert ((pair as (keyi,xi)) :: pairs) = |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
66 |
if xless(keyi,key) then pair :: insert pairs |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
67 |
else if key=keyi then newpr::pairs |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
68 |
else newpr::pair::pairs |
0 | 69 |
in Olist (insert al) end; |
70 |
||
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
71 |
(*apply function to the contents part of each pair*) |
0 | 72 |
fun xmap f (Olist pairs) = |
73 |
let fun xmp [] = [] |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
74 |
| xmp ((key,x)::pairs) = (key, f x) :: xmp pairs |
0 | 75 |
in Olist (xmp pairs) end; |
76 |
||
77 |
(*allows redefinition of a key by "join"ing the contents parts*) |
|
78 |
fun xmerge_olists join (Olist pairsa, Olist pairsb) = |
|
79 |
let fun xmrg ([],pairsb) = pairsb |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
80 |
| xmrg (pairsa,[]) = pairsa |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
81 |
| xmrg ((keya,x)::pairsa, (keyb,y)::pairsb) = |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
82 |
if xless(keya,keyb) then |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
83 |
(keya,x) :: xmrg (pairsa, (keyb,y)::pairsb) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
84 |
else if xless(keyb,keya) then |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
85 |
(keyb,y) :: xmrg ((keya,x)::pairsa, pairsb) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
86 |
else (*equal*) (keya, join(x,y)) :: xmrg (pairsa, pairsb) |
0 | 87 |
in Olist (xmrg (pairsa,pairsb)) end; |
88 |
||
89 |
val null_olist = Olist[]; |
|
90 |
||
91 |
fun alist_of_olist (Olist pairs) = pairs; |
|
92 |
||
93 |
fun olist_of_alist pairs = foldr xinsert (pairs, Olist[]); |
|
94 |
||
95 |
||
96 |
||
97 |
(*updating can destroy environment in 2 ways!! |
|
98 |
(1) variables out of range (2) circular assignments |
|
99 |
*) |
|
100 |
datatype env = Envir of |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
101 |
{maxidx: int, (*maximum index of vars*) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
102 |
asol: term xolist, (*table of assignments to Vars*) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
103 |
iTs: (indexname*typ)list} (*table of assignments to TVars*) |
0 | 104 |
|
105 |
||
106 |
(*Generate a list of distinct variables. |
|
107 |
Increments index to make them distinct from ALL present variables. *) |
|
108 |
fun genvars name (Envir{maxidx, asol, iTs}, Ts) : env * term list = |
|
109 |
let fun genvs (_, [] : typ list) : term list = [] |
|
110 |
| genvs (n, [T]) = [ Var((name, maxidx+1), T) ] |
|
111 |
| genvs (n, T::Ts) = |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
112 |
Var((name ^ radixstring(26,"a",n), maxidx+1), T) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
113 |
:: genvs(n+1,Ts) |
0 | 114 |
in (Envir{maxidx=maxidx+1, asol=asol, iTs=iTs}, genvs (0,Ts)) end; |
115 |
||
116 |
(*Generate a variable.*) |
|
117 |
fun genvar name (env,T) : env * term = |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
118 |
let val (env',[v]) = genvars name (env,[T]) |
0 | 119 |
in (env',v) end; |
120 |
||
121 |
fun lookup (Envir{asol,...}, xname) : term option = xsearch (asol,xname); |
|
122 |
||
123 |
fun update ((xname, t), Envir{maxidx, asol, iTs}) = |
|
124 |
Envir{maxidx=maxidx, asol=xinsert_new ((xname,t), asol), iTs=iTs}; |
|
125 |
||
126 |
(*The empty environment. New variables will start with the given index.*) |
|
127 |
fun empty m = Envir{maxidx=m, asol=null_olist, iTs=[]}; |
|
128 |
||
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
129 |
(*is_empty*) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
130 |
fun is_empty (Envir {asol = Olist [], iTs = [], ...}) = true |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
131 |
| is_empty _ = false; |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
132 |
|
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
133 |
(*minidx*) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
134 |
fun minidx (Envir {asol = Olist asns, iTs, ...}) = |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
135 |
(case (asns, iTs) of |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
136 |
([], []) => None |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
137 |
| (((_, i), _) :: _, _) => Some (min (i :: map (snd o fst) iTs)) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
138 |
| _ => Some (min (map (snd o fst) iTs))); |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
139 |
|
0 | 140 |
(*Update, checking Var-Var assignments: try to suppress higher indexes*) |
141 |
fun vupdate((a,t), env) = case t of |
|
142 |
Var(name',T) => |
|
247
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
143 |
if a=name' then env (*cycle!*) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
144 |
else if xless(a, name') then |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
145 |
(case lookup(env,name') of (*if already assigned, chase*) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
146 |
None => update((name', Var(a,T)), env) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
147 |
| Some u => vupdate((a,u), env)) |
bc10568855ee
added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents:
0
diff
changeset
|
148 |
else update((a,t), env) |
0 | 149 |
| _ => update((a,t), env); |
150 |
||
151 |
||
152 |
(*Convert environment to alist*) |
|
153 |
fun alist_of (Envir{maxidx,asol,...}) = alist_of_olist asol; |
|
154 |
||
155 |
||
1500 | 156 |
(*** Beta normal form for terms (not eta normal form). |
157 |
Chases variables in env; Does not exploit sharing of variable bindings |
|
158 |
Does not check types, so could loop. ***) |
|
159 |
||
160 |
(*raised when norm has no effect on a term, to do sharing instead of copying*) |
|
161 |
exception SAME; |
|
0 | 162 |
|
1500 | 163 |
fun norm_term1 (asol,t) : term = |
164 |
let fun norm (Var (w,T)) = |
|
165 |
(case xsearch(asol,w) of |
|
166 |
Some u => normh u |
|
167 |
| None => raise SAME) |
|
168 |
| norm (Abs(a,T,body)) = Abs(a, T, norm body) |
|
169 |
| norm (Abs(_,_,body) $ t) = normh(subst_bounds([t], body)) |
|
170 |
| norm (f $ t) = |
|
171 |
((case norm f of |
|
172 |
Abs(_,_,body) => normh(subst_bounds([t], body)) |
|
173 |
| nf => nf $ normh t) |
|
174 |
handle SAME => f $ norm t) |
|
175 |
| norm _ = raise SAME |
|
176 |
and normh t = (norm t) handle SAME => t |
|
177 |
in normh t end |
|
0 | 178 |
|
1500 | 179 |
and norm_term2(asol,iTs,t) : term = |
180 |
let fun normT(Type(a,Ts)) = Type(a, normTs Ts) |
|
181 |
| normT(TFree _) = raise SAME |
|
182 |
| normT(TVar(v,_)) = (case assoc(iTs,v) of |
|
183 |
Some(U) => normTh U |
|
184 |
| None => raise SAME) |
|
185 |
and normTh T = ((normT T) handle SAME => T) |
|
186 |
and normTs([]) = raise SAME |
|
187 |
| normTs(T::Ts) = ((normT T :: normTsh Ts) |
|
188 |
handle SAME => T :: normTs Ts) |
|
189 |
and normTsh Ts = ((normTs Ts) handle SAME => Ts) |
|
190 |
and norm(Const(a,T)) = Const(a, normT T) |
|
191 |
| norm(Free(a,T)) = Free(a, normT T) |
|
192 |
| norm(Var (w,T)) = |
|
193 |
(case xsearch(asol,w) of |
|
194 |
Some u => normh u |
|
195 |
| None => Var(w,normT T)) |
|
196 |
| norm(Abs(a,T,body)) = |
|
197 |
(Abs(a,normT T,normh body) handle SAME => Abs(a, T, norm body)) |
|
198 |
| norm(Abs(_,_,body) $ t) = normh(subst_bounds([t], body)) |
|
199 |
| norm(f $ t) = |
|
200 |
((case norm f of |
|
201 |
Abs(_,_,body) => normh(subst_bounds([t], body)) |
|
202 |
| nf => nf $ normh t) |
|
203 |
handle SAME => f $ norm t) |
|
204 |
| norm _ = raise SAME |
|
205 |
and normh t = (norm t) handle SAME => t |
|
206 |
in normh t end; |
|
0 | 207 |
|
208 |
(*curried version might be slower in recursive calls*) |
|
209 |
fun norm_term (env as Envir{asol,iTs,...}) t : term = |
|
719
e3e1d1a6d408
Pure/envir/norm_term: replaced equality test for [] by null
lcp
parents:
247
diff
changeset
|
210 |
if null iTs then norm_term1(asol, t) else norm_term2(asol,iTs, t) |
e3e1d1a6d408
Pure/envir/norm_term: replaced equality test for [] by null
lcp
parents:
247
diff
changeset
|
211 |
|
0 | 212 |
end; |
213 |