Skip to content

Commit 4144802

Browse files
authored
Merge pull request #482 from JuliaSymbolics/s/unityper-updates
[wip] Unityper updates
2 parents 94e0dd0 + 2878dd0 commit 4144802

21 files changed

+1034
-258
lines changed

Project.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
1515
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
1616
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
1717
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
18-
Metatheory = "e9d8d322-4543-424a-9be4-0cc815abe26c"
1918
MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
2019
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
2120
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
2221
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
2322
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2423
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
25-
TermInterface = "8ea1fca8-c5ef-4a55-8b96-4e9afe9c9a3c"
2624
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
2725
Unityper = "a7c27f48-0311-42f6-a7f8-2c11e75eb415"
2826

@@ -37,13 +35,11 @@ DocStringExtensions = "0.8, 0.9"
3735
DynamicPolynomials = "0.3, 0.4"
3836
IfElse = "0.1"
3937
LabelledArrays = "1.5"
40-
Metatheory = "1.2"
4138
MultivariatePolynomials = "0.3, 0.4"
4239
NaNMath = "0.3, 1"
4340
Setfield = "0.7, 0.8, 1"
4441
SpecialFunctions = "0.10, 1.0, 2"
4542
StaticArrays = "0.12, 1.0"
46-
TermInterface = "0.2.1, 0.3"
4743
TimerOutputs = "0.5"
4844
Unityper = "0.1.2"
4945
julia = "1.3"

src/SymbolicUtils.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ module SymbolicUtils
66
using DocStringExtensions
77
export @syms, term, showraw, hasmetadata, getmetadata, setmetadata
88

9-
using TermInterface
10-
using TermInterface: node_count
11-
using Metatheory
12-
using Metatheory.Rules
13-
using Metatheory.Rewriters
149
using Unityper
1510

1611
# Sym, Term,
@@ -20,6 +15,7 @@ using Setfield
2015
import Setfield: PropertyLens
2116
import Base: +, -, *, /, //, \, ^, ImmutableDict
2217
using ConstructionBase
18+
include("interface.jl")
2319
include("types.jl")
2420
export istree, operation, arguments, similarterm
2521

@@ -40,6 +36,8 @@ export @rule, @acrule, RuleSet
4036

4137
# Rule type and @rule macro
4238
include("rule.jl")
39+
include("matchers.jl")
40+
include("rewriters.jl")
4341

4442
# Convert to an efficient multi-variate polynomial representation
4543
import MultivariatePolynomials
@@ -61,10 +59,6 @@ include("simplify.jl")
6159
export substitute
6260
include("substitute.jl")
6361

64-
# EGraph rewriting
65-
include("egraph.jl")
66-
export optimize
67-
6862
include("code.jl")
6963

7064

src/code.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
module Code
22

33
using StaticArrays, LabelledArrays, SparseArrays, LinearAlgebra
4-
using TermInterface
54

65
export toexpr, Assignment, (), Let, Func, DestructuredArgs, LiteralExpr,
76
SetArray, MakeArray, MakeSparseArray, MakeTuple, AtIndex,
87
SpawnFetch, Multithreaded, cse
98

109
import ..SymbolicUtils
1110
import ..SymbolicUtils.Rewriters
12-
import SymbolicUtils: @matchable, BasicSymbolic, Sym, Term, istree, operation, arguments,
13-
symtype, similarterm, unsorted_arguments, metadata
11+
import SymbolicUtils: @matchable, BasicSymbolic, Sym, Term, istree, operation, arguments, issym,
12+
symtype, similarterm, unsorted_arguments, metadata, isterm, term
1413

1514
##== state management ==##
1615

@@ -134,7 +133,7 @@ function function_to_expr(::typeof(SymbolicUtils.ifelse), O, st)
134133
end
135134

