SlideShare a Scribd company logo
1 of 35
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
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

What's hot

09 logic programming
09 logic programming09 logic programming
09 logic programmingsaru40
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)Nitesh Singh
 
Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prologHarry Potter
 
Prolog Programming Language
Prolog Programming  LanguageProlog Programming  Language
Prolog Programming LanguageReham AlBlehid
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologDataminingTools Inc
 
Babar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationBabar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationPierre de Lacaze
 
Predlogic
PredlogicPredlogic
Predlogicsaru40
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Sabu Francis
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingHarry Potter
 
PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG CONTENT
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologDataminingTools Inc
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming LanguageReham AlBlehid
 

What's hot (19)

09 logic programming
09 logic programming09 logic programming
09 logic programming
 
Prolog basics
Prolog basicsProlog basics
Prolog basics
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prolog
 
Prolog Programming Language
Prolog Programming  LanguageProlog Programming  Language
Prolog Programming Language
 
PROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In PrologPROLOG: Recursion And Lists In Prolog
PROLOG: Recursion And Lists In Prolog
 
Babar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and RepresentationBabar: Knowledge Recognition, Extraction and Representation
Babar: Knowledge Recognition, Extraction and Representation
 
Predlogic
PredlogicPredlogic
Predlogic
 
Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1Introduction to logic and prolog - Part 1
Introduction to logic and prolog - Part 1
 
prolog ppt
prolog pptprolog ppt
prolog ppt
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
PROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In PrologPROLOG: Fact Roles And Queries In Prolog
PROLOG: Fact Roles And Queries In Prolog
 
Overview prolog
Overview prologOverview prolog
Overview prolog
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In Prolog
 
Prolog & lisp
Prolog & lispProlog & lisp
Prolog & lisp
 
Scheme Programming Language
Scheme Programming LanguageScheme Programming Language
Scheme Programming Language
 
Plc part 4
Plc  part 4Plc  part 4
Plc part 4
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 

Viewers also liked

Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : BasicsMitul Desai
 
Prolog Code [Family Tree] by Shahzeb Pirzada
Prolog Code [Family Tree] by Shahzeb PirzadaProlog Code [Family Tree] by Shahzeb Pirzada
Prolog Code [Family Tree] by Shahzeb PirzadaShahzeb Pirzada
 
Introduction on Prolog - Programming in Logic
Introduction on Prolog - Programming in LogicIntroduction on Prolog - Programming in Logic
Introduction on Prolog - Programming in LogicVishal Tandel
 
Reinforcement Learning and Artificial Neural Nets
Reinforcement Learning and Artificial Neural NetsReinforcement Learning and Artificial Neural Nets
Reinforcement Learning and Artificial Neural NetsPierre de Lacaze
 
Introduccion a prolog
Introduccion a prologIntroduccion a prolog
Introduccion a prologJeffoG92
 
"That scripting language called Prolog"
"That scripting language called Prolog""That scripting language called Prolog"
"That scripting language called Prolog"Sergei Winitzki
 
Knight’s tour algorithm
Knight’s tour algorithmKnight’s tour algorithm
Knight’s tour algorithmHassan Tariq
 
Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologPROLOG CONTENT
 
Logic Programming and Prolog
Logic Programming and PrologLogic Programming and Prolog
Logic Programming and PrologSadegh Dorri N.
 
What&rsquo;s new in Visual C++
What&rsquo;s new in Visual C++What&rsquo;s new in Visual C++
What&rsquo;s new in Visual C++Microsoft
 
Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Larry Nung
 
How Not To Be Seen
How Not To Be SeenHow Not To Be Seen
How Not To Be SeenMark Pesce
 
Prolog -Cpt114 - Week3
Prolog -Cpt114 - Week3Prolog -Cpt114 - Week3
Prolog -Cpt114 - Week3a_akhavan
 
Pioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsPioneers of Information Science in Europe: The Oeuvre of Norbert Henrichs
Pioneers of Information Science in Europe: The Oeuvre of Norbert HenrichsWolfgang Stock
 

