Lazy Functional Programming for a survey

Similar documents
Vocabulary. In-Class Note-Taking. Why did Grant attack the town of Jackson? I thought he was trying to attack Vicksburg!

Here is the wurst presentation in the history of the world

American History I Unit 5 Crisis and War Day 7 The Civil War (cont.)

What the author is SAYING The Gettysburg Address What the author is DOING

Leadership : Art or Science? H.S.Mohanty

Assignment #7: user-defined commands, minipages, colors and iteration

Midterm #2: March in the Testing Center

... Readers Theatre. Gettysburg and Mr. Lincoln s Speech. Resource 17: Every. Child. Reads

AP Language Summer Assignment Part 1: Rhetorical Strategies and Terms

Eton College King s Scholarship Examination 2017 ENGLISH. (One and a half hours) Remember to write your candidate number on every sheet of paper.

Timed Writing Finish by

Lincoln's Gettysburp Address

What A Union army, consisting of 28,000 men fought 33,000 Confederates. 1 st battle of the Civil War. When July 21, 1861 Where Bull Run Creek,

Four Score and Seven Years Ago: Abraham Lincoln, the Gettysburg Address, and Identity

Practice & Review: Monday, 5/1

THE GETTYSBURG ADDRESS TEACHER RESOURCE GUIDE

Natural Language Processing

Welcome to 11AP Language & Composition

Washington D.C. Packet

Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that

Artificial Intelligence: Valid Arguments and Proof Systems. Prof. Deepak Khemani. Department of Computer Science and Engineering

1863: Shifting Tides. Cut out the following cards and hand one card to each of the pairs.

2.1 Review. 2.2 Inference and justifications

Module 02 Lecture - 10 Inferential Statistics Single Sample Tests

The exiles did not know the details of God s plan for them at the time, and I am sure they were shocked when the plan was revealed.

and that government of the people, by the people, for the people, shall not perish from the earth.

(Refer Slide Time 03:00)

MISSOURI S FRAMEWORK FOR CURRICULAR DEVELOPMENT IN MATH TOPIC I: PROBLEM SOLVING

15 Does God have a Nature?

REMEMBERING THOSE WHO HAVE GIVEN THEIR LIVES FOR OTHERS

Abraham Lincoln

COMMON CORE UNIT: A Close Reading of Lincoln s Gettysburg Address

MITOCW watch?v=4hrhg4euimo

USF MASTERS OF SOCIAL WORK PROGRAM ASSESSMENT OF FOUNDATION STUDENT LEARNING OUTCOMES LAST COMPLETED ON 4/30/17

Module 5. Knowledge Representation and Logic (Propositional Logic) Version 2 CSE IIT, Kharagpur

Digital Logic Lecture 5 Boolean Algebra and Logic Gates Part I

Basic Algorithms Overview

McDougal Littell High School Math Program. correlated to. Oregon Mathematics Grade-Level Standards

Artificial Intelligence Prof. P. Dasgupta Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

The Fallacy in Intelligent Design

Grade 6 correlated to Illinois Learning Standards for Mathematics

Verification and Validation

4.1 A problem with semantic demonstrations of validity

Civil War Lesson #5: Lincoln s Speeches

Rational and Irrational Numbers 2

Georgia Quality Core Curriculum

Russell s Problems of Philosophy

What is a counterexample?

Weekly Bible Study July 5, Scott L. Engle

CAPITALS. Confederacy. Union. Capital = Washington D.C. Capital = Richmond, VA Only 107 Miles apart!

occasions (2) occasions (5.5) occasions (10) occasions (15.5) occasions (22) occasions (28)

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Logical Omniscience in the Many Agent Case

6.041SC Probabilistic Systems Analysis and Applied Probability, Fall 2013 Transcript Lecture 3

Grade 7 Math Connects Suggested Course Outline for Schooling at Home 132 lessons

LGCS 199DR: Independent Study in Pragmatics

Semantic Entailment and Natural Deduction

Tools Andrew Black CS 305 1

UIL READY WRITING PRACTICE PACKET STATE

Deacons Formation School Course Descriptions

GSMEC NEWSLETTER. Team Selection Report

Semantics and Pragmatics of NLP DRT: Constructing LFs and Presuppositions

Macmillan/McGraw-Hill SCIENCE: A CLOSER LOOK 2011, Grade 3 Correlated with Common Core State Standards, Grade 3

Inference in Cyc. Copyright 2002 Cycorp

The Gettysburg Address

