src/Pure/envir.ML
author wenzelm
Wed, 14 Sep 2022 23:09:02 +0200
changeset 76158 0302bdf63a08
parent 74575 ccf599864beb
child 77730 4a174bea55e2
permissions -rw-r--r--
build both arm64-darwin and x86_64-darwin on Apple ARM hardware; tuned messages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/envir.ML
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
     3
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
     4
Free-form environments.  The type of a term variable / sort of a type variable is
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
     5
part of its name.  The lookup function must apply type substitutions,
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
     6
since they may change the identity of a variable.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
     9
signature ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
sig
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
    11
  type tenv = (typ * term) Vartab.table
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    12
  datatype env = Envir of {maxidx: int, tenv: tenv, tyenv: Type.tyenv}
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    13
  val maxidx_of: env -> int
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    14
  val term_env: env -> tenv
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    15
  val type_env: env -> Type.tyenv
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    16
  val is_empty: env -> bool
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    17
  val empty: int -> env
63615
wenzelm
parents: 58949
diff changeset
    18
  val init: env
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    19
  val merge: env * env -> env
26638
1d5d42d8fd66 added insert_sorts (from thm.ML);
wenzelm
parents: 26328
diff changeset
    20
  val insert_sorts: env -> sort list -> sort list
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    21
  val genvars: string -> env * typ list -> env * term list
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    22
  val genvar: string -> env * typ -> env * term
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
    23
  val lookup1: tenv -> indexname * typ -> term option
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
    24
  val lookup: env -> indexname * typ -> term option
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
    25
  val update: (indexname * typ) * term -> env -> env
19861
620d90091788 tuned Seq/Envir/Unify interfaces;
wenzelm
parents: 19422
diff changeset
    26
  val above: env -> int -> bool
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
    27
  val vupdate: (indexname * typ) * term -> env -> env
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
    28
  val norm_type_same: Type.tyenv -> typ Same.operation
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
    29
  val norm_types_same: Type.tyenv -> typ list Same.operation
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    30
  val norm_type: Type.tyenv -> typ -> typ
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
    31
  val norm_term_same: env -> term Same.operation
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
    32
  val norm_term: env -> term -> term
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    33
  val beta_norm: term -> term
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
    34
  val head_norm: env -> term -> term
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
    35
  val eta_long: typ list -> term -> term
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
    36
  val eta_contract: term -> term
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
    37
  val beta_eta_contract: term -> term
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
    38
  val aeconv: term * term -> bool
52221
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
    39
  val body_type: env -> typ -> typ
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
    40
  val binder_types: env -> typ -> typ list
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
    41
  val strip_type: env -> typ -> typ list * typ
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
    42
  val fastype: env -> typ list -> term -> typ
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
    43
  val subst_type_same: Type.tyenv -> typ Same.operation
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
    44
  val subst_term_types_same: Type.tyenv -> term Same.operation
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
    45
  val subst_term_same: Type.tyenv * tenv -> term Same.operation
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
    46
  val subst_type: Type.tyenv -> typ -> typ
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
    47
  val subst_term_types: Type.tyenv -> term -> term
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
    48
  val subst_term: Type.tyenv * tenv -> term -> term
19422
bba26da0f227 expand_atom: Type.raw_match;
wenzelm
parents: 18937
diff changeset
    49
  val expand_atom: typ -> typ * term -> term
21695
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
    50
  val expand_term: (term -> (typ * term) option) -> term -> term
74575
ccf599864beb clarified signature -- avoid clones;
wenzelm
parents: 74232
diff changeset
    51
  val expand_term_defs: (term -> string * typ) -> ((string * typ) * term) list -> term -> term
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    54
structure Envir: ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    57
(** datatype env **)
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    58
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    59
(*Updating can destroy environment in 2 ways!
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    60
   (1) variables out of range
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    61
   (2) circular assignments
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
*)
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    63
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
    64
type tenv = (typ * term) Vartab.table;
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    65
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
datatype env = Envir of
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    67
 {maxidx: int,          (*upper bound of maximum index of vars*)
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    68
  tenv: tenv,           (*assignments to Vars*)
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    69
  tyenv: Type.tyenv};   (*assignments to TVars*)
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    70
32796
2e4485b9a39f eliminated dead code;
wenzelm
parents: 32648
diff changeset
    71
