-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Type-level (low cardinality) integers, implemented using type families. -- -- This package provides unary type level representations of the -- (positive and negative) integers and basic operations (addition, -- subtraction, multiplication, division) on these. Due to the unary -- implementation the practical size of the NumTypes is severely limited -- making them unsuitable for large-cardinality applications. If you will -- be working with integers beyond (-20, 20) this package probably isn't -- for you. The numtype-tf packade differs from the numtype package in -- that the NumTypes are implemented using type families rather than -- functional dependencies. Requires GHC 7.0 or later. @package numtype-tf @version 0.1 -- | This Module provides unary type-level representations, hereafter -- referred to as NumTypes, of the (positive and negative) -- integers and basic operations (addition, subtraction, multiplication, -- division) on these. While functions are provided for the operations -- NumTypes exist solely at the type level and their only value is -- undefined. -- -- There are similarities with the HNats of the HList library, which was -- indeed a source of inspiration. Occasionally references are made to -- the HNats. The main addition in this module is negative numbers. -- -- The practical size of the NumTypes is limited by the type -- checker stack. If the NumTypes grow too large (which can happen -- quickly with multiplication) an error message similar to the following -- will be emitted: -- --
-- Context reduction stack overflow; size = 20 -- Use -fcontext-stack=N to increase stack size to N ---- -- This situation could concievably be mitigated significantly by using -- e.g. a binary representation of integers rather than Peano numbers. -- -- Please refer to the literate Haskell code for a narrative of the -- implementation. module Numeric.NumType.TF -- | Class encompassing all valid type level integers. class NumTypeI n => NumType n -- | Type level zero. data Z -- | Successor type for building type level natural numbers. data S n -- | Negation type, used to represent negative numbers by negating type -- level naturals. data N n -- | Addition (a + b). -- | Subtraction (a - b). -- | Division (a / b). -- | Multiplication (a * b). type Zero = Z type Pos1 = S Z type Pos2 = S Pos1 type Pos3 = S Pos2 type Pos4 = S Pos3 type Pos5 = S Pos4 type Neg1 = N Pos1 type Neg2 = N Pos2 type Neg3 = N Pos3 type Neg4 = N Pos4 type Neg5 = N Pos5 -- | Convert a type level integer to an instance of Num. toNum :: (NumTypeI n, Num a) => n -> a -- | Increment a NumType by one. incr :: NumType a => a -> Succ a -- | Decrement a NumType by one. decr :: NumType a => a -> Pred a -- | Negate a NumType. negate :: NumType a => a -> Negate a -- | Add two NumTypes. (+) :: (NumType a, NumType b) => a -> b -> Add a b -- | Subtract the second NumType from the first. (-) :: (NumType a, NumType b) => a -> b -> Sub a b -- | Multiply two NumTypes. (*) :: (NumType a, NumType b) => a -> b -> Mul a b -- | Divide the first NumType by the second. (/) :: (NumType a, NumType b) => a -> b -> Div a b zero :: Z pos1 :: Pos1 pos2 :: Pos2 pos3 :: Pos3 pos4 :: Pos4 pos5 :: Pos5 neg1 :: Neg1 neg2 :: Neg2 neg3 :: Neg3 neg4 :: Neg4 neg5 :: Neg5 instance NumTypeI (N n) => Show (N n) instance NumTypeI (S n) => Show (S n) instance Show Z instance NumTypeI (S n) => NumTypeI (N (S n)) instance NumTypeI (S n) => NumTypeI (S (S n)) instance NumTypeI (S Z) instance NumTypeI Z instance NumTypeI n => NumType n