Although you've been using various of the ?
wut runes for a while now, let's wrap up some loose ends. This module will cover the nature of loobean logic and the rest of the ?
wut runes.
Loobean Logic
Throughout Hoon School, you have been using %.y
and %.n
, often implicitly, every time you have asked a question like ?: =(5 4)
. The =()
expression returns a loobean, a member of the type union ?(%.y %.n)
. (There is a proper aura @f
but unfortunately it can't be used outside of the compiler.) These can also be written as &
(%.y
, true) and |
(%.n
, false), which is common in older code but should be avoided for clarity in your own compositions.
What are the actual values of these, sans formatting?
> `@`%.y0> `@`%.n1
Pretty much all conditional operators rely on loobeans, although it is very uncommon for you to need to unpack them.
Noun Equality
The most fundamental comparison in Hoon is provided by .=
dottis, a test for equality of two nouns using Nock 5. This is almost always used in its irregular form of =
tis.
> =(0 0)%.y> =('a' 'b')%.n
Since Nock is unaware of the Hoon metadata type system, only bare atoms in the nouns are compared. If you need to compare include type information, create vases with !>
zapgar.
> =('a' 97)%.y> =(!>('a') !>(97))%.n
Making Choices
You are familiar in everyday life with making choices on the basis of a decision expression. For instance, you can compare two prices for similar products and select the cheaper one for purchase.
Essentially, we have to be able to decide whether or not some value or expression evaluates as %.y
true (in which case we will do one thing) or %.n
false (in which case we do another). Some basic expressions are mathematical, but we also check for existence, for equality of two values, etc.
- ++gth (greater than
>
) - ++lth (less than
<
) - ++gte (greater than or equal to
≥
) - ++lte (less than or equal to
≤
) .=
dottis, irregularly=()
(check for equality)
The key conditional decision-making rune is ?:
wutcol, which lets you branch between an expression-if-true
and an expression-if-false
. ?.
wutdot inverts the order of ?:
. Good Hoon style prescribes that the heavier branch of a logical expression should be lower in the file.
There are also two long-form decision-making runes, which we will call switch statements by analogy with languages like C.
?-
wuthep lets you choose between several possibilities, as with a type union. Every case must be handled and no case can be unreachable.Since
@tas
terms are constants first, and not@tas
unless marked as such,?-
wuthep switches over term unions can make it look like the expression is branching on the value. It's actually branching on the type. These are almost exclusively used with term type unions.|= p=?(%1 %2 %3)?- p%1 1%2 2%3 3==?+
wutlus is similar to?-
but allows a default value in case no branch is taken. Otherwise these are similar to?-
wuthep switch statements.|= p=?(%0 %1 %2 %3 %4)?+ p 0%1 1%2 2%3 3==
Logical Operators
Mathematical logic allows the collocation of propositions to determine other propositions. In computer science, we use this functionality to determine which part of an expression is evaluated. We can combine logical statements pairwise:
?&
wutpam, irregularly&()
, is a logicalAND
(i.e. p ∧ q) over loobean values, e.g. both terms must be true.AND
%.y
%.n
%.y
%.y
%.n
%.n
%.n
%.n
> =/ a 5&((gth a 4) (lth a 7))%.y?|
wutbar, irregularly|()
, is a logicalOR
(i.e. p ∨ q) over loobean values, e.g. either term may be true.OR
%.y
%.n
%.y
%.y
%.y
%.n
%.y
%.n
> =/ a 5|((gth a 4) (lth a 7))%.y?!
wutzap, irregularly!
, is a logicalNOT
(i.e. ¬p). Sometimes it can be difficult to parse code including!
because it operates without parentheses.NOT
%.y
%.n
%.n
%.y
> !%.y%.n> !%.n%.y
From these primitive operators, you can build other logical statements at need.
Exercise: Design an XOR
Function
The logical operation XOR
(i.e. p⊕q ; exclusive disjunction) yields true if one but not both operands are true. XOR
can be calculated by (p ∧ ¬q) ∨ (¬p ∧ q).
XOR | %.y | %.n |
---|---|---|
%.y | %.n | %.y |
%.n | %.y | %.n |
Implement
XOR
as a gate in Hoon.|= [p=?(%.y %.n) q=?(%.y %.n)]^- ?(%.y %.n)|(&(p !q) &(!p q))
Exercise: Design a NAND
Function
The logical operation NAND
(i.e. p ↑ q) produces false if both operands are true. NAND
can be calculated by ¬(p ∧ q).
NAND | %.y | %.n |
---|---|---|
%.y | %.n | %.y |
%.n | %.y | %.y |
- Implement
NAND
as a gate in Hoon.
Exercise: Design a NOR
Function
The logical operation NOR
(i.e. p ↓ q) produces true if both operands are false. NOR
can be calculated by ¬(p ∨ q).
NOR | %.y | %.n |
---|---|---|
%.y | %.n | %.n |
%.n | %.n | %.y |
- Implement
NAND
as a gate in Hoon.
Exercise: Implement a Piecewise Boxcar Function
The boxcar function is a piecewise mathematical function which is equal to zero for inputs less than zero and one for inputs greater than or equal to zero. We implemented the similar Heaviside function previously using the ?:
wutcol rune.
Compose a gate which implements the boxcar function,
Use Hoon logical operators to compress the logic into a single statement using at least one
AND
orOR
operation.