src/HOL/Tools/recfun_codegen.ML
changeset 19344 b4e00947c8a1
parent 19202 0b9eb4b0ad98
child 19575 2d9940cd52d3
equal deleted inserted replaced
19343:91c348f05f1a 19344:b4e00947c8a1
    35 val const_of = dest_Const o head_of o fst o dest_eqn;
    35 val const_of = dest_Const o head_of o fst o dest_eqn;
    36 
    36 
    37 fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^
    37 fun warn thm = warning ("RecfunCodegen: Not a proper equation:\n" ^
    38   string_of_thm thm);
    38   string_of_thm thm);
    39 
    39 
    40 fun add optmod = Thm.declaration_attribute (fn thm => Context.map_theory (fn thy =>
    40 fun add optmod = Thm.declaration_attribute (fn thm => Context.map_theory
    41   let
    41   let
    42     val tab = CodegenData.get thy;
    42     fun add thm =
    43     val (s, _) = const_of (prop_of thm);
    43       if Pattern.pattern (lhs_of thm) then
       
    44         CodegenData.map
       
    45           (Symtab.update_list ((fst o const_of o prop_of) thm, (thm, optmod)))
       
    46       else tap (fn _ => warn thm)
       
    47       handle TERM _ => tap (fn _ => warn thm);
    44   in
    48   in
    45     if Pattern.pattern (lhs_of thm) then
    49     add thm
    46       CodegenData.put (Symtab.update_list (s, (thm, optmod)) tab) thy
    50     #> CodegenTheorems.add_fun thm
    47     else (warn thm; thy)
    51   end);
    48   end handle TERM _ => (warn thm; thy)));
       
    49 
    52 
    50 val del = Thm.declaration_attribute (fn thm => Context.map_theory (fn thy =>
    53 fun del_thm thm thy =
    51   let
    54   let
    52     val tab = CodegenData.get thy;
    55     val tab = CodegenData.get thy;
    53     val (s, _) = const_of (prop_of thm);
    56     val (s, _) = const_of (prop_of thm);
    54   in case Symtab.lookup tab s of
    57   in case Symtab.lookup tab s of
    55       NONE => thy
    58       NONE => thy
    56     | SOME thms =>
    59     | SOME thms =>
    57         CodegenData.put (Symtab.update (s, gen_rem (eq_thm o apfst fst) (thms, thm)) tab) thy
    60         CodegenData.put (Symtab.update (s, gen_rem (eq_thm o apfst fst) (thms, thm)) tab) thy
    58   end handle TERM _ => (warn thm; thy)));
    61   end handle TERM _ => (warn thm; thy);
       
    62 
       
    63 val del = Thm.declaration_attribute
       
    64   (fn thm => Context.map_theory (del_thm thm #> CodegenTheorems.del_def thm))
    59 
    65 
    60 fun del_redundant thy eqs [] = eqs
    66 fun del_redundant thy eqs [] = eqs
    61   | del_redundant thy eqs (eq :: eqs') =
    67   | del_redundant thy eqs (eq :: eqs') =
    62     let
    68     let
    63       val matches = curry
    69       val matches = curry
   179 
   185 
   180 val setup =
   186 val setup =
   181   CodegenData.init #>
   187   CodegenData.init #>
   182   add_codegen "recfun" recfun_codegen #>
   188   add_codegen "recfun" recfun_codegen #>
   183   add_attribute ""
   189   add_attribute ""
   184     (   Args.del |-- Scan.succeed del
   190     (Args.del |-- Scan.succeed del
   185      || Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add) #>
   191      || Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> add);
   186   CodegenPackage.add_eqextr
       
   187     ("rec", fn thy => fn _ => get_thm_equations thy);
       
   188 
   192 
   189 end;
   193 end;