1 / 83

Programming Language Concepts, COSC-3308-01 Lecture 6  

Programming Language Concepts, COSC-3308-01 Lecture 6  . Higher-Order Programming and Abstract Data Types. Reminder of the Last Lecture. Last call optimization Recursion versus Iteration Tupled recursion Exceptions Type notation Design methodology. Overview. Higher-order programming

nam
Download Presentation

Programming Language Concepts, COSC-3308-01 Lecture 6  

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language Concepts, COSC-3308-01Lecture 6   Higher-Order Programming and Abstract Data Types COSC-3308-01, Lecture 6

  2. Reminder of the Last Lecture • Last call optimization • Recursion versus Iteration • Tupled recursion • Exceptions • Type notation • Design methodology COSC-3308-01, Lecture 6

  3. Overview • Higher-order programming • Abstract data types • October 22, Friday – 9:00am-10:00am • Mid-term exam (20% of the final grade) • Lectures 1-5, Tutorials 1-4 COSC-3308-01, Lecture 6

  4. Higher-Order Programming COSC-3308-01, Lecture 6

  5. Higher-Order Programming • Higher-order programming = the set of programming techniques that are possible with procedure values (lexically-scoped closures). • Higher-order programming is the foundation of secure data abstraction component-based programming and object-oriented programming. COSC-3308-01, Lecture 6

  6. Higher-Order Programming • A procedure which has no procedures as arguments is called first-order. • A procedure which has at least one procedure as argument is called second-order … • A procedure is of order n+1 if it has at least one argument of order n and none of higher order. • Higher-order programming procedures can be of any order. COSC-3308-01, Lecture 6

  7. Remember (I) • Functions are procedures • Special syntax, nested syntax, expression syntax • They are procedures with one argument as the result arguments • Example: fun {F X} fun {$ Y} X+Y end end • A function that returns a function that is specialized on X COSC-3308-01, Lecture 6

  8. Remember (II) • Successive transformations to procedures • fun {F X}fun {$ Y} X+Y endend • proc {F X ?R} R = fun {$ Y} X+Y endend • proc {F X ?R} R = proc {$ Y ?R1} R1=X+Y endend COSC-3308-01, Lecture 6

  9. Remember (III) • proc {F X ?R} R = proc {$ Y ?R1} R1=X+Y endend • Fis a procedure value • When F is called, its 2nd argument returns a procedure value • ‘?’ is comment indicating output argument COSC-3308-01, Lecture 6

  10. Remember (IV) declare fun {F X} fun {$ Y} X+Y end end {Browse F} G={F 1} {Browse G} {Browse {G 2}} • Fis a function of one argument, which corresponds to a procedure having two arguments  <P/2 F> • G is an unnamed function  <P/2> • {G Y} returns 1+Y  3 COSC-3308-01, Lecture 6

  11. Remember (V) • fun {F X}fun {$ Y} X+Y endend • The type ofF • fun {F Num}: fun {$ Num}: Num  COSC-3308-01, Lecture 6

  12. Higher-Order Programming • Basic operations: • Procedural abstraction: the ability to convert any statement into a procedure value • Genericity: the ability to pass procedure values as arguments to a procedure call • Instantiation: the ability to return procedure values as results from a procedure call • Embedding: the ability to put procedure values in data structures COSC-3308-01, Lecture 6

  13. Higher-Order Programming • Control abstractions • The ability to define control constructs • Integer and list loops, accumulator loops, folding a list (left and right) COSC-3308-01, Lecture 6

  14. Procedural Abstraction • Procedural abstraction is the ability to convert any statement into a procedure value. Statement P = proc{$} Statement end {P} time time Normal Execution Delayed Execution COSC-3308-01, Lecture 6

  15. Procedural Abstraction • A procedure value is usually called a closure, or more precisely, a lexically-scoped closure • A procedure value is a pair: it combines the procedure code with the contextual environment • Basic scheme: • Consider any statement <s> • Convert it into a procedure value: P = proc {$} <s> end • Executing {P} has exactly the same effect as executing <s> COSC-3308-01, Lecture 6

  16. The Same Holds for Expressions • A procedure value is usually called a closure, or more precisely, a lexically-scoped closure • A procedure value is a pair: it combines the procedure code with the contextual environment • Basic scheme: • Consider any expression <E>, embedded in an equality like X = E • Convert it into a function value: F = fun {$} <E> end • Executing X = {F} has exactly the same effect as executing X = E COSC-3308-01, Lecture 6

  17. The Arguments are Evaluated • Xis evaluated as3  3  2 declare Z=3 fun {F X} {Browse X} 2 end Y={F Z} {Browse Y} • Xis evaluated as function valuefun {$} Z end  <P/1>  3  2 declare Z=3 fun {F X} {Browse X} {Browse {X}} 2 end Y={F fun {$} Z end} {Browse Y} COSC-3308-01, Lecture 6

  18. Example • Suppose we want to define the operator andthen (&& in Java) as a function, namely <expression1> andthen <expression2> is false if <expression1> is false, avoiding the evaluation of <expression2> (Exercise 2.8.6, page 109). • Attempt: fun {AndThen B1 B2} if B1 then B2 else false end end if {AndThen X>0 Y>0} then … else … COSC-3308-01, Lecture 6

  19. Example • Does not work as wished because both X>0 and Y>0 are evaluated. • So, even if X>0 is false, Y should be bound in order to evaluate the expression Y>0! fun {AndThen B1 B2} if B1 then B2 else false end end if {AndThen X>0 Y>0} then … else … COSC-3308-01, Lecture 6

  20. Example declare fun {AndThen B1 B2} if B1 then B2 else false end end X=~3 Y if {AndThen X>0 Y>0} then {Browse 1} else {Browse 2} end • Display nothing since Y is unbound! • When called, all function’s arguments are evaluated. COSC-3308-01, Lecture 6

  21. Solution: Use Procedural Abstractions fun {AndThen B1 B2} if {B1} then {B2} else false end end if {AndThen fun{$} X>0 endfun{$} Y>0 end} then … else … end COSC-3308-01, Lecture 6

  22. Example. Solution declare fun {AndThen BP1 BP2} if {BP1} then {BP2} else false end end X=~3 Y if {AndThen fun{$} X>0 end fun{$} Y>0 end} then {Browse 1} else {Browse 2} end • Display 2 (even if Y is unbound) COSC-3308-01, Lecture 6

  23. Genericity • To make a function generic is to let any specific entity (i.e., any operation or value) in the function body become an argument of the function. • The entity is abstracted out of the function body. COSC-3308-01, Lecture 6

  24. Genericity • Replace specific entities (zero 0 and addition +) by function arguments. fun {SumList Ls} case Ls of nil then0 [] X|Lr then X+{SumList Lr} end end COSC-3308-01, Lecture 6

  25. Genericity fun {SumList L} case L of nil then0 [] X|L2 then X+{SumList L2} end end fun {FoldR L FU} case L of nil thenU [] X|L2 then {F X {FoldR L2 F U}} end end COSC-3308-01, Lecture 6

  26. Genericity SumList fun{SumList Ls} {FoldR Lsfun{$ X Y} X+Y end 0 } end {Browse {SumList [1 2 3 4]}} COSC-3308-01, Lecture 6

  27. Genericity ProductList fun{ProductList Ls} {FoldR Lsfun{$ X Y} X*Y end 1 } end {Browse {ProductList [1 2 3 4]}} COSC-3308-01, Lecture 6

  28. Genericity Some fun{Some Ls} {FoldR Ls fun{$ X Y} X orelse Y end false }end {Browse {Some [false true false]}} COSC-3308-01, Lecture 6

  29. List Mapping • Mapping • each element recursively • calling function for each element • Snoctruct list that takes output • Separate function calling by passing function as argument. COSC-3308-01, Lecture 6

  30. Other Generic Functions: Map fun {Map Xs F} case Xs of nil then nil [] X|Xr then {F X}|{Map Xr F} end end {Browse {Map [1 2 3] fun {$ X} X*X end}} COSC-3308-01, Lecture 6

  31. Other Generic Functions: Filter fun {Filter Xs P} case Xs of nil then nil [] X|Xr andthen {P X} then X|{Filter Xr P} [] X|Xr then {Filter Xr P} end end {Browse {Filter [1 2 3] IsOdd}} COSC-3308-01, Lecture 6

  32. Instantiation • Instantiation: the ability to return procedure values as results from a procedure call. • A factory of specialized functions. declare fun {Add X}fun {$ Y} X+Y end end Inc = {Add 1} {Browse {Inc 5}} % shows 6 COSC-3308-01, Lecture 6

  33. Explanations for Nesting Operator $ • The previous Oz code can be rewritten as: declare fun {Add X} Z = fun {$ Y} X+Y end Z end Inc = {Add 1} % Inc is bound to value of Z {Browse {Inc 5}} % shows 6 COSC-3308-01, Lecture 6

  34. Embedding • Embedding is when procedure values are put in data structures. • Embedding has many uses: • Modules: a module is a record that groups together a set of related operations (procedures). • Software components: a software component is a generic function that takes a set of modules as its arguments and returns a new module. It can be seen as specifying a module in terms of the modules it needs. COSC-3308-01, Lecture 6

  35. Embedding. Example declare Algebra local proc {Add X Y ?Z} Z=X+Y end proc {Mul X Y ?Z} Z=X*Y end in Algebra=op(add:Add mul:Mul) end A=2 B=3 {Browse {Algebra.add A B}} {Browse {Algebra.mul A B}} • Add and Mul are procedures embedded in a data structure. COSC-3308-01, Lecture 6

  36. Integer Loop Abstractions • Integer loop: repeats an operation with a sequence of integers: proc {For I J P} if I > J thenskip else {P I} {For I+1 J P} end end {For 1 10 Browse} • Linguistic abstraction for integer loops: for I in 1..10 do {Browse I} end COSC-3308-01, Lecture 6

  37. List Loop Abstractions • List loop: repeats an operation for all elements of a list: proc {ForAll Xs P} case Xs of nil thenskip [] X|Xr then {P X} {ForAll Xr P} end end {ForAll [a b c d] proc{$ I} {Browse I}end} COSC-3308-01, Lecture 6

  38. List Loop Abstractions proc {ForAll Xs P} case Xs of nil thenskip [] X|Xr then {P X} {ForAll Xr P} end end • Linguistic abstraction for list loops: for I in [a b c d] do {Browse I} end COSC-3308-01, Lecture 6

  39. Other Examples • {Filter Xs F} returns all elements of Xs for which F returns true • {Some Xs F} tests whether Xs has an element for which F returns true • {All Xs F} tests whether F returns true for all elements of Xs COSC-3308-01, Lecture 6

  40. Folding Lists • Consider computing the sum of list elements • …or the product • …or all elements appended to a list • …or the maximum • … • What do they have in common? • Example: SumList COSC-3308-01, Lecture 6

  41. SumList: Naive fun {SumList Xs} case Xs of nil then 0 [] X|Xr then {SumList Xr}+X end end • First step: make tail-recursive with accumulator. COSC-3308-01, Lecture 6

  42. SumList: Tail-Recursive fun {SumList Xs N} case Xs of nil then N [] X|Xr then {SumList Xr N+X} end end {SumList Xs 0} • Question: • what is about computing the sum? • what is generic? COSC-3308-01, Lecture 6

  43. SumList: Tail-Recursive fun {SumList Xs N} case Xs of nil thenN [] X|Xr then {SumList Xr N+X} end end {SumList Xs 0} • Question: • what is about computing the sum? • what is generic? COSC-3308-01, Lecture 6

  44. How Does SumList Compute? {SumList [2 5 7] 0} = {SumList [5 7] 0+2} = {SumList [7] (0+2)+5} = {SumList nil ((0+2)+5)+7} = … COSC-3308-01, Lecture 6

  45. SumList Slightly Rewritten… {SumList [2 5 7] 0} = {SumList [5 7] {F 0 2}} = {SumList [7] {F {F 0 2} 5}} = {SumList nil {F {F {F 0 2} 5} 7} = … where F is fun {F X Y} X+Y end COSC-3308-01, Lecture 6

  46. Left-Folding • Two values define “folding” • initial value 0 for SumList • binary function + for SumList • Left-folding {FoldL [x1 …xn] F S} {F … {F {F S x1} x2} … xn} or (…((S Fx1) Fx2) … Fxn) COSC-3308-01, Lecture 6

  47. Left-Folding • Two values define “folding” • initial value 0 for SumList • binary function + for SumList • Left-folding {FoldL [x1 … xn] F S} {F … {F {F S x1} x2} … xn} or (…((S Fx1) Fx2) … Fxn) left is here! COSC-3308-01, Lecture 6

  48. FoldL fun {FoldL Xs F S} case Xs of nil then S [] X|Xr then {FoldL Xr F {F S X}} end end COSC-3308-01, Lecture 6

  49. SumList with FoldL local fun {Plus X Y} X+Y end in fun {SumList Xs} {FoldL Xs Plus 0} end end COSC-3308-01, Lecture 6

  50. Properties of FoldL • Tail recursive • First element of list is folded first… • what does that mean? COSC-3308-01, Lecture 6

More Related