module Math.Spe
(
Spe
, (.+.), assemble, (.*.), (<*.), prod, ordProd, (.^), (<^), (><), o
, dx, pointed, ofSize, nonEmpty
, contact
, set, one, x, kBal, bal, par, kList, list, cyc, perm, kSubset, subset
) where
import Data.List
import Control.Applicative
infixl 6 .+.
infixl 7 .*.
infixl 7 <*.
infixr 8 .^
infixr 8 <^
type Spe a c = [a] -> [c]
type Bipartition a = [a] -> [([a], [a])]
(.+.) :: Spe a b -> Spe a c -> Spe a (Either b c)
(.+.) f g us = (Left <$> f us) ++ (Right <$> g us)
assemble :: [Spe a c] -> Spe a c
assemble fs us = fs >>= \f -> f us
kEndBy :: Bipartition a -> Int -> [a] -> [[[a]]]
kEndBy _ 0 [] = [[]]
kEndBy _ 0 _ = []
kEndBy h k us = h us >>= \(b,vs) -> (b:) <$> kEndBy h (k1) vs
bipartL :: Bipartition a
bipartL [] = [([], [])]
bipartL us@(u:ut) = ([], us) : [ (u:vs, zs) | (vs, zs) <- bipartL ut ]
bipartB :: Bipartition a
bipartB [] = [([], [])]
bipartB (u:us) = bipartB us >>= \(vs, zs) -> [(u:vs, zs), (vs, u:zs)]
mulBy :: Bipartition a -> Spe a b -> Spe a c -> Spe a (b,c)
mulBy h f g us = h us >>= \(vs,zs) -> (,) <$> f vs <*> g zs
(.*.) :: Spe a b -> Spe a c -> Spe a (b, c)
(.*.) = mulBy bipartB
(<*.) :: Spe a b -> Spe a c -> Spe a (b, c)
(<*.) = mulBy bipartL
prodBy :: Bipartition a -> [Spe a b] -> Spe a [b]
prodBy h fs us = zipWith ($) fs <$> kEndBy h (length fs) us >>= sequence
prod :: [Spe a b] -> Spe a [b]
prod = prodBy bipartB
ordProd :: [Spe a b] -> Spe a [b]
ordProd = prodBy bipartL
powerBy :: Bipartition a -> Spe a b -> Int -> Spe a [b]
powerBy h f k = prodBy h $ replicate k f
(.^) :: Spe a b -> Int -> Spe a [b]
(.^) = powerBy bipartB
(<^) :: Spe a b -> Int -> Spe a [b]
(<^) = powerBy bipartL
(><) :: Spe a b -> Spe a c -> Spe a (b,c)
(><) f g us = (,) <$> f us <*> g us
o :: Spe [a] b -> Spe a c -> Spe a (b, [c])
o f g us = par us >>= f >< mapM g
dx :: Spe (Maybe a) b -> Spe a b
dx f us = f $ Nothing : (Just <$> us)
pointed :: Spe a b -> Spe a (b, a)
pointed f = f >< id
isOfLength :: [a] -> Int -> Bool
[] `isOfLength` n = n == 0
(_:us) `isOfLength` n = n > 0 && us `isOfLength` (n1)
ofSize :: Spe a c -> Int -> Spe a c
(f `ofSize` n) us | us `isOfLength` n = f us
| otherwise = []
nonEmpty :: Spe a c -> Spe a c
nonEmpty _ [] = []
nonEmpty f us = f us
contact :: Ord b => Int -> Spe Int b -> Spe Int b -> Bool
contact n f g = and [ sort (f [1..k]) == sort (g [1..k]) | k<-[1..n] ]
set :: Spe a [a]
set = return
one :: Spe a ()
one = const [()] `ofSize` 0
x :: Spe a a
x = id `ofSize` 1
kBal :: Int -> Spe a [[a]]
kBal k = nonEmpty set .^ k
bal :: Spe a [[a]]
bal [] = [[]]
bal us = [ b:bs | (b, vs) <- init (bipartB us), bs <- bal vs ]
par :: Spe a [[a]]
par [] = [[]]
par (u:us) = [ (u:b) : bs | (b, vs) <- bipartB us, bs <- par vs ]
kList :: Int -> Spe a [a]
kList k = x .^ k
list :: Spe a [a]
list us = kList (length us) us
cyc :: Spe a [a]
cyc [] = []
cyc (u:us) = (u:) <$> list us
perm :: Spe a [[a]]
perm = map fst . (set `o` cyc)
kSubset :: Int -> Spe a [a]
kSubset k = map fst . (set `ofSize` k .*. set)
subset :: Spe a [a]
subset = map fst . bipartB