SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
That scripting language called Prolog
Sergei Winitzki
Types, Theorems, and Programming Languages
June 16, 2014
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 1 / 31
The four programming paradigms
Paradigm Example Programs are... Diculty is in... Best used in...
Imperative Fortran lists of commands order of updates numerics
Functional Lisp expressions level of abstraction compilers
Logic Prolog sets of predicates runtime behavior symbolic AI
Constraint TEX data + constraints conicting modules specic domain
Objects, type systems, modules, concurrency, etc., are merely features
added on top of a chosen paradigm
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 2 / 31
A denition of declarative
Programming is declarative when specications are programs.
More formally:
A language L is declarative for an application domain D if:
The domain D has a good specication formalism F
good = visual, pragmatically convenient, complete for the domain D
There is an obvious syntactic transformation from F to L
The resulting program implements the specication
Less formally:
A declarative language is a perfect DSL for the given domain
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 3 / 31
Example: declarative FORTRAN 77
Application domain: numerical mathematical expressions
Specication: a mathematical formula involving numbers and functions
Example specication:
f (x, p, q) =
sin px
x2
−
sin
2
qx
x3
Implementation: F(X,P,Q)=SIN(P*X)/X**2-(SIN(Q*X))**2/X**3
For more complicated tasks, FORTRAN is not declarative
˜Xk = Yk −
n
j =k+1
Akj Xj , ∀k ∈ [1..n]
(example code, 1987)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 4 / 31
Example: declarative Haskell 98
Application domain: recursively dened, algebraic data structures
Specications: inductive denitions of functions on ADTs
Example (from R. Sedgewick, Algorithms in C, 1998)
data BTree α = BTNode α | BTVertex α (BTree α) (BTree α)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 5 / 31
Example: declarative Haskell 98, continued
height :: BTree α → Int
height (BTNode _) = 0
height (BTVertex _ t1 t2) = 1 + max (height t1) (height t2)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 6 / 31
Example: non-declarative Haskell
For a dierent application domain, Haskell is not declarative!
Downloading data from server (from Real World Haskell, 2008)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 7 / 31
Prolog as a DSL for logic puzzles
All jumping creatures are green. All small jumping creatures are martians.
All green martians are intelligent.
Ngtrks is small and green. Pgvdrk is a jumping martian.
Who is intelligent? (inpired by S. Lem, Invasion from Aldebaran)
small(ngtrks). green(ngtrks).
martian(pgvdrk). jumping(pgvdrk).
green(X) :- jumping(X).
martian(X) :- small(X), jumping(X).
intelligent(X) :- green(X), martian(X).
main :-
intelligent(X), format('~w is intelligent.~n', X), halt.
$ swipl -o martians -q -t main -c martians.pl
$ ./martians
pgvdrk is intelligent.
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 8 / 31
Prolog in a nutshell 1: symbols, predicates, rules
Outer-level lowercase symbols are logical predicates
All other lowercase symbols are symbolic constants
All capitalized symbols are quantied logical variables (LVs)
LVs on the left imply ∀; free LVs on the right imply ∃
The symbol :- means implication ←, the comma means and
Rules can be given in any order; no declarations
Examples:
green(X) :- jumping(X) means ∀X : green(X) ← jumping(X)
path(A,B) :- path(A,C), path(C,B) means
∀A, B : [∃C : path(A, C) ∧ path(C, B) → path(A, B)]
main :- intelligent(X) means: prove that ∃X:intelligent(X)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 9 / 31
Prolog in a nutshell 2: variables
Martians recap:
small(ngtrks). green(ngtrks).
martian(pgvdrk). jumping(pgvdrk).
green(X) :- jumping(X).
martian(X) :- small(X), jumping(X).
intelligent(X) :- green(X), martian(X).
main :- intelligent(X) means: prove that ∃X:intelligent(X)
The Prolog engine will prove existence of X by backtracking search!
(we can say trace and follow the search)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 10 / 31
Prolog in a nutshell 2: backtracking search
Martians recap:
small(ngtrks). green(ngtrks).
martian(pgvdrk). jumping(pgvdrk).
green(X) :- jumping(X).
martian(X) :-
small(X), jumping(X).
intelligent(X) :-
green(X), martian(X).
?- intelligent(X).
[trace] ?- intelligent(X).
Call: (6) intelligent(_G2442)
Call: (7) green(_G2442)
Exit: (7) green(ngtrks)
Call: (7) martian(ngtrks)
Call: (8) small(ngtrks)
Exit: (8) small(ngtrks)
Call: (8) jumping(ngtrks)
Fail: (8) jumping(ngtrks)
Fail: (7) martian(ngtrks)
Redo: (7) green(_G2442)
Call: (8) jumping(_G2442)
Exit: (8) jumping(pgvdrk)
Exit: (7) green(pgvdrk)
Call: (7) martian(pgvdrk)
Exit: (7) martian(pgvdrk)
Exit: (6) intelligent(pgvdrk)
X = pgvdrk .
The proof may fail, or may succeed in more than one way
LVs are assigned by unication and unassigned on backtracking
Once assigned, a logical variable is immutable
This is called resolution of Horn clauses and unication
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 11 / 31
Horn clauses and resolution
Consider a restricted fragment of predicate logic:
Expressions are conjunctions of Horn clauses:
(a ∧ b ∧ ... ∧ c → d ) ∧ (p ∧ q ∧ ... ∧ r → s) ∧ (True → w ) ∧ ...
Disjunction is expressible through Horn clauses:
(a ∨ b) → c = (a → c) ∧ (b → c)
Only ∀ quantiers are allowed, and only outside:
∀X ∀Y : a(X , Y ) ∧ b(Y ) → c(X )
Prolog syntax: quantiers are implicit; implication points leftward
Resolution: the b is eliminated from two clauses
(c ← a ∧ p ∧ q) ⇐
b ← a ∧ p
c ← b ∧ q
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 12 / 31
Examples: recursive predicates
Factorial predicate: fact(N,F) holds if F = N!
fact(1,1).
fact(N,F) :- M is N-1, fact(M,E), F is E*N.
Fibonacci predicate: bo(N,F) holds if F is N-th Fibonacci number
bo(0,1). bo(1,1).
bo(N,F) :- M1 is N-1, bo(M1,E1),
M2 is N-2, bo(M2,E2), F is E1+E2.
Instead of computing F through calling a function, we prove existence
of a value F such that some predicate holds.
Prolog has only predicates  no declarations, expressions, functions,
variables, or static types
Note: M is N-1 resembles an expression but is actually a predicate!
it means is(M,'-'(N,1)) , where '-' is an inx data constructor!
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 13 / 31
Prolog in a nutshell 2: syntax extensions
Syntax for data structures: passive predicates (a distinct namespace)
left(pair(X,Y), X). right(pair(X,Y), Y).
since pair(X,Y) is inside a predicate, it must be data
however, pair(X,Y) can contain free logical variables
and so can contain any other structures (no typechecking!):
pair(pair(X,Y),pair(Y,pair(Z,T)))
Syntax sugar for lists: [] or [a,b,c] or [a,b,c|[]]
Similar to Haskell's a:b:c:[]
Examples:
head([X|Xs], X). tail([X|Xs], Xs). empty([]).
length([], 0).
length([X|Xs],N) = length(Xs,M), N is M+1.
User-dened syntax: inx, prex, postx
op(400, xfy, *). op(300, yfx, ^). op(200, xf, :!).
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 14 / 31
What is unication?
Unication is Prolog's way of assigning values to variables
Pattern-matching of recursive structures, tting unassigned variables
Assignment is propagated to other rules:
like(1). like(2).
like(you_know(Y,X)) :- like(X), like(Y).
really(A,B) :- like(A), like(you_know(A,B).
The predicate really(1,2) holds because like(you_know(Y,X)) is
unied with like(you_know(1,2)) to set Y = 1 and X = 2
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 15 / 31
Pointers made safe: dierence lists
Appending ordinary lists takes O(n) operations:
l_append([], X, X).
l_append([X|Xs], Y, [X|Zs]) :- l_append(Xs, Y, Zs).
To optimize, we need a pointer to the end of the list!
having a pointer = a part of a data structure is not yet assigned
solution: a free LV is exposed, unied with some part of the data
Dierence lists: pair([a,b,c|X],X) or [a,b,c|X]-X
op(500, xfy, -). empty(X-X).
dl_append(X-Y,Y-Z,X-Z). /* O(1) operations! */
[trace] ?- dl_append( [a,b,c|A]- A, [d,e|B]-B, C).
dl_append([a, b, c|_G2447]-_G2447, [d, e|_G2456]-_G2456, _G2463)
dl_append([a, b, c, d, e|_G2456]-[d, e|_G2456], [d, e|_G2456]-_G2456,
[a, b, c, d, e|_G2456]-_G2456)
A = [d, e|B], C = [a, b, c, d, e|B]-B.
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 16 / 31
Pointers made safe: dierence lists
How this works in more detail:
We have the program dl_append(X-Y,Y-Z,X-Z).
We have the query ?- dl_append([a,b,c|A]-A, [d,e|B]-B, C).
The value of Y can be unied with the query structure only
if we assign Y=A and also Y=[d,e|B] and Z=B.
Thus we must have A=[d,e|B]
Therefore X must be assigned [a,b,c|A]=[a,b,c,d,e|B]
Finally C=X-Z=[a,b,c,d,e|B]-B
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 17 / 31
Pointers made safe: queues
Implement insertion at end of queue:
list_to_queue(L,Q-Y) :- l_append(L,Y,Q). /* O(n) */
q_insert_at_end(E, Q-[E|Y], Q-Y). /* O(1) */
q_head(E, [E|Q]-Y, Q-Y).
[trace]
?- q_insert_at_end( n, [a,b,c|X]-X, Q ).
X = [n|_G1726], Q = [a, b, c, n|_G1726]-_G1726.
?- q_head( X, [a,b,c,n|Y]-Y, Z).
X = a, Z = [b, c, n|Y]-Y.
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 18 / 31
Pointers made safe: iterators
A list iterator: set at begin, increment, check if at end
Split the list into a pair of queues
Eectively, we have a pointer to the middle of a list:
:- op(600, xfx, ^^).
plist_at_begin(X-X ^^ A-B). plist_at_end(A-B ^^ X-X).
plist_incr(A-[X|B] ^^ [X|C]-D, A-B ^^ C-D).
[trace] ?- plist_incr([a,b|X]-X ^^ [c,d,e|Y]-Y, P).
plist_incr([a, b|_G1978]-_G1978 ^^ [c, d, e|_G1990]-_G1990, _G1999)
plist_incr([a, b, c|_G2113]-[c|_G2113] ^^ [c, d, e|_G1990]-_G1990,
[a, b, c|_G2113]-_G2113 ^^ [d, e|_G1990]-_G1990)
X = [c|_G2113], P = [a, b, c|_G2113]-_G2113 ^^ [d, e|Y]-Y.
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 19 / 31
Parsing with Denite Clause Grammars
Top-down parsing with unlimited backtracking...
expr ::== term | term oper expr | '(' expr ')'
oper ::== 'and' | 'or' | ...
term ::== 'true' | 'false' | ...
...is similar to Prolog's evaluation strategy on token queues:
expr(Ts) :- term(Ts).
expr(Ts-X) :- term(Ts-T1), op(T1-T2), expr(T2-X).
oper([T|X]-X) :- T='and'. oper([T|X]-X) :- T='or'. /* etc. */
term([T|X]-X) :- T='true'. term([T|X]-X) :- T='false'. /* etc. */
Syntactic sugar (--) is provided to avoid writing out the queues:
expr -- term. expr -- term, oper, expr. expr -- ['('], expr, [')'].
oper -- ['and']. oper -- ['or']. term -- ['true']. term -- ['false'].
/* test: */ ?- expr(['true', 'and', 'false', 'or', 'true'], []).
Nonterminals can have extra arguments and call Prolog code
No need for Lex/Yacc (but they do other grammars...)
Can parse some non-CFG grammars (attributed grammars)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 20 / 31
Interpreters
A lambda-calculus interpreter in 3 lines of Prolog?...
:- op(1190, xfy, ~). :- op(1180, yfx, @).
comp(A~B, A~B). comp( (A~B) @ A, B).
comp(A@B@C, R) :- comp(A@B,S), comp(S@C,R). comp(A@B, A@B).
...um, not really (lacking α-conversion and multiple steps)
?- comp( (X~Y~X) @ 1, Result).
Result = (Y~1) . /* okay! */
?- comp( (X~ X@X) @ (Y~Y), R1), comp(R1 @ 1,Res).
X = Y, Y = (Y~Y), R1 = ((Y~Y)@ (Y~Y)), Res = ((Y~Y)@1) . /* ??? */
A Prolog module with about 15 clauses achieves this (hacky!):
:- use_module(lambda).
id - (X ~ X). const - (C ~ _ ~ C).
ycomb - (F ~ (X ~ F @ (X @ X)) @ (X ~ F @ (X @ X))).
fac - ( F ~ N ~ iff @ (N F = const; F = (const @ id) ).
?- lambda((y @ fac @ 4), Result).
Result = 24
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 21 / 31
Prolog and relational databases
Alice lost her umbrella on Monday. Brian lost his glasses on Tuesday. Chris lost his pen
on Monday. Dennis lost his watch on Monday and his pen on Tuesday.
Whoever loses something on Monday will be unlucky for the whole week.
Who is unlucky for the whole week and also lost something on Tuesday?
lost(alice, umbrella, monday). lost(brian, glasses, tuesday).
lost(chris, pen, monday).
lost(dennis, watch, monday). lost(dennis, pen, tuesday).
unlucky(X) :- lost(X, _, monday).
?- unlucky(Who), lost(Who, _, tuesday).
Our predicates are either facts or non-recursive inferences
We will need no backtracking (unlike the Martians example)
A query such as ?- unlucky(X) will usually return many results
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 22 / 31
Prolog and relational databases
We have seen this before... (it is called SQL)
lost: person object day
alice umbrella monday
brian glasses tuesday
chris pen monday
dennis watch monday
dennis pen tuesday
unlucky: person
alice
chris
dennis
Our Prolog code...
unlucky(X) :- lost(X, _, monday).
?- unlucky(Who), lost(Who, _, tuesday).
...is translated into SQL as:
CREATE VIEW unlucky AS SELECT person FROM lost WHERE lost.day='monday';
SELECT person FROM unlucky NATURAL JOIN lost WHERE lost.day='tuesday';
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 23 / 31
Prolog, Datalog, and SQL
SQL Datalog Prolog
recursive data ? +
recursive queries + +
Horn clauses + + +
control ow +
functions +
typed values +
named values +
Prolog + functions + types = functional-logic programming
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 24 / 31
Functional-logic programming in Mercury
Mercury is Prolog + some features of ML
Immutable values, static type inference, algebraic polymorphism
all predicates and all arguments are labeled with modes
static detection of errors in predicate use
functions are deterministic predicates with strict modes
products, sums, labeled records, higher-order functions
I/O by unique types
modules and signatures à la Standard ML
type classes à la Haskell
standard library: arrays, multithreading, etc.
can compile to high-performance machine code, C, Java, Erlang
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 25 / 31
A taste of Mercury
Read an integer and print its factorial
:- module f.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
:- pred fact(int::in, int::out) is det.
:- import_module list, string.
fact(N,F) :- ( if N = 1 then F = 1 else fact(N-1, A), F = A*N ).
main(!IO) :- io.read_line_as_string(Result, !IO),
( if Result = ok(String),
string.to_int(string.strip(String), N)
then
io.format(fact(%d) = %dn, [i(N), i(fact(N))], !IO)
else
io.format(That isn't a number...n, [], !IO)
).
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 26 / 31
Prolog: A perfect scripting language
No declarations, runtime (dynamic) typing, immutable values
Data constructors and predicates have user-dened inx syntax
Pattern-matching on recursive data structures, with backtracking
Metaprogramming, reection, self-modifying code
Easy to do embedded or external DSLs (monadic top-down parsing)
ISO standard since 1995, many free implementations
typically with a REPL and a compiler to high-performance VM
Interface to databases, networking, multithreading, GUI, ...
Core language is small; full language is hard to use?
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 27 / 31
Compiling Prolog to a virtual machine
Warren's Abstract Machine (WAM) is still the most inuential
Instructions assume that WAM manages stack and heap
Compilation of core Prolog to WAM is relatively straightforward:
(D. H. D. Warren, 1983)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 28 / 31
Conclusions and outlook
Declarative programming = creating a good DSL for your domain
It is easy to pick up Prolog, after seeing SQL and Haskell
Prolog makes building DSLs easy (both internal and external DSLs)
Mercury = Prolog + types + functions
Prolog is almost forgotten, but its legacy lives on
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 29 / 31
Suggested reading
Free implementations I used:
SWI-Prolog
The Mercury programming language
A great pedagogical introduction to Prolog and Datalog:
D. Maier, D. S. Warren. Computing with logic: Logic programming
with Prolog. Addison-Wesley, 1988
Advanced Prolog programming:
E. Shapiro, L. Sterling. The art of Prolog. MIT, 1999
T. Van Le. Techniques of Prolog programming. Wiley, 1993
R. O'Keefe. The craft of Prolog. MIT, 1990
Implementation (the WAM is still an important source of inspiration):
H. Aït-Kaci. Warren's Abstract Machine (WAM). MIT, 1991
W. F. Clocksin. Design and simulation of a sequential Prolog machine.
New Gen. Comp., 3 (1985), p. 101 (describes the ZIP machine)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 30 / 31
Summary
What is logic programming and constraint programming
Prolog in a nutshell
How Prolog makes pointers safe
Why Prolog was the ultimate scripting language for AI (backtracking
search, interpreters, and DSLs for free)
What is functional-logic programming (a taste of the programming
language Mercury)
Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 31 / 31

Contenu connexe

Tendances

Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)
saithirumalg
 

Tendances (20)

Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register Allocation
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
淺談編譯器最佳化技術
淺談編譯器最佳化技術淺談編譯器最佳化技術
淺談編譯器最佳化技術
 
Discrete Logarithm Problem over Prime Fields, Non-canonical Lifts and Logarit...
Discrete Logarithm Problem over Prime Fields, Non-canonical Lifts and Logarit...Discrete Logarithm Problem over Prime Fields, Non-canonical Lifts and Logarit...
Discrete Logarithm Problem over Prime Fields, Non-canonical Lifts and Logarit...
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
Loader
LoaderLoader
Loader
 
Temporal logic-model-checking
Temporal logic-model-checkingTemporal logic-model-checking
Temporal logic-model-checking
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQLProgramming with Python and PostgreSQL
Programming with Python and PostgreSQL
 
Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)Flat notes iii i (1)(7-9-20)
Flat notes iii i (1)(7-9-20)
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
 
Loop invarient
Loop invarientLoop invarient
Loop invarient
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Relational Calculus
Relational CalculusRelational Calculus
Relational Calculus
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 

En vedette

Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)
Nitesh Singh
 

