src/Pure/envir.ML
author berghofe
Fri Aug 31 16:08:45 2001 +0200 (2001-08-31)
changeset 11513 2f6fe5b01521
parent 10485 f1576723371f
child 12231 4a25f04bea61
permissions -rw-r--r--
- exported SAME exception
- exported functions for normalizing types
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
clasohm@0
    30
end;
clasohm@0
    31
paulson@1500
    32
structure Envir : ENVIR =
clasohm@0
    33
struct
clasohm@0
    34
clasohm@0
    35
(*updating can destroy environment in 2 ways!!
clasohm@0
    36
   (1) variables out of range   (2) circular assignments
clasohm@0
    37
*)
clasohm@0
    38
datatype env = Envir of
wenzelm@247
    39
    {maxidx: int,               (*maximum index of vars*)
berghofe@8407
    40
     asol: term Vartab.table,   (*table of assignments to Vars*)
berghofe@8407
    41
     iTs: typ Vartab.table}     (*table of assignments to TVars*)
clasohm@0
    42
clasohm@0
    43
clasohm@0
    44
(*Generate a list of distinct variables.
clasohm@0
    45
  Increments index to make them distinct from ALL present variables. *)
clasohm@0
    46
fun genvars name (Envir{maxidx, asol, iTs}, Ts) : env * term list =
clasohm@0
    47
  let fun genvs (_, [] : typ list) : term list = []
clasohm@0
    48
        | genvs (n, [T]) = [ Var((name, maxidx+1), T) ]
clasohm@0
    49
        | genvs (n, T::Ts) =
wenzelm@247
    50
            Var((name ^ radixstring(26,"a",n), maxidx+1), T)
wenzelm@247
    51
            :: genvs(n+1,Ts)
clasohm@0
    52
  in  (Envir{maxidx=maxidx+1, asol=asol, iTs=iTs}, genvs (0,Ts))  end;
clasohm@0
    53
clasohm@0
    54
(*Generate a variable.*)
clasohm@0
    55
