Download presentation
Presentation is loading. Please wait.
Published byJunior Nelson Modified over 9 years ago
1
Introduction to Prolog Facts, Questions & Rules Atoms & Variables
2
LISP/Prolog Data n LISP/Prolog developed for AI –LISP in late 1950s –Prolog in mid-late 1970s n Artificial Intelligence = intelligent artifact n Ability to perceive, reason and act –LISP/Prolog concerned mostly with reasoning –Symbolic manipulation of (model of) world
3
Symbolic Programming n Most languages work with numbers & text –Also more complicated combinations of same n Prolog/LISP work with symbols –Also work with numbers & text –Not as suitable for numeric/textual programs n Symbols represent things in the world –Language makes it easy to use them
4
Prolog n PROgrammation en LOGique –PROgramming in LOGic n Logic model –Program = axioms –Execution gives theorems n Restricted logic – can’t say as much
5
Logic Model n Proof (vs. Command) n Process –predicate specificationassertions –predicate applicationquestions n Data –mathematical objectsatoms, lists & terms
6
Prolog Facts n Simply state what’s true –Need to decide on symbols to state axioms n Family facts parent(mark, alex).% Mark is Alex’s parent parent(di, alex).% Di is Alex’s parent parent(bob, mark).% Bob is Mark’s parent % sign starts a comment Comment continues to end of line
7
Prolog Atoms n mark, di, alex & bob are not variables n 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) n Named atoms start with lower-case letter –may contain letters (UPPER and lower case), numbers & underscores
8
Syntax of Prolog Facts n Consist of functor…parent –AKA name of the predicate n …and argumentsmark, alex n Arguments in (parentheses)… –Right up against the functor n …separated, by, commas n Ends with a period. –Can be split over multiple lines, if you like
9
Syntax of Prolog Facts parent( mark, alex ). parent( mark, alex ).% functor parent( mark, alex ).% parentheses parent( mark, alex ).% 1 st argument parent( mark, alex ).% comma parent( mark, alex ).% 2 nd argument parent( mark, alex ).% period
10
Representations n 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 n Arity of parenthood relationship is 2 –The predicate is parent/2 –May be other predicates with same name
11
% parents(Child, Father, Mother) parents(mark, bob, isabel). % Mark’s dad & mom are Bob And Isabel % parent(Parent, Child) parent(mark, alex).% Mark is Alex’s parent Multiple Representations n Choose based on how info to be used % father(Child, Parent) father(alex, mark).% Alex’s father is Mark
12
Exercise n Write a set of facts about your family using the second representation above. –your parents’ family if you don’t have one n My family: parents(mark, bob, isabel). parents(di, ralph, esther). parents(alex, mark, di). parents(zachary, mark, di).
13
Program File n A program may consist of just facts –family_1.pl n 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
14
Some Predicates to Start With ?- help.% general help ?- help(help).% help on … ?- apropos(help).% help available... ?- consult(‘family_1.pl’).% load a file ?- [‘family_1.pl’].% ditto ?- edit(‘family_1.pl’).% edit/reload file ?- [user].% enter facts interactively ?- halt.% exit Prolog
15
Asking Yes/No Questions n Can ask about parentage n ?- 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
16
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).
17
Asking Who Questions n Can also ask who is parent of whom ?- parent(Who, mark). Who = bob yes n Prolog pauses after saying who Who is –Press Enter or space to signal you’re ready –(systems differ in what keys to press)
18
Asking Whom Questions n Can also ask who is parent of whom ?- parent(bob, Whom). Whom = mark yes ?- parent(Who, Whom). Who = mark Whom = alex yes
19
Variables n Words that start with Capital Letters are variables –More accurately “unknowns” n 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
20
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).
21
Variables & No n “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 n “no” means “don’t know of any” ?- parent(Who, isabel). no
22
Variable Names n Prolog doesn’t care about the names ?- parent(bob, X). X = mark yes ?- parent(bob, Child). Child = mark yes ?- parent(alex, Anyone). no use meaningful variable names X usually not good
23
Multiple Answers n Sometimes more than one correct answer ?- parent(Who, alex). Who = mark ; Who = di ; no n Type a semi-colon to get next answer –n and r also work –(different systems use different characters) Semi-colons here mean – “are there more answers?” This “no” means there were no more answers
24
Asking Anybody Questions n Start the variable with an underscore –Prolog won’t tell you its value ?- parent(mark, _Anybody). 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? Is mark anyone’s parent (don’t care who, just whether)
25
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).
26
Compound Questions n Separate terms with commas –Still just one question –All things must be true, so comma = AND ?- parent(GP, P), parent(P, C). GP = bob P = mark C = alex yes Important Note: -- P appears twice in the question -- only gets one value -- same value in both places
27
Single Assignment n Each variable can only have one value in one answer ?- parent(GP, P), parent(P, C). n Find GP, P and C such that –GP is a parent of P, and –P is a parent of C n Variable can only have different values in different answers
28
Exercise n 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).
29
Rules n That compound question is one we might want to ask often –Who is the grandparent of whom n 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
30
Using Rules n Just ask the question ?- grandparent(ralph, alex). yes ?- grandparent(GP, C). GP = bob C = alex yes n Note – parent not mentioned in the answer
31
Understanding Rules n Consists of head and body –Head and body separated by colon-hyphen n Head is just like a fact –Tho’ it usually has variables as arguments n Body is like a question –It’s usually compound (separated by commas) n Variables connect arguments together
32
Rules in More Detail grandparent(GP, C) :- parent(GP, P), parent(P, C). n Note the way it’s written –Indentation –Punctuation n 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
33
Rule Diagrams n May help you see n Define grandparent/2 predicate –GP = grandparent –C = child n True if there’s a P “between” them –GP parent of P –P parent of C C GP P parent grandparent
34
Clauses n Facts and rules are called clauses n 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 n Looking at a predicate: ?- listing(PredicateName). ?- listing(PredicateName/Arity).
35
Multi-Clause Rules n Two ways to be an uncle uncle(Uncle, NieceOrNephew) :- parent(Parent, NieceOrNephew), brother(Uncle, Parent). uncle(Uncle, NieceOrNephew) :- parent(Parent, NieceOrNephew), sister(Aunt, Parent), husband(Uncle, Aunt). Still need to define brother/2, sister/2 & husband/2.
36
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
37
Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad, cathy). n Who are Alex’s aunts? n Write a definition of aunt/2 –aunt(Aunt, NorN).
38
Recursion n Predicates may be recursive –Needs a base case – fact or non-recursive rule ancestor(Anc, Desc) :- parent(Anc, Desc). ancestor(Anc, Desc) :- parent(Parent, Desc), ancestor(Anc, Parent). n Usually write base case first – more later Base case: your parent is your ancestor Recursive case: an ancestor of your parent is your ancestor
39
Rule Diagrams n Each diagram a clause n Note recursion –Needs base case D A parent ancestor D A P parent ancestor
40
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
41
Next Time n Terms & Proof Procedures n Bratko, Chapter 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.