HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulenatural-arithmetic-0.2.1.0Haskell2010

Arithmetic.Fin

  • 37 values

Modification

8 declarations
valueincrementL :: Nat m -> Fin n -> Fin (m + n)
#

Raise the index by m and weaken the bound by m, adding m to the left-hand side of n.

valueincrementR :: Nat m -> Fin n -> Fin (n + m)
#

Raise the index by m and weaken the bound by m, adding m to the right-hand side of n.

valueweaken :: n <= m -> Fin n -> Fin m
#

Weaken the bound, replacing it by another number greater than or equal to itself. This does not change the index.

valueweakenL :: Fin n -> Fin (m + n)
#

Weaken the bound by m, adding it to the left-hand side of the existing bound. This does not change the index.

valueweakenR :: Fin n -> Fin (n + m)
#

Weaken the bound by m, adding it to the right-hand side of the existing bound. This does not change the index.

valuesucc :: Nat n -> Fin n -> Maybe (Fin n)
#

Return the successor of the Fin or return nothing if the argument is the greatest inhabitant.

Traverse

17 declarations

These use the terms ascend and descend rather than the more popular l (left) and r (right) that pervade the Haskell ecosystem. The general rule is that ascending functions pair the initial accumulator with zero with descending functions pair the initial accumulator with the last index.

valueascend :: Nat n -> a -> (Fin n -> a -> a) -> a
#

Fold over the numbers bounded by n in ascending order. This is lazy in the accumulator.

ascend 4 z f = f 3 (f 2 (f 1 (f 0 z)))
valueascend'
  1. :: Nat n

    Upper bound

  2. -> a

    Initial accumulator

  3. -> (Fin n -> a -> a)

    Update accumulator

  4. -> a
#

Strict fold over the numbers bounded by n in ascending order. For convenince, this differs from foldl' in the order of the parameters.

ascend' 4 z f = f 3 (f 2 (f 1 (f 0 z)))
valueascendFrom'
  1. :: Nat m

    Index to start at

  2. -> Nat n

    Number of steps to take

  3. -> a

    Initial accumulator

  4. -> (Fin (m + n) -> a -> a)

    Update accumulator

  5. -> a
#

Generalization of ascend' that lets the caller pick the starting index:

