Booleans
Last updated
Last updated
Booleans are a fundamental data type in Keelung that are primarily used to construct logical and conditional expressions, although they are essentially just field elements in disguise.
Unlike numeric values that come with a user-friendly interface, Boolean values and logical operators are constructed using specific constructors and functions listed below.
Booleans have 2 inhabitants, they are:
The following constructor converts Haskell Booleans to Keelung Booleans:
true
and false
are made this way under the hood:
The !!!
operator allows you to inspect the bit value at a specific position in an unsigned integer.
BtoF
converts to Booleans to field elements:
BtoU
converts to Booleans to field elements:
Unlike in most imperative languages, conditionals are expressions rather than just statements. Both branches have to be present and must be of the same type. That means you cannot omit the else ...
branch like in C.
Here's a simple calculator has only 2 operations: addition and multiplication. It takes a Boolean operation flag and 2 inputs, and returns the result.
Example usage: x .&. true
Example usage: false .|. x
Example usage: false .^. x
Example usage: complement x
Example usage: false `eq` false
or eq false true
.
You can also use EqB
, which is the underlying implementation of eq
on Booleans.
neq
is implemented as such under the hood:
Returns when two field elements are the same:
Returns when two Booleans are the same: