Skip to content

Commit

Permalink
Merge pull request #82 from AlgebraicJulia/fix_pull_back
Browse files Browse the repository at this point in the history
Fix `pull_back` function for compatibility with catlab v0.16.18
  • Loading branch information
kris-brown authored Nov 6, 2024
2 parents 0f326b2 + 7649919 commit e73be3f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
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 = "AlgebraicRewriting"
uuid = "725a01d3-f174-5bbd-84e1-b9417bad95d9"
license = "MIT"
authors = ["Kris Brown <[email protected]>"]
version = "0.4.1"
version = "0.4.2"

[deps]
ACSets = "227ef7b5-1206-438b-ac65-934d6da304b8"
Expand Down
13 changes: 8 additions & 5 deletions src/incremental/Algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Algorithms
using Catlab
using ...CategoricalAlgebra.CSets: invert_iso, var_reference
using ...Rewrite.Migration: pres_hash
using ACSets.DenseACSets: attrtype_type

"""
Break an ACSet into connected components, represented as a coproduct and an
Expand Down Expand Up @@ -175,7 +176,7 @@ function nac_overlap(nac, update::ACSetTransformation)
end

"""
Given f: L->X and m: X' ↣ X, find the unique map L -> X' making the triangle
Given f: X' ↣ X, m: L->X , find the unique map L -> X' making the triangle
commute, if it exists.
TODO rewrite with @comptime
Expand All @@ -185,11 +186,12 @@ function pull_back(f::ACSetTransformation, m::ACSetTransformation
L, X′ = dom.([m, f])
comps, S = Dict(), acset_schema(L)
for o in ob(S)
vec = []
vec = Int[]
for i in parts(L, o)
pre = preimage(f[o], m[o](i))
# Given that `m` is monic, preimage is empty or singleton
length(pre) == 1 || return nothing
all(attrs(S; from=o)) do (atr, _, _)
all(attrs(S; from=o, just_names=true)) do atr
L[i, atr] isa AttrVar || L[i,atr] == X′[only(pre), atr]
end || return nothing
push!(vec, only(pre))
Expand All @@ -199,6 +201,7 @@ function pull_back(f::ACSetTransformation, m::ACSetTransformation
# Check that attribute variables in L are mapped consistently
# i.e. L->X' doesn't send the variable to two different values.
for o in attrtypes(S)
T = attrtype_type(L, o)
comps[o] = map(AttrVar.(parts(L, o))) do i
vals = Set()
for (f, c, _) in attrs(S; to=o)
Expand All @@ -207,9 +210,9 @@ function pull_back(f::ACSetTransformation, m::ACSetTransformation
end
end
return only(vals)
end
end |> Vector{T}
end
ACSetTransformation(dom(m), dom(f); comps...)
ACSetTransformation(L, X′; comps...)
end

"""Get the pairs for each component of the image and its component"""
Expand Down
20 changes: 19 additions & 1 deletion test/incremental/Algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Test
using Catlab
using AlgebraicRewriting.Incremental.Algorithms:
connected_acset_components, all_subobjects, subobject_cache,
to_subobj, deattr
to_subobj, deattr, pull_back

# test connected_acset_components
#--------------------------------
Expand Down Expand Up @@ -37,5 +37,23 @@ G = SymmetricGraph(3) ⊕ cycle_graph(SymmetricGraph, 3)
#--------
@test deattr(SchWeightedGraph) == SchGraph

# Pull_back
#----------
@present SchAttr(FreeSchema) begin
X::Ob; D::AttrType; f::Attr(X,D); Y::Ob
end
@acset_type Attr(SchAttr)
D1 = @acset Attr{Symbol} begin X=1; f=[AttrVar(1)]; D=1 end
DXY = @acset Attr{Symbol} begin X=2; f=[:X,:Y] end
DX = @acset Attr{Symbol} begin X=1; f=[:X] end
toX,toY = homomorphisms(D1,DXY)
subX = homomorphism(DX,DXY)
toSubX = homomorphism(D1, DX)

pb = pull_back(subX, toX)
@test dom(pb) == dom(toX)
@test codom(pb) == dom(subX)
@test pb == toSubX
@test isnothing(pull_back(subX, toY))

end # module
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ using DataMigrations
#######

@testset "Full Demo" begin
# include("../docs/literate/full_demo.jl")
include("../docs/literate/full_demo.jl")
end

@testset "Lotka Volterra" begin
# include("../docs/literate/lotka_volterra.jl")
end

@testset "Game of Life" begin
include("../docs/literate/game_of_life.jl")
# include("../docs/literate/game_of_life.jl")
end

# Background
Expand Down
14 changes: 7 additions & 7 deletions test/schedules/Eval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ de = loop_rule(RuleApp(:del_edge, Rule(g2ar, id(g2))))
coin = uniform(2, z)
sched = coin (tryrule(av) id([z])) merge_wires(z) de

view_sched(sched, name="Simple schedule", names=N)
# view_sched(sched, name="Simple schedule", names=N)
G = path_graph(Grph, 2)

@test interpret!(sched, G) isa ACSetTransformation
@test ne(G) == 0

res = interpret(sched, path_graph(Grph, 2))
view_traj(sched, res, view_graph; agent=true, names=N)
# view_traj(sched, res, view_graph; agent=true, names=N)

# Query workflow (add loop to each vertex)
##########################################
Expand Down Expand Up @@ -83,9 +83,9 @@ sched = mk_sched((o=Dot,), (i=:Z,), N, Dict(:rule=>al, :query=>q),

typecheck(sched)

view_sched(sched; names=N)
# view_sched(sched; names=N)
res = interpret(sched, Grph(3));
view_traj(sched, res, view_graph; agent=true, names=N)
# view_traj(sched, res, view_graph; agent=true, names=N)


# Dependent query workflow
Expand All @@ -111,18 +111,18 @@ quote
return [trace1, trace2], out
end);

view_sched(sched; names=N)
# view_sched(sched; names=N)

G = @acset Grph begin V=5; E=4; src=[1,2,2,5];tgt=[2,3,4,2] end
arr_start = homomorphism(ar, G; initial=(V=[1,2],))
res = interpret(sched, arr_start);
view_traj(sched, res, view_graph; agent=true, names=N)
# view_traj(sched, res, view_graph; agent=true, names=N)
@test interpret!(sched, arr_start) isa ACSetTransformation

# For-loop: add 3 loops
#######################
sched = for_schedule(maybe_add_loop merge_wires(g1), 3);
view_sched(sched)
# view_sched(sched)
interpret!(sched, id(g1)) |> codom

# TODO add an attributed example
Expand Down

0 comments on commit e73be3f

Please sign in to comment.