SlideShare a Scribd company logo
1 of 20
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

Similar to PR7.ppt

2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...DrkhanchanaR
 
Seismic data processing introductory lecture
Seismic data processing introductory lectureSeismic data processing introductory lecture
Seismic data processing introductory lectureAmin khalil
 
Number Crunching in Python
Number Crunching in PythonNumber Crunching in Python
Number Crunching in PythonValerio Maggio
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasexabhaysonone0
 
Advanced Spreadsheet Skills - Empowerment Technologies
Advanced Spreadsheet Skills - Empowerment TechnologiesAdvanced Spreadsheet Skills - Empowerment Technologies
Advanced Spreadsheet Skills - Empowerment TechnologiesMark Jhon Oxillo
 
Towards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositoriesTowards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositoriesValentina Paunovic
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxchristinamary2620
 
High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...Vissarion Fisikopoulos
 
R-programming-training-in-mumbai
R-programming-training-in-mumbaiR-programming-training-in-mumbai
R-programming-training-in-mumbaiUnmesh Baile
 

Similar to PR7.ppt (20)

2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
(Ai lisp)
(Ai lisp)(Ai lisp)
(Ai lisp)
 
Seismic data processing introductory lecture
Seismic data processing introductory lectureSeismic data processing introductory lecture
Seismic data processing introductory lecture
 
Number Crunching in Python
Number Crunching in PythonNumber Crunching in Python
Number Crunching in Python
 
Mathematical Modeling With Maple
Mathematical Modeling With MapleMathematical Modeling With Maple
Mathematical Modeling With Maple
 
01.ppt
01.ppt01.ppt
01.ppt
 
Relational Calculus
Relational CalculusRelational Calculus
Relational Calculus
 
R training2
R training2R training2
R training2
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex316_16SCCCS4_2020052505222431.pptdatabasex
316_16SCCCS4_2020052505222431.pptdatabasex
 
Advanced Spreadsheet Skills - Empowerment Technologies
Advanced Spreadsheet Skills - Empowerment TechnologiesAdvanced Spreadsheet Skills - Empowerment Technologies
Advanced Spreadsheet Skills - Empowerment Technologies
 
Q
QQ
Q
 
Towards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositoriesTowards advanced data retrieval from learning objects repositories
Towards advanced data retrieval from learning objects repositories
 
Mbd dd
Mbd ddMbd dd
Mbd dd
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
 
High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...High-dimensional polytopes defined by oracles: algorithms, computations and a...
High-dimensional polytopes defined by oracles: algorithms, computations and a...
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
R-programming-training-in-mumbai
R-programming-training-in-mumbaiR-programming-training-in-mumbai
R-programming-training-in-mumbai
 
Trc final
Trc finalTrc final
Trc final
 

More from RithikRaj25

More from RithikRaj25 (17)

html1.ppt
html1.ppthtml1.ppt
html1.ppt
 
Data
DataData
Data
 
Data
DataData
Data
 
Introduction To Database.ppt
Introduction To Database.pptIntroduction To Database.ppt
Introduction To Database.ppt
 
Data.ppt
Data.pptData.ppt
Data.ppt
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
NoSQL.pptx
NoSQL.pptxNoSQL.pptx
NoSQL.pptx
 
NoSQL
NoSQLNoSQL
NoSQL
 
text classification_NB.ppt
text classification_NB.ppttext classification_NB.ppt
text classification_NB.ppt
 
html1.ppt
html1.ppthtml1.ppt
html1.ppt
 
slide-keras-tf.pptx
slide-keras-tf.pptxslide-keras-tf.pptx
slide-keras-tf.pptx
 
Intro_OpenCV.ppt
Intro_OpenCV.pptIntro_OpenCV.ppt
Intro_OpenCV.ppt
 
lec1b.ppt
lec1b.pptlec1b.ppt
lec1b.ppt
 
objectdetect_tutorial.ppt
objectdetect_tutorial.pptobjectdetect_tutorial.ppt
objectdetect_tutorial.ppt
 
14_ReinforcementLearning.pptx
14_ReinforcementLearning.pptx14_ReinforcementLearning.pptx
14_ReinforcementLearning.pptx
 
datamining-lect11.pptx
datamining-lect11.pptxdatamining-lect11.pptx
datamining-lect11.pptx
 
week6a.ppt
week6a.pptweek6a.ppt
week6a.ppt
 

Recently uploaded

꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Callshivangimorya083
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfLars Albertsson
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一F sss
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 

Recently uploaded (20)

꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
꧁❤ Greater Noida Call Girls Delhi ❤꧂ 9711199171 ☎️ Hard And Sexy Vip Call
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
Industrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdfIndustrialised data - the key to AI success.pdf
Industrialised data - the key to AI success.pdf
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
办理学位证中佛罗里达大学毕业证,UCF成绩单原版一比一
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 

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).