You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/interface.md
+12-36Lines changed: 12 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,13 +17,7 @@ This may sound like a roundabout way of doing it, but it can be really fast. In
17
17
18
18
## Defining the interface
19
19
20
-
SymbolicUtils uses a function `to_symbolic` to convert aribtrarty types to it's own internal types. Our intention is for SymbolicUtils to be useful even for packages with their own custom symbolic types which
21
-
differ from those offered by SymbolicUtils. To this end, SymbolicUtils provides an interface which if implemented, will enable automatic conversion of types to SymbolicUtils types.
22
-
23
-
* an `operation`, (i.e. function to apply)
24
-
*`arguments` which the `operation` is applied to
25
-
*`variable` types which are the atoms from which the expression tree is built
26
-
* optionally, a type which should `typeof(operation(arguments...))` should return if it were to be run.
20
+
SymbolicUtils matchers can match any Julia object that implements an interface to traverse it as a tree.
27
21
28
22
In particular, the following methods should be defined for an expression tree type `T` with symbol types `S` to work
29
23
with SymbolicUtils.jl
@@ -48,20 +42,16 @@ Returns the arguments (a `Vector`) for an expression tree.
48
42
Called only if `istree(x)` is `true`. Part of the API required
49
43
for `simplify` to work. Other required methods are `operation` and `istree`
50
44
51
-
#### `to_symbolic(x::S)`
52
-
Convert your variable type to a `SymbolicUtils.Sym`. Suppose you have
53
-
```julia
54
-
struct MySymbol
55
-
s::Symbol
56
-
end
57
-
```
58
-
which could represent any type symbolically, then you would define
In addition, the methods for `Base.hash` and `Base.isequal` should also be implemented by the types for the purposes of substitution and equality matching respectively.
62
46
63
47
### Optional
64
48
49
+
#### `similarterm(t::MyType, f, args)`
50
+
51
+
Construct a new term with the operation `f` and arguments `args`, the term should be similar to `t` in type. if `t` is a `Term` object a new Term is created with the same symtype as `t`. If not, the result is computed as `f(args...)`. Defining this method for your term type will reduce any performance loss in performing `f(args...)` (esp. the splatting, and redundant type computation).
52
+
53
+
The below two functions are internal to SymbolicUtils
54
+
65
55
#### `symtype(x)`
66
56
67
57
The supposed type of values in the domain of x. Tracing tools can use this type to
@@ -102,38 +92,24 @@ How can we use SymbolicUtils.jl to convert `ex` to `(-)(:x, 1)`? We simply imple
102
92
`operation`, `arguments` and `to_symbolic` and we'll be off to the races:
103
93
```julia:piracy2
104
94
using SymbolicUtils
105
-
using SymbolicUtils: Sym, Term, istree, operation, arguments, to_symbolic
95
+
using SymbolicUtils: istree, operation, arguments, similarterm
There was no simplification, because by default SymbolicUtils assumes that the expressoins are of type Any and no particular rules apply. Let's change this by saying that the symbolic type (symtype) of an Expr or Symbol object is actually Real.
130
107
131
-
Now suppose we actaully wanted all `Symbol`s to be treated as `Real` numbers. We can simply define
132
108
```julia:piracy4
133
-
SymbolicUtils.symtype(s::Symbol) = Real
109
+
SymbolicUtils.symtype(s::Expr) = Real
134
110
135
111
dump(simplify(ex))
136
112
```
137
113
\out{piracy4}
138
114
139
-
and now all our analysis is able to figure out that the `Term`s are `Number`s.
115
+
Now SymbolicUtils is able to apply the Number simplification rule to Expr.
0 commit comments