Hi,<br><br>Sorry for taking so long to reply. I don&#39;t have a strong opinion about this; if no one objects I&#39;ll add it.<br><br><br>Thanks,<br>Pedro<br><br><div class="gmail_quote">On Tue, May 15, 2012 at 2:51 AM, Andrew Miller <span dir="ltr">&lt;<a href="mailto:ak.miller@auckland.ac.nz" target="_blank">ak.miller@auckland.ac.nz</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
One thing that is hard to do with SYB (as well as with alternative generics packages in Haskell) at present is to query while keeping state that is carried down the tree but not to siblings, and use that in deciding what to return from the query.<br>


<br>
I propose a new scheme be added to Data.Generics.Schemes, called everythingWithContext (as defined below). An everywhereWithContext might also be useful, but I don&#39;t think you would be able to define the transformation using the existing combinators, so I have limited my proposal to everythingWithContext for now.<br>


<br>
Yours Sincerely,<br>
Andrew Miller<br>
<br>
{- | Summarise all nodes in top-down, left-to-right order, carrying some state down<br>
     the tree during the computation, but not left-to-right to siblings.<br>
     Example: Suppose you want to compute the maximum depth of adds in the below<br>
      simple co-recursive structure, ignoring all the other constructors. You could<br>
      write code like the following:<br>
<br>
data MyStructure = SomeConst Int | Add MyStructure MyStructure | Times MyStructure MyStructure | Wrapped Wrapper deriving (Data, Typeable)<br>
data Wrapper = Wrapper MyStructure deriving (Data, Typeable)<br>
<br>
myExample = Add (SomeConst 10) (Add (Wrapped . Wrapper $ (Add (Add (Add (Times (SomeConst 30) (SomeConst 90)) (SomeConst 70)) (SomeConst 40)) (SomeConst 50))) (Add (SomeConst 20) (Add (SomeConst 60) (SomeConst 80))))<br>


<br>
computeDepth = everythingWithContext 0 max ((\s -&gt; (0, s)) `mkQ` depthOfAdd)<br>
  where<br>
    depthOfAdd (Add _ _) s = (s, s + 1)<br>
    depthOfAdd _ s = (s, s)<br>
<br>
main = print $ computeDepth myExample<br>
 -}<br>
everythingWithContext :: s -&gt; (r -&gt; r -&gt; r) -&gt; GenericQ (s -&gt; (r, s)) -&gt; GenericQ r<br>
everythingWithContext s0 f q x =<br>
  foldl f r (gmapQ (everythingWithContext s&#39; f q) x)<br>
    where (r, s&#39;) = q x s0<br>
<br>
<br>
______________________________<u></u>_________________<br>
Generics mailing list<br>
<a href="mailto:Generics@haskell.org" target="_blank">Generics@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/generics" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/generics</a><br>
</blockquote></div><br>