SlideShare a Scribd company logo
Seven Languages in Seven Months
Language 3: Prolog
Raymond P. de Lacaze
Patch
raymond.delacaze@patch.com
01/30/13
Overview
• Part 1: Introduction to Prolog
Chapter 4 of Seven Languages in Seven Weeks
• Part 2: Advanced Topics
Chapters X-Y of The Art of Prolog
• Part 3: Prolog Today
Prolog Introduction
• Prolog is a declarative language
• Prolog is logic programming language
• Invented in 1972 by Colmerauer & Roussel
– Edinburg Prolog
– Marseilles Prolog
• Initially used for Natural Language Processing
• Programs consist of fact & rules
• Fact is a clause in FOPC
• Rule is an inference: B A1,…,An
• Use queries to run programs and perform retrievals
The Logic Programming Model
• Logic Programming is an abstract model of computation.
• Lambda Calculus is another abstract model of
computation.
• Prolog is a particular implementation of the logic
programming model in much the same way that Clojure
and Haskell are particular implementation of the lambda
calculus.
• OPS5 is another implementation of the logic programming
model.
• The use of mathematical logic to represent and execute
computer programs is also a feature of the lambda
calculus
• Prolog is classified as a functional language (wikipedia)
Logic Programming Paradigm
• A program is logical description of your
problem from which a solution is logically
derivable
• The execution of a program is very much like
the mathematical proof of a theorem
• Where’s my program?
• N! is (N-1)! times N
• 0! is 1
Prolog Facts
• Facts: <predicate>(<arg1>,…,<argN>)
• Example: likes(mary, john)
• Constants must be in lowercase
• Variables must be in uppercase or start with
an underscore.
• Example: eats(mikey, X)
• Example: believes(peter, likes(mary, john))
Basic Inferences & Variables
likes(john, cheese)
likes(mary, cheese)
likes(bob, meat)
similar(X,Y) :- likes(X,Z), likes(Y,Z)
Note: You can use *‘<filename/pathname>’+. to compile and load files
GNU Prolog 1.4.1
By Daniel Diaz
Copyright (C) 1999-2012 Daniel Diaz
| ?- ['C:ProjectsLanguagescodePrologsimilar.pl'].
compiling C:/Projects/Languages/code/Prolog/similar.pl for byte code...
C:/Projects/Languages/code/Prolog/similar.pl compiled, 4 lines read - 935
bytes written,
(16 ms) yes
Filling in the Blanks
| ?- similar(john, mary).
yes
| ?- similar(john, bob).
no
| ?- similar(mary, X).
X = john ?
yes
| ?- similar(X, Y).
X = john
Y = john ? ;
X = john
Y = mary ? ;
X = mary
Y = john ? ;
X = mary
Y = mary ?
Note: can use ; and a to get next or all answers
Map Coloring (1)
different(red, green).
different(red, blue).
different(green, red).
different(green, blue).
different(blue, red).
different(blue, green).
coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-
different(Mississippi, Tennessee),
different(Mississippi, Alabama),
different(Alabama, Tennessee),
different(Alabama, Mississippi),
different(Alabama, Georgia),
different(Alabama, Florida),
different(Georgia, Florida),
different(Georgia, Tennessee).
Map Coloring (2)
| ?- ['c:projectslanguagescodeprologmap.pl'].
compiling c:/projects/languages/code/prolog/map.pl for byte code...
c:/projects/languages/code/prolog/map.pl compiled, 16 lines read - 1716 bytes written, 16 ms
yes
| ?- coloring(Alabama, Mississippi, Georgia, Tennessee, Florida).
Alabama = blue
Florida = green
Georgia = red
Mississippi = red
Tennessee = green ?
(16 ms) yes
Unification (1)
• The Unification Algorithm is a famous algorithm
from the field of AI, often used in theorem
proving, game playing, planning, etc…
• It can loosely be thought of as an algorithm that
tries to make to non-grounded terms the same.
• P(X, 2) = P(1, Y)  X=1 & Y=2  P(1, 2)
• P(X, X) = P(Y, 5)  X=5 & Y=5  P (5, 5)
• P(X, Y) = P(2, Z)  X=2 & Y=Z  P (2, Z)
• See Artificial Intelligence (Russell & Norvig)
Prolog Rules
• Rules: <head> :- <body>
• Head: Single clause typically with variables
• Body: Conjunction of goals with variables
• Examples:
ancestor(X,Y) :- parent(X,Y)
ancestor(X,Y) :- parent(X,Z), parent(Z,Y)
ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y)
A Recursive Example(1)
parent(p1, p2).
parent(p2, p3).
parent(p3, p4).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
| ?- ['c:projectslanguagescodeprologancestor.pl'].
compiling c:/projects/languages/code/prolog/ancestor.pl for byte code...
c:/projects/languages/code/prolog/ancestor.pl compiled, 5 lines read -
818 bytes written, 13 ms
yes
A Recursive Example(2)
| ?- trace.
The debugger will first creep -- showing everything (trace)
yes
{trace}
| ?- ancestor(p1, p4).
1 1 Call: ancestor(p1,p4) ?
2 2 Call: parent(p1,p4) ?
2 2 Fail: parent(p1,p4) ?
2 2 Call: parent(p1,_80) ?
2 2 Exit: parent(p1,p2) ?
3 2 Call: ancestor(p2,p4) ?
4 3 Call: parent(p2,p4) ?
4 3 Fail: parent(p2,p4) ?
4 3 Call: parent(p2,_129) ?
4 3 Exit: parent(p2,p3) ?
5 3 Call: ancestor(p3,p4) ?
6 4 Call: parent(p3,p4) ?
6 4 Exit: parent(p3,p4) ?
5 3 Exit: ancestor(p3,p4) ?
3 2 Exit: ancestor(p2,p4) ?
1 1 Exit: ancestor(p1,p4) ?
true ?
(63 ms) yes
Using Rules in Both Directions
// Find all ancestors
| ?- ancestor(X, p4).
X = p3 ? ;
X = p1 ? ;
X = p2 ? ;
no
// Find all descendants
| ?- ancestor(p1, X).
X = p2 ? ;
X = p3 ? ;
X = p4 ? ;
no
Lists and Tuples (1)
• List are denoted by comma-separated values in square
brackets. i.e. [1, 2, 3]
• Tuples are denoted by comma-separated values in
parentheses. i.e. (1, 2, 3)
| ?- [1, 2, 3] = [X, Y, Z].
X = 1
Y = 2
Z = 3
yes
Accessing Elements of a List
| ?- [1, 2, 3] = [X | Y].
X = 1
Y = [2,3]
| ?- [1, 2, 3] = [_, X | Y].
X = 2
Y = [3]
Lists and Math (1)
count(0, []).
count(Count, [Head|Tail]) :-
count(TailCount, Tail), Count is TailCount + 1.
sum(0, []).
sum(Total, [Head|Tail]) :-
sum(Sum, Tail), Total is Head + Sum.
average(Average, List) :-
sum(Sum, List),
count(Count, List),
Average is Sum/Count.
Lists and Math (2)
| ?- sum(S, [1,2,3]).
1 1 Call: sum(_17,[1,2,3]) ?
2 2 Call: sum(_92,[2,3]) ?
3 3 Call: sum(_116,[3]) ?
4 4 Call: sum(_140,[]) ?
4 4 Exit: sum(0,[]) ?
5 4 Call: _168 is 3+0 ?
5 4 Exit: 3 is 3+0 ?
3 3 Exit: sum(3,[3]) ?
6 3 Call: _197 is 2+3 ?
6 3 Exit: 5 is 2+3 ?
2 2 Exit: sum(5,[2,3]) ?
7 2 Call: _17 is 1+5 ?
7 2 Exit: 6 is 1+5 ?
1 1 Exit: sum(6,[1,2,3]) ?
S = 6 ?
Solving Sudoku (1)
valid([]).
valid([Head|Tail]) :-
valid(Tail), fd_all_different(Head).
sudoku(S11, S12, S13, S14,
S21, S22, S23, S24,
S31, S32, S33, S34,
S41, S42, S43, S44,
Board) :-
Board = [S11, S12, S13, S14,
S21, S22, S23, S24,
S31, S32, S33, S34,
S41, S42, S43, S44, ],
fd_domain(Board, 1, 4),
Row1 = [S11, S12, S13, S14],
Row2 = [S21, S22, S23, S24],
Row3 = [S31, S32, S33, S34],
Row4 = [S41, S42, S43, S44],
Col1 = [S11, S21, S31, S41],
Col2 = [S12, S22, S32, S42],
Col3 = [S13, S23, S33, S43],
Col4 = [S14, S24, S34, S44],
Square1 = [S11, S12, S21, S22],
Square2 = [S13, S14, S23, S24],
Square3 = [S31, S32, S41, S42,],
Square4 = [S33, S34, S43, S44,],
valid([Row1, Row2, Row3, Row4,]),
valid([Col1, Col2, Col3, Col4, ]),
valid([Square1, Square2, Square3, Square4]).
Solving Sudoku (2)
| ?- sudoku(_, _, 2, 3,
_, _, _, _,
_, _ ,_, _,
3, 4, _, _,
Solution).
Solution = [4, 1, 2, 3,
2, 3, 4, 1,
1, 2, 3, 4,
3, 4, 1, 2]
Solving Sudoku (3)
• Finite Domain variables: A new type of data is introduced: FD
variables which can only take values in their domains. The
initial domain of an FD variable is 0..fd_max_integer where
fd_max_integer represents the greatest value that any FD
variable can take.
• fd_domain(Board, 1, 4).
Used to specify the range of values of each Sudoku cell.
• fd_all_different(X).
Used to specify that all elemts in the list must have distinct values.
Part 2: Advanced Topics
• The Art of Prolog, Sterling and Shapiro, MIT Press, 1986
• Structure Inspection
• Meta-Logical Predicates
• Cuts (and Negation)
• Extra-Logical Predicates
Structure Inspection
| ?- functor(father(tom, harry), P, A).
A = 2
P = father
Yes
| ?- arg(1,father(tom, harry), A1).
A1 = tom
Yes
| ?- arg(2,father(tom, harry), A2).
A2 = harry
yes
| ?- functor(X, father, 2).
X = father(_, _)
Yes
| ?- father(tom, harry) =.. [X, Y, Z].
X = father
Y = tom
Z = harry
yes
| ?- X =.. [father, tom, harry].
X = father(tom, harry)
yes
| ?- X =.. [father, tom, harry], assertz(X).
X = father(tom, harry)
yes
| ?- father(tom, harry).
yes
functor, arg and =..
Meta-Logical Predicates
• Outside scope of first-order logic
• Query and affect the state of the proof
• Treat variables as objects
• Convert data structures to goals
• Type Predicates:
• var(<term>)
• nonvar(<term>)
• Variables as objects: freeze & melt
• Dynamically Affecting the Knowledge Base
• assert(<goal>)
• retract(<goal>)
• The Meta-Variable Facility: call(<goal>)
• Memoization: lemma(<goal>)
Extra-Logical Predicates
• These achieve side-effects as a result of being
logically true
• Three types of extra-logical predicates
• Input / Output
– read & write
• Accessing and manipulating the program
– clause(Head, Body)
– assert (X)
– retract(X)
• Interfacing to the Operating System
Cuts (1)
• Predicate cut or ! affects procedural behavior
• Main purpose is to reduce the search space
• It’s use is controversial: purity vs. efficiency
• Green Cuts: Express determinism
• Consider merging two sorted lists
• Only one of X<Y, X=Y or X>Y can succeed
• Once one succeeds, no need for the others.
Merging without Cuts
merge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, merge(Xs, [Y|Ys], Zs).
merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, merge(Xs, Ys, Zs).
merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, merge([X|Xs], Ys, Zs).
merge(Xs, [], Xs).
merge([], Ys, Ys).
| ?- merge([1,3,5],[2,3], Xs).
Xs = [1,2,3,3,5] ?
yes
Merging with Cuts
merge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, !, merge(Xs, [Y|Ys], Zs).
merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, !, merge(Xs, Ys, Zs).
merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, !, merge([X|Xs], Ys, Zs).
merge(Xs, [], Xs) :- !.
merge([], Ys, Ys) :- !.
?- merge([1,3,5],[2,3], Xs).
Xs = [1,2,3,3,5]
yes
The Effect of Cuts
merge([1,3,4],[2, 5], Xs).
1 1 Call: merge([1,3,4],[2,5],_27) ?
2 2 Call: 1>2 ?
2 2 Fail: 1>2 ?
2 2 Call: 1<2 ?
2 2 Exit: 1<2 ?
3 2 Call: merge([3,4],[2,5],_60) ?
4 3 Call: 3>2 ?
4 3 Exit: 3>2 ?
5 3 Call: merge([3,4],[5],_114) ?
6 4 Call: 3>5 ?
6 4 Fail: 3>5 ?
6 4 Call: 3<5 ?
6 4 Exit: 3<5 ?
7 4 Call: merge([4],[5],_168) ?
8 5 Call: 4>5 ?
8 5 Fail: 4>5 ?
8 5 Call: 4<5 ?
8 5 Exit: 4<5 ?
9 5 Call: merge([],[5],_222) ?
9 5 Exit: merge([],[5],[5]) ?
7 4 Exit: merge([4],[5],[4,5]) ?
5 3 Exit: merge([3,4],[5],[3,4,5]) ?
3 2 Exit: merge([3,4],[2,5],[2,3,4,5]) ?
1 1 Exit: merge([1,3,4],[2,5],[1,2,3,4,5]) ?
Xs = [1,2,3,4,5] ?
| ?- merge([1,3,4],[2, 5], Xs).
1 1 Call: merge([1,3,4],[2,5],_27) ?
2 2 Call: 1>2 ?
2 2 Fail: 1>2 ?
2 2 Call: 1<2 ?
2 2 Exit: 1<2 ?
3 2 Call: merge([3,4],[2,5],_60) ?
4 3 Call: 3>2 ?
4 3 Exit: 3>2 ?
5 3 Call: merge([3,4],[5],_114) ?
6 4 Call: 3>5 ?
6 4 Fail: 3>5 ?
6 4 Call: 3<5 ?
6 4 Exit: 3<5 ?
7 4 Call: merge([4],[5],_168) ?
8 5 Call: 4>5 ?
8 5 Fail: 4>5 ?
8 5 Call: 4<5 ?
8 5 Exit: 4<5 ?
9 5 Call: merge([],[5],_222) ?
9 5 Exit: merge([],[5],[5]) ?
7 4 Exit: merge([4],[5],[4,5]) ?
5 3 Exit: merge([3,4],[5],[3,4,5]) ?
3 2 Exit: merge([3,4],[2,5],[2,3,4,5]) ?
1 1 Exit: merge([1,3,4],[2,5],[1,2,3,4,5]) ?
Xs = [1,2,3,4,5]
Prolog Today
• GNU Prolog Compiler
Full I/O capabilities
Complete Socket programming API
Can be linked to C in both directions
• Approx. 20 existing Prolog implementations
• Some other popular ones: SWI-Prolog & Visual Prolog
• http://en.wikipedia.org/wiki/Comparison_of_Prolog_implementations
• http://orgnet.com/inflow3.html
Software for Social Network Analysis & Organizational Network Analysis
Prolog 7-Languages
core.logic
• Clojure core.logic (David Nolen)
This is a Clojure implementation of miniKanren. miniKanren is a Logic Programming library
embedded in Scheme based The Reasoned Schemer by Daniel Friedman.
• Another implementation of the Logic Programming Paradigm
• Presented at Strange Loop 2012
• Very well received by the Clojure community
• https://github.com/clojure/core.logic
AllegroGraph (Franz Inc.)
• AllegroGraph is one the leading graph database and application frameworks
for building Semantic Web applications.
• It can store data and meta-data as trillions of triples
• Query these triples through using PROLOG
• Query these triples using SPARQL (the standard W3C query language)
• AllegroGraph includes support for Federation, Social Network Analysis,
Geospatial capabilities and Temporal reasoning
• AllegroGraph is implemented in Common Lisp & CLOS.
• http://www.franz.com/agraph/allegrograph/
References
Seven Languages in Seven Weeks
Bruce A. Tate, Pragmatic Programmers LLC, 2010
The Art of Prolog: Advanced Programming Techniques
Sterling & Shapiro, MIT Press, 1986
GNU Prolog User Manual
Daniel Diaz, November 2012
Artificial Intelligence: A Modern Approach
Russell & Norvig, Prentice Hall, 1995