En vedette (13)

Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In Prolog
 
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
 
PROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In PrologPROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In 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 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
 
Prolog programming
Prolog programmingProlog programming
Prolog programming
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)
 
Prolog 7-Languages
Prolog 7-LanguagesProlog 7-Languages
Prolog 7-Languages
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introduction
 
PROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In PrologPROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In 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 Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : Basics
 
Prolog basics
Prolog basicsProlog basics
Prolog basics
 

Similaire à "That scripting language called Prolog"

Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow AnalysisDetecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Silvio Cesare
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
CSR2011
 
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
Jie Bao
 
09 logic programming
09 logic programming09 logic programming
09 logic programming
saru40
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
Eelco Visser
 

Similaire à "That scripting language called Prolog" (20)

Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Pl vol1
Pl vol1Pl vol1
Pl vol1
 
Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)
 
Name binding with scope graphs
Name binding with scope graphsName binding with scope graphs
Name binding with scope graphs
 
Detecting paraphrases using recursive autoencoders
Detecting paraphrases using recursive autoencodersDetecting paraphrases using recursive autoencoders
Detecting paraphrases using recursive autoencoders
 
Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow AnalysisDetecting Bugs in Binaries Using Decompilation and Data Flow Analysis
Detecting Bugs in Binaries Using Decompilation and Data Flow Analysis
 
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
 
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
 
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
QMC Program: Trends and Advances in Monte Carlo Sampling Algorithms Workshop,...
 
