Opened 4 years ago

Closed 5 months 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 Difficulty: Unknown
Test Case: th/T4135, T4135a Blocked By: #4230
Blocking: Related Tickets:

Description

Give this a whirl:

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

    class C a where
        type T a

    $([d|  
        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-0.3.0.0 ... linking ... done.
Loading package containers-0.3.0.0 ... linking ... done.
Loading package pretty-1.0.1.1 ... linking ... done.
Loading package template-haskell ... linking ... done.

Bug.hs:7:6:
    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])'
[glastonbury:~/Projects/Haskell/Truth/Core]$ 

Change History (18)

comment:1 Changed 4 years ago by Ashley Yakeley

Bit someone else, too:
http://haskell.org/pipermail/glasgow-haskell-users/2010-January/018291.html

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

comment:2 Changed 4 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:

Sample.lhs:22:10:
    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.

Simon

comment:3 Changed 4 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 4 years ago by simonpj

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

Simon

comment:5 Changed 4 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 4 years ago by simonpj

See #4230 for a more global discussion

Simon

comment:7 Changed 4 years ago by igloo

  • Blocked By 4230 added

comment:8 Changed 4 years ago by igloo

  • Milestone set to 6.16.1

comment:9 Changed 2 years ago by jwlato

  • Cc jwlato@… added

comment:11 Changed 2 years ago by igloo

  • Milestone changed from 7.4.1 to 7.6.1
  • Priority changed from normal to low

comment:12 Changed 2 years ago by simonpj@…

commit 6821789276554a7c8140cafdd65438a79017a638

Author: Simon Peyton Jones <simonpj@microsoft.com>
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 19 months ago by igloo

  • Milestone changed from 7.6.1 to 7.6.2

comment:14 Changed 6 months ago by shelarcy

  • Difficulty set to Unknown
  • Test Case set to 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 months ago by shelarcy (previous) (diff)

comment:15 follow-up: Changed 6 months ago by monoidal

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

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

Replying to monoidal:
I see. I missed that.

comment:17 Changed 5 months ago by Simon Peyton Jones <simonpj@…>

In 7a318d7d3a1467251b3133c43f01c7372c32904e/testsuite:

Test for Trac #4135, comment 2

comment:18 Changed 5 months ago by simonpj

  • Resolution set to fixed
  • Status changed from new to closed
  • Test Case changed from th/T4135 to th/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.