GearsOS の Hoare Logic を用いた検証 |
外間政尊 , 河野真治
- 琉球大学 : 並列信頼研究室
|
data Nat : Set where zero : Nat suc : Nat → Nat
record Env : Set where field varn : Nat vari : Nat
whileTest : {t : Set} → (c10 : Nat) → (Code : Env → t) → t whileTest c10 next = next (record {varn = c10 ; vari = 0} )
whileLoop : {t : Set} → Env → (Code : Env → t) → t whileLoop env next with lt 0 (varn env) whileLoop env next | false = next env whileLoop env next | true = whileLoop (record {varn = (varn env) - 1 ; vari = (vari env) + 1}) next
lt : Nat → Nat → Bool lt x y with (suc x ) ≤? y lt x y | yes p = true lt x y | no ¬p = false
+zero : { y : Nat } → y + zero ≡ y +zero {zero} = refl +zero {suc y} = cong ( λ x → suc x ) ( +zero {y} )
+-sym : { x y : Nat } → x + y ≡ y + x +-sym {zero} {zero} = refl +-sym {zero} {suc y} = let open ≡-Reasoning in begin zero + suc y ≡⟨ { }0 ⟩ { }1 ∎ ---------------------- ?0 : zero + suc y ≡ suc y + zero ?1 : Nat
+-sym {zero} {suc y} = let open ≡-Reasoning in begin zero + suc y ≡⟨⟩ suc y ≡⟨ { }0 ⟩ { }1 ∎ ---------------------- ?0 : suc y ≡ suc y + zero ?1 : Nat
-- +zero : { y : Nat } → y + zero ≡ y +-sym {zero} {suc y} = let open ≡-Reasoning in begin zero + suc y ≡⟨⟩ suc y ≡⟨ sym +zero ⟩ suc y + zero ∎ sym : Symmetric {A = A} _≡_ sym refl = refl
n = 10; i = 0;
while (n>0) { i++; n--; }
test : Env test = whileTest 10 (λ env → whileLoop env (λ env1 → env1))
whileTest : {t : Set} → (c10 : Nat) → (Code : Env → t) → t
whileLoop : {t : Set} → Env → (Code : Env → t) → t
-- test = whileTest 10 (λ env → whileLoop env (λ env1 → env1)) proofGears : {c10 : Nat } → Set proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 ))))
-- whileTest : {t : Set} → (c10 : Nat) → (Code : Env → t) → t whileTest' : {t : Set} → {c10 : Nat } → (Code : (env : Env) → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) → t) → t whileTest' {_} {_} {c10} next = next env proof2 where env : Env env = record {vari = 0 ; varn = c10} proof2 : ((vari env) ≡ 0) /\ ((varn env) ≡ c10) proof2 = record {pi1 = refl ; pi2 = refl}
conversion1 : {t : Set} → (env : Env) → {c10 : Nat } → ((vari env) ≡ 0) /\ ((varn env) ≡ c10) → (Code : (env1 : Env) → (varn env1 + vari env1 ≡ c10) → t) → t conversion1 env {c10} p1 next = next env proof4 where proof4 : varn env + vari env ≡ c10
-- precond : ((vari env) ≡ 0) /\ ((varn env) ≡ c10) conversion1 env {c10} precond next = next env proof4 where proof4 : varn env + vari env ≡ c10 proof4 = let open ≡-Reasoning in begin varn env + vari env ≡⟨ cong ( λ n → n + vari env ) (pi2 precond ) ⟩ c10 + vari env ≡⟨ cong ( λ n → c10 + n ) (pi1 precond ) ⟩ c10 + 0 ≡⟨ +-sym {c10} {0} ⟩ c10 ∎ -- +-sym : { x y : Nat } → x + y ≡ y + x
-- whileLoop : {t : Set} → Env → (Code : Env → t) → t whileLoop' : {t : Set} → (env : Env) → {c10 : Nat } → ((varn env) + (vari env) ≡ c10) → (Code : Env → t) → t
proofGears : {c10 : Nat } → Set proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 ))))
-- HoareLogic でのwhileProgram program : ℕ → Comm program c10 = Seq ( PComm (λ env → record env {varn = c10})) $ Seq ( PComm (λ env → record env {vari = 0})) $ While (λ env → lt zero (varn env ) ) (Seq (PComm (λ env → record env {vari = ((vari env) + 1)} )) $ PComm (λ env → record env {varn = ((varn env) - 1)} )) -- コマンドの証明部分 proof1 : (c10 : ℕ) → HTProof initCond (program c10 ) (termCond {c10}) proof1 c10 = SeqRule {λ e → true} ( PrimRule (init-case {c10} )) $ SeqRule {λ e → Equal (varn e) c10} ( PrimRule lemma1 ) $ WeakeningRule {λ e → (Equal (varn e) c10) ∧ (Equal (vari e) 0)} lemma2 ( WhileRule {_} {λ e → Equal ((varn e) + (vari e)) c10} $ SeqRule (PrimRule {λ e → whileInv e ∧ lt zero (varn e) } lemma3 ) $ PrimRule {whileInv'} {_} {whileInv} lemma4 ) lemma5
proofGears : {c10 : Nat } → Set proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 ))))