-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise2_1.thy
92 lines (71 loc) · 1.79 KB
/
Exercise2_1.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
(* author: wzh*)
theory Exercise2_1
imports Main
begin
(* Exer 2.1 *)
value "1 + (2 :: nat)"
value "1 + (2 :: int)"
value "1 - (2 :: nat)"
value "1 - (2 :: int)"
(* Exer 2.2 *)
fun add :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
"add 0 n = n" |
"add (Suc m) n = Suc(add m n)"
theorem add_assoc [simp]: "add (add x y) z = add x (add y z)"
apply(induction x)
apply(auto)
done
lemma add_zero [simp]: "add x 0 = x"
apply(induction x)
apply(auto)
done
lemma add_suc1 [simp]: "add x (Suc(y)) = Suc(add x y) "
apply(induction x)
apply(auto)
done
lemma add_suc2 [simp]: "add x (Suc(y)) = Suc(add y x) "
apply(induction x)
apply(auto)
done
theorem add_commu [simp]: "add x y = add y x"
apply(induction y)
apply(auto)
done
fun double :: "nat \<Rightarrow> nat" where
"double x = x*2"
theorem dou: "double x = add x x"
apply(induction x)
apply(auto)
done
(* Exer 2.3 *)
fun count :: "'a \<Rightarrow> 'a list \<Rightarrow> nat" where
"count x Nil = 0" |
"count x xs = (if x = (hd xs) then 1 + count x (tl xs) else count x (tl xs))"
theorem count_len: "count x xs \<le> length xs"
apply(induction xs)
apply(auto)
done
(* Exer 2.4 *)
fun snoc2 :: "'a list \<Rightarrow> 'a \<Rightarrow> 'a list" where
"snoc2 [] x = [x]" |
"snoc2 (x # xs) ys = (x # (snoc2 xs ys))"
fun reverse2 :: "'a list \<Rightarrow> 'a list" where
"reverse2 [] = []" |
"reverse2 (x#xs) = snoc2 (reverse2(xs)) x"
lemma [simp]: "reverse2 (snoc2 xs y) = y # (reverse2 xs)"
apply(induction xs)
apply(auto)
done
theorem [simp]: "reverse2 (reverse2 xs) = xs"
apply(induction xs)
apply(auto)
done
(* Exer 2.5 *)
fun sum_upto :: "nat \<Rightarrow> nat" where
"sum_upto 0 = 0" |
"sum_upto x = x + sum_upto (x-1)"
theorem sum: "sum_upto n*2 = n*(n+1)"
apply(induction n)
apply(auto)
done
end