<div class="notebook">
<div class="nb-cell markdown" name="md1">
# Introduction to Logic and Programming
## Notes and Sample Programs - Under Revision
### (Compiled by: Dr. Selvaraj Vadivelu)
This notebook contains example programs with explanation for illustrating various concepts. The example programs have been *either created or picked up from publicly available sources*.
</div>
<div class="nb-cell markdown" name="md21">
### Examples of Atoms
> Strings made up of lower and uppercase letters, digits, and the underscore, starting with a lowercase letter.
abcXYZ.
x_123.
how_are_you_today.
> Any sequence of arbitrary characters enclosed in single quotes denotes an atom.
'This is a Prolog Atom'.
### Examples of Variables
X.
Elephant.
_4711.
X_1_2.
MyVariable.
### Examples of Compound Terms
> Compound terms are made up of a functor (a Prolog atom) and a number of arguments (Prolog terms, i.e., atoms, numbers, variables, or other compound terms) enclosed in parentheses and separated by commas. Examples:
is_bigger(horse, X).
f(g(X, _), 7).
'My Functor'(dog).
### Examples of Facts
bigger(whale,shark).
likes(ram,lakshman).
### Examples of Rules
> A rule consists of a head (a predicate) and a body (a sequence of predicates separated by commas). Head and body are separated by the symbol :- (which denotes ‘if’) A rule must be terminated by a full stop.
is_smaller(X, Y) :- is_bigger(Y, X).
aunt(Aunt, Child) :-
sister(Aunt, Parent),
parent(Parent, Child).
### Examples of Queries
> After compilation, a Prolog program is run by submitting queries to the interpreter. A query has the same structure as the body of a rule, i.e., it is a sequence of predicates separated by commas and terminated by a full stop. They can be entered at the Prolog prompt, which in most implementations looks something like this: **?-**
?- is_bigger(elephant, donkey).
// we are asking if elephant is bigger than donkey
?- small(X), green(X), slimy(X).
// we are asking whether there exists an X such that small(X), green(X), and slimy(X) are all true.
</div>
<div class="nb-cell markdown" name="md22">
### Illustration of Output
> For output the write predicate can be used with any valid Prolog term as the argument.In the case of a variable its value will get printed to the screen. Execution of the predicate nl/0 causes the system to skip a line.
Try executing the two examples below:
</div>
<div class="nb-cell query" name="q29">
write('Hello World!'), nl.
</div>
<div class="nb-cell query" name="q30">
X = elephant, write(X), nl.
</div>
<div class="nb-cell markdown" name="md13">
## Example Program 1
</div>
<div class="nb-cell program" name="p7">
% This is a simple Prolog program that defines facts and rules.
/*
In this program:
The % character is used to start comments in Prolog.
*/
% Facts: These are statements about the world.
% Here, we define facts about some animals and their characteristics.
%Facts are represented by statements like mammal(cat)., which asserts that "cat" is a mammal.
% Animals and their characteristics
mammal(cat).
mammal(dog).
mammal(horse).
bird(sparrow).
bird(eagle).
% Rules: These are statements that define relationships between facts.
% Define a rule for identifying warm-blooded animals (mammals).
% Rules are defined using predicates like warm_blooded(X) :- mammal(X).,
% which states that an X is warm-blooded if it is a mammal.
warm_blooded(X) :-
mammal(X).
% Define a rule for identifying flying animals (birds).
can_fly(X) :-
bird(X).
</div>
<div class="nb-cell markdown" name="md14">
Queries are used to interact with the program, and they can be entered in the Prolog interpreter to obtain answers based on the facts and rules defined.
Try below queries: You can query the program to ask questions or make assertions.
</div>
<div class="nb-cell query" name="q16">
% Is a cat a warm-blooded animal?
% The program will respond with "true" if it matches the rule.
warm_blooded(cat).
</div>
<div class="nb-cell query" name="q17">
% Is a sparrow a warm-blooded animal?
warm_blooded(sparrow).
</div>
<div class="nb-cell query" name="q18">
% Is a dog a flying animal?
% The program will respond with "false" as it doesn't match the rule.
can_fly(dog).
</div>
<div class="nb-cell query" name="q19">
% You can also use variables in queries to find all animals with a certain characteristic.
% Find all warm-blooded animals.
warm_blooded(X).
</div>
<div class="nb-cell query" name="q20">
% Find all flying animals.
can_fly(X).
</div>
<div class="nb-cell markdown" name="md16">
## Example Program 2
</div>
<div class="nb-cell program" name="p8">
bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).
is_bigger(X, Y) :- bigger(X, Y).
is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).
</div>
<div class="nb-cell markdown" name="md17">
Try the below queries
</div>
<div class="nb-cell query" name="q22">
% If Donkey is bigger than any animal(s), find them
bigger(donkey,X).
</div>
<div class="nb-cell query" name="q23">
% Find animals which are bigger than dog
bigger(X, dog).
</div>
<div class="nb-cell query" name="q39">
is_bigger(P,Q).
</div>
<div class="nb-cell markdown" name="md2">
## Example Program 3
</div>
<div class="nb-cell program" name="p11">
eats(fred,oranges). /* 'Fred eats oranges' */
eats(tony,apple). /* 'Tony eats apple' */
eats(john,apple). /* 'John eats apple' */
</div>
<div class="nb-cell query" name="q26">
eats(fred,oranges).
</div>
<div class="nb-cell query" name="q27">
eats(john,apple).
</div>
<div class="nb-cell query" name="q28">
eats(mike,apple).
</div>
<div class="nb-cell markdown" name="md2">
## Example Program 4
</div>
<div class="nb-cell program" name="p1">
/* Simple clauses */
likes(mary,food).
likes(mary,wine).
likes(john,wine).
likes(john,mary).
likes(john,sally).
likes(mary,john).
likes(sally,sally).
%John likes anything that Mary likes
john_likes_rule1(john, X) :- likes(mary, X).
%John likes anyone who likes wine
john_likes_rule2(john, Y) :- likes(Y, wine).
%John likes anyone who likes themselves
john_likes_rule3(john, Z) :- likes(Z, Z).
friendship(X,Y):-
likes(X,Y),
likes(Y,X).
</div>
<div class="nb-cell markdown" name="md3">
Try the following Queries
</div>
<div class="nb-cell query" name="q1">
likes(mary,food).
</div>
<div class="nb-cell query" name="q2">
likes(john,wine).
</div>
<div class="nb-cell query" name="q3">
likes(john,food).
</div>
<div class="nb-cell query" name="q4">
friendship(mary,john).
</div>
<div class="nb-cell query" name="q5">
friendship(john,sally).
</div>
<div class="nb-cell query" name="q40">
john_likes_rule1(john,X).
</div>
<div class="nb-cell query" name="q41">
john_likes_rule2(john,X).
</div>
<div class="nb-cell query" name="q42">
john_likes_rule3(john,X).
</div>
<div class="nb-cell markdown" name="md4">
## Example Program 5
</div>
<div class="nb-cell program" name="p2">
% Sample Prolog program to demonstrate the usage of write statements.
% Define some facts about fruits.
fruit(apple, red, sweet).
fruit(banana, yellow, sweet).
fruit(grape, purple, sweet).
fruit(lemon, yellow, sour).
fruit(orange, orange, sweet).
% Define a predicate to print information about fruits.
print_fruit_info(Fruit) :-
fruit(Fruit, Color, Taste),
nl, write('Fruit: '), write(Fruit), nl, % Print the fruit name on a new line.
write('Color: '), write(Color), nl, % Print the color on a new line.
write('Taste: '), write(Taste), nl, % Print the taste on a new line.
nl. % Add an extra line to separate different fruits.
</div>
<div class="nb-cell markdown" name="md5">
Try the following Queries
</div>
<div class="nb-cell query" name="q6">
% Example usage:
% Query: print_fruit_info(apple).
% This will display information about the apple on multiple lines.
print_fruit_info(apple).
</div>
<div class="nb-cell query" name="q43">
print_fruit_info(X).
</div>
<div class="nb-cell markdown" name="md6">
## Example Program 6
</div>
<div class="nb-cell program" name="p5">
%% Demo coming from http://clwww.essex.ac.uk/course/LG519/2-facts/index_18.html
%%
%% You can also run this demo online at
%% http://swish.swi-prolog.org/?code=https://github.com/SWI-Prolog/swipl-devel/raw/master/demo/likes.pl&q=likes(sam,Food).
likes(sam,Food) :-
indian(Food),
mild(Food).
likes(sam,Food) :-
chinese(Food).
likes(sam,Food) :-
italian(Food).
likes(sam,chips).
indian(curry).
indian(dahl).
indian(tandoori).
indian(kurma).
indian(chatney).
mild(dahl).
mild(tandoori).
mild(kurma).
chinese(chow_mein).
chinese(chop_suey).
chinese(sweet_and_sour).
italian(pizza).
italian(spaghetti).
</div>
<div class="nb-cell markdown" name="md11">
Try the following queries
</div>
<div class="nb-cell query" name="q31">
mild(dahl).
</div>
<div class="nb-cell query" name="q32">
mild(dahl),mild(kurma).
</div>
<div class="nb-cell query" name="q11">
mild(dahl);mild(pizza).
</div>
<div class="nb-cell query" name="q33">
not(mild(dahl)).
</div>
<div class="nb-cell query" name="q34">
likes(sam,dahl).
</div>
<div class="nb-cell markdown" name="md8">
## Example Program 7
</div>
<div class="nb-cell program" name="p4">
/*
James I
|
|
+----------------+-----------------+
| |
Charles I Elizabeth
| |
| |
+----------+------------+ |
| | | |
Catherine Charles II James II Sophia
|
|
|
George I
Here are the resultant clauses:
-------------------------------
*/
% james1 is a male
male(james1).
male(charles1).
male(charles2).
male(james2).
male(george1).
% catherine is a female
female(catherine).
female(elizabeth).
female(sophia).
% james1 is a parent of charles1
parent(charles1, james1).
% james1 is a parent of elizabeth
parent(elizabeth, james1).
parent(charles2, charles1).
parent(catherine, charles1).
parent(james2, charles1).
parent(sophia, elizabeth).
parent(george1, sophia).
% M is the mother of X if she is a parent of X and is female
mother(X,M) :-
parent(X,M),
female(M).
% F is the father of X if he is a parent of X and is male
father(X,F):-
parent(X,F),
male(F).
% X is a sibling of Y if they both have the same parent.
sibling(X,Y):-
parent(X,P),
parent(Y,P),
X \= Y.
% S is a sister of X if S is a female, S and X are siblings.
sister(X,S):-
female(S),
sibling(X,S).
/*
"sister", "brother",
"aunt", "uncle",
"grandparent", "cousin"
*/
</div>
<div class="nb-cell markdown" name="md7">
Try following Queries
</div>
<div class="nb-cell query" name="q8">
% Was George I the parent of Charles I?
parent(charles1, george1).
</div>
<div class="nb-cell query" name="q9">
% Who was Charles I's parent?
parent(charles1,X).
</div>
<div class="nb-cell query" name="q10">
%Who were the children of Charles I?
parent(X,charles1).
</div>
<div class="nb-cell query" name="q44">
mother(sophia,elizabeth).
</div>
<div class="nb-cell query" name="q45">
mother(X,Y).
</div>
<div class="nb-cell query" name="q46">
father(X,F).
</div>
<div class="nb-cell query" name="q47">
sibling(X,Y).
</div>
<div class="nb-cell query" name="q48">
sister(X,S).
</div>
<div class="nb-cell markdown" name="md28">
## [Next: Recursion](ILP_Recursion.swinb)
</div>
</div>