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

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Type Variables in ML Until we know the type of a value (perhaps never!), we use a variable for its type Book uses, e.g., t1, tx, tf PL literature uses.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Tim Sheard Oregon Graduate Institute Lecture 8: Operational Semantics of MetaML CSE 510 Section FSC Winter 2005 Winter 2005.
Topic: EQUATIONS Simple Equations Fractional Equations.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
CSE341: Programming Languages Lecture 12 Equivalence Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Tim Sheard Portland State University Lecture 11: C-Mix and partial evaluation CS510 Section FSC Winter 2005 Winter 2005.
Tim Sheard Oregon Graduate Institute Lecture 6: Monads and Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Generating Efficient code Lecture 9. Assignment Homework #6 is now available on the class web-site. –Due next Tuesday, Feb 15, 2005 Read the paper –ML.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Tim Sheard Oregon Graduate Institute Lecture 2: More on MetaML CS510 Sect FSC Winter 2004 Winter 2004.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Cse321, Programming Languages and Compilers 1 6/26/2015 Lecture #4, Jan. 24, 2007 Homework 3 Representing sets as lists the cross product of two sets epsilon.
Tim Sheard Oregon Graduate Institute CS510 Sect FSC Winter 2004 Winter 2004 Lecture 1: Introduction.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Tim Sheard Oregon Graduate Institute Lecture 6: Monadic Staging of the RE language CSE 510 Section FSC Winter 2004 Winter 2004.
Staging in Haskell What is Staging What does it Mean Using Template Haskell.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Advanced Functional Programming Tim Sheard 1 Lecture 18 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
Solve by Graphing Solve: 3x + 4y = - 4 x + 2y = 2
Chapter SevenModern Programming Languages1 A Second Look At ML.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Recursion Higher Order Functions CSCE 314 Spring 2016.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Advanced Functional Programming Tim Sheard 1 Lecture 16 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
The student will be able to:
Solve by Graphing Solve: 3x + 4y = - 4 x + 2y = 2
CSE 341 Lecture 5 efficiency issues; tail recursion; print
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Tim Sheard Oregon Graduate Institute
PROGRAMMING IN HASKELL
12 Systems of Linear Equations and Inequalities.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
Nicholas Shahan Spring 2016
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 12 Equivalence
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Advanced Functional Programming
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
ALGEBRA what you need to know..
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 12 Equivalence
Presentation transcript:

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

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>

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 = :

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>

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>

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?

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)>;

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)

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 = :

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;

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>

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!

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)>

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)

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;

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 = ;

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>

18Cse583 Winterl 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 >)) >>;

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>

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 >;

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>

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 = ;

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);

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.

25Cse583 Winterl 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 >)) >>;

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>

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 >;

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>

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 ) >>;

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>

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?