fun genvar name (env,T) : env * term =
wenzelm@247
    56
  let val (env',[v]) = genvars name (env,[T])
clasohm@0
    57
  in  (env',v)  end;
clasohm@0
    58
berghofe@8407
    59
fun lookup (Envir{asol,...}, xname) : term option = Vartab.lookup (asol, xname);
clasohm@0
    60
clasohm@0
    61
fun update ((xname, t), Envir{maxidx, asol, iTs}) =
berghofe@8407
    62
  Envir{maxidx=maxidx, asol=Vartab.update_new ((xname,t), asol), iTs=iTs};
clasohm@0
    63
wenzelm@5289
    64
(*The empty environment.  New variables will start with the given index+1.*)
berghofe@8407
    65
fun empty m = Envir{maxidx=m, asol=Vartab.empty, iTs=Vartab.empty};
clasohm@0
    66
paulson@2142
    67
(*Test for empty environment*)
berghofe@8407
    68
fun is_empty (Envir {asol, iTs, ...}) = Vartab.is_empty asol andalso Vartab.is_empty iTs;
wenzelm@247
    69
paulson@2142
    70
(*Determine if the least index updated exceeds lim*)
berghofe@8407
    71
fun above (lim, Envir {asol, iTs, ...}) =
berghofe@8407
    72
  (case (Vartab.min_key asol, Vartab.min_key iTs) of
berghofe@8407
    73
     (None, None) => true
berghofe@8407
    74
   | (Some (_, i), None) => i > lim
berghofe@8407
    75
   | (None, Some (_, i')) => i' > lim
berghofe@8407
    76
   | (Some (_, i), Some (_, i')) => i > lim andalso i' > lim);
wenzelm@247
    77
clasohm@0
    78
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
clasohm@0
    79
fun vupdate((a,t), env) = case t of
clasohm@0
    80
      Var(name',T) =>
wenzelm@247
    81
        if a=name' then env     (*cycle!*)
wenzelm@247
    82
        else if xless(a, name')  then
wenzelm@247
    83
           (case lookup(env,name') of  (*if already assigned, chase*)
wenzelm@247
    84
                None => update((name', Var(a,T)), env)
wenzelm@247
    85
              | Some u => vupdate((a,u), env))
wenzelm@247
    86
        else update((a,t), env)
clasohm@0
    87
    | _ => update((a,t), env);
clasohm@0
    88
clasohm@0
    89
clasohm@0
    90
(*Convert environment to alist*)
berghofe@8407
    91
fun alist_of (Envir{asol,...}) = Vartab.dest asol;
clasohm@0
    92
clasohm@0
    93
paulson@1500
    94
(*** Beta normal form for terms (not eta normal form).
paulson@1500
    95
     Chases variables in env;  Does not exploit sharing of variable bindings
paulson@1500
    96
     Does not check types, so could loop. ***)
paulson@1500
    97
paulson@1500
    98
(*raised when norm has no effect on a term, to do sharing instead of copying*)
paulson@1500
    99
exception SAME;
clasohm@0
   100
berghofe@11513
   101
fun norm_term1 same (asol,t) : term =
paulson@1500
   102
  let fun norm (Var (w,T)) =
wenzelm@10485
   103
            (case Vartab.lookup (asol, w) of
wenzelm@10485
   104
                Some u => (norm u handle SAME => u)
wenzelm@10485
   105
              | None   => raise SAME)
wenzelm@10485
   106
        | norm (Abs(a,T,body)) =  Abs(a, T, norm body)
wenzelm@10485
   107
        | norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
wenzelm@10485
   108
        | norm (f $ t) =
wenzelm@10485
   109
            ((case norm f of
wenzelm@10485
   110
               Abs(_,_,body) => normh(subst_bound (t, body))
wenzelm@10485
   111
             | nf => nf $ (norm t handle SAME => t))
wenzelm@10485
   112
            handle SAME => f $ norm t)
wenzelm@10485
   113
        | norm _ =  raise SAME
paulson@2191
   114
      and normh t = norm t handle SAME => t
berghofe@11513
   115
  in (if same then norm else normh) t end
clasohm@0
   116
berghofe@11513
   117
fun normT iTs (Type (a, Ts)) = Type (a, normTs iTs Ts)
berghofe@11513
   118
  | normT iTs (TFree _) = raise SAME
berghofe@11513
   119
  | normT iTs (TVar(v, _)) = (case Vartab.lookup (iTs, v) of
berghofe@11513
   120
          Some U => normTh iTs U
berghofe@11513
   121
        | None => raise SAME)
berghofe@11513
   122
and normTh iTs T = ((normT iTs T) handle SAME => T)
berghofe@11513
   123
and normTs iTs [] = raise SAME
berghofe@11513
   124
  | normTs iTs (T :: Ts) =
berghofe@11513
   125
      ((normT iTs T :: (normTs iTs Ts handle SAME => Ts))
berghofe@11513
   126
       handle SAME => T :: normTs iTs Ts);
berghofe@11513
   127
berghofe@11513
   128
fun norm_term2 same (asol, iTs, t) : term =
berghofe@11513
   129
  let fun norm (Const (a, T)) = Const(a, normT iTs T)
berghofe@11513
   130
        | norm (Free (a, T)) = Free(a, normT iTs T)
berghofe@11513
   131
        | norm (Var (w, T)) =
wenzelm@10485
   132
            (case Vartab.lookup (asol, w) of
wenzelm@10485
   133
                Some u => normh u
berghofe@11513
   134
              | None   => Var(w, normT iTs T))
berghofe@11513
   135
        | norm (Abs (a, T, body)) =
berghofe@11513
   136
               (Abs (a, normT iTs T, normh body) handle SAME => Abs (a, T, norm body))
berghofe@11513
   137
        | norm (Abs(_, _, body) $ t) = normh (subst_bound (t, body))
berghofe@11513
   138
        | norm (f $ t) =
wenzelm@10485
   139
            ((case norm f of
berghofe@11513
   140
               Abs(_, _, body) => normh (subst_bound (t, body))
wenzelm@10485
   141
             | nf => nf $ normh t)
wenzelm@10485
   142
            handle SAME => f $ norm t)
wenzelm@10485
   143
        | norm _ =  raise SAME
paulson@1500
   144
      and normh t = (norm t) handle SAME => t
berghofe@11513
   145
  in (if same then norm else normh) t end;
clasohm@0
   146
berghofe@11513
   147
fun gen_norm_term same (env as Envir{asol,iTs,...}) t : term =
berghofe@11513
   148
  if Vartab.is_empty iTs then norm_term1 same (asol, t)
berghofe@11513
   149
  else norm_term2 same (asol, iTs, t);
berghofe@11513
   150
berghofe@11513
   151
val norm_term = gen_norm_term false;
berghofe@11513
   152
val norm_term_same = gen_norm_term true;
wenzelm@10485
   153
wenzelm@10485
   154
val beta_norm = norm_term (empty 0);
lcp@719
   155
berghofe@11513
   156
fun norm_type (Envir {iTs, ...}) = normTh iTs;
berghofe@11513
   157
fun norm_type_same (Envir {iTs, ...}) =
berghofe@11513
   158
  if Vartab.is_empty iTs then raise SAME else normT iTs;
berghofe@11513
   159
berghofe@11513
   160
fun norm_types_same (Envir {iTs, ...}) =
berghofe@11513
   161
  if Vartab.is_empty iTs then raise SAME else normTs iTs;
berghofe@11513
   162
berghofe@11513
   163
clasohm@0
   164
end;