annotate paper/type.tex @ 70:4b8a75618f36

Add summary
author atton <atton@cr.ie.u-ryukyu.ac.jp>
date Sun, 05 Feb 2017 16:53:16 +0900
parents 451c510825de
children e9ff08a232f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 \chapter{ラムダ計算と型システム}
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 \label{chapter:type}
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 \ref{chapter:cbc} では CbC のモデル検査的検証アプローチとして、akasha を用いた有限の要素数の挿入時の仕様の検証を行なった。
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 しかし、さらに多くの要素を検証したり無限回の挿入を検証するには状態の抽象化や CbC 側に記号実行の機構を組み込んだり証明を行なう必要がある。
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 CbC は直接自身を証明する機構が存在しない。
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 プログラムの性質を証明するには CbC の形式的な定義が必須となる。
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 \ref{chapter:type} 章ではCbC の項の形式的な定義の一つとして、部分型を用いて CbC の CodeSegment と DataSegment が定義できることを示していく。
30
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
8 また、型システムの別の利用方法として命題が型で表現できる Curry-Howard 対応を利用した証明が存在するが、その利用方法については\ref{chapter:agda}章で述べる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
9
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
10 % {{{ 型システムとは
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
11 \section{型システムとは}
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
12 型システムとは、計算する値を分類することにでプログラムがある種の振舞いを行なわないことを保証する機構の事である\cite{Pierce:2002:TPL:509043}\cite{pierce2013型システム入門プログラミング言語と型の理論}。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
13 ある種の振舞いとはプログラム中の評価不可能な式や、言語として未定義な式などが当て嵌まる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
14 例えば、gcc や clang といったコンパイラは関数定義時に指定された引数の型と呼び出し時の値の型が異なる時に警告を出す。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
15 % TODO: C の warning?
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
16 この警告は関数が受けつける範囲以外の値をプログラマが渡してしまった場合などに有効に働く。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
17 加えて、関数を定義する側も受け付ける値の範囲を限定できるため関数内部の処理を記述しやすい。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
18
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
19 型システムで行なえることには以下のようなものが存在する。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
20
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
21 \begin{itemize}
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
22 \item エラーの検出
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
23
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
24 文字列演算を行なう関数に整数を渡してしまったり、データの単位を間違えてしまったり、複雑な場合分けで境界条件を見落すなど、プログラマの不注意が型の不整合となって早期に指摘できる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
25 この指摘できる詳細さは、型システムの表現力とプログラムの内容に依存する。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
26 多用なデータ構造を扱うプログラム(コンパイラのような記号処理アプリケーションなど)は数値計算のような数種類の単純な型しか使わないプログラムよりも型検査器から受けられる恩恵が大きい。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
27 他にも、ある種のプログラムにとっては型は保守のためのツールともなる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
28 複雑なデータ構造を変更する時、その構造に関連するソースコードを型検査器は明らかにしてくれる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
29
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
30 \item 抽象化
30
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
31
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
32 型は大規模プログラムの抽象化の単位にもなる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
33 例えば特定のデータ構造に対する処理をモジュール化し、パッケージングすることができる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
34 モジュール化されたデータ構造は厳格に定義されたインターフェースを経由して呼び出すことになる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
35 このインターフェースは利用する側に取っては呼び出しの規約となり、実装する側にとってはモジュールの要約となる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
36
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
37 \item ドキュメント化
30
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
38
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
39 型はプログラムを理解する際にも有用である。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
40 関数やモジュールの型を確認することにより、どのデータを対象としているのかといった情報が手に入る。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
41 また、型はコンパイラが実行されるために検査されるため、コメントに埋め込まれた情報と異なり常に正しい情報を提供する。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
42
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
43 \item 言語の安全性
30
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
44
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
45 安全性のの定義は言語によって異なるが、型はデータの抽象化によってある種の安全性を確保できる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
46 例えば、プログラマは配列をソートする関数があった場合、与えられた配列のみがソートされ、他のデータには影響が無いことを期待するだろう。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
47 しかし、低水準言語ではメモリを直接扱えるため、予想された処理の範囲を越えてデータを破壊する可能性がある。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
48 より安全な言語ではメモリアクセスが抽象化し、データを破壊する可能性をプログラマに提供しないという選択肢がある。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
49
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
50 \item 効率性
30
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
51
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
52 そもそも、科学計算機における最初の型システムは Fortran などにおける式の区別であった。% TODO ref fortran
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
53 整数の算術式と実数の算術式を区別し、数値計算の効率化を測るために導入されたのである。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
54 型の導入により、コンパイラはプリミティブな演算とは異なる表現を用い、実行コードを生成する時に適切な機械語表現を行なえるようになった。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
55 昨今の高性能コンパイラでは最適化とコード生成のフェーズにおいて型検査器が収集する情報を多く利用している。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
56
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
57 \end{itemize}
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
58
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
59 型システムの定義には多くの定義が存在する。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
60 型の表現能力には単純型や総称型、部分型などが存在し、動的型付けや静的型付けなど、言語によってどの型システムを採用するかは言語の設計に依存する。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
61 例えば C言語では数値と文字を二項演算子 \verb/+/ で加算できるが、Haskell では加算することができない。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
62 これは Haskell が C言語よりも厳密な型システムを採用しているからである。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
63 具体的には Haskell は暗黙的な型変換を許さず、 C 言語は言語仕様として暗黙の型変換を持っている。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
64
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
65 型システムを定義することはプログラミング言語がどのような特徴を持つか決めることにも繋がる。
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
66
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
67 % }}}
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
68
38
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
69 % {{{ 型無し算術式
34
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
70 \section{型無し算術式}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
71 まず、型システムやその性質について述べるためにプログラミング言語そのものの基本的な性質について述べる。
40
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
72 プログラムの構文と意味論、推論について考えるために自然数とブール値のみで構成される小さな言語を扱いながら考察する。
34
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
73 この言語は二種類の値しか持たないが、項の帰納的定義や証明、評価、実行時エラーのモデル化を表現することができる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
74
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
75 この言語はブール定数 $ true $ と $ false $ 、条件式、数値定数 0 、算術演算子 $ succ $ と $ pred $ 、判定演算子 $ iszero $ のみからなる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
76 算術演算子 $ succ $ は与えられた数の次の数を返し、 $ pred $ はその前の数を返す。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
77 判定演算子$ iszero $ は与えられた項が 0 なら $ true $ を返し、それ以外は $ false $ を返す。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
78 これらを文法として定義すると以下のリスト\ref{src:expr-term}のようになる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
79
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
80 \lstinputlisting[label=src:expr-term, caption=算術式の項定義] {src/expr-term.txt}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
81
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
82 この定義では算術式の項 $ t $ を定義している。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
83 $ ::= $ は項の集合の定義を表であり、$ t $ は項の変数のようなものである。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
84 それに続くすべての行は、構文の選択肢である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
85 構文の選択肢内に存在する記号 $ t $ は任意の項を代入できることを表現している。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
86 このように再帰的に定義することにより、 \verb/ if (ifzero (succ 0)) then true else (pred (succ 0)) / といった項もこの定義に含まれる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
87 例において、 $ succ $ 、 $ pred $ 、 $ iszero $ に複合的な引数を渡す場合は読みやすさのために括弧でくくっている。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
88 括弧の定義は項の定義には含んでいない。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
89 コンパイラなど具体的な字句をパースする必要がある場合、曖昧な構文を排除するために括弧の定義は必須である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
90 しかし、今回は型システムに言及するために曖昧な構文は明示的に括弧で指示することで排除し、抽象的な構文のみを取り扱うこととする。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
91
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
92 現在、項と式という用語は同一である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
93 型のような別の構文表現を持つ計算体系においては式はあらゆる種類の構文を表す。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
94 項は計算の構文的表現という意味である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
95
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
96 この言語におけるプログラムとは上述の文法で与えられた形からなる項である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
97 評価の結果は常にブール定数か自然数のどちらかになる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
98 これら項は値と呼ばれ、項の評価順序の形式化において区別が必要となる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
99
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
100 なお、この項の定義においては \verb/succ true/ といった怪しい項の形成を許してしまう。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
101 実際、これらのプログラムは無意味なものであり、このような項表現を排除するために型システムを利用する。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
102
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
103 ある言語の構文を定義する際に、他の表現かいくつか存在する。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
104 先程の定義は次の帰納的な定義のためのコンパクトな記法である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
105
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
106 \begin{definition}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
107 項の集合とは以下の条件を満たす最小の集合 $ T $ である。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
108 \begin{eqnarray*}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
109 \label{eq:expr}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
110 \{true , false , 0\} \subseteq T \\
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
111 t_1 \in T ならば \{succ \; t_1 , pred \; t_1 , iszero \; t_1\} \subseteq T \\
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
112 t_1 \in T かつ t_2 \in T かつ t_3 \in T ならば if \; t_1 \; then \; t_2 else \; t_3 \subseteq T
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
113 \end{eqnarray*}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
114 \end{definition}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
115
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
116 まず1つめの条件は、$ T $ に属する3つの式を挙げている。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
117 2つめと3つめの条件は、ある種の複合的な式が $ T $ に属することを判断するための規則を表している。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
118 最後の「最小」という単語は $ T $ がこの3つの条件によって要求される要素以外の要素を持たないことを表している。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
119
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
120 また、項の帰納的表現の略記法として、二次元の推論規則形式を用いる方法もある。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
121 これは論理体系を自然演繹スタイルで表現するためによく使われる。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
122 自然演繹による証明は\ref{agda}章内で触れるが、今回は項表現として導入する。
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
123
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
124 \begin{definition}
36
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
125 項の集合は次の規則によって定義される。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
126
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
127 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
128 \AxiomC{$ true \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
129 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
130
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
131 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
132 \AxiomC{$ false \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
133 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
134
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
135 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
136 \AxiomC{$ 0 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
137 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
138
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
139 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
140 \AxiomC{$ t_1 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
141 \UnaryInfC{$ succ \; t_1 \in T$}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
142 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
143
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
144 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
145 \AxiomC{$ t_1 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
146 \UnaryInfC{$ pred \; t_1 \in T$}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
147 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
148
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
149 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
150 \AxiomC{$ t_1 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
151 \UnaryInfC{$ iszero \; t_1 \in T$}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
152 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
153
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
154 \begin{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
155 \AxiomC{$ t_1 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
156 \AxiomC{$ t_2 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
157 \AxiomC{$ t_3 \in T $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
158 \TrinaryInfC{$ if \; t_1 \; then \; t_2 \; else \; t_3 \in T$}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
159 \end{prooftree}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
160
34
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
161 \end{definition}
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
162
36
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
163 最初の$ true, \; false, \; 0 $の3つ規則は再帰的定義の1つめの条件と同じである。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
164 それ以外の4つの規則は再帰的定義の2つめと3つめの条件と同じである。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
165 それぞれの規則は「もし線の上に列挙して前提が成立するのならば、線の下の結論を導出できる」と読む。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
166 $ T $ がこれらの規則を満たす最小の集合である事実は明示的に述べられない。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
167
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
168 言語の構文は定義できたので、次は項がどう評価されるかの意味論について触れていく。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
169 意味論の形式化には操作的意味論や表示的意味論、公理的意味論やゲーム意味論などがあるが、ここでは操作的意味論について述べる。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
170 操作的意味論とは、言語の抽象機械を定義することにより言語の振舞いを規程する。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
171 この抽象機械が示す抽象とは、扱う命令がプロセッサの命令セットなどの具体的なものでないことを表している。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
172 単純な言語の抽象機械における状態は単なる項であり、機械の振舞いは遷移関数で定義される。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
173 この関数は各状態において項の単純化ステップを実行して次の状態を与えるか、機械を停止させる。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
174 ここで項 $ t $ の意味は、$ t $ を初期状態として動き始めた機械が達する最終状態である。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
175
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
176 なお、一つの言語に複数の操作的意味論を与えることもある。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
177 例えば、プログラマが扱う項に似た機械状態を持つ意味論の他に、コンパイラの内部表現やインタプリタが扱う意味論を定義する。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
178 これらの振舞いが同じプログラムを実行した時に何かしらの意味であれば、結果としてその言語の実装の正しさを証明することに繋がる。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
179
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
180 まずはブール式のみの操作的意味論を定義する。
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
181
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
182 \begin{definition}
37
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
183 ブール値(B)
36
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
184
38
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
185
36
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
186 \begin{align*}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
187 t ::= && \text{項} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
188 true && \text{定数真} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
189 false && \text{定数偽} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
190 if \; t \; then \; t \; else \; t && \text{条件式}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
191 \end{align*}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
192
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
193
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
194 \begin{align*}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
195 v ::= && \text{値} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
196 true && \text{真} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
197 false && \text{偽}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
198 \end{align*}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
199
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
200 評価
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
201 \begin{align*}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
202 if \; true \; then \; t_2 \; else t_3 \rightarrow t_2 && \text{(E-IFTRUE)} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
203 if \; false \; then \; t_2 \; else t_3 \rightarrow t_3 && \text{(E-IFFALSE)} \\
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
204 \AxiomC{$ t_1 \rightarrow t_1'$}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
205 \UnaryInfC{$ if \; t_1 \; then \; t_2 \; else \; t_3 \rightarrow if \; t_1' \; then t_2 \; else \;t_3 $}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
206 \DisplayProof
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
207 && \text{(E-IF)}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
208 \end{align*}
37
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
209
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
210 \end{definition}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
211
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
212 評価の最終結果になりえる項である値は定数 $ true $ と $ false $ のみである。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
213 評価の定義は評価関係の定義である。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
214 評価関係 $ t \rightarrow t' $ は「$ t $ が1ステップで $ t' $ に評価される」と読む。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
215 直感的には抽象機械の状態が $ t $ ならば $ t' $ が手に入るという意味である。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
216
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
217 評価関係は3つあるが、2つは前提を持たないため、2つの公理と1つの規則から成る。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
218 1つめの規則 E-IFTRUE の意味は、評価の対象となる項の条件式が定数 $ true $ である時に、then 節にある $ t_2 $ を残して他の全ての項を捨てるという意味である。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
219 E-EIFFALSE も同様に条件式が $ false $ の時に $ t_3 $ のみを残す。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
220 3つ目の規則 E-IF は条件式の評価である。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
221 条件式 $ t $ が $ t'$ に評価されうるのならば then 節と else 節を変えずに条件部のみを評価する。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
222
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
223 評価の定義から分かることの中に、if の中の then節 と else 節は条件部より先に評価されないことがある。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
224 よって、この言語は条件式の評価に対し条件部から評価が優先されるという評価戦略を持つことが分かる。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
225
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
226 \begin{definition}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
227 推論規則のインスタンスとは、規則の結論や前提に対し、一貫して同じ項による書き換えを行なったものである。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
228 \end{definition}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
229
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
230 例えば、
47
45d3ac176bf5 Mini fixes
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
231 $ if \; true \; then \; true \; else \; ( \;if \; false \; then \; false \; else \; false) $
45d3ac176bf5 Mini fixes
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
232 は E-IFTRUE のインスタンスであり、 E-IFTRUEの $ t_2 $ が \verb/true/ かつ、
45d3ac176bf5 Mini fixes
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
233 $ t_3 $ が $ if \; false \; then \; false \; else \; false \;$ の時に相当する。
37
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
234
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
235 \begin{definition}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
236 1ステップ評価関係 $ \rightarrow $ とは、3つの評価の規則を満たす、項に関する最小の二項関係である。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
237 $ (t, \; t') $ がこの関係の元である時、「評価関係式 $ t \rightarrow t'$ は導出可能である」と言う。
36
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
238 \end{definition}
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
239
37
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
240 ここで「最小」という言葉が表れるため、評価関係式 $ t \rightarrow t'$ が導出可能である時かつその時に限り、その関係式は規則によって正当化される。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
241 すなわち評価関係式は公理 E-IFTRUE か E-IFFALSE 、前提が成り立つ時の E-IF のインスタンスとなる。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
242 与えられた評価関係式が導出可能であることを証明するには、葉が E-IFTRUE か E-IFFALSE であり、内部ノードのラベルが E-IF のインスタンスである導出木が示せれば良い。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
243 例えば以下の略記の元 $ if \; t \; then \; false \; then \; false \rightarrow if \; u \; then \; false \; else \; false $ の導出可能性は以下のような導出木によって示せる。
36
34812c1b33c2 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 34
diff changeset
244
37
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
245 \begin{itemize}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
246 \item $ s = if \; true \; then \; false \; else \; false $
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
247 \item $ t = if \; s \; then \; true \; else \; true $
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
248 \item $ u = if \; false \; then \; true \; else \; true $
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
249 \end{itemize}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
250
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
251
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
252 \begin{prooftree}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
253 \AxiomC{}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
254 \RightLabel{E-IFTRUE}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
255 \UnaryInfC{ $ s \rightarrow true $ }
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
256 \RightLabel{E-IF}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
257 \UnaryInfC{ $ t \rightarrow u $}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
258 \RightLabel{E-IF}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
259 \UnaryInfC{ $ if \; t \; then \; false \; then \; false/ \rightarrow if \; u \; then \; false \; else \; false $}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
260 \end{prooftree}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
261
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
262 1ステップ評価関係は与えられた項に対して抽象機械の状態遷移を定義する。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
263 この時、機械がそれ以上ステップを進められない時にそれが最終結果となる。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
264
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
265 \begin{definition}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
266 正規形
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
267
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
268 項 $ t $ が正規形であるとは、$ t \rightarrow t'$となる評価規則が存在しないことである。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
269 \end{definition}
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
270
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
271 この言語において $ true $ や $ false $ は正規形である。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
272 逆に言えば、構文的に正しい if が用いられている場合は評価することが可能なため正規形ではない。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
273 極端に言えばこの言語における全ての値は正規形なのである。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
274 しかし、他の言語における値は一般的に正規形ではない。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
275 実のところ、値でない正規形は実行時エラーとなって表れる。
881bd1d12a45 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
276
40
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
277 実際にこの言語に自然数を導入し、値では無い正規形を確認していく。
34
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
278
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
279
38
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
280 \begin{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
281 算術式BN (B の拡張) の項
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
282 \begin{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
283 t ::= && \text{項} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
284 true && \text{定数真} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
285 false && \text{定数偽} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
286 if \; t \; then \; t \; else \; t && \text{条件式} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
287 0 && \text{定数ゼロ} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
288 succ \; t && \text{後者値} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
289 pred \; t && \text{前者値} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
290 iszero \; t && \text{ゼロ判定}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
291 \end{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
292 \end{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
293
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
294 \begin{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
295 算術式BN の値
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
296 \begin{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
297 v ::= && \text{値} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
298 true && \text{真} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
299 false && \text{偽} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
300 nv && \text{数値} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
301 \end{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
302 \end{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
303
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
304 \begin{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
305 算術式BNの数値
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
306 \begin{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
307 nv ::= && \text{数値} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
308 0 && \text{ゼロ} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
309 succ nv && \text{後者値}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
310 \end{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
311 \end{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
312
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
313 \begin{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
314 算術式BNの評価($ t \rightarrow t' $)
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
315 \begin{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
316 if \; true \; then \; t_2 \; else t_3 \rightarrow t_2 && \text{(E-IFTRUE)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
317 if \; false \; then \; t_2 \; else t_3 \rightarrow t_3 && \text{(E-IFFALSE)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
318 \AxiomC{$ t_1 \rightarrow t_1'$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
319 \UnaryInfC{$ if \; t_1 \; then \; t_2 \; else \; t_3 \rightarrow if \; t_1' \; then t_2 \; else \;t_3 $}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
320 \DisplayProof && \text{(E-IF)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
321 \AxiomC{$ pred \; 0 \rightarrow 0$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
322 \DisplayProof && \text{(E-PREDZERO)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
323 \AxiomC{$ pred \; (succ \; nv_1) \rightarrow nv_1$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
324 \DisplayProof && \text{(E-PREDSUCC)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
325 \AxiomC{$ t_1 \rightarrow t_1'$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
326 \UnaryInfC{$ pred \; t_1 \rightarrow pred \; t_1'$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
327 \DisplayProof && \text{(E-PRED)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
328 \AxiomC{$ iszero \; 0 \rightarrow true$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
329 \DisplayProof && \text{(E-ISZEROZERO)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
330 \AxiomC{$ iszero \; (succ \; nv_1) \rightarrow false$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
331 \DisplayProof && \text{(E-ISZEROSUCC)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
332 \AxiomC{$ t_1 \rightarrow t_1'$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
333 \UnaryInfC{$ iszero \; t_1 \rightarrow iszero \; t_1'$}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
334 \DisplayProof && \text{(E-ISZERO)} \\
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
335 \end{align*}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
336 \end{definition}
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
337
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
338 今回値の定義に数値を表す構文要素が追加されている。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
339 数は0かある数に後者関数を適用したもののどちらかである。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
340 評価規則 E-PREDZERO、E-PREDSUCC、E-ISZEROZERO、E-ISZEROSUCC は演算 \verb/pred/ と \verb/iszero/ が数に適用された時にどう振る舞うかを定義している。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
341 E-SUCC 、 E-PRED 、 E-ISZERO の合同規則も E-IF のように部分項から先に評価することを示している。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
342
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
343 数値の構文要素(nv)はこの定義によって重要な役割をはたす。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
344 例えば、 E-PREDSUCC 規則が適用できる項は任意の項 $ t $ ではなく数値 $nv_1$である。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
345 これは $ pred \; (succ \; (pred \; 0)) $ を $ pred 0 $ に評価できないことを意味する。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
346 なぜなら $ pred \; 0 $ は数値に含まれないからである。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
347
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
348 ここで言語の操作的意味論について考える時、すべての項に関する振舞いを定義する必要がある。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
349 すべての項には $ pred \; 0 $ や $ succ false $ のような項も含まれる。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
350 しかし、 $ succ $ を $ false $ に適用する評価結果は定義されていないため、 $ succ \; false $ は正規形である。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
351 このような、正規形であるが値でない項は行き詰まり状態であるという。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
352 つまり、実行時エラーとは行き詰まり状態の項を指す。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
353 直感的な解釈としてはプログラムが無意味な状態になったこと示しておい、操作的意味論が次に何も行なえないことを特徴付けているのである。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
354 プログラング言語において実行時エラーはセグメンテーションフォールトや不正な命令などいくつかのものが挙げられるが、型システムを考える際にはこれらのエラーは行き詰まり状態という単一の概念で表す。
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
355
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
356 % }}}
34
9800586284e1 Writing expression ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
357
41
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
358 % {{{ 単純型
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
359
40
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
360 \section{単純型}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
361 先程定義した算術式には $ pred \; false $ のようなこれ以上評価できない行き詰まり状態が存在する。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
362 項を実際に評価する前に評価が行き詰まり状態にならないことを保証したい。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
363 そのために、自然数に評価される項とブール値に評価される項とを区別する必要がある。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
364 項を分類するために2つの型 Nat と Bool を定義する。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
365
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
366 ここで、項$t$が型 $T$を持つ、という表現を用いた場合、$t$を評価した結果が明らかに適切な形の値になることを意味する。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
367 明らかに、という意味は項を実行することなく静的に分かるという意味である。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
368 例えば項 $ if \; true \; then \; false \; else \; true $ は Bool 型を持ち、$ pred \; (succ \; (succ \; 0)) $ はNat 型を持つ。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
369 しかし、項の型の分析は保守的であり、$ if \; true \; then \; 0 \; else \; false $ のような項は実際には行き詰まりにならないが型を持てない。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
370
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
371 算術式のための型付け関係は $ t : T $ と書き、項に型を割り当てる推論規則の集合によって定義される。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
372 具体的な数値とブール値に関する拡張は以下である。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
373
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
374 \begin{definition}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
375 NB(型付き) の新しい構文形式
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
376 \begin{align*}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
377 T ::= && \text{型 :} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
378 Bool && \text{ブール型} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
379 Nat && \text{自然数型} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
380 \end{align*}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
381 \end{definition}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
382
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
383
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
384 \begin{definition}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
385 NB(型付き)の型付け規則
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
386 \begin{align*}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
387 true : Bool && \text{T-TRUE} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
388 false : Bool && \text{T-FALSE} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
389 \AxiomC{$t_1 : Bool$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
390 \AxiomC{$t_2 : T$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
391 \AxiomC{$t_3 : T$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
392 \TrinaryInfC{$if \; t_1 \; then \; t_2 \; else \; t_3 : T$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
393 \DisplayProof && \text{T-IF} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
394 0 : Nat && \text{T-ZERO} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
395 \AxiomC{$t_1 : Nat$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
396 \UnaryInfC{$ succ \; t_1 : Nat $}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
397 \DisplayProof && \text{T-SUCC} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
398 \AxiomC{$t_1 : Nat$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
399 \UnaryInfC{$ pred \; t_1 : Nat $}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
400 \DisplayProof && \text{T-PRED} \\
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
401 \AxiomC{$t_1 : Nat$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
402 \UnaryInfC{$ iszero \; t_1 : Bool $}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
403 \DisplayProof && \text{T-BOOL}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
404 \end{align*}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
405 \end{definition}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
406
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
407 T-TRUE と T-FALSE はブール定数に Bool 型を割り当てている。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
408 T-IFは条件式の部分にBool型を、部分式に関しては同じ型を要求している。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
409 これは同じ変数 $ T $ を二回使用することで制約を表している。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
410
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
411 また、数に関しては T-ZERO は Nat 型を $ 0 $ に割り当てている。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
412 T-SUCC と T-PRED は $ t_1 $ が Nat である時に限り Nat 型となる。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
413 同様に、 T-ISZERO は $ t_1 $ が Nat である時に Bool となる。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
414
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
415 \begin{definition}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
416 算術式のための型付け関係とは、NBにおける規則のすべてのインスタンスを満たす、項と型の二項関係である。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
417 項$ t $ に対してある型 $ T $ が存在して $ t : T $ である時、 $ t $ は型付け可能である(または正しく型付けされている)という。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
418 \end{definition}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
419
41
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
420 型推論をを行なう時、$succ t_1$という項が何らかの型を持つならばそれは Nat 型である、といった言及を行なう。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
421 型付け関係を逆転させた補題を定義することで型推論の基本的なアルゴリズムを考えることができる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
422 なお、逆転補題は型付け関係の定義により直ちに成り立つ。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
423
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
424 \begin{lemma}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
425 型付け関係の逆転
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
426 \begin{enumerate}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
427 \item $ true : R $ ならば $ R = Bool $ である
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
428 \item $ false : R $ ならば $ R = Bool $ である
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
429 \item $ if \; t_1 \; then \; t_2 \; else \; t_3 : R $ ならば $ t_1 : Bool $ かつ $ t_2 : R $ かつ $ t_3 : R $ である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
430 \item $ 0 : R $ ならば $ R = Nat $ である
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
431 \item $ succ \; t_1 : R $ ならば $ R = Nat $ かつ $ t_1 : Nat $ である
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
432 \item $ pred \; t_1 : R $ ならば $ R = Nat $ かつ $ t_1 : Nat $ である
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
433 \item $ iszero \; t_1 : R $ ならば $ R = Bool $ かつ $ t_1 : Nat $ である
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
434 \end{enumerate}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
435 \end{lemma}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
436
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
437 逆転補題は型付け関係のための生成補題と呼ばれることもある。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
438 なぜならば、与えられた型付け判断式に対してその証明がどのように生成されたかを示すからである。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
439
40
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
440 型無し算術式の評価導出のように型付けも導出可能であり、それも規則のインスタンスの木である。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
441 型付け関係に含まれる二つ組 $(t, \; T)$は $ t : T $ を結論とする型付け導出により正当化される。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
442 例えば $ if \; (iszero \; 0) \; then \; 0 \; else \; (pred \; 0) : Nat $ の型付け判断の導出木である。
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
443
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
444 \begin{prooftree}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
445 \AxiomC{}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
446 \RightLabel{T-ZERO}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
447 \UnaryInfC{$ 0 : Nat$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
448 \RightLabel{T-ISZERO}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
449 \UnaryInfC{$ iszero \; 0 : Bool$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
450 \AxiomC{}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
451 \RightLabel{T-ZERO}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
452 \UnaryInfC{$ 0 : Nat$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
453 \AxiomC{}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
454 \RightLabel{T-ZERO}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
455 \UnaryInfC{$ 0 : Nat$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
456 \RightLabel{T-PRED}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
457 \UnaryInfC{$ pred \; 0 : Bool$}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
458 \RightLabel{T-IF}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
459 \TrinaryInfC{ if \; (iszero \; 0) \; then \; 0 \; else \; (pred \; 0) : Nat }
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
460 \end{prooftree}
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
461
41
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
462 項その型付けの定義より、型システムが行き詰まり状態にならないことを示す。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
463 その証明は指向定理と保存定理によって証明する。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
464
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
465 \begin{itemize}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
466 \item 進行とは、正しく型付けされた項は行き詰まり状態では無いことである
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
467 \item 保存とは、評価可能な正しく型付けされた項は評価後も正しく型付けされていることである。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
468 \end{itemize}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
469
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
470 型システムがこれらの性質を持つ時、正しく型付けされた項は行き詰まり状態になりえない。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
471
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
472 進行定理の証明の為に Bool 型と Nat 型の標準形(それらの型を持つ正しく型付けされた値)を示す。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
473
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
474 \begin{lemma}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
475 標準形
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
476
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
477 \begin{enumerate}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
478 \item $ v $ が $Bool$ 型の値ならば $v$ は $true$ または $false$ である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
479 \item $v$ が $Nat$ 型の値ならば、$0$ もしくは $Nat$ に対して $succ$ を適用した値である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
480 \end{enumerate}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
481 \end{lemma}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
482
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
483 標準形の証明に関しては値における構造的帰納法を用いる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
484 この言語における値とは $ true $ と $false $ と $ 0$ と $ succ \; nv$ のいずれかの形をしている。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
485 Bool型に関して注目した時、 $ true $ と $ false $ は定義によって正しい。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
486 $ 0 $ と $ succ \; nv $ に関しては逆転補題より Nat 型を持つため、Bool型を持つ値は $ true $ と $ false $ のどちらかとなる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
487 Nat についても同様である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
488
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
489 \begin{theorem}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
490 進行
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
491
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
492 $ t$ が正しく型付けされたと仮定すると、$ t$ は値であるか、またはある $t'$ が存在して$ t \rightarrow t' $ となる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
493 \end{theorem}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
494
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
495 証明は $ t : T $ の導出に関する帰納法による。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
496 T-TRUE 、 T-FALSE 、 T-ZERO の場合は$t$が値であることより成立する。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
497
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
498 T-IF の場合、帰納法の仮定により $ t1 $ は値であるか、$t_1'$ が存在して $ t_1 \rightarrow t_1'$ を満たす。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
499 $ t_1 $ が値ならば、標準形補題により $ true $ か $ false $ であり、その場合は E-IFTRUE か E-IFFALSE が適用可能である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
500 一方 $ t_1 \rightarrow t_1' $ ならば E-IF が適用できる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
501
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
502 T-SUCC の場合も帰納法の仮定により $ t1 $ は値であるか、$t_1'$ が存在して $ t_1 \rightarrow t_1'$ を満たす。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
503 $ t_1 $ が値ならば標準形補題により数値でなければならず、その場合 $ t $ も数値であるため成り立つ。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
504 一方 $ t_1 \rightarrow t_1' $ ならば E-SUCC が適用できる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
505
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
506 T-SUCC の場合も同様で、 $ t_1 $ が値ならば標準形補題により数値でなければならず、その場合 E-PREDZERO か E-PREDSUCC が使える。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
507 $ t_1 \rightarrow t_1' $ ならば E-PRED が適用できる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
508
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
509 T-ISZERO の場合も値ならば標準形補題により $ t_1 $ は数値であり、どちらの場合でも E-ISZEROZERO と E-ISZEROSUCC が適用できる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
510 $ t_1 \rightarrow t_1' $ ならば E-ISZERO が適用できる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
511
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
512 \begin{theorem}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
513 保存
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
514
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
515 $ t : T $ かつ $ t \rightarrow t' $ ならば $ t' : T $ となる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
516 \end{theorem}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
517
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
518 保存定理も $ t : T $ の導出に関する帰納法によって導ける。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
519 帰納法の各ステップにおいて全ての部分導出に関して所望の性質が成り立つと仮定し、導出の最後の規則についての場合分けで証明を行なう。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
520
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
521 導入の最後の規則がT-TRUE の場合、その規則の形から $ t $ は定数 $ true $ でなければならず、 $ T $ は $ Bool$ となる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
522 そして $ t $ は値であるためにどのような $ t' $ も存在せず、定理の要求は満たされる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
523 T-FALSE と T-ZERO の場合も同様である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
524
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
525 導入の最後の規則 T-IF の場合は、$ t $ はある $ t_1, \; t_2 \; t_3 $ に対して $ if \; t_1 then t_2 else t_3 $ という形となる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
526 さらに $ t_1 : Bool $ と $ t_2 : T $ と $ t_3 : T $ となる部分導出がある。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
527 ここで if を持つ評価規則において $ t \rightarrow t'$ を導入できる規則は E-IFTRUE と E-IFFALSE と E-IF のみである。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
528 それぞれの場合について別々に場合分けをして考える。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
529
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
530 \begin{itemize}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
531 \item E-IFTRUE の場合(E-IFFALSE も同様)
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
532
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
533 $ t \rightarrow t' $ が E-IFTRUE を使った導出ならば、 $ t_1$ は $ true $ であり、結果の項 $ t' $ は $ t_2 $ となる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
534 このことより $ t_2 : T $ であることが分かるため、条件を満たす。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
535
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
536 \item E-IF の場合
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
537
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
538 場合分け T-IF の仮定より $ t_1 : Bool $が結論となる、部分導出が得られる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
539 帰納法の仮定を部分導出に適用して $ t_1' : Bool $ とし、 $ t_2 : T $ と $ t_3 : T $ を合わせると規則 T-IF が適用できる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
540 T-IF を適用すると $ if \; t_1' \; then \; t_2 \; else \; t_3$となり、$ t' : T $ が成り立つ。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
541 \end{itemize}
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
542
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
543 T-SUCC が導入の最後であれば、 $ t \rightarrow t'$ を導くためには E-SUCC のみであり、この形から $ t_1 \rightarrow t_1'$が分かる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
544 $ t_1 : Nat $ であることも分かるため、帰納法の仮定より $ t_1' : Nat $ が得られる。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
545 この時 T-SUCC が適用できるため $ succ \; t_1 : Nat$となって $ t' : T $ が成り立つ。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
546 T-PRED も同様である。
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
547
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
548 % }}}
40
9110813f4f68 Writing typed expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 39
diff changeset
549
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
550 % {{{ 型なしラムダ計算
30
55f67e448dcc Add type system description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 28
diff changeset
551 \section{型なしラムダ計算}
38
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
552 計算とは何か、エラーとは何か、を算術式を定義することによって示してきた。
41
d14c3fa5f3ea Wrote simple-type
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 40
diff changeset
553 また、型を導入することにより行き詰まり状態を回避することも示した。
38
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
554 ここで、プログラミング言語における計算を形式的に定義していく。
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
555 プログラミング言語は複雑だが、その計算はある本質的な仕組みからの派生形式として定式化可能であることを Peter Ladin が示した。 % TODO: ref TaPL 61
38
5d4097922243 Wrote untyped expression
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
556 この時 Landin が使った本質的な仕組みとしての核計算がラムダ計算であった。
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
557 ラムダ計算は Alonzo Church が発明した形式的体系の一つである。 % TODO: ref
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
558 ラムダ計算では全ての計算が関数定義と関数適用の基本的な演算に帰着される。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
559 ラムダ計算はプログラミング言語の機能の仕様記述や、言語設計と実装、型システムの研究に多く使われている。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
560 この計算体系の重要な点は、ラムダ計算内部で計算が記述できるプログラミング言語であると同時に、それ自身について厳格な証明が可能な数学的対象としてみなせる点にある。
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
561
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
562 ラムダ計算はいろいろな方法で拡張できる。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
563 数や組やレコードなどはラムダ計算そのもので模倣することができるが、記述が冗長になってしまう。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
564 それらの機能のための具体的な特殊構文を加えることは言語の利用者の視点で便利である。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
565 他にも書き換え可能な参照セルや非局所的な例外といった複雑な機能を表現することもできるが、膨大な変換を用いなければモデル化できない。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
566 それらを言語として備えた拡張に ML や Haskell といったものがある。 % TODO: ref
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
567
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
568 ラムダ計算(または $ \lambda $ 計算) とは、関数定義と関数適用を純粋な形で表現する。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
569 ラムダ計算においてはすべてが関数である。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
570 関数によって受け付ける引数も関数であり、関数が返す結果もまた関数である。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
571
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
572 ラムダ計算の項は変数と抽象と適用の3種類の項からなり、以下の文法に要約される。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
573 変数 $ x $ は項であり、項 $ t_1 $ から変数 $ x $ を抽象化した $ \lambda x . t_1 $ も項であり、項 $ t_1 $ を他の項 $ t_2 $ に適用した $ t_1 t_2 $ も項である。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
574
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
575 \begin{multline*}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
576 t ::= \\
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
577 x \\
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
578 \lambda x . t \\
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
579 t \, t \\
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
580 \end{multline*}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
581
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
582 ラムダ計算において関数適用は左結合とする。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
583 つまり、 $ s \, t \, u $ は $ (s \, t) \, u $ となる。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
584
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
585 また、抽象の本体はできる限り右側へと拡大する。
47
45d3ac176bf5 Mini fixes
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
586 例えば $ \lambda x . \; \lambda y . \; x \, y \, x $ は$ \lambda x . (\lambda . y ((x \, y) \, x)) $ となる。
31
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
587
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
588 ラムダ計算には変数のスコープが存在する。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
589 抽象 $ \lambda x . t $ の本体 $ t $ の中に変数 $ x $ がある時、 $ x $ の出現は束縛されていると言う。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
590 同様に、 $ \lambda x $ は $ t $ をスコープとする束縛子であると言う。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
591 なお、 $ x $ を囲む抽象によって束縛されていない場所の $ x $ の出現は自由であると言う。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
592 例えば $ x \; y $ や $ \lambda y . \; x \; y $ における $ x $ の出現は自由だが、 $ \lambda x . x $ や $ \lambda z . \lambda x . \lambda y . x (y \; z) $ における $ x $ の出現は束縛されている。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
593 $ (\lambda x . x) \;x $ においては、最初の $ x $ の出現は束縛されているが、2つ目の出現は自由である。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
594
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
595 ラムダ計算において、計算とは引数に対する関数の適用である。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
596 抽象に対して項を適用した場合、抽象の本体に存在する束縛変数に適用する項を代入したもので書き換える。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
597 図式的には
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
598
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
599 \begin{equation*}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
600 (\lambda x . t_{12}) t_2 \rightarrow [ x \mapsto t_2] t_{12}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
601 \end{equation*}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
602
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
603 と記述する。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
604 ここで $ [ x \mapsto t_2] t_{12} $ とは、$ t_12 $ 中の自由な $ x $ を全て $ t_2 $ で置換した項を意味する。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
605 例えば、 $ (\lambda x . x) \; y $ は $ y $ となり、項 $ (\lambda x . x (\lambda x . x)) (y \; z) $ は $ y \; z \; (\lambda x . x) $ となる。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
606
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
607 なお、 $ (\lambda x . t_{12}) t_2 $ という形の項を簡約基(redex, reducible expression) と呼び、上記の規則で簡約基を置換する操作をベータ簡約と呼ぶ。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
608 ラムダ計算のための評価戦略には数種類の戦略がある。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
609
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
610 \begin{itemize}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
611 \item 完全ベータ簡約
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
612
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
613 任意の簡約基がいつでも簡約されうる。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
614 つまり項の中からどの順番で簡約しても良い。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
615
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
616 \item 正規順序簡約
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
617
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
618 最も左で最も外側の簡約基が最初に簡約される。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
619
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
620 \item 名前呼び
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
621
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
622 正規順序の中でも抽象の内部での簡約を許さない。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
623 名前呼びの変種は Algol-60 や Haskell で利用されている。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
624 なお、Haskell においては必要呼びという最適化された変種を利用している。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
625
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
626 \item 値呼び
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
627
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
628 ほとんどの言語はこの戦略を用いている。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
629 基本的には最も左の簡約基をを簡約するが、右側が既に値(計算が終了してもう簡約できない閉じた項)になっている簡約基のみを簡約する。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
630 \end{itemize}
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
631
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
632 値呼び戦略は関数の引数が本体で使われるかに関わらず評価され、これは正格と呼ばれる。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
633 名前呼びなどの非正格な戦略は引数が使われる時のみ評価され、これは遅延評価とも呼ばれる。
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
634
9dc9c50a28bd Writing lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 30
diff changeset
635 ラムダ計算において、複数の引数は、関数を返り値として返す高階関数として定義できる。
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
636 項 $ s $ が二つの自由変数 $ x $ と $ y $ を含むとすれば、 $ \lambda x . \lambda y . s $ と書くことで二つの引数を持つ関数を表現できる。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
637 これは $ x $ に $ v $ が与えられた時、$ y $ を受けとり、 $ s $ の抽象内の自由な $ x $ を $ v $ に置き換えた部分を置換する関数、を返す。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
638 例えば $ (\lambda x . \lambda y . s) \; v \; w $ は $ (\lambda y . [x \mapsto v] s) w $ に簡約され、 $ [y \mapsto w][x \mapsto v]s $ に簡約される。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
639 なお、複数の引数を取る関数を高階関数に変換することはカリー化と呼ばれる。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
640
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 48
diff changeset
641 % TODO: ラムダの再帰とかペアとかの解説 直積直和
39
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
642
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
643 ラムダ計算の帰納的な項は以下のように定義される。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
644
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
645 \begin{definition}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
646 $ V $ を変数名の加算集合とする。項の集合は以下を満たす最小の集合 $ T $ である。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
647
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
648 \begin{eqnarray*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
649 任意の x \in V について x \in T \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
650 t_1 \in T かつ x in V ならば \lambda x . t \in T \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
651 t_1 \in T かつ t_2 \in T ならば t_1 \; t_2 \in T
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
652 \end{eqnarray*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
653 \end{definition}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
654
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
655 また、形式的な自由変数の定義を与える。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
656
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
657 \begin{definition}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
658 項 $ t $ の自由変数の集合は $ FV(t)$と書き、以下のように定義される。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
659
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
660 \begin{eqnarray*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
661 FV(x) = \{ x \} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
662 FV(\lambda . t_1 x) = FV(t_1) \setminus \{ x \} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
663 FV(t_1 \; t_2) = FV(t_1) \cup FV(t_2)
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
664 \end{eqnarray*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
665 \end{definition}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
666
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
667 記号 $ \setminus $ は集合に対する二項演算子であり、$ S \setminus T := {x \in S : x \notin T}$ である。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
668 つまり、$ t_1 $の内部の自由変数の集合から $ x $ を抜いた集合である。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
669
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
670 最後に代入について定義する。
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
671 代入の操作は直感的には置換であるが、変数の束縛に注意しなくてはならない。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
672 例えば抽象への代入を以下のように定義する。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
673
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
674 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
675 [ x \mapsto s ] (\lambda y . t_1) = \lambda y . [ x \mapsto s] t_1
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
676 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
677
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
678 この場合、束縛変数の名前によっては定義が破綻してしまう。例えば以下のようになる。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
679
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
680 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
681 [x \mapsto y](\lambda x . x) = \lambda x . y
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
682 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
683
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
684 $ \lambda $ よって束縛されているはずの $ x $ が書き変わっている。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
685 これはスコープとして振る舞っていないので誤っている。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
686 この問題は項 $ t $ 内の変数 $ x $ の自由な出現と束縛された出現を区別しなかったために出現した誤りである。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
687
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
688 そこで、$ x $ を束縛する項に対しては置換行なわないように定義を変える。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
689
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
690 \begin{itemize}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
691 \item $ y = x $ の場合
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
692 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
693 [ x \mapsto s ] (\lambda y . t_1) = \lambda y . t_1
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
694 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
695
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
696 \item $ y \neq x $ の場合
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
697 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
698 [ x \mapsto s ] (\lambda y . t_1) = \lambda y . [ x \mapsto s] t_1
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
699 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
700 \end{itemize}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
701
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
702 この場合は束縛された変数を上書きしないが、逆に自由変数を束縛するケースが発生する。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
703 具体的には以下である。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
704
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
705 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
706 [ x \mapsto z] (\lambda z . x) = \lambda z . z
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
707 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
708
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
709 項 $ s $ 中の自由変数が項 $ t $ に代入されて束縛される現象は変数捕獲と呼ばれる。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
710 これを避けるためには $ t $ の束縛変数の名前が $ s $ の自由変数の名前と異なることを保証する必要がある。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
711 変数捕獲を回避した代入操作は捕獲回避代入と呼ばれる。
33
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
712 代入における名前の衝突を回避するために項の束縛変数の名前を一貫して変更することで変数捕獲を回避する方法も存在する。
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
713 束縛変数の名前を一貫して変更することをアルファ変換と呼ばれる。
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
714 これは関数抽象に対する束縛変数は問わないという直感からくるもので、 $ \lambda x . x $ も $ \lambda y . y $ も振舞いとしては同じ関数であるとみなすものである。
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
715 捕獲回避の条件を追加した代入の定義は以下のような定義となる。
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
716
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
717 \begin{itemize}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
718 \item 変数への代入
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
719
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
720 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
721 [ x \mapsto s ] x = s
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
722 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
723
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
724 \item 存在しない変数への代入($ y \neq x $ の時)
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
725
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
726 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
727 [ x \mapsto s ] y = y
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
728 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
729
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
730 \item 抽象内の項への代入($ y \neq x $ かつ $ y $ が $ s $ の自由変数でない)
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
731
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
732 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
733 [ x \mapsto s ] (\lambda y . t_1) = \lambda y . [ x \mapsto s] t_1
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
734 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
735
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
736 \item 適用への代入
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
737
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
738 \begin{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
739 [x \mapsto s] (t_1 \; t_2) = (t_1[x\mapsto s])([x \mapsto s] t_2)
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
740 \end{equation*}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
741
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
742 \end{itemize}
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
743
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
744 この定義は少なくとも代入が行なわれる際には正しく代入が行なえる。
33
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
745 さらに、抽象が束縛している変数を名前では無く数字として扱う名無し表現も存在する。
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
746 これは De Brujin 表現と呼ばれ、コンパイラ内部などでの項表現として用いられる。 % TODO: ref and spell check
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
747
39
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
748 最終的な型無しラムダ計算 $ \lambda $ の項の定義と評価の要約を示す。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
749
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
750 \begin{definition}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
751 $ \rightarrow $ (型無し)
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
752
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
753
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
754 \begin{align*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
755 t ::= && \text{項} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
756 \lambda x . t && \text{ラムダ抽象} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
757 t \; t && \text{関数適用}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
758 \end{align*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
759
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
760
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
761 \begin{align*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
762 v ::= && \text{値} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
763 \lambda x . t && \text{ラムダ抽象値}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
764 \end{align*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
765
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
766 評価( $ t \rightarrow t' $)
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
767
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
768 \begin{align*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
769 \AxiomC{$ t_1 \rightarrow t_1'$}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
770 \UnaryInfC{$t_1 \; t_2 \rightarrow t_1' t_2$}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
771 \DisplayProof && \text{E-APP1} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
772 \AxiomC{$ t_2 \rightarrow t_2'$}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
773 \UnaryInfC{$v_1 \; t_2 \rightarrow v_1 t_2'$}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
774 \DisplayProof && \text{E-APP2} \\
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
775 (\lambda x . t_{12}) \; v_2 \rightarrow [ x \mapsto v_2] t_{12}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
776 && \text{E-APPABS}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
777 \end{align*}
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
778 \end{definition}
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
779
33
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
780 項は変数かラムダ抽象か関数適用の3つにより構成される。
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
781 また、ラムダ抽象値は全て値である。
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
782 加えて評価は関数適用を行なう E-APPABS 計算規則と、適用の項を書き換える E-APP1 と E-APP2 合同規則により定義される。
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
783
39
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
784 この定義からも評価戦略と評価順序が分かる。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
785 関数を適用する E-APPABS は左側が抽象であり、右側が値である $v_2$ の時にしか適用されない。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
786 逆に、規則 E-APP1 の$t_1$は任意の項にマッチするため関数部分が値でない関数適用に用いる。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
787 一方、E-APP2 は左辺が値であるようになるまで評価されない。
5b1e3b62e6dc Wrote untyped lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 38
diff changeset
788 よって、関数適用 $ t_1 \; t_2 $ の評価順は、まずE-APP1を用いて$t_1$が値となった後にE-APP2を用いて$t_2$を値とし、最後にE-APPABSで関数を適用を行なう。
33
74a29a48575a Update lambda description
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 32
diff changeset
789
32
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
790
63bbbf54d541 Writing lambda ...
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 31
diff changeset
791 % }}}
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
792
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
793 % {{{ 単純型付きラムダ計算
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
794
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
795 \section{単純型付きラムダ計算}
42
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
796 型無しラムダ計算に対して単純型を適用する場合、ラムダ抽象の型について考える必要がある。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
797 ラムダ抽象は値を取って値を返すため、関数として考えることもできる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
798 差し当たりBool型における関数の型を $ \lambda x . t : \rightarrow $ と定義する。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
799 この定義においては $ \lambda x . true $ についても $\lambda x . \lambda y . y $ のような型も同一の型を持つ。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
800 この二つの項は値を適用すると値を返すという点では同じであるが、前者は $ true $ を返し、後者は $ \lambda y . y $ を返す。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
801 これでは関数を適用した際に返す値の型は関数の型から予測できず、加えてどの値に対して適用可能かも分からない。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
802 そのために引数にどのような型を期待しているのか、正しい型の値を適用するとどの型の値を返すのかを型情報に追加する。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
803 具体的には以下のように $ \rightarrow $ を $ T_1 \rightarrow T_2 $ の形をした無限の型の族に置き換える。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
804
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
805 \begin{definition}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
806 型 Bool 上の単純型の集合は次の文法により生成される。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
807
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
808 \begin{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
809 t ::= && 型 : \\
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
810 T \rightarrow T && 関数の型 \\
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
811 Bool && ブール値型
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
812 \end{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
813 \end{definition}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
814
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
815 なお、型構築子 $ \rightarrow $ は右結合である。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
816 つまり $ T_1 \rightarrow T_2 \rightarrow T_3 $ は$ T_1 \rightarrow (T_2 \rightarrow T_3) $となる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
817
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
818 $ \lambda x . t $ に対して型を割り当てる時、明示的に型付けする方法と暗黙的に型付けする方法がある。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
819 明示的に型付けを行なう場合はプログラマが項に型の注釈を記述する。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
820 暗黙的に型付けを行なう場合は型検査器に情報を推論させ、型を再構築させる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
821 型推論は $ \lambda $ 計算の文献内では型割り当て体系と呼ぶこともある。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
822 今回は明示的に型を指定する方法を取る。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
823
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
824 $ \lambda $ 抽象の引数の型が分かれば、結果の型は本体 $ t_2 $となる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
825 ここで、$ t_2 $内における $ x $ の出現は型 $ T_1 $ の項を表すと仮定する必要がある。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
826 これは引数に対して正しい型の値が渡されたにも関わらず抽象内で異なる型として振る舞うのを禁止するためである。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
827 この $ \lambda $ 抽象の型付けは以下の T-ABS によって定義される。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
828
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
829
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
830 \begin{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
831 \AxiomC{$x : T_1 \vdash t_2 : T_2$}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
832 \UnaryInfC{$ \vdash \lambda x : T_1 . t_2 : T_1 \rightarrow T_2$}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
833 \DisplayProof && \text{T-ABS}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
834 \end{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
835
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
836 項は抽象を入れ子で持つ可能性があるため、引数の仮定は複数持ちうる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
837 このため型付け関係は二項関係 $ t : T $ から、三項関係 $ \Gamma \vdash t : T $ となる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
838 ここで $ \Gamma $ とは $ t $ に表われる自由変数の型の仮定の集合である。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
839 $ \Gamma $ は型付け文脈や型環境と呼ばれ、$ \Gamma \vdash t : T $ は「型付け文脈 $ \Gamma $ において項 $ t$ は型$T$を持つ」と読む。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
840 空の文脈は $ \emptyset$ と書かえることもあるが、通常は省略して $ \vdash t : T $ と書く。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
841 また、型環境に対する $ , $ 演算子は $ \Gamma $ の右に新しい束縛を加えて拡張する。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
842
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
843 新しい束縛と既に $ \Gamma $ に表われている束縛は混同しないように名前 $ x$は$\Gamma $に存在しない名前から選ばれるものとする。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
844 これはアルファ変換により$\lambda$抽象の束縛名は一貫して変更ができるため、常に満たせる。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
845
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
846 ラムダ抽象に型を持たせる規則の一般的な形は
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
847
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
848 \begin{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
849 \AxiomC{$ \Gamma, x : T_1 \vdash t_2 : T_2$}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
850 \UnaryInfC{$ \Gamma \vdash \lambda x : T_1 . t_2 : T_1 \rightarrow T_2$}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
851 \DisplayProof && \text{T-ABS}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
852 \end{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
853
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
854 であり、変数の型付け規則は
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
855
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
856 \begin{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
857 \AxiomC{$ x : T \in \Gamma $}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
858 \UnaryInfC{$ \Gamma \vdash x : T$}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
859 \DisplayProof && \text{T-VAR}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
860 \end{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
861
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
862 である。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
863 $ x : T \in T $ は 、$ \Gamma$において $x$ に仮定された型は $ T $ である、と読む。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
864
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
865 最後に関数適用の型付け規則を定義する。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
866
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
867 \begin{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
868 \AxiomC{$ \Gamma \vdash t_1 : T_{11} \rightarrow T_{12}$}
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
869 \AxiomC{$ \Gamma \vdash t_2 : T_{11}$}
42
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
870 \BinaryInfC{$ \Gamma \vdash t_1 \; t_2 : T_{12}$}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
871 \DisplayProof && \text{T-APP}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
872 \end{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
873
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
874 もし $t_1$ が$ T_{11}$の引数を $ T_{12}$の計算結果に移す関数へ評価され、$ t_2$が型$T_{11}$の計算結果にに評価されるのであれば、$t_1$ を $ t_2$に適用した計算結果は $ T_{12}$の型を持つ。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
875 ブール定数と条件式の型付け規則は型付き算術式と時と同様である。
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
876
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
877 最終的な純粋単純型付きラムダ計算の規則を示す。
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
878 純粋とは基本型を持たないという意味であり、純粋単純型付きラムダ計算にはブールのような型を持たない。
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
879 この純粋単純型付きラムダ計算でブール値を扱う場合は型環境$\Gamma$を考慮してブール値の規則を追加すれば良い。
42
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
880
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
881 \begin{definition}
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
882 $ \rightarrow $ (型付き)の構文
42
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
883
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
884 \begin{align*}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
885 t ::= && \text{項} \\
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
886 x && \text{変数} \\
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
887 \lambda x : T . t && \text{ラムダ抽象} \\
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
888 t \; t && \text{関数適用} \\
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
889 \end{align*}
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
890
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
891 \begin{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
892 v ::= && \text{項} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
893 \lambda x : T . t && \text{ラムダ抽象値} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
894 \end{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
895
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
896 \begin{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
897 T ::= && \text{型} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
898 T \rightarrow T && \text{関数型} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
899 \end{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
900
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
901 \begin{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
902 \Gamma ::= && \text{文脈} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
903 \emptyset && \text{空の文脈} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
904 \Gamma , x : T && \text{項変数の束縛} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
905 \end{align*}
42
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
906 \end{definition}
142c8de4a24f Writing typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 41
diff changeset
907
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
908 \begin{definition}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
909 $ \rightarrow $ (型付き)の評価($ t \rightarrow t'$)
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
910
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
911 \begin{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
912 \AxiomC{$t_1 \rightarrow t_1'$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
913 \UnaryInfC{$t_1 \; t_2 \rightarrow t_1' \; t_2$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
914 \DisplayProof && \text{E-APP1} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
915 \AxiomC{$t_2 \rightarrow t_2'$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
916 \UnaryInfC{$v_1 \; t_2 \rightarrow v_1 \; t_2'$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
917 \DisplayProof && \text{E-APP2} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
918 (\lambda x : T_{11} . t_{12}) v_2 \rightarrow [ x \mapsto v_2] t_{12} &&
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
919 \text{E-APPABS}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
920 \end{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
921 \end{definition}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
922
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
923 \begin{definition}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
924 $ \rightarrow $ (型付き)の型付け($\Gamma \vdash t : T $)
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
925
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
926 \begin{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
927 \AxiomC{$ x : T \in \Gamma$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
928 \UnaryInfC{$\Gamma \vdash x : T $}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
929 \DisplayProof && \text{T-VAR} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
930 \AxiomC{$\Gamma , x : T_1 \vdash t_2 : T_2$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
931 \UnaryInfC{$\Gamma \vdash \lambda x : T_1 . t_2 : T_1 \rightarrow T_2$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
932 \DisplayProof && \text{E-ABS} \\
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
933 \AxiomC{$ \Gamma \vdash t_1 : T_{11} \rightarrow T_{12}$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
934 \AxiomC{$ \Gamma \vdash t_2 : T_{11}$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
935 \BinaryInfC{$\Gamma \vdash t_1 \; t_2 : t_{12}$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
936 \DisplayProof && \text{T-APP}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
937 \end{align*}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
938 \end{definition}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
939
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
940 単純型付きラムダ計算の型付け規則のインスタンスも型付き算術式のように導出木をすることで示せる。
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
941 例えば $ (\lambda x : Bool\; x) \; true $ が空の文脈において $ Bool$を持つことは以下の木で表せる。
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
942
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
943 \begin{prooftree}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
944 \AxiomC{$ x : Bool \in x : Bool$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
945 \RightLabel{T-VAR}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
946 \UnaryInfC{$x : Bool \vdash x : Bool$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
947 \RightLabel{T-ABS}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
948 \UnaryInfC{$\vdash \lambda x : Bool . x : Bool \rightarrow Bool$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
949 \AxiomC{}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
950 \RightLabel{T-TRUE}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
951 \UnaryInfC{$\vdash true : Bool$}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
952 \RightLabel{T-APP}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
953 \BinaryInfC{$\vdash (\lambda x : Bool . x) \; true : Bool $}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
954 \end{prooftree}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
955
47
45d3ac176bf5 Mini fixes
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 46
diff changeset
956 純粋型付きラムダ計算の型システムにおいて、閉じた項に対して進行定理と保存定理は成り立つ\cite{Pierce:2002:TPL:509043}\cite{pierce2013型システム入門プログラミング言語と型の理論}。 % FIXME: 進行定理と保存定理の証明。
43
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
957 閉じた項、という制限が付いているのは $ f \; true $ といった自由変数が存在する項は正規形ではあるが値でないからである。
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
958 しかし、開いた項に関しては評価が行なえないために型システムの検査対象に含まれない。
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
959
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
960 % }}}
9030d2680559 Wrote typed-lambda
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 42
diff changeset
961
46
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
962 % {{{ 部分型付け
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
963
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
964 \section{部分型付け}
44
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
965 単純型付きラムダ計算では、ラムダ計算の項が型付けされることを確認した。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
966 ここで、 単純型の拡張として、レコードを導入する。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
967
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
968 レコードとは名前の付いた複数の値を保持するデータである。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
969 C 言語における構造体などがレコードに相当する。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
970 値を保持する各フィールド $ t_1 $ はあらかじめ定められた集合 L からラベル $ l_i$ を名前として持つ。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
971 例えば $ { x : Nat } $ や $ {no : 100, point 33}$ などがこれに相当する。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
972 なお、あるレコードの項や値に表れるラベルはすべて相異なるとする。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
973 レコードから値を取り出す際にはラベルを指定して値を射影する。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
974
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
975 レコードの拡張の定義は以下である。
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
976
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
977 \begin{definition}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
978 レコードの拡張に用いる新しい構文形式
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
979
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
980 \begin{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
981 t :: = ... && \text{項 :} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
982 \{l_i = t_i^{\; i \in 1 .. n}\} && \text{レコード} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
983 t.l && \text{射影} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
984 \end{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
985
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
986 \begin{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
987 v :: = ... && \text{値 :} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
988 {l_i : v_i^{\; i \in 1..n}} && \text{レコードの値}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
989 \end{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
990
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
991 \begin{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
992 T :: = ... && \text{型 :} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
993 \{l_i : T_i^{\; i \in 1..n}\} && \text{レコードの型}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
994 \end{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
995 \end{definition}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
996
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
997 \begin{definition}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
998 レコードの拡張に用いる新しい評価規則
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
999
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1000 \begin{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1001 \{l_i = v_i^{\; i \in 1..n}.l_j \rightarrow v_j\} && \text{E-PROJRCD} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1002 \AxiomC{$t_1 \rightarrow t_1'$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1003 \UnaryInfC{$t_1.l \rightarrow t_1'.l$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1004 \DisplayProof && \text{E-PROJ} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1005 \AxiomC{$ t_j \rightarrow t_j'$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1006 \UnaryInfC{$ \{l_i = v_i^{i \in 1..j-1}, l_j = t_j, l_k = t_k^{k \in j+1 .. n}\}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1007 \rightarrow
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1008 \{l_i = v_i^{i \in 1..j-1}, l_j = t_j', l_k = t_k^{k \in j+1 .. n}\}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1009 $}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1010 \DisplayProof && \text{E-RCD} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1011 \end{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1012 \end{definition}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1013
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1014 \begin{definition}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1015 レコードの拡張に用いる新しい型付け規則
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1016
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1017 \begin{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1018 \AxiomC{各iに対して$ \Gamma \vdash t_i : T_i$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1019 \UnaryInfC{$ \Gamma \vdash \{ l_i = t_i^{\; i \in 1..n} : \{l_i : T_i^{\; i \in 1..n}\}$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1020 \DisplayProof && \text{T-RCD} \\
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1021 \AxiomC{$ \Gamma \vdash t_1 : \{ l_i : T_i^{\; i \in 1.. n}\}$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1022 \UnaryInfC{$\Gamma \vdash t_1.lj : T_j$}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1023 \DisplayProof && \text{T-PROJ}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1024 \end{align*}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1025 \end{definition}
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1026
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1027 レコードを用いることで複数の値を一つの値としてまとめて扱うことができる。
45
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1028 しかし、引数にレコードを取る場合、その型と完全に一致させる必要がある。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1029 例えば $ \{ x : Nat \} $ を引数に取る関数に対して $ \{ x : Nat , y : Bool\}$ といった値を適用することができない。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1030 しかし、直感的には関数の要求はフィールド $x $ が型 $Nat$を持つことのみであり、その部分にのみ注目すると$ \{ x : Nat , y : Bool\}$ も要求に沿っている。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1031
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1032 部分型付けの目標は上記のような場合の項を許すように型付けを改良することにある。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1033 つまり型 $ S $ の任意の項が、型 $ T $ の項が期待される文脈において安全に利用できることを示す。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1034 この時、$ S $ を $ T $ の部分型と呼び、 $ S <: T $ と書く。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1035 これは型 $ S $ が型 $ T $よりも情報を多く持っていることを示しており、$S$は$T$の部分型である、と読む。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1036 $ S <: T $ の別の読み方として、型 $ T $ は型 $ S $ の上位型である、という読み方も存在する。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1037
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1038 型付け関係と部分型関係をつなぐための新しい型付け規則を考える。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1039
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1040 \begin{align*}
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1041 \AxiomC{$\Gamma \vdash t : S $}
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1042 \AxiomC{$ S <: T $}
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1043 \BinaryInfC{$ \Gamma \vdash t : T$}
46
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1044 \DisplayProof && \text {T-SUB}
45
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1045 \end{align*}
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1046
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1047 この規則は $ S <: T $ ならば $ S $ 型の要素 $ t$はすべて $ T $の要素であると言っている。
2c42cfe593e6 Writing subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 44
diff changeset
1048 例えば、先程の $ \{ x : Nat \} $ と $ \{ x : Nat , y : Bool\}$ が $ \{ x : Nat , y : Bool\} <: \{ x : Nat \}$ となるように部分型関係を定義した時に $ \Gamma \vdash \{x=0, y=1\} : \{ x : Nat \}$ を導ける。
44
3b2446944d11 Add record
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 43
diff changeset
1049
46
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1050 部分型関係は $ S <: T $ という形式の部分型付け式を導入するための推論規則の集まりとして定式化される。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1051 始めに、部分型付けの一般的な規定から考える。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1052 部分型は反射的であり、推移的である。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1053
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1054 \begin{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1055 S <: S && \text{S-REFL} \\
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1056 \AxiomC{$S <: U$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1057 \AxiomC{$U <: T$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1058 \BinaryInfC{$ S <: T $}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1059 \DisplayProof && \text{S-TRANS}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1060 \end{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1061
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1062 これらの規則は安全代入に対する直感より正しい。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1063 次に、基本型や関数型、レコード型などの型それぞれに対して、その形の型の要素が期待される部分に対して上位型の要素を利用しても安全である、という規則を導入していく。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1064
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1065 レコード型については型 $ T = \{ l_i : T_1 ... l_n : T_n\} $が持つフィールドが型 $ S = \{ k_1 : S_1 ... k_n : T_n\} $のものよりも少なければ $S$ を $T$の部分型とする。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1066 つまりレコードの終端フィールドのいくつかを忘れてしまっても安全である、ということを意味する。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1067 この直感は幅部分型付け規則となる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1068
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1069 \begin{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1070 \{l_i : T_i^{\; i \in 1..n+k} \} <: \{l_i : T_i^{\; i \in 1..n}\}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1071 && \text{S-RCDWIDTH}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1072 \end{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1073
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1074 フィールドの多い方が部分型となるのは名前に反するように思える。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1075 しかし、フィールドが多いレコードほど制約が多くなり表すことのできる集合の範囲は小さくなる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1076 集合の大きさで見ると明かにフィールドの多い方が小さいのである。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1077
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1078 幅部分型付け規則が適用可能なのは、共通のフィールドの型が全く同じな場合のみである。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1079 しかし、その場合フィールドの型に部分型を導入できず、フィールドの型の扱いで同じ問題を抱えることとなる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1080 そのために導入するのが深さ部分型付けである。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1081 二つのレコードの中で対応するフィールドの型が部分型付け関係にある時に個々のフィールドの型が異なることを許す。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1082 これは具体的には以下の規則となる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1083
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1084 \begin{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1085 \AxiomC{各iに対して $S_i <: T_i$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1086 \UnaryInfC{$\{ l_i : S_i^{\; i \in 1..n}\} <: \{l_i : T_i^{\; i \in 1..n}\}$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1087 \DisplayProof && \text{S-RCDDEPTH}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1088 \end{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1089
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1090 これらを用いて $ \{ x : \{a : Nat , b : Nat\}, y : \{m : Nat\}\}$ が $ \{x : \{ a : Nat\}, y : \{\}\}$の部分型であることは以下のように導出できる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1091
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1092 \begin{prooftree}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1093 \AxiomC{}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1094 \RightLabel{S-RCDWIDTH}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1095 \UnaryInfC{$ \{a : Nat, b : Nat\} <: \{a : Nat\}$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1096 \AxiomC{}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1097 \RightLabel{S-RCDWIDTH}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1098 \UnaryInfC{$ \{m : Nat\} <: \{\}$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1099 \RightLabel{S-RCDDEPTH}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1100 \BinaryInfC{$ \{ x : \{a : Nat , b : Nat\}, y : \{m : Nat\}\} <: \{x : \{ a : Nat\}, y : \{\}\}$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1101 \end{prooftree}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1102
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1103 最後に、レコードを利用する際はフィールドさえ揃っていれば順序は異なっても良いという規則を示す。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1104
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1105 \begin{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1106 \AxiomC{$ \{ k_j : S_j^{\; i \in 1 .. n} \}$ は $ \{ l_i : T_i^{\; i \in 1 ..n}\} $ の並べ替えである}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1107 \UnaryInfC{$ \{k_j : S_j^{\; j \in 1..n} \} <: \{l_i : T_i^{\; i \in 1..n }\}$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1108 \DisplayProof && \text{S-RCDPERM}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1109 \end{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1110
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1111 S-RCDPERM を用いることで、終端フィールドだけではなく任意の位置のフィールドを削ることができる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1112
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1113 レコードの部分型は定義できたので、次は関数の部分型を定義していく。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1114 関数の部分型は以下 S-ARROW として定義できる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1115
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1116 \begin{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1117 \AxiomC{$ T_1 <: S_1$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1118 \AxiomC{$ S_2 <: T_2$}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1119 \BinaryInfC{$ S_1 \rightarrow S_2 <: T_1 \rightarrow T_2 $}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1120 \DisplayProof && \text{S-ARROW}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1121 \end{align*}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1122
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1123 前提の条件二つを見ると部分型の関係が逆転している。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1124 左側の条件は関数型自身の型と逆になっているため反変と言い、返り値の型は同じ向きであるため共変と言う。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1125 引数について考えた時、求める型よりも大きい型であれば明らかに安全に呼ぶことができるために関数の引数の型の方が上位型になる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1126 返り値については関数が返す型が部分型であれば上位型を返すことができるため、関数の返り値の方が部分型になる。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1127
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1128 別の見方をすると、型 $ S_1 \rightarrow S_2 $ の関数を別の型 $ T_1 \rightarrow T_2 $が期待される文脈で用いることが安全な時とは
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1129
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1130 \begin{itemize}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1131 \item 関数に渡される引数がその関数にとって想定外でない($ T_1 <: S_1$)
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1132 \item 関数が返す値も文脈にとって想定外でない($ S_2 <: T_2 $)
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1133 \end{itemize}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1134
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1135 という場合に限る。
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1136
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1137 % }}}
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1138
48
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1139 % {{{ 部分型と Continuation based C
46
3aeabd46d72b Wrote subtype
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
1140
28
36ce493604fb Add akasha result
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1141 \section{部分型と Continuation based C}
48
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1142 部分型を用いて Conituation based C の型システムを定義していく。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1143
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1144 まず、DataSegment の型から定義してく。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1145 DataSegment 自体はCの構造体によって定義されているため、レコード型として考えることができる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1146 例えばリスト~\ref{src:akasha-context}に示していた DataSegment の一つに注目する(リスト~\ref{src:type-ds})。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1147
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1148 \lstinputlisting[label=src:type-ds, caption=akashaContext の DataSegment である AkashaInfo] {src/type-ds.h}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1149 この AkashaInfo は $ \{ minHeight : unsigned\; int , maxHeight : unsigned \; int, akashaNode : AkashaNode*\}$ であると見なせる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1150 CodeSegment は DataSegment を引数に取るため、DataSegment の型は CodeSegment が要求する最低限の制約をまとめたものと言える。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1151
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1152 次に Meta DataSegment について考える。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1153 Meta DataSegment はプログラムに出現する DataSegment の共用体であった。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1154 これを DataSegment の構造体に変更する。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1155 こうすることにより、 Meta DataSegment はプログラム中に出現する DataSegment を必ず持つため、Meta DataSegment は任意の DataSegment の部分型となる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1156 もしくは各 DataSegment の全てのフィールドを含むような1つの構造体でも良い。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1157 第~\ref{chapter:cbc-type}章における Meta DataSegment はそのように定義している。
50
451c510825de Add natural deduction and curry-howard isomorphism
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 48
diff changeset
1158 なお、GearsOSでは DataSegment の共用体をプログラムで必要な数だか持つ実装になっている。
48
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1159
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1160 具体的な CbC における Meta DataSegment であるContext (リスト~\ref{src:type-mds})は、 DataSegment の集合を値として持っているために明らかに DataSegment よりも多くの情報を持っている。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1161 \lstinputlisting[label=src:type-mds, caption=CbC の Meta DataSegment である Context] {src/type-mds.h}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1162
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1163 部分型として定義するなら以下のような定義となる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1164
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1165 \begin{definition}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1166 Meta DataSegment の定義
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1167
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1168 \begin{align*}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1169 Meta \; DataSegment <: DataSegment_i^{i \in N} && \text{S-MDS}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1170 \end{align*}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1171 \end{definition}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1172
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1173 なお、$N$はあるプログラムに出現するデータセグメントの名前の集合であるとする。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1174
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1175 次に CodeSegment の型について考える。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1176 CodeSegment は DataSegment を DataSegment へと移す関数型とする。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1177
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1178 \begin{definition}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1179 CodeSegment の定義
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1180
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1181 \begin{align*}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1182 DataSegment \rightarrow DataSegment && \text{T-CS}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1183 \end{align*}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1184 \end{definition}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1185
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1186 そして Meta CodeSegmentは Meta DataSegment を Meta DataSegment へと移す関数となる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1187
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1188 \begin{definition}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1189 Meta CodeSegment の定義
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1190
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1191 \begin{align*}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1192 Meta \; DataSegment \rightarrow Meta \; DataSegment && \text{T-MCS}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1193 \end{align*}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1194 \end{definition}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1195
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1196 ここで具体的なコード(リスト~\ref{src:type-cs})と比較してみる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1197
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1198 \lstinputlisting[label=src:type-cs, caption=具体的なCbCにおける CodeSegment] {src/type-cs.c}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1199
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1200 CodeSegment \verb/getMinHeight/ は DataSegment \verb/Allocate/ と \verb/AkashaInfo/ を引数に取っている。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1201 現状は \verb/Context/ も継続のために渡しているが、本来ノーマルレベルからはアクセスできないために隠れているとする。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1202 その場合、引数の型は $ \{ allocate : Allocate , akashaInfo : AkahsaInfo\} $ となる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1203 また、返り値は構文的には存在していないが、軽量継続で渡す値は $ Context $ である。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1204 よって \verb/getMinHeight/ の型は $ \{ allocate : Allocate , akashaInfo : AkahsaInfo\} \rightarrow Context $ となる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1205 $ Context $ の型は Meta DataSegment なので、 subtype の S-ARROW より $Context $の上位型への変更ができる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1206
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1207 $ \{ allocat; : Allocate , akashaInfo : AkahsaInfo\} $ を $X$と置いて、\verb/getMinHeignt/ を $ X \rightarrow X $ とする際の導出木は以下である。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1208
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1209 \begin{prooftree}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1210 \AxiomC{}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1211 \RightLabel{S-REFL}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1212 \UnaryInfC{$ X <: X $}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1213 \AxiomC{}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1214 \RightLabel{S-MDS}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1215 \UnaryInfC{$ Conttext <: X$}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1216 \RightLabel{S-ARROW}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1217 \BinaryInfC{$ X \rightarrow Context <: X \rightarrow X$}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1218 \end{prooftree}
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1219
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1220 返り値部分を部分型として定義することにより、軽量継続先が上位型であればどの CodeSegment へと遷移しても良い。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1221 プログラムによっては遷移先は確定しているために部分型にする必要性は無いが、メタ計算やライブラリなどの遷移先が不定の場合は一度部分型として確定し、その後コンパイル時やランタイム時に包摂関係から具体型を導けば良い。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1222 例えばコンパイル時に解決すればライブラリの静的リンク時実行コード生成が行なえ、ランタイム時に解決すればネットワークを経由するプログラムとの接続初期化に利用できる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1223 例えば、プロトコルがバージョンを持つ場合に接続先のプログラムが利用しているプロトコルと互換性があるかの判断を Context どうしの部分型関係で判断できる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1224
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1225 また、stub のみに注目すると、stub は Context から具体的なDataSegment X を取り出す操作に相当し、S-ARROWの左側の前提のような振舞いをする。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1226 加えて、軽量継続する際に X の計算結果を Context に格納してから goto する部分を別の Meta CodeSegment として分離すると、S-ARROWの右側の前提のような振舞いを行なう。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1227 このようにノーマルレベルの CodeSegment の先頭と末尾にメタ計算を接続することによってノーマルレベルの CodeSegment が型付けできる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1228 型付けにDataSegment の集合としての Meta DataSegment が必須になるが、これは構造体として定義可能なためコンパイル時に生成することで CbC に組み込むことができる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1229
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1230 なお、メタ計算に利用する Meta DataSegment と Meta DataSegment も同様に型付けできる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1231 ここで興味深いのはあるレベルの CodeSegment は同レベルの DataSegment において型付けされるが、一つ上の階層から見ると、下の階層のDataSegmentとして一貫して扱えることにある。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1232 このようにメタ計算を階層化することにより、メタ計算で拡張された計算に対しても他のメタ計算が容易に適用できる。
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1233
623c90a21227 Wrote type.tex
atton <atton@cr.ie.u-ryukyu.ac.jp>
parents: 47
diff changeset
1234 % }}}