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

Minor syntactic improvements to data migration macros #627

Merged
merged 2 commits into from
Apr 27, 2022
Merged
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
28 changes: 12 additions & 16 deletions src/programs/DiagrammaticPrograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,17 @@ function parse_ob_hom_maps(C::FinCat, body::Expr; allow_missing::Bool=false)
end
end
ob_rhs = make_map(ob_generators(C)) do x
get(assignments, ob_name(C, x)) do
allow_missing ? missing : error("Object $(ob_name(C,x)) is not assigned")
end
y = pop!(assignments, ob_name(C, x), missing)
(!ismissing(y) || allow_missing) ? y :
error("Object $(ob_name(C,x)) is not assigned")
end
hom_rhs = make_map(hom_generators(C)) do f
get(assignments, hom_name(C, f)) do
allow_missing ? missing : error("Morphism $(hom_name(C,f)) is not assigned")
end
g = pop!(assignments, hom_name(C, f), missing)
(!ismissing(g) || allow_missing) ? g :
error("Morphism $(hom_name(C,f)) is not assigned")
end
isempty(assignments) ||
error(string("Unused assignment(s): ", join(keys(assignments), ", ")))
(ob_rhs, hom_rhs)
end

Expand Down Expand Up @@ -522,8 +524,7 @@ This macro provides a DSL to specify a contravariant data migration from
defined by a functor from ``D`` to a category of queries on ``C``. Thus, every
object of ``D`` is assigned a query on ``C`` and every morphism of ``D`` is
assigned a morphism of queries, in a compatible way. Example usages are in the
unit tests and the AlgebraicJulia blog (TODO: link). What follows is a technical
reference.
unit tests. What follows is a technical reference.

Several categories of queries are supported by this macro:

Expand Down Expand Up @@ -701,6 +702,7 @@ function parse_conj_query_ob_rhs(C::FinCat, expr, d::DiagramData{op}, c′)
Expr(:tuple, x::Symbol, f) => (x, f)
Expr(:call, op, _...) && if op ∈ compose_ops end =>
leftmost_arg(expr, (:(⋅), :(⨟)), all_ops=compose_ops)
Expr(:call, name::Symbol, _) => reverse(destructure_unary_call(expr))
_ => error("Cannot parse object assignment in migration: $expr")
end
j = ob_named(shape(d), j_name)
Expand Down Expand Up @@ -766,10 +768,7 @@ function make_query_hom(f::DiagramHomData{op}, d::Diagram{op}, d′::Diagram{op}
j => j
end
end
f_hom = mapvals(f.hom_map) do h; @match h begin
::Missing => only_hom(shape(d))
_ => h
end end
f_hom = mapvals(h -> ismissing(h) ? only_hom(shape(d)) : h, f.hom_map)
DiagramHom{op}(f_ob, f_hom, d, d′)
end

Expand All @@ -785,10 +784,7 @@ function make_query_hom(f::DiagramHomData{id}, d::Diagram{id}, d′::Diagram{id}
j′ => j′
end
end
f_hom = mapvals(f.hom_map) do h; @match h begin
::Missing => only_hom(shape(d′))
_ => h
end end
f_hom = mapvals(h -> ismissing(h) ? only_hom(shape(d′)) : h, f.hom_map)
DiagramHom{id}(f_ob, f_hom, d, d′)
end

Expand Down
55 changes: 42 additions & 13 deletions test/programs/DiagrammaticPrograms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,34 @@ end
TheoryGraph, ThCPortGraph)

# Incomplete definition.
@test_throws ErrorException @finfunctor(TheoryGraph, ThCPortGraph, begin
V => Box
src => src ⨟ box
tgt => tgt ⨟ box
end)
@test_throws ErrorException begin
@finfunctor TheoryGraph ThCPortGraph begin
V => Box
src => src ⨟ box
tgt => tgt ⨟ box
end
end

# Failure of functorality.
@test_throws ErrorException (@finfunctor TheoryGraph ThCPortGraph begin
V => Box
E => Wire
src => src
tgt => tgt
end)
@test_throws ErrorException begin
@finfunctor TheoryGraph ThCPortGraph begin
V => Box
E => Wire
src => src
tgt => tgt
end
end

# Extra definitions.
@test_throws ErrorException begin
@finfunctor TheoryGraph TheoryReflexiveGraph begin
V => Box
E => Wire
src => src
tgt => tgt
refl => refl
end
end

# GAT expressions.
F = @finfunctor TheoryDDS TheoryDDS begin
Expand Down Expand Up @@ -223,6 +238,20 @@ F_E = diagram(ob_map(F, :E))
F_tgt = hom_map(F, :tgt)
@test collect_ob(F_tgt) == [(3, TheoryGraph[:tgt])]

# Syntactic variant of above.
F′ = @migration TheoryGraph TheoryGraph begin
V => V
E => @join begin
v::V
(e₁, e₂)::E
tgt(e₁) == v
src(e₂) == v
end
src => src(e₁)
tgt => tgt(e₂)
end
@test F′ == F

# Cartesian product of graph with itself.
F = @migration TheoryGraph TheoryGraph begin
V => @product (v₁::V; v₂::V)
Expand Down Expand Up @@ -357,8 +386,8 @@ F = @migration TheoryGraph TheoryGraph begin
path => @join begin
v::V
(e₁, e₂)::E
tgt(e₁) == v
src(e₂) == v
(e₁ → v)::tgt
(e₂ → v)::src
end
end
src => begin
Expand Down