Skip to content

Commit

Permalink
Merge pull request #733 from kris-brown/is_natural_verbose
Browse files Browse the repository at this point in the history
Add debug flag to `is_natural`
  • Loading branch information
epatters authored Jan 27, 2023
2 parents ab4c04f + 323fd48 commit 103191f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/categorical_algebra/CSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,36 @@ end
map_components(f, α::LooseACSetTransformation) =
LooseACSetTransformation(map(f, components(α)), α.type_components, dom(α), codom(α))

function is_natural::ACSetTransformation{S}) where {S}
"""
Check naturality condition for a purported ACSetTransformation, α: X→Y.
For each hom in the schema, e.g. h: m → n, the following square must commute:
αₘ
Xₘ --> Yₘ
Xₕ ↓ ✓ ↓ Yₕ
Xₙ --> Yₙ
αₙ
If `return_failures` is true, return a list of violations, with elements such as:
(h, index i in Xₘ, Yₕ(αₘ(i)), αₙ(Xₕ(i)))
This is type-unstable, so it should not be used in performance-sensitive code.
"""
function is_natural::ACSetTransformation{S}; return_failures::Bool=false) where {S}
X, Y = dom(α), codom(α)
unnatural = []
for (f, c, d) in arrows(S)
Xf, Yf, α_c, α_d = subpart(X,f), subpart(Y,f), α[c], α[d]
all(i -> Yf[α_c(i)] == α_d(Xf[i]), eachindex(Xf)) || return false
for i in eachindex(Xf)
if Yf[α_c(i)] != α_d(Xf[i])
if return_failures
push!(unnatural, (f, i, Yf[α_c(i)], α_d(Xf[i])))
else
return false
end
end
end
end
return true
return return_failures ? unnatural : true
end

function is_monic::TightACSetTransformation{S}) where {S}
Expand Down
5 changes: 5 additions & 0 deletions test/categorical_algebra/CSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ g, h = path_graph(Graph, 4), cycle_graph(Graph, 2)
@test !is_natural(β)
β = CSetTransformation((V=[2,1], E=[2,1]), h, h)
@test is_natural(β)
β = CSetTransformation((V=[2,1], E=[2,2]), h, h)
@test is_natural(β;return_failures=true) == [(:src,2,2,1),(:tgt,2,1,2)]


# Category of C-sets.
@test dom(α) === g
Expand Down Expand Up @@ -256,6 +259,8 @@ h = path_graph(WeightedGraph{Float64}, 4, E=(weight=[1.,2.,3.],))
@test !is_natural(β) # Preserves weight but not graph homomorphism
β = ACSetTransformation((V=[1,2], E=[1]), g, h)
@test !is_natural(β) # Graph homomorphism but does not preserve weight
β = ACSetTransformation((V=[1,3], E=[1]), g, h)
@test is_natural(β; return_failures=true) == [(:tgt,1,2,3), (:weight,1,1.,2.)]

# Loose morphisms.
α = ACSetTransformation(g, h, V=[1,2], E=[1], Weight=x->x/2)
Expand Down

0 comments on commit 103191f

Please sign in to comment.