annotate whileTestGears.agda @ 6:28e80739eed6

fix whileTestGears
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 14 Dec 2018 22:06:24 +0900
parents 17e4f3b58148
children e7d6bdb6039d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
1 module whileTestGears where
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
2
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
3 open import Function
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
4 open import Data.Nat
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
5 open import Data.Bool hiding ( _≟_ ; _∧_)
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
6 open import Level renaming ( suc to succ ; zero to Zero )
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
7 open import Relation.Nullary using (¬_; Dec; yes; no)
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
8 open import Relation.Binary.PropositionalEquality
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
9
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
10
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
11 record _∧_ {n : Level } (a : Set n) (b : Set n): Set n where
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
12 field
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
13 pi1 : a
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
14 pi2 : b
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
15
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
16 open _∧_
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
17
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
18 _-_ : ℕ -> ℕ -> ℕ
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
19 x - zero = x
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
20 zero - _ = zero
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
21 (suc x) - (suc y) = x - y
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
22
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
23 sym1 : { y : ℕ } -> y + zero ≡ y
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
24 sym1 {zero} = refl
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
25 sym1 {suc y} = cong ( λ x → suc x ) ( sym1 {y} )
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
26
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
27 +-sym : { x y : ℕ } -> x + y ≡ y + x
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
28 +-sym {zero} {zero} = refl
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
29 +-sym {zero} {suc y} = let open ≡-Reasoning in
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
30 begin
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
31 zero + suc y
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
32 ≡⟨⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
33 suc y
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
34 ≡⟨ sym sym1 ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
35 suc y + zero
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
36
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
37 +-sym {suc x} {zero} = let open ≡-Reasoning in
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
38 begin
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
39 suc x + zero
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
40 ≡⟨ sym1 ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
41 suc x
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
42 ≡⟨⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
43 zero + suc x
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
44
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
45 +-sym {suc x} {suc y} = cong ( λ z → suc z ) ( let open ≡-Reasoning in
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
46 begin
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
47 x + suc y
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
48 ≡⟨ +-sym {x} {suc y} ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
49 suc (y + x)
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
50 ≡⟨ cong ( λ z → suc z ) (+-sym {y} {x}) ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
51 suc (x + y)
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
52 ≡⟨ sym ( +-sym {y} {suc x}) ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
53 y + suc x
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
54 ∎ )
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
55
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
56 minus-plus : { x y : ℕ } -> (suc x - 1) + (y + 1) ≡ suc x + y
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
57 minus-plus {zero} {y} = +-sym {y} {1}
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
58 minus-plus {suc x} {y} = cong ( λ z → suc z ) (minus-plus {x} {y})
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
59
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
60 lt : ℕ -> ℕ -> Bool
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
61 lt x y with (suc x ) ≤? y
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
62 lt x y | yes p = true
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
63 lt x y | no ¬p = false
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
64
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
65 Equal : ℕ -> ℕ -> Bool
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
66 Equal x y with x ≟ y
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
67 Equal x y | yes p = true
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
68 Equal x y | no ¬p = false
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
69
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
70 record Env : Set where
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
71 field
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
72 varn : ℕ
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
73 vari : ℕ
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
74 open Env
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
75
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
76 whileTest : {l : Level} {t : Set l} -> (Code : Env -> t) -> t
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
77 whileTest next = next (record {varn = 10 ; vari = 0} )
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
78
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
79 {-# TERMINATING #-}
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
80 whileLoop : {l : Level} {t : Set l} -> Env -> (Code : Env -> t) -> t
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
81 whileLoop env next with lt 0 (varn env)
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
82 whileLoop env next | false = next env
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
83 whileLoop env next | true =
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
84 whileLoop (record {varn = (varn env) - 1 ; vari = (vari env) + 1}) next
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
85
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
86 test1 : Env
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
87 test1 = whileTest (λ env → whileLoop env (λ env1 → env1 ))
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
88
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
89
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
90 proof1 : whileTest (λ env → whileLoop env (λ e → (vari e) ≡ 10 ))
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
91 proof1 = refl
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
92
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
93
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
94
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
95 -- stmt2Cond : {l : Level} → EnvWithCond {l} →
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
96 -- stmt2Cond env = (Equal (varn' env) 10) ∧ (Equal (vari' env) 0)
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
97
5
17e4f3b58148 add future code proofGears
ryokka
parents: 4
diff changeset
98 whileTest' : {l : Level} {t : Set l} -> (Code : (env : Env) -> ((vari env) ≡ 0) ∧ ((varn env) ≡ 10) -> t) -> t
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
99 whileTest' next = next env proof2
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
100 where
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
101 env : Env
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
102 env = record {vari = 0 ; varn = 10}
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
103 proof2 : ((vari env) ≡ 0) ∧ ((varn env) ≡ 10)
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
104 proof2 = record {pi1 = refl ; pi2 = refl}
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
105
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
106 {-# TERMINATING #-}
5
17e4f3b58148 add future code proofGears
ryokka
parents: 4
diff changeset
107 whileLoop' : {l : Level} {t : Set l} -> (env : Env) -> ((varn env) + (vari env) ≡ 10) -> (Code : Env -> t) -> t
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
108 whileLoop' env proof next with lt 0 (varn env)
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
109 whileLoop' env proof next | false = next env
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
110 whileLoop' env proof next | true = whileLoop' env1 proof3 next
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
111 where
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
112 env1 = record {varn = (varn env) - 1 ; vari = (vari env) + 1}
5
17e4f3b58148 add future code proofGears
ryokka
parents: 4
diff changeset
113 proof3 : varn env1 + vari env1 ≡ 10
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
114 proof3 = let open ≡-Reasoning in
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
115 begin
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
116 varn env1 + vari env1
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
117 ≡⟨⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
118 (varn env - 1) + (vari env + 1)
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
119 ≡⟨ {!!} ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
120 10
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
121
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
122
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
123
5
17e4f3b58148 add future code proofGears
ryokka
parents: 4
diff changeset
124 conversion1 : {l : Level} {t : Set l } → (env : Env) -> ((vari env) ≡ 0) ∧ ((varn env) ≡ 10)
17e4f3b58148 add future code proofGears
ryokka
parents: 4
diff changeset
125 -> (Code : (env1 : Env) -> (varn env1 + vari env1 ≡ 10) -> t) -> t
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
126 conversion1 env p1 next = next env proof4
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
127 where
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
128 proof4 : varn env + vari env ≡ 10
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
129 proof4 = let open ≡-Reasoning in
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
130 begin
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
131 varn env + vari env
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
132 ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
133 10 + vari env
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
134 ≡⟨ cong ( λ n → 10 + n ) (pi1 p1 ) ⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
135 10 + 0
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
136 ≡⟨⟩
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
137 10
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
138
4
64bd5c236002 add whileTestGears
ryokka
parents:
diff changeset
139
6
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
140
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
141 proofGears : Set
28e80739eed6 fix whileTestGears
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
142 proofGears = whileTest' (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ 10 ))))