L2 GettingStarted
L2 GettingStarted
Spring 23/24
28-04-2024
البداية مع لغة برولوج
1
Prolog
Programing in Logic
2
Outline
➢ Language Characteristics
➢ Language Elements عناصر اللغة
➢ Executing Prolog تنفيذ البرامج
➢ Rules القواعد
➢ A Database in Prolog
➢ Recursive Rules
➢ Operators
3
Characteristics
➢Prolog is based on first-order logic.
➢لغة برولوج مبنية على المنطق البسيط
➢Every program is a set of Facts and Rules.
➢البرنامج عبارة عن مجموعة من الحقائق والقواعد
➢Inference is by resolution.
➢Search is by backtracking with unification.
➢Basic data structure is term or tree.
4
SWI-Prolog
➢To load a file:
?- consult( ‘D:\\prolog\\test’). Or :-[‘filename’].
➢For help:
?-help(predicate-name).
➢“ ; “ will give you next solution.
?- listing(predicate-name) %will give definition.
5
Language Elements
عناصر لغة البرولوج
➢ Term: is a constant or a variable
المفردة هي عبارة عن ثابت او متغير ➢
➢ Facts and Rules: use terms as data structures
الحقائق والقواعد تستخدم المفردات كهياكل بيانات ➢
➢ Predicates: made up of facts and rules
المسندات :تتكون من الحقائق والقواعد ➢
➢ The (Logic) Program: made up of predicates
البرنامج المنطقي يتكون من مجموعة من المسندات ➢
6
Terms المفردات البسيطة
Terms are:
➢ Constants : begin with small letter الثوابت تبدأ بحرف صغير او
تكون رقم او قائمة خالية او اي شئ بين عالمة تنصيص فردية
❖ ali libya 5 3.14 [] ´Ali’
➢ Variables: begin with uppercase or underscore
➢ المتغيرات تبدأ بحرف كبير او شارحة تحتية
❖ X Y List _12 _A _
➢ Compound terms
❖ plus(2,3)
❖ course(java, time(monday, 8, 10), ali, g02).
❖ 2+3 // infix notation
7
Facts
➢Examples:
note the '.'s
teaches(a, b).
parent(‘Ali’, salama).
8
Example
➢ Facts:
❖likes(hend,school).
❖likes(hend,bananas). Defines a predicate likes of
❖likes(hend,hend). arity 2
❖likes/2 for short
➢ Queries
❖?- likes(X,Y).
❖ X=hend, y=bananas % hit “;” for more
❖?- likes(X,X).
❖X=hend.
9
Example
➢ Facts:
likes(awatif,school).
female(awatif).
female(mariam).
➢ Rules
❖likes(hend,X) :- likes(X,school). % :- = if
❖likes(hend,X):- female(X), likes(X, bananas).
➢ Query:
? - likes(hend,Y).
Y = ?? ;
no.
10
Family Example
father(ali,salama).
father(salem, fatima).
mother(hend,salama).
mother(salama,fatima).
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
grandmother(X,Y):-
xxxxxxxxxxxxxxxxxxxx.
% Add your own facts for practice.
11
Unification of terms
مطابقة الفقرات
➢ Two terms UNIFY if there is a common substitution which
makes them identical.
➢ يمكن مطابقة اي فقرتين اذا امكن استبدال اي منهم باالخر
➢ ?- f(X,Y) = f(ali,salem).
X = ali,
Y=salem
❖ A variable matches anything
❖ المتقير يمكن مطابقته مع اي شئ اخر
❖ predicate symbols only match identical predicate symbols.
❖ المسند يمكن مطابقته مع مسند بنفس االسم فقط
12
Summary
13
Download Prolog
www.swi-prolog.org
14