More Related Content

PPTX
ProLog (Artificial Intelligence) Introduction
PDF
Logic Programming and ILP
PPTX
Introduction to Prolog
PPT
Artificial intelligence Prolog Language
PDF
10 logic+programming+with+prolog
PPT
Chaps 1-3-ai-prolog
PPT
Prolog 01
PPTX
PROLOG: Introduction To Prolog
ProLog (Artificial Intelligence) Introduction
Logic Programming and ILP
Introduction to Prolog
Artificial intelligence Prolog Language
10 logic+programming+with+prolog
Chaps 1-3-ai-prolog
Prolog 01
PROLOG: Introduction To Prolog

What's hot (19)

PPT
09 logic programming
PPT
Prolog basics
PDF
Logic programming (1)
PPT
Chaps 1-3-ai-prolog
PPT
Introduction to prolog
PPTX
Prolog Programming Language
PPTX
PROLOG: Recursion And Lists In Prolog
PDF
Babar: Knowledge Recognition, Extraction and Representation
PPT
Predlogic
PPT
Introduction to logic and prolog - Part 1
PPT
prolog ppt
PPT
Prolog programming
PPTX
PROLOG: Fact Roles And Queries In Prolog
PPTX
Overview prolog
PPTX
PROLOG: Cuts And Negation In Prolog
PPTX
Prolog & lisp
PPTX
Scheme Programming Language
PPTX
Plc part 4
PPT
Pl vol1
09 logic programming
Prolog basics
Logic programming (1)
Chaps 1-3-ai-prolog
Introduction to prolog
Prolog Programming Language
PROLOG: Recursion And Lists In Prolog
Babar: Knowledge Recognition, Extraction and Representation
Predlogic
Introduction to logic and prolog - Part 1
prolog ppt
Prolog programming
PROLOG: Fact Roles And Queries In Prolog
Overview prolog
PROLOG: Cuts And Negation In Prolog
Prolog & lisp
Scheme Programming Language
Plc part 4
Pl vol1
Ad

