Chapter 6 Slide, Figure 2.1, Data objects in Prolog Slide, Figure 2.2, Terms Slide, The type of a term can be tested Demo var( X), X is a (non-instantiated) variable. ?- var( a). ?- var( Z). ?- Z = a, var( Z). ?- var( Z), Z = a. nonvar( X), X is not a (non-instantiated) variable ?- nonvar( a). ?- nonvar( Z). ?- Z = a, nonvar( Z). ?- nonvar( Z), Z = a. Conclusion: var( X) succeeds when nonvar( X) fails and vice-versa. atom( X), X is an atom ?- atom( abc). ?- atom( 3.14). ?- atom( *). ?- atom( p(abc)). integer( X), X is an integer ?- integer( 3). ?- integer( 3.14). ?- integer( abc). float( X), X is a real number ?- float( 3.14). ?- float( 3). ?- float( abc). atomic( X), X is either an atom or a number. ?- atomic( 3.14). ?- atomic( abc). ?- atomic( p(abc)). compound( X), X is a structure ?- compound( abc). ?- compound( p(abc)). Load ch6.pl Scroll to % count( A, L, N) and show the implementation of count, one base case and two rules Demo count ?- count( a, [a,b,a,a], N). ?- count( a, [a,b,X,Y], Na). ?- count( b, [a,b,X,Y], Nb). ?- L = [a,b,X,Y], count( a, L, Na), count( b, L, Nb). Conclusion: When a matches X, X is instantiated to a and gets counted as an a. count2 solves this problem with the atom predicate. Demo count2 ?- L = [a,b,X,Y], count2( a, L, Na), count2( b, L, Nb). Scroll to count2 and then its implementation. Slide, Terms can be constructed or decomposed Univ Term =.. [Functor | ArgumentList] Scroll to % Built-in univ operator, =.. % Term =.. [Functor | ArgumentList] % [Functor | ArgumentList] is a list that contains the principle % functor of Term, followed by its arguments. Demo ?- f(a,b,c) =.. L. ?- f(a,b,c) =.. [Functor | ArgumentList]. ?- T =.. [ f | [a,b,c]]. ?- you =.. L. ?- you =.. [Functor | ArgumentList]. ?- 17 =.. L. % succeeds ?- T =.. L. % fails with instantiation error ?- T = you, T =.. L. Scroll to % square( Side) % triangle( Side1, Side2, Side3) % circle( Radius) % enlarge( Fig, Factor, Fig1) % Fig1 is Fig with each argument multiplied by % Factor. The arguments are already numbers. % Test: ?- enlarge( square( 5), 2, Figure). % Test: ?- enlarge( triangle( 3, 4, 5), 3, Figure). Demo tests, then discuss implementation. Slide, Terms can be constructed or decomposed Scroll to % Built-in predicate functor/3. % functor( Term, Functor, Arity) % Functor is the functor of Term and Arity is its arity. Demo ?- functor( f( a,b,c,d), F, N). ?- functor( g( a, 5), F, N). ?- functor( T, g, 3). ?- functor( t(f(X),X,t), Fun, Arity). Slide, Terms can be constructed or decomposed Scroll to % Built-in predicate arg/3. % arg( N, Term, Argument) % Argument is the Nth argument in Term, % left to right starting with 1. Demo ?- arg( 2, f( a,b,c,d,e), A). ?- arg( 2, f( X, t(a), t(b)), Y). ?- arg( 2, f( X, t(X), t(b)), Y). ?- arg( N, f( a,b,c,d,e), c). % Instantiation error Slide, Terms can be compared Demo ?- X = a. ?- X = Y. ?- X == a. ?- X == Y. ?- X = a, X == a. ?- X = a, Y = a, X == Y. Conclusion: == uses the instantiated values for comparison. Uninstantiated variables must be identical for == to succeed. ?- f( a, b) == f( a, b). ?- f( a, b) == f( a, X). ?- f( a, X) == f( a, Y). ?- X \== Y. ?- X = a, X \== a. Conclusion: == succeeds when \== fails and vice-versa. ?- 3 =:= 3. ?- X = 3, X =:= 3. ?- X =:= 3. % instantiation error ?- 4 < 7. ?- 7 < 4. ?- bear @< berry. % alphabetic order ?- berry @< bear. Scroll to % count3( Term, List, N) % Third version. N is the number of literal % occurrences of Term in List. % Test ?- count3(a, [a,b,X,Y,a], N). % Test ?- count3(X, [a,b,X,Y,a], N). % Test ?- count2(X, [a,b,X,Y,a], N). Demo tests, noting the difference between count3 and count2 Discuss implementation with == Slide, A Prolog program can be viewed as a relational database that can be updated by the following procedures Scroll to % Examples of assertz/1 and retract/1. % Note the dynamic declaration not in Bratko. % gprolog does not have assert/1. Scroll to raining. fog. Demo ?- nice. ?- disgusting. ?- retract( fog). ?- disgusting. ?- assertz( sunshine). ?- retract( raining). ?- nice. Scroll to show :- dynamic( fast/1). . . . % Test ?- faster( ann, _). A set of clauses can be removed through backtracking. Demo with the tests in the comments as follows: ?- assertz(( faster( X, Y) :- fast( X), slow( Y))). Installs the faster clause at the end of our database. ?- faster( A, B). Query who is faster than whom. By the facts in the database, ann is faster than tom and pat. ?- retract( slow( X)). Both slow( tom) and slow( pat) are removed through backtracking. ?- faster( ann, _). Now, ann is faster than nobody. Scroll to show :- dynamic( p/1). % asserta vs assertz % Test ?- assertz( p(b)), assertz( p(c)). % Test ?- assertz( p(d)), asserta( p(a)). % Test ?- p( X). Demo with the tests in the comments as follows: ?- assertz( p(b)), assertz( p(c)). p(b) is before p(c). ?- assertz( p(d)), asserta( p(a)). Now, can you predict the order? ?- p( X). Scroll to my_product Demo ?- my_product( 3, 4, P). ?- my_product( 3, X, 12). % instantiation error Concept of memoization: Precompute the values you need and store them in a database. Demo ?- L = [0,1,2,3], member( X, L). ?- L = [0,1,2,3], member( X, L), member( Y, L). Scroll to show :- dynamic( product/3). maketable :- L = [0,1,2,3,4,5,6,7,8,9], member( X, L), member( Y, L), Z is X * Y, assertz( product( X, Y, Z)), fail. Note that the purpose of the last fail is to force backtracking to compute all the elements in the table. Demo :- product( 3, 4, P). % no table yet :- maketable. :- product( 3, 4, P). :- product( X, 4, 12). :- product( X, Y, 12). Slide, All the objects that satisfy a given condition can be collected into a list Scroll to show % Data for bagof tests: age( peter, 7). age( ann, 5). age( pat, 8). age( tom, 5). age( henry, 5). age( henry, 5). % Another henry who is also 5 Demo ?- age( Child, 5). ?- age( pat, Age). ?- age( henry, Age). ?- age( Child, Age). Scroll to show % bagof( X, P, L) % L is a list of all the objects X % such that goal P is satisfied. % Test ?- bagof( Child, age( Child, 5), List). % Test ?- bagof( Age, age( pat, Age), List). % Test ?- bagof( Child, age( Child, Age), List). Demo with the tests in the comments. Scroll to show % setof( X, P, L) % L is a sorted list (set) of all the objects X % such that goal P is satisfied. % Test ?- bagof( Child, age( Child, 5), List). % Test ?- setof( Child, age( Child, 5), List). Demo with the tests in the comments. Scroll to% cube( N, C) % C is the cube of N cube( N, C) :- C is N * N * N. Demo ?- cube( 2, C). ?- cube( 3, C). Slide, Built-in procedures for reading and writing characters and Terms Demo ?- write( 3). ?- write( abc). ?- write( X). ?- read( X). abc. ?- read( X), write( X). abc. ?- read( X), write( X). 34. ?- read( X), write( X). 34.5. ?- read( X). a( 5, bcd). ?- read( X). def( [1,2,3]). ?- read( X). def( Y). Conclusion: The period is required with read, because it reads terms. ?- read( X), write( X). Conclusion: is read as the atom end_of_file ?- read( abc). abc. ?- read( abc). xyz. Conclusion: Reading an atom succeeds if the input is the same atom Slide, Summary of read( X) Demo ?- put( 65). ?- put( 66). ?- put( 122). ?- put( 48). ?- put( 49). ?- put( 10). % new line Conclusion: put( CharCode) outputs the character with ascii value CharCode Scroll to % cube/0 % An interactive program to prompt the user for the values to cube. Demo ?- cube. 2. 3. stop. Scroll to implementation and discuss Scroll to % cube2/0 % Erroneous attempt to simplify cube/0. Scroll to implementation Predict and Demo ?- cube2. 2. 3. 4. 5. stop. Problem: The first read consumes a value that should be used for input. Demo ?- nl. ?- nl, nl, nl. nl means new line Scroll to % cube3/0 % An interactive program to prompt the user for the values to cube. Demo ?- cube3. 2. 3. stop. Scroll to implementation and discuss Scroll to % writelist( L) % Outputs each element of L on a separate line. % One base case fact and one rule. Demo ?- writelist( [a,b,5,6]). Scroll to implementation and discuss Demo ?- write( a), write( b). ?- write( a), tab( 1), write( b). ?- write( a), tab( 3), write( b). Scroll to % writelist2 ( LL) % LL is a list of lists. % Outputs the elements of each list of LL on a separate line. Demo ?- writelist2( [[a,b,c], [d,e,f]]). Scroll to implementation and discuss Scroll to % bars( L) % L is a list of integers. % Prints a row of N *'s for each number N in L. % Ignore any negative values. Demo ?- bars( [5,2,-3,4]). Scroll to implementation and discuss Slide, Figure 6.5, Communication between a Prolog program and several files The default active input stream is the user keyboard. The dafault active output stream is the user display. Slide, Switching between streams Demo Quit more ch6.pl On Unix command line: $ more dog_data $ more gcd.pl $ more ch6.pl Scroll to % showfile( N) % Print the terms in the current stream % with consecutive numbers starting with N. % Test: see( 'dog_data'), showfile( 0), seen. % Test: see( 'gcd.pl'), showfile( 0), seen. Demo with above tests Scroll to implementation and discuss Scroll to % get0( C) % C is the next single character from the input stream. Demo ?- get0( X). a Note: no period ?- get0( X). b X instantiated to ascii value ?- get0( X). ?- get0( C), put( C). d ?- get0( X). abc Note this error ?- get0( X). Second attempt is uncaught exception Scroll to % get( C) % Same as get0, but skips nonprintable characters. Demo ?- get( X). a ?- get( X). b Scroll to % squeeze % Remove multiple blanks from the input stream. % Terminate with period. % Test: see( blanks). squeeze. seen. Demo Quit more ch6.pl On Unix command line: $ more dog_data Note that these are terms $ more blanks Note that these are ascii characters $ more ch6.pl Demo with above Test Scroll to implementation and discuss Scroll to % name( A, L) % L is the list of ASCII codes of characters in A. Demo ?- name( abc, L). ?- name( X, [98,99,100]). ?- name( abc, [97,Y,99]). ?- name( X, Y). Instantiation error Scroll to % taxi( X) % Test whether an atom X has taxi prefix. % A single rule with conc/3 using name/2. Demo ?- taxi( taxicab). ?- taxi( automobile). ?- taxi( taxi). ?- taxi( tax). Scroll to implementation and discuss You do NOT need to know the ascii values of the letters in "taxi" If you use the ascii values for the letters, they are "magic numbers" That is NOT good programming style. You will be marked down for using magic numbers.