annotate paper/agda.tex @ 79:4985359bd08b

Update agda description
author atton <atton@cr.ie.u-ryukyu.ac.jp>
date Wed, 08 Feb 2017 15:52:44 +0900
parents 897fda8e39c5
children 73da47f32888
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
7f1b5c33b282 Add agda.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 \chapter{証明支援系言語 Agda による証明手法}
7f1b5c33b282 Add agda.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 \label{chapter:agda}
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
3 \ref{chapter:akasha} 章では CbC のモデル検査的検証アプローチとして、akasha を用いた有限の要素数の挿入時の仕様の検証を行なった。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
4 しかし、要素数13個分の挿入を検証しても赤黒木の挿入が必ずバランスするとは断言しづらい。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
5
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
6 そこで、 CbC の性質をより厳密に定義し、その上で証明を行なうことを考えた。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
7 CbC のプログラムを証明できる形に変換し、任意の回数の挿入に対しても性質が保証できるよう証明するのである。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
8 証明を行なう機構として注目したのが型システムである。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
9
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
10 型システムは Curry-Howard 同型対応により命題と型付きラムダ計算が一対一に対応する。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
11 依存型という型を持つ証明支援系言語 Agda を用いて型システムで証明が行なえることを示す。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
12
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
13 % {{{ 型システムとは TODO: もう少し大ざっぱに説明
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
14 \section{型システムとは}
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
15 型システムとは、計算する値を分類することにでプログラムがある種の振舞いを行なわないことを保証する機構の事である\cite{Pierce:2002:TPL:509043}\cite{pierce2013型システム入門プログラミング言語と型の理論}。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
16 ある種の振舞いとはプログラム中の評価不可能な式や、言語として未定義な式などが当て嵌まる。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
17 例えば、gcc や clang といったコンパイラは関数定義時に指定された引数の型と呼び出し時の値の型が異なる時に警告を出す。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
18 この警告は関数が受けつける範囲以外の値をプログラマが渡してしまった場合などに有効に働く。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
19 加えて、関数を定義する側も受け付ける値の範囲を限定できるため関数内部の処理を記述しやすい。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
20
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
21 型システムで行なえることには以下のようなものが存在する。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
22
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
23 \begin{itemize}
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
24 \item エラーの検出
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
25
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
26 文字列演算を行なう関数に整数を渡してしまったり、複雑な場合分けで境界条件を見落すなど、プログラマの不注意が型の不整合として表れる。
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
27
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
28 \item 抽象化
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
29
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
30 型は大規模プログラムの抽象化の単位にもなる。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
31 例えば特定のデータ構造に対する処理をモジュール化し、パッケージングすることができる。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
32
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
33 \item ドキュメント化
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
34
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
35 関数やモジュールの型を確認することにより、プログラムの理解の助けになる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
36 また、型はコンパイラが実行されるたびに検査されるため、常に最新の正しい情報を提供する。
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
37
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
38 \item 言語の安全性
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
39
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
40 例えばポインタを直接扱わないようメモリアクセスを抽象化し、データを破壊する可能性をプログラマに提供しないようにできる。
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
41
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
42 \item 効率性
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
43
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
44 そもそも、科学計算機における最初の型システムは Fortran~\ref{Backus:1978:HFI:960118.808380} などにおける整数と実数の算術式の区別だった。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
45 型の導入により、ソースからコンパイラがより最適化されたコードを生成できる。
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
46
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
47 \end{itemize}
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
48
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
49 型システムには多くの定義が存在する。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
50 型の表現能力には単純型や総称型、部分型などが存在する。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
51 型付けにも動的型付けや静的型付けが存在し、どの型システムを採用するかは言語の設計に依存する。
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
52 例えば C言語では数値と文字を二項演算子 \verb/+/ で加算できるが、Haskell では加算することができない。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
53 これは Haskell が C言語よりも厳密な型システムを採用しているからである。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
54 具体的には Haskell は暗黙的な型変換を許さず、 C 言語は言語仕様として暗黙の型変換を持っている。
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
55
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
56 型システムを定義することはプログラミング言語がどのような特徴を持つかを決めることにも繋がる。
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
57
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
58 % }}}
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
59
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
60 % {{{ Natural Deduction
49
7f1b5c33b282 Add agda.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 \section{Natural Deduction}
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
62 \label{section:natural_deduction}
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
63 まず始めに証明を行なうためにNatural Deduction(自然演繹)を示す。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
64
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
65 Natural Deduction は Gentzen によって作られた論理と、その証明システムである~\cite{Girard:1989:PT:64805}。
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
66 命題変数と記号を用いた論理式で論理を記述し、推論規則により変形することで求める論理式を導く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
67
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
68 natural deduction において
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
69
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
70 \begin{eqnarray}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
71 \vdots \\ \nonumber
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
72 A \\ \nonumber
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
73 \end{eqnarray}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
74
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
75 と書いた時、最終的に命題Aを証明したことを意味する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
76 証明は木構造で表わされ、葉の命題は仮定となる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
77 仮定には dead か alive の2つの状態が存在する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
78
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
79 \begin{eqnarray}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
80 \label{exp:a_implies_b}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
81 A \\ \nonumber
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
82 \vdots \\ \nonumber
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
83 B \\ \nonumber
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
84 \end{eqnarray}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
85
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
86 式\ref{exp:a_implies_b}のように A を仮定して B を導いたとする。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
87 この時 A は alive な仮定であり、証明された B は A の仮定に依存していることを意味する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
88
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
89 ここで、推論規則により記号 $ \Rightarrow $ を導入する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
90
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
91 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
92 \AxiomC{[$ A $]}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
93 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
94 \UnaryInfC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
95 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
96 \UnaryInfC{ $ B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
97 \RightLabel{ $ \Rightarrow \mathcal{I} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
98 \UnaryInfC{$ A \Rightarrow B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
99 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
100
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
101 $ \Rightarrow \mathcal{I} $ を適用することで仮定 A は dead となり、新たな命題 $ A \Rightarrow B $ を導くことができる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
102 A という仮定に依存して B を導く証明から、「A が存在すれば B が存在する」という証明を導いたこととなる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
103 このように、仮定から始めて最終的に全ての仮定を dead とすることで、仮定に依存しない証明を導ける。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
104 なお、dead な仮定は \verb/[A]/ のように \verb/[]/ で囲んで書く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
105
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
106 alive な仮定を dead にすることができるのは $ \Rightarrow \mathcal{I} $ 規則のみである。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
107 それを踏まえ、 natural deduction には以下のような規則が存在する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
108
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
109 \begin{itemize}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
110 \item Hypothesis
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
111
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
112 仮定。葉にある式が仮定となるため、論理式A を仮定する場合に以下のように書く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
113
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
114 $ A $
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
115
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
116 \item Introductions
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
117
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
118 導入。証明された論理式に対して記号を導入することで新たな証明を導く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
119
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
120
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
121 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
122 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
123 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
124 \UnaryInfC{ $ A $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
125 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
126 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
127 \UnaryInfC{ $ B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
128 \RightLabel{ $ \land \mathcal{I} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
129 \BinaryInfC{$ A \land B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
130 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
131
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
132 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
133 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
134 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
135 \UnaryInfC{ $ A $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
136 \RightLabel{ $ \lor 1 \mathcal{I} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
137 \UnaryInfC{$ A \lor B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
138 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
139
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
140 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
141 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
142 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
143 \UnaryInfC{ $ B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
144 \RightLabel{ $ \lor 2 \mathcal{I} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
145 \UnaryInfC{$ A \lor B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
146 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
147
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
148 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
149 \AxiomC{[$ A $]}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
150 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
151 \UnaryInfC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
152 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
153 \UnaryInfC{ $ B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
154 \RightLabel{ $ \Rightarrow \mathcal{I} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
155 \UnaryInfC{$ A \Rightarrow B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
156 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
157
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
158 \item Eliminations
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
159
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
160 除去。ある論理記号で構成された証明から別の証明を導く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
161
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
162 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
163 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
164 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
165 \UnaryInfC{ $ A \land B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
166 \RightLabel{ $ \land 1 \mathcal{E} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
167 \UnaryInfC{$ A $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
168 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
169
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
170 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
171 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
172 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
173 \UnaryInfC{ $ A \land B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
174 \RightLabel{ $ \land 2 \mathcal{E} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
175 \UnaryInfC{$ B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
176 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
177
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
178 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
179 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
180 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
181 \UnaryInfC{ $ A \lor B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
182
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
183 \AxiomC{[$A$]}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
184 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
185 \UnaryInfC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
186 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
187 \UnaryInfC{ $ C $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
188
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
189 \AxiomC{[$B$]}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
190 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
191 \UnaryInfC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
192 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
193 \UnaryInfC{ $ C $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
194
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
195 \RightLabel{ $ \lor \mathcal{E} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
196 \TrinaryInfC{ $ C $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
197 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
198
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
199 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
200 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
201 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
202 \UnaryInfC{ $ A $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
203
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
204 \AxiomC{ $ \vdots $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
205 \noLine
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
206 \UnaryInfC{ $ A \Rightarrow B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
207
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
208 \RightLabel{ $ \Rightarrow \mathcal{E} $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
209 \BinaryInfC{$ B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
210 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
211
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
212 \end{itemize}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
213
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
214 記号 $ \lor, \land, \Rightarrow $ の導入の除去規則について述べた。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
215 natural deduction には他にも $ \forall, \exists, \bot $ といった記号が存在するが、ここでは解説を省略する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
216
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
217 それぞれの記号は以下のような意味を持つ
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
218 \begin{itemize}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
219 \item $ \land $
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
220 conjunction。2つの命題が成り立つことを示す。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
221 $ A \land B $ と記述すると A かつ B と考えることができる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
222
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
223 \item $ \lor $
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
224 disjunction。2つの命題のうちどちらかが成り立つことを示す。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
225 $ A \lor B $ と記述すると A または B と考えることができる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
226
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
227 \item $ \Rightarrow $
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
228 implication。左側の命題が成り立つ時、右側の命題が成り立つことを示す。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
229 $ A \Rightarrow B $ と記述すると A ならば B と考えることができる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
230 \end{itemize}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
231
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
232 例として、natural deduction で三段論法を証明する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
233 なお、三段論法とは「A は B であり、 B は C である。よって A は C である」といった文を示す。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
234
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
235 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
236 \AxiomC{ $ [A] $ $_{(1)}$}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
237 \AxiomC{ [$ (A \Rightarrow B) \land (B \Rightarrow C)$] $_{(2)}$ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
238 \RightLabel{ $ \land 1 \mathcal{E} $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
239 \UnaryInfC{ $ (A \Rightarrow B) $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
240 \BinaryInfC{ $ B $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
241
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
242 \AxiomC{ [$ (A \Rightarrow B) \land (B \Rightarrow C)$] $_{(2)}$ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
243 \RightLabel{ $ \land 2 \mathcal{E} $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
244 \UnaryInfC{ $ (B \Rightarrow C) $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
245
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
246 \BinaryInfC{ $ C $ }
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
247 \RightLabel{ $ \Rightarrow \mathcal{I} _{(1)}$}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
248 \UnaryInfC{ $ A \Rightarrow C $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
249 \RightLabel{ $ \Rightarrow \mathcal{I} _{(2)}$}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
250 \UnaryInfC{ $ ((A \Rightarrow B) \land (B \Rightarrow C)) \Rightarrow (A \Rightarrow C) $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
251 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
252
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
253 まず、三段論法を論理式で表す。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
254
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
255 「A は B であり、 B は C である。よって A は C である」 が証明するべき命題である。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
256 まず、「A は B であり」から、Aから性質Bが導けることが分かる。これが $ A \Rightarrow B $ となる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
257 次に、「B は C である」から、Bから性質Cが導けることが分かる。これが $ B \Rightarrow C $ となる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
258 そしてこの2つは同時に成り立つ。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
259 よって $ (A \Rightarrow B) \land (B \Rightarrow C)$ が仮定となる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
260 この仮定が成り立つ時に「Aは Cである」を示せば良い。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
261 仮定と同じように「A は C である」は、 $ A \Rightarrow C $ と書けるため、証明するべき論理式は $ ((A \Rightarrow B) \land (B \Rightarrow C)) \Rightarrow (A \Rightarrow C) $ となる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
262
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
263 証明の手順はこうである。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
264 まず条件$ (A \Rightarrow B) \land (B \Rightarrow C)$とAの2つを仮定する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
265 条件を $ \land 1 \mathcal{E} $ $ \land 2 \mathcal{E} $ により分解する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
266 A と $ A \Rightarrow B$ から B を、 B と $ B \Rightarrow C $ から C を導く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
267 ここで $ \Rightarrow \mathcal{I} $ により $ A \Rightarrow C $ を導く。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
268 この際に dead にする仮定は A である。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
269 数回仮定を dead にする際は $_{(1)} $ のように対応する \verb/[]/の記号に数値を付ける。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
270 これで残る alive な仮定は $ (A \Rightarrow B) \land (B \Rightarrow C)$ となり、これから $ A \Rightarrow C $ を導くことができたためにさらに $ \Rightarrow \mathcal{I} $ を適用する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
271 結果、証明すべき論理式$ ((A \Rightarrow B) \land (B \Rightarrow C)) \Rightarrow (A \Rightarrow C) $ が導けたために証明終了となる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
272
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
273 % }}}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
274
78
897fda8e39c5 Reconstruct paper
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 74
diff changeset
275 % {{{ Curry-Howard Isomorphism TODO: もっと増やす(Agda でラムダ計算を説明する)
49
7f1b5c33b282 Add agda.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 \section{Curry-Howard Isomorphism}
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
277 \label{section:curry_howard_isomorphism}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
278 \ref{section:natural_deduction}節では natural deduction における証明手法について述べた。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
279 natural deduction における証明はほとんど型付き $ \lambda $ 計算のような形をしている。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
280 実際、Curry-Howard Isomorphism により Natural Deduction と型付き $ \lambda $ 計算は対応している。% ref TaPL 104
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
281 Curry-Howard Isomorphism の概要を~\ref{section:curry_howard_isomorphism} 節に述べる。
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
282
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
283 関数型 $ \rightarrow $ のみに注目した時
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
284
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
285 \begin{enumerate}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
286 \item 導入規則(T-ABS) は、その型の要素がどのように作られるかを記述する
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
287 \item 除去規則(T-APP) は、その型の要素がどのように作られるかを記述する
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
288 \end{enumerate}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
289
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
290
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
291 例えば命題Aが成り立つためには A という型を持つ値が存在すれば良い。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
292 しかしこの命題は A という alive な仮定に依存している。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
293 natural deduction では A の仮定を dead にするために $ \Rightarrow \mathcal{I} $ により $ \Rightarrow $ を導入する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
294 これが $ \lambda $ による抽象化(T-ABS)に対応している。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
295
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
296 \begin{eqnarray*}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
297 x : A \\
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
298 \lambda x . x : A \rightarrow A
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
299 \end{eqnarray*}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
300
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
301 プログラムにおいて、変数 x は内部の値により型が決定される。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
302 特に、x の値が未定である場合は未定義の変数としてエラーが発生する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
303 しかし、 x を取って x を返す関数は定義することはできる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
304 これは natural deduction の $ \Rightarrow \mathcal{I} $ により仮定を discharge することに相当する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
305
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
306 また、仮定Aが成り立つ時に結論Bを得ることは、関数適用(T-APP)に相当している。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
307
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
308 \begin{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
309 \AxiomC{$A$}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
310 \AxiomC{$A \rightarrow B $}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
311 \RightLabel{T-APP}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
312 \BinaryInfC{$B$}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
313 \end{prooftree}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
314
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
315 このように、 natural deduction における証明はそのまま 型付き $ \lambda $ 計算に変換することができる。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
316
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
317 それぞれの詳細な対応は省略するが、表\ref{tbl:curry_howard_isomorphism} のような対応が存在する。
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
318
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
319 \begin{center}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
320 \begin{table}[htbp]
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
321 \begin{tabular}{|c||c|c|} \hline
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
322 & natural deduction & 型付き $ \lambda $ 計算 \\ \hline \hline
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
323 hypothesis & $ A $ & 型 A を持つ変数 x \\ \hline
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
324 conjunction & $ A \land B $ & 型 A と型 B の直積型 を持つ変数 x \\ \hline
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
325 disjunction & $ A \lor B $ & 型 A と型 B の直和型 を持つ変数 x \\ \hline
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
326 implication & $ A \Rightarrow B $ & 型 A を取り型 B の変数を返す関数 f \\ \hline
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
327 \end{tabular}
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
328 \caption{natural deuction と 型付き $ \lambda $ 計算との対応(Curry-Howard Isomorphism)}
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
329 \label{tbl:curry_howard_isomorphism}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
330 \end{table}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
331 \end{center}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
332 % }}}
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
333
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
334 % {{{ 依存型を持つ証明支援系言語 Agda
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
335
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 49
diff changeset
336 \section{依存型を持つ証明支援系言語 Agda}
74
e9ff08a232f7 Add references
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
337 型システムを用いて証明を行なうことができる言語に Agda~\cite{agda} が存在する。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
338 Agda は依存型という強力な型システムを持っている。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
339 依存型とは型も第一級オブジェクトとする型であり、型を引数に取る関数や値を取って型を返す関数、型の型などが記述できる。
74
e9ff08a232f7 Add references
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
340 この節では Agda の文法的な紹介を行なう~\cite{Norell:2009:DTP:1481861.1481862}~\cite{agda-documentation}。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
341
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
342 まず Agda のプログラムは全てモジュールの内部に記述されるため、まずはトップレベルにモジュールを定義する必要がある。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
343 トップレベルのモジュールはファイル名と同一となる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
344 例えば \verb/AgdaBasics.agda/ のモジュール名定義はリスト~\ref{src:agda-module}のようになる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
345
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
346 \lstinputlisting[label=src:agda-module, caption=Agdaのモジュールの定義する] {src/AgdaBasics.agda}
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
347
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
348 また、Agda はインデントに意味を持つ言語であるため、インデントはきちんと揃える必要がある。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
349
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
350 まず Agda におけるデータ型について触れていく。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
351 Agda における型指定は $:$ を用いて行なう。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
352 例えば、変数xが型Aを持つ、ということを表すには \verb/x : A/ と記述する。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
353
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
354 データ型は Haskell や ML に似て代数的なデータ構造のパターンマッチを扱うことができる
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
355 データ型の定義は \verb/data/ キーワードを用いる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
356 \verb/data/キーワードの後に \verb/where/ 句を書きインデントを深くした後、値にコンストラクタとその型を列挙する。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
357 例えば Bool 型を定義するとリスト~\ref{src:agda-bool}のようになる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
358
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
359 \lstinputlisting[label=src:agda-bool, caption=Agdaにおけるデータ型 Bool の定義] {src/AgdaBool.agda}
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
360
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
361 Bool はコンストラクタ \verb/true/ か \verb/false/ を持つデータ型である。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
362 Bool 自身の型は \verb/Set/ であり、これは Agda が組み込みで持つ「型の型」である。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
363 Set は階層構造を持ち、型の型の型を指定するには Set1 と書く。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
364
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
365 関数の定義は Haskell に近い。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
366 関数名と型を記述した後に関数の本体を \verb/=/ の後に指定する。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
367 関数の型は単純型付き $ \lambda$ 計算と同様に $ \rightarrow $ を用いる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
368 なお、$\rightarrow$に対しては糖衣構文 \verb/->/ も用意されている。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
369 引数は変数名で受けることもできるが、具体的なコンストラクタを指定することでそのコンストラクタが渡された時の挙動を定義できる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
370 これはパターンマッチと呼ばれ、コンストラクタで case 文を行なっているようなものである。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
371 例えば Bool 型の値を反転する \verb/not/ 関数を書くとリスト~\ref{src:agda-not}のようになる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
372
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
373 \lstinputlisting[label=src:agda-not, caption=Agdaにおける関数 not の定義] {src/AgdaNot.agda}
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
374
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
375 関数にはリテラルが存在し、関数名を定義せずともその場で生成することができる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
376 これをラムダ式と呼び、\verb/\arg1 arg2 -> function body/ のように書く。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
377 例えば Bool 型の引数 \verb/b/ を取って not を適用する \verb/not-apply/ をラムダ式で書くとリスト~\ref{src:agda-lambda}のようになる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
378 関数 \verb/not-apply/ をラムダ式を使わずに定義すると \verb/not-apply-2/ になるが、この二つの関数は同一の動作をする。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
379
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
380 \lstinputlisting[label=src:agda-lambda, caption=Agda におけるラムダ式] {src/AgdaLambda.agda}
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
381
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
382 コンストラクタによって処理を分岐する場合はパターンマッチを利用する。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
383 関数の変数部分にコンストラクタを指定し、それぞれに対する処理を \verb/=/ で繋いで記述する。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
384 パターンマッチは全てのコンストラクタのパターンを含まなくてはならない。
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
385 なお、コンストラクタをいくつか指定した後に変数で受けると、変数が持ちうる値は指定した以外のコンストラクタとなる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
386 例えばリスト~\ref{src:agda-pattern}の not は x には true しか入ることは無い。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
387 なお、マッチした値を変数として利用しない場合は \verb/_/ を用いて捨てることもできる。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
388
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
389 \lstinputlisting[label=src:agda-pattern, caption=Agdaにおけるパターンマッチ] {src/AgdaPattern.agda}
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
390
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
391 Agda では特定の関数内のみで利用する関数を \verb/where/ 句で記述できる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
392 これは関数内部の冗長な記述を省略するのに活用できる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
393 スコープは \verb/where/句が存在する関数内部のみであるため、名前空間が汚染させることも無い。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
394 例えば自然数3つを取ってそれぞれ3倍して加算する関数 \verb/f/ を定義するとき、 \verb/where/ を使うとリスト~\ref{src:agda-where} のように書ける。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
395 これは \verb/f'/ と同様の動作をする。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
396 \verb/where/ 句は利用したい関数の末尾にインデント付きで \verb/where/ キーワードを記述し、改行の後インデントをして関数内部で利用する関数を定義する。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
397
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
398 \lstinputlisting[label=src:agda-where, caption=Agda における where 句] {src/AgdaWhere.agda}
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
399
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
400
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
401 データ型のコンストラクタには自分自身の型を引数に取ることもできる(リスト~\ref{src:agda-nat})。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
402 自然数のコンストラクタは2つあり、片方は自然数ゼロ、片方は自然数を取って後続数を返すものである。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
403 例えば0 は \verb/zero/ であり、1 は \verb/suc zero/に、3は \verb/suc (suc (suc zero))/ に対応する。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
404
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
405 \lstinputlisting[label=src:agda-nat, caption=Agdaにおける自然数の定義] {src/AgdaNat.agda}
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
406
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
407 自然数に対する演算は再帰関数として定義できる。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
408 例えば自然数どうしの加算は二項演算子\verb/+/としてリスト~\ref{src:agda-plus}のように書ける。
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
409
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
410 この二項演算子は正確には中置関数である。
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
411 前置や後置で定義できる部分を関数名に \verb/_/ として埋め込んでおくことで、関数を呼ぶ時にあたかも前置や後置演算子のように振る舞う。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
412 例えば \verb/!_/ 関数を定義すると \verb/! true/ のように利用でき、\verb/_~/ 関数を定義すると \verb/false ~/ のように利用できる。
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
413
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
414 また、Agda は再帰関数が停止するかを判定できる。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
415 この加算の二項演算子は左側がゼロに対しては明らかに停止する。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
416 左側が1以上の時の再帰時には \verb/suc n/ から \verb/n/へと減っているため、再帰で繰り返し減らすことでいつかは停止する。
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
417 もし \verb/suc n/ のまま自分自身へと再帰した場合、Agda は警告を出す。
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
418
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
419 \lstinputlisting[label=src:agda-plus, caption=Agda における自然数の加算の定義] {src/AgdaPlus.agda}
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
420
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
421 次に依存型について見ていく。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
422 依存型で最も基本的なものは関数型である。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
423 依存型を利用した関数は引数の型に依存して返す型を決定できる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
424
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
425 Agda で \verb/(x : A) -> B/ と書くと関数は型 A を持つx を受け取り、Bを返す。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
426 ここで B の中で x を扱っても良い。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
427 例えば任意の型に対する恒等関数はリスト~\ref{src:agda-id}のように書ける。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
428
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
429 \lstinputlisting[label=src:agda-id, caption=依存型を持つ関数の定義] {src/AgdaId.agda}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
430
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
431 この恒等関数 \verb/identitiy/ は任意の型に適用可能である。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
432 実際に関数 \verb/identitiy/ を Nat へ適用した例が \verb/identitiy-zero/ である。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
433
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
434 多相の恒等関数では型を明示的に指定せずとも \verb/zero/ に適用した場合の型は自明に \verb/Nat -> Nat/である。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
435 Agda はこのような推論をサポートしており、推論可能な引数は省略できる。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
436 推論によって解決される引数を暗黙的な引数(implicit arguments) と言い、記号 \verb/{}/ でくくる。
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
437
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
438 例えば、\verb/identitiy/ の対象とする型\verb/A/を暗黙的な引数として省略するとリスト~\ref{src:agda-implicit-id}のようになる。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
439 この恒等関数を利用する際は特定の型に属する値を渡すだけでその型が自動的に推論される。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
440 よって関数を利用する際は \verb/id-zero/ のように型を省略して良い。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
441 なお、関数の本体で暗黙的な引数を利用したい場合は \verb/{variableName}/ で束縛することもできる(\verb/id'/ 関数)。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
442 適用する場合も \verb/{}/でくくり、\verb/id-true/のように使用する。
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
443
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
444 \lstinputlisting[label=src:agda-implicit-id, caption=Agdaにおける暗黙的な引数を持つ関数] {src/AgdaImplicitId.agda}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
445
79
4985359bd08b Update agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 78
diff changeset
446 Agda には C における構造体に相当するレコード型も存在する。
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
447 定義を行なう際は \verb/record/キーワードの後にレコード名、型、\verb/where/ の後に \verb/field/ キーワードを入れた後、フィールド名と型名を列挙する。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
448 例えば x と y の二つの自然数からなるレコード \verb/Point/ を定義するとリスト~\ref{src:agda-record}のようになる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
449 レコードを構築する際は \verb/record/ キーワードの後の \verb/{}/ の内部に \verb/fieldName = value/ の形で値を列挙していく。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
450 複数の値を列挙する際は \verb/;/ で区切る。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
451
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
452 \lstinputlisting[label=src:agda-record, caption=Agdaにおけるレコード型の定義] {src/AgdaRecord.agda}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
453
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
454 構築されたレコードから値を取得する際には \verb/RecordName.fieldName/ という名前の関数を適用する(リスト~\ref{src:agda-record-proj} 内2行目) 。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
455 なお、レコードにもパターンマッチが利用できる(リスト~\ref{src:agda-record-proj} 内5行目)。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
456 また、値を更新する際は \verb/record oldRecord {field = value ; ... }/ という構文を利用する。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
457 Point の中の x の値を5増やす関数 \verb/xPlus5/ はリスト~\ref{src:agda-record-proj}の 7,8行目のように書ける。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
458
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
459 \lstinputlisting[label=src:agda-record-proj, caption=Agda におけるレコードの射影、パターンマッチ、値の更新] {src/AgdaRecordProj.agda}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
460
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
461 Agda における部分型のように振る舞う機能として Instance Arguments が存在する。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
462 これはとあるデータ型が、ある型と名前を持つ関数を持つことを保証する機能であり、Haskell における型クラスや Java におけるインターフェースに相当する。
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
463 Agda における部分型の制約は、必要な関数を定義した record に相当し、その制約を保証するにはその record を instance として登録することになる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
464 例えば、同じ型と比較することができる、という性質を表すとリスト~\ref{src:agda-type-class}のようになる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
465 具体的にはとある型 A における中置関数 \verb/_==_/ を定義することに相当する。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
466
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
467 \lstinputlisting[label=src:agda-type-class, caption=Agdaにおける部分型制約] {src/AgdaTypeClass.agda}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
468
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
469 ある型 T がこの部分型制約を満たすことを示すには、型 T でこのレコードを作成できることを示し、それを instance 構文で登録する。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
470 型Nat が Eq の上位型であることを記述するとリスト~\ref{src:agda-instance}のようになる。
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
471
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
472 \lstinputlisting[label=src:agda-instance, caption=Agdaにおける部分型関係の構築] {src/AgdaInstance.agda}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
473
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
474 これで \verb/Eq/ が要求される関数に対して Nat が適用できるようになる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
475 例えば型 A の要素を持つ List A から要素を探してくる elem を定義する。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
476 部分型のインスタンスは \verb/{{}}/ 内部に名前と型名で記述する。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
477 なお、名前部分は必須である。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
478 仮に変数として受けても利用しない場合は \verb/_/ で捨てると良い。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
479 部分型として登録した record は関数本体において \verb/{{variableName}}/ という構文で変数に束縛できる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
480
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
481 \lstinputlisting[label=src:agda-use-instance, caption=Agdaにおける部分型を使う関数の定義] {src/AgdaElem.agda.replaced}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
482
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
483 この \verb/elem/ 関数はリスト~\ref{src:agda-elem-apply} のように利用できる。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
484 Nat型の要素を持つリストの内部に4が含まれるか確認している。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
485 この \verb/listHas4/ は \verb/true/ に評価される。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
486 なお、 \verb/--/ の後はコメントである。
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
487
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
488 \lstinputlisting[label=src:agda-elem-apply, caption=部分型を持つ関数の適用] {src/AgdaElemApply.agda.replaced}
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
489
53
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
490 最後にモジュールについて述べる。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
491 モジュールはほとんど名前空間として作用する。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
492 なお、依存型の解決はモジュールのインポート時に行なわれる。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
493 モジュールをインポートする時は \verb/import/ キーワードを指定する。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
494 また、インポートを行なう際に名前を別名に変更することもでき、その際は \verb/as/ キーワードを用いる。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
495 モジュールから特定の関数のみをインポートする場合は \verb/using/ キーワードを、関数の名前を変える時は \verb/renaming/キーワードを、特定の関数のみを隠す場合は \verb/hiding/ キーワードを用いる。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
496 なお、モジュールに存在する関数をトップレベルで用いる場合は \verb/open/ キーワードを使うことで展開できる。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
497 モジュールをインポートする例をリスト~\ref{src:agda-import}に示す。
52
fb42478e4c96 Writing agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 51
diff changeset
498
53
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
499 \lstinputlisting[label=src:agda-import, caption=Agdaにおけるモジュールのインポート] {src/AgdaImport.agda}
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
500
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
501 また、モジュールには値を渡すことができる。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
502 そのようなモジュールは Parameterized Module と呼ばれ、渡された値はそのモジュール内部で一貫して扱える。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
503 例えば要素の型と比較する二項演算子を使って並べ替えをするモジュール\verb/Sort/ を考える。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
504 そのモジュールは引数に型Aと二項演算子 \verb/</を取り、ソートする関数を提供する。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
505 \verb/Sort/モジュールを \verb/Nat/ と \verb/Bool/ で利用した例がリスト~\ref{src:agda-parameterized-module}である。
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
506
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
507 \lstinputlisting[label=src:agda-parameterized-module, caption=Agda における Parameterized Module] {src/AgdaParameterizedModule.agda}
9902994fbd1a Writing agda description ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 52
diff changeset
508
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
509 % }}}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
510
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
511 % {{{ Reasoning
51
6318c8f4bb8c Writing Agda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 50
diff changeset
512
49
7f1b5c33b282 Add agda.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
513 \section{Reasoning}
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
514 次に Agda における証明を記述していく。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
515 例題として、自然数の加法の可換法則を示す。
49
7f1b5c33b282 Add agda.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
516
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
517 証明を行なうためにまずは自然数を定義する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
518 今回用いる自然数の定義は以下のようなものである。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
519
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
520 \begin{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
521 \item 0 は自然数である
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
522 \item 任意の自然数には後続数が存在する
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
523 \item 0 はいかなる自然数の後続数でもない
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
524 \item 異なる自然数どうしの後続数は異なる ($ n \neq m \rightarrow S n \neq S m $)
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
525 \item 0がある性質を満たし、aがある性質を満たせばその後続数 S(n) も自然数である
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
526 \end{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
527
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
528 この定義は peano arithmetic における自然数の定義である。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
529
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
530 Agda で自然数型 Nat を定義するとリスト \ref{src:agda-nat-2} のようになる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
531
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
532 \lstinputlisting[label=src:agda-nat-2, caption=Agda における自然数型 Nat の定義] {src/Nat.agda}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
533
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
534 自然数型 Nat は2つのコンストラクタを持つ。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
535
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
536 \begin{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
537 \item O
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
538
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
539 引数を持たないコンストラクタ。これが0に相当する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
540
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
541 \item S
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
542
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
543 Nat を引数に取るコンストラクタ。これが後続数に相当する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
544
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
545 \end{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
546
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
547 よって、数値の 3 は \verb/S (S (S O))/ のように表現される。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
548 Sの個数が数値に対応する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
549
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
550 次に加算を定義する(リスト\ref{src:agda-nat-add})。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
551
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
552 \lstinputlisting[label=src:agda-nat-add, caption=Agdaにおける自然数型に対する加算の定義] {src/NatAdd.agda}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
553
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
554 加算は中置関数 \verb/_+_/ として定義する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
555 2つの Nat を取り、Natを返す。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
556 関数 \verb/_+_/ はパターンマッチにより処理を変える。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
557 0に対して m 加算する場合は m であり、 n の後続数に対して m 加算する場合は n に m 加算した数の後続数とする。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
558 S を左の数から右の数へ1つずつ再帰的に移していくような加算である。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
559
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
560 例えば 3 + 1 といった計算は (S (S (S O))) + (S O) のように記述される。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
561 ここで 3 + 1 が 4と等しいことの証明を行なう。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
562
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
563 等式の証明には agda の standard library における Relation.Binary.Core の定義を用いる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
564
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
565 \lstinputlisting[label=src:agda-equiv, caption= Relation.Binary.Core による等式を示す型 $ \equiv $] {src/Equiv.agda.replaced}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
566 Agda において等式は、等式を示すデータ型 $ \equiv $ により定義される。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
567 $ \equiv $ は同じ両辺が同じ項に簡約される時にコンストラクタ refl で構築できる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
568
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
569 実際に 3 + 1 = 4 の証明は refl で構成できる(リスト\ref{src:agda-three-plus-one})。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
570
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
571 \lstinputlisting[label=src:agda-three-plus-one, caption= Agda における 3 + 1 の結果が 4 と等しい証明] {src/ThreePlusOne.agda}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
572
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
573 3+1 という関数を定義し、その型として証明すべき式を記述し、証明を関数の定義として定義する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
574 証明する式は \verb/(S (S (S O))) + (S O)≡(S (S (S (S O))))/ である。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
575 今回は \verb/_+_/ 関数の定義により \verb/ (S (S (S (S O)))) / に簡約されるためにコンストラクタ refl が証明となる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
576
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
577 $ \equiv $ によって証明する際、必ず同じ式に簡約されるとは限らないため、いくつかの操作が Relation.Binary.PropositionalEquality に定義されている。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
578
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
579 \begin{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
580 \item sym : $ x \equiv y \rightarrow y \equiv x $
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
581
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
582 等式が証明できればその等式の左辺と右辺を反転しても等しい。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
583
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
584 \item cong : $ f \rightarrow x \equiv y \rightarrow f x \equiv f y $
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
585
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
586 証明した等式に同じ関数を与えても等式は保たれる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
587
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
588 \item trans : $ x \equiv y \rightarrow y \equiv z \rightarrow x \equiv z $
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
589
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
590 2つの等式に表れた同じ項を用いて2つの等式を繋げた等式は等しい。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
591 \end{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
592
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
593 ではこれから nat の加法の交換法則を証明していく(リスト\ref{src:agda-nat-add-sym})。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
594
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
595 \lstinputlisting[label=src:agda-nat-add-sym, caption= Agda における加法の交換法則の証明] {src/NatAddSym.agda}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
596
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
597 証明する式は $ n + m \equiv m + n $ である。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
598 n と m は Nat であるため、それぞれがコンストラクタ O か S により構成される。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
599 そのためにパターンは4通りある。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
600
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
601 \begin{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
602 \item n = O, m = O
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
603
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
604 \verb/_+_/ の定義により、 O に簡約されるため refl で証明できる。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
605
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
606 \item n = O, m = S m
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
607
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
608 $ O + (S m) \equiv (S m) + O $ を証明することになる。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
609 この等式は \verb/_+_/ の定義により $ O + (S m) \equiv S (m + O) $ と変形できる。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
610 $ S (m + O) $ は $ m + O $ に S を加えたものであるため、 cong を用いて再帰的に \verb/addSym/ を実行することで証明できる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
611
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
612 この2つの証明はこのような意味を持つ。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
613 n が 0 であるとき、 m も 0 なら簡約により等式が成立する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
614 n が 0 であり、 m が 0 でないとき、 m は後続数である。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
615 よって m が (S x) と書かれる時、 x は m の前の値である。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
616 前の値による交換法則を用いてからその結果の後続数も \verb/_+_/ の定義により等しい。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
617
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
618 ここで、 \verb/addSym/ に渡される m は1つ値が減っているため、最終的には n = 0, m = 0 である refl にまで簡約され、等式が得られる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
619
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
620 \item n = S n, m = O
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
621
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
622 $ (S n) + O \equiv O + (S n) $ を証明する。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
623 この等式は \verb/_+_/ の定義により $ S (n + O) \equiv (S n) $ と変形できる。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
624 さらに変形すれば $ S (n + O) \equiv S (O + n) $ となる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
625 よって \verb/addSym/ により O と n を変換した後に cong で S を加えることで証明ができる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
626
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
627 ここで、 $ O + n \equiv n $ は \verb/_+_/ の定義により自明であるが、$ n + O \equiv n $ をそのまま導出できないことに注意して欲しい。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
628 \verb/_+_/ の定義は左側の項から S を右側の項への移すだけであるため、右側の項への演算はしない。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
629
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
630 \item n = S n, m = S m
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
631
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
632 3つのパターンは証明したが、このパターンは少々長くなるため別に解説することとする。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
633 \end{itemize}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
634
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
635 3 つのパターンにおいては refl や cong といった単純な項で証明を行なうことができた。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
636 しかし長い証明になると refl や cong といった式を trans で大量に繋げていく必要性がある。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
637 長い証明を分かりやすく記述するために $ \equiv $-Reasoning を用いる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
638
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
639 $ \equiv $-Reasoning では等式の左辺を begin の後に記述し、等式の変形を $ \equiv \langle expression \rangle $ に記述することで変形していく。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
640 最終的に等式の左辺を $ \blacksquare $ の項へと変形することで等式の証明が得られる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
641
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
642 自然数の加法の交換法則を $ \equiv $-Reasoning を用いて証明した例がリスト\ref{src:agda-reasoning}である。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
643 特に n と m が1以上である時の証明に注目する。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
644
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
645 \lstinputlisting[label=src:agda-reasoning, caption= $ \equiv $ - Reasoning を用いた証明の例] {src/Reasoning.agda.replaced}
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
646
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
647 まず \verb/ (S n) + (S m)/ は \verb/_+_/ の定義により \verb/ S (n + (S m)) / に等しい。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
648 よって refl で導かれる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
649 なお、基本的に定義などで同じ項に簡約される時は refl によって記述することが多い。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
650
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
651 次に \verb/S (n + (S m)) / に対して addSym を用いて交換し、 cong によって S を追加することで \verb/ S ((S m) + n) / を得る。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
652 これは、前3パターンにおいて + の右辺の項が 1以上であっても上手く交換法則が定義できたことを利用して項を変形している。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
653 このように同じ法則の中でも、違うパターンで証明できた部分を用いて別パターンの証明を行なうこともある。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
654
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
655 最後に \verb/S ((S m) + n)/ から \verb/(S m) + (S n)/を得なくてはならない。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
656 しかし、 \verb/_+_/ の定義には右辺に対して S を移動する演算が含まれていない。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
657 よってこのままでは証明することができない。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
658 そのため、等式 $ S (m + n) \equiv m + (S n) $ を addToRight として定義する。
64
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
659 addToRight はコンストラクタによる分岐を用いて証明できる。
10a550bf7e4a Mini fixes with ryokka-san
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
660 値が0であれば自明に成り立ち、1以上であれば再帰的に addToRight を適用することで任意の数に対して成り立つ。
55
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
661 addToRight を用いることで \verb/S ((S m) + n)/ から \verb/(S m) + (S n)/を得られた。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
662 これで等式 $ (S m) + (S n) \equiv (S n) + (S m) $ の証明が完了した。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
663
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
664 自然数に対する + の演算を考えた時にありえるコンストラクタの組み合せ4パターンのいずれかでも交換法則の等式が成り立つことが分かった。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
665 このように、Agda における等式の証明は、定義や等式を用いて右辺と左辺を同じ項に変形することで行なわれる。
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
666
70bea06ebdf3 Add reasoning
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 53
diff changeset
667 % }}}