Skip to content

Commit 4c71be3

Browse files
authored
Merge pull request #839 from kris-brown/fix_loose_transformation
Infer LooseACSetTransformation when a Julia function is passed as a component
2 parents 546efeb + 56d62cd commit 4c71be3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/categorical_algebra/CSets.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,14 @@ function coerce_attrvar_component(
444444
return f
445445
end
446446

447+
"""Coerce an arbitrary julia function to a LooseVarFunction assuming no variables"""
448+
function coerce_attrvar_component(ob::Symbol, f::Function, d::TypeSet{T},cd::TypeSet{T′},
449+
dom_size::Int, codom_size::Int) where {T,T′}
450+
dom_size == 0 || error("Cannot specify $ob component with $f with $dom_size domain variables")
451+
coerce_attrvar_component(ob, LooseVarFunction{T,T′}([], f, FinSet(codom_size)),
452+
d, cd, dom_size,codom_size)
453+
end
454+
447455
function Base.getindex::ACSetTransformation, c)
448456
get.components, c) do
449457
c attrtypes(acset_schema(dom(α))) || error("No object or attribute type with name $c")
@@ -498,9 +506,16 @@ ACSetTransformation(components, X::DynamicACSet, Y::DynamicACSet) =
498506
acomps = NamedTuple(filter((attrtypes(S))first, pairs(components)))
499507
length(ocomps) + length(acomps) == length(components) ||
500508
error("Not all names in $(keys(components)) are objects or attribute types")
501-
is_tight = true
509+
is_tight = true # we do this with a `for` loop (not `all`) because comptime
502510
for a in acomps
503-
is_tight &= (a isa Union{VarFunction, Function, AbstractVector} || a.loose isa IdentityFunction)
511+
if a isa Function
512+
is_tight = false
513+
elseif a isa LooseVarFunction && !(a.loose isa IdentityFunction)
514+
is_tight = false
515+
elseif a isa Union{VarFunction, AbstractVector}
516+
else
517+
error("Unexpected type for attrtype component of ACSetTransformation")
518+
end
504519
end
505520
if is_tight
506521
T = is_struct ? StructTightACSetTransformation{S} : DynamicTightACSetTransformation

test/categorical_algebra/CSets.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ uns = naturality_failures(β)
309309
collect(uns[:weight]) == [(1,1.0,2.0)]
310310

311311
# Loose morphisms.
312-
α = LooseACSetTransformation((V=[1,2], E=[1]), (Weight=x->x/2,), g, h)
312+
half = x->x/2
313+
α = LooseACSetTransformation((V=[1,2], E=[1]), (Weight=half,), g, h)
314+
α′ = ACSetTransformation(g, h, V=[1,2], E=[1], Weight=half,)
315+
@test α == α′
313316
@test α isa LooseACSetTransformation
314317
@test type_components(α)[:Weight](10.0) == 5.0
315318
@test is_natural(α)

0 commit comments

Comments
 (0)