<div dir="ltr"> Hi Chris,<div><br></div><div>thanks for insightful links. At the first glance, I think the main difference is that machines and iteratees process streams of data, while catamorphisms work on general recursive data structures. (I used "count" + "sum" in the example, which could lead to the impression that it's list oriented.)</div>
<div><br></div><div>However, it seems to me that there is some connection between cata/anamorphisms and free (co)monads generated by a functor. I'm just guessing - perhaps using such a monad in a monadic pipe would lead to a similar result?</div>
<div><br></div><div>BTW, while it seems that using existentials in by Cata data type is natural, I'd like to know if I could do it without them. If you have any ideas, please let me know.</div><div><br></div><div> Best regards,</div>
<div> Petr</div><div><br></div><div>PS: Is there actually anything left that ekmett hasn't implemented?</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/1/27 Chris Wong <span dir="ltr"><<a href="mailto:chrisyco+haskell-cafe@gmail.com" target="_blank">chrisyco+haskell-cafe@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Petr,<br>
<br>
Congratulations -- you've just implemented a Moore machine! [1]<br>
<br>
I posted something very much like this just last year [2]. It's a very<br>
common pattern in Haskell, forming the basis of coroutines and<br>
iteratees and many other things.<br>
<br>
Edward Kmett includes it in his machines package [3]. His variation,<br>
like mine, hides the state inside a closure, removing the need for<br>
existentials. pipes 2.0 contains one implemented as a free monad [4].<br>
<br>
[1] <a href="http://en.wikipedia.org/wiki/Moore_machine" target="_blank">http://en.wikipedia.org/wiki/Moore_machine</a><br>
[2] <a href="http://hackage.haskell.org/packages/archive/machines/0.2.3/doc/html/Data-Machine-Moore.html" target="_blank">http://hackage.haskell.org/packages/archive/machines/0.2.3/doc/html/Data-Machine-Moore.html</a><br>
[3] <a href="http://www.haskell.org/pipermail/haskell-cafe/2012-May/101460.html" target="_blank">http://www.haskell.org/pipermail/haskell-cafe/2012-May/101460.html</a><br>
[4] <a href="http://hackage.haskell.org/packages/archive/pipes/2.0.0/doc/html/Control-Pipe-Common.html" target="_blank">http://hackage.haskell.org/packages/archive/pipes/2.0.0/doc/html/Control-Pipe-Common.html</a><br>
<br>
Chris<br>
<div><div class="h5"><br>
On Sun, Jan 27, 2013 at 11:03 AM, Petr P <<a href="mailto:petr.mvd@gmail.com">petr.mvd@gmail.com</a>> wrote:<br>
> Dear Haskellers,<br>
><br>
> I read some stuff about attribute grammars recently [1] and how UUAGC [2]<br>
> can be used for code generation. I felt like this should be possible inside<br>
> Haskell too so I did some experiments and I realized that indeed<br>
> catamorphisms can be represented in such a way that they can be combined<br>
> together and all run in a single pass over a data structure. In fact, they<br>
> form an applicative functor.<br>
><br>
> [1] <a href="http://www.haskell.org/haskellwiki/Attribute_grammar" target="_blank">http://www.haskell.org/haskellwiki/Attribute_grammar</a><br>
> [2] Utrecht University Attribute Grammar Compiler<br>
><br>
> To give an example, let's say we want to compute the average value of a<br>
> binary tree. If we compute a sum first and then count the elements, the<br>
> whole tree is retained in memory (and moreover, deforestation won't happen).<br>
> So it's desirable to compute both values at once during a single pass:<br>
><br>
> -- Count nodes in a tree.<br>
> count' :: (Num i) => CataBase (BinTree a) i<br>
> count' = ...<br>
><br>
> -- Sums all nodes in a tree.<br>
> sum' :: (Num n) => CataBase (BinTree n) n<br>
> sum' = ...<br>
><br>
> -- Computes the average value of a tree.<br>
> avg' :: (Fractional b) => CataBase (BinTree b) b<br>
> avg' = (/) <$> sum' <*> count'<br>
><br>
> Then we can compute the average in a single pass like<br>
><br>
> runHylo avg' treeAnamorphism seed<br>
><br>
> My experiments together with the example are available at<br>
> <a href="https://github.com/ppetr/recursion-attributes" target="_blank">https://github.com/ppetr/recursion-attributes</a><br>
><br>
> I wonder, is there an existing library that expresses this idea?<br>
><br>
> Best regards,<br>
> Petr Pudlak<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
><br>
</blockquote></div><br></div></div>