src/Pure/envir.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 43278 1fbdcebb364b
child 51701 1e29891759c4
permissions -rw-r--r--
tuned signature;
tuned comments;
wenzelm@247
     1
(*  Title:      Pure/envir.ML
wenzelm@247
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@10485
     3
wenzelm@32031
     4
Free-form environments.  The type of a term variable / sort of a type variable is
wenzelm@32031
     5
part of its name.  The lookup function must apply type substitutions,
berghofe@15797
     6
since they may change the identity of a variable.
clasohm@0
     7
*)
clasohm@0
     8
wenzelm@247
     9
signature ENVIR =
clasohm@0
    10
sig
wenzelm@32018
    11
  type tenv = (typ * term) Vartab.table
wenzelm@32031
    12
  datatype env = Envir of {maxidx: int, tenv: tenv, tyenv: Type.tyenv}
wenzelm@32031
    13
  val maxidx_of: env -> int
wenzelm@32031
    14
  val term_env: env -> tenv
berghofe@15797
    15
  val type_env: env -> Type.tyenv
wenzelm@32031
    16
  val is_empty: env -> bool
wenzelm@32031
    17
  val empty: int -> env
wenzelm@32031
    18
  val merge: env * env -> env
wenzelm@26638
    19
  val insert_sorts: env -> sort list -> sort list
wenzelm@10485
    20
  val genvars: string -> env * typ list -> env * term list
wenzelm@10485
    21
  val genvar: string -> env * typ -> env * term
wenzelm@51700
    22
  val lookup1: tenv -> indexname * typ -> term option
wenzelm@51700
    23
  val lookup: env -> indexname * typ -> term option
wenzelm@51700
    24
  val update: (indexname * typ) * term -> env -> env
wenzelm@19861
    25
  val above: env -> int -> bool
wenzelm@51700
    26
  val vupdate: (indexname * typ) * term -> env -> env
wenzelm@32018
    27
  val norm_type_same: Type.tyenv -> typ Same.operation
wenzelm@32018
    28
  val norm_types_same: Type.tyenv -> typ list Same.operation
berghofe@15797
    29
  val norm_type: Type.tyenv -> typ -> typ
wenzelm@32018
    30
  val norm_term_same: env -> term Same.operation
wenzelm@32018
    31
  val norm_term: env -> term -> term
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
wenzelm@32034
    37
  val subst_type_same: Type.tyenv -> typ Same.operation
wenzelm@32034
    38
  val subst_term_types_same: Type.tyenv -> term Same.operation
wenzelm@32034
    39
  val subst_term_same: Type.tyenv * tenv -> term Same.operation
wenzelm@32034
    40
  val subst_type: Type.tyenv -> typ -> typ
wenzelm@32034
    41
  val subst_term_types: Type.tyenv -> term -> term
wenzelm@32034
    42
  val subst_term: Type.tyenv * tenv -> term -> term
wenzelm@19422
    43
  val expand_atom: typ -> typ * term -> term
wenzelm@21695
    44
  val expand_term: (term -> (typ * term) option) -> term -> term
wenzelm@21795
    45
  val expand_term_frees: ((string * typ) * term) list -> term -> term
clasohm@0
    46
end;
clasohm@0
    47
wenzelm@32031
    48
structure Envir: ENVIR =
clasohm@0
    49
struct
clasohm@0
    50
wenzelm@32031
    51
(** datatype env **)
wenzelm@32031
    52
wenzelm@32031
    53
(*Updating can destroy environment in 2 ways!
wenzelm@32031
    54
   (1) variables out of range
wenzelm@32031
    55
   (2) circular assignments
clasohm@0
    56
*)
wenzelm@32031
    57
wenzelm@32018
    58
type tenv = (typ * term) Vartab.table;
berghofe@15797
    59
clasohm@0
    60
datatype env = Envir of
wenzelm@32031
    61
 {maxidx: int,          (*upper bound of maximum index of vars*)
wenzelm@32031
    62
  tenv: tenv,           (*assignments to Vars*)
wenzelm@32031
    63
  tyenv: Type.tyenv};   (*assignments to TVars*)
wenzelm@32031
    64
wenzelm@32796
    65
fun make_env (maxidx, tenv, tyenv) =
wenzelm@32796
    66
  Envir {maxidx = maxidx, tenv = tenv, tyenv = tyenv};
