Oh, indeed I wasn't aware of this. That's great news—thank you, Frerich!

This actually leads me to another question: what are the tradeoffs between implementing this via Template Haskell as in the case of this package vs the `deriving` mechanism as for the foldable instance?

V

On 2018-02-13, at 00:03, Theodore Lief Gannon <tanuki@gmail.com> wrote:

Vilem, you may have missed the post from the catamorphisms author where he updated the library in response to this thread? :)

On Feb 12, 2018 3:58 PM, "Vilem-Benjamin Liepelt" <vl81@kent.ac.uk> wrote:
Thank you for your great suggestions.

The type-foo looks very cool and I will have to dig more into the different options.

Generating a catamorphism using the library of the same name works like a charm and integrates well with my existing code—once I managed to get it to install (thank you Stack LTS!) it just worked out of the box and let me write a one-line evaluator and a one-line pretty-printer. Woohoo!

Unfortunately It's not really viable to use this "for real" at the moment because it requires such an old version of GHC.

Something I miss is the clear correspondence between data constructors and "substitutions" (like in my `magic` example), since they become entirely positional, leading to potentially brittle code (imagine reordering the data constructors). I think some of the other solutions might be better in this respect.

It's a shame that the catamorphism package doesn't work for a more up-to-date version of GHC, because I think I would use this quite often.

I suppose the fold that Sergey proposed is essentially what the catamorphism package generates. Although I want to avoid having to write functions by hand when really the computer should be doing them for me, I think I will use this for now as it integrates nicely with my existing code and leads to quite idiomatic Haskell.

I will definitely check out the other suggestions as well though, thank you again.

Best,

Vilem

> On 2018-02-12, at 09:08, Frerich Raabe <raabe@froglogic.com> wrote:
>
> On 2018-02-12 03:30, Vilem-Benjamin Liepelt wrote:
>> I am looking for a solution to get rid of this silly boilerplate:
>> eval :: Ord var => Map var Bool -> Proposition var -> Bool
>> eval ctx prop = evalP $ fmap (ctx Map.!) prop
>>  where
>>    evalP = \case
>>        Var b -> b
>>        Not q -> not $ evalP q
>>        And p q -> evalP p && evalP q
>>        Or p q -> evalP p || evalP q
>>        If p q -> evalP p ==> evalP q
>>        Iff p q -> evalP p == evalP q
>
> [..]
>
> You might benefit from the 'catamorphism' package:
>
https://hackage.haskell.org/package/catamorphism-0.5.1.0/docs/Data-Morphism-Cata.html
>
> It provides a template Haskell function which, given a data type, produces a function which reduces (folds) that data type.
>
> --
> Frerich Raabe - raabe@froglogic.com
> www.froglogic.com - Multi-Platform GUI Testing

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.