136135
function function_to_expr(x::BasicSymbolic, O, st)
137-
TermInterface.issym(x) ? get(st.symbolify, O, nothing) : nothing
136+
issym(x) ? get(st.symbolify, O, nothing) : nothing
138137
end
139138

140139
toexpr(O::Expr, st) = O
@@ -693,8 +692,8 @@ end
693692

694693

695694
function _cse(exprs::AbstractArray)
696-
letblock = cse(Term{Any}(tuple, exprs))
697-
letblock.pairs, arguments(letblock.body)
695+
letblock = cse(Term{Any}(tuple, vec(exprs)))
696+
letblock.pairs, reshape(arguments(letblock.body), size(exprs))
698697
end
699698

700699
function cse(x::MakeArray)
@@ -738,8 +737,13 @@ function cse_block!(assignments, counter, names, name, state, x)
738737
end
739738
elseif istree(x)
740739
args = map(a->cse_block!(assignments, counter, names, name, state,a), unsorted_arguments(x))
741-
return similarterm(x, operation(x), args, symtype(x),
742-
metadata=metadata(x))
740+
if isterm(x)
741+
return term(operation(x), args...)
742+
else
743+
return similarterm(x, operation(x),
744+
args, symtype(x),
745+
metadata=metadata(x))
746+
end
743747
else
744748
return x
745749
end

src/egraph.jl

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/interface.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
istree(x)
3+
4+
Returns `true` if `x` is a term. If true, `operation`, `arguments`
5+
must also be defined for `x` appropriately.
6+
"""
7+
istree(x) = false
8+
9+
"""
10+
symtype(x)
11+
12+
Returns the symbolic type of `x`. By default this is just `typeof(x)`.
13+
Define this for your symbolic types if you want `SymbolicUtils.simplify` to apply rules
14+
specific to numbers (such as commutativity of multiplication). Or such
15+
rules that may be implemented in the future.
16+
"""
17+
function symtype(x)
18+
typeof(x)
19+
end
20+
21+
"""
22+
issym(x)
23+
24+
Returns `true` if `x` is a symbol. If true, `nameof` must be defined
25+
on `x` and must return a Symbol.
26+
"""
27+
issym(x) = false
28+
29+
"""
30+
operation(x)
31+
32+
If `x` is a term as defined by `istree(x)`, `operation(x)` returns the
33+
head of the term if `x` represents a function call, for example, the head
34+
is the function being called.
35+
"""
36+
function operation end
37+
38+
"""
39+
arguments(x)
40+
41+
Get the arguments of `x`, must be defined if `istree(x)` is `true`.
42+
"""
43+
function arguments end
44+
45+
"""
46+
unsorted_arguments(x::T)
47+
48+
If x is a term satisfying `istree(x)` and your term type `T` orovides
49+
and optimized implementation for storing the arguments, this function can
50+
be used to retrieve the arguments when the order of arguments does not matter
51+
but the speed of the operation does.
52+
"""
53+
unsorted_arguments(x) = arguments(x)
54+
arity(x) = length(unsorted_arguments(x))
55+
56+
"""
57+
metadata(x)
58+
59+
Return the metadata attached to `x`.
60+
"""
61+
metadata(x) = nothing
62+
63+
"""
64+
metadata(x, md)
65+
66+
Returns a new term which has the structure of `x` but also has
67+
the metadata `md` attached to it.
68+
"""
69+
function metadata(x, data)
70+
error("Setting metadata on $x is not possible")
71+
end
72+
73+
"""
74+
similarterm(x, head, args, symtype=nothing; metadata=nothing, exprhead=:call)
75+
76+
Returns a term that is in the same closure of types as `typeof(x)`,
77+
with `head` as the head and `args` as the arguments, `type` as the symtype
78+
and `metadata` as the metadata. By default this will execute `head(args...)`.
79+
`x` parameter can also be a `Type`. The `exprhead` keyword argument is useful
80+
when manipulating `Expr`s.
81+
"""
82+
function similarterm end

0 commit comments

Comments
 (0)