I7: Passing a Value into a Rule

I’m convinced I’m missing something or not thinking of rules correctly. My question is basically this: how do I pass a value into a rule? I’m thinking of rules as functions and so I’m passing in a value and then the rule is going to operate on that value and spit something back out. I’m thinking in terms of “follow the rule ‘whatever’ with value x”.

Before going down a bad path of frustration, can someone confirm if my thinking is correct here:

I have to create an action-based rulebook that produces a value.
Then I have to have “consider ‘my rule’ with ‘my value’.”
My action-based rulebook then has to return its own value based on processing the value that was passed in.

The I7 equivalent of functions are to-phrases and to decide-phrases (ch. 11 in the doc). To-phrases don’t have a return value but to decide-phrases do. For example:

[code]The snack bar is a room. Alice is a woman in the snack bar. Bob is a man in the snack bar. An apple and a banana are in the snack bar.

To decide which thing is the preference of (eater - a person):
if eater is Alice, decide on the apple;
if eater is Bob, decide on the banana.

To have (eater - a person) choose (food - a thing):
say “[The eater] chooses [the food].”;
now the eater carries the food.

When play begins:
have Alice choose the preference of Alice;
have Bob choose the preference of Bob.[/code]

I have a more blathery response about what rules are.

Rules do operate in response to some kind of value you supply to them (See 18.8 in the docs for elaboration) but they may do anything you ask them to. There’s no law about a relationship between input and output values. You refer to some kind of input that fires the rule, but you’ll only get specific output that you ask for / program. That may be no output, or tons of output.

But at a broader level, every rule does have an internal result as far as Inform is concerned: success, failure or no outcome. This result will always be set after a rule has fired. There are default paths to each state, but you can also explicitly set the result of any action to any state. Inform might have considered that a rule failed, but you could say (not actual code) ‘actually that rule succeeded’ if it suited your purposes.

The success/failure states have real meaning for Inform in some cases (see the topic about ‘How do I make a scene start?’ for an example) but they won’t’t necessarily correspond to what you consider to be failures, successes or non-outcomes in terms of your game. This is an area where I’m personally not yet aware enough about what the states will do on Inform’s terms, so I don’t cleave to them too heavily.

The biggest real outcome of rules succeeding or failing is that at the moment one of these things happens, the rule stops firing. No further code in the rule is run.

This is where ‘consider’ and ‘abide by’ come in. If Inform is carrying out some code, and you ask it to do one of these things, you can get it to run through a rule like a subroutine, and have that rule’s outcome determine the outcome of the whole action, which can cause the main action to be cut off early, etc.

In terms of - how do I pass a value to a rule? Well, as I say, it’s not really an in+out setup. Rules may run at lots of different times - As a result of an action the player inititaes, as a result of being part of the huge machine of Standard Rules that are run through every turn. Or you may specifically call the rule from another action.

But per se, you don’t have to insert a value into a rule. You don’t even have to make a rulebook. Most rules you make will fall into already-existant rulebooks for actions. All you do generally is set some kind of condition before you go to the applicable rule. You might set a variable, or you might use Inform’s English language techniques (you might set Paul’s fatigue to ‘extreme’ before considering the ‘hunger rule’ - which you made.) In the end, rules are just blobs of code that are placed in order by rulebooks. And they always succeed, fail or produce no outcome, no matter what else you do in them. But I was significantly into my first Inform game before I even started to think about the repercussions of success/failure/no outcome, and that turned out not to be too big a deal.

Thanks for the comments.

So let’s take this is a a real simple case. I wanted a rule that takes in a numeric value. The rule will do something with that value – some calculation – and then return another value. Input --> Action --> Output.

So if I pass in a number (from the command line), the rule will calculate MyPassedInValue + 10 / 2, and then return the result.

I realize this is not a game context action necessarily but I suppose I could think of it in terms of a rule that calculates damage points based on an action taken. I’m just trying to understand if I can treat a rule exactly like a function. I feel like I can but the natural language gloss is perhaps hampering me a bit and I’m trying to start with a really simple example: almost the equivalent of a calculator.

