{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Grisette.Core.Data.Class.BitVector
(
SomeBV (..),
someBVZext',
someBVSext',
someBVExt',
someBVSelect',
someBVExtract,
someBVExtract',
SizedBV (..),
sizedBVExtract,
)
where
import Data.Proxy
import GHC.TypeNats
import Grisette.Utils.Parameterized
class SomeBV bv where
someBVConcat :: bv -> bv -> bv
someBVZext ::
forall p l.
KnownNat l =>
p l ->
bv ->
bv
someBVSext ::
forall p l.
KnownNat l =>
p l ->
bv ->
bv
someBVExt ::
forall p l.
KnownNat l =>
p l ->
bv ->
bv
someBVSelect ::
forall p ix q w.
(KnownNat ix, KnownNat w) =>
p ix ->
q w ->
bv ->
bv
someBVZext' ::
forall l bv.
SomeBV bv =>
NatRepr l ->
bv ->
bv
someBVZext' :: forall (l :: Nat) bv. SomeBV bv => NatRepr l -> bv -> bv
someBVZext' p :: NatRepr l
p@(NatRepr l
_ :: NatRepr l) = forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr l
p) forall a b. (a -> b) -> a -> b
$ forall bv (p :: Nat -> *) (l :: Nat).
(SomeBV bv, KnownNat l) =>
p l -> bv -> bv
someBVZext (forall {k} (t :: k). Proxy t
Proxy @l)
{-# INLINE someBVZext' #-}
someBVSext' ::
forall l bv.
SomeBV bv =>
NatRepr l ->
bv ->
bv
someBVSext' :: forall (l :: Nat) bv. SomeBV bv => NatRepr l -> bv -> bv
someBVSext' p :: NatRepr l
p@(NatRepr l
_ :: NatRepr l) = forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr l
p) forall a b. (a -> b) -> a -> b
$ forall bv (p :: Nat -> *) (l :: Nat).
(SomeBV bv, KnownNat l) =>
p l -> bv -> bv
someBVSext (forall {k} (t :: k). Proxy t
Proxy @l)
{-# INLINE someBVSext' #-}
someBVExt' ::
forall l bv.
SomeBV bv =>
NatRepr l ->
bv ->
bv
someBVExt' :: forall (l :: Nat) bv. SomeBV bv => NatRepr l -> bv -> bv
someBVExt' p :: NatRepr l
p@(NatRepr l
_ :: NatRepr l) = forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr l
p) forall a b. (a -> b) -> a -> b
$ forall bv (p :: Nat -> *) (l :: Nat).
(SomeBV bv, KnownNat l) =>
p l -> bv -> bv
someBVExt (forall {k} (t :: k). Proxy t
Proxy @l)
{-# INLINE someBVExt' #-}
someBVSelect' ::
forall ix w bv.
SomeBV bv =>
NatRepr ix ->
NatRepr w ->
bv ->
bv
someBVSelect' :: forall (ix :: Nat) (w :: Nat) bv.
SomeBV bv =>
NatRepr ix -> NatRepr w -> bv -> bv
someBVSelect' p :: NatRepr ix
p@(NatRepr ix
_ :: NatRepr l) q :: NatRepr w
q@(NatRepr w
_ :: NatRepr r) = forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr ix
p) forall a b. (a -> b) -> a -> b
$ forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr w
q) forall a b. (a -> b) -> a -> b
$ forall bv (p :: Nat -> *) (ix :: Nat) (q :: Nat -> *) (w :: Nat).
(SomeBV bv, KnownNat ix, KnownNat w) =>
p ix -> q w -> bv -> bv
someBVSelect NatRepr ix
p NatRepr w
q
{-# INLINE someBVSelect' #-}
someBVExtract ::
forall p (i :: Nat) q (j :: Nat) bv.
(SomeBV bv, KnownNat i, KnownNat j) =>
p i ->
q j ->
bv ->
bv
p i
_ q j
_ =
forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). Nat -> KnownProof n
unsafeKnownProof @(i - j + 1) (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (n :: Nat) (proxy :: Nat -> *). KnownNat n => proxy n -> Nat
natVal (forall {k} (t :: k). Proxy t
Proxy @i)) forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall (n :: Nat) (proxy :: Nat -> *). KnownNat n => proxy n -> Nat
natVal (forall {k} (t :: k). Proxy t
Proxy @j)) forall a. Num a => a -> a -> a
+ Nat
1)) forall a b. (a -> b) -> a -> b
$
forall bv (p :: Nat -> *) (ix :: Nat) (q :: Nat -> *) (w :: Nat).
(SomeBV bv, KnownNat ix, KnownNat w) =>
p ix -> q w -> bv -> bv
someBVSelect (forall {k} (t :: k). Proxy t
Proxy @j) (forall {k} (t :: k). Proxy t
Proxy @(i - j + 1))
{-# INLINE someBVExtract #-}
someBVExtract' ::
forall (i :: Nat) (j :: Nat) bv.
SomeBV bv =>
NatRepr i ->
NatRepr j ->
bv ->
bv
p :: NatRepr i
p@(NatRepr i
_ :: NatRepr l) q :: NatRepr j
q@(NatRepr j
_ :: NatRepr r) = forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr i
p) forall a b. (a -> b) -> a -> b
$ forall (n :: Nat) r. KnownProof n -> (KnownNat n => r) -> r
withKnownProof (forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr NatRepr j
q) forall a b. (a -> b) -> a -> b
$ forall (p :: Nat -> *) (i :: Nat) (q :: Nat -> *) (j :: Nat) bv.
(SomeBV bv, KnownNat i, KnownNat j) =>
p i -> q j -> bv -> bv
someBVExtract NatRepr i
p NatRepr j
q
{-# INLINE someBVExtract' #-}
class SizedBV bv where
sizedBVConcat :: (KnownNat l, KnownNat r, 1 <= l, 1 <= r) => bv l -> bv r -> bv (l + r)
sizedBVZext ::
(KnownNat l, KnownNat r, 1 <= l, KnownNat r, l <= r) =>
proxy r ->
bv l ->
bv r
sizedBVSext ::
(KnownNat l, KnownNat r, 1 <= l, KnownNat r, l <= r) =>
proxy r ->
bv l ->
bv r
sizedBVExt ::
(KnownNat l, KnownNat r, 1 <= l, KnownNat r, l <= r) =>
proxy r ->
bv l ->
bv r
sizedBVSelect ::
(KnownNat n, KnownNat ix, KnownNat w, 1 <= n, 1 <= w, ix + w <= n) =>
proxy ix ->
proxy w ->
bv n ->
bv w
sizedBVExtract ::
forall proxy i j n bv.
(SizedBV bv, KnownNat n, KnownNat i, KnownNat j, 1 <= n, i + 1 <= n, j <= i) =>
proxy i ->
proxy j ->
bv n ->
bv (i - j + 1)
proxy i
_ proxy j
_ =
case ( forall (n :: Nat). NatRepr n -> KnownProof n
hasRepr (forall (m :: Nat) (n :: Nat).
NatRepr m -> NatRepr n -> NatRepr (m + n)
addNat (forall (n :: Nat) (m :: Nat).
(n <= m) =>
NatRepr m -> NatRepr n -> NatRepr (m - n)
subNat (forall (n :: Nat). KnownNat n => NatRepr n
natRepr @i) (forall (n :: Nat). KnownNat n => NatRepr n
natRepr @j)) (forall (n :: Nat). KnownNat n => NatRepr n
natRepr @1)),
forall (m :: Nat) (n :: Nat). LeqProof m n
unsafeLeqProof @(j + (i - j + 1)) @n,
forall (m :: Nat) (n :: Nat). LeqProof m n
unsafeLeqProof @1 @(i - j + 1)
) of
(KnownProof ((i - j) + 1)
KnownProof, LeqProof (j + ((i - j) + 1)) n
LeqProof, LeqProof 1 ((i - j) + 1)
LeqProof) ->
forall (bv :: Nat -> *) (n :: Nat) (ix :: Nat) (w :: Nat)
(proxy :: Nat -> *).
(SizedBV bv, KnownNat n, KnownNat ix, KnownNat w, 1 <= n, 1 <= w,
(ix + w) <= n) =>
proxy ix -> proxy w -> bv n -> bv w
sizedBVSelect (forall {k} (t :: k). Proxy t
Proxy @j) (forall {k} (t :: k). Proxy t
Proxy @(i - j + 1))
{-# INLINE sizedBVExtract #-}