Prolog EN -- Laboratory 2 -- 2006-2007 -- info.uvt.ro
The contents of this section is structured base on the GNU Prolog Manual.
Directives
[edit]consult
[edit]The consult directive is used to load a program from a source file.
Syntax:
consult(user). %=> Allows us to enter the source code by hand ending by pressing Control+D consult(<file-name>). %=> Tries to load the given file in the current folder. (The file must have the .pl extension.)
If in the source code appears a predicate (either a fact or a rule) that was already defined, the previous definition is discarded.
Example:
consult(user). e(1). e(2). <<Control+D>> %=> Loads the facts for the predicate e. consult(ex1). %=> Loads all the predicates (facts and rules) from the file 'ex1.pl' in the current folder.
load
[edit]The load directive is used to load a compiled file. It behaves just like consult. The source files are compiled by using gplc.
listing
[edit]The listing directive is used to list all or a selection of the loaded predicates (facts and rules).
Syntax:
listing. %=> Lists all the known predicates. listing(<predicate>). %=> Lists all the predicates with the given functor.
Example:
listing. %=> % e(1). % e(2). % f(a). % f(b). listing(f). %=> % f(a). % f(b).
halt
[edit]The halt directive is used to exit from the interpreter.
Syntax and example:
halt.
Control structures
[edit]true, fail, and !
[edit]These are built-in predicates but act more as control structures:
- true is a predicate that always succeeds.
- fail is a predicate that always fails, and thus it forces backtracking.
- ! is a predicate that always succeeds, and removes all the backtracking information up to this point.
Syntax and example:
true. %=> yes fail. %=> no !. %=> yes
, -- conjunction, ; -- disjunction, and -> -- if-then
[edit]- , represents the conjunction of two goals. It is used to chain two goals together, and forms a new goal which succeeds if both given goals succeed.
- ; represents the disjunction of two goals. It is used to create a choice-point between tho two coals, and forms a new goal that succeeds if either one succeeds.
- -> represents an if-then construct, and behaves just like ,, except that it has a particular behavior.
Syntax:
Goal1, Goal2. Goal1; Goal2.
Example:
atom(a), number(1). %=> yes atom(a), number(a). %=> no atom(a); number(a). %=> yes atom(1); number(1). %=> yes atom(1), number(1). %=> no atom(1), number(1). %=> no
abort
[edit]The abort directive (or predicate) is used to abort the current running query.
for
[edit]For is used to iterate a given variable over a range.
Syntax:
for(<iterator>, <lower-bound>, <upper-bound>).
Example:
for(X, 1, 10). %=> X = 1; X = 2; ...; X = 10
\+
[edit]This predicate is used to negate a goal. It succeeds if the following goal fails, and fails otherwise.
Syntax:
\+ <goal>.
Example:
\+ member(1, [1, 2, 3]). %=> no \+ 1 = 2. %=> yes
Predicates
[edit]Type predicates
[edit]The following predicates are used to determine if the given argument has a certain type.
- var and nonvar check if the argument is (or is not) an unbound variable. If the variable is bound it will not succeed.
- atom is used to determine if the given argument is an atom.
- number, integer, and float are used to determine if the given argument is a number, integer, respectively a float.
- compound is used to determine if the given argument is a structure.
- callable is used to determine if the given argument is a goal (a query).
- list, partial_list, and list_or_partial_list are used to determine if the given argument is a list, a partial list or both. list succeeds if the given list has as tail another proper list, meanwhile a partial list has as tail an unbound variable.
Syntax:
var(<expression>). atom(<expression>). ...
Examples:
var(X). %=> yes X = 1, var(X). %=> no list([1, 2, 3]). %=> yes list([1, A, B]). %=> yes list([1 | A]). %=> no
Unification predicates = and \=
[edit]- = is used to unify two expressions. The predicate succeeds if the expressions can unify, and as an output the variables are bound to the values that make the unification possible.
- \= succeeds only if the two expressions cannot unify. It does not produce any bindings.
Syntax:
<expression-1> = <expression-2>. <expression-1> \= <expression-2>.
Examples:
1 = 1. %=> yes a = a. %=> yes 1 + 2 = 3 %=> no 1 + 2 = A %=> A = 1 + 2 A = B %=> A = B 1 \= 2. %=> yes 1 \= A. %=> no
Arithmetic
[edit]Arithmetic operators
[edit]The following operators are the standard operators found in other programming languages.
Syntax:
+ E - E E1 + E2 E1 - E2 E1 * E2 E1 / E2 E1 // E2 -- integer division E1 rem E2 E1 mod E2 E1 ** E2 -- power raising E1 /\ E2 -- bitwise and E1 \/ E2 -- bitwise or E1 ^ E2 -- bitwise xor E1 << E2 -- bitwise left shift E1 >> E2 -- bitwise right shift inc(E) dec(E) abs(E) sign(E) floor(E) ceiling(E) round(E) min(E1, E2) max(E1, E2) sqrt(E) exp(E) log(E) cos(E) acos(E) sin(E) asin(E) atan(E) is -- used to force the evaluation. =:= =\= < =< > >=
Examples:
1 + 2 = 3. %=> no 1 + 2 is 3. %=> no 3 is 1 + 2. %=> yes
List predicates
[edit]append
[edit]Syntax:
append(<list-1>, <list-2>, <list-1-2>).
Implementation:
append_([], L, L). append_([H|L1], L2, [H|L]) :- append_(L1, L2, L).
member
[edit]Syntax:
member(<member>, <list>).
Implementation:
member_(M, [M|_]). member_(M, [_|L]) :- member_(M, L).
reverse
[edit]Syntax:
reverse(<list>, <reversed>).
Implementation:
reverse_([], []). reverse_([H|L1], R) :- reverse_(L1, R1), append(R1, [H], R).
delete and select
[edit]Syntax:
delete(<list>, <element>, <result>). %=> Removes all the occurrences in the list. select(<element>, <list>, <result>). %=> Removes only one occurrence in the list.
Implementation:
select_([], _, []). select_(E, [E|T], T). select_(E, [H|T], [H|R]) :- select_(E, T, R).
permutation
[edit]Syntax:
permutation(<list>, <permutation>).
Implementation:
permutation_([], []). permutation_([H|T], P) :- permutation(T, TP), select(H, P, TP).
prefix and suffix
[edit]Syntax:
prefix(<prefix>, <list>). suffix(<suffix>, <list>).
Implementation:
prefix_([], _). prefix_([H|P], [H|L]) :- prefix_(P, L).
sublist
[edit]Syntax:
sublist(<sublist>, <list>).
last and nth
[edit]Syntax:
last(<list>, <element>). nth(<index>, <list>, <element>).
Implementation:
last_([L], L). last_([_|T], L) :- last_(T, L).
length
[edit]Syntax:
length(<list>).
Implementation.
length_([], 0). length_([_|T], L) :- length_(T, L1), L is inc(L1).
max_list, min_list, sum_list
[edit]Syntax:
max_list(<list>, <max>). min_list(<list>, <min>). sum_list(<list>, <sum>).
sort
[edit]Syntax:
sort(<list>, <sorted>).