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