Nondeterministic Evaluation

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 17: Nondeterministic Computing 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
6.001 SICP Compilation Context: special purpose vs. universal machines
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
COP4020 Programming Languages
Env. Model Implementation
Original material by Eric Grimson
Introduction to Functional Programming in Racket
6.001 SICP Data abstractions
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Lecture 18 Infinite Streams and
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Explicit-control evaluator
6.001 SICP Explicit-control evaluator
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Nondeterministic Evaluation September 4, 1997 Nondeterministic Evaluation CS 550 Programming Languages Jeremy Johnson

September 4, 1997 Theme This lecture explores a language feature that provides for nondeterministic computing. It allows a declarative style of programming, that supports the “generate and test” paradigm, where the programmer provides what is to be computed and the underlying language implementation provides the control mechanism (search with backtracking) to carry out the computation. Implementation relies on “continuations” which encapsulate control flow choices.

Outline Motivating example Amb and search September 4, 1997 Outline Motivating example Amb and search Review of analyzing interpreter Continuations and an implementation of the Amb Interpreter

Motivating Example (define (prime-sum-pair list1 list2) September 4, 1997 Motivating Example (define (prime-sum-pair list1 list2) (let ((a (an-element-of list1)) (b (an-element-of list2))) (require (prime? (+ a b))) (list a b)))

Motivating Example (define (search-prime-pairs L1 L2) September 4, 1997 Motivating Example (define (search-prime-pairs L1 L2) (define (search-fixed x L) (if (null? L) null (if (prime? (+ x (first L))) (list x (first L) (+ x (first L))) (search-fixed x (rest L))))) (if (null? L1) '() (let ((p (search-fixed (first L1) L2))) (if (not (null? p)) p (search-prime-pairs (rest L1) L2))))) ;;; backtrack > (search-prime-pairs '(1 3 5 8) '(20 35 110)) '(3 20 23)

September 4, 1997 Motivating Example (define (search-all-prime-pairs L1 L2) (define (search-fixed x L) (if (null? L) null (if (prime? (+ x (first L))) (cons (list x (first L) (+ x (first L))) (search-fixed x (rest L))) ;;; continue search (search-fixed x (rest L))))) (if (null? L1) '() (let ((P (search-fixed (first L1) L2))) (append P (search-all-prime-pairs (rest L1) L2))))) > (search-all-prime-pairs '(1 3 5 8) '(20 35 110)) '((3 20 23) (3 110 113) (8 35 43))

Motivating Example (define (all-pairs L1 L2) September 4, 1997 Motivating Example (define (all-pairs L1 L2) (if (or (null? L1)(null? L2)) null (append (map (lambda (x) (list (first L1) x)) L2) (all-pairs (rest L1) L2)))) > (filter (lambda (p) (prime? (+ (first p) (second p)))) (all-pairs '(1 3 5 8) '(20 35 110))) '((3 20) (3 110) (8 35))

Motivating Example (define (all-pairs-streams L1 L2) September 4, 1997 Motivating Example (define (all-pairs-streams L1 L2) (if (or (stream-empty? L1)(stream-empty? L2)) empty-stream (stream-append (stream-map (lambda (x) (list (stream-first L1) x)) L2) (all-pairs-streams (stream-rest L1) L2)))) > (stream-first (stream-filter (lambda (p) (prime? (+ (first p) (second p)))) (all-pairs-streams '(1 3 5 8) '(20 35 110)))) '(3 20)

Motivating Example (define (prime-sum-pair list1 list2) September 4, 1997 Motivating Example (define (prime-sum-pair list1 list2) (let ((a (an-element-of list1)) (b (an-element-of list2))) (require (prime? (+ a b))) (list a b))) ;;; Amb-Eval input: (prime-sum-pair '(1 3 5 8) '(20 35 110)) ;;; Starting a new problem ;;; Amb-Eval value: (3 20)

Motivating Example ;;; Amb-Eval input: September 4, 1997 Motivating Example ;;; Amb-Eval input: (prime-sum-pair '(1 3 5 8) '(20 35 110)) ;;; Starting a new problem ;;; Amb-Eval value: (3 20) try-again (3 110) (8 35) ;;; Amb-Eval input: try-again ;;; There are no more values of (prime-sum-pair (quote (1 3 5 8)) (quote (20 35 110))) (prime-sum-pair '(19 27 30) '(11 36 58)) ;;; Starting a new problem ;;; Amb-Eval value: (30 11)

September 4, 1997 Amb Construct The (amb <e1> … <en>) “ambiguously” returns one of the values <ei> Example (list (amb 1 2 3) (amb 'a 'b)) Chooses one of the following (1 a) (1 b) (2 a) (2 b) (3 a) (3 b)

Nondeterministic Branching September 4, 1997 Nondeterministic Branching amb splits the computation into branches where computation continues on each branch with one possible value. amb is a non-deterministic choice point Failure causes backtracking Implementation uses depth-first seach “chronological backtracking” Driver loop can be prompted with “try-again” to continue search for more solutions

Nondeterministic Selection September 4, 1997 Nondeterministic Selection (define (require p) (if (not p) (amb))) ;;; (amb) fails (define (an-element-of items) (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) (define (an-integer-starting-from n) (amb n (an-integer-starting-from (+ n 1)))) (define (naturals) (an-integer-starting-from 0))

Backtracking (prime-sum-pair '(1 3 5 8) '(20 35 110)) (amb 1 3 5 8) 8

September 4, 1997 Logic Constraints Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper. Smith does not live on a floor adjacent to Fletcher's. Fletcher does not live on a floor adjacent to Cooper's. Where does everyone live?

Logic Constraints (define (multiple-dwelling) September 4, 1997 Logic Constraints (define (multiple-dwelling) (let ((baker (amb 1 2 3 4 5)) (cooper (amb 1 2 3 4 5)) (fletcher (amb 1 2 3 4 5)) (miller (amb 1 2 3 4 5)) (smith (amb 1 2 3 4 5))) (require (distinct? (list baker cooper fletcher miller smith))) (require (not (= baker 5))) (require (not (= cooper 1))) (require (not (= fletcher 5))) (require (not (= fletcher 1))) (require (> miller cooper)) (require (not (= (abs (- smith fletcher)) 1))) (require (not (= (abs (- fletcher cooper)) 1))) (list (list 'baker baker) (list 'cooper cooper) (list 'fletcher fletcher) (list 'miller miller) (list 'smith smith)))) (multiple-dwelling) => ((baker 3) (cooper 2) (fletcher 4) (miller 5) (smith 1))

Implementation Modify analyzing evaluator September 4, 1997 Implementation Modify analyzing evaluator Each execution procedure now takes three arguments and environment and two functions called continuations to complete the computation Success continuation Failure continuation If a value is returned the success continuation is called with that value If the evaluation results in a dead end, the failure continuation is called

September 4, 1997 Continuations The job of the success continuation is to receive a value and proceed with the computation The job of the failure continuation is to try another branch May result from (amb) Try another non-deterministic choice If none, a failure at an earlier choice point is triggered

September 4, 1997 Structure of Amb-eval A success continuation is a procedure of two arguments: the value just obtained and another failure continuation to be used if that value leads to a subsequent failure. A failure continuation is a procedure of no arguments (define (ambeval exp env succeed fail) ((analyze exp) env succeed fail)) (ambeval <exp> the-global-environment (lambda (value fail) value) (lambda () 'failed))

September 4, 1997 To Be Continued

Creating Iterators (define (make-it L) (let ((Lp L)) (lambda () September 4, 1997 Creating Iterators (define (make-it L) (let ((Lp L)) (lambda () (if (null? Lp) 'fail (let ((val (first Lp))) (begin (set! Lp (rest Lp)) val)))))) (define (until iter done?) (let ((p (iter))) (if (eq? p 'fail) (if (done? p) p (until iter done?)))))

Using Iterators > (define next (make-it '(12 18 21))) > (next) September 4, 1997 Using Iterators > (define next (make-it '(12 18 21))) > (next) 12 18 21 'fail > (define next (make-it '(12 18 21 23 33 37))) > (until next prime?) 23

Motivating Example (define (it-pairs L1 L2) (let ((L1p L1) (L2p L2)) September 4, 1997 Motivating Example (define (it-pairs L1 L2) (let ((L1p L1) (L2p L2)) (lambda () (if (or (null? L1) (null? L2)) 'fail (if (not (null? L2p)) (let ((val (list (first L1p) (first L2p)))) (begin (set! L2p (rest L2p)) val)) (begin (set! L2p L2) (set! L1p (rest L1p)) (if (null? L1p) (begin (set! L2p (rest L2p)) val)))))))))

September 4, 1997 Motivating Example > (define next-pair (it-pairs '(1 3 5 8) '(20 35 110))) > (until next-pair (lambda (p) (prime? (+ (first p) (second p))))) '(3 20)

Iterator with Continuation September 4, 1997 Iterator with Continuation (define (make-continue-it L testp) (let ((Lp L)) (lambda (succeed fail) (define (try-next Lp) (if (null? Lp) (fail) (testp (first Lp) succeed (lambda () (try-next (cdr Lp)))) )) (try-next L))))

Using Continuations (define (testodd? x succeed fail) September 4, 1997 Using Continuations (define (testodd? x succeed fail) (if (odd? x) (succeed x fail) (fail))) > (define it (make-continue-it '(2 5 4 7) testodd?)) > (it (lambda (val fail) val) (lambda () 'fail)) 5 > (it (lambda (val fail) (display val) (newline) (fail)) (lambda () 'fail)) 7 'fail > (it (lambda (val fail) (cons val (fail))) (lambda () '())) '(5 7)

September 4, 1997 Resume Implementing ambeval

Analyzing Interpreter September 4, 1997 Analyzing Interpreter (define (eval exp env) ((analyze exp) env)) (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted exp)) ((variable? exp) (analyze-variable exp)) ((assignment? exp) (analyze-assignment exp)) ((definition? exp) (analyze-definition exp)) ((if? exp) (analyze-if exp)) ((lambda? exp) (analyze-lambda exp)) ((begin? exp) (analyze-sequence (begin-actions exp))) ((cond? exp) (analyze (cond->if exp))) ((application? exp) (analyze-application exp)) (else (error "Unknown expression type -- ANALYZE" exp))))

Simple Expressions (define (analyze-self-evaluating exp) September 4, 1997 Simple Expressions (define (analyze-self-evaluating exp) (lambda (env) exp)) (define (analyze-variable exp) (lambda (env) (lookup-variable-value exp env))) (define (analyze-assignment exp) (let ((var (assignment-variable exp)) (vproc (analyze (assignment-value exp)))) (lambda (env) (set-variable-value! var (vproc env) env) 'ok)))

Conditional (define (analyze-if exp) September 4, 1997 Conditional (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) (lambda (env) (if (true? (pproc env)) (cproc env) (aproc env))))) (define (analyze-lambda exp) (let ((vars (lambda-parameters exp)) (bproc (analyze-sequence (lambda-body exp)))) (lambda (env) (make-procedure vars bproc env)))) ; analyze-sequence composes the functions in the sequence

Amb Interpreter (define (ambeval exp env succeed fail) September 4, 1997 Amb Interpreter (define (ambeval exp env succeed fail) ((analyze exp) env succeed fail)) (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted exp)) ((variable? exp) (analyze-variable exp)) ((assignment? exp) (analyze-assignment exp)) ((definition? exp) (analyze-definition exp)) ((if? exp) (analyze-if exp)) ((lambda? exp) (analyze-lambda exp)) ((begin? exp) (analyze-sequence (begin-actions exp))) ((cond? exp) (analyze (cond->if exp))) ((let? exp) (analyze (let->combination exp))) ;** ((amb? exp) (analyze-amb exp)) ;** ((application? exp) (analyze-application exp)) (else (error "Unknown expression type -- ANALYZE" exp))))

Calling Amb Interpreter September 4, 1997 Calling Amb Interpreter (define (ambeval exp env succeed fail) ((analyze exp) env succeed fail)) (ambeval exp the-global-environment (lambda (val fail) val) (lambda () ‘fail))

Simple Expressions (define (analyze-self-evaluating exp) September 4, 1997 Simple Expressions (define (analyze-self-evaluating exp) (lambda (env succeed fail) (succeed exp fail))) (define (analyze-variable exp) (succeed (lookup-variable-value exp env) fail)))

Conditionals (define (analyze-if exp) September 4, 1997 Conditionals (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) (lambda (env succeed fail) (pproc env ;; success continuation for evaluating the predicate ;; to obtain pred-value (lambda (pred-value fail2) (if (true? pred-value) (cproc env succeed fail2) (aproc env succeed fail2))) ;; failure continuation for evaluating the predicate fail))))

Assignment (define (analyze-assignment exp) September 4, 1997 Assignment (define (analyze-assignment exp) (let ((var (assignment-variable exp)) (vproc (analyze (assignment-value exp)))) (lambda (env succeed fail) (vproc env (lambda (val fail2) ; *1* (let ((old-value (lookup-variable-value var env))) (set-variable-value! var val env) (succeed 'ok (lambda () ; *2* (set-variable-value! var old-value env) (fail2))))) fail))))

Amb (define (analyze-amb exp) September 4, 1997 Amb (define (analyze-amb exp) (let ((cprocs (map analyze (amb-choices exp)))) (lambda (env succeed fail) (define (try-next choices) (if (null? choices) (fail) ((car choices) env succeed (lambda () (try-next (cdr choices)))))) (try-next cprocs))))

Driver Loop September 4, 1997 (define (driver-loop) (define (internal-loop try-again) (prompt-for-input input-prompt) (let ((input (read))) (if (eq? input 'try-again) (try-again) (begin (newline) (display ";;; Starting a new problem ") (ambeval input the-global-environment ;; ambeval success (lambda (val next-alternative) (announce-output output-prompt) (user-print val) (internal-loop next-alternative)) ;; ambeval failure (lambda () (announce-output ";;; There are no more values of") (user-print input) (driver-loop))))))) (internal-loop (lambda () (newline) (display ";;; There is no current problem") (driver-loop))))