[Haskell-cafe] Replace data constructors via meta programming

Vilem-Benjamin Liepelt vl81 at kent.ac.uk
Mon Feb 12 23:55:15 UTC 2018


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 at 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 at froglogic.com
> www.froglogic.com - Multi-Platform GUI Testing



More information about the Haskell-Cafe mailing list