src/Pure/envir.ML
author paulson
Fri May 30 15:14:59 1997 +0200 (1997-05-30)
changeset 3365 86c0d1988622
parent 2191 58383908f177
child 5289 41b949f3b8ac
permissions -rw-r--r--
flushOut ensures that no recent error message are lost (not certain this is
necessary)
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
clasohm@0
     5
*)
clasohm@0
     6
paulson@2142
     7
(*Environments.  They don't take account that typ is part of variable name.
paulson@2142
     8
                 Therefore we check elsewhere that  two variables with same
paulson@2142
     9
		 names and different types cannot occur.
clasohm@0
    10
*)
clasohm@0
    11
clasohm@0
    12
wenzelm@247
    13
signature ENVIR =
clasohm@0
    14
sig
clasohm@0
    15
  type 'a xolist
clasohm@0
    16
  exception ENVIR of string * indexname;
paulson@2142
    17
  datatype env = Envir of {asol: term xolist, iTs: typ xolist, maxidx: int}
clasohm@1460
    18
  val alist_of		: env -> (indexname * term) list
clasohm@1460
    19
  val alist_of_olist	: 'a xolist -> (indexname * 'a) list
clasohm@1460
    20
  val empty		: int->env
clasohm@1460
    21
  val is_empty		: env -> bool
paulson@2142
    22
  val above		: int * env -> bool
clasohm@1460
    23
  val genvar		: string -> env * typ -> env * term
clasohm@1460
    24
  val genvars		: string -> env * typ list -> env * term list
clasohm@1460
    25
  val lookup		: env * indexname -> term option
clasohm@1460
    26
  val norm_term		: env -> term -> term
clasohm@1460
    27
  val null_olist	: 'a xolist
clasohm@1460
    28
  val olist_of_alist	: (indexname * 'a) list -> 'a xolist
clasohm@1460
    29
  val update		: (indexname * term) * env -> env
clasohm@1460
    30
  val vupdate		: (indexname * term) * env -> env
clasohm@0
    31
end;
clasohm@0
    32
paulson@1500
    33
structure Envir : ENVIR =
clasohm@0
    34
struct
clasohm@0
    35
clasohm@0
    36
(*association lists with keys in ascending order, no repeated keys*)
paulson@2142
    37
type 'a xolist = (indexname * 'a) list;
clasohm@0
    38
clasohm@0
    39
clasohm@0
    40
exception ENVIR of string * indexname;
wenzelm@247
    41
wenzelm@247
    42
(*look up key in ordered list*)
paulson@2142
    43
fun xsearch ([], key) = None
paulson@2142
    44
  | xsearch (((keyi,xi)::pairs), key) =
paulson@2142
    45
      if xless(keyi,key) then xsearch (pairs, key)
wenzelm@247
    46
      else if key=keyi then Some xi
clasohm@0
    47
      else None;
clasohm@0
    48
clasohm@0
    49
(*insert pair in ordered list, reject if key already present*)
paulson@2142
    50
fun xinsert_new (newpr as (key, x), al) =
clasohm@0
    51
  let fun insert [] = [newpr]
wenzelm@247
    52
        | insert ((pair as (keyi,xi)) :: pairs) =
wenzelm@247
    53
            if xless(keyi,key) then pair :: insert pairs
wenzelm@247
    54
            else if key=keyi then
wenzelm@247
    55
                raise ENVIR("xinsert_new: already present",key)
wenzelm@247
    56
            else newpr::pair::pairs
paulson@2142
    57
  in  (insert al)  end;
clasohm@0
    58
clasohm@0
    59
(*insert pair in ordered list, overwrite if key already present*)
paulson@2142
    60
fun xinsert (newpr as (key, x), al) =
clasohm@0
    61
  let fun insert [] = [newpr]
wenzelm@247
    62
        | insert ((pair as (keyi,xi)) :: pairs) =
wenzelm@247
    63
            if xless(keyi,key) then pair :: insert pairs
wenzelm@247
    64
            else if key=keyi then newpr::pairs
wenzelm@247
    65
            else newpr::pair::pairs
paulson@2142
    66
  in  (insert al)  end;
clasohm@0
    67
wenzelm@247
    68
(*apply function to the contents part of each pair*)
paulson@2142
    69
fun xmap f (pairs) =
clasohm@0
    70
  let fun xmp [] = []
wenzelm@247
    71
        | xmp ((key,x)::pairs) = (key, f x) :: xmp pairs
paulson@2142
    72
  in (xmp pairs)  end;
clasohm@0
    73
clasohm@0
    74
