
#16213: Unnecessary error -------------------------------------+------------------------------------- Reporter: pjljvdlaar | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.3 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- I don't know whether this is a bug or a feature request.... When compiling {{{ {-# LANGUAGE DataKinds #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleInstances #-} module Main where data Term (b::Bool) where Const :: Int -> Term 'True Sum :: --Show (TermList v) => TermList v -> Term 'False instance Show (Term b) where show (Const a) = "(Const " ++ show a ++ ")" show (Sum a) = "(Sum " ++ show a ++ ")" data TermList (xs :: [ Bool ]) where TNil :: TermList '[] TCons :: Term x -> TermList xs -> TermList (x ': xs) instance Show (TermList '[]) where show TNil = "Nil" instance Show (TermList xs) => Show (TermList (x ': xs)) where show (TCons a b) = "(Cons " ++ show a ++ " " ++ show b ++ ")" main :: IO () main = do putStrLn "Hello world!" }}} one gets the error {{{ src\Main.hs:37:31: error: * Could not deduce (Show (TermList v)) arising from a use of `show' from the context: b ~ 'False bound by a pattern with constructor: Sum :: forall (v :: [Bool]). TermList v -> Term 'False, in an equation for `show' at src\Main.hs:37:11-15 * In the first argument of `(++)', namely `show a' In the second argument of `(++)', namely `show a ++ ")"' In the expression: "(Sum " ++ show a ++ ")" | 37 | show (Sum a) = "(Sum " ++ show a ++ ")" | ^^^^^^ }}} Yet, both patterns are matched, i.e. '[] and (x ': xs), so I was surprised the compiler could not figure this out! By out commenting 'Show (TermList v) => ' the code complies fine. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16213 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#16213: Unnecessary error -------------------------------------+------------------------------------- Reporter: pjljvdlaar | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by RyanGlScott): Replying to [ticket:16213 pjljvdlaar]:
Yet, both patterns are matched, i.e. '[] and (x ': xs), so I was surprised the compiler could not figure this out!
This isn't how type class instances work. Having an `Show (TermList '[])` instance and a `Show (TermList (x ': xs))` instance doesn't imply an instance for `forall (v :: [Bool]). Show (TermList v)`. In the instance you're trying to write, GHC needs a `Show` instance for an arbitrary list `v`, and GHC can't know //a priori// whether that list is `'[]` or `(_ ': _)`. That being said, there's not much sense in writing two separate `Show` instances like this. Why not just combine them into a single instance, like so? {{{#!hs instance Show (TermList v) where show TNil = "Nil" show (TCons a b) = "(Cons " ++ show a ++ " " ++ show b ++ ")" }}} This actually gives you an instance for `forall (v :: [Bool]). Show (TermList v)`, so you avoid your earlier issue entirely. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16213#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#16213: Unnecessary error -------------------------------------+------------------------------------- Reporter: pjljvdlaar | Owner: (none) Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.6.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by RyanGlScott): * status: new => closed * resolution: => invalid -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16213#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC