Skip to content

Commit

Permalink
macros which create export stmts will fail inside @testset due to Jul…
Browse files Browse the repository at this point in the history
…iaLang issue #51325
  • Loading branch information
quffaro committed Oct 1, 2024
1 parent 735edfa commit fe21de4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 40 deletions.
3 changes: 3 additions & 0 deletions src/SymbolicUtilsInterop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ function decapodes.Term(t::SymbolicUtils.BasicSymbolic)
end
end
end
# TODO subtraction is not parsed as such. e.g.,
# a, b = @syms a::Scalar b::Scalar
# Term(a-b) = Plus(Term[Var(:a), Mult(Term[Lit(Symbol("-1")), Var(:b)]))

decapodes.Term(x::Real) = decapodes.Lit(Symbol(x))

Expand Down
10 changes: 6 additions & 4 deletions src/symbolictheoryutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ macro operator(head, body)
s = promote_symtype($f, $(argnames...))
SymbolicUtils.Term{s}($f, Any[$(argnames...)])
end

export $f

Base.show(io::IO, ::typeof($f)) = print(io, $f)
end)

# if there are rewriting rules, add a method which accepts the function symbol and its arity (to prevent shadowing on operators like `-`)
# if there are rewriting rules, add a method which accepts the function symbol and its arity
# (to prevent shadowing on operators like `-`)
if !isempty(rulecalls)
push!(result.args, quote
function rules(::typeof($f), ::Val{$arity})
Expand Down Expand Up @@ -152,16 +154,16 @@ macro alias(body)
result = quote end
foreach(aliases) do alias
push!(result.args,
esc(quote
quote
function $alias(s...)
$rep(s...)
end
export $alias

Base.nameof(::typeof($alias), s) = Symbol("$alias")
end))
end)
end
result
return esc(result)
end
export @alias

Expand Down
70 changes: 34 additions & 36 deletions test/decasymbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,50 +68,47 @@ u, du = @syms u::PrimalForm{0, :X, 2} du::PrimalForm{1, :X, 2}

end

@testset "Operator definition" begin
# this is not nabla but "bizarro Δ"
del_expand_0, del_expand_1 = @operator (S)::DECQuantity begin
@match S begin
PatScalar(_) => error("Argument of type $S is invalid")
PatForm(_) => promote_symtype(★ d d, S)
end
@rule (~x::isForm0) => (d((d(~x))))
@rule (~x::isForm1) => (d((d(~x)))) + d((d((~x))))
end;

# we will test is new operator
(r0, r1, r2) = @operator ρ(S)::DECQuantity begin
S <: Form ? Scalar : Form
@rule ρ(~x::isForm0) => 0
@rule ρ(~x::isForm1) => 1
@rule ρ(~x::isForm2) => 2
end

# this is not nabla but "bizarro Δ"
del_expand_0, del_expand_1 =
@operator (S)::DECQuantity begin
@match S begin
PatScalar(_) => error("Argument of type $S is invalid")
PatForm(_) => promote_symtype(★ d d, S)
end
@rule (~x::isForm0) => (d((d(~x))))
@rule (~x::isForm1) => (d((d(~x)))) + d((d((~x))))
end;
R, = @operator φ(S1, S2, S3)::DECQuantity begin
let T1=S1, T2=S2, T3=S3
Scalar
end
@rule φ(2(~x::isForm0), 2(~y::isForm0), 2(~z::isForm0)) => 2*φ(~x,~y,~z)
end

@alias (φ′,) => φ

@testset "Operator definition" begin

#
@test_throws Exception (b)
@test symtype((u)) == PrimalForm{0, :X ,2}
@test promote_symtype(∇, u) == PrimalForm{0, :X, 2}

@test isequal(del_expand_0((u)), (d((d(u)))))

# we will test is new operator
(r0, r1, r2) = @operator ρ(S)::DECQuantity begin
if S <: Form
Scalar
else
Form
end
@rule ρ(~x::isForm0) => 0
@rule ρ(~x::isForm1) => 1
@rule ρ(~x::isForm2) => 2
end


# ρ
@test symtype(ρ(u)) == Scalar

R, = @operator φ(S1, S2, S3)::DECQuantity begin
let T1=S1, T2=S2, T3=S3
Scalar
end
@rule φ(2(~x::isForm0), 2(~y::isForm0), 2(~z::isForm0)) => 2*φ(~x,~y,~z)
end

# TODO we need to alias rewriting rules
@alias (φ′,) => φ

# R
@test isequal(R(φ(2u,2u,2u)), R(φ′(2u,2u,2u)))
# TODO we need to alias rewriting rules

end

Expand Down Expand Up @@ -149,7 +146,8 @@ end
context = SymbolicContext(Term(TumorInvasion))
TumorInvasion′ = SummationDecapode(DecaExpr(context))

# new terms introduced
# new terms introduced because Symbolics converts subtraction expressions
# e.g., a - b => +(a, -b)
@test_broken TumorInvasion == TumorInvasion′
# TI' has (11, Literal, -1) and (12, infer, mult_1)
# Op1 (2, 1, 4, 7) should be (2, 4, 1, 7)
Expand Down

0 comments on commit fe21de4

Please sign in to comment.