UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.

Slides:



Advertisements
Similar presentations
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Advertisements

Inference Rules Universal Instantiation Existential Generalization
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
1. An Overview of Prolog.
Logic Programming Automated Reasoning in practice.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
13 Automated Reasoning 13.0 Introduction to Weak Methods in Theorem Proving 13.1 The General Problem Solver and Difference Tables 13.2 Resolution.
Prolog Programming for Artificial Intelligence Three edition 2001
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Chapter 12 - Logic Programming
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 580 Artificial Intelligence Ch.5 [P]: Propositions and Inference Sections.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
1 Automated Reasoning Introduction to Weak Methods in Theorem Proving 13.1The General Problem Solver and Difference Tables 13.2Resolution Theorem.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Hole in Goal Trees Notes for: D.W. Loveland and M. Stickel. “A Hole in Goal.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko, Poole, and Van Emden.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
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.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
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 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
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 CSC 358/ Outline Pattern matching Unification Logic programming.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Kansas State University Department of Computing and Information Sciences CIS 730: Introduction to Artificial Intelligence Lecture 15 of 41 Friday 24 September.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko, Poole, and Van Emden.
Knowledge Based Information System
Artificial Intelligence CS370D
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog
Probabilistic Horn abduction and Bayesian Networks
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Little History Prolog was invented by Alain Colmerauer, a professor of computer science at the university of Aix-Marseille in France, in 1972 The first application of Prolog was in natural language processing Prolog stands for programming in logic (PROgrammation en LOgique) Its theoretical underpinning are due to Donald Loveland of Duke university through Robert Kowalski (formerly) of the university of Edinburgh

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Logic Programming Prolog is the only successful example of the family of logic programming languages A Prolog program is a theory written in a subset of first-order logic, called Horn clause logic Prolog is declarative. A Prolog programmer concentrates on what the program needs to do, not on how to do it The other major language for Artificial Intelligence programming is LISP, which is a functional (or applicative) language

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Prolog in CSCE 580 (Skip for 330) Chapters 1-9 (the Prolog programming language), 11 (blind search), 12 (heuristic search), and maybe 14 (constraint logic programming) and some parts of 15 (Bayesian networks), 17 (means-ends analysis) and 18 (induction of decision trees) Prolog is introduced as a programming language before a thorough review of first-order logic

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Defining Relations by Facts parent( tom,bob). parent is the name of a relation A relation of arity n is a function from n-tuples (elements of a Cartesian product) to {true, false}. (It can also be considered a subset of the n-tuples.) parent( pam, bob). parent( tom,bob). parent( tom,liz). parent( bob, ann). parent( bob,pat). parent( pat,jim). A relation is a collection of facts

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Queries ?-parent( bob,pat). yes A query and its answer, which is correct for the relation defined in the previous slide: this query succeeds ?-parent( liz,pat). no A query and its answer, which is correct for the relation defined in the previous slide: this query fails

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Queries cf. pr1_1.pl ?-parent( tom,ben). /* who is Ben? */ ?-parent( X,liz). /* Wow! */ ?-parent( bob,X). /* Bob’s children */ ?-parent( X,Y). /* The relation, fact by fact */

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Composite Queries Grandparents: ?-parent( Y,jim), parent( X,Y). the comma stands for “and” ?-parent( X,Y), parent(Y,jim). order should not matter, and it does not! Grandchildren: ?-parent( tom,X), parent( X,Y). Common parent, i.e. (half-)sibling: ?-parent( X,ann), parent( X,pat).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Facts and Queries Relations and queries about them Facts are a kind of clause Prolog programs consist of a list of clauses The arguments of relations are atoms or variables (a kind of term) Queries consist of one or more goals Satisfiable goals succeed; unsatisfiable goals fail

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Defining Relations by Rules The offspring relation: For all X and Y, Y is an offspring of X if X is a parent of Y This relation is defined by a rule, corresponding to the Prolog clause offspring( Y,X) :- parent( X,Y). Alternative reading: For all X and Y, if X is a parent of Y, then Y is an offspring of X

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Rules Rules are clauses. Facts are clauses A rule has a condition and a conclusion The conclusion of a Prolog rule is its head The condition of a Prolog rule is its body If the condition of a rule is true, then it follows that its conclusion is true also

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering How Prolog Rules are Used Prolog rules may be used to define relations The offspring relation is defined by the rule offspring( Y,X) :- parent( X,Y): –if (X,Y) is in the parent relation, then (Y,X) is in the offspring relation When a goal of the form offspring( Y,X) is set up, the goal succeeds if parent( X,Y) succeeds Procedurally, when a goal matches the head of a rule, Prolog sets up its body as a new goal

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example (ch1_2.pl) ?-offspring(liz,tom). No fact matches this query The head of the clause offspring( Y,X) :- parent( X,Y) does Y is replaced with liz, X is replaced with tom The instantiated body parent( tom,liz) is set up as a new goal ?-parent( tom,liz) succeeds offspring( liz,tom) therefore succeeds too

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Family Relations female and male are defined extensionally, i.e., by facts; mother and grandparent are defined intensionally, I.e., by rules female(pam). … male(jim). mother( X,Y) :- parent( X,Y), female( X). grandparent( X,Z) :- parent( X,Y), parent( Y,Z).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Sister (ch1_3.pl) sister(X,Y) :- parent(Z,X), parent(Z,Y), female( X). Try: ?-sister(X,pat). X = ann; X = pat /* Surprise! */ (Half-)sisters have a common parent and are different people, so the correct rule is: sister(X,Y) :- parent(Z,X), parent(Z,Y), female( X), different(X,Y). –(or: sister(X,Y) :- parent(Z,X), parent(Z,Y), parent(W,X), parent(W,Y), female(X), different(Z,W), different(X,Y).)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Clauses and Instantiation Facts are clauses without body Rules are clauses with both heads and non-empty bodies Queries are clauses that only have a body (!) When variables are substituted by constants, we say that they are instantiated.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Universal Quantification Variables are universally quantified, but beware of variables that only appear in the body, as in haschild( X) :- parent( X,Y)., which is best read as: for all X, X has a child if there exists some Y such that X is a parent of Y (I.e.: for all X and Y, if X is a parent of Y, then X has a child)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Predecessor (Ancestor) predecessor( X,Z) :- parent( X,Z). predecessor( X,Z) :- parent( X,Y), parent(Y,Z). predecessor( X,Z) :- parent( X,Y1), parent( Y1,Y2,), parent( Y2,Z). etc. When do we stop? The length of chain of people between the predecessor and the successor should not arbitrarily bounded.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Note on History in SWI-Prolog See p.24 of manual (query substitution) To set up the history mechanism, edit the pr.ini file and place it in one of the directories in file_search_path. (I placed it in the directory where my Prolog code is.) To check the values of Prolog flags, use ?-current_prolog_flag(X,Y).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Note on Definition Graphs Skip for 330 Definition graphs indicate that definition of relations by rules is somewhat analogous to function composition in applicative (functional) languages See Figures 1.3 and 1.4. “each diagram should be understood as follows: if the relations shown by solid arcs hold, the relation shown by a dashed arc also holds”

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering A Recursive Rule For all X and Z, X is a predecessor of Z if there is a Y such that (1) X is a parent of Y and (2) Y is a predecessor of Z. predecessor( X,Z) :- parent( X,Y), predecessor( Y,Z).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering The Family Program (fig1_8.pl) Comments –/* This is a comment */ –% This comment goes to the end of the line SWI Prolog warns us when the clauses defining a relation are not contiguous.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Prolog Proves Theorems Prolog accepts facts and rules as a set of axioms, and the user’s query as a conjectured theorem. Prolog then tries to prove the theorem, i.e., to show that it can be logically derived from the axioms Prolog builds the proof “backwards”: it does not start with facts and apply rules to derive other facts, but it starts with the goals in the user’s query and replaces them with new goals, until new goals happen to be facts

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Goal Trees In attempting to prove theorems starting from goals, Prolog builds goal trees See example of proving predecessor( tom,pat) in Figures 1.9, 1.10, Variables are matched as new goals are set up The scope of each variable is a single clause, so we rename variables for each rule application Prolog backtracks as needed when a branch of the proof tree is a dead end This is explained in more detail in Chapter 2

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Declarative and Procedural Meaning of Prolog Programs The declarative meaning is concerned with the relations defined by the program: what the program states and logically entails The procedural meaning is concerned with how the output of the program is obtained, i.e., how the relations are actually evaluated by the Prolog system It is best to concentrate on the declarative meaning when writing Prolog programs Unfortunately, sometimes the programmer must also consider procedural aspect (for reasons of efficiency or even correctness): we will see examples of this in Ch.2

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Knight Moves on a Chessboard % This example is from unpublished (to the best of my knowledge) notes by Maarten % Van Emden. /* The extensional representation of the (knight) move relation follows. It consists of 336 facts; only a few are shown. In particular, all moves from position (5,3) on the chess board are shown. */ move(1,1,2,3). move(1,1,3,2)..... move(5,3,6,5). move(5,3,7,4). move(5,3,7,2). move(5,3,6,1). move(5,3,4,1). move(5,3,3,2). move(5,3,3,4). move(5,3,4,5).... move(8,8,7,6). move(8,8,6,7).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Intensional Representation of Moves /* The intensional representation of the (knight) move relation follows. It consists of facts (to define extensionally the relation succ/2) and rules (to define the relations move, diff1, and diff2. */ move(X1,Y1,X2,Y2) :- diff1(X1,X2), diff2(Y1,Y2). move(X1,Y1,X2,Y2) :- diff2(X1,X2), diff1(Y1,Y2). diff1(X,Y) :- succ(X,Y). diff1(X,Y) :- succ(Y,X). diff2(X,Z) :- succ(X,Y), succ(Y,Z). diff2(X,Z) :- succ(Z,Y), succ(Y,X). succ(1,2). succ(2,3). succ(3,4). succ(4,5). succ(5,6). succ(6,7). succ(7,8).