COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
Chapter 11 :: Logic Languages
Advertisements

Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming with Prolog: Resolution, Unification, Backtracking COMP 144 Programming Language Concepts Spring 2003 Stotts, Hernandez-Campos Modified.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
C. Varela1 Programming Languages (CSCI 4430/6969) History, Syntax, Semantics, Essentials, Paradigms Carlos Varela Rennselaer Polytechnic Institute August.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
Artificial Intelligence Chapter 17 Knowledge-Based Systems Biointelligence Lab School of Computer Sci. & Eng. Seoul National University.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Introduction to Logic Programming with Prolog (Section 11.3)
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Knowledge Based Information System
COP4020 Programming Languages Prolog Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
LECTURE 21 Logic Programming. LOGIC PROGRAMMING RESOLUTION STRATEGIES.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 4&7: Will cover material today Download.
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Artificial Intelligence Chapter 17 Knowledge-Based Systems
Knowledge-Based Systems Chapter 17.
Artificial Intelligence Chapter 17 Knowledge-Based Systems
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Logic Programming with Prolog: Resolution, Unification, Backtracking
Tests, Backtracking, and Recursion
Chapter 11 :: Logic Languages
COP4020 Programming Languages
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Artificial Intelligence Chapter 17. Knowledge-Based Systems
Artificial Intelligence Chapter 17 Knowledge-Based Systems
Chapter 2: Prolog (Introduction and Basic Concepts)
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Presentation transcript:

COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan

COP4020 Spring /16/2015 Overview Logic programming principles Introduction to Prolog

COP4020 Spring /16/2015 Logic Programming Logic programming is a form of declarative programming A program is a collection of axioms Each axiom is a Horn clause of the form: H :- B 1, B 2,..., B n. where H is the head term and B i are the body terms Meaning: H is true if all B i are true A user states a goal (a theorem) to be proven The logic programming system uses inference steps to prove the goal (theorem) is true, applying a logical resolution strategy

COP4020 Spring /16/2015 Resolution Strategies To deduce a goal (theorem), the programming system searches axioms and combines sub-goals using a resolution strategy For example, given the axioms: C :- A, B.A :- true. D :- C.B :- true. Forward chaining deduces first that C is true: C :- A, B and then that D is true: D :- C Backward chaining finds that D can be proven if sub-goal C is true: D :- C the system then deduces that the sub-goal is C is true: C :- A, B since the system could prove C it has proven D

COP4020 Spring /16/2015 Prolog Prolog uses backward chaining, which is more efficient than forward chaining for larger collections of axioms Prolog is interactive (mixed compiled/interpreted) Example applications:  Expert systems  Artificial intelligence  Natural language understanding  Logical puzzles and games

COP4020 Spring /16/2015 SWI-Prolog SWI-Prolog is a popular prolog system.  Login linprog.cs.fsu.edu  pl (or swipl ) to start SWI-Prolog  halt. to halt Prolog (period is the Prolog command terminator) SWI-Prolog manual:

COP4020 Spring /16/2015 Definitions: Prolog Clauses A program consists of a collection of Horn clauses Each clause consists of a head predicate and body predicates: H :- B 1, B 2,..., B n.  A clause is either a rule, e.g. snowy(X) :- rainy(X), cold(X). meaning: "If X is rainy and X is cold then this implies that X is snowy"  Or a clause is a fact, e.g. rainy(rochester). meaning "Rochester is rainy."  This fact is identical to the rule with true as the body predicate: rainy(rochester) :- true. A predicate is a term (an atom or a structure), e.g.  rainy(rochester)  member(X,Y)  true

