2

How do I lookup type operator name? This does not work:

IssueTH.hs:

{-# LANGUAGE TemplateHaskell #-}
module IssueTH where
import Language.Haskell.TH

f :: Q [Dec]
f = do
    Just n <- lookupTypeName "GHC.TypeLits.*"
    return []

Issue.hs:

{-# LANGUAGE TemplateHaskell #-}
module Issue where
import IssueTH

$f

ghc Issue.hs fails with message:

Pattern match failure in do expression at IssueTH.hs

Replacing "GHC.TypeLits.*" with "GHC.TypeLits.(*)" or "*" doesn't work either.

Alexey Vagarenko
  • 1,206
  • 8
  • 15
  • 1. You're missing `import GHC.TypeLits` in the second file, without that it wouldn't work even with a non-operator type. 2. After fixing that, this still looks like a bug to me, so I [reported it](https://ghc.haskell.org/trac/ghc/ticket/11046#ticket). – Ørjan Johansen Nov 01 '15 at 20:13
  • I [just discovered](https://ghc.haskell.org/trac/ghc/ticket/11046#comment:1) that it works if the type operator starts with `:`, so my guess is the function wasn't updated when their syntax was liberalized. – Ørjan Johansen Nov 01 '15 at 20:30
  • @Ørjan Johansen, I lookup fully qualified "GHC.TypeLits.*", I don't think I need import GHC.TypeLits there, but it still doesn't work even with import as you see. – Alexey Vagarenko Nov 02 '15 at 04:34
  • The `*` operator doesn't work with or without import, or with or without qualification. But for e.g. `Nat`, you need an import, even with a fully qualified name (and it works without qualification too). Testing a bit further, it respects `qualified` and `hiding` on the import too. – Ørjan Johansen Nov 02 '15 at 10:53

1 Answers1

3

I guess I have enough now for a brief answer. Alas I only found the reason for your problem, but not how to solve it.

My testing shows that lookupTypeName does support type operators, but only if they start with :.

Originally this was a requirement, in analogy with infix data constructors, but this was lifted to allow things like the arithmetical type operators in GHC.TypeLits. (The downside is that you can no longer have type operator variables, as were once popular for things like Arrow code.)

Presumably lookupTypeName was not updated to take this into account, and I have filed a bug report for this.

EDIT: A fix for this has finally been made, and should be in the upcoming GHC 8.2.1.

Ørjan Johansen
  • 18,119
  • 3
  • 43
  • 53