SlideShare a Scribd company logo
1 of 61
INTRODUCTION TO PROLOG
- Brian Hutchinson
Present by -
W.J.A.I.U. Jayaweera- 100227D
G.K.M.C Sajeewa- 100470N
M.M.D.T.K Wijewardane- 100612E
OUTLINE
 Introduction
 Language Features
 More Features
 Behind the scenes
 Language Classifications
 Examples
INTRODUCTION
 Prolog is the most popular language of the logic
programing languages.
 It is goal based language, it has automatic
backtracking and uses recursion.
 Prolog stands for programmation en logique", or
programming in logic."
INTRODUCTION [CTD...]
 Invented by Alain Colmerauer and Philippe Roussel
of the Groupe d'Intelligence Articielle (GIA) in early
1970s.
 The earliest Prolog interpreter was written in Algol
in1972 by Roussel.
 The official ISO standard for Prolog was published
in 1996.
CURRENT IMPLEMENTATIONS
 There are several popular Prolog interpreters
available. They include:
 GNU Prolog (http://pauillac.inria.fr/vdiaz/gnu-
prolog/)
 SWI-Prolog (http://www.swi-prolog.org/)
 Visual Prolog (http://www.visual-
prolog.com/vip6/Download/Default.htm)
 BProlog
(http://www.cad.mse.kyutech.ac.jp/people/zhou/bpr
olog.html)
LANGUAGE FEATURES
Prolog Program
Facts
Rules
(Relationships)
Questions
(Goals)
Clauses
LANGUAGE FEATURES[CTD...]
 Facts-Something that is declared to always be true.
Ex-father(bob,george)
mother(alice,jeffrey)
 Rules- Something which is true only if all conditions
(sub goals) on the right are true
Ex-
parent(X,Y) :- father(X,Y).
parent(X,Y) :- mother(X,Y).
grandparent(X,Z) :- parent(X,Y), parent(Y,Z).
LANGUAGE FEATURES[CTD...]
 Questions- Something which asks from the system
Ex- ?-ancestor(X,cindy),sibling(X,jeffrey)
 Clauses- Something which has a head and a body.
 Fact = head
 Rules = head + body
 Question=body
LANGUAGE FEATURES [CTD...]
Ex-
 uncle(X,Y) :- sibling(X,Z), parent(Z,Y).
(head) (body)
 ?- ancestor(X,cindy),sibling(X,jeffrey)
(body)
 father(john,mary)
(head)
LANGUAGE FEATURES[CTD...]
 Terms - term can be number, constant (Albert) or
variable(X).
 Compound Terms- data structure, convenient way
of grouping relevant data together
Ex- fullname( joe,brown )
student( fullname( joe,brown ), 25 )
Full name , Student are called as functors.
MORE FEATURES
 Order- order matters in prolog, order is top to
bottom.
Ex- dog(rover).
dog(duke).
We enter the following question:
?- dog(X).
Answer : X = rover
 Order is important for the efficiency and correctness
of the program.
MORE FEATURES[CTD...]
 Backtracking-
 prolog stores data in tree.
 It uses depth first search to find answers.
 In its attempt to prove a goal, Prolog sometimes
goes down a dead-end, at which point it needs to
backtrack.ck
MORE FEATURES[CTD...]
Ex- Sample Code
Facts- green(poisonivy).
green(broccoli).
edible(icecream).
edible(broccoli).
Question- ?- green(X),edible(X).
Answer- X = broccoli
MORE FEATURES[CTD...]
Execution trace-
?- green(X),edible(X).
1 1 Call: green(poisoniyy) ?
1 1 Exit: green(poisonivy) ?
2 1 Call: edible(poisonivy) ?
2 1 Fail: edible(poisonivy) ?
1 1 Redo:green(poisonivy)1
1 1 Call: green(broccoli) ?
1 1 Exit: green(broccoli) ?
2 1 Call: edible(broccoli) ?
2 1 Exit: edible(broccoli) ?
Green(p)
Edible(p)
Green(b)
Root
Edible(b)
Answer
MORE FEATURES[CTD...]
 Cuts- The cut is denoted by exclamation mark “!” and
disallows prolog from backtracking past that point.
 Ex-
With Out Cut
grade(G,a) :- G > 90.
grade(G,b) :- G > 80.
grade(G,c) :- G > 70.
grade(G,d) :- G > 60.
grade(_,f).
With Cut
grade2(G,a) :- G > 90, !.
grade2(G,b) :- G > 80, !.
grade2(G,c) :- G > 70, !.
grade2(G,d) :- G > 60, !.
grade2(_,f).
MORE FEATURES[CTD...]
Forced Backtrack With Grade
?- grade(83,X)
X = b;
X = c;
X = d;
X = f
yes
Forced Backtrack With Grade2
?- grade2(83,X)
X = b;
yes
Forced back tracking with semi
colon.
Types of Cuts
Red Cuts
Green Cuts
MORE FEATURES[CTD...]
Recursion- like functional languages prolog uses
recursion.
Ex-
ancestor(X,Z) :- parent(X,Z).
(Base Case)
ancestor(X,Z) :- parent(X,Y),ancestor(Y,Z).
(Recursive Rule)
MORE FEATURES [CTD...]
 Lists- important data structure in prolog and are
processed recursively.
Ex-
[a, b, a, a, c, d]
[]
[the, dog]
List= Head + Tail
List= [Head | Tail ]
Ex-
List- [a,b,a] can be represented as
[a | [b,a] ] or
[a, b | [a]] or
[a, b, a | []]
MORE FEATURES [CTD...]
There are several other features in prolog
 Negation
 Assertion and Retract
BEHIND THE SCENES
RESOLUTION AND UNIFICATION
 Resolution
 The resolution principle,
 If C1 and C2 are Horn clauses and the head of C1 matches
one of the terms in the body of C2 then we can replace the
term in C2 with the body of C1.
 Example :
 takes(Saman, cs123).
 classmates(X, Y) :- takes(X, Z), takes(Y, Z).
 classmates(Saman, Y) :- takes(Y, cs123).
RESOLUTION AND UNIFICATION [CTD…]
 Unification
 Prolog associates variables and values using a process
known as unification
 Variables that receive a value are said to be instantiated
 Example :
 The pattern-matching process used to associate variable X
with Saman and Z with cs123 is known as unification.
RESOLUTION AND UNIFICATION [CTD…]
The unification rules for Prolog :
 A constant unifies only with itself.
 Two structures unify if and only if they have the same
functor and the same number of arguments, and the
corresponding arguments unify recursively.
 Example :
 ?- f(a, b) = f(a, b, c).
No
 A variable unifies with anything.
DEPTH FIRST SEARCH
Prolog uses a depth-first search strategy when
traversing the tree.
 Advantage :
 Less memory usage
 Disadvantage :
 Prolog may descend down an endless branch (Black
holes)
TOP DOWN VERSUS BOTTOM UP
 Top Down Control
 we start from the goal and attempt to reach the
hypotheses
 Bottom Up Control
 we start with the hypotheses and attempt to reach the
goal
BOTTOM UP CONTROL
 Example :
 Method for calculating a fibonacci number,
 Starting with the bases cases and accumulating until it
reaches the goal.
fib(0,1). fib(1,1).
fib(N,F) :- N=M+1, M=K+1, fib(M,G),
fib(K,H), F=G+H, N>1.
:-fib(3,F).
N=3, M=2, K=1,
F = G + H
:-fib(2,F).
N=2, M=1, K=0,
F = G + H
:-fib(1,F).
F = 1
:-fib(1,1).
:-fib(0,F).
F = 1
:-fib(0,1).
:-fib(1,F).
F = 1
:-fib(1,1).
COMPILING PROLOG CODE
 A program exists called wamcc, based on the
Warren Abstract Machine.
 C code may then be compiled to produce stand
alone executable.
GNU Prolog logo
STRENGTHS OF PROLOG [CTD…]
 Simplicity of Prolog
 Ability to model certain problems very concisely and
elegantly
 Capability of solving problems that it wasn't
originally designed to solve
 Ability to manipulate symbolic data
STRENGTHS OF PROLOG [CTD…]
Prolog’s strength to manipulate symbolic data:
“There are well-known examples of symbolic
computation whose implementation in other standard
languages took tens of pages of indigestible code.
When the same algorithms were implemented in
Prolog, the result was a crystal-clear program easily
fitting on one page.”
‒ Bratko
WEAKNESSES OF PROLOG
 Lack of structure
 Issues with readability of Prolog code
 serious implications for the readability and
declarativeness of code due to cut predicate (!).
 Inefficiency of Prolog code
 Logical imperfectness
LANGUAGE CLASSIFICATION
HIERARCHY OF LANGUAGES
According to Programming Language Pragmatics by Michael L.
Scott,
 Declarative
 functional
 Lisp/Scheme
 ML
 Haskell
 dataflow
 Id
 Val
 logic
 Prolog
 VisiCalc
HIERARCHY OF LANGUAGES [CTD…]
 Imperative
 von Neumann
 Fortran
 Pascal
 Basic
 C
 Object-Oriented
 Smalltalk
 Eiel
 C++
 Java
SCOPE
 Scope of a variable
 in scope within a clause
 Scope of the head of each clause
 in global scope
 Scope of constants
 in global scope
• Example
1. hates(X,Y) :- friends(X,Z),hates(Z,Y).
2. loves(X,Y) :- hates(X,Z),hates(Y,Z).
TYPE SYSTEMS
 Prolog is dynamically typed.
 At runtime, if a built-in predicate is given the wrong type
of argument an error will occur.
 For example:
1. ?- X is 1+1.
X = 2
Yes
2. ?- X is 1+dog.
uncaught exception: error(type_error(evaluable,dog/0),(is)/2)
TYPE SYSTEMS [CTD…]
 A term can be quarried to find out its type with
Prolog in the following way:
 ?- number(5).
yes
 ?- number(dog).
no
 ?- var(X).
yes
 ?- var(5).
no
BINDINGS
 Variable Cells
 A variable is stored in a single heap cell.
 This cell contains a tag and a store address of the that
which it is bound to.
 Unbound variables,
 by convention, point at their own cell.
3 REF 5
2 REF 2
BINDINGS [CTD…]
 Structure Cells
 A non-variable term is stored in a structure cell.
 For a structure f(t1:::tn), there will be n + 2 cells in the
heap.
0 STR 1
1 f/n
2 REF 2
…… ……
…… ……
n + 1 REF n + 1
EXAMPLES
1. Implementing the Towers of Hanoi problem
2. Implementing a non-deterministic finite state
automaton
TOWERS OF HANOI
 In this puzzle, we have three pegs and several
disks, initially stacked from largest to smallest on
the left peg
 Our goal is to move the entire tower to the
middle peg
 We can only move one disk at a time
 We can use right peg to temporally hold the
disks
 We can never place a larger disk on a smaller
disk in any peg
A B C
A B C
A B C
A B C
A B C
A B C
A B C
A B C
Recursive solution
- Move N - 1 discs from stack 1 to 3 with the help of stack
2.
- Move the Nth disc from 1 to 2
- Move N - 1 discs from stack 3 to 2 with the help of stack
1
Implementation in Prolog
hanoi(N) :- dohanoi(N, 1, 2, 3).
dohanoi(0, _ , _ , _ ) :- !.
dohanoi(N, A, B, C) :- N_1 is N-1,
dohanoi(N_1, A, C, B),
moveit(A, B),
dohanoi(N_1, C, B, A).
moveit(F, T) :- write([move, F, -->, T]), nl.
Output when n=3
?- hanoi(3).
[move,1,-->,2]
[move,1,-->,3]
[move,2,-->,3]
[move,1,-->,2]
[move,3,-->,1]
[move,3,-->,2]
[move,1,-->,2]
yes
Output when n=4
?- hanoi(4).
[move,1,-->,3]
[move,1,-->,2]
[move,3,-->,2]
[move,1,-->,3]
[move,2,-->,1]
[move,2,-->,3]
[move,1,-->,3]
[move,1,-->,2]
[move,3,-->,2]
[move,3,-->,1]
[move,2,-->,1]
[move,3,-->,2]
[move,1,-->,3]
[move,1,-->,2]
[move,3,-->,2]
yes
NON-DETERMINISTIC FINITE STATE
AUTOMATON
final(s3).
trans(s1,a,s1).
trans(s1,a,s2).
trans(s1,b,s1).
trans(s2,b,s3).
trans(s3,b,s4).
silent(s2,s4).
silent(s3,s1).
accepts(State,[]) :- final(State).
accepts(State, [X|Rest]) :- trans(State,X,State1),
accepts(State1,Rest).
accepts(State, String) :- silent(State,State1),
accepts(State1,String).
Questions we can ask
1) Whether a given string is accepted by the automaton
?- accepts(s1,[a,a,a,b]).
yes
Questions we can ask (cont.)
2) What states can we start in if we accept a certain
string?
?- accepts(S,[a,b]).
S = s1;
S = s3
Questions we can ask (cont.)
3) What are all strings of length 3 that can be accepted by
the automaton?
?- accepts(s1,[X1,X2,X3]).
X1 = a
X2 = a
X3 = a;
X1 = b
X2 = a
X3 = b
yes
CONCLUSION
 Prolog is the most popular language of the logic
