Skip to content

Generating unique names with template haskell

I need to use template haskell to generate a few instances where those instances each use a foreign function obtained from a particular FunPtr.

As far as I'm aware, the only clean way to do this requires top level declarations of the form: foreign import ccall "dynamic" makeFun1 :: FunPtr Foo -> Foo

If there was either:

  • Another way to do this without requiring top level named declarations.
  • The possibility to use these within a where clause (template haskell would suggest so given that it lets you use Dec within a where).

Then I'd be fine probably be fine without names.

I've tried using newName as it apparently generates fresh names but I cannot get it work for me.

I've put a copy of the test case here. I compile with stack build --ghc-options="-ddump-splices -ddump-to-file" and the error is:

th-name-issue/app/Main.hs:7:12: error:
    Multiple declarations of ‘makeFun’
    Declared at: app/Main.hs:7:12
                 app/Main.hs:7:12

Then I run gvim .stack-work/dist/x86_64-linux/Cabal-1.24.0.0/build/th-name-issue-exe/th-name-issue-exe-tmp/app/Main.dump-splices

And can see:

app/Main.hs:7:12-25: Splicing declarations
    template-haskell-2.11.0.0:Language.Haskell.TH.Quote.quoteDec
      importGen " stuff here "
  ======>
    foreign import ccall safe "dynamic" makeFun_a3pc
      :: GHC.Ptr.FunPtr (Foreign.C.String.CString -> IO ())
         -> Foreign.C.String.CString -> IO ()
    foreign import ccall safe "dynamic" makeFun_a3pd
      :: GHC.Ptr.FunPtr (Foreign.C.String.CString -> IO ())
         -> Foreign.C.String.CString -> IO ()

Which suggests that I did manage to get unique names after all, so I don't know why it didn't compile??

Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Template Haskell
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information