(*allows redefinition of a key by "join"ing the contents parts*)
paulson@2142
    75
fun xmerge_olists join (pairsa, pairsb) =
clasohm@0
    76
  let fun xmrg ([],pairsb) = pairsb
wenzelm@247
    77
        | xmrg (pairsa,[]) = pairsa
wenzelm@247
    78
        | xmrg ((keya,x)::pairsa, (keyb,y)::pairsb) =
wenzelm@247
    79
            if xless(keya,keyb) then
wenzelm@247
    80
                (keya,x) :: xmrg (pairsa, (keyb,y)::pairsb)
wenzelm@247
    81
            else if xless(keyb,keya) then
wenzelm@247
    82
                (keyb,y) :: xmrg ((keya,x)::pairsa, pairsb)
wenzelm@247
    83
            else (*equal*)  (keya, join(x,y)) :: xmrg (pairsa, pairsb)
paulson@2142
    84
  in  (xmrg (pairsa,pairsb))  end;
clasohm@0
    85
paulson@2142
    86
val null_olist = [];
clasohm@0
    87
paulson@2142
    88
fun alist_of_olist (pairs) = pairs;
clasohm@0
    89
paulson@2142
    90
fun olist_of_alist pairs = foldr xinsert (pairs, []);
clasohm@0
    91
clasohm@0
    92
clasohm@0
    93
clasohm@0
    94
(*updating can destroy environment in 2 ways!!
clasohm@0
    95
   (1) variables out of range   (2) circular assignments
clasohm@0
    96
*)
clasohm@0
    97
datatype env = Envir of
wenzelm@247
    98
    {maxidx: int,               (*maximum index of vars*)
wenzelm@247
    99
     asol: term xolist,         (*table of assignments to Vars*)
paulson@2142
   100
     iTs:  typ  xolist}         (*table of assignments to TVars*)
clasohm@0
   101
clasohm@0
   102
clasohm@0
   103
(*Generate a list of distinct variables.
clasohm@0
   104
  Increments index to make them distinct from ALL present variables. *)
clasohm@0
   105
fun genvars name (Envir{maxidx, asol, iTs}, Ts) : env * term list =
clasohm@0
   106
  let fun genvs (_, [] : typ list) : term list = []
clasohm@0
   107
        | genvs (n, [T]) = [ Var((name, maxidx+1), T) ]
clasohm@0
   108
        | genvs (n, T::Ts) =
wenzelm@247
   109
            Var((name ^ radixstring(26,"a",n), maxidx+1), T)
wenzelm@247
   110
            :: genvs(n+1,Ts)
clasohm@0
   111
  in  (Envir{maxidx=maxidx+1, asol=asol, iTs=iTs}, genvs (0,Ts))  end;
clasohm@0
   112
clasohm@0
   113
(*Generate a variable.*)
clasohm@0
   114
