Confluent type family instances confuse the solver
While attempting to define integer arithmetic using type families, I encountered a problem with the type checker failing to recognize equivalent types:
{-# LANGUAGE EmptyDataDecls, TypeFamilies #-}
module WithBug where
data Z
data S a
data P a b
type family N a
type instance N (P Z a) = P Z a
type instance N (P a Z) = P a Z -- with bug
type instance N (P (S a) (S b)) = N (P a b)
$ ghci WithBug
*WithBug> :t (undefined :: N (P Z Z)) :: P Z Z
<interactive>:1:1:
Couldn't match expected type `P Z Z'
against inferred type `N (P Z Z)'
In the expression: (undefined :: N (P Z Z)) :: P Z Z
Rewriting the problematic instance fixed the problem:
{-# LANGUAGE EmptyDataDecls, TypeFamilies #-}
module WithoutBug where
data Z
data S a
data P a b
type family N a
type instance N (P Z a) = P Z a
type instance N (P (S a) Z) = P (S a) Z -- without bug
type instance N (P (S a) (S b)) = N (P a b)
$ ghci WithoutBug
*WithoutBug> :t (undefined :: N (P Z Z)) :: P Z Z
(undefined :: N (P Z Z)) :: P Z Z :: P Z Z
Trac metadata
Trac field | Value |
---|---|
Version | 6.10.3 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler (Type checker) |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |