HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulevec-0.5.1.1Haskell2010

Data.Vec.Lazy.Inline

A variant of Data.Vec.Lazy with functions written using SNatI. The hypothesis is that these (goursive) functions could be fully unrolled, if the Vec size n is known at compile time.

The module has the same API as Data.Vec.Lazy (sans L.withDict and foldl'). Note: instance methods aren't changed, the Vec type is the same.

Contents
  1. Construction
  2. Conversions
  3. Indexing
  4. Concatenation and splitting
  5. Reverse
  6. Folds
  7. Special folds
  8. Mapping
  9. Zipping
  10. Monadic
  11. Universe
  12. VecEach
Synopsis
  • Packagevec-0.5.1.1
  • Exports47
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceInline.hs
datadata Vec (n :: Nat) a where

Vector, i.e. length-indexed list.

Constructors

Instances30Monad, Functor, Applicative, Foldable, Traversable, Foldable1, …

Construction

valuesingleton :: a -> Vec ('S 'Z) a

Vec with exactly one element.

Example1 expression
singleton TrueTrue ::: VNil

Conversions

valuetoList :: SNatI n => Vec n a -> [a]

Convert Vec to list.

Example1 expression
toList $ 'f' ::: 'o' ::: 'o' ::: VNil"foo"
valuefromList :: SNatI n => [a] -> Maybe (Vec n a)

Convert list [a] to Vec n a. Returns Nothing if lengths don't match exactly.

Example1 expression
fromList "foo" :: Maybe (Vec N.Nat3 Char)Just ('f' ::: 'o' ::: 'o' ::: VNil)
Example1 expression
fromList "quux" :: Maybe (Vec N.Nat3 Char)Nothing
Example1 expression
fromList "xy" :: Maybe (Vec N.Nat3 Char)Nothing
valuefromListPrefix :: SNatI n => [a] -> Maybe (Vec n a)

Convert list [a] to Vec n a. Returns Nothing if input list is too short.

Example1 expression
fromListPrefix "foo" :: Maybe (Vec N.Nat3 Char)Just ('f' ::: 'o' ::: 'o' ::: VNil)
Example1 expression
fromListPrefix "quux" :: Maybe (Vec N.Nat3 Char)Just ('q' ::: 'u' ::: 'u' ::: VNil)
Example1 expression
fromListPrefix "xy" :: Maybe (Vec N.Nat3 Char)Nothing
valuereifyList :: [a] -> (forall (n :: Nat). SNatI n => Vec n a -> r) -> r

Reify any list [a] to Vec n a.

Example1 expression
reifyList "foo" length3

Indexing

value(!) :: SNatI n => Vec n a -> Fin n -> a

Indexing.

Example1 expression
('a' ::: 'b' ::: 'c' ::: VNil) ! FS FZ'b'
valuetabulate :: SNatI n => (Fin n -> a) -> Vec n a

Tabulating, inverse of !.

Example1 expression
tabulate id :: Vec N.Nat3 (Fin N.Nat3)0 ::: 1 ::: 2 ::: VNil

Concatenation and splitting

value(++) :: SNatI n => Vec n a -> Vec m a -> Vec (Plus n m) a

Append two Vec.

Example1 expression
('a' ::: 'b' ::: VNil) ++ ('c' ::: 'd' ::: VNil)'a' ::: 'b' ::: 'c' ::: 'd' ::: VNil
valuesplit :: SNatI n => Vec (Plus n m) a -> (Vec n a, Vec m a)

Split vector into two parts. Inverse of ++.

Example1 expression
split ('a' ::: 'b' ::: 'c' ::: VNil) :: (Vec N.Nat1 Char, Vec N.Nat2 Char)('a' ::: VNil,'b' ::: 'c' ::: VNil)
Example1 expression
uncurry (++) (split ('a' ::: 'b' ::: 'c' ::: VNil) :: (Vec N.Nat1 Char, Vec N.Nat2 Char))'a' ::: 'b' ::: 'c' ::: VNil
valueconcatMap
  1. :: (SNatI m, SNatI n)
  2. => a -> Vec m b
  3. -> Vec n a
  4. -> Vec (Mult n m) b

Map over all the elements of a Vec and concatenate the resulting Vecs.

Example1 expression
concatMap (\x -> x ::: x ::: VNil) ('a' ::: 'b' ::: VNil)'a' ::: 'a' ::: 'b' ::: 'b' ::: VNil
valuechunks :: (SNatI n, SNatI m) => Vec (Mult n m) a -> Vec n (Vec m a)

Inverse of concat.

Example1 expression
chunks <$> fromListPrefix [1..] :: Maybe (Vec N.Nat2 (Vec N.Nat3 Int))Just ((1 ::: 2 ::: 3 ::: VNil) ::: (4 ::: 5 ::: 6 ::: VNil) ::: VNil)
Example2 expressions
let idVec x = x :: Vec N.Nat2 (Vec N.Nat3 Int)concat . idVec . chunks <$> fromListPrefix [1..]Just (1 ::: 2 ::: 3 ::: 4 ::: 5 ::: 6 ::: VNil)

Reverse

valuereverse :: SNatI n => Vec n a -> Vec n a

Reverse Vec.

Example1 expression
reverse ('a' ::: 'b' ::: 'c' ::: VNil)'c' ::: 'b' ::: 'a' ::: VNil

Folds

Special folds

Mapping

valuemap :: SNatI n => (a -> b) -> Vec n a -> Vec n b
Example1 expression
map not $ True ::: False ::: VNilFalse ::: True ::: VNil
valueimap :: SNatI n => (Fin n -> a -> b) -> Vec n a -> Vec n b
Example1 expression
imap (,) $ 'a' ::: 'b' ::: 'c' ::: VNil(0,'a') ::: (1,'b') ::: (2,'c') ::: VNil

Zipping

valuerepeat :: SNatI n => x -> Vec n x

Repeat value

Example1 expression
repeat 'x' :: Vec N.Nat3 Char'x' ::: 'x' ::: 'x' ::: VNil

Monadic

valuejoin :: SNatI n => Vec n (Vec n a) -> Vec n a

Monadic join.

Example1 expression
join $ ('a' ::: 'b' ::: VNil) ::: ('c' ::: 'd' ::: VNil) ::: VNil'a' ::: 'd' ::: VNil

Universe

VecEach

classclass VecEach s t a b | s -> a, t -> b, s b -> t, t a -> s where

Write functions on Vec. Use them with tuples.

VecEach can be used to avoid "this function won't change the length of the list" in DSLs.

bad: Instead of

[x, y] <- badDslMagic ["foo", "bar"]  -- list!

good: we can write

(x, y) <- betterDslMagic ("foo", "bar") -- homogenic tuple!

where betterDslMagic can be defined using traverseWithVec.

Moreally lens Each should be a superclass, but there's no strict need for it.

Methods

Instances3VecEach
  • (a ~ a', b ~ b') => VecEach (a, a') (b, b') a bDefined in vec-0.5.1.1 · Data.Vec.Lazy
  • (a ~ a2, a ~ a3, b ~ b2, b ~ b3) => VecEach (a, a2, a3) (b, b2, b3) a bDefined in vec-0.5.1.1 · Data.Vec.Lazy
  • (a ~ a2, a ~ a3, a ~ a4, b ~ b2, b ~ b3, b ~ b4) => VecEach (a, a2, a3, a4) (b, b2, b3, b4) a bDefined in vec-0.5.1.1 · Data.Vec.Lazy