author | wenzelm |
Wed, 27 May 2020 20:02:02 +0200 | |
changeset 71910 | f8b0271cc744 |
parent 67721 | 5348bea4accd |
child 73351 | 88dd8a6a42ba |
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 |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
29280 | 3 |
Author: Makarius |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
4 |
|
4444 | 5 |
Simply typed lambda-calculus: types, terms, and basic operations. |
0 | 6 |
*) |
7 |
||
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
8 |
infix 9 $; |
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
9 |
infixr 5 -->; |
4444 | 10 |
infixr --->; |
11 |
infix aconv; |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
12 |
|
4444 | 13 |
signature BASIC_TERM = |
14 |
sig |
|
34922 | 15 |
type indexname = string * int |
16 |
type class = string |
|
17 |
type sort = class list |
|
18 |
type arity = string * sort list * sort |
|
4444 | 19 |
datatype typ = |
20 |
Type of string * typ list | |
|
21 |
TFree of string * sort | |
|
22 |
TVar of indexname * sort |
|
16537 | 23 |
datatype term = |
24 |
Const of string * typ | |
|
25 |
Free of string * typ | |
|
26 |
Var of indexname * typ | |
|
27 |
Bound of int | |
|
28 |
Abs of string * typ * term | |
|
17756 | 29 |
$ of term * term |
16537 | 30 |
exception TYPE of string * typ list * term list |
31 |
exception TERM of string * term list |
|
21353 | 32 |
val dummyS: sort |
16710 | 33 |
val dummyT: typ |
34 |
val no_dummyT: typ -> typ |
|
4444 | 35 |
val --> : typ * typ -> typ |
36 |
val ---> : typ list * typ -> typ |
|
67703 | 37 |
val is_Type: typ -> bool |
38 |
val is_TFree: typ -> bool |
|
39 |
val is_TVar: typ -> bool |
|
16710 | 40 |
val dest_Type: typ -> string * typ list |
67703 | 41 |
val dest_TFree: typ -> string * sort |
16710 | 42 |
val dest_TVar: typ -> indexname * sort |
43 |
val is_Bound: term -> bool |
|
44 |
val is_Const: term -> bool |
|
45 |
val is_Free: term -> bool |
|
46 |
val is_Var: term -> bool |
|
47 |
val dest_Const: term -> string * typ |
|
48 |
val dest_Free: term -> string * typ |
|
49 |
val dest_Var: term -> indexname * typ |
|
35227 | 50 |
val dest_comb: term -> term * term |
4444 | 51 |
val domain_type: typ -> typ |
4480 | 52 |
val range_type: typ -> typ |
40840 | 53 |
val dest_funT: typ -> typ * typ |
4444 | 54 |
val binder_types: typ -> typ list |
55 |
val body_type: typ -> typ |
|
56 |
val strip_type: typ -> typ list * typ |
|
16710 | 57 |
val type_of1: typ list * term -> typ |
4444 | 58 |
val type_of: term -> typ |
16710 | 59 |
val fastype_of1: typ list * term -> typ |
4444 | 60 |
val fastype_of: term -> typ |
18927 | 61 |
val strip_abs: term -> (string * typ) list * term |
4444 | 62 |
val strip_abs_body: term -> term |
63 |
val strip_abs_vars: term -> (string * typ) list |
|
64 |
val strip_qnt_body: string -> term -> term |
|
65 |
val strip_qnt_vars: string -> term -> (string * typ) list |
|
66 |
val list_comb: term * term list -> term |
|
67 |
val strip_comb: term -> term * term list |
|
68 |
val head_of: term -> term |
|
69 |
val size_of_term: term -> int |
|
29882
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29286
diff
changeset
|
70 |
val size_of_typ: typ -> int |
18847 | 71 |
val map_atyps: (typ -> typ) -> typ -> typ |
72 |
val map_aterms: (term -> term) -> term -> term |
|
4444 | 73 |
val map_type_tvar: (indexname * sort -> typ) -> typ -> typ |
74 |
val map_type_tfree: (string * sort -> typ) -> typ -> typ |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20531
diff
changeset
|
75 |
val map_types: (typ -> typ) -> term -> term |
16943 | 76 |
val fold_atyps: (typ -> 'a -> 'a) -> typ -> 'a -> 'a |
35986 | 77 |
val fold_atyps_sorts: (typ * sort -> 'a -> 'a) -> typ -> 'a -> 'a |
16943 | 78 |
val fold_aterms: (term -> 'a -> 'a) -> term -> 'a -> 'a |
79 |
val fold_term_types: (term -> typ -> 'a -> 'a) -> term -> 'a -> 'a |
|
80 |
val fold_types: (typ -> 'a -> 'a) -> term -> 'a -> 'a |
|
24483 | 81 |
val burrow_types: (typ list -> typ list) -> term list -> term list |
16710 | 82 |
val aconv: term * term -> bool |
4444 | 83 |
val propT: typ |
84 |
val strip_all_body: term -> term |
|
85 |
val strip_all_vars: term -> (string * typ) list |
|
86 |
val incr_bv: int * int * term -> term |
|
87 |
val incr_boundvars: int -> term -> term |
|
88 |
val add_loose_bnos: term * int * int list -> int list |
|
89 |
val loose_bnos: term -> int list |
|
90 |
val loose_bvar: term * int -> bool |
|
91 |
val loose_bvar1: term * int -> bool |
|
92 |
val subst_bounds: term list * term -> term |
|
93 |
val subst_bound: term * term -> term |
|
94 |
val betapply: term * term -> term |
|
18183 | 95 |
val betapplys: term * term list -> term |
4444 | 96 |
val subst_free: (term * term) list -> term -> term |
97 |
val abstract_over: term * term -> term |
|
11922 | 98 |
val lambda: term -> term -> term |
44241 | 99 |
val absfree: string * typ -> term -> term |
100 |
val absdummy: typ -> term -> term |
|
16710 | 101 |
val subst_atomic: (term * term) list -> term -> term |
102 |
val typ_subst_atomic: (typ * typ) list -> typ -> typ |
|
103 |
val subst_atomic_types: (typ * typ) list -> term -> term |
|
104 |
val typ_subst_TVars: (indexname * typ) list -> typ -> typ |
|
105 |
val subst_TVars: (indexname * typ) list -> term -> term |
|
106 |
val subst_Vars: (indexname * term) list -> term -> term |
|
107 |
val subst_vars: (indexname * typ) list * (indexname * term) list -> term -> term |
|
108 |
val is_first_order: string list -> term -> bool |
|
4444 | 109 |
val maxidx_of_typ: typ -> int |
110 |
val maxidx_of_typs: typ list -> int |
|
111 |
val maxidx_of_term: term -> int |
|
61248 | 112 |
val fold_subtypes: (typ -> 'a -> 'a) -> typ -> 'a -> 'a |
19909 | 113 |
val exists_subtype: (typ -> bool) -> typ -> bool |
20531 | 114 |
val exists_type: (typ -> bool) -> term -> bool |
16943 | 115 |
val exists_subterm: (term -> bool) -> term -> bool |
16710 | 116 |
val exists_Const: (string * typ -> bool) -> term -> bool |
4444 | 117 |
end; |
0 | 118 |
|
4444 | 119 |
signature TERM = |
120 |
sig |
|
121 |
include BASIC_TERM |
|
19394 | 122 |
val aT: sort -> typ |
123 |
val itselfT: typ -> typ |
|
124 |
val a_itselfT: typ |
|
22908 | 125 |
val argument_type_of: term -> int -> typ |
46219
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents:
46218
diff
changeset
|
126 |
val abs: string * typ -> term -> term |
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
127 |
val add_tvar_namesT: typ -> indexname list -> indexname list |
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
128 |
val add_tvar_names: term -> indexname list -> indexname list |
16943 | 129 |
val add_tvarsT: typ -> (indexname * sort) list -> (indexname * sort) list |
130 |
val add_tvars: term -> (indexname * sort) list -> (indexname * sort) list |
|
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
131 |
val add_var_names: term -> indexname list -> indexname list |
16943 | 132 |
val add_vars: term -> (indexname * typ) list -> (indexname * typ) list |
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
133 |
val add_tfree_namesT: typ -> string list -> string list |
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
134 |
val add_tfree_names: term -> string list -> string list |
16943 | 135 |
val add_tfreesT: typ -> (string * sort) list -> (string * sort) list |
136 |
val add_tfrees: term -> (string * sort) list -> (string * sort) list |
|
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
137 |
val add_free_names: term -> string list -> string list |
16943 | 138 |
val add_frees: term -> (string * typ) list -> (string * typ) list |
29286 | 139 |
val add_const_names: term -> string list -> string list |
140 |
val add_consts: term -> (string * typ) list -> (string * typ) list |
|
25050 | 141 |
val hidden_polymorphism: term -> (indexname * sort) list |
29278 | 142 |
val declare_typ_names: typ -> Name.context -> Name.context |
143 |
val declare_term_names: term -> Name.context -> Name.context |
|
144 |
val declare_term_frees: term -> Name.context -> Name.context |
|
145 |
val variant_frees: term -> (string * 'a) list -> (string * 'a) list |
|
146 |
val rename_wrt_term: term -> (string * 'a) list -> (string * 'a) list |
|
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
147 |
val eq_ix: indexname * indexname -> bool |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
148 |
val eq_tvar: (indexname * sort) * (indexname * sort) -> bool |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
149 |
val eq_var: (indexname * typ) * (indexname * typ) -> bool |
33537
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
150 |
val aconv_untyped: term * term -> bool |
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
151 |
val could_unify: term * term -> bool |
20109 | 152 |
val strip_abs_eta: int -> term -> (string * typ) list * term |
48263 | 153 |
val match_bvars: (term * term) -> (string * string) list -> (string * string) list |
22031 | 154 |
val map_abs_vars: (string -> string) -> term -> term |
12981 | 155 |
val rename_abs: term -> term -> term -> term option |
42083
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
40844
diff
changeset
|
156 |
val is_open: term -> bool |
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
40844
diff
changeset
|
157 |
val is_dependent: term -> bool |
61655
f217bbe4e93e
avoid vacuous quantification, as usual for shared variable scope;
wenzelm
parents:
61248
diff
changeset
|
158 |
val term_name: term -> string |
60404
422b63ef0147
clarified abstracted term bindings (again, see c8384ff11711);
wenzelm
parents:
60311
diff
changeset
|
159 |
val dependent_lambda_name: string * term -> term -> term |
32198 | 160 |
val lambda_name: string * term -> term -> term |
25050 | 161 |
val close_schematic_term: term -> term |
16710 | 162 |
val maxidx_typ: typ -> int -> int |
163 |
val maxidx_typs: typ list -> int -> int |
|
164 |
val maxidx_term: term -> int -> int |
|
63619 | 165 |
val could_beta_contract: term -> bool |
166 |
val could_eta_contract: term -> bool |
|
167 |
val could_beta_eta_contract: term -> bool |
|
20239 | 168 |
val dest_abs: string * typ * term -> string * term |
18253 | 169 |
val dummy_pattern: typ -> term |
45156 | 170 |
val dummy: term |
171 |
val dummy_prop: term |
|
22723 | 172 |
val is_dummy_pattern: term -> bool |
24733 | 173 |
val free_dummy_patterns: term -> Name.context -> term * Name.context |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
174 |
val no_dummy_patterns: term -> term |
24762 | 175 |
val replace_dummy_patterns: term -> int -> term * int |
16035 | 176 |
val show_dummy_patterns: term -> term |
14786
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
177 |
val string_of_vname: indexname -> string |
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
178 |
val string_of_vname': indexname -> string |
4444 | 179 |
end; |
180 |
||
181 |
structure Term: TERM = |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
182 |
struct |
0 | 183 |
|
184 |
(*Indexnames can be quickly renamed by adding an offset to the integer part, |
|
185 |
for resolution.*) |
|
16537 | 186 |
type indexname = string * int; |
0 | 187 |
|
63611 | 188 |
(*Types are classified by sorts.*) |
0 | 189 |
type class = string; |
190 |
type sort = class list; |
|
14829
cfa5fe01a7b7
moved read_int etc. to library.ML; added type 'arity';
wenzelm
parents:
14786
diff
changeset
|
191 |
type arity = string * sort list * sort; |
0 | 192 |
|
63611 | 193 |
(*The sorts attached to TFrees and TVars specify the sort of that variable.*) |
0 | 194 |
datatype typ = Type of string * typ list |
195 |
| TFree of string * sort |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
196 |
| TVar of indexname * sort; |
0 | 197 |
|
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
198 |
(*Terms. Bound variables are indicated by depth number. |
0 | 199 |
Free variables, (scheme) variables and constants have names. |
4626 | 200 |
An term is "closed" if every bound variable of level "lev" |
13000 | 201 |
is enclosed by at least "lev" abstractions. |
0 | 202 |
|
203 |
It is possible to create meaningless terms containing loose bound vars |
|
204 |
or type mismatches. But such terms are not allowed in rules. *) |
|
205 |
||
13000 | 206 |
datatype term = |
0 | 207 |
Const of string * typ |
13000 | 208 |
| Free of string * typ |
0 | 209 |
| Var of indexname * typ |
210 |
| Bound of int |
|
211 |
| Abs of string*typ*term |
|
3965 | 212 |
| op $ of term*term; |
0 | 213 |
|
16537 | 214 |
(*Errors involving type mismatches*) |
0 | 215 |
exception TYPE of string * typ list * term list; |
216 |
||
16537 | 217 |
(*Errors errors involving terms*) |
0 | 218 |
exception TERM of string * term list; |
219 |
||
220 |
(*Note variable naming conventions! |
|
221 |
a,b,c: string |
|
222 |
f,g,h: functions (including terms of function type) |
|
223 |
i,j,m,n: int |
|
224 |
t,u: term |
|
225 |
v,w: indexnames |
|
226 |
x,y: any |
|
227 |
A,B,C: term (denoting formulae) |
|
228 |
T,U: typ |
|
229 |
*) |
|
230 |
||
231 |
||
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
232 |
(** Types **) |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
233 |
|
21353 | 234 |
(*dummies for type-inference etc.*) |
235 |
val dummyS = [""]; |
|
16537 | 236 |
val dummyT = Type ("dummy", []); |
237 |
||
238 |
fun no_dummyT typ = |
|
239 |
let |
|
240 |
fun check (T as Type ("dummy", _)) = |
|
241 |
raise TYPE ("Illegal occurrence of '_' dummy type", [T], []) |
|
242 |
| check (Type (_, Ts)) = List.app check Ts |
|
243 |
| check _ = (); |
|
244 |
in check typ; typ end; |
|
245 |
||
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
246 |
fun S --> T = Type("fun",[S,T]); |
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 |
(*handy for multiple args: [T1,...,Tn]--->T gives T1-->(T2--> ... -->T)*) |
15570 | 249 |
val op ---> = Library.foldr (op -->); |
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
250 |
|
67703 | 251 |
|
252 |
(** Discriminators **) |
|
253 |
||
254 |
fun is_Type (Type _) = true |
|
255 |
| is_Type _ = false; |
|
256 |
||
257 |
fun is_TFree (TFree _) = true |
|
258 |
| is_TFree _ = false; |
|
259 |
||
260 |
fun is_TVar (TVar _) = true |
|
261 |
| is_TVar _ = false; |
|
262 |
||
263 |
||
264 |
(** Destructors **) |
|
265 |
||
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
266 |
fun dest_Type (Type x) = x |
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
267 |
| dest_Type T = raise TYPE ("dest_Type", [T], []); |
67703 | 268 |
|
269 |
fun dest_TFree (TFree x) = x |
|
270 |
| dest_TFree T = raise TYPE ("dest_TFree", [T], []); |
|
271 |
||
15914 | 272 |
fun dest_TVar (TVar x) = x |
273 |
| dest_TVar T = raise TYPE ("dest_TVar", [T], []); |
|
6033
c8c69a4a7762
moved dest_Type to term.ML from HOL/Tools/primrec_package
paulson
parents:
5986
diff
changeset
|
274 |
|
16537 | 275 |
|
0 | 276 |
(** Discriminators **) |
277 |
||
7318 | 278 |
fun is_Bound (Bound _) = true |
279 |
| is_Bound _ = false; |
|
280 |
||
0 | 281 |
fun is_Const (Const _) = true |
282 |
| is_Const _ = false; |
|
283 |
||
284 |
fun is_Free (Free _) = true |
|
285 |
| is_Free _ = false; |
|
286 |
||
287 |
fun is_Var (Var _) = true |
|
288 |
| is_Var _ = false; |
|
289 |
||
16537 | 290 |
|
0 | 291 |
(** Destructors **) |
292 |
||
293 |
fun dest_Const (Const x) = x |
|
294 |
| dest_Const t = raise TERM("dest_Const", [t]); |
|
295 |
||
296 |
fun dest_Free (Free x) = x |
|
297 |
| dest_Free t = raise TERM("dest_Free", [t]); |
|
298 |
||
299 |
fun dest_Var (Var x) = x |
|
300 |
| dest_Var t = raise TERM("dest_Var", [t]); |
|
301 |
||
35227 | 302 |
fun dest_comb (t1 $ t2) = (t1, t2) |
303 |
| dest_comb t = raise TERM("dest_comb", [t]); |
|
304 |
||
0 | 305 |
|
40841 | 306 |
fun domain_type (Type ("fun", [T, _])) = T; |
307 |
||
308 |
fun range_type (Type ("fun", [_, U])) = U; |
|
4064 | 309 |
|
40840 | 310 |
fun dest_funT (Type ("fun", [T, U])) = (T, U) |
311 |
| dest_funT T = raise TYPE ("dest_funT", [T], []); |
|
312 |
||
313 |
||
63611 | 314 |
(*maps [T1,...,Tn]--->T to the list [T1,T2,...,Tn]*) |
40841 | 315 |
fun binder_types (Type ("fun", [T, U])) = T :: binder_types U |
316 |
| binder_types _ = []; |
|
0 | 317 |
|
63611 | 318 |
(*maps [T1,...,Tn]--->T to T*) |
40841 | 319 |
fun body_type (Type ("fun", [_, U])) = body_type U |
320 |
| body_type T = T; |
|
0 | 321 |
|
63611 | 322 |
(*maps [T1,...,Tn]--->T to ([T1,T2,...,Tn], T)*) |
40841 | 323 |
fun strip_type T = (binder_types T, body_type T); |
0 | 324 |
|
325 |
||
326 |
(*Compute the type of the term, checking that combinations are well-typed |
|
327 |
Ts = [T0,T1,...] holds types of bound variables 0, 1, ...*) |
|
328 |
fun type_of1 (Ts, Const (_,T)) = T |
|
329 |
| type_of1 (Ts, Free (_,T)) = T |
|
30146 | 330 |
| type_of1 (Ts, Bound i) = (nth Ts i |
43278 | 331 |
handle General.Subscript => raise TYPE("type_of: bound variable", [], [Bound i])) |
0 | 332 |
| type_of1 (Ts, Var (_,T)) = T |
333 |
| type_of1 (Ts, Abs (_,T,body)) = T --> type_of1(T::Ts, body) |
|
13000 | 334 |
| type_of1 (Ts, f$u) = |
0 | 335 |
let val U = type_of1(Ts,u) |
336 |
and T = type_of1(Ts,f) |
|
337 |
in case T of |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
338 |
Type("fun",[T1,T2]) => |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
339 |
if T1=U then T2 else raise TYPE |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
340 |
("type_of: type mismatch in application", [T1,U], [f$u]) |
13000 | 341 |
| _ => raise TYPE |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
342 |
("type_of: function type is expected in application", |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
343 |
[T,U], [f$u]) |
0 | 344 |
end; |
345 |
||
346 |
fun type_of t : typ = type_of1 ([],t); |
|
347 |
||
348 |
(*Determines the type of a term, with minimal checking*) |
|
13000 | 349 |
fun fastype_of1 (Ts, f$u) = |
61 | 350 |
(case fastype_of1 (Ts,f) of |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
351 |
Type("fun",[_,T]) => T |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
352 |
| _ => raise TERM("fastype_of: expected function type", [f$u])) |
61 | 353 |
| fastype_of1 (_, Const (_,T)) = T |
354 |
| fastype_of1 (_, Free (_,T)) = T |
|
30146 | 355 |
| fastype_of1 (Ts, Bound i) = (nth Ts i |
43278 | 356 |
handle General.Subscript => raise TERM("fastype_of: Bound", [Bound i])) |
13000 | 357 |
| fastype_of1 (_, Var (_,T)) = T |
61 | 358 |
| fastype_of1 (Ts, Abs (_,T,u)) = T --> fastype_of1 (T::Ts, u); |
359 |
||
360 |
fun fastype_of t : typ = fastype_of1 ([],t); |
|
0 | 361 |
|
16678 | 362 |
(*Determine the argument type of a function*) |
22908 | 363 |
fun argument_type_of tm k = |
16678 | 364 |
let |
365 |
fun argT i (Type ("fun", [T, U])) = if i = 0 then T else argT (i - 1) U |
|
366 |
| argT _ T = raise TYPE ("argument_type_of", [T], []); |
|
367 |
||
368 |
fun arg 0 _ (Abs (_, T, _)) = T |
|
369 |
| arg i Ts (Abs (_, T, t)) = arg (i - 1) (T :: Ts) t |
|
370 |
| arg i Ts (t $ _) = arg (i + 1) Ts t |
|
371 |
| arg i Ts a = argT i (fastype_of1 (Ts, a)); |
|
22908 | 372 |
in arg k [] tm end; |
16678 | 373 |
|
0 | 374 |
|
46219
426ed18eba43
discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents:
46218
diff
changeset
|
375 |
fun abs (x, T) t = Abs (x, T, t); |
10806 | 376 |
|
18927 | 377 |
fun strip_abs (Abs (a, T, t)) = |
378 |
let val (a', t') = strip_abs t |
|
379 |
in ((a, T) :: a', t') end |
|
380 |
| strip_abs t = ([], t); |
|
381 |
||
63611 | 382 |
(*maps (x1,...,xn)t to t*) |
13000 | 383 |
fun strip_abs_body (Abs(_,_,t)) = strip_abs_body t |
0 | 384 |
| strip_abs_body u = u; |
385 |
||
63611 | 386 |
(*maps (x1,...,xn)t to [x1, ..., xn]*) |
13000 | 387 |
fun strip_abs_vars (Abs(a,T,t)) = (a,T) :: strip_abs_vars t |
0 | 388 |
| strip_abs_vars u = [] : (string*typ) list; |
389 |
||
390 |
||
391 |
fun strip_qnt_body qnt = |
|
392 |
let fun strip(tm as Const(c,_)$Abs(_,_,t)) = if c=qnt then strip t else tm |
|
393 |
| strip t = t |
|
394 |
in strip end; |
|
395 |
||
396 |
fun strip_qnt_vars qnt = |
|
397 |
let fun strip(Const(c,_)$Abs(a,T,t)) = if c=qnt then (a,T)::strip t else [] |
|
398 |
| strip t = [] : (string*typ) list |
|
399 |
in strip end; |
|
400 |
||
401 |
||
63611 | 402 |
(*maps (f, [t1,...,tn]) to f(t1,...,tn)*) |
15570 | 403 |
val list_comb : term * term list -> term = Library.foldl (op $); |
0 | 404 |
|
405 |
||
63611 | 406 |
(*maps f(t1,...,tn) to (f, [t1,...,tn]) ; naturally tail-recursive*) |
13000 | 407 |
fun strip_comb u : term * term list = |
0 | 408 |
let fun stripc (f$t, ts) = stripc (f, t::ts) |
13000 | 409 |
| stripc x = x |
0 | 410 |
in stripc(u,[]) end; |
411 |
||
412 |
||
63611 | 413 |
(*maps f(t1,...,tn) to f , which is never a combination*) |
0 | 414 |
fun head_of (f$t) = head_of f |
415 |
| head_of u = u; |
|
416 |
||
16599 | 417 |
(*number of atoms and abstractions in a term*) |
418 |
fun size_of_term tm = |
|
419 |
let |
|
30144
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
420 |
fun add_size (t $ u) n = add_size t (add_size u n) |
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
421 |
| add_size (Abs (_ ,_, t)) n = add_size t (n + 1) |
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
422 |
| add_size _ n = n + 1; |
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
423 |
in add_size tm 0 end; |
0 | 424 |
|
30144
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
425 |
(*number of atoms and constructors in a type*) |
29882
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29286
diff
changeset
|
426 |
fun size_of_typ ty = |
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29286
diff
changeset
|
427 |
let |
30144
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
428 |
fun add_size (Type (_, tys)) n = fold add_size tys (n + 1) |
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
429 |
| add_size _ n = n + 1; |
56ae4893e8ae
tuned/unified size_of_term and size_of_typ, eliminated obsolete foldl;
wenzelm
parents:
29882
diff
changeset
|
430 |
in add_size ty 0 end; |
29882
29154e67731d
New command find_consts searching for constants by type (by Timothy Bourke).
kleing
parents:
29286
diff
changeset
|
431 |
|
18847 | 432 |
fun map_atyps f (Type (a, Ts)) = Type (a, map (map_atyps f) Ts) |
18976
4efb82669880
fixed the most silly bug conceivable in map_atyps
haftmann
parents:
18975
diff
changeset
|
433 |
| map_atyps f T = f T; |
18847 | 434 |
|
435 |
fun map_aterms f (t $ u) = map_aterms f t $ map_aterms f u |
|
436 |
| map_aterms f (Abs (a, T, t)) = Abs (a, T, map_aterms f t) |
|
437 |
| map_aterms f t = f t; |
|
438 |
||
18981 | 439 |
fun map_type_tvar f = map_atyps (fn TVar x => f x | T => T); |
440 |
fun map_type_tfree f = map_atyps (fn TFree x => f x | T => T); |
|
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
728
diff
changeset
|
441 |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20531
diff
changeset
|
442 |
fun map_types f = |
16678 | 443 |
let |
444 |
fun map_aux (Const (a, T)) = Const (a, f T) |
|
445 |
| map_aux (Free (a, T)) = Free (a, f T) |
|
446 |
| map_aux (Var (v, T)) = Var (v, f T) |
|
39293 | 447 |
| map_aux (Bound i) = Bound i |
16678 | 448 |
| map_aux (Abs (a, T, t)) = Abs (a, f T, map_aux t) |
449 |
| map_aux (t $ u) = map_aux t $ map_aux u; |
|
450 |
in map_aux end; |
|
0 | 451 |
|
452 |
||
16943 | 453 |
(* fold types and terms *) |
454 |
||
455 |
fun fold_atyps f (Type (_, Ts)) = fold (fold_atyps f) Ts |
|
456 |
| fold_atyps f T = f T; |
|
457 |
||
35986 | 458 |
fun fold_atyps_sorts f = |
459 |
fold_atyps (fn T as TFree (_, S) => f (T, S) | T as TVar (_, S) => f (T, S)); |
|
460 |
||
16943 | 461 |
fun fold_aterms f (t $ u) = fold_aterms f t #> fold_aterms f u |
462 |
| fold_aterms f (Abs (_, _, t)) = fold_aterms f t |
|
463 |
| fold_aterms f a = f a; |
|
464 |
||
465 |
fun fold_term_types f (t as Const (_, T)) = f t T |
|
466 |
| fold_term_types f (t as Free (_, T)) = f t T |
|
467 |
| fold_term_types f (t as Var (_, T)) = f t T |
|
468 |
| fold_term_types f (Bound _) = I |
|
469 |
| fold_term_types f (t as Abs (_, T, b)) = f t T #> fold_term_types f b |
|
470 |
| fold_term_types f (t $ u) = fold_term_types f t #> fold_term_types f u; |
|
471 |
||
472 |
fun fold_types f = fold_term_types (K f); |
|
473 |
||
24483 | 474 |
fun replace_types (Const (c, _)) (T :: Ts) = (Const (c, T), Ts) |
475 |
| replace_types (Free (x, _)) (T :: Ts) = (Free (x, T), Ts) |
|
476 |
| replace_types (Var (xi, _)) (T :: Ts) = (Var (xi, T), Ts) |
|
477 |
| replace_types (Bound i) Ts = (Bound i, Ts) |
|
478 |
| replace_types (Abs (x, _, b)) (T :: Ts) = |
|
479 |
let val (b', Ts') = replace_types b Ts |
|
480 |
in (Abs (x, T, b'), Ts') end |
|
481 |
| replace_types (t $ u) Ts = |
|
482 |
let |
|
483 |
val (t', Ts') = replace_types t Ts; |
|
484 |
val (u', Ts'') = replace_types u Ts'; |
|
485 |
in (t' $ u', Ts'') end; |
|
486 |
||
487 |
fun burrow_types f ts = |
|
488 |
let |
|
49674
dbadb4d03cbc
report sort assignment of visible type variables;
wenzelm
parents:
48263
diff
changeset
|
489 |
val Ts = rev ((fold o fold_types) cons ts []); |
24483 | 490 |
val Ts' = f Ts; |
491 |
val (ts', []) = fold_map replace_types ts Ts'; |
|
492 |
in ts' end; |
|
493 |
||
16943 | 494 |
(*collect variables*) |
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
495 |
val add_tvar_namesT = fold_atyps (fn TVar (xi, _) => insert (op =) xi | _ => I); |
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
496 |
val add_tvar_names = fold_types add_tvar_namesT; |
16943 | 497 |
val add_tvarsT = fold_atyps (fn TVar v => insert (op =) v | _ => I); |
498 |
val add_tvars = fold_types add_tvarsT; |
|
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
499 |
val add_var_names = fold_aterms (fn Var (xi, _) => insert (op =) xi | _ => I); |
16943 | 500 |
val add_vars = fold_aterms (fn Var v => insert (op =) v | _ => I); |
33697 | 501 |
val add_tfree_namesT = fold_atyps (fn TFree (a, _) => insert (op =) a | _ => I); |
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
502 |
val add_tfree_names = fold_types add_tfree_namesT; |
16943 | 503 |
val add_tfreesT = fold_atyps (fn TFree v => insert (op =) v | _ => I); |
504 |
val add_tfrees = fold_types add_tfreesT; |
|
29257
660234d959f7
provide canonical add_tvar_namesT, add_tvar_names, add_tfree_namesT, add_tfree_names, add_free_names;
wenzelm
parents:
29256
diff
changeset
|
505 |
val add_free_names = fold_aterms (fn Free (x, _) => insert (op =) x | _ => I); |
16943 | 506 |
val add_frees = fold_aterms (fn Free v => insert (op =) v | _ => I); |
29286 | 507 |
val add_const_names = fold_aterms (fn Const (c, _) => insert (op =) c | _ => I); |
508 |
val add_consts = fold_aterms (fn Const c => insert (op =) c | _ => I); |
|
16943 | 509 |
|
25050 | 510 |
(*extra type variables in a term, not covered by its type*) |
511 |
fun hidden_polymorphism t = |
|
21682 | 512 |
let |
25050 | 513 |
val T = fastype_of t; |
21682 | 514 |
val tvarsT = add_tvarsT T []; |
515 |
val extra_tvars = fold_types (fold_atyps |
|
516 |
(fn TVar v => if member (op =) tvarsT v then I else insert (op =) v | _ => I)) t []; |
|
517 |
in extra_tvars end; |
|
518 |
||
16943 | 519 |
|
29278 | 520 |
(* renaming variables *) |
521 |
||
522 |
val declare_typ_names = fold_atyps (fn TFree (a, _) => Name.declare a | _ => I); |
|
523 |
||
524 |
fun declare_term_names tm = |
|
525 |
fold_aterms |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30285
diff
changeset
|
526 |
(fn Const (a, _) => Name.declare (Long_Name.base_name a) |
29278 | 527 |
| Free (a, _) => Name.declare a |
528 |
| _ => I) tm #> |
|
529 |
fold_types declare_typ_names tm; |
|
530 |
||
531 |
val declare_term_frees = fold_aterms (fn Free (x, _) => Name.declare x | _ => I); |
|
532 |
||
533 |
fun variant_frees t frees = |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
534 |
fst (fold_map Name.variant (map fst frees) (declare_term_names t Name.context)) ~~ |
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
535 |
map snd frees; |
29278 | 536 |
|
537 |
fun rename_wrt_term t frees = rev (variant_frees t frees); (*reversed result!*) |
|
538 |
||
539 |
||
25050 | 540 |
|
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
541 |
(** Comparing terms **) |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
542 |
|
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
543 |
(* variables *) |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
544 |
|
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
545 |
fun eq_ix ((x, i): indexname, (y, j)) = i = j andalso x = y; |
16537 | 546 |
|
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
547 |
fun eq_tvar ((xi, S: sort), (xi', S')) = eq_ix (xi, xi') andalso S = S'; |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
548 |
fun eq_var ((xi, T: typ), (xi', T')) = eq_ix (xi, xi') andalso T = T'; |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
549 |
|
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
550 |
|
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
551 |
(* alpha equivalence *) |
20511 | 552 |
|
553 |
fun tm1 aconv tm2 = |
|
554 |
pointer_eq (tm1, tm2) orelse |
|
555 |
(case (tm1, tm2) of |
|
556 |
(t1 $ u1, t2 $ u2) => t1 aconv t2 andalso u1 aconv u2 |
|
557 |
| (Abs (_, T1, t1), Abs (_, T2, t2)) => t1 aconv t2 andalso T1 = T2 |
|
558 |
| (a1, a2) => a1 = a2); |
|
559 |
||
33537
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
560 |
fun aconv_untyped (tm1, tm2) = |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
561 |
pointer_eq (tm1, tm2) orelse |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
562 |
(case (tm1, tm2) of |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
563 |
(t1 $ u1, t2 $ u2) => aconv_untyped (t1, t2) andalso aconv_untyped (u1, u2) |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
564 |
| (Abs (_, _, t1), Abs (_, _, t2)) => aconv_untyped (t1, t2) |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
565 |
| (Const (a, _), Const (b, _)) => a = b |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
566 |
| (Free (x, _), Free (y, _)) => x = y |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
567 |
| (Var (xi, _), Var (yj, _)) => xi = yj |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
568 |
| (Bound i, Bound j) => i = j |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
569 |
| _ => false); |
06c87d2c5b5a
locale_const/target_notation: uniform use of Term.aconv_untyped;
wenzelm
parents:
33063
diff
changeset
|
570 |
|
20511 | 571 |
|
29269
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
572 |
(*A fast unification filter: true unless the two terms cannot be unified. |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
573 |
Terms must be NORMAL. Treats all Vars as distinct. *) |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
574 |
fun could_unify (t, u) = |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
575 |
let |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
576 |
fun matchrands (f $ t) (g $ u) = could_unify (t, u) andalso matchrands f g |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
577 |
| matchrands _ _ = true; |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
578 |
in |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
579 |
case (head_of t, head_of u) of |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
580 |
(_, Var _) => true |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
581 |
| (Var _, _) => true |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
582 |
| (Const (a, _), Const (b, _)) => a = b andalso matchrands t u |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
583 |
| (Free (a, _), Free (b, _)) => a = b andalso matchrands t u |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
584 |
| (Bound i, Bound j) => i = j andalso matchrands t u |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
585 |
| (Abs _, _) => true (*because of possible eta equality*) |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
586 |
| (_, Abs _) => true |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
587 |
| _ => false |
5c25a2012975
moved term order operations to structure TermOrd (cf. Pure/term_ord.ML);
wenzelm
parents:
29265
diff
changeset
|
588 |
end; |
16678 | 589 |
|
16570 | 590 |
|
16537 | 591 |
|
0 | 592 |
(** Connectives of higher order logic **) |
593 |
||
24850 | 594 |
fun aT S = TFree (Name.aT, S); |
19394 | 595 |
|
375 | 596 |
fun itselfT ty = Type ("itself", [ty]); |
24850 | 597 |
val a_itselfT = itselfT (TFree (Name.aT, [])); |
375 | 598 |
|
46217
7b19666f0e3d
renamed Term.all to Logic.all_const, in accordance to HOLogic.all_const;
wenzelm
parents:
46215
diff
changeset
|
599 |
val propT : typ = Type ("prop",[]); |
0 | 600 |
|
67721 | 601 |
(*maps \<And>x1...xn. t to t*) |
60311 | 602 |
fun strip_all_body (Const ("Pure.all", _) $ Abs (_, _, t)) = strip_all_body t |
603 |
| strip_all_body t = t; |
|
0 | 604 |
|
67721 | 605 |
(*maps \<And>x1...xn. t to [x1, ..., xn]*) |
60311 | 606 |
fun strip_all_vars (Const ("Pure.all", _) $ Abs (a, T, t)) = (a, T) :: strip_all_vars t |
607 |
| strip_all_vars t = []; |
|
0 | 608 |
|
609 |
(*increments a term's non-local bound variables |
|
610 |
required when moving a term within abstractions |
|
611 |
inc is increment for bound variables |
|
612 |
lev is level at which a bound variable is considered 'loose'*) |
|
13000 | 613 |
fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u |
0 | 614 |
| incr_bv (inc, lev, Abs(a,T,body)) = |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
615 |
Abs(a, T, incr_bv(inc,lev+1,body)) |
13000 | 616 |
| incr_bv (inc, lev, f$t) = |
0 | 617 |
incr_bv(inc,lev,f) $ incr_bv(inc,lev,t) |
618 |
| incr_bv (inc, lev, u) = u; |
|
619 |
||
620 |
fun incr_boundvars 0 t = t |
|
621 |
| incr_boundvars inc t = incr_bv(inc,0,t); |
|
622 |
||
12981 | 623 |
(*Scan a pair of terms; while they are similar, |
624 |
accumulate corresponding bound vars in "al"*) |
|
625 |
fun match_bvs(Abs(x,_,s),Abs(y,_,t), al) = |
|
626 |
match_bvs(s, t, if x="" orelse y="" then al |
|
627 |
else (x,y)::al) |
|
628 |
| match_bvs(f$s, g$t, al) = match_bvs(f,g,match_bvs(s,t,al)) |
|
629 |
| match_bvs(_,_,al) = al; |
|
630 |
||
631 |
(* strip abstractions created by parameters *) |
|
48263 | 632 |
fun match_bvars (s,t) al = match_bvs(strip_abs_body s, strip_abs_body t, al); |
12981 | 633 |
|
22031 | 634 |
fun map_abs_vars f (t $ u) = map_abs_vars f t $ map_abs_vars f u |
635 |
| map_abs_vars f (Abs (a, T, t)) = Abs (f a, T, map_abs_vars f t) |
|
636 |
| map_abs_vars f t = t; |
|
637 |
||
12981 | 638 |
fun rename_abs pat obj t = |
639 |
let |
|
640 |
val ren = match_bvs (pat, obj, []); |
|
641 |
fun ren_abs (Abs (x, T, b)) = |
|
18942 | 642 |
Abs (the_default x (AList.lookup (op =) ren x), T, ren_abs b) |
12981 | 643 |
| ren_abs (f $ t) = ren_abs f $ ren_abs t |
644 |
| ren_abs t = t |
|
15531 | 645 |
in if null ren then NONE else SOME (ren_abs t) end; |
0 | 646 |
|
647 |
(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond. |
|
648 |
(Bound 0) is loose at level 0 *) |
|
13000 | 649 |
fun add_loose_bnos (Bound i, lev, js) = |
20854 | 650 |
if i<lev then js else insert (op =) (i - lev) js |
0 | 651 |
| add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js) |
652 |
| add_loose_bnos (f$t, lev, js) = |
|
13000 | 653 |
add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) |
0 | 654 |
| add_loose_bnos (_, _, js) = js; |
655 |
||
656 |
fun loose_bnos t = add_loose_bnos (t, 0, []); |
|
657 |
||
658 |
(* loose_bvar(t,k) iff t contains a 'loose' bound variable referring to |
|
659 |
level k or beyond. *) |
|
660 |
fun loose_bvar(Bound i,k) = i >= k |
|
661 |
| loose_bvar(f$t, k) = loose_bvar(f,k) orelse loose_bvar(t,k) |
|
662 |
| loose_bvar(Abs(_,_,t),k) = loose_bvar(t,k+1) |
|
663 |
| loose_bvar _ = false; |
|
664 |
||
2792 | 665 |
fun loose_bvar1(Bound i,k) = i = k |
666 |
| loose_bvar1(f$t, k) = loose_bvar1(f,k) orelse loose_bvar1(t,k) |
|
667 |
| loose_bvar1(Abs(_,_,t),k) = loose_bvar1(t,k+1) |
|
668 |
| loose_bvar1 _ = false; |
|
0 | 669 |
|
42083
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
40844
diff
changeset
|
670 |
fun is_open t = loose_bvar (t, 0); |
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
40844
diff
changeset
|
671 |
fun is_dependent t = loose_bvar1 (t, 0); |
e1209fc7ecdc
added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents:
40844
diff
changeset
|
672 |
|
0 | 673 |
(*Substitute arguments for loose bound variables. |
674 |
Beta-reduction of arg(n-1)...arg0 into t replacing (Bound i) with (argi). |
|
67721 | 675 |
Note that for ((\<lambda>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
|
676 |
and the appropriate call is subst_bounds([b,a], c) . |
0 | 677 |
Loose bound variables >=n are reduced by "n" to |
678 |
compensate for the disappearance of lambdas. |
|
679 |
*) |
|
13000 | 680 |
fun subst_bounds (args: term list, t) : term = |
19065 | 681 |
let |
682 |
val n = length args; |
|
683 |
fun subst (t as Bound i, lev) = |
|
32020 | 684 |
(if i < lev then raise Same.SAME (*var is locally bound*) |
30146 | 685 |
else incr_boundvars lev (nth args (i - lev)) |
43278 | 686 |
handle General.Subscript => Bound (i - n)) (*loose: change it*) |
19065 | 687 |
| subst (Abs (a, T, body), lev) = Abs (a, T, subst (body, lev + 1)) |
688 |
| subst (f $ t, lev) = |
|
32020 | 689 |
(subst (f, lev) $ (subst (t, lev) handle Same.SAME => t) |
690 |
handle Same.SAME => f $ subst (t, lev)) |
|
691 |
| subst _ = raise Same.SAME; |
|
692 |
in case args of [] => t | _ => (subst (t, 0) handle Same.SAME => t) end; |
|
0 | 693 |
|
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
694 |
(*Special case: one argument*) |
13000 | 695 |
fun subst_bound (arg, t) : term = |
19065 | 696 |
let |
697 |
fun subst (Bound i, lev) = |
|
32020 | 698 |
if i < lev then raise Same.SAME (*var is locally bound*) |
19065 | 699 |
else if i = lev then incr_boundvars lev arg |
700 |
else Bound (i - 1) (*loose: change it*) |
|
701 |
| subst (Abs (a, T, body), lev) = Abs (a, T, subst (body, lev + 1)) |
|
702 |
| subst (f $ t, lev) = |
|
32020 | 703 |
(subst (f, lev) $ (subst (t, lev) handle Same.SAME => t) |
704 |
handle Same.SAME => f $ subst (t, lev)) |
|
705 |
| subst _ = raise Same.SAME; |
|
706 |
in subst (t, 0) handle Same.SAME => t end; |
|
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
707 |
|
0 | 708 |
(*beta-reduce if possible, else form application*) |
2192
3bf092b5310b
Optimizations: removal of polymorphic equality; one-argument case
paulson
parents:
2182
diff
changeset
|
709 |
fun betapply (Abs(_,_,t), u) = subst_bound (u,t) |
0 | 710 |
| betapply (f,u) = f$u; |
711 |
||
18183 | 712 |
val betapplys = Library.foldl betapply; |
713 |
||
14786
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
714 |
|
20109 | 715 |
(*unfolding abstractions with substitution |
716 |
of bound variables and implicit eta-expansion*) |
|
717 |
fun strip_abs_eta k t = |
|
718 |
let |
|
29278 | 719 |
val used = fold_aterms declare_term_frees t Name.context; |
20109 | 720 |
fun strip_abs t (0, used) = (([], t), (0, used)) |
721 |
| strip_abs (Abs (v, T, t)) (k, used) = |
|
722 |
let |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
723 |
val (v', used') = Name.variant v used; |
21013 | 724 |
val t' = subst_bound (Free (v', T), t); |
20122 | 725 |
val ((vs, t''), (k', used'')) = strip_abs t' (k - 1, used'); |
726 |
in (((v', T) :: vs, t''), (k', used'')) end |
|
20109 | 727 |
| strip_abs t (k, used) = (([], t), (k, used)); |
728 |
fun expand_eta [] t _ = ([], t) |
|
729 |
| expand_eta (T::Ts) t used = |
|
730 |
let |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43324
diff
changeset
|
731 |
val (v, used') = Name.variant "" used; |
20122 | 732 |
val (vs, t') = expand_eta Ts (t $ Free (v, T)) used'; |
20109 | 733 |
in ((v, T) :: vs, t') end; |
734 |
val ((vs1, t'), (k', used')) = strip_abs t (k, used); |
|
40844 | 735 |
val Ts = fst (chop k' (binder_types (fastype_of t'))); |
20109 | 736 |
val (vs2, t'') = expand_eta Ts t' used'; |
737 |
in (vs1 @ vs2, t'') end; |
|
738 |
||
739 |
||
0 | 740 |
(*Substitute new for free occurrences of old in a term*) |
29256 | 741 |
fun subst_free [] = I |
0 | 742 |
| subst_free pairs = |
13000 | 743 |
let fun substf u = |
17314 | 744 |
case AList.lookup (op aconv) pairs u of |
15531 | 745 |
SOME u' => u' |
746 |
| NONE => (case u of Abs(a,T,t) => Abs(a, T, substf t) |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
747 |
| t$u' => substf t $ substf u' |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
748 |
| _ => u) |
0 | 749 |
in substf end; |
750 |
||
13000 | 751 |
(*Abstraction of the term "body" over its occurrences of v, |
0 | 752 |
which must contain no loose bound variables. |
753 |
The resulting term is ready to become the body of an Abs.*) |
|
16882 | 754 |
fun abstract_over (v, body) = |
755 |
let |
|
16990 | 756 |
fun abs lev tm = |
757 |
if v aconv tm then Bound lev |
|
16882 | 758 |
else |
16990 | 759 |
(case tm of |
760 |
Abs (a, T, t) => Abs (a, T, abs (lev + 1) t) |
|
32020 | 761 |
| t $ u => |
762 |
(abs lev t $ (abs lev u handle Same.SAME => u) |
|
763 |
handle Same.SAME => t $ abs lev u) |
|
764 |
| _ => raise Same.SAME); |
|
765 |
in abs 0 body handle Same.SAME => body end; |
|
0 | 766 |
|
32198 | 767 |
fun term_name (Const (x, _)) = Long_Name.base_name x |
768 |
| term_name (Free (x, _)) = x |
|
769 |
| term_name (Var ((x, _), _)) = x |
|
770 |
| term_name _ = Name.uu; |
|
771 |
||
60404
422b63ef0147
clarified abstracted term bindings (again, see c8384ff11711);
wenzelm
parents:
60311
diff
changeset
|
772 |
fun dependent_lambda_name (x, v) t = |
422b63ef0147
clarified abstracted term bindings (again, see c8384ff11711);
wenzelm
parents:
60311
diff
changeset
|
773 |
let val t' = abstract_over (v, t) |
422b63ef0147
clarified abstracted term bindings (again, see c8384ff11711);
wenzelm
parents:
60311
diff
changeset
|
774 |
in if is_dependent t' then Abs (if x = "" then term_name v else x, fastype_of v, t') else t end; |
422b63ef0147
clarified abstracted term bindings (again, see c8384ff11711);
wenzelm
parents:
60311
diff
changeset
|
775 |
|
32198 | 776 |
fun lambda_name (x, v) t = |
777 |
Abs (if x = "" then term_name v else x, fastype_of v, abstract_over (v, t)); |
|
778 |
||
779 |
fun lambda v t = lambda_name ("", v) t; |
|
0 | 780 |
|
44241 | 781 |
fun absfree (a, T) body = Abs (a, T, abstract_over (Free (a, T), body)); |
782 |
fun absdummy T body = Abs (Name.uu_, T, body); |
|
0 | 783 |
|
16678 | 784 |
(*Replace the ATOMIC term ti by ui; inst = [(t1,u1), ..., (tn,un)]. |
0 | 785 |
A simultaneous substitution: [ (a,b), (b,a) ] swaps a and b. *) |
16678 | 786 |
fun subst_atomic [] tm = tm |
787 |
| subst_atomic inst tm = |
|
788 |
let |
|
789 |
fun subst (Abs (a, T, body)) = Abs (a, T, subst body) |
|
790 |
| subst (t $ u) = subst t $ subst u |
|
18942 | 791 |
| subst t = the_default t (AList.lookup (op aconv) inst t); |
16678 | 792 |
in subst tm end; |
0 | 793 |
|
16678 | 794 |
(*Replace the ATOMIC type Ti by Ui; inst = [(T1,U1), ..., (Tn,Un)].*) |
795 |
fun typ_subst_atomic [] ty = ty |
|
796 |
| typ_subst_atomic inst ty = |
|
797 |
let |
|
798 |
fun subst (Type (a, Ts)) = Type (a, map subst Ts) |
|
18942 | 799 |
| subst T = the_default T (AList.lookup (op = : typ * typ -> bool) inst T); |
16678 | 800 |
in subst ty end; |
15797 | 801 |
|
16678 | 802 |
fun subst_atomic_types [] tm = tm |
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20531
diff
changeset
|
803 |
| subst_atomic_types inst tm = map_types (typ_subst_atomic inst) tm; |
16678 | 804 |
|
805 |
fun typ_subst_TVars [] ty = ty |
|
806 |
| typ_subst_TVars inst ty = |
|
807 |
let |
|
808 |
fun subst (Type (a, Ts)) = Type (a, map subst Ts) |
|
18942 | 809 |
| subst (T as TVar (xi, _)) = the_default T (AList.lookup (op =) inst xi) |
16678 | 810 |
| subst T = T; |
811 |
in subst ty end; |
|
0 | 812 |
|
16678 | 813 |
fun subst_TVars [] tm = tm |
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20531
diff
changeset
|
814 |
| subst_TVars inst tm = map_types (typ_subst_TVars inst) tm; |
0 | 815 |
|
16678 | 816 |
fun subst_Vars [] tm = tm |
817 |
| subst_Vars inst tm = |
|
818 |
let |
|
18942 | 819 |
fun subst (t as Var (xi, _)) = the_default t (AList.lookup (op =) inst xi) |
16678 | 820 |
| subst (Abs (a, T, t)) = Abs (a, T, subst t) |
821 |
| subst (t $ u) = subst t $ subst u |
|
822 |
| subst t = t; |
|
823 |
in subst tm end; |
|
0 | 824 |
|
16678 | 825 |
fun subst_vars ([], []) tm = tm |
826 |
| subst_vars ([], inst) tm = subst_Vars inst tm |
|
827 |
| subst_vars (instT, inst) tm = |
|
828 |
let |
|
829 |
fun subst (Const (a, T)) = Const (a, typ_subst_TVars instT T) |
|
830 |
| subst (Free (a, T)) = Free (a, typ_subst_TVars instT T) |
|
32784 | 831 |
| subst (Var (xi, T)) = |
17271 | 832 |
(case AList.lookup (op =) inst xi of |
16678 | 833 |
NONE => Var (xi, typ_subst_TVars instT T) |
834 |
| SOME t => t) |
|
835 |
| subst (t as Bound _) = t |
|
836 |
| subst (Abs (a, T, t)) = Abs (a, typ_subst_TVars instT T, subst t) |
|
837 |
| subst (t $ u) = subst t $ subst u; |
|
838 |
in subst tm end; |
|
0 | 839 |
|
25050 | 840 |
fun close_schematic_term t = |
841 |
let |
|
56243 | 842 |
val extra_types = map (fn v => Const ("Pure.type", itselfT (TVar v))) (hidden_polymorphism t); |
30285
a135bfab6e83
close_schematic_term: uniform order of types/terms;
wenzelm
parents:
30280
diff
changeset
|
843 |
val extra_terms = map Var (add_vars t []); |
a135bfab6e83
close_schematic_term: uniform order of types/terms;
wenzelm
parents:
30280
diff
changeset
|
844 |
in fold lambda (extra_terms @ extra_types) t end; |
25050 | 845 |
|
846 |
||
0 | 847 |
|
15573 | 848 |
(** Identifying first-order terms **) |
849 |
||
20199 | 850 |
(*Differs from proofterm/is_fun in its treatment of TVar*) |
29256 | 851 |
fun is_funtype (Type ("fun", [_, _])) = true |
20199 | 852 |
| is_funtype _ = false; |
853 |
||
15573 | 854 |
(*Argument Ts is a reverse list of binder types, needed if term t contains Bound vars*) |
29256 | 855 |
fun has_not_funtype Ts t = not (is_funtype (fastype_of1 (Ts, t))); |
15573 | 856 |
|
16537 | 857 |
(*First order means in all terms of the form f(t1,...,tn) no argument has a |
16589 | 858 |
function type. The supplied quantifiers are excluded: their argument always |
859 |
has a function type through a recursive call into its body.*) |
|
16667 | 860 |
fun is_first_order quants = |
16589 | 861 |
let fun first_order1 Ts (Abs (_,T,body)) = first_order1 (T::Ts) body |
16667 | 862 |
| first_order1 Ts (Const(q,_) $ Abs(a,T,body)) = |
20664 | 863 |
member (op =) quants q andalso (*it is a known quantifier*) |
16589 | 864 |
not (is_funtype T) andalso first_order1 (T::Ts) body |
16667 | 865 |
| first_order1 Ts t = |
866 |
case strip_comb t of |
|
867 |
(Var _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts |
|
868 |
| (Free _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts |
|
869 |
| (Const _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts |
|
870 |
| (Bound _, ts) => forall (first_order1 Ts andf has_not_funtype Ts) ts |
|
871 |
| (Abs _, ts) => false (*not in beta-normal form*) |
|
872 |
| _ => error "first_order: unexpected case" |
|
16589 | 873 |
in first_order1 [] end; |
15573 | 874 |
|
16710 | 875 |
|
16990 | 876 |
(* maximum index of typs and terms *) |
0 | 877 |
|
16710 | 878 |
fun maxidx_typ (TVar ((_, j), _)) i = Int.max (i, j) |
879 |
| maxidx_typ (Type (_, Ts)) i = maxidx_typs Ts i |
|
880 |
| maxidx_typ (TFree _) i = i |
|
881 |
and maxidx_typs [] i = i |
|
882 |
| maxidx_typs (T :: Ts) i = maxidx_typs Ts (maxidx_typ T i); |
|
0 | 883 |
|
16710 | 884 |
fun maxidx_term (Var ((_, j), T)) i = maxidx_typ T (Int.max (i, j)) |
885 |
| maxidx_term (Const (_, T)) i = maxidx_typ T i |
|
886 |
| maxidx_term (Free (_, T)) i = maxidx_typ T i |
|
887 |
| maxidx_term (Bound _) i = i |
|
888 |
| maxidx_term (Abs (_, T, t)) i = maxidx_term t (maxidx_typ T i) |
|
889 |
| maxidx_term (t $ u) i = maxidx_term u (maxidx_term t i); |
|
0 | 890 |
|
16710 | 891 |
fun maxidx_of_typ T = maxidx_typ T ~1; |
892 |
fun maxidx_of_typs Ts = maxidx_typs Ts ~1; |
|
893 |
fun maxidx_of_term t = maxidx_term t ~1; |
|
13665 | 894 |
|
0 | 895 |
|
896 |
||
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29269
diff
changeset
|
897 |
(** misc syntax operations **) |
0 | 898 |
|
19909 | 899 |
(* substructure *) |
4017
63878e2587a7
add_typ_classes, add_typ_tycons, add_term_classes, add_term_tycons,
wenzelm
parents:
3965
diff
changeset
|
900 |
|
61248 | 901 |
fun fold_subtypes f = |
902 |
let |
|
903 |
fun iter ty = |
|
904 |
(case ty of Type (_, Ts) => f ty #> fold iter Ts | _ => f ty); |
|
905 |
in iter end; |
|
906 |
||
19909 | 907 |
fun exists_subtype P = |
908 |
let |
|
909 |
fun ex ty = P ty orelse |
|
910 |
(case ty of Type (_, Ts) => exists ex Ts | _ => false); |
|
911 |
in ex end; |
|
13646 | 912 |
|
20531 | 913 |
fun exists_type P = |
914 |
let |
|
915 |
fun ex (Const (_, T)) = P T |
|
916 |
| ex (Free (_, T)) = P T |
|
917 |
| ex (Var (_, T)) = P T |
|
918 |
| ex (Bound _) = false |
|
919 |
| ex (Abs (_, T, t)) = P T orelse ex t |
|
920 |
| ex (t $ u) = ex t orelse ex u; |
|
921 |
in ex end; |
|
922 |
||
16943 | 923 |
fun exists_subterm P = |
924 |
let |
|
925 |
fun ex tm = P tm orelse |
|
926 |
(case tm of |
|
927 |
t $ u => ex t orelse ex u |
|
928 |
| Abs (_, _, t) => ex t |
|
929 |
| _ => false); |
|
930 |
in ex end; |
|
16108
cf468b93a02e
Implement cycle-free overloading, so that definitions cannot harm consistency any more (except of course via interaction with axioms).
obua
parents:
16035
diff
changeset
|
931 |
|
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29269
diff
changeset
|
932 |
fun exists_Const P = exists_subterm (fn Const c => P c | _ => false); |
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29269
diff
changeset
|
933 |
|
63619 | 934 |
|
935 |
(* contraction *) |
|
936 |
||
937 |
fun could_beta_contract (Abs _ $ _) = true |
|
938 |
| could_beta_contract (t $ u) = could_beta_contract t orelse could_beta_contract u |
|
939 |
| could_beta_contract (Abs (_, _, b)) = could_beta_contract b |
|
940 |
| could_beta_contract _ = false; |
|
941 |
||
942 |
fun could_eta_contract (Abs (_, _, _ $ Bound 0)) = true |
|
943 |
| could_eta_contract (Abs (_, _, b)) = could_eta_contract b |
|
944 |
| could_eta_contract (t $ u) = could_eta_contract t orelse could_eta_contract u |
|
945 |
| could_eta_contract _ = false; |
|
946 |
||
947 |
fun could_beta_eta_contract (Abs _ $ _) = true |
|
948 |
| could_beta_eta_contract (Abs (_, _, _ $ Bound 0)) = true |
|
949 |
| could_beta_eta_contract (Abs (_, _, b)) = could_beta_eta_contract b |
|
950 |
| could_beta_eta_contract (t $ u) = could_beta_eta_contract t orelse could_beta_eta_contract u |
|
951 |
| could_beta_eta_contract _ = false; |
|
24671 | 952 |
|
953 |
||
20199 | 954 |
(* dest abstraction *) |
0 | 955 |
|
16678 | 956 |
fun dest_abs (x, T, body) = |
957 |
let |
|
958 |
fun name_clash (Free (y, _)) = (x = y) |
|
959 |
| name_clash (t $ u) = name_clash t orelse name_clash u |
|
960 |
| name_clash (Abs (_, _, t)) = name_clash t |
|
961 |
| name_clash _ = false; |
|
962 |
in |
|
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43278
diff
changeset
|
963 |
if name_clash body then |
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43278
diff
changeset
|
964 |
dest_abs (singleton (Name.variant_list [x]) x, T, body) (*potentially slow*) |
16678 | 965 |
else (x, subst_bound (Free (x, T), body)) |
966 |
end; |
|
967 |
||
20160 | 968 |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
969 |
(* dummy patterns *) |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
970 |
|
56241 | 971 |
fun dummy_pattern T = Const ("Pure.dummy_pattern", T); |
45156 | 972 |
val dummy = dummy_pattern dummyT; |
973 |
val dummy_prop = dummy_pattern propT; |
|
18253 | 974 |
|
56241 | 975 |
fun is_dummy_pattern (Const ("Pure.dummy_pattern", _)) = true |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
976 |
| is_dummy_pattern _ = false; |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
977 |
|
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
978 |
fun no_dummy_patterns tm = |
16787 | 979 |
if not (fold_aterms (fn t => fn b => b orelse is_dummy_pattern t) tm false) then tm |
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
980 |
else raise TERM ("Illegal occurrence of '_' dummy pattern", [tm]); |
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
981 |
|
56241 | 982 |
fun free_dummy_patterns (Const ("Pure.dummy_pattern", T)) used = |
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
983 |
let val [x] = Name.invent used Name.uu 1 |
24733 | 984 |
in (Free (Name.internal x, T), Name.declare x used) end |
985 |
| free_dummy_patterns (Abs (x, T, b)) used = |
|
986 |
let val (b', used') = free_dummy_patterns b used |
|
987 |
in (Abs (x, T, b'), used') end |
|
988 |
| free_dummy_patterns (t $ u) used = |
|
989 |
let |
|
990 |
val (t', used') = free_dummy_patterns t used; |
|
991 |
val (u', used'') = free_dummy_patterns u used'; |
|
992 |
in (t' $ u', used'') end |
|
993 |
| free_dummy_patterns a used = (a, used); |
|
994 |
||
56241 | 995 |
fun replace_dummy Ts (Const ("Pure.dummy_pattern", T)) i = |
33063 | 996 |
(list_comb (Var (("_dummy_", i), Ts ---> T), map_range Bound (length Ts)), i + 1) |
24762 | 997 |
| replace_dummy Ts (Abs (x, T, t)) i = |
998 |
let val (t', i') = replace_dummy (T :: Ts) t i |
|
999 |
in (Abs (x, T, t'), i') end |
|
1000 |
| replace_dummy Ts (t $ u) i = |
|
1001 |
let |
|
1002 |
val (t', i') = replace_dummy Ts t i; |
|
1003 |
val (u', i'') = replace_dummy Ts u i'; |
|
1004 |
in (t' $ u', i'') end |
|
1005 |
| replace_dummy _ a i = (a, i); |
|
11903 | 1006 |
|
1007 |
val replace_dummy_patterns = replace_dummy []; |
|
9536
b79b002f32ae
added dummy_patternN, no_dummy_patterns, replace_dummy_patterns;
wenzelm
parents:
9319
diff
changeset
|
1008 |
|
45156 | 1009 |
fun show_dummy_patterns (Var (("_dummy_", _), T)) = dummy_pattern T |
16035 | 1010 |
| show_dummy_patterns (t $ u) = show_dummy_patterns t $ show_dummy_patterns u |
1011 |
| show_dummy_patterns (Abs (x, T, t)) = Abs (x, T, show_dummy_patterns t) |
|
1012 |
| show_dummy_patterns a = a; |
|
1013 |
||
13484 | 1014 |
|
20100 | 1015 |
(* display variables *) |
1016 |
||
14786
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1017 |
fun string_of_vname (x, i) = |
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1018 |
let |
15986 | 1019 |
val idx = string_of_int i; |
1020 |
val dot = |
|
1021 |
(case rev (Symbol.explode x) of |
|
62529
8b7bdfc09f3b
clarified treatment of fragments of Isabelle symbols during bootstrap;
wenzelm
parents:
61655
diff
changeset
|
1022 |
_ :: "\<^sub>" :: _ => false |
15986 | 1023 |
| c :: _ => Symbol.is_digit c |
1024 |
| _ => true); |
|
14786
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1025 |
in |
38980
af73cf0dc31f
turned show_question_marks into proper configuration option;
wenzelm
parents:
35986
diff
changeset
|
1026 |
if dot then "?" ^ x ^ "." ^ idx |
af73cf0dc31f
turned show_question_marks into proper configuration option;
wenzelm
parents:
35986
diff
changeset
|
1027 |
else if i <> 0 then "?" ^ x ^ idx |
af73cf0dc31f
turned show_question_marks into proper configuration option;
wenzelm
parents:
35986
diff
changeset
|
1028 |
else "?" ^ x |
14786
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1029 |
end; |
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1030 |
|
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1031 |
fun string_of_vname' (x, ~1) = x |
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1032 |
| string_of_vname' xi = string_of_vname xi; |
24a7bc97a27a
moved some sort ops to sorts.ML; added string_of_vname (from Syntax module);
wenzelm
parents:
14695
diff
changeset
|
1033 |
|
1364
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
1034 |
end; |
8ea1a962ad72
files now define a structure to allow SML/NJ to optimize the code
clasohm
parents:
1029
diff
changeset
|
1035 |
|
40841 | 1036 |
structure Basic_Term: BASIC_TERM = Term; |
1037 |
open Basic_Term; |