From 5fcd07b30b5c1e261a62ba6cb87333e74c1127b4 Mon Sep 17 00:00:00 2001 From: Kevin Arlin Date: Fri, 19 Apr 2024 12:30:45 -0700 Subject: [PATCH 1/4] smoothing interaction between attrstuff and actual sets and functions --- src/categorical_algebra/FinSets.jl | 9 ++++++++- src/categorical_algebra/Sets.jl | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/categorical_algebra/FinSets.jl b/src/categorical_algebra/FinSets.jl index fb6f5cc5c..f6b3febf3 100644 --- a/src/categorical_algebra/FinSets.jl +++ b/src/categorical_algebra/FinSets.jl @@ -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...) @@ -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) @@ -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 @@ -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 diff --git a/src/categorical_algebra/Sets.jl b/src/categorical_algebra/Sets.jl index 36b0d200b..426dd0f38 100644 --- a/src/categorical_algebra/Sets.jl +++ b/src/categorical_algebra/Sets.jl @@ -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. From 3eb6c2076b3d030bb903228bd0dc63a62b8513cf Mon Sep 17 00:00:00 2001 From: Angeline Aguinaldo Date: Tue, 7 May 2024 11:00:40 -0400 Subject: [PATCH 2/4] add tests for VarSets to FinSets and FinFunction interaction --- test/categorical_algebra/FinSets.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/categorical_algebra/FinSets.jl b/test/categorical_algebra/FinSets.jl index e5decf187..eef041ec4 100644 --- a/test/categorical_algebra/FinSets.jl +++ b/test/categorical_algebra/FinSets.jl @@ -575,4 +575,26 @@ 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 + +S = SetOb(VarSet{Union{}}(5)) +@test SetOb(S) == S + end From e55ccd2f2c3797bdcb02ef60cffacf3e097fd9f1 Mon Sep 17 00:00:00 2001 From: Angeline Aguinaldo Date: Tue, 7 May 2024 11:07:49 -0400 Subject: [PATCH 3/4] move test for sets from FinSet.jl to Sets.jl --- test/categorical_algebra/FinSets.jl | 3 --- test/categorical_algebra/Sets.jl | 4 ++++ 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/categorical_algebra/FinSets.jl b/test/categorical_algebra/FinSets.jl index eef041ec4..48e243602 100644 --- a/test/categorical_algebra/FinSets.jl +++ b/test/categorical_algebra/FinSets.jl @@ -594,7 +594,4 @@ f = FinDomFunction([:a, :b, :a], FinSet(3), TypeSet(Symbol)) @test f(2) == :b @test f(3) == :a -S = SetOb(VarSet{Union{}}(5)) -@test SetOb(S) == S - end diff --git a/test/categorical_algebra/Sets.jl b/test/categorical_algebra/Sets.jl index 851297cdb..e4496e6d1 100644 --- a/test/categorical_algebra/Sets.jl +++ b/test/categorical_algebra/Sets.jl @@ -124,4 +124,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 From c7e1ef66b1b9ed6ceb946eda5850dd08c9245bfa Mon Sep 17 00:00:00 2001 From: Angeline Aguinaldo Date: Tue, 7 May 2024 13:27:20 -0400 Subject: [PATCH 4/4] add missing import for VarSet in Sets.jl --- test/categorical_algebra/Sets.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/categorical_algebra/Sets.jl b/test/categorical_algebra/Sets.jl index e4496e6d1..b5e6d5c81 100644 --- a/test/categorical_algebra/Sets.jl +++ b/test/categorical_algebra/Sets.jl @@ -2,6 +2,7 @@ module TestSets using Test using Catlab.Theories, Catlab.CategoricalAlgebra +using Catlab.CategoricalAlgebra.FinSets: VarSet # Sets from Julia types #######################