polymorphic type systemdreameye/pl/slide/pl11.pdf · 2011-05-19 · 5.8 !!타입!! !" /...

38
다형 타입 시스템 Polymorphic Type System

Upload: others

Post on 11-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

다형 타입 시스템Polymorphic Type System

Page 2: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

개괄

Page 3: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

리뷰: 타입 시스템• 타입오류가 있는 프로그램 안전하게 걸러내기

• ‘논리 시스템’ + ‘유추알고리즘’

• 정확도에 따라 여러가지

• ex) 단순 타입 시스템

타입오류를 가진 프로그램들

타입시스템 1 이 걸러내는 프로그램들

타입시스템 2 가걸러내는 프로그램들

전체 프로그램

Page 4: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

단순 타입 시스템

182 ! !!! !"

let-!! !! !"!let-polymorphic type system#!! let-"" "!" $"!

!##.

E ! n

| x

| !x.E

| E E

| let x = E in E

| rec f !x.E

| E + E

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

156 ! !!! !"

5.7.1 !! !! !! !!

"! "!! !"". !# "#!$ "!!%.

"!!! "#!!$ "## !# !"#" $#!! "!!!&## $

$!! !$ "#!"". "!!! "#!$ !#" !"" %" #". !"

! ! !$ "#!' "!!! "!"!! !#" "!& !'#(! )!#

% *! ## "!$".

!)! "#!" "!& "!!% !!#, (& "!# %! *! )#, $

$ "%"! #!$ !!!!! !%. +#', ! !$ "#!" , "%&!

'(" $*". !$ % *! "#!"'(" $*". !# ## #% "&$

#$, !%$ "$! "!& !($ $!!*".

!# ! ! "!!! &%" $# +(! "#! !%! &"%"". "$

!"$"! "-! %%! "!# %"":

Type ! ! " primitive type

| ! ! ! function type

!$&! ""#!

! " E : !

"".

!! !! !!type environment(! "%%! )"% !#$ "## *":

! # Idfin

! Type

Notation 8 !# $, !$ "%" !{x $! !} "'%" ! + x : !!! !*".

x $! ! # ! "'$ x : ! # !!! !*". !

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

156 ! !!! !"

5.7.1 !! !! !! !!

"! "!! !"". !# "#!$ "!!%.

"!!! "#!!$ "## !# !"#" $#!! "!!!&## $

$!! !$ "#!"". "!!! "#!$ !#" !"" %" #". !"

! ! !$ "#!' "!!! "!"!! !#" "!& !'#(! )!#

% *! ## "!$".

!)! "#!" "!& "!!% !!#, (& "!# %! *! )#, $

$ "%"! #!$ !!!!! !%. +#', ! !$ "#!" , "%&!

'(" $*". !$ % *! "#!"'(" $*". !# ## #% "&$

#$, !%$ "$! "!& !($ $!!*".

!# ! ! "!!! &%" $# +(! "#! !%! &"%"". "$

!"$"! "-! %%! "!# %"":

Type ! ! " primitive type

| ! ! ! function type

!$&! ""#!

! " E : !

"".

!! !! !!type environment(! "%%! )"% !#$ "## *":

! # Idfin

! Type

Notation 8 !# $, !$ "%" !{x $! !} "'%" ! + x : !!! !*".

x $! ! # ! "'$ x : ! # !!! !*". !

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

타입환경

156 ! !!! !"

5.7.1 !! !! !! !!

"! "!! !"". !# "#!$ "!!%.

"!!! "#!!$ "## !# !"#" $#!! "!!!&## $

$!! !$ "#!"". "!!! "#!$ !#" !"" %" #". !"

! ! !$ "#!' "!!! "!"!! !#" "!& !'#(! )!#

% *! ## "!$".

!)! "#!" "!& "!!% !!#, (& "!# %! *! )#, $

$ "%"! #!$ !!!!! !%. +#', ! !$ "#!" , "%&!

'(" $*". !$ % *! "#!"'(" $*". !# ## #% "&$

#$, !%$ "$! "!& !($ $!!*".

!# ! ! "!!! &%" $# +(! "#! !%! &"%"". "$

!"$"! "-! %%! "!# %"":

Type ! ! " primitive type

| ! ! ! function type

!$&! ""#!

! " E : !

"".

!! !! !!type environment(! "%%! )"% !#$ "## *":

! # Idfin

! Type

Notation 8 !# $, !$ "%" !{x $! !} "'%" ! + x : !!! !*".

x $! ! # ! "'$ x : ! # !!! !*". !

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

타입 기본타입 (정수타입)

함수타입

Γ � E : τ

Γ � n : ι

Γ(x) = τ

Γ � x : τ

Γ + x : τ1 � E : τ2

Γ � λx.E : τ1 → τ2

Γ � E1 : τ1 → τ2 Γ � E2 : τ1

Γ � E1 E2 : τ2

Γ � E1 : ι Γ � E2 : ιΓ � E1+E2 : ι

E → n | x | λx.E | E E | E + E

v → n | λx.EC → [] | C E | v C | C + E | v + C

∅ � ff1 f2

f1 ∧ f2

T· · ·

let fac = rec f λn.if n = 0 then 1 else n ∗ (f (n− 1)) in (fac 2)

σ � 1 ⇒ 1· · ·

σ{x �→ 1} � x + 2 ⇒ 3σ � let x = 1 in x + 2 ⇒ 3

σ � λx.x ⇒ �λx.x, σ� σ � 1 ⇒ 1 σ{x �→ 1} � x ⇒ 1σ � (λx.x) 1 ⇒ 1

σ � λx.z x ⇒ �λx.z x, σ� σ � λy.y ⇒ �λy.y,σ�

σ{x �→ �λy.y,σ�} � z ⇒ �λk.k, σ��σ{x �→ �λy.y,σ�} � x ⇒ �λy.y,σ�σ�{k �→ �λy.y,σ� � k ⇒ �λy.y,σ�}σ{x �→ �λy.y,σ�} � z x ⇒ �λy.y,σ�

σ(= {z �→ �λk.k, σ��}) � (λx.z x) (λy.y) ⇒ �λy.y,σ�

σ � λx.E ⇒ λx.E

σ � E1 ⇒ λx.E� σ � E2 ⇒ v σ{x �→ v} � E� ⇒ v�

σ � E1 E2 ⇒ v�

λx.E

E1 E2

S = {x1 �→ Y1, . . . , xn �→ Yn}S E

1

Γ � E : τ

Γ � n : ι

Γ(x) = τ

Γ � x : τ

Γ + x : τ1 � E : τ2

Γ � λx.E : τ1 → τ2

Γ � E1 : τ1 → τ2 Γ � E2 : τ1

Γ � E1 E2 : τ2

Γ � E1 : ι Γ � E2 : ιΓ � E1+E2 : ι

E → n | x | λx.E | E E | E + E

v → n | λx.EC → [] | C E | v C | C + E | v + C

∅ � ff1 f2

f1 ∧ f2

T· · ·

let fac = rec f λn.if n = 0 then 1 else n ∗ (f (n− 1)) in (fac 2)

σ � 1 ⇒ 1· · ·

σ{x �→ 1} � x + 2 ⇒ 3σ � let x = 1 in x + 2 ⇒ 3

σ � λx.x ⇒ �λx.x, σ� σ � 1 ⇒ 1 σ{x �→ 1} � x ⇒ 1σ � (λx.x) 1 ⇒ 1

σ � λx.z x ⇒ �λx.z x, σ� σ � λy.y ⇒ �λy.y,σ�

σ{x �→ �λy.y,σ�} � z ⇒ �λk.k, σ��σ{x �→ �λy.y,σ�} � x ⇒ �λy.y,σ�σ�{k �→ �λy.y,σ� � k ⇒ �λy.y,σ�}σ{x �→ �λy.y,σ�} � z x ⇒ �λy.y,σ�

σ(= {z �→ �λk.k, σ��}) � (λx.z x) (λy.y) ⇒ �λy.y,σ�

σ � λx.E ⇒ λx.E

σ � E1 ⇒ λx.E� σ � E2 ⇒ v σ{x �→ v} � E� ⇒ v�

σ � E1 E2 ⇒ v�

λx.E

E1 E2

S = {x1 �→ Y1, . . . , xn �→ Yn}S E

1

Page 5: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

타입 추론의 예1

Γ + y : ι � y : ιΓ � λy.y : ι → ι Γ � 2 : ι

Γ � (λy.y) 2 : ι

– ∀α1 . . . αn.τ–

– ι.= ι• {}: most general unifier• {α �→ ι} : a (less general) unifier

S � S� ⇔ S� = T S for some T

A substitution S is more general than a substitution S�, written S � S�, ifS� = T ◦ S for some substitution T .

– unify(α, int→ int) = {α �→ int→ int}– unify(α, int→ α) = fail– unify(α → β, int→ int) = {α �→ int, β �→ int}– unify(α → β, int→ α) =

• S = unify(α, int) = {α �→ int}• S� = unify({α �→ int}β, {α �→ int}α) = unify(β, int) = {β �→ int}• S� S = {β �→ int}{α �→ int}

– S = {a �→ b}, S� = {b �→ c}• S� (S a) = S� b = c• S (S� a) = S a = b

{α �→ ι} |= α.= ι

{α �→ ι, α1 �→ ι, α2 �→ ι, τ �→ ι} |= α → τ.= α1 → α2 ∧ α1

.= α2 ∧ α.= ι

V (Γ, E, τ) ⇔ Γ � E : τ

Proof. By structural induction on E.

V (∅, (λx.x) 1, τ)= V (∅, λx.x,α → τ) ∧ V (∅, 1, α) · · · new α= α → τ

.= α1 → α2 ∧ V (x : α1, x,α2) ∧ α.= ι · · · new α1, α2

= α → τ.= α1 → α2 ∧ α1

.= α2 ∧ α.= ι

α1 = αx

α2 = α → βα3 = ια = α3

β = αx

αx = αα4 = β

Page 6: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

단순 타입 시스템의 확장����� �������� �������� �������

������ ����� �������(simple type system)�� “��� ���������”�� ������������� ���������.

����� ��������������

“��� ���������” = “���������������� ���� �����”

����� ����� �������(polymorphic type system)(�p)������� “������������”�� ����� ������� �����, ������ ����� �������(simpletype system)(�) ����:

Γ � e : τ ⇒ Γ �p e : τ

�p��� ��� “conservative extension”������ ���.

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 7: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

다형성 (Polymorphism)

• 하나의 식이 서로 다른 곳에서 서로 다른 타입으로 쓰일 수 있게 하기

let id = \x.xin (id 0, id false)

Page 8: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

비교: 단순/다형 타입시스템

let id = \x.x in (id 0, id false)

id:α→α α=int α=bool타입오류!

let id = \x.x in (id 0, id false)

id:int→int id:bool→bool

: int x bool

id:∀α.α→α

단순타입시스템

다형타입시스템

:α→α

:α→α

타입틀(type scheme)

Page 9: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

단순 타입 시스템의 한계����������� ������ ����� �������

...{f : τ → τ �} � f : τ → τ �

...{f : τ → τ �} � f : τ

τ = τ → τ �

{f : τ → τ �} � f f : τ �

� λf.f f : (τ → τ �)→ τ �

...{f : τ → τ} � f : τ → τ

...{f : τ → τ} � f : τ

τ = τ → τ

{f : τ → τ} � f f : τ → τ...

� λf.f f : (τ → τ)→ (τ → τ)� (λf.f f)(λx.x) : τ → τ

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 10: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

다형 타입 시스템�������� ��������(type generalization) �������?

�����(������ ����� ���, ���������כ �������� �����)�� �������� ���כ� ��������:

∀α.α → ι, ∀α1,α2.α1 → α2, · · ·

������

...{f : ∀α.α → α} � f : (ι → ι) → (ι → ι)

...{f : ∀α.α → α} � f : ι → ι

{f : ∀α.α → α} � f f : ι

� λf.f f : (∀α.α → α) → ι

����

...� λf.f f : (∀α.α → α) → (ι → ι)

...� λx.x : ι → ι

� (λf.f f)(λx.x) : ι → ι

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 11: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

일반화는 조심해서������� ������������� ���������

...{f : ∀α.α → ι} � f : ι → ι · · ·

{f : ∀α.α → ι} � f 1 : ι · · ·{f : ∀α.α → ι} � (f 1, f true) : ι× ι

� λf.(f 1, f true) : (∀α.α → ι) → (ι× ι)

...� λx.x + 1 : ι → ι

� (λf.(f 1, f true))(λx.x + 1) : ι× ι

������

...� (λx.(let y = x in (y 1, y true)))(λz.z + 1) : ι× bool

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

함부로 일반화하면 타입시스템이 불안전

Page 12: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

Let-polymorphism��������� ������ �������: let-polymorphism ��

“Hindley-Milner style let-polymorphism”

����������� �������� ������ �������� ������� �������� ���������������������������� ������ ����� ������� ������� ���������� ����� ��� �������� ����� ���,

(λx. · · · x · · · x · · ·� �� �e

)e�

���,

let x = e�in e

��� ���������� �����, e��� ������������� �� ������ �� “����������” �������������, e������� x�� ������� ���������� �� ������.

������������� 1���(rank-1 polymorphism)�������:

ι→ ι,∀α.α → α, ∀α1,α2.α1 → α2

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 13: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

1단 다형타입까지만(rank-1 polymorphism)

��������� ������ �������: let-polymorphism ��

“Hindley-Milner style let-polymorphism”

����������� �������� ������ �������� ������� �������� ���������������������������� ������ ����� ������� ������� ���������� ����� ��� �������� ����� ���,

(λx. · · · x · · · x · · ·� �� �e

)e�

���,

let x = e�in e

��� ���������� �����, e��� ������������� �� ������ �� “����������” �������������, e������� x�� ������� ���������� �� ������.

������������� 1���(rank-1 polymorphism)�������:

ι→ ι,∀α.α → α, ∀α1,α2.α1 → α2

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

������� ������� ����������� ����������

���������� ������ �������������(complete)��� ������� ��������(undecidable):

2���(rank 2)������� ����������(polymorphism)��� ������������ “���������” ������ ��������� ����������� ���כ� ������:����������������� �����

,���כ���� 1���(rank 1) ����������(polymorphism)������� �����0���(rank 0) = �����������(monomorphic types)1���(rank 1) = “∀”�� ����� �����(prenex forma)��� ����������

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 14: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

예) Let-polymorphism

let id = \x.x in (id 0, id false)

id:int→int id:bool→bool

: int x bool

id:∀α.α→α

:α→α

Page 15: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

Let-다형 타입 시스템

Page 16: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

언어

182 ! !!! !"

let-!! !! !"!let-polymorphic type system#!! let-"" "!" $"!

!##.

E ! n

| x

| !x.E

| E E

| let x = E in E

| rec f !x.E

| E + E

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

Page 17: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

타입 / 타입틀 / 타입환경5.8 !! !! !"!polymorphic type system 183

!!type! !!!type scheme! !"! "!.

Type ! ! " primitive type

| ! ! ! function type

| # type variable

TypeScheme $ ! ! simple type

| "#.$ generalized type

!!!type scheme! "!!#! !!!#$ !!"!.

Notation 10 %# # {#1, · · · ,#n}!, "%#.! # "#1 · · ·#n.!! !"!.

!!!type scheme $ = "%#.!! !#! ftv($) # $! %# &'!! !# !

"! ftv(!) \ %# (!.

!# !! !#! ftv(!)# !! %# !# !"!) !$(!. !# "# !

! !#!, ftv(!) =!

x!Dom !ftv(!(x)).

!# !" S! !#! itv(S) = {# |& # supp(S),# # {&} $ ftv(S&)}(!.

!# !" S ! !!!type scheme $! !#! S$ = "%&.S{%# %! %&}!($, (

! %& & (itv(S) $ ftv($)) = ' * #"!.

!# !" S ! !# "# !! !#!, S! = {x %! S$ |x %! $ # !} (!.

"%#.! " ( !# !#!) " #% !% "!# )!(!. !#!#, !# !"

S % %"! S! " = ! ($ Supp(S) ) %# * #"! !"!.

GEN !(!) = "%#.! ! !%$ %# = ftv(!) \ ftv(!)* #"(!. !

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

184 ! !!! !"

let-!! !! !"!let-polymorphic type system! !" !! !!# !$" #

!.

!!!!inference rules# “! ! E : !”!%!!"!!!&!. !"!"!!

!!!type scheme# !" $!' $"% (!:

! " Idfin

# TypeScheme

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

Page 18: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

다형 타입의 의미

����� ������� ���� γ(∀α.τ)?

�����כ�� ������

����� ����� α�� ������ τ �������� ���כ������������, ∀α.α → α�� �������: ������������ ����������� �����������������������������������כ����������������������������������,

γ(∀α.τ) =�

t∈SimpleType

γ({t/α}τ)

����������,

γ(∀α.α → α) =�

t∈SimpleType γ(t → t)= γ(ι → ι) ∩ γ(bool → bool) ∩ · · ·= {λx.x, λx.1,λx.x + 1, · · · }∩

{λx.x, λx.(x||true), · · · } ∩ · · ·

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

5.8 !! !! !"!polymorphic type system 179

type! ! "!. !! !#!$ !%! "$ #!&!:

!!.! " ", !!1!2.!1 " !2, · · ·

!& !# !!! !"! !""" #&!. !# #" !#' $!! !$ !

"!" !#&!" "&!. !, !! !#' '! [[!!.# ]]" !%! "$ %!

' ""&!& " ! "!:

• !# !# !! !#! # !#( %!.

• "!!", !!.! " !' '!":

[[!!.# ]] =!

t!SimpleType

[[{! #" t}# ]]

"!!",

[[!!.! " !]] ="

t!SimpleType [[t " t]]

= [[" " "]] $ [[bool " bool ]] $ · · ·

= {$x.x,$x.1,$x.x + 1, · · · } $ · · ·

{$x.x, · · · }

#" !# #$!!! %"$ $!' "!( !! !# #$!!!" !"

) ! "" # "! !" #). ## $ $!" #" !# #$!!!" !#

*!% ! $" $!&!.

...{f : # " # "} % f : # " # "

...{f : # " # "} % f : #

! = ! ! ! !

{f : # " # "} % f f : # "

% $f.f f : (# " # ") " # "

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

Page 19: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

표기법

5.8 !! !! !"!polymorphic type system 183

!!type! !!!type scheme! !"! "!.

Type ! ! " primitive type

| ! ! ! function type

| # type variable

TypeScheme $ ! ! simple type

| "#.$ generalized type

!!!type scheme! "!!#! !!!#$ !!"!.

Notation 10 %# # {#1, · · · ,#n}!, "%#.! # "#1 · · ·#n.!! !"!.

!!!type scheme $ = "%#.!! !#! ftv($) # $! %# &'!! !# !

"! ftv(!) \ %# (!.

!# !! !#! ftv(!)# !! %# !# !"!) !$(!. !# "# !

! !#!, ftv(!) =!

x!Dom !ftv(!(x)).

!# !" S! !#! itv(S) = {# |& # supp(S),# # {&} $ ftv(S&)}(!.

!# !" S ! !!!type scheme $! !#! S$ = "%&.S{%# %! %&}!($, (

! %& & (itv(S) $ ftv($)) = ' * #"!.

!# !" S ! !# "# !! !#!, S! = {x %! S$ |x %! $ # !} (!.

"%#.! " ( !# !#!) " #% !% "!# )!(!. !#!#, !# !"

S % %"! S! " = ! ($ Supp(S) ) %# * #"! !"!.

GEN !(!) = "%#.! ! !%$ %# = ftv(!) \ ftv(!)* #"(!. !

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

5.8 !! !! !"!polymorphic type system 183

!!type! !!!type scheme! !"! "!.

Type ! ! " primitive type

| ! ! ! function type

| # type variable

TypeScheme $ ! ! simple type

| "#.$ generalized type

!!!type scheme! "!!#! !!!#$ !!"!.

Notation 10 %# # {#1, · · · ,#n}!, "%#.! # "#1 · · ·#n.!! !"!.

!!!type scheme $ = "%#.!! !#! ftv($) # $! %# &'!! !# !

"! ftv(!) \ %# (!.

!# !! !#! ftv(!)# !! %# !# !"!) !$(!. !# "# !

! !#!, ftv(!) =!

x!Dom !ftv(!(x)).

!# !" S! !#! itv(S) = {# |& # supp(S),# # {&} $ ftv(S&)}(!.

!# !" S ! !!!type scheme $! !#! S$ = "%&.S{%# %! %&}!($, (

! %& & (itv(S) $ ftv($)) = ' * #"!.

!# !" S ! !# "# !! !#!, S! = {x %! S$ |x %! $ # !} (!.

"%#.! " ( !# !#!) " #% !% "!# )!(!. !#!#, !# !"

S % %"! S! " = ! ($ Supp(S) ) %# * #"! !"!.

GEN !(!) = "%#.! ! !%$ %# = ftv(!) \ ftv(!)* #"(!. !

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

Page 20: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

첫번째 시도 (불안전)

5.8 !! !! !"!polymorphic type system 185

! ! E : !

! ! n : " ! ! x : !# " !, x : # # !

! + x : ! ! E : ! !

! ! $x.E : ! $ ! !

! ! E1 : ! ! $ ! ! ! E2 : ! !

! ! E1 E2 : !

! ! E1 : !1 ! + x : GEN !(!1) ! E2 : !2

! ! let x = E1 in E2 : !2

! + f : ! $ ! ! ! $x.E : ! $ ! !

! ! rec f $x.E : ! $ ! !

! ! E1 : " ! ! E2 : "! ! E1 + E2 : "

!!!!!!!!"#!"!"!!$%"!!!#""%"!""

". #! !$& #! !! "! !$%" '!"""! $#".

! !$ !" “'!"” %%&.! !#!! &! ! % !!!! #"!" #!?

GEN !(!) = %&1, · · · ,&n.! &"" {&1, · · · ,&n} = ftv(!) \ ftv(!)

" &(!:

• !% !$(x : #)& !! "! "#! $x.E! "#"% '".

• !% )! !$" !$$! "#! %"!%" %"! *+ !$" (!

# !&".

• %"! *+ !$" '!"!## !" %" "!& !$ (!"! !!

!! !".

• %"! #""!" !##! ## *+!& '!"$ " )! !$! $

& #$ " )" !"&".

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

à � e : � ���������� �����

Γ � n : ι Γ � x : τσ � τ, x : σ ∈ Γ

Γ � e : τ Γ + x : GenΓ(τ) � e� : τ

Γ � let x = e in e� : τ

Γ � e1 : ι Γ � e2 : ιΓ � e1 + e2 : ι

Γ � e1 : τ � → τ Γ � e2 : τ �

Γ � e1 e2 : τ

Γ + x : τ � e : τ �

Γ � λx.e : τ → τ �

generalization GenΓ(τ) = ∀α1, · · · ,αn.τ {α1, · · · ,αn} = FTV (τ) \ FTV (Γ)instantiation σ � τ σ = ∀α1, · · · ,αn.τ �, τ = {τi/αi}iτ

FTV (τ) = TV (τ)FTV (∀α.σ) = FTV σ \ {α}

FTV (Γ) = ∪x:σ∈ΓFTV (σ)

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 21: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

예제let y = \x.x in y y

:α→α

y : ∀α.α→α

y : (β→β)→(β→β)

y : β→β

: β→β

Page 22: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

불안전한 경우let k = \x.(let y = x in (y 1, y true))

in k (\x.x+1)

:α→α

y : ∀α.α→α

:int→int :bool→bool

(α→α) → int x bool

∀α.(α→α) → int x bool

∀α.(α→α) → int x bool

:int→int

Page 23: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

불안전한 이유 / 해결책

let k = \x.(let y = x in (y 1, y true))

in k (\x.x+1)

:α→α

y : ∀α.α→α

:int→int :bool→bool

x:α→α ∈ 𐅃

5.8 !! !! !"!polymorphic type system 185

! ! E : !

! ! n : " ! ! x : !# " !, x : # # !

! + x : ! ! E : ! !

! ! $x.E : ! $ ! !

! ! E1 : ! ! $ ! ! ! E2 : ! !

! ! E1 E2 : !

! ! E1 : !1 ! + x : GEN !(!1) ! E2 : !2

! ! let x = E1 in E2 : !2

! + f : ! $ ! ! ! $x.E : ! $ ! !

! ! rec f $x.E : ! $ ! !

! ! E1 : " ! ! E2 : "! ! E1 + E2 : "

!!!!!!!!"#!"!"!!$%"!!!#""%"!""

". #! !$& #! !! "! !$%" '!"""! $#".

! !$ !" “'!"” %%&.! !#!! &! ! % !!!! #"!" #!?

GEN !(!) = %&1, · · · ,&n.! &"" {&1, · · · ,&n} = ftv(!) \ ftv(!)

" &(!:

• !% !$(x : #)& !! "! "#! $x.E! "#"% '".

• !% )! !$" !$$! "#! %"!%" %"! *+ !$" (!

# !&".

• %"! *+ !$" '!"!## !" %" "!& !$ (!"! !!

!! !".

• %"! #""!" !##! ## *+!& '!"$ " )! !$! $

& #$ " )" !"&".

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

à � e : � ���������� �����

Γ � n : ι Γ � x : τσ � τ, x : σ ∈ Γ

Γ � e : τ Γ + x : GenΓ(τ) � e� : τ

Γ � let x = e in e� : τ

Γ � e1 : ι Γ � e2 : ιΓ � e1 + e2 : ι

Γ � e1 : τ � → τ Γ � e2 : τ �

Γ � e1 e2 : τ

Γ + x : τ � e : τ �

Γ � λx.e : τ → τ �

generalization GenΓ(τ) = ∀α1, · · · ,αn.τ {α1, · · · ,αn} = FTV (τ) \ FTV (Γ)instantiation σ � τ σ = ∀α1, · · · ,αn.τ �, τ = {τi/αi}iτ

FTV (τ) = TV (τ)FTV (∀α.σ) = FTV σ \ {α}

FTV (Γ) = ∪x:σ∈ΓFTV (σ)

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

𐅃에 들어있는 타입변수는 일반화하지 않아야...

실체를 모르는 (𐅃에 들어있는) 타입변수들은 일반화하지 않아야...

Page 24: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

추론 규칙의 안전성����� ������� ��������� ������: ���������� ���� �����������

������� �������� ���������� ������:

Theorem (Progress)

� e : τ ���� e�� ��כ�� ������� ������� ������ e→ e� �����.

��������� �������� �����:

Theorem (Subject Reduction, Preservation)

� e : τ ���� e→ e� ����� � e� : τ .

Prof. Kwangkeun Yi SNU 4541.664A Program Analysis Note 17

Page 25: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

HW

• “Proofs about a Folklore Let-Polymorphic Type Inference Algorithm”, Oukseh Lee and Kwangkeun Yi, TOPLAS, 20(4), 1998

• 읽고, 알고리즘 M, W 작동방식에 대해 정리하기

• 2페이지 이내

• 5/24 (화) 수업시간에 제출

Page 26: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

타입유추 알고리즘

• 오프라인 알고리즘

• 타입 제약식에 대한 연립방정식을 세우고

• 동일화 알고리즘으로 풀기

• 온라인 알고리즘

• 방정식 세우고 푸는 것을 한번에

Page 27: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

온라인 알고리즘 M

170 ! !!! !"

5.7.3.3 !!" #"!"!

!# !!"$ !!!!! !! !! !"#! #"!"! #"!#.

M : TyEnv ! Exp ! Type " (TyVarfin

!" Type)

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

5.7 !! !! "#!simple type system 171

M(!, n, !) = unify(", !)

M(!, x, !) = unify(!, ! !) if x : ! ! ! !

M(!,#x.E, !) = let S = unify($1 " $2, !) new $1,$2

S ! = M(S! + x : S$1, e, S$2)

in S !S

M(!, E E !, !) = let S = M(!, e,$ " !) new $

S ! = M(S!, E !, S$)

in S !S

M(!, E + E !, !) = let S = unify(", !)

S ! = M(S!, e, ")

S !! = M(S !S!, E !, ")

in S !!S !S

!" !!!!! !! !! "#!simple type system" !$!sound & complete !

!#!.

!!sound: M(!, e,$) = S #! S! # E : S$

!!complete:

M(!, e,$) = S

$ !! = RS!

$ ! ! = RS$

!

"""#

"""$

!# !! # E : ! !

"$, "" !"#.

W : TyEnv % Exp " Type % (TyVarfin

%" Type)

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

let y = \x.x in y 1:α→α

y:α→α

β int

M(∅,E, α) = SE: S τ

M(∅,\x.x, α) =

let S = unify (α1 → α2, α) = {α ↦ α1 → α2}

S’ = M (x: α1,x, α2) = {α1 ↦ α2}

in {α1 ↦ α2} {α ↦ α1 → α2}

Page 28: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

온라인 알고리즘 W

5.7 !! !! "#!simple type system 171

M(!, n, !) = unify(", !)

M(!, x, !) = unify(!, ! !) if x : ! ! ! !

M(!,#x.E, !) = let S = unify($1 " $2, !) new $1,$2

S ! = M(S! + x : S$1, e, S$2)

in S !S

M(!, E E !, !) = let S = M(!, e,$ " !) new $

S ! = M(S!, E !, S$)

in S !S

M(!, E + E !, !) = let S = unify(", !)

S ! = M(S!, e, ")

S !! = M(S !S!, E !, ")

in S !!S !S

!" !!!!! !! !! "#!simple type system" !$!sound & complete !

!#!.

!!sound: M(!, e,$) = S #! S! # E : S$

!!complete:

M(!, e,$) = S

$ !! = RS!

$ ! ! = RS$

!

"""#

"""$

!# !! # E : ! !

"$, "" !"#.

W : TyEnv % Exp " Type % (TyVarfin

%" Type)

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

172 ! !!! !"

W (!, n) = (!, !)

W (!, x) = (", !) if x "# " $ !

W (!,#x.E) = let (", S) = W (! + x : $, e) new $

in (S$ # ", S)

W (!, E E !) = let (", S) = W (!, e)

(" !, S !) = W (S!, E !)

S !! = unify(" ! # $, S !") new $

in (S !!S !S, S !!$)

W (!, E + E !) = let (", S) = W (!, e)

S ! = unify(", !)

(" !, S !!) = W (S !S!, E !)

S !!! = unify(" !, !)

in (!, S !!!S !!S !S)

"! #"!"! !! !! "#!simple type system! !$!sound & complete !

!#!.

!!sound: W (!, e) = (", S) #! S! % E : "

!!complete:

W (!, e) = (", S)

& !! = RS!

& " ! = R"

!

"""#

"""$

!# !! % E : " !

5.7.4 !!! !!! !!!

"" !$ #$!# $!!!# !#" %%!# #!.

"!!# "!!. !$ #$!% "#" (!$ &!& !$") #!"!'

&$!& !$ #$% !!!# '# !# &# "(!!. !#", !$ #$

(# ) &$" ' *# ## #!"!# "" !$ #$!% "#!# &'

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

W(∅,E) = (τ,S)E: τ

W(∅,\x.x+1) = ...

Page 29: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

W vs. M

5.7 !! !! "#!simple type system 171

M(!, n, !) = unify(", !)

M(!, x, !) = unify(!, ! !) if x : ! ! ! !

M(!,#x.E, !) = let S = unify($1 " $2, !) new $1,$2

S ! = M(S! + x : S$1, e, S$2)

in S !S

M(!, E E !, !) = let S = M(!, e,$ " !) new $

S ! = M(S!, E !, S$)

in S !S

M(!, E + E !, !) = let S = unify(", !)

S ! = M(S!, e, ")

S !! = M(S !S!, E !, ")

in S !!S !S

!" !!!!! !! !! "#!simple type system" !$!sound & complete !

!#!.

!!sound: M(!, e,$) = S #! S! # E : S$

!!complete:

M(!, e,$) = S

$ !! = RS!

$ ! ! = RS$

!

"""#

"""$

!# !! # E : ! !

"$, "" !"#.

W : TyEnv % Exp " Type % (TyVarfin

%" Type)

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

172 ! !!! !"

W (!, n) = (!, !)

W (!, x) = (", !) if x "# " $ !

W (!,#x.E) = let (", S) = W (! + x : $, e) new $

in (S$ # ", S)

W (!, E E !) = let (", S) = W (!, e)

(" !, S !) = W (S!, E !)

S !! = unify(" ! # $, S !") new $

in (S !!S !S, S !!$)

W (!, E + E !) = let (", S) = W (!, e)

S ! = unify(", !)

(" !, S !!) = W (S !S!, E !)

S !!! = unify(" !, !)

in (!, S !!!S !!S !S)

"! #"!"! !! !! "#!simple type system! !$!sound & complete !

!#!.

!!sound: W (!, e) = (", S) #! S! % E : "

!!complete:

W (!, e) = (", S)

& !! = RS!

& " ! = R"

!

"""#

"""$

!# !! % E : " !

5.7.4 !!! !!! !!!

"" !$ #$!# $!!!# !#" %%!# #!.

"!!# "!!. !$ #$!% "#" (!$ &!& !$") #!"!'

&$!& !$ #$% !!!# '# !# &# "(!!. !#", !$ #$

(# ) &$" ' *# ## #!"!# "" !$ #$!% "#!# &'

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

top-down vs. bottom-up

E E’ ←unify E E’

←unifyunify→

Page 30: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

W vs. M2 · O. Lee and K. Yi

#let rec fac n = if n = 0 then 1 else n *(fac(n=1));;

Toplevel input:

>let rec fac n = if n = 0 then 1 else n *(fac(n=1));;

> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This expression has type int -> int,

but is used with type bool -> int.

(a) from W (CamlLight 0.71)

#let rec fac n = if n = 0 then 1 else n *(fac(n=1));;

Toplevel input

>let rec fac n = if n = 0 then 1 else n *(fac(n=1));;

> ^

Expression of type ’a -> ’a -> bool

cannot be used with type ’a -> ’a -> int

(b) fromM (CamlLight 0.61)

Fig. 1. Different type-error messages from W andM.

constraint (or an expected type) implied by the context of an expression down to

its sub-or-sibling expressions. For example, for an application expression “e1 e2”

with a type constraint, say of int, the type constraint for e1 is α → int and the

constraint for e2 is the type that the α becomes after the type inference of e1. For a

constant or a variable expression, its type must satisfy the type constraint that the

algorithm has carried to that point. Because of this “top-down” nature we name

this algorithm “M.”

In this article we formally define algorithm M, prove its soundness and com-

pleteness, and show that it finds type errors earlier than W. This property implies

that this algorithm in combination with W can generate strictly more informative

type-error messages than either of the two algorithms alone can.

As an example to show the difference of the two algorithms, see Figure 1. The

program is a factorial function whose recursive call is mistakenly “fac(n=1),” in-

stead of “fac(n-1).” Algorithm W (CamlLight 0.71 [Leroy 1995] and SML/NJ

0.93 [MacQueen and Appel 1993]) reports the whole definition as the problem area

because the algorithm fails to unify the argument type bool inferred from the re-

cursive call “fac(n=1)” with the type int inferred from the argument use “if n =0· · ·.” On the other hand, algorithm M (CamlLight 0.61 [Leroy 1993]) pinpoints

the operator “=” as the problem spot. This exact error message is possible because

the type constraint of the function’s argument is int when the argument “(n=1)”of the recursive call is type-checked.

2. THE M ALGORITHM

2.1 Overview

Algorithm M carries a type constraint from the context of an expression and stops

when the expression cannot satisfy the current type constraint. Consider the fol-

lowing expression:

(fn x => x+1)� �� �e1

(

e3� �� �(fn y => if y then true else false)

e4� �� �false )� �� �

e2

# let rec fac n = if n = 0 then 1 else n * (fac (n=1));;Error: This expression has type bool but an expression was expected of type int

OCaml 3.11.2

Page 31: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

let-다형 타입 시스템• 타입 일반화를 조심해서 (let식에서 선택적)

5.8 !! !! !"!polymorphic type system 185

! ! E : !

! ! n : " ! ! x : !# " !, x : # # !

! + x : ! ! E : ! !

! ! $x.E : ! $ ! !

! ! E1 : ! ! $ ! ! ! E2 : ! !

! ! E1 E2 : !

! ! E1 : !1 ! + x : GEN !(!1) ! E2 : !2

! ! let x = E1 in E2 : !2

! + f : ! $ ! ! ! $x.E : ! $ ! !

! ! rec f $x.E : ! $ ! !

! ! E1 : " ! ! E2 : "! ! E1 + E2 : "

!!!!!!!!"#!"!"!!$%"!!!#""%"!""

". #! !$& #! !! "! !$%" '!"""! $#".

! !$ !" “'!"” %%&.! !#!! &! ! % !!!! #"!" #!?

GEN !(!) = %&1, · · · ,&n.! &"" {&1, · · · ,&n} = ftv(!) \ ftv(!)

" &(!:

• !% !$(x : #)& !! "! "#! $x.E! "#"% '".

• !% )! !$" !$$! "#! %"!%" %"! *+ !$" (!

# !&".

• %"! *+ !$" '!"!## !" %" "!& !$ (!"! !!

!! !".

• %"! #""!" !##! ## *+!& '!"$ " )! !$! $

& #$ " )" !"&".

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

5.8 !! !! !"!polymorphic type system 185

! ! E : !

! ! n : " ! ! x : !# " !, x : # # !

! + x : ! ! E : ! !

! ! $x.E : ! $ ! !

! ! E1 : ! ! $ ! ! ! E2 : ! !

! ! E1 E2 : !

! ! E1 : !1 ! + x : GEN !(!1) ! E2 : !2

! ! let x = E1 in E2 : !2

! + f : ! $ ! ! ! $x.E : ! $ ! !

! ! rec f $x.E : ! $ ! !

! ! E1 : " ! ! E2 : "! ! E1 + E2 : "

!!!!!!!!"#!"!"!!$%"!!!#""%"!""

". #! !$& #! !! "! !$%" '!"""! $#".

! !$ !" “'!"” %%&.! !#!! &! ! % !!!! #"!" #!?

GEN !(!) = %&1, · · · ,&n.! &"" {&1, · · · ,&n} = ftv(!) \ ftv(!)

" &(!:

• !% !$(x : #)& !! "! "#! $x.E! "#"% '".

• !% )! !$" !$$! "#! %"!%" %"! *+ !$" (!

# !&".

• %"! *+ !$" '!"!## !" %" "!& !$ (!"! !!

!! !".

• %"! #""!" !##! ## *+!& '!"$ " )! !$! $

& #$ " )" !"&".

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

1

{y : β, z : γ} � E1 : α → β

x : ∀α.α → β

Γ + y : ι � y : ιΓ � λy.y : ι → ι Γ � 2 : ι

Γ � (λy.y) 2 : ι

– ∀α1 . . . αn.τ–

– ι.= ι• {}: most general unifier• {α �→ ι} : a (less general) unifier

S � S� ⇔ S� = T S for some T

A substitution S is more general than a substitution S�, written S � S�, ifS� = T ◦ S for some substitution T .

– unify(α, int→ int) = {α �→ int→ int}– unify(α, int→ α) = fail– unify(α → β, int→ int) = {α �→ int, β �→ int}– unify(α → β, int→ α) =

• S = unify(α, int) = {α �→ int}• S� = unify({α �→ int}β, {α �→ int}α) = unify(β, int) = {β �→ int}• S� S = {β �→ int}{α �→ int}

– S = {a �→ b}, S� = {b �→ c}• S� (S a) = S� b = c• S (S� a) = S a = b

{α �→ ι} |= α.= ι

{α �→ ι, α1 �→ ι, α2 �→ ι, τ �→ ι} |= α → τ.= α1 → α2 ∧ α1

.= α2 ∧ α.= ι

V (Γ, E, τ) ⇔ Γ � E : τ

Proof. By structural induction on E.

V (∅, (λx.x) 1, τ)= V (∅, λx.x,α → τ) ∧ V (∅, 1, α) · · · new α= α → τ

.= α1 → α2 ∧ V (x : α1, x,α2) ∧ α.= ι · · · new α1, α2

= α → τ.= α1 → α2 ∧ α1

.= α2 ∧ α.= ι

• 적절히 구체화해서 사용5.8 !! !! !"!polymorphic type system 185

! ! E : !

! ! n : " ! ! x : !# " !, x : # # !

! + x : ! ! E : ! !

! ! $x.E : ! $ ! !

! ! E1 : ! ! $ ! ! ! E2 : ! !

! ! E1 E2 : !

! ! E1 : !1 ! + x : GEN !(!1) ! E2 : !2

! ! let x = E1 in E2 : !2

! + f : ! $ ! ! ! $x.E : ! $ ! !

! ! rec f $x.E : ! $ ! !

! ! E1 : " ! ! E2 : "! ! E1 + E2 : "

!!!!!!!!"#!"!"!!$%"!!!#""%"!""

". #! !$& #! !! "! !$%" '!"""! $#".

! !$ !" “'!"” %%&.! !#!! &! ! % !!!! #"!" #!?

GEN !(!) = %&1, · · · ,&n.! &"" {&1, · · · ,&n} = ftv(!) \ ftv(!)

" &(!:

• !% !$(x : #)& !! "! "#! $x.E! "#"% '".

• !% )! !$" !$$! "#! %"!%" %"! *+ !$" (!

# !&".

• %"! *+ !$" '!"!## !" %" "!& !$ (!"! !!

!! !".

• %"! #""!" !##! ## *+!& '!"$ " )! !$! $

& #$ " )" !"&".

SNU 4190.310 Programming Languages c!Kwangkeun Yi, Seoul National Univ., 2006, 2007, 2008

1

{f : ∀α.α → α} � f : ι → ι∀α.α → α � ι → ι 1 : ι

{f : ∀α.α → α} � f 1 : ι

{y : β, z : γ} � E1 : α → β

x : ∀α.α → β

Γ + y : ι � y : ιΓ � λy.y : ι → ι Γ � 2 : ι

Γ � (λy.y) 2 : ι

– ∀α1 . . . αn.τ–

– ι.= ι

• {}: most general unifier• {α �→ ι} : a (less general) unifier

S � S� ⇔ S� = T S for some T

A substitution S is more general than a substitution S�, written S � S�, ifS� = T ◦ S for some substitution T .

– unify(α, int→ int) = {α �→ int→ int}– unify(α, int→ α) = fail– unify(α → β, int→ int) = {α �→ int, β �→ int}– unify(α → β, int→ α) =

• S = unify(α, int) = {α �→ int}• S� = unify({α �→ int}β, {α �→ int}α) = unify(β, int) = {β �→ int}• S� S = {β �→ int}{α �→ int}

– S = {a �→ b}, S� = {b �→ c}• S� (S a) = S� b = c• S (S� a) = S a = b

{α �→ ι} |= α.= ι

{α �→ ι, α1 �→ ι, α2 �→ ι, τ �→ ι} |= α → τ.= α1 → α2 ∧ α1

.= α2 ∧ α.= ι

V (Γ, E, τ) ⇔ Γ � E : τ

Proof. By structural induction on E.

아무 제약이 없는 타입변수들만 일반화

Page 32: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

let-다형 타입 유추 알고리즘 W����� ����� ������� Polymorphic Type System

����� ����� ���������� ������� �������������� ������� �����

let-����� ����� ���� ���������� W

W : TyEnv × Exp → ((TyVar fin→ Type)× Type)

W(Γ, n) = (∅, ι)W(Γ, x) = (∅, {αi �→ βi}n

i=1τ) where Γ(x) = ∀�α.τ, new �βW(Γ,λx.E) = let (S1, τ1) = W(Γ + x : β, E), new β

in (S1, S1β → τ1)W(Γ, E1 E2) = let (S1, τ1) = W(Γ, E1)

(S2, τ2) = W(S1Γ, E2)S3 = U(S2τ1, τ2 → β), new β

in (S3S2S1, S3β)W(Γ, let x = E1 in E2) =

let (S1, τ1) = W(Γ, E1)(S2, τ2) = W(S1Γ + x : GEN S1Γ(τ1), E2)

in (S2S1, τ2)

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

Page 33: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

let-다형 타입 유추 알고리즘 M����� ����� ������� Polymorphic Type System

����� ����� ���������� ������� �������������� ������� �����

let-����� ����� ���� ���������� M

M : TyEnv × Exp × Type → (TyVar fin→ Type)

M(Γ, n, τ) = U(τ, ι)M(Γ, x, τ) = U(τ, {αi �→ βi}n

i=1τ�) where Γ(x) = ∀�α.τ �, new �β

M(Γ,λx.E, τ) = let S1 = U(τ,β1 → β2), new β1,β2

S2 = M(S1Γ + x : S1β1, E, S1β2)in S2S1

M(Γ, E1 E2, τ) = let S1 = M(Γ, E1,β → τ), new βS2 = M(S1Γ, E2, S1β)

in S2S1

M(Γ, let x = E1 in E2, τ) =let S1 = M(Γ, E1,β), new β

S2 = M(S1Γ + x : GEN S1Γ(S1β), E2, S1τ)in S2S1

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

Page 34: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

Polymorphic Imperatives����� ����� ������� Polymorphic Type System

����� ����� ���������� ������� �������������� ������� �����

Polymorphic Imperatives

E →...

| malloc E| !E| E := E

Type τ → ι primitive type| α type variable| τ → τ function type| τ loc pointer type

TypeScheme σ → τ simple type| ∀α.σ generalized type

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

Page 35: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

Polymorphic Imperatives: trial����� ����� ������� Polymorphic Type System����� ����� ���������� ������� �������������� ������� �����

Let-polymorphic Imperatives: trial

Γ � E : τΓ � malloc E : τ loc

Γ � E : τ locΓ � !E : τ

Γ � E1 : τ loc Γ � E2 : τΓ � E1 := E2 : τ

Γ � E1 : τ1 Γ � E2 : τ2

Γ � E1 ; E2 : τ2

��������� �������� ��� �� �������������:

Γ � E : τ Γ + x : GEN Γ(τ) � E� : τ

Γ � let x = E in E� : τ

��� E�� ����������� ������ ������� ������� ������������ ����� ������ ���������� ����������.

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

Page 36: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

Typing example

let r = malloc(\x.x) in r:=(\x.x+1); (!r) true

Type check?

Page 37: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

일반화를 조심히

����� ����� ������� Polymorphic Type System����� ����� ���������� ������� �������������� ������� �����

Let-polymorphic Imperatives: trial

Γ � E : τΓ � malloc E : τ loc

Γ � E : τ locΓ � !E : τ

Γ � E1 : τ loc Γ � E2 : τΓ � E1 := E2 : τ

Γ � E1 : τ1 Γ � E2 : τ2

Γ � E1 ; E2 : τ2

��������� �������� ��� �� �������������:

Γ � E : τ Γ + x : GEN Γ(τ) � E� : τ

Γ � let x = E in E� : τ

��� E�� ����������� ������ ������� ������� ������������ ����� ������ ���������� ����������.

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

����� ����� ������� Polymorphic Type System����� ����� ���������� ������� �������������� ������� �����

Let-polymorphic Imperatives: trial

Γ � E : τΓ � malloc E : τ loc

Γ � E : τ locΓ � !E : τ

Γ � E1 : τ loc Γ � E2 : τΓ � E1 := E2 : τ

Γ � E1 : τ1 Γ � E2 : τ2

Γ � E1 ; E2 : τ2

��������� �������� ��� �� �������������:

Γ � E : τ Γ + x : GEN Γ(τ) � E� : τ

Γ � let x = E in E� : τ

��� E�� ����������� ������ ������� ������� ������������ ����� ������ ���������� ����������.

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

����� ����� ������� Polymorphic Type System����� ����� ���������� ������� �������������� ������� �����

��� E�� ����������� ������ ������� ������������ ����� ��������.����������� ����������� ��� �� �����.�������, ������������� ��������� �� �����.

������ let-����� ����� ���� ������� ������� ����� ������� �����:

Γ � E : τ Γ + x : GEN Γ(τ) � E� : τ

Γ � let x = E in E� : τ¬expansive(E)

Γ � E : τ Γ + x : τ � E� : τΓ � let x = E in E� : τ

expansive(E)

expansive(n) = falseexpansive(x) = false

expansive(λx.E) = falseexpansive(E1 E2) = true

expansive(let x = E1 in E2) = expansive(E1) ∨ expansive(E2)

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

let r = malloc(\x.x) in ...let r =\x.x in ...

XO

Page 38: Polymorphic Type Systemdreameye/PL/slide/PL11.pdf · 2011-05-19 · 5.8 !!타입!! !" / !polymorphic타입틀 type system / 타입환경 183!!type! !!!type scheme! !"! Type τ →

Polymorphic Imperatives

����� ����� ������� Polymorphic Type System����� ����� ���������� ������� �������������� ������� �����

Let-polymorphic Imperatives

Γ � E : τΓ � malloc E : τ loc

Γ � E : τ locΓ � !E : τ

Γ � E1 : τ loc Γ � E2 : τΓ � E1 := E2 : τ

Γ � E1 : τ1 Γ � E2 : τ2

Γ � E1 ; E2 : τ2

Γ � E : τ Γ + x : GEN Γ(τ) � E� : τ

Γ � let x = E in E� : τ¬expansive(E)

Γ � E : τ Γ + x : τ � E� : τΓ � let x = E in E� : τ

expansive(E)

expansive(n) = falseexpansive(x) = false

expansive(λx.E) = falseexpansive(E1 E2) = true

expansive(let x = E1 in E2) = expansive(E1) ∨ expansive(E2)

Prof. Kwangkeun Yi SNU 4541.310 Programming Language Let-polymorphic Type System

1

expansive(n) = falseexpansive(x) = false

expansive(malloc E) = trueexpansive(!E) = expansive(E)

expansive(E1:=E2) = expansive(E1) ∨ expansive(E2)expansive(E1;E2) = expansive(E1) ∨ expansive(E2)

expansive(λx.E) = falseexpansive(E1 E2) = true

expansive(let x = E1 in E2) = expansive(E1) ∨ expansive(E2)

{f : ∀α.α → α} � f : ι → ι∀α.α → α � ι → ι 1 : ι

{f : ∀α.α → α} � f 1 : ι

{y : β, z : γ} � E1 : α → β

x : ∀α.α → β

Γ + y : ι � y : ιΓ � λy.y : ι → ι Γ � 2 : ι

Γ � (λy.y) 2 : ι

– ∀α1 . . . αn.τ–

– ι.= ι• {}: most general unifier• {α �→ ι} : a (less general) unifier

S � S� ⇔ S� = T S for some T

A substitution S is more general than a substitution S�, written S � S�, ifS� = T ◦ S for some substitution T .

– unify(α, int→ int) = {α �→ int→ int}– unify(α, int→ α) = fail– unify(α → β, int→ int) = {α �→ int, β �→ int}– unify(α → β, int→ α) =

• S = unify(α, int) = {α �→ int}• S� = unify({α �→ int}β, {α �→ int}α) = unify(β, int) = {β �→ int}• S� S = {β �→ int}{α �→ int}

– S = {a �→ b}, S� = {b �→ c}• S� (S a) = S� b = c• S (S� a) = S a = b