view slide.html @ 14:43c56be5f779 default tip

Fix typo
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Sat, 24 May 2014 11:30:42 +0900
parents 37a7b8c31a0c
children
line wrap: on
line source

<!DOCTYPE HTML>

<html lang="Japanese">
<head>
	<title>Agda 入門</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=1274, user-scalable=no">
	<meta name="generator" content="Slide Show (S9)">
	<meta name="author" content="Yasutaka Higa">
	<link rel="stylesheet" href="themes/ribbon/styles/style.css">
	<style>
		#Cover H2 {
			color:#FFF;
			text-align:center;
			font-size:70px;
			}
	</style>
</head>
<body class="list">
	<header class="caption">
		<h1>Agda 入門</h1>
		<p>Yasutaka Higa</p>
	</header>
	<div class="slide bg" id="Cover"><div>
		<section>
			<header>
				<h2>Agda 入門</h2>
			</header>
			
			<img src="pictures/" alt="">
			<!-- © John Carey, http://fiftyfootshadows.net/ -->
		</section>
	</div></div>

<!-- todo: add slide.classes to div -->
<!-- todo: create slide id from header? like a slug in blogs? -->

<div class="slide" id="2"><div>
		<section>
			<header>
				<h1 id="section">このセミナーの目的</h1>
			</header>
			<!-- === begin markdown block ===

      generated by markdown 1.1.1 on Ruby 2.1.2 (2014-05-08) [x86_64-darwin13.0]
                on 2014-05-24 11:29:40 +0900 with Markdown engine kramdown (1.3.3)
                  using options {}
  -->

<!-- _S9SLIDE_ -->

<ul>
  <li>証明支援系の言語である Agda の入門を目的としています</li>
  <li>具体的には
    <ul>
      <li>Agda による証明の方法を知る</li>
      <li>実際に自然数に対する演算の証明を追う</li>
    </ul>
  </li>
  <li>ことをしていきます</li>
</ul>



		</section>
</div></div>

<div class="slide" id="3"><div>
		<section>
			<header>
				<h1 id="agda-">Agda とはどういう言語なのか</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>証明支援系と呼ばれる分野の言語です
    <ul>
      <li>他には Coq などがあります</li>
    </ul>
  </li>
  <li>Haskell で実装されています</li>
  <li>型が非常に強力で表現力が高いです
    <ul>
      <li>命題や仕様を表す論理式を型として定義することができます</li>
      <li>例えば 1 + 1 = 2 とか</li>
    </ul>
  </li>
</ul>



		</section>
</div></div>

<div class="slide" id="4"><div>
		<section>
			<header>
				<h1 id="curry-howard-isomorphism">型と証明との対応 : Curry-Howard Isomorphism</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>Agda における証明は
    <ul>
      <li>証明したい命題 == 関数の型</li>
      <li>命題の証明     == 関数の定義</li>
    </ul>
  </li>
  <li>として定義します</li>
  <li>関数と命題の対応を Curry-Howard Isomorphism と言います</li>
</ul>



		</section>
</div></div>

<div class="slide" id="5"><div>
		<section>
			<header>
				<h1 id="section-1">命題と定義, 仕様と実装</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>どうしてプログラムで証明できるかというと</li>
  <li>(命題 と 定義) は (仕様 と 実装) のように対応します
    <ul>
      <li>int chars_to_int(char * chars)</li>
      <li>つまり char * から int は作れる、という命題に対応している</li>
      <li>実装は { itoa(chars) }</li>
    </ul>
  </li>
  <li>char * から int は作れる、という仕様(型, 命題)は</li>
  <li>atoi という実装(定義)により証明された</li>
</ul>



		</section>
</div></div>

<div class="slide" id="6"><div>
		<section>
			<header>
				<h1 id="agda--1">Agda をはじめよう</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>emacs に agda-mode があります</li>
  <li>module filename where</li>
  <li>を先頭に書く必要があります</li>
  <li>証明を定義していく</li>
  <li>C-c C-l で型チェック(証明チェック)ができます</li>
</ul>



		</section>
</div></div>

<div class="slide" id="7"><div>
		<section>
			<header>
				<h1 id="peano-arithmetic">自然数の定義 : Peano Arithmetic</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>自然数 0 が存在する</li>
  <li>任意の自然数 x にはその後続数 S (x) が存在する</li>
  <li>0 より前の自然数は存在しない</li>
  <li>異なる自然数は異なる後続数を持つ
    <ul>
      <li>x != y のとき S(x) != S(y) となる</li>
    </ul>
  </li>
  <li>0 が性質を満たし、a も性質を満たせばその後続数も性質を満たすとき、すべての自然数はその性質を満たす</li>
