SlideShare a Scribd company logo
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)
constants variables
atoms numbers
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<- <-1
8 1
6 1
4 3
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
• T1 @< T2 term comparison
• See section 4.6.1 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).

More Related Content

PPTX
PPTX
Prolog 7-Languages
DOCX
AI Lab Manual.docx
PPTX
Prolog & lisp
PPT
09 logic programming
PDF
12 - Overview
PPT
PROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOG
PPT
Pl vol1
Prolog 7-Languages
AI Lab Manual.docx
Prolog & lisp
09 logic programming
12 - Overview
PROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOGPROLOG
Pl vol1

Similar to PR7.ppt (20)

PDF
Logic Programming and ILP
PDF
BCS515B Module 5 vtu notes : Artificial Intelligence Module 5.pdf
PDF
Exchanging More than Complete Data
PDF
10 logic+programming+with+prolog
PPT
Chaps 1-3-ai-prolog
PPT
Pl vol1
PPTX
Prolog -Cpt114 - Week3
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Prolog programming
PPT
Frstorder 9 sldes read
PPTX
Prolog language programming in the facts.pptx
PPTX
Jarrar: First Order Logic
PPT
Prolog basics
PDF
Learn a language : LISP
Logic Programming and ILP
BCS515B Module 5 vtu notes : Artificial Intelligence Module 5.pdf
Exchanging More than Complete Data
10 logic+programming+with+prolog
Chaps 1-3-ai-prolog
Pl vol1
Prolog -Cpt114 - Week3
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Prolog programming
Frstorder 9 sldes read
Prolog language programming in the facts.pptx
Jarrar: First Order Logic
Prolog basics
Learn a language : LISP
Ad

More from RithikRaj25 (17)

PPT
html1.ppt
PPT
PPTX
PPT
Introduction To Database.ppt
PPT
Data.ppt
PPT
DataTypes.ppt
PPTX
NoSQL.pptx
PPTX
PPT
text classification_NB.ppt
PPT
html1.ppt
PPTX
slide-keras-tf.pptx
PPT
Intro_OpenCV.ppt
PPT
lec1b.ppt
PPT
objectdetect_tutorial.ppt
PPTX
14_ReinforcementLearning.pptx
PPTX
datamining-lect11.pptx
PPT
week6a.ppt
html1.ppt
Introduction To Database.ppt
Data.ppt
DataTypes.ppt
NoSQL.pptx
text classification_NB.ppt
html1.ppt
slide-keras-tf.pptx
Intro_OpenCV.ppt
lec1b.ppt
objectdetect_tutorial.ppt
14_ReinforcementLearning.pptx
datamining-lect11.pptx
week6a.ppt
Ad

Recently uploaded (20)

DOCX
Factor Analysis Word Document Presentation
PPTX
(Ali Hamza) Roll No: (F24-BSCS-1103).pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
A Complete Guide to Streamlining Business Processes
PDF
Navigating the Thai Supplements Landscape.pdf
PPTX
CYBER SECURITY the Next Warefare Tactics
PPT
DU, AIS, Big Data and Data Analytics.ppt
PDF
Introduction to Data Science and Data Analysis
PPTX
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
PDF
Business Analytics and business intelligence.pdf
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPTX
Pilar Kemerdekaan dan Identi Bangsa.pptx
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
PPTX
SAP 2 completion done . PRESENTATION.pptx
PDF
Introduction to the R Programming Language
PPTX
New ISO 27001_2022 standard and the changes
PPTX
Managing Community Partner Relationships
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PDF
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...
Factor Analysis Word Document Presentation
(Ali Hamza) Roll No: (F24-BSCS-1103).pptx
Qualitative Qantitative and Mixed Methods.pptx
A Complete Guide to Streamlining Business Processes
Navigating the Thai Supplements Landscape.pdf
CYBER SECURITY the Next Warefare Tactics
DU, AIS, Big Data and Data Analytics.ppt
Introduction to Data Science and Data Analysis
QUANTUM_COMPUTING_AND_ITS_POTENTIAL_APPLICATIONS[2].pptx
Business Analytics and business intelligence.pdf
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
Pilar Kemerdekaan dan Identi Bangsa.pptx
Capcut Pro Crack For PC Latest Version {Fully Unlocked 2025}
SAP 2 completion done . PRESENTATION.pptx
Introduction to the R Programming Language
New ISO 27001_2022 standard and the changes
Managing Community Partner Relationships
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
Data Engineering Interview Questions & Answers Batch Processing (Spark, Hadoo...

PR7.ppt

  • 1. 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
  • 2. 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) constants variables atoms numbers
  • 3. 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
  • 4. 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
  • 5. 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.
  • 6. 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)
  • 7. 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<- <-1 8 1 6 1 4 3
  • 8. 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]).
  • 9. 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
  • 10. 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).
  • 11. 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
  • 12. 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
  • 13. 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).
  • 14. 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
  • 15. 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
  • 16. 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 • T1 @< T2 term comparison • See section 4.6.1 of SWI manual for standard ordering
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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).