Chapter 3 Flashcards
Constants
Must start with a lower letter which can be followed by letters, underscores or digits.
It can also be a quoted string.
Quoted string
Any string of characters (other than single quote) enclosed within single quotes
Are these allowed constants in Prolog?
- George
- granny_sarah
- ‘Hello world’
- ’ _ ‘
- No
- Yes
- Yes
- Yes
Variables
Can start with uppercase and can be followed by any number of letters, underscores and digits
Atomic sentences (or atoms)
they have the following form:
predicate(argument1, …., argumentN)
where predicate = Prolog constant and
arguments = Prolog constants/variables
We always need parenthesis after the predicate unless it has 0 arguments
Think of two examples of correct Prolog atomic sentences in the KB.
- child(john, sue)
2. my_car(red, ‘bmw’)
Conditional sentences (clauses)
structure –> head :- body1, … , bodyN
head –> Prolog atom (then..)
body –> Prolog atom (if…)
Think of an example of a Prolog conditional sentence
sister(X, Y) :- female(X), person(Y), sibling(X, Y)
What would be a case where an atomic sentence is just a special case of a conditional sentence?
A case where a conditional sentence has an empty body (“if” part)
Example:
sister(X, Y) :- nothing –> in this case, sister(X, Y) is just a special case of a conditional sentence.
A Prolog program is a sequence of …
clauses
Prolog clauses can be …
atomic/conditional sentences
Where can spaces, newlines and comments be inserted in a Prolog program?
- at the end of a program or
2. just before a constant or a variable
Comments can be added followed by the character…
%
What is the goal of Prolog by answering a query?
To establish whether there is a relationship between the query and the KB
Query
Atom with/without variables terminated by a period.
What are the 3 possible outputs when posing a query WITHOUT variables?
Yes - the atom is established by back-chaining
No - the atom cannot be established by back-chaining
Nothing - Prolog is still looking for a relationship
What are the 3 possible outputs when posing a query WITH variables?
- Prolog displays the values of the variables for which the query can be established.
- No - the aim cannot be established for any values of the variable
- No answer, still looking
If the posed query contains variables and Prolog displays the values of the variables for which the query can be established, the user has two options:
- press space/return, which will output “Yes” and complete the query answering
- Type semicolon, which will prompt Prolog to look for further variables outcomes
Conjunctive queries
sequences of atoms separated by commas (which play the role of “and”) and terminated by a period. All atoms need to be established in order to return “Yes” by Prolog.
When are conjunctive queries particularly helpful?
When a variable appears in more than one atom in the query
What is this symbol used for in Prolog? “+”
Negation in queries
If a line in Prolog starts with “Call”, what does it mean?
Prolog starts to work on an atomic sentence
If a line in Prolog starts with “Fail”, what does it mean?
An atomic query has failed and Prolog needs to look for alternatives
If a line in Prolog starts with “Exit”, what does it mean?
An atomic query has tentatively succeeded, pending the remaining conjunctive query
If a line in Prolog starts with “Redo”, what does it mean?
Prolog has gone back to a choice point to reconsider an atomic query
When dealing with a negated query, will Prolog consider the negation during the back-chaining or will it drop it?
It will drop it and attempt to look for a matching atomic sentence: if it does, the it will return SUCCEED and the negated query will fail. Go back to the most recent choice point and reconsider.
Uninstantiated variable
It does not have a tentative value yet
Instantiated variable
Has a tentative value
What should we make sure to do when variables appear in a negated query?
We should make sure that they are already instantiated at an earlier stage of the back-tracking. Practically, use another atom in the query BEFORE the negated part.
When does an equality hold in Prolog?
When the two terms (constants or variables) can be made equal by instantiating any variables
Is an equality or inequality more useful in Prolog?
Inequality
How can we interpret this program?
male(X), male(Y), male(Z) , + X = Y, + X = Z, + Y = Z
A program that defines a new predicate “male” that holds when three people X, Y and Z are different people and are all males.
Unification
Clauses in a program are selected during back-chaining via a matching process called unification.
Two atoms whose variables are distinct are said to “unify” when…
there is a substitution of values for the variables that makes the atoms identical.
Will these atoms unify?
- p(b, X, b) and p(Y, a, b) for X = a, Y = b
- p(X, b, X) and p(a, b, Y) for X = a, Y = a
- p(X, b, X, a) and p(Y, Z, Z, Y)
yes
yes
no ( the 2 occurrences of X force Y and Z to be equal, but they must also match a and b respectively)
Why is it important to rename a variable in a query with a name that is not in the KB?
Because we might lose some unifications that would be possible if the name of the variables were different.