Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Prolog 7-Languages

2,605 views

Published on

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

Published in: Technology

Prolog 7-Languages

  1. 1. Seven Languages in Seven MonthsLanguage 3: PrologRaymond P. de LacazePatchraymond.delacaze@patch.com01/30/13
  2. 2. 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
  3. 3. Prolog Introduction• Prolog is a declarative language• Prolog is logic programming language• Invented in 1972 by Colmerauer & Roussel– Edinburg Prolog– Marseilles Prolog• Initially used for Natural Language Processing• Programs consist of fact & rules• Fact is a clause in FOPC• Rule is an inference: B A1,…,An• Use queries to run programs and perform retrievals
  4. 4. The Logic Programming Model• Logic Programming is an abstract model of computation.• Lambda Calculus is another abstract model ofcomputation.• Prolog is a particular implementation of the logicprogramming model in much the same way that Clojureand Haskell are particular implementation of the lambdacalculus.• OPS5 is another implementation of the logic programmingmodel.• The use of mathematical logic to represent and executecomputer programs is also a feature of the lambdacalculus• Prolog is classified as a functional language (wikipedia)
  5. 5. Logic Programming Paradigm• A program is logical description of yourproblem from which a solution is logicallyderivable• The execution of a program is very much likethe mathematical proof of a theorem• Where’s my program?• N! is (N-1)! times N• 0! is 1
  6. 6. Prolog Facts• Facts: <predicate>(<arg1>,…,<argN>)• Example: likes(mary, john)• Constants must be in lowercase• Variables must be in uppercase or start withan underscore.• Example: eats(mikey, X)• Example: believes(peter, likes(mary, john))
  7. 7. 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 filesGNU Prolog 1.4.1By Daniel DiazCopyright (C) 1999-2012 Daniel Diaz| ?- [C:ProjectsLanguagescodePrologsimilar.pl].compiling C:/Projects/Languages/code/Prolog/similar.pl for byte code...C:/Projects/Languages/code/Prolog/similar.pl compiled, 4 lines read - 935bytes written,(16 ms) yes
  8. 8. 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
  9. 9. Map Coloring (1)different(red, green).different(red, blue).different(green, red).different(green, blue).different(blue, red).different(blue, green).coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-different(Mississippi, Tennessee),different(Mississippi, Alabama),different(Alabama, Tennessee),different(Alabama, Mississippi),different(Alabama, Georgia),different(Alabama, Florida),different(Georgia, Florida),different(Georgia, Tennessee).
  10. 10. Map Coloring (2)| ?- [c:projectslanguagescodeprologmap.pl].compiling c:/projects/languages/code/prolog/map.pl for byte code...c:/projects/languages/code/prolog/map.pl compiled, 16 lines read - 1716 bytes written, 16 msyes| ?- coloring(Alabama, Mississippi, Georgia, Tennessee, Florida).Alabama = blueFlorida = greenGeorgia = redMississippi = redTennessee = green ?(16 ms) yes
  11. 11. Unification (1)• The Unification Algorithm is a famous algorithmfrom the field of AI, often used in theoremproving, game playing, planning, etc…• It can loosely be thought of as an algorithm thattries to make to non-grounded terms the same.• P(X, 2) = P(1, Y)  X=1 & Y=2  P(1, 2)• P(X, X) = P(Y, 5)  X=5 & Y=5  P (5, 5)• P(X, Y) = P(2, Z)  X=2 & Y=Z  P (2, Z)• See Artificial Intelligence (Russell & Norvig)
  12. 12. Prolog Rules• Rules: <head> :- <body>• Head: Single clause typically with variables• Body: Conjunction of goals with variables• Examples:ancestor(X,Y) :- parent(X,Y)ancestor(X,Y) :- parent(X,Z), parent(Z,Y)ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y)
  13. 13. A Recursive Example(1)parent(p1, p2).parent(p2, p3).parent(p3, p4).ancestor(X, Y) :- parent(X, Y).ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).| ?- [c:projectslanguagescodeprologancestor.pl].compiling c:/projects/languages/code/prolog/ancestor.pl for byte code...c:/projects/languages/code/prolog/ancestor.pl compiled, 5 lines read -818 bytes written, 13 msyes
  14. 14. A Recursive Example(2)| ?- trace.The debugger will first creep -- showing everything (trace)yes{trace}| ?- ancestor(p1, p4).1 1 Call: ancestor(p1,p4) ?2 2 Call: parent(p1,p4) ?2 2 Fail: parent(p1,p4) ?2 2 Call: parent(p1,_80) ?2 2 Exit: parent(p1,p2) ?3 2 Call: ancestor(p2,p4) ?4 3 Call: parent(p2,p4) ?4 3 Fail: parent(p2,p4) ?4 3 Call: parent(p2,_129) ?4 3 Exit: parent(p2,p3) ?5 3 Call: ancestor(p3,p4) ?6 4 Call: parent(p3,p4) ?6 4 Exit: parent(p3,p4) ?5 3 Exit: ancestor(p3,p4) ?3 2 Exit: ancestor(p2,p4) ?1 1 Exit: ancestor(p1,p4) ?true ?(63 ms) yes
  15. 15. Using Rules in Both Directions// Find all ancestors| ?- ancestor(X, p4).X = p3 ? ;X = p1 ? ;X = p2 ? ;no// Find all descendants| ?- ancestor(p1, X).X = p2 ? ;X = p3 ? ;X = p4 ? ;no
  16. 16. Lists and Tuples (1)• List are denoted by comma-separated values in squarebrackets. i.e. [1, 2, 3]• Tuples are denoted by comma-separated values inparentheses. i.e. (1, 2, 3)| ?- [1, 2, 3] = [X, Y, Z].X = 1Y = 2Z = 3yes
  17. 17. Accessing Elements of a List| ?- [1, 2, 3] = [X | Y].X = 1Y = [2,3]| ?- [1, 2, 3] = [_, X | Y].X = 2Y = [3]
  18. 18. Lists and Math (1)count(0, []).count(Count, [Head|Tail]) :-count(TailCount, Tail), Count is TailCount + 1.sum(0, []).sum(Total, [Head|Tail]) :-sum(Sum, Tail), Total is Head + Sum.average(Average, List) :-sum(Sum, List),count(Count, List),Average is Sum/Count.
  19. 19. Lists and Math (2)| ?- sum(S, [1,2,3]).1 1 Call: sum(_17,[1,2,3]) ?2 2 Call: sum(_92,[2,3]) ?3 3 Call: sum(_116,[3]) ?4 4 Call: sum(_140,[]) ?4 4 Exit: sum(0,[]) ?5 4 Call: _168 is 3+0 ?5 4 Exit: 3 is 3+0 ?3 3 Exit: sum(3,[3]) ?6 3 Call: _197 is 2+3 ?6 3 Exit: 5 is 2+3 ?2 2 Exit: sum(5,[2,3]) ?7 2 Call: _17 is 1+5 ?7 2 Exit: 6 is 1+5 ?1 1 Exit: sum(6,[1,2,3]) ?S = 6 ?
  20. 20. Solving Sudoku (1)valid([]).valid([Head|Tail]) :-valid(Tail), fd_all_different(Head).sudoku(S11, S12, S13, S14,S21, S22, S23, S24,S31, S32, S33, S34,S41, S42, S43, S44,Board) :-Board = [S11, S12, S13, S14,S21, S22, S23, S24,S31, S32, S33, S34,S41, S42, S43, S44, ],fd_domain(Board, 1, 4),Row1 = [S11, S12, S13, S14],Row2 = [S21, S22, S23, S24],Row3 = [S31, S32, S33, S34],Row4 = [S41, S42, S43, S44],Col1 = [S11, S21, S31, S41],Col2 = [S12, S22, S32, S42],Col3 = [S13, S23, S33, S43],Col4 = [S14, S24, S34, S44],Square1 = [S11, S12, S21, S22],Square2 = [S13, S14, S23, S24],Square3 = [S31, S32, S41, S42,],Square4 = [S33, S34, S43, S44,],valid([Row1, Row2, Row3, Row4,]),valid([Col1, Col2, Col3, Col4, ]),valid([Square1, Square2, Square3, Square4]).
  21. 21. Solving Sudoku (2)| ?- sudoku(_, _, 2, 3,_, _, _, _,_, _ ,_, _,3, 4, _, _,Solution).Solution = [4, 1, 2, 3,2, 3, 4, 1,1, 2, 3, 4,3, 4, 1, 2]
  22. 22. Solving Sudoku (3)• Finite Domain variables: A new type of data is introduced: FDvariables which can only take values in their domains. Theinitial domain of an FD variable is 0..fd_max_integer wherefd_max_integer represents the greatest value that any FDvariable can take.• fd_domain(Board, 1, 4).Used to specify the range of values of each Sudoku cell.• fd_all_different(X).Used to specify that all elemts in the list must have distinct values.
  23. 23. Part 2: Advanced Topics• The Art of Prolog, Sterling and Shapiro, MIT Press, 1986• Structure Inspection• Meta-Logical Predicates• Cuts (and Negation)• Extra-Logical Predicates
  24. 24. 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).yesfunctor, arg and =..
  25. 25. Meta-Logical Predicates• Outside scope of first-order logic• Query and affect the state of the proof• Treat variables as objects• Convert data structures to goals• Type Predicates:• var(<term>)• nonvar(<term>)• Variables as objects: freeze & melt• Dynamically Affecting the Knowledge Base• assert(<goal>)• retract(<goal>)• The Meta-Variable Facility: call(<goal>)• Memoization: lemma(<goal>)
  26. 26. Extra-Logical Predicates• These achieve side-effects as a result of beinglogically true• Three types of extra-logical predicates• Input / Output– read & write• Accessing and manipulating the program– clause(Head, Body)– assert (X)– retract(X)• Interfacing to the Operating System
  27. 27. Cuts (1)• Predicate cut or ! affects procedural behavior• Main purpose is to reduce the search space• It’s use is controversial: purity vs. efficiency• Green Cuts: Express determinism• Consider merging two sorted lists• Only one of X<Y, X=Y or X>Y can succeed• Once one succeeds, no need for the others.
  28. 28. Merging without Cutsmerge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, merge(Xs, [Y|Ys], Zs).merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, merge(Xs, Ys, Zs).merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, merge([X|Xs], Ys, Zs).merge(Xs, [], Xs).merge([], Ys, Ys).| ?- merge([1,3,5],[2,3], Xs).Xs = [1,2,3,3,5] ?yes
  29. 29. Merging with Cutsmerge([X|Xs], [Y|Ys], [X|Zs]) :- X<Y, !, merge(Xs, [Y|Ys], Zs).merge([X|Xs], [Y|Ys], [X,Y|Zs]) :- X=Y, !, merge(Xs, Ys, Zs).merge([X|Xs], [Y|Ys], [Y|Zs]) :- X>Y, !, merge([X|Xs], Ys, Zs).merge(Xs, [], Xs) :- !.merge([], Ys, Ys) :- !.?- merge([1,3,5],[2,3], Xs).Xs = [1,2,3,3,5]yes
  30. 30. 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]
  31. 31. Prolog Today• GNU Prolog CompilerFull 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
  32. 32. core.logic• Clojure core.logic (David Nolen)This is a Clojure implementation of miniKanren. miniKanren is a Logic Programming libraryembedded 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
  33. 33. AllegroGraph (Franz Inc.)• AllegroGraph is one the leading graph database and application frameworksfor 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/
  34. 34. ReferencesSeven Languages in Seven WeeksBruce A. Tate, Pragmatic Programmers LLC, 2010The Art of Prolog: Advanced Programming TechniquesSterling & Shapiro, MIT Press, 1986GNU Prolog User ManualDaniel Diaz, November 2012Artificial Intelligence: A Modern ApproachRussell & Norvig, Prentice Hall, 1995

×