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