From 8ce8eb785eae4c00fbc43ad502a17be4a9fa45ef Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Wed, 2 Aug 2023 17:27:14 -0400 Subject: [PATCH 01/15] inspect & pluck --- Project.toml | 3 ++- src/SymbolicUtils.jl | 2 ++ src/inspect.jl | 49 +++++++++++++++++++++++++++++++++++ src/types.jl | 48 ---------------------------------- test/basics.jl | 9 +++++++ test/inspect_output/ex.txt | 20 ++++++++++++++ test/inspect_output/sub10.txt | 5 ++++ test/inspect_output/sub14.txt | 7 +++++ test/runtests.jl | 1 + 9 files changed, 95 insertions(+), 49 deletions(-) create mode 100644 src/inspect.jl create mode 100644 test/inspect_output/ex.txt create mode 100644 test/inspect_output/sub10.txt create mode 100644 test/inspect_output/sub14.txt diff --git a/Project.toml b/Project.toml index 1f50aa38e..3b20c2408 100644 --- a/Project.toml +++ b/Project.toml @@ -50,8 +50,9 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" PkgBenchmark = "32113eaa-f34f-5b0d-bd6c-c81e245fc73d" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +ReferenceTests = "324d217c-45ce-50fc-942e-d289b448e8cf" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [targets] -test = ["BenchmarkTools", "Documenter", "Pkg", "PkgBenchmark", "Random", "Test", "Zygote"] +test = ["BenchmarkTools", "Documenter", "Pkg", "PkgBenchmark", "Random", "ReferenceTests", "Test", "Zygote"] diff --git a/src/SymbolicUtils.jl b/src/SymbolicUtils.jl index 2655263b2..1fb7d777a 100644 --- a/src/SymbolicUtils.jl +++ b/src/SymbolicUtils.jl @@ -27,6 +27,8 @@ include("methods.jl") # LinkedList, simplification utilities include("utils.jl") +# Tree inspection +include("inspect.jl") export Rewriters # A library for composing together expr -> expr functions diff --git a/src/inspect.jl b/src/inspect.jl new file mode 100644 index 000000000..5eaf52485 --- /dev/null +++ b/src/inspect.jl @@ -0,0 +1,49 @@ +import AbstractTrees + +function AbstractTrees.nodevalue(x::Symbolic) + istree(x) ? operation(x) : x +end +function AbstractTrees.nodevalue(x::BasicSymbolic) + if !istree(x) + exprtype(x) => x + elseif isadd(x) + exprtype(x) => (scalar=x.coeff, coeffs=Tuple(k=>v for (k,v) in x.dict)) + elseif ismul(x) + exprtype(x) => (scalar=x.coeff, powers=Tuple(k=>v for (k,v) in x.dict)) + else + exprtype(x) => operation(x) + end +end + +function AbstractTrees.children(x::Symbolic) + istree(x) ? arguments(x) : () +end + +""" + inspect([io::IO=stdout], expr; hint=true) + +Inspect an expression tree `expr`. Uses AbstractTrees to print out an expression. + +BasicSymbolic expressions will print the Unityper type (ADD, MUL, DIV, POW, SYM, TERM) and the relevant internals as the head, and the children in the subsequent lines as accessed by `arguments`. Other types will get printed as subtrees. + +Line numbers will be shown, use `pluck(expr, line_number)` to get the sub expression or leafnode starting at line_number. +""" +function inspect(io::IO, x::Symbolic; hint=true) + lines = readlines(IOBuffer(sprint(io->AbstractTrees.print_tree(io, x)))) + digits = ceil(Int, log10(length(lines))) + line_numbers = lpad.(string.(1:length(lines)), digits) + print(io, join(string.(line_numbers, " ", lines), "\n")) + hint && print(io, "\n\nHint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number") +end + +inspect(x::Symbolic; hint) = inspect(stdout, x; hint) + +""" + pluck(expr, n) + +Pluck the `n`th subexpression from `expr` as given by pre-order DFS. +This is the same as the node numbering in `inspect`. +""" +function pluck(x, item) + collect(Iterators.take(AbstractTrees.PreOrderDFS(x), item))[end] +end diff --git a/src/types.jl b/src/types.jl index e7f3e3ccd..81c161d12 100644 --- a/src/types.jl +++ b/src/types.jl @@ -559,54 +559,6 @@ function basic_similarterm(t, f, args, stype; metadata=nothing) end end -### -### Tree print -### - -import AbstractTrees - -struct TreePrint - op - x -end - -function AbstractTrees.children(x::BasicSymbolic) - if isterm(x) || ispow(x) - return arguments(x) - elseif isadd(x) || ismul(x) - children = Any[x.coeff] - for (key, coeff) in pairs(x.dict) - if coeff == 1 - push!(children, key) - else - push!(children, TreePrint(isadd(x) ? (:*) : (:^), (key, coeff))) - end - end - return children - end -end - -AbstractTrees.children(x::TreePrint) = [x.x[1], x.x[2]] - -print_tree(x; show_type=false, maxdepth=Inf, kw...) = print_tree(stdout, x; show_type=show_type, maxdepth=maxdepth, kw...) - -function print_tree(_io::IO, x::BasicSymbolic; show_type=false, kw...) - if isterm(x) || isadd(x) || ismul(x) || ispow(x) || isdiv(x) - AbstractTrees.print_tree(_io, x; withinds=true, kw...) do io, y, inds - if istree(y) - print(io, operation(y)) - elseif y isa TreePrint - print(io, "(", y.op, ")") - else - print(io, y) - end - if !(y isa TreePrint) && show_type - print(io, " [", typeof(y), "]") - end - end - end -end - ### ### Metadata ### diff --git a/test/basics.jl b/test/basics.jl index ad11ee90c..0bebff957 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -188,6 +188,15 @@ end @test repr((-1)^a) == "(-1)^a" end +@testset "inspect" begin + @syms x y z + ex = z*(2x + 3y + 1)^2/(z+2x) + @test_reference "inspect_output/ex.txt" sprint(io->SymbolicUtils.inspect(io, ex)) + @test SymbolicUtils.pluck(ex, 8) == 2 + @test_reference "inspect_output/sub10.txt" sprint(io->SymbolicUtils.inspect(io, SymbolicUtils.pluck(ex, 10))) + @test_reference "inspect_output/sub14.txt" sprint(io->SymbolicUtils.inspect(io, SymbolicUtils.pluck(ex, 14))) +end + @testset "similarterm" begin @syms a b c @test isequal(SymbolicUtils.similarterm((b + c), +, [a, (b+c)]).dict, Dict(a=>1,b=>1,c=>1)) diff --git a/test/inspect_output/ex.txt b/test/inspect_output/ex.txt new file mode 100644 index 000000000..ae3e22a5e --- /dev/null +++ b/test/inspect_output/ex.txt @@ -0,0 +1,20 @@ + 1 DIV=>(/) + 2 ├─ MUL=>(scalar = 1, powers = (z=>1, 1 + 2x + 3y=>2)) + 3 │ ├─ SYM=>z + 4 │ └─ POW=>(^) + 5 │ ├─ ADD=>(scalar = 1, coeffs = (y=>3, x=>2)) + 6 │ │ ├─ 1 + 7 │ │ ├─ MUL=>(scalar = 2, powers = (x=>1,)) + 8 │ │ │ ├─ 2 + 9 │ │ │ └─ SYM=>x +10 │ │ └─ MUL=>(scalar = 3, powers = (y=>1,)) +11 │ │ ├─ 3 +12 │ │ └─ SYM=>y +13 │ └─ 2 +14 └─ ADD=>(scalar = 0, coeffs = (z=>1, x=>2)) +15 ├─ SYM=>z +16 └─ MUL=>(scalar = 2, powers = (x=>1,)) +17 ├─ 2 +18 └─ SYM=>x + +Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file diff --git a/test/inspect_output/sub10.txt b/test/inspect_output/sub10.txt new file mode 100644 index 000000000..3f8c0c34f --- /dev/null +++ b/test/inspect_output/sub10.txt @@ -0,0 +1,5 @@ +1 MUL=>(scalar = 3, powers = (y=>1,)) +2 ├─ 3 +3 └─ SYM=>y + +Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file diff --git a/test/inspect_output/sub14.txt b/test/inspect_output/sub14.txt new file mode 100644 index 000000000..0b0909b8a --- /dev/null +++ b/test/inspect_output/sub14.txt @@ -0,0 +1,7 @@ +1 ADD=>(scalar = 0, coeffs = (z=>1, x=>2)) +2 ├─ SYM=>z +3 └─ MUL=>(scalar = 2, powers = (x=>1,)) +4 ├─ 2 +5 └─ SYM=>x + +Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 0c9ee4792..004b26b7d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,6 +2,7 @@ using Documenter using Pkg using Test using SymbolicUtils +using ReferenceTests import IfElse: ifelse DocMeta.setdocmeta!( From 9707af72a7435d63b382226e293b862a10d32eb0 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Thu, 3 Aug 2023 11:43:29 -0400 Subject: [PATCH 02/15] better printing and option to enable metadata printing --- src/inspect.jl | 33 ++++++++++++++++++++++++------- test/basics.jl | 3 +++ test/inspect_output/ex-md.txt | 20 +++++++++++++++++++ test/inspect_output/ex-nohint.txt | 18 +++++++++++++++++ test/inspect_output/ex.txt | 26 ++++++++++++------------ test/inspect_output/sub10.txt | 4 ++-- test/inspect_output/sub14.txt | 8 ++++---- 7 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 test/inspect_output/ex-md.txt create mode 100644 test/inspect_output/ex-nohint.txt diff --git a/src/inspect.jl b/src/inspect.jl index 5eaf52485..dc9c429fc 100644 --- a/src/inspect.jl +++ b/src/inspect.jl @@ -1,18 +1,29 @@ import AbstractTrees +const inspect_metadata = Ref{Bool}(false) function AbstractTrees.nodevalue(x::Symbolic) istree(x) ? operation(x) : x end + function AbstractTrees.nodevalue(x::BasicSymbolic) - if !istree(x) - exprtype(x) => x + str = if !istree(x) + string(exprtype(x), "(", x, ")") elseif isadd(x) - exprtype(x) => (scalar=x.coeff, coeffs=Tuple(k=>v for (k,v) in x.dict)) + string(exprtype(x), + (scalar=x.coeff, coeffs=Tuple(k=>v for (k,v) in x.dict))) elseif ismul(x) - exprtype(x) => (scalar=x.coeff, powers=Tuple(k=>v for (k,v) in x.dict)) + string(exprtype(x), + (scalar=x.coeff, powers=Tuple(k=>v for (k,v) in x.dict))) + elseif isdiv(x) || ispow(x) + string(exprtype(x)) else - exprtype(x) => operation(x) + string(exprtype(x),"{", operation(x), "}") + end + + if inspect_metadata[] && !isnothing(metadata(x)) + str *= string(" metadata=", Tuple(k=>v for (k, v) in metadata(x))) end + Text(str) end function AbstractTrees.children(x::Symbolic) @@ -28,15 +39,23 @@ BasicSymbolic expressions will print the Unityper type (ADD, MUL, DIV, POW, SYM, Line numbers will be shown, use `pluck(expr, line_number)` to get the sub expression or leafnode starting at line_number. """ -function inspect(io::IO, x::Symbolic; hint=true) +function inspect(io::IO, x::Symbolic; + hint=true, + metadata=inspect_metadata[]) + + prev_state = inspect_metadata[] + inspect_metadata[] = metadata lines = readlines(IOBuffer(sprint(io->AbstractTrees.print_tree(io, x)))) + inspect_metadata[] = prev_state digits = ceil(Int, log10(length(lines))) line_numbers = lpad.(string.(1:length(lines)), digits) print(io, join(string.(line_numbers, " ", lines), "\n")) hint && print(io, "\n\nHint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number") end -inspect(x::Symbolic; hint) = inspect(stdout, x; hint) +function inspect(x::Symbolic; hint=true, metadata=inspect_metadata[]) + inspect(stdout, x; hint=hint, metadata=metadata) +end """ pluck(expr, n) diff --git a/test/basics.jl b/test/basics.jl index 0bebff957..bd943396a 100644 --- a/test/basics.jl +++ b/test/basics.jl @@ -190,8 +190,11 @@ end @testset "inspect" begin @syms x y z + y = SymbolicUtils.setmetadata(y, Integer, 42) # Set some metadata ex = z*(2x + 3y + 1)^2/(z+2x) @test_reference "inspect_output/ex.txt" sprint(io->SymbolicUtils.inspect(io, ex)) + @test_reference "inspect_output/ex-md.txt" sprint(io->SymbolicUtils.inspect(io, ex, metadata=true)) + @test_reference "inspect_output/ex-nohint.txt" sprint(io->SymbolicUtils.inspect(io, ex, hint=false)) @test SymbolicUtils.pluck(ex, 8) == 2 @test_reference "inspect_output/sub10.txt" sprint(io->SymbolicUtils.inspect(io, SymbolicUtils.pluck(ex, 10))) @test_reference "inspect_output/sub14.txt" sprint(io->SymbolicUtils.inspect(io, SymbolicUtils.pluck(ex, 14))) diff --git a/test/inspect_output/ex-md.txt b/test/inspect_output/ex-md.txt new file mode 100644 index 000000000..4d7a330f3 --- /dev/null +++ b/test/inspect_output/ex-md.txt @@ -0,0 +1,20 @@ + 1 DIV + 2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) + 3 │ ├─ SYM(z) + 4 │ └─ POW + 5 │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2)) + 6 │ │ ├─ 1 + 7 │ │ ├─ MUL(scalar = 2, powers = (x => 1,)) + 8 │ │ │ ├─ 2 + 9 │ │ │ └─ SYM(x) +10 │ │ └─ MUL(scalar = 3, powers = (y => 1,)) +11 │ │ ├─ 3 +12 │ │ └─ SYM(y) metadata=(Integer => 42,) +13 │ └─ 2 +14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2)) +15 ├─ SYM(z) +16 └─ MUL(scalar = 2, powers = (x => 1,)) +17 ├─ 2 +18 └─ SYM(x) + +Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file diff --git a/test/inspect_output/ex-nohint.txt b/test/inspect_output/ex-nohint.txt new file mode 100644 index 000000000..43da94e62 --- /dev/null +++ b/test/inspect_output/ex-nohint.txt @@ -0,0 +1,18 @@ + 1 DIV + 2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) + 3 │ ├─ SYM(z) + 4 │ └─ POW + 5 │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2)) + 6 │ │ ├─ 1 + 7 │ │ ├─ MUL(scalar = 2, powers = (x => 1,)) + 8 │ │ │ ├─ 2 + 9 │ │ │ └─ SYM(x) +10 │ │ └─ MUL(scalar = 3, powers = (y => 1,)) +11 │ │ ├─ 3 +12 │ │ └─ SYM(y) +13 │ └─ 2 +14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2)) +15 ├─ SYM(z) +16 └─ MUL(scalar = 2, powers = (x => 1,)) +17 ├─ 2 +18 └─ SYM(x) \ No newline at end of file diff --git a/test/inspect_output/ex.txt b/test/inspect_output/ex.txt index ae3e22a5e..9a5c5c4fa 100644 --- a/test/inspect_output/ex.txt +++ b/test/inspect_output/ex.txt @@ -1,20 +1,20 @@ - 1 DIV=>(/) - 2 ├─ MUL=>(scalar = 1, powers = (z=>1, 1 + 2x + 3y=>2)) - 3 │ ├─ SYM=>z - 4 │ └─ POW=>(^) - 5 │ ├─ ADD=>(scalar = 1, coeffs = (y=>3, x=>2)) + 1 DIV + 2 ├─ MUL(scalar = 1, powers = (z => 1, 1 + 2x + 3y => 2)) + 3 │ ├─ SYM(z) + 4 │ └─ POW + 5 │ ├─ ADD(scalar = 1, coeffs = (y => 3, x => 2)) 6 │ │ ├─ 1 - 7 │ │ ├─ MUL=>(scalar = 2, powers = (x=>1,)) + 7 │ │ ├─ MUL(scalar = 2, powers = (x => 1,)) 8 │ │ │ ├─ 2 - 9 │ │ │ └─ SYM=>x -10 │ │ └─ MUL=>(scalar = 3, powers = (y=>1,)) + 9 │ │ │ └─ SYM(x) +10 │ │ └─ MUL(scalar = 3, powers = (y => 1,)) 11 │ │ ├─ 3 -12 │ │ └─ SYM=>y +12 │ │ └─ SYM(y) 13 │ └─ 2 -14 └─ ADD=>(scalar = 0, coeffs = (z=>1, x=>2)) -15 ├─ SYM=>z -16 └─ MUL=>(scalar = 2, powers = (x=>1,)) +14 └─ ADD(scalar = 0, coeffs = (z => 1, x => 2)) +15 ├─ SYM(z) +16 └─ MUL(scalar = 2, powers = (x => 1,)) 17 ├─ 2 -18 └─ SYM=>x +18 └─ SYM(x) Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file diff --git a/test/inspect_output/sub10.txt b/test/inspect_output/sub10.txt index 3f8c0c34f..f651d4312 100644 --- a/test/inspect_output/sub10.txt +++ b/test/inspect_output/sub10.txt @@ -1,5 +1,5 @@ -1 MUL=>(scalar = 3, powers = (y=>1,)) +1 MUL(scalar = 3, powers = (y => 1,)) 2 ├─ 3 -3 └─ SYM=>y +3 └─ SYM(y) Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file diff --git a/test/inspect_output/sub14.txt b/test/inspect_output/sub14.txt index 0b0909b8a..e905378e9 100644 --- a/test/inspect_output/sub14.txt +++ b/test/inspect_output/sub14.txt @@ -1,7 +1,7 @@ -1 ADD=>(scalar = 0, coeffs = (z=>1, x=>2)) -2 ├─ SYM=>z -3 └─ MUL=>(scalar = 2, powers = (x=>1,)) +1 ADD(scalar = 0, coeffs = (z => 1, x => 2)) +2 ├─ SYM(z) +3 └─ MUL(scalar = 2, powers = (x => 1,)) 4 ├─ 2 -5 └─ SYM=>x +5 └─ SYM(x) Hint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number \ No newline at end of file From 09cce3e34f433959d231cd4c73f23c1a2b9bf867 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Thu, 3 Aug 2023 11:46:06 -0400 Subject: [PATCH 03/15] add metadata to docstring --- src/inspect.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/inspect.jl b/src/inspect.jl index dc9c429fc..a968b3b3c 100644 --- a/src/inspect.jl +++ b/src/inspect.jl @@ -31,14 +31,16 @@ function AbstractTrees.children(x::Symbolic) end """ - inspect([io::IO=stdout], expr; hint=true) + inspect([io::IO=stdout], expr; hint=true, metadata=false) Inspect an expression tree `expr`. Uses AbstractTrees to print out an expression. -BasicSymbolic expressions will print the Unityper type (ADD, MUL, DIV, POW, SYM, TERM) and the relevant internals as the head, and the children in the subsequent lines as accessed by `arguments`. Other types will get printed as subtrees. +BasicSymbolic expressions will print the Unityper type (ADD, MUL, DIV, POW, SYM, TERM) and the relevant internals as the head, and the children in the subsequent lines as accessed by `arguments`. Other types will get printed as subtrees. Set `metadata=true` to print any metadata carried by the nodes. Line numbers will be shown, use `pluck(expr, line_number)` to get the sub expression or leafnode starting at line_number. """ +function inspect end + function inspect(io::IO, x::Symbolic; hint=true, metadata=inspect_metadata[]) From b9a8c61e81cc39578efd8989371e8af9fb40ec06 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Fri, 4 Aug 2023 14:38:09 -0400 Subject: [PATCH 04/15] which test actually fails? --- test/fuzz.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/fuzz.jl b/test/fuzz.jl index 8303fd165..eff90469f 100644 --- a/test/fuzz.jl +++ b/test/fuzz.jl @@ -3,7 +3,6 @@ include("fuzzlib.jl") using Random: seed! seed!(6174) -@testset "Fuzz test" begin @time @testset "expand fuzz" begin for i=1:500 i % 100 == 0 && @info "expand fuzz" iter=i @@ -45,4 +44,3 @@ seed!(6174) fuzz_addmulpow(4) end end -end From bd4f3c2f2ac365c28edd740089855785708866e2 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 7 Aug 2023 15:37:58 -0400 Subject: [PATCH 05/15] log stuff --- test/fuzzlib.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index 80133ad78..d4e0063c2 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -93,6 +93,8 @@ function gen_rand_expr(inputs; depth=depth+1, min_depth=min_depth, max_depth=max_depth) for i in 1:arity] + @show f + @show args try return f(args...) catch err @@ -121,6 +123,7 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) inputs = Set() expr = gen_rand_expr(inputs; spec=spec, kwargs...) inputs = collect(inputs) + @show expr code = try SymbolicUtils.Code.toexpr(expr) catch err From 77d4c3499dbbdaf4eb2bcd7d5d09b9e4304880b6 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 7 Aug 2023 15:52:51 -0400 Subject: [PATCH 06/15] show args --- test/fuzzlib.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index d4e0063c2..01976c0a5 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -123,7 +123,6 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) inputs = Set() expr = gen_rand_expr(inputs; spec=spec, kwargs...) inputs = collect(inputs) - @show expr code = try SymbolicUtils.Code.toexpr(expr) catch err @@ -142,6 +141,8 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) """ f = include_string(Main, unsimplifiedstr) g = include_string(Main, simplifiedstr) + @show g + @show args for i=1:ntrials args = [spec.input(i) for i in inputs] From 0846f30fc2cb55c3441f96b894e6e5c4ad630a98 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 7 Aug 2023 15:59:18 -0400 Subject: [PATCH 07/15] show args --- test/fuzzlib.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index 01976c0a5..bba3b9393 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -142,10 +142,10 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) f = include_string(Main, unsimplifiedstr) g = include_string(Main, simplifiedstr) @show g - @show args for i=1:ntrials args = [spec.input(i) for i in inputs] + @show args unsimplified = try Base.invokelatest(f, args...) catch err From a3eb35dd6cd6dce730d3cd29f7374466bc4910c3 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 7 Aug 2023 16:14:48 -0400 Subject: [PATCH 08/15] change random seed --- test/fuzz.jl | 2 +- test/fuzzlib.jl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/test/fuzz.jl b/test/fuzz.jl index eff90469f..25de3eeca 100644 --- a/test/fuzz.jl +++ b/test/fuzz.jl @@ -2,7 +2,7 @@ include("fuzzlib.jl") using Random: seed! -seed!(6174) +seed!(6175) @time @testset "expand fuzz" begin for i=1:500 i % 100 == 0 && @info "expand fuzz" iter=i diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index bba3b9393..2608eb6c0 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -123,6 +123,7 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) inputs = Set() expr = gen_rand_expr(inputs; spec=spec, kwargs...) inputs = collect(inputs) + @show expr code = try SymbolicUtils.Code.toexpr(expr) catch err From 3698d278f8736d966f5d093a45b5f5061336d042 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 7 Aug 2023 16:55:43 -0400 Subject: [PATCH 09/15] try harder to get a repro --- test/fuzz.jl | 2 +- test/fuzzlib.jl | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/fuzz.jl b/test/fuzz.jl index 25de3eeca..eff90469f 100644 --- a/test/fuzz.jl +++ b/test/fuzz.jl @@ -2,7 +2,7 @@ include("fuzzlib.jl") using Random: seed! -seed!(6175) +seed!(6174) @time @testset "expand fuzz" begin for i=1:500 i % 100 == 0 && @info "expand fuzz" iter=i diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index 2608eb6c0..8b9d4e7ce 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -142,7 +142,8 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) """ f = include_string(Main, unsimplifiedstr) g = include_string(Main, simplifiedstr) - @show g + @show unsimplifiedstr + @show simplifiedstr for i=1:ntrials args = [spec.input(i) for i in inputs] From 6f2955b83be3594bb5427d37e8a8a5abd0ed32f4 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Tue, 8 Aug 2023 09:56:32 -0400 Subject: [PATCH 10/15] try a different seed --- test/fuzz.jl | 2 +- test/fuzzlib.jl | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/test/fuzz.jl b/test/fuzz.jl index eff90469f..08c90e1fc 100644 --- a/test/fuzz.jl +++ b/test/fuzz.jl @@ -2,7 +2,7 @@ include("fuzzlib.jl") using Random: seed! -seed!(6174) +seed!(8259) @time @testset "expand fuzz" begin for i=1:500 i % 100 == 0 && @info "expand fuzz" iter=i diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index 8b9d4e7ce..80133ad78 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -93,8 +93,6 @@ function gen_rand_expr(inputs; depth=depth+1, min_depth=min_depth, max_depth=max_depth) for i in 1:arity] - @show f - @show args try return f(args...) catch err @@ -123,7 +121,6 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) inputs = Set() expr = gen_rand_expr(inputs; spec=spec, kwargs...) inputs = collect(inputs) - @show expr code = try SymbolicUtils.Code.toexpr(expr) catch err @@ -142,12 +139,9 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...) """ f = include_string(Main, unsimplifiedstr) g = include_string(Main, simplifiedstr) - @show unsimplifiedstr - @show simplifiedstr for i=1:ntrials args = [spec.input(i) for i in inputs] - @show args unsimplified = try Base.invokelatest(f, args...) catch err From 12010d1588e908c9e055c11289b995bbb5fb1d83 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Mon, 14 Aug 2023 15:59:16 -0400 Subject: [PATCH 11/15] avoid --- test/fuzz.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fuzz.jl b/test/fuzz.jl index 08c90e1fc..76edefca1 100644 --- a/test/fuzz.jl +++ b/test/fuzz.jl @@ -2,7 +2,7 @@ include("fuzzlib.jl") using Random: seed! -seed!(8259) +seed!(8258) @time @testset "expand fuzz" begin for i=1:500 i % 100 == 0 && @info "expand fuzz" iter=i From 1bac99fa068bc0bbb61f39de94e125e698239847 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Wed, 16 Aug 2023 15:31:42 -0400 Subject: [PATCH 12/15] remove also besselj from fuzzer --- test/fuzzlib.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index 80133ad78..37544deb7 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -43,7 +43,7 @@ const num_spec = let ()->rand([a b c d e f])] binops = SymbolicUtils.diadic - nopow = filter(x->x!==(^), binops) + nopow = filter(x->x!==(^) || x == besselj, binops) twoargfns = vcat(nopow, (x,y)->x isa Union{Int, Rational, Complex{<:Rational}} ? x * y : x^y) fns = vcat(1 .=> vcat(SymbolicUtils.monadic, [one, zero]), 2 .=> vcat(twoargfns, fill(+, 5), [-,-], fill(*, 5), fill(/, 40)), From 9485f2d19848610145248746011612f1fa300ef6 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Wed, 30 Aug 2023 17:53:44 -0400 Subject: [PATCH 13/15] add inspect for non-symbolic objects so user code does not have to check --- src/inspect.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/inspect.jl b/src/inspect.jl index a968b3b3c..f62551893 100644 --- a/src/inspect.jl +++ b/src/inspect.jl @@ -55,10 +55,12 @@ function inspect(io::IO, x::Symbolic; hint && print(io, "\n\nHint: call SymbolicUtils.pluck(expr, line_number) to get the subexpression starting at line_number") end -function inspect(x::Symbolic; hint=true, metadata=inspect_metadata[]) +function inspect(x; hint=true, metadata=inspect_metadata[]) inspect(stdout, x; hint=hint, metadata=metadata) end +inspect(io::IO, x; kw...) = println(io, "Not Symbolic: $x") + """ pluck(expr, n) From 9f8585bfa3434a7165bf4cee32b02d21eadff8b9 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Thu, 31 Aug 2023 11:46:56 -0400 Subject: [PATCH 14/15] don't test besselj --- test/fuzzlib.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index 37544deb7..e4f8d0c3a 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -43,7 +43,7 @@ const num_spec = let ()->rand([a b c d e f])] binops = SymbolicUtils.diadic - nopow = filter(x->x!==(^) || x == besselj, binops) + nopow = setdiff(binops, [(^), besselj]) twoargfns = vcat(nopow, (x,y)->x isa Union{Int, Rational, Complex{<:Rational}} ? x * y : x^y) fns = vcat(1 .=> vcat(SymbolicUtils.monadic, [one, zero]), 2 .=> vcat(twoargfns, fill(+, 5), [-,-], fill(*, 5), fill(/, 40)), From 33278b63a3a07c1d20ecde4d8c24d0f9b92cc870 Mon Sep 17 00:00:00 2001 From: Shashi Gowda Date: Thu, 31 Aug 2023 13:47:02 -0400 Subject: [PATCH 15/15] don't test any bessel funcs --- test/fuzzlib.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fuzzlib.jl b/test/fuzzlib.jl index e4f8d0c3a..adac553cf 100644 --- a/test/fuzzlib.jl +++ b/test/fuzzlib.jl @@ -43,7 +43,7 @@ const num_spec = let ()->rand([a b c d e f])] binops = SymbolicUtils.diadic - nopow = setdiff(binops, [(^), besselj]) + nopow = setdiff(binops, [(^), besselj0, besselj1, bessely0, bessely1, besselj, bessely, besseli, besselk]) twoargfns = vcat(nopow, (x,y)->x isa Union{Int, Rational, Complex{<:Rational}} ? x * y : x^y) fns = vcat(1 .=> vcat(SymbolicUtils.monadic, [one, zero]), 2 .=> vcat(twoargfns, fill(+, 5), [-,-], fill(*, 5), fill(/, 40)),