author | paulson |
Tue, 12 Sep 2000 10:27:16 +0200 | |
changeset 9930 | c02d48a47ed1 |
parent 9721 | 7e51c9f3d5a0 |
child 10552 | a177b8571026 |
permissions | -rw-r--r-- |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1 |
(* Title: Pure/term.ML |
0 | 2 |
ID: $Id$ |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
0 | 4 |
Copyright Cambridge University 1992 |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
5 |
|
4444 | 6 |
Simply typed lambda-calculus: types, terms, and basic operations. |
0 | 7 |
*) |
8 |
||
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
9 |
infix 9 $; |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
10 |
infixr 5 -->; |
4444 | 11 |
infixr --->; |
12 |
infix aconv; |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
13 |
|
4444 | 14 |
signature BASIC_TERM = |
15 |
sig |
|
16 |
type indexname |
|
17 |
type class |
|
18 |
type sort |
|
19 |
datatype typ = |
|
20 |
Type of string * typ list | |
|
21 |
TFree of string * sort | |
|
22 |
TVar of indexname * sort |
|
23 |
val --> : typ * typ -> typ |
|
24 |
val ---> : typ list * typ -> typ |
|
25 |
val is_TVar: typ -> bool |
|
26 |
val domain_type: typ -> typ |
|
4480 | 27 |
val range_type: typ -> typ |
4444 | 28 |
val binder_types: typ -> typ list |
29 |
val body_type: typ -> typ |
|
30 |
val strip_type: typ -> typ list * typ |
|
31 |
datatype term = |
|
32 |
Const of string * typ | |
|
33 |
Free of string * typ | |
|
34 |
Var of indexname * typ | |
|
35 |
Bound of int | |
|
36 |
Abs of string * typ * term | |
|
4493 | 37 |
$ of term * term |
8408
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
38 |
structure Vartab : TABLE |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
39 |
structure Termtab : TABLE |
4444 | 40 |
exception TYPE of string * typ list * term list |
41 |
exception TERM of string * term list |
|
7318 | 42 |
val is_Bound: term -> bool |
4444 | 43 |
val is_Const: term -> bool |
44 |
val is_Free: term -> bool |
|
45 |
val is_Var: term -> bool |
|
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
46 |
val dest_Type: typ -> string * typ list |
4444 | 47 |
val dest_Const: term -> string * typ |
48 |
val dest_Free: term -> string * typ |
|
49 |
val dest_Var: term -> indexname * typ |
|
50 |
val type_of: term -> typ |
|
51 |
val type_of1: typ list * term -> typ |
|
52 |
val fastype_of: term -> typ |
|
53 |
val fastype_of1: typ list * term -> typ |
|
54 |
val strip_abs_body: term -> term |
|
55 |
val strip_abs_vars: term -> (string * typ) list |
|
56 |
val strip_qnt_body: string -> term -> term |
|
57 |
val strip_qnt_vars: string -> term -> (string * typ) list |
|
58 |
val list_comb: term * term list -> term |
|
59 |
val strip_comb: term -> term * term list |
|
60 |
val head_of: term -> term |
|
61 |
val size_of_term: term -> int |
|
62 |
val map_type_tvar: (indexname * sort -> typ) -> typ -> typ |
|
63 |
val map_type_tfree: (string * sort -> typ) -> typ -> typ |
|
64 |
val map_term_types: (typ -> typ) -> term -> term |
|
65 |
val it_term_types: (typ * 'a -> 'a) -> term * 'a -> 'a |
|
66 |
val map_typ: (class -> class) -> (string -> string) -> typ -> typ |
|
67 |
val map_term: |
|
68 |
(class -> class) -> |
|
69 |
(string -> string) -> (string -> string) -> term -> term |
|
70 |
val foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a |
|
8609 | 71 |
val foldl_term_types: (term -> 'a * typ -> 'a) -> 'a * term -> 'a |
4444 | 72 |
val foldl_types: ('a * typ -> 'a) -> 'a * term -> 'a |
73 |
val foldl_aterms: ('a * term -> 'a) -> 'a * term -> 'a |
|
6548
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
74 |
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term |
4444 | 75 |
val dummyT: typ |
76 |
val logicC: class |
|
77 |
val logicS: sort |
|
78 |
val itselfT: typ -> typ |
|
79 |
val a_itselfT: typ |
|
80 |
val propT: typ |
|
81 |
val implies: term |
|
82 |
val all: typ -> term |
|
83 |
val equals: typ -> term |
|
84 |
val flexpair: typ -> term |
|
85 |
val strip_all_body: term -> term |
|
86 |
val strip_all_vars: term -> (string * typ) list |
|
87 |
val incr_bv: int * int * term -> term |
|
88 |
val incr_boundvars: int -> term -> term |
|
89 |
val add_loose_bnos: term * int * int list -> int list |
|
90 |
val loose_bnos: term -> int list |
|
91 |
val loose_bvar: term * int -> bool |
|
92 |
val loose_bvar1: term * int -> bool |
|
93 |
val subst_bounds: term list * term -> term |
|
94 |
val subst_bound: term * term -> term |
|
95 |
val subst_TVars: (indexname * typ) list -> term -> term |
|
8408
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
96 |
val subst_TVars_Vartab: typ Vartab.table -> term -> term |
4444 | 97 |
val betapply: term * term -> term |
98 |
val eq_ix: indexname * indexname -> bool |
|
99 |
val ins_ix: indexname * indexname list -> indexname list |
|
100 |
val mem_ix: indexname * indexname list -> bool |
|
101 |
val eq_sort: sort * class list -> bool |
|
102 |
val mem_sort: sort * class list list -> bool |
|
103 |
val subset_sort: sort list * class list list -> bool |
|
104 |
val eq_set_sort: sort list * sort list -> bool |
|
105 |
val ins_sort: sort * class list list -> class list list |
|
106 |
val union_sort: sort list * sort list -> sort list |
|
7638 | 107 |
val rems_sort: sort list * sort list -> sort list |
4444 | 108 |
val aconv: term * term -> bool |
109 |
val aconvs: term list * term list -> bool |
|
110 |
val mem_term: term * term list -> bool |
|
111 |
val subset_term: term list * term list -> bool |
|
112 |
val eq_set_term: term list * term list -> bool |
|
113 |
val ins_term: term * term list -> term list |
|
114 |
val union_term: term list * term list -> term list |
|
5585 | 115 |
val inter_term: term list * term list -> term list |
4444 | 116 |
val could_unify: term * term -> bool |
117 |
val subst_free: (term * term) list -> term -> term |
|
118 |
val subst_atomic: (term * term) list -> term -> term |
|
119 |
val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term |
|
120 |
val typ_subst_TVars: (indexname * typ) list -> typ -> typ |
|
8408
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
121 |
val typ_subst_TVars_Vartab : typ Vartab.table -> typ -> typ |
4444 | 122 |
val subst_Vars: (indexname * term) list -> term -> term |
123 |
val incr_tvar: int -> typ -> typ |
|
124 |
val xless: (string * int) * indexname -> bool |
|
125 |
val atless: term * term -> bool |
|
126 |
val insert_aterm: term * term list -> term list |
|
127 |
val abstract_over: term * term -> term |
|
128 |
val absfree: string * typ * term -> term |
|
129 |
val list_abs_free: (string * typ) list * term -> term |
|
130 |
val list_all_free: (string * typ) list * term -> term |
|
131 |
val list_all: (string * typ) list * term -> term |
|
132 |
val maxidx_of_typ: typ -> int |
|
133 |
val maxidx_of_typs: typ list -> int |
|
134 |
val maxidx_of_term: term -> int |
|
4694 | 135 |
val read_radixint: int * string list -> int * string list |
136 |
val read_int: string list -> int * string list |
|
5986 | 137 |
val oct_char: string -> string |
4444 | 138 |
val variant: string list -> string -> string |
139 |
val variantlist: string list * string list -> string list |
|
140 |
val variant_abs: string * typ * term -> string * term |
|
141 |
val rename_wrt_term: term -> (string * typ) list -> (string * typ) list |
|
142 |
val add_new_id: string list * string -> string list |
|
143 |
val add_typ_classes: typ * class list -> class list |
|
144 |
val add_typ_ixns: indexname list * typ -> indexname list |
|
145 |
val add_typ_tfree_names: typ * string list -> string list |
|
146 |
val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list |
|
147 |
val typ_tfrees: typ -> (string * sort) list |
|
148 |
val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list |
|
149 |
val typ_tvars: typ -> (indexname * sort) list |
|
150 |
val add_typ_tycons: typ * string list -> string list |
|
151 |
val add_typ_varnames: typ * string list -> string list |
|
152 |
val add_term_classes: term * class list -> class list |
|
153 |
val add_term_consts: term * string list -> string list |
|
154 |
val add_term_frees: term * term list -> term list |
|
155 |
val term_frees: term -> term list |
|
156 |
val add_term_names: term * string list -> string list |
|
157 |
val add_term_tfree_names: term * string list -> string list |
|
158 |
val add_term_tfrees: term * (string * sort) list -> (string * sort) list |
|
159 |
val term_tfrees: term -> (string * sort) list |
|
160 |
val add_term_tvar_ixns: term * indexname list -> indexname list |
|
161 |
val add_term_tvarnames: term * string list -> string list |
|
162 |
val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list |
|
163 |
val term_tvars: term -> (indexname * sort) list |
|
164 |
val add_term_tycons: term * string list -> string list |
|
165 |
val add_term_vars: term * term list -> term list |
|
166 |
val term_vars: term -> term list |
|
167 |
val exists_Const: (string * typ -> bool) -> term -> bool |
|
4631 | 168 |
val exists_subterm: (term -> bool) -> term -> bool |
4444 | 169 |
val compress_type: typ -> typ |
170 |
val compress_term: term -> term |
|
171 |
end; |
|
0 | 172 |
|
4444 | 173 |
signature TERM = |
174 |
sig |
|
175 |
include BASIC_TERM |
|
176 |
val indexname_ord: indexname * indexname -> order |
|
177 |
val typ_ord: typ * typ -> order |
|
178 |
val typs_ord: typ list * typ list -> order |
|
179 |
val term_ord: term * term -> order |
|
180 |
val terms_ord: term list * term list -> order |
|
181 |
val termless: term * term -> bool |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
182 |
val dummy_patternN: string |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
183 |
val no_dummy_patterns: term -> term |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
184 |
val replace_dummy_patterns: int * term -> int * term |
4444 | 185 |
end; |
186 |
||
187 |
structure Term: TERM = |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
188 |
struct |
0 | 189 |
|
190 |
(*Indexnames can be quickly renamed by adding an offset to the integer part, |
|
191 |
for resolution.*) |
|
192 |
type indexname = string*int; |
|
193 |
||
4626 | 194 |
(* Types are classified by sorts. *) |
0 | 195 |
type class = string; |
196 |
type sort = class list; |
|
197 |
||
198 |
(* The sorts attached to TFrees and TVars specify the sort of that variable *) |
|
199 |
datatype typ = Type of string * typ list |
|
200 |
| TFree of string * sort |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
201 |
| TVar of indexname * sort; |
0 | 202 |
|
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
203 |
(*Terms. Bound variables are indicated by depth number. |
0 | 204 |
Free variables, (scheme) variables and constants have names. |
4626 | 205 |
An term is "closed" if every bound variable of level "lev" |
0 | 206 |
is enclosed by at least "lev" abstractions. |
207 |
||
208 |
It is possible to create meaningless terms containing loose bound vars |
|
209 |
or type mismatches. But such terms are not allowed in rules. *) |
|
210 |
||
211 |
||
212 |
||
213 |
datatype term = |
|
214 |
Const of string * typ |
|
215 |
| Free of string * typ |
|
216 |
| Var of indexname * typ |
|
217 |
| Bound of int |
|
218 |
| Abs of string*typ*term |
|
3965 | 219 |
| op $ of term*term; |
0 | 220 |
|
221 |
||
222 |
(*For errors involving type mismatches*) |
|
223 |
exception TYPE of string * typ list * term list; |
|
224 |
||
225 |
(*For system errors involving terms*) |
|
226 |
exception TERM of string * term list; |
|
227 |
||
228 |
||
229 |
(*Note variable naming conventions! |
|
230 |
a,b,c: string |
|
231 |
f,g,h: functions (including terms of function type) |
|
232 |
i,j,m,n: int |
|
233 |
t,u: term |
|
234 |
v,w: indexnames |
|
235 |
x,y: any |
|
236 |
A,B,C: term (denoting formulae) |
|
237 |
T,U: typ |
|
238 |
*) |
|
239 |
||
240 |
||
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
241 |
(** Types **) |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
242 |
|
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
243 |
fun S --> T = Type("fun",[S,T]); |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
244 |
|
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
245 |
(*handy for multiple args: [T1,...,Tn]--->T gives T1-->(T2--> ... -->T)*) |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
246 |
val op ---> = foldr (op -->); |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
247 |
|
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
248 |
fun dest_Type (Type x) = x |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
249 |
| dest_Type T = raise TYPE ("dest_Type", [T], []); |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
250 |
|
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
251 |
|
0 | 252 |
(** Discriminators **) |
253 |
||
7318 | 254 |
fun is_Bound (Bound _) = true |
255 |
| is_Bound _ = false; |
|
256 |
||
0 | 257 |
fun is_Const (Const _) = true |
258 |
| is_Const _ = false; |
|
259 |
||
260 |
fun is_Free (Free _) = true |
|
261 |
| is_Free _ = false; |
|
262 |
||
263 |
fun is_Var (Var _) = true |
|
264 |
| is_Var _ = false; |
|
265 |
||
266 |
fun is_TVar (TVar _) = true |
|
267 |
| is_TVar _ = false; |
|
268 |
||
269 |
(** Destructors **) |
|
270 |
||
271 |
fun dest_Const (Const x) = x |
|
272 |
| dest_Const t = raise TERM("dest_Const", [t]); |
|
273 |
||
274 |
fun dest_Free (Free x) = x |
|
275 |
| dest_Free t = raise TERM("dest_Free", [t]); |
|
276 |
||
277 |
fun dest_Var (Var x) = x |
|
278 |
| dest_Var t = raise TERM("dest_Var", [t]); |
|
279 |
||
280 |
||
4464 | 281 |
fun domain_type (Type("fun", [T,_])) = T |
282 |
and range_type (Type("fun", [_,T])) = T; |
|
4064 | 283 |
|
0 | 284 |
(* maps [T1,...,Tn]--->T to the list [T1,T2,...,Tn]*) |
285 |
fun binder_types (Type("fun",[S,T])) = S :: binder_types T |
|
286 |
| binder_types _ = []; |
|
287 |
||
288 |
(* maps [T1,...,Tn]--->T to T*) |
|
289 |
fun body_type (Type("fun",[S,T])) = body_type T |
|
290 |
| body_type T = T; |
|
291 |
||
292 |
(* maps [T1,...,Tn]--->T to ([T1,T2,...,Tn], T) *) |
|
293 |
fun strip_type T : typ list * typ = |
|
294 |
(binder_types T, body_type T); |
|
295 |
||
296 |
||
297 |
(*Compute the type of the term, checking that combinations are well-typed |
|
298 |
Ts = [T0,T1,...] holds types of bound variables 0, 1, ...*) |
|
299 |
fun type_of1 (Ts, Const (_,T)) = T |
|
300 |
| type_of1 (Ts, Free (_,T)) = T |
|
301 |
| type_of1 (Ts, Bound i) = (nth_elem (i,Ts) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
302 |
handle LIST _ => raise TYPE("type_of: bound variable", [], [Bound i])) |
0 | 303 |
| type_of1 (Ts, Var (_,T)) = T |
304 |
| type_of1 (Ts, Abs (_,T,body)) = T --> type_of1(T::Ts, body) |
|
305 |
| type_of1 (Ts, f$u) = |
|
306 |
let val U = type_of1(Ts,u) |
|
307 |
and T = type_of1(Ts,f) |
|
308 |
in case T of |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
309 |
Type("fun",[T1,T2]) => |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
310 |
if T1=U then T2 else raise TYPE |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
311 |
("type_of: type mismatch in application", [T1,U], [f$u]) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
312 |
| _ => raise TYPE |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
313 |
("type_of: function type is expected in application", |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
314 |
[T,U], [f$u]) |
0 | 315 |
end; |
316 |
||
317 |
fun type_of t : typ = type_of1 ([],t); |
|
318 |
||
319 |
(*Determines the type of a term, with minimal checking*) |
|
61 | 320 |
fun fastype_of1 (Ts, f$u) = |
321 |
(case fastype_of1 (Ts,f) of |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
322 |
Type("fun",[_,T]) => T |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
323 |
| _ => raise TERM("fastype_of: expected function type", [f$u])) |
61 | 324 |
| fastype_of1 (_, Const (_,T)) = T |
325 |
| fastype_of1 (_, Free (_,T)) = T |
|
326 |
| fastype_of1 (Ts, Bound i) = (nth_elem(i,Ts) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
327 |
handle LIST _ => raise TERM("fastype_of: Bound", [Bound i])) |
61 | 328 |
| fastype_of1 (_, Var (_,T)) = T |
329 |
| fastype_of1 (Ts, Abs (_,T,u)) = T --> fastype_of1 (T::Ts, u); |
|
330 |
||
331 |
fun fastype_of t : typ = fastype_of1 ([],t); |
|
0 | 332 |
|
333 |
||
334 |
(* maps (x1,...,xn)t to t *) |
|
335 |
fun strip_abs_body (Abs(_,_,t)) = strip_abs_body t |
|
336 |
| strip_abs_body u = u; |
|
337 |
||
338 |
||
339 |
(* maps (x1,...,xn)t to [x1, ..., xn] *) |
|
340 |
fun strip_abs_vars (Abs(a,T,t)) = (a,T) :: strip_abs_vars t |
|
341 |
| strip_abs_vars u = [] : (string*typ) list; |
|
342 |
||
343 |
||
344 |
fun strip_qnt_body qnt = |
|
345 |
let fun strip(tm as Const(c,_)$Abs(_,_,t)) = if c=qnt then strip t else tm |
|
346 |
| strip t = t |
|
347 |
in strip end; |
|
348 |
||
349 |
fun strip_qnt_vars qnt = |
|
350 |
let fun strip(Const(c,_)$Abs(a,T,t)) = if c=qnt then (a,T)::strip t else [] |
|
351 |
| strip t = [] : (string*typ) list |
|
352 |
in strip end; |
|
353 |
||
354 |
||
355 |
(* maps (f, [t1,...,tn]) to f(t1,...,tn) *) |
|
356 |
val list_comb : term * term list -> term = foldl (op $); |
|
357 |
||
358 |
||
359 |
(* maps f(t1,...,tn) to (f, [t1,...,tn]) ; naturally tail-recursive*) |
|
360 |
fun strip_comb u : term * term list = |
|
361 |
let fun stripc (f$t, ts) = stripc (f, t::ts) |
|
362 |
| stripc x = x |
|
363 |
in stripc(u,[]) end; |
|
364 |
||
365 |
||
366 |
(* maps f(t1,...,tn) to f , which is never a combination *) |
|
367 |
fun head_of (f$t) = head_of f |
|
368 |
| head_of u = u; |
|
369 |
||
370 |
||
371 |
(*Number of atoms and abstractions in a term*) |
|
372 |
fun size_of_term (Abs (_,_,body)) = 1 + size_of_term body |
|
373 |
| size_of_term (f$t) = size_of_term f + size_of_term t |
|
374 |
| size_of_term _ = 1; |
|
375 |
||
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
376 |
fun map_type_tvar f (Type(a,Ts)) = Type(a, map (map_type_tvar f) Ts) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
377 |
| map_type_tvar f (T as TFree _) = T |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
378 |
| map_type_tvar f (TVar x) = f x; |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
379 |
|
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
380 |
fun map_type_tfree f (Type(a,Ts)) = Type(a, map (map_type_tfree f) Ts) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
381 |
| map_type_tfree f (TFree x) = f x |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
382 |
| map_type_tfree f (T as TVar _) = T; |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
383 |
|
0 | 384 |
(* apply a function to all types in a term *) |
385 |
fun map_term_types f = |
|
386 |
let fun map(Const(a,T)) = Const(a, f T) |
|
387 |
| map(Free(a,T)) = Free(a, f T) |
|
388 |
| map(Var(v,T)) = Var(v, f T) |
|
389 |
| map(t as Bound _) = t |
|
390 |
| map(Abs(a,T,t)) = Abs(a, f T, map t) |
|
391 |
| map(f$t) = map f $ map t; |
|
392 |
in map end; |
|
393 |
||
394 |
(* iterate a function over all types in a term *) |
|
395 |
fun it_term_types f = |
|
396 |
let fun iter(Const(_,T), a) = f(T,a) |
|
397 |
| iter(Free(_,T), a) = f(T,a) |
|
398 |
| iter(Var(_,T), a) = f(T,a) |
|
399 |
| iter(Abs(_,T,t), a) = iter(t,f(T,a)) |
|
400 |
| iter(f$u, a) = iter(f, iter(u, a)) |
|
401 |
| iter(Bound _, a) = a |
|
402 |
in iter end |
|
403 |
||
404 |
||
405 |
(** Connectives of higher order logic **) |
|
406 |
||
375 | 407 |
val logicC: class = "logic"; |
408 |
val logicS: sort = [logicC]; |
|
409 |
||
410 |
fun itselfT ty = Type ("itself", [ty]); |
|
411 |
val a_itselfT = itselfT (TFree ("'a", logicS)); |
|
412 |
||
0 | 413 |
val propT : typ = Type("prop",[]); |
414 |
||
415 |
val implies = Const("==>", propT-->propT-->propT); |
|
416 |
||
417 |
fun all T = Const("all", (T-->propT)-->propT); |
|
418 |
||
419 |
fun equals T = Const("==", T-->T-->propT); |
|
420 |
||
421 |
fun flexpair T = Const("=?=", T-->T-->propT); |
|
422 |
||
423 |
(* maps !!x1...xn. t to t *) |
|
424 |
fun strip_all_body (Const("all",_)$Abs(_,_,t)) = strip_all_body t |
|
425 |
| strip_all_body t = t; |
|
426 |
||
427 |
(* maps !!x1...xn. t to [x1, ..., xn] *) |
|
428 |
fun strip_all_vars (Const("all",_)$Abs(a,T,t)) = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
429 |
(a,T) :: strip_all_vars t |
0 | 430 |
| strip_all_vars t = [] : (string*typ) list; |
431 |
||
432 |
(*increments a term's non-local bound variables |
|
433 |
required when moving a term within abstractions |
|
434 |
inc is increment for bound variables |
|
435 |
lev is level at which a bound variable is considered 'loose'*) |
|
436 |
fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u |
|
437 |
| incr_bv (inc, lev, Abs(a,T,body)) = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
438 |
Abs(a, T, incr_bv(inc,lev+1,body)) |
0 | 439 |
| incr_bv (inc, lev, f$t) = |
440 |
incr_bv(inc,lev,f) $ incr_bv(inc,lev,t) |
|
441 |
| incr_bv (inc, lev, u) = u; |
|
442 |
||
443 |
fun incr_boundvars 0 t = t |
|
444 |
| incr_boundvars inc t = incr_bv(inc,0,t); |
|
445 |
||
446 |
||
447 |
(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond. |
|
448 |
(Bound 0) is loose at level 0 *) |
|
449 |
fun add_loose_bnos (Bound i, lev, js) = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
450 |
if i<lev then js else (i-lev) ins_int js |
0 | 451 |
| add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js) |
452 |
| add_loose_bnos (f$t, lev, js) = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
453 |
add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) |
0 | 454 |
| add_loose_bnos (_, _, js) = js; |
455 |
||
456 |
fun loose_bnos t = add_loose_bnos (t, 0, []); |
|
457 |
||
458 |
(* loose_bvar(t,k) iff t contains a 'loose' bound variable referring to |
|
459 |
level k or beyond. *) |
|
460 |
fun loose_bvar(Bound i,k) = i >= k |
|
461 |
| loose_bvar(f$t, k) = loose_bvar(f,k) orelse loose_bvar(t,k) |
|
462 |
| loose_bvar(Abs(_,_,t),k) = loose_bvar(t,k+1) |
|
463 |
| loose_bvar _ = false; |
|
464 |
||
2792 | 465 |
fun loose_bvar1(Bound i,k) = i = k |
466 |
| loose_bvar1(f$t, k) = loose_bvar1(f,k) orelse loose_bvar1(t,k) |
|
467 |
| loose_bvar1(Abs(_,_,t),k) = loose_bvar1(t,k+1) |
|
468 |
| loose_bvar1 _ = false; |
|
0 | 469 |
|
470 |
(*Substitute arguments for loose bound variables. |
|
471 |
Beta-reduction of arg(n-1)...arg0 into t replacing (Bound i) with (argi). |
|
4626 | 472 |
Note that for ((%x y. c) a b), the bound vars in c are x=1 and y=0 |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
473 |
and the appropriate call is subst_bounds([b,a], c) . |
0 | 474 |
Loose bound variables >=n are reduced by "n" to |
475 |
compensate for the disappearance of lambdas. |
|
476 |
*) |
|
477 |
fun subst_bounds (args: term list, t) : term = |
|
478 |
let val n = length args; |
|
479 |
fun subst (t as Bound i, lev) = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
480 |
(if i<lev then t (*var is locally bound*) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
481 |
else incr_boundvars lev (List.nth(args, i-lev)) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
482 |
handle Subscript => Bound(i-n) (*loose: change it*)) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
483 |
| subst (Abs(a,T,body), lev) = Abs(a, T, subst(body,lev+1)) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
484 |
| subst (f$t, lev) = subst(f,lev) $ subst(t,lev) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
485 |
| subst (t,lev) = t |
0 | 486 |
in case args of [] => t | _ => subst (t,0) end; |
487 |
||
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
488 |
(*Special case: one argument*) |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
489 |
fun subst_bound (arg, t) : term = |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
490 |
let fun subst (t as Bound i, lev) = |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
491 |
if i<lev then t (*var is locally bound*) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
492 |
else if i=lev then incr_boundvars lev arg |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
493 |
else Bound(i-1) (*loose: change it*) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
494 |
| subst (Abs(a,T,body), lev) = Abs(a, T, subst(body,lev+1)) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
495 |
| subst (f$t, lev) = subst(f,lev) $ subst(t,lev) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
496 |
| subst (t,lev) = t |
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
497 |
in subst (t,0) end; |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
498 |
|
0 | 499 |
(*beta-reduce if possible, else form application*) |
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
500 |
fun betapply (Abs(_,_,t), u) = subst_bound (u,t) |
0 | 501 |
| betapply (f,u) = f$u; |
502 |
||
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
503 |
(** Equality, membership and insertion of indexnames (string*ints) **) |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
504 |
|
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
505 |
(*optimized equality test for indexnames. Yields a huge gain under Poly/ML*) |
2959 | 506 |
fun eq_ix ((a, i):indexname, (a',i'):indexname) = (a=a') andalso i=i'; |
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
507 |
|
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
508 |
(*membership in a list, optimized version for indexnames*) |
2959 | 509 |
fun mem_ix (_, []) = false |
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
510 |
| mem_ix (x, y :: ys) = eq_ix(x,y) orelse mem_ix (x, ys); |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
511 |
|
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
512 |
(*insertion into list, optimized version for indexnames*) |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
513 |
fun ins_ix (x,xs) = if mem_ix (x, xs) then xs else x :: xs; |
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
514 |
|
0 | 515 |
(*Tests whether 2 terms are alpha-convertible and have same type. |
4626 | 516 |
Note that constants may have more than one type.*) |
0 | 517 |
fun (Const(a,T)) aconv (Const(b,U)) = a=b andalso T=U |
2752 | 518 |
| (Free(a,T)) aconv (Free(b,U)) = a=b andalso T=U |
519 |
| (Var(v,T)) aconv (Var(w,U)) = eq_ix(v,w) andalso T=U |
|
520 |
| (Bound i) aconv (Bound j) = i=j |
|
0 | 521 |
| (Abs(_,T,t)) aconv (Abs(_,U,u)) = t aconv u andalso T=U |
2752 | 522 |
| (f$t) aconv (g$u) = (f aconv g) andalso (t aconv u) |
0 | 523 |
| _ aconv _ = false; |
524 |
||
2176 | 525 |
(** Membership, insertion, union for terms **) |
526 |
||
527 |
fun mem_term (_, []) = false |
|
528 |
| mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts); |
|
529 |
||
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
530 |
fun subset_term ([], ys) = true |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
531 |
| subset_term (x :: xs, ys) = mem_term (x, ys) andalso subset_term(xs, ys); |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
532 |
|
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
533 |
fun eq_set_term (xs, ys) = |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
534 |
xs = ys orelse (subset_term (xs, ys) andalso subset_term (ys, xs)); |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
535 |
|
2176 | 536 |
fun ins_term(t,ts) = if mem_term(t,ts) then ts else t :: ts; |
537 |
||
538 |
fun union_term (xs, []) = xs |
|
539 |
| union_term ([], ys) = ys |
|
540 |
| union_term ((x :: xs), ys) = union_term (xs, ins_term (x, ys)); |
|
541 |
||
5585 | 542 |
fun inter_term ([], ys) = [] |
543 |
| inter_term (x::xs, ys) = |
|
544 |
if mem_term (x,ys) then x :: inter_term(xs,ys) else inter_term(xs,ys); |
|
545 |
||
2176 | 546 |
(** Equality, membership and insertion of sorts (string lists) **) |
547 |
||
548 |
fun eq_sort ([]:sort, []) = true |
|
549 |
| eq_sort ((s::ss), (t::ts)) = s=t andalso eq_sort(ss,ts) |
|
550 |
| eq_sort (_, _) = false; |
|
551 |
||
552 |
fun mem_sort (_:sort, []) = false |
|
553 |
| mem_sort (S, S'::Ss) = eq_sort (S, S') orelse mem_sort(S,Ss); |
|
554 |
||
555 |
fun ins_sort(S,Ss) = if mem_sort(S,Ss) then Ss else S :: Ss; |
|
556 |
||
557 |
fun union_sort (xs, []) = xs |
|
558 |
| union_sort ([], ys) = ys |
|
559 |
| union_sort ((x :: xs), ys) = union_sort (xs, ins_sort (x, ys)); |
|
560 |
||
2182
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
561 |
fun subset_sort ([], ys) = true |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
562 |
| subset_sort (x :: xs, ys) = mem_sort (x, ys) andalso subset_sort(xs, ys); |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
563 |
|
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
564 |
fun eq_set_sort (xs, ys) = |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
565 |
xs = ys orelse (subset_sort (xs, ys) andalso subset_sort (ys, xs)); |
29e56f003599
Removal of polymorphic equality via mem, subset, eq_set, etc
paulson
parents:
2176
diff
changeset
|
566 |
|
7638 | 567 |
val rems_sort = gen_rems eq_sort; |
568 |
||
0 | 569 |
(*are two term lists alpha-convertible in corresponding elements?*) |
570 |
fun aconvs ([],[]) = true |
|
571 |
| aconvs (t::ts, u::us) = t aconv u andalso aconvs(ts,us) |
|
572 |
| aconvs _ = false; |
|
573 |
||
574 |
(*A fast unification filter: true unless the two terms cannot be unified. |
|
575 |
Terms must be NORMAL. Treats all Vars as distinct. *) |
|
576 |
fun could_unify (t,u) = |
|
577 |
let fun matchrands (f$t, g$u) = could_unify(t,u) andalso matchrands(f,g) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
578 |
| matchrands _ = true |
0 | 579 |
in case (head_of t , head_of u) of |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
580 |
(_, Var _) => true |
0 | 581 |
| (Var _, _) => true |
582 |
| (Const(a,_), Const(b,_)) => a=b andalso matchrands(t,u) |
|
583 |
| (Free(a,_), Free(b,_)) => a=b andalso matchrands(t,u) |
|
584 |
| (Bound i, Bound j) => i=j andalso matchrands(t,u) |
|
585 |
| (Abs _, _) => true (*because of possible eta equality*) |
|
586 |
| (_, Abs _) => true |
|
587 |
| _ => false |
|
588 |
end; |
|
589 |
||
590 |
(*Substitute new for free occurrences of old in a term*) |
|
591 |
fun subst_free [] = (fn t=>t) |
|
592 |
| subst_free pairs = |
|
593 |
let fun substf u = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
594 |
case gen_assoc (op aconv) (pairs, u) of |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
595 |
Some u' => u' |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
596 |
| None => (case u of Abs(a,T,t) => Abs(a, T, substf t) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
597 |
| t$u' => substf t $ substf u' |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
598 |
| _ => u) |
0 | 599 |
in substf end; |
600 |
||
601 |
(*a total, irreflexive ordering on index names*) |
|
602 |
fun xless ((a,i), (b,j): indexname) = i<j orelse (i=j andalso a<b); |
|
603 |
||
604 |
||
605 |
(*Abstraction of the term "body" over its occurrences of v, |
|
606 |
which must contain no loose bound variables. |
|
607 |
The resulting term is ready to become the body of an Abs.*) |
|
608 |
fun abstract_over (v,body) = |
|
609 |
let fun abst (lev,u) = if (v aconv u) then (Bound lev) else |
|
610 |
(case u of |
|
611 |
Abs(a,T,t) => Abs(a, T, abst(lev+1, t)) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
612 |
| f$rand => abst(lev,f) $ abst(lev,rand) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
613 |
| _ => u) |
0 | 614 |
in abst(0,body) end; |
615 |
||
616 |
||
617 |
(*Form an abstraction over a free variable.*) |
|
618 |
fun absfree (a,T,body) = Abs(a, T, abstract_over (Free(a,T), body)); |
|
619 |
||
620 |
(*Abstraction over a list of free variables*) |
|
621 |
fun list_abs_free ([ ] , t) = t |
|
622 |
| list_abs_free ((a,T)::vars, t) = |
|
623 |
absfree(a, T, list_abs_free(vars,t)); |
|
624 |
||
625 |
(*Quantification over a list of free variables*) |
|
626 |
fun list_all_free ([], t: term) = t |
|
627 |
| list_all_free ((a,T)::vars, t) = |
|
628 |
(all T) $ (absfree(a, T, list_all_free(vars,t))); |
|
629 |
||
630 |
(*Quantification over a list of variables (already bound in body) *) |
|
631 |
fun list_all ([], t) = t |
|
632 |
| list_all ((a,T)::vars, t) = |
|
633 |
(all T) $ (Abs(a, T, list_all(vars,t))); |
|
634 |
||
635 |
(*Replace the ATOMIC term ti by ui; instl = [(t1,u1), ..., (tn,un)]. |
|
636 |
A simultaneous substitution: [ (a,b), (b,a) ] swaps a and b. *) |
|
637 |
fun subst_atomic [] t = t : term |
|
638 |
| subst_atomic (instl: (term*term) list) t = |
|
639 |
let fun subst (Abs(a,T,body)) = Abs(a, T, subst body) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
640 |
| subst (f$t') = subst f $ subst t' |
9721 | 641 |
| subst t = if_none (assoc(instl,t)) t |
0 | 642 |
in subst t end; |
643 |
||
728
9a973c3ba350
Pure/term: commented typ_subst_TVars, subst_TVars, subst_Vars, subst_vars
lcp
parents:
375
diff
changeset
|
644 |
(*Substitute for type Vars in a type*) |
0 | 645 |
fun typ_subst_TVars iTs T = if null iTs then T else |
646 |
let fun subst(Type(a,Ts)) = Type(a, map subst Ts) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
647 |
| subst(T as TFree _) = T |
9721 | 648 |
| subst(T as TVar(ixn,_)) = if_none (assoc(iTs,ixn)) T |
0 | 649 |
in subst T end; |
650 |
||
728
9a973c3ba350
Pure/term: commented typ_subst_TVars, subst_TVars, subst_Vars, subst_vars
lcp
parents:
375
diff
changeset
|
651 |
(*Substitute for type Vars in a term*) |
0 | 652 |
val subst_TVars = map_term_types o typ_subst_TVars; |
653 |
||
728
9a973c3ba350
Pure/term: commented typ_subst_TVars, subst_TVars, subst_Vars, subst_vars
lcp
parents:
375
diff
changeset
|
654 |
(*Substitute for Vars in a term; see also envir/norm_term*) |
0 | 655 |
fun subst_Vars itms t = if null itms then t else |
9721 | 656 |
let fun subst(v as Var(ixn,_)) = if_none (assoc(itms,ixn)) v |
0 | 657 |
| subst(Abs(a,T,t)) = Abs(a,T,subst t) |
658 |
| subst(f$t) = subst f $ subst t |
|
659 |
| subst(t) = t |
|
660 |
in subst t end; |
|
661 |
||
728
9a973c3ba350
Pure/term: commented typ_subst_TVars, subst_TVars, subst_Vars, subst_vars
lcp
parents:
375
diff
changeset
|
662 |
(*Substitute for type/term Vars in a term; see also envir/norm_term*) |
0 | 663 |
fun subst_vars(iTs,itms) = if null iTs then subst_Vars itms else |
664 |
let fun subst(Const(a,T)) = Const(a,typ_subst_TVars iTs T) |
|
665 |
| subst(Free(a,T)) = Free(a,typ_subst_TVars iTs T) |
|
666 |
| subst(v as Var(ixn,T)) = (case assoc(itms,ixn) of |
|
667 |
None => Var(ixn,typ_subst_TVars iTs T) |
|
668 |
| Some t => t) |
|
669 |
| subst(b as Bound _) = b |
|
670 |
| subst(Abs(a,T,t)) = Abs(a,typ_subst_TVars iTs T,subst t) |
|
671 |
| subst(f$t) = subst f $ subst t |
|
672 |
in subst end; |
|
673 |
||
674 |
||
675 |
(*Computing the maximum index of a typ*) |
|
2146 | 676 |
fun maxidx_of_typ(Type(_,Ts)) = maxidx_of_typs Ts |
0 | 677 |
| maxidx_of_typ(TFree _) = ~1 |
2146 | 678 |
| maxidx_of_typ(TVar((_,i),_)) = i |
679 |
and maxidx_of_typs [] = ~1 |
|
680 |
| maxidx_of_typs (T::Ts) = Int.max(maxidx_of_typ T, maxidx_of_typs Ts); |
|
0 | 681 |
|
682 |
||
683 |
(*Computing the maximum index of a term*) |
|
684 |
fun maxidx_of_term (Const(_,T)) = maxidx_of_typ T |
|
685 |
| maxidx_of_term (Bound _) = ~1 |
|
686 |
| maxidx_of_term (Free(_,T)) = maxidx_of_typ T |
|
2146 | 687 |
| maxidx_of_term (Var ((_,i), T)) = Int.max(i, maxidx_of_typ T) |
688 |
| maxidx_of_term (Abs (_,T,u)) = Int.max(maxidx_of_term u, maxidx_of_typ T) |
|
689 |
| maxidx_of_term (f$t) = Int.max(maxidx_of_term f, maxidx_of_term t); |
|
0 | 690 |
|
691 |
||
692 |
(* Increment the index of all Poly's in T by k *) |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
693 |
fun incr_tvar k = map_type_tvar (fn ((a,i),S) => TVar((a,i+k),S)); |
0 | 694 |
|
695 |
||
696 |
(**** Syntax-related declarations ****) |
|
697 |
||
698 |
||
4626 | 699 |
(*Dummy type for parsing and printing. Will be replaced during type inference. *) |
0 | 700 |
val dummyT = Type("dummy",[]); |
701 |
||
4694 | 702 |
(*read a numeral of the given radix, normally 10*) |
703 |
fun read_radixint (radix: int, cs) : int * string list = |
|
0 | 704 |
let val zero = ord"0" |
705 |
val limit = zero+radix |
|
706 |
fun scan (num,[]) = (num,[]) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
707 |
| scan (num, c::cs) = |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
708 |
if zero <= ord c andalso ord c < limit |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
709 |
then scan(radix*num + ord c - zero, cs) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
710 |
else (num, c::cs) |
0 | 711 |
in scan(0,cs) end; |
712 |
||
5986 | 713 |
fun read_int cs = read_radixint (10, cs); |
714 |
||
715 |
fun octal s = #1 (read_radixint (8, explode s)); |
|
716 |
val oct_char = chr o octal; |
|
0 | 717 |
|
718 |
||
719 |
(*** Printing ***) |
|
720 |
||
721 |
||
722 |
(*Makes a variant of the name c distinct from the names in bs. |
|
723 |
First attaches the suffix "a" and then increments this. *) |
|
724 |
fun variant bs c : string = |
|
2138
056dead45ae8
Changed some mem calls to mem_string for greater efficiency (not that it could matter)
paulson
parents:
1460
diff
changeset
|
725 |
let fun vary2 c = if (c mem_string bs) then vary2 (bump_string c) else c |
056dead45ae8
Changed some mem calls to mem_string for greater efficiency (not that it could matter)
paulson
parents:
1460
diff
changeset
|
726 |
fun vary1 c = if (c mem_string bs) then vary2 (c ^ "a") else c |
0 | 727 |
in vary1 (if c="" then "u" else c) end; |
728 |
||
729 |
(*Create variants of the list of names, with priority to the first ones*) |
|
730 |
fun variantlist ([], used) = [] |
|
731 |
| variantlist(b::bs, used) = |
|
732 |
let val b' = variant used b |
|
733 |
in b' :: variantlist (bs, b'::used) end; |
|
734 |
||
4017
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
735 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
736 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
737 |
(** Consts etc. **) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
738 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
739 |
fun add_typ_classes (Type (_, Ts), cs) = foldr add_typ_classes (Ts, cs) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
740 |
| add_typ_classes (TFree (_, S), cs) = S union cs |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
741 |
| add_typ_classes (TVar (_, S), cs) = S union cs; |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
742 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
743 |
fun add_typ_tycons (Type (c, Ts), cs) = foldr add_typ_tycons (Ts, c ins cs) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
744 |
| add_typ_tycons (_, cs) = cs; |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
745 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
746 |
val add_term_classes = it_term_types add_typ_classes; |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
747 |
val add_term_tycons = it_term_types add_typ_tycons; |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
748 |
|
9319 | 749 |
fun add_term_consts (Const (c, _), cs) = c ins_string cs |
4017
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
750 |
| add_term_consts (t $ u, cs) = add_term_consts (t, add_term_consts (u, cs)) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
751 |
| add_term_consts (Abs (_, _, t), cs) = add_term_consts (t, cs) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
752 |
| add_term_consts (_, cs) = cs; |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
753 |
|
4185 | 754 |
fun exists_Const P t = let |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
755 |
fun ex (Const c ) = P c |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
756 |
| ex (t $ u ) = ex t orelse ex u |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
757 |
| ex (Abs (_, _, t)) = ex t |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
758 |
| ex _ = false |
4185 | 759 |
in ex t end; |
4017
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
760 |
|
4631 | 761 |
fun exists_subterm P = |
762 |
let fun ex t = P t orelse |
|
763 |
(case t of |
|
764 |
u $ v => ex u orelse ex v |
|
765 |
| Abs(_, _, u) => ex u |
|
766 |
| _ => false) |
|
767 |
in ex end; |
|
768 |
||
4017
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
769 |
(*map classes, tycons*) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
770 |
fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
771 |
| map_typ f _ (TFree (x, S)) = TFree (x, map f S) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
772 |
| map_typ f _ (TVar (xi, S)) = TVar (xi, map f S); |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
773 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
774 |
(*map classes, tycons, consts*) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
775 |
fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
776 |
| map_term f g _ (Free (x, T)) = Free (x, map_typ f g T) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
777 |
| map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
778 |
| map_term _ _ _ (t as Bound _) = t |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
779 |
| map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t) |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
780 |
| map_term f g h (t $ u) = map_term f g h t $ map_term f g h u; |
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
781 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
782 |
|
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
783 |
|
0 | 784 |
(** TFrees and TVars **) |
785 |
||
786 |
(*maps (bs,v) to v'::bs this reverses the identifiers bs*) |
|
787 |
fun add_new_id (bs, c) : string list = variant bs c :: bs; |
|
788 |
||
789 |
(*Accumulates the names in the term, suppressing duplicates. |
|
790 |
Includes Frees and Consts. For choosing unambiguous bound var names.*) |
|
2176 | 791 |
fun add_term_names (Const(a,_), bs) = a ins_string bs |
792 |
| add_term_names (Free(a,_), bs) = a ins_string bs |
|
0 | 793 |
| add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs)) |
794 |
| add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs) |
|
795 |
| add_term_names (_, bs) = bs; |
|
796 |
||
797 |
(*Accumulates the TVars in a type, suppressing duplicates. *) |
|
798 |
fun add_typ_tvars(Type(_,Ts),vs) = foldr add_typ_tvars (Ts,vs) |
|
799 |
| add_typ_tvars(TFree(_),vs) = vs |
|
800 |
| add_typ_tvars(TVar(v),vs) = v ins vs; |
|
801 |
||
802 |
(*Accumulates the TFrees in a type, suppressing duplicates. *) |
|
803 |
fun add_typ_tfree_names(Type(_,Ts),fs) = foldr add_typ_tfree_names (Ts,fs) |
|
2176 | 804 |
| add_typ_tfree_names(TFree(f,_),fs) = f ins_string fs |
0 | 805 |
| add_typ_tfree_names(TVar(_),fs) = fs; |
806 |
||
807 |
fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs) |
|
808 |
| add_typ_tfrees(TFree(f),fs) = f ins fs |
|
809 |
| add_typ_tfrees(TVar(_),fs) = fs; |
|
810 |
||
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
811 |
fun add_typ_varnames(Type(_,Ts),nms) = foldr add_typ_varnames (Ts,nms) |
2176 | 812 |
| add_typ_varnames(TFree(nm,_),nms) = nm ins_string nms |
813 |
| add_typ_varnames(TVar((nm,_),_),nms) = nm ins_string nms; |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
814 |
|
0 | 815 |
(*Accumulates the TVars in a term, suppressing duplicates. *) |
816 |
val add_term_tvars = it_term_types add_typ_tvars; |
|
817 |
||
818 |
(*Accumulates the TFrees in a term, suppressing duplicates. *) |
|
819 |
val add_term_tfrees = it_term_types add_typ_tfrees; |
|
820 |
val add_term_tfree_names = it_term_types add_typ_tfree_names; |
|
821 |
||
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
822 |
val add_term_tvarnames = it_term_types add_typ_varnames; |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
823 |
|
0 | 824 |
(*Non-list versions*) |
825 |
fun typ_tfrees T = add_typ_tfrees(T,[]); |
|
826 |
fun typ_tvars T = add_typ_tvars(T,[]); |
|
827 |
fun term_tfrees t = add_term_tfrees(t,[]); |
|
828 |
fun term_tvars t = add_term_tvars(t,[]); |
|
829 |
||
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
830 |
(*special code to enforce left-to-right collection of TVar-indexnames*) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
831 |
|
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
832 |
fun add_typ_ixns(ixns,Type(_,Ts)) = foldl add_typ_ixns (ixns,Ts) |
2176 | 833 |
| add_typ_ixns(ixns,TVar(ixn,_)) = if mem_ix (ixn, ixns) then ixns |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
834 |
else ixns@[ixn] |
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
835 |
| add_typ_ixns(ixns,TFree(_)) = ixns; |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
836 |
|
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
837 |
fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
838 |
| add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
839 |
| add_term_tvar_ixns(Var(_,T),ixns) = add_typ_ixns(ixns,T) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
840 |
| add_term_tvar_ixns(Bound _,ixns) = ixns |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
841 |
| add_term_tvar_ixns(Abs(_,T,t),ixns) = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
842 |
add_term_tvar_ixns(t,add_typ_ixns(ixns,T)) |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
843 |
| add_term_tvar_ixns(f$t,ixns) = |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
844 |
add_term_tvar_ixns(t,add_term_tvar_ixns(f,ixns)); |
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
845 |
|
0 | 846 |
(** Frees and Vars **) |
847 |
||
848 |
(*a partial ordering (not reflexive) for atomic terms*) |
|
849 |
fun atless (Const (a,_), Const (b,_)) = a<b |
|
850 |
| atless (Free (a,_), Free (b,_)) = a<b |
|
851 |
| atless (Var(v,_), Var(w,_)) = xless(v,w) |
|
852 |
| atless (Bound i, Bound j) = i<j |
|
853 |
| atless _ = false; |
|
854 |
||
855 |
(*insert atomic term into partially sorted list, suppressing duplicates (?)*) |
|
856 |
fun insert_aterm (t,us) = |
|
857 |
let fun inserta [] = [t] |
|
858 |
| inserta (us as u::us') = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
859 |
if atless(t,u) then t::us |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
860 |
else if t=u then us (*duplicate*) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
861 |
else u :: inserta(us') |
0 | 862 |
in inserta us end; |
863 |
||
864 |
(*Accumulates the Vars in the term, suppressing duplicates*) |
|
865 |
fun add_term_vars (t, vars: term list) = case t of |
|
866 |
Var _ => insert_aterm(t,vars) |
|
867 |
| Abs (_,_,body) => add_term_vars(body,vars) |
|
868 |
| f$t => add_term_vars (f, add_term_vars(t, vars)) |
|
869 |
| _ => vars; |
|
870 |
||
871 |
fun term_vars t = add_term_vars(t,[]); |
|
872 |
||
873 |
(*Accumulates the Frees in the term, suppressing duplicates*) |
|
874 |
fun add_term_frees (t, frees: term list) = case t of |
|
875 |
Free _ => insert_aterm(t,frees) |
|
876 |
| Abs (_,_,body) => add_term_frees(body,frees) |
|
877 |
| f$t => add_term_frees (f, add_term_frees(t, frees)) |
|
878 |
| _ => frees; |
|
879 |
||
880 |
fun term_frees t = add_term_frees(t,[]); |
|
881 |
||
882 |
(*Given an abstraction over P, replaces the bound variable by a Free variable |
|
883 |
having a unique name. *) |
|
884 |
fun variant_abs (a,T,P) = |
|
885 |
let val b = variant (add_term_names(P,[])) a |
|
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
886 |
in (b, subst_bound (Free(b,T), P)) end; |
0 | 887 |
|
888 |
(* renames and reverses the strings in vars away from names *) |
|
889 |
fun rename_aTs names vars : (string*typ)list = |
|
890 |
let fun rename_aT (vars,(a,T)) = |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
891 |
(variant (map #1 vars @ names) a, T) :: vars |
0 | 892 |
in foldl rename_aT ([],vars) end; |
893 |
||
894 |
fun rename_wrt_term t = rename_aTs (add_term_names(t,[])); |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
895 |
|
1417 | 896 |
|
4286
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
897 |
(* left-ro-right traversal *) |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
898 |
|
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
899 |
(*foldl atoms of type*) |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
900 |
fun foldl_atyps f (x, Type (_, Ts)) = foldl (foldl_atyps f) (x, Ts) |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
901 |
| foldl_atyps f x_atom = f x_atom; |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
902 |
|
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
903 |
(*foldl atoms of term*) |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
904 |
fun foldl_aterms f (x, t $ u) = foldl_aterms f (foldl_aterms f (x, t), u) |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
905 |
| foldl_aterms f (x, Abs (_, _, t)) = foldl_aterms f (x, t) |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
906 |
| foldl_aterms f x_atom = f x_atom; |
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
907 |
|
6548
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
908 |
fun foldl_map_aterms f (x, t $ u) = |
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
909 |
let val (x', t') = foldl_map_aterms f (x, t); val (x'', u') = foldl_map_aterms f (x', u); |
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
910 |
in (x'', t' $ u') end |
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
911 |
| foldl_map_aterms f (x, Abs (a, T, t)) = |
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
912 |
let val (x', t') = foldl_map_aterms f (x, t) in (x', Abs (a, T, t')) end |
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
913 |
| foldl_map_aterms f x_atom = f x_atom; |
9b108dd75c25
val foldl_map_aterms: ('a * term -> 'a * term) -> 'a * term -> 'a * term;
wenzelm
parents:
6033
diff
changeset
|
914 |
|
4286
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
915 |
(*foldl types of term*) |
8609 | 916 |
fun foldl_term_types f (x, t as Const (_, T)) = f t (x, T) |
917 |
| foldl_term_types f (x, t as Free (_, T)) = f t (x, T) |
|
918 |
| foldl_term_types f (x, t as Var (_, T)) = f t (x, T) |
|
919 |
| foldl_term_types f (x, Bound _) = x |
|
920 |
| foldl_term_types f (x, t as Abs (_, T, b)) = foldl_term_types f (f t (x, T), b) |
|
921 |
| foldl_term_types f (x, t $ u) = foldl_term_types f (foldl_term_types f (x, t), u); |
|
922 |
||
923 |
fun foldl_types f = foldl_term_types (fn _ => f); |
|
4286
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
924 |
|
a09e3e6da2de
added foldl_atyps: ('a * typ -> 'a) -> 'a * typ -> 'a;
wenzelm
parents:
4188
diff
changeset
|
925 |
|
1417 | 926 |
|
4444 | 927 |
(** type and term orders **) |
928 |
||
929 |
fun indexname_ord ((x, i), (y, j)) = |
|
930 |
(case int_ord (i, j) of EQUAL => string_ord (x, y) | ord => ord); |
|
931 |
||
932 |
||
933 |
(* typ_ord *) |
|
934 |
||
935 |
(*assumes that TFrees / TVars with the same name have same sorts*) |
|
936 |
fun typ_ord (Type (a, Ts), Type (b, Us)) = |
|
937 |
(case string_ord (a, b) of EQUAL => typs_ord (Ts, Us) | ord => ord) |
|
938 |
| typ_ord (Type _, _) = GREATER |
|
939 |
| typ_ord (TFree _, Type _) = LESS |
|
940 |
| typ_ord (TFree (a, _), TFree (b, _)) = string_ord (a, b) |
|
941 |
| typ_ord (TFree _, TVar _) = GREATER |
|
942 |
| typ_ord (TVar _, Type _) = LESS |
|
943 |
| typ_ord (TVar _, TFree _) = LESS |
|
944 |
| typ_ord (TVar (xi, _), TVar (yj, _)) = indexname_ord (xi, yj) |
|
945 |
and typs_ord Ts_Us = list_ord typ_ord Ts_Us; |
|
946 |
||
947 |
||
948 |
(* term_ord *) |
|
949 |
||
950 |
(*a linear well-founded AC-compatible ordering for terms: |
|
951 |
s < t <=> 1. size(s) < size(t) or |
|
952 |
2. size(s) = size(t) and s=f(...) and t=g(...) and f<g or |
|
953 |
3. size(s) = size(t) and s=f(s1..sn) and t=f(t1..tn) and |
|
954 |
(s1..sn) < (t1..tn) (lexicographically)*) |
|
955 |
||
956 |
fun dest_hd (Const (a, T)) = (((a, 0), T), 0) |
|
957 |
| dest_hd (Free (a, T)) = (((a, 0), T), 1) |
|
958 |
| dest_hd (Var v) = (v, 2) |
|
959 |
| dest_hd (Bound i) = ((("", i), dummyT), 3) |
|
960 |
| dest_hd (Abs (_, T, _)) = ((("", 0), T), 4); |
|
961 |
||
962 |
fun term_ord (Abs (_, T, t), Abs(_, U, u)) = |
|
963 |
(case term_ord (t, u) of EQUAL => typ_ord (T, U) | ord => ord) |
|
964 |
| term_ord (t, u) = |
|
965 |
(case int_ord (size_of_term t, size_of_term u) of |
|
966 |
EQUAL => |
|
967 |
let val (f, ts) = strip_comb t and (g, us) = strip_comb u in |
|
968 |
(case hd_ord (f, g) of EQUAL => terms_ord (ts, us) | ord => ord) |
|
969 |
end |
|
970 |
| ord => ord) |
|
971 |
and hd_ord (f, g) = |
|
972 |
prod_ord (prod_ord indexname_ord typ_ord) int_ord (dest_hd f, dest_hd g) |
|
973 |
and terms_ord (ts, us) = list_ord term_ord (ts, us); |
|
974 |
||
975 |
fun termless tu = (term_ord tu = LESS); |
|
976 |
||
8408
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
977 |
structure Vartab = TableFun(type key = indexname val ord = indexname_ord); |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
978 |
structure Termtab = TableFun(type key = term val ord = term_ord); |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
979 |
|
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
980 |
(*Substitute for type Vars in a type, version using Vartab*) |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
981 |
fun typ_subst_TVars_Vartab iTs T = if Vartab.is_empty iTs then T else |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
982 |
let fun subst(Type(a, Ts)) = Type(a, map subst Ts) |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
983 |
| subst(T as TFree _) = T |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
984 |
| subst(T as TVar(ixn, _)) = |
8408
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
985 |
(case Vartab.lookup (iTs, ixn) of None => T | Some(U) => U) |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
986 |
in subst T end; |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
987 |
|
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
988 |
(*Substitute for type Vars in a term, version using Vartab*) |
4d981311dab7
Added functions subst_TVars_Vartab and typ_subst_TVars_Vartab.
berghofe
parents:
7638
diff
changeset
|
989 |
val subst_TVars_Vartab = map_term_types o typ_subst_TVars_Vartab; |
4444 | 990 |
|
991 |
||
1426 | 992 |
(*** Compression of terms and types by sharing common subtrees. |
993 |
Saves 50-75% on storage requirements. As it is fairly slow, |
|
994 |
it is called only for axioms, stored theorems, etc. ***) |
|
1417 | 995 |
|
996 |
(** Sharing of types **) |
|
997 |
||
998 |
fun atomic_tag (Type (a,_)) = if a<>"fun" then a else raise Match |
|
999 |
| atomic_tag (TFree (a,_)) = a |
|
1000 |
| atomic_tag (TVar ((a,_),_)) = a; |
|
1001 |
||
1002 |
fun type_tag (Type("fun",[S,T])) = atomic_tag S ^ type_tag T |
|
1003 |
| type_tag T = atomic_tag T; |
|
1004 |
||
4487 | 1005 |
val memo_types = ref (Symtab.empty : typ list Symtab.table); |
1417 | 1006 |
|
4188
1025a27b08f9
changed libraray function find to find_index_eq, currying it
oheimb
parents:
4185
diff
changeset
|
1007 |
(* special case of library/find_first *) |
1417 | 1008 |
fun find_type (T, []: typ list) = None |
1009 |
| find_type (T, T'::Ts) = |
|
1010 |
if T=T' then Some T' |
|
1011 |
else find_type (T, Ts); |
|
1012 |
||
1013 |
fun compress_type T = |
|
1014 |
let val tag = type_tag T |
|
6963 | 1015 |
val tylist = Symtab.lookup_multi (!memo_types, tag) |
1417 | 1016 |
in |
1017 |
case find_type (T,tylist) of |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1018 |
Some T' => T' |
1417 | 1019 |
| None => |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1020 |
let val T' = |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1021 |
case T of |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1022 |
Type (a,Ts) => Type (a, map compress_type Ts) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1023 |
| _ => T |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1024 |
in memo_types := Symtab.update ((tag, T'::tylist), !memo_types); |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1025 |
T |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1026 |
end |
1417 | 1027 |
end |
1028 |
handle Match => |
|
1029 |
let val Type (a,Ts) = T |
|
1030 |
in Type (a, map compress_type Ts) end; |
|
1031 |
||
1032 |
(** Sharing of atomic terms **) |
|
1033 |
||
4487 | 1034 |
val memo_terms = ref (Symtab.empty : term list Symtab.table); |
1417 | 1035 |
|
4188
1025a27b08f9
changed libraray function find to find_index_eq, currying it
oheimb
parents:
4185
diff
changeset
|
1036 |
(* special case of library/find_first *) |
1417 | 1037 |
fun find_term (t, []: term list) = None |
1038 |
| find_term (t, t'::ts) = |
|
1039 |
if t=t' then Some t' |
|
1040 |
else find_term (t, ts); |
|
1041 |
||
1042 |
fun const_tag (Const (a,_)) = a |
|
1043 |
| const_tag (Free (a,_)) = a |
|
1044 |
| const_tag (Var ((a,i),_)) = a |
|
1045 |
| const_tag (t as Bound _) = ".B."; |
|
1046 |
||
1047 |
fun share_term (t $ u) = share_term t $ share_term u |
|
1048 |
| share_term (Abs (a,T,u)) = Abs (a, T, share_term u) |
|
1049 |
| share_term t = |
|
1050 |
let val tag = const_tag t |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1051 |
val ts = Symtab.lookup_multi (!memo_terms, tag) |
1417 | 1052 |
in |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1053 |
case find_term (t,ts) of |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1054 |
Some t' => t' |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1055 |
| None => (memo_terms := Symtab.update ((tag, t::ts), !memo_terms); |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1056 |
t) |
1417 | 1057 |
end; |
1058 |
||
1059 |
val compress_term = share_term o map_term_types compress_type; |
|
1060 |
||
4444 | 1061 |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1062 |
(* dummy patterns *) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1063 |
|
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1064 |
val dummy_patternN = "dummy_pattern"; |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1065 |
|
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1066 |
fun is_dummy_pattern (Const ("dummy_pattern", _)) = true |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1067 |
| is_dummy_pattern _ = false; |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1068 |
|
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1069 |
fun no_dummy_patterns tm = |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1070 |
if not (foldl_aterms (fn (b, t) => b orelse is_dummy_pattern t) (false, tm)) then tm |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1071 |
else raise TERM ("Illegal occurrence of '_' dummy pattern", [tm]); |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1072 |
|
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1073 |
val replace_dummy_patterns = foldl_map_aterms (fn (i, t) => |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1074 |
if is_dummy_pattern t then (i + 1, Var (("_dummy_", i), fastype_of t)) else (i, t)); |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1075 |
|
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1076 |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
1077 |
end; |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
1078 |
|
4444 | 1079 |
|
1080 |
structure BasicTerm: BASIC_TERM = Term; |
|
1081 |
open BasicTerm; |