4
|
1 module whileTestGears where
|
|
2
|
|
3 open import Function
|
|
4 open import Data.Nat
|
10
|
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
|
4
|
19 whileTest : {l : Level} {t : Set l} -> (Code : Env -> t) -> t
|
|
20 whileTest next = next (record {varn = 10 ; vari = 0} )
|
|
21
|
|
22 {-# TERMINATING #-}
|
|
23 whileLoop : {l : Level} {t : Set l} -> Env -> (Code : Env -> t) -> t
|
|
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
|
|
30 test1 = whileTest (λ env → whileLoop env (λ env1 → env1 ))
|
|
31
|
|
32
|
|
33 proof1 : whileTest (λ env → whileLoop env (λ e → (vari e) ≡ 10 ))
|
|
34 proof1 = refl
|
|
35
|
10
|
36 whileTest' : {l : Level} {t : Set l} -> (Code : (env : Env) -> ((vari env) ≡ 0) /\ ((varn env) ≡ 10) -> t) -> t
|
4
|
37 whileTest' next = next env proof2
|
|
38 where
|
|
39 env : Env
|
|
40 env = record {vari = 0 ; varn = 10}
|
10
|
41 proof2 : ((vari env) ≡ 0) /\ ((varn env) ≡ 10)
|
4
|
42 proof2 = record {pi1 = refl ; pi2 = refl}
|
|
43
|
|
44 {-# TERMINATING #-}
|
5
|
45 whileLoop' : {l : Level} {t : Set l} -> (env : Env) -> ((varn env) + (vari env) ≡ 10) -> (Code : Env -> t) -> t
|
9
|
46 whileLoop' env proof next with ( suc zero ≤? (varn env) )
|
|
47 whileLoop' env proof next | no p = next env
|
|
48 whileLoop' env proof next | yes p = whileLoop' env1 (proof3 p ) next
|
4
|
49 where
|
|
50 env1 = record {varn = (varn env) - 1 ; vari = (vari env) + 1}
|
9
|
51 proof3 : (suc zero ≤ (varn env)) → varn env1 + vari env1 ≡ 10
|
|
52 proof3 (s≤s lt) with varn env
|
|
53 proof3 (s≤s z≤n) | zero = {!!}
|
|
54 proof3 (s≤s lt) | suc n = {!!}
|
6
|
55
|
4
|
56
|
10
|
57 conversion1 : {l : Level} {t : Set l } → (env : Env) -> ((vari env) ≡ 0) /\ ((varn env) ≡ 10)
|
5
|
58 -> (Code : (env1 : Env) -> (varn env1 + vari env1 ≡ 10) -> t) -> t
|
6
|
59 conversion1 env p1 next = next env proof4
|
|
60 where
|
|
61 proof4 : varn env + vari env ≡ 10
|
|
62 proof4 = let open ≡-Reasoning in
|
|
63 begin
|
|
64 varn env + vari env
|
|
65 ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩
|
|
66 10 + vari env
|
|
67 ≡⟨ cong ( λ n → 10 + n ) (pi1 p1 ) ⟩
|
|
68 10 + 0
|
|
69 ≡⟨⟩
|
|
70 10
|
|
71 ∎
|
4
|
72
|
6
|
73
|
|
74 proofGears : Set
|
|
75 proofGears = whileTest' (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ 10 ))))
|
9
|
76
|
|
77 proofGearsMeta : whileTest' (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ 10 ))))
|
|
78 proofGearsMeta = refl
|