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 rewrite rules in RuleSet to top level of term
docstr_tok: ["Apply","the","rewrite","rules","in","RuleSet","to","top","level","of","term"]
code: def _rewrite(self, term):
      """Apply the rewrite rules in RuleSet to top level of term"""
  
      for rule, sd in self.iter_matches(term):
          # We use for (...) because it's fast in all cases for getting the
          # first element from the match iterator. As we only want that
          # element, we break here
          term = rule.subs(sd)
          break
      return term
  
imports: from __future__ import annotations
  from collections import deque
  from dask.core import istask, subs
label_desc: Use image node_id 4 for calling the RuleSet obj's underlying member method code with example usage: obj._rewrite(term) and returns: term