wenzelm@32031
    67
wenzelm@32031
    68
fun maxidx_of (Envir {maxidx, ...}) = maxidx;
wenzelm@32031
    69
fun term_env (Envir {tenv, ...}) = tenv;
wenzelm@32031
    70
fun type_env (Envir {tyenv, ...}) = tyenv;
wenzelm@32031
    71
wenzelm@32031
    72
fun is_empty env =
wenzelm@32031
    73
  Vartab.is_empty (term_env env) andalso
wenzelm@32031
    74
  Vartab.is_empty (type_env env);
clasohm@0
    75
wenzelm@32031
    76
wenzelm@32031
    77
(* build env *)
wenzelm@32031
    78
wenzelm@32031
    79
fun empty maxidx = make_env (maxidx, Vartab.empty, Vartab.empty);
clasohm@0
    80
wenzelm@32031
    81
fun merge
wenzelm@32031
    82
   (Envir {maxidx = maxidx1, tenv = tenv1, tyenv = tyenv1},
wenzelm@32031
    83
    Envir {maxidx = maxidx2, tenv = tenv2, tyenv = tyenv2}) =
wenzelm@32031
    84
  make_env (Int.max (maxidx1, maxidx2),
wenzelm@32031
    85
    Vartab.merge (op =) (tenv1, tenv2),
wenzelm@32031
    86
    Vartab.merge (op =) (tyenv1, tyenv2));
wenzelm@32031
    87
wenzelm@32031
    88
wenzelm@32031
    89
(*NB: type unification may invent new sorts*)  (* FIXME tenv!? *)
wenzelm@26638
    90
val insert_sorts = Vartab.fold (fn (_, (_, T)) => Sorts.insert_typ T) o type_env;
wenzelm@26638
    91
clasohm@0
    92
(*Generate a list of distinct variables.
clasohm@0
    93
  Increments index to make them distinct from ALL present variables. *)
wenzelm@32031
    94
fun genvars name (Envir {maxidx, tenv, tyenv}, Ts) : env * term list =
wenzelm@32018
    95
  let
wenzelm@32018
    96
    fun genvs (_, [] : typ list) : term list = []
wenzelm@32018
    97
      | genvs (n, [T]) = [Var ((name, maxidx + 1), T)]
wenzelm@32018
    98
      | genvs (n, T :: Ts) =
wenzelm@32018
    99
          Var ((name ^ radixstring (26, "a" , n), maxidx + 1), T)
wenzelm@32018
   100
            :: genvs (n + 1, Ts);
wenzelm@32031
   101
  in (Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv}, genvs (0, Ts)) end;
clasohm@0
   102
clasohm@0
   103
(*Generate a variable.*)
wenzelm@32018
   104
