Skip to content

Commit cf9324b

Browse files
committed
negative exponent feature is done in a different way, more clean
1 parent 4c2b475 commit cf9324b

File tree

2 files changed

+6
-46
lines changed

2 files changed

+6
-46
lines changed

src/matchers.jl

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,6 @@ function matcher(defslot::DefSlot; acSets = nothing)
4848
matcher(Slot(defslot.name, defslot.predicate))
4949
end
5050

51-
# function opposite_sign_matcher(val::Any)
52-
# end
53-
54-
function opposite_sign_matcher(slot::Slot)
55-
function slot_matcher(next, data, bindings)
56-
!islist(data) && return nothing
57-
val = get(bindings, slot.name, nothing)
58-
if val !== nothing
59-
if isequal(val, car(data))
60-
return next(bindings, 1)
61-
end
62-
elseif slot.predicate(car(data))
63-
next(assoc(bindings, slot.name, -car(data)), 1) # this - is the only difference wrt matcher(slot::Slot)
64-
end
65-
end
66-
end
67-
68-
function opposite_sign_matcher(defslot::DefSlot)
69-
opposite_sign_matcher(Slot(defslot.name, defslot.predicate))
70-
end
71-
7251
# returns n == offset, 0 if failed
7352
function trymatchexpr(data, value, n)
7453
if !islist(value)
@@ -149,13 +128,6 @@ function term_matcher_constructor(term, acSets)
149128

150129
# if the operation is a pow, we have to match also 1/(...)^(...) with negative exponent
151130
if operation(term)==^
152-
# the below 4 lines could stay in the function term_matcher_pow, but
153-
# are here to speed up the rule matcher function
154-
cond = isa(arguments(term)[2], Slot) || isa(arguments(term)[2], DefSlot)
155-
if cond
156-
matchers_modified = (matcher(operation(term)), matcher(arguments(term)[1]), opposite_sign_matcher(arguments(term)[2])) # is this ok to be here or should it be outside neg_pow_term_matcher?
157-
end
158-
159131
function term_matcher_pow(success, data, bindings)
160132
!islist(data) && return nothing # if data is not a list, return nothing
161133
!iscall(car(data)) && return nothing # if first element is not a call, return nothing
@@ -166,27 +138,15 @@ function term_matcher_constructor(term, acSets)
166138
# if data is of the alternative form 1/(...)^(...), it might match with negative exponent
167139
if (operation(car(data))==/) && arguments(car(data))[1]==1 && iscall(arguments(car(data))[2]) && (operation(arguments(car(data))[2])==^)
168140
denominator = arguments(car(data))[2]
169-
# let's say data = a^b with a and b can be whatever
170-
# if b is not a number then call the loop function with a^-b
171-
if !isa(arguments(denominator)[2], Number)
172-
frankestein = arguments(denominator)[1] ^ -(arguments(denominator)[2])
173-
result = loop(frankestein, bindings, matchers)
174-
# if b is a number, like 3, we cant call loop with a^-3 bc it
175-
# will automatically transform into 1/a^3. Therefore we need to
176-
# create a matcher that flips the sign of the exponent. I created
177-
# this matecher just for `Slot`s and `DefSlot`s, but not for
178-
# terms or literals, because if b is a number and not a call,
179-
# certainly doesn't match a term (I hope).
180-
# Also not a literal because...?
181-
elseif cond
182-
result = loop(denominator, bindings, matchers_modified)
183-
end
141+
T = symtype(denominator)
142+
frankestein = Term{T}(^, [arguments(denominator)[1], -arguments(denominator)[2]])
143+
result = loop(frankestein, bindings, matchers)
144+
result !== nothing && return success(result, 1)
184145
end
185-
result !== nothing && return success(result, 1)
186146
return nothing
187147
end
188148
return term_matcher_pow
189-
# if the operation is commutative
149+
# if we want to do commutative checks, i.e. call matcher with different order of the arguments
190150
elseif acSets!==nothing && !isa(arguments(term)[1], Segment) && operation(term) in [+, *]
191151
function term_matcher_comm(success, data, bindings)
192152
!islist(data) && return nothing # if data is not a list, return nothing

test/rewrite.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using SymbolicUtils
22

33
include("utils.jl")
44

5-
@syms a b c x
5+
@syms a b c d x
66

77
@testset "Equality" begin
88
@eqtest a == a

0 commit comments

Comments
 (0)