Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Functional Programming Tim Sheard 1 Lecture 16 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:

Similar presentations


Presentation on theme: "Advanced Functional Programming Tim Sheard 1 Lecture 16 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:"— Presentation transcript:

1 Advanced Functional Programming Tim Sheard 1 Lecture 16 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture: Extensible Records in MetaML in Haskell – the TRex extension

2 Advanced Functional Programming Tim Sheard 2 Lecture 16 Simple Records import Trex ex1 = ( a= 4, b= "z") ex2 = ( a = True, c = [1,2,3]) ex3 = #a ex1 ex4 = #a ex2 Trex library allows Show A record is a parenthesized list of label value pairs Selectors start with a #, they work on any record with that label

3 Advanced Functional Programming Tim Sheard 3 Lecture 16 Types of records Main> :t ex3 ex3 :: Integer Main> :t ex4 ex4 :: Bool Main> :t ex1 ex1 :: Rec (a :: Integer, b :: [Char]) ex1 = ( a= 4, b= "z") ex2 = ( a = True, c = [1,2,3]) ex3 = #a ex1 ex4 = #a ex2

4 Advanced Functional Programming Tim Sheard 4 Lecture 16 Order doesn't matter ex5 = ( name = "tim", age = 12 ) ex6 = (age = 12, name = "tim") Main> show ex5 "(age=12, name="tim")" Main> show ex6 "(age=12, name="tim")" Main> ex5==ex6 Bool_True

5 Advanced Functional Programming Tim Sheard 5 Lecture 16 pattern matching f (name = x, age = y) = y +1 g ( a = x ) = x Main> :t f f :: Num a => Rec (name :: b, age :: a) -> a Main> f ex5 13 Main> g ex1 ERROR: Type error in application *** Expression : g ex1 *** Term : ex1 *** Type : Rec (a :: Integer, b :: [Char]) *** Does not match : Rec (a :: Integer) *** Because : rows are not compatible

6 Advanced Functional Programming Tim Sheard 6 Lecture 16 extensible records Records can be extended h1 z = ( a = True | z ) h2 z = ( c = 24, d= True | z ) Main> show (h1 ex5) "(a=True, age=12, name="tim")" Main> show (h2 ex1) "(a=4, b="z", c=24, d=True)"

7 Advanced Functional Programming Tim Sheard 7 Lecture 16 But labels must be unique Main> h1 ex1 ERROR: Cannot satisfy constraint (a :: Integer, b :: [Char])\a Main> h2 (c=False) ERROR: Cannot satisfy constraint (c :: Bool)\c ex1 = ( a= 4, b= "z") h1 z = ( a = True | z ) h2 z = ( c = 24, d= True | z )

8 Advanced Functional Programming Tim Sheard 8 Lecture 16 Label constraints A class is like a constraint on types Show t there exists a function show with type: t -> String Eq a there exists a function (==) with type: a -> a -> Bool The constraint a\name says a is a record without a label called name. This explains Main> h1 ex1 ERROR: Cannot satisfy constraint (a :: Integer, b :: [Char])\a

9 Advanced Functional Programming Tim Sheard 9 Lecture 16 A second look at types Main> :t #a #a :: r\a => Rec (a :: b | r) -> b Main> :t h1 h1 :: z\a => Rec z -> Rec (a :: Bool | z) Main> :t h3 h3 :: (z\a, z\b, z\red) => Rec (a :: b | z) -> c -> Rec (b :: Bool, red :: c | z) h1 z = ( a = True | z ) h3 (a = _ | z ) w = (b = True, red = w | z)

10 Advanced Functional Programming Tim Sheard 10 Lecture 16 Removing labels from a record ex7 = (a = 1, b = True, c = [1], d ="z", e = 23.1 ) removeac ( a = _, c = _ | z ) = z Main> show(removeac ex7) "(b=True, d=\"z\", e=23.1)"

11 Advanced Functional Programming Tim Sheard 11 Lecture 16 selector notation The selector notation: #a is a shorthand #a = \ (a=x | r) -> x Main> :t (\ (a=x | r) -> x) \(a=x | r) -> x :: a\a => Rec (a :: b | a) -> b Main> :t #a #a :: a\a => Rec (a :: b | a) -> b

12 Advanced Functional Programming Tim Sheard 12 Lecture 16 More about record types Like all types, record types have kinds. The kind of a record type is called a "Row" BNF for "kinds" K ::= Star | K -> K | Row

13 Advanced Functional Programming Tim Sheard 13 Lecture 16 Row types ( a :: x, b:: y ) has kind Row if x has kind Star y has kind Star In general (... a i :: t i...) has kind Row if the a i are unique, and the t i have kind Star (a:: x | r ) has kind Row if x has kind Star, and r has kind Row The type constructor Rec has kind: Row -> Star

14 Advanced Functional Programming Tim Sheard 14 Lecture 16 The Lacks Predicate The lacks predicate r\a is well formed if r has kind Row, and a is a label

15 Advanced Functional Programming Tim Sheard 15 Lecture 16 Extensible records in MetaML MetaML has a similar mechanism: fun f {age=x,... } = x+1; fun g {name=x, age = w,... } = w+1; fun h {name=x, age = w, address=z,... } = w+1; fun j {name=x, age = w | z } = (w+1,{name = x |z}); -| f; val it = Fn : ['a\age].{age: int | 'a } -> int -| g; val it = Fn : ['a\age\name,'b].{age: int,name: 'b | 'a } -> int -| h; val it = Fn : ['a\address\age\name,'b,'c]. {address: 'c,age: int,name: 'b | 'a } -> int

16 Advanced Functional Programming Tim Sheard 16 Lecture 16 Comparison j (name=x, age = w | z ) = (w+1,(name = x |z)); Main> :t j j :: (a\name, a\age, Num b) => Rec (name :: c, age :: b | a) -> (b,Rec (name :: c | a)) fun j {name=x, age = w | z } = (w+1,{name = x |z}); -| j; val it = Fn : ['a\age\name,'b]. {age: int,name: 'b | 'a } -> (int * {name: 'b | 'a })

17 Advanced Functional Programming Tim Sheard 17 Lecture 16 MetaML's Dual, extensible Sums -| %student 4; val it = %student 4 : Sum{student: int | '1 } -| %teacher(3,"x"); val it = %teacher (3,"x") : Sum{teacher: (int * string) | '1 }

18 Advanced Functional Programming Tim Sheard 18 Lecture 16 More examples fun even x = (x mod 2)=0; fun f x = if even x then %even x else %odd x; val putA = %a; fun getA (%a x) = x; -| f; val it = Fn : ['a\even\odd]. int -> Sum{even: int,odd: int | 'a } -| putA ; val it = Fn : ['a\a,'b]. 'b -> Sum{a: 'b | 'a }


Download ppt "Advanced Functional Programming Tim Sheard 1 Lecture 16 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:"

Similar presentations


Ads by Google