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