src/Pure/envir.ML
author kleing
Wed, 07 Jan 2004 07:52:12 +0100
changeset 14343 6bc647f472b9
parent 12496 0a9bd5034e05
child 15531 08c8dad8e399
permissions -rw-r--r--
map_idI
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
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
     6
Environments.  They don't take account that typ is part of variable
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
     7
name.  Therefore we check elsewhere that two variables with same names
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
     8
and different types cannot occur.
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
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    13
  datatype env = Envir of {asol: term Vartab.table, iTs: typ Vartab.table, maxidx: int}
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
    14
  val type_env: env -> typ Vartab.table
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
    15
  exception SAME
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    16
  val genvars: string -> env * typ list -> env * term list
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    17
  val genvar: string -> env * typ -> env * term
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    18
  val lookup: env * indexname -> term option
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    19
  val update: (indexname * term) * env -> env
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    20
  val empty: int -> env
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    21
  val is_empty: env -> bool
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    22
  val above: int * env -> bool
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    23
  val vupdate: (indexname * term) * env -> env
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    24
  val alist_of: env -> (indexname * term) list
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    25
  val norm_term: env -> term -> term
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
    26
  val norm_term_same: env -> term -> term
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
    27
  val norm_type: typ Vartab.table -> typ -> typ
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
    28
  val norm_type_same: typ Vartab.table -> typ -> typ
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
    29
  val norm_types_same: typ Vartab.table -> typ list -> typ list
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
    30
  val beta_norm: term -> term
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
    31
  val head_norm: env -> term -> term
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
    32
  val fastype: env -> typ list -> term -> typ
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    35
structure Envir : ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    36
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
(*updating can destroy environment in 2 ways!!
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
   (1) variables out of range   (2) circular assignments
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
datatype env = Envir of
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    42
    {maxidx: int,               (*maximum index of vars*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    43
     asol: term Vartab.table,   (*table of assignments to Vars*)
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    44
     iTs: typ Vartab.table}     (*table of assignments to TVars*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
    46
fun type_env (Envir {iTs, ...}) = iTs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    47
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
(*Generate a list of distinct variables.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
  Increments index to make them distinct from ALL present variables. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
fun genvars name (Envir{maxidx, asol, iTs}, Ts) : env * term list =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
  let fun genvs (_, [] : typ list) : term list = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    52
        | genvs (n, [T]) = [ Var((name, maxidx+1), T) ]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
        | genvs (n, T::Ts) =
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    54
            Var((name ^ radixstring(26,"a",n), maxidx+1), T)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    55
            :: genvs(n+1,Ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
  in  (Envir{maxidx=maxidx+1, asol=asol, iTs=iTs}, genvs (0,Ts))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    58
(*Generate a variable.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
fun genvar name (env,T) : env * term =
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    60
  let val (env',[v]) = genvars name (env,[T])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    61
  in  (env',v)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    63
fun lookup (Envir{asol,...}, xname) : term option = Vartab.lookup (asol, xname);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
fun update ((xname, t), Envir{maxidx, asol, iTs}) =
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    66
  Envir{maxidx=maxidx, asol=Vartab.update_new ((xname,t), asol), iTs=iTs};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
5289
41b949f3b8ac fixed comment;
wenzelm
parents: 2191
diff changeset
    68
(*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
    69
fun empty m = Envir{maxidx=m, asol=Vartab.empty, iTs=Vartab.empty};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
    71
(*Test for empty environment*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    72
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
    73
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
    74
(*Determine if the least index updated exceeds lim*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    75
fun above (lim, Envir {asol, iTs, ...}) =
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    76
  (case (Vartab.min_key asol, Vartab.min_key iTs) of
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    77
     (None, None) => true
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    78
   | (Some (_, i), None) => i > lim
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    79
   | (None, Some (_, i')) => i' > lim
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    80
   | (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
    81
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
fun vupdate((a,t), env) = case t of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
      Var(name',T) =>
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    85
        if a=name' then env     (*cycle!*)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    86
        else if xless(a, name')  then
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    87
           (case lookup(env,name') of  (*if already assigned, chase*)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    88
                None => update((name', Var(a,T)), env)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    89
              | Some u => vupdate((a,u), env))
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    90
        else update((a,t), env)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
    | _ => update((a,t), env);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    92
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
(*Convert environment to alist*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    95
fun alist_of (Envir{asol,...}) = Vartab.dest asol;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    98
(*** Beta normal form for terms (not eta normal form).
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    99
     Chases variables in env;  Does not exploit sharing of variable bindings
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   100
     Does not check types, so could loop. ***)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   101
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   102
(*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
   103
exception SAME;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   104
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   105
fun norm_term1 same (asol,t) : term =
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   106
  let fun norm (Var (w,T)) =
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   107
            (case Vartab.lookup (asol, w) of
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   108
                Some u => (norm u handle SAME => u)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   109
              | None   => raise SAME)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   110
        | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   111
        | norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   112
        | norm (f $ t) =
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   113
            ((case norm f of
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   114
               Abs(_,_,body) => normh(subst_bound (t, body))
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   115
             | nf => nf $ (norm t handle SAME => t))
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   116
            handle SAME => f $ norm t)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   117
        | norm _ =  raise SAME
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   118
      and normh t = norm t handle SAME => t
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   119
  in (if same then norm else normh) t end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   120
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   121
fun normT iTs (Type (a, Ts)) = Type (a, normTs iTs Ts)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   122
  | normT iTs (TFree _) = raise SAME
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   123
  | normT iTs (TVar(v, _)) = (case Vartab.lookup (iTs, v) of
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   124
          Some U => normTh iTs U
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   125
        | None => raise SAME)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   126
and normTh iTs T = ((normT iTs T) handle SAME => T)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   127
and normTs iTs [] = raise SAME
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   128
  | normTs iTs (T :: Ts) =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   129
      ((normT iTs T :: (normTs iTs Ts handle SAME => Ts))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   130
       handle SAME => T :: normTs iTs Ts);
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   131
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   132
fun norm_term2 same (asol, iTs, t) : term =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   133
  let fun norm (Const (a, T)) = Const(a, normT iTs T)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   134
        | norm (Free (a, T)) = Free(a, normT iTs T)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   135
        | norm (Var (w, T)) =
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   136
            (case Vartab.lookup (asol, w) of
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   137
                Some u => normh u
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   138
              | None   => Var(w, normT iTs T))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   139
        | norm (Abs (a, T, body)) =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   140
               (Abs (a, normT iTs T, normh body) handle SAME => Abs (a, T, norm body))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   141
        | norm (Abs(_, _, body) $ t) = normh (subst_bound (t, body))
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   142
        | norm (f $ t) =
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   143
            ((case norm f of
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   144
               Abs(_, _, body) => normh (subst_bound (t, body))
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   145
             | nf => nf $ normh t)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   146
            handle SAME => f $ norm t)
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   147
        | norm _ =  raise SAME
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   148
      and normh t = (norm t) handle SAME => t
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   149
  in (if same then norm else normh) t end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   150
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   151
fun gen_norm_term same (env as Envir{asol,iTs,...}) t : term =
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   152
  if Vartab.is_empty iTs then norm_term1 same (asol, t)
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   153
  else norm_term2 same (asol, iTs, t);
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   154
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   155
val norm_term = gen_norm_term false;
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   156
val norm_term_same = gen_norm_term true;
10485
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   157
f1576723371f added beta_norm;
wenzelm
parents: 8407
diff changeset
   158
val beta_norm = norm_term (empty 0);
719
e3e1d1a6d408 Pure/envir/norm_term: replaced equality test for [] by null
lcp
parents: 247
diff changeset
   159
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
   160
fun norm_type iTs = normTh iTs;
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
   161
fun norm_type_same iTs =
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   162
  if Vartab.is_empty iTs then raise SAME else normT iTs;
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   163
12496
0a9bd5034e05 added type_env function;
wenzelm
parents: 12231
diff changeset
   164
fun norm_types_same iTs =
11513
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   165
  if Vartab.is_empty iTs then raise SAME else normTs iTs;
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   166
2f6fe5b01521 - exported SAME exception
berghofe
parents: 10485
diff changeset
   167
12231
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   168
(*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
   169
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   170
fun head_norm env t =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   171
  let
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   172
    fun hnorm (Var (v, T)) = (case lookup (env, v) of
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   173
          Some u => head_norm env u
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   174
        | None => raise SAME)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   175
      | 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
   176
      | hnorm (Abs (_, _, body) $ t) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   177
          head_norm env (subst_bound (t, body))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   178
      | hnorm (f $ t) = (case hnorm f of
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   179
          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
   180
        | nf => nf $ t)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   181
	  | hnorm _ =  raise SAME
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   182
  in hnorm t handle SAME => t end;
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   183
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   184
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   185
(*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
   186
  Ts holds types of bound variables*)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   187
fun fastype (Envir {iTs, ...}) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   188
let val funerr = "fastype: expected function type";
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   189
    fun fast Ts (f $ u) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   190
	(case fast Ts f of
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   191
	   Type ("fun", [_, T]) => T
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   192
	 | TVar(ixn, _) =>
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   193
		(case Vartab.lookup (iTs, ixn) of
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   194
		   Some (Type ("fun", [_, T])) => T
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   195
		 | _ => raise TERM (funerr, [f $ u]))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   196
	 | _ => raise TERM (funerr, [f $ u]))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   197
      | fast Ts (Const (_, T)) = T
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   198
      | fast Ts (Free (_, T)) = T
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   199
      | fast Ts (Bound i) =
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   200
	(nth_elem (i, Ts)
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   201
  	 handle LIST _=> raise TERM ("fastype: Bound", [Bound i]))
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   202
      | fast Ts (Var (_, T)) = T 
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   203
      | 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
   204
in fast end;
4a25f04bea61 Moved head_norm and fastype from unify.ML to envir.ML
berghofe
parents: 11513
diff changeset
   205
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   206
end;