src/Tools/Code/code_simp.ML
author haftmann
Thu, 16 Sep 2010 16:51:34 +0200
changeset 39475 9cc1ba3c5706
parent 38759 37a9092de102
child 39486 e992bcc4440e
permissions -rw-r--r--
separation of static and dynamic thy context
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 37463
diff changeset
     1
(*  Title:      Tools/Code/code_simp.ML
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     3
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     4
Connecting the simplifier and the code generator.
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     5
*)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     6
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     7
signature CODE_SIMP =
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     8
sig
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
     9
  val no_frees_conv: conv -> conv
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    10
  val map_ss: (simpset -> simpset) -> theory -> theory
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    11
  val dynamic_eval_conv: theory -> conv
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    12
  val dynamic_eval_tac: theory -> int -> tactic
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    13
  val dynamic_eval_value: theory -> term -> term
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    14
  val static_eval_conv: theory -> simpset option -> string list -> conv
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    15
  val static_eval_tac: theory -> simpset option -> string list -> int -> tactic
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    16
  val setup: theory -> theory
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    17
end;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    18
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    19
structure Code_Simp : CODE_SIMP =
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    20
struct
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    21
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    22
(* avoid free variables during conversion *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    23
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    24
fun no_frees_conv conv ct =
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    25
  let
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    26
    val frees = Thm.add_cterm_frees ct [];
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    27
    fun apply_beta free thm = Thm.combination thm (Thm.reflexive free)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    28
      |> Conv.fconv_rule (Conv.arg_conv (Conv.try_conv (Thm.beta_conversion false)))
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    29
      |> Conv.fconv_rule (Conv.arg1_conv (Thm.beta_conversion false));
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    30
  in
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    31
    ct
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    32
    |> fold_rev Thm.cabs frees
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    33
    |> conv
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    34
    |> fold apply_beta frees
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    35
  end;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    36
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    37
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    38
(* dedicated simpset *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    39
38759
37a9092de102 simplification/standardization of some theory data;
wenzelm
parents: 38671
diff changeset
    40
structure Simpset = Theory_Data
37a9092de102 simplification/standardization of some theory data;
wenzelm
parents: 38671
diff changeset
    41
(
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    42
  type T = simpset;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    43
  val empty = empty_ss;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    44
  fun extend ss = MetaSimplifier.inherit_context empty_ss ss;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    45
  val merge = merge_ss;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    46
);
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    47
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    48
val map_ss = Simpset.map;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    49
37461
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    50
fun simpset_default thy = Simplifier.global_context thy o the_default (Simpset.get thy);
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    51
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    52
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    53
(* build simpset and conversion from program *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    54
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    55
fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss =
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    56
      ss addsimps (map_filter (fst o snd)) eqs addcongs (the_list some_cong)
37449
034ebe92f090 more precise code
haftmann
parents: 37444
diff changeset
    57
  | add_stmt (Code_Thingol.Classinst (_, (_, (classparam_instances, _)))) ss =
034ebe92f090 more precise code
haftmann
parents: 37444
diff changeset
    58
      ss addsimps (map (fst o snd) classparam_instances)
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    59
  | add_stmt _ ss = ss;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    60
37839
b77e521e9f50 tuned interpunctation
haftmann
parents: 37744
diff changeset
    61
val add_program = Graph.fold (add_stmt o fst o snd);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    62
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    63
fun rewrite_modulo thy some_ss program = Simplifier.full_rewrite
37461
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    64
  (add_program program (simpset_default thy some_ss));
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    65
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    66
fun conclude_tac thy some_ss = Simplifier.full_simp_tac (simpset_default thy some_ss);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    67
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    68
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    69
(* evaluation with current code context *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    70
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    71
fun dynamic_eval_conv thy = no_frees_conv (Code_Thingol.dynamic_eval_conv thy
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    72
  (fn naming => fn program => fn t => fn deps => rewrite_modulo thy NONE program));
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    73
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    74
fun dynamic_eval_tac thy = CONVERSION (dynamic_eval_conv thy) THEN' conclude_tac thy NONE;
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    75
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    76
fun dynamic_eval_value thy = snd o Logic.dest_equals o Thm.prop_of o dynamic_eval_conv thy o Thm.cterm_of thy;
37444
2e7e7ff21e25 added simp evaluator
haftmann
parents: 37442
diff changeset
    77
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    78
val setup = Method.setup (Binding.name "code_simp")
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    79
  (Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_eval_tac o ProofContext.theory_of)))
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    80
  "simplification with code equations"
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    81
  #> Value.add_evaluator ("simp", dynamic_eval_value o ProofContext.theory_of);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    82
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    83
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    84
(* evaluation with freezed code context *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    85
38671
febcd1733229 use Code_Thingol.static_eval_conv_simple
haftmann
parents: 38669
diff changeset
    86
fun static_eval_conv thy some_ss consts = no_frees_conv
39475
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 38759
diff changeset
    87
  (Code_Thingol.static_eval_conv_simple thy consts
9cc1ba3c5706 separation of static and dynamic thy context
haftmann
parents: 38759
diff changeset
    88
    (fn program => fn thy => rewrite_modulo thy some_ss program));
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    89
38669
9ff76d0f0610 refined and unified naming convention for dynamic code evaluation techniques
haftmann
parents: 37839
diff changeset
    90
fun static_eval_tac thy some_ss consts = CONVERSION (static_eval_conv thy some_ss consts)
37461
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    91
  THEN' conclude_tac thy some_ss;
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    92
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    93
end;