Skip to content

Commit c62beb7

Browse files
committed
add some docs
1 parent e2517f5 commit c62beb7

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

docs/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ using SymbolicUtils # hide
2020

2121
{{doc Term Term type}}
2222

23+
{{doc Add Add type}}
24+
25+
{{doc Mul Mul type}}
26+
27+
{{doc Pow Pow type}}
28+
2329
{{doc promote_symtype promote_symtype fn}}
2430

2531
## Interfacing

docs/index.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ where appropriate -->
1212

1313
The main features are:
1414

15-
- Symbols (`Sym`s) carry type information. ([read more](#symbolic_expressions))
16-
- Compound expressions composed of `Sym`s propagate type information. ([read more](#symbolic_expressions))
17-
- A flexible [rule-based rewriting language](#rule-based_rewriting) allowing liberal use of user defined matchers and rewriters.
15+
- Fast expressions
1816
- A [combinator library](#composing-rewriters) for making rewriters.
17+
- A [rule-based rewriting language](#rule-based_rewriting).
18+
- Type promotion:
19+
- Symbols (`Sym`s) carry type information. ([read more](#symbolic_expressions))
20+
- Compound expressions composed of `Sym`s propagate type information. ([read more](#symbolic_expressions))
1921
- Set of [simplification rules](#simplification). These can be remixed and extended for special purposes.
2022

2123

2224
## Table of contents
2325

2426
\tableofcontents <!-- you can use \toc as well -->
2527

26-
## Symbolic expressions
28+
## `Sym`s
2729

2830
First, let's use the `@syms` macro to create a few symbols.
2931

@@ -66,17 +68,6 @@ expr1 + expr2
6668
```
6769
\out{expr}
6870

69-
### Simplified printing
70-
71-
Tip: you can set `SymbolicUtils.show_simplified[] = true` to enable simplification on printing, or call `SymbolicUtils.showraw(expr)` to display an expression without simplification.
72-
In the REPL, if an expression was successfully simplified before printing, it will appear in yellow rather than white, as a visual cue that what you are looking at is not the exact datastructure.
73-
74-
```julia:showraw
75-
using SymbolicUtils: showraw
76-
77-
showraw(expr1 + expr2)
78-
```
79-
\out{showraw}
8071

8172
**Function-like symbols**
8273

@@ -106,6 +97,20 @@ g(2//5, g(1, β))
10697

10798
This works because `g` "returns" a `Real`.
10899

100+
101+
## Expression interface
102+
103+
Symbolic expressions are of type `Term{T}`, `Add{T}`, `Mul{T}` or `Pow{T}` and denote some function call where one or more arguments are themselves such expressions or `Sym`s.
104+
105+
All the expression types support the following:
106+
107+
- `istree(x)` -- always returns `true` denoting, `x` is not a leaf node like Sym or a literal.
108+
- `operation(x)` -- the function being called
109+
- `arguments(x)` -- a vector of arguments
110+
- `symtype(x)` -- the "inferred" type (`T`)
111+
112+
See more on the interface [here](/interface)
113+
109114
## Rule-based rewriting
110115

111116
Rewrite rules match and transform an expression. A rule is written using either the `@rule` macro or the `@acrule` macro.
@@ -151,7 +156,7 @@ Notice that there is a subexpression `(2 * w) + (2 * w)` that could be simplifie
151156

152157
### Predicates for matching
153158

154-
Matcher pattern may contain slot variables with attached predicates, written as `~x::f` where `f` is a function that takes a matched expression (a `Term` object a `Sym` or any Julia value that is in the expression tree) and returns a boolean value. Such a slot will be considered a match only if `f` returns true.
159+
Matcher pattern may contain slot variables with attached predicates, written as `~x::f` where `f` is a function that takes a matched expression and returns a boolean value. Such a slot will be considered a match only if `f` returns true.
155160

156161
Similarly `~~x::g` is a way of attaching a predicate `g` to a segment variable. In the case of segment variables `g` gets a vector of 0 or more expressions and must return a boolean value. If the same slot or segment variable appears twice in the matcher pattern, then at most one of the occurance should have a predicate.
157162

src/types.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,17 @@ sdict(kv...) = Dict{Any, Number}(kv...)
391391
# this cannot be Symbolic{<:Number} to make MTK Parameters work. See #155
392392
const SN = Symbolic
393393
"""
394-
Add(coeff, dict)
394+
Add(T, coeff, dict::Dict)
395395
396-
Represents coeff + (key1 * val1) + (key2 * val2) + ...
396+
Represents `coeff + (key1 * val1) + (key2 * val2) + ...`
397397
398-
where coeff and the vals are non-symbolic numbers.
398+
where keys and values come from the dictionary (`dict`).
399+
where `coeff` and the vals are `<:Number` and keys are symbolic.
399400
401+
- `operation(::Add)` -- returns `+`.
402+
- `symtype(::Add)` -- returns `T`.
403+
- `arguments(::Add)` -- returns a totally ordered vector of arguments. i.e.
404+
`[coeff, keyM*valM, keyN*valN...]`
400405
"""
401406
struct Add{X, T<:Number, D} <: Symbolic{X}
402407
coeff::T
@@ -507,11 +512,17 @@ end
507512
-(a::SN, b::Number) = a + (-b)
508513

509514
"""
510-
Mul(coeff, dict)
515+
Mul(T, coeff, dict)
511516
512517
Represents coeff * (key1 ^ val1) * (key2 ^ val2) * ....
513518
514-
where coeff is a non-symbolic number.
519+
where coeff is a <:Number and keys and values come from the dictionary (`dict`).
520+
where `coeff` and the vals are `<:Number` and keys are symbolic.
521+
522+
- `symtype(::Add)` -- returns `T`.
523+
- `operation(::Add)` -- returns `*`.
524+
- `arguments(::Add)` -- returns a totally ordered vector of arguments. i.e.
525+
`[coeff, keyM^valM, keyN^valN...]`
515526
"""
516527
struct Mul{X, T<:Number, D} <: Symbolic{X}
517528
coeff::T
@@ -552,9 +563,6 @@ Base.isequal(a::Mul, b::Mul) = isequal(a.coeff, b.coeff) && isequal(a.dict, b.di
552563

553564
Base.show(io::IO, a::Mul) = show_term(io, a)
554565

555-
"""
556-
makemul(xs...)
557-
"""
558566
function makemul(sign, coeff, xs...; d=sdict())
559567
for x in xs
560568
if x isa Pow && x.exp isa Number
@@ -606,7 +614,7 @@ end
606614
"""
607615
Pow(base, exp)
608616
609-
Represents base^exp, a lighter version of Mul(1, Dict(base=>exp))
617+
Represents `base^exp`, a lighter version of `Mul(1, Dict(base=>exp))`
610618
"""
611619
struct Pow{X, B, E} <: Symbolic{X}
612620
base::B

0 commit comments

Comments
 (0)