Vector Databases 101 - An introduction to the world of Vector Databases
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
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)
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.
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.
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