View
29
Download
0
Category
Tags:
Preview:
DESCRIPTION
Logic Programming (Control and Backtracking). Contents. Logic Programming sans Control with Backtracking Streams. 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) - PowerPoint PPT Presentation
Citation preview
Logic Programming (Control and Backtracking)
Contents
• Logic Programming– sans Control– with Backtracking
• Streams
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))
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))
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) }
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)
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 }
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))
Logic Programming
• solve– Input: List of Goals– KB: List of Rules– Output: List of mappings
– Problem:• Keeping variables straight
Logic Programming
• Keeping variables straight– Variable renaming
• Replace:– (append empty x x) ←
• With:– (append empty v0 v0) ←
– Instantiate as needed
Logic Programming
(define solve
(lambda (goals)
(if (null? goals)
<then-expression>
<else-expression>)))
Logic Programming
(define solve
(lambda (goals)
(if (null? goals)
<found a solution>
<try to find rule for first goal>)))
• Fake Problem– Output solution when found
Logic Programming
(define solve
(lambda (goals soln)
(if (null? goals)
(add-to-answer soln)
<try to find all rules for first goal>)))
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 <call to solve>)))))))
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)))))))))
Backtracking
• Problem– Want to return solution
Backtracking
• Problem– Want to return solution
(define solve
(lambda (goals soln k)
(if (null? goals)
(k soln)
…)))
Backtracking
• Problem– Want to return solution *and be able to
continue*
(define solve
(lambda (goals soln k)
(if (null? goals)
(backtrack k soln)
…)))
Backtracking
• Problem– Want to return solution *and be able to
continue*
(define backtrack
(lambda (k soln)
(call-with-current-continuation
(lambda (x) …))))
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))))))
Backtracking
• Problem– Want to return solution, be able to continue
*and return other solutions*
– Current “solution” will use the old continuation to return answers
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))))))
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
Backtracking
(define backtrack
(lambda (soln)
(set! solve-k
(call-with-current-continuation
(lambda (x) …)))))
Backtracking
(define backtrack
(lambda (soln)
(set! solve-k
(call-with-current-continuation
(lambda (x) (solve-k (cons soln …)))))))
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)))))))))))
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 <answer> <continue>)
Backtracking
• Book– Control entirely handled by continuations
• This– Mixed control: Backtracking handled by
continuations, function control flow handled by Scheme call stack
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
Streams
• Infinite Lists– (define x (cons 1 1))– (set-cdr! x x)– (eq? x (cdr x)) → #t– (car x) → 1
Streams
• Infinite List– Consecutive numbers– Primes– Digits of pi– Random numbers
Streams
• stream = (cons <value> <func:→ stream>)
• Very similar to backtracking returns
• (car x) = (car x)
• (cdr x) = ((cdr x))
Recommended