    {-|
    Copyright        : (c) Galois, Inc 2014-2018
    Maintainer       : Joe Hendrix <jhendrix@galois.com>

    This internal module exports the 'NatRepr' type and its constructor. It is intended
    for use only within parameterized-utils, and is excluded from the module export list.
    -}
    {-# LANGUAGE DataKinds #-}
    {-# LANGUAGE DeriveDataTypeable #-}
    {-# LANGUAGE FlexibleInstances #-}
    {-# LANGUAGE GADTs #-}
    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    {-# LANGUAGE KindSignatures #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE PolyKinds #-}
    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE RoleAnnotations #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE TypeApplications #-}
    {-# LANGUAGE TypeOperators #-}

    module Data.Parameterized.NatRepr.Internal where

    import Data.Data
    import Data.Hashable
    import GHC.TypeNats
    import qualified Numeric.Natural as Natural
    import Unsafe.Coerce

    import Data.Parameterized.Axiom
    import Data.Parameterized.Classes
    import Data.Parameterized.DecidableEq

    ------------------------------------------------------------------------
    -- Nat

    -- | A runtime presentation of a type-level 'Nat'.
    --
    -- This can be used for performing dynamic checks on a type-level natural
    -- numbers.
    newtype NatRepr (n::Nat) = NatRepr { forall (n :: Nat). NatRepr n -> Nat
    natValue :: Natural.Natural
                                         -- ^ The underlying natural value of the number.
                                       }
      deriving (Eq (NatRepr n)
    Eq (NatRepr n) =>
    (Int -> NatRepr n -> Int)
    -> (NatRepr n -> Int) -> Hashable (NatRepr n)
    Int -> NatRepr n -> Int
    NatRepr n -> Int
    forall (n :: Nat). Eq (NatRepr n)
    forall (n :: Nat). Int -> NatRepr n -> Int
    forall (n :: Nat). NatRepr n -> Int
    forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
    $chashWithSalt :: forall (n :: Nat). Int -> NatRepr n -> Int
    hashWithSalt :: Int -> NatRepr n -> Int
    $chash :: forall (n :: Nat). NatRepr n -> Int
    hash :: NatRepr n -> Int
    Hashable, Typeable (NatRepr n)
    Typeable (NatRepr n) =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> NatRepr n -> c (NatRepr n))
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c (NatRepr n))
    -> (NatRepr n -> Constr)
    -> (NatRepr n -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c (NatRepr n)))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e))
        -> Maybe (c (NatRepr n)))
    -> ((forall b. Data b => b -> b) -> NatRepr n -> NatRepr n)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r)
    -> (forall u. (forall d. Data d => d -> u) -> NatRepr n -> [u])
    -> (forall u.
        Int -> (forall d. Data d => d -> u) -> NatRepr n -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n))
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n))
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n))
    -> Data (NatRepr n)
    NatRepr n -> Constr
    NatRepr n -> DataType
    (forall b. Data b => b -> b) -> NatRepr n -> NatRepr n
    forall (n :: Nat). KnownNat n => Typeable (NatRepr n)
    forall (n :: Nat). KnownNat n => NatRepr n -> Constr
    forall (n :: Nat). KnownNat n => NatRepr n -> DataType
    forall (n :: Nat).
    KnownNat n =>
    (forall b. Data b => b -> b) -> NatRepr n -> NatRepr n
    forall (n :: Nat) u.
    KnownNat n =>
    Int -> (forall d. Data d => d -> u) -> NatRepr n -> u
    forall (n :: Nat) u.
    KnownNat n =>
    (forall d. Data d => d -> u) -> NatRepr n -> [u]
    forall (n :: Nat) r r'.
    KnownNat n =>
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    forall (n :: Nat) r r'.
    KnownNat n =>
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    forall (n :: Nat) (m :: * -> *).
    (KnownNat n, Monad m) =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    forall (n :: Nat) (m :: * -> *).
    (KnownNat n, MonadPlus m) =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    forall (n :: Nat) (c :: * -> *).
    KnownNat n =>
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (NatRepr n)
    forall (n :: Nat) (c :: * -> *).
    KnownNat n =>
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> NatRepr n -> c (NatRepr n)
    forall (n :: Nat) (t :: * -> *) (c :: * -> *).
    (KnownNat n, Typeable t) =>
    (forall d. Data d => c (t d)) -> Maybe (c (NatRepr n))
    forall (n :: Nat) (t :: * -> * -> *) (c :: * -> *).
    (KnownNat n, Typeable t) =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (NatRepr n))
    forall a.
    Typeable a =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> a -> c a)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c a)
    -> (a -> Constr)
    -> (a -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c a))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
    -> ((forall b. Data b => b -> b) -> a -> a)
    -> (forall r r'.
        (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall r r'.
        (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall u. (forall d. Data d => d -> u) -> a -> [u])
    -> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> Data a
    forall u. Int -> (forall d. Data d => d -> u) -> NatRepr n -> u
    forall u. (forall d. Data d => d -> u) -> NatRepr n -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (NatRepr n)
    forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> NatRepr n -> c (NatRepr n)
    forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (NatRepr n))
    forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (NatRepr n))
    $cgfoldl :: forall (n :: Nat) (c :: * -> *).
    KnownNat n =>
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> NatRepr n -> c (NatRepr n)
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> NatRepr n -> c (NatRepr n)
    $cgunfold :: forall (n :: Nat) (c :: * -> *).
    KnownNat n =>
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (NatRepr n)
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (NatRepr n)
    $ctoConstr :: forall (n :: Nat). KnownNat n => NatRepr n -> Constr
    toConstr :: NatRepr n -> Constr
    $cdataTypeOf :: forall (n :: Nat). KnownNat n => NatRepr n -> DataType
    dataTypeOf :: NatRepr n -> DataType
    $cdataCast1 :: forall (n :: Nat) (t :: * -> *) (c :: * -> *).
    (KnownNat n, Typeable t) =>
    (forall d. Data d => c (t d)) -> Maybe (c (NatRepr n))
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (NatRepr n))
    $cdataCast2 :: forall (n :: Nat) (t :: * -> * -> *) (c :: * -> *).
    (KnownNat n, Typeable t) =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (NatRepr n))
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (NatRepr n))
    $cgmapT :: forall (n :: Nat).
    KnownNat n =>
    (forall b. Data b => b -> b) -> NatRepr n -> NatRepr n
    gmapT :: (forall b. Data b => b -> b) -> NatRepr n -> NatRepr n
    $cgmapQl :: forall (n :: Nat) r r'.
    KnownNat n =>
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    $cgmapQr :: forall (n :: Nat) r r'.
    KnownNat n =>
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> NatRepr n -> r
    $cgmapQ :: forall (n :: Nat) u.
    KnownNat n =>
    (forall d. Data d => d -> u) -> NatRepr n -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> NatRepr n -> [u]
    $cgmapQi :: forall (n :: Nat) u.
    KnownNat n =>
    Int -> (forall d. Data d => d -> u) -> NatRepr n -> u
    gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> NatRepr n -> u
    $cgmapM :: forall (n :: Nat) (m :: * -> *).
    (KnownNat n, Monad m) =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    gmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    $cgmapMp :: forall (n :: Nat) (m :: * -> *).
    (KnownNat n, MonadPlus m) =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    gmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    $cgmapMo :: forall (n :: Nat) (m :: * -> *).
    (KnownNat n, MonadPlus m) =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> NatRepr n -> m (NatRepr n)
    Data)

    type role NatRepr nominal

    instance Eq (NatRepr m) where
      NatRepr m
    _ == :: NatRepr m -> NatRepr m -> Bool
    == NatRepr m
    _ = Bool
    True

    instance EqF NatRepr where
      eqF :: forall (m :: Nat). NatRepr m -> NatRepr m -> Bool
    eqF NatRepr a
    _ NatRepr a
    _ = Bool
    True

    instance TestEquality NatRepr where
      testEquality :: forall (a :: Nat) (b :: Nat).
    NatRepr a -> NatRepr b -> Maybe (a :~: b)
    testEquality (NatRepr Nat
    m) (NatRepr Nat
    n)
        | Nat
    m Nat -> Nat -> Bool
    forall a. Eq a => a -> a -> Bool
    == Nat
    n = (a :~: b) -> Maybe (a :~: b)
    forall a. a -> Maybe a
    Just a :~: b
    forall {k} (a :: k) (b :: k). a :~: b
    unsafeAxiom
        | Bool
    otherwise = Maybe (a :~: b)
    forall a. Maybe a
    Nothing

    instance DecidableEq NatRepr where
      decEq :: forall (a :: Nat) (b :: Nat).
    NatRepr a -> NatRepr b -> Either (a :~: b) ((a :~: b) -> Void)
    decEq (NatRepr Nat
    m) (NatRepr Nat
    n)
        | Nat
    m Nat -> Nat -> Bool
    forall a. Eq a => a -> a -> Bool
    == Nat
    n    = (a :~: b) -> Either (a :~: b) ((a :~: b) -> Void)
    forall a b. a -> Either a b
    Left a :~: b
    forall {k} (a :: k) (b :: k). a :~: b
    unsafeAxiom
        | Bool
    otherwise = ((a :~: b) -> Void) -> Either (a :~: b) ((a :~: b) -> Void)
    forall a b. b -> Either a b
    Right (((a :~: b) -> Void) -> Either (a :~: b) ((a :~: b) -> Void))
    -> ((a :~: b) -> Void) -> Either (a :~: b) ((a :~: b) -> Void)
    forall a b. (a -> b) -> a -> b
    $
            \a :~: b
    x -> (a :~: b) -> Void -> Void
    forall a b. a -> b -> b
    seq a :~: b
    x (Void -> Void) -> Void -> Void
    forall a b. (a -> b) -> a -> b
    $ [Char] -> Void
    forall a. HasCallStack => [Char] -> a
    error [Char]
    "Impossible [DecidableEq on NatRepr]"

    compareNat :: NatRepr m -> NatRepr n -> NatComparison m n
    compareNat :: forall (m :: Nat) (n :: Nat).
    NatRepr m -> NatRepr n -> NatComparison m n
    compareNat NatRepr m
    m NatRepr n
    n =
      case Nat -> Nat -> Ordering
    forall a. Ord a => a -> a -> Ordering
    compare (NatRepr m -> Nat
    forall (n :: Nat). NatRepr n -> Nat
    natValue NatRepr m
    m) (NatRepr n -> Nat
    forall (n :: Nat). NatRepr n -> Nat
    natValue NatRepr n
    n) of
        Ordering
    LT -> (NatRepr 0 -> NatComparison 0 1)
    -> NatRepr (ZonkAny 0) -> NatComparison m n
    forall a b. a -> b
    unsafeCoerce (forall (x :: Nat) (y :: Nat).
    ((x + 1) <= (x + (y + 1))) =>
    NatRepr y -> NatComparison x (x + (y + 1))
    NatLT @0 @0) (Nat -> NatRepr (ZonkAny 0)
    forall (n :: Nat). Nat -> NatRepr n
    NatRepr (NatRepr n -> Nat
    forall (n :: Nat). NatRepr n -> Nat
    natValue NatRepr n
    n Nat -> Nat -> Nat
    forall a. Num a => a -> a -> a
    - NatRepr m -> Nat
    forall (n :: Nat). NatRepr n -> Nat
    natValue NatRepr m
    m Nat -> Nat -> Nat
    forall a. Num a => a -> a -> a
    - Nat
    1))
        Ordering
    EQ -> NatComparison (ZonkAny 1) (ZonkAny 1) -> NatComparison m n
    forall a b. a -> b
    unsafeCoerce  NatComparison (ZonkAny 1) (ZonkAny 1)
    forall (x :: Nat). NatComparison x x
    NatEQ
        Ordering
    GT -> (NatRepr 0 -> NatComparison 1 0)
    -> NatRepr (ZonkAny 2) -> NatComparison m n
    forall a b. a -> b
    unsafeCoerce (forall (x :: Nat) (y :: Nat).
    ((x + 1) <= (x + (y + 1))) =>
    NatRepr y -> NatComparison (x + (y + 1)) x
    NatGT @0 @0) (Nat -> NatRepr (ZonkAny 2)
    forall (n :: Nat). Nat -> NatRepr n
    NatRepr (NatRepr m -> Nat
    forall (n :: Nat). NatRepr n -> Nat
    natValue NatRepr m
    m Nat -> Nat -> Nat
    forall a. Num a => a -> a -> a
    - NatRepr n -> Nat
    forall (n :: Nat). NatRepr n -> Nat
    natValue NatRepr n
    n Nat -> Nat -> Nat
    forall a. Num a => a -> a -> a
    - Nat
    1))

    -- | Result of comparing two numbers.
    data NatComparison m n where
      -- First number is less than second.
      NatLT :: x+1 <= x+(y+1) => !(NatRepr y) -> NatComparison x (x+(y+1))
      NatEQ :: NatComparison x x
      -- First number is greater than second.
      NatGT :: x+1 <= x+(y+1) => !(NatRepr y) -> NatComparison (x+(y+1)) x

    instance OrdF NatRepr where
      compareF :: forall (x :: Nat) (y :: Nat).
    NatRepr x -> NatRepr y -> OrderingF x y
    compareF NatRepr x
    x NatRepr y
    y =
        case NatRepr x -> NatRepr y -> NatComparison x y
    forall (m :: Nat) (n :: Nat).
    NatRepr m -> NatRepr n -> NatComparison m n
    compareNat NatRepr x
    x NatRepr y
    y of
          NatLT NatRepr y
    _ -> OrderingF x y
    forall {k} (x :: k) (y :: k). OrderingF x y
    LTF
          NatComparison x y
    NatEQ -> OrderingF x x
    OrderingF x y
    forall {k} (x :: k). OrderingF x x
    EQF
          NatGT NatRepr y
    _ -> OrderingF x y
    forall {k} (x :: k) (y :: k). OrderingF x y
    GTF

    instance PolyEq (NatRepr m) (NatRepr n) where
      polyEqF :: NatRepr m -> NatRepr n -> Maybe (NatRepr m :~: NatRepr n)
    polyEqF NatRepr m
    x NatRepr n
    y = ((m :~: n) -> NatRepr m :~: NatRepr n)
    -> Maybe (m :~: n) -> Maybe (NatRepr m :~: NatRepr n)
    forall a b. (a -> b) -> Maybe a -> Maybe b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap (\m :~: n
    Refl -> NatRepr m :~: NatRepr m
    NatRepr m :~: NatRepr n
    forall {k} (a :: k). a :~: a
    Refl) (Maybe (m :~: n) -> Maybe (NatRepr m :~: NatRepr n))
    -> Maybe (m :~: n) -> Maybe (NatRepr m :~: NatRepr n)
    forall a b. (a -> b) -> a -> b
    $ NatRepr m -> NatRepr n -> Maybe (m :~: n)
    forall (a :: Nat) (b :: Nat).
    NatRepr a -> NatRepr b -> Maybe (a :~: b)
    forall {k} (f :: k -> *) (a :: k) (b :: k).
    TestEquality f =>
    f a -> f b -> Maybe (a :~: b)
    testEquality NatRepr m
    x NatRepr n
    y

    instance Show (NatRepr n) where
      show :: NatRepr n -> [Char]
    show (NatRepr Nat
    n) = Nat -> [Char]
    forall a. Show a => a -> [Char]
    show Nat
    n

    instance ShowF NatRepr

    instance HashableF NatRepr where
      hashWithSaltF :: forall (n :: Nat). Int -> NatRepr n -> Int
    hashWithSaltF = Int -> NatRepr tp -> Int
    forall a. Hashable a => Int -> a -> Int
    hashWithSalt

    -- | This generates a NatRepr from a type-level context.
    knownNat :: forall n . KnownNat n => NatRepr n
    knownNat :: forall (n :: Nat). KnownNat n => NatRepr n
    knownNat = Nat -> NatRepr n
    forall (n :: Nat). Nat -> NatRepr n
    NatRepr (Proxy n -> Nat
    forall (n :: Nat) (proxy :: Nat -> *). KnownNat n => proxy n -> Nat
    natVal (Proxy n
    forall {k} (t :: k). Proxy t
    Proxy :: Proxy n))

    instance (KnownNat n) => KnownRepr NatRepr n where
      knownRepr :: NatRepr n
    knownRepr = NatRepr n
    forall (n :: Nat). KnownNat n => NatRepr n
    knownNat