Today: … simple language extensions … 1. Derived Forms 2. Labeled Records Type Systems 3. Labeled Variants 4. Lists Lecture 4 Nov. 10th, 2004 5. Normalization Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 1. Derived Forms 1. Derived Forms Example Sequencing. Idea Give more freedom to the programmer by introducing new syntactic forms f to the surface language L. First, add new type Unit with unique constant value uni t , and typing rule Γ ` uni t : Unit If A. the evaluation behavior and � similar to voi d in languages like C or Java. B. the typing behavior � useful if we care about side effects , not result. of f can be derived from those of L, then f is a derived form of L . Sequencing: t 1 ; t 2 = “evaluate t 1 , throw away its trivial result, then evaluate t 2 .” Derived forms give more freedom to the language designer, Possible evaluation / typing rules because the complexity of the internal language does not change. t 1 � t 1 ’ Γ ` t 1 : Unit Γ ` t 2 : T 2 uni t ; t 2 � t 2 t 1 ; t 2 � t 1 ’ ; t 2 � type safety (progress+preservation) need NOT be reproved! Γ ` t 1 ; t 2 : T 2 1. Derived Forms 1. Derived Forms Example Sequencing. Example Sequencing. Γ ` t 1 : Unit Γ ` t 2 : T 2 Γ ` t 1 : Unit Γ ` t 2 : T 2 Γ ` t 1 ; t 2 : T 2 Γ ` t 1 ; t 2 : T 2 � similar to l et / application of an abstraction � similar to l et / application of an abstraction � is there a lambda term with same typing?? � is there a lambda term with same typing?? := ( λ x:Unit. t 2 ) t 1 x ∉ FV(t 2 ), fresh! YES! Define t 1 ; t 2 Γ ` ( λ x:Unit. t 2 ) t 1 : T 2 t 1 � t 1 ’ t 1 � t 1 ’ uni t ; t 2 � t 2 uni t ; t 2 � t 2 t 1 ; t 2 � t 1 ’ ; t 2 t 1 ; t 2 � t 1 ’ ; t 2 1
1. Derived Forms 1. Derived Forms Example Sequencing. Example Sequencing. Γ ` t 1 : Unit Γ ` t 2 : T 2 Γ ` t 1 : Unit Γ ` t 2 : T 2 Γ ` t 1 ; t 2 : T 2 Γ ` t 1 ; t 2 : T 2 � similar to l et / application of an abstraction � similar to l et / application of an abstraction � is there a lambda term with same typing?? � is there a lambda term with same typing?? := ( λ x:Unit. t 2 ) t 1 x ∉ FV(t 2 ), fresh! := ( λ x:Unit. t 2 ) t 1 x ∉ FV(t 2 ), fresh! YES! Define t 1 ; t 2 YES! Define t 1 ; t 2 Γ, x:Unit ` ` t 2 : T 2 Γ ` ( λ x:Unit. t 2 ) : Unit � T 2 Γ ` t 1 : Unit Γ ` ( λ x:Unit. t 2 ) : Unit � T 2 Γ ` t 1 : Unit Γ ` ( λ x:Unit. t 2 ) t 1 : T 2 Γ ` ( λ x:Unit. t 2 ) t 1 : T 2 t 1 � t 1 ’ t 1 � t 1 ’ uni t ; t 2 � t 2 uni t ; t 2 � t 2 t 1 ; t 2 � t 1 ’ ; t 2 t 1 ; t 2 � t 1 ’ ; t 2 1. Derived Forms 1. Derived Forms Example Sequencing. Example Sequencing. Γ ` t 1 : Unit Γ ` t 2 : T 2 Γ ` t 1 : Unit Γ ` t 2 : T 2 Γ ` t 1 ; t 2 : T 2 Γ ` t 1 ; t 2 : T 2 � similar to l et / application of an abstraction � similar to l et / application of an abstraction � is there a lambda term with same typing?? � is there a lambda term with same typing?? e := ( λ x:Unit. t 2 ) t 1 x ∉ FV(t 2 ), fresh! := ( λ x:Unit. t 2 ) t 1 x ∉ FV(t 2 ), fresh! YES! Define t 1 ; t 2 YES! Define t 1 ; t 2 Γ ` t 2 : T 2 x ∉ FV(t 2 ) Γ, x:Unit ` ` t 2 : T 2 syntactic sugar “ desugaring ” Γ ` ( λ x:Unit. t 2 ) : Unit � T 2 Γ ` t 1 : Unit Γ ` ( λ x:Unit. t 2 ) t 1 : T 2 t 1 � t 1 ’ t 1 � t 1 ’ A. t � ext t’ iff e(t) � int e(t’) e: ext � int uni t ; t 2 � t 2 uni t ; t 2 � t 2 t 1 ; t 2 � t 1 ’ ; t 2 t 1 ; t 2 � t 1 ’ ; t 2 Γ ` ext t : T iff Γ ` int e(t) : T B. not needed anymore!! 1. Derived Forms 2. Labeled Records Example Sequencing. { x=5} record of type { x:Nat } { par t no=5524, avai l abl e=t r ue} record of type { partno:Nat, available:Bool } Questions: � selection: { x=5, y=6} . y 6 1. Can you prove that ; is a derived form (= A. and B.) evaluation 2. Is l et a derived form? � v j { l 1 =v 1 , … , l n =v n } . l j “if everything is value, you can select” t 1 � t 1 ’ “evaluate inside of selections, … t 1 . l � t 1 ’ . l � t j ’ t j …, from left to right.” t 1 � t 1 ’ t � ext t’ iff e(t) � int e(t’) e: ext � int A. uni t ; t 2 � t 2 , l n =t n } � { l 1 =v 1 , … , l j - 1 =v j - 1 , l j =t j , … t 1 ; t 2 � t 1 ’ ; t 2 ( � ordered !) Γ ` ext t : T iff Γ ` int e(t) : T B. { l 1 =v 1 , … , l j - 1 =v j - 1 , l j =t j ’ , … , l n =t n } not needed anymore!! 2
2. Labeled Records 2. Labeled Records { x=5} record of type { x:Nat } Note: our records are ordered : { par t no=5524, avai l abl e=t r ue} { x=5, y=6} is NOT the same as { y=6, x=5} record of type { partno:Nat, available:Bool } � selection: { x=5, y=6} . y 6 � { x:Nat, y:Nat } ≠ { y:Nat, x:Nat } typing Will change in the presence of subtyping. Γ ` t 1 : T 1 , …, Γ ` t n : T n Γ ` { l 1 =t 1 , … (then, one will be a subtype of the other, i.e., terms of the one , l n =t n } : { l 1 : T 1 , … , l n : T n } type can be used in any context where terms of the other are expected) Γ ` t 1 : { l 1 : T 1 , … , l n : T n } Γ ` t 1 . l j : T j 3. Labeled Variants 3. Labeled Variants (like records: ordered !) Often programs deal with heterogeneous collections of values . evaluation: i ∈ 1…n e.g., a node of a binary tree can be internal or a leaf case ( <l j =v j > as T) of <l i =x i > � t i � [x j � v j ] t j a list can be nil (empty) or consisting of a head and tail etc. t 0 � t 0 ’ case t 0 of <l i =x i > � t i � of <l i =x i > � t i case t 0 ’ variant type: i ∈ 1…n i ∈ 1…n Addr = <physi cal : Physi cal Addr , vi r t ual : Vi r t ual Addr > � t i ’ t i a = <physi cal =pa> as Addr ; <l i =t i > as T � <l i =t i ’ > as T variant value Γ ` t j : T j � test which “internal” type a variant value has: case typing: Γ ` <l j =t j > as <l i : T i > : <l i : T i > e = λ a: Addr . case a of get Nam i ∈ 1…n <pyhsi cal =x> � x. f i r st l ast Γ ` t 0 : <l i : T i > for each i Γ , x i : T i ` t i : T <vi r t ual =y> � y. nam | e Γ ` case t 0 of <l i =x i > � t i : T i ∈ 1…n 3. Labeled Variants 3. Labeled Variants Some useful variants: a. Options Some useful variants: a. Options b. Enumerations b. Enumerations c. Single-Field Variants c. Single-Field Variants a. Options a. Options O pt Nat = <none: Uni t , som e: Nat > O pt Nat = <none: Uni t , som e: Nat > Tabl e = Nat � O Tabl e = Nat � O pt Nat partial functions on numbers pt Nat partial functions on numbers � how to define the empty table? � how to define the empty table? pt yTabl e = λ n: Nat . <none=uni t > as O pt yTabl e = λ n: Nat . <none=uni t > as O em pt Nat em pt Nat � how to update (m, v) of a table? � how to update (m, v) of a table? updat e = λ t : Tabl e. λ m : Nat . λ v: Nat . λ n: Nat i f equal n m t hen <som e=v> as O pt Nat el se t n 3
3. Labeled Variants 3. Labeled Variants Some useful variants: a. Options Some useful variants: a. Options b. Enumerations b. Enumerations c. Single-Field Variants c. Single-Field Variants a. Options a. Options O pt Nat = <none: Uni t , som e: Nat > O pt Nat = <none: Uni t , som e: Nat > Tabl e = Nat � O Tabl e = Nat � O pt Nat partial functions on numbers pt Nat partial functions on numbers � how to define the empty table? � table lookup: (e.g., of entry ‘5’) pt yTabl e = λ n: Nat . <none=uni t > as O em pt Nat x = case t ( 5) of � how to update (m, v) of a table? (type Table � Nat � Nat � Table) <none=u> � 0 e=v> � v | <som updat e = λ t : Tabl e. λ m : Nat . λ v: Nat . λ n: Nat i f equal n m t hen <som e=v> as O pt Nat el se t n 3. Labeled Variants 3. Labeled Variants Some useful variants: a. Options Some useful variants: a. Options b. Enumerations b. Enumerations c. Single-Field Variants c. Single-Field Variants a. Enumerations a. Single-Field Variants dol l ar s2eur os = λ d: Fl oat . t i m esf l oat d 0. 8145 W eekday = <m onday: Uni t , t uesday: Uni t , wednesday: Uni t , eur os2dol l ar s = λ d: Fl oat . t i m t hur sday: Uni t , f r i day: Uni t > esf l oat d 1. 2277 eur os2dol l ar s( dol l ar s2eur os 39. 50) � 39. 4984 function that returns the next buisiness day: next Bui si nessDay = λ w: W eekday. case w of onday=x> � <t uesday=uni t > as W <m eekday <t uesday=x> � <wednesday=uni t > as W eekday . . <f r i day=x> � <m onday=uni t > as W eekday 3. Labeled Variants 3. Labeled Variants Some useful variants: a. Options Some useful variants: a. Options b. Enumerations b. Enumerations c. Single-Field Variants c. Single-Field Variants a. Single-Field Variants a. Single-Field Variants dol l ar s2eur os = λ d: Fl oat . t i m esf l oat d 0. 8145 Dol l ar Am ount = <dol l ar s: Fl oat > eur os2dol l ar s = λ d: Fl oat . t i m esf l oat d 1. 2277 Eur oAm ount = <eur os: Fl oat >; eur os2dol l ar s( dol l ar s2eur os 39. 50) � 39. 4984 dol l ar 2eur os = λ d: Dol l ar Am ount . case d of <dol l ar s=x> � But, dol l ar s2eur os( dol l ar s2eur os 39. 50) <eur os=t i m esf l oat x 0. 8145> as Eur oAm ount nonsense! ount � Eur oAm Type of dol l ar 2eur os: Dol l ar Am ount 4
Recommend
More recommend