lue-bird / elm-morph / Natural

Arbitrary-size, safe natural number ≥ 0. See also Natural.Morph


type Natural
    = N0
    | AtLeast1 AtLeast1

Whole number (integer) ≥ 0 of arbitrary precision. Either 0 directly or a positive number represented by the bit I followed by some Bits

Why does the type look like this?

If you need a natural number representation with a specific number of bits, you can try

ArraySized Bit (Exactly (On bitLength))

For larger numbers, where you want to allow numbers of arbitrary precision, only our version can enforce that == always gives the correct answer, since the ArraySized could be constructed with leading Os!


type alias AtLeast1 =
RecordWithoutConstructorFunction { bitsAfterI : List Bit }

Positive natural number, can be arbitrarily large

create

fromN : N range_ -> Natural

Convert from a natural number of type N

transform

toN : Natural -> N (N.Min (N.Up0 minX_))

Convert to a natural number of type N

Keep in mind that this can overflow since N is fixed in bit size just like Int while Natural is not.