Apply the `RuleSet` to `task`. This applies the most specific matching rule in the RuleSet to the task, using the provided strategy. Parameters ---------- term: a task The task to be rewritten strategy: str, optional The rewri
(self, task, strategy="bottom_up")
| 313 | return term |
| 314 | |
| 315 | def rewrite(self, task, strategy="bottom_up"): |
| 316 | """Apply the `RuleSet` to `task`. |
| 317 | |
| 318 | This applies the most specific matching rule in the RuleSet to the |
| 319 | task, using the provided strategy. |
| 320 | |
| 321 | Parameters |
| 322 | ---------- |
| 323 | term: a task |
| 324 | The task to be rewritten |
| 325 | strategy: str, optional |
| 326 | The rewriting strategy to use. Options are "bottom_up" (default), |
| 327 | or "top_level". |
| 328 | |
| 329 | Examples |
| 330 | -------- |
| 331 | Suppose there was a function `add` that returned the sum of 2 numbers, |
| 332 | and another function `double` that returned twice its input: |
| 333 | |
| 334 | >>> add = lambda x, y: x + y |
| 335 | >>> double = lambda x: 2*x |
| 336 | |
| 337 | Now suppose `double` was *significantly* faster than `add`, so |
| 338 | you'd like to replace all expressions `(add, x, x)` with `(double, |
| 339 | x)`, where `x` is a variable. This can be expressed as a rewrite rule: |
| 340 | |
| 341 | >>> rule = RewriteRule((add, 'x', 'x'), (double, 'x'), ('x',)) |
| 342 | >>> rs = RuleSet(rule) |
| 343 | |
| 344 | This can then be applied to terms to perform the rewriting: |
| 345 | |
| 346 | >>> term = (add, (add, 2, 2), (add, 2, 2)) |
| 347 | >>> rs.rewrite(term) # doctest: +SKIP |
| 348 | (double, (double, 2)) |
| 349 | |
| 350 | If we only wanted to apply this to the top level of the term, the |
| 351 | `strategy` kwarg can be set to "top_level". |
| 352 | |
| 353 | >>> rs.rewrite(term) # doctest: +SKIP |
| 354 | (double, (add, 2, 2)) |
| 355 | """ |
| 356 | return strategies[strategy](self, task) |
| 357 | |
| 358 | |
| 359 | def _top_level(net, term): |
nothing calls this directly
no outgoing calls
no test coverage detected