Copyright | (c) 2021 Xy Ren |
---|---|
License | BSD3 |
Maintainer | xy.r@outlook.com |
Stability | experimental |
Portability | non-portable (GHC only) |
Safe Haskell | None |
Language | Haskell2010 |
This module defines an immutable extensible record type, similar to vinyl
and data-diverse
. However this
implementation focuses on fast reads, hence has very different performance characteristics from other libraries:
- Lookup: Amortized \( O(1) \).
- Update: \( O(n) \).
- Shrink: \( O(1) \).
- Append: \( O(n) \).
Synopsis
- data Rec (f :: k -> Type) (es :: [k])
- length :: Rec f es -> Int
- empty :: Rec f '[]
- singleton :: f e -> Rec f '[e]
- cons :: f e -> Rec f es -> Rec f (e ': es)
- pattern (:~:) :: f e -> Rec f es -> Rec f (e ': es)
- type family xs ++ ys where ...
- concat :: Rec f es -> Rec f es' -> Rec f (es ++ es')
- pattern (:++:) :: forall es es' f. KnownList es => Rec f es -> Rec f es' -> Rec f (es ++ es')
- tail :: Rec f (e ': es) -> Rec f es
- class KnownList (es :: [k])
- drop :: forall es es' f. KnownList es => Rec f (es ++ es') -> Rec f es'
- head :: Rec f (e ': es) -> f e
- take :: forall es es' f. KnownList es => Rec f (es ++ es') -> Rec f es
- class Elem (e :: k) (es :: [k])
- index :: forall e es f. Elem e es => Rec f es -> f e
- class KnownList es => Subset (es :: [k]) (es' :: [k])
- pick :: forall es es' f. Subset es es' => Rec f es' -> Rec f es
- update :: forall e es f. Elem e es => f e -> Rec f es -> Rec f es
- (/~/) :: Elem e es => f e -> Rec f es -> Rec f es
- modify :: forall e es f. Elem e es => (f e -> f e) -> Rec f es -> Rec f es
- batch :: forall es es' f. Subset es es' => Rec f es -> Rec f es' -> Rec f es'
- (/++/) :: Subset es es' => Rec f es -> Rec f es' -> Rec f es'
- type (~>) f g = forall a. f a -> g a
- natural :: (f ~> g) -> Rec f es -> Rec g es
- (<#>) :: (f ~> g) -> Rec f es -> Rec g es
- zipWith :: (forall x. f x -> g x -> h x) -> Rec f es -> Rec g es -> Rec h es
- all :: (forall x. f x -> Bool) -> Rec f es -> Bool
- any :: (forall x. f x -> Bool) -> Rec f es -> Bool
- degenerate :: Rec (Const a) es -> [a]
- extract :: (forall x. f x -> a) -> Rec f es -> [a]
- invariant :: Rec f es -> Rec f es
- sizeInvariant :: Rec f es -> Rec f es
- allAccessible :: Rec f es -> Rec f es
Documentation
data Rec (f :: k -> Type) (es :: [k]) Source #
Extensible record type supporting efficient \( O(1) \) reads. The underlying implementation is SmallArray
slices, therefore suits small numbers of entries (i.e. less than 128).
Instances
(forall (x :: k). Eq (f x)) => Eq (Rec f xs) Source # | |
(Eq (Rec f xs), Eq (f x)) => Eq (Rec f (x ': xs)) Source # | |
Eq (Rec f ('[] :: [k])) Source # | |
(Read (f x), Read (Rec f xs)) => Read (Rec f (x ': xs)) Source # |
|
Read (Rec f ('[] :: [k])) Source # |
|
(forall (x :: k). Show (f x)) => Show (Rec f xs) Source # |
|
(Show (f x), Show (Rec f xs)) => Show (Rec f (x ': xs)) Source # |
|
Show (Rec f ('[] :: [k])) Source # |
|
(forall (x :: k). Semigroup (f x)) => Semigroup (Rec f xs) Source # | |
(Semigroup (f x), Semigroup (Rec f xs)) => Semigroup (Rec f (x ': xs)) Source # | One-by-one semigroup operation instead of concatenation. (x |
Semigroup (Rec f ('[] :: [k])) Source # | |
(Monoid (f x), Monoid (Rec f xs)) => Monoid (Rec f (x ': xs)) Source # | The unit of a record type are the units of its element types:
|
Monoid (Rec f ('[] :: [k])) Source # |
|
Construction
pattern (:~:) :: f e -> Rec f es -> Rec f (e ': es) infixr 5 Source #
Infix version of cons
that also supports destructuring.
pattern (:++:) :: forall es es' f. KnownList es => Rec f es -> Rec f es' -> Rec f (es ++ es') infixr 5 Source #
Infix version of concat
that also supports destructuring.
Deconstruction
tail :: Rec f (e ': es) -> Rec f es Source #
Slice off one entry from the top of the record. \( O(1) \).
class KnownList (es :: [k]) Source #
The list es
list is concrete, i.e. is of the form '[a1, a2, ..., an]
, i.e. is not a type variable.
drop :: forall es es' f. KnownList es => Rec f (es ++ es') -> Rec f es' Source #
Slice off several entries from the top of the record. \( O(1) \).
Retrieval
take :: forall es es' f. KnownList es => Rec f (es ++ es') -> Rec f es Source #
Take elements from the top of the record. \( O(m) \).
class Elem (e :: k) (es :: [k]) Source #
The element e
is present in the list es
.
Instances
(TypeError (ElemNotFound e) :: Constraint) => Elem (e :: k) ('[] :: [k]) Source # | |
Defined in Data.Rec.SmallArray reifyIndex :: Int | |
Elem e es => Elem (e :: a) (e' ': es :: [a]) Source # | |
Defined in Data.Rec.SmallArray reifyIndex :: Int | |
Elem (e :: a) (e ': es :: [a]) Source # | |
Defined in Data.Rec.SmallArray reifyIndex :: Int |
index :: forall e es f. Elem e es => Rec f es -> f e Source #
Get an element in the record. Amortized \( O(1) \).
class KnownList es => Subset (es :: [k]) (es' :: [k]) Source #
es
is a subset of es'
.
Instances
Subset ('[] :: [k]) (es :: [k]) Source # | |
Defined in Data.Rec.SmallArray reifyIndices :: [Int] | |
(Subset es es', Elem e es') => Subset (e ': es :: [k]) (es' :: [k]) Source # | |
Defined in Data.Rec.SmallArray reifyIndices :: [Int] |
pick :: forall es es' f. Subset es es' => Rec f es' -> Rec f es Source #
Get a subset of the record. Amortized \( O(m) \).
Updating
update :: forall e es f. Elem e es => f e -> Rec f es -> Rec f es Source #
Update an entry in the record. \( O(n) \).
modify :: forall e es f. Elem e es => (f e -> f e) -> Rec f es -> Rec f es Source #
Modify an entry in the record via a function. \( O(n) \).
batch :: forall es es' f. Subset es es' => Rec f es -> Rec f es' -> Rec f es' Source #
Merge a subset into the original record, updating several entries at once. \( O(m+n) \).
(/++/) :: Subset es es' => Rec f es -> Rec f es' -> Rec f es' infixl 9 Source #
Infix version of batch
.
Mapping and Folding
type (~>) f g = forall a. f a -> g a infixr 0 Source #
The type of natural transformations from functor f
to g
.
natural :: (f ~> g) -> Rec f es -> Rec g es Source #
Apply a natural transformation to the record. \( O(n) \).
zipWith :: (forall x. f x -> g x -> h x) -> Rec f es -> Rec g es -> Rec h es Source #
Zip two records with a natural transformation. \( O(n) \).
all :: (forall x. f x -> Bool) -> Rec f es -> Bool Source #
Check if a predicate is true on all elements. \( O(n) \).
any :: (forall x. f x -> Bool) -> Rec f es -> Bool Source #
Check if a predicate is true on at least one element. \( O(n) \).
degenerate :: Rec (Const a) es -> [a] Source #
Convert a record that effectively contains a fixed type into a list of the fixed type. \( O(n) \).
extract :: (forall x. f x -> a) -> Rec f es -> [a] Source #
Map each element to a fixed type. \( O(n) \).