Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004.

Similar presentations


Presentation on theme: "Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004."— Presentation transcript:

1 Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004

2 2Cse583 Winterl 2002 Discussion of homework 2 We have add : int -> int -> int mult : int -> int -> int member : ['a=].'a list -> 'a -> bool dotprod : int -> int list -> int list -> int We want add' : int -> -> mult' : int -> int> member' : 'a list -> -> dotprod' int -> int list -> int>

3 3Cse583 Winterl 2002 Add Have: add : int -> int -> int Want: add' : int -> -> fun add 0 y = y | add n y = 1 + (add (n-1) y); fun add' 0 y = y | add' n y = ; Example -| add' 3 ; val it = :

4 4Cse583 Winterl 2002 mult Have: mult : int -> int -> int Want: mult' : int -> int> fun mult 0 y = 0 | mult n y = y + (mult (n-1) y); fun mult' 0 = 0> | mult' n = y + ~(mult' (n-1)) y>; Note type of result int>

5 5Cse583 Winterl 2002 Unfold mult’ by hand (mult’ 2) = y + ~(mult’ 1) y> = y + ~( z + ~(mult’ 0) z>) y> = y + ~( z + ~( 0>) z>) y> = Note the alpha renaming of y,z,x y + ~( z + (fn x => 0) z>) y> Using the bracket escape cancel law y + (fn z => z + (fn x => 0) z) y> Why is it then, that when I type mult’ 2 into MetaML I get the following? -| mult' 2; val it = a %+ a %+ 0)> : int>

6 6Cse583 Winterl 2002 Safe beta at work! -| feature 1; Safe-beta is off. val it = false : bool -| mult' 2 ; val it = a %+ ((fn b => b %+ ((fn c => 0)) b)) a)> : int> With safe-beta turned off we get the expected answer. Note how much uglier the expected answer is What property makes safe-beta applicable?

7 7Cse583 Winterl 2002 member Have: member : 'a list -> 'a -> bool Want: member' : 'a list -> -> fun member [] x = false | member (y::ys) x = if x=y then true else member ys x; fun member' [] x = | member' (y::ys) x = <if ~x = y then true else ~(member' ys x)>;

8 8Cse583 Winterl 2002 Example: member -| member' [2,3] ; val it = <if 9 %= %y then true else if 9 %= %y then true else false> : Why do we have %y in the result? How can we fix this aesthetic problem? (hint use: lift) Can we make the last nested if if 9 %= %y then true else false Be replaced by just (9 %= %y) (hint make a special case for lists of length 1)

9 9Cse583 Winterl 2002 member’ again fun member' [] x = | member' [y] x = | member' (y::ys) x = <if ~x = ~(lift y) then true else ~(member' ys x)>; -| member' [2,3] ; val it = :

10 10Cse583 Winterl 2002 dotprod Have: dotprod : int -> int list -> int list -> int Want: dotprod’ : int -> int list -> int> fun dotprod 0 xs ys = 0 | dotprod n (x::xs) (y::ys) = (x * y) + (dotprod (n-1) xs ys) The function nth will come in handy fun nth 0 (x::xs) = x | nth n (x::xs) = nth (n-1) xs;

11 11Cse583 Winterl 2002 Dotprod (continued) fun help 0 next xs = 0> | help n next (x::xs) = ( ~(lift x) * (nth ~(lift next) ys) ) + ~(help (n-1) (next+1) xs) ys>; fun dotprod' n xs = help n 0 xs; Example: -| dotprod' 3 [0,1,2]; val it = (0 %* %nth 0 a) %+ (1 %* %nth 1 a) %+ (2 %* %nth 2 a) %+ 0)> : int>

12 12Cse583 Winterl 2002 Can we do without nth ? We can fun dotprod' 0 xs = 0> | dotprod' n (x::xs) = ( ~(lift x) * y ) + ~(dotprod' (n-1) xs) ys>; But -| dotprod' 3 [0,1,2]; val it = 0 %* b %+ ((fn (d::c) => 1 %* d %+ ((fn (f::e) => 2 %* f %+ 0)) c)) a)> : int> Safe-Beta doesn’t apply!