Viewers also liked (19)

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

Similar to Prolog 7-Languages

Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In RubyRoss Lawley
 
Text analytics in Python and R with examples from Tobacco Control
Text analytics in Python and R with examples from Tobacco ControlText analytics in Python and R with examples from Tobacco Control
Text analytics in Python and R with examples from Tobacco ControlBen Healey
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programaciónSoftware Guru
 
Fosdem 2013 petra selmer flexible querying of graph data
Fosdem 2013 petra selmer   flexible querying of graph dataFosdem 2013 petra selmer   flexible querying of graph data
Fosdem 2013 petra selmer flexible querying of graph dataPetra Selmer
 
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdfINTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdfranapoonam1
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Hamidreza Soleimani
 
prolog-coolPrograms-flora.ppt
prolog-coolPrograms-flora.pptprolog-coolPrograms-flora.ppt
prolog-coolPrograms-flora.pptdatapro2
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Cypher.PL: an executable specification of Cypher semantics
Cypher.PL: an executable specification of Cypher semanticsCypher.PL: an executable specification of Cypher semantics
Cypher.PL: an executable specification of Cypher semanticsopenCypher
 
There's a Prolog in your Scala!
There's a Prolog in your Scala!There's a Prolog in your Scala!
There's a Prolog in your Scala!George Leontiev
 
Tree representation in map reduce world
Tree representation  in map reduce worldTree representation  in map reduce world
Tree representation in map reduce worldYu Liu
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupReuven Lerner
 
#8 formal methods – pro logic
#8 formal methods – pro logic#8 formal methods – pro logic
#8 formal methods – pro logicSharif Omar Salem
 
Knowledge engg using & in fol
Knowledge engg using & in folKnowledge engg using & in fol
Knowledge engg using & in folchandsek666
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojurePaul Lam
 

Similar to Prolog 7-Languages (20)

Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
Text analytics in Python and R with examples from Tobacco Control
Text analytics in Python and R with examples from Tobacco ControlText analytics in Python and R with examples from Tobacco Control
Text analytics in Python and R with examples from Tobacco Control
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
Fosdem 2013 petra selmer flexible querying of graph data
Fosdem 2013 petra selmer   flexible querying of graph dataFosdem 2013 petra selmer   flexible querying of graph data
Fosdem 2013 petra selmer flexible querying of graph data
 
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdfINTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
INTRODUCTION AND HISTORY OF R PROGRAMMING.pdf
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
 
prolog-coolPrograms-flora.ppt
prolog-coolPrograms-flora.pptprolog-coolPrograms-flora.ppt
prolog-coolPrograms-flora.ppt
 
Mathematical Modeling With Maple
Mathematical Modeling With MapleMathematical Modeling With Maple
Mathematical Modeling With Maple
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Cypher.PL: an executable specification of Cypher semantics
Cypher.PL: an executable specification of Cypher semanticsCypher.PL: an executable specification of Cypher semantics
Cypher.PL: an executable specification of Cypher semantics
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
 
There's a Prolog in your Scala!
There's a Prolog in your Scala!There's a Prolog in your Scala!
There's a Prolog in your Scala!
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
 
Tree representation in map reduce world
Tree representation  in map reduce worldTree representation  in map reduce world
Tree representation in map reduce world
 
Dynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship groupDynamic languages, for software craftmanship group
Dynamic languages, for software craftmanship group
 
#8 formal methods – pro logic
#8 formal methods – pro logic#8 formal methods – pro logic
#8 formal methods – pro logic
 
Knowledge engg using & in fol
Knowledge engg using & in folKnowledge engg using & in fol
Knowledge engg using & in fol
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojure
 
R language, an introduction
R language, an introductionR language, an introduction
R language, an introduction
 
Week 4
Week 4Week 4
Week 4
 

Recently uploaded

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 

Recently uploaded (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 

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
  • 32.
  • 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