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