13 13Cse583 Winterl 2002 Applying optimizations -| dotprod' 3 [0,1,2]; val it = (0 %* %nth 0 a) %+ (1 %* %nth 1 a) %+ (2 %* %nth 2 a) %+ 0)> Rules 1*x = x 0*x = 0 x+0 = x 0 %+ (%nth 1 a) %+ (2 %* %nth 2 a)>

14 14Cse583 Winterl 2002 Strategy The rules 1*x = x 0*x = 0 Can be applied by writing staged versions of multiply ( * ) mult : int -> -> The Rule x+0 = x Can be applied by writing a special case for lists of length 1. (We’ve seen this one before)

15 15Cse583 Winterl 2002 Code fun mult 0 x = | mult 1 x = x | mult y x = ; fun help 0 next xs = 0> | help 1 next [x] = ~(mult x )> | help n next (x::xs) = ~(mult x ) + ~(help (n-1) (next+1) xs) ys>; fun dotprod' n xs = help n 0 xs;

16 16Cse583 Winterl 2002 Example -| dotprod' 3 [0,1,2]; val it = 0 %+ (%nth 1 a) %+ 2 %* (%nth 2 a)> : int> How do we get rid of the last (0 + …) A analysis version of addition ( + ) add : -> -> fun add x = x | add n x = ;

17 17Cse583 Winterl 2002 Final version of dotprod’ fun help 0 next xs = 0> | help 1 next [x] = ~(mult x )> | help n next (x::xs) = ~(add (mult x ) )>; fun dotprod' n xs = help n 0 xs; -| dotprod' 3 [0,1,2]; val it = (%nth 1 a) %+ (2 %* %nth 2 a)> : int>

18 18Cse583 Winterl 2002 3 stage dot-prod fun nth (x::xs) 1 = x | nth (x::xs) n = nth xs (n-1); (* iprod : int -> Vector -> Vector -> int *) fun iprod n v w = if n '>' 0 then ((nth v n) * (nth w n)) + (iprod (n-1) v w) else 0; (* iprod2 : int -> int>> *) fun iprod2 n = ~(~(if n '>' 0 then << (~(lift (nth v n)) * (nth w n)) + (~(~(iprod2 (n-1)) v) w) >> else >)) >>;

19 19Cse583 Winterl 2002 Results of staging -| val f1 = iprod2 3; (* Results from stage 1 *) val f1 = ~(%lift (%nth a %n)) %* (%nth b %n) %+ 0)>)> : int>> -| val f2 = (run f1) [1,0,4]; (* Results stage 2 *) val f2 = (4 %* (%nth a %n)) %+ (0 %* (%nth a %n)) %+ (1 %* (%nth a %n)) %+ 0)> : int>

20 20Cse583 Winterl 2002 Generator vs Transformer The function iprod2 is written in generator form iprod2 : int -> int>> fun iprod2 n = ~(~(if n '>' 0 then << (~(lift (nth v n)) * (nth w n)) + (~(~(iprod2 (n-1)) v) w) >> else >)) >>; Also possible to write as Transformer p3 : int -> -> > -> > fun p3 n v w = if n '>' 0 then << (~(lift (nth ~v n)) * (nth ~ ~w n)) + ~ ~(p3 (n-1) v w) >> else >;

21 21Cse583 Winterl 2002 From 3-level transformer to generator fun back2 f = ~ ~(f >)>>; fun iprod3 n = back2 (p3 n); val f3 = iprod3 3; (* Results from stage 1 *) ~(%lift (%nth a %n)) %* %nth b %n %+ ~(%lift (%nth a %n)) %* %nth b %n %+ 0)>)> val f4 = (run f3) [1,0,4]; (* Results stage 2 *) (4 %* %nth a %n) %+ (0 %* %nth a %n) %+ (1 %* %nth a %n) %+ 0>

