Opened 4 years ago

Closed 4 years ago

Last modified 20 months ago

#10634 closed bug (duplicate)

Type class with injective type functions

Reported by: Lemming Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.10.1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Test Case:
Blocked By: Blocking:
Related Tickets: #10009, #6018 Differential Rev(s):
Wiki Page:


See the attached module.

$ cat TypeFunctionBijection.hs
{-# LANGUAGE TypeFamilies #-}
module TypeFunctionBijection where

import Data.Int (Int8, Int16, Int32)

type family Up a
type instance Up Int8  = Int16
type instance Up Int16 = Int32

class (Up (Down a) ~ a) => Convert a where
   type Down a
   down :: a -> Down a

instance Convert Int16 where
   type Down Int16 = Int8
   down = fromIntegral

instance Convert Int32 where
   type Down Int32 = Int16
   down = fromIntegral

x :: Int8
x = down 8
$ ghci-7.8.4 -Wall TypeFunctionBijection.hs
GHCi, version 7.8.4:  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling TypeFunctionBijection ( TypeFunctionBijection.hs, interpreted )
Ok, modules loaded: TypeFunctionBijection.
*TypeFunctionBijection> :q
Leaving GHCi.
$ ghci-7.10.1 -Wall TypeFunctionBijection.hs
GHCi, version 7.10.1:  :? for help
[1 of 1] Compiling TypeFunctionBijection ( TypeFunctionBijection.hs, interpreted )

    Couldn't match expected type ‘Int8’ with actual type ‘Down a0’
    The type variable ‘a0’ is ambiguous
    In the expression: down 8
    In an equation for ‘x’: x = down 8
Failed, modules loaded: none.
Prelude> :q
Leaving GHCi.

Up to GHC-7.8.4 I could make a type function like Down a bijection by adding equality constraints to the Convert class. In GHC-7.10.1 this fails. Is this a bug or a feature?

Attachments (1)

TypeFunctionBijection.hs (447 bytes) - added by Lemming 4 years ago.

Download all attachments as: .zip

Change History (5)

Changed 4 years ago by Lemming

Attachment: TypeFunctionBijection.hs added

comment:1 Changed 4 years ago by jstolarek

Resolution: duplicate
Status: newclosed

It's a bug. See #10009.

As a side note: I don't think it is correct to call this a bijection. Bijection means that we have injectivity (which is true) and surjectivity (which is not true).

comment:2 Changed 4 years ago by Lemming

Summary: Type class with bijective type functionsType class with injective type functions

comment:3 Changed 4 years ago by Simon Peyton Jones <simonpj@…>

comment:4 Changed 20 months ago by Lemming

Note: See TracTickets for help on using tickets.