module Haskus.Utils.Types
( Nat
, Symbol
, natValue
, natValue'
, symbolValue
, KnownNat
, KnownSymbol
, CmpNat
, CmpSymbol
, type (<=?)
, type (<=)
, type (+)
, type ()
, type (*)
, type (^)
, Assert
, If
, Modulo
, Same
, Proxy (..)
, TypeError
, ErrorMessage (..)
)
where
import GHC.TypeLits
import Data.Proxy
natValue :: forall (n :: Nat) a. (KnownNat n, Num a) => a
natValue = fromIntegral (natVal (Proxy :: Proxy n))
natValue' :: forall (n :: Nat). KnownNat n => Word
natValue' = natValue @n
symbolValue :: forall (s :: Symbol). (KnownSymbol s) => String
symbolValue = symbolVal (Proxy :: Proxy s)
type family If (c :: Bool) (t :: k) (e :: k) where
If 'True t e = t
If 'False t e = e
type family Assert (prop :: Bool) (val :: k) (msg :: ErrorMessage) where
Assert 'True val msg = val
Assert 'False val msg = TypeError msg
type family Modulo (a :: Nat) (b :: Nat) where
Modulo a b = Modulo' (a <=? b) a b
type family Modulo' c a b where
Modulo' 'True a b = a
Modulo' 'False a b = Modulo' ((ab) <=? b) (ab) b
type family Same a b :: Nat where
Same a a = 1
Same a b = 0