Seven Languages in Seven MonthsLanguage 3: PrologRaymond P. de LacazePatchraymond.email@example.com/30/13
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
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
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)
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
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))
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
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
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)
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)
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
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
Accessing Elements of a List| ?- [1, 2, 3] = [X | Y].X = 1Y = [2,3]| ?- [1, 2, 3] = [_, X | Y].X = 2Y = 
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.
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,) ?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,) ?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 ?
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.
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
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>)
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
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.
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
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
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/
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
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.