I am using the Nat
type in from the GHC.TypeLits
module, which admittedly says the programmer interface should be defined in a separate library. In any case, the GHC.TypeLits
has a class KnownNat
with a class function natVal
which converts a compile time Nat
into a runtime Integer
. There's also a type function (+)
which adds compile time Nat
s.
The problem is that given (KnownNat n1, KnownNat n2)
, GHC can't derive that KnownNat (n1 + n2)
.
This results in an explosion of constraints needed to be added whenever I add type level naturals.
One alternative would be to define Natural numbers myself like so:
data Nat = Zero | Succ Nat
Or perhaps use a library like type-natural. But it seems silly to not use the Nats which are built into GHC, which also allow you to nicely use literal numbers in types (i.e. 0
, 1
) instead of having to define:
N0 = Zero
N1 = Succ N0
etc...
Is there anyway around this issue with GHC KnownNat
constraints being required all over the place? Or should I just ignore the GHC.TypeLits
module for my problem?