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