22 22Cse583 Winterl 2002 Optimizing 3-stages First write staged arithmetic functions fun add' y = y | add' x = x | add' x y = ; fun mult' 0 y = | mult' 1 y = y | mult' x y = ;

23 23Cse583 Winterl 2002 Next use them in 3 level code fun p4 n v w = if n '>' 0 then <add' < ~(mult' (nth ~v n) ) > ~(p4 (n-1) v w) > else >; fun iprod4 n = back2 (p4 n);

24 24Cse583 Winterl 2002 Results val f1 = iprod4 3; (* Results from stage 1 *) -| val f1 = ~(%add' ( )>) (%add' ( )>) ( )))))>)> val f2 = (run f1) [1,0,4]; (* Results stage 2 *) -| val f2 = 4 %* (%nth a %n) %+ (%nth a %n)> Note the calls to add’ and mult’ are escaped to run when the first generator generates the second generator.

25 25Cse583 Winterl 2002 3 stage dot-prod fun nth (x::xs) 1 = x | nth (x::xs) n = nth xs (n-1); (* iprod : int -> Vector -> Vector -> int *) fun iprod n v w = if n '>' 0 then ((nth v n) * (nth w n)) + (iprod (n-1) v w) else 0; (* iprod2 : int -> int>> *) fun iprod2 n = ~(~(if n '>' 0 then << (~(lift (nth v n)) * (nth w n)) + (~(~(iprod2 (n-1)) v) w) >> else >)) >>;

26 26Cse583 Winterl 2002 Results of staging -| val f1 = iprod2 3; (* Results from stage 1 *) val f1 = ~(%lift (%nth a %n)) %* (%nth b %n) %+ 0)>)> : int>> -| val f2 = (run f1) [1,0,4]; (* Results stage 2 *) val f2 = (4 %* (%nth a %n)) %+ (0 %* (%nth a %n)) %+ (1 %* (%nth a %n)) %+ 0)> : int>

27 27Cse583 Winterl 2002 Generator vs Transformer The function iprod2 is written in generator form iprod2 : int -> int>> fun iprod2 n = ~(~(if n '>' 0 then << (~(lift (nth v n)) * (nth w n)) + (~(~(iprod2 (n-1)) v) w) >> else >)) >>; Also possible to write as Transformer p3 : int -> -> > -> > fun p3 n v w = if n '>' 0 then << (~(lift (nth ~v n)) * (nth ~ ~w n)) + ~ ~(p3 (n-1) v w) >> else >;

28 28Cse583 Winterl 2002 From 3-level transformer to generator fun back2 f = ~ ~(f >)>>; fun iprod3 n = back2 (p3 n); val f3 = iprod3 3; (* Results from stage 1 *) ~(%lift (%nth a %n)) %* %nth b %n %+ ~(%lift (%nth a %n)) %* %nth b %n %+ 0)>)> val f4 = (run f3) [1,0,4]; (* Results stage 2 *) (4 %* %nth a %n) %+ (0 %* %nth a %n) %+ (1 %* %nth a %n) %+ 0>

29 29Cse583 Winterl 2002 Optimizing 3-stages (* add : int -> int -> -> *) fun add i x y e = if x=0 then e else if x=1 then else ; (* p3 : int -> -> > -> > *) fun p3 n v w = if n = 1 then ) >> else << ~(add n (nth ~v n) ~w ) >>;

30 30Cse583 Winterl 2002 Results fun iprod3 n = back2 (p3 n); val f3 = iprod3 3; val f4 = (run f3) [1,0,4]; val f4 = 4 %* %nth a 3 %+ %nth a 1 %+ 0)> : int>

31 31Cse583 Winterl 2002 Review Using lift where appropriate The use of helper functions Transformers vs generators Special case for static lists of length 1 Staged versions of binary operators where one of the arguments is static, often allow special “rules” to be applied When will the “safe” object level equations like safe-beta, safe-eta, and let- lifting apply?


Download ppt "Tim Sheard Oregon Graduate Institute Lecture 5: Review of homework 2 CS510 Sect FSC Winter 2004 Winter 2004."

Similar presentations


Ads by Google