nat-0.1: Lazy binary natural numbers

Data.Number.Nat

Contents

Description

The implementations of all functions are supposed to be as non-strict as possible. This is non-trivial as for example a naive implementation of (*) yields O _|_ for the application I _|_ * O IHi while a least-strict version yields O (I _|_). Also the naive / standard implementations of (-), compare, (<), (<=), (>), (>=) are more strict than necessary.

Synopsis

Datatype

data Nat Source

A binary representation of natural numbers which starts with the least significant bit.

Constructors

IHi

This constructor represents the most significant bit. There are no leading zero bits.

O Nat

A zero bit

I Nat

A one bit

Instances

Helper Functions

cmpNatLT :: Nat -> Nat -> OrderingSource

This function is used to implement lazy instances of compare and (<), (<=), (>), (>=). It is used to transfer information to more significant bits. Instead of yielding EQ it yields LT if the numbers are equal.

invOrd :: Ordering -> OrderingSource

Maps LT to GT and GT to LT. It is used instead of defining a function cmpNatGT.

minusNat :: Nat -> Nat -> NatSource

minusNat x y yields x - y + 1. This is used to implement (-) for natural numbers.

fromNat :: Num n => Nat -> nSource

This is used for the implementation of toInteger and fromEnum.

toNat :: (Integral n, Num n) => n -> NatSource

This is used for the implementation of fromInteger and toEnum.