prolog 7-languages

35
Seven Languages in Seven Months Language 3: Prolog Raymond P. de Lacaze Patch [email protected] 01/30/13

Upload: raymond-pierre-de-lacaze

Post on 25-May-2015

1.460 views

Category:

Technology


0 download

DESCRIPTION

This is a presentation given January 2013 at the 7-Languages-in-7-Months New York meetup group.

TRANSCRIPT

Page 1: Prolog 7-Languages

Seven Languages in Seven Months

Language 3: PrologRaymond P. de Lacaze

[email protected]

01/30/13

Page 2: Prolog 7-Languages

Overview

• Part 1: Introduction to PrologChapter 4 of Seven Languages in Seven Weeks

• Part 2: Advanced TopicsChapters X-Y of The Art of Prolog

• Part 3: Prolog Today

Page 3: Prolog 7-Languages

Prolog Introduction

• Prolog is a declarative language• Prolog is logic programming language• Invented in 1972 by Colmerauer & Roussel– Edinburg Prolog– Marseilles Prolog

• Initially used for Natural Language Processing• Programs consist of fact & rules• Fact is a clause in FOPC• Rule is an inference: B A1,…,An• Use queries to run programs and perform retrievals

Page 4: Prolog 7-Languages

The Logic Programming Model• Logic Programming is an abstract model of computation.• Lambda Calculus is another abstract model of computation.• Prolog is a particular implementation of the logic

programming model in much the same way that Clojure and Haskell are particular implementation of the lambda calculus.

• OPS5 is another implementation of the logic programming model.

• The use of mathematical logic to represent and execute computer programs is also a feature of the lambda calculus

• Prolog is classified as a functional language (wikipedia)

Page 5: Prolog 7-Languages

Logic Programming Paradigm

• A program is logical description of your problem from which a solution is logically derivable

• The execution of a program is very much like the mathematical proof of a theorem

• Where’s my program?• N! is (N-1)! times N• 0! is 1

Page 6: Prolog 7-Languages

Prolog Facts

• Facts: <predicate>(<arg1>,…,<argN>)• Example: likes(mary, john)• Constants must be in lowercase• Variables must be in uppercase or start with

an underscore.• Example: eats(mikey, X)• Example: believes(peter, likes(mary, john))

Page 7: Prolog 7-Languages

Basic Inferences & Variableslikes(john, cheese)likes(mary, cheese)likes(bob, meat)similar(X,Y) :- likes(X,Z), likes(Y,Z)

Note: You can use [‘<filename/pathname>’]. to compile and load files

GNU Prolog 1.4.1By Daniel DiazCopyright (C) 1999-2012 Daniel Diaz

| ?- ['C:\\Projects\\Languages\\code\\Prolog\\similar.pl'].compiling C:/Projects/Languages/code/Prolog/similar.pl for byte code...C:/Projects/Languages/code/Prolog/similar.pl compiled, 4 lines read - 935 bytes written, (16 ms) yes

Page 8: Prolog 7-Languages

Filling in the Blanks| ?- similar(john, mary).yes

| ?- similar(john, bob).no

| ?- similar(mary, X).X = john ? yes

| ?- similar(X, Y).

X = johnY = john ? ;

X = johnY = mary ? ;

X = maryY = john ? ;

X = maryY = mary ?

Note: can use ; and a to get next or all answers

Page 9: Prolog 7-Languages

Map Coloring (1)different(red, green). different(red, blue). different(green, red). different(green, blue). different(blue, red). different(blue, green).

coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :- different(Mississippi, Tennessee), different(Mississippi, Alabama), different(Alabama, Tennessee), different(Alabama, Mississippi), different(Alabama, Georgia), different(Alabama, Florida), different(Georgia, Florida), different(Georgia, Tennessee).

Page 10: Prolog 7-Languages

Map Coloring (2)| ?- ['c:\\projects\\languages\\code\\prolog\\map.pl'].compiling c:/projects/languages/code/prolog/map.pl for byte code...c:/projects/languages/code/prolog/map.pl compiled, 16 lines read - 1716 bytes written, 16 msyes

| ?- coloring(Alabama, Mississippi, Georgia, Tennessee, Florida).

Alabama = blueFlorida = greenGeorgia = redMississippi = redTennessee = green ?

(16 ms) yes

Page 11: Prolog 7-Languages

Unification (1)• The Unification Algorithm is a famous algorithm from

the field of AI, often used in theorem proving, game playing, planning, etc…

• It can loosely be thought of as an algorithm that tries to make to non-grounded terms the same.

