0% found this document useful (0 votes)
3 views7 pages

Prolog Notes

The document provides a comprehensive guide on using SWI-Prolog, including how to consult files, types of clauses, operators, recursion, and list manipulation. It explains the difference between head and tail recursion, as well as various predicates for list operations such as finding elements, deleting items, and checking prefixes and suffixes. Additionally, it covers modules for linking files and demonstrates examples of queries and rules.

Uploaded by

sofyafarahat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Prolog Notes

The document provides a comprehensive guide on using SWI-Prolog, including how to consult files, types of clauses, operators, recursion, and list manipulation. It explains the difference between head and tail recursion, as well as various predicates for list operations such as finding elements, deleting items, and checking prefixes and suffixes. Additionally, it covers modules for linking files and demonstrates examples of queries and rules.

Uploaded by

sofyafarahat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Variable: capital letter

each predicate ends by .

Consult pl files to SWI:


-----------------------
?- consult ('C:\myfile.pl').
or from tool bar (click File → Consult → Browse for (.pl files))

Consult Multiple pl files to SWI:


---------------------------------
?– consult ([file1, file2, file3]).

if u made some edit in file.pl and want SWI to see changes:


-----------------------------------------------------------
?– make.

Types of clauses in Prolog:


---------------------------
● Facts: define things that are unconditionally (always) true.
● Rules: define things that are true depending on given conditions.
● Queries: are questions or goals that the Prolog interpreter tries to prove.(in
SWI Command Line)

Is:
---
<Variable> is <expression>
N is 6+1 -> N =7

<Constant> is <expression>
7 is 6+1 -> true

7+1 is 7+1 (wrong)XX

equal(=):
---------
N = 6+1 -> N = 6+1 (take it as string with no evaluation)

7 = 6+1 -> false (no evaluation "6+1" != "7")

7+1 = 8 -> false

(=:=):
------
N =:= 6 -> error (doesnt take variable)

5 =:= 4+1 -> true (do evaluation on right)

6+1 =:= 8-1 -> true (do expression on left)

(\=):
-----
4 \= 4. -> false (checks if 4 is unequal to 4)

4 \= 2+2. -> true (doesnt evaluate term on right and it takes it as string)
X \= 4. -> false (doesnt know what X is so it return false as fails to know X)

X \== 4. -> true (can differ bet variable and constant and return they are not
the same)

(=\=):
------
4 \= 2+2. -> false (evaluate term on right)

(==):
-----
X == 2. -> false (matches both)

2 == 2. -> true

---- You cant make a query of (2+3), you need to put it in a variable to return
result like (x is 2+3)

Operators:
---------
// integer division -> 5//3
/ double division (result is decimal) -> 5/3
mod -> 5 mod 3
abs() -> abs(-3)
float() -> float(19) -> 19.0
round() -> round(5.7) -> 6 (‫)تقريب‬
ceiling() -> ceiling(5.1) -> 6
floor() -> floor(5.7) -> 5 (floor(-5.1) -> -6)
truncate() -> truncate(5.9) -> 5

*Queries Examples:
------------------

?- parent(X, Y), X \= ali. % means give me those relations when parent is not
ali
X = nour,
Y = amr;
X = amr,
Y = camilia;
X = amr,
Y = omar;

?- aunt(X,_). % means any parent that matches _ ( _ is dont care or ignore)


X = maha;
X = maha;
X = camilia.

trace:
-----
?- trace.

?- notrace. (to exit trace)


Predecessor:
-----------
predecessor(X, Y):- % base case is being parent
parent(X, Y).

predecessor(X, Y):-
parent(X, Z),
predecessor(Z, Y).

Recursion:
---------
Head Recursion(call then arithmatic operation):
-----------------------------------------------
sumToN(1,1).

sumToN(N,R):-
N1 is N-1,
sumToN(N1,R1),
R is N+R1.

Tail Recursion (arithmatic operation then call):


-----------------------------------------------
sumToN(0,R,R).

sumToN(N,Temp,R):-
N1 is N-1,
NewTemp is N+Temp,
sumToN(N1,NewTemp,R).

Power (Head Version):


---------------------
powerr(X,0,1).

powerr(X,Y,R):-
Y >0,
Y1 is Y-1,
powerr(X,Y1,R1),
R is X*R1.

Power (Tail Version):


---------------------
powerr(X, 0, R, R).

powerr(X, Y, Temp, R) :-
Y > 0,
Y1 is Y - 1,
NewTemp is Temp * X,
powerr(X, Y1, NewTemp, R).

Factorial(Head Version):
------------------------
factorial(1,1).

factorial(X,R):-
X > 1,
X1 is X-1,
factorial(X1,R1),
R is X*R1.

Factorial(Tail Version):
------------------------
factorial(1,R,R).

factorial(X,Temp,R):-
X > 1,
X1 is X-1,
NewTemp is X*Temp,
factorial(X1,NewTemp,R).

Module:
-------
to link two files

In first file:
-------------
module(name_module, [rule1/no_arguments , rule2/no_arguments]).

rule1(argument1, argument2, Result1):-


......

rule2(argument3, Result2):-
......

In second file:
--------------
use_module(name_module).

go(argument1, argument2, argument3, Result1 , Result2):-


rule1(argument1, argument2, Result1),
rule2(argument3, Result2).

--if u consult 2nd file and run predicate go, it will call other predicates from
file1.

-----------------------------------------------------------------------------------
-------

List:
-----
N = [dog, 1, 2, X, Parent(X, Ali)]
or N = [] (empty)

[H|T] = [1,2,3,4] -> H = 1, T = [2,3,4]

[H,S|T] = [1,2,3,4] -> H=1, S=2, T=[3,4]


[H] = [1,2,3] -> False (cause one element only in left to match list in
right)

[H] = [1] -> H = 1 (accepted)

[H,S] = [1] -> false (not accepted as there should be matching of 2


elements)

[H|T] = [1] -> H = 1 , T = []

[H|[1,2,3]] = [5, S|T] -> H=5, S=1,T=[2,3]

[[a,b]|[c]] = [[a,b],c]

member function:
---------------
member(a, [a, b, c]). -> true.

member([a], [a, b|[c]]). -> false. (a is member while [a] is not member)

member(3, [1,3,2,3,5]). -> true; true; false.

Example Rules for lists:


------------------------
firstElement([H|T], H).
secondElement([H,S|T]], S).
thirdElement([H,S,E|T]], E).

lastElement([H],H).
lastElement([H|T],L):-
lastElement(T,L).

beforeLast([H,S],H).
beforeLast([H|T],L):-
beforeLast(T,L).

length([],0).
length([H|T], R):-
length(T,R1),
R is R1+1.

example:
length([a, b,[c, d],e], N). -> N = 4.

delete from list:


-----------------
delete(X, [X|Y], [Y]).

delete(X, [Y|Tail1], [Y|Tail2]):-


delete(X, Tail1, Tail2).

append: (add two lists together) (built-in predicate)


-------
append([], L, L).
append([H|T], L2, [H|NT]):-
append(T, L2, NT).

suffix (using append): (first list appears at the end of second list)
----------------------
suffix(List1, List2):-
append(_, Last1, List2).

example:
suffix([3,4,5],[1,2,3,4,5]). -> true.

prefix (using append): (first list appears at the beginning of second list)
---------------------
prefix(List1, List2):-
append(Last1, _, List2).

example:
prefix([1,2,3],[1,2,3,4,5]). -> true.

lastElement in list (using append):


----------------------------------
lastElement(Element, List):-
append(_, [Element], List).

example:
last([5,[1,2,3,4,5]). -> true.

Addone(add 1 to each element in list):


-------------------------------------
addone([],[]).

addone([First|Rest], [Add|Rest1]):-
Add is First+1,
addone(Rest,Rest1).

adjacent(if two elements are adjacent in list):


----------------------------------------------
adjacent(X, Y, [X,Y|_]).
adjacent(X, Y, [_|T]):-
adjacent(X, Y, T).

find nth element in list:


------------------------
nth(X, L, N):-
nth(X, L, 0, N).

nth(X, [X|_], I, I).


nth(X, [_|T], I, N):-
I1 is I+1,
nth(X, T, I1, N).

example:
nth(X,[x,3,5,9,10],4). -> X = 10.

nth(3,[x,3,5,9,10,3],X). -> X = 1; X = 5; false.

You might also like