Prolog Tutorial part I
1.Relations
Prolog programs specify relationships among objects and properties of objects.
When we say, “John owns the book”, we are declaring the ownership between two objects: John and book
When we say,”Does John owns the book?”we are trying to find out about a relationship.
Relationships can also rules such as:
two people are sisiters if
they are both female and
they have the same parents
A rule allows us to find out about a relationship even if the relationship isn’t explicitly stated as fact.
2.A little more on being sisters
As usual in programming, you need to be a bit careful how you pharse things:
A and B are sisters if
A and B are both female and
they have the same father and
they have the same mother and
A is not the same as B
3.programming in prolog
declare facts decsribing explicit relationships between objects and properties might have( e.g. Mike likes hooker, Janne has_colour green, Janne_girlfriend works_as hooker )
define rules defining implicit relationships between objects( e.g. the sister rule above) and/or rules defining implicit object properties( e.g. X is a parent if there is a Y such that Y is a child of X)
asking questions above relationships between objects, and/or about object properties( e.g. Does John likes hooker?)
4.Facts
Properties of objects, or relationships between objects:
“Dr. Turning lectures in course 9020”, is written in Prolog as:
lectures(turning, 9020).
Notice that:
names of properties/relationships begin with lower case letters.
the relationship name appears as the first term
objects appear as comma-seprated arguments within parentheses
A period “.” must end a fact
objects also begin with lower case letters. They also can begin with digits( e.g. 9020), and can be strings of characters enclosed in quotes( as in reads(fred, “War and Peace").)
lectures(turning, 9020). is also called a predicate(谓词)
e.g of Facts.
%year(x, y): person X is in year Y.
year(freshman, 1).
5.Queries
Once we have a database of facts( and, soon, rules) we can ask questions about the stored infromation
Suppose we want to know if Turning lectures in course 9020. We can ask:
?- a supposed fact
return boolean value true or false
Notice that:
In SWI Prolog, queries are terminated by full stop.
To answer this query, Prolog consults its databse to see if it’s a known fact.
In example dialogue with Prolog, the text in green italics is what users inputs.
Variables
Supposed we want to ask,”What course does Truning teach?”
This could be written as:
Is there a course, X, that Turning teaches?
The variable X stands for an object that the questioner does not know yet.
To answer the question, Prolog has to find out the value of X, if it exists.
As long as we do not know the value of variable it is said to be unbound.
When a value is found, the variable is said to be bound to that value.
The name of a variable must begin with a capitalised character or and underscore character “_”
Variables2
If prolog can tell that there are no more solutions, it just gives you the ?- prompt for a new query, as here, it will let you type ; again, and then if no further solution, report false.
Prolog can find all possible ways to answer the query, unless you explicitly tell it not to( see cut, later)
Conjunction of goals in queries
How do we ask,”Does Turning teach Fred?”
This is means finding out if Turning lectures in a course that Fred studies.
?- lecture(turning, Course), studies(fred, Course)
i.e. “Turning lectures in course, Course and Fred studies (the same) Course”.
This question consist of two goals
To answer this question, Prolog must find a single value for Course, that satisfies both goals.
Read the comma “,” as and.
However, note that Prolog will evaluate the two goals left-to-right. In pure logic, P1^P2 is the same as P2^P1. In Prolog, there is the practical consideration of which goal should be evaluated first —the code might be more efficient one way or the other. In particular, in “P1, P2”, if P1 fails, then P2 does not need to be evaluated at all.This is sometimes referenced as “conditional-and”
Disjunction of Goals in Prolog
What about or (i.e. disjunction)? It turns out explicit ors aren’t needed much in Prolog.
There is a way to write “or” : (“;”)
The reason ors aren’t needed much is that:
head :- body1.
head :- body2.
has the same effect as:
head :- body1 ; body2.
Tips:
1.Avoid using ”;” if you can, at least util you have learned how to manage without it. While some uses of ; as harmless, others can make your code hard to follow.
2.To reinforce this message, you will be penalised if you use the or operator in assignment of comop9414.
This prohibition mean you can’t sue the … -> … ; … construct either, in the first assignment. The … -> … ; … construct is not taught in comop9414, but in the past, some people have found out about it.
Backtracking in Prolog
Who does the Codd teach?
Prolog solve these problems by processing from left-to-right and then backtracking.
When given the initial query, Prolog starts by trying to solve
lecture(codd, Course)
There are six lectures clause that refers to clauses, but only two have Codd as their first argument.
Prolog uses the first clause that refers to codd; lectures (codd, 9311).
With Course = 9311, it tries to satisfy the next goal, studies(Student, 9311).
It finds the fact studies(Course, 9311). and hence the first solution: (Course = 9311, Student = jack)
Backtracking in Prolog 2
After the first solution is found, Prolog retraces its steps up the tree and looks for alternatives solutions.
First it looks for other students studying 9311 (but find it none).
Then it:
1.back up
2.rebinds Course to 9314.
3.goes down the lectures(codd, 9314) branch
4.tries studies(Student, 9314)
5.finds the other two solutions:
(Course = 9314, Student = jill)
and
(Course = 9314, Student = henry)
The backtracking path is just like standard DFS(depth first search)
Rules
The previous question can be restated as general rule:
One person, Teacher, teaches another person, Student if
Teacher lectures in a course, Course and
Student studies Course.
In Prolog this is written as:
teaches(Teacher, Student) :-
lectures(Teacher, Course),
studies(Student, Course).
?- teaches(codd, Student).
Facts are unit and rules are non-unit clauses.
Clause Syntax
“:-“ means “if” or “is implied by”. Also called the neck symbol.
The left hand side of the neck is called the head
The right hand side of the neck is called the body
The comma “,” separating the goals, stands for and.
Another rule, using the predefined predicate “>”.
more_advanced(s1, s2) :-
year(S1, Year1),
year(S2, Year2),
Year1 > Year2.
next item: Tracing Execution
Summary:
1.在Prolog里,左右关系有有意义的,大多逻辑的执行顺序都是从左到右(这一点在C,python等一些其他编程语言中也有体现, and/or)
2.符号对应逻辑关系:
comma 逗号 “,” 对应 and
semicolon 分号 “;” 对应 or(注意,尽量不要用; 因为这样的表达会让表达式变得可读性差)
3.变量大写字母开头,常量小写字母开头
这一点要尤其注意,因为:1.这样的命名规范和大多数语言是相反的2.这不仅是命名规范,而会直接影响到编译器的处理方式