• P(X, 2) = P(1, Y) X=1 & Y=2 P(1, 2)• P(X, X) = P(Y, 5) X=5 & Y=5 P (5, 5)• P(X, Y) = P(2, Z) X=2 & Y=Z P (2, Z)

• See Artificial Intelligence (Russell & Norvig)

Page 12: Prolog 7-Languages

Prolog Rules

• Rules: <head> :- <body>• Head: Single clause typically with variables• Body: Conjunction of goals with variables• Examples:

ancestor(X,Y) :- parent(X,Y)ancestor(X,Y) :- parent(X,Z), parent(Z,Y)ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y)

Page 13: Prolog 7-Languages

A Recursive Example(1)parent(p1, p2). parent(p2, p3). parent(p3, p4).ancestor(X, Y) :- parent(X, Y).ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).

| ?- ['c:\\projects\\languages\\code\\prolog\\ancestor.pl'].compiling c:/projects/languages/code/prolog/ancestor.pl for byte code...c:/projects/languages/code/prolog/ancestor.pl compiled, 5 lines read - 818 bytes written, 13 ms

yes

Page 14: Prolog 7-Languages

A Recursive Example(2)| ?- trace.The debugger will first creep -- showing everything (trace)yes{trace}

| ?- ancestor(p1, p4). 1 1 Call: ancestor(p1,p4) ? 2 2 Call: parent(p1,p4) ? 2 2 Fail: parent(p1,p4) ? 2 2 Call: parent(p1,_80) ? 2 2 Exit: parent(p1,p2) ? 3 2 Call: ancestor(p2,p4) ? 4 3 Call: parent(p2,p4) ? 4 3 Fail: parent(p2,p4) ? 4 3 Call: parent(p2,_129) ? 4 3 Exit: parent(p2,p3) ? 5 3 Call: ancestor(p3,p4) ? 6 4 Call: parent(p3,p4) ? 6 4 Exit: parent(p3,p4) ? 5 3 Exit: ancestor(p3,p4) ? 3 2 Exit: ancestor(p2,p4) ? 1 1 Exit: ancestor(p1,p4) ?

true ?

(63 ms) yes

Page 15: Prolog 7-Languages

Using Rules in Both Directions// Find all ancestors| ?- ancestor(X, p4).

X = p3 ? ;X = p1 ? ;X = p2 ? ;

no

// Find all descendants| ?- ancestor(p1, X).

X = p2 ? ;X = p3 ? ;X = p4 ? ;

no

Page 16: Prolog 7-Languages

Lists and Tuples (1)• List are denoted by comma-separated values in square brackets. i.e.

[1, 2, 3]• Tuples are denoted by comma-separated values in parentheses. i.e.

(1, 2, 3)

| ?- [1, 2, 3] = [X, Y, Z].

X = 1Y = 2Z = 3

yes

Page 17: Prolog 7-Languages

Accessing Elements of a List

| ?- [1, 2, 3] = [X | Y].X = 1Y = [2,3]

| ?- [1, 2, 3] = [_, X | Y].X = 2Y = [3]

Page 18: Prolog 7-Languages

Lists and Math (1)count(0, []).count(Count, [Head|Tail]) :-

count(TailCount, Tail), Count is TailCount + 1.

sum(0, []).sum(Total, [Head|Tail]) :-

sum(Sum, Tail), Total is Head + Sum.

average(Average, List) :- sum(Sum, List), count(Count, List), Average is Sum/Count.

Page 19: Prolog 7-Languages

Lists and Math (2)| ?- sum(S, [1,2,3]). 1 1 Call: sum(_17,[1,2,3]) ? 2 2 Call: sum(_92,[2,3]) ? 3 3 Call: sum(_116,[3]) ? 4 4 Call: sum(_140,[]) ? 4 4 Exit: sum(0,[]) ? 5 4 Call: _168 is 3+0 ? 5 4 Exit: 3 is 3+0 ? 3 3 Exit: sum(3,[3]) ? 6 3 Call: _197 is 2+3 ? 6 3 Exit: 5 is 2+3 ? 2 2 Exit: sum(5,[2,3]) ? 7 2 Call: _17 is 1+5 ? 7 2 Exit: 6 is 1+5 ? 1 1 Exit: sum(6,[1,2,3]) ?

S = 6 ?

Page 20: Prolog 7-Languages

Solving Sudoku (1)valid([]).valid([Head|Tail]) :-

valid(Tail), fd_all_different(Head).

sudoku(S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, S44, Board) :-

