src/Pure/envir.ML
changeset 21795 d7dcc3dfa7e9
parent 21695 6c07cc87fe2b
child 22174 f2bf6bcd4a98
equal deleted inserted replaced
21794:1a9f57f1bc3a 21795:d7dcc3dfa7e9
    38   val subst_TVars: Type.tyenv -> term -> term
    38   val subst_TVars: Type.tyenv -> term -> term
    39   val subst_Vars: tenv -> term -> term
    39   val subst_Vars: tenv -> term -> term
    40   val subst_vars: Type.tyenv * tenv -> term -> term
    40   val subst_vars: Type.tyenv * tenv -> term -> term
    41   val expand_atom: typ -> typ * term -> term
    41   val expand_atom: typ -> typ * term -> term
    42   val expand_term: (term -> (typ * term) option) -> term -> term
    42   val expand_term: (term -> (typ * term) option) -> term -> term
       
    43   val expand_term_frees: ((string * typ) * term) list -> term -> term
    43 end;
    44 end;
    44 
    45 
    45 structure Envir : ENVIR =
    46 structure Envir : ENVIR =
    46 struct
    47 struct
    47 
    48 
   294         | subst (Abs (a, T, t)) = Abs(a, typ_subst_TVars iTs T, subst t)
   295         | subst (Abs (a, T, t)) = Abs(a, typ_subst_TVars iTs T, subst t)
   295         | subst (f $ t) = subst f $ subst t
   296         | subst (f $ t) = subst f $ subst t
   296   in subst end;
   297   in subst end;
   297 
   298 
   298 
   299 
   299 (* expand defined atoms *)
   300 (* expand defined atoms -- with local beta reduction *)
   300 
   301 
   301 fun expand_atom T (U, u) =
   302 fun expand_atom T (U, u) =
   302   subst_TVars (Type.raw_match (U, T) Vartab.empty) u
   303   subst_TVars (Type.raw_match (U, T) Vartab.empty) u
   303   handle Type.TYPE_MATCH => raise TYPE ("expand_atom: ill-typed replacement", [T, U], [u]);
   304   handle Type.TYPE_MATCH => raise TYPE ("expand_atom: ill-typed replacement", [T, U], [u]);
   304 
   305 
   305 fun expand_term get_def =
   306 fun expand_term get =
   306   let
   307   let
   307     fun expand tm =
   308     fun expand tm =
   308       let
   309       let
   309         val (head, args) = Term.strip_comb tm;
   310         val (head, args) = Term.strip_comb tm;
   310         val args' = map expand args;
   311         val args' = map expand args;
   311         fun comb head' = Term.list_comb (head', args');
   312         fun comb head' = Term.list_comb (head', args');
   312       in
   313       in
   313         (case head of
   314         (case head of
   314           Abs (x, T, t) => comb (Abs (x, T, expand t))
   315           Abs (x, T, t) => comb (Abs (x, T, expand t))
   315         | _ =>
   316         | _ =>
   316             (case get_def head of
   317             (case get head of
   317               SOME def => Term.betapplys (expand_atom (Term.fastype_of head) def, args')
   318               SOME def => Term.betapplys (expand_atom (Term.fastype_of head) def, args')
   318             | NONE => comb head)
   319             | NONE => comb head)
   319         | _ => comb head)
   320         | _ => comb head)
   320       end;
   321       end;
   321   in expand end;
   322   in expand end;
   322 
   323 
       
   324 fun expand_term_frees defs =
       
   325   let
       
   326     val eqs = map (fn ((x, U), u) => (x, (U, u))) defs;
       
   327     val get = fn Free (x, _) => AList.lookup (op =) eqs x | _ => NONE;
       
   328   in expand_term get end;
       
   329 
   323 end;
   330 end;