</ul>



		</section>
</div></div>

<div class="slide" id="8"><div>
		<section>
			<header>
				<h1 id="agda--2">Agda における自然数の定義</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>data 型を使います</li>
</ul>

<pre><code>  data Int : Set where
    O : Int
    S : Int -&gt; Int
</code></pre>
<ul>
  <li>Int は O か、 Int に S がかかったもののみで構成される</li>
  <li>Set は組込みで存在する型で、”成り立つ”と考えてもらうと良いです。</li>
</ul>



		</section>
</div></div>

<div class="slide" id="9"><div>
		<section>
			<header>
				<h1 id="section-2">自然数の例</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>0 = O</li>
  <li>1 = S O</li>
  <li>2 = S (S O)</li>
  <li>5 = S (S (S (S (S O))))</li>
</ul>



		</section>
</div></div>

<div class="slide" id="10"><div>
		<section>
			<header>
				<h1 id="section-3">自然数に対する演算の定義</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>x と y の加算 : x にかかっている S の分だけ S を y に適用する</li>
  <li>
    <p>x と y の乗算 : x にかかっている S の分だけ y を 0 に加算する</p>
  </li>
  <li>Agda tips : 不明な項を ? と書くこともできます。 goal と呼びます</li>
  <li>その状態で C-c C-l するとその項の型が分かります</li>
</ul>



		</section>
</div></div>

<div class="slide" id="11"><div>
		<section>
			<header>
				<h1 id="agda--3">Agda における自然数に対する演算の定義</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  infixl 30 _+_
  _+_ : Int -&gt; Int -&gt; Int
  x + O     = x
  x + (S y) = S (x + y)
</code></pre>

<ul>
  <li>Agda tips : C-c C-n すると式を評価することができます</li>
  <li>(S O) + (S (S O)) などしてみましょう</li>
</ul>



		</section>
</div></div>

<div class="slide" id="12"><div>
		<section>
			<header>
				<h1 id="agda-syntax">Agda における関数定義のSyntax</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>Agda において、関数は
    <ul>
      <li>_ + _ : Int -&gt; Int -&gt; Int</li>
      <li>関数名                      : 型</li>
      <li>関数名 引数はスペース区切り = 関数の定義や値</li>
    </ul>
  </li>
  <li>のように定義します</li>
  <li>中置関数は、引数があるべきところに _ を書くことでできます</li>
</ul>



		</section>
</div></div>

<div class="slide" id="13"><div>
		<section>
			<header>
				<h1 id="agda--4">Agda で複数の引数がある関数の型</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>_ + _ : Int -&gt; Int -&gt; Int</li>
  <li>
    <p>func : A -&gt; (A -&gt; B) -&gt; B</p>
  </li>
  <li>引数の型 -&gt; 返り値の型</li>
  <li>-&gt; の結合は右結合です。なので括弧を付けると以下のようになります
    <ul>
      <li>A -&gt; ((A -&gt; B) -&gt; B)</li>
    </ul>
  </li>
  <li>右結合のため、A を受けとって ((A -&gt; B) -&gt; B) を返す、とも読めます</li>
</ul>



		</section>
</div></div>

<div class="slide" id="14"><div>
		<section>
			<header>
				<h1 id="section-4">パターンマッチ</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>Agda においてはデータ型は引数で分解することができます</li>
  <li>ある型に続している値が、どのコンストラクタにおいて構成されたかをパターンで示せます</li>
  <li>Int は O か Int に S が付いたもの、でした
    <ul>
      <li>x + O     = x</li>
      <li>x + (S y) = S (x + y)</li>
    </ul>
  </li>
  <li>関数名 (引数のパターン) = 定義</li>
</ul>



		</section>
</div></div>

<div class="slide" id="15"><div>
		<section>
			<header>
				<h1 id="section-5">もういちど : 自然数に対する演算の定義</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  infixl 30 _+_
  _+_ : Int -&gt; Int -&gt; Int
  x + O     = x
  x + (S y) = S (x + y)
</code></pre>
<ul>
  <li>中置、関数、パターンマッチが使われています</li>
  <li>infixl は左結合を意味します。数値は結合強度です</li>
</ul>



		</section>
</div></div>

