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

Incorporating GATlab more deeply #949

Draft
wants to merge 2 commits into
base: clean_refactor
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Manifest.toml
*.jl.cov
*.jl.*.cov
*.jl.mem

lcov.info
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Catlab"
uuid = "134e5e36-593f-5add-ad60-77f754baafbe"
license = "MIT"
authors = ["Evan Patterson <evan@epatters.org>"]
version = "0.16.19"
version = "0.17.1"

[deps]
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Expand Down
4 changes: 1 addition & 3 deletions docs/src/apis/categorical_algebra.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ The following APIs implement FinSet, the category of Finite Sets (actually the s

```@autodocs
Modules = [
BasicSets.Sets,
BasicSets.FinSets,
CategoricalAlgebra.FinRelations,
CategoricalAlgebra.BasicSets,
]
Private = false
```
Expand Down
2 changes: 1 addition & 1 deletion ext/CatlabDataFramesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import Catlab.WiringDiagrams.WiringDiagramAlgebras: make_table
make_table(::Type{MaybeDataFrame}, columns, names) =
make_table(DataFrame, columns, names)

end
end # module
2 changes: 1 addition & 1 deletion src/ACSetsGATsInterop.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Compatibility module that integrates ACSets with GATs.
"""
module ACSetsGATsInterop
export ThSchema, FreeSchema, Presentation, @present
export ThSchema, FreeSchema, Presentation, @present, getvalue

using DataStructures: OrderedDict

Expand Down
4 changes: 2 additions & 2 deletions src/Catlab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Catlab

using Reexport

include("theories/Theories.jl")
include("ACSetsGATsInterop.jl") # This should be upstreamed to an extension of ACSets.jl
include("theories/module.jl")
include("ACSetsGATsInterop.jl")
include("graphs/Graphs.jl")
include("basic_sets/module.jl")
include("categorical_algebra/module.jl")
Expand Down
98 changes: 68 additions & 30 deletions src/basic_sets/finfunctions/FinFnDict.jl
Original file line number Diff line number Diff line change
@@ -1,50 +1,88 @@
module FinFnDict
export FinFunctionDict, FinDomFunctionDict

using StructEquality
export FinFunctionDict

import ....Theories: dom, codom
using ..Sets, ..FinSets, ..FinFunctions, ..SetFunctions
using StructEquality

using GATlab

# Dict-based functions
#---------------------
using ...Sets: AbsSet, SetOb
using ...SetFunctions: AbsFunction, ThSetFunction, SetFunction, dom, codom
using ...FinSets: FinSet

""" Function in **Set** represented by a dictionary.
import ..FinFunctions: FinFunction, FinDomFunction

The domain is a `FinSet{S}` where `S` is the type of the dictionary's `keys`
collection.
"""
Valid function when domain is indexed by positive integers less than the
vector length.
"""
@struct_hash_equal struct FinDomFunctionDict{K,D<:AbstractDict{K},Codom<:SetOb} <:
SetFunction{FinSetCollection{Base.KeySet{K,D},K},Codom}
func::D
codom::Codom
@struct_hash_equal struct FinFunctionDict{T<:AbsSet}
val::Dict
dom::FinSet
codom::T
function FinFunctionDict(val::Dict, dom::FinSet, codom::T) where T<:AbsSet
for e in dom
haskey(val, e) || error("Missing key $e ∈ $dom from $val")
end
new{T}(val, dom, codom)
end
end

FinDomFunctionDict(d::AbstractDict{K,V}) where {K,V} =
FinDomFunctionDict(d, TypeSet{V}())
""" Default assumed domain """
FinFunctionDict(val::Dict, codom::AbsSet) =
FinFunctionDict(val, FinSet(Set(collect(keys(val)))), codom)


# Accessor
##########

dom(f::FinDomFunctionDict) = FinSet(keys(f.func))
GATlab.getvalue(f::FinFunctionDict) = f.val

(f::FinDomFunctionDict)(x) = f.func[x]
# Other methods
###############

function Base.show(io::IO, f::F) where F <: FinDomFunctionDict
SetFunctions.show_type_constructor(io, F)
print(io, "(")
show(io, f.func)
print(io, ", ")
SetFunctions.show_domains(io, f, domain=false)
function Base.show(io::IO, f::FinFunctionDict)
print(io, "Fin")
f.codom isa FinSet || print(io, "Dom")
print(io, "Function($(getvalue(f)), ")
print(io, f.codom)
print(io, ")")
end

# SetFunction implementation
############################

""" Function in **FinSet** represented by a dictionary.
"""
const FinFunctionDict{K,D<:AbstractDict{K},Codom<:FinSet} =
FinDomFunctionDict{K,D,Codom}
@instance ThSetFunction [model::FinFunctionDict{T}] where T begin

dom()::AbsSet = model.dom

codom()::T = model.codom

app(i::Any, )::Any = getvalue(model)[i]

function postcompose(g::AbsFunction)::AbsFunction
C = codom(g)
(C isa FinSet ? FinFunction : FinDomFunction)(SetFunction(
FinFunctionDict(Dict(k => g(v) for (k,v) in getvalue(model)), C)))
end
end

""" Default `FinFunction` from a `AbstractDict`"""
FinFunction(f::AbstractDict) = FinFunction(f, FinSet(Set(values(f))))

""" Default `FinFunction` from a `AbstractDict` and codom"""
FinFunction(f::AbstractDict, cod::FinSet) =
FinFunction(SetFunction(FinFunctionDict(f, cod)))

""" Default `FinFunction` from a `AbstractDict` and codom"""
FinFunction(f::AbstractDict, dom::FinSet, cod::FinSet) =
FinFunction(SetFunction(FinFunctionDict(f, dom, cod)))


FinDomFunction(f::AbstractDict, cod::AbsSet) =
FinDomFunction(SetFunction(FinFunctionDict(f, cod)))

FinFunctionDict(d::AbstractDict, codom::FinSet) = FinDomFunctionDict(d, codom)
FinFunctionDict(d::AbstractDict{K,V}) where {K,V} =
FinDomFunctionDict(d, FinSet(Set(values(d))))
FinDomFunction(f::AbstractDict, dom::FinSet, cod::AbsSet) =
FinDomFunction(SetFunction(FinFunctionDict(f, dom, cod)))

end # module
Loading
Loading