src/Pure/term_subst.ML
author wenzelm
Sun, 12 Mar 2017 18:50:02 +0100
changeset 65202 187277b77d50
parent 45395 830c9b9b0d66
child 67698 67caf783b9ee
permissions -rw-r--r--
suppress vacuous messages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/term_subst.ML
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     3
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
     4
Efficient type/term substitution.
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     5
*)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     6
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     7
signature TERM_SUBST =
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
     8
sig
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
     9
  val map_atypsT_same: typ Same.operation -> typ Same.operation
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    10
  val map_types_same: typ Same.operation -> term Same.operation
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    11
  val map_aterms_same: term Same.operation -> term Same.operation
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
    12
  val generalizeT_same: string list -> int -> typ Same.operation
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
    13
  val generalize_same: string list * string list -> int -> term Same.operation
36766
wenzelm
parents: 36765
diff changeset
    14
  val generalizeT: string list -> int -> typ -> typ
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    15
  val generalize: string list * string list -> int -> term -> term
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    16
  val instantiateT_maxidx: ((indexname * sort) * (typ * int)) list -> typ -> int -> typ * int
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    17
  val instantiate_maxidx:
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    18
    ((indexname * sort) * (typ * int)) list * ((indexname * typ) * (term * int)) list ->
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    19
    term -> int -> term * int
36766
wenzelm
parents: 36765
diff changeset
    20
  val instantiateT_same: ((indexname * sort) * typ) list -> typ Same.operation
wenzelm
parents: 36765
diff changeset
    21
  val instantiate_same: ((indexname * sort) * typ) list * ((indexname * typ) * term) list ->
wenzelm
parents: 36765
diff changeset
    22
    term Same.operation
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
    23
  val instantiateT: ((indexname * sort) * typ) list -> typ -> typ
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    24
  val instantiate: ((indexname * sort) * typ) list * ((indexname * typ) * term) list ->
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    25
    term -> term
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    26
  val zero_var_indexes: term -> term
21607
3698319f6503 zero_var_indexes_inst: multiple terms;
wenzelm
parents: 21315
diff changeset
    27
  val zero_var_indexes_inst: term list ->
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    28
    ((indexname * sort) * typ) list * ((indexname * typ) * term) list
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    29
end;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    30
31977
e03059ae2d82 renamed structure TermSubst to Term_Subst;
wenzelm
parents: 29269
diff changeset
    31
structure Term_Subst: TERM_SUBST =
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    32
struct
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    33
31980
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    34
(* generic mapping *)
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    35
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    36
fun map_atypsT_same f =
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    37
  let
32020
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    38
    fun typ (Type (a, Ts)) = Type (a, Same.map typ Ts)
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    39
      | typ T = f T;
31980
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    40
  in typ end;
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    41
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    42
fun map_types_same f =
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    43
  let
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    44
    fun term (Const (a, T)) = Const (a, f T)
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    45
      | term (Free (a, T)) = Free (a, f T)
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    46
      | term (Var (v, T)) = Var (v, f T)
32020
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    47
      | term (Bound _) = raise Same.SAME
31980
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    48
      | term (Abs (x, T, t)) =
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    49
          (Abs (x, f T, Same.commit term t)
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    50
            handle Same.SAME => Abs (x, T, term t))
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    51
      | term (t $ u) = (term t $ Same.commit term u handle Same.SAME => t $ term u);
31980
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    52
  in term end;
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    53
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    54
fun map_aterms_same f =
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    55
  let
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    56
    fun term (Abs (x, T, t)) = Abs (x, T, term t)
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    57
      | term (t $ u) = (term t $ Same.commit term u handle Same.SAME => t $ term u)
31980
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    58
      | term a = f a;
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    59
  in term end;
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    60
c7c1d545007e added some generic mapping combinators;
wenzelm
parents: 31977
diff changeset
    61
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    62
(* generalization of fixed variables *)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    63
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    64
fun generalizeT_same [] _ _ = raise Same.SAME
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    65
  | generalizeT_same tfrees idx ty =
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    66
      let
