<div class="gmail_quote"><div>Is it necessary (helpful) to use &#39;rewrite&#39;?  Nearly every time I&#39;ve tried it, in the end &#39;transform&#39; has been a better choice.  Then you wouldn&#39;t need the &#39;Just&#39;s at all, and it should work fine.</div>
<div><br></div><div>John</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
From: Lyndon Maydwell &lt;<a href="mailto:maydwell@gmail.com">maydwell@gmail.com</a>&gt;<br>
<br>
(missed including cafe)<br>
<br>
f :: [Modification] -&gt; Maybe [Modification]<br>
and<br>
f _ = Just $ f ...<br>
are incompatible<br>
<br>
I managed to get the behaviour I&#39;m after with the use of Either, but<br>
this really is messy:<br>
<br>
<br>
-- Sets of changes<br>
o (Modifier (Changes [])  i) = Just $ i<br>
o (Modifier (Changes [c]) i) = Just $ Modifier c i<br>
o (Modifier (Changes l)   i) = g (f (Left l))<br>
  where<br>
    g (Right l) = Just $ Modifier (Changes l) i<br>
    g (Left  l) = Nothing<br>
<br>
    f (Left  (Scale     x y : Scale     x&#39; y&#39; : l)) =<br>
        f $ Right $ Scale     (x*x&#39;) (y*y&#39;) : h (f $ Left l)<br>
    f (Left  (Translate x y : Translate x&#39; y&#39; : l)) =<br>
        f $ Right $ Translate (x+x&#39;) (y+y&#39;) : h (f $ Left l)<br>
    f (Left  (Rotate    x   : Rotate    x&#39;    : l)) =<br>
        f $ Right $ Rotate    (x+x&#39;)        : h (f $ Left l)<br>
    f x = x<br>
<br>
    h (Left  l) = l<br>
    h (Right l) = l<br>
<br>
<br>
On Tue, Jun 7, 2011 at 3:11 AM, Maciej Marcin Piechotka<br>
&lt;<a href="mailto:uzytkownik2@gmail.com">uzytkownik2@gmail.com</a>&gt; wrote:<br>
&gt; On Mon, 2011-06-06 at 23:38 +0800, Lyndon Maydwell wrote:<br>
&gt;&gt; I&#39;m writing an optimisation routine using Uniplate. Unfortunately, a<br>
&gt;&gt; sub-function I&#39;m writing is getting caught in an infinite loop because<br>
&gt;&gt; it doesn&#39;t return Nothing when there are no optimisations left.<br>
&gt;&gt;<br>
&gt;&gt; I&#39;d like a way to move the last Just into f, but this makes recursion<br>
&gt;&gt; very messy. I was wondering if there was a nice way to use something<br>
&gt;&gt; like the Monad or Applicative instance to help here.<br>
&gt;&gt;<br>
&gt;&gt; -- Sets of changes<br>
&gt;&gt; o (Modifier (Changes []) ?i) = Just $ i<br>
&gt;&gt; o (Modifier (Changes [c]) i) = Just $ Modifier c i<br>
&gt;&gt; o (Modifier (Changes l) ? i) = Just $ Modifier (Changes (f l)) i<br>
&gt;&gt; ? where<br>
&gt;&gt; ? ? f (Scale ? ? x y : Scale ? ? x&#39; y&#39; : l) = f $ Scale ? ? (x*x&#39;) (y*y&#39;) : f l<br>
&gt;&gt; ? ? f (Translate x y : Translate x&#39; y&#39; : l) = f $ Translate (x+x&#39;) (y+y&#39;) : f l<br>
&gt;&gt; ? ? f (Rotate ? ?x ? : Rotate ? ?x&#39; ? ?: l) = f $ Rotate ? ?(x+x&#39;) ? ? ? ?: f l<br>
&gt;&gt; ? ? f l = l<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Any ideas?<br>
&gt;<br>
&gt; Something like:<br>
&gt;<br>
&gt; ...<br>
&gt; f (Rotate ? ?x ? : Rotate ? ?x&#39; ? ?: l)<br>
&gt; ? ?= Just $ f (Rotate (x+x&#39;) : fromMaybe l (f l))<br>
&gt; f l = Nothing -- As far as I understend<br>
&gt;<br>
&gt; Regards<br>
&gt;<br>
&gt; _______________________________________________<br></blockquote></div>