<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">I'd like to programmatically find out which functions in a module could possibly apply to a particular expression.</font></div><div class=""><br class=""></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">I also posted this to stack overflow.</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">To make this concrete:</font></div><div class=""><br class=""></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">{-# LANGUAGE TemplateHaskell #-}</font></div><div class=""><br class=""></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">module Test where</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- we'll import template-haskell from Lens</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- so we can create prisms automatically for our 'AST'</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">import qualified Control.Lens.TH as LTH</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">--- some 'AST' in a toy language</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">data CExp</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> = CLit Int -- a literal integer</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> | CAdd CExp CExp -- addition</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> | CMul CExp CExp -- multiplication</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> | CSub CExp CExp -- subtraction</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> deriving Show</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- an eval for our AST</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">eval :: CExp -> Int</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">eval exp =</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> case exp of</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> CLit i -> i</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> CAdd e1 e2 -></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> eval e1 + eval e2</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> CMul e1 e2 -></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> eval e1 * eval e2</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> CSub e1 e2 -></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> eval e1 - eval e2</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- a function to build a sum using add with our AST, from a list of Int values</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">listToSums :: [Int] -> CExp</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">listToSums =</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""> foldr CAdd (CLit 0) . fmap CLit</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- here we make prisms for looking at particular values</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- in the CExp AST</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">LTH.makePrisms ''CExp</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">-- let's have an expression:</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">theList1 :: CExp</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">theList1 = listToSums [1..38]</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">Now, at this point, I'd like a function that can give me a list of all the top level functions of a particular module (including this one) that are able to be applied to the expression theList1. This will include the prisms that were created with makePrisms.</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">It would be fine if it uses the hint library's Interpreter monad. I've been experimenting with it a bit, and while I can get a list of all of the definitions at the top level of any module, and I can find the types of them, too (more or less), I'm a bit lost about how to pass an expression in as an argument to these functions then check if that exprssions will typecheck.</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">If I can do that, I can run filter across all of the functions in a module, which lets me find out which ones are applicable.</font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class=""><br class=""></font></div><div class=""><font color="#242729" face="Arial, Helvetica Neue, Helvetica, sans-serif" class="">Many thanks in advance.</font></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>