Logic Programming and Prolog What Is Logic Programming?
Total Page:16
File Type:pdf, Size:1020Kb
CPT S 355 LOGIC PROGRAMMING AND PROLOG WHAT IS LOGIC PROGRAMMING? “The use of mathematical logic for computer programming.” (Wikipedia) “A declarative, relational style of programming based on first-order logic.” (Dictionary.com) HISTORY OF LOGIC PROGRAMMING Logic programming is based on rules of logical inference going back to Aristotle. Came about in 1960s and 1970s due to debates about using declarative or procedural representations in AI SYSTEMS FOR LOGIC PROGRAMMING Prolog is the most common system Prolog has many variations: &-Prolog, And-parallel Prolog. ACE, And-Or-parallel Prolog. Actor Prolog Andorra -I, an Or - and (deterministic) and -parallel Prolog. Aurora, Or-parallel Prolog. cu-Prolog, a constraint logic programming language lambda Prolog LeanTaP, a small theorem prover written in SICStus Prolog. Logtalk, an extension for Object-Oriented Programming in Prolog. Mixtus, an automatic partial evaluator for full Prolog. Muse, Or-parallel Prolog. ALL ABOUT PROLOG Developed in 1972 by Alain Colmerauer and Philippe Roussel Name comes from “PROgramming in LOGic” A solution to the debate about which kinds of logic to use PROLOG STRUCTURE Prolog facts – a database of predicates and associations. Ex: instructor (looney, cs311);enrolled (ben, cs365); Prolog rules – define new predicates by using Prolog facts. Ex: teaches (P,S) :- instructor (P,C), enrolled (S,C) Note: Prolog considers capital letters to denote variables, not predicates. PROLOG STRUCTURE – QUERIES A query searches the database for the first fact that satisfies its goal. If a fact is found it either unifies the variable with a constant or returns yes. If a fact is not found that meets that condition it returns no. PROLOG STRUCTURE – QUERIES, CONT. Use a semi-colon to request subsequent answers. In other words, a semi -colon signifies disjunction. A comma signifies conjunction. PROLOG STRUCTURE - UNIFICATION Process of making one predicate same as another. A query resolves by unifying all of its elements. A constant unifies with itself and any variable. Scope is limited to the rule in which a variable occurs. When a variable is unified with a constant in a rule, all instances of that variable in that rule are unified to that constant. EXAMPLE OF UNIFICATION 1] f(a,b) f(a,X) Results: X = b 2] f(plus(4,5), g(5)) Result: f(X, Y) ; X = plus(4,5); Y = g(5) 3] f(plus(plus(4,5),7), b) Results: f(plus(Y,X), b) ; Y = plus(4,5) ; X = 7 Some examples where unification will fail: 1] f(apple, pie) f(pie, apple) 2] f(jim, X) f(X, fred) PROLOG DATA STRUCTURES Prolog's single data type is the term . Terms are either atoms , numbers , variables or compound terms . atoms are: x, blue, 'Some atom', and []. Numbers can be floats or integers Variables are denoted by a string consisting of letters, numbers and underscore characters, and beginning with an upper-case letter or underscore. A compound term has a functor and a number of arguments, which are again terms. HOW PROLOG WORKS Example: instructor (looney, cs311) instructor (yuksel, cs446) enrolled (joseph, cs311) enrolled (joseph, cs365) enrolled (joseph, cs446) enrolled (danielle, cs365) enrolled (danielle, cs446) This is the database of Prolog facts. HOW PROLOG WORKS Prolog rules: teaches (P,S) :- instructor (P,C), enrolled (S,C) This is to say that an instructor only teaches if he teaches a class and students are enrolled in that class. HOW PROLOG WORKS Prolog answers queries based off of the database that has been given. ?enrolled (joseph, cs365) yes ?enrolled (X, cs365) joseph danielle ?teaches (X, joseph) bebis looney yuksel HOW PROLOG WORKS Imagine what happens if we expand the database: instructor (bebis, cs365) instructor (looney, cs311) instructor (yuksel, cs446) enrolled (joel, cs365) instructor (helfand, cs493) enrolled (joseph, cs311) instructor (quint, math486) enrolled (joseph, cs365) enrolled (ben, cs365) enrolled (joseph, cs446) enrolled (bill, cs365) enrolled (joseph, cs493) enrolled (bill, cs446) enrolled (joseph, math486) enrolled ( brian , cs311) enrolled (kellen, cs365) enrolled (brian, cs365) enrolled (matts, cs311) enrolled (brittney, cs311) enrolled (matts, cs365) enrolled (brittney, cs365) enrolled (mattw, cs311) enrolled (brittney, cs446) enrolled (mattw, cs365) enrolled (cody, cs311) enrolled (mattw, cs446) enrolled (cody, cs365) enrolled (miran, cs365) enrolled (danielle, cs365) enrolled (ryan, cs365) enrolled (danielle, cs446) enrolled (samuel, cs365) enrolled (danielle, cs493) enrolled (shane, cs311) enrolled (david, cs365) enrolled (shane, cs365) enrolled (javier, cs365) enrolled (shane, cs446) enrolled (jeffrey, cs365) enrolled (tiffany, cs311) enrolled (jessica, cs311) enrolled (tiffany, cs365) enrolled (jessica, cs446) enrolled (tiffany, cs446) enrolled (jessica, math486) HOW PROLOG WORKS ?enrolled (X, cs365) ben bill brian brittney cody danielle david javier This list now gives us the entire roster jeffrey of students in CS 365. joel joseph kellen matts mattw miran ryan samuel shane tiffany HOW PROLOG WORKS Queries can be more complicated to compare more data: classmates (S1, S2) :- enrolled (S1, C), enrolled (S2, C) ?classmates (joseph, danielle) yes ?classmates (joseph, jessica) yes ?classmates (jessica, danielle) no HOW PROLOG WORKS classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C) ?classmates (joseph, danielle, C) cs365 cs446 cs493 no ?classmates (joseph, jessica, C) math ?classmates (jessica, danielle, C) no FREE PROLOG ACCESS SWI-Prolog http://www.swi-prolog.org/ YAProlog http://www.ncc.up.pt/~vsc/Yap/ Strawberry Prolog http://www.dobrev.com/ SOURCES http://en.wikipedia.org/wiki/Prolog http://www.afm.sbu.ac.uk/logic-prog/ http://en.wikipedia.org/wiki/Logic_programming http://dictionary.reference.com/browse/logic%20programming Colmerauer, Alain, Philippe Roussel, The Birth of Prolog, Nov. 1992, URL: http://www.lim.univ- mrs.fr/~colmer/ArchivesPublications/HistoireProlog/19november92.pdf Fisher, J.R., Prolog Tutorial , 2004, URL: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/contents.html.