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