Mercurial > hg > Members > ryokka > HoareLogic
annotate whileTestGears.agda @ 52:0bde332e1215
use state as t
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 Dec 2019 15:48:11 +0900 |
parents | 3f4f93ac841d |
children | 03235251b3a7 |
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 |
42 | 13 record Env : Set (succ Zero) where |
6 | 14 field |
15 varn : ℕ | |
16 vari : ℕ | |
42 | 17 open Env |
6 | 18 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
19 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
|
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 = | |
42 | 27 whileLoop (record env {varn = (varn env) - 1 ; vari = (vari env) + 1}) next |
4 | 28 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
29 test1 : Env |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
30 test1 = whileTest 10 (λ env → whileLoop env (λ env1 → env1 )) |
4 | 31 |
32 | |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
33 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
|
34 proof1 = refl |
4 | 35 |
16 | 36 -- ↓PostCondition |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
37 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
|
38 whileTest' {_} {_} {c10} next = next env proof2 |
4 | 39 where |
42 | 40 env : Env |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
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) |
42 | 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 |
42 | 55 env1 = record env {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 |
47 | 59 proof3 (s≤s lt) with varn env |
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) ) ⟩ | |
67 (n' + 1) + vari env | |
68 ≡⟨ cong ( λ z → z + vari env ) +1≡suc ⟩ | |
69 (suc n' ) + vari env | |
70 ≡⟨⟩ | |
71 varn env + vari env | |
72 ≡⟨ proof ⟩ | |
73 c10 | |
74 ∎ | |
6 | 75 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
76 -- Condition to Invariant |
42 | 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 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
94 proofGears : {c10 : ℕ } → Set |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
95 proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 )))) |
9 | 96 |
51 | 97 |
49 | 98 -- proofGearsMeta : {c10 : ℕ } → proofGears {c10} |
99 -- proofGearsMeta {c10} = {!!} -- net yet done | |
43 | 100 |
41 | 101 -- |
42 | 102 -- openended Env c <=> Context |
41 | 103 -- |
104 | |
105 open import Relation.Nullary | |
106 open import Relation.Binary | |
107 | |
49 | 108 whileTestP : {l : Level} {t : Set l} → (c10 : ℕ) → (Code : Env → t) → t |
109 whileTestP c10 next = next (record {varn = c10 ; vari = 0 } ) | |
110 | |
111 whileLoopP : {l : Level} {t : Set l} → Env → (next : Env → t) → (exit : Env → t) → t | |
112 whileLoopP env next exit with <-cmp 0 (varn env) | |
113 whileLoopP env next exit | tri≈ ¬a b ¬c = exit env | |
114 whileLoopP env next exit | tri< a ¬b ¬c = | |
115 next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 }) | |
116 | |
117 {-# TERMINATING #-} | |
118 loopP : {l : Level} {t : Set l} → Env → (exit : Env → t) → t | |
119 loopP env exit = whileLoopP env (λ env → loopP env exit ) exit | |
120 | |
121 whileTestPCall : {c10 : ℕ } → Set | |
122 whileTestPCall {c10} = whileTestP {_} {_} c10 (λ env → loopP env (λ env → ( vari env ≡ c10 ))) | |
123 | |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
124 data whileTestStateP (c10 i n : ℕ ) : Set where |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
125 pstate1 : (i ≡ 0) /\ (n ≡ c10) → whileTestStateP c10 i n -- n ≡ c10 |
52 | 126 pstate2 : (0 ≤ n) → (n ≤ c10) → (n + i ≡ c10) → whileTestStateP c10 i n -- 0 < n < c10 |
49 | 127 pfinstate : (n ≡ 0 ) → (i ≡ c10 ) → whileTestStateP c10 i n -- n ≡ 0 |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
128 |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
129 record EnvP : Set (succ Zero) where |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
130 field |
49 | 131 env : Env |
132 c10 : ℕ | |
133 cx : whileTestStateP c10 (vari env) (varn env) | |
134 | |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
135 open EnvP |
33 | 136 |
50 | 137 s1 : (c10 : ℕ) → EnvP |
138 s1 c10 = record {env = record {vari = 0 ; varn = c10} ; c10 = c10 ; cx = pstate1 (record {pi1 = refl ; pi2 = refl})} | |
139 | |
140 s2 : (e : EnvP) → varn (env e) > 0 → varn (env e) < c10 e → EnvP | |
141 s2 e n>0 n<c10 with <-cmp 0 (varn (env e)) | |
52 | 142 s2 e n>0 n<c10 | tri< a ¬b ¬c = |
143 record e { env = record { varn = varn (env e) - 1 ; vari = vari (env e) + 1 } ; cx = pstate2 {!!} {!!} {!!} } where | |
144 s2-1 : 0 ≤ (varn (env e) - 1) | |
145 s2-1 = {!!} | |
50 | 146 s2 e n>0 n<c10 | tri≈ ¬a b ¬c = record { env = record { varn = varn (env e) - 1 ; vari = vari (env e) + 1 } ; c10 = c10 e ; cx = pfinstate {!!} {!!} } |
147 | |
148 s3 : (e : EnvP) → varn (env e) ≡ 0 → vari (env e) ≡ c10 e | |
149 s3 record { env = record { varn = .0 ; vari = vari₁ } ; c10 = c11 ; cx = cx₁ } refl = {!!} | |
150 | |
51 | 151 s1ors2 : (e : EnvP) → EnvP |
152 s1ors2 e = {!!} | |
153 | |
154 proofs : (c : ℕ) → (λ e → vari (env e) ≡ c10 e ) (s2 {!!} {!!} {!!}) | |
155 proofs c = {!!} -- s3 (s2 ({!!}) {!!} {!!}) {!!} | |
156 | |
157 s3' : (e : EnvP) → varn (env e) ≡ 0 → (ℕ → EnvP → vari (env e) ≡ c10 e) → vari (env e) ≡ c10 e | |
158 s3' record { env = record { varn = .0 ; vari = vari₁ } ; c10 = c11 ; cx = cx₁ } refl proof = {!!} | |
159 | |
50 | 160 |
161 | |
49 | 162 whileTestPwithProof : {l : Level} {t : Set l} → (c10 : ℕ ) → (next : (e : EnvP ) → t) → t |
163 whileTestPwithProof {l} {t} c10 next = next record { env = env1 ; c10 = c10 ; cx = cx1 } where | |
164 env1 : Env | |
165 env1 = whileTestP c10 ( λ e → e ) | |
166 cx1 : whileTestStateP c10 (vari env1) (varn env1) | |
167 cx1 = pstate1 record { pi1 = refl ; pi2 = refl } | |
27 | 168 |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
169 {-# TERMINATING #-} |
49 | 170 loopPwithProof : {l : Level} {t : Set l} → (e : EnvP ) → (exit : (e : EnvP ) → t ) → t |
52 | 171 loopPwithProof e exit with <-cmp 0 (varn (env e)) | whileLoopP (env e) (λ e1 → pstate2 {!!} {!!} {!!} ) ( λ env → pfinstate {!!} {!!} ) |
172 loopPwithProof e exit | tri≈ ¬a b ¬c | tt = loopPwithProof {!!} {!!} | |
173 loopPwithProof e exit | tri< a ¬b ¬c | tt = exit {!!} | |
27 | 174 |
49 | 175 ConvP : (e : EnvP) → EnvP |
47 | 176 ConvP = {!!} |
177 | |
178 whileTestPProof : {c : ℕ } → Set | |
179 whileTestPProof {c} = whileTestPwithProof c | |
49 | 180 $ λ e → loopPwithProof e (λ e eq → vari (env e) ≡ c10 e ) (ConvP e ) |
46
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
181 |
8bf82026e4fe
simplified env with state condition
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
43
diff
changeset
|
182 whileTestPProofMeta : {c10 : ℕ } → whileTestPProof {c10} |
48 | 183 whileTestPProofMeta {c10} = {!!} |
30
dd66b94bf365
loop causes agda inifinite loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
29
diff
changeset
|
184 |
35 | 185 |