src/Tools/Code/code_simp.ML
author haftmann
Tue, 21 Sep 2010 14:42:29 +0200
changeset 39825 922634ecdda4
parent 39726 e992bcc4440e
child 39830 7af0441a3a47
permissions -rw-r--r--
more conventional conversion signature
     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 no_frees_conv: conv -> conv
    10   val map_ss: (simpset -> simpset) -> theory -> theory
    11   val dynamic_eval_conv: conv
    12   val dynamic_eval_tac: theory -> int -> tactic
    13   val dynamic_eval_value: theory -> term -> term
    14   val static_eval_conv: theory -> simpset option -> string list -> conv
    15   val static_eval_tac: theory -> simpset option -> string list -> int -> tactic
    16   val setup: theory -> theory
    17 end;
    18 
    19 structure Code_Simp : CODE_SIMP =
    20 struct
    21 
    22 (* avoid free variables during conversion *)
    23 
    24 fun no_frees_conv conv ct =
    25   let
    26     val frees = Thm.add_cterm_frees ct [];
    27     fun apply_beta free thm = Thm.combination thm (Thm.reflexive free)
    28       |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
    29       |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
    30   in
    31     ct
    32     |> fold_rev Thm.cabs frees
    33     |> conv
    34     |> fold apply_beta frees
    35   end;
    36 
    37 
    38 (* dedicated simpset *)
    39 
    40 structure Simpset = Theory_Data
    41 (
    42   type T = simpset;
    43   val empty = empty_ss;
    44   fun extend ss = MetaSimplifier.inherit_context empty_ss ss;
    45   val merge = merge_ss;
    46 );
    47 
    48 val map_ss = Simpset.map;
    49 
    50 fun simpset_default thy = Simplifier.global_context thy o the_default (Simpset.get thy);
    51 
    52 
    53 (* build simpset and conversion from program *)
    54 
    55 fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss =
    56       ss addsimps (map_filter (fst o snd)) eqs addcongs (the_list some_cong)
    57   | add_stmt (Code_Thingol.Classinst (_, (_, (classparam_instances, _)))) ss =
    58       ss addsimps (map (fst o snd) classparam_instances)
    59   | add_stmt _ ss = ss;
    60 
    61 val add_program = Graph.fold (add_stmt o fst o snd);
    62 
    63 fun rewrite_modulo thy some_ss program = Simplifier.full_rewrite
    64   (add_program program (simpset_default thy some_ss));
    65 
    66 fun conclude_tac thy some_ss = Simplifier.full_simp_tac (simpset_default thy some_ss);
    67 
    68 
    69 (* evaluation with dynamic code context *)
    70 
    71 val dynamic_eval_conv = Conv.tap_thy (fn thy => no_frees_conv (Code_Thingol.dynamic_eval_conv thy
    72   (fn naming => fn program => fn t => fn deps => rewrite_modulo thy NONE program)));
    73 
    74 fun dynamic_eval_tac thy = CONVERSION dynamic_eval_conv THEN' conclude_tac thy NONE;
    75 
    76 fun dynamic_eval_value thy = snd o Logic.dest_equals o Thm.prop_of o dynamic_eval_conv o Thm.cterm_of thy;
    77 
    78 val setup = Method.setup (Binding.name "code_simp")
    79   (Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_eval_tac o ProofContext.theory_of)))
    80   "simplification with code equations"
    81   #> Value.add_evaluator ("simp", dynamic_eval_value o ProofContext.theory_of);
    82 
    83 
    84 (* evaluation with static code context *)
    85 
    86 fun static_eval_conv thy some_ss consts = no_frees_conv
    87   (Code_Thingol.static_eval_conv_simple thy consts
    88     (fn program => fn thy => fn _ => fn _ => rewrite_modulo thy some_ss program));
    89 
    90 fun static_eval_tac thy some_ss consts = CONVERSION (static_eval_conv thy some_ss consts)
    91   THEN' conclude_tac thy some_ss;
    92 
    93 end;