Skip to content

Commit

Permalink
Merge pull request #839 from kris-brown/fix_loose_transformation
Browse files Browse the repository at this point in the history
Infer LooseACSetTransformation when a Julia function is passed as a component
  • Loading branch information
epatters authored Aug 23, 2023
2 parents 546efeb + 56d62cd commit 4c71be3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/categorical_algebra/CSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,14 @@ function coerce_attrvar_component(
return f
end

"""Coerce an arbitrary julia function to a LooseVarFunction assuming no variables"""
function coerce_attrvar_component(ob::Symbol, f::Function, d::TypeSet{T},cd::TypeSet{T′},
dom_size::Int, codom_size::Int) where {T,T′}
dom_size == 0 || error("Cannot specify $ob component with $f with $dom_size domain variables")
coerce_attrvar_component(ob, LooseVarFunction{T,T′}([], f, FinSet(codom_size)),
d, cd, dom_size,codom_size)
end

function Base.getindex::ACSetTransformation, c)
get.components, c) do
c attrtypes(acset_schema(dom(α))) || error("No object or attribute type with name $c")
Expand Down Expand Up @@ -498,9 +506,16 @@ ACSetTransformation(components, X::DynamicACSet, Y::DynamicACSet) =
acomps = NamedTuple(filter((attrtypes(S))first, pairs(components)))
length(ocomps) + length(acomps) == length(components) ||
error("Not all names in $(keys(components)) are objects or attribute types")
is_tight = true
is_tight = true # we do this with a `for` loop (not `all`) because comptime
for a in acomps
is_tight &= (a isa Union{VarFunction, Function, AbstractVector} || a.loose isa IdentityFunction)
if a isa Function
is_tight = false
elseif a isa LooseVarFunction && !(a.loose isa IdentityFunction)
is_tight = false
elseif a isa Union{VarFunction, AbstractVector}
else
error("Unexpected type for attrtype component of ACSetTransformation")
end
end
if is_tight
T = is_struct ? StructTightACSetTransformation{S} : DynamicTightACSetTransformation
Expand Down
5 changes: 4 additions & 1 deletion test/categorical_algebra/CSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ uns = naturality_failures(β)
collect(uns[:weight]) == [(1,1.0,2.0)]

# Loose morphisms.
α = LooseACSetTransformation((V=[1,2], E=[1]), (Weight=x->x/2,), g, h)
half = x->x/2
α = LooseACSetTransformation((V=[1,2], E=[1]), (Weight=half,), g, h)
α′ = ACSetTransformation(g, h, V=[1,2], E=[1], Weight=half,)
@test α == α′
@test α isa LooseACSetTransformation
@test type_components(α)[:Weight](10.0) == 5.0
@test is_natural(α)
Expand Down

0 comments on commit 4c71be3

Please sign in to comment.