Field elements
Field element is the most fundamental datatype in Keelung.
They can be constructed with numeric literals like 3
, 42
, or -7
.
Negative numbers don't really exist in finite fields, they will be normalized to their positive counterparts after compilation. For example, -7
would become:
in GF181 where there are only 1552511030102430251236801561344621993261920897571225601
numbers.
Construction
Field elements are constructed with the following constructor under the hood:
Conversion
From Haskell Integer
Integer
A numeric literal represents the application of the function fromInteger
to the appropriate value of type Integer
.
From Haskell Rational
Rational
A floating literal stands for an application of fromRational
to a value of type Rational
.
Example usage: fromRational 3.5
From Keelung Booleans
BtoF
converts to Booleans to field elements:
Functions
Since Field
is an instance of Num
and Fractional
, all methods of Num
and Fractional
works on Numbers.
Addition
Example usage: 3 + 4
Subtraction
Example usage: 3 - 4
Multiplication
Example usage: 3 * 4
Fractional division (binary)
Example usage: 3 / 4
Reciprocal division (unary)
Example usage: recip 4
Unary negation
Example usage: negate 4
Absolute value
Since there are no negative numbers in Field
, the abs
function has the same effect as the id
function.
Sign of number
signum
would always return 1
on any field element.
Exponentiation
pow
takes a Field
as the base and an Integer
as the power, and computes the exponentiation.
If you are multiplying the same stuff for more than 3 times, for example:
You should consider replacing it with:
To allow the exponentiation by squaring optimization to kick in.
Equality
Returns true
when two field elements are the same:
Example usage: false `eq` false
or eq false true
.
You can also use EqF
, which is the underlying implementation of eq
on field elements.
Inequality
Returns false
when two numbers are the same:
neq
is implemented as such under the hood:
Last updated