Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views3 pages

Prolog Section

Prolog is a declarative programming language based on formal logic, used for symbolic reasoning in AI and computational linguistics. Programs consist of facts and rules, allowing for queries to infer relationships, including recursive definitions. Users can run Prolog programs using interpreters like SWI-Prolog and are encouraged to practice by creating family trees and calculating factorials.

Uploaded by

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

Prolog Section

Prolog is a declarative programming language based on formal logic, used for symbolic reasoning in AI and computational linguistics. Programs consist of facts and rules, allowing for queries to infer relationships, including recursive definitions. Users can run Prolog programs using interpreters like SWI-Prolog and are encouraged to practice by creating family trees and calculating factorials.

Uploaded by

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

Introduction to Prolog

1. What is Prolog?
Prolog, short for "Programming in Logic," is a declarative programming language that is based
on formal logic. In Prolog, you express relationships and rules rather than giving step-by-step
instructions. It excels at solving problems involving symbolic reasoning and is widely used in
artificial intelligence and computational linguistics.

2. Basic Structure of Prolog Programs


A Prolog program consists of facts and rules. Facts are statements about relationships, and rules
define how new facts can be inferred from existing ones.
% Facts
parent(john, mary).
parent(john, jim).
parent(sue, mary).
parent(sue, jim).

% Rule
ancestor(X, Y) :- parent(X, Y).
In this example, we have facts about parent-child relationships and a rule that defines the
relationship of being an ancestor.

3. Basic Queries
You can query Prolog by asking questions about the relationships defined in your program.
?- parent(john, mary). % true
?- parent(john, george). % false

4. Variables and Queries


Prolog uses variables to represent unknown values. You can use queries with variables to find
solutions.
?- parent(john, X).
% X = mary ;
% X = jim.
This query finds all children of John.

5. Simple Rules
Expand on your knowledge by defining and using rules.
% Rule
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X \= Y.
This rule defines the relationship of being siblings. The \= operator denotes inequality.
?- sibling(mary, jim).
% true

6. Recursive Rules
Prolog supports recursion, allowing you to define rules that refer to themselves.
% Recursive rule
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
This rule defines the relationship of being an ancestor recursively.

7.Running Prolog
You can use an interactive Prolog interpreter to run your programs. Popular Prolog interpreters
include SWI-Prolog, GNU Prolog, and SICStus Prolog. Install one of these on your system and
open the interpreter.
Compiler: https://swish.swi-prolog.org/

Homework
-Try creating a simple Prolog program that models a family tree with at least three generations.
-Write a prolog program that calculates factorial.
Exercises
fact. | ?- Query.
male(ali). | ?- male(X). |?- male(X).
male(albert). %a fact stating albert is a male ?-male(albert).
male(edward). ?-male(mycat).
female(alice). %a fact stating alice is a female ?-male(X).
female(victoria). %X is a variable, we are asking
parent(albert,edward). %a fact: albert is parent of edward “who is male?”
parent(victoria,edward). ?-father(F,C).
#rule or relation %F & C are variables, we are
father(X,Y) :- %a rule: X is father of Y if X if a male parent of Y asking “who is father of whom”
parent(X,Y), male(X). %body of above rule, can be on same line.
mother(X,Y) :- parent(X,Y), female(X). %a similar rule for X being
mother of Y
elephant(fred). animal(fred), animal(mary),
elephant(mary). animal(joe)
elephant(joe). elephant(joe)
animal(fred) :- elephant(fred). animal(X)
animal(mary) :- elephant(mary). elephant(X)
animal(joe) :- elephant(joe).
%The three rules can be replaced by the single rule
animal(X) :- elephant(X). %variables allow many additional matches.

X is 3+5.
5+7=5+7.
5+7=7+5.

You might also like