annotate Paper/src/agda/hoare-while.agda.replaced @ 2:9176dff8f38a

ADD while loop description
author soto <soto@cr.ie.u-ryukyu.ac.jp>
date Fri, 05 Nov 2021 15:19:08 +0900
parents
children 339fb67b4375
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module hoare-while where
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Data.Nat
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Level renaming ( suc to succ ; zero to Zero )
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Nat.Properties as NatProp -- <-cmp
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 open import Relation.Binary
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 record Envc : Set (succ Zero) where
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 field
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 c10 : @$\mathbb{N}$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 varn : @$\mathbb{N}$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 vari : @$\mathbb{N}$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 open Envc
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 whileTestP : {l : Level} {t : Set l} @$\rightarrow$@ (c10 : @$\mathbb{N}$@) @$\rightarrow$@ (next : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 whileTestP c10 next = next (record {varn = c10 ; vari = 0 ; c10 = c10 } )
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 whileLoopP : {l : Level} {t : Set l} @$\rightarrow$@ Envc @$\rightarrow$@ (next : Envc @$\rightarrow$@ t) @$\rightarrow$@ (exit : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 whileLoopP env next exit with (varn env)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 ... | zero = exit env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 ... | suc n = exit (record env { varn = n ; vari = (suc n) })
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 {-@$\#$@ TERMINATING @$\#$@-}
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 loopP : {l : Level} {t : Set l} @$\rightarrow$@ Envc @$\rightarrow$@ (exit : Envc @$\rightarrow$@ t) @$\rightarrow$@ t
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 loopP env exit = whileLoopP env (@$\lambda$@ env @$\rightarrow$@ loopP env exit ) exit
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 whileTestPCall : (c10 : @$\mathbb{N}$@ ) @$\rightarrow$@ Envc
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 whileTestPCall c10 = whileTestP {_} {_} c10 (@$\lambda$@ env @$\rightarrow$@ loopP env (@$\lambda$@ env @$\rightarrow$@ env))
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 ---
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 open import Data.Empty
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 --open import Relation.Nullary using (@$\neg$@_; Dec; yes; no)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 --open import Agda.Builtin.Unit
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 open import utilities
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 open import Relation.Binary.PropositionalEquality
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 open _@$\wedge$@_
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 data whileTestState : Set where
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 s1 : whileTestState
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 s2 : whileTestState
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 sf : whileTestState
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 whileTestStateP : whileTestState @$\rightarrow$@ Envc @$\rightarrow$@ Set
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 whileTestStateP s1 env = (vari env @$\equiv$@ 0) @$\wedge$@ (varn env @$\equiv$@ c10 env)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 whileTestStateP s2 env = (varn env + vari env @$\equiv$@ c10 env)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 whileTestStateP sf env = (vari env @$\equiv$@ c10 env)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
51
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 whileTestPwP : {l : Level} {t : Set l} @$\rightarrow$@ (c10 : @$\mathbb{N}$@) @$\rightarrow$@ ((env : Envc ) @$\rightarrow$@ whileTestStateP s1 env @$\rightarrow$@ t) @$\rightarrow$@ t
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 whileTestPwP c10 next = next env record { pi1 = refl ; pi2 = refl } where
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 env : Envc
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 env = whileTestP c10 ( @$\lambda$@ env @$\rightarrow$@ env )
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 whileLoopPwP : {l : Level} {t : Set l} @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ whileTestStateP s2 env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 @$\rightarrow$@ (next : (env : Envc ) @$\rightarrow$@ whileTestStateP s2 env @$\rightarrow$@ t)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 @$\rightarrow$@ (exit : (env : Envc ) @$\rightarrow$@ whileTestStateP sf env @$\rightarrow$@ t) @$\rightarrow$@ t
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 whileLoopPwP env s next exit with <-cmp 0 (varn env)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 whileLoopPwP env s next exit | tri≈ @$\neg$@a b @$\neg$@c = exit env (lem (sym b) s)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 where
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 lem : (varn env @$\equiv$@ 0) @$\rightarrow$@ (varn env + vari env @$\equiv$@ c10 env) @$\rightarrow$@ vari env @$\equiv$@ c10 env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 lem refl refl = refl
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 whileLoopPwP env s next exit | tri< a @$\neg$@b @$\neg$@c = next (record env {varn = (varn env) - 1 ; vari = (vari env) + 1 }) (proof5 a)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 where
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 1<0 : 1 @$\leq$@ zero @$\rightarrow$@ @$\bot$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 1<0 ()
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 proof5 : (suc zero @$\leq$@ (varn env)) @$\rightarrow$@ ((varn env ) - 1) + (vari env + 1) @$\equiv$@ c10 env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 proof5 (s@$\leq$@s lt) with varn env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 proof5 (s@$\leq$@s z@$\leq$@n) | zero = @$\bot$@-elim (1<0 a)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 proof5 (s@$\leq$@s (z@$\leq$@n {n'}) ) | suc n = let open @$\equiv$@-Reasoning in
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 begin
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 n' + (vari env + 1)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ z @$\rightarrow$@ n' + z ) ( +-sym {vari env} {1} ) @$\rangle$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 n' + (1 + vari env )
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 @$\equiv$@@$\langle$@ sym ( +-assoc (n') 1 (vari env) ) @$\rangle$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 (n' + 1) + vari env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 @$\equiv$@@$\langle$@ cong ( @$\lambda$@ z @$\rightarrow$@ z + vari env ) +1@$\equiv$@suc @$\rangle$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 (suc n' ) + vari env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 @$\equiv$@@$\langle$@@$\rangle$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 varn env + vari env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 @$\equiv$@@$\langle$@ s @$\rangle$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 c10 env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 @$\blacksquare$@
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
87
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 whileLoopPwP' : {l : Level} {t : Set l} @$\rightarrow$@ (n : @$\mathbb{N}$@) @$\rightarrow$@ (env : Envc ) @$\rightarrow$@ (n @$\equiv$@ varn env) @$\rightarrow$@ whileTestStateP s2 env
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 @$\rightarrow$@ (next : (env : Envc ) @$\rightarrow$@ (pred n @$\equiv$@ varn env) @$\rightarrow$@ whileTestStateP s2 env @$\rightarrow$@ t)
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 @$\rightarrow$@ (exit : (env : Envc ) @$\rightarrow$@ whileTestStateP sf env @$\rightarrow$@ t) @$\rightarrow$@ t
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 whileLoopPwP' zero env refl refl next exit = exit env refl
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 whileLoopPwP' (suc n) env refl refl next exit = next (record env {varn = pred (varn env) ; vari = suc (vari env) }) refl (+-suc n (vari env))
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
95
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 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))
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 whileTestPSemSound c output refl = whileTestPSem c
9176dff8f38a ADD while loop description
soto <soto@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98