Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.

Slides:



Advertisements
Similar presentations
Messiaen Quartet for the end of time And another.
Advertisements

C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
CSE 425: Target Machine Architecture Target Machine Details Many architectures can be similar in overall structure –E.g., Von Neumann with CISC instruction.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
CSE 425: Concurrency II Semaphores and Mutexes Can avoid bad inter-leavings by acquiring locks –Guard access to a shared resource to take turns using it.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols.
CSE 425: Control Abstraction II Exception Handling Previous discussion focuses on normal control flow –Sometimes program reaches a point where it cannot.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Functional Programming
CS314 – Section 5 Recitation 9
(Thunking about Thunks)
Names and Attributes Names are a key programming language feature
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
6.001 SICP Variations on a Scheme
6.001 SICP Compilation Context: special purpose vs. universal machines
CS 326 Programming Languages, Concepts and Implementation
Revision Lecture
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Introduction to Functional Programming in Racket
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
Chapter 14: Exception Handling
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Exception Handling Chapter 9.
Mini Language Interpreter Programming Languages (CS 550)
Streams Sections 3.5.1,3.5.2 Pages
Transactional Memory Semaphores, monitors, and conditional critical regions all suffer from limitations based on lock semantics Naïve synchronization may.
The Metacircular Evaluator
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Dynamic Scoping Lazy Evaluation
Lecture #6 section pages pages72-77
Iteration Implemented through loop constructs (e.g., in C++)
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Parallelism and Concurrency
Announcements Quiz 6 HW7 due Tuesday, October 30
6.001 SICP Streams – the lazy way
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
Lecture 12: Message passing The Environment Model
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
Backpropagation Disclaimer: This PPT is modified based on
6.001 SICP Interpretation Parts of an interpreter
CST8177 Scripting 2: What?.
Chapter 10 Thinking in Objects Part 1
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
The return Statement © 2018 Kris Jordan.
Presentation transcript:

Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually evaluated Evaluation of some expressions is delayed until after another is evaluated (e.g., a then b or c in (if a b c) in Scheme) With procedures another form of delay may occur Procedure does some computation before calling another May decide whether or not to call it at all Useful especially for recursive calls – selective descent of a data structure, termination conditions, etc.

Lazy Evaluation Six rules (the Scheme run-time environment follows) All arguments to user defined functions are delayed All local bindings (e.g., let and letrec) are delayed All arguments to constructors (e.g., cons) are delayed All arguments to other pre-defined functions (e.g., * and +) are forced (to be evaluated as soon as possible) All function valued arguments are forced All conditions in selection forms (e.g., if, cond) are forced Efficiency considerations: using delay, force yourself It’s efficient not to compute results you don’t need However, if a delayed expression is repeatedly evaluated then you can lose some of that benefit

Functions as Values Functions can be computed and returned by other functions, passed as arguments to other functions I.e., functions are “first class” data values Composition of one function with another If f : X → Y and g : Y → Z then (g ס f)(x) = g(f(x)) Composition is itself a higher-order function Function(s) as parameter(s) and/or returns another function

Stream Oriented Functional Programs A program may compute only part of a long list Allows the rest to be ignored if the program halts sooner A program may make arbitrary progress through a list Alternatively, allows the list to be unbounded in length Motivates a model of programs operating on streams Can abstract away notion of the length of a list Allows functional programs to handle abstractions like the standard input, output, and error streams, files, etc. Also known as the generator-filter programming model Taken farther, supports databases, etc. naturally Data input, output, and manipulation, user input, file input and output, etc. all handled similarly via interacting functions

Today’s Studio Exercises We’ll continue to look at ideas from Scott Chapter 10 Especially delayed evaluation, continuations, etc. Today’s exercises are again all in C++ Please take advantage of the on-line reference manual pages that are linked on the course web site The text books also may be helpful for some exercises As always, please ask us for help as needed When done, email your answers to the course account with subject line “Functional Programming Studio II” Send to cse425@seas.wustl.edu