{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE QuantifiedConstraints #-}
module GHC.Internal.TypeNats
(
Natural
, Nat
, KnownNat(natSing), natVal, natVal'
, SomeNat(..)
, someNatVal
, sameNat
, decideNat
, SNat (UnsafeSNat)
, pattern SNat
, fromSNat
, withSomeSNat
, withKnownNat
, unsafeWithSNatCo
, type (<=), type (<=?), type (+), type (*), type (^), type (-)
, CmpNat
, cmpNat
, Div, Mod, Log2
) where
import GHC.Internal.Base( Eq(..), Functor(..), Ord(..), WithDict(..), (.), otherwise
, Void, errorWithoutStackTrace, (++))
import GHC.Types
import GHC.Internal.Bignum.Natural(Natural)
import GHC.Internal.Show(Show(..), appPrec, appPrec1, showParen, showString)
import GHC.Internal.Read(Read(..))
import GHC.Prim(Proxy#)
import GHC.Internal.Data.Either(Either(..))
import GHC.Internal.Data.Maybe(Maybe(..))
import GHC.Internal.Data.Proxy (Proxy(..))
import GHC.Internal.Data.Type.Coercion (Coercion(..), TestCoercion(..))
import GHC.Internal.Data.Type.Equality((:~:)(Refl), TestEquality(..))
import GHC.Internal.Data.Type.Ord(OrderingI(..), type (<=), type (<=?))
import GHC.Internal.Unsafe.Coerce(unsafeCoerce)
import GHC.Internal.TypeNats.Internal(CmpNat)
type Nat = Natural
class KnownNat (n :: Nat) where
natSing :: SNat n
natVal :: forall n proxy. KnownNat n => proxy n -> Natural
natVal :: forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal proxy n
_ = case SNat n
forall (n :: Nat). KnownNat n => SNat n
natSing :: SNat n of
UnsafeSNat Nat
x -> Nat
x
natVal' :: forall n. KnownNat n => Proxy# n -> Natural
natVal' :: forall (n :: Nat). KnownNat n => Proxy# n -> Nat
natVal' Proxy# n
_ = case SNat n
forall (n :: Nat). KnownNat n => SNat n
natSing :: SNat n of
UnsafeSNat Nat
x -> Nat
x
data SomeNat = forall n. KnownNat n => SomeNat (Proxy n)
someNatVal :: Natural -> SomeNat
someNatVal :: Nat -> SomeNat
someNatVal Nat
n = Nat -> (forall (n :: Nat). SNat n -> SomeNat) -> SomeNat
forall r. Nat -> (forall (n :: Nat). SNat n -> r) -> r
withSomeSNat Nat
n (\(SNat n
sn :: SNat n) ->
SNat n -> (KnownNat n => SomeNat) -> SomeNat
forall (n :: Nat) r. SNat n -> (KnownNat n => r) -> r
withKnownNat SNat n
sn (forall (n :: Nat). KnownNat n => Proxy n -> SomeNat
SomeNat @n Proxy n
forall {k} (t :: k). Proxy t
Proxy))
instance Eq SomeNat where
SomeNat Proxy n
x == :: SomeNat -> SomeNat -> Bool
== SomeNat Proxy n
y = Proxy n -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal Proxy n
x Nat -> Nat -> Bool
forall a. Eq a => a -> a -> Bool
== Proxy n -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal Proxy n
y
instance Ord SomeNat where
compare :: SomeNat -> SomeNat -> Ordering
compare (SomeNat Proxy n
x) (SomeNat Proxy n
y) = Nat -> Nat -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Proxy n -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal Proxy n
x) (Proxy n -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal Proxy n
y)
instance Show SomeNat where
showsPrec :: Int -> SomeNat -> ShowS
showsPrec Int
p (SomeNat Proxy n
x) = Int -> Nat -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p (Proxy n -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal Proxy n
x)
instance Read SomeNat where
readsPrec :: Int -> ReadS SomeNat
readsPrec Int
p String
xs = do (a,ys) <- Int -> ReadS Nat
forall a. Read a => Int -> ReadS a
readsPrec Int
p String
xs
[(someNatVal a, ys)]
infixl 6 +, -
infixl 7 *, `Div`, `Mod`
infixr 8 ^
type family (m :: Nat) + (n :: Nat) :: Nat
type family (m :: Nat) * (n :: Nat) :: Nat
type family (m :: Nat) ^ (n :: Nat) :: Nat
type family (m :: Nat) - (n :: Nat) :: Nat
type family Div (m :: Nat) (n :: Nat) :: Nat
type family Mod (m :: Nat) (n :: Nat) :: Nat
type family Log2 (m :: Nat) :: Nat
sameNat :: forall a b proxy1 proxy2.
(KnownNat a, KnownNat b) =>
proxy1 a -> proxy2 b -> Maybe (a :~: b)
sameNat :: forall (a :: Nat) (b :: Nat) (proxy1 :: Nat -> Type)
(proxy2 :: Nat -> Type).
(KnownNat a, KnownNat b) =>
proxy1 a -> proxy2 b -> Maybe (a :~: b)
sameNat proxy1 a
_ proxy2 b
_ = SNat a -> SNat b -> Maybe (a :~: b)
forall (a :: Nat) (b :: Nat). SNat a -> SNat b -> Maybe (a :~: b)
forall {k} (f :: k -> Type) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality (forall (n :: Nat). KnownNat n => SNat n
natSing @a) (forall (n :: Nat). KnownNat n => SNat n
natSing @b)
decideNat :: forall a b proxy1 proxy2.
(KnownNat a, KnownNat b) =>
proxy1 a -> proxy2 b -> Either (a :~: b -> Void) (a :~: b)
decideNat :: forall (a :: Nat) (b :: Nat) (proxy1 :: Nat -> Type)
(proxy2 :: Nat -> Type).
(KnownNat a, KnownNat b) =>
proxy1 a -> proxy2 b -> Either ((a :~: b) -> Void) (a :~: b)
decideNat proxy1 a
_ proxy2 b
_ = SNat a -> SNat b -> Either ((a :~: b) -> Void) (a :~: b)
forall (a :: Nat) (b :: Nat).
SNat a -> SNat b -> Either ((a :~: b) -> Void) (a :~: b)
decNat (forall (n :: Nat). KnownNat n => SNat n
natSing @a) (forall (n :: Nat). KnownNat n => SNat n
natSing @b)
decNat :: SNat a -> SNat b -> Either (a :~: b -> Void) (a :~: b)
decNat :: forall (a :: Nat) (b :: Nat).
SNat a -> SNat b -> Either ((a :~: b) -> Void) (a :~: b)
decNat (UnsafeSNat Nat
x) (UnsafeSNat Nat
y)
| Nat
x Nat -> Nat -> Bool
forall a. Eq a => a -> a -> Bool
== Nat
y = (a :~: b) -> Either ((a :~: b) -> Void) (a :~: b)
forall a b. b -> Either a b
Right ((ZonkAny 2 :~: ZonkAny 2) -> a :~: b
forall a b. a -> b
unsafeCoerce ZonkAny 2 :~: ZonkAny 2
forall {k} (a :: k). a :~: a
Refl)
| Bool
otherwise = ((a :~: b) -> Void) -> Either ((a :~: b) -> Void) (a :~: b)
forall a b. a -> Either a b
Left (\a :~: b
Refl -> String -> Void
forall a. String -> a
errorWithoutStackTrace (String
"decideNat: Impossible equality proof " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Nat -> String
forall a. Show a => a -> String
show Nat
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" :~: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Nat -> String
forall a. Show a => a -> String
show Nat
y))
cmpNat :: forall a b proxy1 proxy2. (KnownNat a, KnownNat b)
=> proxy1 a -> proxy2 b -> OrderingI a b
cmpNat :: forall (a :: Nat) (b :: Nat) (proxy1 :: Nat -> Type)
(proxy2 :: Nat -> Type).
(KnownNat a, KnownNat b) =>
proxy1 a -> proxy2 b -> OrderingI a b
cmpNat proxy1 a
x proxy2 b
y = case Nat -> Nat -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (proxy1 a -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal proxy1 a
x) (proxy2 b -> Nat
forall (n :: Nat) (proxy :: Nat -> Type).
KnownNat n =>
proxy n -> Nat
natVal proxy2 b
y) of
Ordering
EQ -> case (ZonkAny 4 :~: ZonkAny 4, ZonkAny 6 :~: ZonkAny 6)
-> (CmpNat a b :~: 'EQ, a :~: b)
forall a b. a -> b
unsafeCoerce (ZonkAny 4 :~: ZonkAny 4
forall {k} (a :: k). a :~: a
Refl, ZonkAny 6 :~: ZonkAny 6
forall {k} (a :: k). a :~: a
Refl) :: (CmpNat a b :~: 'EQ, a :~: b) of
(CmpNat a b :~: 'EQ
Refl, a :~: b
Refl) -> OrderingI a a
OrderingI a b
forall {k} (a :: k). (Compare a a ~ 'EQ) => OrderingI a a
EQI
Ordering
LT -> case (ZonkAny 8 :~: ZonkAny 8) -> CmpNat a b :~: 'LT
forall a b. a -> b
unsafeCoerce ZonkAny 8 :~: ZonkAny 8
forall {k} (a :: k). a :~: a
Refl :: (CmpNat a b :~: 'LT) of
CmpNat a b :~: 'LT
Refl -> OrderingI a b
forall {k} (a :: k) (b :: k). (Compare a b ~ 'LT) => OrderingI a b
LTI
Ordering
GT -> case (ZonkAny 10 :~: ZonkAny 10) -> CmpNat a b :~: 'GT
forall a b. a -> b
unsafeCoerce ZonkAny 10 :~: ZonkAny 10
forall {k} (a :: k). a :~: a
Refl :: (CmpNat a b :~: 'GT) of
CmpNat a b :~: 'GT
Refl -> OrderingI a b
forall {k} (a :: k) (b :: k). (Compare a b ~ 'GT) => OrderingI a b
GTI
newtype SNat (n :: Nat) = UnsafeSNat_ Natural
type role SNat nominal
pattern SNat :: forall n. () => KnownNat n => SNat n
pattern $mSNat :: forall {r} {n :: Nat}.
SNat n -> (KnownNat n => r) -> ((# #) -> r) -> r
$bSNat :: forall (n :: Nat). KnownNat n => SNat n
SNat <- (knownNatInstance -> KnownNatInstance)
where SNat = SNat n
forall (n :: Nat). KnownNat n => SNat n
natSing
{-# COMPLETE SNat #-}
data KnownNatInstance (n :: Nat) where
KnownNatInstance :: KnownNat n => KnownNatInstance n
knownNatInstance :: SNat n -> KnownNatInstance n
knownNatInstance :: forall (n :: Nat). SNat n -> KnownNatInstance n
knownNatInstance SNat n
sn = SNat n -> (KnownNat n => KnownNatInstance n) -> KnownNatInstance n
forall (n :: Nat) r. SNat n -> (KnownNat n => r) -> r
withKnownNat SNat n
sn KnownNatInstance n
KnownNat n => KnownNatInstance n
forall (n :: Nat). KnownNat n => KnownNatInstance n
KnownNatInstance
pattern UnsafeSNat :: forall n. Natural -> SNat n
pattern $mUnsafeSNat :: forall {r} {n :: Nat}. SNat n -> (Nat -> r) -> ((# #) -> r) -> r
$bUnsafeSNat :: forall (n :: Nat). Nat -> SNat n
UnsafeSNat guts = UnsafeSNat_ guts
{-# COMPLETE UnsafeSNat #-}
unsafeWithSNatCo
:: forall r. ((forall n. Coercible (SNat n) Natural) => r) -> r
unsafeWithSNatCo :: forall r. ((forall (n :: Nat). Coercible (SNat n) Nat) => r) -> r
unsafeWithSNatCo (forall (n :: Nat). Coercible (SNat n) Nat) => r
v = r
(forall (n :: Nat). Coercible (SNat n) Nat) => r
v
instance Eq (SNat n) where
SNat n
_ == :: SNat n -> SNat n -> Bool
== SNat n
_ = Bool
True
instance Ord (SNat n) where
compare :: SNat n -> SNat n -> Ordering
compare SNat n
_ SNat n
_ = Ordering
EQ
instance Show (SNat n) where
showsPrec :: Int -> SNat n -> ShowS
showsPrec Int
p (UnsafeSNat Nat
n)
= Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
appPrec)
( String -> ShowS
showString String
"SNat @"
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Nat -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
appPrec1 Nat
n
)
instance TestEquality SNat where
testEquality :: forall (a :: Nat) (b :: Nat). SNat a -> SNat b -> Maybe (a :~: b)
testEquality SNat a
a SNat b
b = case SNat a -> SNat b -> Either ((a :~: b) -> Void) (a :~: b)
forall (a :: Nat) (b :: Nat).
SNat a -> SNat b -> Either ((a :~: b) -> Void) (a :~: b)
decNat SNat a
a SNat b
b of
Right a :~: b
x -> (a :~: b) -> Maybe (a :~: b)
forall a. a -> Maybe a
Just a :~: b
x
Left (a :~: b) -> Void
_ -> Maybe (a :~: b)
forall a. Maybe a
Nothing
instance TestCoercion SNat where
testCoercion :: forall (a :: Nat) (b :: Nat).
SNat a -> SNat b -> Maybe (Coercion a b)
testCoercion SNat a
x SNat b
y = ((a :~: b) -> Coercion a b)
-> Maybe (a :~: b) -> Maybe (Coercion a b)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a :~: b
Refl -> Coercion a b
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion) (SNat a -> SNat b -> Maybe (a :~: b)
forall (a :: Nat) (b :: Nat). SNat a -> SNat b -> Maybe (a :~: b)
forall {k} (f :: k -> Type) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality SNat a
x SNat b
y)
fromSNat :: SNat n -> Natural
fromSNat :: forall (n :: Nat). SNat n -> Nat
fromSNat (UnsafeSNat Nat
n) = Nat
n
withKnownNat :: forall n rep (r :: TYPE rep).
SNat n -> (KnownNat n => r) -> r
withKnownNat :: forall (n :: Nat) r. SNat n -> (KnownNat n => r) -> r
withKnownNat = forall (cls :: Constraint) meth r.
WithDict cls meth =>
meth -> (cls => r) -> r
withDict @(KnownNat n)
withSomeSNat :: forall rep (r :: TYPE rep).
Natural -> (forall n. SNat n -> r) -> r
withSomeSNat :: forall r. Nat -> (forall (n :: Nat). SNat n -> r) -> r
withSomeSNat Nat
n forall (n :: Nat). SNat n -> r
k = SNat (ZonkAny 0) -> r
forall (n :: Nat). SNat n -> r
k (Nat -> SNat (ZonkAny 0)
forall (n :: Nat). Nat -> SNat n
UnsafeSNat Nat
n)
{-# NOINLINE withSomeSNat #-}