attapl 輪講 ( 第4回 ) 続 dependent types

22
ATTAPL 輪輪 ( 輪 ) 輪 Dependent Types 2006/06/21 輪輪

Upload: olive

Post on 01-Feb-2016

57 views

Category:

Documents


0 download

DESCRIPTION

ATTAPL 輪講 ( 第4回 ) 続 Dependent Types. 2006/06/21 稲葉 一浩. 今日の内容. Dependent Types と多相型 Calculus of Construction (2.6) λ-cube (2.7) Dependent Types を使ったプログラミング DML (2.8). Calculus of Construction (CC). λLF の拡張. t::= x | λx:T.t | t t | all x:T.t - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

ATTAPL 輪講 ( 第4回 ) 続 Dependent Types

2006/06/21

稲葉 一浩

Page 2: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

今日の内容

ه Dependent Types と多相型ه Calculus of Construction (2.6)ه λ-cube (2.7)

ه Dependent Types を使ったプログラミングه DML (2.8)

Page 3: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Calculus of Construction (CC)

ه λLF の拡張t ::= x | λx:T.t | t t | all x:T.tT ::= X | Πx:T.T | T t | Prop | PrfK ::= * | Πx:T.K

Γ├ Prop :: *Γ├ Prf :: Πx:Prop.*

Γ├ T :: * Γ,x:T ├ t : Prop Γ├ all x:T.t : Prop

Γ├ T :: * Γ,x:T ├ t : Prop________ Γ├ Prf(all x:T.t) ≡ Πx:T.(Prf t) :: *

Page 4: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Calculus of Construction (CC)

ه おおまかな気持ちه 型 Prop を持つ term とは

命題ىデータ型ى

ه 型 Prf p を持つ term とは命題 p の証明ىデータ型 p のメンバى

Page 5: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

ه 自然数のエンコーディングه zero = λz.λs.z : X. (X→(X→X)→X)∀ه one = λz.λs. s z : X. (X→(X→X)→X)∀ه succ = λm.λz.λs. s (m s z) : 略

ه 自然数というデータ型を項 nat として表現ه nat : Prop

all a:Prop. all z:Prf a. all s:Prf a→Prf a. aىه Prf nat

≡ Πa:Prop. Πz:Prf a Πs:Prf a→Prf a. Prf a

Page 6: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

ه Prf nat 型をもつ項ه zero : Prf nat

λa:Prop. λz:Prf a. λs:Prf a→Prf a. zى

ه succ : Prf nat → Prf nat.λn:Prf nat. λa:Propى

λz:Prf a. λs:Prf a→Prf a. s (n a z s)

Page 7: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

CC のできることه さまざまな Abstraction

ه λx:T. t 項に依存する項 λ→ふつうのى λ 抽象

ه Πx:T. T 項に依存する型 λLFDependent Typesى

ه ΛX::*.t 型に依存する項 Fه ∀X::*.T 型に依存する型 F

∀多相型 id = ΛX::*.λx:X. x : X.X→Xىه ΛX::K. t 高階の型に依存する項 Fωه ∀X::K. T 高階の型に依存する型 Fω

高階の多相型ى

λx:Prop→Prop. t 等

all x:Prop. T

λx:Prop. t

all x:Prop→Prop. T 等

Page 8: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

論理結合子と推論規則の表現

ه and = λp:Prop. λq:Prop. all a:Prop. all pair:Prf p→Prf q→Prf a. a

ه left : Prf(and p q) → Prf(p)ه λx:Prf(and p q). (x p) (λa.λb.a)

ه right: Prf(and p q) → Prf(q)ه λx:Prf(and p q). (x q) (λa.λb.b)

Page 9: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

論理結合子と推論規則の表現

ه eq = λa:Prop. λx:Prf a. λy:Prf a. all p:Prf a→Prop. all h:Prf (p x). p y

ه eqRefl : Πa:Prop. Πx:Prf a. Prf (eq a x x)ه λa:Prop. λx:Prf a. λp:Prf a→Prop. λh:Prf(p x). h

ه eqSym : Πa:Prop. Πx:Prf a. Πy:Prf a. Prf (eq a x y) → Prf (eq a y x)

ه eqRefl : 略

Page 10: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

いろいろ

ه Algorithmic Typingه 67 ページ

ه Strong Normalization of Well-Typed Termsه Fω に帰着して証明するなど

ه CC + Sigma Typesه T ::= ... | Σx:S.T

