Booleans

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.

Construction

Booleans have 2 inhabitants, they are:

true  :: Boolean
false :: Boolean

Conversion

The following constructor converts Haskell Booleans to Keelung Booleans:

Boolean :: Bool -> Boolean

true and false are made this way under the hood:

true :: Boolean
true = Boolean True 

false :: Boolean
false = Boolean False

From Keelung unsigned integers

The !!! operator allows you to inspect the bit value at a specific position in an unsigned integer.

To Keelung field elements

BtoF converts to Booleans to field elements:

BtoU converts to Booleans to field elements:

Functions

Conditionals

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.

Conjunction

Example usage: x .&. true

Disjunction

Example usage: false .|. x

Exclusive disjunction

Example usage: false .^. x

Complement

Example usage: complement x

Equality

Returns true when two field elements are the same:

Example usage: false `eq` false or eq false true.

You can also use EqB , which is the underlying implementation of eq on Booleans.

circle-info

Binary function such as eq can be treated like an infix operator by surrounding it with a pair of backticks `eq`

Inequality

Returns false when two Booleans are the same:

neq is implemented as such under the hood:

Last updated