SlideShare una empresa de Scribd logo
1 de 31
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 
AI Propositional logic
AI Propositional logicAI Propositional logic
AI Propositional logicSURBHI SAROHA
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRRiazul Islam
 
Unit 1 rules of inference
Unit 1  rules of inferenceUnit 1  rules of inference
Unit 1 rules of inferenceraksharao
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy AlgorithmWaqar Akram
 
Asymptotes and holes 97
Asymptotes and holes 97Asymptotes and holes 97
Asymptotes and holes 97swartzje
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rulesHarini Balamurugan
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptishan743441
 
Rules of inference
Rules of inferenceRules of inference
Rules of inferenceLakshmi R
 
Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)Ikhtiar Khan Sohan
 
Lesson3.1 The Derivative And The Tangent Line
Lesson3.1 The Derivative And The Tangent LineLesson3.1 The Derivative And The Tangent Line
Lesson3.1 The Derivative And The Tangent Lineseltzermath
 
#5 formal methods – hoare logic
#5 formal methods – hoare logic#5 formal methods – hoare logic
#5 formal methods – hoare logicSharif Omar Salem
 
Lesson 1 derivative of trigonometric functions
Lesson 1 derivative of trigonometric functionsLesson 1 derivative of trigonometric functions
Lesson 1 derivative of trigonometric functionsLawrence De Vera
 
recursion tree method.pdf
recursion tree method.pdfrecursion tree method.pdf
recursion tree method.pdfMalikShazen
 
Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzerahmed51236
 

La actualidad más candente (20)

CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
AI Propositional logic
AI Propositional logicAI Propositional logic
AI Propositional logic
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
 
Unit 1 rules of inference
Unit 1  rules of inferenceUnit 1  rules of inference
Unit 1 rules of inference
 
Wordnet
WordnetWordnet
Wordnet
 
Greedy Algorithm
Greedy AlgorithmGreedy Algorithm
Greedy Algorithm
 
Asymptotes and holes 97
Asymptotes and holes 97Asymptotes and holes 97
Asymptotes and holes 97
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rules
 
Chapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).pptChapter 5 Graphs (1).ppt
Chapter 5 Graphs (1).ppt
 
Relations
RelationsRelations
Relations
 
Rules of inference
Rules of inferenceRules of inference
Rules of inference
 
Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)Discrete Math Presentation(Rules of Inference)
Discrete Math Presentation(Rules of Inference)
 
Lesson3.1 The Derivative And The Tangent Line
Lesson3.1 The Derivative And The Tangent LineLesson3.1 The Derivative And The Tangent Line
Lesson3.1 The Derivative And The Tangent Line
 
LSA algorithm
LSA algorithmLSA algorithm
LSA algorithm
 
#5 formal methods – hoare logic
#5 formal methods – hoare logic#5 formal methods – hoare logic
#5 formal methods – hoare logic
 
Lesson 1 derivative of trigonometric functions
Lesson 1 derivative of trigonometric functionsLesson 1 derivative of trigonometric functions
Lesson 1 derivative of trigonometric functions
 
recursion tree method.pdf
recursion tree method.pdfrecursion tree method.pdf
recursion tree method.pdf
 
Shortest Path Problem
Shortest Path ProblemShortest Path Problem
Shortest Path Problem
 
NLP_KASHK:Smoothing N-gram Models
NLP_KASHK:Smoothing N-gram ModelsNLP_KASHK:Smoothing N-gram Models
NLP_KASHK:Smoothing N-gram Models
 
Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzer
 

Destacado

Prolog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologProlog: Arithmetic Operations In Prolog
Prolog: Arithmetic Operations In PrologPROLOG CONTENT
 
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: Database Manipulation In Prolog
PROLOG: Database Manipulation In PrologPROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation 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 PrologPROLOG CONTENT
 
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
 
Prolog programming
Prolog programmingProlog programming
Prolog programmingHarry Potter
 
Logic programming (1)
Logic programming (1)Logic programming (1)
Logic programming (1)Nitesh Singh
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introductionwahab khan
 
PROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In PrologPROLOG: Database Manipulation In Prolog
PROLOG: Database Manipulation In PrologDataminingTools Inc
 
PROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologPROLOG: Cuts And Negation In Prolog
PROLOG: Cuts And Negation In PrologDataminingTools Inc
 

Destacado (12)

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 basics
Prolog basicsProlog basics
Prolog basics
 

Similar a "That scripting language called Prolog"

Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Programming with Millions of Examples (HRL)
Programming with Millions of Examples (HRL)Eran Yahav
 
Name binding with scope graphs
Name binding with scope graphsName binding with scope graphs
Name binding with scope graphsEelco Visser
 
Detecting paraphrases using recursive autoencoders
Detecting paraphrases using recursive autoencodersDetecting paraphrases using recursive autoencoders
Detecting paraphrases using recursive autoencodersFeynman Liang
 
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 AnalysisSilvio Cesare
 
Dedalo, looking for Cluster Explanations in a labyrinth of Linked Data
Dedalo, looking for Cluster Explanations in a labyrinth of Linked DataDedalo, looking for Cluster Explanations in a labyrinth of Linked Data
Dedalo, looking for Cluster Explanations in a labyrinth of Linked DataVrije Universiteit Amsterdam
 
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
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolutionEelco Visser
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
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 18Olga Zinkevych
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCSR2011
 
A Distributed Tableau Algorithm for Package-based Description Logics
A Distributed Tableau Algorithm for Package-based Description LogicsA Distributed Tableau Algorithm for Package-based Description Logics
A Distributed Tableau Algorithm for Package-based Description LogicsJie Bao
 
09 logic programming
09 logic programming09 logic programming
09 logic programmingsaru40
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)Eelco Visser
 
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 Eliezer Silva
 

Similar a "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
 

Último

Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyDrAnita Sharma
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfSumit Kumar yadav
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRDelhi Call girls
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfSumit Kumar yadav
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)Areesha Ahmad
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)Areesha Ahmad
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000Sapana Sha
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencySheetal Arora
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdfPests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdfPirithiRaju
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bSérgio Sacani
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)Areesha Ahmad
 

Último (20)

Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomology
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)GBSN - Microbiology (Unit 1)
GBSN - Microbiology (Unit 1)
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdfPests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
Pests of cotton_Borer_Pests_Binomics_Dr.UPR.pdf
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)
 

"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