programing languages.
 It is a declarative logic programming language
 It is goal based language, it has automatic
backtracking and uses recursion.
THANK YOU

More Related Content

What's hot

Predicate logic_2(Artificial Intelligence)
Predicate logic_2(Artificial Intelligence)Predicate logic_2(Artificial Intelligence)
Predicate logic_2(Artificial Intelligence)SHUBHAM KUMAR GUPTA
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicManjula V
 
First order predicate logic (fopl)
First order predicate logic (fopl)First order predicate logic (fopl)
First order predicate logic (fopl)chauhankapil
 
Prolog (present)
Prolog (present) Prolog (present)
Prolog (present) Melody Joey
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardAnimesh Chaturvedi
 
I.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AII.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AIvikas dhakane
 
Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Ziyauddin Shaik
 
Knowledge engg using & in fol
Knowledge engg using & in folKnowledge engg using & in fol
Knowledge engg using & in folchandsek666
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithmsabiya sabiya
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 

What's hot (20)

First order logic
First order logicFirst order logic
First order logic
 
Predicate logic_2(Artificial Intelligence)
Predicate logic_2(Artificial Intelligence)Predicate logic_2(Artificial Intelligence)
Predicate logic_2(Artificial Intelligence)
 
L03 ai - knowledge representation using logic
L03 ai - knowledge representation using logicL03 ai - knowledge representation using logic
L03 ai - knowledge representation using logic
 
