row-types-0.2.0.0: Open Records and Variants

Safe HaskellNone
LanguageHaskell98

Data.Row

Contents

Description

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

Types and constraints

data Label (s :: Symbol) Source #

A label

Constructors

Label 

Instances

(≈) Symbol x y => IsLabel x (Label y) Source # 

Methods

fromLabel :: Label y #

KnownSymbol s => Show (Label s) Source # 

Methods

showsPrec :: Int -> Label s -> ShowS #

show :: Label s -> String #

showList :: [Label s] -> ShowS #

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

Minimal complete definition

symbolSing

type family AllUniqueLabels (r :: Row *) :: Constraint where ... Source #

Are all of the labels in this Row unique?

Equations

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

Forall r Eq => Eq (Var r) Source # 

Methods

(==) :: Var r -> Var r -> Bool #

(/=) :: Var r -> Var r -> Bool #

(Forall r Eq, Forall r Ord) => Ord (Var r) Source # 

Methods

compare :: Var r -> Var r -> Ordering #

(<) :: Var r -> Var r -> Bool #

(<=) :: Var r -> Var r -> Bool #

(>) :: Var r -> Var r -> Bool #

(>=) :: Var r -> Var r -> Bool #

max :: Var r -> Var r -> Var r #

min :: Var r -> Var r -> Var r #

Forall r Show => Show (Var r) Source # 

Methods

showsPrec :: Int -> Var r -> ShowS #

show :: Var r -> String #

showList :: [Var r] -> ShowS #

Forall r NFData => NFData (Var r) Source # 

Methods

rnf :: Var r -> () #

data Rec (r :: Row *) Source #

A record with row r.

Instances

(Forall r Bounded, AllUniqueLabels r) => Bounded (Rec r) Source # 

Methods

minBound :: Rec r #

maxBound :: Rec r #

Forall r Eq => Eq (Rec r) Source # 

Methods

(==) :: Rec r -> Rec r -> Bool #

(/=) :: Rec r -> Rec r -> Bool #

(Forall r Eq, Forall r Ord) => Ord (Rec r) Source # 

Methods

compare :: Rec r -> Rec r -> Ordering #

(<) :: Rec r -> Rec r -> Bool #

(<=) :: Rec r -> Rec r -> Bool #

(>) :: Rec r -> Rec r -> Bool #

(>=) :: Rec r -> Rec r -> Bool #

max :: Rec r -> Rec r -> Rec r #

min :: Rec r -> Rec r -> Rec r #

Forall r Show => Show (Rec r) Source # 

Methods

showsPrec :: Int -> Rec r -> ShowS #

show :: Rec r -> String #

showList :: [Rec r] -> ShowS #

Forall r NFData => NFData (Rec r) Source # 

Methods

rnf :: Rec r -> () #

data Row a Source #

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.

type Empty = R '[] Source #

Type level version of empty

type (≈) a b = a ~ b infix 4 Source #

A lower fixity operator for type equality

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.

Instances

(≈) * ((.!) r l) a => HasType l a r Source # 

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 # 

type family (r :: Row *) .\ (l :: Symbol) :: Constraint where ... infixl 4 Source #

Does the row lack (i.e. it does not have) the specified label?

Equations

(R r) .\ l = LacksR l r r 

type family (l :: Row *) .+ (r :: Row *) :: Row * where ... infixl 6 Source #

Type level Row append

Equations

(R l) .+ (R r) = R (Merge l r) 

class Forall (r :: Row *) (c :: * -> Constraint) Source #

Any structure over a row in which every element is similarly constrained can be metamorphized into another structure over the same row.

Minimal complete definition

metamorph, metamorph'

Instances

(KnownSymbol ℓ, c τ, FoldStep ℓ τ ρ, Forall (R * ρ) c) => Forall (R * ((:) (LT *) ((:->) * ℓ τ) ρ)) c Source # 

Methods

metamorph :: Proxy (* -> *) h -> (f (Empty *) -> g (Empty *)) -> (forall (a :: Symbol) b (d :: [LT *]). (KnownSymbol a, c b) => Label a -> f (R * ((LT * ': (* :-> a) b) d)) -> (h b, f (R * d))) -> (forall (a :: Symbol) b (d :: [LT *]). (KnownSymbol a, c b, FoldStep a b d) => Label a -> h b -> g (R * d) -> g (R * ((LT * ': (* :-> a) b) d))) -> f (R * ((LT * ': (* :-> ℓ) τ) ρ)) -> g (R * ((LT * ': (* :-> ℓ) τ) ρ)) Source #