fun genvar name (env, T) : env * term =
wenzelm@32018
   105
  let val (env', [v]) = genvars name (env, [T])
wenzelm@32018
   106
  in (env', v) end;
clasohm@0
   107
wenzelm@32031
   108
fun var_clash xi T T' =
wenzelm@32031
   109
  raise TYPE ("Variable " ^ quote (Term.string_of_vname xi) ^ " has two distinct types",
wenzelm@32018
   110
    [T', T], []);
clasohm@0
   111
wenzelm@32031
   112
fun lookup_check check tenv (xi, T) =
wenzelm@32031
   113
  (case Vartab.lookup tenv xi of
wenzelm@32018
   114
    NONE => NONE
wenzelm@32031
   115
  | SOME (U, t) => if check (T, U) then SOME t else var_clash xi T U);
berghofe@15797
   116
wenzelm@51700
   117
(*When dealing with environments produced by matching instead
wenzelm@51700
   118
  of unification, there is no need to chase assigned TVars.
wenzelm@51700
   119
  In this case, we can simply ignore the type substitution
wenzelm@51700
   120
  and use = instead of eq_type.*)
wenzelm@51700
   121
val lookup1 = lookup_check (op =);
berghofe@15797
   122
wenzelm@51700
   123
fun lookup2 (tyenv, tenv) = lookup_check (Type.eq_type tyenv) tenv;
berghofe@15797
   124
wenzelm@51700
   125
fun lookup (Envir {tenv, tyenv, ...}) = lookup2 (tyenv, tenv);
clasohm@0
   126
wenzelm@51700
   127
fun update ((xi, T), t) (Envir {maxidx, tenv, tyenv}) =
wenzelm@32031
   128
  Envir {maxidx = maxidx, tenv = Vartab.update_new (xi, (T, t)) tenv, tyenv = tyenv};
wenzelm@247
   129
paulson@2142
   130
(*Determine if the least index updated exceeds lim*)
wenzelm@32031
   131
fun above (Envir {tenv, tyenv, ...}) lim =
wenzelm@32031
   132
  (case Vartab.min_key tenv of SOME (_, i) => i > lim | NONE => true) andalso
wenzelm@32031
   133
  (case Vartab.min_key tyenv of SOME (_, i) => i > lim | NONE => true);
wenzelm@247
   134
clasohm@0
   135
(*Update, checking Var-Var assignments: try to suppress higher indexes*)
wenzelm@51700
   136
fun vupdate (aU as (a, U), t) (env as Envir {tyenv, ...}) =
wenzelm@32031
   137
  (case t of
wenzelm@32031
   138
    Var (nT as (name', T)) =>
wenzelm@32031
   139
      if a = name' then env     (*cycle!*)
wenzelm@35408
   140
      else if Term_Ord.indexname_ord (a, name') = LESS then
wenzelm@51700
   141
        (case lookup env nT of  (*if already assigned, chase*)
wenzelm@51700
   142
          NONE => update (nT, Var (a, T)) env
wenzelm@51700
   143
        | SOME u => vupdate (aU, u) env)
wenzelm@51700
   144
      else update (aU, t) env
wenzelm@51700
   145
  | _ => update (aU, t) env);
clasohm@0
   146
clasohm@0
   147
clasohm@0
   148
wenzelm@32031
   149
(** beta normalization wrt. environment **)
clasohm@0
   150
wenzelm@32031
   151
(*Chases variables in env.  Does not exploit sharing of variable bindings
wenzelm@32031
   152
  Does not check types, so could loop.*)
paulson@1500
   153
wenzelm@32018
   154
local
wenzelm@32018
   155
wenzelm@32031
   156
fun norm_type0 tyenv : typ Same.operation =
wenzelm@32018
   157
  let
wenzelm@32018
   158
    fun norm (Type (a, Ts)) = Type (a, Same.map norm Ts)
wenzelm@32018
   159
      | norm (TFree _) = raise Same.SAME
wenzelm@32018
   160
      | norm (TVar v) =
wenzelm@32031
   161
          (case Type.lookup tyenv v of
wenzelm@32018
   162
            SOME U => Same.commit norm U
wenzelm@32018
   163
          | NONE => raise Same.SAME);
wenzelm@32018
   164
  in norm end;
clasohm@0
   165
wenzelm@32031
   166
fun norm_term1 tenv : term Same.operation =
wenzelm@32018
   167
  let
wenzelm@32018
   168
    fun norm (Var v) =
wenzelm@51700
   169
          (case lookup1 tenv v of
wenzelm@32018
   170
            SOME u => Same.commit norm u
wenzelm@32018
   171
          | NONE => raise Same.SAME)
wenzelm@32031
   172
      | norm (Abs (a, T, body)) = Abs (a, T, norm body)
wenzelm@32018
   173
      | norm (Abs (_, _, body) $ t) = Same.commit norm (subst_bound (t, body))
wenzelm@32018
   174
      | norm (f $ t) =
wenzelm@32018
   175
          ((case norm f of
wenzelm@32018
   176
             Abs (_, _, body) => Same.commit norm (subst_bound (t, body))
wenzelm@32018
   177
           | nf => nf $ Same.commit norm t)
wenzelm@32018
   178
          handle Same.SAME => f $ norm t)
wenzelm@32018
   179
      | norm _ = raise Same.SAME;
wenzelm@32018
   180
  in norm end;
clasohm@0
   181
wenzelm@32031
   182
fun norm_term2 tenv tyenv : term Same.operation =
wenzelm@32018
   183
  let
wenzelm@32031
   184
    val normT = norm_type0 tyenv;
wenzelm@32018
   185
    fun norm (Const (a, T)) = Const (a, normT T)
wenzelm@32018
   186
      | norm (Free (a, T)) = Free (a, normT T)
wenzelm@32018
   187
      | norm (Var (xi, T)) =
wenzelm@32031
   188
          (case lookup2 (tyenv, tenv) (xi, T) of
wenzelm@32018
   189
            SOME u => Same.commit norm u
wenzelm@32018
   190
          | NONE => Var (xi, normT T))
wenzelm@32018
   191
      | norm (Abs (a, T, body)) =
wenzelm@32018
   192
          (Abs (a, normT T, Same.commit norm body)
wenzelm@32018
   193
            handle Same.SAME => Abs (a, T, norm body))
wenzelm@32018
   194
      | norm (Abs (_, _, body) $ t) = Same.commit norm (subst_bound (t, body))
wenzelm@32018
   195
      | norm (f $ t) =
wenzelm@32018
   196
          ((case norm f of
wenzelm@32018
   197
             Abs (_, _, body) => Same.commit norm (subst_bound (t, body))
wenzelm@32018
   198
           | nf => nf $ Same.commit norm t)
wenzelm@32018
   199
          handle Same.SAME => f $ norm t)
wenzelm@32018
   200
      | norm _ = raise Same.SAME;
wenzelm@32018
   201
  in norm end;
berghofe@11513
   202
wenzelm@32018
   203
in
wenzelm@32018
   204
wenzelm@32031
   205
fun norm_type_same tyenv T =
wenzelm@32031
   206
  if Vartab.is_empty tyenv then raise Same.SAME
wenzelm@32031
   207
  else norm_type0 tyenv T;
clasohm@0
   208
wenzelm@32031
   209
fun norm_types_same tyenv Ts =
wenzelm@32031
   210
  if Vartab.is_empty tyenv then raise Same.SAME
wenzelm@32031
   211
  else Same.map (norm_type0 tyenv) Ts;
wenzelm@32018
   212
wenzelm@32031
   213
fun norm_type tyenv T = norm_type_same tyenv T handle Same.SAME => T;
berghofe@11513
   214
wenzelm@32031
   215
fun norm_term_same (Envir {tenv, tyenv, ...}) =
wenzelm@32031
   216
  if Vartab.is_empty tyenv then norm_term1 tenv
wenzelm@32031
   217
  else norm_term2 tenv tyenv;
wenzelm@10485
   218
wenzelm@32018
   219
fun norm_term envir t = norm_term_same envir t handle Same.SAME => t;
berghofe@25471
   220
fun beta_norm t = if Term.has_abs t then norm_term (empty 0) t else t;
lcp@719
   221
wenzelm@32018
   222
end;
berghofe@11513
   223
berghofe@11513
   224
berghofe@12231
   225
(*Put a term into head normal form for unification.*)
berghofe@12231
   226
wenzelm@32018
   227
fun head_norm env =
berghofe@12231
   228
  let
wenzelm@32018
   229
    fun norm (Var v) =
wenzelm@51700
   230
        (case lookup env v of
skalberg@15531
   231
          SOME u => head_norm env u
wenzelm@32018
   232
        | NONE => raise Same.SAME)
wenzelm@32018
   233
      | norm (Abs (a, T, body)) = Abs (a, T, norm body)
wenzelm@32018
   234
      | norm (Abs (_, _, body) $ t) = Same.commit norm (subst_bound (t, body))
wenzelm@32018
   235
      | norm (f $ t) =
wenzelm@32018
   236
          (case norm f of
wenzelm@32018
   237
            Abs (_, _, body) => Same.commit norm (subst_bound (t, body))
wenzelm@32018
   238
          | nf => nf $ t)
wenzelm@32018
   239
      | norm _ = raise Same.SAME;
wenzelm@32018
   240
  in Same.commit norm end;
berghofe@12231
   241
berghofe@12231
   242
wenzelm@18937
   243
(*Eta-contract a term (fully)*)
wenzelm@18937
   244
wenzelm@22174
   245
local
wenzelm@22174
   246
wenzelm@32018
   247
fun decr lev (Bound i) = if i >= lev then Bound (i - 1) else raise Same.SAME
wenzelm@22174
   248
  | decr lev (Abs (a, T, body)) = Abs (a, T, decr (lev + 1) body)
wenzelm@32018
   249
  | decr lev (t $ u) = (decr lev t $ decrh lev u handle Same.SAME => t $ decr lev u)
wenzelm@32018
   250
  | decr _ _ = raise Same.SAME
wenzelm@32018
   251
and decrh lev t = (decr lev t handle Same.SAME => t);
wenzelm@20670
   252
wenzelm@22174
   253
fun eta (Abs (a, T, body)) =
wenzelm@22174
   254
    ((case eta body of
wenzelm@22174
   255
        body' as (f $ Bound 0) =>
wenzelm@42083
   256
          if Term.is_dependent f then Abs (a, T, body')
wenzelm@22174
   257
          else decrh 0 f
wenzelm@32018
   258
     | body' => Abs (a, T, body')) handle Same.SAME =>
wenzelm@22174
   259
        (case body of
wenzelm@22174
   260
          f $ Bound 0 =>
wenzelm@42083
   261
            if Term.is_dependent f then raise Same.SAME
wenzelm@22174
   262
            else decrh 0 f
wenzelm@32018
   263
        | _ => raise Same.SAME))
wenzelm@32018
   264
  | eta (t $ u) = (eta t $ Same.commit eta u handle Same.SAME => t $ eta u)
wenzelm@32018
   265
  | eta _ = raise Same.SAME;
wenzelm@22174
   266
wenzelm@22174
   267
in
wenzelm@22174
   268
wenzelm@22174
   269
fun eta_contract t =
wenzelm@32018
   270
  if Term.has_abs t then Same.commit eta t else t;
wenzelm@18937
   271
wenzelm@18937
   272
val beta_eta_contract = eta_contract o beta_norm;
wenzelm@18937
   273
wenzelm@22174
   274
end;
wenzelm@22174
   275
wenzelm@18937
   276
berghofe@12231
   277
(*finds type of term without checking that combinations are consistent
berghofe@12231
   278
  Ts holds types of bound variables*)
wenzelm@32031
   279
fun fastype (Envir {tyenv, ...}) =
wenzelm@32031
   280
  let
wenzelm@32031
   281
    val funerr = "fastype: expected function type";
berghofe@12231
   282
    fun fast Ts (f $ u) =
paulson@32648
   283
          (case Type.devar tyenv (fast Ts f) of
wenzelm@32031
   284
            Type ("fun", [_, T]) => T
paulson@32648
   285
          | TVar v => raise TERM (funerr, [f $ u])
wenzelm@32031
   286
          | _ => raise TERM (funerr, [f $ u]))
berghofe@12231
   287
      | fast Ts (Const (_, T)) = T
berghofe@12231
   288
      | fast Ts (Free (_, T)) = T
berghofe@12231
   289
      | fast Ts (Bound i) =
wenzelm@43278
   290
          (nth Ts i handle General.Subscript => raise TERM ("fastype: Bound", [Bound i]))
wenzelm@20670
   291
      | fast Ts (Var (_, T)) = T
wenzelm@32031
   292
      | fast Ts (Abs (_, T, u)) = T --> fast (T :: Ts) u;
wenzelm@32031
   293
  in fast end;
berghofe@12231
   294
berghofe@15797
   295
wenzelm@32034
   296
wenzelm@32034
   297
(** plain substitution -- without variable chasing **)
wenzelm@32034
   298
wenzelm@32034
   299
local
berghofe@15797
   300
wenzelm@32034
   301
fun subst_type0 tyenv = Term_Subst.map_atypsT_same
wenzelm@32034
   302
  (fn TVar v =>
wenzelm@32034
   303
        (case Type.lookup tyenv v of
wenzelm@32034
   304
          SOME U => U
wenzelm@32034
   305
        | NONE => raise Same.SAME)
wenzelm@32034
   306
    | _ => raise Same.SAME);
wenzelm@32034
   307
wenzelm@32034
   308
fun subst_term1 tenv = Term_Subst.map_aterms_same
wenzelm@32034
   309
  (fn Var v =>
wenzelm@51700
   310
        (case lookup1 tenv v of
wenzelm@32034
   311
          SOME u => u
wenzelm@32034
   312
        | NONE => raise Same.SAME)
wenzelm@32034
   313
    | _ => raise Same.SAME);
berghofe@15797
   314
wenzelm@32034
   315
fun subst_term2 tenv tyenv : term Same.operation =
wenzelm@32034
   316
  let
wenzelm@32034
   317
    val substT = subst_type0 tyenv;
wenzelm@32034
   318
    fun subst (Const (a, T)) = Const (a, substT T)
wenzelm@32034
   319
      | subst (Free (a, T)) = Free (a, substT T)
wenzelm@32034
   320
      | subst (Var (xi, T)) =
wenzelm@51700
   321
          (case lookup1 tenv (xi, T) of
wenzelm@32034
   322
            SOME u => u
wenzelm@32034
   323
          | NONE => Var (xi, substT T))
wenzelm@32034
   324
      | subst (Bound _) = raise Same.SAME
wenzelm@32034
   325
      | subst (Abs (a, T, t)) =
wenzelm@32034
   326
          (Abs (a, substT T, Same.commit subst t)
wenzelm@32034
   327
            handle Same.SAME => Abs (a, T, subst t))
wenzelm@32034
   328
      | subst (t $ u) = (subst t $ Same.commit subst u handle Same.SAME => t $ subst u);
wenzelm@32034
   329
  in subst end;
wenzelm@32034
   330
wenzelm@32034
   331
in
berghofe@15797
   332
wenzelm@32034
   333
fun subst_type_same tyenv T =
wenzelm@32034
   334
  if Vartab.is_empty tyenv then raise Same.SAME
wenzelm@32034
   335
  else subst_type0 tyenv T;
wenzelm@32034
   336
wenzelm@32034
   337
fun subst_term_types_same tyenv t =
wenzelm@32034
   338
  if Vartab.is_empty tyenv then raise Same.SAME
wenzelm@32034
   339
  else Term_Subst.map_types_same (subst_type0 tyenv) t;
wenzelm@32034
   340
wenzelm@32034
   341
fun subst_term_same (tyenv, tenv) =
wenzelm@32034
   342
  if Vartab.is_empty tenv then subst_term_types_same tyenv
wenzelm@32034
   343
  else if Vartab.is_empty tyenv then subst_term1 tenv
wenzelm@32034
   344
  else subst_term2 tenv tyenv;
wenzelm@32034
   345
wenzelm@32034
   346
fun subst_type tyenv T = subst_type_same tyenv T handle Same.SAME => T;
wenzelm@32034
   347
fun subst_term_types tyenv t = subst_term_types_same tyenv t handle Same.SAME => t;
wenzelm@32034
   348
fun subst_term envs t = subst_term_same envs t handle Same.SAME => t;
wenzelm@32034
   349
wenzelm@32034
   350
end;
berghofe@15797
   351
wenzelm@18937
   352
wenzelm@32034
   353
wenzelm@32034
   354
(** expand defined atoms -- with local beta reduction **)
wenzelm@18937
   355
wenzelm@19422
   356
fun expand_atom T (U, u) =
wenzelm@32034
   357
  subst_term_types (Type.raw_match (U, T) Vartab.empty) u
wenzelm@32031
   358
    handle Type.TYPE_MATCH => raise TYPE ("expand_atom: ill-typed replacement", [T, U], [u]);
wenzelm@18937
   359
wenzelm@21795
   360
fun expand_term get =
wenzelm@21695
   361
  let
wenzelm@21695
   362
    fun expand tm =
wenzelm@21695
   363
      let
wenzelm@21695
   364
        val (head, args) = Term.strip_comb tm;
wenzelm@21695
   365
        val args' = map expand args;
wenzelm@21695
   366
        fun comb head' = Term.list_comb (head', args');
wenzelm@21695
   367
      in
wenzelm@21695
   368
        (case head of
wenzelm@21695
   369
          Abs (x, T, t) => comb (Abs (x, T, expand t))
wenzelm@21695
   370
        | _ =>
wenzelm@32031
   371
          (case get head of
wenzelm@32031
   372
            SOME def => Term.betapplys (expand_atom (Term.fastype_of head) def, args')
wenzelm@32031
   373
          | NONE => comb head))
wenzelm@21695
   374
      end;
wenzelm@21695
   375
  in expand end;
wenzelm@21695
   376
wenzelm@21795
   377
fun expand_term_frees defs =
wenzelm@21795
   378
  let
wenzelm@21795
   379
    val eqs = map (fn ((x, U), u) => (x, (U, u))) defs;
wenzelm@21795
   380
    val get = fn Free (x, _) => AList.lookup (op =) eqs x | _ => NONE;
wenzelm@21795
   381
  in expand_term get end;
wenzelm@21795
   382
clasohm@0
   383
end;