I definitely think these functions should be added to syb. I certainly could not have written them myself without hours, perhaps days, of study.<br><br><div class="gmail_quote">2009/6/2 José Pedro Magalhães <span dir="ltr"><<a href="mailto:jpm@cs.uu.nl">jpm@cs.uu.nl</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hello,<br><br>Would there be interest in having this function added to the SYB library?<br><br>
<br>Thanks,<br>Pedro<br><br><div class="gmail_quote"><div><div></div><div class="h5">On Tue, Jun 2, 2009 at 00:40, Ralf Laemmel <span dir="ltr"><<a href="mailto:rlaemmel@gmail.com" target="_blank">rlaemmel@gmail.com</a>></span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5"><div>> Thank you! What I have in mind is three way merging - you have two<br>
> revisions based on the same original value, and you need to decide whether<br>
> they can be merged automatically or they need to be merged by a user. You<br>
> only have a real conflict when both revisions differ from the original and<br>
> from each other.<br>
<br>
</div>Here is the completed exercise.<br>
For comparison, the two args versions are shown up-front.<br>
There is gzipWithM3 needed for gzip3, and gzip3 itself.<br>
I also made it so that the top-level gzip functions have the<br>
appropriate polymorphism.<br>
Say same type for the args rather than independent polymorphism.<br>
<br>
{-# LANGUAGE RankNTypes #-}<br>
<div><br>
import Prelude hiding (GT)<br>
import Data.Generics<br>
<br>
-- As originally defined: Twin map for transformation<br>
<br>
gzipWithT2 :: GenericQ (GenericT) -> GenericQ (GenericT)<br>
gzipWithT2 f x y = case gmapAccumT perkid funs y of<br>
([], c) -> c<br>
_ -> error "gzipWithT2"<br>
where<br>
perkid a d = (tail a, unGT (head a) d)<br>
funs = gmapQ (\k -> GT (f k)) x<br>
<br>
<br>
-- As originally defined: Twin map for transformation<br>
<br>
</div>gzipWithM2 :: Monad m => GenericQ (GenericM m) -> GenericQ (GenericM m)<br>
gzipWithM2 f x y = case gmapAccumM perkid funs y of<br>
([], c) -> c<br>
_ -> error "gzipWithM"<br>
where<br>
perkid a d = (tail a, unGM (head a) d)<br>
funs = gmapQ (\k -> GM (f k)) x<br>
<br>
<br>
-- As originally defined: generic zip<br>
<br>
gzip2 ::<br>
(forall x. Data x => x -> x -> Maybe x)<br>
-> (forall x. Data x => x -> x -> Maybe x)<br>
<br>
gzip2 f = gzip2' f'<br>
where<br>
f' :: GenericQ (GenericM Maybe)<br>
f' x y = cast x >>= \x' -> f x' y<br>
gzip2' :: GenericQ (GenericM Maybe) -> GenericQ (GenericM Maybe)<br>
gzip2' f x y =<br>
f x y<br>
`orElse`<br>
if toConstr x == toConstr y<br>
then gzipWithM2 (gzip2' f) x y<br>
else Nothing<br>
<div><br>
<br>
-- For three args now<br>
<br>
gzipWithT3 ::<br>
GenericQ (GenericQ (GenericT))<br>
-> GenericQ (GenericQ (GenericT))<br>
gzipWithT3 f x y z =<br>
case gmapAccumT perkid funs z of<br>
([], c) -> c<br>
_ -> error "gzipWithT3"<br>
where<br>
perkid a d = (tail a, unGT (head a) d)<br>
funs = case gmapAccumQ perkid' funs' y of<br>
([], q) -> q<br>
_ -> error "gzipWithT3"<br>
where<br>
perkid' a d = (tail a, unGQ (head a) d)<br>
funs' = gmapQ (\k -> (GQ (\k' -> GT (f k k')))) x<br>
<br>
</div>gzipWithM3 :: Monad m<br>
=> GenericQ (GenericQ (GenericM m))<br>
-> GenericQ (GenericQ (GenericM m))<br>
gzipWithM3 f x y z =<br>
case gmapAccumM perkid funs z of<br>
([], c) -> c<br>
_ -> error "gzipWithM3"<br>
where<br>
perkid a d = (tail a, unGM (head a) d)<br>
<div> funs = case gmapAccumQ perkid' funs' y of<br>
([], q) -> q<br>
</div> _ -> error "gzipWithM3"<br>
<div> where<br>
perkid' a d = (tail a, unGQ (head a) d)<br>
</div> funs' = gmapQ (\k -> (GQ (\k' -> GM (f k k')))) x<br>
<br>
gzip3 ::<br>
(forall x. Data x => x -> x -> x -> Maybe x)<br>
-> (forall x. Data x => x -> x -> x -> Maybe x)<br>
<br>
gzip3 f = gzip3' f'<br>
where<br>
f' :: GenericQ (GenericQ (GenericM Maybe))<br>
f' x y z = cast x >>= \x' -> cast y >>= \y' -> f x' y' z<br>
gzip3' ::<br>
GenericQ (GenericQ (GenericM Maybe))<br>
-> GenericQ (GenericQ (GenericM Maybe))<br>
gzip3' f x y z =<br>
f x y z<br>
`orElse`<br>
if and [toConstr x == toConstr y, toConstr y == toConstr z]<br>
then gzipWithM3 (gzip3' f) x y z<br>
else Nothing<br>
</div></div><div><div></div><div>_______________________________________________<br>
Haskell mailing list<br>
<a href="mailto:Haskell@haskell.org" target="_blank">Haskell@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell" target="_blank">http://www.haskell.org/mailman/listinfo/haskell</a><br>
</div></div></blockquote></div><br>
<br>_______________________________________________<br>
Haskell mailing list<br>
<a href="mailto:Haskell@haskell.org">Haskell@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell" target="_blank">http://www.haskell.org/mailman/listinfo/haskell</a><br>
<br></blockquote></div><br>