{-# language Trustworthy #-}
module NatOptics.NonNegative
(
NonNegative,
refine, natPrism, intPrism,
natIso, textPrism, stringPrism,
Natural, Integer, Prism', Iso',
view, review, preview,
) where
import Control.Applicative ( (*>) )
import Control.Monad ( guard )
import Data.Bits ( Bits, toIntegralSized )
import Data.Function ( (.) )
import Data.Functor ( fmap, ($>), (<$>) )
import Data.Maybe ( Maybe )
import Data.Ord ( Ord, (>=) )
import Data.String ( String )
import Data.Text ( Text )
import NatOptics.Internal ( strNat, textStr )
import Numeric.Natural ( Natural )
import Optics.AffineFold ( preview )
import Optics.Getter ( view )
import Optics.Iso ( Iso', iso )
import Optics.Optic ( (%) )
import Optics.Prism ( Prism', prism' )
import Optics.Review ( review )
import Prelude ( Integer, Integral, Num,
fromIntegral, toInteger )
import NatOptics.NonNegative.Unsafe (NonNegative (..))
refine :: (Num n, Ord n) => Prism' n (NonNegative n)
refine :: Prism' n (NonNegative n)
refine = (NonNegative n -> n)
-> (n -> Maybe (NonNegative n)) -> Prism' n (NonNegative n)
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' NonNegative n -> n
forall number. NonNegative number -> number
number n -> Maybe (NonNegative n)
forall n. (Ord n, Num n) => n -> Maybe (NonNegative n)
verify
natPrism :: (Integral n, Bits n) => Prism' Natural (NonNegative n)
natPrism :: Prism' Natural (NonNegative n)
natPrism =
(NonNegative n -> Natural)
-> (Natural -> Maybe (NonNegative n))
-> Prism' Natural (NonNegative n)
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism'
(n -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral (n -> Natural) -> (NonNegative n -> n) -> NonNegative n -> Natural
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonNegative n -> n
forall number. NonNegative number -> number
number)
((n -> NonNegative n) -> Maybe n -> Maybe (NonNegative n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap n -> NonNegative n
forall number. number -> NonNegative number
NonNegativeUnsafe (Maybe n -> Maybe (NonNegative n))
-> (Natural -> Maybe n) -> Natural -> Maybe (NonNegative n)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Natural -> Maybe n
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
toIntegralSized)
intPrism :: (Integral n, Bits n) => Prism' Integer (NonNegative n)
intPrism :: Prism' Integer (NonNegative n)
intPrism = (NonNegative n -> Integer)
-> (Integer -> Maybe (NonNegative n))
-> Prism' Integer (NonNegative n)
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism' (n -> Integer
forall a. Integral a => a -> Integer
toInteger (n -> Integer) -> (NonNegative n -> n) -> NonNegative n -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonNegative n -> n
forall number. NonNegative number -> number
number) Integer -> Maybe (NonNegative n)
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe (NonNegative b)
verifyAndResize
natIso :: Iso' Natural (NonNegative Natural)
natIso :: Iso' Natural (NonNegative Natural)
natIso = (Natural -> NonNegative Natural)
-> (NonNegative Natural -> Natural)
-> Iso' Natural (NonNegative Natural)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso Natural -> NonNegative Natural
forall number. number -> NonNegative number
NonNegativeUnsafe NonNegative Natural -> Natural
forall number. NonNegative number -> number
number
stringPrism :: (Integral n, Bits n) => Prism' String (NonNegative n)
stringPrism :: Prism' String (NonNegative n)
stringPrism = Prism' String Natural
strNat Prism' String Natural
-> Optic
A_Prism NoIx Natural Natural (NonNegative n) (NonNegative n)
-> Prism' String (NonNegative n)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic A_Prism NoIx Natural Natural (NonNegative n) (NonNegative n)
forall n. (Integral n, Bits n) => Prism' Natural (NonNegative n)
natPrism
textPrism :: (Integral n, Bits n) => Prism' Text (NonNegative n)
textPrism :: Prism' Text (NonNegative n)
textPrism = Iso' Text String
textStr Iso' Text String
-> Optic A_Prism NoIx String String (NonNegative n) (NonNegative n)
-> Prism' Text (NonNegative n)
forall k l m (is :: IxList) (js :: IxList) (ks :: IxList) s t u v a
b.
(JoinKinds k l m, AppendIndices is js ks) =>
Optic k is s t u v -> Optic l js u v a b -> Optic m ks s t a b
% Optic A_Prism NoIx String String (NonNegative n) (NonNegative n)
forall n. (Integral n, Bits n) => Prism' String (NonNegative n)
stringPrism
verify :: (Ord n, Num n) => n -> Maybe (NonNegative n)
verify :: n -> Maybe (NonNegative n)
verify n
n = Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (n
n n -> n -> Bool
forall a. Ord a => a -> a -> Bool
>= n
0) Maybe () -> NonNegative n -> Maybe (NonNegative n)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> n -> NonNegative n
forall number. number -> NonNegative number
NonNegativeUnsafe n
n
verifyAndResize :: (Integral a, Integral b, Bits a, Bits b)
=> a -> Maybe (NonNegative b)
verifyAndResize :: a -> Maybe (NonNegative b)
verifyAndResize a
x = a -> Maybe (NonNegative a)
forall n. (Ord n, Num n) => n -> Maybe (NonNegative n)
verify a
x Maybe (NonNegative a)
-> Maybe (NonNegative b) -> Maybe (NonNegative b)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (b -> NonNegative b
forall number. number -> NonNegative number
NonNegativeUnsafe (b -> NonNegative b) -> Maybe b -> Maybe (NonNegative b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe b
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
toIntegralSized a
x)