{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
module Data.Ring.Ordered
(
Modular(..),
OrderedRing(..),
) where
import Control.Applicative (Const (Const))
import Data.Data (Data)
import Data.Fixed (HasResolution, Fixed)
import Data.Functor.Identity (Identity (Identity))
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Monoid (Dual (Dual))
import Data.Ord (Down (Down))
import Data.Ratio (Ratio)
import Data.Semiring (Ring, Semiring(zero))
import Data.Word (Word8, Word16, Word32, Word64)
import GHC.Generics (Generic)
import Prelude hiding (signum, abs, negate, (-))
import qualified Prelude as Num
import Data.Typeable (Typeable)
newtype Modular a = Modular { forall a. Modular a -> a
getModular :: a }
deriving
( Modular a
Modular a -> Modular a -> Bounded (Modular a)
forall a. a -> a -> Bounded a
forall a. Bounded a => Modular a
$cminBound :: forall a. Bounded a => Modular a
minBound :: Modular a
$cmaxBound :: forall a. Bounded a => Modular a
maxBound :: Modular a
Bounded
, Modular a -> Modular a -> Bool
(Modular a -> Modular a -> Bool)
-> (Modular a -> Modular a -> Bool) -> Eq (Modular a)
forall a. Eq a => Modular a -> Modular a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Modular a -> Modular a -> Bool
== :: Modular a -> Modular a -> Bool
$c/= :: forall a. Eq a => Modular a -> Modular a -> Bool
/= :: Modular a -> Modular a -> Bool
Eq
, Eq (Modular a)
Eq (Modular a) =>
(Modular a -> Modular a -> Ordering)
-> (Modular a -> Modular a -> Bool)
-> (Modular a -> Modular a -> Bool)
-> (Modular a -> Modular a -> Bool)
-> (Modular a -> Modular a -> Bool)
-> (Modular a -> Modular a -> Modular a)
-> (Modular a -> Modular a -> Modular a)
-> Ord (Modular a)
Modular a -> Modular a -> Bool
Modular a -> Modular a -> Ordering
Modular a -> Modular a -> Modular a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Modular a)
forall a. Ord a => Modular a -> Modular a -> Bool
forall a. Ord a => Modular a -> Modular a -> Ordering
forall a. Ord a => Modular a -> Modular a -> Modular a
$ccompare :: forall a. Ord a => Modular a -> Modular a -> Ordering
compare :: Modular a -> Modular a -> Ordering
$c< :: forall a. Ord a => Modular a -> Modular a -> Bool
< :: Modular a -> Modular a -> Bool
$c<= :: forall a. Ord a => Modular a -> Modular a -> Bool
<= :: Modular a -> Modular a -> Bool
$c> :: forall a. Ord a => Modular a -> Modular a -> Bool
> :: Modular a -> Modular a -> Bool
$c>= :: forall a. Ord a => Modular a -> Modular a -> Bool
>= :: Modular a -> Modular a -> Bool
$cmax :: forall a. Ord a => Modular a -> Modular a -> Modular a
max :: Modular a -> Modular a -> Modular a
$cmin :: forall a. Ord a => Modular a -> Modular a -> Modular a
min :: Modular a -> Modular a -> Modular a
Ord
, Int -> Modular a -> ShowS
[Modular a] -> ShowS
Modular a -> String
(Int -> Modular a -> ShowS)
-> (Modular a -> String)
-> ([Modular a] -> ShowS)
-> Show (Modular a)
forall a. Show a => Int -> Modular a -> ShowS
forall a. Show a => [Modular a] -> ShowS
forall a. Show a => Modular a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Modular a -> ShowS
showsPrec :: Int -> Modular a -> ShowS
$cshow :: forall a. Show a => Modular a -> String
show :: Modular a -> String
$cshowList :: forall a. Show a => [Modular a] -> ShowS
showList :: [Modular a] -> ShowS
Show
, ReadPrec [Modular a]
ReadPrec (Modular a)
Int -> ReadS (Modular a)
ReadS [Modular a]
(Int -> ReadS (Modular a))
-> ReadS [Modular a]
-> ReadPrec (Modular a)
-> ReadPrec [Modular a]
-> Read (Modular a)
forall a. Read a => ReadPrec [Modular a]
forall a. Read a => ReadPrec (Modular a)
forall a. Read a => Int -> ReadS (Modular a)
forall a. Read a => ReadS [Modular a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Int -> ReadS (Modular a)
readsPrec :: Int -> ReadS (Modular a)
$creadList :: forall a. Read a => ReadS [Modular a]
readList :: ReadS [Modular a]
$creadPrec :: forall a. Read a => ReadPrec (Modular a)
readPrec :: ReadPrec (Modular a)
$creadListPrec :: forall a. Read a => ReadPrec [Modular a]
readListPrec :: ReadPrec [Modular a]
Read
, (forall x. Modular a -> Rep (Modular a) x)
-> (forall x. Rep (Modular a) x -> Modular a)
-> Generic (Modular a)
forall x. Rep (Modular a) x -> Modular a
forall x. Modular a -> Rep (Modular a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Modular a) x -> Modular a
forall a x. Modular a -> Rep (Modular a) x
$cfrom :: forall a x. Modular a -> Rep (Modular a) x
from :: forall x. Modular a -> Rep (Modular a) x
$cto :: forall a x. Rep (Modular a) x -> Modular a
to :: forall x. Rep (Modular a) x -> Modular a
Generic
, Typeable (Modular a)
Typeable (Modular a) =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Modular a -> c (Modular a))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Modular a))
-> (Modular a -> Constr)
-> (Modular a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Modular a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Modular a)))
-> ((forall b. Data b => b -> b) -> Modular a -> Modular a)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r)
-> (forall u. (forall d. Data d => d -> u) -> Modular a -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> Modular a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a))
-> Data (Modular a)
Modular a -> Constr
Modular a -> DataType
(forall b. Data b => b -> b) -> Modular a -> Modular a
forall a. Data a => Typeable (Modular a)
forall a. Data a => Modular a -> Constr
forall a. Data a => Modular a -> DataType
forall a.
Data a =>
(forall b. Data b => b -> b) -> Modular a -> Modular a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Modular a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> Modular a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Modular a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Modular a -> c (Modular a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Modular a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Modular a))
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) -> Modular a -> u
forall u. (forall d. Data d => d -> u) -> Modular a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Modular a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Modular a -> c (Modular a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Modular a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Modular a))
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Modular a -> c (Modular a)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Modular a -> c (Modular a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Modular a)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Modular a)
$ctoConstr :: forall a. Data a => Modular a -> Constr
toConstr :: Modular a -> Constr
$cdataTypeOf :: forall a. Data a => Modular a -> DataType
dataTypeOf :: Modular a -> DataType
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Modular a))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Modular a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Modular a))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Modular a))
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> Modular a -> Modular a
gmapT :: (forall b. Data b => b -> b) -> Modular a -> Modular a
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Modular a -> r
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> Modular a -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Modular a -> [u]
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> Modular a -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Modular a -> u
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Modular a -> m (Modular a)
Data
, Typeable
)
deriving instance Semiring (Modular Word8)
deriving instance Semiring (Modular Word16)
deriving instance Semiring (Modular Word32)
deriving instance Semiring (Modular Word64)
deriving instance Semiring (Modular Word)
deriving instance Ring (Modular Word8)
deriving instance Ring (Modular Word16)
deriving instance Ring (Modular Word32)
deriving instance Ring (Modular Word64)
deriving instance Ring (Modular Word)
class (Ring a, Ord a) => OrderedRing a where
abs :: a -> a
signum :: a -> a
instance OrderedRing () where
abs :: () -> ()
abs = () -> () -> ()
forall a b. a -> b -> a
const ()
signum :: () -> ()
signum = () -> () -> ()
forall a b. a -> b -> a
const ()
forall a. Semiring a => a
zero
instance (OrderedRing a) => OrderedRing (Dual a) where
abs :: Dual a -> Dual a
abs (Dual a
x) = a -> Dual a
forall a. a -> Dual a
Dual (a -> Dual a) -> (a -> a) -> a -> Dual a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. OrderedRing a => a -> a
abs (a -> Dual a) -> a -> Dual a
forall a b. (a -> b) -> a -> b
$ a
x
signum :: Dual a -> Dual a
signum (Dual a
x) = a -> Dual a
forall a. a -> Dual a
Dual (a -> Dual a) -> (a -> a) -> a -> Dual a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. OrderedRing a => a -> a
signum (a -> Dual a) -> a -> Dual a
forall a b. (a -> b) -> a -> b
$ a
x
instance (OrderedRing a) => OrderedRing (Const a b) where
abs :: Const a b -> Const a b
abs (Const a
x) = a -> Const a b
forall {k} a (b :: k). a -> Const a b
Const (a -> Const a b) -> (a -> a) -> a -> Const a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. OrderedRing a => a -> a
abs (a -> Const a b) -> a -> Const a b
forall a b. (a -> b) -> a -> b
$ a
x
signum :: Const a b -> Const a b
signum (Const a
x) = a -> Const a b
forall {k} a (b :: k). a -> Const a b
Const (a -> Const a b) -> (a -> a) -> a -> Const a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
forall a. OrderedRing a => a -> a
signum (a -> Const a b) -> a -> Const a b
forall a b. (a -> b) -> a -> b
$ a
x
instance (Integral a) => OrderedRing (Ratio a) where
abs :: Ratio a -> Ratio a
abs = Ratio a -> Ratio a
forall a. Num a => a -> a
Num.abs
signum :: Ratio a -> Ratio a
signum = Ratio a -> Ratio a
forall a. Num a => a -> a
Num.signum
deriving instance (OrderedRing a) => OrderedRing (Down a)
deriving instance (OrderedRing a) => OrderedRing (Identity a)
instance (HasResolution a) => OrderedRing (Fixed a) where
abs :: Fixed a -> Fixed a
abs = Fixed a -> Fixed a
forall a. Num a => a -> a
Num.abs
signum :: Fixed a -> Fixed a
signum = Fixed a -> Fixed a
forall a. Num a => a -> a
Num.signum
instance OrderedRing Int8 where
abs :: Int8 -> Int8
abs = Int8 -> Int8
forall a. Num a => a -> a
Num.abs
signum :: Int8 -> Int8
signum = Int8 -> Int8
forall a. Num a => a -> a
Num.signum
instance OrderedRing Int16 where
abs :: Int16 -> Int16
abs = Int16 -> Int16
forall a. Num a => a -> a
Num.abs
signum :: Int16 -> Int16
signum = Int16 -> Int16
forall a. Num a => a -> a
Num.signum
instance OrderedRing Int32 where
abs :: Int32 -> Int32
abs = Int32 -> Int32
forall a. Num a => a -> a
Num.abs
signum :: Int32 -> Int32
signum = Int32 -> Int32
forall a. Num a => a -> a
Num.signum
instance OrderedRing Int64 where
abs :: Int64 -> Int64
abs = Int64 -> Int64
forall a. Num a => a -> a
Num.abs
signum :: Int64 -> Int64
signum = Int64 -> Int64
forall a. Num a => a -> a
Num.signum
instance OrderedRing Int where
abs :: Int -> Int
abs = Int -> Int
forall a. Num a => a -> a
Num.abs
signum :: Int -> Int
signum = Int -> Int
forall a. Num a => a -> a
Num.signum
instance OrderedRing Integer where
abs :: Integer -> Integer
abs = Integer -> Integer
forall a. Num a => a -> a
Num.abs
signum :: Integer -> Integer
signum = Integer -> Integer
forall a. Num a => a -> a
Num.signum
instance OrderedRing (Modular Word8) where
abs :: Modular Word8 -> Modular Word8
abs Modular Word8
x = Modular Word8
x
signum :: Modular Word8 -> Modular Word8
signum (Modular Word8
x) = Word8 -> Modular Word8
forall a. a -> Modular a
Modular (Word8 -> Modular Word8)
-> (Word8 -> Word8) -> Word8 -> Modular Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word8
forall a. Num a => a -> a
Num.signum (Word8 -> Modular Word8) -> Word8 -> Modular Word8
forall a b. (a -> b) -> a -> b
$ Word8
x
instance OrderedRing (Modular Word16) where
abs :: Modular Word16 -> Modular Word16
abs Modular Word16
x = Modular Word16
x
signum :: Modular Word16 -> Modular Word16
signum (Modular Word16
x) = Word16 -> Modular Word16
forall a. a -> Modular a
Modular (Word16 -> Modular Word16)
-> (Word16 -> Word16) -> Word16 -> Modular Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Word16
forall a. Num a => a -> a
Num.signum (Word16 -> Modular Word16) -> Word16 -> Modular Word16
forall a b. (a -> b) -> a -> b
$ Word16
x
instance OrderedRing (Modular Word32) where
abs :: Modular Word32 -> Modular Word32
abs Modular Word32
x = Modular Word32
x
signum :: Modular Word32 -> Modular Word32
signum (Modular Word32
x) = Word32 -> Modular Word32
forall a. a -> Modular a
Modular (Word32 -> Modular Word32)
-> (Word32 -> Word32) -> Word32 -> Modular Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Word32
forall a. Num a => a -> a
Num.signum (Word32 -> Modular Word32) -> Word32 -> Modular Word32
forall a b. (a -> b) -> a -> b
$ Word32
x
instance OrderedRing (Modular Word64) where
abs :: Modular Word64 -> Modular Word64
abs Modular Word64
x = Modular Word64
x
signum :: Modular Word64 -> Modular Word64
signum (Modular Word64
x) = Word64 -> Modular Word64
forall a. a -> Modular a
Modular (Word64 -> Modular Word64)
-> (Word64 -> Word64) -> Word64 -> Modular Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Word64
forall a. Num a => a -> a
Num.signum (Word64 -> Modular Word64) -> Word64 -> Modular Word64
forall a b. (a -> b) -> a -> b
$ Word64
x
instance OrderedRing (Modular Word) where
abs :: Modular Word -> Modular Word
abs Modular Word
x = Modular Word
x
signum :: Modular Word -> Modular Word
signum (Modular Word
x) = Word -> Modular Word
forall a. a -> Modular a
Modular (Word -> Modular Word) -> (Word -> Word) -> Word -> Modular Word
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Word
forall a. Num a => a -> a
Num.signum (Word -> Modular Word) -> Word -> Modular Word
forall a b. (a -> b) -> a -> b
$ Word
x