Avoiding class constraints at the level of naturals

I am using a type Natfrom a module GHC.TypeLits, which admittedly indicates that the programmer interface should be defined in a separate library. In any case, it GHC.TypeLitshas a class KnownNatwith a class function natValthat converts compile Nattime at runtime Integer. There is also a type function (+)that adds Nats compilation time .

The problem is that given the (KnownNat n1, KnownNat n2)GHC cannot get that KnownNat (n1 + n2).

This leads to an explosion of restrictions that need to be added when I add naturals level.

One option would be to define natural numbers like this:

data Nat = Zero | Succ Nat

Or perhaps use a library like type-natural . But it seems foolish not to use Nats that are built into the GHC, which also allows you to nicely use literal numbers in types (i.e. 0, 1) instead of defining:

N0 = Zero
N1 = Succ N0
etc...

Is there a problem with the GHC restrictions KnownNatthat are required everywhere? Or should I just ignore the module GHC.TypeLitsfor my problem?

+4
source share
1 answer

This plugin for checking the GHC type does exactly this (it removes the "complex" KnownNatrestrictions from other ones already available): https://hackage.haskell.org/package/ghc-typelits-knownnat

" " ( ), . ( ), , :

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

( <) LANGUAGE) .

, typelit Nats: https://hackage.haskell.org/package/ghc-typelits-natnormalise. Nat, GHC : , n + (m + 1) ~ (n + 1) + m, , GHC "" "" .

+9

Source: https://habr.com/ru/post/1666330/


All Articles