src/Tools/Code/code_simp.ML
author haftmann
Thu, 19 Apr 2012 10:16:51 +0200
changeset 48447 b32aae03e3d6
parent 46491 f2a587696afb
child 49087 ace701efe203
permissions -rw-r--r--
dropped dead code;
tuned
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 map_ss: (simpset -> simpset) -> theory -> theory
haftmann@41488
    10
  val dynamic_conv: theory -> conv
haftmann@41436
    11
  val dynamic_tac: theory -> int -> tactic
haftmann@41432
    12
  val dynamic_value: theory -> term -> term
haftmann@41432
    13
  val static_conv: theory -> simpset option -> string list -> conv
haftmann@41436
    14
  val static_tac: theory -> simpset option -> string list -> int -> tactic
haftmann@37417
    15
  val setup: theory -> theory
haftmann@37417
    16
end;
haftmann@37417
    17
haftmann@37417
    18
structure Code_Simp : CODE_SIMP =
haftmann@37417
    19
struct
haftmann@37417
    20
haftmann@37417
    21
(* dedicated simpset *)
haftmann@37417
    22
wenzelm@39034
    23
structure Simpset = Theory_Data
wenzelm@39034
    24
(
haftmann@37417
    25
  type T = simpset;
haftmann@37417
    26
  val empty = empty_ss;
wenzelm@41491
    27
  fun extend ss = Simplifier.inherit_context empty_ss ss;
haftmann@37417
    28
  val merge = merge_ss;
haftmann@37417
    29
);
haftmann@37417
    30
haftmann@37417
    31
val map_ss = Simpset.map;
haftmann@37417
    32
haftmann@37436
    33
fun simpset_default thy = Simplifier.global_context thy o the_default (Simpset.get thy);
haftmann@37436
    34
haftmann@37417
    35
haftmann@37417
    36
(* build simpset and conversion from program *)
haftmann@37417
    37
haftmann@37417
    38
fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss =
wenzelm@46491
    39
      ss addsimps (map_filter (fst o snd)) eqs
wenzelm@46491
    40
      |> fold Simplifier.add_cong (the_list some_cong)
haftmann@37424
    41
  | add_stmt (Code_Thingol.Classinst (_, (_, (classparam_instances, _)))) ss =
haftmann@37424
    42
      ss addsimps (map (fst o snd) classparam_instances)
haftmann@37417
    43
  | add_stmt _ ss = ss;
haftmann@37417
    44
haftmann@37839
    45
val add_program = Graph.fold (add_stmt o fst o snd);
haftmann@37417
    46
haftmann@37417
    47
fun rewrite_modulo thy some_ss program = Simplifier.full_rewrite
haftmann@37436
    48
  (add_program program (simpset_default thy some_ss));
haftmann@37436
    49
haftmann@37436
    50
fun conclude_tac thy some_ss = Simplifier.full_simp_tac (simpset_default thy some_ss);
haftmann@37417
    51
haftmann@37417
    52
haftmann@39726
    53
(* evaluation with dynamic code context *)
haftmann@37417
    54
haftmann@41488
    55
fun dynamic_conv thy = Code_Thingol.dynamic_conv thy
haftmann@48447
    56
  (fn _ => fn program => fn _ => fn _ => rewrite_modulo thy NONE program);
haftmann@37417
    57
haftmann@41488
    58
fun dynamic_tac thy = CONVERSION (dynamic_conv thy) THEN' conclude_tac thy NONE;
haftmann@37417
    59
haftmann@41488
    60
fun dynamic_value thy = snd o Logic.dest_equals o Thm.prop_of o dynamic_conv thy o Thm.cterm_of thy;
haftmann@37419
    61
wenzelm@44500
    62
val setup =
wenzelm@44500
    63
  Method.setup @{binding code_simp}
wenzelm@44500
    64
    (Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_tac o Proof_Context.theory_of)))
wenzelm@44500
    65
    "simplification with code equations"
wenzelm@43232
    66
  #> Value.add_evaluator ("simp", dynamic_value o Proof_Context.theory_of);
haftmann@37417
    67
haftmann@37417
    68
haftmann@39726
    69
(* evaluation with static code context *)
haftmann@37417
    70
haftmann@41432
    71
fun static_conv thy some_ss consts =
haftmann@41432
    72
  Code_Thingol.static_conv_simple thy consts
haftmann@41594
    73
    (fn program => fn _ => fn _ => rewrite_modulo thy some_ss program);
haftmann@37417
    74
haftmann@41436
    75
fun static_tac thy some_ss consts = CONVERSION (static_conv thy some_ss consts)
haftmann@37436
    76
  THEN' conclude_tac thy some_ss;
haftmann@37417
    77
haftmann@37417
    78
end;