HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Modulevec-0.5.1.1Haskell2010

Data.Vec.Lazy

Lazy (in elements and spine) length-indexed list: Vec.

Contents
  1. Construction
  2. Conversions
  3. Indexing
  4. Reverse
  5. Dependent folds
  6. Concatenation and splitting
  7. Take and drop
  8. Folds
  9. Special folds
  10. Mapping
  11. Zipping
  12. Monadic
  13. Universe
  14. VecEach
Synopsis
  • Packagevec-0.5.1.1
  • Exports54
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceLazy.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
valuewithDict :: Vec n a -> (SNatI n => r) -> r

O(n). Recover SNatI (and SNatI) dictionary from a Vec value.

Example: reflect is constrained with SNatI n, but if we have a Vec n a, we can recover that dictionary:

Example1 expression
let f :: forall n a. Vec n a -> N.Nat; f v = withDict v (N.reflect (Proxy :: Proxy n)) in f (True ::: VNil)1

Note: using SNatI will be suboptimal, as if GHC has no opportunity to optimise the code, the recusion won't be unfold. How bad such code will perform? I don't know, we'll need benchmarks.

Conversions

valuetoList :: 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(!) :: 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

Reverse

valuereverse :: Vec n a -> Vec n a

Reverse Vec.

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

Dependent folds

valuedfoldr
  1. :: forall (m :: Nat). a -> f m -> f ('S m)
  2. -> f 'Z
  3. -> Vec n a
  4. -> f n

Dependent right fold.

This could been called an indexed fold, but that name is already used.

valuedfoldl
  1. :: forall (m :: Nat). f m -> a -> f ('S m)
  2. -> f 'Z
  3. -> Vec n a
  4. -> f n

Dependent left fold.

valuedfoldl'
  1. :: forall (m :: Nat). f m -> a -> f ('S m)
  2. -> f 'Z
  3. -> Vec n a
  4. -> f n

Dependent strict left fold.

Concatenation and splitting

value(++) :: 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 :: (a -> Vec m b) -> Vec n a -> 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)

Take and drop

valuetake :: LE n m => Vec m a -> Vec n a
Example2 expressions
let xs = 'a' ::: 'b' ::: 'c' ::: 'd' ::: 'e' ::: VNiltake xs :: Vec N.Nat3 Char'a' ::: 'b' ::: 'c' ::: VNil
valuedrop :: (LE n m, SNatI m) => Vec m a -> Vec n a
Example2 expressions
let xs = 'a' ::: 'b' ::: 'c' ::: 'd' ::: 'e' ::: VNildrop xs :: Vec N.Nat3 Char'c' ::: 'd' ::: 'e' ::: VNil

Folds

valueifoldr :: (Fin n -> a -> b -> b) -> b -> Vec n a -> b

Right fold with an index.

Special folds

Mapping

valuemap :: (a -> b) -> Vec n a -> Vec n b
Example1 expression
map not $ True ::: False ::: VNilFalse ::: True ::: VNil
valueimap :: (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

valueizipWith :: (Fin n -> a -> b -> c) -> Vec n a -> Vec n b -> Vec n c

Zip two Vecs. with a function that also takes the elements' indices.

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

Repeat a value.

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

Monadic

valuejoin :: 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