Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture: Staged computation Difference between Haskell and ML Intro to MetaML Using staging to control evaluation order

2 Advanced Functional Programming Tim Sheard 2 Lecture 17 Differences in Types type variables and type application 'a Tree type constructors int, string, bool tuples (int * string * int) type variables and type application Tree a type constructors Int, String, Bool tuples (Int, String, Bool)

3 Advanced Functional Programming Tim Sheard 3 Lecture 17 Differences (* Data definitions *) datatype 'a Tree = Tip of 'a | Fork of ('a Tree)*('a Tree) 'a = type variable constructors are not capitalized -- data definitions data Tree a = Tip a | Fork (Tree a) (Tree a) comments Type constructors are post fix constructor functions are always uncurried (use tuples) note use of "of"

4 Advanced Functional Programming Tim Sheard 4 Lecture 17 difference 2 (* function definitions *) fun fact n = if n=0 then 1 else n * (fact (n-1)); fun depth (Tip a) = 0 | depth (Fork(x,y)) = 1 + max (depth x, depth y); -- function definitions fact n = if n==0 then 1 else n * (fact (n-1)) depth (Tip a) = 0 depth (Fork x y) = 1 + max (depth x) (depth y) note use of "fun" equality is double = use "bar" (|) to separate clauses primitives more likely to be curried in Haskell

5 Advanced Functional Programming Tim Sheard 5 Lecture 17 Data List a = [] | (:) a (list a) map f [] = [] map f (x:xs)=(f x):(map f xs) pi = 3.14159 \ x -> x + 1 Datatype ‘a list = [] | (op ::) of ‘a * (‘a list) fun map f [] = … | map f (x::xs) = val pi = 3.14159 fn x => x + 1 Differences between Haskell & ML

6 Advanced Functional Programming Tim Sheard 6 Lecture 17 Some Primitives - op +; val it = fn:int * int -> int - op *; val it = fn:int * int -> int - op -; val it = fn:int * int -> int - op @; val it = fn:'a list * 'a list -> 'a list - length; val it = fn:'a list -> int - op ::; val it = fn:'a * 'a list -> 'a list - hd; val it = fn:'a list -> 'a - tl; val it = fn:'a list -> 'a list Main> :t (+) (+) :: Num a => a -> a -> a Main> :t (*) (*) :: Num a => a -> a -> a Main> :t (-) (-) :: Num a => a -> a -> a Main> :t (++) (++) :: [a] -> [a] -> [a] Main> :t length length :: [a] -> Int Main> :t (:) (:) :: a -> [a] -> [a] Main> :t head head :: [a] -> a Main> :t tail tail :: [a] -> [a]

7 Advanced Functional Programming Tim Sheard 7 Lecture 17 Differences 3 (* variable definition *) val tree1 = Fork(Fork(Tip 1,Tip 2), Tip 3); -- variable definition tree1 = Fork (Fork (Tip 1) (Tip 2)) (Tip 3) use val to declare variables

8 Advanced Functional Programming Tim Sheard 8 Lecture 17 functions fn x => x + 1 let fun f x = x + 1 in f 7 end has type in ML is : datatype T =... datatype ('a,'b) T =... = (in MetaML only) 'a ('b T) \ x -> x + 1 let f x = x + 1 in f 7 has type in Haskell is :: data T =... data T b a =...

9 Advanced Functional Programming Tim Sheard 9 Lecture 17 case expressions case x of [] => 5 | (x::xs) => len xs + m3 case x of [] -> 5 (x:xs) -> len xs + m3

10 Advanced Functional Programming Tim Sheard 10 Lecture 17 Differences 4 (* booleans *) - true; val it = true : bool - false; val it = false : bool (* mutual recursion *) fun even 0 = true | even n = odd (n-1) and odd 0 = false | odd n = even (n-1); -- booleans Main> :t True True :: Bool Main> :t False False :: Bool -- mutual recursion even 0 = True even n = odd (n-1) odd 0 = False odd n = even (n-1) No capitals use of "and" must separate mutually recursive functions all functions are implicitly mutually recursive

11 Advanced Functional Programming Tim Sheard 11 Lecture 17 Intro to MetaML

12 Advanced Functional Programming Tim Sheard 12 Lecture 17 Controlling Evaluation Order We want more than just the correct result! We want to control other resources such as time and space without resorting to tricks or unnatural programming styles. Mechanism - Control Evaluation Order

