Ticket #4135 (new bug)

Opened 3 years ago

Last modified 8 months ago

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:
Test Case: 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

Changed 3 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.

Changed 3 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

Changed 3 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.

Changed 3 years ago by simonpj

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

Simon

Changed 3 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.

Changed 3 years ago by simonpj

See #4230 for a more global discussion

Simon

Changed 3 years ago by igloo

  • blockedby 4230 added

Changed 3 years ago by igloo

  • milestone set to 6.16.1

Changed 19 months ago by jwlato

  • cc jwlato@… added

Changed 16 months ago by igloo

  • priority changed from normal to low
  • milestone changed from 7.4.1 to 7.6.1

Changed 14 months 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(-)

Changed 8 months ago by igloo

  • milestone changed from 7.6.1 to 7.6.2
Note: See TracTickets for help on using tickets.