src/Pure/envir.ML
author berghofe
Tue, 07 Nov 2000 17:44:48 +0100
changeset 10413 0e015d9bea4e
parent 8407 d522ad1809e9
child 10485 f1576723371f
permissions -rw-r--r--
Added new file meta_simplifier.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
     1
(*  Title:      Pure/envir.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1988  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
     7
(*Environments.  They don't take account that typ is part of variable name.
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
     8
                 Therefore we check elsewhere that  two variables with same
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
     9
		 names and different types cannot occur.
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    13
signature ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
sig
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    15
  datatype env = Envir of {asol: term Vartab.table, iTs: typ Vartab.table, maxidx: int}
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    16
  val alist_of		: env -> (indexname * term) list
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    17
  val empty		: int->env
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    18
  val is_empty		: env -> bool
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
    19
  val above		: int * env -> bool
1460
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    20
  val genvar		: string -> env * typ -> env * term
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    21
  val genvars		: string -> env * typ list -> env * term list
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    22
  val lookup		: env * indexname -> term option
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    23
  val norm_term		: env -> term -> term
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    24
  val update		: (indexname * term) * env -> env
5a6f2aabd538 inserted tabs again
clasohm
parents: 1458
diff changeset
    25
  val vupdate		: (indexname * term) * env -> env
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    28
structure Envir : ENVIR =
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
struct
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
(*updating can destroy environment in 2 ways!!
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
   (1) variables out of range   (2) circular assignments
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
datatype env = Envir of
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    35
    {maxidx: int,               (*maximum index of vars*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    36
     asol: term Vartab.table,   (*table of assignments to Vars*)
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    37
     iTs: typ Vartab.table}     (*table of assignments to TVars*)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
(*Generate a list of distinct variables.
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
  Increments index to make them distinct from ALL present variables. *)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    42
fun genvars name (Envir{maxidx, asol, iTs}, Ts) : env * term list =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    43
  let fun genvs (_, [] : typ list) : term list = []
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
        | genvs (n, [T]) = [ Var((name, maxidx+1), T) ]
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    45
        | genvs (n, T::Ts) =
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    46
            Var((name ^ radixstring(26,"a",n), maxidx+1), T)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    47
            :: genvs(n+1,Ts)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
  in  (Envir{maxidx=maxidx+1, asol=asol, iTs=iTs}, genvs (0,Ts))  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    50
(*Generate a variable.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
fun genvar name (env,T) : env * term =
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    52
  let val (env',[v]) = genvars name (env,[T])
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
  in  (env',v)  end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    55
fun lookup (Envir{asol,...}, xname) : term option = Vartab.lookup (asol, xname);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    56
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
fun update ((xname, t), Envir{maxidx, asol, iTs}) =
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    58
  Envir{maxidx=maxidx, asol=Vartab.update_new ((xname,t), asol), iTs=iTs};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
5289
41b949f3b8ac fixed comment;
wenzelm
parents: 2191
diff changeset
    60
(*The empty environment.  New variables will start with the given index+1.*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    61
fun empty m = Envir{maxidx=m, asol=Vartab.empty, iTs=Vartab.empty};
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
    63
(*Test for empty environment*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    64
fun is_empty (Envir {asol, iTs, ...}) = Vartab.is_empty asol andalso Vartab.is_empty iTs;
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    65
2142
20f208ff085d Deleted Olist constructor. Replaced minidx by "above" function
paulson
parents: 1500
diff changeset
    66
(*Determine if the least index updated exceeds lim*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    67
fun above (lim, Envir {asol, iTs, ...}) =
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    68
  (case (Vartab.min_key asol, Vartab.min_key iTs) of
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    69
     (None, None) => true
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    70
   | (Some (_, i), None) => i > lim
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    71
   | (None, Some (_, i')) => i' > lim
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    72
   | (Some (_, i), Some (_, i')) => i > lim andalso i' > lim);
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    73
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
fun vupdate((a,t), env) = case t of
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
      Var(name',T) =>
247
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    77
        if a=name' then env     (*cycle!*)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    78
        else if xless(a, name')  then
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    79
           (case lookup(env,name') of  (*if already assigned, chase*)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    80
                None => update((name', Var(a,T)), env)
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    81
              | Some u => vupdate((a,u), env))
bc10568855ee added is_empty: env -> bool, minidx: env -> int option;
wenzelm
parents: 0
diff changeset
    82
        else update((a,t), env)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
    | _ => update((a,t), env);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    84
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    85
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
(*Convert environment to alist*)
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    87
fun alist_of (Envir{asol,...}) = Vartab.dest asol;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    90
(*** Beta normal form for terms (not eta normal form).
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    91
     Chases variables in env;  Does not exploit sharing of variable bindings
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    92
     Does not check types, so could loop. ***)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    93
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    94
(*raised when norm has no effect on a term, to do sharing instead of copying*)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    95
exception SAME;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    97
fun norm_term1 (asol,t) : term =
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
    98
  let fun norm (Var (w,T)) =
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
    99
	    (case Vartab.lookup (asol, w) of
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   100
		Some u => (norm u handle SAME => u)
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   101
	      | None   => raise SAME)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   102
	| norm (Abs(a,T,body)) =  Abs(a, T, norm body)
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   103
	| norm (Abs(_,_,body) $ t) = normh(subst_bound (t, body))
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   104
	| norm (f $ t) =
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   105
	    ((case norm f of
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   106
	       Abs(_,_,body) => normh(subst_bound (t, body))
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   107
	     | nf => nf $ (norm t handle SAME => t))
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   108
	    handle SAME => f $ norm t)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   109
	| norm _ =  raise SAME
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   110
      and normh t = norm t handle SAME => t
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   111
  in normh t end
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   112
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   113
and norm_term2(asol,iTs,t) : term =
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   114
  let fun normT(Type(a,Ts)) = Type(a, normTs Ts)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   115
	| normT(TFree _) = raise SAME
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   116
	| normT(TVar(v,_)) = (case Vartab.lookup (iTs, v) of
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   117
		Some(U) => normTh U
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   118
	      | None => raise SAME)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   119
      and normTh T = ((normT T) handle SAME => T)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   120
      and normTs([]) = raise SAME
2181
9c2b4728641d In-lined the one function call to normTsh
paulson
parents: 2142
diff changeset
   121
	| normTs(T::Ts) = ((normT T :: (normTs Ts handle SAME => Ts))
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   122
			   handle SAME => T :: normTs Ts)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   123
      and norm(Const(a,T)) = Const(a, normT T)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   124
	| norm(Free(a,T)) = Free(a, normT T)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   125
	| norm(Var (w,T)) =
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   126
	    (case Vartab.lookup (asol, w) of
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   127
		Some u => normh u
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   128
	      | None   => Var(w,normT T))
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   129
	| norm(Abs(a,T,body)) =
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   130
	      (Abs(a,normT T,normh body) handle SAME => Abs(a, T, norm body))
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   131
	| norm(Abs(_,_,body) $ t) = normh(subst_bound (t, body))
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   132
	| norm(f $ t) =
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   133
	    ((case norm f of
2191
58383908f177 Speedups involving norm
paulson
parents: 2181
diff changeset
   134
	       Abs(_,_,body) => normh(subst_bound (t, body))
1500
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   135
	     | nf => nf $ normh t)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   136
	    handle SAME => f $ norm t)
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   137
	| norm _ =  raise SAME
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   138
      and normh t = (norm t) handle SAME => t
b2de3b3277b8 Elimination of fully-functorial style.
paulson
parents: 1460
diff changeset
   139
  in normh t end;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   140
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   141
(*curried version might be slower in recursive calls*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   142
fun norm_term (env as Envir{asol,iTs,...}) t : term =
8407
d522ad1809e9 Envir now uses Vartab instead of association lists.
berghofe
parents: 5289
diff changeset
   143
        if Vartab.is_empty iTs then norm_term1(asol, t) else norm_term2(asol,iTs, t)
719
e3e1d1a6d408 Pure/envir/norm_term: replaced equality test for [] by null
lcp
parents: 247
diff changeset
   144
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   145
end;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   146