(9.1) COEN 171 - Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
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.
Chapter 11 :: Logic Languages
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
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).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Introduction to Python
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
Input, Output, and Processing
Linux Operations and Administration
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
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.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© 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.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Chapter 2 Syntax and meaning of prolog programs Part 1.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Chapter 2 Syntax and meaning of prolog programs
Presentation transcript:

(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking  List operations  Arithmetic  Controlling backtracking  Running Prolog  N queens hints

(9.2) Logic Programming  Logic programming – uses a form of symbolic (mathematical) logic as programming language » declarative language – imperative and functional languages allow programs that describe how to compute a solution – declarative languages allow programs that describe facts about the problem domain and the form of the result (what, not how) » how to achieve that result is left up to the system

(9.3) Predicate Calculus  Predicate calculus is one form of symbolic logic  Propositions consist of – atomic propositions consist of compound terms – compound terms have a functor (name or relation) and arguments » professor (ron) » nieces (heidi, sasha, anna) » professor (Z), where Z is a variable – compound propositions consist of 2 or more atomic propositions joined by logical connectors

(9.4) Predicate Calculus (continued)  A clausal form is a standard way of writing propositions

(9.5) Predicate Calculus (continued)  One wants to infer things from propositions  Can do so using resolution techniques

(9.6) Predicate Calculus (continued)  Unification is the process of selecting values for variables in clauses – “instantiating” variables  With resolution, can use a restricted kind of clause called Horn clauses – nothing on the left side (no implication, either) » niece (anna, ron)% state facts – single atomic proposition on the left side (“headed” Horn clause

(9.7) Prolog  Prolog is the most prominent example of a declarative language  Prolog uses resolution and unification on Horn clauses  Prolog statements consist of terms – constants » atom (any string of letters, digits, and _ starting with a lower case letter, or any printable string in single quotes) anna miss_JONES ‘South America’ » numbers(integers or reals, usually integers) – variables » any string of letters, digits, and _ starting with an upper case letter or an _ X List_2 _

(9.8) Prolog (continued)  Prolog statements (continued) – structures » functor and arguments, which may also be structures professor(ron) date(4, may, 1995) date (Day, may, 1995) » defined by a name (functor) and arity (number of arguments) » so date (4, may, 1995) and date (124, 1995) are different » think of these as trees date 4may1995

(9.9) Facts and Rules  Prolog programs consist of facts and rules – facts are always true. they are “unheaded” Horn clauses » they need not be actually true in real life, but are in the Prolog universe – big (bear).% NOTICE “.” – small (cat). – big (mouse). – mother (ron, eva). – rules are full (“headed”) Horn clauses » head is a single term » antecedent is a single term or a series of terms joined by and – brother (X, Y) :-male (X),% “,” means – parents (X, M, F),% and – parents (Y, M, F). » can also join with “;” meaning or

(9.10) Matching  One of the most important operations to do on terms is matching – two terms match if » they are identical » the variables in both terms can be instantiated to objects so that the terms become identical – so date (D, M, 1981) and date (D1, july, Y1) match with the instantiations » D = D1 » M = july » Y – we say matching succeeds if two terms match, otherwise it fails

(9.11) Matching (continued)  The formal matching rules are – if S and T are constants, then they match only if they’re the same object – if S is a variable and T is anything, they match and S is instantiated to T. Conversely, if T is a variable, then T is instantiated to S. – if S and T are structures, they match only if » S and T have the same principal functor » all their corresponding components match – triangle(point(1,1), A, point(2,3)) – triangle(X, point(4,Y), point(2,Z)) triangle point Y2Z AX

(9.12) Subgoals and Backtracking  Prolog operates by starting with a database of facts and rules  Then the interpreter is given a goal – Prolog searches the database trying to match the goal against a fact or the LHS of a rule – if match fact » done – if match LHS of rule » RHS of rule becomes set of subgoals, try to match subgoals in turn – if fail, back up to immediately preceding match, try to satisfy the goal that matched with a different match – subgoals are always satisfied in left-to-right order – database is always searched beginning to end » physical layout of facts and rules determines order of processing

(9.13) Subgoals and Backtracking (continued)  Example

(9.14) Subgoals and Backtracking (continued)  Failure causes control to return to previous goal (redo)  Success causes invocation of next goal Goal 1 Goal 2 call fail redo succeed Control Flow model

(9.15) Backtracking Performance  Reordering the clauses and goals in the database can have a significant impact on the performance of a program – consider the following examples

(9.16) Backtracking Performance (continued) The same logical program, with 4 different orderings of statements

(9.17) Backtracking Performance (continued)

(9.18) Backtracking Performance (continued)

(9.19) Backtracking Performance (continued)

(9.20) Backtracking Performance (continued)

(9.21) List Operations  Lists are one of the basic data structures of Prolog – the other is structures, which are equivalent to records  Lists are represented in programs, and printed out, as a sequence of items in [ ] – [ ann, tom, tennis, skiing ] headtail ann tom tennis skiing [ ]

(9.22) List Operations (continued)  Prolog provides a notation to separate head and tail of a list – [ head | tail ] » [ ann | [ tom, tennis, skiing] ]  Some useful list operations » not built in, but some are in libraries distributed with various Prologs – member (X, L) succeeds if X is in L » member (X, [X | Tail] ). » member (X, [ Head | Tail ] ) :- member (X, Tail). – conc (L1, L2, L3) succeeds if L3 = L1 concatenated with L2 » and returns L3 = L1 cat L2 » conc ([], L, L). » conc ( [X | L1], L2, [X | L3] ) :- conc (L1, L2, L3). conc ( [a, b, c], [1, 2, 3], L). L = [a, b, c, 1, 2, 3]

(9.23) List Operations (continued)  Useful operations (continued) – add (X, L, [X | L] ).% or just [X | L] – del (X, L, L1) deletes one occurrence of X from L giving L1 » del (X, [X | Tail], Tail ). » del (X, [Y | Tail], [Y | Tail1] ) :- del (X, Tail, Tail1). ?- del (a, [a, b, a, a], L). L = [b, a, a] ; L = [a, b, a] ; no interpreter response input goal ; typed by user causes rematch as if goal had failed

(9.24) Arithmetic  In Prolog, arithmetic is performed by built-in predicates that take arithmetic expressions as arguments and evaluate them  arithmetic expressions (ae) consist of numbers, variables, and arithmetic functors – arithmetic functors are » X + Y, X - Y, X * Y, X / Y (real division), X // Y (integer division), X mod Y, -X » variable must be bound to a non-variable expression at evaluation time  To evaluate an ae, pass as argument to one of these predicates – Z is X (assignment statement), X =:= Y (numeric equality test), X =\= Y (not equal), X Y, X = = Y

(9.25) Arithmetic (continued)  Examples – suppose database with facts born (name, yearborn). Can retrieve anyone born between 1950 and 1960 by » ?- born (Name, Year), Year >= 1950, Year =< – find the length (number of top level nodes) of a list » length ( [], 0). » length ( [X | Tail], N ) :- length (Tail, N1), N is N » ?- length ( [a, b, [ c, d ], e ], N). » N = 4  It’s important to differentiate between X = Y (which tries to match X and Y) and X =:= Y, which tests numeric equality » ?- 1+2 =:= » yes » ? = » no » ?- 1 + A = B + 2%matches and A set to 2, B set to 1

(9.26) Controlling Backtracking  Another way to control efficiency in a Prolog program is to directly control the backtracking mechanism  Means to do this called the cut (denoted !) – cut is a subgoal that is always satisfied, but backtracking can’t pass through » freezes choices made to that point – C :- P, Q, R, !, S, T, U. – C :- V. – Suppose try to satisfy rule A :- B, C, D. » satisfy B, try to satisfy C, match first LHS, try to satisfy P, Q, R, S, T, U » backtracking works freely when trying to satisfy P, Q or R » once past !, backtracking works freely trying to satisfy S, T or U » if S eventually fails, won’t backtrack to try other alternatives for P, Q, R, which means C fails. Also won’t try rule C :- V.

(9.27) Controlling Backtracking (continued)  Example – max (first, second, larger) » max (X, Y, X) :- X >= Y. » max (X, Y, Y). – if first rule succeeds, never have to check second, so add cut » max (X, Y, X) :- X >= Y, !. » max (X, Y, Y). – important if max is one of several subgoals » foo (A, B) :- max (A, B, Large), » bar (Large). – if max is satisfied by A >= B and bar fails, no point in trying to satisfy max again.

(9.28) N Queens Hints  One way to start this is to pass in a list of the row positions, with variables for each of the columns – variables get instantiated while executing – add rule » board ([1/C1, 2/C2,..., 8/C8]). – then to invoke your program » |?- board(Soln), nqueens(Soln). » Soln = [1/4, 2/2,..., 8/1] » type ; to find more than one solution

(9.29) Running Prolog  Log onto workstation in design center and type Prolog at system prompt.  The interpreter provides a top level prompt |?- – load a file » |?- [‘filename’]. – allow clauses to be entered directly from terminal » |?- [user] » |%no way to save - for play only » | ^D%control D returns to top level » |?- – to interrupt execution » |?- foo (X). » ^C » Prolog interrupt - press h for help%gets menu of choices – to exit » |?- ^D

(9.30) Running Prolog (continued)  debugging – |?- trace. » starts extensive tracing mode » Enter key single steps » |?- notrace. turns off trace – |?- spy (rulename). » traces only rule » |?- spy (member). » |?- nospy. turns of spy  Use Unix script command to capture screen output to hand in