Mercurial > hg > Members > ryokka > HoareLogic
annotate whileTestGears.agda @ 41:107cd3825e61
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 15 Dec 2019 10:18:26 +0900 |
parents | a9cac7960e81 |
children | 8813f26da3b7 |
rev | line source |
---|---|
4 | 1 module whileTestGears where |
2 | |
3 open import Function | |
4 open import Data.Nat | |
34 | 5 open import Data.Bool hiding ( _≟_ ; _≤?_ ; _≤_ ; _<_) |
4 | 6 open import Level renaming ( suc to succ ; zero to Zero ) |
7 open import Relation.Nullary using (¬_; Dec; yes; no) | |
8 open import Relation.Binary.PropositionalEquality | |
9 | |
10 | 10 open import utilities |
11 open _/\_ | |
4 | 12 |
6 | 13 record Env : Set where |
14 field | |
15 varn : ℕ | |
16 vari : ℕ | |
17 open Env | |
18 | |
33 | 19 whileTest : {l : Level} {t : Set l} → (c10 : ℕ) → (Code : Env → t) → t |
14 | 20 whileTest c10 next = next (record {varn = c10 ; vari = 0} ) |
4 | 21 |
22 {-# TERMINATING #-} | |
33 | 23 whileLoop : {l : Level} {t : Set l} → Env → (Code : Env → t) → t |
4 | 24 whileLoop env next with lt 0 (varn env) |
25 whileLoop env next | false = next env | |
26 whileLoop env next | true = | |
27 whileLoop (record {varn = (varn env) - 1 ; vari = (vari env) + 1}) next | |
28 | |
29 test1 : Env | |
14 | 30 test1 = whileTest 10 (λ env → whileLoop env (λ env1 → env1 )) |
4 | 31 |
32 | |
14 | 33 proof1 : whileTest 10 (λ env → whileLoop env (λ e → (vari e) ≡ 10 )) |
4 | 34 proof1 = refl |
35 | |
16 | 36 -- ↓PostCondition |
33 | 37 whileTest' : {l : Level} {t : Set l} → {c10 : ℕ } → (Code : (env : Env) → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) → t) → t |
14 | 38 whileTest' {_} {_} {c10} next = next env proof2 |
4 | 39 where |
40 env : Env | |
14 | 41 env = record {vari = 0 ; varn = c10} |
16 | 42 proof2 : ((vari env) ≡ 0) /\ ((varn env) ≡ c10) -- PostCondition |
4 | 43 proof2 = record {pi1 = refl ; pi2 = refl} |
11 | 44 |
45 open import Data.Empty | |
46 open import Data.Nat.Properties | |
47 | |
48 | |
16 | 49 {-# TERMINATING #-} -- ↓PreCondition(Invaliant) |
33 | 50 whileLoop' : {l : Level} {t : Set l} → (env : Env) → {c10 : ℕ } → ((varn env) + (vari env) ≡ c10) → (Code : Env → t) → t |
9 | 51 whileLoop' env proof next with ( suc zero ≤? (varn env) ) |
52 whileLoop' env proof next | no p = next env | |
14 | 53 whileLoop' env {c10} proof next | yes p = whileLoop' env1 (proof3 p ) next |
4 | 54 where |
55 env1 = record {varn = (varn env) - 1 ; vari = (vari env) + 1} | |
11 | 56 1<0 : 1 ≤ zero → ⊥ |
57 1<0 () | |
14 | 58 proof3 : (suc zero ≤ (varn env)) → varn env1 + vari env1 ≡ c10 |
9 | 59 proof3 (s≤s lt) with varn env |
11 | 60 proof3 (s≤s z≤n) | zero = ⊥-elim (1<0 p) |
61 proof3 (s≤s (z≤n {n'}) ) | suc n = let open ≡-Reasoning in | |
62 begin | |
63 n' + (vari env + 1) | |
64 ≡⟨ cong ( λ z → n' + z ) ( +-sym {vari env} {1} ) ⟩ | |
65 n' + (1 + vari env ) | |
66 ≡⟨ sym ( +-assoc (n') 1 (vari env) ) ⟩ | |
13 | 67 (n' + 1) + vari env |
11 | 68 ≡⟨ cong ( λ z → z + vari env ) +1≡suc ⟩ |
69 (suc n' ) + vari env | |
70 ≡⟨⟩ | |
71 varn env + vari env | |
72 ≡⟨ proof ⟩ | |
14 | 73 c10 |
11 | 74 ∎ |
6 | 75 |
16 | 76 -- Condition to Invaliant |
33 | 77 conversion1 : {l : Level} {t : Set l } → (env : Env) → {c10 : ℕ } → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) |
78 → (Code : (env1 : Env) → (varn env1 + vari env1 ≡ c10) → t) → t | |
14 | 79 conversion1 env {c10} p1 next = next env proof4 |
6 | 80 where |
14 | 81 proof4 : varn env + vari env ≡ c10 |
6 | 82 proof4 = let open ≡-Reasoning in |
83 begin | |
84 varn env + vari env | |
85 ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩ | |
14 | 86 c10 + vari env |
87 ≡⟨ cong ( λ n → c10 + n ) (pi1 p1 ) ⟩ | |
88 c10 + 0 | |
89 ≡⟨ +-sym {c10} {0} ⟩ | |
90 c10 | |
6 | 91 ∎ |
4 | 92 |
6 | 93 |
14 | 94 proofGears : {c10 : ℕ } → Set |
95 proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 )))) | |
9 | 96 |
33 | 97 data whileTestState (c10 : ℕ ) (env : Env ) : Set where |
98 error : whileTestState c10 env | |
99 state1 : ((vari env) ≡ 0) /\ ((varn env) ≡ c10) → whileTestState c10 env | |
100 state2 : (varn env + vari env ≡ c10) → whileTestState c10 env | |
101 finstate : ((vari env) ≡ c10 ) → whileTestState c10 env | |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
102 |
41 | 103 -- |
104 -- openended Env <=> Context | |
105 -- | |
106 | |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
107 record Context : Set where |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
108 field |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
109 c10 : ℕ |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
110 whileDG : Env |
33 | 111 whileCond : whileTestState c10 whileDG |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
112 |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
113 open Context |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
114 |
41 | 115 open import Relation.Nullary |
116 open import Relation.Binary | |
117 | |
118 -- | |
119 -- transparency of condition | |
120 -- | |
121 | |
122 whileCondP : Env → Set | |
123 whileCondP env = varn env > 0 | |
124 | |
125 whileDec : (cxt : Context) → Dec (whileCondP (whileDG cxt)) | |
126 whileDec cxt = {!!} | |
127 | |
33 | 128 whileTestContext : {l : Level} {t : Set l} → Context → (Code : Context → t) → t |
129 whileTestContext cxt next = next (record cxt { whileDG = record (whileDG cxt) {varn = c10 cxt ; vari = 0} ; whileCond = {!!} } ) | |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
130 |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
131 {-# TERMINATING #-} |
33 | 132 whileLoopContext : {l : Level} {t : Set l} → Context → (Code : Context → t) → t |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
133 whileLoopContext cxt next with lt 0 (varn (whileDG cxt) ) |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
134 whileLoopContext cxt next | false = next cxt |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
135 whileLoopContext cxt next | true = |
38 | 136 whileLoopContext (record cxt { whileDG = record {varn = (varn (whileDG cxt)) - 1 ; vari = (vari (whileDG cxt)) + 1} ; whileCond = state2 (proof cxt) } ) next |
137 where | |
138 proof : (cxt : Context) → (varn (whileDG cxt) - 1) + (vari (whileDG cxt) + 1) ≡ c10 cxt | |
139 proof cxt = {!!} | |
33 | 140 |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
141 {-# TERMINATING #-} |
41 | 142 whileLoopStep : {l : Level} {t : Set l} → Env → (next : (e : Env ) → t) (exit : (e : Env) → t) → t |
33 | 143 whileLoopStep env next exit with <-cmp 0 (varn env) |
41 | 144 whileLoopStep env next exit | tri≈ _ eq _ = exit env |
145 whileLoopStep env next exit | tri< gt ¬eq _ = next record {varn = (varn env) - 1 ; vari = (vari env) + 1} | |
39 | 146 whileLoopStep env next exit | tri> _ _ c = ⊥-elim (m<n⇒n≢0 {varn env} {0} c refl) |
27 | 147 |
40 | 148 whileTestProof : {l : Level} {t : Set l} → Context → (Code : (cxt : Context ) → ¬ (vari (whileDG cxt) ≡ varn (whileDG cxt) ) → t) → t |
149 whileTestProof cxt next = next record cxt { whileDG = out ; whileCond = init } i!=n where | |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
150 out : Env |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
151 out = whileTest (c10 cxt) ( λ e → e ) |
33 | 152 init : whileTestState (c10 cxt) out |
153 init = state1 record { pi1 = refl ; pi2 = refl } | |
40 | 154 i!=n : ¬ vari out ≡ varn out |
155 i!=n eq = {!!} | |
27 | 156 |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
157 {-# TERMINATING #-} |
41 | 158 whileLoopProof : {l : Level} {t : Set l} → (cxt : Context ) → whileCondP (whileDG cxt) |
159 → (continue : (cxt : Context ) → whileCondP (whileDG cxt) → t) (exit : Context → ¬ whileCondP (whileDG cxt) → t) → t | |
40 | 160 whileLoopProof cxt i!=n next exit = whileLoopStep (whileDG cxt) |
41 | 161 ( λ env → next record cxt { whileDG = env ; whileCond = {!!} } {!!} ) |
162 ( λ env → exit record cxt { whileDG = env ; whileCond = exitCond env {!!} } {!!} ) where | |
33 | 163 proof5 : (e : Env ) → varn e + vari e ≡ c10 cxt → 0 ≡ varn e → vari e ≡ c10 cxt |
34 | 164 proof5 record { varn = .0 ; vari = vari } refl refl = refl |
33 | 165 exitCond : (e : Env ) → 0 ≡ varn e → whileTestState (c10 cxt) e |
166 exitCond nenv eq1 with whileCond cxt | inspect whileDG cxt | |
167 ... | state2 cond | record { eq = eq2 } = finstate ( proof5 nenv {!!} eq1 ) | |
168 ... | _ | _ = error | |
27 | 169 |
40 | 170 whileConvProof : {l : Level} {t : Set l} → (cxt : Context ) → ¬ (vari (whileDG cxt) ≡ varn (whileDG cxt)) |
171 → ((cxt : Context ) → ¬ (vari (whileDG cxt) ≡ varn (whileDG cxt)) → t) → t | |
172 whileConvProof cxt i!=n next = next record cxt { whileCond = postCond } i!=n where | |
32
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
173 proof4 : (e : Env ) → (vari e ≡ 0) /\ (varn e ≡ c10 cxt) → varn e + vari e ≡ c10 cxt |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
174 proof4 env p1 = let open ≡-Reasoning in |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
175 begin |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
176 varn env + vari env |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
177 ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩ |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
178 c10 cxt + vari env |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
179 ≡⟨ cong ( λ n → c10 cxt + n ) (pi1 p1 ) ⟩ |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
180 c10 cxt + 0 |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
181 ≡⟨ +-sym {c10 cxt} {0} ⟩ |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
182 c10 cxt |
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
183 ∎ |
33 | 184 postCond : whileTestState (c10 cxt) (whileDG cxt) |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
185 postCond with whileCond cxt |
33 | 186 ... | state1 cond = state2 (proof4 (whileDG cxt) cond ) |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
187 ... | _ = error |
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
188 |
41 | 189 {-# TERMINATING #-} |
190 loop : {l : Level} {t : Set l} → (cxt : Context ) → whileCondP (whileDG cxt) | |
191 → (exit : (cxt : Context ) → ¬ whileCondP (whileDG cxt) → t) → t | |
192 loop cxt i!=n exit = whileLoopProof cxt i!=n (λ cxt → loop cxt {!!} {!!} exit ) {!!} | |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
193 |
35 | 194 |
36 | 195 {-# TERMINATING #-} |
37 | 196 loopProof : {l : Level} {t : Set l} {P Inv : Context → Set } → (c : Context) → (if : (c : Context) → Dec (P c)) |
41 | 197 → ( (cont : (c2 : Context) → (P c2) → t) → (exit : (c2 : Context) → ¬ (P c2) → t) → t ) |
198 → t | |
199 loopProof {l} {t} {P} {Inv} cxt if f = {!!} | |
36 | 200 where |
41 | 201 lem : (c : Context) → t |
202 lem c with if c | |
203 lem c | no ¬p = {!!} -- f c (λ c1 _ → lem c1 ) | |
204 lem c | yes p = {!!} | |
36 | 205 |
41 | 206 whileLoopProof' : {l : Level} {t : Set l} → (cxt : Context ) |
207 → (continue : (cxt : Context ) → whileCondP (whileDG cxt) → t) (exit : Context → ¬ whileCondP (whileDG cxt) → t) → t | |
208 whileLoopProof' = ? | |
35 | 209 |
41 | 210 proofWhileGear : (c : ℕ) (cxt : Context) → whileTestProof (record cxt { c10 = c ; whileCond = error }) |
211 $ λ cxt i!=n → whileConvProof cxt i!=n | |
212 $ λ cxt i+n=c10 → loopProof cxt whileDec ? | |
213 $ λ cxt _ → ? | |
32
bf7c7bd69e35
conversion. loop needs cases
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
31
diff
changeset
|
214 proofWhileGear c cxt = {!!} |