# PolyLib - a library of polytypic functions

The library contains a number of basic polytypic building blocks divided into a number of modules. Below is a first a list by module and then an alphabetical index of the library functions. A companion paper describes the polytypic functions in PolyLib, motivates their presence in the library, and gives a rationale for their design.

## Contents

[ Base BaseM ConstructorName Crush CrushFuns Compare Equal Flatten Propagate Sum Thread ThreadFuns Zip ZipVariants ]

### Base

```-- Basic polytypic functions
module Base(pmap,fmap2,cata,ana,hylo,para,(-*-),(-+-)) where
pmap :: Regular d => (a -> b) -> d a -> d b
polytypic fmap2 :: (a -> c) -> (b -> d) -> f a b -> f c d
cata :: Regular d => (FunctorOf d a b -> b) -> (d a -> b)
ana  :: Regular d => (b -> FunctorOf d a b) -> (b -> d a)
hylo :: Bifunctor f => (f a b -> b) -> (c -> f a c) -> c -> b
para :: Regular d => (d a -> FunctorOf d a b -> b) -> d a -> b
-- non-polytypic help functions
(-*-) :: (a -> c) -> (b -> d) -> (a,b) -> (c,d)
(-+-) :: (a -> c) -> (b -> d) -> Either a b -> Either c d
```

### BaseM

```-- Monadic polytypic functions
module BaseM(pmapM,fmap2M,cataM,anaM,hyloM,paraM,innM,outM,idM,(@@)) where
pmapM :: (Regular d, Monad m) => (a -> m b) -> d a -> m (d b)
polytypic fmap2M :: Monad m => (a -> m c) -> (b -> m d) -> f a b -> m (f c d)
summapM :: Monad b => (a -> b c) -> (d -> b e) -> Either a d -> b (Either c e)
prodmapM :: Monad b => (a -> b c) -> (d -> b e) -> (a,d) -> b (c,e)
cataM :: (Regular d,Monad m) => (FunctorOf d a b -> m b) -> (d a -> m b)
anaM  :: (Regular d,Monad m) => (b -> m (FunctorOf d a b)) -> (b -> m (d a))
hyloM :: (Bifunctor f,Monad m) => (f a b -> m b)-> (c -> m (f a c)) -> c -> m b
-- new names for symmetry
idM :: Monad m => a -> m a
innM :: (Regular d, Monad m) => FunctorOf d a (d a) -> m (d a)
outM :: (Regular d, Monad m) => d a -> m (FunctorOf d a (d a))
-- synonym
pmapMl :: (Regular d, Monad m) => (a -> m b) -> d a -> m (d b)
paraM :: (Regular d, Monad m) => (d a -> FunctorOf d a b -> m b) -> d a -> m b
-- Reverse order traversals
pmapMr :: (Regular d, Monad m) => (a -> m b) -> d a -> m (d b)
polytypic fmap2Mr :: Monad m => (a -> m c) -> (b -> m d) -> f a b -> m (f c d)
cataMr :: (Regular d,Monad m) => (FunctorOf d a b -> m b) -> (d a -> m b)
anaMr  :: (Regular d,Monad m) => (b -> m (FunctorOf d a b)) -> (b -> m (d a))
hyloMr :: (Bifunctor f,Monad m) => (f a b -> m b)-> (c -> m (f a c)) -> c -> m b
-- Monad operations (that are not in PolyPs prelude)
liftM :: Monad m => (a->b) -> m a -> m b
(@@) :: Monad m => (b->m c) -> (a->m b) -> (a->m c)
```

### ConstructorName

```module ConstructorName where
constructorName :: Regular d => d a -> String
fconstructorName :: Bifunctor f => f a b -> String -- is built in
constructorNames :: Regular d => d a -> [String]
fconstructorNames :: Bifunctor f => f a b -> [String]
constructorNamesAndArities :: Regular d => d a -> [(String,Int)]
fconstructorNamesAndArities :: Bifunctor f => f a b -> [(String,Int)]
mapFst :: (a->b) -> (a,c) -> (b,c)
constructors  :: Regular d => [d a]
polytypic fconstructors :: [f a b] =
polytypic fconstructorsAndArities :: [(f a b,Int)] =
polytypic fconstructorArity :: f a b -> Int =
constructor2Int :: Regular d => d a -> Int
polytypic fconstructor2Int :: f a b -> Int =
int2constructor :: Regular d => Int -> d a
int2fconstructor :: Bifunctor f => Int -> f a b
```

### Crush

```module Crush(crush,fcrush) where
crush :: Regular d => (a->a->a) -> a -> d a -> a
polytypic fcrush :: (a->a->a) -> a -> f a a -> a
```

### CrushFuns

```module CrushFuns(psum,prod,conc,pand,por,size,flatten,pall,pany,pelem) where
psum :: Regular d => d Int -> Int
prod :: Regular d => d Int -> Int
comp :: Regular d => d (a->a) -> (a->a)
conc :: Regular d => d [a] -> [a]
pand :: Regular d => d Bool -> Bool
por  :: Regular d => d Bool -> Bool
size    :: Regular d => d a -> Int
flatten :: Regular d => d a -> [a]
pall    :: Regular d => (a -> Bool) -> d a -> Bool
pany    :: Regular d => (a -> Bool) -> d a -> Bool
pelem   :: (Regular d,Eq a) => a -> d a -> Bool
flatten' :: Regular d => d a -> [a]
```

### Compare

