Skip to content

Commit

Permalink
added rules function which dispatches on function symbol and the Val(…
Browse files Browse the repository at this point in the history
…arity)
  • Loading branch information
quffaro committed Sep 10, 2024
1 parent f32b3c0 commit ecfa931
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/deca/ThDEC.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ThDEC

using ..DiagrammaticEquations: @operator, @alias, Quantity
import ..DiagrammaticEquations: rules

using MLStyle
using StructEquality
Expand Down Expand Up @@ -173,6 +174,8 @@ end
PatForm(_) => promote_symtype(★ d d, S)
_ => throw(LaplacianError(S))
end
@rule Δ(~x::isForm0) => (d((d(~x))))
@rule Δ(~x::isForm1) => (d((d(~x)))) + d((d((~x))))
end

@operator +(S1, S2)::DECQuantity begin
Expand Down
14 changes: 13 additions & 1 deletion src/symbolictheoryutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ using SymbolicUtils
using SymbolicUtils: Symbolic, BasicSymbolic, FnType, Sym, symtype
import SymbolicUtils: promote_symtype

function rules end
export rules

function promote_symtype(f::ComposedFunction, args)
promote_symtype(f.outer, promote_symtype(f.inner, args))
end
Expand Down Expand Up @@ -106,9 +109,18 @@ macro operator(head, body)
export $f
end)

if !isempty(rulecalls)
push!(result.args, quote
function rules(::typeof($f), ::Val{$arity})
[($(rulecalls...))]
end
end)
end

# we want to feed symtype the generics
push!(result.args, quote
function SymbolicUtils.promote_symtype(::typeof($f), $(sort_types...)) where {$(sort_constraints...)}
function SymbolicUtils.promote_symtype(::typeof($f),
$(sort_types...)) where {$(sort_constraints...)}
$block
end
function SymbolicUtils.promote_symtype(::typeof($f), args::Vararg{Symbolic, $arity})
Expand Down
24 changes: 12 additions & 12 deletions test/klausmeier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ Phytodynamics = parse_decapode(quote
∂ₜ(n) == w - m*n + Δ(n)
end)

symbmodel = SymbolicContext(Phytodynamics)
dexpr = DecaExpr(symbmodel)
symbmodel= SymbolicContext(dexpr)
ps = SymbolicContext(Phytodynamics)
dexpr = DecaExpr(ps)
ps= SymbolicContext(dexpr)
# TODO variables are the same but the equations don't match

n = ps.vars[1]
SymbolicUtils.symtype(n)
Δ(n)

r = @rule Δ(~n) => (d((d(~n))))
r, _ = rules(Δ, Val(1));

t2 = r(Δ(n))
t2 |> dump


using SymbolicUtils.Rewriters
using SymbolicUtils: promote_symtype
r = @rule ((~n)) => ~n
r = @rule ((~n)) => ~n

nested_star_cancel = Postwalk(Chain([r]))
nested_star_cancel(d(((n))))
nested_star_cancel(d(((n))))
nsc = nested_star_cancel

isequal(nsc(((d(n)))), d(n))
dump(nsc(((d(n)))))
@test isequal(nsc(((d(n)))), d(n))
dump(nsc(((d(n)))))
dump(d(n))
((d(((n)))))
nsc(((d(((n))))))
nsc(nsc(((d(((n)))))))
((d(((n)))))
nsc(((d(((n))))))
nsc(nsc(((d(((n)))))))

0 comments on commit ecfa931

Please sign in to comment.