Skip to content

Commit

Permalink
Merge pull request #261 from mohamed-barakat/Digraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-barakat authored Jun 25, 2023
2 parents 755e41e + b06e12a commit 20fc21c
Show file tree
Hide file tree
Showing 12 changed files with 422 additions and 385 deletions.
6 changes: 3 additions & 3 deletions LazyCategories/PackageInfo.g
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SetPackageInfo( rec(

PackageName := "LazyCategories",
Subtitle := "Construct an equivalent lazy category out of a CAP category",
Version := "2023.06-02",
Version := "2023.06-03",
Date := ~.Version{[ 1 .. 10 ]},
Date := (function ( ) if IsBound( GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE ) then return GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE; else return Concatenation( ~.Version{[ 1 .. 4 ]}, "-", ~.Version{[ 6, 7 ]}, "-01" ); fi; end)( ),
License := "GPL-2.0-or-later",
Expand Down Expand Up @@ -76,10 +76,10 @@ Dependencies := rec(
[ "MonoidalCategories", ">= 2022.06-03" ],
[ "CategoryConstructor", ">= 2022.11-10" ],
[ "Toposes", ">= 2022.04-19" ],
[ "Digraphs", ">= 0.12.1" ],
],
SuggestedOtherPackages := [
],
[ "Digraphs", ">= 1.3.1" ],
],
ExternalConditions := [ ],
),

Expand Down
168 changes: 0 additions & 168 deletions LazyCategories/gap/LazyCategory.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1142,174 +1142,6 @@ InstallGlobalFunction( PositionsOfChildrenOfALazyCell,

end );

##
InstallMethod( ListOfEvaluationNodes,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )
local nodes, queue, add_to_nodes, add_to_queue, children, D;

nodes := [ ];

queue := [ c ];

add_to_nodes :=
function( a )
if PositionProperty( nodes, b -> AreEqualForLazyCells( a, b ) ) = fail then
Add( nodes, a );
fi;
end;

add_to_queue :=
function( a )
if PositionProperty( Concatenation( nodes, queue ), b -> AreEqualForLazyCells( a, b ) ) = fail then
Add( queue, a );
fi;
end;

while not IsEmpty( queue ) do

c := Remove( queue, 1 );

add_to_nodes( c );

if HasGenesisOfCellArguments( c ) then

children := GenesisOfCellArguments( c );

children := Flat( children );

children := Filtered( children, IsLazyCapCategoryCell );

Perform( children, add_to_queue );

fi;

od;

nodes := Reversed( nodes );

D := List( nodes, node -> PositionsOfChildrenOfALazyCell( node, nodes ) );

D := Digraph( D );

return nodes{DigraphTopologicalSort( D )};

end );

##
InstallMethod( DigraphOfEvaluation,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )
local nodes, D;

nodes := ListOfEvaluationNodes( c );

D := List( nodes, node -> PositionsOfChildrenOfALazyCell( node, nodes ) );

D := Digraph( D );

D := DigraphReverse( D );

D!.list_of_children := [ ];

Perform( [ 1 .. Length( nodes ) ],
function( i )
local node, l, ints;

node := nodes[i];

if HasGenesisOfCellOperation( node ) then
l := GenesisOfCellOperation( node );
l := CAP_INTERNAL_COMPACT_NAME_OF_CATEGORICAL_OPERATION( l );
ints := Filtered( GenesisOfCellArguments( node ), IsInt );
if not IsEmpty( ints ) then
l := Concatenation( l, "( ", JoinStringsWithSeparator( ints, ", " ), " )" );
fi;
elif IsCapCategoryCell( node ) then
l := "primitive";
if IsCapCategoryObject( node ) then
l := Concatenation( l, "\n", "object" );
elif IsCapCategoryMorphism( node ) then
l := Concatenation( l, "\n", "morphism" );
fi;
GetLabel( node );
if IsBound( node!.Label ) then
l := Concatenation( l, "\n<", node!.Label, ">" );
fi;
fi;

l := Concatenation( "[", String( i ), "]\n", l );

SetDigraphVertexLabel( D, i, l );

D!.list_of_children[i] := PositionsOfChildrenOfALazyCell( node, nodes );

end );

return D;

end );

##
InstallOtherMethod( DotVertexLabelledDigraph,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )
local D, str, i, j, list_of_children, children, l;

D := DigraphOfEvaluation( c );

# Copied from DotVertexLabeledDigraph() at Digraphs/gap/display.gi
str := "//dot\n";

Append( str, "digraph hgn{\n" );
Append( str, "node [shape=rect]\n" );

for i in DigraphVertices( D ) do
Append( str, String(i) );
Append( str, " [label=\"" );
Append( str, String( DigraphVertexLabel( D, i ) ) );
Append( str, "\"]\n" );
od;

list_of_children := D!.list_of_children;

for i in DigraphVertices( D ) do
children := list_of_children[i];
l := Length( children );
if l > 1 and Length( Set( children ) ) > 1 then
for j in [ 1 .. l ] do
Append( str, Concatenation( String(children[j]), " -> ", String(i), " [ label=\"", String(j), "\" ]\n" ) );
od;
else
for j in [ 1 .. l ] do
Append( str, Concatenation( String(children[j]), " -> ", String(i), " \n" ) );
od;
fi;
od;

