FiniteCategories-0.6.4.0: Finite categories and usual categorical constructions on them.
CopyrightGuillaume Sabbagh 2022
LicenseGPL-3
Maintainerguillaumesabbagh@protonmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Math.FiniteCategories.SafeCompositionGraph

Description

A SafeCompositionGraph is a CompositionGraph where infinite loops are prevented.

The readSCGFile function is the most important for ease of use.

Synopsis

Types for a morphism of safe composition graph

data SCGMorphism a b Source #

The type SCGMorphism is the type of SafeCompositionGraphs morphisms.

It is just like a CGMorphism, we also store the maximum number of cycles.

Constructors

SCGMorphism 

Fields

Instances

Instances details
(PrettyPrint a, PrettyPrint b, Eq a, Eq b) => PrettyPrint (SCGMorphism a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

pprint :: Int -> SCGMorphism a b -> String Source #

pprintWithIndentations :: Int -> Int -> String -> SCGMorphism a b -> String Source #

pprintIndent :: Int -> SCGMorphism a b -> String Source #

(Simplifiable a, Simplifiable b, Eq a, Eq b) => Simplifiable (SCGMorphism a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

simplify :: SCGMorphism a b -> SCGMorphism a b #

Generic (SCGMorphism a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Associated Types

type Rep (SCGMorphism a b) :: Type -> Type

Methods

from :: SCGMorphism a b -> Rep (SCGMorphism a b) x

to :: Rep (SCGMorphism a b) x -> SCGMorphism a b

(Show a, Show b) => Show (SCGMorphism a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

showsPrec :: Int -> SCGMorphism a b -> ShowS

show :: SCGMorphism a b -> String

showList :: [SCGMorphism a b] -> ShowS

(Eq a, Eq b) => Eq (SCGMorphism a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

(==) :: SCGMorphism a b -> SCGMorphism a b -> Bool

(/=) :: SCGMorphism a b -> SCGMorphism a b -> Bool

(Eq a, Eq b) => Morphism (SCGMorphism a b) a Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

(@) :: SCGMorphism a b -> SCGMorphism a b -> SCGMorphism a b Source #

(@?) :: SCGMorphism a b -> SCGMorphism a b -> Maybe (SCGMorphism a b) Source #

source :: SCGMorphism a b -> a Source #

target :: SCGMorphism a b -> a Source #

(Eq a, Eq b) => Category (SafeCompositionGraph a b) (SCGMorphism a b) a Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

(Eq a, Eq b) => FiniteCategory (SafeCompositionGraph a b) (SCGMorphism a b) a Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

ob :: SafeCompositionGraph a b -> Set a Source #

type Rep (SCGMorphism a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

type Rep (SCGMorphism a b) = D1 ('MetaData "SCGMorphism" "Math.FiniteCategories.SafeCompositionGraph" "FiniteCategories-0.6.4.0-inplace" 'False) (C1 ('MetaCons "SCGMorphism" 'PrefixI 'True) (S1 ('MetaSel ('Just "pathS") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Path a b)) :*: (S1 ('MetaSel ('Just "compositionLawS") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (CompositionLaw a b)) :*: S1 ('MetaSel ('Just "maxNbCycles") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int))))

Functions for morphism

getLabelS :: SCGMorphism a b -> Maybe b Source #

Return the label of a SafeCompositionGraph generator.

Safe composition graph

data SafeCompositionGraph a b Source #

A SafeCompositionGraph is a graph with a composition law such that the free category generated by the graph quotiented out by the composition law gives a FiniteCategory. It has a maximum number of composition for loops.

SafeCompositionGraph is private, use the smart constructors safeCompositionGraph or unsafeSafeCompositionGraph to instantiate one.

Instances

Instances details
(PrettyPrint a, PrettyPrint b, Eq a, Eq b) => PrettyPrint (SafeCompositionGraph a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

pprint :: Int -> SafeCompositionGraph a b -> String Source #

pprintWithIndentations :: Int -> Int -> String -> SafeCompositionGraph a b -> String Source #

pprintIndent :: Int -> SafeCompositionGraph a b -> String Source #

(Simplifiable a, Simplifiable b, Eq a, Eq b) => Simplifiable (SafeCompositionGraph a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Generic (SafeCompositionGraph a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Associated Types

type Rep (SafeCompositionGraph a b) :: Type -> Type

(Show a, Show b) => Show (SafeCompositionGraph a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

showsPrec :: Int -> SafeCompositionGraph a b -> ShowS

show :: SafeCompositionGraph a b -> String

showList :: [SafeCompositionGraph a b] -> ShowS

(Eq a, Eq b) => Eq (SafeCompositionGraph a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

(Eq a, Eq b) => Category (SafeCompositionGraph a b) (SCGMorphism a b) a Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

(Eq a, Eq b) => FiniteCategory (SafeCompositionGraph a b) (SCGMorphism a b) a Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

Methods

ob :: SafeCompositionGraph a b -> Set a Source #

type Rep (SafeCompositionGraph a b) Source # 
Instance details

Defined in Math.FiniteCategories.SafeCompositionGraph

type Rep (SafeCompositionGraph a b) = D1 ('MetaData "SafeCompositionGraph" "Math.FiniteCategories.SafeCompositionGraph" "FiniteCategories-0.6.4.0-inplace" 'False) (C1 ('MetaCons "SafeCompositionGraph" 'PrefixI 'True) (S1 ('MetaSel ('Just "supportS") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Graph a b)) :*: (S1 ('MetaSel ('Just "lawS") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (CompositionLaw a b)) :*: S1 ('MetaSel ('Just "maxCycles") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int))))

Getters

supportS :: SafeCompositionGraph a b -> Graph a b Source #

The generating graph of the safe composition graph.

lawS :: SafeCompositionGraph a b -> CompositionLaw a b Source #

The composition law of the safe composition graph.

maxCycles :: SafeCompositionGraph a b -> Int Source #

The maximum number of times a cycle can be composed with itself.

Construction

safeCompositionGraph :: (Eq a, Eq b) => Graph a b -> CompositionLaw a b -> Int -> Either (FiniteCategoryError (SCGMorphism a b) a) (SafeCompositionGraph a b) Source #

Smart constructor of SafeCompositionGraph.

If the SafeCompositionGraph constructed is valid, returns Right the composition graph, otherwise returns Left a FiniteCategoryError.

unsafeSafeCompositionGraph :: Graph a b -> CompositionLaw a b -> Int -> SafeCompositionGraph a b Source #

Unsafe constructor of SafeCompositionGraph for performance purposes. It does not check the structure of the SafeCompositionGraph.

Use this constructor only if the SafeCompositionGraph is necessarily well formed.

readSCGString :: String -> Either (FiniteCategoryError (SCGMorphism Text Text) Text) SCG Source #

Read a .scg string to create a SafeCompositionGraph.

A .scg string follows the following rules :

  1. Every character of a line following a "#" character are ignored.
  2. Each line defines either an object, a morphism or a composition law entry.
  3. The following strings are reserved : " -","-> "," = ", "<ID>", "<SRC>", "<SRC>", "<TGT>", "</TGT>", " => "
  4. To define an object, write a line containing its name.
  5. To define an arrow, the syntax "source_object -name_of_morphism-> target_object" is used, where "source_object", "target_object" and "name_of_morphism" should be replaced.
  6. 1. If an object mentionned in an arrow does not exist, it is created.
  7. 2. The spaces are important.
  8. To define a composition law entry, the syntax "source_object1 -name_of_first_morphism-> middle_object -name_of_second_morphism-> target_object1 = source_object2 -name_of_composite_morphism-> target_object2" is used, where "source_object1", "name_of_first_morphism", "middle_object", "name_of_second_morphism", "target_object1", "source_object2", "name_of_composite_morphism", "target_object2" should be replaced.
  9. 1 If an object mentionned does not exist, it is created.
  10. 2 If a morphism mentionned does not exist, it is created.
  11. 3 You can use the tag <ID/> in order to map a morphism to an identity.
  12. The first line of the should be a number, this number determines the maximum number of cycles.

unsafeReadSCGString :: String -> SCG Source #

Unsafe version of readSCGString which does not check the structure of the resulting SafeCompositionGraph.

readSCGFile :: String -> IO (Either (FiniteCategoryError (SCGMorphism Text Text) Text) SCG) Source #

Read a .scg file to create a SafeCompositionGraph.

A .scg file follows the following rules :

  1. Every character of a line following a "#" character are ignored.
  2. Each line defines either an object, a morphism or a composition law entry.
  3. The following strings are reserved : " -","-> "," = ", "<ID>", "<SRC>", "<SRC>", "<TGT>", "</TGT>", " => "
  4. To define an object, write a line containing its name.
  5. To define an arrow, the syntax "source_object -name_of_morphism-> target_object" is used, where "source_object", "target_object" and "name_of_morphism" should be replaced.
  6. 1. If an object mentionned in an arrow does not exist, it is created.
  7. 2. The spaces are important.
  8. To define a composition law entry, the syntax "source_object1 -name_of_first_morphism-> middle_object -name_of_second_morphism-> target_object1 = source_object2 -name_of_composite_morphism-> target_object2" is used, where "source_object1", "name_of_first_morphism", "middle_object", "name_of_second_morphism", "target_object1", "source_object2", "name_of_composite_morphism", "target_object2" should be replaced.
  9. 1 If an object mentionned does not exist, it is created.
  10. 2 If a morphism mentionned does not exist, it is created.
  11. 3 You can use the tag <ID/> in order to map a morphism to an identity.
  12. The first line of the should be a number, this number determines the maximum number of cycles.

unsafeReadSCGFile :: String -> IO SCG Source #

Unsafe version of readSCGFile which does not check the structure of the resulting SafeCompositionGraph.

Serialization

writeSCGString :: (PrettyPrint a, PrettyPrint b, Eq a, Eq b) => SafeCompositionGraph a b -> String Source #

Transform a composition graph into a string following the .scg convention.

writeSCGFile :: (PrettyPrint a, PrettyPrint b, Eq a, Eq b) => SafeCompositionGraph a b -> String -> IO () Source #

Saves a safe composition graph into a file located at a given path.

Construction of diagrams

unsafeReadSCGDString :: String -> SCGD Source #

Unsafe version of readCGDString which does not check the structure of the resulting Diagram.

readSCGDString :: String -> Either (DiagramError SCG (SCGMorphism Text Text) Text SCG (SCGMorphism Text Text) Text) SCGD Source #

Read a .scgd string and returns a diagram. A .scgd string obeys the following rules :

  1. There is a line "<SRC>" and a line "</SRC>".
  2. 1 Between these two lines, the source composition graph is defined as in a scg file.
  3. There is a line "<TGT>" and a line "</TGT>".
  4. 1 Between these two lines, the target composition graph is defined as in a scg file.
  5. Outside of the two previously described sections, you can declare the maps between objects and morphisms.
  6. 1 You map an object to another with the following syntax : "object1 => object2".
  7. 2 You map a morphism to another with the following syntax : "objSrc1 -arrowSrc1-> objSrc2 => objTgt1 -arrowTgt1-> objTgt2".
  8. You don't have to (and you shouldn't) specify maps from identities, nor maps from composite arrows.

unsafeReadSCGDFile :: String -> IO SCGD Source #

Unsafe version readSCGDFile which does not check the structure of the resulting Diagram.

readSCGDFile :: String -> IO (Either (DiagramError SCG (SCGMorphism Text Text) Text SCG (SCGMorphism Text Text) Text) SCGD) Source #

Read a .scgd file and returns a diagram. A .scgd file obeys the following rules :

  1. There is a line "<SRC>" and a line "</SRC>".
  2. 1 Between these two lines, the source composition graph is defined as in a scg file.
  3. There is a line "<TGT>" and a line "</TGT>".
  4. 1 Between these two lines, the target composition graph is defined as in a scg file.
  5. Outside of the two previously described sections, you can declare the maps between objects and morphisms.
  6. 1 You map an object to another with the following syntax : "object1 => object2".
  7. 2 You map a morphism to another with the following syntax : "objSrc1 -arrowSrc1-> objSrc2 => objTgt1 -arrowTgt1-> objTgt2".
  8. You don't have to (and you shouldn't) specify maps from identities, nor maps from composite arrows.

Serialization of diagrams

writeSCGDString :: (PrettyPrint a1, PrettyPrint b1, Eq a1, Eq b1, PrettyPrint a2, PrettyPrint b2, Eq a2, Eq b2) => Diagram (SafeCompositionGraph a1 b1) (SCGMorphism a1 b1) a1 (SafeCompositionGraph a2 b2) (SCGMorphism a2 b2) a2 -> String Source #

Transform a safe composition graph diagram into a string following the .scgd convention.

writeSCGDFile :: (PrettyPrint a1, PrettyPrint b1, Eq a1, Eq b1, PrettyPrint a2, PrettyPrint b2, Eq a2, Eq b2) => Diagram (SafeCompositionGraph a1 b1) (SCGMorphism a1 b1) a1 (SafeCompositionGraph a2 b2) (SCGMorphism a2 b2) a2 -> String -> IO () Source #

Saves a safe composition graph diagram into a file located at a given path.

Random safe composition graph

constructRandomSafeCompositionGraph Source #

Arguments

:: RandomGen g 
=> Int

Number of arrows of the random composition graph.

-> Int

Number of monoidification attempts, a bigger number will produce more morphisms that will compose but the function will be slower.

-> Int

Perseverance : how much we pursure an attempt far away to find a law that works, a bigger number will make the attemps more successful, but slower. (When in doubt put 4.)

-> g

Random generator.

-> Int

The maximum number of loops of the SafeCompositionGraph

-> (SafeCompositionGraph Int Int, g) 

Generates a random CompositionGraph and transforms it into a SafeCompositionGraph (see constructRandomCompositionGraph).

defaultConstructRandomSafeCompositionGraph :: RandomGen g => g -> (SafeCompositionGraph Int Int, g) Source #

Creates a random safe composition graph with default random values.

The number of arrows will be in the interval [1, 20].

The max number of loops is set to 100 as it is almost impossible to have a greater number of loops with monoidification attempts.

defaultConstructRandomSafeDiagram :: RandomGen g => g -> (Diagram (SafeCompositionGraph Int Int) (SCGMorphism Int Int) Int (SafeCompositionGraph Int Int) (SCGMorphism Int Int) Int, g) Source #

Constructs two random safe composition graphs and choose a random diagram between the two.

The max number of loops is set to 100 as it is almost impossible to have a greater number of loops with monoidification attempts.