R/MRatCalcpowered by RiX

RiX walkthrough 5 c

Multifunctions

Prepared variants, dispatch, and recursion.

Orientation

A multifunction holds ordered variants. Preparation clauses decide whether a variant accepts an input; $ refers to the active variant and $$ to the parent multifunction.

Prerequisites: Define and call and the first two sections of Ternaries and cases. A prepared guard chooses which implementation applies; use an ordinary ternary when one function is simply choosing a result.

Run the simple absolute-value variants first, then compare how later guards accept, reject, or leave a case undecided.

A worked example

Runnable RiX

The final line is the displayed value; the earlier lines set up the experiment. Keep the setup visible so you can tell whether a name, a cell, or a collection is being reused when the expression changes.

Read the result

Use a soft prep clause to skip a variant and a strict one when failure should stop evaluation.

Explicit and inline multifunctions

{> ... } creates a multifunction value directly. This makes quick pipe dispatch readable without first choosing a name:

Runnable RiX

The result is [3, 0, 16]. Variants are tried in order. A decisively false soft prep falls through; an undecided prep does not.

Undecided guards block unsafe fallthrough

The three possible guard results have different dispatch meanings:

  • true selects the variant and evaluates its body;
  • false or null rejects a soft variant and tries the next one;
  • undecided ? returns ? from the multifunction without evaluating the body

or any later variant.

Runnable RiX

The last result is ?. The enclosure represented by 0.5? contains values on both sides of 0.55, so the first variant might apply. Skipping it would make selecting AtOrAbove or Fallback unsound. Strict ?!- still propagates undecided as ?; strictness turns decisive prep failure or an error into an error, not lack of evidence into one.

When a later variant is explicitly prepared to handle the ambiguous region, use ??- to make undecided a no-match:

Runnable RiX

Use ??!- when dispatch may continue only after the guard becomes decidable. An undecided result then throws, as do decisive prep failures and prep errors. Reason-bearing undecided values still display as ?; their external reason property and trace data distinguish halo overlap, resolution, and budget exhaustion for diagnostics.

Handle undecided explicitly

When uncertainty is part of the intended result, move the comparison into ordinary decision control and give it an explicit ?? branch:

Runnable RiX

This makes the policy visible: ordered guard dispatch remains conservative, while the function body chooses how an unresolved comparison should be reported.

Entries can also be existing functions or whole multifunctions. Whole multifunctions flatten in the order written:

Runnable RiX

A named variant is an ordinary function value. Select it with F[:Name], call it directly, pass it to a pipe, or insert it into another multifunction:

Runnable RiX

Change the input from negative to positive and check which variant runs before exploring the advanced guard forms.

Keep going

Continue with Rule dispatcher for a complete policy task.