ascend' === ascendFrom' 0
valueascendFrom'#
  1. :: Nat# m

    Index to start at

  2. -> Nat# n

    Number of steps to take

  3. -> a

    Initial accumulator

  4. -> (Fin# (m + n) -> a -> a)

    Update accumulator

  5. -> a
#

Variant of ascendFrom' with unboxed arguments.

valueascendM
  1. :: Monad m
  2. => Nat n

    Upper bound

  3. -> a

    Initial accumulator

  4. -> (Fin n -> a -> m a)

    Update accumulator

  5. -> m a
#

Strict monadic left fold over the numbers bounded by n in ascending order. Roughly:

ascendM 4 z0 f =
  f 0 z0 >>= \z1 ->
  f 1 z1 >>= \z2 ->
  f 2 z2 >>= \z3 ->
  f 3 z3
valueascendM#
  1. :: Monad m
  2. => Nat# n

    Upper bound

  3. -> a

    Initial accumulator

  4. -> (Fin# n -> a -> m a)

    Update accumulator

  5. -> m a
#

Variant of ascendM that takes an unboxed Nat and provides an unboxed Fin to the callback.

valueascendM_
  1. :: Applicative m
  2. => Nat n

    Upper bound

  3. -> (Fin n -> m a)

    Effectful interpretion

  4. -> m ()
#

Monadic traversal of the numbers bounded by n in ascending order.

ascendM_ 4 f = f 0 *> f 1 *> f 2 *> f 3
valueascendM_#
  1. :: Monad m
  2. => Nat# n

    Upper bound

  3. -> (Fin# n -> m a)

    Update accumulator

  4. -> m ()
#

Variant of ascendM_ that takes an unboxed Nat and provides an unboxed Fin to the callback.

valuedescend
  1. :: Nat n

    Upper bound

  2. -> a

    Initial accumulator

  3. -> (Fin n -> a -> a)

    Update accumulator

  4. -> a
#

Fold over the numbers bounded by n in descending order. This is lazy in the accumulator. For convenince, this differs from foldr in the order of the parameters.

descend 4 z f = f 0 (f 1 (f 2 (f 3 z)))
valuedescend#
  1. :: Nat# n

    Upper bound

  2. -> a

    Initial accumulator

  3. -> (Fin# n -> a -> a)

    Update accumulator

  4. -> a
#
valuedescend'
  1. :: Nat n

    Upper bound

  2. -> a

    Initial accumulator

  3. -> (Fin n -> a -> a)

    Update accumulator

  4. -> a
#

Fold over the numbers bounded by n in descending order. This is strict in the accumulator. For convenince, this differs from foldr' in the order of the parameters.

descend 4 z f = f 0 (f 1 (f 2 (f 3 z)))
valuedescendM :: Monad m => Nat n -> a -> (Fin n -> a -> m a) -> m a
#

Strict monadic left fold over the numbers bounded by n in descending order. Roughly:

descendM 4 z f =
  f 3 z0 >>= \z1 ->
  f 2 z1 >>= \z2 ->
  f 1 z2 >>= \z3 ->
  f 0 z3
valuedescendM_
  1. :: Applicative m
  2. => Nat n

    Upper bound

  3. -> (Fin n -> m a)

    Effectful interpretion

  4. -> m ()
#

Monadic traversal of the numbers bounded by n in descending order.

descendM_ 4 f = f 3 *> f 2 *> f 1 *> f 0
valueascending :: Nat n -> [Fin n]
#

Generate all values of a finite set in ascending order.

Example1 expression
ascending (Nat.constant @3)[Fin 0,Fin 1,Fin 2]
valuedescending :: Nat n -> [Fin n]
#

Generate all values of a finite set in descending order.

Example1 expression
descending (Nat.constant @3)[Fin 2,Fin 1,Fin 0]
valueascendingSlice :: Nat off -> Nat len -> (off + len) <= n -> [Fin n]
#

Generate len values starting from off in ascending order.

Example1 expression
ascendingSlice (Nat.constant @2) (Nat.constant @3) (Lte.constant @_ @6)[Fin 2,Fin 3,Fin 4]
valuedescendingSlice :: Nat off -> Nat len -> (off + len) <= n -> [Fin n]
#

Generate len values starting from 'off + len - 1' in descending order.

Example1 expression
descendingSlice (Nat.constant @2) (Nat.constant @3) (Lt.constant @6)[Fin 4,Fin 3,Fin 2]

Absurdities

1 declaration
valueabsurd :: Fin 0 -> void
#

A finite set of no values is impossible.

Demote

2 declarations
valuedemote :: Fin n -> Int
#

Extract the Int from a 'Fin n'. This is intended to be used at a boundary where a safe interface meets the unsafe primitives on top of which it is built.

Deconstruct

2 declarations
valuewith :: Fin n -> (forall (i :: Nat). i < n -> Nat i -> a) -> a
#

Consume the natural number and the proof in the Fin.

valuewith# :: Fin# n -> (forall (i :: Nat). i <# n -> Nat# i -> a) -> a
#

Variant of with for unboxed argument and result types.

Construct

5 declarations
valueremInt# :: Int# -> Nat# n -> Fin# n
#

This crashes if n = 0. Divides i by n and takes the remainder.

valueremWord# :: Word# -> Nat# n -> Fin# n
#

This crashes if n = 0. Divides i by n and takes the remainder.

valuefromInt
  1. :: Nat n

    exclusive upper bound

  2. -> Int
  3. -> Maybe (Fin n)
#

Convert an Int to a finite number, testing that it is less than the upper bound. This crashes with an uncatchable exception when given a negative number.

Lift and Unlift

2 declarations