32020
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    67
        fun gen (Type (a, Ts)) = Type (a, Same.map gen Ts)
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    68
          | gen (TFree (a, S)) =
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    69
              if member (op =) tfrees a then TVar ((a, idx), S)
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    70
              else raise Same.SAME
32020
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    71
          | gen _ = raise Same.SAME;
9abf5d66606e use structure Same;
wenzelm
parents: 32016
diff changeset
    72
      in gen ty end;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    73
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    74
fun generalize_same ([], []) _ _ = raise Same.SAME
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    75
  | generalize_same (tfrees, frees) idx tm =
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    76
      let
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    77
        val genT = generalizeT_same tfrees idx;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    78
        fun gen (Free (x, T)) =
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    79
              if member (op =) frees x then
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    80
                Var (Name.clean_index (x, idx), Same.commit genT T)
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    81
              else Free (x, genT T)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    82
          | gen (Var (xi, T)) = Var (xi, genT T)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    83
          | gen (Const (c, T)) = Const (c, genT T)
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    84
          | gen (Bound _) = raise Same.SAME
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    85
          | gen (Abs (x, T, t)) =
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    86
              (Abs (x, genT T, Same.commit gen t)
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    87
                handle Same.SAME => Abs (x, T, gen t))
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
    88
          | gen (t $ u) = (gen t $ Same.commit gen u handle Same.SAME => t $ gen u);
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    89
      in gen tm end;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    90
36766
wenzelm
parents: 36765
diff changeset
    91
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
    92
fun generalize names i tm = Same.commit (generalize_same names i) tm;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    93
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    94
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    95
(* instantiation of schematic variables (types before terms) -- recomputes maxidx *)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    96
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    97
local
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    98
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
    99
fun no_index (x, y) = (x, (y, ~1));
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   100
fun no_indexes1 inst = map no_index inst;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   101
fun no_indexes2 (inst1, inst2) = (map no_index inst1, map no_index inst2);
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   102
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   103
fun instT_same maxidx instT ty =
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   104
  let
21184
35baf14cfb6d instantiate: avoid global references;
wenzelm
parents: 20513
diff changeset
   105
    fun maxify i = if i > ! maxidx then maxidx := i else ();
35baf14cfb6d instantiate: avoid global references;
wenzelm
parents: 20513
diff changeset
   106
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   107
    fun subst_typ (Type (a, Ts)) = Type (a, subst_typs Ts)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   108
      | subst_typ (TVar ((a, i), S)) =
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   109
          (case AList.lookup Term.eq_tvar instT ((a, i), S) of
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   110
            SOME (T, j) => (maxify j; T)
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   111
          | NONE => (maxify i; raise Same.SAME))
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   112
      | subst_typ _ = raise Same.SAME
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   113
    and subst_typs (T :: Ts) =
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   114
        (subst_typ T :: Same.commit subst_typs Ts
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   115
          handle Same.SAME => T :: subst_typs Ts)
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   116
      | subst_typs [] = raise Same.SAME;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   117
  in subst_typ ty end;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   118
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   119
fun inst_same maxidx (instT, inst) tm =
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   120
  let
21184
35baf14cfb6d instantiate: avoid global references;
wenzelm
parents: 20513
diff changeset
   121
    fun maxify i = if i > ! maxidx then maxidx := i else ();
