Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(co)products/(co)equalizers for Fincats and Diagrams #615

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 150 additions & 10 deletions src/categorical_algebra/CSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,33 @@ export ACSetTransformation, CSetTransformation,
ACSetHomomorphismAlgorithm, BacktrackingSearch, HomomorphismQuery,
components, force, is_natural, homomorphism, homomorphisms, is_homomorphic,
isomorphism, isomorphisms, is_isomorphic,
generate_json_acset, parse_json_acset, read_json_acset, write_json_acset
generate_json_acset, parse_json_acset, read_json_acset, write_json_acset,
uncurry, curry, ACSetCat

using Base.Iterators: flatten
using Base.Meta: quot
using AutoHashEquals
using JSON
using Reexport
using Tables
using DataStructures: DefaultDict

@reexport using ...CSetDataStructures
using ...GAT, ...Present
using ...Theories: Category, SchemaDescType, CSetSchemaDescType,
attrtype, attrtype_num, attr, adom, acodom, acodom_nums
import ...Theories: dom, codom, compose, ⋅, id,
ob, hom, meet, ∧, join, ∨, top, ⊤, bottom, ⊥
ob, hom, meet, ∧, join, ∨, top, ⊤, bottom, ⊥, curry
using ..FreeDiagrams, ..Limits, ..Subobjects, ..FinSets, ..FinCats
import ..Limits: limit, colimit, universal, pushout_complement,
can_pushout_complement
import ..Subobjects: Subobject, SubobjectBiHeytingAlgebra,
implies, ⟹, subtract, \, negate, ¬, non, ~
import ..Categories: is_hom_equal
import ..Sets: SetOb, SetFunction, TypeSet
import ..FinSets: FinSet, FinFunction, FinDomFunction, force, predicate
import ..FinCats: FinDomFunctor, components, is_natural
import ..FinCats: FinDomFunctor, components, is_natural, FinTransformationMap,
FinDomFunctorMap

# Sets interop
##############
Expand Down Expand Up @@ -139,16 +143,24 @@ end
const ACSetDomCat = FinCats.FinCatPresentation{
Symbol, Union{FreeSchema.Ob,FreeSchema.AttrType},
Union{FreeSchema.Hom,FreeSchema.Attr,FreeSchema.AttrType}}
const FinSetCat = TypeCat{SetOb,FinDomFunction{Int}}

""" Wrapper type to interpret attributed C-set as a functor.
"""
@auto_hash_equals struct ACSetFunctor{ACS<:ACSet} <:
Functor{ACSetDomCat,TypeCat{SetOb,FinDomFunction{Int}}}
Functor{ACSetDomCat,FinSetCat}
acset::ACS
eqs::Vector{Pair}
end
FinDomFunctor(X::ACSet) = ACSetFunctor(X)
FinDomFunctor(X::ACSet; eqs=Pair[]) = ACSetFunctor(X, eqs)

dom(F::ACSetFunctor) = FinCat(Presentation(F.acset))
function dom(F::ACSetFunctor)
p = Presentation(F.acset)
for (l,r) in F.eqs
add_equation!(p, l, r)
end
FinCat(p)
end
codom(F::ACSetFunctor) = TypeCat{SetOb,FinDomFunction{Int}}()

Categories.do_ob_map(F::ACSetFunctor, x) = SetOb(F.acset, x)
Expand All @@ -159,9 +171,10 @@ Categories.do_hom_map(F::ACSetFunctor, f) = SetFunction(F.acset, f)
function (::Type{ACS})(F::FinDomFunctor) where ACS <: ACSet
X = if ACS isa UnionAll
pres = presentation(dom(F))
ACS{(eltype(ob_map(F, c)) for c in generators(pres, :AttrType))...}()
Base.invokelatest(
ACS{(eltype(ob_map(F, c)) for c in generators(pres, :AttrType))...})
else
ACS()
Base.invokelatest(ACS)
end
copy_parts!(X, F)
return X
Expand Down Expand Up @@ -236,6 +249,8 @@ end

components(α::ACSetTransformation) = α.components
force(α::ACSetTransformation) = map_components(force, α)
is_hom_equal(f::ACSetTransformation, g::ACSetTransformation) =
force(f) == force(g)

""" Transformation between C-sets.

Expand Down Expand Up @@ -385,6 +400,17 @@ function is_natural(α::ACSetTransformation{S}) where {S}
return true
end

function (C::Type{ACS})(F::FinTransformationMap) where ACS <: ACSet
Cd, CCd = C(dom(F)), C(codom(F))
return CSetTransformation(Cd, CCd; components(F)...)
end


FinTransformationMap(f::ACSetTransformation; eqs=Pair[]) =
FinTransformationMap(components(f),
FinDomFunctor(dom(f); eqs=eqs),
FinDomFunctor(codom(f); eqs=eqs))

# Category of C-sets
####################

Expand Down Expand Up @@ -773,7 +799,7 @@ function limit(::Type{Tuple{ACS,Hom}}, diagram) where
{S, ACS <: StructCSet{S}, Hom <: TightACSetTransformation}
limits = map(limit, unpack_diagram(diagram))
Xs = cone_objects(diagram)
Y = ACS()
Y = Base.invokelatest(ACS)
limit!(Y, diagram, Xs, limits)
end

Expand Down Expand Up @@ -818,7 +844,7 @@ function colimit(::Type{Tuple{ACS,Hom}}, diagram) where
# Colimit of C-set without attributes.
colimits = map(colimit, unpack_diagram(diagram))
Xs = cocone_objects(diagram)
Y = ACS()
Y = Base.invokelatest(ACS)
for (c, colim) in pairs(colimits)
add_parts!(Y, c, length(ob(colim)))
end
Expand Down Expand Up @@ -1134,6 +1160,120 @@ end
end...))
end


# Tensor-hom adjunction (currying of diagrams in C-Set)
#######################################################
const ACSetCat{S} = TypeCat{S, ACSetTransformation}

""" curry(d::FinFunctor{D, ACSetCat{S}}) where {D,S}
Currying on objects of a functor category
"""
function curry(d::FinFunctor{D, ACSetCat{S}}) where {D,S}
shapelim = product([dom(d), FinCat(Presentation(S))])
shape_ind, part_ind = legs(shapelim)
asl = apex(shapelim)
omap = Dict(map(ob_generators(asl)) do o
x = ob_map(shape_ind, o)
y = ob_map(part_ind, o)
o => FinSet(ob_map(d, x), Symbol(y))
end)

hmap = Dict(map(hom_generators(asl)) do o
x = hom_map(shape_ind, o)
y = hom_map(part_ind, o)
if x isa FreeSchema.Hom{:id}
o => FinFunction(ob_map(d, only(x.args)), Symbol(y))
elseif y isa FreeSchema.Hom{:id}
o => hom_map(d, x)[Symbol(only(y.args))]
else
error("x $x y $y")
end
end)

FinDomFunctor(omap,hmap,asl,FinSetCat())
end

"""
Uses an example FinDomFunctor (in the original uncurried format).
"""
function uncurry(d::FinDomFunctor{D1, FinSetCat},
old_d::FinDomFunctor{D2, ACSetCat{S}}) where {D1,D2,S}
# Recover schema for d as a product, not just the apex
shapelim = product([dom(old_d), FinCat(Presentation(S))])
asl = apex(shapelim)
shape_ind, part_ind = legs(shapelim)

cset_type = typeof(first(old_d.ob_map)[2])
omap = Dict(map(ob_generators(dom(old_d))) do o
x = Base.invokelatest(cset_type)
for o_ in ob_generators(asl)
if ob_map(shape_ind, o_) == o
add_parts!(x, Symbol(ob_map(part_ind, o_)), length(ob_map(d, o_)))
end
end
for h in hom_generators(asl)
h_ = hom_map(shape_ind, h)
if h_ == id(o)
set_subpart!(x, Symbol(hom_map(part_ind, h)), collect(hom_map(d, h)))
end
end
o => x
end)
hmap = Dict(map(hom_generators(dom(old_d))) do h
comps = Dict()
for h_ in hom_generators(asl)
if hom_map(shape_ind, h_) == h
comps[Symbol(only(hom_map(part_ind, h_).args))] = hom_map(d, h_)
end
end
dom_, codom_ = [omap[get(h)] for get in [dom, codom]]
h => ACSetTransformation(dom_,codom_; comps...)
end)
FinDomFunctor(omap,hmap,dom(old_d),ACSetCat{S}())
end

""" curry(d::FinFunctor{D, ACSetCat{S}}) where {D,S}
Currying on morphisms of a functor category with an ACSetCat as codom
"""
function curry(ϕ::FinTransformationMap{D, ACSetCat{S}}) where {D,S}
cur_d, cur_cd = curry.([dom(ϕ), codom(ϕ)])
shapelim = product([dom(dom(ϕ)), FinCat(Presentation(S))])
shape_ind, part_ind = legs(shapelim)
comps = Dict(map(ob_generators(apex(shapelim))) do o
oshape, opart = Symbol(shape_ind(o)), Symbol(part_ind(o))
Symbol(o) => components(ϕ)[oshape][opart]
end)
FinTransformationMap(comps,cur_d,cur_cd)
end

""" uncurry(d::FinTransformationMap, old_d::FinTransformationMap{D, ACSetCat{S}}) where {D, S}
Inverse to currying on morphisms of a functor category with an ACSetCat as codom
"""
function uncurry(d::FinTransformationMap,
old_d::FinTransformationMap{D, ACSetCat{S}}) where {D, S}
# Recover schema for d as a product, not just the apex
shapelim = product([dom(dom(old_d)), FinCat(Presentation(S))])
shape_ind, part_ind = legs(shapelim)

αcomps = Dict(o => DefaultDict{Symbol,Vector{Int}}(()->Int[])
for o in keys(components(old_d)))

for o in (ob_generators(apex(shapelim)))
dic = αcomps[Symbol(ob_map(shape_ind, o))]
dic[Symbol(ob_map(part_ind, o))] = collect(components(d)[Symbol(o)])
end

uc_d, uc_cd = [uncurry(get(d), get(old_d)) for get in [dom, codom]]

α = Dict(map(collect(αcomps)) do (o, comps)
o => ACSetTransformation(ob_map(uc_d, o),
ob_map(uc_cd, o); comps...)
end)


FinTransformationMap(α, uc_d, uc_cd)
end

# Serialization
###############

Expand Down
2 changes: 2 additions & 0 deletions src/categorical_algebra/CategoricalAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include("Matrices.jl")
include("FinRelations.jl")
include("CSets.jl")
include("GraphCategories.jl")
include("Chase.jl")
include("Diagrams.jl")
include("CommutativeDiagrams.jl")
include("CatElements.jl")
Expand All @@ -32,6 +33,7 @@ include("Slices.jl")
@reexport using .CSets
@reexport using .CatElements

@reexport using .Chase
@reexport using .Diagrams
@reexport using .CommutativeDiagrams
@reexport using .DataMigrations
Expand Down
Loading