Viewers also liked (19)

PPTX
Prolog Programming : Basics
TXT
Prolog Code [Family Tree] by Shahzeb Pirzada
PPTX
Introduction on Prolog - Programming in Logic
PPTX
Clojure 7-Languages
PPTX
Reinforcement Learning and Artificial Neural Nets
PPTX
Introduccion a prolog
PPTX
Meta Object Protocols
PPT
Ch10 Recursion
PDF
"That scripting language called Prolog"
PPTX
Knight’s tour algorithm
PDF
Knight's Tour
PPTX
Prolog: Arithmetic Operations In Prolog
PPT
Logic Programming and Prolog
PPTX
What&rsquo;s new in Visual C++
PPTX
Information Overload and Information Science / Mieczysław Muraszkiewicz
PPTX
Debugging in visual studio (basic level)
PDF
How Not To Be Seen
PPTX
Prolog -Cpt114 - Week3
PPTX
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Prolog Programming : Basics
Prolog Code [Family Tree] by Shahzeb Pirzada
Introduction on Prolog - Programming in Logic
Clojure 7-Languages
Reinforcement Learning and Artificial Neural Nets
Introduccion a prolog
Meta Object Protocols
Ch10 Recursion
"That scripting language called Prolog"
Knight’s tour algorithm
Knight's Tour
Prolog: Arithmetic Operations In Prolog
Logic Programming and Prolog
What&rsquo;s new in Visual C++
Information Overload and Information Science / Mieczysław Muraszkiewicz
Debugging in visual studio (basic level)
How Not To Be Seen
Prolog -Cpt114 - Week3
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Ad