Improving Students' "Dialectic Tracking" Skills (Diagramming Complex Arguments) Cathal Woods for 2010 AAPT Meeting.

Macmillan/McGraw-Hill SCIENCE: A CLOSER LOOK 2011, Grade 1 Correlated with Common Core State Standards, Grade 1

Prentice Hall United States History Survey Edition 2013

Artificial Intelligence Prof. P. Dasgupta Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Worksheet 4.1: Chapter 4 methods of persuasion

ON SOPHIE GERMAIN PRIMES

9 Knowledge-Based Systems

Discussion Notes for Bayesian Reasoning

CS485/685 Lecture 5: Jan 19, 2016

Causation and Free Will

Sorting: Merge Sort. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

NATIONAL CEMETERY Walking Tour

Excel Lesson 3 page 1 April 15

Pronominal, temporal and descriptive anaphora

Evolution and Meaning. Richard Oxenberg. Suppose an infinite number of monkeys were to pound on an infinite number of

Logic and Pragmatics: linear logic for inferential practice

Liberty s Believers Enrichment Program. Keys of the Kingdom I. SESSION 1 (v )

CS 2104 Intro Problem Solving in Computer Science Test 1 READ THIS NOW!

The following content is provided under a Creative Commons license. Your support

Module - 02 Lecturer - 09 Inferential Statistics - Motivation

Prentice Hall U.S. History Modern America 2013

Philosophy of Logic and Artificial Intelligence

PROSPECTIVE TEACHERS UNDERSTANDING OF PROOF: WHAT IF THE TRUTH SET OF AN OPEN SENTENCE IS BROADER THAN THAT COVERED BY THE PROOF?

ECE 5424: Introduction to Machine Learning

Correspondence with Martin Gardner On Fibonacci Kolams

Abraham Lincoln.. Speaks

Computational Learning Theory: Agnostic Learning

Logic I or Moving in on the Monkey & Bananas Problem

CD 511 The Pastor and Christian Discipleship

OSSA Conference Archive OSSA 8

Artificial Intelligence. Clause Form and The Resolution Rule. Prof. Deepak Khemani. Department of Computer Science and Engineering

INTERMEDIATE LOGIC Glossary of key terms

Abraham Lincoln and the Civil War

SQL: An Implementation of the Relational Algebra

Transcription:

Lazy Functional Programming for a survey Norman Ramsey Tufts November 2012

Book: Programming languages for practitioners Why? For people who will write code Gives future practitioners something to do I want your help: What can they do with laziness?

Modest goals for readers A few ideas of lasting value Embodied in little languages Build, prove and compare A starting point, not mastery Motivated readers might tackle ICFP, POPL (Intended for 3rd- or 4th-year students, young professionals)

What ideas have lasting value? Proven stuff! Can they build something using Functions? Types? Objects? A little operational semantics; a little type theory Bonus: garbage collection, logic programming

µhaskell chapter is being written in a context One common syntax (Lisp-like) Untyped functional programming: Lists, symbols, integers Mutation discouraged First-class functions Standard higher-order funs (map, filter, foldr, curry,... ) Typed functional programming: The agony of System F µml, a nearly pure language with type inference Some algebraic data types

µhaskell: I want eyes to pop and heads to explode Other outcomes: They can apply a couple of standard lazy tricks They want to try laziness for themselves They think maybe it s not just a toy Assuming they give it a week and 6 10 exercises

To motivate and introduce, I use search Example where eager evaluation is not so good: Find square multiple of 5 greater than 137 (find-in-list (lambda (n) (> n 137)) (filter (lambda (n) (= (mod n 5) 0)) (map square (integer-range 0 13)))) How high do you look? Is [0..13] high enough?

Mechanism: infinite lazy data structures -> (val-rec ones (cons 1 ones))... : int list -> (val-rec nats (cons 0 (map ((curry +) 1) nats)))... : int list -> (full (take 10 nats)) (0 1 2 3 4 5 6 7 8 9) : int list

More mechanism: Incompletely evaluated list -> (val squares (map square nats))... : int list -> (full (take 13 squares)) (0 1 4 9 16 25 36 49 64 81 100 121 144) : int list -> (car (drop 19 squares)) 361 : int -> squares (0 1 4 9 16 25 36 49 64 81 100 121 144.................. 361...) : int list

