Modules · Core · Logic

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

OptionTypeDescription
yesLogic.YesThe affirmative case.
noLogic.NoThe negative case.

Definition

TYPE Logic.Maybe
  OPTION Logic.Yes yes
  OPTION Logic.No no
Logic.Maybe can be either yes or no.

Example

STATE Logic.Maybe ready = Logic.Yes
A reactive Logic.Maybe initialised to yes.

Logic.No

An empty record, the negative case of Logic.Maybe.

Definition

TYPE Logic.No

Example

LET Logic.Maybe negative = Logic.No
Constructs a Logic.Maybe in its negative case.

Logic.Yes

An empty record, the affirmative case of Logic.Maybe.

Definition

TYPE Logic.Yes

Example

LET Logic.Maybe affirmative = Logic.Yes
Constructs a Logic.Maybe in its affirmative case.

Functions

Logic.And

Yields yes when both inputs are yes.

Parameters

NameTypeDescription
aLogic.MaybeFirst operand.
bLogic.MaybeSecond 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 willing
Combines a yes ready with a no willing, so go is no — both must be yes.

Logic.If

Runs a block when a condition is yes.

Parameters

NameTypeDescription
conditionLogic.MaybeWhen yes, the block runs.
onYesblockThe block to run.

Example

STATE Logic.Maybe agreed = Logic.Yes
Logic.If agreed
  Ui.Content.Text "Thanks!"
Shows the thank-you text only while agreed is yes.

Logic.IfNot

Runs a block when a condition is no.

Parameters

NameTypeDescription
conditionLogic.MaybeWhen no, the block runs.
onNoblockThe block to run.

Example

STATE Logic.Maybe loaded = Logic.No
Logic.IfNot loaded
  Ui.Content.Text "Loading"
Shows the loading text only while loaded is no.

Logic.Not

The negation of a value.

Parameters

NameTypeDescription
conditionLogic.MaybeThe 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 loading
Negates a yes loading, so ready is no.

Logic.Or

Yields yes when either input is yes.

Parameters

NameTypeDescription
aLogic.MaybeFirst operand.
bLogic.MaybeSecond 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 owner
An admin 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

NameTypeDescription
conditionLogic.MaybeChooses which hole runs.

Holes

NameReturnsDescription
onYesnothingRun when the condition is yes.
onNonothingRun 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"
Shows "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

NameDescription
ValueTypeThe type of value each function returns, and of the result.

Parameters

NameTypeDescription
conditionLogic.MaybeChooses which hole is called.

Holes

NameReturnsDescription
yesFunctionValueTypeCalled, and its result returned, when the condition is yes.
noFunctionValueTypeCalled, 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 BuildFreshLabel
Because cached is no, it calls BuildFreshLabel and returns its TextReadCachedLabel is never run.

Logic.SelectValue

Picks one of two values from a condition.

Generics

NameDescription
ValueTypeThe type of the two values being chosen between, and of the result.

Parameters

NameTypeDescription
conditionLogic.MaybeWhich value to choose.
yesValueValueTypeReturned when the condition is yes.
noValueValueTypeReturned 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"
Chooses "midnight" when dark is yes, and "daylight" when it is no.