Board = [S11, S12, S13, S14, S21, S22, S23, S24, S31, S32, S33, S34, S41, S42, S43, S44, ],

fd_domain(Board, 1, 4),

Row1 = [S11, S12, S13, S14], Row2 = [S21, S22, S23, S24], Row3 = [S31, S32, S33, S34], Row4 = [S41, S42, S43, S44],

Col1 = [S11, S21, S31, S41], Col2 = [S12, S22, S32, S42], Col3 = [S13, S23, S33, S43], Col4 = [S14, S24, S34, S44],

Square1 = [S11, S12, S21, S22], Square2 = [S13, S14, S23, S24], Square3 = [S31, S32, S41, S42,], Square4 = [S33, S34, S43, S44,],

valid([Row1, Row2, Row3, Row4,]), valid([Col1, Col2, Col3, Col4, ]), valid([Square1, Square2, Square3, Square4]).

Page 21: Prolog 7-Languages

Solving Sudoku (2)| ?- sudoku(_, _, 2, 3, _, _, _, _,

_, _ ,_, _,3, 4, _, _, Solution).

Solution = [4, 1, 2, 3,2, 3, 4, 1,1, 2, 3, 4,3, 4, 1, 2]

Page 22: Prolog 7-Languages

Solving Sudoku (3)

• Finite Domain variables: A new type of data is introduced: FD variables which can only take values in their domains. The initial domain of an FD variable is 0..fd_max_integer where fd_max_integer represents the greatest value that any FD variable can take.

• fd_domain(Board, 1, 4). Used to specify the range of values of each Sudoku cell.

• fd_all_different(X).Used to specify that all elemts in the list must have distinct values.

Page 23: Prolog 7-Languages

Part 2: Advanced Topics

• The Art of Prolog, Sterling and Shapiro, MIT Press, 1986

• Structure Inspection

• Meta-Logical Predicates

• Cuts (and Negation)

• Extra-Logical Predicates

Page 24: Prolog 7-Languages

Structure Inspection| ?- functor(father(tom, harry), P, A).A = 2P = fatherYes

| ?- arg(1,father(tom, harry), A1).A1 = tomYes

| ?- arg(2,father(tom, harry), A2).A2 = harryyes

| ?- functor(X, father, 2).X = father(_, _)Yes

| ?- father(tom, harry) =.. [X, Y, Z].X = fatherY = tomZ = harryyes

| ?- X =.. [father, tom, harry].X = father(tom, harry)yes

| ?- X =.. [father, tom, harry], assertz(X).X = father(tom, harry)yes

| ?- father(tom, harry).yes

functor, arg and =..

Page 25: Prolog 7-Languages

Meta-Logical Predicates• Outside scope of first-order logic

• Query and affect the state of the proof• Treat variables as objects• Convert data structures to goals

• Type Predicates:• var(<term>)• nonvar(<term>)

• Variables as objects: freeze & melt• Dynamically Affecting the Knowledge Base

• assert(<goal>)• retract(<goal>)

• The Meta-Variable Facility: call(<goal>)• Memoization: lemma(<goal>)

Page 26: Prolog 7-Languages

Extra-Logical Predicates

• These achieve side-effects as a result of being logically true

• Three types of extra-logical predicates• Input / Output– read & write

• Accessing and manipulating the program– clause(Head, Body)– assert (X)– retract(X)

• Interfacing to the Operating System

Page 27: Prolog 7-Languages

Cuts (1)

• Predicate cut or ! affects procedural behavior• Main purpose is to reduce the search space• It’s use is controversial: purity vs. efficiency• Green Cuts: Express determinism• Consider merging two sorted lists• Only one of X<Y, X=Y or X>Y can succeed• Once one succeeds, no need for the others.

Page 28: Prolog 7-Languages

Merging without Cuts

merge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, merge(Xs, [Y|Ys], Zs).merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, merge(Xs, Ys, Zs).merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, merge([X|Xs], Ys, Zs).merge(Xs, [], Xs).merge([], Ys, Ys).

| ?- merge([1,3,5],[2,3], Xs).Xs = [1,2,3,3,5] ? yes

Page 29: Prolog 7-Languages

Merging with Cuts

merge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, !, merge(Xs, [Y|Ys], Zs).merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, !, merge(Xs, Ys, Zs).merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, !, merge([X|Xs], Ys, Zs).merge(Xs, [], Xs) :- !.merge([], Ys, Ys) :- !.

?- merge([1,3,5],[2,3], Xs).Xs = [1,2,3,3,5]yes

