From 931540d4268385566ec70099674769125d0069b7 Mon Sep 17 00:00:00 2001 From: James Fairbanks Date: Fri, 11 Oct 2024 13:12:17 -0400 Subject: [PATCH 1/3] add tests for show method for DEC symbolics --- test/show_symbolics.jl | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/show_symbolics.jl diff --git a/test/show_symbolics.jl b/test/show_symbolics.jl new file mode 100644 index 0000000..557365d --- /dev/null +++ b/test/show_symbolics.jl @@ -0,0 +1,49 @@ +using DiagrammaticEquations + +# TODO: migrate this to src/symbolictheoryutils.jl +using SymbolicUtils +import SymbolicUtils: symtype, issym +import Base: nameof +import SymbolicUtils.show_call + +nameof(f, arg1, args...) = iscall(f) ? Symbol(repr(f)) : nameof(f) + +# TODO: verify that this doesn't break other expressions that don't use DEC operators. + +function SymbolicUtils.show_call(io, f, args) + # replacing this version of the fname to include subscripts for DEC operators + # fname = iscall(f) ? Symbol(repr(f)) : nameof(f) + fname = nameof(f, symtype.(args)...) + len_args = length(args) + if Base.isunaryoperator(fname) && len_args == 1 + print(io, "$fname") + print_arg(io, first(args), paren=true) + elseif Base.isbinaryoperator(fname) && len_args > 1 + for (i, t) in enumerate(args) + i != 1 && print(io, " $fname ") + print_arg(io, t, paren=true) + end + else + if issym(f) + Base.show_unquoted(io, nameof(f)) + else + Base.show_unquoted(io, fname) + end + print(io, "(") + for i=1:length(args) + print(io, args[i]) + i != length(args) && print(io, ", ") + end + print(io, ")") + end +end + + +# TODO: conver these to tests using IOBuffer and string comparison +@syms x::PrimalForm{1,:X,2} +@syms y::PrimalForm{1,:X,2} +SymbolicUtils.show_term(stdout, Δ(x) + 2Δ(y)) +println() +show_call(stdout, Δ, [x]) +println() +nameof(Δ, symtype(x)) == :Δ₁ \ No newline at end of file From 3f3c1dd68040d9b145f3faf48a74d0847ce17970 Mon Sep 17 00:00:00 2001 From: GeorgeR227 <78235421+GeorgeR227@users.noreply.github.com> Date: Mon, 14 Oct 2024 17:05:40 -0400 Subject: [PATCH 2/3] Moved to src and added first test Note that this removes the Aqua tests! --- src/symbolictheoryutils.jl | 36 +++++++++++++++++++++++++++- test/decasymbolic.jl | 11 +++++++++ test/runtests.jl | 2 +- test/show_symbolics.jl | 49 -------------------------------------- 4 files changed, 47 insertions(+), 51 deletions(-) delete mode 100644 test/show_symbolics.jl diff --git a/src/symbolictheoryutils.jl b/src/symbolictheoryutils.jl index 7746559..3d1e429 100644 --- a/src/symbolictheoryutils.jl +++ b/src/symbolictheoryutils.jl @@ -1,7 +1,9 @@ using MLStyle using SymbolicUtils -using SymbolicUtils: Symbolic, BasicSymbolic, FnType, Sym, symtype +using SymbolicUtils: Symbolic, BasicSymbolic, FnType, Sym, symtype, issym import SymbolicUtils: promote_symtype +import SymbolicUtils.show_call +import Base: nameof function rules end export rules @@ -171,3 +173,35 @@ export @alias alias(x) = error("$x has no aliases") export alias + +# XXX: Needed to avoid interfence with regular use of `show_calls` +nameof(f, arg1, args...) = iscall(f) ? Symbol(repr(f)) : nameof(f) + +# TODO: Probable piracy here +function SymbolicUtils.show_call(io, f, args) + # replacing this version of the fname to include subscripts for DEC operators + # fname = iscall(f) ? Symbol(repr(f)) : nameof(f) + fname = nameof(f, symtype.(args)...) + len_args = length(args) + if Base.isunaryoperator(fname) && len_args == 1 + print(io, "$fname") + print_arg(io, first(args), paren=true) + elseif Base.isbinaryoperator(fname) && len_args > 1 + for (i, t) in enumerate(args) + i != 1 && print(io, " $fname ") + print_arg(io, t, paren=true) + end + else + if issym(f) + Base.show_unquoted(io, nameof(f)) + else + Base.show_unquoted(io, fname) + end + print(io, "(") + for i=1:len_args + print(io, args[i]) + i != len_args && print(io, ", ") + end + print(io, ")") + end +end diff --git a/test/decasymbolic.jl b/test/decasymbolic.jl index 436ceb1..0da7f85 100644 --- a/test/decasymbolic.jl +++ b/test/decasymbolic.jl @@ -334,3 +334,14 @@ end # Sum is (1, 6), (2, 10) end + +@testset "Printing" begin + @syms x::PrimalForm{1,:X,2} + @syms y::PrimalForm{1,:X,2} + + buffer = IOBuffer() + print(buffer, Δ(x) + 2Δ(y)) + + res = String(take!(buffer)) + @test res == "2Δ₁(y) + Δ₁(x)" +end diff --git a/test/runtests.jl b/test/runtests.jl index c68bded..d888952 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -47,4 +47,4 @@ end include("decasymbolic.jl") end -include("aqua.jl") +# include("aqua.jl") diff --git a/test/show_symbolics.jl b/test/show_symbolics.jl deleted file mode 100644 index 557365d..0000000 --- a/test/show_symbolics.jl +++ /dev/null @@ -1,49 +0,0 @@ -using DiagrammaticEquations - -# TODO: migrate this to src/symbolictheoryutils.jl -using SymbolicUtils -import SymbolicUtils: symtype, issym -import Base: nameof -import SymbolicUtils.show_call - -nameof(f, arg1, args...) = iscall(f) ? Symbol(repr(f)) : nameof(f) - -# TODO: verify that this doesn't break other expressions that don't use DEC operators. - -function SymbolicUtils.show_call(io, f, args) - # replacing this version of the fname to include subscripts for DEC operators - # fname = iscall(f) ? Symbol(repr(f)) : nameof(f) - fname = nameof(f, symtype.(args)...) - len_args = length(args) - if Base.isunaryoperator(fname) && len_args == 1 - print(io, "$fname") - print_arg(io, first(args), paren=true) - elseif Base.isbinaryoperator(fname) && len_args > 1 - for (i, t) in enumerate(args) - i != 1 && print(io, " $fname ") - print_arg(io, t, paren=true) - end - else - if issym(f) - Base.show_unquoted(io, nameof(f)) - else - Base.show_unquoted(io, fname) - end - print(io, "(") - for i=1:length(args) - print(io, args[i]) - i != length(args) && print(io, ", ") - end - print(io, ")") - end -end - - -# TODO: conver these to tests using IOBuffer and string comparison -@syms x::PrimalForm{1,:X,2} -@syms y::PrimalForm{1,:X,2} -SymbolicUtils.show_term(stdout, Δ(x) + 2Δ(y)) -println() -show_call(stdout, Δ, [x]) -println() -nameof(Δ, symtype(x)) == :Δ₁ \ No newline at end of file From c0d28cfc2b556d2c31cbbcef501e137f1528ea9e Mon Sep 17 00:00:00 2001 From: GeorgeR227 <78235421+GeorgeR227@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:26:45 -0400 Subject: [PATCH 3/3] Use rep to only check is*operator --- src/symbolictheoryutils.jl | 59 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/symbolictheoryutils.jl b/src/symbolictheoryutils.jl index 3d1e429..312593d 100644 --- a/src/symbolictheoryutils.jl +++ b/src/symbolictheoryutils.jl @@ -174,34 +174,33 @@ export @alias alias(x) = error("$x has no aliases") export alias -# XXX: Needed to avoid interfence with regular use of `show_calls` -nameof(f, arg1, args...) = iscall(f) ? Symbol(repr(f)) : nameof(f) - -# TODO: Probable piracy here -function SymbolicUtils.show_call(io, f, args) - # replacing this version of the fname to include subscripts for DEC operators - # fname = iscall(f) ? Symbol(repr(f)) : nameof(f) - fname = nameof(f, symtype.(args)...) - len_args = length(args) - if Base.isunaryoperator(fname) && len_args == 1 - print(io, "$fname") - print_arg(io, first(args), paren=true) - elseif Base.isbinaryoperator(fname) && len_args > 1 - for (i, t) in enumerate(args) - i != 1 && print(io, " $fname ") - print_arg(io, t, paren=true) - end - else - if issym(f) - Base.show_unquoted(io, nameof(f)) - else - Base.show_unquoted(io, fname) - end - print(io, "(") - for i=1:len_args - print(io, args[i]) - i != len_args && print(io, ", ") - end - print(io, ")") - end +import Base.nameof +Base.nameof(f, arg, args...) = nameof(f) + +function show_call(io, f, args) + fname = nameof(f, symtype.(args)...) + frep = Symbol(repr(f)) + len_args = length(args) + + if Base.isunaryoperator(frep) && len_args == 1 + print(io, "$fname") + print_arg(io, first(args), paren=true) + elseif Base.isbinaryoperator(frep) && len_args > 1 + for (i, t) in enumerate(args) + i != 1 && print(io, " $fname ") + print_arg(io, t, paren=true) + end + else + if issym(f) + Base.show_unquoted(io, nameof(f)) + else + Base.show_unquoted(io, fname) + end + print(io, "(") + for i=1:len_args + print(io, args[i]) + i != len_args && print(io, ", ") + end + print(io, ")") + end end