Placing a new rule between two existing rules

Suppose you’re writing a new rule (Rule Z) that needs to fall between two existing rules (Rule A and Rule B) in a rulebook.

Is there any easy way to say so?

If you just say

Rule Z is listed after Rule A in the foo rulebook. Rule Z is listed before Rule B in the foo rulebook. then the second statement supersedes the first; your new rule will definitely be sorted before Rule B, but may not lie after Rule A.

Of course, you could always just figure out where Inform would sort the new rule with no guidance (either by reasoning out from the rule sorting laws, or by just compiling and checking the Rules Index.)
There are three possibilities:

Rule Z is normally sorted before Rule A:
Say “Rule Z is listed after Rule A”, and let Inform take it from there.
Rule Z is normally sorted between Rule A and Rule B: Great; just leave it alone.
Rule Z is normally sorted after rule B: Say “Rule Z is listed before Rule B”, and let Inform take it from there.

But that seems like poor practice. Changes that you later make in seemingly unrelated parts of this rulebook might mess up the position of Rule Z. Any better alternative that I’m missing?

I’m afraid not, and it can be a pain.

Of course, a better alternative wouldn’t be that easy to devise. The syntax that Inform allows you has the advantage that you cannot end up with a set of incompatible requirements yourself. Were you allowed to say “Rule A comes exactly before rule B”, for instance, this would be contradicted by “Rule C comes exactly after rule A”, and there would be no way for Inform to do both, nor could it just ignore the earlier rule (because then it wouldn’t have any way to place rule A).

I must add, though, that this problem comes up only rarely. Most of the time, you can get what you want by just using “First X rule”, “X rule” and “Last X rule”. The only time I have ever needed to pay attention to how the rules were sorted was when I was messing with the turn sequence rules and needed some stuff to happen between reading a command and parsing the command. So though you may need it for what you are doing right now, I doubt that you will need it often.

What if we tried something like this: You can declare a rule to come “immediately after” or “immediately before” another rule, and it will be placed there for now, but can still be superseded by declarations later in the source code.

How that would work in practice:
You need Rule Z to occur after Rule A and before Rule B.
So you say Rule Z is listed immediately after Rule A in the foo rulebook.
Later, you have another rule, Rule Y, which also has to come between A and B. So you say Rule Y is listed immediately after Rule A in the foo rulebook.
Now, Rule Z is no longer directly after rule A; the sequence has become A, Y, Z, B.
But you still have what you needed; Z and Y both happen after A and before B. If the order of Y and Z matters, of course, you could instead specify that Y is listed immediately before or after Z.

I actually run across the issue fairly often, specifically in the context of rulebooks that govern character behavior.

For example, a generic Dog Behavior rulebook might contain the Dog Chooses Intended Direction rule, followed later by the Dog Takes Intended Action rule.

If I want to later add a Some Dogs Bark at Closed Doors rule, then it has to happen after the Intended Direction Rule – the dog won’t bark unless it knows it wants to go through that door – but before the Take Action Rule, since the action it takes will depend on how people reacted to the incessant barking.

It’s not a huge problem. The new rule is usually more specific than the existing ones: the Dog Chooses Intended Direction Rule is a Dog Behavior rule for a dog (called canine). the Dog Takes Action Rule is a Dog Behavior rule for a dog (called canine). the Dogs Sometimes Bark At Closed Doors Rule is a Dog Behavior rule for a rude dog (called pest).
So it’s enough to say that The Dogs Sometimes Bark At Closed Doors Rule is listed after the Dog Chooses Intended Direction Rule in the Dog Behavior rulebook. since it would otherwise be listed before both of 'em.

But I always feel like I’m guilty of sloppy programming when I do this…

It’s possible to rearrange the order of rules in an existing rulebook using “last” and “first”.

E.g. the carry out looking rules normally run in this order:

(1) The room description heading rule
(2) The room description body text rule
(3) The room description paragraphs about objects rule
(4) The check new arrival rule

You can move the room description body text rule last and stick a new rule in between the the room description heading rule and the room description paragraphs about objects rule like so:

This is the Marseillaise rule:
	say "'Allons, enfants de la patrie, le jour de gloire est arrivé!'".
	
The so tired rule is listed last in the carry out looking rulebook.

The room description paragraphs about objects rule is listed last in the carry out looking rulebook.

The check new arrival rule is listed last in the carry out looking rulebook.

The room description body text rule is listed last in the carry out looking rulebook.

Now the carry out looking rules end up ordered thus:

(1) The room description heading rule
(2) The Marseillaise rule
(3) The room description paragraphs about objects rule
(4) The check new arrival rule
(5) The room description body text rule

The idea of arranging a number of things into a desired order by repeatedly moving a selected thing to the first or last position almost seems like it might be the kind of puzzle one might build into a game for the player to solve. :laughing:

Robert Rothman

… Have you seen ‘Sorting out sorting?’

http://www.youtube.com/watch?v=YvTW7341kpA

You might be able to say

Rule Z is listed after Rule A in the foo rulebook. Rule B is listed after Rule Z in the foo rulebook.You still have to make sure that Rule B is not breaking anything by being moved (or, if Rule A is the more mobile, switch the order and write two “listed before” instructions), but that would place rule Z between Rule A and Rule B. This does seem like something that Inform should be able to implement, but it would require keeping track of the relative positions of at least every manually placed rule, and I can see that getting complicated, though not impossible.

Thanks, everyone; those are all good thoughts.

That’s the part that has me nervous, though. What I was looking for is a way to take a rulebook that might already have a dozen or more rules whose exact sequence matters, and slip a new rule into a specific place in the sequence without otherwise disturbing the existing ones…and without laying inadvertent traps for anyone who later wants to make yet more changes to the book.

I think what I’ll go with is the questionable kludge I mentioned before: compile the code once with no instructions on where the rule should lie, check the index to see where it landed (or just reason out where it would land from the specificity laws that Inform uses), and then issue either a “listed after” if it sorted too early, or “listed before” if it sorted too late. Along with comment lines specifying where it needs to be, in case later changes to other parts of the rulebook cause this rule to sort into the wrong place again.

That was awesome! I am so addicted to watching sorts.

flowingdata.com/2011/04/14/sorti … olk-dance/