First order predicate logic (fopl)
First order predicate logic (fopl)First order predicate logic (fopl)
First order predicate logic (fopl)
 
Prolog (present)
Prolog (present) Prolog (present)
Prolog (present)
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
5 csp
5 csp5 csp
5 csp
 
I.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AII.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AI
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems Webinar : P, NP, NP-Hard , NP - Complete problems
Webinar : P, NP, NP-Hard , NP - Complete problems
 
AI-09 Logic in AI
AI-09 Logic in AIAI-09 Logic in AI
AI-09 Logic in AI
 
Knowledge engg using & in fol
Knowledge engg using & in folKnowledge engg using & in fol
Knowledge engg using & in fol
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithm
 
Prolog
PrologProlog
Prolog
 
Reasoning in AI
Reasoning in AIReasoning in AI
Reasoning in AI
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 

Similar to Introduction to Prolog

Threshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random PermutationsThreshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random PermutationsAleksandr Yampolskiy
 
A Distributed Tableau Algorithm for Package-based Description Logics
A Distributed Tableau Algorithm for Package-based Description LogicsA Distributed Tableau Algorithm for Package-based Description Logics
A Distributed Tableau Algorithm for Package-based Description LogicsJie Bao
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues listsJames Wong
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
StacksqueueslistsFraboni Ec
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsYoung Alista
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsTony Nguyen
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues listsHarry Potter
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
XtremeDistil: Multi-stage Distillation for Massive Multilingual Models
XtremeDistil: Multi-stage Distillation for Massive Multilingual ModelsXtremeDistil: Multi-stage Distillation for Massive Multilingual Models
XtremeDistil: Multi-stage Distillation for Massive Multilingual ModelsSubhabrata Mukherjee
 
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)Pierre Schaus
 
DeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingDeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingThomas Winters
 
Recent Advances in Natural Language Processing
Recent Advances in Natural Language ProcessingRecent Advances in Natural Language Processing
Recent Advances in Natural Language ProcessingApache MXNet
 
Dedalo, looking for Cluster Explanations in a labyrinth of Linked Data
Dedalo, looking for Cluster Explanations in a labyrinth of Linked DataDedalo, looking for Cluster Explanations in a labyrinth of Linked Data
Dedalo, looking for Cluster Explanations in a labyrinth of Linked DataVrije Universiteit Amsterdam
 
Ontology-mediated query answering with data-tractable description logics
Ontology-mediated query answering with data-tractable description logicsOntology-mediated query answering with data-tractable description logics
Ontology-mediated query answering with data-tractable description logicsINRIA-CEDAR
 
SP14 CS188 Lecture 2 -- Uninformed Search.pptx
SP14 CS188 Lecture 2 -- Uninformed Search.pptxSP14 CS188 Lecture 2 -- Uninformed Search.pptx
SP14 CS188 Lecture 2 -- Uninformed Search.pptxAnimeGuru1
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"Sergei Winitzki
 
Dsm as theory building
Dsm as theory buildingDsm as theory building
Dsm as theory buildingClarkTony
 

Similar to Introduction to Prolog (20)

Logic Programming and ILP
Logic Programming and ILPLogic Programming and ILP
Logic Programming and ILP
 
Threshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random PermutationsThreshold and Proactive Pseudo-Random Permutations
Threshold and Proactive Pseudo-Random Permutations
 
A Distributed Tableau Algorithm for Package-based Description Logics
A Distributed Tableau Algorithm for Package-based Description LogicsA Distributed Tableau Algorithm for Package-based Description Logics
A Distributed Tableau Algorithm for Package-based Description Logics
 
Stack squeues lists
Stack squeues listsStack squeues lists
Stack squeues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacksqueueslists
StacksqueueslistsStacksqueueslists
Stacksqueueslists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Stacks queues lists
Stacks queues listsStacks queues lists
Stacks queues lists
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
XtremeDistil: Multi-stage Distillation for Massive Multilingual Models
XtremeDistil: Multi-stage Distillation for Massive Multilingual ModelsXtremeDistil: Multi-stage Distillation for Massive Multilingual Models
XtremeDistil: Multi-stage Distillation for Massive Multilingual Models
 
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)The Concurrent Constraint Programming Research Programmes -- Redux (part2)
The Concurrent Constraint Programming Research Programmes -- Redux (part2)
 
DeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic ProgrammingDeepStochLog: Neural Stochastic Logic Programming
DeepStochLog: Neural Stochastic Logic Programming
 
Recent Advances in Natural Language Processing
Recent Advances in Natural Language ProcessingRecent Advances in Natural Language Processing
Recent Advances in Natural Language Processing
 
Dedalo, looking for Cluster Explanations in a labyrinth of Linked Data
Dedalo, looking for Cluster Explanations in a labyrinth of Linked DataDedalo, looking for Cluster Explanations in a labyrinth of Linked Data
Dedalo, looking for Cluster Explanations in a labyrinth of Linked Data
 
Ontology-mediated query answering with data-tractable description logics
Ontology-mediated query answering with data-tractable description logicsOntology-mediated query answering with data-tractable description logics
Ontology-mediated query answering with data-tractable description logics
 
SP14 CS188 Lecture 2 -- Uninformed Search.pptx
SP14 CS188 Lecture 2 -- Uninformed Search.pptxSP14 CS188 Lecture 2 -- Uninformed Search.pptx
SP14 CS188 Lecture 2 -- Uninformed Search.pptx
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"
 
Dsm as theory building
Dsm as theory buildingDsm as theory building
Dsm as theory building
 

Recently uploaded

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 

Introduction to Prolog

  • 1. INTRODUCTION TO PROLOG - Brian Hutchinson Present by - W.J.A.I.U. Jayaweera- 100227D G.K.M.C Sajeewa- 100470N M.M.D.T.K Wijewardane- 100612E
  • 2. OUTLINE  Introduction  Language Features  More Features  Behind the scenes  Language Classifications  Examples
  • 3. INTRODUCTION  Prolog is the most popular language of the logic programing languages.  It is goal based language, it has automatic backtracking and uses recursion.  Prolog stands for programmation en logique", or programming in logic."
  • 4. INTRODUCTION [CTD...]  Invented by Alain Colmerauer and Philippe Roussel of the Groupe d'Intelligence Articielle (GIA) in early 1970s.  The earliest Prolog interpreter was written in Algol in1972 by Roussel.  The official ISO standard for Prolog was published in 1996.
  • 5. CURRENT IMPLEMENTATIONS  There are several popular Prolog interpreters available. They include:  GNU Prolog (http://pauillac.inria.fr/vdiaz/gnu- prolog/)  SWI-Prolog (http://www.swi-prolog.org/)  Visual Prolog (http://www.visual- prolog.com/vip6/Download/Default.htm)  BProlog (http://www.cad.mse.kyutech.ac.jp/people/zhou/bpr olog.html)
  • 7. LANGUAGE FEATURES[CTD...]  Facts-Something that is declared to always be true. Ex-father(bob,george) mother(alice,jeffrey)  Rules- Something which is true only if all conditions (sub goals) on the right are true Ex- parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandparent(X,Z) :- parent(X,Y), parent(Y,Z).
  • 8. LANGUAGE FEATURES[CTD...]  Questions- Something which asks from the system Ex- ?-ancestor(X,cindy),sibling(X,jeffrey)  Clauses- Something which has a head and a body.  Fact = head  Rules = head + body  Question=body
  • 9. LANGUAGE FEATURES [CTD...] Ex-  uncle(X,Y) :- sibling(X,Z), parent(Z,Y). (head) (body)  ?- ancestor(X,cindy),sibling(X,jeffrey) (body)  father(john,mary) (head)
  • 10. LANGUAGE FEATURES[CTD...]  Terms - term can be number, constant (Albert) or variable(X).  Compound Terms- data structure, convenient way of grouping relevant data together Ex- fullname( joe,brown ) student( fullname( joe,brown ), 25 ) Full name , Student are called as functors.
  • 11. MORE FEATURES  Order- order matters in prolog, order is top to bottom. Ex- dog(rover). dog(duke). We enter the following question: ?- dog(X). Answer : X = rover  Order is important for the efficiency and correctness of the program.
  • 12. MORE FEATURES[CTD...]  Backtracking-  prolog stores data in tree.  It uses depth first search to find answers.  In its attempt to prove a goal, Prolog sometimes goes down a dead-end, at which point it needs to backtrack.ck
  • 13. MORE FEATURES[CTD...] Ex- Sample Code Facts- green(poisonivy). green(broccoli). edible(icecream). edible(broccoli). Question- ?- green(X),edible(X). Answer- X = broccoli
  • 14. MORE FEATURES[CTD...] Execution trace- ?- green(X),edible(X). 1 1 Call: green(poisoniyy) ? 1 1 Exit: green(poisonivy) ? 2 1 Call: edible(poisonivy) ? 2 1 Fail: edible(poisonivy) ? 1 1 Redo:green(poisonivy)1 1 1 Call: green(broccoli) ? 1 1 Exit: green(broccoli) ? 2 1 Call: edible(broccoli) ? 2 1 Exit: edible(broccoli) ? Green(p) Edible(p) Green(b) Root Edible(b) Answer
  • 15. MORE FEATURES[CTD...]  Cuts- The cut is denoted by exclamation mark “!” and disallows prolog from backtracking past that point.  Ex- With Out Cut grade(G,a) :- G > 90. grade(G,b) :- G > 80. grade(G,c) :- G > 70. grade(G,d) :- G > 60. grade(_,f). With Cut grade2(G,a) :- G > 90, !. grade2(G,b) :- G > 80, !. grade2(G,c) :- G > 70, !. grade2(G,d) :- G > 60, !. grade2(_,f).
  • 16. MORE FEATURES[CTD...] Forced Backtrack With Grade ?- grade(83,X) X = b; X = c; X = d; X = f yes Forced Backtrack With Grade2 ?- grade2(83,X) X = b; yes Forced back tracking with semi colon. Types of Cuts Red Cuts Green Cuts
  • 17. MORE FEATURES[CTD...] Recursion- like functional languages prolog uses recursion. Ex- ancestor(X,Z) :- parent(X,Z). (Base Case) ancestor(X,Z) :- parent(X,Y),ancestor(Y,Z). (Recursive Rule)
  • 18. MORE FEATURES [CTD...]  Lists- important data structure in prolog and are processed recursively. Ex- [a, b, a, a, c, d] [] [the, dog] List= Head + Tail List= [Head | Tail ] Ex- List- [a,b,a] can be represented as [a | [b,a] ] or [a, b | [a]] or [a, b, a | []]
  • 19. MORE FEATURES [CTD...] There are several other features in prolog  Negation  Assertion and Retract
  • 21. RESOLUTION AND UNIFICATION  Resolution  The resolution principle,  If C1 and C2 are Horn clauses and the head of C1 matches one of the terms in the body of C2 then we can replace the term in C2 with the body of C1.  Example :  takes(Saman, cs123).  classmates(X, Y) :- takes(X, Z), takes(Y, Z).  classmates(Saman, Y) :- takes(Y, cs123).
  • 22. RESOLUTION AND UNIFICATION [CTD…]  Unification  Prolog associates variables and values using a process known as unification  Variables that receive a value are said to be instantiated  Example :  The pattern-matching process used to associate variable X with Saman and Z with cs123 is known as unification.
  • 23. RESOLUTION AND UNIFICATION [CTD…] The unification rules for Prolog :  A constant unifies only with itself.  Two structures unify if and only if they have the same functor and the same number of arguments, and the corresponding arguments unify recursively.  Example :  ?- f(a, b) = f(a, b, c). No  A variable unifies with anything.
  • 24. DEPTH FIRST SEARCH Prolog uses a depth-first search strategy when traversing the tree.  Advantage :  Less memory usage  Disadvantage :  Prolog may descend down an endless branch (Black holes)
  • 25. TOP DOWN VERSUS BOTTOM UP  Top Down Control  we start from the goal and attempt to reach the hypotheses  Bottom Up Control  we start with the hypotheses and attempt to reach the goal
  • 26. BOTTOM UP CONTROL  Example :  Method for calculating a fibonacci number,  Starting with the bases cases and accumulating until it reaches the goal. fib(0,1). fib(1,1). fib(N,F) :- N=M+1, M=K+1, fib(M,G), fib(K,H), F=G+H, N>1. :-fib(3,F). N=3, M=2, K=1, F = G + H :-fib(2,F). N=2, M=1, K=0, F = G + H :-fib(1,F). F = 1 :-fib(1,1). :-fib(0,F). F = 1 :-fib(0,1). :-fib(1,F). F = 1 :-fib(1,1).
  • 27. COMPILING PROLOG CODE  A program exists called wamcc, based on the Warren Abstract Machine.  C code may then be compiled to produce stand alone executable. GNU Prolog logo
  • 28. STRENGTHS OF PROLOG [CTD…]  Simplicity of Prolog  Ability to model certain problems very concisely and elegantly  Capability of solving problems that it wasn't originally designed to solve  Ability to manipulate symbolic data
  • 29. STRENGTHS OF PROLOG [CTD…] Prolog’s strength to manipulate symbolic data: “There are well-known examples of symbolic computation whose implementation in other standard languages took tens of pages of indigestible code. When the same algorithms were implemented in Prolog, the result was a crystal-clear program easily fitting on one page.” ‒ Bratko
  • 30. WEAKNESSES OF PROLOG  Lack of structure  Issues with readability of Prolog code  serious implications for the readability and declarativeness of code due to cut predicate (!).  Inefficiency of Prolog code  Logical imperfectness
  • 32. HIERARCHY OF LANGUAGES According to Programming Language Pragmatics by Michael L. Scott,  Declarative  functional  Lisp/Scheme  ML  Haskell  dataflow  Id  Val  logic  Prolog  VisiCalc
  • 33. HIERARCHY OF LANGUAGES [CTD…]  Imperative  von Neumann  Fortran  Pascal  Basic  C  Object-Oriented  Smalltalk  Eiel  C++  Java
  • 34. SCOPE  Scope of a variable  in scope within a clause  Scope of the head of each clause  in global scope  Scope of constants  in global scope • Example 1. hates(X,Y) :- friends(X,Z),hates(Z,Y). 2. loves(X,Y) :- hates(X,Z),hates(Y,Z).
  • 35. TYPE SYSTEMS  Prolog is dynamically typed.  At runtime, if a built-in predicate is given the wrong type of argument an error will occur.  For example: 1. ?- X is 1+1. X = 2 Yes 2. ?- X is 1+dog. uncaught exception: error(type_error(evaluable,dog/0),(is)/2)
  • 36. TYPE SYSTEMS [CTD…]  A term can be quarried to find out its type with Prolog in the following way:  ?- number(5). yes  ?- number(dog). no  ?- var(X). yes  ?- var(5). no
  • 37. BINDINGS  Variable Cells  A variable is stored in a single heap cell.  This cell contains a tag and a store address of the that which it is bound to.  Unbound variables,  by convention, point at their own cell. 3 REF 5 2 REF 2
  • 38. BINDINGS [CTD…]  Structure Cells  A non-variable term is stored in a structure cell.  For a structure f(t1:::tn), there will be n + 2 cells in the heap. 0 STR 1 1 f/n 2 REF 2 …… …… …… …… n + 1 REF n + 1
  • 39. EXAMPLES 1. Implementing the Towers of Hanoi problem 2. Implementing a non-deterministic finite state automaton
  • 40. TOWERS OF HANOI  In this puzzle, we have three pegs and several disks, initially stacked from largest to smallest on the left peg
  • 41.  Our goal is to move the entire tower to the middle peg  We can only move one disk at a time  We can use right peg to temporally hold the disks  We can never place a larger disk on a smaller disk in any peg
  • 42. A B C
  • 43. A B C
  • 44. A B C
  • 45. A B C
  • 46. A B C
  • 47. A B C
  • 48. A B C
  • 49. A B C
  • 50. Recursive solution - Move N - 1 discs from stack 1 to 3 with the help of stack 2. - Move the Nth disc from 1 to 2 - Move N - 1 discs from stack 3 to 2 with the help of stack 1
  • 51. Implementation in Prolog hanoi(N) :- dohanoi(N, 1, 2, 3). dohanoi(0, _ , _ , _ ) :- !. dohanoi(N, A, B, C) :- N_1 is N-1, dohanoi(N_1, A, C, B), moveit(A, B), dohanoi(N_1, C, B, A). moveit(F, T) :- write([move, F, -->, T]), nl.
  • 52. Output when n=3 ?- hanoi(3). [move,1,-->,2] [move,1,-->,3] [move,2,-->,3] [move,1,-->,2] [move,3,-->,1] [move,3,-->,2] [move,1,-->,2] yes
  • 53. Output when n=4 ?- hanoi(4). [move,1,-->,3] [move,1,-->,2] [move,3,-->,2] [move,1,-->,3] [move,2,-->,1] [move,2,-->,3] [move,1,-->,3] [move,1,-->,2] [move,3,-->,2] [move,3,-->,1] [move,2,-->,1] [move,3,-->,2] [move,1,-->,3] [move,1,-->,2] [move,3,-->,2] yes
  • 56. accepts(State,[]) :- final(State). accepts(State, [X|Rest]) :- trans(State,X,State1), accepts(State1,Rest). accepts(State, String) :- silent(State,State1), accepts(State1,String).
  • 57. Questions we can ask 1) Whether a given string is accepted by the automaton ?- accepts(s1,[a,a,a,b]). yes
  • 58. Questions we can ask (cont.) 2) What states can we start in if we accept a certain string? ?- accepts(S,[a,b]). S = s1; S = s3
  • 59. Questions we can ask (cont.) 3) What are all strings of length 3 that can be accepted by the automaton? ?- accepts(s1,[X1,X2,X3]). X1 = a X2 = a X3 = a; X1 = b X2 = a X3 = b yes
  • 60. CONCLUSION  Prolog is the most popular language of the logic programing languages.  It is a declarative logic programming language  It is goal based language, it has automatic backtracking and uses recursion.

