Skip to content

Commit

Permalink
Added proof of concept
Browse files Browse the repository at this point in the history
Added a short script showcasing how rewriting could be done with the `Sort` types and a reference ACSet.
  • Loading branch information
GeorgeR227 committed Aug 30, 2024
1 parent 9d8dfca commit 4badec4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Catlab = "134e5e36-593f-5add-ad60-77f754baafbe"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
MLStyle = "d8e11817-5142-5d16-987a-aa16d5891078"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StructEquality = "6ec83bb0-ed9f-11e9-3b4c-2b04cb4e219c"
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
Expand Down
60 changes: 60 additions & 0 deletions src/sym_rewrite.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using DiagrammaticEquations
using SymbolicUtils
using MLStyle

test_space = Space(:X, 2)

test_type = Form(0, false, test_space)

Heat = @decapode begin
C::Form0
D::Constant
∂ₜ(C) == (D+2)*Δ(C)
end

infer_types!(Heat)
resolve_overloads!(Heat)

function oldtype_to_new(old::Symbol, space::Space = Space(:I, 2))::Sort
@match old begin
:Form0 => PrimalForm(0, space)
:Form1 => PrimalForm(1, space)
:Form2 => PrimalForm(2, space)

Check warning on line 22 in src/sym_rewrite.jl

View check run for this annotation

Codecov / codecov/patch

src/sym_rewrite.jl#L18-L22

Added lines #L18 - L22 were not covered by tests

:DualForm0 => DualForm(0, space)
:DualForm1 => DualForm(1, space)
:DualForm2 => DualForm(2, space)

Check warning on line 26 in src/sym_rewrite.jl

View check run for this annotation

Codecov / codecov/patch

src/sym_rewrite.jl#L24-L26

Added lines #L24 - L26 were not covered by tests

:Constant => Scalar()
:Parameter => Scalar()

Check warning on line 29 in src/sym_rewrite.jl

View check run for this annotation

Codecov / codecov/patch

src/sym_rewrite.jl#L28-L29

Added lines #L28 - L29 were not covered by tests
end
end

function isform_zero(x)
getmetadata(x, Sort).dim == 0

Check warning on line 34 in src/sym_rewrite.jl

View check run for this annotation

Codecov / codecov/patch

src/sym_rewrite.jl#L33-L34

Added lines #L33 - L34 were not covered by tests
end

function isform_two(x)
getmetadata(x, Sort).dim == 2

Check warning on line 38 in src/sym_rewrite.jl

View check run for this annotation

Codecov / codecov/patch

src/sym_rewrite.jl#L37-L38

Added lines #L37 - L38 were not covered by tests
end

@syms Δ(x) d(x) (x)

@syms C D Ċ sum_1

C = setmetadata(C, Sort, oldtype_to_new(Heat[1, :type]))

lap_0_rule = @rule Δ(~x::(isform_zero)) => (d((d(~x))))
lap_2_rule = @rule Δ(~x::(isform_two)) => d((d((~x))))

test_eq = Δ(C)
lap_0_rule(test_eq)
lap_2_rule(test_eq) === nothing

rewriter = SymbolicUtils.Postwalk(SymbolicUtils.Chain([lap_0_rule]))
test_eq_long = (D + 2) * Δ(C)
rewriter(test_eq_long)

# Δ₀(x) = ⋆₀⁻¹(dual_d₁(⋆₁(d₀(x))))
# @syms Δ₀(x) d₀(x) ⋆₁(x) dual_d₁(x) ⋆₀⁻¹(x)
# lap_0_rule = @rule Δ₀(~x::(isform_zero)) => ⋆₀⁻¹(dual_d₁(⋆₁(d₀(~x))))

0 comments on commit 4badec4

Please sign in to comment.