big(bear). big(elephant). small(cat). brown(bear). black(cat). grey(elephant).(a) Write definitions for: anything dark is either black or brown.
(b) Trace the execution of the goals:
?- big(X), dark(X). ?- dark(X), big(X).using the tracer. This is invoked using
?- trace.Respond to each prompt with carriage return. Count the number of lines in the trace in each case. Comment on the execution of the two cases.
(c) Write a relation to test and display how many times the predicates big, small, brown, black and grey are used in the above.
:- ['/home/mros/www/prologcc/code/utils'].This causes the following definitions to be loaded.
?- initcounter(foo), inc(foo), writev(foo). foo = 1 Yes
link(bondst,[bakerst,greenpk,oxfordc]).N.B. The data is present in the files you copied (tube.pl) if you want to make use of it.
Here is an example of what happens when the search terminates successfully.
6 ?- bfs(wrs,tcr,S). S = n(tcr, n(gdg, n(wrs, 0, 0), 1), 2)The query means: find a path from wrs (warren st) to tcr (tottenham ct. rd) and bind the solution node to S. In this case the search succeeds. Note carefully the format of the solution, which is a search node.
?- bfs(wrs,tcr,S), show_path(S). wrs - gdg - tcrYou will also need to find out about the built in predicates write, nl, and for the more ambitious, format. Help on all such built-ins can be accessed from Prolog using
?- help(predicate-name).
3 ?- bfs(cmt,wrs,S), show_solution(S). solution path is: cmt - eus - wrs no. of nodes generated = 0 no. of nodes expanded = 0 max. no. of nodes in memory = 0 S = n(wrs, n(eus, n(cmt, 0, 0), 1), 2)In order to make it work properly, calls to set the the relevant counters (nodes_generated and nodes_expanded, nodes_in_mem, max_nodes_in_mem) must be inserted at appropriate points in the file search.pl.
The exercise is to examine the file search.pl and discover where to set the counters
Hint: You will need to use the following predefined predicates to set the variables. All of them are defined in utils.pl.
where S is the name of the state, P is the parent and D is the depth, so the solution node here has state tcr (which is the goal), depth 2 and parent n(gdg, n(wrs, 0, 0), 1)), i.e. a node with state gdg, depth 1 and parent , which is the initial node. By convention, this has parent 0 and depth 0 (see the predicate make_initial_node in search.pl).