2021-11-17 13:41:03 +03:00
|
|
|
.. _builtins:
|
|
|
|
|
2021-04-22 15:08:32 +03:00
|
|
|
********
|
|
|
|
Builtins
|
|
|
|
********
|
|
|
|
|
|
|
|
.. role:: idris(code)
|
|
|
|
:language: idris
|
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Natural numbers
|
|
|
|
===============
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Idris2 supports an optimized runtime representation of natural numbers (non-negative integers).
|
|
|
|
This optimization is automatic,
|
|
|
|
however it only works when natural numbers are represented in a specific way
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Here is an example of a natural number that would be optimized:
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
.. code-block:: idris
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
data Natural
|
|
|
|
= Zero
|
|
|
|
| Succ Natural
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Natural numbers are generally represented as either zero or the successor (1 more than)
|
|
|
|
of another natural number. These are called Peano numbers.
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
At runtime, Idris2 will automatically represent this the same as the ``Integer`` type.
|
|
|
|
This will massively reduce the memory usage.
|
2021-08-11 13:31:43 +03:00
|
|
|
|
2023-05-14 14:06:29 +03:00
|
|
|
There are a few rules governing when this optimization occurs:
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
- The data type must have 2 constructors
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
- After erasure of runtime irrelevant arguments
|
|
|
|
+ One must have no arguments
|
|
|
|
+ One must have exactly 1 argument (called ``Succ``)
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
- The type of the argument to ``Succ`` must have the same type constructor as the parent type.
|
|
|
|
This means indexed data types, like ``Fin``, can be optimised.
|
|
|
|
- The argument to ``Succ`` must be strict, ie not ``Lazy Natural``
|
2021-08-11 13:31:43 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
To ensure that a type is optimized to an ``Integer``, use ``%builtin Natural`` ie
|
2021-08-11 13:31:43 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
.. code-block:: idris
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
data MyNat
|
|
|
|
= Succ MyNat
|
|
|
|
| Zero
|
2023-12-07 01:20:21 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
%builtin Natural MyNat
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Casting between natural numbers and integer
|
|
|
|
===========================================
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Idris optimizes functions which convert between natural numbers and integers,
|
|
|
|
so that it takes constant time rather than linear time.
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Such functions must be written in a specific way,
|
|
|
|
so that idris can detect that it can be optimised.
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Here is an example of a natural to ``Integer`` function.
|
2021-04-22 15:08:32 +03:00
|
|
|
|
|
|
|
.. code-block:: idris
|
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
cast : Natural -> Integer
|
|
|
|
cast Z = 0
|
|
|
|
cast (S k) = cast k + 1
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
This optimization is applied late in the compilation process,
|
|
|
|
so it may be sensitive to seemingly insignificant changes.
|
2021-04-22 15:08:32 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
However here are roughly the rules governing this optimisation:
|
2021-05-10 14:14:19 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
- Exactly one argument must be pattern matched on
|
|
|
|
(any other forced or dotted patterns are allowed)
|
|
|
|
- The right hand side of the 'Zero' case must be ``0``
|
|
|
|
- The right hand side of the 'Succ' case must be ``1 + cast k``
|
|
|
|
where ``k`` is the predecessor of the pattern matched argument
|
2021-05-10 14:14:19 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
Casting from an ``Integer`` to a natural is a little more complex.
|
2021-05-10 14:14:19 +03:00
|
|
|
|
|
|
|
.. code-block:: idris
|
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
castNonNegative : Integer -> Natural
|
|
|
|
castNonNegative x = case x of
|
|
|
|
0 => Zero
|
|
|
|
_ => Succ $ castNonNegative (x - 1)
|
2021-05-10 14:14:19 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
cast : Integer -> Natural
|
|
|
|
cast x = if x < 0 then Zero else castNonNegative x
|
2021-05-13 20:44:24 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
For now you must manually check the given integer is non-negative.
|
2021-05-13 20:44:24 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
If you are using an indexed data type it may be very hard to write
|
|
|
|
your ``Integer`` to natural cast in such a way,
|
|
|
|
so you can use ``%builtin IntegerToNatural`` to assert to the compiler
|
|
|
|
that a function is correct. It is your responsibility to make sure this is correct.
|
2021-05-13 20:44:24 +03:00
|
|
|
|
|
|
|
.. code-block:: idris
|
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
module ComplexNat
|
2021-05-13 20:44:24 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
import Data.Maybe
|
2021-05-13 20:44:24 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
data ComplexNat
|
|
|
|
= Zero
|
|
|
|
| Succ ComplexNat
|
2023-12-07 01:20:21 +03:00
|
|
|
|
2022-04-22 16:52:02 +03:00
|
|
|
integerToMaybeNat : Integer -> Maybe ComplexNat
|
|
|
|
integerToMaybeNat _ = ...
|
|
|
|
|
|
|
|
integerToNat :
|
|
|
|
(x : Integer) ->
|
|
|
|
{auto 0 prf : IsJust (ComplexNat.integerToMaybeNat x)} ->
|
|
|
|
ComplexNat
|
|
|
|
integerToNat x {prf} = fromJust (integerToMaybeNat x) @{prf}
|
|
|
|
|
|
|
|
%builtin IntegerToNatural ComplexNat.integerToNat
|
|
|
|
|
|
|
|
Other operations
|
|
|
|
================
|
2021-05-10 14:14:19 +03:00
|
|
|
|
|
|
|
This can be used with ``%transform`` to allow many other operations to be O(1) too.
|
|
|
|
|
|
|
|
.. code-block:: idris
|
|
|
|
|
|
|
|
eqNat : Nat -> Nat -> Bool
|
|
|
|
eqNat Z Z = True
|
|
|
|
eqNat (S j) (S k) = eqNat j k
|
|
|
|
eqNat _ _ = False
|
|
|
|
|
|
|
|
%transform "eqNat" eqNat j k = natToInteger j == natToInteger k
|
|
|
|
|
2021-05-13 20:44:24 +03:00
|
|
|
plus : Nat -> Nat -> Nat
|
|
|
|
plus Z y = y
|
|
|
|
plus (S x) y = S $ plus x y
|
|
|
|
|
|
|
|
%transform "plus" plus j k = integerToNat (natToInteger j + natToInteger j)
|
2022-04-22 16:52:02 +03:00
|
|
|
|
|
|
|
Compilation
|
|
|
|
===========
|
|
|
|
|
|
|
|
Here are the details of how natural numbers are compiled to ``Integer`` s.
|
|
|
|
Note: a numeric literal here is an ``Integer``.
|
|
|
|
|
|
|
|
``Zero`` => ``0``
|
|
|
|
|
|
|
|
``Succ k`` => ``1 + k``
|
|
|
|
|
|
|
|
.. code-block:: idris
|
|
|
|
|
|
|
|
case k of
|
|
|
|
Z => zexp
|
|
|
|
S k' => sexp
|
|
|
|
|
|
|
|
=>
|
|
|
|
|
|
|
|
.. code-block:: idris
|
|
|
|
|
|
|
|
case k of
|
|
|
|
0 => zexp
|
|
|
|
_ => let k' = k - 1 in sexp
|
|
|
|
|