Skip to content

Commit b93026f

Browse files
authored
Update condition type and dynamic semantics in RAP15.md
Clarify condition type requirements and enhance dynamic semantics explanation.
1 parent 110f656 commit b93026f

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

  • courses/RascalAmendmentProposals/RAP15

courses/RascalAmendmentProposals/RAP15/RAP15.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ Static semantics:
5454

5555
* The pattern may bind variables that are used in the condition
5656
* The condition may bind variables *inside* of it, but does not leak new variables beyond the current pattern.
57-
* The condition must be of type `bool`
57+
* The condition must be of type `bool` or the function type `bool(value)`
5858

5959
Dynamic semantics:
6060

6161
* First the pattern is matched against the current subject; with optional bindings as a side-effect. Note that if the pattern is not singular, backtracking may occur later.
6262
* Then a new backtracking and binding scope is wrapped around the evaluation of the condition (such that complex back-tracking conditions can introduce variables and be cleaned up nicely)
63-
* The condition finds the first way to evaluate to true (includes possible backtracking over the original pattern, but certainly also over non-singular parameters of `&&` and `||`. The semantics is comparable to the sematics of a `when` clause.
64-
* If there is no way to evaluate the condition to true, the entire pattern fails.
63+
* The condition finds the first way to evaluate to true (includes possible backtracking over the original pattern, but certainly also over non-singular parameters of `&&` and `||`. The semantics is comparable to the sematics of a `when` clause.
64+
* if the condition is of type `bool` we simply evaluate it
65+
* if the condition is of type `bool(value)` we evaluate the expression as `condition(p)`
66+
* If there is no way to evaluate the condition to true, the entire pattern fails. if `condition(p)` fails (CallFailed) then we also fail the condition.
6567
* Otherwise the pattern succeeds and continues with the bindings introduced by the pattern side (and drops the new bindings introduced by the condition side).
6668

6769
Test semantics
@@ -110,6 +112,12 @@ bool evenOdd(int E if E % 2 == 0,
110112
111113
default bool evenOdd(int _, int _) = false;
112114
115+
// shorter and sweeter:
116+
bool isOdd(int i) = i %% 2 == 1;
117+
bool isEven(int i) = i %% 2 == 0;
118+
119+
bool evenOdd(int E if isEven, int O if isOdd) = true;
120+
113121
// remove duplicates with list matching
114122
Bool and([*Bool x, Bool a, *Bool y, _ == a, *Bool z])
115123
= and([*x, a, *y])

0 commit comments

Comments
 (0)