11.3. Pattern matching

| Chapter 11: Phrases |

| 11.3. Pattern matching

In this section, let's take the built-in phrase "remove ... from play" as an example. As the wording suggests, this causes whatever thing is named to disappear from every room, going into a sort of off-stage limbo.

Although the wording can be different each time this is used:

> remove the diamonds from play;
> remove Mr Cogito from play;
> remove the honey sandwich from play;

... a single definition in Inform's Standard Rules nevertheless covers all of these possibilities. It is written like so:

> To remove (item - an object) from play: ...

The bracketed part of the definition tells Inform to expect an object in that position, and Inform enforces this carefully. So this definition might tell Inform what "remove the barricade from play" means, but not what

> remove "blue cheese" from play;
> remove 63 from play;

mean. Unless some other definition sorts the matter out, Inform will reply to uses like this with a Problem message:

> Problem. You wrote 'remove 63 from play' , but '63' has the wrong kind of value: a number rather than an object.

The object does not need to be named literally, but can be anything which works out to be an object: for instance,

> After dropping something in the Nebulous Mist:
> remove the noun from play.

which Inform allows because "noun", here, is a name for the object which is being acted on.

Inform decides which definition to apply in a process called "pattern matching".

The bracketed part of the example definition has the form "(name - kind)". The definition only applies if the text supplied agrees with the "kind" part - for instance, the diamonds agreed with "object", but 63 did not. If the definition does apply, then the Inform works through the rest of the phrase using "name" to mean whatever value matched. For example:

> To slam shut (box - an open container):
> say "With great panache, you slam shut [the box].";
> now the box is closed.

When this phrase is followed, "box" means whatever open container the pattern-matcher found when it was called for. For example, if Inform reads

> slam shut the Dutch armoire;

then it acts on this by following the definition of "slam shut ...", using the Dutch armoire object as the value of "box", so it prints:

> With great panache, you slam shut the Dutch armoire.

and renders it closed.

In fact any description can be given in the definition, and that includes a single, specific value. For instance, we could define:

> To grant (bonus - a number) points:
> increase the score by the bonus.

> To grant (bonus - 7) points:
> say "You shiver uncontrollably."

which would withhold this unlucky bounty. That would mean that:

> grant 7 points;
> grant seven points;

would each produce uncontrollable shivers, because Inform uses the definition applying to the number 7; but

> grant six points;

would increase the score by 6. In general Inform always follows the principle that more specific definitions take priority over more general ones. So although the definitions:

> To grant (bonus - a number) points: ...
> To grant (bonus - 7) points: ...

both apply to the case of "grant 7 points", Inform uses the second, because it's the more specific of the two possibilities.

Sometimes it will not be possible to tell if the value supplied meets the requirements until the game is actually playing. If, at run-time, no definition fits some phrase which has to be carried out, a run-time problem message is produced.

Finally, and more straightforwardly, we can specify variations in wording using slashes between alternative words in a "To ..." definition. For instance:

> To grant (bonus - a number) point/points: ...

allows the final word to be either "point" or "points". Slashes like this can only be used with literal words, not bracketed values, and give alternative forms only of a single word at a time. (If we need more variation than that, we should make more than one definition.)

169
| Example Ahem Writing a phrase, with several variant forms, whose function is to follow a rule several times. |
RB

170
| Example Ferragamo Again Using the same phrase to produce different results with different characters. |
RB

Here we use phrases that match individual items where possible, and the general kind otherwise: "Ferragamo Again" The Break Room is a room. Vanessa, Tina, and Lisa are women in the Break Room. Mark and Holman are men in the Break Room. Understand the commands "ask" and "tell" and "answer" as something new. Understand "talk about [any subject]" as talking about. Talking about is an action applying to one visible thing. Understand "talk about [text]" as talking randomly about. Talking randomly about is an action applying to one topic. Carry out talking randomly about: say "Mostly you're interested in [the list of subjects]." Carry out talking about something: now the previous subject is the noun. Report talking about something: say "You chat for a while about [the noun]." A subject is a kind of thing. Assyrian vowel sounds, designer handbags, and instant run-off voting are subjects. Understand "linguistics" and "mute" and "stop" as sounds. Understand "prada" and "tods" and "coach" and "carmen marc valvo" as designer handbags. Understand "reform" and "election" and "election fraud" and "two-party system" and "Diebold" as instant run-off voting. To say (annoyed-person - a person) gestures in irritation: say "[The annoyed-person] sighs heavily. [run paragraph on]" To say (annoyed-person - Vanessa) gestures in irritation: say "[The annoyed-person] takes off her glasses and polishes them on her sleeve. [run paragraph on]". To say (annoyed-person - Holman) gestures in irritation: say "Holman bobs his head. [run paragraph on]" The previous subject is a subject that varies. Instead of talking about something for more than one turn: if the noun is the previous subject, say "[a random visible person who is not the player gestures in irritation]Maybe you should let this one go.[line break][paragraph break]"; otherwise continue the action. Test me with "talk about chocolate / talk about vowel sounds / g / talk about handbags / talk about prada / talk about tods".

Previous | Contents | Next