annotate tex/agda.tex @ 11:a8bc8c6b48bd default tip

fix
author soto@cr.ie.u-ryukyu.ac.jp
date Tue, 15 Sep 2020 07:06:29 +0900
parents 27a6616b6683
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
1 \section{Agda}
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
2
4
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
3 Agda とは定理証明支援器であり、関数型言語である。Agda は依存型という型システ
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
4 ムを持ち、型を第一級オブジェクトとして扱うことが可能である。また、型システムは
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
5 Curry-Howard 同型対応により命題と型付きラムダ計算が一対一で対応するため Agda で
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
6 は記述したプログラムを証明することができる。
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
7
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
8 \subsection{プログラムの読み方}
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
9 本節ではAgdaの基本事項について \coderef{plus} を例として解説する。
7
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
10
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
11 基本事項として、$ \mathbb{N} $ というのは自然数 (Natulal Number) のことである。
7
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
12 また - (ハイフン) が2つ連続して並んでいる部分はコメントアウトであり、
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
13 ここでは関数を実行した際の例を記述している。
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
14 したがって、これは2つの自然数を受け取って足す関数であることが推測できる。
7
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
15
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
16 \lstinputlisting[label=plus, caption=plus] {src/agda/plus.agda}
4
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
17
7
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
18 この関数の定義部分の説明をする。コードの1行目に : (セミコロン)がある。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
19 この : の前が関数名になり、その後ろがその関数の定義となる。
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
20 : 以降の (x y : $ \mathbb{N} $) は関数は x, y の自然数2つを受けとるという意味になる。
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
21 $ \rightarrow $ 以降は関数が返す型を記述している。
7
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
22 まとめると、この関数 plus は、型が自然数である2つの変数が x, y を受け取り、
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
23 自然数を返すという定義になる。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
24
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
25 関数の定義をしたコードの直下で実装を行うのが常である。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
26 関数名を記述した後に引数を記述して受け取り、= (イコール) 以降で
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
27 引数に対応した実装をする。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
28
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
29 今回の場合 plus x zero であれば +0 である為、そのまま x を返す。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
30 実装2行目の方で受け取った y の値を減らし、x の値を増やして再び plus の関数に
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
31 遷移している。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
32 受け取った y を +1 されていたとして y の値を減らしている。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
33
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
34 関数の実装全体をまとめると、x と y の値を足す為に y から x に数値を1つずつ渡す。
acad18934981 add description of rbtree
soto@cr.ie.u-ryukyu.ac.jp
parents: 6
diff changeset
35 y が 0 になった際に計算が終了となっている。
8
soto@cr.ie.u-ryukyu.ac.jp
parents: 7
diff changeset
36 指折りでの足し算を実装していると捉えても良い。
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
37
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
38 \subsection{Data 型}
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
39 Deta 型とは分岐のことである。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
40 そのため、それぞれの動作について実装する必要がある。
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
41 例として既出で Data 型である $ \mathbb{N} $ の実装を \coderef{Nat} に示す。
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
42
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
43 \lstinputlisting[label=Nat, caption=Nat] {src/agda/Nat.agda}
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
44
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
45 実装から、$ \mathbb{N} $ という型は zero と suc の2つのコンストラクタを持っていることが分かる。
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
46 それぞれの仕様を見てみると、zeroは $ \mathbb{N} $ のみであるが、suc は (n : $ \mathbb{N} $ ) $ \rightarrow \ \mathbb{N} $ である。
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
47 つまり、suc 自体の型は $ \mathbb{N} $ であるが、そこから $ \mathbb{N} $ に遷移するということである。
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
48 そのため、suc からは suc か zero に遷移する必要があり、また zero に遷移することで停止する。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
49 したがって、数値は zero に遷移するまでの suc が遷移した数によって決定される。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
50
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
51 Data型にはそれぞれの動作について実装する必要があると述べたが、
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
52 言い換えればパターンマッチをする必要があると言える。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
53 これは puls 関数で suc 同士の場合と、zeroが含まれる場合の両方を実装していることの説明となる。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
54
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
55 \subsection{Record 型}
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
56 Record 型とはオブジェクトあるいは構造体ののようなものである。
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
57 \coderef{And}は AND の関数となる。p1で前方部分が取得でき、p2で後方部分が取得できる。
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
58
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
59 \lstinputlisting[label=And, caption=And] {src/agda/And.agda}
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
60
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
61 また、Agda の関数定義では\_(アンダースコア)で囲むことで三項演算子を定義することができる。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
62
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
63 これを使用して三段論法を定義することができる。
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
64 定義は「AならばB」かつ「BならばC」なら「AならばC」となる。
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
65 \coderef{syllogism}を以下に示す。
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
66
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
67 \lstinputlisting[label=syllogism, caption=syllogism] {src/agda/syllogism.agda}
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
68
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
69 コードの解説をすると、引数として x と a が関数に与えられている。
11
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
70 引数 x の中身は ((A $ \rightarrow $ B) ∧ (B $ \rightarrow $ C)) 、引数 a の中身は A である。
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
71 したがって、(\_∧\_.p1 x a) で (A $ \rightarrow $ B) に A を与えて B を取得し、
soto@cr.ie.u-ryukyu.ac.jp
parents: 8
diff changeset
72 \_∧\_.p2 x で (B $ \rightarrow $ C) であるため、これに B を与えると C が取得できる。
6
4bf00f7ba825 add description of agda
soto@cr.ie.u-ryukyu.ac.jp
parents: 4
diff changeset
73 よって A を与えて C を取得することができたため、三段論法を定義できた。
4
35f0e5f12fe6 add verification method
soto@cr.ie.u-ryukyu.ac.jp
parents: 3
diff changeset
74