|
53430
|
1 |
theory Simps_Case_Conv_Examples imports
|
|
|
2 |
"~~/src/HOL/Library/Simps_Case_Conv"
|
|
|
3 |
begin
|
|
|
4 |
|
|
|
5 |
section {* Tests for the Simps<->Case conversion tools *}
|
|
|
6 |
|
|
|
7 |
fun foo where
|
|
|
8 |
"foo (x # xs) Nil = 0" |
|
|
|
9 |
"foo (x # xs) (y # ys) = foo [] []" |
|
|
|
10 |
"foo Nil (y # ys) = 1" |
|
|
|
11 |
"foo Nil Nil = 3"
|
|
|
12 |
|
|
|
13 |
fun bar where
|
|
|
14 |
"bar x 0 y = 0 + x" |
|
|
|
15 |
"bar x (Suc n) y = n + x"
|
|
|
16 |
|
|
|
17 |
definition
|
|
|
18 |
split_rule_test :: "((nat => 'a) + ('b * (('b => 'a) option))) => ('a => nat) => nat"
|
|
|
19 |
where
|
|
|
20 |
"split_rule_test x f = f (case x of Inl af \<Rightarrow> af 1
|
|
|
21 |
| Inr (b, None) => inv f 0
|
|
|
22 |
| Inr (b, Some g) => g b)"
|
|
|
23 |
|
|
|
24 |
definition test where "test x y = (case x of None => (case y of [] => 1 | _ # _ => 2) | Some x => x)"
|
|
|
25 |
|
|
|
26 |
definition nosplit where "nosplit x = x @ (case x of [] \<Rightarrow> [1] | xs \<Rightarrow> xs)"
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
text {* Function with complete, non-overlapping patterns *}
|
|
|
30 |
case_of_simps foo_cases1: foo.simps
|
|
60701
|
31 |
lemma
|
|
|
32 |
fixes xs :: "'a list" and ys :: "'b list"
|
|
|
33 |
shows "foo xs ys = (case (xs, ys) of
|
|
|
34 |
( [], []) \<Rightarrow> 3
|
|
|
35 |
| ([], y # ys) \<Rightarrow> 1
|
|
|
36 |
| (x # xs, []) \<Rightarrow> 0
|
|
|
37 |
| (x # xs, y # ys) \<Rightarrow> foo ([] :: 'a list) ([] :: 'b list))"
|
|
|
38 |
by (fact foo_cases1)
|
|
53430
|
39 |
|
|
|
40 |
text {* Redundant equations are ignored *}
|
|
|
41 |
case_of_simps foo_cases2: foo.simps foo.simps
|
|
60701
|
42 |
lemma
|
|
|
43 |
fixes xs :: "'a list" and ys :: "'b list"
|
|
|
44 |
shows "foo xs ys = (case (xs, ys) of
|
|
|
45 |
( [], []) \<Rightarrow> 3
|
|
|
46 |
| ([], y # ys) \<Rightarrow> 1
|
|
|
47 |
| (x # xs, []) \<Rightarrow> 0
|
|
|
48 |
| (x # xs, y # ys) \<Rightarrow> foo ([] :: 'a list) ([] :: 'b list))"
|
|
|
49 |
by (fact foo_cases2)
|
|
53430
|
50 |
|
|
|
51 |
text {* Variable patterns *}
|
|
|
52 |
case_of_simps bar_cases: bar.simps
|
|
|
53 |
print_theorems
|
|
|
54 |
|
|
|
55 |
text {* Case expression not at top level *}
|
|
|
56 |
simps_of_case split_rule_test_simps: split_rule_test_def
|
|
60701
|
57 |
lemma
|
|
|
58 |
"split_rule_test (Inl x) f = f (x 1)"
|
|
|
59 |
"split_rule_test (Inr (x, None)) f = f (inv f 0)"
|
|
|
60 |
"split_rule_test (Inr (x, Some y)) f = f (y x)"
|
|
|
61 |
by (fact split_rule_test_simps)+
|
|
53430
|
62 |
|
|
|
63 |
text {* Argument occurs both as case parameter and seperately*}
|
|
|
64 |
simps_of_case nosplit_simps1: nosplit_def
|
|
60701
|
65 |
lemma
|
|
|
66 |
"nosplit [] = [] @ [1]"
|
|
|
67 |
"nosplit (x # xs) = (x # xs) @ x # xs"
|
|
|
68 |
by (fact nosplit_simps1)+
|
|
53430
|
69 |
|
|
|
70 |
text {* Nested case expressions *}
|
|
|
71 |
simps_of_case test_simps1: test_def
|
|
60701
|
72 |
lemma
|
|
|
73 |
"test None [] = 1"
|
|
|
74 |
"test None (x # xs) = 2"
|
|
|
75 |
"test (Some x) y = x"
|
|
|
76 |
by (fact test_simps1)+
|
|
53430
|
77 |
|
|
|
78 |
text {* Partial split of case *}
|
|
|
79 |
simps_of_case nosplit_simps2: nosplit_def (splits: list.split)
|
|
60701
|
80 |
lemma
|
|
|
81 |
"nosplit [] = [] @ [1]"
|
|
|
82 |
"nosplit (x # xs) = (x # xs) @ x # xs"
|
|
|
83 |
by (fact nosplit_simps1)+
|
|
|
84 |
|
|
53430
|
85 |
simps_of_case test_simps2: test_def (splits: option.split)
|
|
60701
|
86 |
lemma
|
|
|
87 |
"test None y = (case y of [] \<Rightarrow> 1 | x # xs \<Rightarrow> 2)"
|
|
|
88 |
"test (Some x) y = x"
|
|
|
89 |
by (fact test_simps2)+
|
|
53430
|
90 |
|
|
|
91 |
text {* Reversal *}
|
|
|
92 |
case_of_simps test_def1: test_simps1
|
|
60701
|
93 |
lemma
|
|
|
94 |
"test x y = (case (x,y) of
|
|
|
95 |
(None, []) \<Rightarrow> 1
|
|
|
96 |
| (None, _#_) \<Rightarrow> 2
|
|
|
97 |
| (Some x, _) \<Rightarrow> x)"
|
|
|
98 |
by (fact test_def1)
|
|
53430
|
99 |
|
|
|
100 |
text {* Case expressions on RHS *}
|
|
|
101 |
case_of_simps test_def2: test_simps2
|
|
60701
|
102 |
lemma "test xs y = (case (xs, y) of (None, []) \<Rightarrow> 1 | (None, x # xa) \<Rightarrow> 2 | (Some x, y) \<Rightarrow> x)"
|
|
|
103 |
by (fact test_def2)
|
|
53430
|
104 |
|
|
|
105 |
text {* Partial split of simps *}
|
|
|
106 |
case_of_simps foo_cons_def: foo.simps(1,2)
|
|
60701
|
107 |
lemma
|
|
|
108 |
fixes xs :: "'a list" and ys :: "'b list"
|
|
|
109 |
shows "foo (x # xs) ys = (case (x,xs,ys) of
|
|
|
110 |
(_,_,[]) \<Rightarrow> 0
|
|
|
111 |
| (_,_,_ # _) \<Rightarrow> foo ([] :: 'a list) ([] :: 'b list))"
|
|
|
112 |
by (fact foo_cons_def)
|
|
53430
|
113 |
|
|
|
114 |
end
|