Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#9641 closed bug (fixed)

Point-free do block gives missing instance error

Reported by: rasfar Owned by: rwbarton
Priority: normal Milestone:
Component: Documentation Version: 7.8.1
Keywords: plugins HasDynFlags Cc:
Operating System: Linux Architecture: x86
Type of failure: Documentation bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

https://www.haskell.org/ghc/docs/latest/html/users_guide/compiler-plugins.html#manipulating-bindings

This API example won't compile for me.

module SayNames.Plugin (plugin) where
import GhcPlugins

plugin :: Plugin
plugin = defaultPlugin {
  installCoreToDos = install
  }

install :: [CommandLineOption] -> [CoreToDo] -> CoreM [CoreToDo]
install _ todo = do
  reinitializeGlobals
  return (CoreDoPluginPass "Say name" pass : todo)

pass :: ModGuts -> CoreM ModGuts
pass = do dflags <- getDynFlags
          bindsOnlyPass (mapM (printBind dflags))
  where printBind :: DynFlags -> CoreBind -> CoreM CoreBind
        printBind dflags bndr@(NonRec b _) = do
          putMsgS $ "Non-recursive binding named " ++ showSDoc dflags (ppr b)
          return bndr 
        printBind _ bndr = return bndr

Compiling (GHC 7.8.1, or 7.6.3). I get:

SayNames/Plugin.hs:25:21:
    No instance for (HasDynFlags ((->) ModGuts))
      arising from a use of ‘getDynFlags’
    In a stmt of a 'do' block: dflags <- getDynFlags
    In the expression:
      do { dflags <- getDynFlags;
           bindsOnlyPass (mapM (printBind dflags)) }
    In an equation for ‘pass’:
        pass
          = do { dflags <- getDynFlags;
                 bindsOnlyPass (mapM (printBind dflags)) }
          where
              printBind :: DynFlags -> CoreBind -> CoreM CoreBind
              printBind dflags bndr@(NonRec b _)
                = do { putMsgS
                       $ "Non-recursive binding named " ++ showSDoc dflags (ppr b);
                       .... }
              printBind _ bndr = return bndr

Changing to pointful style it compiles and works:

pass modguts = do dflags <- getDynFlags
                  bindsOnlyPass (mapM (printBind dflags)) modguts

Maybe this is a compiler bug, actually? Dunno, I never use point-free style with monadic functions.

Change History (4)

comment:1 Changed 5 years ago by rwbarton

Owner: set to rwbarton

Thanks for the report. Yes, it is a documentation bug, just a mixup when the example was updated for 7.6.

comment:2 Changed 5 years ago by Reid Barton <rwbarton@…>

In 071167c793489f4071c348223f9591d20dbe11a3/ghc:

User's Guide: Fix compiler plugin example (#9641, #7682)

The previous fix was incorrectly eta-reduced.

comment:3 Changed 5 years ago by rwbarton

Resolution: fixed
Status: newclosed

comment:4 Changed 5 years ago by rasfar

I can't determine whether it's against etiquette to comment "Thanks!" here after it's closed, but since I already did, thanks...

Note: See TracTickets for help on using tickets.