author | wenzelm |
Sat, 04 Sep 2021 13:49:26 +0200 | |
changeset 74227 | fdcc7e8f95ea |
parent 74222 | bf595bfbdc98 |
child 74232 | 1091880266e5 |
permissions | -rw-r--r-- |
20513 | 1 |
(* Title: Pure/term_subst.ML |
2 |
Author: Makarius |
|
3 |
||
32016 | 4 |
Efficient type/term substitution. |
20513 | 5 |
*) |
6 |
||
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
7 |
signature INST_TABLE = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
8 |
sig |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
9 |
include TABLE |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
10 |
val add: key * 'a -> 'a table -> 'a table |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
11 |
val table: (key * 'a) list -> 'a table |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
12 |
end; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
13 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
14 |
functor Inst_Table(Key: KEY): INST_TABLE = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
15 |
struct |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
16 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
17 |
structure Tab = Table(Key); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
18 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
19 |
fun add entry = Tab.insert (K true) entry; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
20 |
fun table entries = fold add entries Tab.empty; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
21 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
22 |
open Tab; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
23 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
24 |
end; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
25 |
|
20513 | 26 |
signature TERM_SUBST = |
27 |
sig |
|
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
28 |
structure TFrees: INST_TABLE |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
29 |
structure TVars: INST_TABLE |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
30 |
structure Frees: INST_TABLE |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
31 |
structure Vars: INST_TABLE |
74227 | 32 |
val add_tfreesT: typ -> TFrees.set -> TFrees.set |
33 |
val add_tfrees: term -> TFrees.set -> TFrees.set |
|
34 |
val add_tvarsT: typ -> TVars.set -> TVars.set |
|
35 |
val add_tvars: term -> TVars.set -> TVars.set |
|
36 |
val add_frees: term -> Frees.set -> Frees.set |
|
37 |
val add_vars: term -> Vars.set -> Vars.set |
|
38 |
val add_tfree_namesT: typ -> Symtab.set -> Symtab.set |
|
39 |
val add_tfree_names: term -> Symtab.set -> Symtab.set |
|
40 |
val add_free_names: term -> Symtab.set -> Symtab.set |
|
32016 | 41 |
val map_atypsT_same: typ Same.operation -> typ Same.operation |
42 |
val map_types_same: typ Same.operation -> term Same.operation |
|
43 |
val map_aterms_same: term Same.operation -> term Same.operation |
|
74200
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
44 |
val generalizeT_same: Symtab.set -> int -> typ Same.operation |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
45 |
val generalize_same: Symtab.set * Symtab.set -> int -> term Same.operation |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
46 |
val generalizeT: Symtab.set -> int -> typ -> typ |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
47 |
val generalize: Symtab.set * Symtab.set -> int -> term -> term |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
48 |
val instantiateT_maxidx: (typ * int) TVars.table -> typ -> int -> typ * int |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
49 |
val instantiate_maxidx: (typ * int) TVars.table * (term * int) Vars.table -> |
20513 | 50 |
term -> int -> term * int |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
51 |
val instantiateT_frees_same: typ TFrees.table -> typ Same.operation |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
52 |
val instantiate_frees_same: typ TFrees.table * term Frees.table -> term Same.operation |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
53 |
val instantiateT_frees: typ TFrees.table -> typ -> typ |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
54 |
val instantiate_frees: typ TFrees.table * term Frees.table -> term -> term |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
55 |
val instantiateT_same: typ TVars.table -> typ Same.operation |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
56 |
val instantiate_same: typ TVars.table * term Vars.table -> term Same.operation |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
57 |
val instantiateT: typ TVars.table -> typ -> typ |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
58 |
val instantiate: typ TVars.table * term Vars.table -> term -> term |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
59 |
val zero_var_indexes_inst: Name.context -> term list -> typ TVars.table * term Vars.table |
68234 | 60 |
val zero_var_indexes: term -> term |
61 |
val zero_var_indexes_list: term list -> term list |
|
20513 | 62 |
end; |
63 |
||
31977 | 64 |
structure Term_Subst: TERM_SUBST = |
20513 | 65 |
struct |
66 |
||
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
67 |
(* instantiation as table *) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
68 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
69 |
structure TFrees = Inst_Table( |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
70 |
type key = string * sort |
74222 | 71 |
val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.sort_ord) |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
72 |
); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
73 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
74 |
structure TVars = Inst_Table( |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
75 |
type key = indexname * sort |
74222 | 76 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.sort_ord) |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
77 |
); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
78 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
79 |
structure Frees = Inst_Table( |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
80 |
type key = string * typ |
74222 | 81 |
val ord = pointer_eq_ord (prod_ord fast_string_ord Term_Ord.typ_ord) |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
82 |
); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
83 |
|
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
84 |
structure Vars = Inst_Table( |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
85 |
type key = indexname * typ |
74222 | 86 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord Term_Ord.typ_ord) |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
87 |
); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
88 |
|
74227 | 89 |
val add_tfreesT = fold_atyps (fn TFree v => TFrees.add (v, ()) | _ => I); |
90 |
val add_tfrees = fold_types add_tfreesT; |
|
91 |
val add_tvarsT = fold_atyps (fn TVar v => TVars.add (v, ()) | _ => I); |
|
92 |
val add_tvars = fold_types add_tvarsT; |
|
93 |
val add_frees = fold_aterms (fn Free v => Frees.add (v, ()) | _ => I); |
|
94 |
val add_vars = fold_aterms (fn Var v => Vars.add (v, ()) | _ => I); |
|
95 |
val add_tfree_namesT = fold_atyps (fn TFree (a, _) => Symtab.insert_set a | _ => I); |
|
96 |
val add_tfree_names = fold_types add_tfree_namesT; |
|
97 |
val add_free_names = fold_aterms (fn Free (x, _) => Symtab.insert_set x | _ => I); |
|
98 |
||
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
99 |
|
31980 | 100 |
(* generic mapping *) |
101 |
||
102 |
fun map_atypsT_same f = |
|
103 |
let |
|
32020 | 104 |
fun typ (Type (a, Ts)) = Type (a, Same.map typ Ts) |
105 |
| typ T = f T; |
|
31980 | 106 |
in typ end; |
107 |
||
108 |
fun map_types_same f = |
|
109 |
let |
|
110 |
fun term (Const (a, T)) = Const (a, f T) |
|
111 |
| term (Free (a, T)) = Free (a, f T) |
|
112 |
| term (Var (v, T)) = Var (v, f T) |
|
32020 | 113 |
| term (Bound _) = raise Same.SAME |
31980 | 114 |
| term (Abs (x, T, t)) = |
32016 | 115 |
(Abs (x, f T, Same.commit term t) |
116 |
handle Same.SAME => Abs (x, T, term t)) |
|
117 |
| term (t $ u) = (term t $ Same.commit term u handle Same.SAME => t $ term u); |
|
31980 | 118 |
in term end; |
119 |
||
120 |
fun map_aterms_same f = |
|
121 |
let |
|
122 |
fun term (Abs (x, T, t)) = Abs (x, T, term t) |
|
32016 | 123 |
| term (t $ u) = (term t $ Same.commit term u handle Same.SAME => t $ term u) |
31980 | 124 |
| term a = f a; |
125 |
in term end; |
|
126 |
||
127 |
||
20513 | 128 |
(* generalization of fixed variables *) |
129 |
||
74200
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
130 |
fun generalizeT_same tfrees idx ty = |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
131 |
if Symtab.is_empty tfrees then raise Same.SAME |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
132 |
else |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
133 |
let |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
134 |
fun gen (Type (a, Ts)) = Type (a, Same.map gen Ts) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
135 |
| gen (TFree (a, S)) = |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
136 |
if Symtab.defined tfrees a then TVar ((a, idx), S) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
137 |
else raise Same.SAME |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
138 |
| gen _ = raise Same.SAME; |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
139 |
in gen ty end; |
20513 | 140 |
|
74200
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
141 |
fun generalize_same (tfrees, frees) idx tm = |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
142 |
if Symtab.is_empty tfrees andalso Symtab.is_empty frees then raise Same.SAME |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
143 |
else |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
144 |
let |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
145 |
val genT = generalizeT_same tfrees idx; |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
146 |
fun gen (Free (x, T)) = |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
147 |
if Symtab.defined frees x then |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
148 |
Var (Name.clean_index (x, idx), Same.commit genT T) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
149 |
else Free (x, genT T) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
150 |
| gen (Var (xi, T)) = Var (xi, genT T) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
151 |
| gen (Const (c, T)) = Const (c, genT T) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
152 |
| gen (Bound _) = raise Same.SAME |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
153 |
| gen (Abs (x, T, t)) = |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
154 |
(Abs (x, genT T, Same.commit gen t) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
155 |
handle Same.SAME => Abs (x, T, gen t)) |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
156 |
| gen (t $ u) = (gen t $ Same.commit gen u handle Same.SAME => t $ gen u); |
17090e27aae9
more scalable data structure (but: rarely used with > 5 arguments);
wenzelm
parents:
69023
diff
changeset
|
157 |
in gen tm end; |
20513 | 158 |
|
36766 | 159 |
fun generalizeT names i ty = Same.commit (generalizeT_same names i) ty; |
36620
e6bb250402b5
simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents:
35408
diff
changeset
|
160 |
fun generalize names i tm = Same.commit (generalize_same names i) tm; |
20513 | 161 |
|
162 |
||
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
163 |
(* instantiation of free variables (types before terms) *) |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
164 |
|
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
165 |
fun instantiateT_frees_same instT ty = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
166 |
if TFrees.is_empty instT then raise Same.SAME |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
167 |
else |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
168 |
let |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
169 |
fun subst (Type (a, Ts)) = Type (a, Same.map subst Ts) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
170 |
| subst (TFree v) = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
171 |
(case TFrees.lookup instT v of |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
172 |
SOME T => T |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
173 |
| NONE => raise Same.SAME) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
174 |
| subst _ = raise Same.SAME; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
175 |
in subst ty end; |
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
176 |
|
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
177 |
fun instantiate_frees_same (instT, inst) tm = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
178 |
if TFrees.is_empty instT andalso Frees.is_empty inst then raise Same.SAME |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
179 |
else |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
180 |
let |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
181 |
val substT = instantiateT_frees_same instT; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
182 |
fun subst (Const (c, T)) = Const (c, substT T) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
183 |
| subst (Free (x, T)) = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
184 |
let val (T', same) = (substT T, false) handle Same.SAME => (T, true) in |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
185 |
(case Frees.lookup inst (x, T') of |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
186 |
SOME t => t |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
187 |
| NONE => if same then raise Same.SAME else Free (x, T')) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
188 |
end |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
189 |
| subst (Var (xi, T)) = Var (xi, substT T) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
190 |
| subst (Bound _) = raise Same.SAME |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
191 |
| subst (Abs (x, T, t)) = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
192 |
(Abs (x, substT T, Same.commit subst t) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
193 |
handle Same.SAME => Abs (x, T, subst t)) |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
194 |
| subst (t $ u) = (subst t $ Same.commit subst u handle Same.SAME => t $ subst u); |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
195 |
in subst tm end; |
67698
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
196 |
|
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
197 |
fun instantiateT_frees instT ty = Same.commit (instantiateT_frees_same instT) ty; |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
198 |
fun instantiate_frees inst tm = Same.commit (instantiate_frees_same inst) tm; |
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
199 |
|
67caf783b9ee
explicit operations to instantiate frees: typ, term, thm, morphism;
wenzelm
parents:
45395
diff
changeset
|
200 |
|
20513 | 201 |
(* instantiation of schematic variables (types before terms) -- recomputes maxidx *) |
202 |
||
203 |
local |
|
204 |
||
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
205 |
fun no_indexesT instT = TVars.map (fn _ => rpair ~1) instT; |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
206 |
fun no_indexes inst = Vars.map (fn _ => rpair ~1) inst; |
20513 | 207 |
|
36620
e6bb250402b5
simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents:
35408
diff
changeset
|
208 |
fun instT_same maxidx instT ty = |
20513 | 209 |
let |
21184 | 210 |
fun maxify i = if i > ! maxidx then maxidx := i else (); |
211 |
||
20513 | 212 |
fun subst_typ (Type (a, Ts)) = Type (a, subst_typs Ts) |
213 |
| subst_typ (TVar ((a, i), S)) = |
|
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
214 |
(case TVars.lookup instT ((a, i), S) of |
20513 | 215 |
SOME (T, j) => (maxify j; T) |
32016 | 216 |
| NONE => (maxify i; raise Same.SAME)) |
217 |
| subst_typ _ = raise Same.SAME |
|
20513 | 218 |
and subst_typs (T :: Ts) = |
32016 | 219 |
(subst_typ T :: Same.commit subst_typs Ts |
220 |
handle Same.SAME => T :: subst_typs Ts) |
|
221 |
| subst_typs [] = raise Same.SAME; |
|
20513 | 222 |
in subst_typ ty end; |
223 |
||
36620
e6bb250402b5
simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents:
35408
diff
changeset
|
224 |
fun inst_same maxidx (instT, inst) tm = |
20513 | 225 |
let |
21184 | 226 |
fun maxify i = if i > ! maxidx then maxidx := i else (); |
227 |
||
36620
e6bb250402b5
simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents:
35408
diff
changeset
|
228 |
val substT = instT_same maxidx instT; |
20513 | 229 |
fun subst (Const (c, T)) = Const (c, substT T) |
230 |
| subst (Free (x, T)) = Free (x, substT T) |
|
231 |
| subst (Var ((x, i), T)) = |
|
32016 | 232 |
let val (T', same) = (substT T, false) handle Same.SAME => (T, true) in |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
233 |
(case Vars.lookup inst ((x, i), T') of |
20513 | 234 |
SOME (t, j) => (maxify j; t) |
32016 | 235 |
| NONE => (maxify i; if same then raise Same.SAME else Var ((x, i), T'))) |
20513 | 236 |
end |
32016 | 237 |
| subst (Bound _) = raise Same.SAME |
20513 | 238 |
| subst (Abs (x, T, t)) = |
32016 | 239 |
(Abs (x, substT T, Same.commit subst t) |
240 |
handle Same.SAME => Abs (x, T, subst t)) |
|
241 |
| subst (t $ u) = (subst t $ Same.commit subst u handle Same.SAME => t $ subst u); |
|
20513 | 242 |
in subst tm end; |
243 |
||
244 |
in |
|
245 |
||
246 |
fun instantiateT_maxidx instT ty i = |
|
32738 | 247 |
let val maxidx = Unsynchronized.ref i |
36620
e6bb250402b5
simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents:
35408
diff
changeset
|
248 |
in (Same.commit (instT_same maxidx instT) ty, ! maxidx) end; |
20513 | 249 |
|
250 |
fun instantiate_maxidx insts tm i = |
|
32738 | 251 |
let val maxidx = Unsynchronized.ref i |
36620
e6bb250402b5
simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents:
35408
diff
changeset
|
252 |
in (Same.commit (inst_same maxidx insts) tm, ! maxidx) end; |
20513 | 253 |
|
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
254 |
fun instantiateT_same instT ty = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
255 |
if TVars.is_empty instT then raise Same.SAME |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
256 |
else instT_same (Unsynchronized.ref ~1) (no_indexesT instT) ty; |
20513 | 257 |
|
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
258 |
fun instantiate_same (instT, inst) tm = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
259 |
if TVars.is_empty instT andalso Vars.is_empty inst then raise Same.SAME |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
260 |
else inst_same (Unsynchronized.ref ~1) (no_indexesT instT, no_indexes inst) tm; |
20513 | 261 |
|
36766 | 262 |
fun instantiateT instT ty = Same.commit (instantiateT_same instT) ty; |
263 |
fun instantiate inst tm = Same.commit (instantiate_same inst) tm; |
|
264 |
||
20513 | 265 |
end; |
266 |
||
267 |
||
268 |
(* zero var indexes *) |
|
269 |
||
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
270 |
fun zero_var_inst ins mk (v as ((x, i), X)) (inst, used) = |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
271 |
let val (x', used') = Name.variant (if Name.is_bound x then "u" else x) used |
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
272 |
in if x = x' andalso i = 0 then (inst, used') else (ins (v, mk ((x', 0), X)) inst, used') end; |
20513 | 273 |
|
69023
cef000855cf4
clarified standardization of variables, with proper treatment of local variables;
wenzelm
parents:
68234
diff
changeset
|
274 |
fun zero_var_indexes_inst used ts = |
20513 | 275 |
let |
45395
830c9b9b0d66
more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents:
43326
diff
changeset
|
276 |
val (instT, _) = |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
277 |
(TVars.empty, used) |> TVars.fold (zero_var_inst TVars.add TVar o #1) |
45395
830c9b9b0d66
more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents:
43326
diff
changeset
|
278 |
((fold o fold_types o fold_atyps) (fn TVar v => |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
279 |
TVars.add (v, ()) | _ => I) ts TVars.empty); |
45395
830c9b9b0d66
more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents:
43326
diff
changeset
|
280 |
val (inst, _) = |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
281 |
(Vars.empty, used) |> Vars.fold (zero_var_inst Vars.add Var o #1) |
45395
830c9b9b0d66
more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents:
43326
diff
changeset
|
282 |
((fold o fold_aterms) (fn Var (xi, T) => |
74220
c49134ee16c1
more scalable data structure (but: rarely used many arguments);
wenzelm
parents:
74200
diff
changeset
|
283 |
Vars.add ((xi, instantiateT instT T), ()) | _ => I) ts Vars.empty); |
20513 | 284 |
in (instT, inst) end; |
285 |
||
69023
cef000855cf4
clarified standardization of variables, with proper treatment of local variables;
wenzelm
parents:
68234
diff
changeset
|
286 |
fun zero_var_indexes_list ts = map (instantiate (zero_var_indexes_inst Name.context ts)) ts; |
68234 | 287 |
val zero_var_indexes = singleton zero_var_indexes_list; |
20513 | 288 |
|
289 |
end; |