fun make_env (maxidx, tenv, tyenv) =
2e4485b9a39f eliminated dead code;
wenzelm
parents: 32648
diff changeset
    72
  Envir {maxidx = maxidx, tenv = tenv, tyenv = tyenv};
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    73
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    74
fun maxidx_of (Envir {maxidx, ...}) = maxidx;
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    75
fun term_env (Envir {tenv, ...}) = tenv;
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    76
fun type_env (Envir {tyenv, ...}) = tyenv;
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    77
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    78
fun is_empty env =
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    79
  Vartab.is_empty (term_env env) andalso
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    80
  Vartab.is_empty (type_env env);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    82
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    83
(* build env *)
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    84
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    85
fun empty maxidx = make_env (maxidx, Vartab.empty, Vartab.empty);
63615
wenzelm
parents: 58949
diff changeset
    86
val init = empty ~1;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    87
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    88
fun merge
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    89
   (Envir {maxidx = maxidx1, tenv = tenv1, tyenv = tyenv1},
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    90
    Envir {maxidx = maxidx2, tenv = tenv2, tyenv = tyenv2}) =
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    91
  make_env (Int.max (maxidx1, maxidx2),
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    92
    Vartab.merge (op =) (tenv1, tenv2),
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    93
    Vartab.merge (op =) (tyenv1, tyenv2));
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    94
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    95
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
    96
(*NB: type unification may invent new sorts*)  (* FIXME tenv!? *)
26638
1d5d42d8fd66 added insert_sorts (from thm.ML);
wenzelm
parents: 26328
diff changeset
    97
val insert_sorts = Vartab.fold (fn (_, (_, T)) => Sorts.insert_typ T) o type_env;
1d5d42d8fd66 added insert_sorts (from thm.ML);
wenzelm
parents: 26328
diff changeset
    98
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
(*Generate a list of distinct variables.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
  Increments index to make them distinct from ALL present variables. *)
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   101
fun genvars name (Envir {maxidx, tenv, tyenv}, Ts) : env * term list =
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   102
  let
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   103
    fun genvs (_, [] : typ list) : term list = []