```-- Ordering
module Compare(pcompare,pcompareBy) where
pcompare :: (Regular d, Ord a) => d a -> d a -> Ordering
pcompareBy :: Regular d => (a->a->Ordering) ->
d a -> d a -> Ordering
ordop :: Ordering -> Ordering -> Ordering
```

### Equal

```-- Equivalence
module Equal(pequal,fequal,peq) where
pequal :: Regular d => (a->b->Bool) -> d a -> d b -> Bool
peq :: (Regular d, Eq a) => d a -> d a -> Bool
polytypic fequal :: (a->b->Bool) -> (c->d->Bool) -> f a c -> f b d -> Bool
sumequal :: (a->b->Bool) -> (c->d->Bool) -> Either a c -> Either b d -> Bool
prodequal :: (a->b->Bool) -> (c->d->Bool) -> (a,c)->(b,d) -> Bool
```

### Flatten

```module Flatten(flatten,fflatten,fl_par,fl_rec,fl_all) where
flatten :: Regular d => d a -> [a]
polytypic fflatten :: f [a] [a] -> [a]
fl_par :: Bifunctor f => f a b -> [a]
fl_rec :: Bifunctor f => f a b -> [b]
fl_all :: Bifunctor f => f a a -> [a]
-- Help functions for lists
singleton :: a -> [a]
nil :: a -> [b]
-- alternative definitions
polytypic fl_rec :: f a b -> [b]
polytypic fl_par :: f a [a] -> [a]
```

### Propagate

```-- Propagate Maybe to the top
module Propagate(propagate,fprop,sumprop,prodprop,mapMaybe) where
propagate  :: Regular d => d (Maybe a) -> Maybe (d a)
polytypic fprop :: f (Maybe a) (Maybe b) -> Maybe (f a b)
sumprop  :: Either (Maybe a) (Maybe b) -> Maybe (Either a b)
prodprop :: (Maybe a,Maybe b) -> Maybe (a,b)
--  Maybe functions
mapMaybe :: (a->b) -> Maybe a -> Maybe b
```

### Sum

```-- Sum and size
module Sum(psum,fsum,size) where
psum :: Regular d => d Int -> Int
polytypic fsum :: f Int Int -> Int
size :: Regular d => d a -> Int
```

```-- Threads a monad through a value
thread :: (Regular d, Monad m) => d (m a) -> m (d a)
polytypic fthread :: Monad m => f (m a) (m b) -> m (f a b)
sumthread  :: Monad m => Either (m a) (m b) -> m (Either a b)
-- Monad operations (that are not in PolyPs prelude)
liftM :: Monad m => (a->b) -> m a -> m b
-- pmapM :: (Regular d, Monad m) => (a -> m b) -> d a -> m (d b)
-- fmap2M :: (Bifunctor f, Monad m) => (a -> m c) -> (b -> m d) ->
--                                     f a b -> m (f c d)
```

```module ThreadFuns(propagate,cross) where
cross :: Regular d => d [a] -> [d a]
propagate :: Regular d => d (Maybe a) -> Maybe (d a)
```

### Zip

```module Zip(pzip,fzip,pzipWith,pzipWith',(@@),resultM) where
-- Maybe could be replaced by any MonadZero
pzip :: Regular d => (d a,d b) -> Maybe (d (a,b))
punzip :: Regular d => d (a,b) -> (d a,d b)
pzipWith' :: Regular d => (FunctorOf d c e -> e) ->
((d a,d b)->e) ->
((a,b)->c) -> (d a,d b) -> e
pzipWith :: Regular d => ((a, b) -> Maybe c) -> (d a, d b) -> Maybe (d c)
funzip :: Bifunctor f => f (a,c) (b,d) -> (f a b,f c d)
polytypic fzip :: (f a b,f c d) -> Maybe (f (a,c) (b,d))
sumzip :: (Either a b,Either c d)-> Maybe (Either (a,c) (b,d))
prodzip :: ((a,b),(c,d)) -> Maybe ((a,c),(b,d))
resultM  :: a -> Maybe a
(@@)     :: (a -> Maybe b) -> (c -> Maybe a) -> c -> Maybe b
```

### ZipVariants

```module ZipVariants(pzipWith'',pzip'',pzip') where
pzipWith'' :: Regular d => ((a, b) -> c) -> (d a, d b) -> Maybe (d c)
pzip'' :: Regular d => (d a,d b) -> Maybe (d (a,b))
pzip' :: Regular d => (d a, d b) -> (d (a,b), Bool)
```

## Index

-*- -+- @@ @@ ana anaM anaMr cata cataM cataMr comp conc constructor2Int constructorName constructorNames constructorNamesAndArities constructors cross crush fconstructor2Int fconstructorArity fconstructorName fconstructorNames fconstructorNamesAndArities fconstructors fconstructorsAndArities fcrush fequal fflatten fl_all fl_par fl_rec flatten flatten flatten' fmap2 fmap2M fmap2Mr fprop fsum fthread funzip fzip hylo hyloM hyloMr idM innM int2constructor int2fconstructor liftM liftM mapFst mapMaybe nil ordop outM pall pand pany para paraM pcompare pcompareBy pelem peq pequal pmap pmapM pmapMl pmapMr por prod prodequal prodmapM prodprop prodthread prodzip propagate propagate psum psum punzip pzip pzip' pzip'' pzipWith pzipWith' pzipWith'' resultM singleton size size sumequal summapM sumprop sumthread sumzip thread