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