annotate slide/slide.md @ 11:17b7605a5deb

add figures, some slides
author ryokka
date Sun, 13 Jan 2019 23:42:16 +0900
parents a87fec07fd78
children e8fe28afe61e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
1 title: GearsOS の Hoare triple を用いた検証
a87fec07fd78 add slide
ryokka
parents:
diff changeset
2 author: Masataka Hokama
a87fec07fd78 add slide
ryokka
parents:
diff changeset
3 profile: 琉球大学 : 並列信頼研究室
a87fec07fd78 add slide
ryokka
parents:
diff changeset
4 lang: Japanese
a87fec07fd78 add slide
ryokka
parents:
diff changeset
5
a87fec07fd78 add slide
ryokka
parents:
diff changeset
6 <!-- 発表20分、質疑応答5分 -->
a87fec07fd78 add slide
ryokka
parents:
diff changeset
7
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
8 ## 研究背景
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
9 - OS やアプリケーションなどの信頼性は重要な課題
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
10 - 信頼性を上げるために仕様を検証する必要
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
11 - 仕様検証の手法として Floyd-Hoare Logic (以下 HoareLogic) がある
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
12 - 事前条件(PreCondition)が成り立つとき、関数(Command)を実行、それが停止したとき、事後条件(PostCondition)を満たす
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
13 - 既存の言語ではあまり利用されていない(python の pyrefine ってコードチェッカーくらい...?)
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
14
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
15 ## 背景
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
16 - 当研究室では 処理の単位を **CodeGear**、データの単位を **DataGear** としてプログラムを記述する手法を提案
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
17 - CodeGear は Input DataGear を受け取り、処理を行って Output DataGear に書き込む
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
18 - Gear 間の接続処理はメタ計算として定義
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
19 - メタ計算部分に検証を埋め込むことで通常処理に手を加えずに検証
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
20 - 本研究では Gears OS の信頼性を高めるため、 Gears の単位を用いた HoareLogic ベースの検証手法を提案する
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
21
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
22 <p style="text-align:center;"><img src="./pic/cgdg-small.svg" alt="" width="75%" height="75%"/></p>
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
23
a87fec07fd78 add slide
ryokka
parents:
diff changeset
24 ## Gears について
a87fec07fd78 add slide
ryokka
parents:
diff changeset
25 - **Gears** は当研究室で提案しているプログラム記述手法
a87fec07fd78 add slide
ryokka
parents:
diff changeset
26 - 計算の単位を **CodeGear** 、データの単位を **DataGear**
a87fec07fd78 add slide
ryokka
parents:
diff changeset
27 - CodeGear は引数として Input の DataGear を受け取り、 Output の DataGear を返す
a87fec07fd78 add slide
ryokka
parents:
diff changeset
28 - Output の DataGear は次の CodeGear の Input として接続される
a87fec07fd78 add slide
ryokka
parents:
diff changeset
29 <!-- [fig1](file://./fig/cgdg.pdf) -->
a87fec07fd78 add slide
ryokka
parents:
diff changeset
30 - CodeGear の接続処理は通常の計算とは異なるメタ計算として定義
a87fec07fd78 add slide
ryokka
parents:
diff changeset
31 - メタ計算で信頼性の検証を行う
a87fec07fd78 add slide
ryokka
parents:
diff changeset
32
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
33 <!-- ![cgdg](./pic/codeGear_dataGear.pdf){} -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
34 <p style="text-align:center;"><img src="./pic/cgdg.svg" alt="" width="30%" height="30%"/></p>
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
35
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
36 ## CbC について
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
37 - Gears の単位でプログラミングできる言語として当研究室で開発している **CbC** (Continuation based C) が存在
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
38 - これは C からループ制御構造と関数呼び出しを取り除き継続を導入したもの
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
39 - 環境を継続に含めない
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
40 - 現在の CbC ではメタ計算での検証
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
41 - 将来的には証明も扱えるようにしたいが現段階では未実装
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
42 - そのため Gears の単位を定理証明支援系の言語である **Agda** で記述し、 Agda で証明している
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
43
a87fec07fd78 add slide
ryokka
parents:
diff changeset
44 ## Agda
a87fec07fd78 add slide
ryokka
parents:
diff changeset
45 - Agda は定理証明支援系の言語
a87fec07fd78 add slide
ryokka
parents:
diff changeset
46 - 依存型を持つ関数型言語
a87fec07fd78 add slide
ryokka
parents:
diff changeset
47 - 型を明記する必要がある
a87fec07fd78 add slide
ryokka
parents:
diff changeset
48 - Agda の文法については次のスライドから軽く説明する
a87fec07fd78 add slide
ryokka
parents:
diff changeset
49
a87fec07fd78 add slide
ryokka
parents:
diff changeset
50 ## Agda のデータ型
a87fec07fd78 add slide
ryokka
parents:
diff changeset
51 - データ型は代数的なデータ構造
a87fec07fd78 add slide
ryokka
parents:
diff changeset
52 - **data** キーワードの後に、**名前 : 型**、 where 句
a87fec07fd78 add slide
ryokka
parents:
diff changeset
53 - 次の行以降は**コンストラクタ名 : 型**
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
54 - 型は**->**または**→**で繋げる
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
55 - 例えば、型**PrimComm −> Comm**は**PrimComm** を受け取り**Comm**を返す型
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
56 - 再帰的な定義も可能
a87fec07fd78 add slide
ryokka
parents:
diff changeset
57 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
58 data Comm : Set where
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
59 Skip : Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
60 Abort : Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
61 PComm : PrimComm −> Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
62 Seq : Comm −> Comm −> Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
63 If : Cond −> Comm −> Comm −> Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
64 While : Cond −> Comm −> Comm
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
65 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
66
a87fec07fd78 add slide
ryokka
parents:
diff changeset
67 <!-- - where は宣言した部分に束縛する -->
a87fec07fd78 add slide
ryokka
parents:
diff changeset
68
a87fec07fd78 add slide
ryokka
parents:
diff changeset
69 ## Agda のレコード型
a87fec07fd78 add slide
ryokka
parents:
diff changeset
70 - C 言語での構造体に近い
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
71 - 複数のデータをまとめる
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
72 - 関数内で構築できる
a87fec07fd78 add slide
ryokka
parents:
diff changeset
73 - 構築時は**レコード名 {フィールド名 = 値}**
a87fec07fd78 add slide
ryokka
parents:
diff changeset
74 - 複数ある場合は **{フィールド1 = 1 ; フィールド2 = 2}**のように **;** を使って列挙
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
75 - (varn,vari の型 **ℕ** は Agda 上の 自然数、 データ型で zero : ℕ と succ : ℕ -> ℕ で定義されてる)
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
76 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
77 record Env : Set where
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
78 field
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
79 varn : ℕ
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
80 vari : ℕ
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
81 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
82
a87fec07fd78 add slide
ryokka
parents:
diff changeset
83 ## Agda の関数
a87fec07fd78 add slide
ryokka
parents:
diff changeset
84 - 関数にも型が必要
a87fec07fd78 add slide
ryokka
parents:
diff changeset
85 - 関数は **関数名 = 値**
a87fec07fd78 add slide
ryokka
parents:
diff changeset
86 - 関数ではパターンマッチがかける
a87fec07fd78 add slide
ryokka
parents:
diff changeset
87 - **_** は任意の引数
a87fec07fd78 add slide
ryokka
parents:
diff changeset
88 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
89 _-_ : ℕ → ℕ → ℕ
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
90 x - zero = x
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
91 zero - _ = zero
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
92 (suc x) - (suc y) = x - y
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
93 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
94
a87fec07fd78 add slide
ryokka
parents:
diff changeset
95 ## Agda での証明
a87fec07fd78 add slide
ryokka
parents:
diff changeset
96 - 関数の型に論理式
a87fec07fd78 add slide
ryokka
parents:
diff changeset
97 - 関数自体にそれを満たす導出
a87fec07fd78 add slide
ryokka
parents:
diff changeset
98 - 完成した関数は証明
a87fec07fd78 add slide
ryokka
parents:
diff changeset
99 - **{}** は暗黙的(推論される)
a87fec07fd78 add slide
ryokka
parents:
diff changeset
100 - 下のコードは自然数に 0 を足したとき値が変わらないことの証明
a87fec07fd78 add slide
ryokka
parents:
diff changeset
101 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
102 +zero : { y : ℕ } → y + zero ≡ y
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
103 +zero {zero} = refl
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
104 +zero {suc y} = cong ( λ x → suc x ) ( +zero {y} )
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
105 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
106
a87fec07fd78 add slide
ryokka
parents:
diff changeset
107 ## Agda 上での HoareLogic
a87fec07fd78 add slide
ryokka
parents:
diff changeset
108 * 現在 Agda での HoareLogic は初期のAgda の実装である Agda1(現在のものはAgda2)で実装されたものと
a87fec07fd78 add slide
ryokka
parents:
diff changeset
109 それを Agda2 に書き写したものが存在している。
a87fec07fd78 add slide
ryokka
parents:
diff changeset
110 * 今回はAgda2側の HoareLogic で使うコマンド定義の一部と、コマンドの証明に使うルールを借りて説明を行う。
a87fec07fd78 add slide
ryokka
parents:
diff changeset
111
a87fec07fd78 add slide
ryokka
parents:
diff changeset
112
a87fec07fd78 add slide
ryokka
parents:
diff changeset
113 ## Agda での HoareLogic の理解
a87fec07fd78 add slide
ryokka
parents:
diff changeset
114 * HoareLogic を用いて次のようなプログラム(while Program)を検証した。
a87fec07fd78 add slide
ryokka
parents:
diff changeset
115 ```C
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
116 n = 10;
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
117 i = 0;
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
118
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
119 while (n>0)
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
120 {
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
121 i++;
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
122 n--;
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
123 }
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
124 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
125 * このプログラムは変数iとnをもち、 n>0 の間nの値を減らし、i の値を増やす
a87fec07fd78 add slide
ryokka
parents:
diff changeset
126 * n==0のとき停止するため、終了時の変数の結果はi==10、n==0 になるはずである。
a87fec07fd78 add slide
ryokka
parents:
diff changeset
127
a87fec07fd78 add slide
ryokka
parents:
diff changeset
128 ## HoareLogic
a87fec07fd78 add slide
ryokka
parents:
diff changeset
129 * Floyd-Hoare Logic (以下HoareLogic)は部分的な正当性を検証する
a87fec07fd78 add slide
ryokka
parents:
diff changeset
130 * プログラムは事前条件(Pre Condition)、事後条件(Post Condition)を持ち、条件がコマンドで更新され、事後条件になる
a87fec07fd78 add slide
ryokka
parents:
diff changeset
131 * 事前、事後条件には変数や論理式、コマンドには代入や、繰り返し、条件分岐などがある。
a87fec07fd78 add slide
ryokka
parents:
diff changeset
132 * コマンドが正しく成り立つことを保証することで、このコマンドを用いて記述されたプログラムの部分的な正しさを検証する
a87fec07fd78 add slide
ryokka
parents:
diff changeset
133
a87fec07fd78 add slide
ryokka
parents:
diff changeset
134
a87fec07fd78 add slide
ryokka
parents:
diff changeset
135 ## Agda 上での HoareLogic(コマンド定義)
a87fec07fd78 add slide
ryokka
parents:
diff changeset
136 * Env は while Program の変数である var n, i
a87fec07fd78 add slide
ryokka
parents:
diff changeset
137 * **PrimComm** は代入時に使用される
a87fec07fd78 add slide
ryokka
parents:
diff changeset
138 * **Cond** は Condition で Env を受け取って Boolean の値を返す
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
139 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
140 record Env : Set where
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
141 field
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
142 varn : ℕ
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
143 vari : ℕ
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
144
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
145 PrimComm : Set
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
146 PrimComm = Env → Env
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
147
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
148 Cond : Set
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
149 Cond = (Env → Bool)
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
150 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
151
a87fec07fd78 add slide
ryokka
parents:
diff changeset
152 ## Agda 上での HoareLogic(コマンド定義)
a87fec07fd78 add slide
ryokka
parents:
diff changeset
153 * **Comm** は Agda のデータ型で定義した HoareLogic の Command
a87fec07fd78 add slide
ryokka
parents:
diff changeset
154 * **Skip** は何も変更しない
a87fec07fd78 add slide
ryokka
parents:
diff changeset
155 * **PComm** は変数を代入する
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
156 * **Seq** は Command を実行して次の Command に移す
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
157 * **If** は Cond と2つの Comm を受け取り Cond の状態により実行する Comm を変える
a87fec07fd78 add slide
ryokka
parents:
diff changeset
158 * **while** は Cond と Comm を受け取り Cond の中身が真である間 Comm を繰り返す
a87fec07fd78 add slide
ryokka
parents:
diff changeset
159 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
160 data Comm : Set where
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
161 Skip : Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
162 Abort : Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
163 PComm : PrimComm -> Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
164 Seq : Comm -> Comm -> Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
165 If : Cond -> Comm -> Comm -> Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
166 While : Cond -> Comm -> Comm
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
167 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
168
a87fec07fd78 add slide
ryokka
parents:
diff changeset
169 ## Agda 上での HoareLogic(実際のプログラムの記述)
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
170 * Command を使って while Program を記述した。
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
171 * **$** は **()** の糖衣で行頭から行末までを ( ) で囲う
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
172 * 見やすさのため改行しているため 3~7 行はまとまっている
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
173 * Seq は Comm を2つ取って次の Comm に移行する
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
174 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
175 program : Comm
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
176 program =
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
177 Seq ( PComm (λ env → record env {varn = 10}))
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
178 $ Seq ( PComm (λ env → record env {vari = 0}))
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
179 $ While (λ env → lt zero (varn env ) )
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
180 (Seq (PComm (λ env → record env {vari = ((vari env) + 1)} ))
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
181 $ PComm (λ env → record env {varn = ((varn env) - 1)} ))
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
182 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
183
a87fec07fd78 add slide
ryokka
parents:
diff changeset
184
a87fec07fd78 add slide
ryokka
parents:
diff changeset
185 ## Agda 上での HoareLogicの理解
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
186 * 規則は HTProof にまとめられてる
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
187 * **PrimRule** は **PComm** で行う代入を保証する
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
188 * 3行目の pr の型 Axiom は PreCondition に PrimComm が適用されると PostCondition になることの記述
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
189 * **_⇒_** は pre, post の Condition をとって post の Condition が成り立つときに True を返す関数
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
190 * SkipRule は PreCondition を変更しないことの保証
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
191 * AbortRule は プログラムが停止するときのルール
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
192 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
193 data HTProof : Cond -> Comm -> Cond -> Set where
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
194 PrimRule : {bPre : Cond} -> {pcm : PrimComm} -> {bPost : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
195 (pr : Axiom bPre pcm bPost) ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
196 HTProof bPre (PComm pcm) bPost
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
197 SkipRule : (b : Cond) -> HTProof b Skip b
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
198 AbortRule : (bPre : Cond) -> (bPost : Cond) ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
199 HTProof bPre Abort bPost
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
200 -- 次のスライドに続く
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
201 ```
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
202 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
203 Axiom : Cond -> PrimComm -> Cond -> Set
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
204 Axiom pre comm post = ∀ (env : Env) → (pre env) ⇒ ( post (comm env)) ≡ true
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
205 ```
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
206
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
207 ## Agda 上での HoareLogicの理解
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
208 * **SeqRule** は Command を推移させる Seq の保証
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
209 * **IfRule** は If の Command が正しく動くことを保証
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
210 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
211 -- HTProof の続き
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
212 SeqRule : {bPre : Cond} -> {cm1 : Comm} -> {bMid : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
213 {cm2 : Comm} -> {bPost : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
214 HTProof bPre cm1 bMid ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
215 HTProof bMid cm2 bPost ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
216 HTProof bPre (Seq cm1 cm2) bPost
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
217 IfRule : {cmThen : Comm} -> {cmElse : Comm} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
218 {bPre : Cond} -> {bPost : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
219 {b : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
220 HTProof (bPre /\ b) cmThen bPost ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
221 HTProof (bPre /\ neg b) cmElse bPost ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
222 HTProof bPre (If b cmThen cmElse) bPost
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
223 ```
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
224
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
225 ## Agda 上での HoareLogicの理解
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
226 * **WeakeningRule** は通常の Condition からループ不変条件(Loop Invaliant)に変換
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
227 * Tautology は Condition と不変条件が等しく成り立つ
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
228 * **WhileRule** はループ不変条件が成り立つ間 Comm を繰り返す
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
229 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
230 -- HTProof の続き
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
231 WeakeningRule : {bPre : Cond} -> {bPre' : Cond} -> {cm : Comm} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
232 {bPost' : Cond} -> {bPost : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
233 Tautology bPre bPre' ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
234 HTProof bPre' cm bPost' ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
235 Tautology bPost' bPost ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
236 HTProof bPre cm bPost
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
237 WhileRule : {cm : Comm} -> {bInv : Cond} -> {b : Cond} ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
238 HTProof (bInv /\ b) cm bInv ->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
239 HTProof bInv (While b cm) (bInv /\ neg b)
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
240 ```
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
241 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
242 Tautology : Cond -> Cond -> Set
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
243 Tautology pre post = ∀ (env : Env) → (pre env) ⇒ (post env) ≡ true
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
244 ```
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
245
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
246 ## Agda 上での HoareLogic(証明)
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
247 * **proof1** は while Program の証明
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
248 * HTProof に 初期状態とコマンドで書かれた **program** と終了状態を渡す
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
249 * lemma1~5は rule それぞれの証明
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
250 *
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
251 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
252 proof1 : HTProof initCond program termCond
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
253 proof1 =
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
254 SeqRule {λ e → true} ( PrimRule empty-case )
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
255 $ SeqRule {λ e → Equal (varn e) 10} ( PrimRule lemma1 )
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
256 $ WeakeningRule {λ e → (Equal (varn e) 10) ∧ (Equal (vari e) 0)} lemma2 (
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
257 WhileRule {_} {λ e → Equal ((varn e) + (vari e)) 10}
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
258 $ SeqRule (PrimRule {λ e → whileInv e ∧ lt zero (varn e) } lemma3 )
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
259 $ PrimRule {whileInv'} {_} {whileInv} lemma4 ) lemma5
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
260
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
261 initCond : Cond
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
262 initCond env = true
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
263
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
264 termCond : {c10 : ℕ} → Cond
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
265 termCond {c10} env = Equal (vari env) c10
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
266 ```
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
267
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
268 <!-- program : Comm -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
269 <!-- program = -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
270 <!-- Seq ( PComm (λ env → record env {varn = 10})) -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
271 <!-- $ Seq ( PComm (λ env → record env {vari = 0})) -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
272 <!-- $ While (λ env → lt zero (varn env ) ) -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
273 <!-- (Seq (PComm (λ env → record env {vari = ((vari env) + 1)} )) -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
274 <!-- $ PComm (λ env → record env {varn = ((varn env) - 1)} )) -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
275
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
276 ## 証明の一部
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
277 - 基本的な証明方法は Condtition を変化させて次の Condition が成り立つように変形する
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
278 - impl⇒
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
279 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
280 lemma1 : {c10 : ℕ} → Axiom (stmt1Cond {c10}) (λ env → record { varn = varn env ; vari = 0 }) (stmt2Cond {c10})
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
281 lemma1 {c10} env = impl⇒ ( λ cond → let open ≡-Reasoning in
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
282 begin
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
283 (Equal (varn env) c10 ) ∧ true
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
284 ≡⟨ ∧true ⟩
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
285 Equal (varn env) c10
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
286 ≡⟨ cond ⟩
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
287 true
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
288 ∎ )
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
289 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
290
a87fec07fd78 add slide
ryokka
parents:
diff changeset
291
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
292 <!-- lemma2 : {c10 : ℕ} → Tautology stmt2Cond whileInv -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
293
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
294 <!-- lemma3 : Axiom (λ e → whileInv e ∧ lt zero (varn e)) (λ env → record { varn = varn env ; vari = vari env + 1 }) whileInv' -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
295
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
296 <!-- lemma4 : {c10 : ℕ} → Axiom whileInv' (λ env → record { varn = varn env - 1 ; vari = vari env }) whileInv -->
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
297
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
298 <!-- lemma5 : {c10 : ℕ} → Tautology ((λ e → Equal (varn e + vari e) c10) and (neg (λ z → lt zero (varn z)))) termCond -->
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
299
a87fec07fd78 add slide
ryokka
parents:
diff changeset
300 ## Agda での Gears
a87fec07fd78 add slide
ryokka
parents:
diff changeset
301 - Agda での CodeGear は通常の関数とは異なり、継続渡し (CPS : Continuation Passing Style) で記述された関数
a87fec07fd78 add slide
ryokka
parents:
diff changeset
302 - CPS の関数は引数として継続を受け取って継続に計算結果を渡す
a87fec07fd78 add slide
ryokka
parents:
diff changeset
303 - **名前 : 引数 -> (Code : fa -> t) -> t**
a87fec07fd78 add slide
ryokka
parents:
diff changeset
304 - **t** は継続
a87fec07fd78 add slide
ryokka
parents:
diff changeset
305 - **(Code : fa -> t)** は次の継続先
a87fec07fd78 add slide
ryokka
parents:
diff changeset
306 - DataGear は Agda での CodeGear に使われる引数
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
307 ```AGDA
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
308 _g-_ : {t : Set} → ℕ → ℕ → (Code : ℕ → t) → t
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
309 x g- zero next = next x
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
310 zero g- _ = next zero
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
311 (suc x) g- (suc y) = next (x g- y)
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
312 ```
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
313
a87fec07fd78 add slide
ryokka
parents:
diff changeset
314 ## Gears をベースにした HoareLogic
a87fec07fd78 add slide
ryokka
parents:
diff changeset
315 * 次に Gears をベースにした while Program をみる。
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
316 * このプログラムは自然数と継続先を受け取って t を返す
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
317 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
318 {-# TERMINATING #-}
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
319 whileLoop : {l : Level} {t : Set l} -> Env -> (Code : Env -> t) -> t
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
320 whileLoop env next with lt 0 (varn env)
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
321 whileLoop env next | false = next env
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
322 whileLoop env next | true =
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
323 whileLoop (record {varn = (varn env) - 1 ; vari = (vari env) + 1}) next
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
324 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
325
a87fec07fd78 add slide
ryokka
parents:
diff changeset
326 ## Gears と HoareLogic をベースにした証明
a87fec07fd78 add slide
ryokka
parents:
diff changeset
327 * ここでは
a87fec07fd78 add slide
ryokka
parents:
diff changeset
328 ```AGDA
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
329 proofGears : {c10 : ℕ } → Set
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
330 proofGears {c10} = whileTest' {_} {_} {c10} (λ n p1 → conversion1 n p1
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
331 (λ n1 p2 → whileLoop' n1 p2 (λ n2 → ( vari n2 ≡ c10 ))))
10
a87fec07fd78 add slide
ryokka
parents:
diff changeset
332 ```
a87fec07fd78 add slide
ryokka
parents:
diff changeset
333
a87fec07fd78 add slide
ryokka
parents:
diff changeset
334
11
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
335 ## まとめと今後の課題
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
336 - HoareLogic の while を使った例題を作成、証明を行った
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
337 - Gears を用いた HoareLogic ベースの検証方法を導入した
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
338 - 証明が引数として渡される記述のため証明とプログラムを一体化できた
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
339 - 今後の課題
17b7605a5deb add figures, some slides
ryokka
parents: 10
diff changeset
340 - RedBlackTree や SynchronizedQueue などのデータ構造の検証(HoareLogic ベースで)