Page 30: Prolog 7-Languages

The Effect of Cutsmerge([1,3,4],[2, 5], Xs). 1 1 Call: merge([1,3,4],[2,5],_27) ? 2 2 Call: 1>2 ? 2 2 Fail: 1>2 ? 2 2 Call: 1<2 ? 2 2 Exit: 1<2 ? 3 2 Call: merge([3,4],[2,5],_60) ? 4 3 Call: 3>2 ? 4 3 Exit: 3>2 ? 5 3 Call: merge([3,4],[5],_114) ? 6 4 Call: 3>5 ? 6 4 Fail: 3>5 ? 6 4 Call: 3<5 ? 6 4 Exit: 3<5 ? 7 4 Call: merge([4],[5],_168) ? 8 5 Call: 4>5 ? 8 5 Fail: 4>5 ? 8 5 Call: 4<5 ? 8 5 Exit: 4<5 ? 9 5 Call: merge([],[5],_222) ? 9 5 Exit: merge([],[5],[5]) ? 7 4 Exit: merge([4],[5],[4,5]) ? 5 3 Exit: merge([3,4],[5],[3,4,5]) ? 3 2 Exit: merge([3,4],[2,5],[2,3,4,5]) ? 1 1 Exit: merge([1,3,4],[2,5],[1,2,3,4,5]) ?

Xs = [1,2,3,4,5] ?

| ?- merge([1,3,4],[2, 5], Xs). 1 1 Call: merge([1,3,4],[2,5],_27) ? 2 2 Call: 1>2 ? 2 2 Fail: 1>2 ? 2 2 Call: 1<2 ? 2 2 Exit: 1<2 ? 3 2 Call: merge([3,4],[2,5],_60) ? 4 3 Call: 3>2 ? 4 3 Exit: 3>2 ? 5 3 Call: merge([3,4],[5],_114) ? 6 4 Call: 3>5 ? 6 4 Fail: 3>5 ? 6 4 Call: 3<5 ? 6 4 Exit: 3<5 ? 7 4 Call: merge([4],[5],_168) ? 8 5 Call: 4>5 ? 8 5 Fail: 4>5 ? 8 5 Call: 4<5 ? 8 5 Exit: 4<5 ? 9 5 Call: merge([],[5],_222) ? 9 5 Exit: merge([],[5],[5]) ? 7 4 Exit: merge([4],[5],[4,5]) ? 5 3 Exit: merge([3,4],[5],[3,4,5]) ? 3 2 Exit: merge([3,4],[2,5],[2,3,4,5]) ? 1 1 Exit: merge([1,3,4],[2,5],[1,2,3,4,5]) ?

Xs = [1,2,3,4,5]

Page 31: Prolog 7-Languages

Prolog Today• GNU Prolog Compiler

Full I/O capabilitiesComplete Socket programming APICan be linked to C in both directions

• Approx. 20 existing Prolog implementations

• Some other popular ones: SWI-Prolog & Visual Prolog

• http://en.wikipedia.org/wiki/Comparison_of_Prolog_implementations

• http://orgnet.com/inflow3.htmlSoftware for Social Network Analysis & Organizational Network Analysis

Page 32: Prolog 7-Languages
Page 33: Prolog 7-Languages

core.logic

• Clojure core.logic (David Nolen) This is a Clojure implementation of miniKanren. miniKanren is a Logic Programming library embedded in Scheme based The Reasoned Schemer by Daniel Friedman.

• Another implementation of the Logic Programming Paradigm

• Presented at Strange Loop 2012

• Very well received by the Clojure community

• https://github.com/clojure/core.logic

Page 34: Prolog 7-Languages

AllegroGraph (Franz Inc.)• AllegroGraph is one the leading graph database and application frameworks for

building Semantic Web applications.

• It can store data and meta-data as trillions of triples

• Query these triples through using PROLOG

• Query these triples using SPARQL (the standard W3C query language)

• AllegroGraph includes support for Federation, Social Network Analysis, Geospatial capabilities and Temporal reasoning

• AllegroGraph is implemented in Common Lisp & CLOS.

• http://www.franz.com/agraph/allegrograph/

Page 35: Prolog 7-Languages

References

Seven Languages in Seven WeeksBruce A. Tate, Pragmatic Programmers LLC, 2010

The Art of Prolog: Advanced Programming TechniquesSterling & Shapiro, MIT Press, 1986

GNU Prolog User ManualDaniel Diaz, November 2012

Artificial Intelligence: A Modern ApproachRussell & Norvig, Prentice Hall, 1995