최종욱
가입: 2009년 9월 15일 올린 글: 84
|
올려짐: 2010년11월26일 20:56 주제: Homework 7 Polymorphism test |
|
|
simple type과 관련한 테스트들(기본적인 동작)은 아래 올린 6-1, 6-2 테스트셋을 참고하시면 되구요
이중에서는 polymorphism으로는 통과해야하지만 simple type checking이라 통과하지 못해서 error로 되어있는 코드들이 몇 개 있을텐데요,
그런 것들은 눈치껏 잘 테스트를 해주시고...
여기서는 polymorphism과 관련된 것들만 몇 개 더...
숙제의 테스트 입력에서는 일반화된 타입으로 결과가 나오는 경우가 하나도 없는데
올바르게 짠 것을 테스트하기 위해서는 일반화된 결과를 찍어보...는게 편리합니다.
그래서 아래 코드들은 (숙제에서 들어오는 입력은 아니지만) output에 일반화된 타입이 있을 수 있습니다.
일반화된 타입이 맞는가를 테스트해보기 위해서는 여러분의 구현에 맞는 toString 함수를 장착하시면 되겠습니다. (결과가 simple type으로 나오는 경우는 그냥 됩니다)
====================================
숙제 문서의 예제들
====================================
코드: |
(* example 1: polymorphic toys *)
let val I = fn x => x
val add = fn x => x.1 + x.1
val const = fn n => 10
in
I I;
add(1, true) + add(2, "snu 310 fall 2009");
const 1 + const true + const "kwangkeun yi"
end
|
int
코드: |
(* example 2: polymorphism with imperatives *)
let val f = fn x => malloc x
in
let val a = f 10
val b = f "pl"
val c = f true
in
a := !a + 1;
b := "hw7";
c := !c or false
end
end
|
bool
코드: |
(* example 3: polymorphic swap *)
let val swap =
fn order_pair =>
if (order_pair.1) (order_pair.2)
then (order_pair.2)
else (order_pair.2.2, order_pair.2.1)
in
swap(fn pair => pair.1 + 1 = pair.2, (1,2));
swap(fn pair => pair.1 or pair.2, (true, false))
end
|
(bool, bool)
코드: |
(* example 4 : S K I combinators *)
let val I = fn x => x
val K = fn x => fn y => x
val S = fn x => fn y => fn z => (x z) (y z)
in
S (K (S I)) (S (K K) I) 1 (fn x => x+1)
end
|
int
====================================
a polymorphism test
====================================
int
('a -> 'b) -> ('a' -> 'b)
코드: |
let rec f = fn x=>x in
f "이것은"; f true; f "폴리모픽"; f "identity"; f ("function", 1); f (malloc false)
end
|
loc(bool)
코드: |
let rec f = fn x => (f "wook" ; f x) in (f 1) end
|
ERROR (string, int unification fail)
코드: |
let rec f = fn x => (f "wook" ; f x) in f end
|
string -> 'a
코드: |
let rec f = fn x=> let rec g = fn x => f x in g x end in f end
|
'a -> 'b
코드: |
let rec f = fn x=> let rec g = fn x => f x in g x end in f; (f true) + 1; (f 1) and false end
|
bool (무한루프)
코드: |
let rec f = fn x => f f in f end
|
error (타입이 정의되지 않음)
코드: |
let val f = fn x => fn y => write (write x; y) in
f end
|
'a -> 'b -> 'b
====================================
write, eq의 polymorphism with constraints 는 조금(?) 까다롭습니다.
====================================
코드: |
(fn x=> write x) write (3, 4)
|
error
코드: |
let val f = fn x => write x in
f 1; f true; f "asdf" end
|
string
코드: |
let val f = fn x => write x in
f 1; f true; f "asdf"; f (1,2)
end
|
error ( pair is not able to be written )
코드: |
let val g = fn t => let rec f = fn x=> f (write x) in f t end in g (malloc 1) end
|
error (write location ?)
error (pair is not comparable)
코드: |
let val comp = fn x=> fn y=> (x = y) in
comp 3 1;
comp "asdf" "gg";
comp (fn x=>1) (fn y=>1)
end
|
error (function is not comparable)
코드: |
let val comp = fn x=> fn y=> (x = y) in
comp 3 1;
comp "asdf" "gg"
(* syntax error수정했어요 *)
end
|
bool
코드: |
let val f = fn x => fn y => write (write x; y) in
f (0, 0)
end
|
error ( pair 는 write 불가) _________________ Jongwook Choi
Seoul National University, School of Computer Science & Engineering |
|