35baf14cfb6d instantiate: avoid global references;
wenzelm
parents: 20513
diff changeset
   122
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   123
    val substT = instT_same maxidx instT;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   124
    fun subst (Const (c, T)) = Const (c, substT T)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   125
      | subst (Free (x, T)) = Free (x, substT T)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   126
      | subst (Var ((x, i), T)) =
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   127
          let val (T', same) = (substT T, false) handle Same.SAME => (T, true) in
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   128
            (case AList.lookup Term.eq_var inst ((x, i), T') of
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   129
               SOME (t, j) => (maxify j; t)
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   130
             | NONE => (maxify i; if same then raise Same.SAME else Var ((x, i), T')))
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   131
          end
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   132
      | subst (Bound _) = raise Same.SAME
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   133
      | subst (Abs (x, T, t)) =
32016
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   134
          (Abs (x, substT T, Same.commit subst t)
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   135
            handle Same.SAME => Abs (x, T, subst t))
597b3b69b8d8 use structure Same;
wenzelm
parents: 31980
diff changeset
   136
      | subst (t $ u) = (subst t $ Same.commit subst u handle Same.SAME => t $ subst u);
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   137
  in subst tm end;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   138
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   139
in
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   140
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   141
fun instantiateT_maxidx instT ty i =
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32020
diff changeset
   142
  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
   143
  in (Same.commit (instT_same maxidx instT) ty, ! maxidx) end;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   144
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   145
fun instantiate_maxidx insts tm i =
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32020
diff changeset
   146
  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
   147
  in (Same.commit (inst_same maxidx insts) tm, ! maxidx) end;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   148
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   149
fun instantiateT_same [] _ = raise Same.SAME
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   150
  | instantiateT_same instT ty = instT_same (Unsynchronized.ref ~1) (no_indexes1 instT) ty;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   151
36620
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   152
fun instantiate_same ([], []) _ = raise Same.SAME
e6bb250402b5 simplified/unified fundamental operations on types/terms/proofterms -- prefer Same.operation over "option" variant;
wenzelm
parents: 35408
diff changeset
   153
  | instantiate_same insts tm = inst_same (Unsynchronized.ref ~1) (no_indexes2 insts) tm;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   154
36766
wenzelm
parents: 36765
diff changeset
   155
fun instantiateT instT ty = Same.commit (instantiateT_same instT) ty;
wenzelm
parents: 36765
diff changeset
   156
fun instantiate inst tm = Same.commit (instantiate_same inst) tm;
wenzelm
parents: 36765
diff changeset
   157
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   158
end;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   159
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   160
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   161
(* zero var indexes *)
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   162
45395
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   163
structure TVars = Table(type key = indexname * sort val ord = Term_Ord.tvar_ord);
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   164
structure Vars = Table(type key = indexname * typ val ord = Term_Ord.var_ord);
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   165
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   166
fun zero_var_inst mk (v as ((x, i), X)) (inst, used) =
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   167
  let
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   168
    val (x', used') = Name.variant (if Name.is_bound x then "u" else x) used;
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   169
  in if x = x' andalso i = 0 then (inst, used') else ((v, mk ((x', 0), X)) :: inst, used') end;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   170
21607
3698319f6503 zero_var_indexes_inst: multiple terms;
wenzelm
parents: 21315
diff changeset
   171
fun zero_var_indexes_inst ts =
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   172
  let
45395
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   173
    val (instT, _) =
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   174
      TVars.fold (zero_var_inst TVar o #1)
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   175
        ((fold o fold_types o fold_atyps) (fn TVar v =>
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   176
          TVars.insert (K true) (v, ()) | _ => I) ts TVars.empty)
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   177
        ([], Name.context);
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   178
    val (inst, _) =
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   179
      Vars.fold (zero_var_inst Var o #1)
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   180
        ((fold o fold_aterms) (fn Var (xi, T) =>
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   181
          Vars.insert (K true) ((xi, instantiateT instT T), ()) | _ => I) ts Vars.empty)
830c9b9b0d66 more scalable zero_var_indexes, depending on canonical order within table;
wenzelm
parents: 43326
diff changeset
   182
        ([], Name.context);
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   183
  in (instT, inst) end;
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   184
21607
3698319f6503 zero_var_indexes_inst: multiple terms;
wenzelm
parents: 21315
diff changeset
   185
fun zero_var_indexes t = instantiate (zero_var_indexes_inst [t]) t;
20513
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   186
4294eb252283 Efficient term substitution -- avoids copying.
wenzelm
parents:
diff changeset
   187
end;