annotate paper/src/whileTestGears.agda.replaced @ 14:a63df15c9afc default tip

DONE
author soto <soto@cr.ie.u-ryukyu.ac.jp>
date Mon, 15 Feb 2021 23:36:39 +0900
parents 959f4b34d6f4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
1 module whileTestGears where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
2
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
3 open import Function
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
4 open import Data.Nat
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
5 open import Data.Bool hiding ( _@$\stackrel{?}{=}$@_ ; _@$\leq$@?_ ; _@$\leq$@_ ; _<_)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
6 open import Data.Product
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
7 open import Level renaming ( suc to succ ; zero to Zero )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
8 open import Relation.Nullary using (@$\neg$@_; Dec; yes; no)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
9 open import Relation.Binary.PropositionalEquality
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
10 open import Agda.Builtin.Unit
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
11
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
12 open import utilities
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
13 open _@$\wedge$@_
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
14
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
15 -- original codeGear (with non terminatinng )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
16
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
17 record Env : Set (succ Zero) where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
18 field
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
19 varn : @$\mathbb{N}$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
20 vari : @$\mathbb{N}$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
21 open Env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
22
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
23 whileTest : {l : Level} {t : Set l} @$\rightarrow$@ (c10 : @$\mathbb{N}$@) @$\rightarrow$@ (Code : Env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
24 whileTest c10 next = next (record {varn = c10 ; vari = 0 } )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
25
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
26 {-@$\#$@ TERMINATING @$\#$@-}
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
27 whileLoop : {l : Level} {t : Set l} @$\rightarrow$@ Env @$\rightarrow$@ (Code : Env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
28 whileLoop env next with lt 0 (varn env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
29 whileLoop env next | false = next env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
30 whileLoop env next | true =
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
31 whileLoop (record env {varn = (varn env) - 1 ; vari = (vari env) + 1}) next
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
32
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
33 test1 : Env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
34 test1 = whileTest 10 (@$\lambda$@ env @$\rightarrow$@ whileLoop env (@$\lambda$@ env1 @$\rightarrow$@ env1 ))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
35
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
36 proof1 : whileTest 10 (@$\lambda$@ env @$\rightarrow$@ whileLoop env (@$\lambda$@ e @$\rightarrow$@ (vari e) @$\equiv$@ 10 ))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
37 proof1 = refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
38
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
39 -- codeGear with pre-condtion and post-condition
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
40 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
41 -- ↓PostCondition
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
42 whileTest' : {l : Level} {t : Set l} @$\rightarrow$@ {c10 : @$\mathbb{N}$@ } @$\rightarrow$@ (Code : (env : Env ) @$\rightarrow$@ ((vari env) @$\equiv$@ 0) @$\wedge$@ ((varn env) @$\equiv$@ c10) @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
43 whileTest' {_} {_} {c10} next = next env proof2
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
44 where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
45 env : Env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
46 env = record {vari = 0 ; varn = c10 }
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
47 proof2 : ((vari env) @$\equiv$@ 0) @$\wedge$@ ((varn env) @$\equiv$@ c10) -- PostCondition
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
48 proof2 = record {pi1 = refl ; pi2 = refl}
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
49
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
50
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
51 open import Data.Empty
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
52 open import Data.Nat.Properties
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
53
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
54
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
55 {-@$\#$@ TERMINATING @$\#$@-} -- ↓PreCondition(Invaliant)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
56 whileLoop' : {l : Level} {t : Set l} @$\rightarrow$@ (env : Env ) @$\rightarrow$@ {c10 : @$\mathbb{N}$@ } @$\rightarrow$@ ((varn env) + (vari env) @$\equiv$@ c10) @$\rightarrow$@ (Code : Env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
57 whileLoop' env proof next with ( suc zero @$\leq$@? (varn env) )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
58 whileLoop' env proof next | no p = next env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
59 whileLoop' env {c10} proof next | yes p = whileLoop' env1 (proof3 p ) next
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
60 where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
61 env1 = record env {varn = (varn env) - 1 ; vari = (vari env) + 1}
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
62 1<0 : 1 @$\leq$@ zero @$\rightarrow$@ @$\bot$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
63 1<0 ()
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
64 proof3 : (suc zero @$\leq$@ (varn env)) @$\rightarrow$@ varn env1 + vari env1 @$\equiv$@ c10
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
65 proof3 (s@$\leq$@s lt) with varn env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
66 proof3 (s@$\leq$@s z@$\leq$@n) | zero = @$\bot$@-elim (1<0 p)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
67 proof3 (s@$\leq$@s (z@$\leq$@n {n'}) ) | suc n = let open @$\equiv$@-Reasoning in
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
68 begin
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
69 n' + (vari env + 1)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
70 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ z @$\rightarrow$@ n' + z ) ( +-sym {vari env} {1} ) @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
71 n' + (1 + vari env )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
72 @$\equiv$@@$\langle$@ sym ( +-assoc (n') 1 (vari env) ) @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
73 (n' + 1) + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
74 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ z @$\rightarrow$@ z + vari env ) +1@$\equiv$@suc @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
75 (suc n' ) + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
76 @$\equiv$@@$\langle$@@$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
77 varn env + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
78 @$\equiv$@@$\langle$@ proof @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
79 c10
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
80 @$\blacksquare$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
81
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
82 -- Condition to Invariant
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
83 conversion1 : {l : Level} {t : Set l } @$\rightarrow$@ (env : Env ) @$\rightarrow$@ {c10 : @$\mathbb{N}$@ } @$\rightarrow$@ ((vari env) @$\equiv$@ 0) @$\wedge$@ ((varn env) @$\equiv$@ c10)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
84 @$\rightarrow$@ (Code : (env1 : Env ) @$\rightarrow$@ (varn env1 + vari env1 @$\equiv$@ c10) @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
85 conversion1 env {c10} p1 next = next env proof4
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
86 where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
87 proof4 : varn env + vari env @$\equiv$@ c10
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
88 proof4 = let open @$\equiv$@-Reasoning in
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
89 begin
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
90 varn env + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
91 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ n @$\rightarrow$@ n + vari env ) (pi2 p1 ) @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
92 c10 + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
93 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ n @$\rightarrow$@ c10 + n ) (pi1 p1 ) @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
94 c10 + 0
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
95 @$\equiv$@@$\langle$@ +-sym {c10} {0} @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
96 c10
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
97 @$\blacksquare$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
98
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
99 -- all proofs are connected
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
100 proofGears : {c10 : @$\mathbb{N}$@ } @$\rightarrow$@ Set
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
101 proofGears {c10} = whileTest' {_} {_} {c10} (@$\lambda$@ n p1 @$\rightarrow$@ conversion1 n p1 (@$\lambda$@ n1 p2 @$\rightarrow$@ whileLoop' n1 p2 (@$\lambda$@ n2 @$\rightarrow$@ ( vari n2 @$\equiv$@ c10 ))))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
102
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
103 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
104 -- codeGear with loop step and closed environment
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
105 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
106
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
107 open import Relation.Binary
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
108
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
109 record Envc : Set (succ Zero) where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
110 field
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
111 c10 : @$\mathbb{N}$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
112 varn : @$\mathbb{N}$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
113 vari : @$\mathbb{N}$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
114 open Envc
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
115
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
116 whileTestP : {l : Level} {t : Set l} @$\rightarrow$@ (c10 : @$\mathbb{N}$@) @$\rightarrow$@ (Code : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
117 whileTestP c10 next = next (record {varn = c10 ; vari = 0 ; c10 = c10 } )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
118
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
119 whileLoopP : {l : Level} {t : Set l} @$\rightarrow$@ Envc @$\rightarrow$@ (next : Envc @$\rightarrow$@ t) @$\rightarrow$@ (exit : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
120 whileLoopP env next exit with <-cmp 0 (varn env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
121 whileLoopP env next exit | tri@$\thickapprox$@ @$\neg$@a b @$\neg$@c = exit env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
122 whileLoopP env next exit | tri< a @$\neg$@b @$\neg$@c =
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
123 next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 })
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
124
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
125 -- equivalent of whileLoopP but it looks like an induction on varn
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
126 whileLoopP' : {l : Level} {t : Set l} @$\rightarrow$@ (n : @$\mathbb{N}$@) @$\rightarrow$@ (env : Envc) @$\rightarrow$@ (n @$\equiv$@ varn env) @$\rightarrow$@ (next : Envc @$\rightarrow$@ t) @$\rightarrow$@ (exit : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
127 whileLoopP' zero env refl _ exit = exit env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
128 whileLoopP' (suc n) env refl next _ = next (record {c10 = (c10 env) ; varn = varn env ; vari = suc (vari env) })
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
129
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
130 -- normal loop without termination
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
131 {-@$\#$@ TERMINATING @$\#$@-}
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
132 loopP : {l : Level} {t : Set l} @$\rightarrow$@ Envc @$\rightarrow$@ (exit : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
133 loopP env exit = whileLoopP env (@$\lambda$@ env @$\rightarrow$@ loopP env exit ) exit
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
134
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
135 whileTestPCall : (c10 : @$\mathbb{N}$@ ) @$\rightarrow$@ Envc
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
136 whileTestPCall c10 = whileTestP {_} {_} c10 (@$\lambda$@ env @$\rightarrow$@ loopP env (@$\lambda$@ env @$\rightarrow$@ env))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
137
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
138 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
139 -- codeGears with states of condition
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
140 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
141 data whileTestState : Set where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
142 s1 : whileTestState
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
143 s2 : whileTestState
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
144 sf : whileTestState
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
145
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
146 whileTestStateP : whileTestState @$\rightarrow$@ Envc @$\rightarrow$@ Set
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
147 whileTestStateP s1 env = (vari env @$\equiv$@ 0) @$\wedge$@ (varn env @$\equiv$@ c10 env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
148 whileTestStateP s2 env = (varn env + vari env @$\equiv$@ c10 env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
149 whileTestStateP sf env = (vari env @$\equiv$@ c10 env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
150
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
151 whileTestPwP : {l : Level} {t : Set l} @$\rightarrow$@ (c10 : @$\mathbb{N}$@) @$\rightarrow$@ ((env : Envc ) @$\rightarrow$@ whileTestStateP s1 env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
152 whileTestPwP c10 next = next env record { pi1 = refl ; pi2 = refl } where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
153 env : Envc
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
154 env = whileTestP c10 ( @$\lambda$@ env @$\rightarrow$@ env )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
155
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
156 whileLoopPwP : {l : Level} {t : Set l} @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ whileTestStateP s2 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
157 @$\rightarrow$@ (next : (env : Envc ) @$\rightarrow$@ whileTestStateP s2 env @$\rightarrow$@ t)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
158 @$\rightarrow$@ (exit : (env : Envc ) @$\rightarrow$@ whileTestStateP sf env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
159 whileLoopPwP env s next exit with <-cmp 0 (varn env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
160 whileLoopPwP env s next exit | tri@$\thickapprox$@ @$\neg$@a b @$\neg$@c = exit env (lem (sym b) s)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
161 where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
162 lem : (varn env @$\equiv$@ 0) @$\rightarrow$@ (varn env + vari env @$\equiv$@ c10 env) @$\rightarrow$@ vari env @$\equiv$@ c10 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
163 lem refl refl = refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
164 whileLoopPwP env s next exit | tri< a @$\neg$@b @$\neg$@c = next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 }) (proof5 a)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
165 where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
166 1<0 : 1 @$\leq$@ zero @$\rightarrow$@ @$\bot$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
167 1<0 ()
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
168 proof5 : (suc zero @$\leq$@ (varn env)) @$\rightarrow$@ (varn env - 1) + (vari env + 1) @$\equiv$@ c10 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
169 proof5 (s@$\leq$@s lt) with varn env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
170 proof5 (s@$\leq$@s z@$\leq$@n) | zero = @$\bot$@-elim (1<0 a)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
171 proof5 (s@$\leq$@s (z@$\leq$@n {n'}) ) | suc n = let open @$\equiv$@-Reasoning in
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
172 begin
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
173 n' + (vari env + 1)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
174 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ z @$\rightarrow$@ n' + z ) ( +-sym {vari env} {1} ) @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
175 n' + (1 + vari env )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
176 @$\equiv$@@$\langle$@ sym ( +-assoc (n') 1 (vari env) ) @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
177 (n' + 1) + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
178 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ z @$\rightarrow$@ z + vari env ) +1@$\equiv$@suc @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
179 (suc n' ) + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
180 @$\equiv$@@$\langle$@@$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
181 varn env + vari env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
182 @$\equiv$@@$\langle$@ s @$\rangle$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
183 c10 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
184 @$\blacksquare$@
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
185
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
186
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
187 whileLoopPwP' : {l : Level} {t : Set l} @$\rightarrow$@ (n : @$\mathbb{N}$@) @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ (n @$\equiv$@ varn env) @$\rightarrow$@ whileTestStateP s2 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
188 @$\rightarrow$@ (next : (env : Envc ) @$\rightarrow$@ (pred n @$\equiv$@ varn env) @$\rightarrow$@ whileTestStateP s2 env @$\rightarrow$@ t)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
189 @$\rightarrow$@ (exit : (env : Envc ) @$\rightarrow$@ whileTestStateP sf env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
190 whileLoopPwP' zero env refl refl next exit = exit env refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
191 whileLoopPwP' (suc n) env refl refl next exit = next (record env {varn = pred (varn env) ; vari = suc (vari env) }) refl (+-suc n (vari env))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
192
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
193
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
194 {-@$\#$@ TERMINATING @$\#$@-}
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
195 loopPwP : {l : Level} {t : Set l} @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ whileTestStateP s2 env @$\rightarrow$@ (exit : (env : Envc ) @$\rightarrow$@ whileTestStateP sf env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
196 loopPwP env s exit = whileLoopPwP env s (@$\lambda$@ env s @$\rightarrow$@ loopPwP env s exit ) exit
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
197
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
198
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
199 loopPwP' : {l : Level} {t : Set l} @$\rightarrow$@ (n : @$\mathbb{N}$@) @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ (n @$\equiv$@ varn env) @$\rightarrow$@ whileTestStateP s2 env @$\rightarrow$@ (exit : (env : Envc ) @$\rightarrow$@ whileTestStateP sf env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
200 loopPwP' zero env refl refl exit = exit env refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
201 loopPwP' (suc n) env refl refl exit = whileLoopPwP' (suc n) env refl refl (@$\lambda$@ env x y @$\rightarrow$@ loopPwP' n env x y exit) exit
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
202
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
203
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
204 loopHelper : (n : @$\mathbb{N}$@) @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ (eq : varn env @$\equiv$@ n) @$\rightarrow$@ (seq : whileTestStateP s2 env) @$\rightarrow$@ loopPwP' n env (sym eq) seq @$\lambda$@ env@$\_{1}$@ x @$\rightarrow$@ (vari env@$\_{1}$@ @$\equiv$@ c10 env@$\_{1}$@)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
205 loopHelper zero env eq refl rewrite eq = refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
206 loopHelper (suc n) env eq refl rewrite eq = loopHelper n (record { c10 = suc (n + vari env) ; varn = n ; vari = suc (vari env) }) refl (+-suc n (vari env))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
207
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
208
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
209 -- all codtions are correctly connected and required condtion is proved in the continuation
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
210 -- use required condition as t in (env @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
211 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
212 whileTestPCallwP : (c : @$\mathbb{N}$@ ) @$\rightarrow$@ Set
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
213 whileTestPCallwP c = whileTestPwP {_} {_} c ( @$\lambda$@ env s @$\rightarrow$@ loopPwP env (conv env s) ( @$\lambda$@ env s @$\rightarrow$@ vari env @$\equiv$@ c10 env ) ) where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
214 conv : (env : Envc ) @$\rightarrow$@ (vari env @$\equiv$@ 0) @$\wedge$@ (varn env @$\equiv$@ c10 env) @$\rightarrow$@ varn env + vari env @$\equiv$@ c10 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
215 conv e record { pi1 = refl ; pi2 = refl } = +zero
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
216
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
217
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
218 whileTestPCallwP' : (c : @$\mathbb{N}$@ ) @$\rightarrow$@ Set
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
219 whileTestPCallwP' c = whileTestPwP {_} {_} c (@$\lambda$@ env s @$\rightarrow$@ loopPwP' (varn env) env refl (conv env s) ( @$\lambda$@ env s @$\rightarrow$@ vari env @$\equiv$@ c10 env ) ) where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
220 conv : (env : Envc ) @$\rightarrow$@ (vari env @$\equiv$@ 0) @$\wedge$@ (varn env @$\equiv$@ c10 env) @$\rightarrow$@ varn env + vari env @$\equiv$@ c10 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
221 conv e record { pi1 = refl ; pi2 = refl } = +zero
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
222
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
223 helperCallwP : (c : @$\mathbb{N}$@) @$\rightarrow$@ whileTestPCallwP' c
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
224 helperCallwP c = whileTestPwP {_} {_} c (@$\lambda$@ env s @$\rightarrow$@ loopHelper c (record { c10 = c ; varn = c ; vari = zero }) refl +zero)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
225
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
226 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
227 -- Using imply relation to make soundness explicit
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
228 -- termination is shown by induction on varn
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
229 --
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
230
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
231 data _implies_ (A B : Set ) : Set (succ Zero) where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
232 proof : ( A @$\rightarrow$@ B ) @$\rightarrow$@ A implies B
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
233
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
234 whileTestPSem : (c : @$\mathbb{N}$@) @$\rightarrow$@ whileTestP c ( @$\lambda$@ env @$\rightarrow$@ @$\top$@ implies (whileTestStateP s1 env) )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
235 whileTestPSem c = proof ( @$\lambda$@ _ @$\rightarrow$@ record { pi1 = refl ; pi2 = refl } )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
236
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
237 whileTestPSemSound : (c : @$\mathbb{N}$@ ) (output : Envc ) @$\rightarrow$@ output @$\equiv$@ whileTestP c (@$\lambda$@ e @$\rightarrow$@ e) @$\rightarrow$@ @$\top$@ implies ((vari output @$\equiv$@ 0) @$\wedge$@ (varn output @$\equiv$@ c))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
238 whileTestPSemSound c output refl = whileTestPSem c
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
239
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
240
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
241 whileConvPSemSound : {l : Level} @$\rightarrow$@ (input : Envc) @$\rightarrow$@ (whileTestStateP s1 input ) implies (whileTestStateP s2 input)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
242 whileConvPSemSound input = proof @$\lambda$@ x @$\rightarrow$@ (conv input x) where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
243 conv : (env : Envc ) @$\rightarrow$@ (vari env @$\equiv$@ 0) @$\wedge$@ (varn env @$\equiv$@ c10 env) @$\rightarrow$@ varn env + vari env @$\equiv$@ c10 env
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
244 conv e record { pi1 = refl ; pi2 = refl } = +zero
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
245
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
246 loopPP : (n : @$\mathbb{N}$@) @$\rightarrow$@ (input : Envc ) @$\rightarrow$@ (n @$\equiv$@ varn input) @$\rightarrow$@ Envc
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
247 loopPP zero input refl = input
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
248 loopPP (suc n) input refl =
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
249 loopPP n (record input { varn = pred (varn input) ; vari = suc (vari input)}) refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
250
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
251 whileLoopPSem : {l : Level} {t : Set l} @$\rightarrow$@ (input : Envc ) @$\rightarrow$@ whileTestStateP s2 input
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
252 @$\rightarrow$@ (next : (output : Envc ) @$\rightarrow$@ (whileTestStateP s2 input ) implies (whileTestStateP s2 output) @$\rightarrow$@ t)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
253 @$\rightarrow$@ (exit : (output : Envc ) @$\rightarrow$@ (whileTestStateP s2 input ) implies (whileTestStateP sf output) @$\rightarrow$@ t) @$\rightarrow$@ t
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
254 whileLoopPSem env s next exit with varn env | s
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
255 ... | zero | _ = exit env (proof (@$\lambda$@ z @$\rightarrow$@ z))
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
256 ... | (suc varn ) | refl = next ( record env { varn = varn ; vari = suc (vari env) } ) (proof @$\lambda$@ x @$\rightarrow$@ +-suc varn (vari env) )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
257
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
258 loopPPSem : (input output : Envc ) @$\rightarrow$@ output @$\equiv$@ loopPP (varn input) input refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
259 @$\rightarrow$@ (whileTestStateP s2 input ) @$\rightarrow$@ (whileTestStateP s2 input ) implies (whileTestStateP sf output)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
260 loopPPSem input output refl s2p = loopPPSemInduct (varn input) input refl refl s2p
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
261 where
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
262 lem : (n : @$\mathbb{N}$@) @$\rightarrow$@ (env : Envc) @$\rightarrow$@ n + suc (vari env) @$\equiv$@ suc (n + vari env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
263 lem n env = +-suc (n) (vari env)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
264 loopPPSemInduct : (n : @$\mathbb{N}$@) @$\rightarrow$@ (current : Envc) @$\rightarrow$@ (eq : n @$\equiv$@ varn current) @$\rightarrow$@ (loopeq : output @$\equiv$@ loopPP n current eq)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
265 @$\rightarrow$@ (whileTestStateP s2 current ) @$\rightarrow$@ (whileTestStateP s2 current ) implies (whileTestStateP sf output)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
266 loopPPSemInduct zero current refl loopeq refl rewrite loopeq = proof (@$\lambda$@ x @$\rightarrow$@ refl)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
267 loopPPSemInduct (suc n) current refl loopeq refl rewrite (sym (lem n current)) =
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
268 whileLoopPSem current refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
269 (@$\lambda$@ output x @$\rightarrow$@ loopPPSemInduct n (record { c10 = n + suc (vari current) ; varn = n ; vari = suc (vari current) }) refl loopeq refl)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
270 (@$\lambda$@ output x @$\rightarrow$@ loopPPSemInduct n (record { c10 = n + suc (vari current) ; varn = n ; vari = suc (vari current) }) refl loopeq refl)
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
271
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
272 whileLoopPSemSound : {l : Level} @$\rightarrow$@ (input output : Envc )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
273 @$\rightarrow$@ whileTestStateP s2 input
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
274 @$\rightarrow$@ output @$\equiv$@ loopPP (varn input) input refl
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
275 @$\rightarrow$@ (whileTestStateP s2 input ) implies ( whileTestStateP sf output )
959f4b34d6f4 add final thesis
soto
parents:
diff changeset
276 whileLoopPSemSound {l} input output pre eq = loopPPSem input output eq pre