330 likes | 460 Views
CSE-321 Programming Languages Extensions to the Simply Typed -Calculus. 박성우. POSTECH April 10, 2006. Abstract Syntax. Operational Semantics. Type System. Outline. Product types pairs generalized product types unit type Sum types Fixed point construct. Pairs in SML.
E N D
CSE-321 Programming LanguagesExtensions to the Simply Typed -Calculus 박성우 POSTECH April 10, 2006
Outline • Product types • pairs • generalized product types • unit type • Sum types • Fixed point construct
Pairs in SML - (1, true); val it = (1,true) : int * bool - #1 (1, true) ; val it = 1 : int - #2 (1, true) ; val it = true : bool
Outline • Product types • pairs V • generalized product types • unit type • Sum types • Fixed point construct
Tuples in SML - (1, true, "hello", fn x : int => x); val it = (1,true,"hello",fn) : int * bool * string * (int -> int) - #1 (1, true, "hello", fn x : int => x); val it = 1 : int - #2 (1, true, "hello", fn x : int => x); val it = true : bool - #3 (1, true, "hello", fn x : int => x); val it = "hello" : string
Generalized Product Types • We have n components. • n = 2 • pair types • n > 2 • tuple types • n = 1 • seems irrelevant • n = 0?
Outline • Product types • pairs V • generalized product types V • unit type • Sum types • Fixed point construct
Unit in SML - (); val it = () : unit
n = 0: Nullary Product Type • We need n elements to build a tuple: • I want to extract the i-th element where 1 ·i : No elimination rule for the unit type!
Outline • Product types V • pairs • generalized product types • unit type • Sum types • Fixed point construct
Datatypes in SML - datatype t = Inl of int | Inr of bool; datatype t = Inl of int | Inr of bool - val x = Inl 1; val x = Inl 1 : t - val y = Inr true; val y = Inr true : t - case (Inl 1) of Inl x => x | Inr _ => 0; val it = 1 : int - case (Inr true) of Inl x => x | Inr _ => 0; val it = 0 : int
Datatypes in SML 2007 - datatype 'int + bool' = Inl of int | Inr of bool; datatype 'int + bool' = Inl of int | Inr of bool - val x = Inl 1; val x = Inl 1 : 'int + bool' - val y = Inr true; val y = Inr true : 'int + bool' - case (Inl 1) of Inl x => x | Inr _ => 0; val it = 1 : int - case (Inr true) of Inl x => x | Inr _ => 0; val it = 0 : int
Why do we need the Elim rule then? • void type is not found in SML.
Outline • Product types V • pairs • generalized product types • unit type • Sum types V • Fixed point construct
Recursion? • Why not use the fixed point combinator? • It does not typecheck in the simply typed -calculus. • f has type A and also A ! A.