Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Similar documents
CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog

INTRODUCTION TO PROLOG

Prolog Programming. Chapter 5. Controlling Backtracking : 1/16/2010

Backtrack control. Chapter 5

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01

Logic Languages. Hwansoo Han

Chapter 16. Logic Programming Languages

Week 7 Prolog overview

Logic Programming Languages

Chapter 16. Logic Programming. Topics. Logic Programming. Logic Programming Paradigm

Chapter 16. Logic Programming Languages ISBN

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

First-Order Logic (FOL)

CSC384: Intro to Artificial Intelligence Prolog Tutorials 3&4. Hojjat Ghaderi, Fall 2006, University of Toronto

Fundamentals of Prolog

Prolog Programming. Lecture Module 8

Advanced Logic and Functional Programming

Backtracking. Ch. 5 Controlling Backtracking. Backtracking. Backtracking Example. Example. Example

Introduction to predicate calculus

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Notes for Chapter 12 Logic Programming. The AI War Basic Concepts of Logic Programming Prolog Review questions

Lecture 9: A closer look at terms

Prolog. Intro to Logic Programming

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

Family Example: Some Facts. respects(barb,dan). respects(barb,katie). respects(dan,brian). respects(dan,barbara).

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

More Non-logical Features of Prolog

Prolog. Logic Programming vs Prolog

Quick n Dirty Prolog Tutorial

Lecture 11: Feb. 10, 2016

CS 360: Programming Language Concepts Lecture 15 Logic Programming

CILOG User Manual Version 0.14

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

Topic B: Backtracking and Lists

Operational Semantics

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012.

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07

Introduction to Logic Programming in Prolog 1 / 39

Snakes. Declarative Languages. Relation different. Overview. Negation as failure. Relation different

IF/Prolog V5.3. User s Guide. Siemens AG Austria

Logic Programming. Let us have airline flight information of the form: 1. Application Domains: 2. Definitions

Sec Negative Goals ( Not ) 21

Reviews. Arithmetic Operators (2)

Last Week. Today. Prolog: recursion examples. Recursion in Prolog. CSC326 Programming Languages (Week 12,Friday)

Chapter 16. Logic Programming Languages ISBN

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Lecture 16: Logic Programming in Prolog

Operators (2A) Young Won Lim 10/5/13

Recursion, Structures, and Lists

Programming Paradigms

Tests, Backtracking, and Recursion

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

The current topic: Prolog. Announcements. Meaning of a Prolog rule. Prolog syntax. Reminder: The deadline for Lab 2 re-mark requests is Friday.

Implementação de Linguagens 2016/2017

Logic as a Programming Language

CSC4504/Prolog. : Formal Languages & Applications. J Paul Gibson, D311. An Introduction To Prolog

Plan of the lecture. G53RDB: Theory of Relational Databases Lecture 14. Example. Datalog syntax: rules. Datalog query. Meaning of Datalog rules

Programming Languages Third Edition

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

Chapter 5. Pure PROLOG. Foundations of Logic Programming

6.034 Notes: Section 11.1

AILog User Manual Version 2.3

Functions 2/1/2017. Exercises. Exercises. Exercises. and the following mathematical appetizer is about. Functions. Functions

Data Integration: Logic Query Languages

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Wan Hussain Wan Ishak

6. Inference and resolution

CAP 5602 Summer, Lesson 4: Loops. The topics 1. the cut and some of its uses 2. the while loop 3. the do until loop

Wan Hussain Wan Ishak

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

CS 4700: Artificial Intelligence

Part I Logic programming paradigm

Predicate Calculus. Problems? Syntax. Atomic Sentences. Complex Sentences. Truth

Prolog-2 nd Lecture. Prolog Predicate - Box Model

Mathematical Logic Prof. Arindama Singh Department of Mathematics Indian Institute of Technology, Madras. Lecture - 37 Resolution Rules

Information technology Programming languages Prolog Part 1: General core

Lecture 9. Exercises. Theory. Solutions to exercises LPN 8.1 & 8.2. Patrick Blackburn, Johan Bos & Kristina Striegnitz

