Mercurial > hg > Members > ryokka > HoareLogic
annotate whileTestGears.agda @ 73:52acd110df18
fix
author | ryokka |
---|---|
date | Thu, 26 Dec 2019 17:39:56 +0900 |
parents | 66ba3b1eec0a |
children | 6f26de2fb7fe |
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 ( _≟_ ; _≤?_ ; _≤_ ; _<_) |
62 | 6 open import Data.Product |
4 | 7 open import Level renaming ( suc to succ ; zero to Zero ) |
8 open import Relation.Nullary using (¬_; Dec; yes; no) | |
9 open import Relation.Binary.PropositionalEquality | |
62 | 10 open import Agda.Builtin.Unit |
4 | 11 |
10 | 12 open import utilities |
13 open _/\_ | |
4 | 14 |
42 | 15 record Env : Set (succ Zero) where |
6 | 16 field |
17 varn : ℕ | |
18 vari : ℕ | |
42 | 19 open Env |
6 | 20 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
21 whileTest : {l : Level} {t : Set l} → (c10 : ℕ) → (Code : Env → t) → t |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
22 whileTest c10 next = next (record {varn = c10 ; vari = 0 } ) |
4 | 23 |
24 {-# TERMINATING #-} | |
33 | 25 whileLoop : {l : Level} {t : Set l} → Env → (Code : Env → t) → t |
4 | 26 whileLoop env next with lt 0 (varn env) |
27 whileLoop env next | false = next env | |
28 whileLoop env next | true = | |
42 | 29 whileLoop (record env {varn = (varn env) - 1 ; vari = (vari env) + 1}) next |
4 | 30 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
31 test1 : Env |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
32 test1 = whileTest 10 (λ env → whileLoop env (λ env1 → env1 )) |
4 | 33 |
34 | |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
35 proof1 : whileTest 10 (λ env → whileLoop env (λ e → (vari e) ≡ 10 )) |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
36 proof1 = refl |
4 | 37 |
16 | 38 -- ↓PostCondition |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
39 whileTest' : {l : Level} {t : Set l} → {c10 : ℕ } → (Code : (env : Env ) → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) → t) → t |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
40 whileTest' {_} {_} {c10} next = next env proof2 |
4 | 41 where |
42 | 42 env : Env |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
43 env = record {vari = 0 ; varn = c10 } |
16 | 44 proof2 : ((vari env) ≡ 0) /\ ((varn env) ≡ c10) -- PostCondition |
4 | 45 proof2 = record {pi1 = refl ; pi2 = refl} |
11 | 46 |
47 open import Data.Empty | |
48 open import Data.Nat.Properties | |
49 | |
50 | |
16 | 51 {-# TERMINATING #-} -- ↓PreCondition(Invaliant) |
42 | 52 whileLoop' : {l : Level} {t : Set l} → (env : Env ) → {c10 : ℕ } → ((varn env) + (vari env) ≡ c10) → (Code : Env → t) → t |
9 | 53 whileLoop' env proof next with ( suc zero ≤? (varn env) ) |
54 whileLoop' env proof next | no p = next env | |
14 | 55 whileLoop' env {c10} proof next | yes p = whileLoop' env1 (proof3 p ) next |
4 | 56 where |
42 | 57 env1 = record env {varn = (varn env) - 1 ; vari = (vari env) + 1} |
11 | 58 1<0 : 1 ≤ zero → ⊥ |
59 1<0 () | |
14 | 60 proof3 : (suc zero ≤ (varn env)) → varn env1 + vari env1 ≡ c10 |
47 | 61 proof3 (s≤s lt) with varn env |
62 proof3 (s≤s z≤n) | zero = ⊥-elim (1<0 p) | |
63 proof3 (s≤s (z≤n {n'}) ) | suc n = let open ≡-Reasoning in | |
64 begin | |
65 n' + (vari env + 1) | |
66 ≡⟨ cong ( λ z → n' + z ) ( +-sym {vari env} {1} ) ⟩ | |
67 n' + (1 + vari env ) | |
68 ≡⟨ sym ( +-assoc (n') 1 (vari env) ) ⟩ | |
69 (n' + 1) + vari env | |
70 ≡⟨ cong ( λ z → z + vari env ) +1≡suc ⟩ | |
71 (suc n' ) + vari env | |
72 ≡⟨⟩ | |
73 varn env + vari env | |
74 ≡⟨ proof ⟩ | |
75 c10 | |
76 ∎ | |
6 | 77 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
78 -- Condition to Invariant |
42 | 79 conversion1 : {l : Level} {t : Set l } → (env : Env ) → {c10 : ℕ } → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) |
80 → (Code : (env1 : Env ) → (varn env1 + vari env1 ≡ c10) → t) → t | |
14 | 81 conversion1 env {c10} p1 next = next env proof4 |
6 | 82 where |
14 | 83 proof4 : varn env + vari env ≡ c10 |
6 | 84 proof4 = let open ≡-Reasoning in |
85 begin | |
86 varn env + vari env | |
87 ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩ | |
14 | 88 c10 + vari env |
89 ≡⟨ cong ( λ n → c10 + n ) (pi1 p1 ) ⟩ | |
90 c10 + 0 | |
91 ≡⟨ +-sym {c10} {0} ⟩ | |
92 c10 | |
6 | 93 ∎ |
4 | 94 |
6 | 95 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
96 proofGears : {c10 : ℕ } → Set |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
97 proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 )))) |
9 | 98 |
49 | 99 -- proofGearsMeta : {c10 : ℕ } → proofGears {c10} |
100 -- proofGearsMeta {c10} = {!!} -- net yet done | |
43 | 101 |
41 | 102 -- |
42 | 103 -- openended Env c <=> Context |
41 | 104 -- |
105 | |
71 | 106 open import Relation.Nullary hiding (proof) |
41 | 107 open import Relation.Binary |
108 | |
53 | 109 record Envc : Set (succ Zero) where |
110 field | |
111 c10 : ℕ | |
112 varn : ℕ | |
113 vari : ℕ | |
71 | 114 open Envc |
49 | 115 |
53 | 116 whileTestP : {l : Level} {t : Set l} → (c10 : ℕ) → (Code : Envc → t) → t |
117 whileTestP c10 next = next (record {varn = c10 ; vari = 0 ; c10 = c10 } ) | |
118 | |
119 whileLoopP : {l : Level} {t : Set l} → Envc → (next : Envc → t) → (exit : Envc → t) → t | |
49 | 120 whileLoopP env next exit with <-cmp 0 (varn env) |
121 whileLoopP env next exit | tri≈ ¬a b ¬c = exit env | |
71 | 122 whileLoopP env next exit | tri< a ¬b ¬c = |
123 next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 }) | |
124 | |
125 whileLoopP' : {l : Level} {t : Set l} → Envc → (next : Envc → t) → (exit : Envc → t) → t | |
72 | 126 whileLoopP' env@record { c10 = c10 ; varn = zero ; vari = vari } next exit = exit env |
127 whileLoopP' env@record { c10 = c10 ; varn = (suc varn1) ; vari = vari } next exit = next (record env {varn = varn1 ; vari = vari + 1 }) | |
71 | 128 |
129 -- whileLoopP env next exit | tri≈ ¬a b ¬c = exit env | |
130 -- whileLoopP env next exit | tri< a ¬b ¬c = | |
131 -- next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 }) | |
49 | 132 |
133 {-# TERMINATING #-} | |
53 | 134 loopP : {l : Level} {t : Set l} → Envc → (exit : Envc → t) → t |
49 | 135 loopP env exit = whileLoopP env (λ env → loopP env exit ) exit |
136 | |
53 | 137 whileTestPCall : (c10 : ℕ ) → Envc |
138 whileTestPCall c10 = whileTestP {_} {_} c10 (λ env → loopP env (λ env → env)) | |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
139 |
53 | 140 data whileTestState : Set where |
141 s1 : whileTestState | |
142 s2 : whileTestState | |
143 sf : whileTestState | |
49 | 144 |
53 | 145 whileTestStateP : whileTestState → Envc → Set |
146 whileTestStateP s1 env = (vari env ≡ 0) /\ (varn env ≡ c10 env) | |
147 whileTestStateP s2 env = (varn env + vari env ≡ c10 env) | |
148 whileTestStateP sf env = (vari env ≡ c10 env) | |
50 | 149 |
53 | 150 whileTestPwP : {l : Level} {t : Set l} → (c10 : ℕ) → ((env : Envc ) → whileTestStateP s1 env → t) → t |
151 whileTestPwP c10 next = next env record { pi1 = refl ; pi2 = refl } where | |
152 env : Envc | |
153 env = whileTestP c10 ( λ env → env ) | |
50 | 154 |
56 | 155 whileLoopPwP : {l : Level} {t : Set l} → (env : Envc ) → whileTestStateP s2 env |
53 | 156 → (next : (env : Envc ) → whileTestStateP s2 env → t) |
157 → (exit : (env : Envc ) → whileTestStateP sf env → t) → t | |
54 | 158 whileLoopPwP env s next exit with <-cmp 0 (varn env) |
55 | 159 whileLoopPwP env s next exit | tri≈ ¬a b ¬c = exit env (lem (sym b) s) |
160 where | |
161 lem : (varn env ≡ 0) → (varn env + vari env ≡ c10 env) → vari env ≡ c10 env | |
162 lem p1 p2 rewrite p1 = p2 | |
163 | |
56 | 164 whileLoopPwP env s next exit | tri< a ¬b ¬c = next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 }) (proof5 a) |
165 where | |
166 1<0 : 1 ≤ zero → ⊥ | |
167 1<0 () | |
168 proof5 : (suc zero ≤ (varn env)) → (varn env - 1) + (vari env + 1) ≡ c10 env | |
169 proof5 (s≤s lt) with varn env | |
170 proof5 (s≤s z≤n) | zero = ⊥-elim (1<0 a) | |
171 proof5 (s≤s (z≤n {n'}) ) | suc n = let open ≡-Reasoning in | |
172 begin | |
173 n' + (vari env + 1) | |
174 ≡⟨ cong ( λ z → n' + z ) ( +-sym {vari env} {1} ) ⟩ | |
175 n' + (1 + vari env ) | |
176 ≡⟨ sym ( +-assoc (n') 1 (vari env) ) ⟩ | |
177 (n' + 1) + vari env | |
178 ≡⟨ cong ( λ z → z + vari env ) +1≡suc ⟩ | |
179 (suc n' ) + vari env | |
180 ≡⟨⟩ | |
181 varn env + vari env | |
182 ≡⟨ s ⟩ | |
183 c10 env | |
184 ∎ | |
51 | 185 |
66 | 186 data _implies_ (A B : Set ) : Set (succ Zero) where |
187 proof : ( A → B ) → A implies B | |
188 | |
189 implies2p : {A B : Set } → A implies B → A → B | |
190 implies2p (proof x) = x | |
191 | |
68 | 192 whileTestPSem : (c : ℕ) → whileTestP c ( λ env → ⊤ implies (whileTestStateP s1 env) ) |
193 whileTestPSem c = proof ( λ _ → record { pi1 = refl ; pi2 = refl } ) | |
64 | 194 |
67 | 195 SemGears : (f : {l : Level } {t : Set l } → (e0 : Envc ) → ((e : Envc) → t) → t ) → Set (succ Zero) |
196 SemGears f = Envc → Envc → Set | |
197 | |
68 | 198 GearsUnitSound : (e0 e1 : Envc) {pre : Envc → Set} {post : Envc → Set} |
199 → (f : {l : Level } {t : Set l } → (e0 : Envc ) → (Envc → t) → t ) | |
200 → (fsem : (e0 : Envc ) → f e0 ( λ e1 → (pre e0) implies (post e1))) | |
201 → f e0 (λ e1 → pre e0 implies post e1) | |
69 | 202 GearsUnitSound e0 e1 f fsem = fsem e0 |
203 | |
204 whileTestPSemSound : (c : ℕ ) (output : Envc ) → output ≡ whileTestP c (λ e → e) → ⊤ implies ((vari output ≡ 0) /\ (varn output ≡ c)) | |
71 | 205 whileTestPSemSound c output refl = whileTestPSem c |
64 | 206 |
69 | 207 whileLoopPSem : {l : Level} {t : Set l} → (input : Envc ) → whileTestStateP s2 input |
208 → (next : (output : Envc ) → (whileTestStateP s2 input ) implies (whileTestStateP s2 output) → t) | |
209 → (exit : (output : Envc ) → (whileTestStateP s2 input ) implies (whileTestStateP sf output) → t) → t | |
210 whileLoopPSem env s next exit with <-cmp 0 (varn env) | |
71 | 211 whileLoopPSem env s next exit | tri≈ ¬a b ¬c rewrite (sym b) = exit env (proof (λ z → z)) |
212 whileLoopPSem env s next exit | tri< a ¬b ¬c = next env (proof (λ z → z)) | |
69 | 213 |
71 | 214 |
215 | |
72 | 216 whileLoopPSem' : {l : Level} {t : Set l} → (input : Envc ) → whileTestStateP s2 input |
217 → (next : (output : Envc ) → (whileTestStateP s2 input ) implies (whileTestStateP s2 output) → t) | |
218 → (exit : (output : Envc ) → (whileTestStateP s2 input ) implies (whileTestStateP sf output) → t) → t | |
219 whileLoopPSem' env@(record { c10 = c10 ; varn = zero ; vari = vari }) s next exit = exit env (proof (λ z → z)) | |
220 whileLoopPSem' env@(record { c10 = c10 ; varn = (suc varn₁) ; vari = vari }) s next exit = next env (proof (λ z → z)) | |
221 | |
222 | |
73 | 223 loopPP : (n : ℕ) → (input : Envc ) → (n ≡ varn input) → Envc |
224 loopPP n input@(record { c10 = c10 ; varn = zero ; vari = vari }) refl = input | |
225 loopPP n input@(record { c10 = c10 ; varn = (suc varn₁) ; vari = vari }) refl = whileLoopP (record { c10 = c10 ; varn = (varn₁) ; vari = vari }) (λ x → loopPP (n - 1) (record { c10 = c10 ; varn = (varn₁) ; vari = vari }) refl) (λ output → output) | |
72 | 226 |
73 | 227 loopPPSem : {l : Level} {t : Set l} → (input output : Envc ) → loopPP (varn input) input refl ≡ output |
228 → (whileTestStateP s2 input ) implies (whileTestStateP sf output) | |
229 loopPPSem {l} {t} input output refl = loopPPSemInduct (varn input) input refl | |
230 where | |
231 loopPPSemInduct : (n : ℕ) → (current : Envc) → varn current ≡ n → (whileTestStateP s2 current ) implies (whileTestStateP sf output) | |
232 loopPPSemInduct zero current refl = proof λ x → {!!} | |
233 loopPPSemInduct (suc n) current refl with whileLoopPSem current {!!} {!!} {!!} | |
234 ... | aa = {!!} | |
72 | 235 |
236 | |
237 | |
238 lpc : (input : Envc ) → Envc | |
239 lpc input@(record { c10 = c10 ; varn = zero ; vari = vari }) = input | |
240 lpc input@(record { c10 = c10 ; varn = (suc varn₁) ; vari = vari }) = whileLoopP (record { c10 = c10 ; varn = (varn₁) ; vari = vari }) (λ x → lpc record { c10 = c10 ; varn = (varn₁) ; vari = vari }) (λ output → output) | |
241 | |
242 | |
243 | |
244 | |
245 -- loopPP' input | tri≈ ¬a b ¬c = input | |
246 -- loopPP' input | tri< a ¬b ¬c = whileLoopP input (λ enext → loopPP' enext) (λ eout → eout) | |
247 -- loopPP' (whileLoopP input (λ next → loopPP' next) (λ output → output)) | |
248 | |
249 | |
70 | 250 -- = whileLoopP input (λ next → loopPP next ) (λ output → output ) |
251 | |
69 | 252 whileLoopPSemSound : (input output : Envc ) |
253 → whileTestStateP s2 input | |
72 | 254 → output ≡ lpc input |
69 | 255 → (whileTestStateP s2 input ) implies ( whileTestStateP sf output ) |
73 | 256 whileLoopPSemSound input output pre eq = {!!} |
257 | |
258 -- with (lpc input) | |
259 -- record { c10 = c11 ; varn = varn₁ ; vari = vari₁ } .(lpc (record { c10 = c11 ; varn = varn₁ ; vari = vari₁ })) pre refl | record { c10 = c10 ; varn = varn ; vari = vari } = proof λ x → {!!} | |
72 | 260 -- where |
261 -- lem : (whileTestStateP s2 input ) → (varn input + vari input ≡ c10 input) | |
262 -- implies (vari output ≡ c10 output) | |
263 -- lem refl = proof λ x → {!!} | |
264 | |
265 | |
266 -- whileLoopPSem' input pre (λ output1 x → proof (λ x₁ → ?)) (λ output₁ x → proof (λ x₁ → ?)))) | |
267 -- proof (whileLoopPwP input pre (λ e p1 p11 → {!!}) (λ e2 p2 p22 → {!!}) ) | |
71 | 268 -- with <-cmp 0 (varn input ) |
269 -- ... | ttt = {!!} | |
62 | 270 |
271 -- induction にする | |
53 | 272 {-# TERMINATING #-} |
54 | 273 loopPwP : {l : Level} {t : Set l} → (env : Envc ) → whileTestStateP s2 env → (exit : (env : Envc ) → whileTestStateP sf env → t) → t |
274 loopPwP env s exit = whileLoopPwP env s (λ env s → loopPwP env s exit ) exit | |
51 | 275 |
62 | 276 -- wP を Env のRel にする Env → Env → Set にしちゃう |
54 | 277 whileTestPCallwP : (c : ℕ ) → Set |
278 whileTestPCallwP c = whileTestPwP {_} {_} c ( λ env s → loopPwP env (conv env s) ( λ env s → vari env ≡ c ) ) where | |
70 | 279 conv : (env : Envc ) → (vari env ≡ 0) /\ (varn env ≡ c10 env) → varn env + vari env ≡ c10 env |
280 conv e record { pi1 = refl ; pi2 = refl } = +zero | |
55 | 281 |
59 | 282 |
283 conv1 : (env : Envc ) → (vari env ≡ 0) /\ (varn env ≡ c10 env) → varn env + vari env ≡ c10 env | |
284 conv1 e record { pi1 = refl ; pi2 = refl } = +zero | |
285 | |
286 -- = whileTestPwP (suc c) (λ env s → loopPwP env (conv1 env s) (λ env₁ s₁ → {!!})) | |
287 | |
61 | 288 |
62 | 289 data GComm : Set (succ Zero) where |
290 Skip : GComm | |
291 Abort : GComm | |
292 PComm : Set → GComm | |
293 -- Seq : GComm → GComm → GComm | |
294 -- If : whileTestState → GComm → GComm → GComm | |
295 while : whileTestState → GComm → GComm | |
61 | 296 |
62 | 297 gearsSem : {l : Level} {t : Set l} → {c10 : ℕ} → Envc → Envc → (Envc → (Envc → t) → t) → Set |
298 gearsSem pre post = {!!} | |
299 | |
300 unionInf : ∀ {l} -> (ℕ -> Rel Set l) -> Rel Set l | |
301 unionInf f a b = ∃ (λ (n : ℕ) → f n a b) | |
302 | |
303 comp : ∀ {l} → Rel Set l → Rel Set l → Rel Set (succ Zero Level.⊔ l) | |
304 comp r1 r2 a b = ∃ (λ (a' : Set) → r1 a a' × r2 a' b) | |
305 | |
306 -- repeat : ℕ -> rel set zero -> rel set zero | |
307 -- repeat ℕ.zero r = λ x x₁ → ⊤ | |
308 -- repeat (ℕ.suc m) r = comp (repeat m r) r | |
309 | |
310 GSemComm : {l : Level} {t : Set l} → GComm → Rel whileTestState (Zero) | |
311 GSemComm Skip = λ x x₁ → ⊤ | |
312 GSemComm Abort = λ x x₁ → ⊥ | |
313 GSemComm (PComm x) = λ x₁ x₂ → x | |
314 -- GSemComm (Seq con con₁ con₃) = λ x₁ x₂ → {!!} | |
315 -- GSemComm (If x con con₁) = {!!} | |
316 GSemComm (while x con) = λ x₁ x₂ → unionInf {Zero} (λ (n : ℕ) → {!!}) {!!} {!!} | |
317 | |
318 ProofConnect : {l : Level} {t : Set l} | |
319 → (pr1 : Envc → Set → Set) | |
320 → (Envc → Set → (Envc → Set → t)) | |
321 → (Envc → Set → Set) | |
322 ProofConnect prev f env post = {!!} -- with f env ({!!}) {!!} | |
60 | 323 |
324 Proof2 : (env : Envc) → (vari env ≡ c10 env) → vari env ≡ c10 env | |
325 Proof2 _ refl = refl | |
326 | |
327 | |
61 | 328 -- Proof1 : (env : Envc) → (s : varn env + vari env ≡ c10 env) → ((env : Envc) → (vari env ≡ c10 env) → vari env ≡ c10 env) → vari env ≡ c10 env |
60 | 329 Proof1 : (env : Envc) → (s : varn env + vari env ≡ c10 env) → loopPwP env s ( λ env s → vari env ≡ c10 env ) |
61 | 330 Proof1 env s = {!!} |
60 | 331 |
55 | 332 Proof : (c : ℕ ) → whileTestPCallwP c |
61 | 333 Proof c = {!!} |