# Associativity / Commutativity for Boolean operations in SymbolicUtils.jl

**URL:** <https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468>\
**Category:** General Usage\
**Tags:** package, symbolic-utils\
**Created:** [March 30, 2026, 8:11pm UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468 "2026-03-30T20:11:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![logic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logic/32/221437_2.png) [@logic](https://discourse.julialang.org/u/logic)\
**Post date:** [March 30, 2026, 8:11pm UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468/1 "2026-03-30T20:11:08Z")

</div>

I’m working on a Boolean Algebra Rewriter with SymbolicUtils.jl, and I’ve noticed that for some reason, associativity and commutativity are not being properly applied for Boolean operations, which makes defining rules painful. I’m wondering if I’m missing something or if there is some way to manually tell SymbolicUtils to apply associativity/commutativity to boolean operations.

To clarify: I need to write these rules

> @acrule((~a & ~b) & ~a =\> ~a & ~b)
> 
> @acrule((~a & ~b) & ~b =\> ~a & ~b)

If I only write the first one, then (A & B) & B has no change.

Furthermore, I have to define:

> @acrule((~a & ~b & ~c) & ~a =\> ~a & ~b & ~c)
> 
> @acrule((~a & (~b & ~c)) & ~a =\> ~a & ~b & ~c)

If I only define the first one, then (A & (B & C)) & A doesn’t simplify.

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [March 30, 2026, 9:35pm UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468/2 "2026-03-30T21:35:50Z")

</div>

there was a discussion about it here: [[ANN] SymbolicIntegration.jl - indefinite integration with Symbolics.jl symbols - #8 by CiccioPasticcio](https://discourse.julialang.org/t/ann-symbolicintegration-jl-indefinite-integration-with-symbolics-jl-symbols/134777/8)

maybe @CiccioPasticcio has any idea?

---

<div class="post-metadata">

**Author:** ![CiccioPasticcio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cicciopasticcio/32/215909_2.png) [@CiccioPasticcio](https://discourse.julialang.org/u/CiccioPasticcio)\
**Post date:** [March 31, 2026, 8:14am UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468/3 "2026-03-31T08:14:04Z")

</div>

yes so the commutativity thing is still a bit messy. What I did was to implement in the normal rule macro commutativity for the + and \* operation, and that works for almost all the cases (still some problems with many nested operations). Currentyl associativity and commutativity for & is not supported. also i am not 100% sure what acrule macro does, but I think is not different from the rule macro

---

<div class="post-metadata">

**Author:** ![CiccioPasticcio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cicciopasticcio/32/215909_2.png) [@CiccioPasticcio](https://discourse.julialang.org/u/CiccioPasticcio)\
**Post date:** [March 31, 2026, 8:15am UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468/4 "2026-03-31T08:15:07Z")

</div>

also there is no way to manually tell that some operations are commutative. would be nice to implement is, whad do you think @cryptic.ax ?

---

<div class="post-metadata">

**Author:** ![cryptic.ax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cryptic.ax/32/220540_2.png) [@cryptic.ax](https://discourse.julialang.org/u/cryptic.ax)\
**Post date:** [March 31, 2026, 8:35am UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468/5 "2026-03-31T08:35:03Z")

</div>

Yes, currently there’s no way to add associative/commutative (AC) operations. `+` and `*` for scalars (and `+` for arrays) are manually implemented to leverage the AC representation. It’s not an implementation thing - we simply don’t have the ability to support arbitrary AC operations, somewhat by design. SymbolicUtils itself can’t easily implement an AC boolean algebra, since even if the representation for `+` was repurposed for `|` and `*` for `&`, there’s no structural difference between `a + b` and `a | b` when both `a` and `b` are booleans. One alternative might be to define a custom `primitive type` that mimics `Bool` and define the algebra on that symtype, though I’m not 100% sure how well this would work. Alternatively, define a wrapper type

```julia
struct BoolTerm
  x::BasicSymbolic{SymReal}
end

```

And define all boolean operations as operating on `BoolTerm` and returning `BoolTerm`. That way, you control all symbolic dispatches and can use the internal representation as you wish. This is the standard way of using SU to define a custom algebra.

---

<div class="post-metadata">

**Author:** ![logic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/logic/32/221437_2.png) [@logic](https://discourse.julialang.org/u/logic)\
**Post date:** [March 31, 2026, 5:11pm UTC](https://discourse.julialang.org/t/associativity-commutativity-for-boolean-operations-in-symbolicutils-jl/136468/6 "2026-03-31T17:11:58Z")

</div>

How would I go about defining the boolean operations as operating on `BoolTerm` and returning `BoolTerm`?
