1 / 20

More Built-in Predicates

Learn about the built-in predicates in Prolog for testing the type of terms and manipulating the Prolog database. Includes examples and explanations.

gswan
Download Presentation

More Built-in Predicates

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta

  2. Testing the Type of Terms data objects (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 Compound objects (structures) simple objects constants variables atoms numbers

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