63618
wenzelm
parents: 63616
diff changeset
   104
      | genvs (_, [T]) = [Var ((name, maxidx + 1), T)]
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   105
      | genvs (n, T :: Ts) =
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   106
          Var ((name ^ radixstring (26, "a" , n), maxidx + 1), T)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   107
            :: genvs (n + 1, Ts);
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   108
  in (Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv}, genvs (0, Ts)) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   109
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   110
(*Generate a variable.*)
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   111
fun genvar name (env, T) : env * term =
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   112
  let val (env', [v]) = genvars name (env, [T])
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   113
  in (env', v) end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   114
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   115
fun var_clash xi T T' =
51701
1e29891759c4 tuned exceptions -- avoid composing error messages in low-level situations;
wenzelm
parents: 51700
diff changeset
   116
  raise TYPE ("Variable has two distinct types", [], [Var (xi, T'), Var (xi, T)]);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   117
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   118
fun lookup_check check tenv (xi, T) =
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   119
  (case Vartab.lookup tenv xi of
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   120
    NONE => NONE
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   121
  | SOME (U, t) => if check (T, U) then SOME t else var_clash xi T U);
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   122
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   123
(*When dealing with environments produced by matching instead
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   124
  of unification, there is no need to chase assigned TVars.
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   125
  In this case, we can simply ignore the type substitution
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   126
  and use = instead of eq_type.*)
51707
21d7933de1eb make SML/NJ happy;
wenzelm
parents: 51701
diff changeset
   127
fun lookup1 tenv = lookup_check (op =) tenv;
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   128
58949
e9559088ba29 clarified name of Type.unified, to emphasize its connection to the "unify" family;
wenzelm
parents: 58945
diff changeset
   129
fun lookup (Envir {tenv, tyenv, ...}) = lookup_check (Type.unified tyenv) tenv;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   130
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   131
fun update ((xi, T), t) (Envir {maxidx, tenv, tyenv}) =
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   132
  Envir {maxidx = maxidx, tenv = Vartab.update_new (xi, (T, t)) tenv, tyenv = tyenv};
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
   133
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
   134
(*Determine if the least index updated exceeds lim*)
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   135
fun above (Envir {tenv, tyenv, ...}) lim =
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 51707
diff changeset
   136
  (case Vartab.min tenv of SOME ((_, i), _) => i > lim | NONE => true) andalso
156e12d5cb92 tuned signature;
wenzelm
parents: 51707
diff changeset
   137
  (case Vartab.min tyenv of SOME ((_, i), _) => i > lim | NONE => true);
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
   138
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   139
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
63618
wenzelm
parents: 63616
diff changeset
   140
fun vupdate ((a, U), t) env =
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   141
  (case t of
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   142
    Var (nT as (name', T)) =>
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   143
      if a = name' then env     (*cycle!*)
35408
b48ab741683b modernized structure Term_Ord;
wenzelm
parents: 32796
diff changeset
   144
      else if Term_Ord.indexname_ord (a, name') = LESS then
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   145
        (case lookup env nT of  (*if already assigned, chase*)
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   146
          NONE => update (nT, Var (a, T)) env
63618
wenzelm
parents: 63616
diff changeset
   147
        | SOME u => vupdate ((a, U), u) env)
wenzelm
parents: 63616
diff changeset
   148
      else update ((a, U), t) env
wenzelm
parents: 63616
diff changeset
   149
  | _ => update ((a, U), t) env);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   151
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   152
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   153
(** beta normalization wrt. environment **)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   154
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   155
(*Chases variables in env.  Does not exploit sharing of variable bindings
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   156
  Does not check types, so could loop.*)
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   157
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   158
local
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   159
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   160
fun norm_type0 tyenv : typ Same.operation =
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   161
  let
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   162
    fun norm (Type (a, Ts)) = Type (a, Same.map norm Ts)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   163
      | norm (TFree _) = raise Same.SAME
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   164
      | norm (TVar v) =
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   165
          (case Type.lookup tyenv v of
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   166
            SOME U => Same.commit norm U
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   167
          | NONE => raise Same.SAME);
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   168
  in norm end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   169
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   170
fun norm_term1 tenv : term Same.operation =
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   171
  let
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   172
    fun norm (Var v) =
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   173
          (case lookup1 tenv v of
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   174
            SOME u => Same.commit norm u
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   175
          | NONE => raise Same.SAME)
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   176
      | norm (Abs (a, T, body)) = Abs (a, T, norm body)
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   177
      | norm (Abs (_, _, body) $ t) = Same.commit norm (subst_bound (t, body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   178
      | norm (f $ t) =
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   179
          ((case norm f of
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   180
             Abs (_, _, body) => Same.commit norm (subst_bound (t, body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   181
           | nf => nf $ Same.commit norm t)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   182
          handle Same.SAME => f $ norm t)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   183
      | norm _ = raise Same.SAME;
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   184
  in norm end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   185
63618
wenzelm
parents: 63616
diff changeset
   186
fun norm_term2 (envir as Envir {tyenv, ...}) : term Same.operation =
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   187
  let
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   188
    val normT = norm_type0 tyenv;
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   189
    fun norm (Const (a, T)) = Const (a, normT T)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   190
      | norm (Free (a, T)) = Free (a, normT T)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   191
      | norm (Var (xi, T)) =
58945
wenzelm
parents: 52221
diff changeset
   192
          (case lookup envir (xi, T) of
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   193
            SOME u => Same.commit norm u
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   194
          | NONE => Var (xi, normT T))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   195
      | norm (Abs (a, T, body)) =
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   196
          (Abs (a, normT T, Same.commit norm body)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   197
            handle Same.SAME => Abs (a, T, norm body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   198
      | norm (Abs (_, _, body) $ t) = Same.commit norm (subst_bound (t, body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   199
      | norm (f $ t) =
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   200
          ((case norm f of
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   201
             Abs (_, _, body) => Same.commit norm (subst_bound (t, body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   202
           | nf => nf $ Same.commit norm t)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   203
          handle Same.SAME => f $ norm t)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   204
      | norm _ = raise Same.SAME;
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   205
  in norm end;
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   206
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   207
in
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   208
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   209
fun norm_type_same tyenv T =
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   210
  if Vartab.is_empty tyenv then raise Same.SAME
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   211
  else norm_type0 tyenv T;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   212
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   213
fun norm_types_same tyenv Ts =
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   214
  if Vartab.is_empty tyenv then raise Same.SAME
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   215
  else Same.map (norm_type0 tyenv) Ts;
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   216
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   217
fun norm_type tyenv T = norm_type_same tyenv T handle Same.SAME => T;
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   218
58945
wenzelm
parents: 52221
diff changeset
   219
fun norm_term_same (envir as Envir {tenv, tyenv, ...}) =
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   220
  if Vartab.is_empty tyenv then norm_term1 tenv
58945
wenzelm
parents: 52221
diff changeset
   221
  else norm_term2 envir;
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   222
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   223
fun norm_term envir t = norm_term_same envir t handle Same.SAME => t;
63619
9c870388e87a more tight filtering;
wenzelm
parents: 63618
diff changeset
   224
9c870388e87a more tight filtering;
wenzelm
parents: 63618
diff changeset
   225
fun beta_norm t =
9c870388e87a more tight filtering;
wenzelm
parents: 63618
diff changeset
   226
  if Term.could_beta_contract t then norm_term init t else t;
719
e3e1d1a6d408 Pure/envir/norm_term: replaced equality test for [] by null
lcp
parents: 247
diff changeset
   227
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   228
end;
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   229
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   230
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   231
(* head normal form for unification *)
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   232
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   233
fun head_norm env =
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   234
  let
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   235
    fun norm (Var v) =
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   236
        (case lookup env v of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   237
          SOME u => head_norm env u
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   238
        | NONE => raise Same.SAME)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   239
      | norm (Abs (a, T, body)) = Abs (a, T, norm body)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   240
      | norm (Abs (_, _, body) $ t) = Same.commit norm (subst_bound (t, body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   241
      | norm (f $ t) =
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   242
          (case norm f of
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   243
            Abs (_, _, body) => Same.commit norm (subst_bound (t, body))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   244
          | nf => nf $ t)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   245
      | norm _ = raise Same.SAME;
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   246
  in Same.commit norm end;
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   247
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   248
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   249
(* eta-long beta-normal form *)
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   250
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   251
fun eta_long Ts (Abs (s, T, t)) = Abs (s, T, eta_long (T :: Ts) t)
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   252
  | eta_long Ts t =
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   253
      (case strip_comb t of
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   254
        (Abs _, _) => eta_long Ts (beta_norm t)
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   255
      | (u, ts) =>
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   256
          let
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   257
            val Us = binder_types (fastype_of1 (Ts, t));
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   258
            val i = length Us;
52132
wenzelm
parents: 52131
diff changeset
   259
            val long = eta_long (rev Us @ Ts);
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   260
          in
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   261
            fold_rev (Term.abs o pair "x") Us
52132
wenzelm
parents: 52131
diff changeset
   262
              (list_comb (incr_boundvars i u,
wenzelm
parents: 52131
diff changeset
   263
                map (long o incr_boundvars i) ts @ map (long o Bound) (i - 1 downto 0)))
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   264
          end);
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   265
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   266
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   267
(* full eta contraction *)
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   268
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   269
local
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   270
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   271
fun decr lev (Bound i) = if i >= lev then Bound (i - 1) else raise Same.SAME
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   272
  | decr lev (Abs (a, T, body)) = Abs (a, T, decr (lev + 1) body)
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   273
  | decr lev (t $ u) = (decr lev t $ decrh lev u handle Same.SAME => t $ decr lev u)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   274
  | decr _ _ = raise Same.SAME
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   275
and decrh lev t = (decr lev t handle Same.SAME => t);
20670
115262dd18e2 tuned eta_contract;
wenzelm
parents: 20548
diff changeset
   276
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   277
fun eta (Abs (a, T, body)) =
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   278
    ((case eta body of
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   279
        body' as (f $ Bound 0) =>
42083
e1209fc7ecdc added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents: 35408
diff changeset
   280
          if Term.is_dependent f then Abs (a, T, body')
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   281
          else decrh 0 f
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   282
     | body' => Abs (a, T, body')) handle Same.SAME =>
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   283
        (case body of
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   284
          f $ Bound 0 =>
42083
e1209fc7ecdc added Term.is_open and Term.is_dependent convenience, to cover common situations of loose bounds;
wenzelm
parents: 35408
diff changeset
   285
            if Term.is_dependent f then raise Same.SAME
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   286
            else decrh 0 f
32018
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   287
        | _ => raise Same.SAME))
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   288
  | eta (t $ u) = (eta t $ Same.commit eta u handle Same.SAME => t $ eta u)
3370cea95387 use structure Same;
wenzelm
parents: 30146
diff changeset
   289
  | eta _ = raise Same.SAME;
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   290
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   291
in
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   292
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   293
fun eta_contract t =
63619
9c870388e87a more tight filtering;
wenzelm
parents: 63618
diff changeset
   294
  if Term.could_eta_contract t then Same.commit eta t else t;
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   295
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   296
end;
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   297
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   298
val beta_eta_contract = eta_contract o beta_norm;
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   299
52131
366fa32ee2a3 tuned signature;
wenzelm
parents: 52128
diff changeset
   300
fun aeconv (t, u) = t aconv u orelse eta_contract t aconv eta_contract u;
22174
f2bf6bcd4a98 tuned eta_contract;
wenzelm
parents: 21795
diff changeset
   301
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   302
52221
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   303
fun body_type env (Type ("fun", [_, T])) = body_type env T
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   304
  | body_type env (T as TVar v) =
52128
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   305
      (case Type.lookup (type_env env) v of
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   306
        NONE => T
52221
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   307
      | SOME T' => body_type env T')
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   308
  | body_type _ T = T;
52128
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   309
52221
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   310
fun binder_types env (Type ("fun", [T, U])) = T :: binder_types env U
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   311
  | binder_types env (TVar v) =
52128
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   312
      (case Type.lookup (type_env env) v of
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   313
        NONE => []
52221
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   314
      | SOME T' => binder_types env T')
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   315
  | binder_types _ _ = [];
52128
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   316
52221
4ffe819a9b11 tuned signature;
wenzelm
parents: 52132
diff changeset
   317
fun strip_type env T = (binder_types env T, body_type env T);
52128
7f3549a316f4 tuned signature -- slightly more general operations (cf. term.ML);
wenzelm
parents: 52049
diff changeset
   318
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   319
(*finds type of term without checking that combinations are consistent
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   320
  Ts holds types of bound variables*)
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   321
fun fastype (Envir {tyenv, ...}) =
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   322
  let
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   323
    val funerr = "fastype: expected function type";
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   324
    fun fast Ts (f $ u) =
32648
143e0b0a6b33 Correct chasing of type variable instantiations during type unification.
paulson
parents: 32034
diff changeset
   325
          (case Type.devar tyenv (fast Ts f) of
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   326
            Type ("fun", [_, T]) => T
63618
wenzelm
parents: 63616
diff changeset
   327
          | TVar _ => raise TERM (funerr, [f $ u])
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   328
          | _ => raise TERM (funerr, [f $ u]))
63618
wenzelm
parents: 63616
diff changeset
   329
      | fast _ (Const (_, T)) = T
wenzelm
parents: 63616
diff changeset
   330
      | fast _ (Free (_, T)) = T
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   331
      | fast Ts (Bound i) =
43278
1fbdcebb364b more robust exception pattern General.Subscript;
wenzelm
parents: 42083
diff changeset
   332
          (nth Ts i handle General.Subscript => raise TERM ("fastype: Bound", [Bound i]))
63618
wenzelm
parents: 63616
diff changeset
   333
      | fast _ (Var (_, T)) = T
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   334
      | fast Ts (Abs (_, T, u)) = T --> fast (T :: Ts) u;
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   335
  in fast end;
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   336
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   337
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   338
(** plain substitution -- without variable chasing **)
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   339
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   340
local
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   341
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   342
fun subst_type0 tyenv = Term_Subst.map_atypsT_same
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   343
  (fn TVar v =>
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   344
        (case Type.lookup tyenv v of
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   345
          SOME U => U
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   346
        | NONE => raise Same.SAME)
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   347
    | _ => raise Same.SAME);
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   348
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   349
fun subst_term1 tenv = Term_Subst.map_aterms_same
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   350
  (fn Var v =>
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   351
        (case lookup1 tenv v of
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   352
          SOME u => u
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   353
        | NONE => raise Same.SAME)
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   354
    | _ => raise Same.SAME);
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   355
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   356
fun subst_term2 tenv tyenv : term Same.operation =
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   357
  let
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   358
    val substT = subst_type0 tyenv;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   359
    fun subst (Const (a, T)) = Const (a, substT T)
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   360
      | subst (Free (a, T)) = Free (a, substT T)
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   361
      | subst (Var (xi, T)) =
51700
c8f2bad67dbb tuned signature;
wenzelm
parents: 43278
diff changeset
   362
          (case lookup1 tenv (xi, T) of
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   363
            SOME u => u
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   364
          | NONE => Var (xi, substT T))
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   365
      | subst (Bound _) = raise Same.SAME
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   366
      | subst (Abs (a, T, t)) =
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   367
          (Abs (a, substT T, Same.commit subst t)
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   368
            handle Same.SAME => Abs (a, T, subst t))
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   369
      | subst (t $ u) = (subst t $ Same.commit subst u handle Same.SAME => t $ subst u);
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   370
  in subst end;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   371
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   372
in
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   373
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   374
fun subst_type_same tyenv T =
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   375
  if Vartab.is_empty tyenv then raise Same.SAME
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   376
  else subst_type0 tyenv T;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   377
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   378
fun subst_term_types_same tyenv t =
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   379
  if Vartab.is_empty tyenv then raise Same.SAME
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   380
  else Term_Subst.map_types_same (subst_type0 tyenv) t;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   381
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   382
fun subst_term_same (tyenv, tenv) =
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   383
  if Vartab.is_empty tenv then subst_term_types_same tyenv
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   384
  else if Vartab.is_empty tyenv then subst_term1 tenv
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   385
  else subst_term2 tenv tyenv;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   386
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   387
fun subst_type tyenv T = subst_type_same tyenv T handle Same.SAME => T;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   388
fun subst_term_types tyenv t = subst_term_types_same tyenv t handle Same.SAME => t;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   389
fun subst_term envs t = subst_term_same envs t handle Same.SAME => t;
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   390
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   391
end;
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   392
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   393
32034
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   394
70c0bcd0adfb tuned/modernized subst: Same.operation;
wenzelm
parents: 32031
diff changeset
   395
(** expand defined atoms -- with local beta reduction **)
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   396
19422
bba26da0f227 expand_atom: Type.raw_match;
wenzelm
parents: 18937
diff changeset
   397
fun expand_atom T (U, u) =
74232
1091880266e5 clarified signature;
wenzelm
parents: 63619
diff changeset
   398
  subst_term_types (Vartab.build (Type.raw_match (U, T))) u
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   399
    handle Type.TYPE_MATCH => raise TYPE ("expand_atom: ill-typed replacement", [T, U], [u]);
18937
0eb35519f0f3 added (beta_)eta_contract (from pattern.ML);
wenzelm
parents: 17412
diff changeset
   400
21795
d7dcc3dfa7e9 added expand_term_frees;
wenzelm
parents: 21695
diff changeset
   401
fun expand_term get =
21695
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   402
  let
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   403
    fun expand tm =
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   404
      let
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   405
        val (head, args) = Term.strip_comb tm;
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   406
        val args' = map expand args;
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   407
        fun comb head' = Term.list_comb (head', args');
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   408
      in
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   409
        (case head of
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   410
          Abs (x, T, t) => comb (Abs (x, T, expand t))
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   411
        | _ =>
32031
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   412
          (case get head of
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   413
            SOME def => Term.betapplys (expand_atom (Term.fastype_of head) def, args')
e2e6b0691264 major cleanup, simplification, modernization;
wenzelm
parents: 32018
diff changeset
   414
          | NONE => comb head))
21695
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   415
      end;
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   416
  in expand end;
6c07cc87fe2b added expand_term;
wenzelm
parents: 20670
diff changeset
   417
74575
ccf599864beb clarified signature -- avoid clones;
wenzelm
parents: 74232
diff changeset
   418
fun expand_term_defs dest defs =
21795
d7dcc3dfa7e9 added expand_term_frees;
wenzelm
parents: 21695
diff changeset
   419
  let
74575
ccf599864beb clarified signature -- avoid clones;
wenzelm
parents: 74232
diff changeset
   420
    val eqs = map (fn ((x, U), u) => (x: string, (U, u))) defs;
ccf599864beb clarified signature -- avoid clones;
wenzelm
parents: 74232
diff changeset
   421
    fun get t = (case try dest t of SOME (x, _: typ) => AList.lookup (op =) eqs x | _ => NONE);
21795
d7dcc3dfa7e9 added expand_term_frees;
wenzelm
parents: 21695
diff changeset
   422
  in expand_term get end;
d7dcc3dfa7e9 added expand_term_frees;
wenzelm
parents: 21695
diff changeset
   423
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   424
end;