Topic 1: Introduction to Knowledge- Based Systems (KBSs)

CS 381: Programming Language Fundamentals

PHIL 240, Introduction to Logic, Sections Fall 2011 FINAL EXAM 14 December Name (5 points): Section (5 points):

A Pearl on SAT Solving in Prolog (extended abstract)

tuprolog with exceptions

Resolution (14A) Young W. Lim 6/14/14

Overview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del

Lecture 4: January 12, 2015

Logic Programming: Lecture 1

Reasoning About Programs Panagiotis Manolios

Logical reasoning systems

ITS336 Lecture 7 Prolog

Symbolic Programming Declarative Programming

COMP4418 Knowledge Representation and Reasoning

LOGIC AND DISCRETE MATHEMATICS

Losp for Predicate Calculus

Software Paradigms (Lesson 6) Logic Programming

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Transcription:

Topics Chapter 16 Logic Programming Summary (resolution, unification, Prolog search strategy ) Disjoint goals The cut operator Negative goals Predicate fail Debugger / tracer Lists 2 Resolution Resolution is an inference rule for Horn clauses. Given two clauses: If the head of the first clause can be matched with one of the statements in the body of the second clause then the first clause can be used to replace its head in the second clause by its body. a a 1,,a n. b b 1,,b i,,b m. and b i matches a, then b b 1,, b i-1, a 1,, a n, b i+1,, b m 3 Unification Unification is the process by which variables are instantiated so that patterns match during resolution. Unification is the process of making two terms the same in some sense. foo = foo Prolog s answer:. Both terms are atoms. 5 = 5 Prolog s answer: no. LHS is an atom and RHS is a number. 4 Prolog applies resolution in a strictly linear fashion Replaces goals left to right. Considers clauses in top-to-bottom order. Subgoals are considered immediately once they are set up. Search can be viewed as a depth-first search on a tree of possible choices. 5 Given the following clauses: (1) ancestor(x,y) :- parent(x,z), ancestor(z,y). Given the goal ancestor(x,bob), Prolog s search strategy is left to right and depth first on the following tree of subgoals. Edges are labelled by the number of the clause used by Prolog for resolution Instantiation of variables are written in curly brackets. 6 1

Leaf nodes in this tree occur No match is found for the leftmost clause. All clauses have been eliminated (success). Whenever failure occurs Prolog backtracks up the tree to find further paths to a leaf, releasing instantiations of variables. 7 Original Prolog program: (1) ancestor(x,y) :- parent(x,z), ancestor(z,y). Clauses in slightly different order: (1) ancestor(x,y) :- ancestor(z,y), parent(x,z). Problem? 8 Existential Queries Universal Facts Variables in queries are existentially quantified. father(abraham,x)? Reads: Does there exist an X such that abraham is the father of X? For convenience, existential quantification is omitted. Variables in facts are implicitly universally quantified. In general, a fact p(t 1, T n ) reads that for all X 1,,X k where the X i are variables occurring in the fact p(t 1, T n ) is true. likes(x,apple). From a universally quantified fact one can deduce any instance of it. likes(adan,apple). 9 10 Universal Rules Disjoint Goals ( or ) Rule specifies things that are true if some condition is satisfied. For all X and Y, Y is an offspring of X if X is a parent of Y.?-offspring(Y,X):-parent(X,Y). Prolog provides a semicolon, meaning or. The definition of parent could be written as a single rule: parent(x,y) :- father(x,y); mother(x,y). The normal way to express an or relation in Prolog is to state two rules. The semicolon adds little or no expressive power to the language. It looks so much like the comma that it often leads to typographical errors. The use of semicolon is not recommended. 11 12 2

