Skip to content

Defaulting bug or unfortunate error message with closed type families

https://github.com/nh2/infinite-type-families/blob/master/Test2.hs

Some code extracted from hmatrix:

{-# LANGUAGE CPP #-}

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}

module Test where


data Vector a = Vector a
data Matrix a = Matrix a

class Build f where
    build' :: BoundsOf f -> f -> ContainerOf f



#if __GLASGOW_HASKELL__ <= 706
-- Normal type families with GHC 7.6, works well.

type family BoundsOf x
type instance BoundsOf (a->a)    = Int
type instance BoundsOf (a->a->a) = (Int,Int)

type family ContainerOf x
type instance ContainerOf (a->a) = Vector a
type instance ContainerOf (a->a->a) = Matrix a

#else
-- Closed type families for GHC 7.8

type family BoundsOf x where
    BoundsOf (a->a->a) = (Int,Int)
    BoundsOf (a->a)    = Int

type family ContainerOf x where
    ContainerOf (a->a)    = Vector a
    ContainerOf (a->a->a) = Matrix a

#endif


instance (Num a) => Build (a->a) where
    build' = buildV


buildV :: (Integral a, Num b) => a -> (b -> c) -> Vector c
buildV _ _ = undefined

This is probably related to #8154 (closed).

With GHC 7.7, we get the following error:

Test2.hs:59:14:               
    Could not deduce (BoundsOf (a -> a) ~ Integer)
    from the context (Num a)
      bound by the instance declaration at Test2.hs:58:10-32
    Expected type: BoundsOf (a -> a)
                   -> (a -> a) -> ContainerOf (a -> a)
      Actual type: Integer -> (a -> a) -> Vector a
    Relevant bindings include
      build' :: BoundsOf (a -> a) -> (a -> a) -> ContainerOf (a -> a)
        (bound at Test2.hs:59:5)
    In the expression: buildV
    In an equation for ‛build'’: build' = buildV
  1. Where does this Integer come from?

  2. Change

BoundsOf (a->a) = Int

into

BoundsOf (a->a) = Integer

You get the same error message, although clearly BoundsOf (a -> a) ~ Integer (you just wrote that down).[[BR]] Even if the order matters in closed type families, is this not the wrong error message?

  1. If you flip the order so that the (a->a) case comes first, it works (with both it Int or Integer). I don't quite understand why.
Trac metadata
Trac field Value
Version 7.6.3
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC mail@nh2.me
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information