module Internal.TwoCatOfCats where
import Prelude hiding (Functor)
import Control.Monad.Trans.Except (Except, runExcept, throwE)
import Data.Either (isRight)
data Category = Category
{ Category -> String
cat_id :: !String
, Category -> String
cat_displayString :: !String
} deriving (Category -> Category -> Bool
(Category -> Category -> Bool)
-> (Category -> Category -> Bool) -> Eq Category
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Category -> Category -> Bool
$c/= :: Category -> Category -> Bool
== :: Category -> Category -> Bool
$c== :: Category -> Category -> Bool
Eq)
instance Show Category where
show :: Category -> String
show Category
c = String
"Category "String -> ShowS
forall a. [a] -> [a] -> [a]
++ (Category -> String
cat_displayString Category
c)
data ZeroGlobelet = ZeroGlobelet
{ ZeroGlobelet -> Category
glob0_source :: !Category
, ZeroGlobelet -> Category
glob0_target :: !Category
} deriving (ZeroGlobelet -> ZeroGlobelet -> Bool
(ZeroGlobelet -> ZeroGlobelet -> Bool)
-> (ZeroGlobelet -> ZeroGlobelet -> Bool) -> Eq ZeroGlobelet
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ZeroGlobelet -> ZeroGlobelet -> Bool
$c/= :: ZeroGlobelet -> ZeroGlobelet -> Bool
== :: ZeroGlobelet -> ZeroGlobelet -> Bool
$c== :: ZeroGlobelet -> ZeroGlobelet -> Bool
Eq)
instance Show ZeroGlobelet where
show :: ZeroGlobelet -> String
show ZeroGlobelet
g = String
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ (Category -> String
forall a. Show a => a -> String
show (Category -> String) -> Category -> String
forall a b. (a -> b) -> a -> b
$ ZeroGlobelet -> Category
glob0_source ZeroGlobelet
g) String -> ShowS
forall a. [a] -> [a] -> [a]
++String
", " String -> ShowS
forall a. [a] -> [a] -> [a]
++ (Category -> String
forall a. Show a => a -> String
show (Category -> String) -> Category -> String
forall a b. (a -> b) -> a -> b
$ ZeroGlobelet -> Category
glob0_target ZeroGlobelet
g) String -> ShowS
forall a. [a] -> [a] -> [a]
++String
")"
data Functor =
Functor
{ Functor -> String
func_id :: !String
, Functor -> String
func_displayString :: !String
, Functor -> ZeroGlobelet
func_boundaryGlobelet :: !ZeroGlobelet
, Functor -> String
func_options :: !String
} |
CompositeFunctor
{ Functor -> ZeroGlobelet
cfs_boundaryGlobelet :: !ZeroGlobelet
, Functor -> [Functor]
cfs_functorList :: ![Functor]
} deriving Int -> Functor -> ShowS
[Functor] -> ShowS
Functor -> String
(Int -> Functor -> ShowS)
-> (Functor -> String) -> ([Functor] -> ShowS) -> Show Functor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Functor] -> ShowS
$cshowList :: [Functor] -> ShowS
show :: Functor -> String
$cshow :: Functor -> String
showsPrec :: Int -> Functor -> ShowS
$cshowsPrec :: Int -> Functor -> ShowS
Show
func_boundary :: Functor -> ZeroGlobelet
func_boundary :: Functor -> ZeroGlobelet
func_boundary (Functor String
_i String
_d ZeroGlobelet
bg String
_o) = ZeroGlobelet
bg
func_boundary (CompositeFunctor ZeroGlobelet
bg [Functor]
_fs) = ZeroGlobelet
bg
func_source :: Functor -> Category
func_source :: Functor -> Category
func_source = ZeroGlobelet -> Category
glob0_source(ZeroGlobelet -> Category)
-> (Functor -> ZeroGlobelet) -> Functor -> Category
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Functor -> ZeroGlobelet
func_boundary
func_target :: Functor -> Category
func_target :: Functor -> Category
func_target = ZeroGlobelet -> Category
glob0_target(ZeroGlobelet -> Category)
-> (Functor -> ZeroGlobelet) -> Functor -> Category
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Functor -> ZeroGlobelet
func_boundary
identityFunctor :: Category -> Functor
identityFunctor :: Category -> Functor
identityFunctor Category
c = ZeroGlobelet -> [Functor] -> Functor
CompositeFunctor (Category -> Category -> ZeroGlobelet
ZeroGlobelet Category
c Category
c) []
func_composable :: [Functor] -> Bool
func_composable :: [Functor] -> Bool
func_composable = Either FuncCompositionError Functor -> Bool
forall a b. Either a b -> Bool
isRight (Either FuncCompositionError Functor -> Bool)
-> ([Functor] -> Either FuncCompositionError Functor)
-> [Functor]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Except FuncCompositionError Functor
-> Either FuncCompositionError Functor
forall e a. Except e a -> Either e a
runExcept (Except FuncCompositionError Functor
-> Either FuncCompositionError Functor)
-> ([Functor] -> Except FuncCompositionError Functor)
-> [Functor]
-> Either FuncCompositionError Functor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Functor] -> Except FuncCompositionError Functor
func_compose_with_error
func_compose :: [Functor] -> Maybe Functor
func_compose :: [Functor] -> Maybe Functor
func_compose = ((FuncCompositionError -> Maybe Functor)
-> (Functor -> Maybe Functor)
-> Either FuncCompositionError Functor
-> Maybe Functor
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Functor -> FuncCompositionError -> Maybe Functor
forall a b. a -> b -> a
const Maybe Functor
forall a. Maybe a
Nothing) Functor -> Maybe Functor
forall a. a -> Maybe a
Just) (Either FuncCompositionError Functor -> Maybe Functor)
-> ([Functor] -> Either FuncCompositionError Functor)
-> [Functor]
-> Maybe Functor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Except FuncCompositionError Functor
-> Either FuncCompositionError Functor
forall e a. Except e a -> Either e a
runExcept (Except FuncCompositionError Functor
-> Either FuncCompositionError Functor)
-> ([Functor] -> Except FuncCompositionError Functor)
-> [Functor]
-> Either FuncCompositionError Functor
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Functor] -> Except FuncCompositionError Functor
func_compose_with_error
data FuncCompositionError = FuncCompositionError [Int]
func_compose_with_error :: [Functor] -> Except FuncCompositionError Functor
func_compose_with_error :: [Functor] -> Except FuncCompositionError Functor
func_compose_with_error [] = FuncCompositionError -> Except FuncCompositionError Functor
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (FuncCompositionError -> Except FuncCompositionError Functor)
-> FuncCompositionError -> Except FuncCompositionError Functor
forall a b. (a -> b) -> a -> b
$ [Int] -> FuncCompositionError
FuncCompositionError []
func_compose_with_error [Functor]
fs
| [(Bool, Int)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Bool, Int)]
errs = let bd :: ZeroGlobelet
bd = Category -> Category -> ZeroGlobelet
ZeroGlobelet (Functor -> Category
func_source (Functor -> Category) -> Functor -> Category
forall a b. (a -> b) -> a -> b
$ [Functor] -> Functor
forall a. [a] -> a
head [Functor]
fs) (Functor -> Category
func_target (Functor -> Category) -> Functor -> Category
forall a b. (a -> b) -> a -> b
$ [Functor] -> Functor
forall a. [a] -> a
last [Functor]
fs)
sfs :: [Functor]
sfs = (Functor -> [Functor]) -> [Functor] -> [Functor]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Functor -> [Functor]
func_to_single_list [Functor]
fs
in Functor -> Except FuncCompositionError Functor
forall (m :: * -> *) a. Monad m => a -> m a
return (Functor -> Except FuncCompositionError Functor)
-> Functor -> Except FuncCompositionError Functor
forall a b. (a -> b) -> a -> b
$ ZeroGlobelet -> [Functor] -> Functor
CompositeFunctor ZeroGlobelet
bd [Functor]
sfs
| Bool
otherwise = FuncCompositionError -> Except FuncCompositionError Functor
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (FuncCompositionError -> Except FuncCompositionError Functor)
-> FuncCompositionError -> Except FuncCompositionError Functor
forall a b. (a -> b) -> a -> b
$ [Int] -> FuncCompositionError
FuncCompositionError ([Int] -> FuncCompositionError) -> [Int] -> FuncCompositionError
forall a b. (a -> b) -> a -> b
$ (Bool, Int) -> Int
forall a b. (a, b) -> b
snd ((Bool, Int) -> Int) -> [(Bool, Int)] -> [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Bool, Int)]
errs
where
composable :: Functor -> Functor -> Bool
composable Functor
f1 Functor
f2 = Functor -> Category
func_target Functor
f1 Category -> Category -> Bool
forall a. Eq a => a -> a -> Bool
== Functor -> Category
func_source Functor
f2
comp_list :: [Functor] -> [Bool]
comp_list [Functor]
fs = (Functor -> Functor -> Bool) -> [Functor] -> [Functor] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Functor -> Functor -> Bool
composable [Functor]
fs ([Functor] -> [Functor]
forall a. [a] -> [a]
tail [Functor]
fs)
errs :: [(Bool, Int)]
errs = ((Bool, Int) -> Bool) -> [(Bool, Int)] -> [(Bool, Int)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Bool
x,Int
y) -> Bool -> Bool
not Bool
x) ([(Bool, Int)] -> [(Bool, Int)]) -> [(Bool, Int)] -> [(Bool, Int)]
forall a b. (a -> b) -> a -> b
$ [Bool] -> [Int] -> [(Bool, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip ([Functor] -> [Bool]
comp_list [Functor]
fs) [Int
0..]
func_to_single_composition :: Functor -> Functor
func_to_single_composition :: Functor -> Functor
func_to_single_composition Functor
f = ZeroGlobelet -> [Functor] -> Functor
CompositeFunctor (Functor -> ZeroGlobelet
func_boundary Functor
f) (Functor -> [Functor]
func_to_single_list Functor
f)
func_to_single_list :: Functor -> [Functor]
func_to_single_list :: Functor -> [Functor]
func_to_single_list (Functor String
i String
ds ZeroGlobelet
bg String
o) = [(String -> String -> ZeroGlobelet -> String -> Functor
Functor String
i String
ds ZeroGlobelet
bg String
o)]
func_to_single_list (CompositeFunctor ZeroGlobelet
_bg [Functor]
fl) = (Functor -> [Functor]) -> [Functor] -> [Functor]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Functor -> [Functor]
func_to_single_list [Functor]
fl
func_reduced_length :: Functor -> Int
func_reduced_length :: Functor -> Int
func_reduced_length = [Functor] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Functor] -> Int) -> (Functor -> [Functor]) -> Functor -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Functor -> [Functor]
func_to_single_list
instance Eq Functor where
Functor
f1 == :: Functor -> Functor -> Bool
== Functor
f2 = (Functor -> ZeroGlobelet
func_boundary Functor
f1)ZeroGlobelet -> ZeroGlobelet -> Bool
forall a. Eq a => a -> a -> Bool
==(Functor -> ZeroGlobelet
func_boundary Functor
f2) Bool -> Bool -> Bool
&& ([Functor] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Functor]
list1)Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==([Functor] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Functor]
list2)
Bool -> Bool -> Bool
&& ((Bool -> Bool -> Bool) -> Bool -> [Bool] -> Bool
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Bool -> Bool -> Bool
(&&) Bool
True ([Bool] -> Bool) -> [Bool] -> Bool
forall a b. (a -> b) -> a -> b
$ (Functor -> Functor -> Bool) -> [Functor] -> [Functor] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Functor -> Functor -> Bool
basicCompare [Functor]
list1 [Functor]
list2)
where
list1 :: [Functor]
list1 = Functor -> [Functor]
func_to_single_list Functor
f1
list2 :: [Functor]
list2 = Functor -> [Functor]
func_to_single_list Functor
f2
basicCompare :: Functor -> Functor -> Bool
basicCompare (Functor String
i1 String
_d1 ZeroGlobelet
bg1 String
_o1) (Functor String
i2 String
_d2 ZeroGlobelet
bg2 String
_o2)
= (String
i1 String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
i2) Bool -> Bool -> Bool
&& (ZeroGlobelet
bg1 ZeroGlobelet -> ZeroGlobelet -> Bool
forall a. Eq a => a -> a -> Bool
== ZeroGlobelet
bg2)
basicCompare Functor
_ Functor
_ = String -> Bool
forall a. HasCallStack => String -> a
error (String -> Bool) -> String -> Bool
forall a b. (a -> b) -> a -> b
$ String
"func_to_single_list should be a list of basic functors "
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"so this shouldn't be matched."
is_identity_func :: Functor -> Bool
is_identity_func :: Functor -> Bool
is_identity_func Functor
f = Functor -> Int
func_reduced_length Functor
f Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
is_basic_func :: Functor -> Bool
is_basic_func :: Functor -> Bool
is_basic_func (Functor {}) = Bool
True
is_basic_func Functor
_ = Bool
False
data OneGlobelet = OneGlobelet
{ OneGlobelet -> Functor
glob1_source :: !Functor
, OneGlobelet -> Functor
glob1_target :: !Functor
} deriving Int -> OneGlobelet -> ShowS
[OneGlobelet] -> ShowS
OneGlobelet -> String
(Int -> OneGlobelet -> ShowS)
-> (OneGlobelet -> String)
-> ([OneGlobelet] -> ShowS)
-> Show OneGlobelet
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OneGlobelet] -> ShowS
$cshowList :: [OneGlobelet] -> ShowS
show :: OneGlobelet -> String
$cshow :: OneGlobelet -> String
showsPrec :: Int -> OneGlobelet -> ShowS
$cshowsPrec :: Int -> OneGlobelet -> ShowS
Show
funcs_globeletable :: Functor -> Functor -> Bool
funcs_globeletable :: Functor -> Functor -> Bool
funcs_globeletable Functor
north Functor
south = Functor -> ZeroGlobelet
func_boundary Functor
north ZeroGlobelet -> ZeroGlobelet -> Bool
forall a. Eq a => a -> a -> Bool
== Functor -> ZeroGlobelet
func_boundary Functor
south
funcs_to_globelet :: Functor -> Functor -> Maybe OneGlobelet
funcs_to_globelet :: Functor -> Functor -> Maybe OneGlobelet
funcs_to_globelet Functor
north Functor
south
| Functor -> Functor -> Bool
funcs_globeletable Functor
north Functor
south = OneGlobelet -> Maybe OneGlobelet
forall a. a -> Maybe a
Just (Functor -> Functor -> OneGlobelet
OneGlobelet Functor
north Functor
south)
| Bool
otherwise = Maybe OneGlobelet
forall a. Maybe a
Nothing
glob1_pos :: OneGlobelet -> Category
glob1_pos :: OneGlobelet -> Category
glob1_pos = Functor -> Category
func_source(Functor -> Category)
-> (OneGlobelet -> Functor) -> OneGlobelet -> Category
forall b c a. (b -> c) -> (a -> b) -> a -> c
.OneGlobelet -> Functor
glob1_source
glob1_neg :: OneGlobelet -> Category
glob1_neg :: OneGlobelet -> Category
glob1_neg = Functor -> Category
func_target(Functor -> Category)
-> (OneGlobelet -> Functor) -> OneGlobelet -> Category
forall b c a. (b -> c) -> (a -> b) -> a -> c
.OneGlobelet -> Functor
glob1_target
data NaturalTransformation =
NaturalTransformation
{ NaturalTransformation -> String
nt_id :: !String
, NaturalTransformation -> String
nt_displayString :: !String
, NaturalTransformation -> String
nt_shapeString :: !String
, NaturalTransformation -> OneGlobelet
nt_boundaryGlobelet :: !OneGlobelet
, NaturalTransformation -> String
nt_options :: !String
} |
NatTransHorizontalComposite
{ NaturalTransformation -> OneGlobelet
nt_horz_comp_boundaryGlobelet :: !OneGlobelet
, NaturalTransformation -> [NaturalTransformation]
nt_horz_comp_list :: ![NaturalTransformation]
} |
NatTransVerticalComposite
{ NaturalTransformation -> OneGlobelet
nt_vert_comp_boundaryGlobelet :: !OneGlobelet
, NaturalTransformation -> [NaturalTransformation]
nt_vert_comp_list :: ![NaturalTransformation]
} deriving Int -> NaturalTransformation -> ShowS
[NaturalTransformation] -> ShowS
NaturalTransformation -> String
(Int -> NaturalTransformation -> ShowS)
-> (NaturalTransformation -> String)
-> ([NaturalTransformation] -> ShowS)
-> Show NaturalTransformation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NaturalTransformation] -> ShowS
$cshowList :: [NaturalTransformation] -> ShowS
show :: NaturalTransformation -> String
$cshow :: NaturalTransformation -> String
showsPrec :: Int -> NaturalTransformation -> ShowS
$cshowsPrec :: Int -> NaturalTransformation -> ShowS
Show
nat_boundary :: NaturalTransformation -> OneGlobelet
nat_boundary :: NaturalTransformation -> OneGlobelet
nat_boundary (NaturalTransformation String
_i String
_ds String
_ss OneGlobelet
bg String
_o) = OneGlobelet
bg
nat_boundary (NatTransHorizontalComposite OneGlobelet
bg [NaturalTransformation]
_nl) = OneGlobelet
bg
nat_boundary (NatTransVerticalComposite OneGlobelet
bg [NaturalTransformation]
_nl) = OneGlobelet
bg
nat_source :: NaturalTransformation -> Functor
nat_source :: NaturalTransformation -> Functor
nat_source = OneGlobelet -> Functor
glob1_source(OneGlobelet -> Functor)
-> (NaturalTransformation -> OneGlobelet)
-> NaturalTransformation
-> Functor
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NaturalTransformation -> OneGlobelet
nat_boundary
nat_target :: NaturalTransformation -> Functor
nat_target :: NaturalTransformation -> Functor
nat_target = OneGlobelet -> Functor
glob1_target(OneGlobelet -> Functor)
-> (NaturalTransformation -> OneGlobelet)
-> NaturalTransformation
-> Functor
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NaturalTransformation -> OneGlobelet
nat_boundary
nat_pos :: NaturalTransformation -> Category
nat_pos :: NaturalTransformation -> Category
nat_pos = OneGlobelet -> Category
glob1_pos(OneGlobelet -> Category)
-> (NaturalTransformation -> OneGlobelet)
-> NaturalTransformation
-> Category
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NaturalTransformation -> OneGlobelet
nat_boundary
nat_neg :: NaturalTransformation -> Category
nat_neg :: NaturalTransformation -> Category
nat_neg = OneGlobelet -> Category
glob1_neg(OneGlobelet -> Category)
-> (NaturalTransformation -> OneGlobelet)
-> NaturalTransformation
-> Category
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NaturalTransformation -> OneGlobelet
nat_boundary
identityNaturalTransformation :: Functor -> NaturalTransformation
identityNaturalTransformation :: Functor -> NaturalTransformation
identityNaturalTransformation Functor
f = OneGlobelet -> [NaturalTransformation] -> NaturalTransformation
NatTransVerticalComposite (Functor -> Functor -> OneGlobelet
OneGlobelet Functor
f Functor
f) []
nat_horz_composable :: [NaturalTransformation] -> Bool
nat_horz_composable :: [NaturalTransformation] -> Bool
nat_horz_composable = [Functor] -> Bool
func_composable ([Functor] -> Bool)
-> ([NaturalTransformation] -> [Functor])
-> [NaturalTransformation]
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((NaturalTransformation -> Functor)
-> [NaturalTransformation] -> [Functor]
forall a b. (a -> b) -> [a] -> [b]
map NaturalTransformation -> Functor
nat_source)
nat_horz_compose :: [NaturalTransformation] -> Maybe NaturalTransformation
nat_horz_compose :: [NaturalTransformation] -> Maybe NaturalTransformation
nat_horz_compose = ((NatHorzCompositionError -> Maybe NaturalTransformation)
-> (NaturalTransformation -> Maybe NaturalTransformation)
-> Either NatHorzCompositionError NaturalTransformation
-> Maybe NaturalTransformation
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe NaturalTransformation
-> NatHorzCompositionError -> Maybe NaturalTransformation
forall a b. a -> b -> a
const Maybe NaturalTransformation
forall a. Maybe a
Nothing) NaturalTransformation -> Maybe NaturalTransformation
forall a. a -> Maybe a
Just) (Either NatHorzCompositionError NaturalTransformation
-> Maybe NaturalTransformation)
-> ([NaturalTransformation]
-> Either NatHorzCompositionError NaturalTransformation)
-> [NaturalTransformation]
-> Maybe NaturalTransformation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Except NatHorzCompositionError NaturalTransformation
-> Either NatHorzCompositionError NaturalTransformation
forall e a. Except e a -> Either e a
runExcept (Except NatHorzCompositionError NaturalTransformation
-> Either NatHorzCompositionError NaturalTransformation)
-> ([NaturalTransformation]
-> Except NatHorzCompositionError NaturalTransformation)
-> [NaturalTransformation]
-> Either NatHorzCompositionError NaturalTransformation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NaturalTransformation]
-> Except NatHorzCompositionError NaturalTransformation
nat_horz_compose_with_error
data NatHorzCompositionError = NatHorzCompositionError [Int]
nat_horz_compose_with_error :: [NaturalTransformation] -> Except NatHorzCompositionError NaturalTransformation
nat_horz_compose_with_error :: [NaturalTransformation]
-> Except NatHorzCompositionError NaturalTransformation
nat_horz_compose_with_error [] = NatHorzCompositionError
-> Except NatHorzCompositionError NaturalTransformation
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NatHorzCompositionError
-> Except NatHorzCompositionError NaturalTransformation)
-> NatHorzCompositionError
-> Except NatHorzCompositionError NaturalTransformation
forall a b. (a -> b) -> a -> b
$ [Int] -> NatHorzCompositionError
NatHorzCompositionError []
nat_horz_compose_with_error [NaturalTransformation]
nats
| [(Bool, Int)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Bool, Int)]
errs = let pos :: Category
pos = NaturalTransformation -> Category
nat_pos (NaturalTransformation -> Category)
-> NaturalTransformation -> Category
forall a b. (a -> b) -> a -> b
$ [NaturalTransformation] -> NaturalTransformation
forall a. [a] -> a
head [NaturalTransformation]
nats
neg :: Category
neg = NaturalTransformation -> Category
nat_neg (NaturalTransformation -> Category)
-> NaturalTransformation -> Category
forall a b. (a -> b) -> a -> b
$ [NaturalTransformation] -> NaturalTransformation
forall a. [a] -> a
last [NaturalTransformation]
nats
podes :: ZeroGlobelet
podes = Category -> Category -> ZeroGlobelet
ZeroGlobelet Category
pos Category
neg
bd_north :: Functor
bd_north = ZeroGlobelet -> [Functor] -> Functor
CompositeFunctor ZeroGlobelet
podes ([Functor] -> Functor) -> [Functor] -> Functor
forall a b. (a -> b) -> a -> b
$ (NaturalTransformation -> [Functor])
-> [NaturalTransformation] -> [Functor]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Functor -> [Functor]
func_to_single_list(Functor -> [Functor])
-> (NaturalTransformation -> Functor)
-> NaturalTransformation
-> [Functor]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NaturalTransformation -> Functor
nat_source) [NaturalTransformation]
nats
bd_south :: Functor
bd_south = ZeroGlobelet -> [Functor] -> Functor
CompositeFunctor ZeroGlobelet
podes ([Functor] -> Functor) -> [Functor] -> Functor
forall a b. (a -> b) -> a -> b
$ (NaturalTransformation -> [Functor])
-> [NaturalTransformation] -> [Functor]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Functor -> [Functor]
func_to_single_list(Functor -> [Functor])
-> (NaturalTransformation -> Functor)
-> NaturalTransformation
-> [Functor]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.NaturalTransformation -> Functor
nat_target) [NaturalTransformation]
nats
bd :: OneGlobelet
bd = Functor -> Functor -> OneGlobelet
OneGlobelet Functor
bd_north Functor
bd_south
in NaturalTransformation
-> Except NatHorzCompositionError NaturalTransformation
forall (m :: * -> *) a. Monad m => a -> m a
return (OneGlobelet -> [NaturalTransformation] -> NaturalTransformation
NatTransHorizontalComposite OneGlobelet
bd [NaturalTransformation]
nats)
| Bool
otherwise = NatHorzCompositionError
-> Except NatHorzCompositionError NaturalTransformation
forall (m :: * -> *) e a. Monad m => e -> ExceptT e m a
throwE (NatHorzCompositionError
-> Except NatHorzCompositionError NaturalTransformation)
-> NatHorzCompositionError
-> Except NatHorzCompositionError NaturalTransformation
forall a b. (a -> b) -> a -> b
$ [Int] -> NatHorzCompositionError
NatHorzCompositionError ([Int] -> NatHorzCompositionError)
-> [Int] -> NatHorzCompositionError
forall a b. (a -> b) -> a -> b
$ (Bool, Int) -> Int
forall a b. (a, b) -> b
snd ((Bool, Int) -> Int) -> [(Bool, Int)] -> [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Bool, Int)]
errs
where
h_composable :: NaturalTransformation -> NaturalTransformation -> Bool
h_composable NaturalTransformation
n1 NaturalTransformation
n2 = (NaturalTransformation -> Category
nat_neg NaturalTransformation
n1) Category -> Category -> Bool
forall a. Eq a => a -> a -> Bool
== (NaturalTransformation -> Category
nat_pos NaturalTransformation
n2)
comp_list :: [NaturalTransformation] -> [Bool]
comp_list [NaturalTransformation]
ns = (NaturalTransformation -> NaturalTransformation -> Bool)
-> [NaturalTransformation] -> [NaturalTransformation] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith NaturalTransformation -> NaturalTransformation -> Bool
h_composable [NaturalTransformation]
nats ([NaturalTransformation] -> [NaturalTransformation]
forall a. [a] -> [a]
tail [NaturalTransformation]
ns)
errs :: [(Bool, Int)]
errs = ((Bool, Int) -> Bool) -> [(Bool, Int)] -> [(Bool, Int)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Bool
x,Int
y) -> Bool -> Bool
not Bool
x) ([(Bool, Int)] -> [(Bool, Int)]) -> [(Bool, Int)] -> [(Bool, Int)]
forall a b. (a -> b) -> a -> b
$ [Bool] -> [Int] -> [(Bool, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip ([NaturalTransformation] -> [Bool]
comp_list [NaturalTransformation]
nats) [Int
0..]
nat_vert_composable :: [NaturalTransformation] -> Bool
nat_vert_composable :: [NaturalTransformation] -> Bool
nat_vert_composable [] = Bool
False
nat_vert_composable [NaturalTransformation]
ns = (Bool -> Bool -> Bool) -> Bool -> [Bool] -> Bool
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Bool -> Bool -> Bool
(&&) Bool
True ([Bool] -> Bool) -> [Bool] -> Bool
forall a b. (a -> b) -> a -> b
$ (NaturalTransformation -> NaturalTransformation -> Bool)
-> [NaturalTransformation] -> [NaturalTransformation] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith NaturalTransformation -> NaturalTransformation -> Bool
composable [NaturalTransformation]
ns ([NaturalTransformation] -> [NaturalTransformation]
forall a. [a] -> [a]
tail [NaturalTransformation]
ns)
where
composable :: NaturalTransformation -> NaturalTransformation -> Bool
composable NaturalTransformation
n1 NaturalTransformation
n2 = (NaturalTransformation -> Functor
nat_target NaturalTransformation
n1) Functor -> Functor -> Bool
forall a. Eq a => a -> a -> Bool
== (NaturalTransformation -> Functor
nat_source NaturalTransformation
n2)
nat_vert_compose :: [NaturalTransformation] -> Maybe NaturalTransformation
nat_vert_compose :: [NaturalTransformation] -> Maybe NaturalTransformation
nat_vert_compose [NaturalTransformation]
ns
| [NaturalTransformation] -> Bool
nat_vert_composable [NaturalTransformation]
ns = let bd :: OneGlobelet
bd = Functor -> Functor -> OneGlobelet
OneGlobelet (NaturalTransformation -> Functor
nat_source (NaturalTransformation -> Functor)
-> NaturalTransformation -> Functor
forall a b. (a -> b) -> a -> b
$ [NaturalTransformation] -> NaturalTransformation
forall a. [a] -> a
head [NaturalTransformation]
ns) (NaturalTransformation -> Functor
nat_target (NaturalTransformation -> Functor)
-> NaturalTransformation -> Functor
forall a b. (a -> b) -> a -> b
$ [NaturalTransformation] -> NaturalTransformation
forall a. [a] -> a
last [NaturalTransformation]
ns)
in NaturalTransformation -> Maybe NaturalTransformation
forall a. a -> Maybe a
Just (OneGlobelet -> [NaturalTransformation] -> NaturalTransformation
NatTransVerticalComposite OneGlobelet
bd [NaturalTransformation]
ns)
| Bool
otherwise = Maybe NaturalTransformation
forall a. Maybe a
Nothing
nat_source_length :: NaturalTransformation -> Int
nat_source_length :: NaturalTransformation -> Int
nat_source_length = Functor -> Int
func_reduced_length (Functor -> Int)
-> (NaturalTransformation -> Functor)
-> NaturalTransformation
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NaturalTransformation -> Functor
nat_source
nat_target_length :: NaturalTransformation -> Int
nat_target_length :: NaturalTransformation -> Int
nat_target_length = Functor -> Int
func_reduced_length (Functor -> Int)
-> (NaturalTransformation -> Functor)
-> NaturalTransformation
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NaturalTransformation -> Functor
nat_target
is_basic_nt :: NaturalTransformation -> Bool
is_basic_nt :: NaturalTransformation -> Bool
is_basic_nt (NaturalTransformation {}) = Bool
True
is_basic_nt NaturalTransformation
_ = Bool
False
is_identity_nt :: NaturalTransformation -> Bool
is_identity_nt :: NaturalTransformation -> Bool
is_identity_nt (NatTransVerticalComposite OneGlobelet
_ []) = Bool
True
is_identity_nt NaturalTransformation
_ = Bool
False