Facts, Questions & Rules

advertisement
Introduction to Prolog
Facts, Questions & Rules
Atoms & Variables
LISP/Prolog Data

LISP/Prolog developed for AI
– LISP in late 1950s
– Prolog in mid-late 1970s
Artificial Intelligence = intelligent artifact
 Ability to perceive, reason and act

– LISP/Prolog concerned mostly with reasoning
– Symbolic manipulation of (model of) world
Symbolic Programming

Most languages work with numbers & text
– Also more complicated combinations of same

Prolog/LISP work with symbols
– Also work with numbers & text
– Not as suitable for numeric/textual programs

Symbols represent things in the world
– Language makes it easy to use them
Prolog

PROgrammation en LOGique
– PROgramming in LOGic

Logic model
– Program = axioms
– Execution gives theorems

Restricted logic – can’t say as much
Logic Model
Proof (vs. Command)
 Process

– predicate specification
– predicate application

assertions
questions
Data
– mathematical objects
atoms, lists & terms
Prolog Facts

Simply state what’s true
– Need to decide on symbols to state axioms

Family facts
parent(mark, alex).
parent(di, alex).
parent(bob, mark).
% Mark is Alex’s parent
% Di is Alex’s parent
% Bob is Mark’s parent
% sign starts a comment
Comment continues to end of line
Prolog Atoms
mark, di, alex & bob are not variables
 Atoms are themselves and nothing else

– an atom does not have a value
– only one atom with any given name (no local
scope, only global)

Named atoms start with lower-case letter
– may contain letters (UPPER and lower case),
numbers & underscores
Syntax of Prolog Facts

Consist of functor…
parent
– AKA name of the predicate
…and arguments
 Arguments in (parentheses)…

mark, alex
– Right up against the functor
…separated, by, commas
 Ends with a period.

– Can be split over multiple lines, if you like
Syntax of Prolog Facts
parent( mark, alex ).
parent( mark, alex ).
parent( mark, alex ).
parent( mark, alex ).
parent( mark, alex ).
parent( mark, alex ).
parent( mark, alex ).
% functor
% parentheses
% 1st argument
% comma
% 2nd argument
% period
Representations

Parenthood a relationship with two people
–
–
–
–

parent represents the relationship
mark and alex represent the people
Position distinguishes parent from child
Note: all start with lowercase letters
Arity of parenthood relationship is 2
– The predicate is parent/2
– May be other predicates with same name
Multiple Representations

Choose based on how info to be used
% parent(Parent, Child)
parent(mark, alex).
% Mark is Alex’s parent
% parents(Child, Father, Mother)
parents(mark, bob, isabel).
% Mark’s dad & mom are Bob And Isabel
% father(Child, Parent)
father(alex, mark).
% Alex’s father is Mark
Exercise

Write a set of facts about your family using
the second representation above.
– your parents’ family if you don’t have one

My family:
parents(mark, bob, isabel).
parents(di, ralph, esther).
parents(alex, mark, di).
parents(zachary, mark, di).
Program File

A program may consist of just facts
– family_1.pl

Want to start Prolog, assert the facts, and
then start asking questions
– We want to consult the file
– Windows – double-click file to start Prolog and
file is consulted automatically
– Linux – need to use consult/1 predicate
Some Predicates to Start With
?- help.
?- help(help).
?- apropos(help).
?- consult(‘family_1.pl’).
?- [‘family_1.pl’].
?- edit(‘family_1.pl’).
?- [user].
?- halt.
% general help
% help on …
% help available ...
% load a file
% ditto
% edit/reload file
% enter facts interactively
% exit Prolog
Asking Yes/No Questions
Can ask about parentage
 ?- is Prolog prompts for a question

– type in (what looks like) a fact
– Prolog will tell you if it’s in the database
?- parent(mark, alex).
yes
?- parent(bob, di).
no
Show Prolog’s Response…
parent(mark, alex).
parent(di, alex).
parent(bob, mark).
parent(isabel, mark).
parent(ralph, di).
parent(esther, di).
parent(franklin, bob).
?- parent(mark, alex).
?- parent(ralph, esther).
?- parent(alex, mark).
?- parent(ralph, di).
Asking Who Questions

Can also ask who is parent of whom
?- parent(Who, mark).
Who = bob
yes

Prolog pauses after saying who Who is
– Press Enter or space to signal you’re ready
– (systems differ in what keys to press)
Asking Whom Questions

Can also ask who is parent of whom
?- parent(bob, Whom).
Whom = mark
yes
?- parent(Who, Whom).
Who = mark
Whom = alex
yes
Variables

Words that start with Capital Letters are
variables
– More accurately “unknowns”

If you ask a question with variables, Prolog
says what values they could have to make
what you wrote true
– If there’s no such, Prolog just says no
Show Prolog’s Response…
parent(mark, alex).
parent(di, alex).
parent(bob, mark).
parent(isabel, mark).
parent(ralph, di).
parent(esther, di).
parent(franklin, bob).
?- parent(Who, bob).
?- parent(ralph, Whom).
?- parent(alex, Whom).
Variables & No

“Closed world assumption”
– if it’s not in the database, it isn’t true
– everything relevant to the question is known
?- parent(garvie, franklin).
no

