src/Tools/Code/code_simp.ML
author wenzelm
Fri, 01 Jul 2011 15:16:03 +0200
changeset 44500 3803869014aa
parent 43232 23f352990944
child 46491 f2a587696afb
permissions -rw-r--r--
proper @{binding} antiquotations (relevant for formal references);
     1 (*  Title:      Tools/Code/code_simp.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Connecting the simplifier and the code generator.
     5 *)
     6 
     7 signature CODE_SIMP =
     8 sig
     9   val map_ss: (simpset -> simpset) -> theory -> theory
    10   val dynamic_conv: theory -> conv
    11   val dynamic_tac: theory -> int -> tactic
    12   val dynamic_value: theory -> term -> term
    13   val static_conv: theory -> simpset option -> string list -> conv
    14   val static_tac: theory -> simpset option -> string list -> int -> tactic
    15   val setup: theory -> theory
    16 end;
    17 
    18 structure Code_Simp : CODE_SIMP =
    19 struct
    20 
    21 (* dedicated simpset *)
    22 
    23 structure Simpset = Theory_Data
    24 (
    25   type T = simpset;
    26   val empty = empty_ss;
    27   fun extend ss = Simplifier.inherit_context empty_ss ss;
    28   val merge = merge_ss;
    29 );
    30 
    31 val map_ss = Simpset.map;
    32 
    33 fun simpset_default thy = Simplifier.global_context thy o the_default (Simpset.get thy);
    34 
    35 
    36 (* build simpset and conversion from program *)
    37 
    38 fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss =
    39       ss addsimps (map_filter (fst o snd)) eqs addcongs (the_list some_cong)
    40   | add_stmt (Code_Thingol.Classinst (_, (_, (classparam_instances, _)))) ss =
    41       ss addsimps (map (fst o snd) classparam_instances)
    42   | add_stmt _ ss = ss;
    43 
    44 val add_program = Graph.fold (add_stmt o fst o snd);
    45 
    46 fun rewrite_modulo thy some_ss program = Simplifier.full_rewrite
    47   (add_program program (simpset_default thy some_ss));
    48 
    49 fun conclude_tac thy some_ss = Simplifier.full_simp_tac (simpset_default thy some_ss);
    50 
    51 
    52 (* evaluation with dynamic code context *)
    53 
    54 fun dynamic_conv thy = Code_Thingol.dynamic_conv thy
    55   (fn naming => fn program => fn t => fn deps => rewrite_modulo thy NONE program);
    56 
    57 fun dynamic_tac thy = CONVERSION (dynamic_conv thy) THEN' conclude_tac thy NONE;
    58 
    59 fun dynamic_value thy = snd o Logic.dest_equals o Thm.prop_of o dynamic_conv thy o Thm.cterm_of thy;
    60 
    61 val setup =
    62   Method.setup @{binding code_simp}
    63     (Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_tac o Proof_Context.theory_of)))
    64     "simplification with code equations"
    65   #> Value.add_evaluator ("simp", dynamic_value o Proof_Context.theory_of);
    66 
    67 
    68 (* evaluation with static code context *)
    69 
    70 fun static_conv thy some_ss consts =
    71   Code_Thingol.static_conv_simple thy consts
    72     (fn program => fn _ => fn _ => rewrite_modulo thy some_ss program);
    73 
    74 fun static_tac thy some_ss consts = CONVERSION (static_conv thy some_ss consts)
    75   THEN' conclude_tac thy some_ss;
    76 
    77 end;