Opened 10 years ago

Closed 6 years ago

#4135 closed bug (fixed)

Can't Quote Instance Associated Types in Template Haskell

Reported by: Ashley Yakeley Owned by:
Priority: low Milestone: 7.6.2
Component: Template Haskell Version: 6.12.1
Keywords: Cc: jwlato@…, acfoltzer@…
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: GHC rejects valid program Test Case: th/T4135, T4135a
Blocked By: #4230 Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Give this a whirl:

{-# LANGUAGE TypeFamilies,TemplateHaskell #-}
module Bug where

    class C a where
        type T a

        instance C (Maybe a) where
            type T (Maybe a) = Char
$ ghc -c Bug.hs 
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Loading package array- ... linking ... done.
Loading package containers- ... linking ... done.
Loading package pretty- ... linking ... done.
Loading package template-haskell ... linking ... done.

    Type indexes must match class instance head
    Found `Maybe a[aMy]' but expected `Maybe a[aMx]'
    In the associated type instance for `T'
    In the instance declaration for `C (Maybe a[aMx])'

Change History (18)

comment:1 Changed 10 years ago by Ashley Yakeley

Bit someone else, too:

Workaround is to construct the instance in TH rather than use a quote.

comment:2 Changed 10 years ago by simonpj

Actually the email cited has a different problem

{-# LANGUAGE TemplateHaskell, MultiParamTypeClasses, TypeFamilies,
             FlexibleInstances, OverlappingInstances #-}

module Sample where

import Control.Monad
import Language.Haskell.TH

class Foo a where
    type FooType a

createInstance' :: Q Type -> Q Dec
createInstance' t = liftM head [d|
    instance Foo $t where
    type FooType $t = String |]

This fails with a similar error:

    Type indexes must match class instance head
    Found `t_aMn' but expected `t_aMl'

Here it's plain that we can't really do full type-checking of the quoted instance until createInstance' is applied, so that we can run the splice $t. Somehow TH needs to be less picky about the consistency checks on types when typechecking quotes.

The earlier example, though, should be fine. I'll look into it.


comment:3 Changed 10 years ago by Ashley Yakeley

Ah. My actual problem is closer to the email than to my example, because I am also splicing in the types.

comment:4 Changed 10 years ago by simonpj

OK. That will require a bit more thought. Use the workaround for now!


comment:5 Changed 10 years ago by LouisWasserman

Just a brief whine: I encountered this problem, and I'm not even sure it's possible to apply the workaround to my task. I'm trying to use TH to generate a large number of very straightforward class methods, but there's also a single associated type which is too complicated to derive (and hellish to pass to my TH method).

Personally, I'd settle in an instant for being able to splice in declarations at the class method level, which currently gives a parse error.

comment:6 Changed 9 years ago by simonpj

See #4230 for a more global discussion


comment:7 Changed 9 years ago by igloo

Blocked By: 4230 added

comment:8 Changed 9 years ago by igloo

Milestone: 6.16.1

comment:9 Changed 8 years ago by jwlato

Cc: jwlato@… added

comment:11 Changed 8 years ago by igloo

Priority: normallow

comment:12 Changed 8 years ago by simonpj@…

commit 6821789276554a7c8140cafdd65438a79017a638

Author: Simon Peyton Jones <>
Date:   Tue Mar 20 09:37:09 2012 +0000

    Fix scoping of type variables in DsMeta
    This fixes Trac #4135.  It's been wrong for a long time!

 compiler/deSugar/DsMeta.hs   |  110 ++++++++++++++++++-----------------------
 compiler/deSugar/DsMonad.lhs |    5 ++-
 2 files changed, 52 insertions(+), 63 deletions(-)

comment:13 Changed 7 years ago by igloo


comment:14 Changed 6 years ago by shelarcy

difficulty: Unknown
Test Case: th/T4135

Why is this still open? Above commit log says that commit fixes this bug, and test case is already added by ccd14ae24656440e4746a888d070b8922871408a/testsuite.

Last edited 6 years ago by shelarcy (previous) (diff)

comment:15 Changed 6 years ago by monoidal

shelarcy: The program from comment:2 still does not compile in HEAD.

comment:16 in reply to:  15 Changed 6 years ago by shelarcy

Replying to monoidal: I see. I missed that.

comment:17 Changed 6 years ago by Simon Peyton Jones <simonpj@…>

In 7a318d7d3a1467251b3133c43f01c7372c32904e/testsuite:

Test for Trac #4135, comment 2

comment:18 Changed 6 years ago by simonpj

Resolution: fixed
Status: newclosed
Test Case: th/T4135th/T4135, T4135a

Comment 2 works now, with the new Template Haskell story. I've added a new test T4135a.

Note: See TracTickets for help on using tickets.