The Cut Operator (!) Automatic backtracking is one of the most characteristic features of Prolog. Lead to inefficiency: explore possibilities that lead nowhere. The cut predicate tells the Prolog system to forget about some of the backtrack points. Discards all backtrack points that have been recorded since execution entered the current clause. The Cut Operator (!) After executing a cut: It is no longer possible to try other clauses as alternatives to the current clause. It is no longer possible to try alternative solutions to subgoalspreceding the cut in the current clause. Reduces the search space. do not go to (alternatives that we know are bound to fail). writename(1) :-!, write( One ). Confirms the choice of a rule. max(x,y,y) :- X>=Y,!. max(x,y,x). 13 14 The Cut Operator (!) Negative Goals ( not) Simulates an else statement when we are testing cases. Mutually exclusive conclusions. f(x,0) :- X=0,!. f(x,1) :- X>0,!. f(x,undefined). Example: Given the knowledge base f(x) :- g(x),!, h(x). f(x) :- j(x). g(a). j(a). What is the result of executing the query?- f(a).? 15 The special predicate \+ means not or cannot prove. If g is any goal, then \+ g succeeds if g fails, and fails if g succeeds.?- \+ likes(adan,apple). The predicate \+ can appear only in a query or on the right-hand side of a rule. It cannot appear in a fact or in the head of a rule. \+ likes(adan,apple). 16 Negation as Failure Closed-World Assumption The behaviour of \+ is called negation as failure. In Prolog, you cannot state a negative fact. All you can do is conclude a negative statement if you cannot conclude the corresponding positive statement. What is the definition a a person who is not a parent? non_parent(x,y) :- \+ father(x,y), \+ mother(x,y). What happens if you ask about people who are not in the knowledge base? The Prolog systems assumes that its knowledge base is complete (this is called the closed-world assumption). Something that cannot be proved to be true is assumed to be false. 17 18 3

Predicate fail Predicate fail is a special symbol that will immediately fail when Prolog encounters it as a goal. Cut-fail combination is used to say that something is not true. likes(mary,x) :- snake(x),!, fail likes(mary,x) :- animal(x). Debugger / Tracer The debugger allows you to trace exactly what is happening as Prolog executes a query.?- trace. Return: computation is shown step by step. s (for skip ): the debugger will skip to the end of the current query (useful if the current query has a lot of subgoals which you do not want to see). a (for abort ): the computation will stop. 19 20 Prolog supports both integers and floating-point numbers and interconvert them as needed. Operator is : takes an arithmetic expression on its right, evaluates it, and unifies the result with its argument on the left.?- Y is 2+2.?- 5 is 3+3. Y = 4 no?- Z is 4.5+(3.9/2.1). Z = 6.3571428 The precedence of operators is about the same as in other programming languages. Prolog is not an equation solver. Prolog does not solve for unknowns on the right hand side of is:?- 5 is 2 + What. instantiation error. 21 22 Constructing Expressions Prolog vs. other programming languages Other programming languages evaluate arithmetic expressions wherever they occur. Prolog evaluates arithmetic expressions only in specific places. 2+2 evaluates to 4 only when it is an argument of the predicates of the following table; the rest of the time it is just a data structure consisting of 2,+, and 2. Built-in Predicates that Evaluate Expressions R is Expr Expr1 =:= Expr2 Expr1 =\= Expr2 Expr1 > Expr2 Expr1 < Expr2 Expr1 >= Expr2 Expr1 =< Expr2 Evaluates Expr and unifies result with R Succeeds if results of both expressions are equal. Succeeds if results of the expressions are not equal. Succeeds if Expr1 > Expr2 Succeeds if Expr1 < Expr2 Succeeds if Expr1 >= Expr2 Succeeds if Expr1 =< Expr2 23 24 4

Constructing Expressions There is a clear difference between: is, which takes an expression (on the right), evaluates it, and unifies the result with its argument on the left. =:=, which evaluates two expressions and compares the results. =, which unifies two terms (which need not be expressions and, if expressions, will not be evaluated). Constructing Expressions: examples?- What is 2+3. What = 5 % Evaluates 2+3, unify result with What?- 4+1 =:= 2+3. % Evaluates 4+1 and 2+3, compare results?- What = 2+3 What = 2+3 % Unify What with the expression 2+3 25 26 5