src/Tools/Code/code_thingol.ML
changeset 44996 410eea28b0f7
parent 44855 f4a6786057d9
child 44997 e534939f880d
equal deleted inserted replaced
44995:eff5bccc9b76 44996:410eea28b0f7
   598     apfst (fn t => Abs (x, T, t)) (annotate_term (t1, t) tvar_names)
   598     apfst (fn t => Abs (x, T, t)) (annotate_term (t1, t) tvar_names)
   599   | annotate_term (Free _, t as Free _) tvar_names = (t, tvar_names)
   599   | annotate_term (Free _, t as Free _) tvar_names = (t, tvar_names)
   600   | annotate_term (Var _, t as Var _) tvar_names = (t, tvar_names)
   600   | annotate_term (Var _, t as Var _) tvar_names = (t, tvar_names)
   601   | annotate_term (Bound   _, t as Bound _) tvar_names = (t, tvar_names)
   601   | annotate_term (Bound   _, t as Bound _) tvar_names = (t, tvar_names)
   602 
   602 
   603 fun annotate_eqns thy (c, ty) eqns = 
   603 fun annotate thy (c, ty) args rhs =
   604   let
   604   let
   605     val ctxt = ProofContext.init_global thy |> Config.put Type_Infer_Context.const_sorts false
   605     val ctxt = ProofContext.init_global thy |> Config.put Type_Infer_Context.const_sorts false
   606     val erase = map_types (fn _ => Type_Infer.anyT [])
   606     val erase = map_types (fn _ => Type_Infer.anyT [])
   607     val reinfer = singleton (Type_Infer_Context.infer_types ctxt)
   607     val reinfer = singleton (Type_Infer_Context.infer_types ctxt)
   608     fun add_annotations (args, (rhs, some_abs)) =
   608     val lhs = list_comb (Const (c, ty), map (map_types Type.strip_sorts o fst) args)
   609       let
   609     val reinferred_rhs = snd (Logic.dest_equals (reinfer (Logic.mk_equals (lhs, erase rhs))))
   610         val lhs = list_comb (Const (c, ty), map (map_types Type.strip_sorts o fst) args)
       
   611         val reinferred_rhs = snd (Logic.dest_equals (reinfer (Logic.mk_equals (lhs, erase rhs))))
       
   612         val (rhs', _) = annotate_term (reinferred_rhs, rhs) []
       
   613      in
       
   614         (args, (rhs', some_abs))
       
   615      end
       
   616   in
   610   in
   617     map (apfst add_annotations) eqns
   611     fst (annotate_term (reinferred_rhs, rhs) [])
   618   end;
   612   end
       
   613 
       
   614 fun annotate_eqns thy (c, ty) eqns = 
       
   615   map (apfst (fn (args, (rhs, some_abs)) => (args, (annotate thy (c, ty) args rhs, some_abs)))) eqns
   619 
   616 
   620 (* translation *)
   617 (* translation *)
   621 
   618 
   622 fun ensure_tyco thy algbr eqngr permissive tyco =
   619 fun ensure_tyco thy algbr eqngr permissive tyco =
   623   let
   620   let
   920 fun ensure_value thy algbr eqngr t =
   917 fun ensure_value thy algbr eqngr t =
   921   let
   918   let
   922     val ty = fastype_of t;
   919     val ty = fastype_of t;
   923     val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
   920     val vs = fold_term_types (K (fold_atyps (insert (eq_fst op =)
   924       o dest_TFree))) t [];
   921       o dest_TFree))) t [];
       
   922     val t' = annotate thy (Term.dummy_patternN, ty) [] (Code.subst_signatures thy t) 
   925     val stmt_value =
   923     val stmt_value =
   926       fold_map (translate_tyvar_sort thy algbr eqngr false) vs
   924       fold_map (translate_tyvar_sort thy algbr eqngr false) vs
   927       ##>> translate_typ thy algbr eqngr false ty
   925       ##>> translate_typ thy algbr eqngr false ty
   928       ##>> translate_term thy algbr eqngr false NONE (Code.subst_signatures thy t, NONE)
   926       ##>> translate_term thy algbr eqngr false NONE (t', NONE)
   929       #>> (fn ((vs, ty), t) => Fun
   927       #>> (fn ((vs, ty), t) => Fun
   930         (Term.dummy_patternN, (((vs, ty), [(([], t), (NONE, true))]), NONE)));
   928         (Term.dummy_patternN, (((vs, ty), [(([], t), (NONE, true))]), NONE)));
   931     fun term_value (dep, (naming, program1)) =
   929     fun term_value (dep, (naming, program1)) =
   932       let
   930       let
   933         val Fun (_, ((vs_ty, [(([], t), _)]), _)) =
   931         val Fun (_, ((vs_ty, [(([], t), _)]), _)) =