GearsOS の Hoare Logic を用いた検証 |
Masataka Hokama
琉球大学 : 並列信頼研究室
|
1 program : ℕ → Comm 2 program c10 = 3 Seq ( PComm (λ env → record env {varn = c10})) -- n = 10; 4 $ Seq ( PComm (λ env → record env {vari = 0})) -- i = 0; 5 $ While (λ env → lt zero (varn env ) ) -- while (n>0) { 6 (Seq (PComm (λ env → record env {vari = ((vari env) + 1)} )) -- i++; 7 $ PComm (λ env → record env {varn = ((varn env) - 1)} )) -- n--;
1-- HTProof の続き 2 SeqRule : {bPre : Cond} → {cm1 : Comm} → {bMid : Cond} → 3 {cm2 : Comm} → {bPost : Cond} → 4 HTProof bPre cm1 bMid → 5 HTProof bMid cm2 bPost → 6 HTProof bPre (Seq cm1 cm2) bPost 7 IfRule : {cmThen : Comm} → {cmElse : Comm} → 8 {bPre : Cond} → {bPost : Cond} → 9 {b : Cond} → 10 HTProof (bPre /\ b) cmThen bPost → 11 HTProof (bPre /\ neg b) cmElse bPost → 12 HTProof bPre (If b cmThen cmElse) bPost
1-- HTProof の続き 2 WeakeningRule : {bPre : Cond} → {bPre' : Cond} → {cm : Comm} → 3 {bPost' : Cond} → {bPost : Cond} → 4 Tautology bPre bPre' → 5 HTProof bPre' cm bPost' → 6 Tautology bPost' bPost → 7 HTProof bPre cm bPost 8 WhileRule : {cm : Comm} → {bInv : Cond} → {b : Cond} → 9 HTProof (bInv /\ b) cm bInv → 10 HTProof bInv (While b cm) (bInv /\ neg b)
1 proof1 : (c10 : ℕ) → HTProof initCond (program c10 ) (termCond {c10}) 2 proof1 c10 = 3 SeqRule {λ e → true} ( PrimRule (init-case {c10} )) 4 $ SeqRule {λ e → Equal (varn e) c10} ( PrimRule lemma1 ) 5 $ WeakeningRule {λ e → (Equal (varn e) c10) ∧ (Equal (vari e) 0)} lemma2 ( 6 WhileRule {_} {λ e → Equal ((varn e) + (vari e)) c10} 7 $ SeqRule (PrimRule {λ e → whileInv e ∧ lt zero (varn e) } lemma3 ) 8 $ PrimRule {whileInv'} {_} {whileInv} lemma4 ) lemma5 9 10 initCond : Cond 11 initCond env = true 12 13 termCond : {c10 : Nat} → Cond 14 termCond {c10} env = Equal (vari env) c10
1 lemma1 : {c10 : Nat} → Axiom (stmt1Cond {c10}) 2 (λ env → record { varn = varn env ; vari = 0 }) (stmt2Cond {c10}) 3 lemma1 {c10} env = impl⇒ ( λ cond → let open ≡-Reasoning in 4 begin 5 (Equal (varn env) c10 ) ∧ true 6 ≡⟨ ∧true ⟩ 7 Equal (varn env) c10 8 ≡⟨ cond ⟩ 9 true 10 ∎ ) 11 12 stmt1Cond : {c10 : ℕ} → Cond 13 stmt1Cond {c10} env = Equal (varn env) c10 14 15 stmt2Cond : {c10 : ℕ} → Cond 16 stmt2Cond {c10} env = (Equal (varn env) c10) ∧ (Equal (vari env) 0)
1 whileTest' : {l : Level} {t : Set l} → {c10 : Nat } → 2 (Code : (env : Env) → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) → t) → t 3 whileTest' {_} {_} {c10} next = next env proof2 4 where 5 env : Env 6 env = record {vari = 0 ; varn = c10} 7 proof2 : ((vari env) ≡ 0) /\ ((varn env) ≡ c10) <-- PostCondition 8 proof2 = record {pi1 = refl ; pi2 = refl}
1 conversion1 : {l : Level} {t : Set l } → (env : Env) → {c10 : Nat } → 2 ((vari env) ≡ 0) /\ ((varn env) ≡ c10) 3 → (Code : (env1 : Env) → (varn env1 + vari env1 ≡ c10) → t) → t 4 conversion1 env {c10} p1 next = next env proof4 5 where 6 proof4 : varn env + vari env ≡ c10 7 proof4 = let open ≡-Reasoning in 8 begin 9 varn env + vari env 10 ≡⟨ cong ( λ n → n + vari env ) (pi2 p1 ) ⟩ 11 c10 + vari env 12 ≡⟨ cong ( λ n → c10 + n ) (pi1 p1 ) ⟩ 13 c10 + 0 14 ≡⟨ +-sym {c10} {0} ⟩ 15 c10 16 ∎