{-| Module : FiniteCategories Description : Functions to convert all functor types. Copyright : Guillaume Sabbagh 2021 License : GPL-3 Maintainer : guillaumesabbagh@protonmail.com Stability : experimental Portability : portable Functions to convert all functor types. -} module Diagram.Conversion ( -- * Diagram to something diagramToFinFunctor, diagramToPartialFunctor, -- * FinFunctor to something finFunctorToDiagram, finFunctorToPartialFunctor, -- * PartialFunctor to something partialFunctorToDiagram, partialFunctorToFinFunctor ) where import FiniteCategory.FiniteCategory import Diagram.Diagram import Cat.FinCat import Cat.PartialFinCat import Utils.SetList import Utils.AssociationList -- | Converts a homogeneous `Diagram` to a `FinFunctor`. diagramToFinFunctor :: (FiniteCategory c m o, Morphism m o) => Diagram c m o c m o -> FinFunctor c m o diagramToFinFunctor Diagram{src=s,tgt=t,omap=om,mmap=fm} = FinFunctor{srcF=s,tgtF=t,omapF=om,mmapF=fm} -- | Converts a homogeneous `Diagram` to a `PartialFunctor` diagramToPartialFunctor :: (FiniteCategory c m o, Morphism m o) => Diagram c m o c m o -> PartialFunctor c m o diagramToPartialFunctor Diagram{src=s,tgt=t,omap=om,mmap=fm} = PartialFunctor{srcPF=s,tgtPF=t,omapPF=om,mmapPF=fm} -- | Converts a `FinFunctor` into a `Diagram`. -- -- A `FinFunctor` is a morphism of the `FinCat` category, it is a homogeneous FinFunctor. This functions casts it to a heterogeneous FinFunctor (i.e. a `Diagram`). finFunctorToDiagram :: FinFunctor c m o -> Diagram c m o c m o finFunctorToDiagram FinFunctor{srcF=s,tgtF=t,omapF=om,mmapF=fm} = Diagram {src=s,tgt=t,omap=om,mmap=fm} -- | Converts a total functor to a partial functor. finFunctorToPartialFunctor :: (FiniteCategory c m o, Morphism m o) => (FinFunctor c m o) -> (PartialFunctor c m o) finFunctorToPartialFunctor FinFunctor{srcF=s,tgtF=t,omapF=om,mmapF=fm} = PartialFunctor{srcPF=s,tgtPF=t,omapPF=om,mmapPF=fm} -- | Try to convert a `PartialFunctor` into a `Diagram` if it can (if it is total). partialFunctorToDiagram :: (FiniteCategory c m o, Morphism m o, Eq m, Eq o, Show o, Show m) => PartialFunctor c m o -> Maybe (Diagram c m o c m o) partialFunctorToDiagram x = finFunctorToDiagram <$> partialFunctorToFinFunctor x -- | Try to convert a partial functor to a total functor if it is possible. partialFunctorToFinFunctor :: (FiniteCategory c m o, Morphism m o, Eq m, Eq o, Show o, Show m) => (PartialFunctor c m o) -> Maybe (FinFunctor c m o) partialFunctorToFinFunctor PartialFunctor{srcPF=s,tgtPF=t,omapPF=om,mmapPF=fm} | not ((keys om) `doubleInclusion` (ob s)) = error $ (show $ ob s) ++"," ++ (show $ keys om)--Nothing | not ((keys fm) `doubleInclusion` (arrows s)) = error $ (show $ arrows s) ++"," ++ (show $ keys fm)--Nothing | otherwise = Just FinFunctor{srcF=s,tgtF=t,omapF=om,mmapF=fm}