SlideShare a Scribd company logo
Prolog Programming Notes
1. Introduction to Prolog
Prolog (Programming in Logic) is a declarative programming language used for logical
reasoning, AI, and computational linguistics. Prolog focuses on what to solve (logic) rather than
how to solve (procedures).
Main elements:
1. 1. Facts: Represent truths about the world.
2. 2. Rules: Logical relationships between facts.
3. 3. Queries: Questions to the Prolog system to infer knowledge.
2. Prolog Syntax
Facts: Define relationships or properties using predicates.
Example:
loves(john, mary). % John loves Mary
male(john). % John is male
Rules: Define logical conditions using ':-' (if).
Example:
parent(X, Y) :- father(X, Y). % X is parent of Y if X is father of Y
parent(X, Y) :- mother(X, Y). % X is parent of Y if X is mother of Y
Queries: Ask questions to the system using '?-'
Example:
?- loves(john, mary). % Is John in love with Mary?
Yes.
3. Key Components
4. 1. Facts: Statements that are always true.
5. 2. Rules: Logical inference derived from facts.
6. 3. Queries: Questions asked to Prolog.
7. 4. Variables: Denoted by uppercase letters.
4. Execution Flow in Prolog
Prolog uses backtracking to find all possible solutions. If a rule or fact doesn’t satisfy the query,
it tries another path.
Example:
parent(john, mary).
parent(john, peter).
?- parent(john, X).
Result:
X = mary ;
X = peter.
5. Built-in Predicates
Arithmetic: Use 'is' for evaluation.
Example:
sum(X, Y, Z) :- Z is X + Y.
Comparison Operators:
- X > Y, X < Y
- X >= Y, X =< Y
- X =:= Y (equal), X == Y (not equal)
List Operations:
- [Head|Tail] syntax splits a list into its first element (Head) and the rest (Tail).
Example:
member(X, [X|_]).
member(X, [_|Tail]) :- member(X, Tail).
6. Example Programs
Family Tree:
% Facts
father(john, mary).
mother(susan, mary).
father(john, peter).
mother(susan, peter).
% Rules
parent(X, Y) :- father(X, Y).
parent(X, Y) :- mother(X, Y).
sibling(X, Y) :- parent(Z, X), parent(Z, Y), X = Y.
Queries:
?- sibling(mary, peter).
Yes.
?- parent(john, Who).
Who = mary ;
Who = peter.
Simple Calculator:
add(X, Y, Z) :- Z is X + Y.
subtract(X, Y, Z) :- Z is X - Y.
multiply(X, Y, Z) :- Z is X * Y.
divide(X, Y, Z) :- Y = 0, Z is X / Y.
Queries:
?- add(5, 3, Result).
Result = 8.
?- divide(10, 2, Result).
Result = 5.
7. Tips for Prolog Programming
8. 1. Always define facts and rules clearly before testing queries.
9. 2. Use comments (%) for better readability.
10. 3. Debug queries by step-by-step evaluation.
11. 4. Test edge cases, especially when using recursion or arithmetic.
8. Advantages of Prolog
12. 1. Natural representation of logic.
2. Built-in pattern matching.
3. Easy to express knowledge and relationships.
4. Great for AI, expert systems, and natural language processing.

More Related Content

PPTX
Explain Knowledge Representation(prolog) in AI.pptx
PPT
Prolog
PPTX
Untitled presentation unification algorithm.pptx
PDF
PROLOG in artificial intelligence(Basic of pprolog)).pdf
PPTX
Plc part 4
PPTX
Overview prolog
PPTX
Overview prolog
PPTX
Overview prolog
Explain Knowledge Representation(prolog) in AI.pptx
Prolog
Untitled presentation unification algorithm.pptx
PROLOG in artificial intelligence(Basic of pprolog)).pdf
Plc part 4
Overview prolog
Overview prolog
Overview prolog

Similar to Prolog_Programminvfygugy7gtugbugtg_Notes.docx (20)

PPTX
Overview prolog
PPTX
Overview prolog
PPTX
An introduction to Prolog language slide
PPTX
Overview prolog
PPTX
Overview prolog
PPTX
logic for Advanced Programming Practice.pptx
PPTX
PROLOG: Introduction To Prolog
PPT
Prolog basics
DOCX
AI Lab Manual.docx
PPT
Chavbbhhgghhhhhjjnnnngffffgggbpter 15.ppt
PPT
cs344-lect15-robotic-knowledge-inferencing-prolog-11feb08.ppt
PPTX
Prolog Programming : Basics
PPT
ppt
Β 
PPT
ppt
Β 
PPT
Introduction toprolog
PPT
Introduction to prolog
PPT
Introduction toprolog
PPT
Introduction to prolog
PPT
Introduction toprolog
PPT
Introduction to prolog
Overview prolog
Overview prolog
An introduction to Prolog language slide
Overview prolog
Overview prolog
logic for Advanced Programming Practice.pptx
PROLOG: Introduction To Prolog
Prolog basics
AI Lab Manual.docx
Chavbbhhgghhhhhjjnnnngffffgggbpter 15.ppt
cs344-lect15-robotic-knowledge-inferencing-prolog-11feb08.ppt
Prolog Programming : Basics
ppt
Β 
ppt
Β 
Introduction toprolog
Introduction to prolog
Introduction toprolog
Introduction to prolog
Introduction toprolog
Introduction to prolog
Ad