Editor's Notes

  1. A Prolog program consists of facts, rules (relationships), and questions (goals),all of which are clauses.
  2. The first line states that bob is the father of george, the second line states that alice is the mother of jeffrey. Facts represents the data base of the programme.The comma denotes the logical and relationship. For example, the third rule says that X is the grandparent of Z if X is the parent of some Y, and Y is the parentof Z. Notice that in Prolog variables begin with an upper-case letter. Rules based on facts. Complex rules are based on simple rules
  3. These question (or goal) is asking: is there somebody, X, who is an ancestor of cindy, where this same X is a sibling to jerey.Prolog will combine facts and rules to answer this question.Facts rules and questions are altogether called as clauses.
  4. 1- rule2-quetsion3-fact
  5. Prolog variable are upper case letters.Compound terms are user as data structures. 1- contains first name and last name 2- full name + age
  6. In a pure logic programming language, order does not affect the meaning ofthe program. However, as you will see in later section, in Prolog order is veryimportant for the effciency and even the correctness of the program
  7. The semi-colon, in a sense, tells Prolog to imagine that the solution it came upwith had failed. Prolog then backtracks and attempts to find another solution.In grade 2 forced backtracking did not work due to the cut.
  8. Programmers may add or remove clauses from a program dynamically, withassert and retract. Retract(C) removes the firrst instance of clause.Assert typically has two forms, asserta(C) and assertz(C), which insert clause C at the beginning and end of the program, respectively
  9. In this section we consider a variety of topics about, how Prolog does what it does, strengths weaknesses
  10. These are two approaches use with Prolog when proving goalsHorn clauses is a clause who’s,head is true if the body is true
  11. B) The process use to associate variables and values in Prolog is unification.A) In Prolog,saying that two terms are equal is saying that they are unifiable.
  12. A) If the other thing has a value, then the variable is instantiated. If the other thing is an uninstantiated variable, then the two variables are associated in such a way that if either is given a value later, that value will be shared by both.
  13. B) The process of backtracking which was mentioned, is essentially a traversal of a tree.A) DisadvantageEndless branches will, preventing it from ever finding the solution
  14. B) There are two principal ways in which,sub goals can be generated: top-down - recursive and bottom-up - iterativeA) Prolog uses the technique of bottom-up control
  15. B) Bottom-up method, more iterative more efficient
  16. B) wamcc compiles Prolog code to C code
  17. 3rd point - The flexibility to choose which variables to instantiate in a goal make4th point – This makes it a very useful tool for artificial intelligence applications
  18. In this section Prolog will be classified according to, its place in the language hierarchy,its scope, bindings and type systems.
  19. B) Main two types: Declarative ImperativeA) Prolog is a Declarative – Logic language
  20. B) In Prolog there are no,procedures other typical blocks of code therefore concept of scope is a little different for Prolog1) Each X refers to the same thing in first lineThe variable X in line one has nothing to do with the X in line two2) The head of each clause can be either a fact or part of a ruleThe predicate hates(X;Z) in second line refers to the same predicate in the first line
  21. A) In the first case checks whether, 1+1 equals 2In the second case trying to checks solution for 1+dog, as argument type dog doesn’t match with the given predicate, Prolog produces an exceptionHowever, this program can be compiled with no problem, GNU Prolog compiler does no type-checking at compile time.All type checking would have to occur at run-time.
  22. B) Terms in Prolog are stored in an “addressable heap”.There are, variable cells structure cells
  23. A) f is the functorti are the remaining items in this data structure, First, there will be a structure cell (denoted STR), which points to the address of the functor cell.Structure cell, and the functor cell need not be contiguous.The functor cell will always be followed by n contiguous blocks containing the cells of the terms ti.
  24. 2 -> In theory of computing
  25. Ancient, famousEverybody knows itJust to refresh ur memory
  26. In this example, goal is moving the entire left stack to middle stack3 rules
  27. We are done.But this is for n equals 3For a general solution we have to consider an arbitrary N value
  28. In the general solution that can be implemented to solve using a computer, there are 3 steps involved1Pls remember our final goal is to move the stack 1 to stack 2 not to 323Recursive solution, we considering an arbitrary N valueMoving N-1 -> similar and smaller versionDoesn’t violate the rule: larger cant be on smaller
  29. 1st rule -> start2nd -> terminate. Stopping condition3N A B C F T are variables4Write -> implemented function in prolog. Like println in javaNothing more is needed
  30. Same as we did it manually
  31. 2nd exampleImplementing ----- in prolog
  32. Have to define some FactsConstants not variablesDefines this state diagramStore it in prolog
  33. Basic function of FA to accept strings 1st -> no string
  34. What we can get from this implementation
  35. Declarative----- imperative