<div dir="ltr">You might need to enable optimization for RULES to get picked up at all.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 11, 2019 at 11:03 PM Bill Hallahan <<a href="mailto:william.hallahan@yale.edu">william.hallahan@yale.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi,<br>
<br>
I'm trying to use the GHC API (8.2.2) to extract the rewrite rules from a module, but have run into some difficulties.  I've written the following code (all code is also attached as files):<br>
<br>
    module Main where<br>
<br>
    import GHC<br>
    import GHC.Paths<br>
    import HscTypes<br>
<br>
    main :: IO ()<br>
    main = do<br>
        loadProj "Test.hs"<br>
<br>
    loadProj :: FilePath -> IO ()<br>
    loadProj src = do<br>
        modgutss <- runGhc (Just libdir) $ do<br>
            flags <- getSessionDynFlags<br>
            _ <- setSessionDynFlags flags<br>
<br>
            target <- guessTarget src Nothing<br>
            _ <- setTargets [target]<br>
            _ <- load LoadAllTargets<br>
<br>
            mod_graph <- getModuleGraph<br>
            parsed_mods <- mapM parseModule mod_graph<br>
            typed_mods <- mapM typecheckModule parsed_mods<br>
            desug_mods <- mapM desugarModule typed_mods<br>
<br>
            return $ map coreModule desug_mods<br>
<br>
        let rules = map (\mg -> ( moduleNameString . moduleName $ mg_module mg<br>
                                , length $ mg_rules mg)<br>
                        ) modgutss<br>
        print $ rules<br>
<br>
        return ()<br>
<br>
It loads Test.hs (see below my signature) which contains a single rewrite rule.  Then, it obtains the ModGuts, and prints a tuple of the name of the module, and the length of the rule list.  I would expect this to be ("Test", 1), but, in fact, what gets printed is ("Test", 0).  For some reason, the ModGuts does not have the rewrite rule from the file.<br>
<br>
I'm guessing I'm misunderstanding something about how the GHC API works.  If anyone has any advice about how to fix this code to load rewrite rules (or even where to begin looking) I would appreciate it!<br>
<br>
Thanks,<br>
Bill Hallahan<br>
<br>
Test.hs:<br>
    module Test where<br>
<br>
    import Prelude hiding (map)<br>
<br>
    map :: (a -> b) -> [a] -> [b]<br>
    map f (x:xs) = f x:map f xs<br>
    map _ [] = []<br>
    {-# NOINLINE [0] map #-}<br>
<br>
    {-# RULES<br>
     "map/map"   forall f g xs . map f (map g xs) = map (f . g) xs<br>
      #-}<br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>brandon s allbery kf8nh</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a></div></div></div></div></div>