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.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Prolog Nonmonotonic logic.
Modern Programming 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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
”assert” and ”retract”
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
FATIH UNIVERSITY Department of Computer Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CENG421&553 Fall03.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
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 Input and Output Notes for Ch.6 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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
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.
TES3111 October 2001 Artificial Intelligence LISP.
Dr Eleni Mangina – COURSE: LOGIC PROGRAMMING (during a joint degree with Fudan University in Software Engineering) DEPT. OF COMPUTER SCIENCE UCD LOGIC.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Monkey and Bananas Exercise Notes on Exercise 3.10 of Bratko For CSCE 580 Sp03.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
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
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
1 Problem Solving using computers Data.. Representation & storage Representation of Numeric data The Binary System.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Lecture 9: Constraint logic programming
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
Tutorial 9 Iteration. Reminder Assignment 8 is due Wednesday.
For Friday No reading Prolog Handout 2. Homework.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Chapter 2 Syntax and meaning of prolog programs Part 1.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 8: Term manipulation & Meta-programming.
Logic Programming Lecture 2: Unification and proof search.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Prolog syntax + Unification
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
PROLOG.
Presentation transcript:

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

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Testing the Type of Terms var( X) succeeds if X is an uninstantiated variable nonvar( X) atom( X) succeeds if X currently stands for an atom integer( X) float( X) number( X) number (integer or float) atomic( X) number or atom compound( X) structure data objects (terms) simple objects Compound objects (structures) constantsvariables atomsnumbers

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Type of Terms in SWI-Prolog See section 4.5 of the manual There a few more built-in predicates, such as –string(+Term) –ground(+Term) succeeds if Term contains no uninstantiated variables

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Example: Counting Atoms ch7_1.pl We want to count actual occurrences of an atom, not terms that match an atom count1(Atom,List,Number) counts terms that match Atom 3 ?- count1(a,[a,b,X,Y],Na). X = a Y = a Na = 3 count2(Atom,List,Number) counts actual occurrences of Atom 5 ?- count2(a,[a,b,X,Y],Na). X = _G304 Y = _G307 Na = 1

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cryptarithmetic Puzzle SEND + MORE = MONEY Assign distinct decimal digits to distinct letters so that the sum is valid Albert Newell and Herbert Simon studied in depth puzzle like these in their study of human problem solving (Human Problem Solving, Prentice Hall, 1972) People use a mixture of trial and error and constraint processing: e.g., M must be 1, S must be 8 or 9, O must be 0, etc.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cryptarithmetic Puzzle II sum( N1,N2,N) if N is N1+N2 Numbers are represented by lists of digits: the query is: ?-sum( [S,E,N,D],[M,O,R,E],[M,O,N,E,Y]). We need to define the sum relation; we generalize to a relation sum1, with Carry digit from the right (before summing, C1) Carry digit to the left (after summing, C) Set of digits available before summing (Digits1) Set of digits left after summing (Digits) sum1( N1,N2,N,C1,C,Digits1,Digits)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cryptarithmetic Puzzle III Example: 1 ?- sum1([H,E],[6,E],[U,S],1,1,[1,3,4,7,8,9],Digits). H = 8 E = 1 U = 4 S = 3 Digits = [7, 9] There are several (four, in fact) other answers. 1<- <

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cryptarithmetic Puzzle IV We start off with all digits available, we do not want any carries at the end, and we do not care about which digits are left unused: sum(N1,N2,N) :- sum1(N1,N2,N,0,0,[0,1,2,3,4,5,6,7,8,9],_). We assume all three lists are of the same lengths, padding with zeros if necessary: ?- sum([0,S,E,N,D],[0,M,O,R,E],[M,O,N,E,Y]).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cryptarithmetic Puzzle V sum1( [], [], [], C, C, Digits, Digits). sum1( [D1|N1], [D2|N2], [D|N], C1, C, Digs1, Digs) :- sum1( N1, N2, N, C1, C2, Digs1, Digs2), digitsum( D1, D2, C2, D, C, Digs2, Digs). digitsum( D1, D2, C1, D, C, Digs1, Digs) :- del_var( D1, Digs1, Digs2), % Select an available digit for D1 del_var( D2, Digs2, Digs3), % Select an available digit for D2 del_var( D, Digs3, Digs), % Select an available digit for D S is D1 + D2 + C1, D is S mod 10, % Reminder C is S // 10. % Integer division

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Cryptarithmetic Puzzle VI Nonderministic deletion of variables del_var( A, L, L) :- nonvar(A), !. % A already instantiated del_var( A, [A|L], L). del_var( A, [B|L], [B|L1]) :- del_var(A, L, L1).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Constructing and Decomposing Terms Clauses are terms too! As in LISP, data and programs have a common representation: S-expressions (atoms or lists) in LISP, terms in Prolog 2 ?- [user]. |: a. |: a :- b. 3 ?- clause(a,X). X = true ; X = b ; No

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Clauses are Terms The principal functor of a clause is its neck (:- ) 4 ?- [user]. |: :-(b,c). |: % user compiled 0.00 sec, 32 bytes Yes 5 ?- listing(b). b :- c. Yes

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering univ Term =.. L, if –L is a list containing the principal functor of Term, followed by Term’s argument –Example: fig7_3.pl substitute( Subterm,Term,Subterm1,Term1) ?-substitute( sin(x),2*sin(x)*f(sin(x)),t,F).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering univ Example: –fig7_3.pl –substitute( Subterm,Term,Subterm1,Term1) –?-substitute( sin(x),2*sin(x)*f(sin(x)),t,F). An occurrence of Subterm in Term is something in Term that matches Subterm If Subterm = Term, then Term1 = Subterm1 Else if Term is atomic, then Term1 = Term Else carry out the substitution on the arguments of Term

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Dynamic Goals Goals may be created at run time, as in: Obtain( Functor), Compute( Arglist), Goal =.. [ Functor|Arglist], Goal. % works in SWI; with some Prologs, call( Goal) See ch7_2.pl for an example: try_call :- Goal =.. [ member| [a, [a,b,c]]], % Goal is a term Goal. % Goal is a predicate This is also an example of Prolog’s ambiguous syntax: the first Goal is a term (a variable), while the second Goal is a predicate

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Equality and Comparison X = Y matching (unification) X is E matches arithmetic value (discouraged in SWI) E1 =:= E2 arithmetic E1 =\= E2 arithmetic inequality T1 == T2 literal equality of terms T1 \== T2 not identical T2 term comparison See section of SWI manual for standard ordering

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Prolog Database Manipulation asserta/1 assertz/1 Same as assert/1 retract/1 More, described in section 4.13 of SWI manual Only dynamic predicates may be asserted, retracted, etc. dynamic +Functor/+Arity,... Section 4.14 of SWI manual

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Control Facilities once( P) fail true not P (alternative syntax: \+ P) call( P) repeat, defined as: repeat. repeat :- repeat. dosquares example: ch7_3.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering bagof, setof, and findall bagof( X,P,L) if L is the list of Xs such that P(X) holds setof( X,P,L) is like bagof, but without duplication Read ^ as “there exists” in bagof and setof findall( X,P,L) is like bagof but collects all objects X regardless of (possibly) different solutions for variables in P that are not shared with X Code for findall is in fig7_4.pl renamed findall1, because findall is built-in

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering bagof, etc. Examples fig7_4.pl: –age( peter,7). age( ann,5). age( pat,8). age( tom,5). ?- bagof( Child,age(Child,5),L). ?- bagof( Child,age(Child,Age),L). ?- bagof( Child,Age^age(Child,Age),L). ?- bagof( Age,Child^age(Child,Age),L). ?- setof( Age, Child^age(Child,Age),L). ?- setof( Age:Child,age(Child,Age),L). ?-findall( Child,age( Child,Age),L).