Skip to content

Misleading solverWanteds: too many iterations error message #68

@kleinreact

Description

@kleinreact

The following reproducer

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE DeriveAnyClass #-}

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

module Test where

import Clash.Prelude

type family Go xs where
  Go (_ : xr) = Go xr
  Go '[]      = 0

type Elements a = 1 + Go a

data RIndex a = RIndex (Index (Elements a))
  deriving (Generic)

deriving instance
  ( KnownNat (Elements a)
  ) =>
  BitPack (RIndex a)

causes some

    solveWanteds: too many iterations (limit = 4)
      Unsolved: WC {wc_simple =
                      [W] $dKnownNat_ajqO {0}:: KnownNat (Go a1) (CNonCanonical)}
      Simples: {[W] $dKnownNat_ajoy {0}:: KnownNat
                                            (BitSize (RIndex a1)) (CNonCanonical)}
    Suggested fix:
      Set limit with -fconstraint-solver-iterations=n; n=0 for no limit
  |
1 | {-# LANGUAGE DataKinds #-}
  | ^

error, which is misleading in order to figure out the actual problem. Note that the iteration limit is not the problem here. Disabling just the KnownNat plugin reveals

Test.hs:22:1: error: [GHC-39999]
    • Could not deduce ‘KnownNat (CLog 2 (1 + Go a2))’
        arising from the superclasses of an instance declaration
      from the context: KnownNat (Elements a2)
        bound by the instance declaration at Test.hs:(22,1)-(26,20)
      Possible fix:
        If the constraint looks soluble from a superclass of the instance context,
        read 'Undecidable instances and loopy superclasses' in the user manual
    • In the instance declaration for ‘BitPack (RIndex a1)’
   |
22 | deriving instance
   | ^^^^^^^^^^^^^^^^^...

Test.hs:22:1: error: [GHC-39999]
    • Could not deduce ‘KnownNat (Go a2)’
        arising from a use of ‘Clash.Class.BitPack.Internal.$dmpack’
      from the context: KnownNat (Elements a2)
        bound by the instance declaration at Test.hs:(22,1)-(26,20)
    • In the expression:
        Clash.Class.BitPack.Internal.$dmpack @(RIndex a1)
      In an equation for ‘pack’:
          pack = Clash.Class.BitPack.Internal.$dmpack @(RIndex a1)
      When typechecking the code for ‘pack’
        in a derived instance for ‘BitPack (RIndex a)’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘BitPack (RIndex a1)’
   |
22 | deriving instance
   | ^^^^^^^^^^^^^^^^^...

Test.hs:22:1: error: [GHC-39999]
    • Could not deduce ‘KnownNat (Go a2)’
        arising from a use of ‘Clash.Class.BitPack.Internal.$dmunpack’
      from the context: KnownNat (Elements a2)
        bound by the instance declaration at Test.hs:(22,1)-(26,20)
    • In the expression:
        Clash.Class.BitPack.Internal.$dmunpack @(RIndex a1)
      In an equation for ‘unpack’:
          unpack = Clash.Class.BitPack.Internal.$dmunpack @(RIndex a1)
      When typechecking the code for ‘unpack’
        in a derived instance for ‘BitPack (RIndex a)’:
        To see the code I am typechecking, use -ddump-deriv
      In the instance declaration for ‘BitPack (RIndex a1)’
   |
22 | deriving instance
   | ^^^^^^^^^^^^^^^^^...

which makes it immediately clear that there is just a KnownNat (Go a) constraint missing, e.g., with the KnownNat plugin enabled and

deriving instance
  ( KnownNat (Elements a)
  , KnownNat (Go a)
  ) =>
  BitPack (RIndex a)

it typechecks.

It would be much more useful if the plugins still could print the original type errors in that case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions