Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
예제로 본 재귀 모듈 프로그래밍과이를 위한 구문 기반 타입 시스템*
임현승 (Hyeonseung Im)
Jacques Garrigue
Keiko Nakata
Sungwoo Park
-------
* 이 연구는 A Syntactic Type System for Recursive Modules이란 제목으로SPLASH/OOPSLA 2011에서 발표할 예정입니다.
ROSAEC Center Workshop @ 파주 지지향
2011-06-28
실용적인 재귀 모듈 타입 시스템을 설계하려면
• 복시 문제
• 순홖 타입 정의
• 실용적인 타입 일치 검사 알고리즘
• 타입 안전성
• 따로 컴파일
• 모듈 초기화
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 2
타입 검사 관렦문제들
발표 순서
• 재귀 모듈 프로그래밍 예
• 복시 문제 및 해결책
• 순홖 타입 문제
• 결론 및 요약
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 3
재귀 모듈 프로그래밍 예: Tree/Forest 데이터 구조
# module rec Tree : sig
type t
val max : t -> int
end = struct
type t = Leaf of int | Node of int * Forest.f
let max = function
| Leaf i -> i
| Node (i, f) -> Pervasives.max i (Forest.max f)
end
and Forest : sig
type f
val max : f -> int
end = struct
type f = Tree.t list
let rec max = function
| [] -> 0
| t :: ts -> Pervasives.max (Tree.max t) (max ts)
end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 4
데이터 추상화모듈화 유지 보수 용이
새로운 함수 추가
# module rec Tree : sig
type t val max : t -> int
val mk_tree : Forest.f -> tend = struct
type t = Leaf of int | Node of int * Forest.f
let max = function ...
let mk_tree f = Node (Forest.max f, f)end
and Forest : sig
type f val max : f -> int
val combine : f -> f -> Tree.tend = struct
type f = Tree.t list
let rec max = function ...
let combine f1 f2 = Tree.mk_tree (f1 @ f2)end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 5
복시 문제 (double vision problem)
# module rec Tree : sig
type t val max : t -> int
val mk_tree : Forest.f -> tend = struct
type t = Leaf of int | Node of int * Forest.f
let max = function ...
let mk_tree f = Node (Forest.max f, f)
end
and Forest : sig
type f val max : f -> int
val combine : f -> f -> Tree.t
end = struct
type f = Tree.t list
let rec max = function ...
let combine f1 f2 = Tree.mk_tree (f1 @ f2)(* 타입 오류 *)end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 6
타입 시스템이α list ≈ Forest.f가성립함을 알 수 없음
내부 이름 ≠외부 이름
해결책: 기본 아이디어
# module rec Tree : sig (X)
type t val max : X.t -> int
val mk_tree : Forest.f -> X.t
end = struct (X)type t = Leaf of int | Node of int * Forest.f
let max = function ...
let mk_tree f = X.Node (Forest.max f, f)
end
and Forest : sig (Y)
type f val max : Y.f -> int
val combine : Y.f -> Y.f -> Tree.t
end = struct (Y)type f = Tree.t list
let rec max = function ...
let combine f1 f2 = Tree.mk_tree (f1 @ f2)end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 7
내부 재귀 변수(recursion
variable) 추가
해결책: 모듈 경로 치홖 (path substitution)
# module rec Tree : sig (X)
type t val max : X.t -> int
val mk_tree : Forest.f -> X.t
end = struct (X)type t = Leaf of int | Node of int * Forest.f
let max = function ...
let mk_tree f = X.Node (Forest.max f, f)
end
and Forest : sig (Y)
type f val max : Y.f -> int
val combine : Y.f -> Y.f -> Tree.t
end = struct (Y)type f = Tree.t list
let rec max = function ...
let combine f1 f2 = Tree.mk_tree (f1 @ f2)end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 8
Tree -> X
로 치홖
Forest -> Y
로 치홖
Forest.f -> Y.f -> Tree.t list
우리의 문법 구조로 쓴 재귀 모듈 예module type ST(type f) = rec(Y)sig
type t
val max : Y.t -> int
val mk_tree : f -> Y.t
end
module type SF(type t) = rec(Z)sig
type f
val max : Z.f -> int
val combine : Z.f -> Z.f -> t
end
module type S = rec(X)sig
module Tree : ST(X.Forest.f)
module Forest : SF(X.Tree.t)
end
rec(X : S)struct
module Tree : ST(X.Forest.f) =
rec(Y : ST(X.Forest.f) with
type t = Leaf of int
| Node of int * X.Forest.f
)struct
type t = ...
let max = function ...
let mk_tree f = ...
end
module Forest : SF(X.Tree.t) =
rec(Z : SF(X.Tree.t) with
type f = X.Tree.t list
)struct
type f = ...
let rec max = function ...
let combine f1 f2 =
X.Tree.mk_tree (f1 @ f2)
end
end2011-06-28 ROSAEC Center Workshop @ 파주 지지향 9
복시 문제 해결 다시 보기
rec(X : S)struct
module Tree : ST(X.Forest.f) =
rec(Y : ST(X.Forest.f) with
type t = Leaf of int
| Node of int * X.Forest.f)
struct
type t = ...
let max = function ...
let mk_tree f = ...
end
module Forest : SF(X.Tree.t) =
rec(Z : SF(X.Tree.t) with type f = X.Tree.t list)
struct
type f = ...
let rec max = function ...
let combine f1 f2 = X.Tree.mk_tree (f1 @ f2)
end
end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 10
X.Tree -> Y
X.Forest -> Z
X.Forest.f -> Z.f = X.Tree.t list ≈ 'a list
복시 문제 요약
• 내부 재귀 변수를 도입하고 모듈 경로 치홖 기법을 이용하여해결
• 특징– 구문 기반 (syntactic approach)
– 모듈 언어에서 발생하는 복시 문제 해결
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 11
발표 순서
• 재귀 모듈 프로그래밍의 전형적 예
• 복시 문제 및 해결책
• 순홖 타입 문제
• 결론 및 요약
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 12
순홖 타입(cyclic types)이란?
• 차단된 타입 순홖 (guarded type cycles)
• 명백한 타입 순홖 (transparent type cycles)
• 감춰진 타입 순홖 (opaque type cycles)
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 13
순홖 타입(cyclic types)이란?
• 차단된 타입 순홖 (guarded type cycles)
• 명백한 타입 순홖 (transparent type cycles)
• 감춰진 타입 순홖 (opaque type cycles)
# module M : sig
module rec N : sig
type s = Barrier of N.s (* guarded *)
type t = N.t (* transparent *)
type u = N.u * N.u (* transparent *)
end
end = struct ... end
# module rec M : sig type t end =
struct type t = N.t end (* opaque *)
and N : sig type t end =
struct type t = M.t end (* opaque *)
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 14
왜 순홖 타입이 문제가 되는가?
• 차단된 타입 순홖– 문제 없음
• 명백한 타입 순홖– 실용적인 타입 검사 알고리즘을 설계하기 매우 어려움
– 타입 안전성 증명에서 타입 순홖을 고려해야 함
– 그러나 타입 시스템이 쉽게 찾아낼 수 있음
• 감춰진 타입 순홖– Type abstraction을 제거하지 않고는 일반적으로 검출 불가능
– 보수적으로 검출하면 언어의 표현력이 작아짐; 특히 functor fixpoints 홗용 방법이 적어짐
– 허용하면 타입 안전성 증명에서 타입 순홖을 고려해야 함
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 15
왜 감춰진 타입 순홖이 어려운가?
# module rec Forest : sig
type fval max : f -> int
end = struct
type f = Tree.t listlet rec max = function
| [] -> 0
| t :: ts -> Pervasives.max (Tree.max t) (max ts)
end
and Tree : sig
type tval max : t -> int
end = struct
type t = Leaf of int | Node of int * Forest.flet max = function ...
end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 16
이 시점에서 Forest.f 와Tree.t 가 어떤 타입 순홖을
만드는지 알 수 없음
우리의 접근법
• 명백한 타입 순홖만 제거
• 감춰진 타입 순홖은 허용
• 따라서 타입 안전성 증명 시 타입 순홖을 고려해야 함
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 17
명백한 타입 순홖을 허용하는 강력한 타입 시스템
실용적 타입 시스템
타입 안전성 증명 증명 재사용
Weak bisimulations!
Type normalization!
발표 순서
• 재귀 모듈 프로그래밍의 전형적 예
• 복시 문제 및 해결책
• 순홖 타입 문제
• 결론 및 요약
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 18
결론 및 요약
• 복시 문제를 해결
• 약한 상호 흉내내기 기법을 이용하여 순홖 타입을 지원하는타입 시스템 설계
• 이를 바탕으로 타입 정규화를 기반으로 하는 실용적 타입 시스템 설계
• 실행과정을 드러내는 의미구조(operational semantics)를정의하고 타입 안전성 증명
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 19
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 20
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 21
Backup Slides
재귀 모듈 프로그래밍 예: Tree/Forest 데이터 구조
(* Tree 데이터 구조 *)
# module rec Tree : sig
type t = Leaf of string | Node of Forest.t
val compare: t -> t -> int
end
= struct
type t = Leaf of string | Node of Forest.t
let compare t1 t2 =
match (t1, t2) with
| (Leaf s1, Leaf s2) -> Pervasives.compare s1 s2
| (Leaf _, Node _) -> 1
| (Node _, Leaf _) -> -1
| (Node n1, Node n2) -> Forest.compare n1 n2
end
and Forest : Set.S with type elt = Tree.t
= Set.Make(Tree) (* 이미 구현되어 있는 Set 라이브러리를 이용 *)
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 22
복시 문제 기존 해결책
• Dreyer (ICFP 2007): 의미론 기반 (semantic approach)
• Montagu and Remy (POPL 2009): 코어 언어에서 발생하는복시 문제 해결
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 23
문법구조
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 24
모듈 언어
코어 언어
Only forward references via recursion variables
are allowed
타입 시스템 맛보기
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 25
외부 이름 -> 내부 이름
Type equivalence by weak bisimilarity
모듈 경로 치홖 확장
타입 동치 = 약한 상호 흉내 (weak bisimilarity)
• 조용한 타입 변홖 (silent type transition)– 타입 정의를 unfolding
e.g., type t = s, type s = int |- t → s → int
• 레이블이 있는 타입 변홖 (labeled type transition)– Type constructors, 데이터 타입, 추상 타입 변홖
e.g., int * bool → int (label: fst)
int * bool → bool (label: snd)
type t |- t → 0 (label: t)
• 두 타입 t와 s가 서로 약하게 흉내낸다 (weakly bisimilar)– t의 모든 타입 변홖에 대하여 이에 대응되는 s의 타입 변홖이 존재,
and vice versa
• 강력하지만 일반적으로 실용적이지 못 함
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 26
약한 상호 흉내내기 예
rec(X : S)struct
module Tree : ST(X.Forest.f) =
rec(Y : ST(X.Forest.f) with
type t = Leaf of int
| Node of int * X.Forest.f)
struct
type t = ...
let max = function ...
let mk_tree f = ...
end
module Forest : SF(X.Tree.t) =
rec(Z : SF(X.Tree.t) with type f = X.Tree.t list)
struct
type f = ...
let rec max = function ...
let combine f1 f2 = X.Tree.mk_tree (f1 @ f2)
end
end
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 27
X.Forest -> Z
X.Forest.f 'a list
Z.f
X.Tree.t list
'a list
'a list
타입 정규화 (type normalization) 기반 타입 동치
• 실용적 타입 시스템을 위해 설계
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 28
1. 타입 정규화2. 구문 비교
명시적 순환 타입 검출
앞으로 할 일
• 프로토타입 시스템 구현
• 타입 재구성 (type reconstruction) 알고리즘 설계
• 타입 정의에 타입 매개변수 (type parameter) 추가
• 따로 컴파일 아이디어 formulation
2011-06-28 ROSAEC Center Workshop @ 파주 지지향 29