Similar to Prolog 7-Languages (20)

PDF
Thinking Functionally In Ruby
PPTX
Prolog language programming in the facts.pptx
PPTX
Text analytics in Python and R with examples from Tobacco Control
PDF
Ejercicios de estilo en la programación
PDF
Fosdem 2013 petra selmer flexible querying of graph data
PDF
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
PDF
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
PPT
prolog-coolPrograms-flora.ppt
PPTX
Mathematical Modeling With Maple
PPTX
Basics of Python programming (part 2)
PDF
Cypher.PL: an executable specification of Cypher semantics
PDF
01. haskell introduction
PPT
cs344-lect15-robotic-knowledge-inferencing-prolog-11feb08.ppt
PDF
There's a Prolog in your Scala!
PDF
Erlang session1
PDF
Tree representation in map reduce world
PDF
Dynamic languages, for software craftmanship group
PPTX
#8 formal methods – pro logic
PPT
Knowledge engg using & in fol
PDF
A gentle introduction to functional programming through music and clojure
Thinking Functionally In Ruby
Prolog language programming in the facts.pptx
Text analytics in Python and R with examples from Tobacco Control
Ejercicios de estilo en la programación
Fosdem 2013 petra selmer flexible querying of graph data
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
prolog-coolPrograms-flora.ppt
Mathematical Modeling With Maple
Basics of Python programming (part 2)
Cypher.PL: an executable specification of Cypher semantics
01. haskell introduction
cs344-lect15-robotic-knowledge-inferencing-prolog-11feb08.ppt
There's a Prolog in your Scala!
Erlang session1
Tree representation in map reduce world
Dynamic languages, for software craftmanship group
#8 formal methods – pro logic
Knowledge engg using & in fol
A gentle introduction to functional programming through music and clojure

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Approach and Philosophy of On baking technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
cuic standard and advanced reporting.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PDF
KodekX | Application Modernization Development
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Unlocking AI with Model Context Protocol (MCP)
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Cloud computing and distributed systems.
NewMind AI Weekly Chronicles - August'25 Week I
cuic standard and advanced reporting.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
Diabetes mellitus diagnosis method based random forest with bat algorithm
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
KodekX | Application Modernization Development
Understanding_Digital_Forensics_Presentation.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”