COP4020 Spring /16/2015 Definitions: Queries and Goals A program basically is a database of facts and rules (clauses). In SWI-Prolog  To load program: ?- [‘prologfilename'].  To list all clauses: ?- listing.  To list clauses related to a name: ?- listing(name). After the program is loaded, the goals can be ‘executed’ by doing queries.

COP4020 Spring /16/2015 Definitions: Queries and Goals A query is interactively entered by a user after a program is loaded  A query has the form ?- G 1, G 2,..., G n. where G i are goals (predicates) A goal is a predicate to be proven true by the programming system  Example program (example1.pl) with two facts: rainy(seattle). rainy(rochester).  Query with one goal to find which city C is rainy (if any): ?- rainy(C).  Response by the interpreter: C = seattle  Type a semicolon ; to get next solution: C = rochester  Typing another semicolon does not return another solution

COP4020 Spring /16/2015 Example (example2.pl) Consider a program with three facts and one rule: rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X).  Query and response: ?- snowy(rochester). yes  Query and response: ?- snowy(seattle). no  Query and response: ?- snowy(paris). no  Query and response: ?- snowy(C). C = rochester because rainy(rochester) and cold(rochester) are sub-goals that are both true fact

COP4020 Spring /16/2015 Backward Chaining with Backtracking Consider again: ?- snowy(C). C = rochester The system first tries C=seattle : rainy(seattle) cold(seattle) fail Then C=rochester : rainy(rochester) cold(rochester) When a goal fails, backtracking is used to search for solutions The system keeps this execution point in memory together with the current variable bindings Backtracking unwinds variable bindings to establish new bindings An unsuccessful match forces backtracking in which alternative clauses are searched that match (sub-)goals

COP4020 Spring /16/2015 Example: Family Relationships (example3.pl) Facts:  male(albert).  male(edward).  female(alice).  female(victoria).  parents(edward, victoria, albert).  parents(alice, victoria, albert). Rule: sister(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). Query: ?- sister(alice, Z). The system applies backward chaining to find the answer: 1. sister(alice,Z) matches the rule: X=alice, Y=Z 2. New goals: female(alice),parents(alice,M,F),parents(Z,M,F) 3. female(alice) matches 3rd fact 4. parents(alice,M,F) matches 5 th fact: M=victoria, F=albert 5. parents(Z,victoria,albert) matches 6 th fact: Z=edward

COP4020 Spring /16/2015 Murder Mystery (example4.pl) % the murderer had brown hair: murderer(X) :- hair(X, brown). % mr_holman had a ring: attire(mr_holman, ring). % mr_pope had a watch: attire(mr_pope, watch). % If sir_raymond had tattered cuffs then mr_woodley had the pincenez: attire(mr_woodley, pincenez) :- attire(sir_raymond, tattered_cuffs). % and vice versa: attire(sir_raymond,pincenez) :- attire(mr_woodley, tattered_cuffs). % A person has tattered cuffs if he is in room 16: attire(X, tattered_cuffs) :- room(X, 16). % A person has black hair if he is in room 14, etc: hair(X, black) :- room(X, 14). hair(X, grey) :- room(X, 12). hair(X, brown) :- attire(X, pincenez). hair(X, red) :- attire(X, tattered_cuffs). % mr_holman was in room 12, etc: room(mr_holman, 12). room(sir_raymond, 10). room(mr_woodley, 16). room(X, 14) :- attire(X, watch).

COP4020 Spring /16/2015 Murder Mystery (cont’d) Question: who is the murderer? ?- murderer(X). Execution trace (indentation shows nesting depth): murderer(X) hair(X, brown) /* murderer(X) :- hair(X, brown). */ attire(X, pincenez) /* hair(X, brown) :- attire(X, pincenez). */ X = mr_woodley attire(sir_raymond, tattered_cuffs) room(sir_raymond, 16) FAIL (no facts or rules) FAIL (no alternative rules) REDO (found one alternative rule) attire(X, pincenez) X = sir_raymond attire(mr_woodley, tattered_cuffs) room(mr_woodley, 16) SUCCESS SUCCESS: X = sir_raymond SUCCESS: X = sir_raymond SUCCESS: X = sir_raymond SUCCESS: X = sir_raymond