Mercurial > hg > Members > ryokka > HoareLogic
changeset 98:2d2b0b06945b default tip
simplfied version
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 08 Apr 2023 17:00:53 +0900 |
parents | 1b2d58c5d75b |
children | |
files | whileTestGears1.agda |
diffstat | 1 files changed, 106 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/whileTestGears1.agda Sat Apr 08 17:00:53 2023 +0900 @@ -0,0 +1,106 @@ +module whileTestGears1 where + +open import Function +open import Data.Nat renaming ( _∸_ to _-_) +open import Data.Bool hiding ( _≟_ ; _≤?_ ; _≤_ ; _<_ ) +open import Level renaming ( suc to succ ; zero to Zero ) +open import Relation.Nullary using (¬_; Dec; yes; no) +open import Relation.Binary.PropositionalEquality +open import utilities +open import Data.Empty +open import Data.Nat.Properties +open import Data.Unit hiding ( _≟_ ; _≤?_ ; _≤_) + +lemma1 : {i : ℕ} → ¬ 1 ≤ i → i ≡ 0 +lemma1 {zero} not = refl +lemma1 {suc i} not = ⊥-elim ( not (s≤s z≤n) ) + +open import Relation.Binary.Definitions + +nat-≤> : { x y : ℕ } → x ≤ y → y < x → ⊥ +nat-≤> (s≤s x<y) (s≤s y<x) = nat-≤> x<y y<x +lemma3 : {i j : ℕ} → 0 ≡ i → j < i → ⊥ +lemma3 refl () +lemma5 : {i j : ℕ} → i < 1 → j < i → ⊥ +lemma5 (s≤s z≤n) () + +open _/\_ + +record Env ( c : ℕ ) : Set where + field + varn : ℕ + vari : ℕ + n+i=c : varn + vari ≡ c +open Env + +TerminatingLoopS : {l : Level} {t : Set l} (Index : Set ) → ( reduce : Index → ℕ) + → (loop : (r : Index) → (next : (r1 : Index) → reduce r1 < reduce r → t ) → t) + → (r : Index) → t +TerminatingLoopS {_} {t} Index reduce loop r with <-cmp 0 (reduce r) +... | tri≈ ¬a b ¬c = loop r (λ r1 lt → ⊥-elim (lemma3 b lt) ) +... | tri< a ¬b ¬c = loop r (λ r1 lt1 → TerminatingLoop1 (reduce r) r r1 (≤-step lt1) lt1 ) where + TerminatingLoop1 : (j : ℕ) → (r r1 : Index) → reduce r1 < suc j → reduce r1 < reduce r → t + TerminatingLoop1 zero r r1 n≤j lt = loop r1 (λ r2 lt1 → ⊥-elim (lemma5 n≤j lt1)) + TerminatingLoop1 (suc j) r r1 n≤j lt with <-cmp (reduce r1) (suc j) + ... | tri< a ¬b ¬c = TerminatingLoop1 j r r1 a lt + ... | tri≈ ¬a b ¬c = loop r1 (λ r2 lt1 → TerminatingLoop1 j r1 r2 (subst (λ k → reduce r2 < k ) b lt1 ) lt1 ) + ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> c n≤j ) + +whileTestSpec1 : (c10 : ℕ) → (e1 : Env c10 ) → vari e1 ≡ c10 → ⊤ +whileTestSpec1 _ _ x = tt + +whileLoopSeg : {l : Level} {t : Set l} → (c10 : ℕ ) → (env : Env c10 ) + → (next : (e1 : Env c10 ) → varn e1 < varn env → t) + → (exit : (e1 : Env c10 ) → vari e1 ≡ c10 → t) → t +whileLoopSeg c10 env next exit with ( suc zero ≤? (varn env) ) +whileLoopSeg c10 env next exit | no p = exit env ( begin + vari env ≡⟨ refl ⟩ + 0 + vari env ≡⟨ cong (λ k → k + vari env) (sym (lemma1 p )) ⟩ + varn env + vari env ≡⟨ n+i=c env ⟩ + c10 ∎ ) where open ≡-Reasoning +whileLoopSeg c10 env next exit | yes p = next env1 (proof4 (varn env) p) where + env1 = record {varn = (varn env) - 1 ; vari = (vari env) + 1 ; n+i=c = proof3 p } where + 1<0 : 1 ≤ zero → ⊥ + 1<0 () + proof3 : (suc zero ≤ (varn env)) → ((varn env) - 1) + (vari env + 1) ≡ c10 + proof3 (s≤s lt) with varn env + proof3 (s≤s z≤n) | zero = ⊥-elim (1<0 p) + proof3 (s≤s (z≤n {n'}) ) | suc n = let open ≡-Reasoning in begin + n' + (vari env + 1) ≡⟨ cong ( λ z → n' + z ) ( +-sym {vari env} {1} ) ⟩ + n' + (1 + vari env ) ≡⟨ sym ( +-assoc (n') 1 (vari env) ) ⟩ + (n' + 1) + vari env ≡⟨ cong ( λ z → z + vari env ) +1≡suc ⟩ + (suc n' ) + vari env ≡⟨⟩ + varn env + vari env ≡⟨ n+i=c env ⟩ + c10 + ∎ + proof4 : (i : ℕ) → 1 ≤ i → i - 1 < i + proof4 zero () + proof4 (suc i) lt = begin + suc (suc i - 1 ) ≤⟨ ≤-refl ⟩ + suc i ∎ where open ≤-Reasoning + +proofGearsTermS : (c10 : ℕ ) → ⊤ +proofGearsTermS c10 = + TerminatingLoopS (Env c10) (λ env → varn env) (λ n2 loop → whileLoopSeg c10 n2 loop (λ ne pe → whileTestSpec1 c10 ne pe ) ) + record { varn = 0 ; vari = c10 ; n+i=c = refl } + +proofGearsExec : (c10 : ℕ ) → ℕ +proofGearsExec c10 = + TerminatingLoopS (Env c10) (λ env → varn env) (λ n2 loop → whileLoopSeg c10 n2 loop (λ ne pe → vari ne ) ) + record { varn = 0 ; vari = c10 ; n+i=c = refl } + +test = proofGearsExec 20 + + + + + + + + + + + + + +