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