.
By altering the clauses for element
, you
can describe a variety of other
structures, for example menus, conversations, or stages in someone's
lifetime: school leads to PPE or psychology or philosophy; PPE leads to
merchant banking; psychology leads to a D.Phil and thence to academic
fame; philosophy leads to destitution .... You might try this. When
you have done so, you can call spin
again.
This is a very simple demonstration of Prolog in grammar-writing: we are all familiar with grammars for describing sentences, but they can also describe other structures, such as stories or music. In general, you can use a grammar in one of two ways. The first is for analysing structures into their parts. The second, which we do here, is generating structures from those parts.
These kinds of grammar, incidentally, have been made out of some very simple Prolog components, and would not really be suitable for describing the structure of sentences. (Why?) However, Prolog also contains a special feature called DCG rules that allow you to write grammars for natural languages very easily. If you want to try your hand at this, ask later on in the course.
Note that the grammar in SPIN lacks coherence between its elements, as was probably obvious from some of the stories. In the same way, a grammar for English which stated that
A sentence is a noun phrase followed by a verb phrasewould not as it stands prohibit singular nouns from being followed by plural verbs. Nor would it prohibit sentences like ``Melancholy Justice flirted purposefully with 127 purple elephants''. To take account of such things, we need some way to specify agreement: to say that the form of one element much depend on that of another. You can do this easily with DCG rules.
Incidentally, SPIN
is similar to a transition matrix
which is
another method used to describe behaviour and changes of state in a
system. In a transition matrix, each element of the grammar is linked
to each element which can follow it by a number: the number specifies
the probability that one will succeed the other. So instead of, as in
spin
, choosing any branch with equal probability, we can state that
some branches are high-probability and others are very low. Here's one
way we might encode a transition matrix for behaviour at CTC:
Here, prob(X,Y,P)
means ``there is a probability of P that state Y
will follow state X''. There is no chance of state 6 following state 1;
it can only follow state 5 (you can't apply help you
don't have). We could write a modified spin
that took these
probabilities into account, and you might find the idea useful if you
want to have a go at story generation or something similar.
.