Skip to content

Commit

Permalink
Fix bugs due to changes in Julia AST and sympy
Browse files Browse the repository at this point in the history
Several changes in the Julia AST, sympy, and elsewhere have broken Symata.
This PR allows sympy to compile/load and some of the tests to pass.
  • Loading branch information
jlapeyre committed Apr 20, 2021
1 parent d78efa6 commit 440f59b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
12 changes: 6 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name = "Symata"
uuid = "a906b1d5-d016-55c4-aab3-8a20cba0db2a"
license = "MIT"
desc = "language for symbolic mathematics"
authors = ["John Lapeyre"]
license = "MIT"
version = "0.4.8"
uuid = "a906b1d5-d016-55c4-aab3-8a20cba0db2a"
version = "0.4.9"

[deps]
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
Expand Down Expand Up @@ -32,13 +32,13 @@ Combinatorics = "0.7, 1.0"
CommonSubexpressions = "0.2"
Conda = "1.1, 1.2, 1.3"
DataStructures = "0.15, 0.16, 0.17"
Formatting = "0.3, 0.4"
Formatting = "0.3, 0.4, 0.4.2"
IterTools = "1.2, 1.3"
MacroTools = "0.4, 0.5"
MacroTools = "0.4, 0.5, 0.5.6"
Memoize = "0.3, 0.4"
Polynomials = "0.5, 0.6"
Primes = "0.2, 0.3, 0.4"
PyCall = "1.91"
PyCall = "1.91, 1.92"
QuadGK = "0.2, 1.0, 2.0, 2.3"
Roots = "0.7, 0.8, 1.0"
SpecialFunctions = "0.6, 0.7, 0.8, 0.10"
Expand Down
4 changes: 3 additions & 1 deletion src/AST_translation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ iscall(ex::Expr, op::Symbol, len::Int) = ex.head == :call && ex.args[1] == op &&
# is ex a call with len args (including the op) ?
iscall(ex::Expr, len::Int) = iscall(ex) && length(ex.args) == len
# is of the form a op b , where op is <,>, etc. i.e. this is not a chained comparison
is_single_comparison(ex::Expr, op::Symbol) = ex.head == :comparison && length(ex.args) == 3 && ex.args[2] == op
#is_single_comparison(ex::Expr, op::Symbol) = ex.head == :comparison && length(ex.args) == 3 && ex.args[2] == op
# op is now an Expr, not a Symbol. This is likely broken even with this workaround 2021Apr20
is_single_comparison(ex::Expr, op) = ex.head == :comparison && length(ex.args) == 3 && ex.args[2] == op
# We check for :call repeatedly. We can optimize this later.
is_binary_minus(ex::Expr) = iscall(ex, :-, 3)
is_unary_minus(ex::Expr) = iscall(ex, :-, 2)
Expand Down
10 changes: 7 additions & 3 deletions src/AST_translation_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ const unicode_output = Dict{Symbol,Symbol}()
for (k,v) in unicode_translation unicode_output[v] = k end

# Reverse dict of ascii symbols for printing
const MTOJSYM = Dict{Symbol,Symbol}()
for (k,v) in JTOMSYM MTOJSYM[v] = k end
const MTOJSYM = Dict{Any,Any}()
#const MTOJSYM = Dict{Symbol,Symbol}()
for (k,v) in JTOMSYM
MTOJSYM[v] = k
end

# Finally, add the unicode symbols to the input translation table.
merge!(JTOMSYM, unicode_translation)
Expand Down Expand Up @@ -177,7 +180,8 @@ end

#### Operator type: infix, prefix, postfix

const OPTYPE = Dict{Symbol,Symbol}()
#const OPTYPE = Dict{Symbol,Symbol}()
const OPTYPE = Dict{Any,Any}()

for op in (:(=), :(:=), :(=>), :Rule , :RuleDelayed, :Power, :(.>),
:Set, :SetDelayed, :UpSet, :(*=), :(+=), :, :(->), :Function,
Expand Down
20 changes: 14 additions & 6 deletions src/apprules_core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,20 @@ macro olddoap(func)
end

macro doap(ex)
d = MacroTools.splitdef(ex)
quotename = QuoteNode(d[:name])
d[:name] = Symbol("do_", d[:name])
pushfirst!(d[:args], :(mx::Mxpr{$quotename}))
newfunc = MacroTools.combinedef(d)
:(($(esc(newfunc))))
dict = MacroTools.splitdef(ex)
quotename = QuoteNode(dict[:name])
dict[:name] = Symbol("do_", dict[:name])
pushfirst!(dict[:args], :(mx::Mxpr{$quotename}))
# The following broke mysteriously, with error message "dict undefined" in combinedef.
# So, we follow MacroTools docs and construct the function by hand.
# This is probably a Julia 1.7-dev bug. It may be fixed soon.
# newfunc = MacroTools.combinedef(dict)
rtype = get(dict, :rtype, :Any)
newfunc = :(function $(dict[:name])($(dict[:args]...);
$(dict[:kwargs]...))::$rtype where {$(dict[:whereparams]...)}
$(dict[:body].args...)
end)
return :(($(esc(newfunc))))
end

### Default apprule
Expand Down
20 changes: 9 additions & 11 deletions src/sympy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function populate_py_to_mx_dict()
(sympy.Mul, :Times),
(sympy.Pow ,:Power),
(sympy.Derivative, :D), ## We need to implement or handle Derivative
(sympy.containers.Tuple, :List),
(sympy.Tuple, :List),
(sympy.oo, :Infinity),
(sympy.zoo,:ComplexInfinity),
(sympy.Eq, :Equal),
Expand Down Expand Up @@ -280,12 +280,12 @@ have_rewrite_function_sympy_to_julia(pytype::PyCall.PyObject) = haskey(py_to_mx_
## We put more than user symbols here
function populate_user_symbol_dict()
for onepair in (
(sympy.numbers.ComplexInfinity(), :ComplexInfinity),
(sympy.numbers.Pi(), :Pi),
(sympy.numbers.EulerGamma(), :EulerGamma),
(sympy.numbers.Exp1(), :E),
(sympy.numbers.ImaginaryUnit(), complex(0,1)),
(sympy.numbers.NegativeInfinity(), MinusInfinity))
(sympy.S.ComplexInfinity, :ComplexInfinity),
(sympy.S.Pi, :Pi),
(sympy.S.EulerGamma, :EulerGamma),
(sympy.S.Exp1, :E),
(sympy.S.ImaginaryUnit, complex(0,1)),
(sympy.S.NegativeInfinity, MinusInfinity))
SYMPY_USER_SYMBOLS_REVERSE[onepair[1]] = onepair[2]
end
end
Expand Down Expand Up @@ -859,10 +859,8 @@ function init_sympy()
@eval copy!($(Symbol("sympy_", s)) , sympy.$s)
# @eval copy!($(Symbol("sympy_", s)) , sympy[$(QuoteNode(s))])
end
copy!(sympy_True,sympy.boolalg.BooleanTrue)
copy!(sympy_False,sympy.boolalg.BooleanFalse)
# copy!(sympy_True,sympy[:boolalg][:BooleanTrue])
# copy!(sympy_False,sympy[:boolalg][:BooleanFalse])
copy!(sympy_True,sympy.logic.boolalg.BooleanTrue)
copy!(sympy_False,sympy.logic.boolalg.BooleanFalse)
populate_rewrite_dict()
init_isympy()
end
Expand Down

2 comments on commit 440f59b

@jlapeyre
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/35781

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.9 -m "<description of version>" 440f59bcbbc230302c4f03630957e20b91a41700
git push origin v0.4.9

Please sign in to comment.