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

Infer LooseACSetTransformation when a Julia function is passed as a component #839

Merged
merged 2 commits into from
Aug 23, 2023
Merged
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
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 @@
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 @@
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")

Check warning on line 517 in src/categorical_algebra/CSets.jl

View check run for this annotation

Codecov / codecov/patch

src/categorical_algebra/CSets.jl#L517

Added line #L517 was not covered by tests
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