src/Tools/code/code_funcgr.ML
changeset 28724 4656aacba2bc
parent 28423 9fc3befd8191
child 28924 5c8781b7d6a4
equal deleted inserted replaced
28723:c4fcffe0fe48 28724:4656aacba2bc
    12   val eqns: T -> string -> (thm * bool) list
    12   val eqns: T -> string -> (thm * bool) list
    13   val typ: T -> string -> (string * sort) list * typ
    13   val typ: T -> string -> (string * sort) list * typ
    14   val all: T -> string list
    14   val all: T -> string list
    15   val pretty: theory -> T -> Pretty.T
    15   val pretty: theory -> T -> Pretty.T
    16   val make: theory -> string list -> T
    16   val make: theory -> string list -> T
    17   val eval_conv: theory -> (term -> term * (T -> term -> thm)) -> cterm -> thm
    17   val eval_conv: theory -> (term -> term * (T -> thm)) -> cterm -> thm
    18   val eval_term: theory -> (term -> term * (T -> term -> 'a)) -> term -> 'a
    18   val eval_term: theory -> (term -> term * (T -> 'a)) -> term -> 'a
    19   val timing: bool ref
    19   val timing: bool ref
    20 end
    20 end
    21 
    21 
    22 structure Code_Funcgr : CODE_FUNCGR =
    22 structure Code_Funcgr : CODE_FUNCGR =
    23 struct
    23 struct
   216 
   216 
   217 (** retrieval interfaces **)
   217 (** retrieval interfaces **)
   218 
   218 
   219 val ensure_consts = ensure_consts;
   219 val ensure_consts = ensure_consts;
   220 
   220 
   221 fun proto_eval thy cterm_of evaluator_fr evaluator proto_ct funcgr =
   221 fun proto_eval thy cterm_of evaluator_lift evaluator proto_ct funcgr =
   222   let
   222   let
   223     val ct = cterm_of proto_ct;
   223     val ct = cterm_of proto_ct;
   224     val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
   224     val _ = Sign.no_vars (Syntax.pp_global thy) (Thm.term_of ct);
   225     val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
   225     val _ = Term.fold_types (Type.no_tvars #> K I) (Thm.term_of ct) ();
   226     fun consts_of t = fold_aterms (fn Const c_ty => cons c_ty | _ => I)
   226     fun consts_of t =
   227       t [];
   227       fold_aterms (fn Const c_ty => cons c_ty | _ => I) t [];
   228     val algebra = Code.coregular_algebra thy;
   228     val algebra = Code.coregular_algebra thy;
   229     val thm = Code.preprocess_conv thy ct;
   229     val thm = Code.preprocess_conv thy ct;
   230     val ct' = Thm.rhs_of thm;
   230     val ct' = Thm.rhs_of thm;
   231     val t' = Thm.term_of ct';
   231     val t' = Thm.term_of ct';
   232     val consts = map fst (consts_of t');
   232     val consts = map fst (consts_of t');
   233     val funcgr' = ensure_consts thy algebra consts funcgr;
   233     val funcgr' = ensure_consts thy algebra consts funcgr;
   234     val (t'', evaluator') = apsnd evaluator_fr (evaluator t');
   234     val (t'', evaluator_funcgr) = evaluator t';
   235     val consts' = consts_of t'';
   235     val consts' = consts_of t'';
   236     val dicts = instances_of_consts thy algebra funcgr' consts';
   236     val dicts = instances_of_consts thy algebra funcgr' consts';
   237     val funcgr'' = ensure_consts thy algebra dicts funcgr';
   237     val funcgr'' = ensure_consts thy algebra dicts funcgr';
   238   in (evaluator' thm funcgr'' t'', funcgr'') end;
   238   in (evaluator_lift evaluator_funcgr thm funcgr'', funcgr'') end;
   239 
   239 
   240 fun proto_eval_conv thy =
   240 fun proto_eval_conv thy =
   241   let
   241   let
   242     fun evaluator evaluator' thm1 funcgr t =
   242     fun evaluator_lift evaluator thm1 funcgr =
   243       let
   243       let
   244         val thm2 = evaluator' funcgr t;
   244         val thm2 = evaluator funcgr;
   245         val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
   245         val thm3 = Code.postprocess_conv thy (Thm.rhs_of thm2);
   246       in
   246       in
   247         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   247         Thm.transitive thm1 (Thm.transitive thm2 thm3) handle THM _ =>
   248           error ("could not construct evaluation proof:\n"
   248           error ("could not construct evaluation proof:\n"
   249           ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
   249           ^ (cat_lines o map Display.string_of_thm) [thm1, thm2, thm3])
   250       end;
   250       end;
   251   in proto_eval thy I evaluator end;
   251   in proto_eval thy I evaluator_lift end;
   252 
   252 
   253 fun proto_eval_term thy =
   253 fun proto_eval_term thy =
   254   let
   254   let
   255     fun evaluator evaluator' _ funcgr t = evaluator' funcgr t;
   255     fun evaluator_lift evaluator _ funcgr = evaluator funcgr;
   256   in proto_eval thy (Thm.cterm_of thy) evaluator end;
   256   in proto_eval thy (Thm.cterm_of thy) evaluator_lift end;
   257 
   257 
   258 end; (*local*)
   258 end; (*local*)
   259 
   259 
   260 structure Funcgr = CodeDataFun
   260 structure Funcgr = CodeDataFun
   261 (
   261 (