HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulevec-0.5.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.

  • 1 type
  • 1 class
  • 45 values
  • Packagevec-0.5.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

2 declarations
valuesingleton :: a -> Vec ('S 'Z) a
#

Vec with exactly one element.

Example1 expression
singleton TrueTrue ::: VNil

Conversions

7 declarations
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

8 declarations
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
valuecons :: a -> Vec n a -> Vec ('S n) a
#

Cons an element in front of a Vec.

valuehead :: Vec ('S n) a -> a
#

The first element of a Vec.

Concatenation and splitting

5 declarations
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

1 declaration
valuereverse :: SNatI n => Vec n a -> Vec n a
#

Reverse Vec.

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

Folds

6 declarations
valuefoldr :: SNatI n => (a -> b -> b) -> b -> Vec n a -> b
#

Right fold.

valueifoldr :: SNatI n => (Fin n -> a -> b -> b) -> b -> Vec n a -> b
#

Right fold with an index.

Special folds

4 declarations

Mapping

6 declarations
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

3 declarations
valueizipWith
  1. :: SNatI n
  2. => Fin n -> a -> b -> c
  3. -> Vec n a
  4. -> Vec n b
  5. -> Vec n c
#

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

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

Repeat value

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

Monadic

2 declarations
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

1 declaration
valueuniverse :: SNatI n => Vec n (Fin n)
#

Get all Fin n in a Vec n.

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

VecEach

1 declaration
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 · 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 · 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 · Data.Vec.Lazy