Many small examples that do something Putting infinite sequences to work (thanks to John Hughes) Searching for numbers Square roots and cube roots (scaled integers) -> (full (take 8 (approximate-roots 9))) (9000 5000 3400 3023 3000 3000 3000 3000) : int list -> (full (take 8 (approximate-roots 2))) (2000 1500 1416 1414 1414 1414 1414 1414) : int list Multiple examples of convergence Prime numbers by trial division Backing up CDs to DVDs (bin packing) Speedup via memoization Naive Fibonacci

Medium-sized example: Boolean satisfiability They ve done it in an eager language by passing continuations Two new approaches that rely on laziness (filter ((curry satisfied-by?) formula) (all-assignments formula)) Backtracking search á la Replace failure by a list of successes (Neither approach dominates!)

Integrative example: Paragraphs into lines But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us --- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion --- that we here highly resolve that these dead shall not have died in vain --- that this nation, under God, shall have a new birth of freedom --- and that government of the people, by the people, for the people, shall not perish from the earth. But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us --- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion --- that we here highly resolve that these dead shall not have died in vain --- that this nation, under God, shall have a new birth of freedom --- and that government of the people, by the people, for the people, shall not perish from the earth. Greedy Knuth-Plass (Both sides monospaced)

Knuth-Plass on a simple paragraph Input: The dog ate my code oh yes he did. Output for 7 columns, shrink 1, stretch 2: The dog ate my code oh yes he did. Best breaks and their costs: 0 The 656100 dog 100 656200 ate my 584 120993 code oh 684 121477 yes he 1168 1268 did.

Core of solution uses a mutually recursive nest 0 The 656100 dog 100 656200 ate my 584 120993 code oh 684 121477 yes he 1168 1268 did. Best breakpoint computed using ordinary-badness, last-badness: word list -> badness best : (word list -> badness) -> word list -> break best-ordinary : word list -> break = (best ordinary-badness) candidate-lines : word list -> (break * word list) list

Memoization makes it practical 0 The 656100 dog 100 656200 ate my 584 120993 code oh 684 121477 yes he 1168 1268 did. Insert memo list into recursion: best : (word list -> badness) -> word list -> break One new definition memo : break list = (map (best badness) (tails words)) One changed definition best-ordinary : word list -> break = (lambda (ws) (... memo... ws...)) candidate-lines : word list -> (break * word list) list

Laziness + Memoization = Gettysburg Address Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us --- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion --- that we here highly resolve that these dead shall not have died in vain --- that this nation, under God, shall have a new birth of freedom --- and that government of the people, by the people, for the people, shall not perish from the earth. Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us --- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion --- that we here highly resolve that these dead shall not have died in vain --- that this nation, under God, shall have a new birth of freedom --- and that government of the people, by the people, for the people, shall not perish from the earth. Greedy Knuth-Plass

I like some of this Three things people can do Have fun with infinite lists Write search using modular generate and test Memoize Two things people can gawk at Clean dynamic programming Line breaking

Some things concern me Who cares about sequences of numbers? Line breaking may be too big a leap Not enough good exercises!

Two questions you can answer If you ve learned a little lazy functional programming, What do you know? What can you do?

Randomized improvement of greedy algorithms Bubble search concept from Lesh and Mitzenmacher bubblesearch :: (result -> result -> Ordering) -- quality -> ([a] -> result) -- greedy search algorithm -> Double -- biased coin -> [a] -- items to be searched -> Random [result] -- monadic, monotone results (E.g., type a is a CD and result is a packing of DVDs.)

Some ideas for exercises Smallest prime at least 1000 and contains a 7 Memoization combinators? Bubble search (higher-order improvement for greedy algorithms) Stupid line-breaking tricks Add lazy streams to an eager language

µhaskell concrete syntax is small Definition forms: val, val-rec, define, datatype definition top-level expression Expression forms: literal, variable, application, λ-abstraction let-binding (let, let*, letrec) if non-nested case (datatype elimination, forcing)

Numerical example: square root (thanks RJMH) n approximations: xi+1 = 1 2 (x i + n/x i ) (define approximate-roots (n) ;;; on SCALED integers (letrec ((n# (scale n)) (roots-from (lambda (x_i) (let ((x_{i+1} (/ (+s x_i (/s n# x_i)) 2))) (cons x_i (roots-from x_{i+1})))))) (roots-from n#))) Transcript: -> (val three (approximate-roots 9))... : int list -> (full (take 8 three)) (9000 5000 3400 3023 3000 3000 3000 3000) : int list -> (full (take 8 (approximate-roots 2))) (2000 1500 1416 1414 1414 1414 1414 1414) : int list