Append( str, "}\n" );

return str;

end );

##
InstallMethod( Visualize,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )

Splash( DotVertexLabelledDigraph( c ) );

end );

##################################
##
## View & Display
Expand Down
173 changes: 173 additions & 0 deletions LazyCategories/gap/ToolsUsingDigraphs.gi
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# SPDX-License-Identifier: GPL-2.0-or-later
# LazyCategories: Construct an equivalent lazy category out of a CAP category
#
# Implementations
#

##
InstallMethod( ListOfEvaluationNodes,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )
local nodes, queue, add_to_nodes, add_to_queue, children, D;

nodes := [ ];

queue := [ c ];

add_to_nodes :=
function( a )
if PositionProperty( nodes, b -> AreEqualForLazyCells( a, b ) ) = fail then
Add( nodes, a );
fi;
end;

add_to_queue :=
function( a )
if PositionProperty( Concatenation( nodes, queue ), b -> AreEqualForLazyCells( a, b ) ) = fail then
Add( queue, a );
fi;
end;

while not IsEmpty( queue ) do

c := Remove( queue, 1 );

add_to_nodes( c );

if HasGenesisOfCellArguments( c ) then

children := GenesisOfCellArguments( c );

children := Flat( children );

children := Filtered( children, IsLazyCapCategoryCell );

Perform( children, add_to_queue );

fi;

od;

nodes := Reversed( nodes );

D := List( nodes, node -> PositionsOfChildrenOfALazyCell( node, nodes ) );

D := Digraph( D );

return nodes{DigraphTopologicalSort( D )};

end );

##
InstallMethod( DigraphOfEvaluation,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )
local nodes, D;

nodes := ListOfEvaluationNodes( c );

D := List( nodes, node -> PositionsOfChildrenOfALazyCell( node, nodes ) );

D := Digraph( D );

D := DigraphReverse( D );

D!.list_of_children := [ ];

Perform( [ 1 .. Length( nodes ) ],
function( i )
local node, l, ints;

node := nodes[i];

if HasGenesisOfCellOperation( node ) then
l := GenesisOfCellOperation( node );
l := CAP_INTERNAL_COMPACT_NAME_OF_CATEGORICAL_OPERATION( l );
ints := Filtered( GenesisOfCellArguments( node ), IsInt );
if not IsEmpty( ints ) then
l := Concatenation( l, "( ", JoinStringsWithSeparator( ints, ", " ), " )" );
fi;
elif IsCapCategoryCell( node ) then
l := "primitive";
if IsCapCategoryObject( node ) then
l := Concatenation( l, "\n", "object" );
elif IsCapCategoryMorphism( node ) then
l := Concatenation( l, "\n", "morphism" );
fi;
GetLabel( node );
if IsBound( node!.Label ) then
l := Concatenation( l, "\n<", node!.Label, ">" );
fi;
fi;

l := Concatenation( "[", String( i ), "]\n", l );

SetDigraphVertexLabel( D, i, l );

D!.list_of_children[i] := PositionsOfChildrenOfALazyCell( node, nodes );

end );

return D;

end );

##
InstallOtherMethod( DotVertexLabelledDigraph,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )
local D, str, i, j, list_of_children, children, l;

D := DigraphOfEvaluation( c );

# Copied from DotVertexLabeledDigraph() at Digraphs/gap/display.gi
str := "//dot\n";

Append( str, "digraph hgn{\n" );
Append( str, "node [shape=rect]\n" );

for i in DigraphVertices( D ) do
Append( str, String(i) );
Append( str, " [label=\"" );
Append( str, String( DigraphVertexLabel( D, i ) ) );
Append( str, "\"]\n" );
od;

list_of_children := D!.list_of_children;

for i in DigraphVertices( D ) do
children := list_of_children[i];
l := Length( children );
if l > 1 and Length( Set( children ) ) > 1 then
for j in [ 1 .. l ] do
Append( str, Concatenation( String(children[j]), " -> ", String(i), " [ label=\"", String(j), "\" ]\n" ) );
od;
else
for j in [ 1 .. l ] do
Append( str, Concatenation( String(children[j]), " -> ", String(i), " \n" ) );
od;
fi;
od;

Append( str, "}\n" );

return str;

end );

##
InstallMethod( Visualize,
"for a cell in a lazy CAP category",
[ IsLazyCapCategoryCell ],

function( c )

Splash( DotVertexLabelledDigraph( c ) );

end );
4 changes: 4 additions & 0 deletions LazyCategories/read.g
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

ReadPackage( "LazyCategories", "gap/LazyCategory.gi");

if IsPackageMarkedForLoading( "Digraphs", ">= 1.3.1" ) then
ReadPackage( "LazyCategories", "gap/ToolsUsingDigraphs.gi");
fi;

if IsPackageMarkedForLoading( "JuliaInterface", ">= 0.2" ) then
ReadPackage( "LazyCategories", "gap/Julia.gi" );
fi;
Loading

0 comments on commit 20fc21c

Please sign in to comment.