fun genvar name (env,T) : env * term =
wenzelm@247
   115
  let val (env',[v]) = genvars name (env,[T])
clasohm@0
   116
  in  (env',v)  end;
clasohm@0
   117
clasohm@0
   118
fun lookup (Envir{asol,...}, xname) : term option = xsearch (asol,xname);
clasohm@0
   119
clasohm@0
   120
fun update ((xname, t), Envir{maxidx, asol, iTs}) =
clasohm@0
   121
  Envir{maxidx=maxidx, asol=xinsert_new ((xname,t), asol), iTs=iTs};
clasohm@0
   122
clasohm@0
   123
(*The empty environment.  New variables will start with the given index.*)
clasohm@0
   124
fun empty m = Envir{maxidx=m, asol=null_olist, iTs=[]};
clasohm@0
   125
paulson@2142
   126
(*Test for empty environment*)
paulson@2142
   127
fun is_empty (Envir {asol = [], iTs = [], ...}) = true
wenzelm@247
   128
  | is_empty _ = false;
wenzelm@247
   129
paulson@2142
   130
(*Determine if the least index updated exceeds lim*)
paulson@2142
   131
fun above (lim, Envir {asol, iTs, ...}) = 
paulson@2142
   132
    let fun upd [] = true
paulson@2142
   133
	  | upd (i::is) = i>lim andalso upd is
paulson@2142
   134
    in  upd (map (#2 o #1) asol @ (map (#2 o #1) iTs))
paulson@2142
   135
    end;
wenzelm@247
   136
clasohm@0
   137
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
clasohm@0
   138
fun vupdate((a,t), env) = case t of
clasohm@0
   139
      Var(name',T) =>
wenzelm@247
   140
        if a=name' then env     (*cycle!*)
wenzelm@247
   141
        else if xless(a, name')  then
wenzelm@247
   142
           (case lookup(env,name') of  (*if already assigned, chase*)
wenzelm@247
   143
                None => update((name', Var(a,T)), env)
wenzelm@247
   144
              | Some u => vupdate((a,u), env))
wenzelm@247
   145
        else update((a,t), env)
clasohm@0
   146
    | _ => update((a,t), env);
clasohm@0
   147
clasohm@0
   148
clasohm@0
   149
(*Convert environment to alist*)
clasohm@0
   150
fun alist_of (Envir{maxidx,asol,...}) = alist_of_olist asol;
clasohm@0
   151
clasohm@0
   152
paulson@1500
   153
(*** Beta normal form for terms (not eta normal form).
paulson@1500
   154
     Chases variables in env;  Does not exploit sharing of variable bindings
paulson@1500
   155
     Does not check types, so could loop. ***)
paulson@1500
   156
paulson@1500
   157
(*raised when norm has no effect on a term, to do sharing instead of copying*)
paulson@1500
   158
exception SAME;
clasohm@0
   159
paulson@1500
   160
fun norm_term1 (asol,t) : term =
paulson@1500
   161
  let fun norm (Var (w,T)) =
paulson@1500
   162
	    (case xsearch(asol,w) of
paulson@2191
   163
		Some u => (norm u handle SAME => u)
paulson@1500
   164
	      | None   => raise SAME)
paulson@1500
   165
	| norm (Abs(a,T,body)) =  Abs(a, T, norm body)
paulson@2191
   166
	| norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
paulson@1500
   167
	| norm (f $ t) =
paulson@1500
   168
	    ((case norm f of
paulson@2191
   169
	       Abs(_,_,body) => normh(subst_bound (t, body))
paulson@2191
   170
	     | nf => nf $ (norm t handle SAME => t))
paulson@1500
   171
	    handle SAME => f $ norm t)
paulson@1500
   172
	| norm _ =  raise SAME
paulson@2191
   173
      and normh t = norm t handle SAME => t
paulson@1500
   174
  in normh t end
clasohm@0
   175
paulson@1500
   176
and norm_term2(asol,iTs,t) : term =
paulson@1500
   177
  let fun normT(Type(a,Ts)) = Type(a, normTs Ts)
paulson@1500
   178
	| normT(TFree _) = raise SAME
paulson@1500
   179
	| normT(TVar(v,_)) = (case assoc(iTs,v) of
paulson@1500
   180
		Some(U) => normTh U
paulson@1500
   181
	      | None => raise SAME)
paulson@1500
   182
      and normTh T = ((normT T) handle SAME => T)
paulson@1500
   183
      and normTs([]) = raise SAME
paulson@2181
   184
	| normTs(T::Ts) = ((normT T :: (normTs Ts handle SAME => Ts))
paulson@1500
   185
			   handle SAME => T :: normTs Ts)
paulson@1500
   186
      and norm(Const(a,T)) = Const(a, normT T)
paulson@1500
   187
	| norm(Free(a,T)) = Free(a, normT T)
paulson@1500
   188
	| norm(Var (w,T)) =
paulson@1500
   189
	    (case xsearch(asol,w) of
paulson@1500
   190
		Some u => normh u
paulson@1500
   191
	      | None   => Var(w,normT T))
paulson@1500
   192
	| norm(Abs(a,T,body)) =
paulson@1500
   193
	      (Abs(a,normT T,normh body) handle SAME => Abs(a, T, norm body))
paulson@2191
   194
	| norm(Abs(_,_,body) $ t) = normh(subst_bound (t, body))
paulson@1500
   195
	| norm(f $ t) =
paulson@1500
   196
	    ((case norm f of
paulson@2191
   197
	       Abs(_,_,body) => normh(subst_bound (t, body))
paulson@1500
   198
	     | nf => nf $ normh t)
paulson@1500
   199
	    handle SAME => f $ norm t)
paulson@1500
   200
	| norm _ =  raise SAME
paulson@1500
   201
      and normh t = (norm t) handle SAME => t
paulson@1500
   202
  in normh t end;
clasohm@0
   203
clasohm@0
   204
(*curried version might be slower in recursive calls*)
clasohm@0
   205
fun norm_term (env as Envir{asol,iTs,...}) t : term =
lcp@719
   206
        if null iTs then norm_term1(asol, t) else norm_term2(asol,iTs, t)
lcp@719
   207
clasohm@0
   208
end;
clasohm@0
   209