<div dir="ltr"><div class="gmail_default" style="font-size:small">You actually can pattern match on constructor only:</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"><div class="gmail_default">magic = \case</div><div class="gmail_default">    Var {} -> id</div><div class="gmail_default">    Not  {}-> not</div><div class="gmail_default">    And {} -> (&&)</div><div class="gmail_default">    Or {} -> (||)</div><div class="gmail_default">    If {} -> (==>)</div><div class="gmail_default">    Iff {} -> (==)</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Feb 11, 2018 at 6:30 PM, Vilem-Benjamin Liepelt <span dir="ltr"><<a href="mailto:vl81@kent.ac.uk" target="_blank">vl81@kent.ac.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div>Hi,</div><div><br></div><div>I am looking for a solution to get rid of this silly boilerplate:</div><div><br></div><div><div style="font-family:Menlo">eval :: Ord var => Map var Bool -> Proposition var -> Bool</div><div style="font-family:Menlo">eval ctx prop = evalP $ fmap (ctx Map.!) prop</div><div style="font-family:Menlo">  where</div><div style="font-family:Menlo">    evalP = \case</div><div style="font-family:Menlo">        Var b -> b</div><div style="font-family:Menlo">        Not q -> not $ evalP q</div><div style="font-family:Menlo">        And p q -> evalP p && evalP q</div><div style="font-family:Menlo">        Or p q -> evalP p || evalP q</div><div style="font-family:Menlo">        If p q -> evalP p ==> evalP q</div><div style="font-family:Menlo">        Iff p q -> evalP p == evalP q</div></div><div style="font-family:Menlo"><br></div><div>What I would like to do in essence is to replace the data constructors like so:</div></div><div><br></div><div><font face="Menlo"><div><div>-- Not valid Haskell!! Can't pattern match on constructor only...</div><div>magic = \case</div><div>    Var -> id</div><div>    Not -> not</div><div>    And -> (&&)</div><div>    Or -> (||)</div><div>    If -> (==>)</div><div>    Iff -> (==)</div></div><div><br></div><div><div>compile = transformAST magic $ fmap (\case 'P' -> False; 'Q' -> True)</div><div><br></div><div>>>> compile (Iff (Not (And (Var 'P') (Var 'Q'))) (Or (Not (Var 'P')) (Not (Var 'Q'))))</div><div>            ((==) (not ((&&) (id True) (id False))) ((||) (not (id True)) (not (id False))))</div></div><div><br></div></font></div><div>Note how the compiled expression exactly mirrors the AST, so there should be some meta programming technique for this.</div><div><br></div><div>Does anyone have an idea how I can achieve this?</div><div><br></div><div>The full source code is here: <a href="https://gist.github.com/vimuel/7dcb8a9f1d2b7b72f020d66ec4157d7b" target="_blank">https://gist.github.com/<wbr>vimuel/<wbr>7dcb8a9f1d2b7b72f020d66ec4157d<wbr>7b</a></div><div><br></div><div>I am happy to take any other comments relating to my code...</div><div><br></div><div>Best,</div><div><br></div><div>Vilem</div></div><br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div>