[Haskell-cafe] Maybe use advice

Lyndon Maydwell maydwell at gmail.com
Tue Jun 7 11:12:34 CEST 2011


The fixpoint nature of rewrite catches some cases that transform might
not if I'm interpreting it correctly.

(Changes [Translate 1 1, Scale 1 1, Translate 1 1]) could be rewritten
as (Translate 2 2), but I'm not sure that it could be translated as
such if it matches against (Changes [Translate _ _, Translate _ _])
first.

I have the code on github at
https://github.com/sordina/Diagrams-AST/blob/master/src/Graphics/Rendering/Diagrams/AST/Optimize.hs
if you're interested.

At the moment I'm not worrying about speed as I really just wrote this
optimisation function as a demo of why an AST interface to Diagrams
might be useful.

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



More information about the Haskell-Cafe mailing list