CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 17: Nondeterministic Computing 한 태숙.

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
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.
Scheme in Scheme?!? …the metacircular evaluator….
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
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).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
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.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Env. Model Implementation & Analyzer Practice Session #10 Env. Model Implementation & Analyzer Practice Session #10.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
CS61A Lecture Colleen Lewis 1. Clicker poll Are you in a two person group for project 4? A)Yes – I have my partner B)No – I plan to work.
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.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Lecture 4: Metacircles Eval Apply David Evans
Abstract Syntax cs7100 (Prasad) L7AST.
CS314 – Section 5 Recitation 10
Additional Scheme examples
6.001 SICP Variations on a Scheme
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
Nondeterministic Evaluation
First-class continuations
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.
FP Foundations, Scheme In Text: Chapter 14.
Programming Languages (CS 550) Mini Language Semantics
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
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
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
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:

CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 17: Nondeterministic Computing 한 태숙

2 Nondeterministic Computing Nondeterministic computing can be introduced into Scheme with the new special form amb. Interpreting the expression (amb e 1 e 2 ) –“nondeterministically” select one of expressions e 1 or e 2 and continue the evaluation with that expression Since the language with amb is nondeterministic, a given expression can have many different possible executions and many difference possible values.

3 Amb (list (amb 1 2 3) (amb ’a ’b)) =>?? (1 a) (1 b) (2 a) (2 b) (3 a) (3 b) (list (amb 1) (amb ’a)) => (1 a) (define (a-number-between low high) (cond ((= low high) low) (else (amb low (a-number-between (+ low 1) high))))) ( a-number-between 1 10) ==> ?????

4 New Programming Paradiagm (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 ’( ) ’( )) ;;; Starting a new problem ;;; Amb-Eval value: (3 20)

5 Amb and search Amb with no choice (amb) is an expression with no acceptable values. Operationally, we think of (amb) as an expression that when evaluated causes the computation to “fail”: The computation aborts and no value is produced. n Amb represents a nondeterministic choice point. n We need to systematically search all possible execution paths. –need backtracking –depth-first search

6 Programming with amb (define (require p) (if (not p) (amb))) (define (an-element-of items) (require (not (null? items))) (amb (car items) (an-element-of (cdr items)))) ;; infinite ranges of choices (define (an-integer-starting-from n) (amb n (an-integer-starting-from (+ n 1))))

7 Read-eval-print Loop for Amb Evaluator (define (a-pythagorean-triple-between low high) (let ((n (a-number-between low high)) (m (a-number-between low high)) (p (a-number-between low high))) (require (= (+ (* n n) (* m m)) (* p p))) (list n m p))) ;;; Amb-Eval input: (a-number-between 1 10) ;;; Starting a new problem ;;; Amb-Eval value: 1

8 ;;; Amb-Eval input: try-again ;;; Amb-Eval value: 2 ;;; Amb-Eval input: (a-pythagorean-triple-between 1 10) ;;; Starting a new problem ;;; Amb-Eval value: (3 4 5) ;;; Amb-Eval input: try-again ;;; Amb-Eval value:

9 Multiple Dwelling Problem n 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 a Fletcher’s. –Fletcher does not live on a floor adjacent to Cooper’s. Where does everyone live?

10 (define (distinct list) (cond ((null? list) true) ((null? (cdr list)) true) ((member (car list) (cdr list)) false) (else (distinct (cdr list))))) (define (multiple-dwelling) (let ((baker (amb )) (cooper (amb )) (fletcher (amb )) (miller (amb )) (smith (amb ))) ;; Beginning of filteration (require (distinct (list baker cooper fletcher miller smith))) (require (not (= baker 5))) (require (not (= cooper 1))) (require (not (= fletcher 5))) (require (not (= fletcher 1)))

11 (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)))) ;;;Amb-Eval input: (multiple-dwelling) ;;; Starting a new problem ;;; Amb-Eval value: ((baker 3)(cooper 2)(fletcher 4)(miller 5)(smith 1)) ;;; Amb-Eval input: try-again ;;; There are no more values of (multiple-dwelling)

12 Amb Eval Implementation n Our strategy is to extend the notion of the execution object from the analyze evaluator. n It becomes a procedure that can be used to complete an evaluation, and can also be used to try for more values from an evaluation. n This execution object takes an environment, a succeed procedure, and a fail procedure.

13 (define (analyze-self-evaluating exp) (lambda (env succeed fail) (succeed exp fail))) ;; Draw a picture ((analyze ’1) env mainloop-succeed mainloop-fail)

14 Simplified version of Amb evaluator n Simplified version of read-eval-print –mainloop-succeed, mainloop-fail –when succeed, when rejected, when fail (define (mainloop-succeed value fail-continuation) (display “got a value” value) (let ((exp (read))) (if (eq? exp ‘’try-again) (fail-continuation) (amb-eval exp t-g-e mainloop-succeed mainloop-fail)))) (define mainloop-fail) (display “no more values”))

15 Amb expression - backtracking (define (analyze-amb exp) (let ((choice-exes (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 choice-exes))))

16 n Draw a picture representing the execution object ((analyze ’(amb 1 2)) env mainloop-succeed mainloop-fail)

17 Undo side-effects when backtrack n analyze-assignment (define (analyze-assignment exp) (let ((var (assignment-variable exp)) (value-exe (analyze (assignment-value exp)))) (lambda (env succeed fail) (value-exe env (lambda (val fail2) (let ((old-value (lookup-variable-value var env))) (set-variable-value! var val env) (succeed ’ok (lambda() (set-variable-value! var old-value env) (fail2))))) fail))))