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