One way to do that:

[code]Carry out calculating:
let N be the number understood transformed;
say “[N].”

To decide what number is (N - a number) transformed:
decide on (N + 10) / 2.[/code]

–Erik

Another way, if you need it to be a rule and not a phrase,[code]There is a room.

The calculating from rulebook is a number based rulebook producing a number.
Calculating from a number (called N) (this is the calculating rule):
rule succeeds with result ( N + 10 ) / 2.

When play begins:
showme the number produced by the calculating rule for 12.[/code]
Usually you would only use rules instead of phrases when you want to have a line of rules that can either settle on an outcome or fall back on those that remain:[code]There is a room.

The calculating from rulebook is a number based rulebook producing a number.
Calculating from a number (called N):
if N is even and the player has been in the room for at least one turn, rule succeeds with result 193;
make no decision.
Calculating from a number (called N):
rule succeeds with result ( N + 10 ) / 2.

Every turn:
showme the number produced by the calculating from rulebook for 11;
showme the number produced by the calculating from rulebook for 12;
showme the number produced by the calculating from rulebook for 13.

Test me with “z / z”.[/code]

I often want to use a rulebook producing a value so I can put it into an extension where the behavior can be easily modified. But so far, I’ve chickened out on doing anything complicated this way - it doesn’t seem like there’s much of an Informish convention for it. Plus, I think there are some bugs involving rulebooks that take a number or a kind of value as an input.

At the risk of derailing my own thread, you bring up a really good point. What are the conventions of Inform? I do see many responses here that say this is the “Inform way” to do it and that’s cool because I’m a Ruby programmer and as many know, there are most definitely aspects of the “Ruby way” of doing things.

But what are those things for Inform? Once in awhile I pick up what might be such in the Inform manual but they are so scattered it’s hard to draw a cohesive picture.

I think to some extent that is still being discovered.

Fair enough. But for people to say something does or does not match Inform conventions (-- and I’ve seen that said quite a bit --) implies that such conventions do exist. Any language is developed with an eye towards encoding the expression of logic via certain mechanisms that promote stylistic design choices. I’m not even talking “Gang of Four” design patterns here necessarily but Inform’s language design clearly places a heavy emphasis on rules and activities and various ways of tying things together. So there had to have been some operative principles in its design. Even if things are still being discovered (which makes sense in an evolving language), is there a referenceable place where those discoveries are being recorded even if they’re only provisional?

I think this is it. I think its easier for forum members to state their views on specific conventions than for the conventions to be categorized and organized at this point.

Have you seen Zarf’s notes on rule-based programming? There’s more there than last time I looked…

eblong.com/zarf/rule-language.html

Well, in that case they’re not conventions except in the individual sense. Which may be fine … but it’s a bit hard to search through these forums to get that kind of information unless you happen upon it. There’s a difference between categorizing/organizing and just having a single point of (provisional) reference. (Maybe a Wiki format?) I’m not worried about the structural aspects of the knowledge now; rather just getting the knowledge itself.

The fact that Inform is evolving/growing is the best time to start gathering that kind of information because then the language designers stand a much better chance of coding the use of certain design patterns into the language. (As they [correctly] say, the best patterns are the ones that the language so thoroughly absorbs that they aren’t something you necessarily apply, but rather just part of the normal operations.)

I’m not well-versed enough yet in Inform to lead this sort of effort but I’d be happy to contribute to discussions.

Rulebooks that return a value are pretty much brand-new to Inform. I don’t think I’ve seen anyone use them except to illustrate the fact that they exist, and there are no examples in the manual that present them either. So it’s not really a surprise that they don’t feel Informy. Moreover, these new “functional programming” features will probably always be the province of extension authors and “expert” users, and so will likely remain in a kind of penumbral zone as far as most users are concerned.

To be honest I don’t see what kicks are to be got by cataloging and organizing individuals’ ideas on the conventions for using the language, but if there are folks out there for whom this would be a good time, more power to them!

–Erik