After introducing the if-then-else operator, a switch-case construct would be quite useful as well.
Syntax:
switch s0
case s1 : s1’
case s2 : s2’
// …
otherwise : s’
end

Semantics:
s0 will be applied to the current term from right before the switch. Its result will be the input term for each case-condition strategy si.
The body strategies of the case branches, si’, will be applied to the current term from right before the switch.
The case branches will be applied in their normal order until a case condition succeeds. If a body strategy of a selected case branch fails, no backtracking to other (next) branches occurs, the entire switch then fails. Other branches will only be selected if all previous case-condition strategies failed.
The optional default branch can only occur at the end and directly applies its body strategy s’.

Desugaring:
?t
; s0 => t0
; (where( t0)
< <s1’> t
+ (where( t0)
< <s2’> t
+
// …
+ <s’> t // or fail if no default branch
)
)
)

Open Problem:
Any bindings resulting from previously tried case-condition-strategies s1..si-1 should be unbound before trying si. This has to do with the general problem that no unbinding occurs on backtracking (STR-9).
The same holds for undefining any generated dynamic rules.

Submitted on 27 April 2004 at 09:50

On 28 April 2004 at 13:03 Jira commented:

STR-68, adam:
All branch conditions should apply to the original selector (thus use a BuildApp)

Log in to post comments