AlgebraicJulia / Catlab.jl

A framework for applied category theory in the Julia language
https://www.algebraicjulia.org
MIT License
599 stars 56 forks source link

JSON serialization of VarFunctions #790

Open jpfairbanks opened 1 year ago

jpfairbanks commented 1 year ago

FinFunctions are just simple structs so they serialize just fine, but VarFunctions (even empty ones) don't serialize.

MWE:

using Catlab
using Catlab.CategoricalAlgebra
using Catlab.CategoricalAlgebra.CSets
import Catlab.CategoricalAlgebra.CSets: generate_json_acset, _parse_json_acset

using Catlab.Graphs

g = @acset Graph begin
    V = 3
    E = 3
    src = [1,2,3]
    tgt = [2,3,1]
end

h = @acset Graph begin
    V = 4
    E = 5
    src = [1,2,3,3,2]
    tgt = [2,3,1,4,4]
end

gdict = generate_json_acset(g)
hdict = generate_json_acset(h)
ϕ = homomorphism(g,h, monic=true)
JSON.json(ϕ.components.Weight)
kris-brown commented 1 year ago

(Assuming that the example was supposed to have g and h being WeightedGraphs rather than Graphs)

The stack trace involved doesn't seem to involve any Catlab code. Do you know what method would need to be changed/added for VarFunction{T} to avoid this problem? (So far I've tried Base.show without any effect)

jpfairbanks commented 1 year ago

Oh yeah, I copied the minimal working example instead of the minimal breaking one. You would just have to overload JSON.json

kris-brown commented 1 year ago

Catlab doesn't actually use JSON anywhere (it's only in the test Project.toml) so I don't think that can be responsible for the difference between FinFunctions and VarFunctions

epatters commented 1 year ago

Wait, is this about using JSON.json to serialize structs (in this case, the structs for FinFunctions or VarFunctions) using the default serialization? IMO, we should not be doing that since, as this issue shows, it's too general to be reliable or invertible. We should decide on the format, then implement the serializers and deserializers ourselves.

Cf. https://github.com/AlgebraicJulia/ACSets.jl/issues/28, which I was planning to resolve before the next release.

Among the Topos folks we've started to discuss possibilities for a more systematic and less piecemeal approach to serialization, but that's a much bigger topic that should not detract from getting workable solutions in the short term.

jpfairbanks commented 1 year ago

Yes, finfunctions just work because they are structs with serializable fields. I guess we should collect them manually to make things work in the short term.

What are some of the ideas about long term serialization?