<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">I am looking for a solution to get rid of this silly boilerplate:</div><div class=""><br class=""></div><div class=""><div style="font-family: Menlo;" class="">eval :: Ord var => Map var Bool -> Proposition var -> Bool</div><div style="font-family: Menlo;" class="">eval ctx prop = evalP $ fmap (ctx Map.!) prop</div><div style="font-family: Menlo;" class=""> where</div><div style="font-family: Menlo;" class=""> evalP = \case</div><div style="font-family: Menlo;" class=""> Var b -> b</div><div style="font-family: Menlo;" class=""> Not q -> not $ evalP q</div><div style="font-family: Menlo;" class=""> And p q -> evalP p && evalP q</div><div style="font-family: Menlo;" class=""> Or p q -> evalP p || evalP q</div><div style="font-family: Menlo;" class=""> If p q -> evalP p ==> evalP q</div><div style="font-family: Menlo;" class=""> Iff p q -> evalP p == evalP q</div></div><div style="font-family: Menlo;" class=""><br class=""></div><div class="">What I would like to do in essence is to replace the data constructors like so:</div></div><div class=""><br class=""></div><div class=""><font face="Menlo" class=""><div class=""><div class="">-- Not valid Haskell!! Can't pattern match on constructor only...</div><div class="">magic = \case</div><div class=""> Var -> id</div><div class=""> Not -> not</div><div class=""> And -> (&&)</div><div class=""> Or -> (||)</div><div class=""> If -> (==>)</div><div class=""> Iff -> (==)</div></div><div class=""><br class=""></div><div class=""><div class="">compile = transformAST magic $ fmap (\case 'P' -> False; 'Q' -> True)</div><div class=""><br class=""></div><div class="">>>> compile (Iff (Not (And (Var 'P') (Var 'Q'))) (Or (Not (Var 'P')) (Not (Var 'Q'))))</div><div class=""> ((==) (not ((&&) (id True) (id False))) ((||) (not (id True)) (not (id False))))</div></div><div class=""><br class=""></div></font></div><div class="">Note how the compiled expression exactly mirrors the AST, so there should be some meta programming technique for this.</div><div class=""><br class=""></div><div class="">Does anyone have an idea how I can achieve this?</div><div class=""><br class=""></div><div class="">The full source code is here: <a href="https://gist.github.com/vimuel/7dcb8a9f1d2b7b72f020d66ec4157d7b" class="">https://gist.github.com/vimuel/7dcb8a9f1d2b7b72f020d66ec4157d7b</a></div><div class=""><br class=""></div><div class="">I am happy to take any other comments relating to my code...</div><div class=""><br class=""></div><div class="">Best,</div><div class=""><br class=""></div><div class="">Vilem</div></body></html>