Lazy evaluation. Lazy evaluation (3) Lazy evaluation (2) Lazy execution. Example. Declarative Concurrency. Lazy Execution (VRH 4.

Size: px
Start display at page:

Download "Lazy evaluation. Lazy evaluation (3) Lazy evaluation (2) Lazy execution. Example. Declarative Concurrency. Lazy Execution (VRH 4."

Transcription

1 Declarative Concurrency Lazy Execution (VRH 4.5) Carlos Varela RPI Lazy evaluation The deault unctions in Oz are evaluated eagerly (as soon as they are called) Another way is lazy evaluation where a computation is done only when the result is needed Adapted with permission rom: Sei Haridi KTH Peter Van Roy UCL November 30, 2006 Calculates the ininite list: declare un lazy {Ints N N {Ints N+ C. Varela; Adapted rom S. Haridi and P. Van Roy C. Varela; Adapted rom S. Haridi and P. Van Roy 2 Lazy evaluation (2) Lazy evaluation (3) Write a unction that computes as many rows o Pascal s triangle as needed We do not know how many beorehand A unction is lazy i it is evaluated only when its result is needed The unction PascalList is evaluated when needed un lazy {PascalList Row Row {PascalList {AddList Row {ShitRight Row Lazy evaluation will avoid redoing work i you decide irst you need the 0 th row and later the th row The unction continues where it let o declare L = {PascalList [] {Browse L {Browse L. {Browse L.2. L<Future> [] [ ] C. Varela; Adapted rom S. Haridi and P. Van Roy 3 C. Varela; Adapted rom S. Haridi and P. Van Roy 4 Lazy execution Without lazyness, the execution order o each thread ollows textual order, i.e., when a statement comes as the irst in a sequence it will execute, whether or not its results are needed later This execution scheme is called eager execution, or supply-driven execution Another execution order is that a statement is executed only i its results are needed somewhere in the program This scheme is called lazy evaluation, or demand-driven evaluation (some languages use lazy evaluation by deault, e.g., Haskell) Example B = {F X C = {F2 Y D = {F3 Z A = B+C Assume F, F2 and F3 are lazy unctions B = {F X and C = {F2 Y are executed only i and when their results are needed in A = B+C D = {F3 Z is not executed since it is not needed C. Varela; Adapted rom S. Haridi and P. Van Roy 5 C. Varela; Adapted rom S. Haridi and P. Van Roy 6

2 Example Example II In lazy execution, an operation susps until its result are needed The susped operation is triggered when another operation needs the value or its arguments In general multiple susped operations could start concurrently B = {F X C = {F2 Y Demand A = B+C In data-driven execution, an operation susps until the values o its arguments results are available In general the susped computation could start concurrently B = {F X C = {F2 Y Data driven A = B+C C. Varela; Adapted rom S. Haridi and P. Van Roy 7 C. Varela; Adapted rom S. Haridi and P. Van Roy 8 Using Lazy Streams How does it work? un {Sum Xs A Limit i Limit>0 then case Xs o X Xr then {Sum Xr A+X Limit- else A local Xs S in Xs={Ints 0 S={Sum Xs {Browse S un {Sum Xs A Limit i Limit>0 then case Xs o X Xr then {Sum Xr A+X Limit- else A un lazy {Ints N N {Ints N+ local Xs S in Xs = {Ints 0 S={Sum Xs {Browse S C. Varela; Adapted rom S. Haridi and P. Van Roy 9 C. Varela; Adapted rom S. Haridi and P. Van Roy 0 Improving throughput Use a lazy buer It takes a lazy input stream In and an integer N, and returns a lazy output stream Out When it is irst called, it irst ills itsel with N elements by asking the producer The buer now has N elements illed Whenever the consumer asks or an element, the buer in turn asks the producer or another element The buer example N producer buer consumer N producer buer consumer C. Varela; Adapted rom S. Haridi and P. Van Roy C. Varela; Adapted rom S. Haridi and P. Van Roy 2 2

3 The buer The buer II un {Buer In N End={List.drop In N un lazy {Loop In End In. {Loop In.2 End.2 in {Loop In End Traversing the In stream, orces the producer to emit N elements un {Buer2 In N End = thread {List.drop In N un lazy {Loop In End In. {Loop In.2 End.2 in {Loop In End Traversing the In stream, orces the producer to emit N elements and at the same time serves the consumer C. Varela; Adapted rom S. Haridi and P. Van Roy 3 C. Varela; Adapted rom S. Haridi and P. Van Roy 4 un {Buer3 In N End = thread {List.drop In N un lazy {Loop In End E2 = thread End.2 In. {Loop In.2 E2 in {Loop In End The buer III Traverse the In stream, orces the producer to emit N elements and at the same time serves the consumer, and requests the next element ahead Xs Larger Example: The Sieve o Eratosthenes Produces prime numbers It takes a stream 2...N, peals o 2 rom the rest o the stream Delivers the rest to the next sieve Sieve X Xr Filter Ys Sieve Zs X Zs C. Varela; Adapted rom S. Haridi and P. Van Roy 5 C. Varela; Adapted rom S. Haridi and P. Van Roy 6 Lazy Sieve un lazy {Sieve Xs X Xr = Xs in X {Sieve {LFilter Xr un {$ Y Y mod X \= 0 un {Primes {Sieve {Ints 2 Lazy Filter For the Sieve program we need a lazy ilter un lazy {LFilter Xs F case Xs o nil then nil [] X Xr then i {F X then X {LFilter Xr F else {LFilter Xr F C. Varela; Adapted rom S. Haridi and P. Van Roy 7 C. Varela; Adapted rom S. Haridi and P. Van Roy 8 3

4 Deine streams implicitly Deine streams implicitly Ones = Ones Ininite stream o ones cons Ones Xs = {LMap Xs un {$ X X+ What is Xs? cons Xs? + C. Varela; Adapted rom S. Haridi and P. Van Roy 9 C. Varela; Adapted rom S. Haridi and P. Van Roy 20 The Hamming problem Generate the irst N elements o stream o integers o the orm: 2 a 3 b 5 c with a,b,c 0 (in ascing order) The Hamming problem Generate the irst N elements o stream o integers o the orm: 2 a 3 b 5 c with a,b,c 0 (in ascing order) *2 *2 *3 *5 Merge *3 *5 C. Varela; Adapted rom S. Haridi and P. Van Roy 2 C. Varela; Adapted rom S. Haridi and P. Van Roy 22 The Hamming problem Generate the irst N elements o stream o integers o the orm: 2 a 3 b 5 c with a,b,c 0 (in ascing order) H cons Merge *2 *3 *5 Lazy File Reading un {ToList FO un lazy {LRead L T in i {File.readBlock FO L T then T = {LRead else T = nil {File.close FO L {LRead This avoids reading the whole ile in memory C. Varela; Adapted rom S. Haridi and P. Van Roy 23 C. Varela; Adapted rom S. Haridi and P. Van Roy 24 4

5 List Comprehensions Abstraction provided in lazy unctional languages that allows writing higher level set-like expressions In our context we produce lazy lists instead o sets The mathematical set expression {x*y x 0, y x Equivalent List comprehension expression is [X*Y X =..0 ; Y =..X] Example: [* 2* 2*2 3* 3*2 3*3... 0*0] List Comprehensions The general orm is [ (x,y,...,z) x gen(a,...,an) ; guard(x,...) y gen(x, a,...,an) ; guard(y,x,...)... ] No linguistic support in Mozart/Oz, but can be easily expressed C. Varela; Adapted rom S. Haridi and P. Van Roy 25 C. Varela; Adapted rom S. Haridi and P. Van Roy 26 Example z = [x#x x rom(,0)] Z = {LMap {LFrom 0 un{$ X X#X z = [x#y x rom(,0), y rom(,x)] Z = {LFlatten {LMap {LFrom 0 un{$ X {LMap {LFrom X un {$ Y X#Y C. Varela; Adapted rom S. Haridi and P. Van Roy 27 Example 2 z = [x#y x rom(,0), y rom(,x), x+y 0] Z ={LFilter {LFlatten {LMap {LFrom 0 un{$ X {LMap {LFrom X un {$ Y X#Y un {$ X#Y X+Y=<0 C. Varela; Adapted rom S. Haridi and P. Van Roy 28 Implementation o lazy execution Implementation The ollowing deines the syntax o a statement, s denotes a statement s ::= skip empty statement... thread s thread creation {ByNeed un{$ e x by need statement zero arity unction variable some statement {ByNeed un{$ e X,E store x stack A unction value is created in the store (say ) the unction is associated with the variable x execution proceeds immediately to next statement C. Varela; Adapted rom S. Haridi and P. Van Roy 29 C. Varela; Adapted rom S. Haridi and P. Van Roy 30 5

6 Implementation Accessing the ByNeed variable some statement {ByNeed un{$ e X,E store x : (un{$ e X,E) stack A unction value is created in the store (say ) the unction is associated with the variable x execution proceeds immediately to next statement X = {ByNeed un{$ * (by thread T0) Access by some thread T i X > 000 then {Browse hello#x or {Wait X Causes X to be bound to 232 (i.e. *) C. Varela; Adapted rom S. Haridi and P. Van Roy 3 C. Varela; Adapted rom S. Haridi and P. Van Roy 32 Thread T Implementation. X is needed 2. start a thread T2 to execute F (the unction) 3. only T2 is allowed to bind X Thread T2 4. Allow access on X. Evaluate Y = {F 2. Bind X the value Y 3. Terminate T2 un lazy {Ints N N {Ints N+ Lazy unctions un {Ints N un {F N {Ints N+ in {ByNeed F C. Varela; Adapted rom S. Haridi and P. Van Roy 33 C. Varela; Adapted rom S. Haridi and P. Van Roy 34 Exercises 90. Write a lazy app list operation LazyApp. Can you also write LazyFoldL? Why or why not? 9. Exercise VRH 4..0 (pg 34) 92. *Exercise VRH 4..3 (pg 342) 93. *Exercise VRH 4..7 (pg 342) C. Varela; Adapted rom S. Haridi and P. Van Roy 35 6

Declarative Concurrency (CTM 4)

Declarative Concurrency (CTM 4) Declarative Concurrency (CTM 4) Carlos Varela Rensselaer Polytechnic Institute Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL November 21, 2014 C. Varela; Adapted with permission from

More information

Declarative Concurrency (CTM 4)

Declarative Concurrency (CTM 4) Declarative Concurrency (CTM 4) Carlos Varela Rensselaer Polytechnic Institute Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL October 30, 2015 C. Varela; Adapted with permission from S.

More information

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3)

On Academic Dishonesty. Declarative Computation Model. Single assignment store. Single assignment store (2) Single assignment store (3) Declarative Computation Model Single assignment store (VRH 2.2) Kernel language syntax (VRH 2.3) Carlos Varela RPI October 6, 2009 Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL On Academic

More information

Declarative Computation Model

Declarative Computation Model Declarative Computation Model Kernel language semantics revisited (VRH 2.4.5) From kernel to practical language (VRH 2.6) Exceptions (VRH 2.7) Carlos Varela RPI March 19, 2012 Adapted with permission from:

More information

Programming Language Concepts, cs2104 Lecture 09 ( )

Programming Language Concepts, cs2104 Lecture 09 ( ) Programming Language Concepts, cs2104 Lecture 09 (2003-10-10) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-10-03 S. Haridi, CS2104, L09 (slides: C. Schulte, S. Haridi) 1

More information

Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary

Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary Programming Languages (CSCI 4430/6430) Part 1: Functional Programming: Summary Carlos Varela Rennselaer Polytechnic Institute September 29, 2017 C. Varela 1 Other programming languages Imperative Algol

More information

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL Higher-Order Programming: Closures, procedural abstraction, genericity, instantiation, embedding. Control abstractions: iterate, map, reduce, fold, filter (CTM Sections 1.9, 3.6, 4.7) Carlos Varela RPI

More information

Declarative Programming Techniques

Declarative Programming Techniques Declarative Programming Techniques Declarativeness, iterative computation (CTM 3.1-3.2) Higher-order programming (CTM 3.6) Abstract data types (CTM 3.7) Carlos Varela Rensselaer Polytechnic Institute April

More information

Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces

Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Logic Programming (CTM 12.3-12.5) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute December 4, 2015 C. Varela 1 Generate and Test Example We can

More information

State, Object-Oriented Programming Explicit State, Polymorphism (CTM ) Objects, Classes, and Inheritance (CTM )

State, Object-Oriented Programming Explicit State, Polymorphism (CTM ) Objects, Classes, and Inheritance (CTM ) State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6.1-6.4.4) Objects, Classes, and Inheritance (CTM 7.1-7.2) Carlos Varela Rensselaer Polytechnic Institute October 27, 2017 Adapted with

More information

Shell CSCE 314 TAMU. Haskell Functions

Shell CSCE 314 TAMU. Haskell Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can

More information

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}

More information

Sometimes it s good to be lazy

Sometimes it s good to be lazy Chapter 8 Sometimes it s good to be lazy Typically, languages such as OCaml (SML, Lisp or Scheme) evaluate expressions by a call-by-value discipline. All variables in OCaml are bound by value, which means

More information

Exam for 2G1512 DatalogiII, Apr 12th

Exam for 2G1512 DatalogiII, Apr 12th Exam for 2G1512 DatalogiII, Apr 12th 2002 09.00 14.00 Dept. of Microelectronics and Information Technology April 11, 2002 Allowed materials You are allowed to bring the course book and all handouts to

More information

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Carlos Varela Rensselaer Polytechnic Institute September 23,

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

More information

Programming Language Concepts, cs2104 Lecture 04 ( )

Programming Language Concepts, cs2104 Lecture 04 ( ) Programming Language Concepts, cs2104 Lecture 04 (2003-08-29) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-09-05 S. Haridi, CS2104, L04 (slides: C. Schulte, S. Haridi) 1

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

More information

proc {Produce State Out} local State2 Out2 in State2 = State + 1 Out = State Out2 {Produce State2 Out2}

proc {Produce State Out} local State2 Out2 in State2 = State + 1 Out = State Out2 {Produce State2 Out2} Laziness and Declarative Concurrency Raphael Collet Universite Catholique de Louvain, B-1348 Louvain-la-Neuve, Belgium raph@info.ucl.ac.be May 7, 2004 Abstract Concurrency and distribution in a programming

More information

Shell CSCE 314 TAMU. Functions continued

Shell CSCE 314 TAMU. Functions continued 1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in

More information

Declarative concurrency. March 3, 2014

Declarative concurrency. March 3, 2014 March 3, 2014 (DP) what is declarativeness lists, trees iterative comutation recursive computation (DC) DP and DC in Haskell and other languages 2 / 32 Some quotes What is declarativeness? ness is important

More information

Lecture 7: General Computation Models

Lecture 7: General Computation Models Lecture 7: General Computation Models Declarative Programming Techniques (Part 2) November 15, 2017 Difference lists Difference list: pair L1#L2 of two lists, such that L2 is a suffix of the first list:

More information

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011

Lecture 5: Declarative Programming. The Declarative Kernel Language Machine. September 12th, 2011 Lecture 5: Declarative Programming. The Declarative Kernel Language Machine September 12th, 2011 1 Lecture Outline Declarative Programming contd Dataflow Variables contd Expressions and Statements Functions

More information

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists CITS3211 FUNCTIONAL PROGRAMMING 7. Lazy evaluation and infinite lists Summary: This lecture introduces lazy evaluation and infinite lists in functional languages. cs123 notes: Lecture 19 R.L. While, 1997

More information

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d)

Connecting Definition and Use? Tiger Semantic Analysis. Symbol Tables. Symbol Tables (cont d) Tiger source program Tiger Semantic Analysis lexical analyzer report all lexical errors token get next token parser construct variable deinitions to their uses report all syntactic errors absyn checks

More information

Lazy Evaluation of Function Arguments. Lazy Evaluation of Function Arguments. Evaluation with Lazy Arguments. Evaluation with Lazy Arguments

Lazy Evaluation of Function Arguments. Lazy Evaluation of Function Arguments. Evaluation with Lazy Arguments. Evaluation with Lazy Arguments Lazy Evaluation o Function Arguments Lazy Evaluation o Function Arguments let = proc()0 in ( +(1,+(2,+(3,+(4,+(5,6)))))) The computed 21 is never used. What i we were lazy about computing unction arguments

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]

More information

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program

More information

ITERATORS AND STREAMS 9

ITERATORS AND STREAMS 9 ITERATORS AND STREAMS 9 COMPUTER SCIENCE 61A November 12, 2015 1 Iterators An iterator is an object that tracks the position in a sequence of values. It can return an element at a time, and it is only

More information

Concepts, Techniques, and Models of Computer Programming

Concepts, Techniques, and Models of Computer Programming Concepts, Techniques, and Models of Computer Programming Dec. 9, 2004 Peter Van Roy Université catholique de Louvain Louvain-la-Neuve, Belgium Seif Haridi Kungliga Tekniska Högskolan Kista, Sweden Invited

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

An introduction introduction to functional functional programming programming using usin Haskell

An introduction introduction to functional functional programming programming using usin Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program

More information

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Haskell 98 in short! CPSC 449 Principles of Programming Languages Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has

More information

CSc 372 Comparative Programming Languages

CSc 372 Comparative Programming Languages CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

More information

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False

More information

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona 1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists

More information

CS 340 Spring 2019 Midterm Exam

CS 340 Spring 2019 Midterm Exam CS 340 Spring 2019 Midterm Exam Instructions: This exam is closed-book, closed-notes. Electronic devices of any kind are not permitted. Write your final answers, tidily, in the boxes provided. Scratch

More information

Topic 8: Lazy Evaluation

Topic 8: Lazy Evaluation Topic 8: Lazy Evaluation 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 17.1, 17.2, 17.4, 17.8, 17.23, 17.25, 17.28, 17.29 Readings: Chapter

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Lecture 2: List algorithms using recursion and list comprehensions

Lecture 2: List algorithms using recursion and list comprehensions Lecture 2: List algorithms using recursion and list comprehensions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 12, 2017 Expressions, patterns

More information

Overview. Distributed Computing with Oz/Mozart (VRH 11) Mozart research at a glance. Basic principles. Language design.

Overview. Distributed Computing with Oz/Mozart (VRH 11) Mozart research at a glance. Basic principles. Language design. Distributed Computing with Oz/Mozart (VRH 11) Carlos Varela RPI March 15, 2007 Adapted with permission from: Peter Van Roy UCL Overview Designing a platform for robust distributed programming requires

More information

Mozart System Limitations for Version (draft)

Mozart System Limitations for Version (draft) Mozart System Limitations for Version 1.3.0 (draft) Peter Van Roy and Seif Haridi April 28, 2004 In der Beschränkung zeigt sich erst der Meister. Within limitations the Master is first revealed. Johann

More information

Programming Language Concepts, CS2104 Lecture 7

Programming Language Concepts, CS2104 Lecture 7 Reminder of Last Lecture Programming Language Concepts, CS2104 Lecture 7 Tupled Recursion Exceptions Types, ADT, Haskell, Components 5th Oct 2007 CS2104, Lecture 7 1 Overview 5th Oct 2007 CS2104, Lecture

More information

General Overview of Mozart/Oz

General Overview of Mozart/Oz General Overview of Mozart/Oz Peter Van Roy pvr@info.ucl.ac.be 2004 P. Van Roy, MOZ 2004 General Overview 1 At a Glance Oz language Dataflow concurrent, compositional, state-aware, object-oriented language

More information

02157 Functional Programming Tagged values and Higher-order list functions

02157 Functional Programming Tagged values and Higher-order list functions Tagged values and Higher-order list functions nsen 1 DTU Informatics, Technical University of Denmark Tagged values and Higher-order list functions MRH 27/09/2012 Part I: Disjoint Sets An Example A shape

More information

Programming Language Concepts, cs2104 Lecture 01 ( )

Programming Language Concepts, cs2104 Lecture 01 ( ) Programming Language Concepts, cs2104 Lecture 01 (2003-08-15) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2002-08-15 S. Haridi, CS2104, L01 (slides: C. Schulte, S. Haridi) 1

More information

Logical Methods in... using Haskell Getting Started

Logical Methods in... using Haskell Getting Started Logical Methods in... using Haskell Getting Started Jan van Eijck May 4, 2005 Abstract The purpose of this course is to teach a bit of functional programming and logic, and to connect logical reasoning

More information

Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms

Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms Programming Languages (CSCI 4430/6430) History, Syntax, Semantics, Essentials, Paradigms Carlos Varela Rennselaer Polytechnic Institute August 30, 2016 C. Varela 1 The first programmer ever Ada Augusta,

More information

Programming Language Concepts: Lecture 14

Programming Language Concepts: Lecture 14 Programming Language Concepts: Lecture 14 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 14, 11 March 2009 Function programming

More information

Data Types and Pattern Matching by Function Application

Data Types and Pattern Matching by Function Application Data Types and Pattern Matching by Function Application Jan Martin Jansen 1, Pieter Koopman 2, Rinus Plasmeijer 2 1 Royal Netherlands Navy and ICIS, Radboud University Nijmegen, 2 Institute for Computing

More information

A Concepts-Based Approach for Teaching Programming

A Concepts-Based Approach for Teaching Programming A Concepts-Based Approach for Teaching Programming November 2005 Peter Van Roy Université catholique de Louvain Louvain-la-Neuve, Belgium Nov. 2005 P. Van Roy, UCL, Louvain-la-Neuve 1 Overview Teaching

More information

Exercise 1 ( = 22 points)

Exercise 1 ( = 22 points) 1 Exercise 1 (4 + 3 + 4 + 5 + 6 = 22 points) The following data structure represents polymorphic lists that can contain values of two types in arbitrary order: data DuoList a b = C a (DuoList a b) D b

More information

Functional Logic Programming Language Curry

Functional Logic Programming Language Curry Functional Logic Programming Language Curry Xiang Yin Department of Computer Science McMaster University November 9, 2010 Outline Functional Logic Programming Language 1 Functional Logic Programming Language

More information

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington Programming Languages Thunks, Laziness, Streams, Memoization Adapted from Dan Grossman s PL class, U. of Washington You ve been lied to Everything that looks like a function call in Racket is not necessarily

More information

Advanced Programming Handout 3

Advanced Programming Handout 3 Advanced Programming Handout 3 Review What are the types of these functions? f x = [x] g x = [x+1] h [] = 0 h (y:ys) = h ys + 1 Review How about these? f1 x y = [x] : [y] f2 x [] = x f2 x (y:ys) = f2 y

More information

Review. Advanced Programming Handout 3. Review. Review. Review. Trees. What are the types of these functions? How about these? f1 x y = [x] : [y]

Review. Advanced Programming Handout 3. Review. Review. Review. Trees. What are the types of these functions? How about these? f1 x y = [x] : [y] Advanced Programming Handout 3 Review What are the types of these functions? f x = [x] g x = [x+1] h [] = 0 h (y:ys) = h ys + 1 Review How about these? f1 x y = [x] : [y] f2 x [] = x f2 x (y:ys) = f2 y

More information

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

More information

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen Disjoint Unions and Higher-order list functions nsen 1 DTU Compute, Technical University of Denmark Disjoint Unions and Higher-order list functions MRH 27/09/2018 Overview Recap Disjoint union (or Tagged

More information

foldr CS 5010 Program Design Paradigms Lesson 5.4

foldr CS 5010 Program Design Paradigms Lesson 5.4 oldr CS 5010 Program Design Paradigms Lesson 5.4 Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1 Introduction In this lesson,

More information

State Monad (3D) Young Won Lim 9/25/17

State Monad (3D) Young Won Lim 9/25/17 Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Exercise 1 ( = 24 points)

Exercise 1 ( = 24 points) 1 Exercise 1 (4 + 5 + 4 + 6 + 5 = 24 points) The following data structure represents polymorphic binary trees that contain values only in special Value nodes that have a single successor: data Tree a =

More information

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group Advanced Type System Features Tom Schrijvers Leuven Haskell User Group Data Recursion Genericity Schemes Expression Problem Monads GADTs DSLs Type Type Families Classes Lists and Effect Free Other Handlers

More information

Functional Programming

Functional Programming Functional Programming Overview! Functional vs. imperative programming! Fundamental concepts! Evaluation strategies! Pattern matching! Higher order functions! Lazy lists References! Richard Bird, Introduction

More information

Functional Programming and Haskell

Functional Programming and Haskell Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming

More information

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList

More information

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen Tagged values and Higher-order list functions nsen 1 DTU Compute, Technical University of Denmark Tagged values and Higher-order list functions MRH 3/10/2017 Overview Disjoint union (or Tagged Values)

More information

Haskell Overview II (2A) Young Won Lim 9/26/16

Haskell Overview II (2A) Young Won Lim 9/26/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Homework 4: Declarative Concurrency

Homework 4: Declarative Concurrency COP 4020 Programming Languages 1 October 30, 2011 Homework 4: Declarative Concurrency See Webcourses and the syllabus for due dates. In this homework you will learn about the declarative concurrent model

More information

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen Lecture 2: Functions, Types and Lists nsen 1 DTU Compute, Technical University of Denmark Lecture 2: Functions, Types and Lists MRH 13/09/2018 Outline Functions as first-class citizens Types, polymorphism

More information

Homework 4: Declarative Concurrency

Homework 4: Declarative Concurrency COP 4020 Programming Languages 1 March 31, 2008 Homework 4: Declarative Concurrency Due: problems 3 4, 8 Monday, March 24, 2007 at 11pm; problems 5, 7 Wednesday, March 26, 2007 at 11pm; problems 9 17,

More information

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

Haskell Overview II (2A) Young Won Lim 8/9/16

Haskell Overview II (2A) Young Won Lim 8/9/16 (2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published

More information

Reasoning about programs. Chapter 9 of Thompson

Reasoning about programs. Chapter 9 of Thompson Reasoning about programs Chapter 9 of Thompson Proof versus testing A proof will state some property of a program that holds for all inputs. Testing shows only that a property holds for a particular set

More information

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona 1/20 CSc 372 Comparative Programming Languages 15 : Haskell List Comprehension Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/20 List Comprehensions

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information

Larger K-maps. So far we have only discussed 2 and 3-variable K-maps. We can now create a 4-variable map in the

Larger K-maps. So far we have only discussed 2 and 3-variable K-maps. We can now create a 4-variable map in the EET 3 Chapter 3 7/3/2 PAGE - 23 Larger K-maps The -variable K-map So ar we have only discussed 2 and 3-variable K-maps. We can now create a -variable map in the same way that we created the 3-variable

More information

Patterns The Essence of Functional Programming

Patterns The Essence of Functional Programming Patterns The Essence of Functional Programming Up to now we have defined functions in a very traditional way: function name + variable name parameters Read Chap 7 In functional programming we can exploit

More information

CPM: A Declarative Package Manager with Semantic Versioning

CPM: A Declarative Package Manager with Semantic Versioning Michael Hanus (CAU Kiel) CPM: A Declarative Package Manager with Semantic Versioning CICLOPS 2017 1 CPM: A Declarative Package Manager with Semantic Versioning Michael Hanus University of Kiel Programming

More information

Fall 2018 Lecture N

Fall 2018 Lecture N 15-150 Fall 2018 Lecture N Tuesday, 20 November I m too lazy to figure out the correct number Stephen Brookes midterm2 Mean: 79.5 Std Dev: 18.4 Median: 84 today or, we could procrastinate lazy functional

More information

Reasoning About Programs Panagiotis Manolios

Reasoning About Programs Panagiotis Manolios Reasoning About Programs Panagiotis Manolios Northeastern University March 22, 2012 Version: 58 Copyright c 2012 by Panagiotis Manolios All rights reserved. We hereby grant permission for this publication

More information

Programming Paradigms: Exercises

Programming Paradigms: Exercises Programming Paradigms: Exercises December 5, 2012 1 Generalities 1.1 Paradigms, Languages, Features Consider the language C++ (or your favourite programming language,...). Exercise 1 Write a list of features

More information

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen,

More information

Homework 12 Due Wednesday, 12/8/10

Homework 12 Due Wednesday, 12/8/10 Homework 12 Due Wednesday, 12/8/10 Please turn in your homework solutions online at http://www.dci.pomona.edu/tools-bin/cs131upload.php before the beginning of class on the due date. 1. (10 points) Atomicity

More information

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee 1 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as

More information

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

Functional Programming in Haskell Part I : Basics

Functional Programming in Haskell Part I : Basics Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian

More information

Shell CSCE 314 TAMU. Higher Order Functions

Shell CSCE 314 TAMU. Higher Order Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Higher Order Functions 2 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as a result.

More information

An introduction to functional programming. July 23, 2010

An introduction to functional programming. July 23, 2010 An introduction to functional programming July 23, 2010 About Outline About About What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief

More information

Efficient Interpretation by Transforming Data Types and Patterns to Functions

Efficient Interpretation by Transforming Data Types and Patterns to Functions Efficient Interpretation by Transforming Data Types and Patterns to Functions Jan Martin Jansen 1, Pieter Koopman 2, Rinus Plasmeijer 2 1 The Netherlands Ministry of Defence, 2 Institute for Computing

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

Lecture 5: Lazy Evaluation and Infinite Data Structures

Lecture 5: Lazy Evaluation and Infinite Data Structures Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a

More information

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation

CS 312. Lecture April Lazy Evaluation, Thunks, and Streams. Evaluation CS 312 Lecture 27 29 April 2008 Lazy Evaluation, Thunks, and Streams Evaluation SML as you know it (substitution semantics) if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 if eagerly evaluates

More information

Introduction to Functional Programming: Lecture 7 2 Functions as innite data structures Ordinary ML data structures are always nite. We can, however,

Introduction to Functional Programming: Lecture 7 2 Functions as innite data structures Ordinary ML data structures are always nite. We can, however, Introduction to Functional Programming: Lecture 7 1 Introduction to Functional Programming John Harrison University of Cambridge Lecture 7 Innite data structures Topics covered: Functions as innite data

More information

Logic programming in the context of multiparadigm programming: the Oz experience

Logic programming in the context of multiparadigm programming: the Oz experience Under consideration for publication in Theory and Practice of Logic Programming 1 Logic programming in the context of multiparadigm programming: the Oz experience PETER VAN ROY Université catholique de

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 06: Useful Haskell Syntax, HO Programming Continued o Goodbye to Bare Bones Haskell: Built-in

More information