<div class="slide" id="16"><div>
		<section>
			<header>
				<h1 id="section-6">これから証明していきたいこと</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>加法の交換法則 : (x + y) = (y + x)</li>
  <li>
    <p>加法の結合法則 : (x + y) + z = x + (y + z) &lt;- 目標ライン</p>
  </li>
  <li>乗法の交換法則 : (x * y) = (y * x)</li>
  <li>乗法の結合法則 : (x * y) * z = x * (y * z)</li>
</ul>



		</section>
</div></div>

<div class="slide" id="17"><div>
		<section>
			<header>
				<h1 id="section-7">‘等しい’ ということ</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>‘等しい’という型 _ ≡ _ を data で定義します。</li>
</ul>

<pre><code>  data _≡_  {A : Set} : A -&gt; A -&gt; Set where
    refl  : {x : A} -&gt; x == x
</code></pre>

<ul>
  <li>defined : Relation.Binary.PropositionalEquality in Standard Library</li>
</ul>



		</section>
</div></div>

<div class="slide" id="18"><div>
		<section>
			<header>
				<h1 id="section-8">等しさを保ったままできること</h1>
			</header>
			<!-- _S9SLIDE_ -->

<p>等しさを保ったまま変換する関数を作ると良い</p>

<ul>
  <li>sym   : {A : Set} {x y : A} -&gt; x ≡ y -&gt; y ≡ x</li>
  <li>cong  : {A B : Set} {x y : A} -&gt; (f : A -&gt; B) -&gt; x ≡ y -&gt; f x ≡ f y</li>
  <li>
    <p>trans : {A : Set} {x y z : A} -&gt; x ≡ y -&gt; y ≡ z -&gt; x ≡ z</p>
  </li>
  <li>Agda tips : 記号は \ の後に文字列を入れると出てきます。 ‘≡’ は “\ ==”</li>
</ul>



		</section>
</div></div>

<div class="slide" id="19"><div>
		<section>
			<header>
				<h1 id="section-9">‘同じもの’とは</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>項なら同じ項
    <ul>
      <li>term : (A : Set) -&gt; (a : Set) -&gt; a ≡ a</li>
      <li>term A a = refl</li>
    </ul>
  </li>
  <li>関数なら normal form が同じなら同じ
    <ul>
      <li>lambda-term : (A : Set)  -&gt; (\ (x : A) -&gt; x) ≡ (\ (y : A) -&gt; y)</li>
      <li>lambda-term A = refl</li>
    </ul>
  </li>
  <li>関数による式変形は等しいものとして扱います</li>
</ul>



		</section>
</div></div>

<div class="slide" id="20"><div>
		<section>
			<header>
				<h1 id="section-10">単純な証明 : 1 + 1 = 2</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>型として (S O) + (S O) ≡ (S (S O)) を定義</li>
  <li>証明を書く</li>
  <li>‘同じもの’ の refl でおしまい</li>
  <li>自明に同じものになるのなら refl で証明ができます</li>
</ul>



		</section>
</div></div>

<div class="slide" id="21"><div>
		<section>
			<header>
				<h1 id="section-11">交換法則を型として定義する</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>≡を用いて
    <ul>
      <li>(x : Int) -&gt; (y : Int) -&gt; x + y ≡ y + x</li>
    </ul>
  </li>
  <li>引数は (名前 : 型) として名前付けできます</li>
</ul>



		</section>
</div></div>

<div class="slide" id="22"><div>
		<section>
			<header>
				<h1 id="section-12">交換法則を証明していく</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  add-sym : (x y : Int)  -&gt; x + y ≡ y + x
  add-sym    O     O  = refl
  add-sym    O  (S y) = cong S (add-sym O y)
  add-sym (S x)    O  = cong S (add-sym x O)
  add-sym (S x) (S y) = ?
</code></pre>



		</section>
</div></div>

<div class="slide" id="23"><div>
		<section>
			<header>
				<h1 id="o-o-">O, O の場合</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>add-sym    O     O  = refl</li>
  <li>両方ともO の時、証明したい命題は O + O ≡ O + O</li>
  <li>_ + _ の定義の x + O  = x より</li>
  <li>O ≡ O を構成したい</li>
  <li>refl によって構成する</li>
  <li>refl O と考えてもらえると良い</li>
</ul>



		</section>
</div></div>