“no” means “don’t know of any”
?- parent(Who, isabel).
no
Variable Names
Prolog doesn’t care about the names
?- parent(bob, X).
X = mark
yes
use meaningful variable names
?- parent(bob, Child).
X usually not good
Child = mark
yes
?- parent(alex, Anyone).
no

Multiple Answers

Sometimes more than one correct answer
Semi-colons here mean –
?- parent(Who, alex).
“are there more answers?”
Who = mark ;
Who = di ;
no
This “no” means there were no more answers

Type a semi-colon to get next answer
– n and r also work
– (different systems use different characters)
Asking Anybody Questions

Start the variable with an underscore
– Prolog won’t tell you its value
?- parent(mark, _Anybody). Is mark anyone’s parent
(don’t care who, just whether)
yes
?- parent(Parent, _Anyone).
Parent = mark ;
Parent = di
yes
Why did it say “yes” here instead of “no”?
And why didn’t it find Bob?
Show Prolog’s Response…
parent(mark, alex).
parent(di, alex).
parent(bob, mark).
parent(isabel, mark).
parent(ralph, di).
parent(esther, di).
parent(franklin, bob).
?- parent(Who, di).
(show all answers)
?- parent(_Who, alex).
?- parent(alex, _Whom).
Compound Questions

Separate terms with commas
– Still just one question
– All things must be true, so comma = AND
?- parent(GP, P), parent(P, C).
GP = bob
Important Note:
P = mark
-- P appears twice in the question
C = alex
-- only gets one value
yes
-- same value in both places
Single Assignment

Each variable can only have one value in
one answer
?- parent(GP, P), parent(P, C).

Find GP, P and C such that
– GP is a parent of P, and
– P is a parent of C

Variable can only have different values in
different answers
Exercise

Answer these questions based on the
database on pp. 1 & 2 of the text
?- parent(pam, Whom).
?- parent(tom, Whom). % give all answers
?- parent(bob, Child), parent(Child, _Any).
?- parent(Ann, jim).
% careful!
?- parent(pam, Whom), parent(Who, liz).
?- parent(tom, _Any), parent(_Any, GChild).
Rules

That compound question is one we might
want to ask often
– Who is the grandparent of whom

New predicate: grandparent/2
– First argument is grandparent of second
grandparent(GP, C) :- parent(GP, P), parent(P, C).
– This is a rule, and we can put it in the file
Using Rules

Just ask the question
?- grandparent(ralph, alex).
yes
?- grandparent(GP, C).
GP = bob
C = alex
yes

Note – parent not mentioned in the answer
Understanding Rules

Consists of head and body
– Head and body separated by colon-hyphen

Head is just like a fact
– Tho’ it usually has variables as arguments

Body is like a question
– It’s usually compound (separated by commas)

Variables connect arguments together
Rules in More Detail
grandparent(GP, C) :parent(GP, P),
parent(P, C).

Note the way it’s
written
– Indentation
– Punctuation

GP is a grandparent of C
if there is some P such
that
– GP is a parent of P, and
– P is a parent of C
Rule Diagrams

May help you see

GP
– GP = grandparent
– C = child
parent
grandparent
P
parent
C
Define grandparent/2
predicate

True if there’s a P
“between” them
– GP parent of P
– P parent of C
Clauses
Facts and rules are called clauses
 A predicate can be made up of any number
of clauses

– All facts, all rules, or some of each
– Usually kept together, but need not be

Looking at a predicate:
?- listing(PredicateName).
?- listing(PredicateName/Arity).
Multi-Clause Rules

Two ways to be an uncle
uncle(Uncle, NieceOrNephew) :parent(Parent, NieceOrNephew),
brother(Uncle, Parent).
Still need to define
uncle(Uncle, NieceOrNephew) :brother/2,
sister/2 &
parent(Parent, NieceOrNephew),
husband/2.
sister(Aunt, Parent),
husband(Uncle, Aunt).
Uncle Example
parent(mark, alex).
parent(di, alex).
brother(brian, mark).
sister(cathy, di).
wife(susan, brian).
husband(brad, cathy).
?- uncle(Who, alex).
Who = brian ;
Who = brad ;
no
Exercise
parent(mark, alex).
parent(di, alex).
brother(brian, mark).
sister(cathy, di).
wife(susan, brian).
husband(brad, cathy).
Who are Alex’s
aunts?
 Write a definition
of aunt/2

– aunt(Aunt, NorN).
Recursion

Predicates may be recursive
– Needs a base case – fact or non-recursive rule
ancestor(Anc, Desc) :Base case: your parent is
your ancestor
parent(Anc, Desc).
ancestor(Anc, Desc) :Recursive case: an ancestor of
parent(Parent, Desc),
your parent is your ancestor
ancestor(Anc, Parent).

Usually write base case first – more later
Rule Diagrams
A
parent
ancestor
D
A
ancestor
ancestor
P
parent
D
 Each
diagram a
clause
 Note recursion
– Needs base case
Alex’s Ancestors
parent(mark, alex).
parent(bob, mark).
parent(franklin, bob).
parent(garvie, franklin).
ancestor(A, D) :parent(A, D).
ancestor(A, D) :parent(P, D),
ancestor(A, P).
?- ancestor(Who, alex).
Who = mark ;
Who = bob ;
Who = franklin ;
Who = garvie ;
no
Next Time
Terms & Proof Procedures
 Bratko, Chapter 2

Download