」 ×ى ex y:T.t : Prop 」 「 Prf(ex y:T.t) ≡ Σy:T.Prf t 」」 ○ى σy:Prf s.t : Prop 」 「 Prf(σy:Prf s.t)≡Σy:Prf s.Prf t 」

Page 11: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Calculus of Inductive Construction

ه さっきのやり方では、「数学的帰納法」が「証明」できないه 以下の型をもつ term natInd が構成できな

ه natInd : Πp:Prf nat→Prop. Prf (p zero) →Πx:Prf nat. (Prf (p x) → Prf (p succ(x)) →Πx:Prf nat. Prf (p x)

Page 12: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

CIC

ه プリミティブとして、帰納法を表す term を追加ه nat:Prop

zero : Prf natىى succ : Prf nat → Prf nat

ه natInd : Πp:Prf nat→Prop. Prf (p zero) →Πx:Prf nat. (Prf (p x) → Prf (p succ(x)) →Πx:Prf nat. Prf (p x)

ه natInd p hz hs zero ≡ hzه natInd p hz hs (succ n) ≡ hs n (nadInt p hz hs n)

自動的に宣言

Page 13: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

CIC

ه 任意の帰納的な Prop に対しても同様ه list : Prop

nil: Prf list cons: Prf nat → Prf list → Prf list

ه listInd : Πp:Prf list→Prop. Prf (p nil) → Πx:Prf nat. Πy:Prf list. (Prf (p y) → Prf (p (cons x y)) →Πx:Prf list. Prf (p x)

ه listInd p hn hc nil ≡ hn

ه listInd p hn hc (cons x y) ≡hc x (listInd p hn hc y)

自動的に宣言

Page 14: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

注: 帰納法と再帰関数

ه Strong Normalization を保証する言語では、任意の再帰関数は定義できない(停止しない可能性があるため)

ه CIC では、 Induction term を使ってある程度の再帰関数は記述できる

Page 15: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Coq (Barras et al., 1997)

ه CIC に基づくه T ::= .. | Prop | Setه CC の Prop を「 Prop 」と「 Set 」に分割

命題として使うところではى Prop

データ型として使うところではى Set を使うあとでى Prop 部分を除いて Set 部分だけ残した

プログラムを抽出すると、普通の関数型言語のプログラムが抽出できる

Page 16: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

λ-cube

ه さまざまな Abstraction

F

λ→

Fω CC

λP

Page 17: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Pure Type Systems

t ::= s | x| λx:t.t | t t | Πx:t.ts ::= * | □

Γ├ *:□ x:t ∈ Γ Γ├ x:t

Γ├ S : * Γ,x:S├ t:T Γ├λx:S.t : Πx:S.T

Γ├t1 :Πx:S.T Γ├t2:S Γ├t1 t2 : [x→t2]T

Γ├t:T T≡S Γ├S:s Γ├ t: S

Γ├ S : si Γ,x:S ├ T : sj Γ├Πx:S.T : sj

where (si, sj) ∈ {(*,*)} λ←

{(*,*) (*,□)} λP {(*,*) (□, *)} F {(*,*) (□, *), (□,□)} Fω {(*,*) (*,□) (□, *), (□,□)} CC

Page 18: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

今日の内容

ه Dependent Types と多相型ه Calculus of Construction (2.6)ه λ-cube (2.7)

ه Dependent Types を使ったプログラミングه DML (2.8)

Page 19: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Dependent Types を導入した言語

ه 古くはه Pebble (Lampson and Burstall, 1988)

ه Quest(Cardelli and Longo, 1991)

ه 最近ではه Cayenne (Augustsson, 1998)

Haskell ベースى

ه Dependent ML (Xi and Pfenning, 1998)SML ベースىCaml ベースの実装 (de Caml) もありى

Page 20: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

問題点と解決策ه Dependent Types の型チェックは term の equality 検査が必要ه 一般の言語では、 term の reduction は止まらないことがあるه 停止性は決定不能

ه Cayenneه 任意の term への依存を許すه 十分長い時間走らせて計算が止まらなければ型エラー

ه Dependent MLه 依存できる term を制限して、

効率的に equality を決定可能な範囲に限る

Page 21: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Dependent ML (Simplified)

ه 依存できる term を、整数の線形な部分集合に限る

I ::= int | {x:I | P}P ::= P ∧ P | i≦i

i ::= x | q | qi | i + i (q∈Z)t ::= x | λx:I.t | λx:T.t | t[i] | t t

T ::= X | Πx:I.T | T→T

Page 22: ATTAPL 輪講  ( 第4回 ) 続  Dependent Types

Dependent ML 例

ه append

ه brake