Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
trs
Commits
76634490
Commit
76634490
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Updated numeric rules to use Scope, so that they work for the new n-ary match system.
parent
822e6c4d
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/rules/numerics.py
+31
-46
31 additions, 46 deletions
src/rules/numerics.py
with
31 additions
and
46 deletions
src/rules/numerics.py
+
31
−
46
View file @
76634490
...
...
@@ -192,73 +192,58 @@ def match_multiply_zero(node):
return
[]
def
multiply_
zero
(
root
,
args
):
def
match_
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
match_
multiply_
numerics
(
node
):
def
multiply_
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
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment