Skip to content

Commit

Permalink
Merge pull request #900 from AlgebraicJulia/attrfuncChanges
Browse files Browse the repository at this point in the history
Smoothing interaction between VarSets and actual sets and functions
  • Loading branch information
epatters committed May 7, 2024
2 parents 6d1c7f3 + c7e1ef6 commit 1d9f8e3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/categorical_algebra/FinSets.jl
Expand Up @@ -229,6 +229,7 @@ FinDomFunction(f::Function, dom, codom) =
FinDomFunction(::typeof(identity), args...) =
IdentityFunction((FinSet(arg) for arg in args)...)
FinDomFunction(f::AbstractDict, args...) = FinDomFunctionDict(f, args...)
FinDomFunction(f::FinDomFunction) = f

#kw is to capture is_correct, which does nothing for this type.
function FinDomFunction(f::AbstractVector, args...; index=false, kw...)
Expand Down Expand Up @@ -311,7 +312,9 @@ Control dispatch in the category of VarFunctions
n::Int
end
VarSet(i::Int) = VarSet{Union{}}(i)
FinSet(s::VarSet) = FinSet(s.n)
SetOb(s::VarSet{Union{}}) = FinSet(s)
SetOb(s::VarSet{T}) where T = TypeSet{Union{AttrVar,T}}()
FinSet(s::VarSet) = FinSet(s.n) #Note this throws away `T`, most accurate when thinking about tight `VarFunction`s.
Base.iterate(set::VarSet{T}, args...) where T = iterate(1:set.n, args...)
Base.length(set::VarSet{T}) where T = set.n
Base.in(set::VarSet{T}, elem) where T = in(elem, 1:set.n)
Expand All @@ -338,6 +341,7 @@ VarFunction(f::FinDomFunction) = VarFunction{Union{}}(AttrVar.(collect(f)),codom
FinFunction(f::VarFunction{T}) where T = FinFunction(
[f.fun(i) isa AttrVar ? f.fun(i).val : error("Cannot cast to FinFunction")
for i in dom(f)], f.codom)
FinDomFunction(f::VarFunction{T}) where T = f.fun
Base.length(f::AbsVarFunction{T}) where T = length(collect(f.fun))
Base.collect(f::AbsVarFunction{T}) where T = collect(f.fun)
(f::VarFunction{T})(v::T) where T = v
Expand All @@ -356,6 +360,9 @@ is_epic(f::VarFunction) = AttrVar.(f.codom) ⊆ collect(f)
compose(::IdentityFunction{TypeSet{T}}, f::AbsVarFunction{T}) where T = f
compose(f::VarFunction{T}, ::IdentityFunction{TypeSet{T}}) where T = f

FinDomFunction(f::Function, dom, codom::VarSet{T}) where T =
SetFunctionCallable(f, FinSet(dom), SetOb(codom))

"""Kleisi composition of [n]->T+[m] and [m]->T'+[p], yielding a [n]->T'+[p]"""
compose(f::VarFunction{T},g::VarFunction{T}) where {T} =
VarFunction{T}([elem isa AttrVar ? g.fun(elem.val) : elem
Expand Down
2 changes: 1 addition & 1 deletion src/categorical_algebra/Sets.jl
Expand Up @@ -29,7 +29,7 @@ Note: This type is more abstract than the built-in Julia types `AbstractSet` and
encompassed by the subtype [`FinSet`](@ref).
"""
abstract type SetOb{T} end

SetOb(S::SetOb) = S
Base.eltype(::Type{<:SetOb{T}}) where T = T

""" A Julia data type regarded as a set.
Expand Down
19 changes: 19 additions & 0 deletions test/categorical_algebra/FinSets.jl
Expand Up @@ -575,4 +575,23 @@ f = VarFunction{Bool}(AttrVar.([1,2,true]),FinSet(2))
# Create
@test dom(create(VarSet{Int}(1))) == VarSet{Int}(0)

# VarSets to FinSets
#############
f = FinDomFunction([:a, :b, :c])
@test FinDomFunction(f) == f

s = VarSet{Union{}}(2)
@test SetOb(s) == FinSet(s)

@test SetOb(VarSet{Int}(4)) == TypeSet(Union{AttrVar,Int64})

f = VarFunction{Bool}(AttrVar.([1, 2]), FinSet(2))
fin_f = FinDomFunction(Union{AttrVar,Bool}[AttrVar(1), AttrVar(2)], FinSet(2), TypeSet(Union{AttrVar,Bool}))
@test FinDomFunction(f) == fin_f

f = FinDomFunction([:a, :b, :a], FinSet(3), TypeSet(Symbol))
@test f(1) == :a
@test f(2) == :b
@test f(3) == :a

end
5 changes: 5 additions & 0 deletions test/categorical_algebra/Sets.jl
Expand Up @@ -2,6 +2,7 @@ module TestSets
using Test

using Catlab.Theories, Catlab.CategoricalAlgebra
using Catlab.CategoricalAlgebra.FinSets: VarSet

# Sets from Julia types
#######################
Expand Down Expand Up @@ -124,4 +125,8 @@ colim = colimit(SingletonDiagram(TypeSet(Int)))
f = SetFunction(string, TypeSet(Int), TypeSet(String))
@test universal(colim, SMulticospan{1}(f)) === f

# VarSets
S = SetOb(VarSet{Union{}}(5))
@test SetOb(S) == S

end

0 comments on commit 1d9f8e3

Please sign in to comment.