13 Advanced Functional Programming Tim Sheard 13 Lecture 17 Traditional Approaches Fixed evaluation order with language extensions Lazy - Outermost add strictness annotations Strict - Innermost add annotations like force and delay Encode laziness using lambda in a strict setting datatype 'a lazylist = lazyNil | lazyCons of 'a * (unit -> 'a lazylist); fun count n = lazyCons(n, fn () => count (n+1))

14 Advanced Functional Programming Tim Sheard 14 Lecture 17 Limitations None of these approaches allow computation under a lambda! This is sometimes just what is needed. For example: fun power n = (fn x => if n=0 then 1 else x * (power (n-1) x)) power 2;

15 Advanced Functional Programming Tim Sheard 15 Lecture 17 Example reduction (power 2) unfold the definition (fn x => if 2=0 then 1 else x * (power (2-1) x)) perform the if, under the lambda (fn x => x * (power (2-1) x)) unfold power again (fn x => x * ((fn x => if 1=0 then 1 else x * (power (1-1) x)) x)) use the beta rule to apply the explicit lambda to x

16 Advanced Functional Programming Tim Sheard 16 Lecture 17 Example (cont.) (fn x => x * (if 1=0 then 1 else x * (power (1-1) x))) perform the if (fn x => x * (x * (power (1-1) x))) unfold power again (fn x => x * (x * ((fn x => if 0=0 then 1 else x * (power (0-1) x))) x)) use the beta rule to apply the explicit lambda to x (fn x => x * (x * (if 0=0 then 1 else x * (power (0-1) x)))) perform the if (fn x => x * (x * 1))

17 Advanced Functional Programming Tim Sheard 17 Lecture 17 Solution - Use richer annotations Brackets: no reductions allowed in e delay computation if e:t then : (pronounced code of t) Escape: ~ e relax the no reduction rule of brackets above Escape may only occur inside Brackets splice code together to build larger code Run: run e remove outermost brackets force computations which have been delayed

18 Advanced Functional Programming Tim Sheard 18 Lecture 17 Calculus A calculus describes equivalences between program fragments. The rules of a calculus can be applied in any order. An implementation applies the rules in some fixed order. Traditional rules beta – (fn x => e) v  e[v/x] if – if true then x else y  x – if false then x else y  y delta – 5 + 2  7

19 Advanced Functional Programming Tim Sheard 19 Lecture 17 Rules for code Introduction rule for code 1st elimination rule for code (escape-bracket elim) … > ---> ~ must appear inside enclosing brackets e must be escape free must be at level 0 2nd elimination rule for code (run-bracket elim) run ---> e provided e has no escapes the whole expression is at level 0

20 Advanced Functional Programming Tim Sheard 20 Lecture 17 Power example revisited (* power : int -> -> *) fun power n = fn x => if n=0 then else (* ans : int > *) val ans = ~(power 2 ) >;

21 Advanced Functional Programming Tim Sheard 21 Lecture 17 ~ (power 2 ) > ~(if 2=0 then else * ~(power (2-1) ) >)> ~ * ~(power (2-1) ) >>) ~ ) >>) ~< z * ~(if 1=0 then else * ~(power (1-1) ) >) >>)

22 Advanced Functional Programming Tim Sheard 22 Lecture 17 ~ * ~(power (1-1) ) >>> ~ ) >>> ~ >>> ~ >> ~ > z * z * 1>

23 Advanced Functional Programming Tim Sheard 23 Lecture 17 MetaML: Meta-programming Meta-Programming: Programs that write programs What Infrastructure is possible in a language designed to help support the algorithmic construction of other programs? Advantages of meta-programs capture knowledge efficient solutions design ideas can be communicated and shared

24 Advanced Functional Programming Tim Sheard 24 Lecture 17 Types of meta-programs Static generators Generate code which is then “written to disk” and processed by normal compilers etc. Two kinds Homogeneous systems object language = meta-language Heterogeneous systems object-language different from meta-language Run-time generators Staged Programs Programs that generate other programs, and then execute the programs they generated MetaML is a staged programming language which does run-time code generation

25 Advanced Functional Programming Tim Sheard 25 Lecture 17 MetaML & the Mustang Project Theory Semantics of staged programs What does it mean to have a staged program? How can we tell if a staged program computes the same result as its unstaged counterpart? Calculus of programs - Rules for reasoning Staged type systems A static type system keeps bad programs from executing by throwing away bad programs by refusing to compile them. A staged program admits more bad things. E.g. using a variable before it is defined, or executing a program before its complete, for example. Staged languages need richer type systems to throw away these bad programs. Automatic staging partial evaluation

26 Advanced Functional Programming Tim Sheard 26 Lecture 17 MetaML & the Mustang Project Tools Staged Languages MetaML reflective: obj-lang = meta-lang = ML multi-stage Heterogeneous ( meta-lang=ML, obj-lang varies, 2 stage )

27 Advanced Functional Programming Tim Sheard 27 Lecture 17 Building pieces of code -| ; val it = : -| ; val it = : -| ; val it = : -| x + 5>; val it = a %+ 5)> : int> -| >; val it = )> : >

28 Advanced Functional Programming Tim Sheard 28 Lecture 17 Let’s try it ! Run MetaML

29 Advanced Functional Programming Tim Sheard 29 Lecture 17 Composing pieces of code -| val x = ; val x = : -| val y = ; val y = : -| fun pair x = ; val pair = Fn : ['b]. -> -| pair ; val it = :

30 Advanced Functional Programming Tim Sheard 30 Lecture 17 Executing constructed code -| val x = ; val x = : ['a]. -| run x; val it = 36 : int -| fun id x = x; val id = Fn : ['a].'a -> 'a -| val q = ) >; val q = : -| run q; val it = 6 : int

31 Advanced Functional Programming Tim Sheard 31 Lecture 17 Multi-stage code -| val z = >; val z = )> : > -| val f = run z; val f = Fn : int -> -| f 12; val it = : -| run it; val it = 13 : int

32 Advanced Functional Programming Tim Sheard 32 Lecture 17 The lift operator -| ; val it = : -| lift (4+1); val it = : -| val z = >; val z = )> : > -| run z; val it = Fn : int -> -| it 5; val it = : -| run it; val it = 6 : int

33 Advanced Functional Programming Tim Sheard 33 Lecture 17 Lift v.s. lexical capture Lift cannot be used on functions -| lift id; Error: The term: id Non Qualified type, not liftable: ? -> ? Lift makes code readable -| fun f x = ; val f = Fn : ['b^].'b -> -| f 3; val it = : Lexical capture is more efficient -| lift [1+3,4,8-4]; val it = :


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

Similar presentations


Ads by Google