class_docstr: A set of rewrite rules.
  
  Forms a structure for fast rewriting over a set of rewrite rules. This
  allows for syntactic matching of terms to patterns for many patterns at
  the same time.
  
  Examples
  --------
  
  >>> import dask.rewrite as dr
  >>> def f(*args): pass
  >>> def g(*args): pass
  >>> def h(*args): pass
  >>> from operator import add
  
  >>> rs = dr.RuleSet(
  ...         dr.RewriteRule((add, 'x', 0), 'x', ('x',)),
  ...         dr.RewriteRule((f, (g, 'x'), 'y'),
  ...                        (h, 'x', 'y'),
  ...                        ('x', 'y')))
  
  >>> rs.rewrite((add, 2, 0))
  2
  
  >>> rs.rewrite((f, (g, 'a', 3)))    # doctest: +ELLIPSIS
  (<function h at ...>, 'a', 3)
  
  >>> dsk = {'a': (add, 2, 0),
  ...        'b': (f, (g, 'a', 3))}
  
  >>> from toolz import valmap
  >>> valmap(rs.rewrite, dsk)         # doctest: +ELLIPSIS
  {'a': 2, 'b': (<function h at ...>, 'a', 3)}
  
  Attributes
  ----------
  rules : list
      A list of `RewriteRule`s included in the `RuleSet`.
class_docstr_tok: ["A","set","of","rewrite","rules",".","Forms","a","structure","for","fast","rewriting","over","a","set","of","rewrite","rules",".","This","allows","for","syntactic","matching","of","terms","to","patterns","for","many","patterns","at","the","same","time",".","Examples","--","--","--","--",">",">",">","import","dask.rewrite","as","dr",">",">",">","def","f","(","*","args",")",":","pass",">",">",">","def","g","(","*","args",")",":","pass",">",">",">","def","h","(","*","args",")",":","pass",">",">",">","from","operator","import","add",">",">",">","rs","=","dr.RuleSet","(","...","dr.RewriteRule","(","(","add",",","'","x","'",",","0",")",",","'","x","'",",","(","'","x","'",",",")",")",",","...","dr.RewriteRule","(","(","f",",","(","g",",","'","x","'",")",",","'","y","'",")",",","...","(","h",",","'","x","'",",","'","y","'",")",",","...","(","'","x","'",",","'","y","'",")",")",")",">",">",">","rs.rewrite","(","(","add",",","2",",","0",")",")","2",">",">",">","rs.rewrite","(","(","f",",","(","g",",","'","a","'",",","3",")",")",")","#","doctest",":","+ELLIPSIS","(","<","function","h","at","...",">",",","'","a","'",",","3",")",">",">",">","dsk","=","{","'","a","'",":","(","add",",","2",",","0",")",",","...","'","b","'",":","(","f",",","(","g",",","'","a","'",",","3",")",")","}",">",">",">","from","toolz","import","valmap",">",">",">","valmap","(","rs.rewrite",",","dsk",")","#","doctest",":","+ELLIPSIS","{","'","a","'",":","2",",","'","b","'",":","(","<","function","h","at","...",">",",","'","a","'",",","3",")","}","Attributes","--","--","--","--","--","rules",":","list","A","list","of","`","RewriteRule","`","s","included","in","the","`","RuleSet","`","."]
docstr: 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 rewriting strategy to use. Options are "bottom_up" (default),
      or "top_level".
  
  Examples
  --------
  Suppose there was a function `add` that returned the sum of 2 numbers,
  and another function `double` that returned twice its input:
  
  >>> add = lambda x, y: x + y
  >>> double = lambda x: 2*x
  
  Now suppose `double` was *significantly* faster than `add`, so
  you'd like to replace all expressions `(add, x, x)` with `(double,
  x)`, where `x` is a variable. This can be expressed as a rewrite rule:
  
  >>> rule = RewriteRule((add, 'x', 'x'), (double, 'x'), ('x',))
  >>> rs = RuleSet(rule)
  
  This can then be applied to terms to perform the rewriting:
  
  >>> term = (add, (add, 2, 2), (add, 2, 2))
  >>> rs.rewrite(term)  # doctest: +SKIP
  (double, (double, 2))
  
  If we only wanted to apply this to the top level of the term, the
  `strategy` kwarg can be set to "top_level".
  
  >>> rs.rewrite(term)  # doctest: +SKIP
  (double, (add, 2, 2))
docstr_tok: ["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","rewriting","strategy","to","use",".","Options","are","``","bottom_up","''","(","default",")",",","or","``","top_level","''",".","Examples","--","--","--","--","Suppose","there","was","a","function","`","add","`","that","returned","the","sum","of","2","numbers",",","and","another","function","`","double","`","that","returned","twice","its","input",":",">",">",">","add","=","lambda","x",",","y",":","x","+","y",">",">",">","double","=","lambda","x",":","2","*","x","Now","suppose","`","double","`","was","*","significantly","*","faster","than","`","add","`",",","so","you","'d","like","to","replace","all","expressions","`","(","add",",","x",",","x",")","`","with","`","(","double",",","x",")","`",",","where","`","x","`","is","a","variable",".","This","can","be","expressed","as","a","rewrite","rule",":",">",">",">","rule","=","RewriteRule","(","(","add",",","'","x","'",",","'","x","'",")",",","(","double",",","'","x","'",")",",","(","'","x","'",",",")",")",">",">",">","rs","=","RuleSet","(","rule",")","This","can","then","be","applied","to","terms","to","perform","the","rewriting",":",">",">",">","term","=","(","add",",","(","add",",","2",",","2",")",",","(","add",",","2",",","2",")",")",">",">",">","rs.rewrite","(","term",")","#","doctest",":","+SKIP","(","double",",","(","double",",","2",")",")","If","we","only","wanted","to","apply","this","to","the","top","level","of","the","term",",","the","`","strategy","`","kwarg","can","be","set","to","``","top_level","''",".",">",">",">","rs.rewrite","(","term",")","#","doctest",":","+SKIP","(","double",",","(","add",",","2",",","2",")",")"]
code: def rewrite(self, task, strategy="bottom_up"):
      """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 rewriting strategy to use. Options are "bottom_up" (default),
          or "top_level".
  
      Examples
      --------
      Suppose there was a function `add` that returned the sum of 2 numbers,
      and another function `double` that returned twice its input:
  
      >>> add = lambda x, y: x + y
      >>> double = lambda x: 2*x
  
      Now suppose `double` was *significantly* faster than `add`, so
      you'd like to replace all expressions `(add, x, x)` with `(double,
      x)`, where `x` is a variable. This can be expressed as a rewrite rule:
  
      >>> rule = RewriteRule((add, 'x', 'x'), (double, 'x'), ('x',))
      >>> rs = RuleSet(rule)
  
      This can then be applied to terms to perform the rewriting:
  
      >>> term = (add, (add, 2, 2), (add, 2, 2))
      >>> rs.rewrite(term)  # doctest: +SKIP
      (double, (double, 2))
  
      If we only wanted to apply this to the top level of the term, the
      `strategy` kwarg can be set to "top_level".
  
      >>> rs.rewrite(term)  # doctest: +SKIP
      (double, (add, 2, 2))
      """
      return strategies[strategy](self, task)
  
imports: from __future__ import annotations
  from collections import deque
  from dask.core import istask, subs
label_desc: Use image node_id 5 for calling the RuleSet obj's underlying member method code with example usage: obj.rewrite(task, strategy) and returns: strategies