annotate paper/agda.tex @ 80:73da47f32888

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