14523
|
1 |
(*<*)
|
|
2 |
theory a1 = Main:
|
|
3 |
(*>*)
|
|
4 |
subsection {* Lists *}
|
|
5 |
|
|
6 |
text{*
|
|
7 |
Define a function @{term occurs}, such that @{term"occurs x xs"}
|
|
8 |
is the number of occurrences of the element @{term x} in the list
|
|
9 |
@{term xs}.
|
|
10 |
*}
|
|
11 |
|
|
12 |
(*<*) consts (*>*)
|
|
13 |
occurs :: "'a \<Rightarrow> 'a list \<Rightarrow> nat"
|
|
14 |
|
|
15 |
text {*
|
|
16 |
Prove or disprove (by counter example) the theorems that follow. You may have to prove some lemmas first.
|
|
17 |
|
|
18 |
Use the @{text "[simp]"}-attribute only if the equation is truly a
|
|
19 |
simplification and is necessary for some later proof.
|
|
20 |
|
|
21 |
In the case of a non-theorem try to find a suitable assumption under which the theorem holds. *};
|
|
22 |
|
|
23 |
theorem "occurs a (xs @ ys) = occurs a xs + occurs a ys "
|
|
24 |
(*<*)oops(*>*)
|
|
25 |
|
|
26 |
theorem "occurs a xs = occurs a (rev xs)"
|
|
27 |
(*<*)oops(*>*)
|
|
28 |
|
|
29 |
theorem "occurs a xs <= length xs"
|
|
30 |
(*<*)oops(*>*)
|
|
31 |
|
|
32 |
theorem "occurs a (replicate n a) = n"
|
|
33 |
(*<*)oops(*>*)
|
|
34 |
|
|
35 |
text{*
|
|
36 |
Define a function @{term areAll}, such that @{term"areAll xs x"} is true iff all elements of @{term xs} are equal to @{term x}.
|
|
37 |
*}
|
|
38 |
(*<*) consts (*>*)
|
|
39 |
areAll :: "'a list \<Rightarrow> 'a \<Rightarrow> bool"
|
|
40 |
|
|
41 |
theorem "areAll xs a \<longrightarrow> occurs a xs = length xs"
|
|
42 |
(*<*)oops(*>*)
|
|
43 |
|
|
44 |
theorem "occurs a xs = length xs \<longrightarrow> areAll xs a"
|
|
45 |
(*<*)oops(*>*)
|
|
46 |
|
|
47 |
text{*
|
|
48 |
Define two functions to delete elements from a list:
|
|
49 |
@{term"del1 x xs"} deletes the first (leftmost) occurrence of @{term x} from @{term xs}.
|
|
50 |
@{term"delall x xs"} deletes all occurrences of @{term x} from @{term xs}.
|
|
51 |
*}
|
|
52 |
(*<*) consts (*>*)
|
|
53 |
delall :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
54 |
del1 :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
55 |
|
|
56 |
theorem "occurs a (delall a xs) = 0"
|
|
57 |
(*<*)oops(*>*)
|
|
58 |
theorem "Suc (occurs a (del1 a xs)) = occurs a xs"
|
|
59 |
(*<*)oops(*>*)
|
|
60 |
|
|
61 |
text{*
|
|
62 |
Define a function @{term replace}, such that @{term"replace x y zs"} yields @{term zs} with every occurrence of @{term x} replaced by @{term y}.
|
|
63 |
*}
|
|
64 |
(*<*) consts (*>*)
|
|
65 |
replace :: "'a \<Rightarrow> 'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
|
|
66 |
|
|
67 |
theorem "occurs a xs = occurs b (replace a b xs)"
|
|
68 |
(*<*)oops(*>*)
|
|
69 |
|
|
70 |
text{*
|
|
71 |
With the help of @{term occurs}, define a function @{term remDups} that removes all duplicates from a list.*}
|
|
72 |
(*<*) consts (*>*)
|
|
73 |
remDups :: "'a list \<Rightarrow> 'a list"
|
|
74 |
text{* Use @{term occurs} to formulate and prove a lemma that expresses the fact that @{term remDups} never inserts a new element into a list.*}
|
|
75 |
|
|
76 |
text{* Use @{term occurs} to formulate and prove a lemma that expresses the fact that @{term remDups} always returns a list without duplicates (i.e.\ the correctness of @{term remDups}).
|
|
77 |
*}
|
|
78 |
|
|
79 |
text{*
|
|
80 |
Now, with the help of @{term occurs} define a function @{term unique}, such that @{term"unique xs"} is true iff every element in @{term xs} occurs only once.
|
|
81 |
*}
|
|
82 |
(*<*) consts (*>*)
|
|
83 |
unique :: "'a list \<Rightarrow> bool"
|
|
84 |
|
|
85 |
text{*
|
|
86 |
Formulate and prove the correctness of @{term remDups} with the help of @{term unique}.
|
|
87 |
*}
|
|
88 |
(*<*) end (*>*)
|