Download - Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming
![Page 1: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/1.jpg)
Logic Programming
CSC 358/458
5.13.2003
![Page 2: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/2.jpg)
Outline
Pattern matching Unification Logic programming
![Page 3: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/3.jpg)
Pattern matching
Slade's version(A *WILD* B) matches (A B C B)return (B C)
More general(A ?x B ?y B) matches (A B B C B)?X => B?Y => C(A ?x B ?x B) matches (A B B B B)
![Page 4: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/4.jpg)
Bindings
Pattern matcher maintains a binding list ((?X . B) (?Y . C))
Once bound a pattern variable never changes in value
The rest of the target must be consistent with the bindings
Matching against a variable extends bindings, or confirms consistency, or fails
![Page 5: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/5.jpg)
Norvig code
Pattern matchervariables for single values not
segmentsdifferent syntax for extended patternspatmatch.lsp
![Page 6: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/6.jpg)
Unification
Variables on both sides(foo ?a x) matches (foo x ?a)
There is a binding that unifies the expressions?a = x
![Page 7: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/7.jpg)
Unify function
Tree-basedmatches both car and cdr of pattern
Attempts to unify variablesregardless of where they appear
![Page 8: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/8.jpg)
Issues
Must allow variables bound to variables ?X -> ?Y
Must not have circular evaluation ?X -> ?Y ?Y -> ?X
Must not have recursive bindings ?X -> (F ?X) "occurs in" check
![Page 9: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/9.jpg)
Examples
![Page 10: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/10.jpg)
Prolog
Assertionsstatements
Rulesimplications
Queriesstatements of unknown truth
![Page 11: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/11.jpg)
Example
(<- (mortal ?x) (human ?x)) (<- (human socrates)) (?- (mortal socrates)) or (? (mortal ?y))
![Page 12: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/12.jpg)
Brief detour: macros
What are<-?-
Obviously not a functionarguments are not evaluated
![Page 13: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/13.jpg)
Macro
(defmacro <- (&rest clause)
"add a clause to the data base."
`(add-clause ',(replace-?-vars clause)))
Arguments are not evaluated Can be used to build code to be
evaluated
![Page 14: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/14.jpg)
Macro expansion
A macro is an arbitrary piece of Lisp coderesult is an expression to be evaluated
Make your own syntax Examples
expand <-expand OR
More about macros next week
![Page 15: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/15.jpg)
Logic Review
Predicates has-professor(csc358, burke)
Variables if has-professor(course, professor) and subject-of
(course, topic) then knows(professor, topic)
Quantification for all x > 0, there exists s such that s * s = x
Implication Equivalence
daughter (A, B) iff mother (B, A) or father (B, A) and female (A)
![Page 16: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/16.jpg)
Terminology
Left-hand sideIF part
Right-hand sideTHEN part
Clausea statement headed by a predicate
Assertiona true statement
![Page 17: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/17.jpg)
Prolog
Handles only implication no equivalence
Handles only a single implicated clause No A => B and C
Handles only AND clauses no OR no NOT
Handles only universally-quantified variables no "there exists"
Called "Horn clauses"
![Page 18: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/18.jpg)
Backward chaining
If we want to prove something is truewe want to prove that is follows from
what we already know Is socrates mortal?
follows from rules and assertions We follow the rules backwards
![Page 19: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/19.jpg)
Algorithm
Start with item to proveif matching assertion exists, bind
variables and returnif not, select rules that could prove ittry to prove each of these
![Page 20: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/20.jpg)
Proof Tree
Root S = statement to prove S is the consequent of some rule The rule may have several
precendent partseach of these can be considered
something to prove
![Page 21: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/21.jpg)
Search problem
Horn clause theorem proving is effectively search we are searching the tree of rules and
assertions for something that will prove what we want to
know Prolog does this depth-first
order of rule definition Interestingly
Horn clause theorem proving is Turing-complete
![Page 22: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/22.jpg)
Backtracking
Involves making choices testing them when they fail, re-doing the choice differently
Must keep track of what choices have been tried what to do differently where the last choice was
![Page 23: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/23.jpg)
In Prolog
To prove Qwe may have several rules with Q is a
consequentif one rule fails to resolve, we try the
next Recursive formulation
since resolve a rule requires more clauses to satisfy
etc.
![Page 24: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/24.jpg)
Complexity
Large rule base = more stuff to search Crucial "fan out" factor
how many ways to prove something
![Page 25: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/25.jpg)
Programming with Prolog
Many basic Lisp functions can be definedmemberlength
Relational rather than functional
![Page 26: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/26.jpg)
Knowledge Representation
Prolog requires that we be declarative We have to have a language for
representing our problem Prolog doesn't help you write a good
language Good language = difference between
computable and not We have to have rules that will answer our
queries Easy to write bad rules
![Page 27: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/27.jpg)
How to build a language
Select primitivesthe most basic facts to be representedfrom which other useful things can be
inferred Select more complex predicates
more like the queries you want to answer
Write rules that relate complex to simple
![Page 28: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/28.jpg)
Exercise
KnowingProf. Burke teaches CSC 358 CSC 358 is a CTI courseProfessors teach courses associated
with their home department Prove
Prof. Burke has CTI as his home department
![Page 29: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/29.jpg)
Example
I married a widow who has a grown-up daughter. My father, who visited us often, fell in love with my step-daughter and married her. Hence my father became my son-in-law and my step-daughter became my mother. Some months later, my wife gave birth to a son, who became the brother-in-law of my father, as well as my uncle. The wife of father, that is, my step-daughter also had a son. I'm my own grandpa.
![Page 30: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/30.jpg)
Frustrations
Numbers pure Prolog doesn't have math operations no relational operators like >.
Control pure Prolog only has one programmer-
defined control over evaluation: order of definition
badly written rules can create infinite loops Evaluation
pure Prolog has no notion of value beyond variable binding
![Page 31: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/31.jpg)
Hard to derive primitives(abduction)
Knowing that Beatrice is someone's auntcan't infer that she is femaleeven though this information is in the
rules, sort of
![Page 32: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/32.jpg)
Only backward chaining
Sometime it is desirable to make inference in both directions
ExampleMammals
![Page 33: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/33.jpg)
Real Prolog
Is a modern programming language Has mechanisms for math operations Has data structures Has some control structures
Cut (max example)
![Page 34: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/34.jpg)
Constraint Logic Programming Next-generation logic programming
paradigmallows the assertion of constraints a
solution must satisfy Example
Sudoku puzzle• 1..9 can only appear once in each row
very cumbersome in Prologsimple as a constraint
![Page 35: Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming](https://reader036.vdocuments.net/reader036/viewer/2022070411/56649f3a5503460f94c57527/html5/thumbnails/35.jpg)
Homework #5
1. Solve a logic puzzle
2. Reason about cards