Opened 5 years ago

Last modified 5 years ago

#4863 new bug

TH crashes if you reify the Name of a dfun

Reported by: guest Owned by: simonpj
Priority: low Milestone:
Component: Template Haskell Version: 7.0.1
Keywords: Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: Compile-time crash Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

Prelude Language.Haskell.TH Language.Haskell.TH.Syntax> $(fmap show (classInstances ''Eq [ConT ''Int] >>= reify . head) >>= lift)

<interactive>:1:3:
    Exception when trying to run compile-time code:
      <interactive>: panic! (the 'impossible' happened)
  (GHC version 7.0.1 for x86_64-unknown-linux):
	reifyType PredTy
    <pred>base:GHC.Classes.Eq{tc 23} ghc-prim:GHC.Types.Int{(w) tc 3J}

Please report this as a GHC bug:  http://www.haskell.org/ghc/reportabug

      Code: (>>=)
              fmap
                (show)
                ((>>=)
                   classInstances 'Eq [Language.Haskell.TH.Syntax.ConT 'Int]
                   (.) reify head)
              lift
    In the expression:
      $(fmap show (classInstances 'Eq [ConT 'Int] >>= reify . head)
      >>=
        lift)
    In an equation for `it':
        it
          = $(fmap show (classInstances 'Eq [ConT 'Int] >>= reify . head)
            >>=
              lift)

The same thing happens when compiling with ghc rather than using GHCi. Also, it looks like the pretty printer is displaying ''Eq and ''Int incorrectly.

Change History (3)

comment:1 Changed 5 years ago by igloo

  • Milestone set to 7.0.3

comment:2 Changed 5 years ago by simonmar

  • Owner set to simonpj

comment:3 Changed 5 years ago by simonpj

  • Milestone changed from 7.0.3 to _|_
  • Priority changed from normal to low
  • Summary changed from ghc panic when reifying the result of classInstances to TH crashes if you reify the Name of a dfun

Looking at this I realise that TH.classInstances has completely the wrong type. It should be

classInstances :: Name -> [Type] -> Q [ClassInstance]

Before we were returning [Name] where the Name was the name of the dictionary function of the instance.

I've fixed this. It's a API change, but classInstances is very new anyway. Patch to HEAD:

Thu Jan 13 11:14:21 GMT 2011  [email protected]
  * Make Template Haskell classInstances function return [ClassInstance]
  
  This is a recently-introduce function, which was returning
  a [Name], being the names of the dfuns.  But what you really
  want (obviously!) is the ClassInstances, and we have a TH type
  for that.
  
  This is an API change, so don't merge into GHC 7.0.  But it's
  a new part of TH which is still settling down.
  
  Fixes Trac #4863.

    M ./compiler/typecheck/TcSplice.lhs -3 +2

Patch to template-haskell library:

Thu Jan 13 11:54:37 GMT 2011  [email protected]
  * Change type of TH.classInstances (and qClassInstances)
  
  This patch accompanies the HEAD commit
  
    Thu Jan 13 11:14:21 GMT 2011  [email protected]
    * Make Template Haskell classInstances function return [ClassInstance]
    
  It accomplishes the data type change

    M ./Language/Haskell/TH.hs -1 +1
    M ./Language/Haskell/TH/Syntax.hs -2 +2

There's still a residual problem, in that a ClassInstance still contains the Name of the dfun, and you can't reify that; or at least doing so gives the above crash. Why? Because TH.Type doesn't have a representation for a type like

df :: forall a. Eq a => Eq [a]

I'm not sure of the best way to fix that. For example, reifying a dfun could yield a ClassInstance rather than a VarI; but that would need a new constructor in TH.Info. So I'm just going to leave it for now until it becomes imporant to someone. Meanwhile I'll re-title and make low priority.

Note: See TracTickets for help on using tickets.