Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams.

Similar presentations


Presentation on theme: "Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams."— Presentation transcript:

1 Logic Programming (Control and Backtracking)

2 Contents Logic Programming –sans Control –with Backtracking Streams

3 Example (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append q r (cons 1 (cons 2 empty))) –Unify: (append empty x x) { q |→ empty, r |→ (cons 1 (cons 2 empty))

4 Example (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append q r (cons 1 (cons 2 empty))) –Unify: (append (cons w x) y (cons w z)) (append x r (cons 2 empty))

5 Example (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append x r (cons 2 empty)) –Unify: (append empty x x) { x |→ empty, r |→ (cons 2 empty) }

6 Example (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append x r (cons 2 empty)) –Unify: (append (cons w x) y (cons w z)) (append x r empty)

7 Example (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append x r empty) –Unify: (append empty x x) { x |→ empty, r |→ empty }

8 Example (append empty x x) ← (append (cons w x) y (cons w z)) ← (append x y z) (append x r empty) –!Unify: (append (cons w x) y (cons w z))

9 Logic Programming solve –Input: List of Goals –KB: List of Rules –Output: List of mappings –Problem: Keeping variables straight

10 Logic Programming Keeping variables straight –Variable renaming Replace: –(append empty x x) ← With: –(append empty v0 v0) ← –Instantiate as needed

11 Logic Programming (define solve (lambda (goals) (if (null? goals) )))

12 Logic Programming (define solve (lambda (goals) (if (null? goals) ))) Fake Problem –Output solution when found

13 Logic Programming (define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) )))

14 Logic Programming (define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) (for-each (lambda (x) (let ((u (unify (car goals) (head x)))) (if u )))))))

15 Logic Programming (define solve (lambda (goals soln) (if (null? goals) (add-to-answer soln) (for-each (lambda (x) (let ((u (unify (car goals) (head x)))) (if u (solve (append (cdr goals) (inst (tail x) u)) (append soln u)))))))))

16 Backtracking Problem –Want to return solution

17 Backtracking Problem –Want to return solution (define solve (lambda (goals soln k) (if (null? goals) (k soln) …)))

18 Backtracking Problem –Want to return solution *and be able to continue* (define solve (lambda (goals soln k) (if (null? goals) (backtrack k soln) …)))

19 Backtracking Problem –Want to return solution *and be able to continue* (define backtrack (lambda (k soln) (call-with-current-continuation (lambda (x) …))))

20 Backtracking Problem –Want to return solution *and be able to continue* (define backtrack (lambda (k soln) (call-with-current-continuation (lambda (x) (k (cons soln x))))))

21 Backtracking Problem –Want to return solution, be able to continue *and return other solutions* –Current “solution” will use the old continuation to return answers

22 Backtracking (define solve-k (lambda (x) x)) (define solve (lambda (goals soln) (if (null? goals) (backtrack soln) …))) (define backtrack (lambda (soln) (call-with-current-continuation (lambda (x) (solve-k (cons soln x))))))

23 Backtracking Problem –Need to return 0 argument function –When executed, function should get the current-continuation (for return continuation) –Must be able to overwrite old return continuation

24 Backtracking (define backtrack (lambda (soln) (set! solve-k (call-with-current-continuation (lambda (x) …)))))

25 Backtracking (define backtrack (lambda (soln) (set! solve-k (call-with-current-continuation (lambda (x) (solve-k (cons soln …)))))))

26 Backtracking (define backtrack (lambda (soln) (set! solve-k (call-with-current-continuation (lambda (x) (solve-k (cons soln (lambda () (call-with-current- continuation (lambda (y) (x y)))))))))))

27 Backtracking Want to return answer ASAP –Pass your answer forward Need return continuation –Will need to be changed with every continue –Make it global Remember to return continuation –(cons )

28 Backtracking Book –Control entirely handled by continuations This –Mixed control: Backtracking handled by continuations, function control flow handled by Scheme call stack

29 Backtracking Book –sk (success continuation) is solve-k What to do with answers –fk (failure continuation) has no direct equivalent Essentially says, here’s what to do next Is related to for-each statements in code

30 Streams Infinite Lists –(define x (cons 1 1)) –(set-cdr! x x) –(eq? x (cdr x)) → #t –(car x) → 1

31 Streams Infinite List –Consecutive numbers –Primes –Digits of pi –Random numbers

32 Streams stream = (cons ) Very similar to backtracking returns (car x) = (car x) (cdr x) = ((cdr x))


Download ppt "Logic Programming (Control and Backtracking). Contents Logic Programming –sans Control –with Backtracking Streams."

Similar presentations


Ads by Google