11
|
1 open import Level renaming (suc to Suc ; zero to Zero )
|
|
2 module WhileTest where
|
|
3
|
|
4 open import Relation.Binary.PropositionalEquality
|
|
5 open import Relation.Binary.Core
|
|
6 open import Data.Nat hiding (compare)
|
|
7 open import Data.Maybe
|
|
8 open import Data.List
|
|
9 open import Function
|
|
10 open import logic
|
|
11
|
13
|
12 open import Data.Bool hiding ( _∧_ ; _≟_ ) -- ; _∧_ ; _≤_ ; _<_)
|
|
13 open import Data.Product
|
11
|
14 open import Agda.Builtin.Unit
|
13
|
15 open import Relation.Nullary using (¬_; Dec; yes; no)
|
|
16
|
|
17 open import Data.Empty
|
|
18 open import Data.Nat.Properties
|
|
19
|
|
20 -- logicへ
|
|
21 +zero : { y : ℕ } → y + zero ≡ y
|
|
22 +zero {zero} = refl
|
|
23 +zero {suc y} = cong ( λ x → suc x ) ( +zero {y} )
|
|
24
|
|
25 -- utilへ
|
|
26 _-_ : ℕ → ℕ → ℕ
|
|
27 x - zero = x
|
|
28 zero - _ = zero
|
|
29 (suc x) - (suc y) = x - y
|
11
|
30
|
|
31 record Env : Set (Suc Zero) where
|
|
32 field
|
13
|
33 c10 : ℕ
|
11
|
34 varn : ℕ
|
|
35 vari : ℕ
|
|
36 open Env
|
|
37
|
|
38 data _implies_ (A B : Set ) : Set (Suc Zero) where
|
|
39 proof : ( A → B ) → A implies B
|
|
40
|
13
|
41 implies2p : {A B : Set } → A implies B → A → B
|
|
42 implies2p (proof x) = x
|
|
43
|
11
|
44 data whileTestState : Set where
|
|
45 s1 : whileTestState
|
|
46 s2 : whileTestState
|
|
47 sf : whileTestState
|
|
48
|
13
|
49 whileTestStateP : whileTestState → Env → Set
|
|
50 whileTestStateP s1 env = (vari env ≡ 0) ∧ (varn env ≡ c10 env)
|
|
51 whileTestStateP s2 env = (varn env + vari env ≡ c10 env)
|
|
52 whileTestStateP sf env = (vari env ≡ c10 env)
|
11
|
53
|
|
54 record WhileTest {m : Level } {t : Set m } : Set (Suc m) where
|
|
55 field
|
|
56 env : Env
|
|
57 whileInit : {m : Level } {t : Set m } → (c10 : ℕ) → (Env → t) → t
|
13
|
58 whileInit c10 next = next (record {c10 = c10 ; varn = c10 ; vari = 0 } )
|
11
|
59 whileLoop : Env → (Code : Env → t) → t
|
|
60 whileLoop env next = whileLoop1 (varn env) env where
|
|
61 whileLoop1 : ℕ → Env → t
|
|
62 whileLoop1 zero env = next env
|
|
63 whileLoop1 (suc t ) env = whileLoop1 t (record env {varn = t ; vari = (vari env) + 1})
|
|
64 whileTest : (c10 : ℕ) → (Env → t) → t
|
|
65 whileTest c10 next = whileInit c10 $ λ env → whileLoop env next
|
|
66
|
13
|
67 loopPP : (n : ℕ) → (input : Env ) → (n ≡ varn input) → Env
|
|
68 loopPP zero input refl = input
|
|
69 loopPP (suc n) input refl = loopPP n (record input { varn = pred (varn input) ; vari = suc (vari input)}) refl
|
|
70
|
|
71 -- init
|
|
72 whileInit-impl : (c10 : ℕ) → whileInit c10 (λ env → ⊤ implies (whileTestStateP s1 env) )
|
|
73 whileInit-impl c = proof ( λ _ → record { proj1 = refl ; proj2 = refl } )
|
|
74 whileTestPSemSound : (c : ℕ ) (output : Env ) → output ≡ whileInit c (λ e → e) → ⊤ implies ((vari output ≡ 0) ∧ (varn output ≡ c))
|
|
75 whileTestPSemSound c output refl = whileInit-impl c
|
|
76 -- init → loop
|
|
77 whileConvPSemSound : {l : Level} → (input : Env) → (whileTestStateP s1 input ) implies (whileTestStateP s2 input)
|
|
78 whileConvPSemSound input = proof λ x → (conv input x) where
|
|
79 conv : (env : Env ) → (vari env ≡ 0) ∧ (varn env ≡ c10 env ) → varn env + vari env ≡ c10 env
|
|
80 conv e record { proj1 = refl ; proj2 = refl } = +zero
|
|
81 -- loop → loop
|
|
82 whileLoopPSem : {l : Level} {t : Set l} → (input : Env ) → whileTestStateP s2 input
|
|
83 → (next : (output : Env ) → (whileTestStateP s2 input ) implies (whileTestStateP s2 output) → t)
|
|
84 → (exit : (output : Env ) → (whileTestStateP s2 input ) implies (whileTestStateP sf output) → t) → t
|
|
85 whileLoopPSem env s next exit with varn env | s
|
|
86 ... | zero | _ = exit env (proof (λ z → z))
|
|
87 ... | (suc varn ) | refl = next ( record env { varn = varn ; vari = suc (vari env) } ) (proof λ x → +-suc varn (vari env) )
|
|
88 -- loop → fin
|
|
89
|
|
90 loopPPSem : (input output : Env ) → output ≡ loopPP (varn input) input refl
|
|
91 → (whileTestStateP s2 input ) → (whileTestStateP s2 input ) implies (whileTestStateP sf output)
|
|
92 loopPPSem input output refl s2p = loopPPSemInduct (varn input) input refl refl s2p
|
|
93 where
|
|
94 lem : (n : ℕ) → (env : Env) → n + suc (vari env) ≡ suc (n + vari env)
|
|
95 lem n env = +-suc (n) (vari env)
|
|
96 loopPPSemInduct : (n : ℕ) → (current : Env) → (eq : n ≡ varn current) → (loopeq : output ≡ loopPP n current eq)
|
|
97 → (whileTestStateP s2 current ) → (whileTestStateP s2 current ) implies (whileTestStateP sf output)
|
|
98 loopPPSemInduct zero current refl loopeq refl rewrite loopeq = proof (λ x → refl)
|
|
99 loopPPSemInduct (suc n) current refl loopeq refl rewrite (sym (lem n current)) =
|
|
100 whileLoopPSem current refl
|
|
101 (λ output x → loopPPSemInduct n (record { c10 = n + suc (vari current) ; varn = n ; vari = suc (vari current) }) refl loopeq refl)
|
|
102 (λ output x → loopPPSemInduct n (record { c10 = n + suc (vari current) ; varn = n ; vari = suc (vari current) }) refl loopeq refl)
|
|
103
|
|
104
|
|
105 whileLoopPSemSound : {l : Level} → (input output : Env )
|
|
106 → whileTestStateP s2 input
|
|
107 → output ≡ loopPP (varn input) input refl
|
|
108 → (whileTestStateP s2 input ) implies ( whileTestStateP sf output )
|
|
109 whileLoopPSemSound {l} input output pre eq = loopPPSem input output eq pre
|
|
110
|
|
111 whileTestSound : {l : Level} (c : ℕ) → (output : Env) → ⊤ → whileTestStateP sf output
|
|
112 whileTestSound {l} c record { c10 = c10 ; varn = varn ; vari = vari } top =
|
|
113 implies2p (whileLoopPSemSound {l} (record { c10 = c ; varn = c ; vari = zero }) (record { c10 = c10 ; varn = c ; vari = vari}) (+zero) {!!}) (implies2p (whileConvPSemSound {l} (record { c10 = c ; varn = c ; vari = zero })) (implies2p (whileTestPSemSound c (whileInit c (λ e → e)) refl) top))
|
|
114
|
11
|
115 open WhileTest
|
|
116
|
|
117 createWhileTest : {m : Level} {t : Set m } → WhileTest {m} {t}
|
13
|
118 createWhileTest = record { env = record { c10 = 0; varn = 0; vari = 0 } }
|
11
|
119
|
|
120 test2 : ℕ
|
|
121 test2 = whileTest createWhileTest 10 $ λ e → vari e
|
|
122
|
|
123 ---
|
|
124
|
|
125 {-
|
|
126 whileTestStateP : whileTestState → Envc → Set
|
|
127 whileTestStateP s1 env = (vari env ≡ 0) /\ (varn env ≡ c10 env)
|
|
128 whileTestStateP s2 env = (varn env + vari env ≡ c10 env)
|
|
129 whileTestStateP sf env = (vari env ≡ c10 env)
|
|
130
|
|
131 whileTestP : {l : Level} {t : Set l} → (c10 : ℕ) → (Code : Envc → t) → t
|
|
132 whileTestP c10 next = next (record {varn = c10 ; vari = 0 ; c10 = c10 } )
|
|
133
|
|
134 whileTestPSem : (c : ℕ) → whileTestP c ( λ env → ⊤ implies (whileTestStateP s1 env) )
|
|
135 whileTestPSem c = proof ( λ _ → record { pi1 = refl ; pi2 = refl } )
|
|
136 -}
|