3.3.2 Implementation of scan/1

The code of scan/1 and its helpers.

Now we will have a look at the code of scan/1 and its helpers. We will again assume that our first laughing machine (see Section 1.4.1) is in the database under the name a1 and discuss what happens when we copy it to b1 by calling scan(cp(a1,b1)).

Here's the code of the toplevel predicate scan/1. It has only one clause:

scan(A):-
  retractall(state(A,_)),       % retract state markers
  start(A,S),                   % find start state
  scan_state(A,S).              % start scanning from there

Line 1 contains the head of the unary predicate scan(A). Its single argument is the specification of the operation to be carried out. Hence in the case of our example, A will be cp(a1,b1). Line 2 then removes all database entries with the pattern state(A,_). As we will see soon, our system uses such entries to remember what has already been done. Some such state/2-entries might still be in the database from earlier runs of scan/1. So it's a good idea to start by removing all of them.

Line 3 consists of a call to start/2. Here, the execution of our program branches according to the operation specified as input. Different clauses of start/2 match for different properties. They call different other predicates, but there is one invariant: All of them output a state name in S, namely the name of the start state of the automaton resulting from the requested operation. So in our example with A=cp(a1,b1), S will be 1. To see how this is achieved and what else happens, let's look at the relevant clause of start/2:

start(cp(A1,A2),S):-
  start(A1,S),
  retractall(start(A2,_)),
  retractall(trans(A2,_,_,_)),
  retractall(final(A2,_)),
  assert(start(A2,S)).

In our example A1 and A2 are unified with a1 and b1. First of all in the body, the name of the start state of a1 is retrieved (resulting in the instantiation S=1). Next any potential remains of other automata name b1 are removed. Finally, and a start state of the name just retrieved is asserted for a2.


Kristina Striegnitz, Patrick Blackburn, Katrin Erk, Stephan Walter, Aljoscha Burchardt and Dimitra Tsovaltzi
Version 1.2.5 (20030212)