Origins of Free
Origins of FreeOrigins of Free
Origins of Free
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Variational autoencoders for speech processing d.bielievtsov dataconf 21 04 18
Variational autoencoders for speech processing d.bielievtsov dataconf 21 04 18Variational autoencoders for speech processing d.bielievtsov dataconf 21 04 18
Variational autoencoders for speech processing d.bielievtsov dataconf 21 04 18
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
 
070
070070
070
 
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
 
Introduction to Prolog
Introduction to PrologIntroduction to Prolog
Introduction to Prolog
 
09 logic programming
09 logic programming09 logic programming
09 logic programming
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
 
Locality-sensitive hashing for search in metric space
Locality-sensitive hashing for search in metric space Locality-sensitive hashing for search in metric space
Locality-sensitive hashing for search in metric space
 

Dernier

Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Sérgio Sacani
 
Human genetics..........................pptx
Human genetics..........................pptxHuman genetics..........................pptx
Human genetics..........................pptx
Silpa
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
Silpa
 
LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.
Silpa
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
levieagacer
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
Silpa
 

Dernier (20)

Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
Human genetics..........................pptx
Human genetics..........................pptxHuman genetics..........................pptx
Human genetics..........................pptx
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptx
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 
Atp synthase , Atp synthase complex 1 to 4.
Atp synthase , Atp synthase complex 1 to 4.Atp synthase , Atp synthase complex 1 to 4.
Atp synthase , Atp synthase complex 1 to 4.
 
Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS ESCORT SERVICE In Bhiwan...
Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS  ESCORT SERVICE In Bhiwan...Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS  ESCORT SERVICE In Bhiwan...
Bhiwandi Bhiwandi ❤CALL GIRL 7870993772 ❤CALL GIRLS ESCORT SERVICE In Bhiwan...
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditions
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.
 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx
 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptx
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICEPATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
PATNA CALL GIRLS 8617370543 LOW PRICE ESCORT SERVICE
 
