-
Notifications
You must be signed in to change notification settings - Fork 0
/
Try_Attribute.thy
105 lines (84 loc) · 2.93 KB
/
Try_Attribute.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
(*
* Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
*
* SPDX-License-Identifier: BSD-2-Clause
*)
theory Try_Attribute
imports Main
begin
ML \<open>
local
val parse_warn = Scan.lift (Scan.optional (Args.parens (Parse.reserved "warn") >> K true) false)
val attribute_generic = Context.cases Attrib.attribute_global Attrib.attribute
fun try_attribute_cmd (warn, attr_srcs) (ctxt, thm) =
let
val attrs = map (attribute_generic ctxt) attr_srcs
val (th', context') =
fold (uncurry o Thm.apply_attribute) attrs (thm, ctxt)
handle e =>
(if Exn.is_interrupt e then Exn.reraise e
else if warn then warning ("TRY: ignoring exception: " ^ (@{make_string} e))
else ();
(thm, ctxt))
in (SOME context', SOME th') end
in
val _ = Theory.setup
(Attrib.setup @{binding TRY}
(parse_warn -- Attrib.attribs >> try_attribute_cmd)
"higher order attribute combinator to try other attributes, ignoring failure")
end
\<close>
text \<open>
The @{attribute TRY} attribute is an attribute combinator that applies other
attributes, ignoring any failures by returning the original state. Note that since attributes
are applied separately to each theorem in a theorem list, @{attribute TRY} will leave
failing theorems unchanged while modifying the rest.
Accepts a "warn" flag to print any errors encountered.
Usage:
thm foo[TRY [<attributes>]]
thm foo[TRY (warn) [<attributes>]]
\<close>
section \<open>Examples\<close>
experiment begin
lemma eq1: "(1 :: nat) = 1 + 0" by simp
lemma eq2: "(2 :: nat) = 1 + 1" by simp
lemmas eqs = eq1 TrueI eq2
text \<open>
`eqs[symmetric]` would fail because there are no unifiers with @{term True}, but
@{attribute TRY} ignores that.
\<close>
lemma
"1 + 0 = (1 :: nat)"
"True"
"1 + 1 = (2 :: nat)"
by (rule eqs[TRY [symmetric]])+
text \<open>
You can chain calls to @{attribute TRY} at the top level, to apply different attributes to
different theorems.
\<close>
lemma ineq: "(1 :: nat) < 2" by simp
lemmas ineqs = eq1 ineq
lemma
"1 + 0 = (1 :: nat)"
"(1 :: nat) \<le> 2"
by (rule ineqs[TRY [symmetric], TRY [THEN order.strict_implies_order]])+
text \<open>
You can chain calls to @{attribute TRY} within each other, to chain more attributes onto
particular theorems.
\<close>
lemmas more_eqs = eq1 eq2
lemma
"1 = (1 :: nat)"
"1 + 1 = (2 :: nat)"
by (rule more_eqs[TRY [symmetric, TRY [simplified add_0_right]]])+
text \<open>
The 'warn' flag will print out any exceptions encountered. Since @{attribute symmetric}
doesn't apply to @{term True} or @{term "(1 :: nat) < 2"}, this will log two errors.
\<close>
lemmas yet_another_group = eq1 TrueI eq2 ineq
thm yet_another_group[TRY (warn) [symmetric]]
text \<open>@{attribute TRY} should handle pretty much anything it might encounter.\<close>
thm eq1[TRY (warn) [where x=5]]
thm eq1[TRY (warn) [OF refl]]
end
end