| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Generics.MRSOP.Base.Universe
Contents
Description
Synopsis
- data Atom kon
- data NA :: (kon -> *) -> (Nat -> *) -> Atom kon -> * where
- mapNA :: (forall k. ki k -> kj k) -> (forall ix. IsNat ix => f ix -> g ix) -> NA ki f a -> NA kj g a
- mapNAM :: Monad m => (forall k. ki k -> m (kj k)) -> (forall ix. IsNat ix => f ix -> m (g ix)) -> NA ki f a -> m (NA kj g a)
- elimNA :: (forall k. ki k -> b) -> (forall k. IsNat k => phi k -> b) -> NA ki phi a -> b
- zipNA :: NA ki f a -> NA kj g a -> NA (ki :*: kj) (f :*: g) a
- eqNA :: (forall k. ki k -> ki k -> Bool) -> (forall ix. fam ix -> fam ix -> Bool) -> NA ki fam l -> NA ki fam l -> Bool
- newtype Rep (ki :: kon -> *) (phi :: Nat -> *) (code :: [[Atom kon]]) = Rep {}
- type PoA (ki :: kon -> *) (phi :: Nat -> *) = NP (NA ki phi)
- mapRep :: (forall ix. IsNat ix => f ix -> g ix) -> Rep ki f c -> Rep ki g c
- mapRepM :: Monad m => (forall ix. IsNat ix => f ix -> m (g ix)) -> Rep ki f c -> m (Rep ki g c)
- bimapRep :: (forall k. ki k -> kj k) -> (forall ix. IsNat ix => f ix -> g ix) -> Rep ki f c -> Rep kj g c
- bimapRepM :: Monad m => (forall k. ki k -> m (kj k)) -> (forall ix. IsNat ix => f ix -> m (g ix)) -> Rep ki f c -> m (Rep kj g c)
- zipRep :: MonadPlus m => Rep ki f c -> Rep kj g c -> m (Rep (ki :*: kj) (f :*: g) c)
- elimRepM :: Monad m => (forall k. ki k -> m a) -> (forall k. IsNat k => f k -> m a) -> ([a] -> m b) -> Rep ki f c -> m b
- elimRep :: (forall k. ki k -> a) -> (forall k. IsNat k => f k -> a) -> ([a] -> b) -> Rep ki f c -> b
- eqRep :: (forall k. ki k -> ki k -> Bool) -> (forall ix. fam ix -> fam ix -> Bool) -> Rep ki fam c -> Rep ki fam c -> Bool
- data Constr :: [k] -> Nat -> * where
- injNS :: Constr sum n -> PoA ki fam (Lkup n sum) -> NS (NP (NA ki fam)) sum
- inj :: Constr sum n -> PoA ki fam (Lkup n sum) -> Rep ki fam sum
- matchNS :: Constr sum c -> NS (NP (NA ki fam)) sum -> Maybe (PoA ki fam (Lkup c sum))
- match :: Constr sum c -> Rep ki fam sum -> Maybe (PoA ki fam (Lkup c sum))
- data View :: (kon -> *) -> (Nat -> *) -> [[Atom kon]] -> * where
- sop :: Rep ki fam sum -> View ki fam sum
- fromView :: View ki fam sum -> Rep ki fam sum
- data AnnFix (ki :: kon -> *) (codes :: [[[Atom kon]]]) (phi :: Nat -> *) (n :: Nat) = AnnFix (phi n) (Rep ki (AnnFix ki codes phi) (Lkup n codes))
- type Fix ki codes = AnnFix ki codes (Const ())
- pattern Fix :: forall kon (ki :: kon -> Type) (codes :: [[[Atom kon]]]) (n :: Nat). Rep ki (AnnFix ki codes (Const () :: Nat -> Type)) (Lkup n codes) -> AnnFix ki codes (Const () :: Nat -> Type) n
- unFix :: Fix ki codes ix -> Rep ki (Fix ki codes) (Lkup ix codes)
- cata :: IsNat ix => (forall iy. IsNat iy => Rep ki phi (Lkup iy codes) -> phi iy) -> Fix ki codes ix -> phi ix
- getAnn :: AnnFix ki codes ann ix -> ann ix
- annCata :: (forall iy. chi iy -> Rep ki phi (Lkup iy codes) -> phi iy) -> AnnFix ki codes chi ix -> phi ix
- forgetAnn :: AnnFix ki codes a ix -> Fix ki codes ix
- proxyFixIdx :: phi ix -> Proxy ix
- sNatFixIdx :: IsNat ix => phi ix -> SNat ix
- mapFixM :: Monad m => (forall k. ki k -> m (kj k)) -> Fix ki fam ix -> m (Fix kj fam ix)
- eqFix :: (forall k. ki k -> ki k -> Bool) -> Fix ki fam ix -> Fix ki fam ix -> Bool
- heqFixIx :: (IsNat ix, IsNat ix') => Fix ki fam ix -> Fix ki fam ix' -> Maybe (ix :~: ix')
Universe of Codes
We will use nested lists to represent the Sums-of-Products structure. The atoms, however, will be parametrized by a kind used to index what are the types that are opaque to the library.
Atoms can be either opaque types, kon, or
type variables, Nat.
Instances
data NA :: (kon -> *) -> (Nat -> *) -> Atom kon -> * where #
NA ki phi a provides an interpretation for an atom a,
using either ki or phi to interpret the type variable
or opaque type.
Instances
| TestEquality ki => TestEquality (NA ki phi :: Atom kon -> Type) # | |
Defined in Generics.MRSOP.Base.Universe | |
| (Eq1 phi, Eq1 ki) => Eq1 (NA ki phi :: Atom kon -> Type) # | |
| (Show1 phi, Show1 ki) => Show (PoA ki (AnnFix ki codes phi) xs) # | |
| (Show1 phi, Show1 ki) => Show (NA ki (AnnFix ki codes phi) a) # | |
Map, Elim and Zip
mapNA :: (forall k. ki k -> kj k) -> (forall ix. IsNat ix => f ix -> g ix) -> NA ki f a -> NA kj g a #
Maps a natural transformation over an atom interpretation
mapNAM :: Monad m => (forall k. ki k -> m (kj k)) -> (forall ix. IsNat ix => f ix -> m (g ix)) -> NA ki f a -> m (NA kj g a) #
Maps a monadic natural transformation over an atom interpretation
elimNA :: (forall k. ki k -> b) -> (forall k. IsNat k => phi k -> b) -> NA ki phi a -> b #
Eliminates an atom interpretation
eqNA :: (forall k. ki k -> ki k -> Bool) -> (forall ix. fam ix -> fam ix -> Bool) -> NA ki fam l -> NA ki fam l -> Bool #
Compares atoms provided we know how to compare the leaves, both recursive and constant.
Representation of Codes
Codes are represented using the Rep newtype,
which wraps an n-ary sum of n-ary products. Note it receives two
functors: ki and phi, to interpret opaque types and type variables
respectively.
newtype Rep (ki :: kon -> *) (phi :: Nat -> *) (code :: [[Atom kon]]) #
Representation of codes.
type PoA (ki :: kon -> *) (phi :: Nat -> *) = NP (NA ki phi) #
Product of Atoms is a handy synonym to have.
Map, Elim and Zip
Just like for NS, NP and NA, we provide
a couple convenient functions working over
a Rep. These are just the cannonical combination
of their homonym versions in NS, NP or NA.
mapRep :: (forall ix. IsNat ix => f ix -> g ix) -> Rep ki f c -> Rep ki g c #
Maps over a representation.
mapRepM :: Monad m => (forall ix. IsNat ix => f ix -> m (g ix)) -> Rep ki f c -> m (Rep ki g c) #
Maps a monadic function over a representation.
bimapRep :: (forall k. ki k -> kj k) -> (forall ix. IsNat ix => f ix -> g ix) -> Rep ki f c -> Rep kj g c #
Maps over both indexes of a representation.
bimapRepM :: Monad m => (forall k. ki k -> m (kj k)) -> (forall ix. IsNat ix => f ix -> m (g ix)) -> Rep ki f c -> m (Rep kj g c) #
Monadic version of bimapRep
zipRep :: MonadPlus m => Rep ki f c -> Rep kj g c -> m (Rep (ki :*: kj) (f :*: g) c) #
Zip two representations together, in case they are made with the same constructor.
zipRep (Here (NA_I x :* NP0)) (Here (NA_I y :* NP0)) = return $ Here (NA_I (x :*: y) :* NP0)
zipRep (Here (NA_I x :* NP0)) (There (Here ...)) = mzero
elimRepM :: Monad m => (forall k. ki k -> m a) -> (forall k. IsNat k => f k -> m a) -> ([a] -> m b) -> Rep ki f c -> m b #
elimRep :: (forall k. ki k -> a) -> (forall k. IsNat k => f k -> a) -> ([a] -> b) -> Rep ki f c -> b #
Pure eliminator.
eqRep :: (forall k. ki k -> ki k -> Bool) -> (forall ix. fam ix -> fam ix -> Bool) -> Rep ki fam c -> Rep ki fam c -> Bool #
SOP functionality
It is often more convenient to view a value of Rep
as a constructor and its fields, instead of having to
traverse the inner NS structure.
data Constr :: [k] -> Nat -> * where #
A value c :: Constr ks n specifies a position
in a type-level list. It is, in fact, isomorphic to Fin (length ks).
Instances
| TestEquality (Constr codes :: Nat -> Type) # | |
Defined in Generics.MRSOP.Base.Universe | |
| IsNat n => Show (Constr xs n) # | |
injNS :: Constr sum n -> PoA ki fam (Lkup n sum) -> NS (NP (NA ki fam)) sum #
We can define injections into an n-ary sum from
its Constructors
matchNS :: Constr sum c -> NS (NP (NA ki fam)) sum -> Maybe (PoA ki fam (Lkup c sum)) #
Inverse of injNS. Given some Constructor, see if Rep is of this constructor
match :: Constr sum c -> Rep ki fam sum -> Maybe (PoA ki fam (Lkup c sum)) #
Inverse of inj. Given some Constructor, see if Rep is of this constructor
data View :: (kon -> *) -> (Nat -> *) -> [[Atom kon]] -> * where #
Finally, we can view a sum-of-products as a constructor and a product-of-atoms.
Least Fixpoints
Finally we tie the recursive knot. Given an interpretation for the constant types, a family of sums-of-products and an index ix into such family, we take the least fixpoint of the representation of the code indexed by ix
data AnnFix (ki :: kon -> *) (codes :: [[[Atom kon]]]) (phi :: Nat -> *) (n :: Nat) #
Indexed least fixpoints
Annotated version of Fix. This is basically the Cofree datatype,
but for n-ary functors
Instances
| Eq1 ki => Eq1 (Fix ki codes :: Nat -> Type) # | |
| Eq1 ki => Eq (Fix ki codes ix) # | |
| (Show1 phi, Show1 ki) => Show (PoA ki (AnnFix ki codes phi) xs) # | |
| (Show1 phi, Show1 ki) => Show (Rep ki (AnnFix ki codes phi) xs) # | |
| (Show1 phi, Show1 ki) => Show (NA ki (AnnFix ki codes phi) a) # | |
| (Show1 phi, Show1 ki) => Show (AnnFix ki codes phi ix) # | |
pattern Fix :: forall kon (ki :: kon -> Type) (codes :: [[[Atom kon]]]) (n :: Nat). Rep ki (AnnFix ki codes (Const () :: Nat -> Type)) (Lkup n codes) -> AnnFix ki codes (Const () :: Nat -> Type) n #
cata :: IsNat ix => (forall iy. IsNat iy => Rep ki phi (Lkup iy codes) -> phi iy) -> Fix ki codes ix -> phi ix #
Catamorphism over fixpoints
annCata :: (forall iy. chi iy -> Rep ki phi (Lkup iy codes) -> phi iy) -> AnnFix ki codes chi ix -> phi ix #
proxyFixIdx :: phi ix -> Proxy ix #
Retrieves the index of a Fix
sNatFixIdx :: IsNat ix => phi ix -> SNat ix #
mapFixM :: Monad m => (forall k. ki k -> m (kj k)) -> Fix ki fam ix -> m (Fix kj fam ix) #
Maps over the values of opaque types within the fixpoint.