You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: courses/RascalAmendmentProposals/RAP15/RAP15.md
+11-3Lines changed: 11 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,14 +54,16 @@ Static semantics:
54
54
55
55
* The pattern may bind variables that are used in the condition
56
56
* 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)`
58
58
59
59
Dynamic semantics:
60
60
61
61
* 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.
62
62
* 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.
65
67
* Otherwise the pattern succeeds and continues with the bindings introduced by the pattern side (and drops the new bindings introduced by the condition side).
66
68
67
69
Test semantics
@@ -110,6 +112,12 @@ bool evenOdd(int E if E % 2 == 0,
110
112
111
113
default bool evenOdd(int _, int _) = false;
112
114
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
+
113
121
// remove duplicates with list matching
114
122
Bool and([*Bool x, Bool a, *Bool y, _ == a, *Bool z])
0 commit comments