Prolog 7-Languages

  • 1. Seven Languages in Seven Months Language 3: Prolog Raymond P. de Lacaze Patch raymond.delacaze@patch.com 01/30/13
  • 2. Overview • Part 1: Introduction to Prolog Chapter 4 of Seven Languages in Seven Weeks • Part 2: Advanced Topics Chapters X-Y of The Art of Prolog • Part 3: Prolog Today
  • 3. Prolog Introduction • Prolog is a declarative language • Prolog is logic programming language • Invented in 1972 by Colmerauer & Roussel – Edinburg Prolog – Marseilles Prolog • Initially used for Natural Language Processing • Programs consist of fact & rules • Fact is a clause in FOPC • Rule is an inference: B A1,…,An • Use queries to run programs and perform retrievals
  • 4. The Logic Programming Model • Logic Programming is an abstract model of computation. • Lambda Calculus is another abstract model of computation. • Prolog is a particular implementation of the logic programming model in much the same way that Clojure and Haskell are particular implementation of the lambda calculus. • OPS5 is another implementation of the logic programming model. • The use of mathematical logic to represent and execute computer programs is also a feature of the lambda calculus • Prolog is classified as a functional language (wikipedia)
  • 5. Logic Programming Paradigm • A program is logical description of your problem from which a solution is logically derivable • The execution of a program is very much like the mathematical proof of a theorem • Where’s my program? • N! is (N-1)! times N • 0! is 1
  • 6. Prolog Facts • Facts: <predicate>(<arg1>,…,<argN>) • Example: likes(mary, john) • Constants must be in lowercase • Variables must be in uppercase or start with an underscore. • Example: eats(mikey, X) • Example: believes(peter, likes(mary, john))
  • 7. Basic Inferences & Variables likes(john, cheese) likes(mary, cheese) likes(bob, meat) similar(X,Y) :- likes(X,Z), likes(Y,Z) Note: You can use *‘<filename/pathname>’+. to compile and load files GNU Prolog 1.4.1 By Daniel Diaz Copyright (C) 1999-2012 Daniel Diaz | ?- ['C:ProjectsLanguagescodePrologsimilar.pl']. compiling C:/Projects/Languages/code/Prolog/similar.pl for byte code... C:/Projects/Languages/code/Prolog/similar.pl compiled, 4 lines read - 935 bytes written, (16 ms) yes
  • 8. Filling in the Blanks | ?- similar(john, mary). yes | ?- similar(john, bob). no | ?- similar(mary, X). X = john ? yes | ?- similar(X, Y). X = john Y = john ? ; X = john Y = mary ? ; X = mary Y = john ? ; X = mary Y = mary ? Note: can use ; and a to get next or all answers
  • 9. Map Coloring (1) different(red, green). different(red, blue). different(green, red). different(green, blue). different(blue, red). different(blue, green). coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :- different(Mississippi, Tennessee), different(Mississippi, Alabama), different(Alabama, Tennessee), different(Alabama, Mississippi), different(Alabama, Georgia), different(Alabama, Florida), different(Georgia, Florida), different(Georgia, Tennessee).
  • 10. Map Coloring (2) | ?- ['c:projectslanguagescodeprologmap.pl']. compiling c:/projects/languages/code/prolog/map.pl for byte code... c:/projects/languages/code/prolog/map.pl compiled, 16 lines read - 1716 bytes written, 16 ms yes | ?- coloring(Alabama, Mississippi, Georgia, Tennessee, Florida). Alabama = blue Florida = green Georgia = red Mississippi = red Tennessee = green ? (16 ms) yes
  • 11. Unification (1) • The Unification Algorithm is a famous algorithm from the field of AI, often used in theorem proving, game playing, planning, etc… • It can loosely be thought of as an algorithm that tries to make to non-grounded terms the same. • P(X, 2) = P(1, Y)  X=1 & Y=2  P(1, 2) • P(X, X) = P(Y, 5)  X=5 & Y=5  P (5, 5) • P(X, Y) = P(2, Z)  X=2 & Y=Z  P (2, Z) • See Artificial Intelligence (Russell & Norvig)
  • 12. Prolog Rules • Rules: <head> :- <body> • Head: Single clause typically with variables • Body: Conjunction of goals with variables • Examples: ancestor(X,Y) :- parent(X,Y) ancestor(X,Y) :- parent(X,Z), parent(Z,Y) ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y)
  • 13. A Recursive Example(1) parent(p1, p2). parent(p2, p3). parent(p3, p4). ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). | ?- ['c:projectslanguagescodeprologancestor.pl']. compiling c:/projects/languages/code/prolog/ancestor.pl for byte code... c:/projects/languages/code/prolog/ancestor.pl compiled, 5 lines read - 818 bytes written, 13 ms yes
  • 14. A Recursive Example(2) | ?- trace. The debugger will first creep -- showing everything (trace) yes {trace} | ?- ancestor(p1, p4). 1 1 Call: ancestor(p1,p4) ? 2 2 Call: parent(p1,p4) ? 2 2 Fail: parent(p1,p4) ? 2 2 Call: parent(p1,_80) ? 2 2 Exit: parent(p1,p2) ? 3 2 Call: ancestor(p2,p4) ? 4 3 Call: parent(p2,p4) ? 4 3 Fail: parent(p2,p4) ? 4 3 Call: parent(p2,_129) ? 4 3 Exit: parent(p2,p3) ? 5 3 Call: ancestor(p3,p4) ? 6 4 Call: parent(p3,p4) ? 6 4 Exit: parent(p3,p4) ? 5 3 Exit: ancestor(p3,p4) ? 3 2 Exit: ancestor(p2,p4) ? 1 1 Exit: ancestor(p1,p4) ? true ? (63 ms) yes
  • 15. Using Rules in Both Directions // Find all ancestors | ?- ancestor(X, p4). X = p3 ? ; X = p1 ? ; X = p2 ? ; no // Find all descendants | ?- ancestor(p1, X). X = p2 ? ; X = p3 ? ; X = p4 ? ; no
  • 16. Lists and Tuples (1) • List are denoted by comma-separated values in square brackets. i.e. [1, 2, 3] • Tuples are denoted by comma-separated values in parentheses. i.e. (1, 2, 3) | ?- [1, 2, 3] = [X, Y, Z]. X = 1 Y = 2 Z = 3 yes
  • 17. Accessing Elements of a List | ?- [1, 2, 3] = [X | Y]. X = 1 Y = [2,3] | ?- [1, 2, 3] = [_, X | Y]. X = 2 Y = [3]
  • 18. Lists and Math (1) count(0, []). count(Count, [Head|Tail]) :- count(TailCount, Tail), Count is TailCount + 1. sum(0, []). sum(Total, [Head|Tail]) :- sum(Sum, Tail), Total is Head + Sum. average(Average, List) :- sum(Sum, List), count(Count, List), Average is Sum/Count.
  • 19. Lists and Math (2) | ?- sum(S, [1,2,3]). 1 1 Call: sum(_17,[1,2,3]) ? 2 2 Call: sum(_92,[2,3]) ? 3 3 Call: sum(_116,[3]) ? 4 4 Call: sum(_140,[]) ? 4 4 Exit: sum(0,[]) ? 5 4 Call: _168 is 3+0 ? 5 4 Exit: 3 is 3+0 ? 3 3 Exit: sum(3,[3]) ? 6 3 Call: _197 is 2+3 ? 6 3 Exit: 5 is 2+3 ? 2 2 Exit: sum(5,[2,3]) ? 7 2 Call: _17 is 1+5 ? 7 2 Exit: 6 is 1+5 ? 1 1 Exit: sum(6,[1,2,3]) ? S = 6 ?
  • 20. Solving Sudoku (1) valid([]). valid([Head|Tail]) :- valid(Tail), fd_all_different(Head). sudoku(S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, S44, Board) :- Board = [S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, S44, ], fd_domain(Board, 1, 4), Row1 = [S11, S12, S13, S14], Row2 = [S21, S22, S23, S24], Row3 = [S31, S32, S33, S34], Row4 = [S41, S42, S43, S44], Col1 = [S11, S21, S31, S41], Col2 = [S12, S22, S32, S42], Col3 = [S13, S23, S33, S43], Col4 = [S14, S24, S34, S44], Square1 = [S11, S12, S21, S22], Square2 = [S13, S14, S23, S24], Square3 = [S31, S32, S41, S42,], Square4 = [S33, S34, S43, S44,], valid([Row1, Row2, Row3, Row4,]), valid([Col1, Col2, Col3, Col4, ]), valid([Square1, Square2, Square3, Square4]).
  • 21. Solving Sudoku (2) | ?- sudoku(_, _, 2, 3, _, _, _, _, _, _ ,_, _, 3, 4, _, _, Solution). Solution = [4, 1, 2, 3, 2, 3, 4, 1, 1, 2, 3, 4, 3, 4, 1, 2]
  • 22. Solving Sudoku (3) • Finite Domain variables: A new type of data is introduced: FD variables which can only take values in their domains. The initial domain of an FD variable is 0..fd_max_integer where fd_max_integer represents the greatest value that any FD variable can take. • fd_domain(Board, 1, 4). Used to specify the range of values of each Sudoku cell. • fd_all_different(X). Used to specify that all elemts in the list must have distinct values.
  • 23. Part 2: Advanced Topics • The Art of Prolog, Sterling and Shapiro, MIT Press, 1986 • Structure Inspection • Meta-Logical Predicates • Cuts (and Negation) • Extra-Logical Predicates
  • 24. Structure Inspection | ?- functor(father(tom, harry), P, A). A = 2 P = father Yes | ?- arg(1,father(tom, harry), A1). A1 = tom Yes | ?- arg(2,father(tom, harry), A2). A2 = harry yes | ?- functor(X, father, 2). X = father(_, _) Yes | ?- father(tom, harry) =.. [X, Y, Z]. X = father Y = tom Z = harry yes | ?- X =.. [father, tom, harry]. X = father(tom, harry) yes | ?- X =.. [father, tom, harry], assertz(X). X = father(tom, harry) yes | ?- father(tom, harry). yes functor, arg and =..
  • 25. Meta-Logical Predicates • Outside scope of first-order logic • Query and affect the state of the proof • Treat variables as objects • Convert data structures to goals • Type Predicates: • var(<term>) • nonvar(<term>) • Variables as objects: freeze & melt • Dynamically Affecting the Knowledge Base • assert(<goal>) • retract(<goal>) • The Meta-Variable Facility: call(<goal>) • Memoization: lemma(<goal>)
  • 26. Extra-Logical Predicates • These achieve side-effects as a result of being logically true • Three types of extra-logical predicates • Input / Output – read & write • Accessing and manipulating the program – clause(Head, Body) – assert (X) – retract(X) • Interfacing to the Operating System
  • 27. Cuts (1) • Predicate cut or ! affects procedural behavior • Main purpose is to reduce the search space • It’s use is controversial: purity vs. efficiency • Green Cuts: Express determinism • Consider merging two sorted lists • Only one of X<Y, X=Y or X>Y can succeed • Once one succeeds, no need for the others.
  • 28. Merging without Cuts merge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, merge(Xs, [Y|Ys], Zs). merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, merge(Xs, Ys, Zs). merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, merge([X|Xs], Ys, Zs). merge(Xs, [], Xs). merge([], Ys, Ys). | ?- merge([1,3,5],[2,3], Xs). Xs = [1,2,3,3,5] ? yes
  • 29. Merging with Cuts merge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, !, merge(Xs, [Y|Ys], Zs). merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, !, merge(Xs, Ys, Zs). merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, !, merge([X|Xs], Ys, Zs). merge(Xs, [], Xs) :- !. merge([], Ys, Ys) :- !. ?- merge([1,3,5],[2,3], Xs). Xs = [1,2,3,3,5] yes
  • 30. The Effect of Cuts merge([1,3,4],[2, 5], Xs). 1 1 Call: merge([1,3,4],[2,5],_27) ? 2 2 Call: 1>2 ? 2 2 Fail: 1>2 ? 2 2 Call: 1<2 ? 2 2 Exit: 1<2 ? 3 2 Call: merge([3,4],[2,5],_60) ? 4 3 Call: 3>2 ? 4 3 Exit: 3>2 ? 5 3 Call: merge([3,4],[5],_114) ? 6 4 Call: 3>5 ? 6 4 Fail: 3>5 ? 6 4 Call: 3<5 ? 6 4 Exit: 3<5 ? 7 4 Call: merge([4],[5],_168) ? 8 5 Call: 4>5 ? 8 5 Fail: 4>5 ? 8 5 Call: 4<5 ? 8 5 Exit: 4<5 ? 9 5 Call: merge([],[5],_222) ? 9 5 Exit: merge([],[5],[5]) ? 7 4 Exit: merge([4],[5],[4,5]) ? 5 3 Exit: merge([3,4],[5],[3,4,5]) ? 3 2 Exit: merge([3,4],[2,5],[2,3,4,5]) ? 1 1 Exit: merge([1,3,4],[2,5],[1,2,3,4,5]) ? Xs = [1,2,3,4,5] ? | ?- merge([1,3,4],[2, 5], Xs). 1 1 Call: merge([1,3,4],[2,5],_27) ? 2 2 Call: 1>2 ? 2 2 Fail: 1>2 ? 2 2 Call: 1<2 ? 2 2 Exit: 1<2 ? 3 2 Call: merge([3,4],[2,5],_60) ? 4 3 Call: 3>2 ? 4 3 Exit: 3>2 ? 5 3 Call: merge([3,4],[5],_114) ? 6 4 Call: 3>5 ? 6 4 Fail: 3>5 ? 6 4 Call: 3<5 ? 6 4 Exit: 3<5 ? 7 4 Call: merge([4],[5],_168) ? 8 5 Call: 4>5 ? 8 5 Fail: 4>5 ? 8 5 Call: 4<5 ? 8 5 Exit: 4<5 ? 9 5 Call: merge([],[5],_222) ? 9 5 Exit: merge([],[5],[5]) ? 7 4 Exit: merge([4],[5],[4,5]) ? 5 3 Exit: merge([3,4],[5],[3,4,5]) ? 3 2 Exit: merge([3,4],[2,5],[2,3,4,5]) ? 1 1 Exit: merge([1,3,4],[2,5],[1,2,3,4,5]) ? Xs = [1,2,3,4,5]
  • 31. Prolog Today • GNU Prolog Compiler Full I/O capabilities Complete Socket programming API Can be linked to C in both directions • Approx. 20 existing Prolog implementations • Some other popular ones: SWI-Prolog & Visual Prolog • http://en.wikipedia.org/wiki/Comparison_of_Prolog_implementations • http://orgnet.com/inflow3.html Software for Social Network Analysis & Organizational Network Analysis
  • 33. core.logic • Clojure core.logic (David Nolen) This is a Clojure implementation of miniKanren. miniKanren is a Logic Programming library embedded in Scheme based The Reasoned Schemer by Daniel Friedman. • Another implementation of the Logic Programming Paradigm • Presented at Strange Loop 2012 • Very well received by the Clojure community • https://github.com/clojure/core.logic
  • 34. AllegroGraph (Franz Inc.) • AllegroGraph is one the leading graph database and application frameworks for building Semantic Web applications. • It can store data and meta-data as trillions of triples • Query these triples through using PROLOG • Query these triples using SPARQL (the standard W3C query language) • AllegroGraph includes support for Federation, Social Network Analysis, Geospatial capabilities and Temporal reasoning • AllegroGraph is implemented in Common Lisp & CLOS. • http://www.franz.com/agraph/allegrograph/
  • 35. References Seven Languages in Seven Weeks Bruce A. Tate, Pragmatic Programmers LLC, 2010 The Art of Prolog: Advanced Programming Techniques Sterling & Shapiro, MIT Press, 1986 GNU Prolog User Manual Daniel Diaz, November 2012 Artificial Intelligence: A Modern Approach Russell & Norvig, Prentice Hall, 1995