[Haskell-cafe] Replace data constructors via meta programming

Frerich Raabe raabe at froglogic.com
Mon Feb 12 09:08:24 UTC 2018


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