<div class="slide" id="24"><div>
		<section>
			<header>
				<h1 id="o--s-">片方が O, 片方に S が付いている場合</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>add-sym    O  (S y) = cong S (add-sym O y)</li>
  <li>式的には O + (S y) ≡ (S y) + O</li>
  <li>_ + _ の定義 x + (S y) = S (x + y) より</li>
  <li>O + (S y) ≡ S (O + y)</li>
  <li>O と y を交換して O + (S y) ≡ S (y + O)</li>
  <li>つまり y と O を交換して S をかけると良い</li>
  <li>交換して S をかける -&gt; cong S (add-sym O y)</li>
</ul>



		</section>
</div></div>

<div class="slide" id="25"><div>
		<section>
			<header>
				<h1 id="trans-">trans による等式変形</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>add-sym (S x) (S y) の時は1つの関数のみで等しさを証明できない</li>
  <li>
    <p>等しさを保ったまま式を変形していくことが必要になります</p>
  </li>
  <li>add-sym (S x) (S y) = trans (f : a ≡ b) (g : b ≡ c)
    <ul>
      <li>trans (refl) ?</li>
      <li>trans (trans refl (cong S (add-sym (S x) y)) ?</li>
    </ul>
  </li>
</ul>



		</section>
</div></div>

<div class="slide" id="26"><div>
		<section>
			<header>
				<h1 id="reasoning-">≡-reasoning による等式変形</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>trans が何段もネストしていくと分かりづらい</li>
  <li>≡-reasoning という等式変形の構文が Standard Library にあります</li>
  <li>Agda では見掛け上構文のような関数をAgdaでは定義できます</li>
</ul>

<pre><code>  begin
    変換前の式
      ≡⟨ 変換する関数 ⟩
    変換後の式

</code></pre>



		</section>
</div></div>

<div class="slide" id="27"><div>
		<section>
			<header>
				<h1 id="reasoning--1">≡-reasoning による最初の定義</h1>
			</header>
			<!-- _S9SLIDE_ -->


<pre><code>  add-sym (S x) (S y) = begin
      (S x) + (S y)
    ≡⟨ ? ⟩
      (S y) + (S x)

</code></pre>



		</section>
</div></div>

<div class="slide" id="28"><div>
		<section>
			<header>
				<h1 id="section-13">交換法則の証明 : + の定義による変形</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>_ + _ の定義である x + (S y) = S (x + y) により変形</li>
</ul>

<pre><code>  add-sym (S x) (S y) = begin
      (S x) + (S y)
    ≡⟨ refl ⟩
      S (S x + y)
    ≡⟨ ? ⟩
      (S y) + (S x)

</code></pre>



		</section>
</div></div>

<div class="slide" id="29"><div>
		<section>
			<header>
				<h1 id="cong--add-sym-">cong と add-sym を使って交換</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>S が1つ取れたのでadd-symで交換できる</li>
  <li>add-sym で交換した後に cong で S がかかる</li>
</ul>

<pre><code>    S ((S x) + y)
  ≡⟨ cong S (add-sym (S x) y) ⟩
    S ((y + (S x)))
</code></pre>



		</section>
</div></div>

<div class="slide" id="30"><div>
		<section>
			<header>
				<h1 id="s">加法の時に左側からSを移動させられない</h1>
			</header>
			<!-- _S9SLIDE_ -->


<ul>
  <li>加法の定義は
    <ul>
      <li>x + (S y) = S (x + y)</li>
    </ul>
  </li>
  <li>left-operand にかかっている S を移動させる方法が無い</li>
  <li>たしかに ? のについて
    <ul>
      <li>S (y + S x) ≡ S y + S x</li>
    </ul>
  </li>
  <li>にあてはまるものを入れてくれ、と出てきている</li>
</ul>



		</section>
</div></div>

<div class="slide" id="31"><div>
		<section>
			<header>
				<h1 id="left-operand-s">left-operand からSを操作する証明を定義</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  left-increment : (x y : Int) -&gt; (S x) + y ≡ S (x + y)
  left-increment x y = ?
</code></pre>

<ul>
  <li>Agda tips : goal の上で C-c C-c で引数のパターン分け
    <ul>
      <li>例えば y にのみ適用してみる</li>
    </ul>
  </li>
  <li>Agda tips : goal の上で C-c C-a で証明を探してくれる</li>
</ul>



		</section>
</div></div>

<div class="slide" id="32"><div>
		<section>
			<header>
				<h1 id="left-operand-s-1">left-operand からSを移動させる</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>left-increment は (S x) + y ≡ S (x + y) なので逆にして使う</li>
</ul>

<pre><code>    ...
    S ((S x) + y)
      ≡⟨ sym (left-increment x (S y)) ⟩
    (S y) + (S x)

</code></pre>



		</section>
</div></div>

<div class="slide" id="33"><div>
		<section>
			<header>
				<h1 id="x--y--y--x">加法の交換法則 : (x + y) = (y + x)</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  add-sym (S x) (S y) = begin
      (S x) + (S y)
    ≡⟨ refl ⟩
      S ((S x) + y)
    ≡⟨ cong S (add-sym (S x) y) ⟩
      S (y + (S x))
    ≡⟨ (sym (left-increment y (S x))) ⟩
      (S y) + (S x)

</code></pre>



		</section>
</div></div>

<div class="slide" id="34"><div>
		<section>
			<header>
				<h1 id="x--y--z--x--y--z">加法の結合法則 : (x + y) + z = x + (y + z)</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>次に結合法則を証明します</li>
  <li>手順は同じです
    <ul>
      <li>≡ で証明したい式を定義</li>
      <li>定義に証明を書く</li>
      <li>必要ならば等式を変形していく</li>
    </ul>
  </li>
  <li>ちなみに x + y + z は infixl なので ((x + y) + z) となります</li>
</ul>



		</section>
</div></div>

<div class="slide" id="35"><div>
		<section>
			<header>
				<h1 id="agda--5">Agda による証明方法のまとめ</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>関数の型を命題、関数の定義を証明とする</li>
  <li>等しさを証明するには等しいという型を定義する</li>
  <li>等しさを保ったまま式を変形していくことにより等価性を証明できる
    <ul>
      <li>trans, reasoning</li>
    </ul>
  </li>
  <li>C-c C-l により型のチェックが成功すれば証明終了</li>
</ul>



		</section>
</div></div>

<div class="slide" id="36"><div>
		<section>
			<header>
				<h1 id="section-14">乗法の定義</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  infixl 40 _*_
  _*_ : Int -&gt; Int -&gt; Int
  n *    O  = O
  n * (S O) = n
  n * (S m) = n + (n * m)
</code></pre>

<ul>
  <li><em>+</em> よりも結合強度を上げるといわゆる自然数の演算</li>
</ul>



		</section>
</div></div>

<div class="slide" id="37"><div>
		<section>
			<header>
				<h1 id="x--y--y--x-1">乗法の交換法則 : (x * y) = (y * x)</h1>
			</header>
			<!-- _S9SLIDE_ -->

<pre><code>  mult-sym : (x y : Int) -&gt; x * y ≡ y * x
</code></pre>

<p>途中で</p>

<pre><code>  (x y : Int) -&gt; (S x) * y ≡ y + (x * y)
</code></pre>

<p>が必要になることが分かる</p>



		</section>
</div></div>

<div class="slide" id="38"><div>
		<section>
			<header>
				<h1 id="agda">Agdaにおいて何ができるのか</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>証明の正しさを対話的に教えてくれる
    <ul>
      <li>それに必要な証明が結果的に分かることもある</li>
    </ul>
  </li>
  <li>今回は Int に対する演算だった
    <ul>
      <li>lambda-term に落とせれば Agda で証明していける</li>
    </ul>
  </li>
  <li>他にも命題論理の証明などがある</li>
  <li>プログラミング言語そのものに対するアプローチ
    <ul>
      <li>lambda-term の等価性によってリファクタリングなど</li>
    </ul>
  </li>
</ul>



		</section>
</div></div>

<div class="slide" id="39"><div>
		<section>
			<header>
				<h1 id="section-15">良くあるエラー</h1>
			</header>
			<!-- _S9SLIDE_ -->

<ul>
  <li>parse error
    <ul>
      <li>スペースある無しで意味が変わります</li>
      <li>A: Set &lt;- NG</li>
      <li>A : Set &lt;- OK</li>
      <li>A: という term がありえるから</li>
    </ul>
  </li>
  <li>型が合わない   -&gt; 赤で警告されます</li>
  <li>情報が足りない -&gt; 黄色で警告されます</li>
</ul>

<!-- vim: set filetype=markdown.slide: -->
<!-- === end markdown block === -->

		</section>
</div></div>


	<!--
		To hide progress bar from entire presentation
		just remove “progress” element.
		-->
	<div class="progress"><div></div></div>
	<script src="scripts/script.js"></script>
	<!-- Copyright © 2010–2011 Vadim Makeev, http://pepelsbey.net/ -->
</body>
</html>