Prolong programming
Prolong programming
PROGRAMMING
INTRODUCTION
TO PROLOG
• Prolog (Programming in Logic) is a declarative programming
language primarily used for artificial intelligence and
computational linguistics.
• Unlike procedural programming languages like C or Python,
Prolog is based on logic and rules.
• Instead of giving step-by-step instructions, the programmer
defines facts and rules, and Prolog figures out the solution
through logical inference.
• Used in AI applications, natural language processing, expert
systems, and symbolic reasoning.
CHARACTERISTICS OF
PROLOG
1.Declarative Language: Focuses on what needs to be solved rather than
how to solve it.
2. Rule-Based System: Programs are written using facts, rules, and queries.
3.Pattern Matching: Uses a process called unification to match variables with
data.
4.Backtracking Mechanism: If one solution path fails, Prolog automatically
goes back and tries another.
5.Recursive Processing: Prolog heavily relies on recursion instead of loops
for iteration.
6.Symbolic Computation: Best suited for handling symbols, strings, and
logic-based operations, rather than numerical computations.
SYNTAX AND
A Prolog program consists of three key components:
STRUCTURE
1.Facts: We can define fact as an explicit relationship between
objects, and properties these objects might have. So facts are
unconditionally true in nature.
Following are some guidelines to write facts −
• Names of properties/relationships begin with lower case letters.
• The relationship name appears as the first term.
• Objects appear as comma-separated arguments within
parentheses.
• A period "." must end a fact.
The syntax for facts is as follows −
relation(object1,object2...).
Example
parent(john, mary).
parent(mary, alice).
2. Rules:
We can define rule as an implicit relationship between objects. So facts are
conditionally true. So when one associated condition is true, then the predicate is also
true.
Suppose we have some rules as given below −
• Lili is happy if she dances.
• Tom is hungry if he is searching for food.
So these are some rules that are conditionally true, so when the right hand side is true,
then the left hand side is also true.
Syntax:
rule_name(object1, object2, ...) :- fact/rule(object1, object2, ...)
Example:
grandparent(X, Y) :- parent(X, Z), parent(Z, Y).
3. Queries:
Queries are some questions on the relationships between objects and object properties. So
question can be anything, as given below −
• Is tom a cat?
• Does Kunal love to eat pasta?
• Is Lili happy?
• Will Ryan go to play?
So according to these queries, Logic programming language can find the answer and return
them.
Example:
?- grandparent(john, Who).