type reconstruction (ii) - algorithm w -

12
Type Reconstruction (II) - Algorithm W - 20110205 CSE 김김김 (id: rucatia) 6/4 POSTECH 무무무무무무 307 무

Upload: astro

Post on 23-Feb-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Type Reconstruction (II) - Algorithm W -. 20110205 CSE 김민철 (id: rucatia ) 6/4 POSTECH 무은재기념관 307 호. Implicit let-polymorphism. Type Annotation X Type Abstraction X Type Application X. A :: = A  A | α U :: = A | ∀ α .U e :: = x | λ x.e | e e | let x = e in e - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Type Reconstruction (II) - Algorithm W -

Type Reconstruction (II)

- Algorithm W -20110205 CSE 김민철 (id: rucatia)

6/4 POSTECH 무은재기념관 307 호

Page 2: Type Reconstruction (II) - Algorithm W -

Implicit let-polymorphism

Type Annotation X Type Abstraction X Type Application X

A :: = A A | αU :: = A | ∀α.Ue :: = x | λx.e | e e | let x = e in eΓ :: = · | Γ , x : U , α type

Page 3: Type Reconstruction (II) - Algorithm W -

Rules for typing judgement Γ▷e:U

Bottom upAlgorithm W 를 간단하게 !

Page 4: Type Reconstruction (II) - Algorithm W -

Type reconstruction

Implicit let-polymorphism 에서 type reconstruction! A :: = A A | α U :: = A | ∀α.U e :: x | λx.e | e e | let x = e in e Γ :: = · | Γ , x : U [ 모든 type 이 유효하다는 Assumption!

(No Γ, α type)]

Page 5: Type Reconstruction (II) - Algorithm W -

Revised version of Gen, Spec rule

Top down Ap-proach

의도하지 않은중복 방지 !

Polytype -> Monotype

Page 6: Type Reconstruction (II) - Algorithm W -

Algorithm W

W(Γ, e) = (S, A) [A(monotype) 인 이유는 W 를 보다 편하게 하기 위해서 ] S :: = id | {A/α} | S o S [{U/α} 가 될 수 없는 이유는 α 는 monotype 이니까 ] Γ + x:U = (Overriding of type binding!)

Soundness of Algorithm WIf W(Γ, e) = (S, A), then S·Γ ▷ e:A.S 는 A 에 영향을 주지 않는다 ! (A 라는 type 이 S 가 Γ 에 적용된 후에 얻어지기 때문 !)

Completeness of Algorithm W

Page 7: Type Reconstruction (II) - Algorithm W -

Algorithm W

1. W(Γ, x) = (id, {/}·A) if ∀.A ∈ Γ , fresh Why id? : α 가 모두 Γ 에 있기 때문에 Substitution 은 따로 없다 .({A/α}∀α.U = ∀α.U 가 그렇죠 .)

2. W(Γ, λx.e) = let (S, A) = W(Γ+x:α, e) in (S, (S·α) A)fresh α

Soundness of W 에 의해 , typing context 는 S·Γ+x:S·α, expression e 는 A type이 됨 !Ex> λx.x+1 을 알아봅시다 . λx. x+1

Function body 를 안 봐서 x 의 타입은 그냥 임의로 αBody 에서 x 가 int 임을 얻음 {int/α}

({int/α}·α A) 가 최종 type

Page 8: Type Reconstruction (II) - Algorithm W -

Algorithm W

3. W(Γ, e1 e2) = let (S1, A1) = W(Γ, e1) in

let (S2, A2) = W(S1·Γ, e2) in

let S3 = Unify(S2·A1 = A2 α) in fresh α

(S3 o S2 o S1, S3·α)

Application Rule 을 잘 생각해보면 ?Unify 는 안의 Equation 의 type 이 같도록 하는 Substitution 을 return 한다 .

Γ

S1·Γ A1 ???

e1 e2

S2·S1·Γ S2·A1 A2

S3·S2·A1 S3·A2

Page 9: Type Reconstruction (II) - Algorithm W -

Unify Function

type equations E ::= · | A = A Unify(E) = S [Substitution S 를 적용하면 type equation E 는 true S·E]

Unify(·) = id

Unify(E, α = A) = if α = A then Unify(E)

else if α ∈ ftv(A) then fail

else Unify({A/α}·E) o {A/α}

Unify(E, A1A2 = B1B2) = Unify(E, A1=B1, A2=B2)

Unify(α = α int)

E 가 α 를 가지고 있을 때를 생각해보자 .Unify(αβ = βα, α =

int)

Page 10: Type Reconstruction (II) - Algorithm W -

Algorithm W

4. W(Γ, let x = e1 in e2) = let (S1, A1) = W(Γ, e1) in

let (S2, A2) = W(S1·Γ+x:(A1), e2) in

(S2 o S1, A2)

A1(e1 의 type) 이 monotype 이므로 , x 에 그대로 쓰면 polymorphism 의 의도와 X따라서 이를 Generalize 하는 것이 필요하다 !Gen Function 은 A1 의 the most general type 을 return 한다 ! – Gen rule 여러 개

Γ

S1·Γ A1 ???

e1 e2

S2·S1·Γ A2

Gen(A1)

xUSE

αβ ∀α.αβ ∀β.∀α.αβGen Rule Gen Rule

Gen Function

Page 11: Type Reconstruction (II) - Algorithm W -

Completeness of Algorithm W

If W(Γ, e) = (S, A), then S·Γ ▷ e:A. (Soundness of W) 여기에서 monotype A 는 free type variable 들을 가지고 있기 때문에 , A 를

Generalize 하기만 하면 e 의 most general type 을 구할 수 있음 . 즉 , If W(Γ, e) = (S, A), then S·Γ ▷ e:A, and S·Γ ▷ e:(A).

Suppose A’ = (A). If · ▷ e:A, then W(·, e) successfully returns A’. Completeness of W < 빠뜨림이 없음 >

Page 12: Type Reconstruction (II) - Algorithm W -

Thank you!Thanks for 류각사