1 / 17

CS220 Programming Principles

CS220 Programming Principles. 프로그래밍의 이해 2002 가을학기 Class 17: Nondeterministic Computing 한 태숙. Nondeterministic Computing. Nondeterministic computing can be introduced into Scheme with the new special form amb . Interpreting the expression (amb e 1 e 2 )

lorne
Download Presentation

CS220 Programming Principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS220Programming 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 e1 e2) • “nondeterministically” select one of expressions e1or e2 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 ’(1 3 5 8) ’(20 35 110)) ;;; 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. • Amb represents a nondeterministic choice point. • 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 • 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 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))) ;; 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 • Our strategy is to extend the notion of the execution object from the analyze evaluator. • 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. • 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 • 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. Draw a picture representing the execution object ((analyze ’(amb 1 2)) env mainloop-succeed mainloop-fail)

  17. Undo side-effects when backtrack • 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))))

More Related