From ddc2ec56f53902564ae138bcdb716394fa84214b Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Wed, 13 Sep 2023 13:51:16 -0700 Subject: [PATCH 1/4] op --- src/stdlib/models/FinSets.jl | 1 + src/stdlib/models/Op.jl | 35 +++++++++++++++++++++++++++++++++++ src/stdlib/models/module.jl | 2 ++ test/stdlib/models/FinSets.jl | 3 ++- test/stdlib/models/Op.jl | 20 ++++++++++++++++++++ test/stdlib/models/tests.jl | 1 + 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/stdlib/models/Op.jl create mode 100644 test/stdlib/models/Op.jl diff --git a/src/stdlib/models/FinSets.jl b/src/stdlib/models/FinSets.jl index 8d9aa3ea..7864dbe8 100644 --- a/src/stdlib/models/FinSets.jl +++ b/src/stdlib/models/FinSets.jl @@ -27,6 +27,7 @@ end compose(f::Vector{Int}, g::Vector{Int}) = g[f] dom(f::Vector{Int}) = length(f) + codom(::Vector{Int}; context) = context[:c] end end diff --git a/src/stdlib/models/Op.jl b/src/stdlib/models/Op.jl new file mode 100644 index 00000000..6658edad --- /dev/null +++ b/src/stdlib/models/Op.jl @@ -0,0 +1,35 @@ +module Op + +export OpC, op + + +using ....Models +using ...StdTheories +using StructEquality + + +@struct_hash_equal struct OpC{ObT, HomT, C<:Model{Tuple{ObT, HomT}}} <: Model{Tuple{ObT, HomT}} + cat::C +end + +op(c) = OpC(c) + +using .ThCategory + +rename(::Nothing, ::Dict{Symbol,Symbol}) = nothing +rename(nt::NamedTuple, d::Dict{Symbol,Symbol}) = + NamedTuple(Dict([get(d, k, k) => v for (k, v) in pairs(nt)])) + + +@instance ThCategory{ObT, HomT} (;model::OpC{ObT, HomT, C}) where {ObT, HomT, C} begin + Ob(x::ObT) = Ob(x; model=model.cat) + Hom(x::HomT, d::ObT, cd::ObT) = Hom(x, cd, d; model=model.cat) + id(x::ObT) = id(x; model=model.cat) + dom(f::HomT; context) = codom(f; model=model.cat, context) + codom(f::HomT; context) = dom(f; model=model.cat, context) + compose(f::HomT, g::HomT; context=nothing) = + compose(g, f; model=model.cat, + context=rename(context, Dict(:a=>:c, :c=>:a, :b=>:b))) +end + +end # module diff --git a/src/stdlib/models/module.jl b/src/stdlib/models/module.jl index b7613e4e..328c7fe6 100644 --- a/src/stdlib/models/module.jl +++ b/src/stdlib/models/module.jl @@ -5,12 +5,14 @@ using Reexport include("FinSets.jl") include("FinMatrices.jl") include("SliceCategories.jl") +include("Op.jl") include("Nothings.jl") include("GATs.jl") @reexport using .FinSets @reexport using .FinMatrices @reexport using .SliceCategories +@reexport using .Op @reexport using .Nothings @reexport using .GATs diff --git a/test/stdlib/models/FinSets.jl b/test/stdlib/models/FinSets.jl index 04f67287..9f031bbd 100644 --- a/test/stdlib/models/FinSets.jl +++ b/test/stdlib/models/FinSets.jl @@ -4,7 +4,7 @@ using GATlab, Test using .ThCategory -@withmodel FinSetC() (Ob, Hom, id, compose, dom) begin +@withmodel FinSetC() (Ob, Hom, id, compose, dom, codom) begin @test Ob(0) == 0 @test_throws TypeCheckFail Ob(-1) @test_throws TypeCheckFail Hom([1,5,2], 3, 4) @@ -13,6 +13,7 @@ using .ThCategory @test id(2) == [1,2] @test compose([5], [1,1,1,3,2]) == [2] @test dom([5]) == 1 + @test codom([5]; context=(c=10,)) == 10 end end diff --git a/test/stdlib/models/Op.jl b/test/stdlib/models/Op.jl new file mode 100644 index 00000000..a18fa22d --- /dev/null +++ b/test/stdlib/models/Op.jl @@ -0,0 +1,20 @@ +"""Same as FinSetC tests but with all doms/codoms reversed""" +module TestOp + +using GATlab, Test + +using .ThCategory + +@withmodel op(FinSetC()) (Ob, Hom, id, compose, dom, codom) begin + @test Ob(0) == 0 + @test_throws TypeCheckFail Ob(-1) + @test_throws TypeCheckFail Hom([1,5,2], 4, 3) + @test Hom(Int[], 4, 0) == Int[] + + @test id(2) == [1,2] + @test compose([1,1,1,3,2], [5]) == [2] + @test codom([5]) == 1 + @test dom([5]; context=(c=10,)) == 10 +end + +end # module diff --git a/test/stdlib/models/tests.jl b/test/stdlib/models/tests.jl index 6e7e807a..6555e913 100644 --- a/test/stdlib/models/tests.jl +++ b/test/stdlib/models/tests.jl @@ -3,6 +3,7 @@ module TestStdModels include("FinSets.jl") include("FinMatrices.jl") include("SliceCategories.jl") +include("Op.jl") include("Nothings.jl") include("GATs.jl") From daec705b13e82b9d0f6da8ef29ce3bd93b166b6f Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Tue, 19 Sep 2023 11:01:47 -0700 Subject: [PATCH 2/4] fix namedtuple --- src/stdlib/models/Op.jl | 2 +- test/stdlib/models/Op.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stdlib/models/Op.jl b/src/stdlib/models/Op.jl index 6658edad..9c34013f 100644 --- a/src/stdlib/models/Op.jl +++ b/src/stdlib/models/Op.jl @@ -18,7 +18,7 @@ using .ThCategory rename(::Nothing, ::Dict{Symbol,Symbol}) = nothing rename(nt::NamedTuple, d::Dict{Symbol,Symbol}) = - NamedTuple(Dict([get(d, k, k) => v for (k, v) in pairs(nt)])) + NamedTuple(get(d, k, k) => v for (k, v) in pairs(nt)) @instance ThCategory{ObT, HomT} (;model::OpC{ObT, HomT, C}) where {ObT, HomT, C} begin diff --git a/test/stdlib/models/Op.jl b/test/stdlib/models/Op.jl index a18fa22d..c2a4b215 100644 --- a/test/stdlib/models/Op.jl +++ b/test/stdlib/models/Op.jl @@ -13,6 +13,7 @@ using .ThCategory @test id(2) == [1,2] @test compose([1,1,1,3,2], [5]) == [2] + @test compose([1,1,1,3,2], [5]; context=(;)) == [2] @test codom([5]) == 1 @test dom([5]; context=(c=10,)) == 10 end From 7f02e4ac32a3218fec020ff64031df144890a3c3 Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Wed, 20 Sep 2023 16:47:20 -0700 Subject: [PATCH 3/4] adjust to [model] --- src/stdlib/models/FinSets.jl | 2 +- src/stdlib/models/Op.jl | 16 +++++++++------- test/stdlib/models/Op.jl | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/stdlib/models/FinSets.jl b/src/stdlib/models/FinSets.jl index 7864dbe8..8bf8d303 100644 --- a/src/stdlib/models/FinSets.jl +++ b/src/stdlib/models/FinSets.jl @@ -27,7 +27,7 @@ end compose(f::Vector{Int}, g::Vector{Int}) = g[f] dom(f::Vector{Int}) = length(f) - codom(::Vector{Int}; context) = context[:c] + codom(::Vector{Int}; context) = context[:codom] end end diff --git a/src/stdlib/models/Op.jl b/src/stdlib/models/Op.jl index 9c34013f..cbb6da38 100644 --- a/src/stdlib/models/Op.jl +++ b/src/stdlib/models/Op.jl @@ -21,14 +21,16 @@ rename(nt::NamedTuple, d::Dict{Symbol,Symbol}) = NamedTuple(get(d, k, k) => v for (k, v) in pairs(nt)) -@instance ThCategory{ObT, HomT} (;model::OpC{ObT, HomT, C}) where {ObT, HomT, C} begin - Ob(x::ObT) = Ob(x; model=model.cat) - Hom(x::HomT, d::ObT, cd::ObT) = Hom(x, cd, d; model=model.cat) - id(x::ObT) = id(x; model=model.cat) - dom(f::HomT; context) = codom(f; model=model.cat, context) - codom(f::HomT; context) = dom(f; model=model.cat, context) +@instance ThCategory{ObT, HomT} [model::OpC{ObT, HomT, C}] where {ObT, HomT, C} begin + Ob(x::ObT) = Ob[model.cat](x) + Hom(x::HomT, d::ObT, cd::ObT) = Hom[model.cat](x, cd, d) + id(x::ObT) = id[model.cat](x) + dom(f::HomT; context) = + codom[model.cat](f; context=rename(context, Dict(:dom=>:codom, :codom=>:dom))) + codom(f::HomT; context) = + dom[model.cat](f; context=rename(context, Dict(:dom=>:codom, :codom=>:dom))) compose(f::HomT, g::HomT; context=nothing) = - compose(g, f; model=model.cat, + compose[model.cat](g, f; context=rename(context, Dict(:a=>:c, :c=>:a, :b=>:b))) end diff --git a/test/stdlib/models/Op.jl b/test/stdlib/models/Op.jl index c2a4b215..06295f1f 100644 --- a/test/stdlib/models/Op.jl +++ b/test/stdlib/models/Op.jl @@ -15,7 +15,7 @@ using .ThCategory @test compose([1,1,1,3,2], [5]) == [2] @test compose([1,1,1,3,2], [5]; context=(;)) == [2] @test codom([5]) == 1 - @test dom([5]; context=(c=10,)) == 10 + @test dom([5]; context=(dom=10,)) == 10 end end # module From e3dae71d095f6781b3042444f27a0e1dda017f8a Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Wed, 20 Sep 2023 16:53:08 -0700 Subject: [PATCH 4/4] c is short for codom --- test/stdlib/models/FinSets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/stdlib/models/FinSets.jl b/test/stdlib/models/FinSets.jl index 9f031bbd..7887388e 100644 --- a/test/stdlib/models/FinSets.jl +++ b/test/stdlib/models/FinSets.jl @@ -13,7 +13,7 @@ using .ThCategory @test id(2) == [1,2] @test compose([5], [1,1,1,3,2]) == [2] @test dom([5]) == 1 - @test codom([5]; context=(c=10,)) == 10 + @test codom([5]; context=(codom=10,)) == 10 end end