metamorph' :: Proxy (* -> *) h -> (f (Empty *) -> g (Empty *)) -> (forall (a :: Symbol) b (d :: [LT *]). (KnownSymbol a, c b) => Label a -> f (R * ((LT * ': (* :-> a) b) d)) -> Either (h b) (f (R * d))) -> (forall (a :: Symbol) b (d :: [LT *]). (KnownSymbol a, c b, FoldStep a b d) => Label a -> Either (h b) (g (R * d)) -> g (R * ((LT * ': (* :-> a) b) d))) -> f (R * ((LT * ': (* :-> ℓ) τ) ρ)) -> g (R * ((LT * ': (* :-> ℓ) τ) ρ)) Source #

Forall (R * ([] (LT *))) c Source # 

Methods

metamorph :: Proxy (* -> *) h -> (f (Empty *) -> g (Empty *)) -> (forall (ℓ :: Symbol) τ (ρ :: [LT *]). (KnownSymbol ℓ, c τ) => Label ℓ -> f (R * ((LT * ': (* :-> ℓ) τ) ρ)) -> (h τ, f (R * ρ))) -> (forall (ℓ :: Symbol) τ (ρ :: [LT *]). (KnownSymbol ℓ, c τ, FoldStep ℓ τ ρ) => Label ℓ -> h τ -> g (R * ρ) -> g (R * ((LT * ': (* :-> ℓ) τ) ρ))) -> f (R * [LT *]) -> g (R * [LT *]) Source #

metamorph' :: Proxy (* -> *) h -> (f (Empty *) -> g (Empty *)) -> (forall (ℓ :: Symbol) τ (ρ :: [LT *]). (KnownSymbol ℓ, c τ) => Label ℓ -> f (R * ((LT * ': (* :-> ℓ) τ) ρ)) -> Either (h τ) (f (R * ρ))) -> (forall (ℓ :: Symbol) τ (ρ :: [LT *]). (KnownSymbol ℓ, c τ, FoldStep ℓ τ ρ) => Label ℓ -> Either (h τ) (g (R * ρ)) -> g (R * ((LT * ': (* :-> ℓ) τ) ρ))) -> f (R * [LT *]) -> g (R * [LT *]) Source #

class Switch (v :: Row *) (r :: Row *) x | v x -> r, r x -> v where Source #

A Var and a Rec can combine if their rows line up properly.

Minimal complete definition

switch | caseon

Methods

switch :: Var v -> Rec r -> x Source #

Given a Variant and a Record of functions from each possible value of the variant to a single output type, apply the correct function to the value in the variant.

caseon :: Rec r -> Var v -> x Source #

The same as switch but with the argument order reversed

Instances

(KnownSymbol l, Switch (R * v) (R * r) b) => Switch (R * ((:) (LT *) ((:->) * l a) v)) (R * ((:) (LT *) ((:->) * l (a -> b)) r)) b Source # 

Methods

switch :: Var (R * ((LT * ': (* :-> l) a) v)) -> Rec (R * ((LT * ': (* :-> l) (a -> b)) r)) -> b Source #

caseon :: Rec (R * ((LT * ': (* :-> l) (a -> b)) r)) -> Var (R * ((LT * ': (* :-> l) a) v)) -> b Source #

Switch (R * ([] (LT *))) (R * ([] (LT *))) x Source # 

Methods

switch :: Var (R * [LT *]) -> Rec (R * [LT *]) -> x Source #

caseon :: Rec (R * [LT *]) -> Var (R * [LT *]) -> x Source #

Record Construction

empty :: Rec Empty Source #

The empty record

type (.==) (l :: Symbol) (a :: *) = Extend l a Empty infix 7 Source #

A type level way to create a singleton Row.

(.==) :: KnownSymbol l => Label l -> a -> Rec (l .== a) infix 7 Source #

The singleton record

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.

unSingleton :: forall l a. KnownSymbol l => Rec (l .== a) -> (Label l, a) Source #

Turns a singleton record into a pair of the label and value.

Restriction

type family (r :: Row *) .- (s :: Symbol) :: Row * where ... infixl 6 Source #

Type level Row element removal

Equations

(R r) .- l = R (Remove l r) 

(.-) :: KnownSymbol l => Rec r -> Label l -> Rec (r .- l) infixl 6 Source #

Record restriction. Remove the label l from the record.

restrict :: forall r r'. (Forall r Unconstrained1, Subset r r') => Rec r' -> Rec r Source #

Arbitrary record restriction. Turn a record into a subset of itself.

Query

type family (r :: Row *) .! (t :: Symbol) :: * where ... infixl 5 Source #

Type level label fetching

Equations

(R r) .! l = Get l r 

(.!) :: KnownSymbol l => Rec r -> Label l -> r .! l Source #

Record selection

Disjoint union

(.+) :: 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.

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.

Restriction

diversify :: forall r' r. AllUniqueLabels (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 (r .! l) (Var (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 x) (Var (y .\\ x)) Source #

A trial over multiple types

type family (l :: Row *) .\\ (r :: Row *) :: Row * where ... infixl 6 Source #

Type level Row difference. That is, l .\\ r is the row remaining after removing any matching elements of r from l.

Equations

(R l) .\\ (R r) = R (Diff l r) 

Labels

labels :: forall ρ c s. (IsString s, Forall ρ c) => [s] Source #

Return a list of the labels in a row type.