cubix-compdata
Copyright(c) 2020 James Koppel
LicenseBSD3
Safe HaskellNone
LanguageHaskell2010

Data.Comp.Dict

Description

The goal of this package is to define the All typeclass, where, if fs :: [k] is a type-level list of types or type constructors, then All c fs holds if c f holds for each f in fs.

Synopsis

Documentation

data Dict (c :: k -> Constraint) (a :: k) where Source #

A reified Constraint. Similar in spirit to Dict from ekmett's constraints package, but that definition has a kind of @Constraint -> */

Constructors

Dict :: forall {k} (c :: k -> Constraint) (a :: k). c a => Dict c a 

class All (c :: k -> Constraint) (fs :: [k]) Source #

An instance of All c fs holds if c f holds for all f in fs.

Example: All HFunctor '[Add, Mul] holds if there are HFunctor instances for signatures Add and Mul.

The primary way to consume an All instance is with the caseCxt function. E.g.:

class Pretty f where
  pretty :: f e l -> String

instance (All Pretty fs) => Pretty (Sum fs) where
  pretty x = caseCxt @Pretty pretty x

Minimal complete definition

dicts

Instances

Instances details
All (c :: k -> Constraint) ('[] :: [k]) Source # 
Instance details

Defined in Data.Comp.Dict

Methods

dicts :: Proxy# ('[] :: [k]) -> [E (Dict c)]

(All c fs, c f) => All (c :: a -> Constraint) (f ': fs :: [a]) Source # 
Instance details

Defined in Data.Comp.Dict

Methods

dicts :: Proxy# (f ': fs) -> [E (Dict c)]

withDict :: forall {k} c (a :: k) r. Dict c a -> (c a => r) -> r Source #

Puts the typeclass instance of a Dict back into the context.

(\\) :: forall {k} c (a :: k) r. (c a => r) -> Dict c a -> r infixl 1 Source #

Inline version of withDict

dictFor :: forall {k} (c :: k -> Constraint) (f :: k) (fs :: [k]). All c fs => Elem f fs -> Dict c f Source #

If All c fs holds, and e is type-level proof that f is in fs, then dictFor e is the instance of c for f

This uses unsafeCoerce under the hood, but is safe if e is constructed by the public API.

mapAll :: forall {k} cxt (fs :: [k]) a. All cxt fs => (forall (f :: k). cxt f => Proxy f -> a) -> [a] Source #

Lifts a mapping from type to value (k -> *) -> a into a mapping from list of types to list of values [(k -> *)] -> [a]

Example usage:

class TypeNum f where
 typeNum :: Proxy f -> Int

instance TypeNum [] where
  typeNum _ = 0

instance TypeNum Maybe where
  typeNum _ = 1

test = mapAll TypeNum '[[], Maybe] typeNum

For commentary on why Proxy is needed in the polymorphic argument, see https://stackoverflow.com/questions/65488453/preventing-premature-monomorphization-of-constrained-polymorphic-values