Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
76634490
Commit
76634490
authored
Apr 17, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated numeric rules to use Scope, so that they work for the new n-ary match system.
parent
822e6c4d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
46 deletions
+31
-46
src/rules/numerics.py
src/rules/numerics.py
+31
-46
No files found.
src/rules/numerics.py
View file @
76634490
...
...
@@ -192,73 +192,58 @@ def match_multiply_zero(node):
return
[]
def
m
ultiply_zero
(
root
,
args
):
def
m
atch_multiply_numerics
(
node
):
"""
a * 0 -> 0
0 * a -> 0
-0 * a -> -0
0 * -a -> -0
-0 * -a -> 0
3 * 2 -> 6
3.0 * 2 -> 6.0
3 * 2.0 -> 6.0
3.0 * 2.0 -> 6.0
"""
return
negate
(
Leaf
(
0
),
args
[
0
])
MESSAGES
[
multiply_zero
]
=
_
(
'Multiplication with zero yields zero.'
)
assert
node
.
is_op
(
OP_MUL
)
p
=
[]
scope
=
Scope
(
node
)
numerics
=
filter
(
lambda
n
:
n
.
is_numeric
(),
scope
)
def
match_multiply_one
(
node
):
"""
a * 1 -> a
1 * a -> a
-1 * a -> -a
1 * -a -> -a
-1 * -a -> a
"""
assert
node
.
is_op
(
OP_MUL
)
for
n
in
numerics
:
if
n
.
negated
:
continue
left
,
right
=
node
if
n
.
value
==
0
:
p
.
append
(
P
(
node
,
multiply_zero
,
(
n
,)))
if
left
.
value
==
1
:
return
[
P
(
node
,
multiply_one
,
(
right
,
left
))]
if
n
.
value
==
1
:
p
.
append
(
P
(
node
,
multiply_one
,
(
scope
,
n
)))
if
right
.
value
==
1
:
return
[
P
(
node
,
multiply_one
,
(
left
,
right
))]
for
c0
,
c1
in
combinations
(
numerics
,
2
)
:
p
.
append
(
P
(
node
,
multiply_numerics
,
(
scope
,
c0
,
c1
)))
return
[]
return
p
def
multiply_
one
(
root
,
args
):
def
multiply_
zero
(
root
,
args
):
"""
a * 1 -> a
1 * a -> a
-1 * a -> -a
1 * -a -> -a
-1 * -a -> a
0 * a -> 0
-0 * a -> -0
"""
a
,
one
=
args
return
a
.
negate
(
one
.
negated
+
root
.
negated
)
return
args
[
0
].
negate
(
root
.
negated
)
MESSAGES
[
multiply_
one
]
=
_
(
'Multiplication with one yields the multiplicant
.'
)
MESSAGES
[
multiply_
zero
]
=
_
(
'Multiplication with zero yields zero
.'
)
def
m
atch_multiply_numerics
(
node
):
def
m
ultiply_one
(
root
,
args
):
"""
3 * 2 -> 6
3.0 * 2 -> 6.0
3 * 2.0 -> 6.0
3.0 * 2.0 -> 6.0
1 * a -> a
-1 * a -> -a
"""
assert
node
.
is_op
(
OP_MUL
)
scope
,
one
=
args
scope
.
remove
(
one
)
p
=
[]
scope
=
Scope
(
node
)
numerics
=
filter
(
lambda
n
:
n
.
is_numeric
(),
scope
)
return
scope
.
as_nary_node
().
negate
(
one
.
negated
)
for
c0
,
c1
in
combinations
(
numerics
,
2
):
p
.
append
(
P
(
node
,
multiply_numerics
,
(
scope
,
c0
,
c1
)))
return
p
MESSAGES
[
multiply_one
]
=
_
(
'Multiplication with one yields the multiplicant.'
)
def
multiply_numerics
(
root
,
args
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment