clash-lang / ghc-typelits-knownnat

Derive KnownNat constraints from other KnownNat constraints
Other
14 stars 10 forks source link

Could not deduce (Typeable (n + 1)) with GHC-8.4.3 #21

Open chshersh opened 6 years ago

chshersh commented 6 years ago

I'm using ghc-typelits-knownnat-0.5. And I have the following code:

{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators       #-}

{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}

module Nats where

import Data.Proxy (Proxy (..))
import Data.Typeable (Typeable)
import GHC.TypeLits (KnownNat, type (+))

f :: forall n . (Typeable n, KnownNat n) => Proxy n -> [()] -> ()
f _ []     = ()
f _ (_:xs) = f (Proxy :: Proxy (n + 1)) xs

This code produces the following error when compiling with GHC-8.4.3:

    • Could not deduce (Typeable (n + 1)) arising from a use of ‘f’
      from the context: (Typeable n, KnownNat n)
        bound by the type signature for:
                   f :: forall (n :: ghc-prim-0.5.2.0:GHC.Types.Nat).
                        (Typeable n, KnownNat n) =>
                        Proxy n -> [()] -> ()
        at src/Nats.hs:13:1-65
    • In the expression: f (Proxy :: Proxy (n + 1)) xs
      In an equation for ‘f’:
          f _ (_ : xs) = f (Proxy :: Proxy (n + 1)) xs
   |
15 | f _ (_:xs) = f (Proxy :: Proxy (n + 1)) xs
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

However, it compiles and works completely okay with GHC-8.2.2 and GHC-8.0.2. What should I do to make this code work with latest GHC version?

christiaanb commented 6 years ago

This seems more of a GHC issue than a ghc-typelits-knownnat issue, because for both GHC 8.2 and GHC 8.4 ghc-typelits-knownnat solves the KnownNat (n+1) constraint from the KnownNat n constraint. The difference seems to be that in GHC 8.2 and earlier, GHC used the KnownNat (n+1) constraint solved by ghc-typelits-knownnat to solve the Typeable (n+1) constraint, whereas in GHC 8.4 no longer does this.

chshersh commented 6 years ago

@christiaanb Thanks for quick response! I discovered that there already was similar GHC issue but it's fixed:

Looks like it become broken again... I will open new issue in GHC.

UPDATE: Here is the issue: