Logic
Booleans and conditional logic. Logic gives you reactive and, or and
not, plus conditional blocks that run only when a condition holds. The results are
ordinary reactive values, so a condition stays live — it re-evaluates whenever the values it
depends on change.
Contents
Types
Functions
Types
Logic.Maybe
Program's boolean. It is not built in: it is simply this tagged union of yes and no.
Options
| Option | Type | Description |
|---|---|---|
yes | Logic.Yes | The affirmative case. |
no | Logic.No | The negative case. |
Definition
TYPE Logic.Maybe
OPTION Logic.Yes yes
OPTION Logic.No noLogic.Maybe can be either yes or no.Example
STATE Logic.Maybe ready = Logic.YesLogic.Maybe initialised to yes.Logic.No
An empty record, the negative case of Logic.Maybe.
Definition
TYPE Logic.NoExample
LET Logic.Maybe negative = Logic.NoLogic.Maybe in its negative case.Logic.Yes
An empty record, the affirmative case of Logic.Maybe.
Definition
TYPE Logic.YesExample
LET Logic.Maybe affirmative = Logic.YesLogic.Maybe in its affirmative case.Functions
Logic.And
Yields yes when both inputs are yes.
Parameters
| Name | Type | Description |
|---|---|---|
a | Logic.Maybe | First operand. |
b | Logic.Maybe | Second operand. |
Returns
Returns Logic.Maybe — yes only when both a and b are yes.
Example
STATE Logic.Maybe ready = Logic.Yes
STATE Logic.Maybe willing = Logic.No
LET Logic.Maybe go = Logic.And ready willingready with a no willing, so go is no — both must be yes.Logic.If
Runs a block when a condition is yes.
Parameters
| Name | Type | Description |
|---|---|---|
condition | Logic.Maybe | When yes, the block runs. |
onYes | block | The block to run. |
Example
STATE Logic.Maybe agreed = Logic.Yes
Logic.If agreed
Ui.Content.Text "Thanks!"agreed is yes.Logic.IfNot
Runs a block when a condition is no.
Parameters
| Name | Type | Description |
|---|---|---|
condition | Logic.Maybe | When no, the block runs. |
onNo | block | The block to run. |
Example
STATE Logic.Maybe loaded = Logic.No
Logic.IfNot loaded
Ui.Content.Text "Loading"loaded is no.Logic.Not
The negation of a value.
Parameters
| Name | Type | Description |
|---|---|---|
condition | Logic.Maybe | The value to negate. |
Returns
Returns Logic.Maybe — no when condition is yes, and yes when it is no.
Example
STATE Logic.Maybe loading = Logic.Yes
LET Logic.Maybe ready = Logic.Not loadingloading, so ready is no.Logic.Or
Yields yes when either input is yes.
Parameters
| Name | Type | Description |
|---|---|---|
a | Logic.Maybe | First operand. |
b | Logic.Maybe | Second operand. |
Returns
Returns Logic.Maybe — yes when a or b (or both) are yes.
Example
STATE Logic.Maybe admin = Logic.No
STATE Logic.Maybe owner = Logic.Yes
LET Logic.Maybe canEdit = Logic.Or admin owneradmin or an owner may edit, so canEdit is yes because owner is yes.Logic.Select
Runs the first block when yes, otherwise the second.
Parameters
| Name | Type | Description |
|---|---|---|
condition | Logic.Maybe | Chooses which hole runs. |
Holes
| Name | Returns | Description |
|---|---|---|
onYes | nothing | Run when the condition is yes. |
onNo | nothing | Run when the condition is no. |
Example
STATE Logic.Maybe online = Logic.Yes
Logic.Select online
Logic.Select.Yes
Ui.Content.Text "Online"
Logic.Select.No
Ui.Content.Text "Offline""Online" when online is yes, and "Offline" when it is no.Logic.SelectFunction
Picks one of two functions by a condition and returns the value it produces.
Generics
| Name | Description |
|---|---|
ValueType | The type of value each function returns, and of the result. |
Parameters
| Name | Type | Description |
|---|---|---|
condition | Logic.Maybe | Chooses which hole is called. |
Holes
| Name | Returns | Description |
|---|---|---|
yesFunction | ValueType | Called, and its result returned, when the condition is yes. |
noFunction | ValueType | Called, and its result returned, when the condition is no. |
Returns
Returns ValueType — the value produced by whichever function the condition selects.
Example
STATE Logic.Maybe cached = Logic.No
LET Text label = Logic.SelectFunction cached ReadCachedLabel BuildFreshLabelcached is no, it calls BuildFreshLabel and returns its Text — ReadCachedLabel is never run.Logic.SelectValue
Picks one of two values from a condition.
Generics
| Name | Description |
|---|---|
ValueType | The type of the two values being chosen between, and of the result. |
Parameters
| Name | Type | Description |
|---|---|---|
condition | Logic.Maybe | Which value to choose. |
yesValue | ValueType | Returned when the condition is yes. |
noValue | ValueType | Returned when the condition is no. |
Returns
Returns ValueType — yesValue or noValue, matching the condition.
Example
STATE Logic.Maybe dark = Logic.Yes
LET Text theme = Logic.SelectValue dark "midnight" "daylight""midnight" when dark is yes, and "daylight" when it is no.