src/Pure/envir.ML
author wenzelm
Sat, 21 Jan 2006 23:02:14 +0100
changeset 18728 6790126ab5f6
parent 17412 e26cb20ef0cc
child 18937 0eb35519f0f3
permissions -rw-r--r--
simplified type attribute;
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
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1988  University of Cambridge
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
     5
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
     6
Environments.  The type of a term variable / sort of a type variable is
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
     7
part of its name. The lookup function must apply type substitutions,
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
     8
since they may change the identity of a variable.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    11
signature ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
sig
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    13
  type tenv
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    14
  datatype env = Envir of {asol: tenv, iTs: Type.tyenv, maxidx: int}
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    15
  val type_env: env -> Type.tyenv
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
    16
  exception SAME
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    17
  val genvars: string -> env * typ list -> env * term list
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    18
  val genvar: string -> env * typ -> env * term
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    19
  val lookup: env * (indexname * typ) -> term option
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    20
  val lookup': tenv * (indexname * typ) -> term option
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    21
  val update: ((indexname * typ) * term) * env -> env
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    22
  val empty: int -> env
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    23
  val is_empty: env -> bool
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    24
  val above: int * env -> bool
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    25
  val vupdate: ((indexname * typ) * term) * env -> env
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    26
  val alist_of: env -> (indexname * (typ * term)) list
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    27
  val norm_term: env -> term -> term
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
    28
  val norm_term_same: env -> term -> term
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    29
  val norm_type: Type.tyenv -> typ -> typ
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    30
  val norm_type_same: Type.tyenv -> typ -> typ
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    31
  val norm_types_same: Type.tyenv -> typ list -> typ list
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    32
  val beta_norm: term -> term
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
    33
  val head_norm: env -> term -> term
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
    34
  val fastype: env -> typ list -> term -> typ
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    35
  val typ_subst_TVars: Type.tyenv -> typ -> typ
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    36
  val subst_TVars: Type.tyenv -> term -> term
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    37
  val subst_Vars: tenv -> term -> term
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    38
  val subst_vars: Type.tyenv * tenv -> term -> term
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    41
structure Envir : ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
(*updating can destroy environment in 2 ways!!
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
   (1) variables out of range   (2) circular assignments
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
*)
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    47
type tenv = (typ * term) Vartab.table
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    48
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
datatype env = Envir of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    50
    {maxidx: int,      (*maximum index of vars*)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    51
     asol: tenv,       (*table of assignments to Vars*)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    52
     iTs: Type.tyenv}  (*table of assignments to TVars*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
    54
fun type_env (Envir {iTs, ...}) = iTs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
(*Generate a list of distinct variables.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
  Increments index to make them distinct from ALL present variables. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
fun genvars name (Envir{maxidx, asol, iTs}, Ts) : env * term list =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
  let fun genvs (_, [] : typ list) : term list = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
        | genvs (n, [T]) = [ Var((name, maxidx+1), T) ]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
        | genvs (n, T::Ts) =
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    62
            Var((name ^ radixstring(26,"a",n), maxidx+1), T)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    63
            :: genvs(n+1,Ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  in  (Envir{maxidx=maxidx+1, asol=asol, iTs=iTs}, genvs (0,Ts))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    66
(*Generate a variable.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
fun genvar name (env,T) : env * term =
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    68
  let val (env',[v]) = genvars name (env,[T])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
  in  (env',v)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    71
fun var_clash ixn T T' = raise TYPE ("Variable " ^
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    72
  quote (Syntax.string_of_vname ixn) ^ " has two distinct types",
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    73
  [T', T], []);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    75
fun gen_lookup f asol (xname, T) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17224
diff changeset
    76
  (case Vartab.lookup asol xname of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    77
     NONE => NONE
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    78
   | SOME (U, t) => if f (T, U) then SOME t
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    79
       else var_clash xname T U);
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    80
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    81
(* When dealing with environments produced by matching instead *)
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    82
(* of unification, there is no need to chase assigned TVars.   *)
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    83
(* In this case, we can simply ignore the type substitution    *)
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    84
(* and use = instead of eq_type.                               *)
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    85
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    86
fun lookup' (asol, p) = gen_lookup op = asol p;
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    87
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    88
fun lookup2 (iTs, asol) p =
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    89
  if Vartab.is_empty iTs then lookup' (asol, p)
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
    90
  else gen_lookup (Type.eq_type iTs) asol p;
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    91
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    92
fun lookup (Envir {asol, iTs, ...}, p) = lookup2 (iTs, asol) p;
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    93
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
    94
fun update (((xname, T), t), Envir {maxidx, asol, iTs}) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17224
diff changeset
    95
  Envir{maxidx=maxidx, asol=Vartab.update_new (xname, (T, t)) asol, iTs=iTs};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
5289
41b949f3b8ac fixed comment;
wenzelm
parents: 2191
diff changeset
    97
(*The empty environment.  New variables will start with the given index+1.*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    98
fun empty m = Envir{maxidx=m, asol=Vartab.empty, iTs=Vartab.empty};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    99
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
   100
(*Test for empty environment*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   101
fun is_empty (Envir {asol, iTs, ...}) = Vartab.is_empty asol andalso Vartab.is_empty iTs;
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
   102
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
   103
(*Determine if the least index updated exceeds lim*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   104
fun above (lim, Envir {asol, iTs, ...}) =
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   105
  (case (Vartab.min_key asol, Vartab.min_key iTs) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   106
     (NONE, NONE) => true
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   107
   | (SOME (_, i), NONE) => i > lim
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   108
   | (NONE, SOME (_, i')) => i' > lim
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   109
   | (SOME (_, i), SOME (_, i')) => i > lim andalso i' > lim);
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
   110
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   111
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   112
fun vupdate ((aU as (a, U), t), env as Envir {iTs, ...}) = case t of
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   113
      Var (nT as (name', T)) =>
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   114
        if a = name' then env     (*cycle!*)
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
   115
        else if xless(a, name')  then
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   116
           (case lookup (env, nT) of  (*if already assigned, chase*)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   117
                NONE => update ((nT, Var (a, T)), env)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   118
              | SOME u => vupdate ((aU, u), env))
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   119
        else update ((aU, t), env)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   120
    | _ => update ((aU, t), env);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   122
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   123
(*Convert environment to alist*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   124
fun alist_of (Envir{asol,...}) = Vartab.dest asol;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   125
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   126
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   127
(*** Beta normal form for terms (not eta normal form).
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   128
     Chases variables in env;  Does not exploit sharing of variable bindings
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   129
     Does not check types, so could loop. ***)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   130
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   131
(*raised when norm has no effect on a term, to do sharing instead of copying*)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   132
exception SAME;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   133
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   134
fun norm_term1 same (asol,t) : term =
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   135
  let fun norm (Var wT) =
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
   136
            (case lookup' (asol, wT) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   137
                SOME u => (norm u handle SAME => u)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   138
              | NONE   => raise SAME)
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   139
        | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   140
        | norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   141
        | norm (f $ t) =
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   142
            ((case norm f of
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   143
               Abs(_,_,body) => normh(subst_bound (t, body))
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   144
             | nf => nf $ (norm t handle SAME => t))
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   145
            handle SAME => f $ norm t)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   146
        | norm _ =  raise SAME
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   147
      and normh t = norm t handle SAME => t
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   148
  in (if same then norm else normh) t end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   149
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   150
fun normT iTs (Type (a, Ts)) = Type (a, normTs iTs Ts)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   151
  | normT iTs (TFree _) = raise SAME
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   152
  | normT iTs (TVar vS) = (case Type.lookup (iTs, vS) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   153
          SOME U => normTh iTs U
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   154
        | NONE => raise SAME)
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   155
and normTh iTs T = ((normT iTs T) handle SAME => T)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   156
and normTs iTs [] = raise SAME
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   157
  | normTs iTs (T :: Ts) =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   158
      ((normT iTs T :: (normTs iTs Ts handle SAME => Ts))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   159
       handle SAME => T :: normTs iTs Ts);
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   160
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   161
fun norm_term2 same (asol, iTs, t) : term =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   162
  let fun norm (Const (a, T)) = Const(a, normT iTs T)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   163
        | norm (Free (a, T)) = Free(a, normT iTs T)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   164
        | norm (Var (w, T)) =
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   165
            (case lookup2 (iTs, asol) (w, T) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   166
                SOME u => normh u
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   167
              | NONE   => Var(w, normT iTs T))
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   168
        | norm (Abs (a, T, body)) =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   169
               (Abs (a, normT iTs T, normh body) handle SAME => Abs (a, T, norm body))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   170
        | norm (Abs(_, _, body) $ t) = normh (subst_bound (t, body))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   171
        | norm (f $ t) =
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   172
            ((case norm f of
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   173
               Abs(_, _, body) => normh (subst_bound (t, body))
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   174
             | nf => nf $ normh t)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   175
            handle SAME => f $ norm t)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   176
        | norm _ =  raise SAME
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   177
      and normh t = (norm t) handle SAME => t
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   178
  in (if same then norm else normh) t end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   179
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   180
fun gen_norm_term same (env as Envir{asol,iTs,...}) t : term =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   181
  if Vartab.is_empty iTs then norm_term1 same (asol, t)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   182
  else norm_term2 same (asol, iTs, t);
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   183
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   184
val norm_term = gen_norm_term false;
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   185
val norm_term_same = gen_norm_term true;
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   186
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   187
val beta_norm = norm_term (empty 0);
719
e3e1d1a6d408 Pure/envir/norm_term: replaced equality test for [] by null
lcp
parents: 247
diff changeset
   188
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
   189
fun norm_type iTs = normTh iTs;
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
   190
fun norm_type_same iTs =
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   191
  if Vartab.is_empty iTs then raise SAME else normT iTs;
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   192
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
   193
fun norm_types_same iTs =
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   194
  if Vartab.is_empty iTs then raise SAME else normTs iTs;
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   195
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   196
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   197
(*Put a term into head normal form for unification.*)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   198
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   199
fun head_norm env t =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   200
  let
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   201
    fun hnorm (Var vT) = (case lookup (env, vT) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   202
          SOME u => head_norm env u
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   203
        | NONE => raise SAME)
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   204
      | hnorm (Abs (a, T, body)) =  Abs (a, T, hnorm body)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   205
      | hnorm (Abs (_, _, body) $ t) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   206
          head_norm env (subst_bound (t, body))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   207
      | hnorm (f $ t) = (case hnorm f of
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   208
          Abs (_, _, body) => head_norm env (subst_bound (t, body))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   209
        | nf => nf $ t)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   210
	  | hnorm _ =  raise SAME
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   211
  in hnorm t handle SAME => t end;
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   212
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   213
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   214
(*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
   215
  Ts holds types of bound variables*)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   216
fun fastype (Envir {iTs, ...}) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   217
let val funerr = "fastype: expected function type";
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   218
    fun fast Ts (f $ u) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   219
	(case fast Ts f of
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   220
	   Type ("fun", [_, T]) => T
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   221
	 | TVar ixnS =>
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   222
		(case Type.lookup (iTs, ixnS) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 12496
diff changeset
   223
		   SOME (Type ("fun", [_, T])) => T
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   224
		 | _ => raise TERM (funerr, [f $ u]))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   225
	 | _ => raise TERM (funerr, [f $ u]))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   226
      | fast Ts (Const (_, T)) = T
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   227
      | fast Ts (Free (_, T)) = T
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   228
      | fast Ts (Bound i) =
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   229
	(List.nth (Ts, i)
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
   230
  	 handle Subscript => raise TERM ("fastype: Bound", [Bound i]))
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   231
      | fast Ts (Var (_, T)) = T 
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   232
      | fast Ts (Abs (_, T, u)) = T --> fast (T :: Ts) u
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   233
in fast end;
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   234
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   235
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   236
(*Substitute for type Vars in a type*)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   237
fun typ_subst_TVars iTs T = if Vartab.is_empty iTs then T else
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   238
  let fun subst(Type(a, Ts)) = Type(a, map subst Ts)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   239
        | subst(T as TFree _) = T
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   240
        | subst(T as TVar ixnS) =
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   241
            (case Type.lookup (iTs, ixnS) of NONE => T | SOME(U) => U)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   242
  in subst T end;
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   243
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   244
(*Substitute for type Vars in a term*)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   245
val subst_TVars = map_term_types o typ_subst_TVars;
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   246
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   247
(*Substitute for Vars in a term *)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   248
fun subst_Vars itms t = if Vartab.is_empty itms then t else
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
   249
  let fun subst (v as Var ixnT) = getOpt (lookup' (itms, ixnT), v)
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   250
        | subst (Abs (a, T, t)) = Abs (a, T, subst t)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   251
        | subst (f $ t) = subst f $ subst t
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   252
        | subst t = t
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   253
  in subst t end;
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   254
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   255
(*Substitute for type/term Vars in a term *)
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
   256
fun subst_vars (iTs, itms) =
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   257
  if Vartab.is_empty iTs then subst_Vars itms else
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   258
  let fun subst (Const (a, T)) = Const(a, typ_subst_TVars iTs T)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   259
        | subst (Free (a, T)) = Free (a, typ_subst_TVars iTs T)
16652
4ecf94235ec7 Fixed bug: lookup' must use = instead of eq_type to compare types of
berghofe
parents: 15797
diff changeset
   260
        | subst (Var (ixn, T)) = (case lookup' (itms, (ixn, T)) of
15797
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   261
            NONE   => Var (ixn, typ_subst_TVars iTs T)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   262
          | SOME t => t)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   263
        | subst (b as Bound _) = b
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   264
        | subst (Abs (a, T, t)) = Abs(a, typ_subst_TVars iTs T, subst t)
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   265
        | subst (f $ t) = subst f $ subst t
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   266
  in subst end;
a63605582573 - Eliminated nodup_vars check.
berghofe
parents: 15570
diff changeset
   267
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   268
end;