Nat (or KnownNat , I don't get distintion yet)
Nat is the type of natural numbers of the type. He has no residents at the timeline level. The idea is that the GHC promotes any natural number at the level level and gives it the form Nat .
KnownNat is a restriction on something like Nat , the implementation of which shows how to transform a thing of the form Nat into the terminological level of Integer . GHC automatically creates KnownNat instances for all type types of type type Nat 1 .
However, if every n :: Nat (reading type n type Nat ) has an instance of KnownNat on it, 1 you still need to write a restriction.
I found two ways to promote Integer in Nat
You really? At the end of Nat day today, the GHC is just magical. singletons enters into the same magic. Under the hood uses someNatVal .
So what is the “standard” way to push Integer to Nat ? Or what is the best approach to promotion using GHC.TypeLits and Proxy , or singleton?
There is no standard way. My trick: use singletons when you can afford its dependency and GHC.TypeLits otherwise. The advantage of singletons is that a class of type SingI allows SingI to conveniently conduct analysis based on induction, while relying on a special type of Nat GHC.
1 As noted in the comments, not every resident of type Nat has an instance of KnownNat . For example, Any Nat :: Nat , where Any is one of the GHC.Exts . Only residents 0 , 1 , 2 , ... have KnownNat instances.
source share