Chemistry 5th semester paper 1st Notes.pdf
Chemistry 5th semester paper 1st Notes.pdfChemistry 5th semester paper 1st Notes.pdf
Chemistry 5th semester paper 1st Notes.pdf
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
 

"That scripting language called Prolog"

  • 1. That scripting language called Prolog Sergei Winitzki Types, Theorems, and Programming Languages June 16, 2014 Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 1 / 31
  • 2. The four programming paradigms Paradigm Example Programs are... Diculty is in... Best used in... Imperative Fortran lists of commands order of updates numerics Functional Lisp expressions level of abstraction compilers Logic Prolog sets of predicates runtime behavior symbolic AI Constraint TEX data + constraints conicting modules specic domain Objects, type systems, modules, concurrency, etc., are merely features added on top of a chosen paradigm Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 2 / 31
  • 3. A denition of declarative Programming is declarative when specications are programs. More formally: A language L is declarative for an application domain D if: The domain D has a good specication formalism F good = visual, pragmatically convenient, complete for the domain D There is an obvious syntactic transformation from F to L The resulting program implements the specication Less formally: A declarative language is a perfect DSL for the given domain Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 3 / 31
  • 4. Example: declarative FORTRAN 77 Application domain: numerical mathematical expressions Specication: a mathematical formula involving numbers and functions Example specication: f (x, p, q) = sin px x2 − sin 2 qx x3 Implementation: F(X,P,Q)=SIN(P*X)/X**2-(SIN(Q*X))**2/X**3 For more complicated tasks, FORTRAN is not declarative ˜Xk = Yk − n j =k+1 Akj Xj , ∀k ∈ [1..n] (example code, 1987) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 4 / 31
  • 5. Example: declarative Haskell 98 Application domain: recursively dened, algebraic data structures Specications: inductive denitions of functions on ADTs Example (from R. Sedgewick, Algorithms in C, 1998) data BTree α = BTNode α | BTVertex α (BTree α) (BTree α) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 5 / 31
  • 6. Example: declarative Haskell 98, continued height :: BTree α → Int height (BTNode _) = 0 height (BTVertex _ t1 t2) = 1 + max (height t1) (height t2) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 6 / 31
  • 7. Example: non-declarative Haskell For a dierent application domain, Haskell is not declarative! Downloading data from server (from Real World Haskell, 2008) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 7 / 31
  • 8. Prolog as a DSL for logic puzzles All jumping creatures are green. All small jumping creatures are martians. All green martians are intelligent. Ngtrks is small and green. Pgvdrk is a jumping martian. Who is intelligent? (inpired by S. Lem, Invasion from Aldebaran) small(ngtrks). green(ngtrks). martian(pgvdrk). jumping(pgvdrk). green(X) :- jumping(X). martian(X) :- small(X), jumping(X). intelligent(X) :- green(X), martian(X). main :- intelligent(X), format('~w is intelligent.~n', X), halt. $ swipl -o martians -q -t main -c martians.pl $ ./martians pgvdrk is intelligent. Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 8 / 31
  • 9. Prolog in a nutshell 1: symbols, predicates, rules Outer-level lowercase symbols are logical predicates All other lowercase symbols are symbolic constants All capitalized symbols are quantied logical variables (LVs) LVs on the left imply ∀; free LVs on the right imply ∃ The symbol :- means implication ←, the comma means and Rules can be given in any order; no declarations Examples: green(X) :- jumping(X) means ∀X : green(X) ← jumping(X) path(A,B) :- path(A,C), path(C,B) means ∀A, B : [∃C : path(A, C) ∧ path(C, B) → path(A, B)] main :- intelligent(X) means: prove that ∃X:intelligent(X) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 9 / 31
  • 10. Prolog in a nutshell 2: variables Martians recap: small(ngtrks). green(ngtrks). martian(pgvdrk). jumping(pgvdrk). green(X) :- jumping(X). martian(X) :- small(X), jumping(X). intelligent(X) :- green(X), martian(X). main :- intelligent(X) means: prove that ∃X:intelligent(X) The Prolog engine will prove existence of X by backtracking search! (we can say trace and follow the search) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 10 / 31
  • 11. Prolog in a nutshell 2: backtracking search Martians recap: small(ngtrks). green(ngtrks). martian(pgvdrk). jumping(pgvdrk). green(X) :- jumping(X). martian(X) :- small(X), jumping(X). intelligent(X) :- green(X), martian(X). ?- intelligent(X). [trace] ?- intelligent(X). Call: (6) intelligent(_G2442) Call: (7) green(_G2442) Exit: (7) green(ngtrks) Call: (7) martian(ngtrks) Call: (8) small(ngtrks) Exit: (8) small(ngtrks) Call: (8) jumping(ngtrks) Fail: (8) jumping(ngtrks) Fail: (7) martian(ngtrks) Redo: (7) green(_G2442) Call: (8) jumping(_G2442) Exit: (8) jumping(pgvdrk) Exit: (7) green(pgvdrk) Call: (7) martian(pgvdrk) Exit: (7) martian(pgvdrk) Exit: (6) intelligent(pgvdrk) X = pgvdrk . The proof may fail, or may succeed in more than one way LVs are assigned by unication and unassigned on backtracking Once assigned, a logical variable is immutable This is called resolution of Horn clauses and unication Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 11 / 31
  • 12. Horn clauses and resolution Consider a restricted fragment of predicate logic: Expressions are conjunctions of Horn clauses: (a ∧ b ∧ ... ∧ c → d ) ∧ (p ∧ q ∧ ... ∧ r → s) ∧ (True → w ) ∧ ... Disjunction is expressible through Horn clauses: (a ∨ b) → c = (a → c) ∧ (b → c) Only ∀ quantiers are allowed, and only outside: ∀X ∀Y : a(X , Y ) ∧ b(Y ) → c(X ) Prolog syntax: quantiers are implicit; implication points leftward Resolution: the b is eliminated from two clauses (c ← a ∧ p ∧ q) ⇐ b ← a ∧ p c ← b ∧ q Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 12 / 31
  • 13. Examples: recursive predicates Factorial predicate: fact(N,F) holds if F = N! fact(1,1). fact(N,F) :- M is N-1, fact(M,E), F is E*N. Fibonacci predicate: bo(N,F) holds if F is N-th Fibonacci number bo(0,1). bo(1,1). bo(N,F) :- M1 is N-1, bo(M1,E1), M2 is N-2, bo(M2,E2), F is E1+E2. Instead of computing F through calling a function, we prove existence of a value F such that some predicate holds. Prolog has only predicates no declarations, expressions, functions, variables, or static types Note: M is N-1 resembles an expression but is actually a predicate! it means is(M,'-'(N,1)) , where '-' is an inx data constructor! Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 13 / 31
  • 14. Prolog in a nutshell 2: syntax extensions Syntax for data structures: passive predicates (a distinct namespace) left(pair(X,Y), X). right(pair(X,Y), Y). since pair(X,Y) is inside a predicate, it must be data however, pair(X,Y) can contain free logical variables and so can contain any other structures (no typechecking!): pair(pair(X,Y),pair(Y,pair(Z,T))) Syntax sugar for lists: [] or [a,b,c] or [a,b,c|[]] Similar to Haskell's a:b:c:[] Examples: head([X|Xs], X). tail([X|Xs], Xs). empty([]). length([], 0). length([X|Xs],N) = length(Xs,M), N is M+1. User-dened syntax: inx, prex, postx op(400, xfy, *). op(300, yfx, ^). op(200, xf, :!). Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 14 / 31
  • 15. What is unication? Unication is Prolog's way of assigning values to variables Pattern-matching of recursive structures, tting unassigned variables Assignment is propagated to other rules: like(1). like(2). like(you_know(Y,X)) :- like(X), like(Y). really(A,B) :- like(A), like(you_know(A,B). The predicate really(1,2) holds because like(you_know(Y,X)) is unied with like(you_know(1,2)) to set Y = 1 and X = 2 Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 15 / 31
  • 16. Pointers made safe: dierence lists Appending ordinary lists takes O(n) operations: l_append([], X, X). l_append([X|Xs], Y, [X|Zs]) :- l_append(Xs, Y, Zs). To optimize, we need a pointer to the end of the list! having a pointer = a part of a data structure is not yet assigned solution: a free LV is exposed, unied with some part of the data Dierence lists: pair([a,b,c|X],X) or [a,b,c|X]-X op(500, xfy, -). empty(X-X). dl_append(X-Y,Y-Z,X-Z). /* O(1) operations! */ [trace] ?- dl_append( [a,b,c|A]- A, [d,e|B]-B, C). dl_append([a, b, c|_G2447]-_G2447, [d, e|_G2456]-_G2456, _G2463) dl_append([a, b, c, d, e|_G2456]-[d, e|_G2456], [d, e|_G2456]-_G2456, [a, b, c, d, e|_G2456]-_G2456) A = [d, e|B], C = [a, b, c, d, e|B]-B. Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 16 / 31
  • 17. Pointers made safe: dierence lists How this works in more detail: We have the program dl_append(X-Y,Y-Z,X-Z). We have the query ?- dl_append([a,b,c|A]-A, [d,e|B]-B, C). The value of Y can be unied with the query structure only if we assign Y=A and also Y=[d,e|B] and Z=B. Thus we must have A=[d,e|B] Therefore X must be assigned [a,b,c|A]=[a,b,c,d,e|B] Finally C=X-Z=[a,b,c,d,e|B]-B Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 17 / 31
  • 18. Pointers made safe: queues Implement insertion at end of queue: list_to_queue(L,Q-Y) :- l_append(L,Y,Q). /* O(n) */ q_insert_at_end(E, Q-[E|Y], Q-Y). /* O(1) */ q_head(E, [E|Q]-Y, Q-Y). [trace] ?- q_insert_at_end( n, [a,b,c|X]-X, Q ). X = [n|_G1726], Q = [a, b, c, n|_G1726]-_G1726. ?- q_head( X, [a,b,c,n|Y]-Y, Z). X = a, Z = [b, c, n|Y]-Y. Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 18 / 31
  • 19. Pointers made safe: iterators A list iterator: set at begin, increment, check if at end Split the list into a pair of queues Eectively, we have a pointer to the middle of a list: :- op(600, xfx, ^^). plist_at_begin(X-X ^^ A-B). plist_at_end(A-B ^^ X-X). plist_incr(A-[X|B] ^^ [X|C]-D, A-B ^^ C-D). [trace] ?- plist_incr([a,b|X]-X ^^ [c,d,e|Y]-Y, P). plist_incr([a, b|_G1978]-_G1978 ^^ [c, d, e|_G1990]-_G1990, _G1999) plist_incr([a, b, c|_G2113]-[c|_G2113] ^^ [c, d, e|_G1990]-_G1990, [a, b, c|_G2113]-_G2113 ^^ [d, e|_G1990]-_G1990) X = [c|_G2113], P = [a, b, c|_G2113]-_G2113 ^^ [d, e|Y]-Y. Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 19 / 31
  • 20. Parsing with Denite Clause Grammars Top-down parsing with unlimited backtracking... expr ::== term | term oper expr | '(' expr ')' oper ::== 'and' | 'or' | ... term ::== 'true' | 'false' | ... ...is similar to Prolog's evaluation strategy on token queues: expr(Ts) :- term(Ts). expr(Ts-X) :- term(Ts-T1), op(T1-T2), expr(T2-X). oper([T|X]-X) :- T='and'. oper([T|X]-X) :- T='or'. /* etc. */ term([T|X]-X) :- T='true'. term([T|X]-X) :- T='false'. /* etc. */ Syntactic sugar (--) is provided to avoid writing out the queues: expr -- term. expr -- term, oper, expr. expr -- ['('], expr, [')']. oper -- ['and']. oper -- ['or']. term -- ['true']. term -- ['false']. /* test: */ ?- expr(['true', 'and', 'false', 'or', 'true'], []). Nonterminals can have extra arguments and call Prolog code No need for Lex/Yacc (but they do other grammars...) Can parse some non-CFG grammars (attributed grammars) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 20 / 31
  • 21. Interpreters A lambda-calculus interpreter in 3 lines of Prolog?... :- op(1190, xfy, ~). :- op(1180, yfx, @). comp(A~B, A~B). comp( (A~B) @ A, B). comp(A@B@C, R) :- comp(A@B,S), comp(S@C,R). comp(A@B, A@B). ...um, not really (lacking α-conversion and multiple steps) ?- comp( (X~Y~X) @ 1, Result). Result = (Y~1) . /* okay! */ ?- comp( (X~ X@X) @ (Y~Y), R1), comp(R1 @ 1,Res). X = Y, Y = (Y~Y), R1 = ((Y~Y)@ (Y~Y)), Res = ((Y~Y)@1) . /* ??? */ A Prolog module with about 15 clauses achieves this (hacky!): :- use_module(lambda). id - (X ~ X). const - (C ~ _ ~ C). ycomb - (F ~ (X ~ F @ (X @ X)) @ (X ~ F @ (X @ X))). fac - ( F ~ N ~ iff @ (N F = const; F = (const @ id) ). ?- lambda((y @ fac @ 4), Result). Result = 24 Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 21 / 31
  • 22. Prolog and relational databases Alice lost her umbrella on Monday. Brian lost his glasses on Tuesday. Chris lost his pen on Monday. Dennis lost his watch on Monday and his pen on Tuesday. Whoever loses something on Monday will be unlucky for the whole week. Who is unlucky for the whole week and also lost something on Tuesday? lost(alice, umbrella, monday). lost(brian, glasses, tuesday). lost(chris, pen, monday). lost(dennis, watch, monday). lost(dennis, pen, tuesday). unlucky(X) :- lost(X, _, monday). ?- unlucky(Who), lost(Who, _, tuesday). Our predicates are either facts or non-recursive inferences We will need no backtracking (unlike the Martians example) A query such as ?- unlucky(X) will usually return many results Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 22 / 31
  • 23. Prolog and relational databases We have seen this before... (it is called SQL) lost: person object day alice umbrella monday brian glasses tuesday chris pen monday dennis watch monday dennis pen tuesday unlucky: person alice chris dennis Our Prolog code... unlucky(X) :- lost(X, _, monday). ?- unlucky(Who), lost(Who, _, tuesday). ...is translated into SQL as: CREATE VIEW unlucky AS SELECT person FROM lost WHERE lost.day='monday'; SELECT person FROM unlucky NATURAL JOIN lost WHERE lost.day='tuesday'; Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 23 / 31
  • 24. Prolog, Datalog, and SQL SQL Datalog Prolog recursive data ? + recursive queries + + Horn clauses + + + control ow + functions + typed values + named values + Prolog + functions + types = functional-logic programming Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 24 / 31
  • 25. Functional-logic programming in Mercury Mercury is Prolog + some features of ML Immutable values, static type inference, algebraic polymorphism all predicates and all arguments are labeled with modes static detection of errors in predicate use functions are deterministic predicates with strict modes products, sums, labeled records, higher-order functions I/O by unique types modules and signatures à la Standard ML type classes à la Haskell standard library: arrays, multithreading, etc. can compile to high-performance machine code, C, Java, Erlang Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 25 / 31
  • 26. A taste of Mercury Read an integer and print its factorial :- module f. :- interface. :- import_module io. :- pred main(io::di, io::uo) is det. :- implementation. :- import_module int. :- pred fact(int::in, int::out) is det. :- import_module list, string. fact(N,F) :- ( if N = 1 then F = 1 else fact(N-1, A), F = A*N ). main(!IO) :- io.read_line_as_string(Result, !IO), ( if Result = ok(String), string.to_int(string.strip(String), N) then io.format(fact(%d) = %dn, [i(N), i(fact(N))], !IO) else io.format(That isn't a number...n, [], !IO) ). Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 26 / 31
  • 27. Prolog: A perfect scripting language No declarations, runtime (dynamic) typing, immutable values Data constructors and predicates have user-dened inx syntax Pattern-matching on recursive data structures, with backtracking Metaprogramming, reection, self-modifying code Easy to do embedded or external DSLs (monadic top-down parsing) ISO standard since 1995, many free implementations typically with a REPL and a compiler to high-performance VM Interface to databases, networking, multithreading, GUI, ... Core language is small; full language is hard to use? Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 27 / 31
  • 28. Compiling Prolog to a virtual machine Warren's Abstract Machine (WAM) is still the most inuential Instructions assume that WAM manages stack and heap Compilation of core Prolog to WAM is relatively straightforward: (D. H. D. Warren, 1983) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 28 / 31
  • 29. Conclusions and outlook Declarative programming = creating a good DSL for your domain It is easy to pick up Prolog, after seeing SQL and Haskell Prolog makes building DSLs easy (both internal and external DSLs) Mercury = Prolog + types + functions Prolog is almost forgotten, but its legacy lives on Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 29 / 31
  • 30. Suggested reading Free implementations I used: SWI-Prolog The Mercury programming language A great pedagogical introduction to Prolog and Datalog: D. Maier, D. S. Warren. Computing with logic: Logic programming with Prolog. Addison-Wesley, 1988 Advanced Prolog programming: E. Shapiro, L. Sterling. The art of Prolog. MIT, 1999 T. Van Le. Techniques of Prolog programming. Wiley, 1993 R. O'Keefe. The craft of Prolog. MIT, 1990 Implementation (the WAM is still an important source of inspiration): H. Aït-Kaci. Warren's Abstract Machine (WAM). MIT, 1991 W. F. Clocksin. Design and simulation of a sequential Prolog machine. New Gen. Comp., 3 (1985), p. 101 (describes the ZIP machine) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 30 / 31
  • 31. Summary What is logic programming and constraint programming Prolog in a nutshell How Prolog makes pointers safe Why Prolog was the ultimate scripting language for AI (backtracking search, interpreters, and DSLs for free) What is functional-logic programming (a taste of the programming language Mercury) Sergei Winitzki (Versal Group Inc.) That scripting language called Prolog June 16, 2014 31 / 31