Logic declarative programming language. Uses predicate syntax ie. it is similar to natural language.
-
swi-prolog
- installed:
brew tap homebrew/x11 brew install swi-prolog
- installed:
-
swipl compiler (command line)
What is known. Axioms of an object and its relationship to others.
Example:
- Dana is fat.
- The cat is white.
- Hamad likes Mona.
The predicates are:
- fat
- white
- likes
To convert it to terms which prolog would understand:
Who is fat? fat(dana).
What is white? white(cat).
Who likes who? likes(hamad, mona).
"Arity" = number of parameters passed. the first two are 1, while the last one is 2.
Rules extend the fact about object and their relationship.
Example:
These are facts
likes(jana, omar).
likes(omar, jana).
likes(jana, farah).
Now we want to know if two persons are dating. We need a Rule. Let's assume that two persons(x,y) are dating if and only if x likes y and y likes x.
Prolog code:
dating(X,Y) :-
likes(X,Y),
likes(Y,X).
Note how X and Y are uppercase letters. In prolog variables should be either uppercase letters of full caps words.
logical prolog operators table
Op | syntax |
---|---|
IF | :- |
NOT | not |
OR | ; |
AND | , |
Combines the idea of facts and rules together.
in the terminal:
likes(khaled, ahmed).
> false (value returned)
likes(jana, omar).
> true (value returned)