Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module includes a set of common functions for Records and Variants. It includes:
- Common constructors, destructors, and querying functions
It specifically excludes:
- Functions that have the same name for Records and Variants (e.g.
focus
,update
,fromLabels
, etc.) - Common clashes with the standard Prelude or other modules (e.g.
map
,sequence
,zip
,Map
, etc.)
If these particular functions are needed, they should be brought in qualified from one of the Data.Row.*** modules directly.
Synopsis
- data Label (s :: Symbol) = Label
- class KnownSymbol (n :: Symbol)
- type family AllUniqueLabels (r :: Row k) :: Constraint where ...
- type WellBehaved ρ = (Forall ρ Unconstrained1, AllUniqueLabels ρ)
- data Var (r :: Row *)
- data Rec (r :: Row *)
- data Row a
- type Empty = R '[]
- type (≈) a b = a ~ b
- class (r .! l) ≈ a => HasType l a r
- class Lacks (l :: Symbol) (r :: Row *)
- type family (r :: Row k) .\ (l :: Symbol) :: Constraint where ...
- type family (l :: Row k) .+ (r :: Row k) :: Row k where ...
- type family (l :: Row k) .\/ (r :: Row k) where ...
- type family (l :: Row k) .\\ (r :: Row k) :: Row k where ...
- type family (l :: Row k) .// (r :: Row k) where ...
- class BiForall (r1 :: Row k1) (r2 :: Row k2) (c :: k1 -> k2 -> Constraint)
- class Forall (r :: Row k) (c :: k -> Constraint)
- type FreeForall r = Forall r Unconstrained1
- type FreeBiForall r1 r2 = BiForall r1 r2 Unconstrained2
- switch :: forall v r x. BiForall r v (AppliesTo x) => Var v -> Rec r -> x
- caseon :: forall v r x. BiForall r v (AppliesTo x) => Rec r -> Var v -> x
- empty :: Rec Empty
- type (.==) (l :: Symbol) (a :: k) = Extend l a Empty
- (.==) :: KnownSymbol l => Label l -> a -> Rec (l .== a)
- pattern (:==) :: forall l a. KnownSymbol l => Label l -> a -> Rec (l .== a)
- type family (r :: Row k) .- (s :: Symbol) :: Row k where ...
- (.-) :: KnownSymbol l => Rec r -> Label l -> Rec (r .- l)
- type family (r :: Row k) .! (t :: Symbol) :: k where ...
- (.!) :: KnownSymbol l => Rec r -> Label l -> r .! l
- (.+) :: forall l r. FreeForall l => Rec l -> Rec r -> Rec (l .+ r)
- type Disjoint l r = (WellBehaved l, WellBehaved r, Subset l (l .+ r), Subset r (l .+ r), ((l .+ r) .\\ l) ≈ r, ((l .+ r) .\\ r) ≈ l)
- pattern (:+) :: forall l r. Disjoint l r => Rec l -> Rec r -> Rec (l .+ r)
- (.//) :: Rec r -> Rec r' -> Rec (r .// r')
- pattern IsJust :: forall l r. (AllUniqueLabels r, KnownSymbol l) => Label l -> (r .! l) -> Var r
- diversify :: forall r' r. Var r -> Var (r .\/ r')
- impossible :: Var Empty -> a
- trial :: KnownSymbol l => Var r -> Label l -> Either (Var (r .- l)) (r .! l)
- trial' :: KnownSymbol l => Var r -> Label l -> Maybe (r .! l)
- multiTrial :: forall x y. (AllUniqueLabels x, Forall (y .\\ x) Unconstrained1) => Var y -> Either (Var (y .\\ x)) (Var x)
- labels :: forall ρ c s. (IsString s, Forall ρ c) => [s]
Types and constraints
data Label (s :: Symbol) Source #
A label
class KnownSymbol (n :: Symbol) #
This class gives the string associated with a type-level symbol. There are instances of the class for every concrete literal: "hello", etc.
Since: base-4.7.0.0
symbolSing
type family AllUniqueLabels (r :: Row k) :: Constraint where ... Source #
Are all of the labels in this Row unique?
AllUniqueLabels (R r) = AllUniqueLabelsR r |
type WellBehaved ρ = (Forall ρ Unconstrained1, AllUniqueLabels ρ) Source #
A convenient way to provide common, easy constraints
data Var (r :: Row *) Source #
The variant type.
Instances
(AllUniqueLabels r, KnownSymbol name, (r .! name) ≈ a, r ≈ ((r .- name) .\/ (name .== a))) => AsConstructor' name (Var r) a Source # | |
(AllUniqueLabels r, AllUniqueLabels r', KnownSymbol name, (r .! name) ≈ a, (r' .! name) ≈ b, r' ≈ ((r .- name) .\/ (name .== b))) => AsConstructor name (Var r) (Var r') a b Source # | Every possibility of a row-types based variant has an |
Forall r Eq => Eq (Var r) Source # | |
(Forall r Eq, Forall r Ord) => Ord (Var r) Source # | |
Forall r Show => Show (Var r) Source # | |
GenericVar r => Generic (Var r) Source # | |
Forall r NFData => NFData (Var r) Source # | |
Defined in Data.Row.Variants | |
type Rep (Var r) Source # | |
Defined in Data.Row.Variants |
data Rec (r :: Row *) Source #
A record with row r.
Instances
(KnownSymbol name, (r .! name) ≈ a, r ~ Modify name a r) => HasField' name (Rec r) a Source # | |
(KnownSymbol name, (r' .! name) ≈ b, (r .! name) ≈ a, r' ~ Modify name b r, r ~ Modify name a r') => HasField name (Rec r) (Rec r') a b Source # | Every field in a row-types based record has a |
(Forall r Bounded, AllUniqueLabels r) => Bounded (Rec r) Source # | |
Forall r Eq => Eq (Rec r) Source # | |
(Forall r Eq, Forall r Ord) => Ord (Rec r) Source # | |
Forall r Show => Show (Rec r) Source # | |
GenericRec r => Generic (Rec r) Source # | |
Forall r NFData => NFData (Rec r) Source # | |
Defined in Data.Row.Records | |
type Rep (Rec r) Source # | |
Defined in Data.Row.Records |
The kind of rows. This type is only used as a datakind. A row is a typelevel entity telling us which symbols are associated with which types.
class (r .! l) ≈ a => HasType l a r Source #
Alias for (r .! l) ≈ a
. It is a class rather than an alias, so that
it can be partially applied.
class Lacks (l :: Symbol) (r :: Row *) Source #
Alias for .\
. It is a class rather than an alias, so that
it can be partially applied.
Instances
r .\ l => Lacks l r Source # | |
Defined in Data.Row.Internal |
type family (r :: Row k) .\ (l :: Symbol) :: Constraint where ... infixl 4 Source #
Does the row lack (i.e. it does not have) the specified label?
type family (l :: Row k) .\/ (r :: Row k) where ... infixl 6 Source #
The minimum join of the two rows.
type family (l :: Row k) .\\ (r :: Row k) :: Row k where ... infixl 6 Source #
Type level Row difference. That is, l
is the row remaining after
removing any matching elements of .\\
rr
from l
.
type family (l :: Row k) .// (r :: Row k) where ... infixl 6 Source #
The overwriting union, where the left row overwrites the types of the right row where the labels overlap.
class BiForall (r1 :: Row k1) (r2 :: Row k2) (c :: k1 -> k2 -> Constraint) Source #
Any structure over two rows in which the elements of each row satisfy some constraints can be metamorphized into another structure over both of the rows.
Instances
(KnownSymbol ℓ, c τ1 τ2, BiForall (R ρ1) (R ρ2) c, FrontExtends ℓ τ1 (R ρ1), FrontExtends ℓ τ2 (R ρ2), AllUniqueLabels (Extend ℓ τ1 (R ρ1)), AllUniqueLabels (Extend ℓ τ2 (R ρ2))) => BiForall (R ((ℓ :-> τ1) ': ρ1) :: Row k1) (R ((ℓ :-> τ2) ': ρ2) :: Row k2) (c :: k1 -> k2 -> Constraint) Source # | |
Defined in Data.Row.Internal biMetamorph :: Bifunctor p => Proxy (Proxy h, Proxy p) -> (f Empty Empty -> g Empty Empty) -> (forall (ℓ0 :: Symbol) (τ10 :: k10) (τ20 :: k20) (ρ10 :: Row k10) (ρ20 :: Row k20). (KnownSymbol ℓ0, c τ10 τ20, HasType ℓ0 τ10 ρ10, HasType ℓ0 τ20 ρ20) => Label ℓ0 -> f ρ10 ρ20 -> p (f (ρ10 .- ℓ0) (ρ20 .- ℓ0)) (h τ10 τ20)) -> (forall (ℓ1 :: Symbol) (τ11 :: k10) (τ21 :: k20) (ρ11 :: Row k10) (ρ21 :: Row k20). (KnownSymbol ℓ1, c τ11 τ21, FrontExtends ℓ1 τ11 ρ11, FrontExtends ℓ1 τ21 ρ21, AllUniqueLabels (Extend ℓ1 τ11 ρ11), AllUniqueLabels (Extend ℓ1 τ21 ρ21)) => Label ℓ1 -> p (g ρ11 ρ21) (h τ11 τ21) -> g (Extend ℓ1 τ11 ρ11) (Extend ℓ1 τ21 ρ21)) -> f (R ((ℓ :-> τ1) ': ρ1)) (R ((ℓ :-> τ2) ': ρ2)) -> g (R ((ℓ :-> τ1) ': ρ1)) (R ((ℓ :-> τ2) ': ρ2)) Source # | |
BiForall (R ([] :: [LT k1]) :: Row k1) (R ([] :: [LT k2]) :: Row k2) (c1 :: k1 -> k2 -> Constraint) Source # | |
Defined in Data.Row.Internal biMetamorph :: Bifunctor p => Proxy (Proxy h, Proxy p) -> (f Empty Empty -> g Empty Empty) -> (forall (ℓ :: Symbol) (τ1 :: k10) (τ2 :: k20) (ρ1 :: Row k10) (ρ2 :: Row k20). (KnownSymbol ℓ, c1 τ1 τ2, HasType ℓ τ1 ρ1, HasType ℓ τ2 ρ2) => Label ℓ -> f ρ1 ρ2 -> p (f (ρ1 .- ℓ) (ρ2 .- ℓ)) (h τ1 τ2)) -> (forall (ℓ :: Symbol) (τ1 :: k10) (τ2 :: k20) (ρ1 :: Row k10) (ρ2 :: Row k20). (KnownSymbol ℓ, c1 τ1 τ2, FrontExtends ℓ τ1 ρ1, FrontExtends ℓ τ2 ρ2, AllUniqueLabels (Extend ℓ τ1 ρ1), AllUniqueLabels (Extend ℓ τ2 ρ2)) => Label ℓ -> p (g ρ1 ρ2) (h τ1 τ2) -> g (Extend ℓ τ1 ρ1) (Extend ℓ τ2 ρ2)) -> f (R []) (R []) -> g (R []) (R []) Source # |
class Forall (r :: Row k) (c :: k -> Constraint) Source #
Any structure over a row in which every element is similarly constrained can be metamorphized into another structure over the same row.
Instances
(KnownSymbol ℓ, c τ, Forall (R ρ) c, FrontExtends ℓ τ (R ρ), AllUniqueLabels (Extend ℓ τ (R ρ))) => Forall (R ((ℓ :-> τ) ': ρ) :: Row k) (c :: k -> Constraint) Source # | |
Defined in Data.Row.Internal metamorph :: Bifunctor p => Proxy (Proxy h, Proxy p) -> (f Empty -> g Empty) -> (forall (ℓ0 :: Symbol) (τ0 :: k0) (ρ0 :: Row k0). (KnownSymbol ℓ0, c τ0, HasType ℓ0 τ0 ρ0) => Label ℓ0 -> f ρ0 -> p (f (ρ0 .- ℓ0)) (h τ0)) -> (forall (ℓ1 :: Symbol) (τ1 :: k0) (ρ1 :: Row k0). (KnownSymbol ℓ1, c τ1, FrontExtends ℓ1 τ1 ρ1, AllUniqueLabels (Extend ℓ1 τ1 ρ1)) => Label ℓ1 -> p (g ρ1) (h τ1) -> g (Extend ℓ1 τ1 ρ1)) -> f (R ((ℓ :-> τ) ': ρ)) -> g (R ((ℓ :-> τ) ': ρ)) Source # | |
Forall (R ([] :: [LT k]) :: Row k) (c :: k -> Constraint) Source # | |
Defined in Data.Row.Internal metamorph :: Bifunctor p => Proxy (Proxy h, Proxy p) -> (f Empty -> g Empty) -> (forall (ℓ :: Symbol) (τ :: k0) (ρ :: Row k0). (KnownSymbol ℓ, c τ, HasType ℓ τ ρ) => Label ℓ -> f ρ -> p (f (ρ .- ℓ)) (h τ)) -> (forall (ℓ :: Symbol) (τ :: k0) (ρ :: Row k0). (KnownSymbol ℓ, c τ, FrontExtends ℓ τ ρ, AllUniqueLabels (Extend ℓ τ ρ)) => Label ℓ -> p (g ρ) (h τ) -> g (Extend ℓ τ ρ)) -> f (R []) -> g (R []) Source # |
type FreeForall r = Forall r Unconstrained1 Source #
FreeForall
can be used when a Forall
constraint is necessary but there
is no particular constraint we care about.
type FreeBiForall r1 r2 = BiForall r1 r2 Unconstrained2 Source #
FreeForall
can be used when a BiForall
constraint is necessary but
there is no particular constraint we care about.
caseon :: forall v r x. BiForall r v (AppliesTo x) => Rec r -> Var v -> x Source #
The same as switch
but with the argument order reversed
Record Construction
type (.==) (l :: Symbol) (a :: k) = Extend l a Empty infix 7 Source #
A type level way to create a singleton Row.
pattern (:==) :: forall l a. KnownSymbol l => Label l -> a -> Rec (l .== a) infix 7 Source #
A pattern for the singleton record; can be used to both destruct a record when in a pattern position or construct one in an expression position.
Restriction
type family (r :: Row k) .- (s :: Symbol) :: Row k where ... infixl 6 Source #
Type level Row element removal
(.-) :: KnownSymbol l => Rec r -> Label l -> Rec (r .- l) infixl 6 Source #
Record restriction. Remove the label l from the record.
Query
Union
(.+) :: forall l r. FreeForall l => Rec l -> Rec r -> Rec (l .+ r) infixl 6 Source #
Record disjoint union (commutative)
type Disjoint l r = (WellBehaved l, WellBehaved r, Subset l (l .+ r), Subset r (l .+ r), ((l .+ r) .\\ l) ≈ r, ((l .+ r) .\\ r) ≈ l) Source #
A type synonym for disjointness.
pattern (:+) :: forall l r. Disjoint l r => Rec l -> Rec r -> Rec (l .+ r) infixl 6 Source #
A pattern version of record union, for use in pattern matching.
(.//) :: Rec r -> Rec r' -> Rec (r .// r') Source #
Record overwrite.
The operation r .// r'
creates a new record such that:
- Any label that is in both
r
andr'
is in the resulting record with the type and value given by the fields inr
, - Any label that is only found in
r
is in the resulting record. - Any label that is only found in
r'
is in the resulting record.
This can be thought of as r
"overwriting" r'
.
Variant construction
pattern IsJust :: forall l r. (AllUniqueLabels r, KnownSymbol l) => Label l -> (r .! l) -> Var r Source #
A pattern for variants; can be used to both destruct a variant when in a pattern position or construct one in an expression position.
Expansion
diversify :: forall r' r. Var r -> Var (r .\/ r') Source #
Make the variant arbitrarily more diverse.
Destruction
impossible :: Var Empty -> a Source #
A Variant with no options is uninhabited.
trial :: KnownSymbol l => Var r -> Label l -> Either (Var (r .- l)) (r .! l) Source #
Convert a variant into either the value at the given label or a variant without that label. This is the basic variant destructor.
trial' :: KnownSymbol l => Var r -> Label l -> Maybe (r .! l) Source #
A version of trial
that ignores the leftover variant.
multiTrial :: forall x y. (AllUniqueLabels x, Forall (y .\\ x) Unconstrained1) => Var y -> Either (Var (y .\\ x)) (Var x) Source #
A trial over multiple types