papa-0.7.0: src/Papa/Base/Data/NonZero.hs
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | A number that is not zero: a divisor (classy optics, rule 14, a data
-- type with an invariant).
--
-- Division by a 'NonZero' needs no 'Data.Maybe.Maybe': the divisor is
-- checked once, where it is built, and then divides any number of times
-- ('Papa.Base.Data.Integral.div', 'Papa.Base.Data.Fractional./',
-- 'Papa.Base.Data.Ratio.%', ...).
--
-- * The constructor is not exported.
-- * Its own name, 'nonZero', is also its smart constructor: a prism from the
-- number, whose 'Control.Lens.preview' checks that it is not zero and
-- whose 'Control.Lens.review' gives the number back. The prism laws hold,
-- because the check never changes the number.
-- * The smart constructor has an instance for each numeric type of base
-- ('Int', 'Int8' .. 'Int64', 'Integer', 'Word', 'Word8' .. 'Word64',
-- 'Natural', 'Float', 'Double', 'Ratio'), not one for every type: an
-- instance for a type variable would overlap the identity instance.
-- * Its field, 'divisor', is a getter, so no optic can make it zero.
-- * No instance that could build a zero is written: not 'Prelude.Num'
-- (@fromInteger 0@, @-@), 'Data.Semigroup.Semigroup' (a product of
-- fixed-width integers can overflow to 0), 'GHC.Generics.Generic' or
-- 'Data.Data.Data'.
--
-- @since 0.7.0
module Papa.Base.Data.NonZero (
-- * Type
NonZero,
validNonZero,
previewNonZero,
-- * Optics
NonZeroLike (..),
lawNamesNonZero,
InheritNonZero,
inheritNonZero,
) where
import Control.Category (id)
import Control.DeepSeq (NFData)
import Control.Lens (Choice, Optic, preview, prism')
import Data.Bool (Bool (False), bool)
import Data.Eq (Eq ((/=)))
import Data.Foldable (Foldable (foldMap, foldr, length, null))
import Data.Foldable1 (Foldable1 (foldMap1, toNonEmpty))
import Data.Functor (Functor)
import Data.Functor.Classes (Eq1 (liftEq), Ord1 (liftCompare), Show1 (liftShowsPrec), showsUnaryWith)
import Data.Functor.Const (Const)
import Data.Functor.Contravariant (Contravariant, phantom)
import Data.Int (Int, Int16, Int32, Int64, Int8)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.Maybe (Maybe (Just, Nothing))
import Data.Monoid (First)
import Data.Ord (Ord)
import Data.Ratio (Ratio)
import Data.Type.Equality (type (~))
import Data.Word (Word, Word16, Word32, Word64, Word8)
import Numeric.Natural (Natural)
import Papa.Base.Data.Divisor (DivisorLike (divisor))
import Papa.Lens.Optic (the)
import Prelude (Applicative, Double, Float, Integer, Integral, Num, Show)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInt = Gen.int (Range.linearFrom 0 (-100) 100)
-- >>> let genNonZero = Gen.just (preview nonZero <$> genInt)
-- | A number that is not zero. Build one with @preview nonZero@.
--
-- ==== __Examples__
--
-- >>> preview nonZero (3 :: Int)
-- Just (NonZero 3)
--
-- >>> preview nonZero (0 :: Int)
-- Nothing
--
-- >>> preview nonZero (0.5 :: Double)
-- Just (NonZero 0.5)
--
-- @since 0.7.0
newtype NonZero a = NonZero a
deriving stock (Eq, Ord, Show)
deriving newtype (NFData)
-- | The two values are compared with the given equality.
--
-- >>> liftEq (==) <$> preview nonZero (1 :: Int) <*> preview nonZero (1 :: Int)
-- Just True
--
-- >>> check $ withTests 100 $ property $ do x <- forAll genNonZero; y <- forAll genNonZero; liftEq (==) x y === (x == y)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
instance Eq1 NonZero where
liftEq f (NonZero a) (NonZero b) =
f a b
{-# INLINE liftEq #-}
-- | The two values are compared with the given comparison.
--
-- >>> check $ withTests 100 $ property $ do x <- forAll genNonZero; y <- forAll genNonZero; liftCompare compare x y === compare x y
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
instance Ord1 NonZero where
liftCompare f (NonZero a) (NonZero b) =
f a b
{-# INLINE liftCompare #-}
-- | Shown as the derived 'Show' shows it.
--
-- >>> check $ withTests 100 $ property $ do x <- forAll genNonZero; d <- forAll (Gen.int (Range.linear 0 11)); liftShowsPrec showsPrec showList d x "" === showsPrec d x ""
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
instance Show1 NonZero where
liftShowsPrec sp _ d (NonZero a) =
showsUnaryWith sp "NonZero" d a
{-# INLINE liftShowsPrec #-}
-- | Right fold of a 'NonZero': the worker of 'foldr' (fusion-rules
-- pattern).
--
-- A 'NonZero' holds exactly one value and has no 'Functor' (rule 14: @fmap@
-- could make it zero), and no @build@, so none of the fusion rules of the
-- fusion-rules skill (map/fold, map/map, build/fold) has a left-hand side
-- here; the workers are not recursive, so they are @INLINE@.
foldrNonZero :: (a -> b -> b) -> b -> NonZero a -> b
foldrNonZero k z (NonZero a) =
k a z
{-# INLINE foldrNonZero #-}
-- | The worker of 'foldMap' and 'foldMap1'.
foldMapNonZero :: (a -> m) -> NonZero a -> m
foldMapNonZero f (NonZero a) =
f a
{-# INLINE foldMapNonZero #-}
-- | The one value.
--
-- >>> fmap toList (preview nonZero (3 :: Int))
-- Just [3]
--
-- >>> check $ withTests 100 $ property $ do x <- forAll genNonZero; (toList x, length x, null x, sum x) === ([the divisor x], 1, False, the divisor x)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
instance Foldable NonZero where
foldr =
foldrNonZero
{-# INLINE foldr #-}
foldMap =
foldMapNonZero
{-# INLINE foldMap #-}
length _ =
1
{-# INLINE length #-}
null _ =
False
{-# INLINE null #-}
-- | Exactly one value, so a 'NonZero' is never empty.
--
-- >>> check $ withTests 100 $ property $ do x <- forAll genNonZero; (toNonEmpty x, foldMap1 (: []) x) === (the divisor x :| [], [the divisor x])
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
instance Foldable1 NonZero where
foldMap1 =
foldMapNonZero
{-# INLINE foldMap1 #-}
toNonEmpty (NonZero a) =
a :| []
{-# INLINE toNonEmpty #-}
-- | The invariant: the number is not zero.
--
-- >>> validNonZero (0 :: Int)
-- False
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genInt; validNonZero n === (n /= 0)
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
validNonZero :: (Eq a, Num a) => a -> Bool
validNonZero n =
n /= 0
{-# INLINE validNonZero #-}
-- | The 'preview' of the smart constructor: 'Just' a 'NonZero' where the
-- invariant holds. Each instance of 'nonZero' for a number is the prism
-- whose 'preview' this is.
--
-- Unlike @preview nonZero@, it takes the type of the 'NonZero' from where it
-- is used, so a literal needs no annotation: in @quotNonZero n \<$\>
-- previewNonZero 100@, @100@ has the type of @n@. ('nonZero''s class goes
-- from the number's type to the 'NonZero''s, so @preview nonZero 100@ needs
-- @(100 :: Int)@.) It works at any 'Num' with 'Eq', such as "Data.Fixed".
--
-- >>> previewNonZero (0 :: Int)
-- Nothing
--
-- >>> (\n -> quotNonZero n <$> previewNonZero 100) (1234 :: Integer)
-- Just 12
--
-- >>> check $ withTests 100 $ property $ do n <- forAll genInt; previewNonZero n === preview nonZero n
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
previewNonZero :: (Eq a, Num a) => a -> Maybe (NonZero a)
previewNonZero n =
bool Nothing (Just (NonZero n)) (validNonZero n)
{-# INLINE previewNonZero #-}
-- | The class of 'NonZero' (rule 3).
--
-- @since 0.7.0
class NonZeroLike p f x x' a a' | x -> a where
nonZero ::
Optic p f x x' (NonZero a) (NonZero a')
-- Identity
instance (x' ~ NonZero a') => NonZeroLike p f (NonZero a) x' a a' where
nonZero = id
{-# INLINE nonZero #-}
-- Prism: the smart constructor, for each numeric type of base. preview
-- checks the invariant, and review gives back the number.
instance (Choice p, Applicative f, x' ~ Int, a' ~ Int) => NonZeroLike p f Int x' Int a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Int8, a' ~ Int8) => NonZeroLike p f Int8 x' Int8 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Int16, a' ~ Int16) => NonZeroLike p f Int16 x' Int16 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Int32, a' ~ Int32) => NonZeroLike p f Int32 x' Int32 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Int64, a' ~ Int64) => NonZeroLike p f Int64 x' Int64 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Integer, a' ~ Integer) => NonZeroLike p f Integer x' Integer a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Word, a' ~ Word) => NonZeroLike p f Word x' Word a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Word8, a' ~ Word8) => NonZeroLike p f Word8 x' Word8 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Word16, a' ~ Word16) => NonZeroLike p f Word16 x' Word16 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Word32, a' ~ Word32) => NonZeroLike p f Word32 x' Word32 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Word64, a' ~ Word64) => NonZeroLike p f Word64 x' Word64 a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Natural, a' ~ Natural) => NonZeroLike p f Natural x' Natural a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Float, a' ~ Float) => NonZeroLike p f Float x' Float a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, x' ~ Double, a' ~ Double) => NonZeroLike p f Double x' Double a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Prism
instance (Choice p, Applicative f, Integral b, x' ~ Ratio b, a' ~ Ratio b) => NonZeroLike p f (Ratio b) x' (Ratio b) a' where
nonZero = prism' (the divisor) previewNonZero
{-# INLINE nonZero #-}
-- Getter
instance (p ~ (->), Contravariant f, Functor f, x' ~ NonZero a, a' ~ a) => DivisorLike p f (NonZero a) x' a a' where
divisor f (NonZero n) = phantom (f n)
{-# INLINE divisor #-}
-- | Rebuild a value through the smart constructor (rule 7), so it can fail.
-- It holds at each numeric type that has a smart constructor.
--
-- Laws:
--
-- > lawNamesNonZero == Just
--
-- >>> check $ withTests 100 $ property $ do s <- forAll (Gen.just (preview nonZero <$> genInt)); lawNamesNonZero s === Just s
-- ...passed 100 tests.
-- True
--
-- @since 0.7.0
lawNamesNonZero :: (NonZeroLike (->) (Const (First (NonZero a))) a a a a) => NonZero a -> Maybe (NonZero a)
lawNamesNonZero s =
preview nonZero (the divisor s)
{-# INLINE lawNamesNonZero #-}
-- | The classes that a data type holding 'NonZero' inherits (rule 6).
--
-- @since 0.7.0
type InheritNonZero s a =
( NonZeroLike (->) (Const ()) s s a a
, DivisorLike (->) (Const ()) s s a a
)
-- | Check, where it is applied, that @s@ inherits every name of 'NonZero'.
--
-- @since 0.7.0
inheritNonZero :: (InheritNonZero s a) => proxy s -> ()
inheritNonZero _ =
()
{-# INLINE inheritNonZero #-}