Opened 2 years ago

Closed 2 years ago

#14260 closed bug (invalid)

Type family in instance signature confuses GHC

Reported by: Iceland_jack Owned by:
Priority: normal Milestone:
Component: Compiler Version: 8.2.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

This works:

class F a where
  type T a
  u :: T a -> a -> a

newtype W a = W a

instance F (W a) where
  type T (W a) = T a
  u = undefined 

instance F () where
  type T () = T (W ())
  u _ = undefined 

Remove one argument to u and you get

     Reduction stack overflow; size = 201
      When simplifying the following type: T (W ())
      Use -freduction-depth=0 to disable this check
      (any upper bound you could choose might fail unpredictably with
       minor updates to GHC, so disabling the check is recommended if
       you're sure that type checking should terminate)
     In the expression: undefined
      In an equation for u: u = undefined
      In the instance declaration for F ()

It is also not accepted to

  u :: T (W ()) -> () -> ()
  u _ = id @()

Change History (2)

comment:1 Changed 2 years ago by RyanGlScott

You almost pulled the wool over my eyes and made me think that GHC was infinitely looping without provocation. But then I realized that you conveniently left off some important language extensions needed to compile this code :)

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}

The fact that you have to enable UndecidableInstances should be an important clue as to what's happening. You have these two type family instances:

type T () = T (W ())
type T (W a) = T a

So if you have u :: T (W ()) -> () -> (); u = undefined, GHC attempts to generalize the type T (W ()) -> () -> (), and thus reduce T (W ()). But that means:

   T (W ())
-> T ()
-> T (W ())
-> ...

Bam. Stack overflow. So I claim there is no bug here, only reckless use of UndecidableInstances and type families.

comment:2 Changed 2 years ago by RyanGlScott

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.