upper-case letters: A B ... Z
lower-case letters: a b ... z
digits: 0 1 2 3 4 5 6 7 8 9
symbols: + - * / \ ^ < > = ` " : . ? @ # $ &
(1). Sequences of letters and digits. They must begin with a lower-case letter. Underscore might be used to improve the readability. For example:
michael
cs230
aBC
george_washington
(2). Sequences of symbols. For example:
:-
?-
=
<==>
<--
::=
-&-
(3). Arbitrary sequences of characters enclosed in a pair of single quotes. For example:
'Michael'
'George Washington'
'X + Y = Z'
0
1996
-100
3.14
-100.01
Who
X
Job
Street_No
_33
A variable is instantiated if there is a value assigned to it, otherwise it is uninstantiated.
In some cases, we might not need to use a name for a variable. We can use the so-called anonymous variable, which is written as a single underscore character. When Prolog answers questions, the value of an anonymous variable is not output. Here is an example.
| ?- [example2].
{consulting /u2/zhiwei/language/prolog/example2...}
{/u2/zhiwei/language/prolog/example2 consulted, 50 msec 2592 bytes}
yes
| ?- child(Who, david).
Who = bob ? ;[return]
Who = jason ? ;[return]
no
| ?- child(_, david).
yes
The first query contains a variable 'Who'. To answer this query, Prolog looks at the consulted file 'example2', finds out a matching value 'bob', instantiates the variable 'Who' with the value, and output it. If we want to find out more values, we type a semicolon. Prolog will uninstantiate 'Who' from the current value 'bob', looks at the rest of the file, finds out another matching value 'jason', and output the new value. This process can be continued until there is no matching value. At that time Prolog output a 'no'.
However, if we only want to find out if David has any child but don't care who they are, we use anonymous variable as we did in the second query. To answer this query, Prolog confirms the fact with a 'yes' without telling who the children are.
name(Given_name, Family_name)
date(Day, Month, Year)
dinner(Appetizer, Soup, Main_dish, Dessert)
To denote a structure we have to choose a functor. In the preceding examples, 'name', 'date' , and 'dinner' are functors. Components enclosed in brackets are called arguments. If more than one arguments are presented, they are separated by commas. The number of arguments is called the arity of the functor. The above structures represent unspecified items from a set of objects described by the functor. We can replace variables with constants to represent particular objects, for example:
name(Richard, Wang)
date(30, January, 1978)
dinner(appetizer, beef_soup, main_dish, cake)
The components themselves can, in turn, be structures. For example, the main dish may consist of meat and vegetables. In a particular case we might have the structure:
main_dish(roast_beef, broccoli)
In this way, the 'dinner' structure will become:
dinner(appetizer, beef_soup, main_dish(roast_beef,broccoli),cake)
[mon, tue, wen, thu, fri, sat, sun]
is a list of the days of a week.
[1, 3, 5, 7, 9]
is a list of integers.
[a]
is a list containing only one element.
[]
is a list with no elements and is called an empty list .
Elements in a list can be any Prolog terms. For example:
[date(30, January, 1978),
date(23, August, 1945),
date(12, March, 1976)]
is a list of structures.
Elements in a list can also be lists:
[[a,b],[a],[b],[]]
The first element of a list is called the head. The rest of the list is called the tail. Here are some examples:
list head tail [a,b,c] a [b,c] [a,b] a [b] [[a],b] [a] [b] [[a,b],[a],[b],[]] [a,b] [[a],[b],[]] [a] a []
A vertical bar '|' can be used to separate the head from the tail. For example, the same list [a,b,c] can be written in the following ways:[a,b,c]
To check that they are identical, you can use the identical equality sign '==':
| ?- [a,b,c] == [a|[b,c]].
yes
| ?- [a,b,c] == [a,b|[c]].
yes
| ?- [a,b,c|[]] == [a,b,c].
yes
| ?-
More about the different equalities will be shown in 7.17.3.7.
The operator '=..' can be used for constructing structures or instantiating lists. The syntax is:
X =.. L
which means: "L is the list consisting of the functor of X followed by the arguments of X". Here are some examples:
| ?- X =.. [parent, mary, tom].
X = parent(mary, tom)
| ?- main_dish(roast_beef, broccoli) =.. L.
L = [main_dish, roast_beef, broccoli]
A special case of lists is strings of characters. If a string of characters is enclosed in double quotes, the string is represented as a list of ASCII codes. For example, the string:
"system"
is written by Prolog as:
[115,121,115,116,101,109].
+ addition - subtraction * multiplication / division The result is the whole number part of a division mod remainder The result is the remainder of a division The built-in predicate 'is' is used to evaluate an expression. For example, if we type:| ?- X is 1 + 2.
The system will answer:
X = 3
>, <, >=, =<, =:=, =\=
Their meanings are listed below:
X > Y X is greater than Y X < Y X is less than Y X >= Y X is greater than or equal to Y X =< Y X is less than or equal to Y X =:= Y the values of X and Y are equal X =\= Y the values of X and Y are not equal
7.17.3.6.3. 'OP' DIRECTIVENew operators can be defined by inserting directives into the program. The general form for a directive is:
:- op(precedence, type, name).
The operator definition must appear before any expression using that operator. For example, the operator love can be defined as below:
:- op(600, xfx, love).
This clause tells Prolog system that we want to define an operator, the precedence of which is 600, the type is 'xfx', and the name is 'love'.
The form 'xfx' is a type specifier which suggests that the operator is between two arguments. In a type specifier, we use 'f' to denote an operator, 'x' and 'y' to denote arguments. 'x' represents an argument whose precedence is strictly lower than that of the operator, 'y' represents an argument whose precedence is lower than or equal to that of the operator. . There are three groups of operator types:
(1). infix operators:
xfx xfy yfx
(2). prefix operators:
fx fy
(3), postfix operators:
xf yf
Note that operations do not define any action, they only introduce new notation.
=, ==, \==, =:=, =\=
Their meanings are listed below:
/* This is a bracketed comment */
/* A bracketed comment
can cross lines */
male(larry).
male(david).
% This is an end-of-line comment appearing on a line
child(david,larry).
father(X,Y) :- % An end-of-line comment
child(Y,X),
male(X).