Recently uploaded (20)

PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
artificial intelligence overview of it and more
PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
Funds Management Learning Material for Beg
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PDF
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
PPTX
innovation process that make everything different.pptx
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
DOCX
Unit-3 cyber security network security of internet system
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PDF
Paper PDF World Game (s) Great Redesign.pdf
Design_with_Watersergyerge45hrbgre4top (1).ppt
artificial intelligence overview of it and more
international classification of diseases ICD-10 review PPT.pptx
Funds Management Learning Material for Beg
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Introuction about WHO-FIC in ICD-10.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
Best Practices for Testing and Debugging Shopify Third-Party API Integrations...
innovation process that make everything different.pptx
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
Unit-3 cyber security network security of internet system
An introduction to the IFRS (ISSB) Stndards.pdf
522797556-Unit-2-Temperature-measurement-1-1.pptx
The New Creative Director: How AI Tools for Social Media Content Creation Are...
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Module 1 - Cyber Law and Ethics 101.pptx
Job_Card_System_Styled_lorem_ipsum_.pptx
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
Unit-1 introduction to cyber security discuss about how to secure a system
Paper PDF World Game (s) Great Redesign.pdf
Ad

Prolog_Programminvfygugy7gtugbugtg_Notes.docx

  • 1. Prolog Programming Notes 1. Introduction to Prolog Prolog (Programming in Logic) is a declarative programming language used for logical reasoning, AI, and computational linguistics. Prolog focuses on what to solve (logic) rather than how to solve (procedures). Main elements: 1. 1. Facts: Represent truths about the world. 2. 2. Rules: Logical relationships between facts. 3. 3. Queries: Questions to the Prolog system to infer knowledge. 2. Prolog Syntax Facts: Define relationships or properties using predicates. Example: loves(john, mary). % John loves Mary male(john). % John is male Rules: Define logical conditions using ':-' (if). Example: parent(X, Y) :- father(X, Y). % X is parent of Y if X is father of Y parent(X, Y) :- mother(X, Y). % X is parent of Y if X is mother of Y Queries: Ask questions to the system using '?-' Example: ?- loves(john, mary). % Is John in love with Mary? Yes. 3. Key Components 4. 1. Facts: Statements that are always true. 5. 2. Rules: Logical inference derived from facts. 6. 3. Queries: Questions asked to Prolog. 7. 4. Variables: Denoted by uppercase letters. 4. Execution Flow in Prolog Prolog uses backtracking to find all possible solutions. If a rule or fact doesn’t satisfy the query, it tries another path. Example: parent(john, mary). parent(john, peter).
  • 2. ?- parent(john, X). Result: X = mary ; X = peter. 5. Built-in Predicates Arithmetic: Use 'is' for evaluation. Example: sum(X, Y, Z) :- Z is X + Y. Comparison Operators: - X > Y, X < Y - X >= Y, X =< Y - X =:= Y (equal), X == Y (not equal) List Operations: - [Head|Tail] syntax splits a list into its first element (Head) and the rest (Tail). Example: member(X, [X|_]). member(X, [_|Tail]) :- member(X, Tail). 6. Example Programs Family Tree: % Facts father(john, mary). mother(susan, mary). father(john, peter). mother(susan, peter). % Rules parent(X, Y) :- father(X, Y). parent(X, Y) :- mother(X, Y). sibling(X, Y) :- parent(Z, X), parent(Z, Y), X = Y. Queries: ?- sibling(mary, peter). Yes. ?- parent(john, Who). Who = mary ; Who = peter.
  • 3. Simple Calculator: add(X, Y, Z) :- Z is X + Y. subtract(X, Y, Z) :- Z is X - Y. multiply(X, Y, Z) :- Z is X * Y. divide(X, Y, Z) :- Y = 0, Z is X / Y. Queries: ?- add(5, 3, Result). Result = 8. ?- divide(10, 2, Result). Result = 5. 7. Tips for Prolog Programming 8. 1. Always define facts and rules clearly before testing queries. 9. 2. Use comments (%) for better readability. 10. 3. Debug queries by step-by-step evaluation. 11. 4. Test edge cases, especially when using recursion or arithmetic. 8. Advantages of Prolog 12. 1. Natural representation of logic. 2. Built-in pattern matching. 3. Easy to express knowledge and relationships. 4. Great for AI, expert systems, and natural language processing.