1 / 12

Type Reconstruction (II) - Algorithm W -

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

astro
Download Presentation

Type Reconstruction (II) - Algorithm W -

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Type Reconstruction (II)- Algorithm W - 20110205 CSE 김민철(id: rucatia) 6/4 POSTECH 무은재기념관307호

  2. 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 Γ :: = · | Γ , x : U , α type

  3. Rules for typing judgementΓ▷e:U Bottom up Algorithm W를 간단하게!

  4. 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)]

  5. Revised version of Gen, Spec rule 의도하지 않은 중복 방지! Top down Approach Polytype -> Monotype

  6. 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 W If W(Γ, e) = (S, A), then S·Γ ▷ e:A. S는 A에 영향을 주지 않는다! (A라는 type이 S가 Γ에 적용된 후에 얻어지기 때문!) • Completeness of Algorithm W

  7. 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 Body에서 x가 int임을 얻음 {int/α} Function body를 안 봐서 x의 타입은 그냥 임의로 α  ({int/α}·αA) 가 최종 type

  8. Algorithm W Γ e1 e2 • 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한다. ??? A1 S1·Γ A2 S2·A1 S2·S1·Γ S3·S2·A1 S3·A2

  9. 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, A1A2 = B1B2) = Unify(E, A1=B1, A2=B2) Unify(α = αint) E가 α를 가지고 있을 때를 생각해보자. Unify(αβ = βα, α = int)

  10. Algorithm W Γ e1 e2 • 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의 themost general type을 return한다! – Gen rule여러 개 ??? A1 S1·Γ A2 S2·S1·Γ x USE Gen(A1) Gen Rule Gen Rule ∀β.∀α.αβ ∀α.αβ αβ Gen Function

  11. 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 <빠뜨림이 없음>

  12. Thank you! Thanks for 류각사

More Related