-
Notifications
You must be signed in to change notification settings - Fork 108
/
SimpStrategy.thy
108 lines (89 loc) · 3.04 KB
/
SimpStrategy.thy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(*
* Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
*
* SPDX-License-Identifier: BSD-2-Clause
*)
theory SimpStrategy
imports Main
begin
text \<open>
Support for defining alternative simplifier strategies for some parts of terms
or some premises of rewrite rules. The "name" parameter to the simp_strategy
constant is used to identify which simplification strategy should be used on
this term. Note that, although names have type nat, it is safe for them to all
be defined as 0. The important thing is that the simplifier doesn't know they're
equal.
\<close>
definition
simp_strategy :: "nat \<Rightarrow> ('a :: {}) \<Rightarrow> 'a"
where
"simp_strategy name x \<equiv> x"
text \<open>
This congruence rule forbids the simplifier from simplifying the arguments of
simp_strategy normally.
\<close>
lemma simp_strategy_cong[cong]:
"simp_strategy name x = simp_strategy name x"
by simp
text \<open>
This strategy, or rather lack thereof, can be used to forbid simplification.
\<close>
definition
NoSimp :: nat
where "NoSimp = 0"
text \<open>
This strategy indicates that a boolean subterm should be simplified only by
using explicit assumptions of the simpset.
\<close>
definition
ByAssum :: nat
where "ByAssum = 0"
lemma Eq_TrueI_ByAssum:
"P \<Longrightarrow> simp_strategy ByAssum P \<equiv> True"
by (simp add: simp_strategy_def)
simproc_setup simp_strategy_ByAssum ("simp_strategy ByAssum b") =
\<open>K (fn ss => fn ct => let
val b = Thm.dest_arg ct
val t = Thm.instantiate (TVars.empty, Vars.make [((("P",0),@{typ bool}), b)])
@{thm Eq_TrueI_ByAssum}
val prems = Raw_Simplifier.prems_of ss
in get_first (try (fn p => p RS t)) prems end)\<close>
lemma ByAssum:
"simp_strategy ByAssum P \<Longrightarrow> P"
by (simp add: simp_strategy_def)
lemma simp_ByAssum_test:
"P \<Longrightarrow> simp_strategy ByAssum P"
by simp
text \<open>
Generic framework for instantiating a simproc which simplifies within a
simp_strategy with a given simpset.
The boolean determines whether simp_strategy Name True should be rewritten
to True.
\<close>
lemma simp_strategy_Eq_True:
"simp_strategy name True \<equiv> True"
by (simp add: simp_strategy_def)
ML \<open>
fun simp_strategy_True_conv ct = case Thm.term_of ct of
(Const (@{const_name simp_strategy}, _) $ _ $ @{term True})
=> Thm.instantiate (TVars.empty, Vars.make [((("name",0), @{typ nat}), Thm.dest_arg1 ct)])
@{thm simp_strategy_Eq_True}
| _ => Conv.all_conv ct
fun new_simp_strategy thy (name : term) ss rewr_True =
let
val ctxt = Proof_Context.init_global thy;
val ss = Simplifier.make_simproc ctxt
{name = "simp_strategy_" ^ fst (dest_Const name),
lhss = [@{term simp_strategy} $ name $ @{term x}],
proc = (fn _ => fn ctxt' => fn ct =>
ct
|> (Conv.arg_conv (Simplifier.rewrite (put_simpset ss ctxt'))
then_conv (if rewr_True then simp_strategy_True_conv
else Conv.all_conv))
|> (fn c => if Thm.is_reflexive c then NONE else SOME c)),
identifier = []}
in
ss
end
\<close>
end