src/Tools/Code/code_simp.ML
author blanchet
Thu, 22 Aug 2013 12:12:51 +0200
changeset 53139 07a6e11f1631
parent 52736 317663b422bb
child 53147 8e8941fea278
permissions -rw-r--r--
cleanup old duplicated functionality
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
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 48072
diff changeset
     9
  val map_ss: (Proof.context -> Proof.context) -> theory -> theory
41247
c5cb19ecbd41 avoid slightly odd Conv.tap_thy
haftmann
parents: 41188
diff changeset
    10
  val dynamic_conv: theory -> conv
41188
7cded8957e72 more uniform naming
haftmann
parents: 41184
diff changeset
    11
  val dynamic_tac: theory -> int -> tactic
41184
5c6f44d22f51 simplified evaluation function names
haftmann
parents: 39606
diff changeset
    12
  val dynamic_value: theory -> term -> term
5c6f44d22f51 simplified evaluation function names
haftmann
parents: 39606
diff changeset
    13
  val static_conv: theory -> simpset option -> string list -> conv
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    14
  val static_tac: theory -> simpset option -> string list -> Proof.context -> int -> tactic
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    15
  val setup: theory -> theory
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    16
end;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    17
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    18
structure Code_Simp : CODE_SIMP =
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    19
struct
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    20
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    21
(* dedicated simpset *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    22
38759
37a9092de102 simplification/standardization of some theory data;
wenzelm
parents: 38671
diff changeset
    23
structure Simpset = Theory_Data
37a9092de102 simplification/standardization of some theory data;
wenzelm
parents: 38671
diff changeset
    24
(
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    25
  type T = simpset;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    26
  val empty = empty_ss;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 48072
diff changeset
    27
  val extend = I;
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    28
  val merge = merge_ss;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    29
);
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    30
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 48072
diff changeset
    31
fun map_ss f thy = Simpset.map (simpset_map (Proof_Context.init_global thy) f) thy;
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    32
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    33
fun simpset_default thy = the_default (Simpset.get thy);
37461
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    34
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    35
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    36
(* build simpset and conversion from program *)
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    37
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    38
fun add_stmt (Code_Thingol.Fun (_, ((_, eqs), some_cong))) ss =
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    39
      ss
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    40
      |> fold Simplifier.add_simp ((map_filter (fst o snd)) eqs)
45620
f2a587696afb modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 43619
diff changeset
    41
      |> fold Simplifier.add_cong (the_list some_cong)
48072
ace701efe203 prefer records with speaking labels over deeply nested tuples
haftmann
parents: 47576
diff changeset
    42
  | add_stmt (Code_Thingol.Classinst { inst_params, ... }) ss =
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    43
      ss
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    44
      |> fold Simplifier.add_simp (map (fst o snd) inst_params)
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    45
  | add_stmt _ ss = ss;
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    46
37839
b77e521e9f50 tuned interpunctation
haftmann
parents: 37744
diff changeset
    47
val add_program = Graph.fold (add_stmt o fst o snd);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    48
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    49
fun simpset_program thy some_ss program =
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    50
  simpset_map (Proof_Context.init_global thy) (add_program program) (simpset_default thy some_ss);
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    51
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    52
fun lift_ss_conv f ss ct = f (Simplifier.put_simpset ss (Proof_Context.init_global (theory_of_cterm ct))) ct;
37461
3489cea0e0e9 conclude simplification with default simpset
haftmann
parents: 37449
diff changeset
    53
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    54
fun rewrite_modulo thy some_ss program =
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    55
  lift_ss_conv Simplifier.full_rewrite (simpset_program thy some_ss program);
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    56
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    57
fun conclude_tac thy some_ss =
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    58
  let
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    59
    val ss = simpset_default thy some_ss;
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    60
  in fn ctxt => Simplifier.full_simp_tac (Simplifier.put_simpset ss ctxt) end;
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    61
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    62
39486
e992bcc4440e refined static_eval_conv_simple; tuned comments
haftmann
parents: 39475
diff changeset
    63
(* evaluation with dynamic code context *)
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    64
41247
c5cb19ecbd41 avoid slightly odd Conv.tap_thy
haftmann
parents: 41188
diff changeset
    65
fun dynamic_conv thy = Code_Thingol.dynamic_conv thy
47576
b32aae03e3d6 dropped dead code;
haftmann
parents: 45620
diff changeset
    66
  (fn _ => fn program => fn _ => fn _ => rewrite_modulo thy NONE program);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    67
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    68
fun dynamic_tac thy = CONVERSION (dynamic_conv thy) THEN' conclude_tac thy NONE (Proof_Context.init_global thy);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    69
41247
c5cb19ecbd41 avoid slightly odd Conv.tap_thy
haftmann
parents: 41188
diff changeset
    70
fun dynamic_value thy = snd o Logic.dest_equals o Thm.prop_of o dynamic_conv thy o Thm.cterm_of thy;
37444
2e7e7ff21e25 added simp evaluator
haftmann
parents: 37442
diff changeset
    71
43619
3803869014aa proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents: 42361
diff changeset
    72
val setup =
3803869014aa proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents: 42361
diff changeset
    73
  Method.setup @{binding code_simp}
3803869014aa proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents: 42361
diff changeset
    74
    (Scan.succeed (SIMPLE_METHOD' o (CHANGED_PROP oo dynamic_tac o Proof_Context.theory_of)))
3803869014aa proper @{binding} antiquotations (relevant for formal references);
wenzelm
parents: 42361
diff changeset
    75
    "simplification with code equations"
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 41346
diff changeset
    76
  #> Value.add_evaluator ("simp", dynamic_value o Proof_Context.theory_of);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    77
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    78
39486
e992bcc4440e refined static_eval_conv_simple; tuned comments
haftmann
parents: 39475
diff changeset
    79
(* evaluation with static code context *)
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    80
41184
5c6f44d22f51 simplified evaluation function names
haftmann
parents: 39606
diff changeset
    81
fun static_conv thy some_ss consts =
5c6f44d22f51 simplified evaluation function names
haftmann
parents: 39606
diff changeset
    82
  Code_Thingol.static_conv_simple thy consts
41346
6673f6fa94ca canonical handling of theory context argument
haftmann
parents: 41251
diff changeset
    83
    (fn program => fn _ => fn _ => rewrite_modulo thy some_ss program);
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    84
52736
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    85
fun static_tac thy some_ss consts =
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    86
  let
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    87
    val conv = static_conv thy some_ss consts;
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    88
    val tac = conclude_tac thy some_ss;
317663b422bb more correct context for dynamic invocations of static code conversions etc.
haftmann
parents: 51717
diff changeset
    89
  in fn ctxt => CONVERSION conv THEN' tac ctxt end;
37442
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    90
037ee7b712b2 added code_simp infrastructure
haftmann
parents:
diff changeset
    91
end;