Prolog EN -- Laboratory 2 -- 2006-2007 -- info.uvt.ro
The contents of this section is structured base on the GNU Prolog Manual.
Directives
editconsult
editThe 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
editThe load directive is used to load a compiled file. It behaves just like consult. The source files are compiled by using gplc.
listing
editThe 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
editThe halt directive is used to exit from the interpreter.
Syntax and example:
halt.
Control structures
edittrue, fail, and !
editThese 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
editThe abort directive (or predicate) is used to abort the current running query.
for
editFor 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
\+
editThis 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
editType predicates
editThe 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
editArithmetic operators
editThe 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
editappend
editSyntax:
append(<list-1>, <list-2>, <list-1-2>).
Implementation:
append_([], L, L). append_([H|L1], L2, [H|L]) :- append_(L1, L2, L).
member
editSyntax:
member(<member>, <list>).
Implementation:
member_(M, [M|_]). member_(M, [_|L]) :- member_(M, L).
reverse
editSyntax:
reverse(<list>, <reversed>).
Implementation:
reverse_([], []). reverse_([H|L1], R) :- reverse_(L1, R1), append(R1, [H], R).
delete and select
editSyntax:
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
editSyntax:
permutation(<list>, <permutation>).
Implementation:
permutation_([], []). permutation_([H|T], P) :- permutation(T, TP), select(H, P, TP).
prefix and suffix
editSyntax:
prefix(<prefix>, <list>). suffix(<suffix>, <list>).
Implementation:
prefix_([], _). prefix_([H|P], [H|L]) :- prefix_(P, L).
sublist
editSyntax:
sublist(<sublist>, <list>).
last and nth
editSyntax:
last(<list>, <element>). nth(<index>, <list>, <element>).
Implementation:
last_([L], L). last_([_|T], L) :- last_(T, L).
length
editSyntax:
length(<list>).
Implementation.
length_([], 0). length_([_|T], L) :- length_(T, L1), L is inc(L1).
max_list, min_list, sum_list
editSyntax:
max_list(<list>, <max>). min_list(<list>, <min>). sum_list(<list>, <sum>).
sort
editSyntax:
sort(<list>, <sorted>).