Logic Programming Week 1 Summary
Chapter 1
A collection of facts and rules is called a knowledge base (or a database)
Posing queries - asking questions about the information stored in the knowledge
base
Facts are unconditionally true
?- prompt symbol
listensToMusic(mia). FACT
happy(yolanda). FACT
playsAirGuitar(mia) :- listensToMusic(mia). RULE
playsAirGuitar(yolanda) :- listensToMusic(yolanda). RULE
Rules state information that is conditionally true
:- should be read as “if” or “is implied by”
Left side is called head of the rule, right side is called body of the rule
If the body of the rule is true, then the head of the rule is also true
Modus ponens - if a knowledge base contains a rule head :- body, and Prolog
knows that body follows from the information in the knowledge base, then Prolog
can infer head
Prolog is able to retrieve information that logically follows from the rules and facts
recorded in the knowledge base
Clauses - the facts and rules contained in a knowledge base (all the dots)
Predicates - the concepts we find important (listensToMusic, happy,
playsAirGuitar)
We can view a fact as a rule with an empty body
playsAirGuitar(vincent):-
listensToMusic(vincent),happy(vincent).
The comma means and (Vincent plays air guitar if he listens to music and he is
happy), both need to be fulfilled to get a “yes”
, Logical disjunction (or) can be written separately or together with a semicolon ;
but it is not recommended because the code becomes hard to read
playsAirGuitar(butch):- happy(butch); listensToMusic(butch).
Any word beginning with an upper-case letter is a Prolog variable (X, Y, Z)
Prolog answers a query by working its way from top to bottom
loves(marcellus,X),woman(X). (Is there any individual X such that
Marcellus loves X and X is a woman?)
jealous(X,Y) :- loves(X,Z),loves(Y,Z). (Conditional
statement, an individual X will be jealous of an individual Y if there is some
individual Z that X loves, and Y loves that same individual Z too)
There are four kinds of terms in Prolog: atoms, numbers, variables, and complex
terms (or structures)
Atoms and numbers are lumped together under the heading constants, and
constants and variables together make up the simple terms of Prolog
An atom is either:
- A string of characters made up of upper-case letters, lower-case letters,
digits, and the underscore character, that begins with a lower-case letter
(butch, big_kahuna_burger, and m_monroe2)
- An arbitrary sequence of characters enclosed in single quotes (’Vincent’,
’The Gimp’, ’Five_Dollar_Shake’, ’&^%&#@$ &*’, and ’ ’), the character
between the single quotes is called the atom name
- A string of special characters (@= and ====> and ; and :-)
Numbers (integers, floats)
A variable is a string of upper-case letters, lower-case letters, digits and
underscore characters that starts either with an upper-case letter or with underscore
(X, Y, Variable, _tag, X_526, List, List24, _head, Tail, _input, Output)
Complex terms are built out of a functor followed by a sequence of arguments.
The arguments are put in ordinary brackets, separated by commas, and placed after
the functor. The functor must be an atom. That is, variables cannot be used as
functors. On the other hand, arguments can be any kind of term
playsAirGuitar(jody) is a complex term, “playsAirGuitar” is a functor, “jody” is an
argument
, In Prolog we can nest complex terms: hide(X,father(father(father(butch))))
Arity is the number of arguments that a complex term has
woman(mia) is a complex term with arity 1, while loves(vincent,mia) is a complex
term with arity 2
Woman /1 loves/2 (how you write the predicates), loves/2 and loves/3 are
considered different by Prolog
?- listing. (display the contents of the current knowledge base)
?- [kb2]. (load the contents of file “kb2.pl”)
?- listing(playsAirGuitar). (lists information about a specific predicate)
Chapter 2
Matching/Unification - Prolog matches woman(X) with woman(mia), thereby
instantiating the variable X to mia
Three types of terms:
- Constants (atoms or numbers)
- Variables
- Complex terms
Two terms match, if they are equal or if they contain variables that can be
instantiated in such a way that the resulting terms are equal (mia - mia, 42 - 42, X -
X match as they are all the same) (X - mia would match)
=(mia,mia). Testing if they match, YES
=(mia,vincent). Testing if they match, NO
mia = mia. Nicer way to write this testing
’mia’ = mia. YES
’2’ = 2. NO (2 is a number, ‘2’ is an atom!)
X = Y. YES (variables unify with anything, so with each other too)
kill(shoot(gun),Y) = kill(X,stab(knife)). YES (they use the same atom as the
functor name and have the same number of arguments)
Prolog is optimistic and assumes that you are not going to give it anything
dangerous, so it does not make an occurs check and as soon as you give it two
terms, it charges full steam ahead and tries to match them (that makes it work
faster as a programming language)