870 likes | 1.02k Views
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
E N D
Programming Language Concepts, COSC-3308-01Lecture 6 Higher-Order Programming and Abstract Data Types COSC-3308-01, Lecture 6
Reminder of the Last Lecture • Last call optimization • Recursion versus Iteration • Tupled recursion • Exceptions • Type notation • Design methodology COSC-3308-01, Lecture 6
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
Higher-Order Programming COSC-3308-01, Lecture 6
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
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
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
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
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
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
Remember (V) • fun {F X}fun {$ Y} X+Y endend • The type ofF • fun {F Num}: fun {$ Num}: Num COSC-3308-01, Lecture 6
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Properties of FoldL • Tail recursive • First element of list is folded first… • what does that mean? COSC-3308-01, Lecture 6