{-# LANGUAGE CPP #-}
{-# LANGUAGE UnicodeSyntax #-}
{-# LANGUAGE DeriveDataTypeable #-}
#if __GLASGOW_HASKELL__ >= 706
{-# LANGUAGE DeriveGeneric #-}
#endif
module Text.Printer.Fractional
(
PositionalSystem(..)
, BitSystem(..)
, Binary(..)
, Octal(..)
, Decimal(..)
, Hexadecimal(..)
, LowHex(..)
, UpHex(..)
, Optional(..)
, isOptional
, isRequired
, fraction'
, fraction
) where
#if __GLASGOW_HASKELL__ >= 706
import GHC.Generics (Generic)
#endif
import Data.Typeable (Typeable)
import Data.Ix (Ix)
import Data.Monoid (mempty)
import Data.Ratio (numerator, denominator)
import Text.Printer
import Text.Printer.Integral
data Optional = Optional
| Required
deriving ( Typeable
#if __GLASGOW_HASKELL__ >= 706
, (forall x. Optional -> Rep Optional x)
-> (forall x. Rep Optional x -> Optional) -> Generic Optional
forall x. Rep Optional x -> Optional
forall x. Optional -> Rep Optional x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Optional -> Rep Optional x
from :: forall x. Optional -> Rep Optional x
$cto :: forall x. Rep Optional x -> Optional
to :: forall x. Rep Optional x -> Optional
Generic
#endif
, Int -> Optional -> ShowS
[Optional] -> ShowS
Optional -> String
(Int -> Optional -> ShowS)
-> (Optional -> String) -> ([Optional] -> ShowS) -> Show Optional
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Optional -> ShowS
showsPrec :: Int -> Optional -> ShowS
$cshow :: Optional -> String
show :: Optional -> String
$cshowList :: [Optional] -> ShowS
showList :: [Optional] -> ShowS
Show, ReadPrec [Optional]
ReadPrec Optional
Int -> ReadS Optional
ReadS [Optional]
(Int -> ReadS Optional)
-> ReadS [Optional]
-> ReadPrec Optional
-> ReadPrec [Optional]
-> Read Optional
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Optional
readsPrec :: Int -> ReadS Optional
$creadList :: ReadS [Optional]
readList :: ReadS [Optional]
$creadPrec :: ReadPrec Optional
readPrec :: ReadPrec Optional
$creadListPrec :: ReadPrec [Optional]
readListPrec :: ReadPrec [Optional]
Read, Optional -> Optional -> Bool
(Optional -> Optional -> Bool)
-> (Optional -> Optional -> Bool) -> Eq Optional
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Optional -> Optional -> Bool
== :: Optional -> Optional -> Bool
$c/= :: Optional -> Optional -> Bool
/= :: Optional -> Optional -> Bool
Eq, Eq Optional
Eq Optional =>
(Optional -> Optional -> Ordering)
-> (Optional -> Optional -> Bool)
-> (Optional -> Optional -> Bool)
-> (Optional -> Optional -> Bool)
-> (Optional -> Optional -> Bool)
-> (Optional -> Optional -> Optional)
-> (Optional -> Optional -> Optional)
-> Ord Optional
Optional -> Optional -> Bool
Optional -> Optional -> Ordering
Optional -> Optional -> Optional
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
$ccompare :: Optional -> Optional -> Ordering
compare :: Optional -> Optional -> Ordering
$c< :: Optional -> Optional -> Bool
< :: Optional -> Optional -> Bool
$c<= :: Optional -> Optional -> Bool
<= :: Optional -> Optional -> Bool
$c> :: Optional -> Optional -> Bool
> :: Optional -> Optional -> Bool
$c>= :: Optional -> Optional -> Bool
>= :: Optional -> Optional -> Bool
$cmax :: Optional -> Optional -> Optional
max :: Optional -> Optional -> Optional
$cmin :: Optional -> Optional -> Optional
min :: Optional -> Optional -> Optional
Ord, Int -> Optional
Optional -> Int
Optional -> [Optional]
Optional -> Optional
Optional -> Optional -> [Optional]
Optional -> Optional -> Optional -> [Optional]
(Optional -> Optional)
-> (Optional -> Optional)
-> (Int -> Optional)
-> (Optional -> Int)
-> (Optional -> [Optional])
-> (Optional -> Optional -> [Optional])
-> (Optional -> Optional -> [Optional])
-> (Optional -> Optional -> Optional -> [Optional])
-> Enum Optional
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Optional -> Optional
succ :: Optional -> Optional
$cpred :: Optional -> Optional
pred :: Optional -> Optional
$ctoEnum :: Int -> Optional
toEnum :: Int -> Optional
$cfromEnum :: Optional -> Int
fromEnum :: Optional -> Int
$cenumFrom :: Optional -> [Optional]
enumFrom :: Optional -> [Optional]
$cenumFromThen :: Optional -> Optional -> [Optional]
enumFromThen :: Optional -> Optional -> [Optional]
$cenumFromTo :: Optional -> Optional -> [Optional]
enumFromTo :: Optional -> Optional -> [Optional]
$cenumFromThenTo :: Optional -> Optional -> Optional -> [Optional]
enumFromThenTo :: Optional -> Optional -> Optional -> [Optional]
Enum, Optional
Optional -> Optional -> Bounded Optional
forall a. a -> a -> Bounded a
$cminBound :: Optional
minBound :: Optional
$cmaxBound :: Optional
maxBound :: Optional
Bounded, Ord Optional
Ord Optional =>
((Optional, Optional) -> [Optional])
-> ((Optional, Optional) -> Optional -> Int)
-> ((Optional, Optional) -> Optional -> Int)
-> ((Optional, Optional) -> Optional -> Bool)
-> ((Optional, Optional) -> Int)
-> ((Optional, Optional) -> Int)
-> Ix Optional
(Optional, Optional) -> Int
(Optional, Optional) -> [Optional]
(Optional, Optional) -> Optional -> Bool
(Optional, Optional) -> Optional -> Int
forall a.
Ord a =>
((a, a) -> [a])
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Bool)
-> ((a, a) -> Int)
-> ((a, a) -> Int)
-> Ix a
$crange :: (Optional, Optional) -> [Optional]
range :: (Optional, Optional) -> [Optional]
$cindex :: (Optional, Optional) -> Optional -> Int
index :: (Optional, Optional) -> Optional -> Int
$cunsafeIndex :: (Optional, Optional) -> Optional -> Int
unsafeIndex :: (Optional, Optional) -> Optional -> Int
$cinRange :: (Optional, Optional) -> Optional -> Bool
inRange :: (Optional, Optional) -> Optional -> Bool
$crangeSize :: (Optional, Optional) -> Int
rangeSize :: (Optional, Optional) -> Int
$cunsafeRangeSize :: (Optional, Optional) -> Int
unsafeRangeSize :: (Optional, Optional) -> Int
Ix)
isOptional ∷ Optional → Bool
isOptional :: Optional -> Bool
isOptional Optional
Optional = Bool
True
isOptional Optional
Required = Bool
False
isRequired ∷ Optional → Bool
isRequired :: Optional -> Bool
isRequired Optional
Optional = Bool
False
isRequired Optional
Required = Bool
True
fraction' ∷ (PositionalSystem s, Real α, Printer p)
⇒ s
→ p
→ p
→ p
→ p
→ Optional
→ α → p
fraction' :: forall s α p.
(PositionalSystem s, Real α, Printer p) =>
s -> p -> p -> p -> p -> Optional -> α -> p
fraction' s
s p
neg p
z p
pos p
sep Optional
i α
a
| Integer
n Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = p
z
| Integer
d Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1 = case Optional
i of
Optional
Optional → s -> p -> p -> p -> Integer -> p
forall s α p.
(PositionalSystem s, Integral α, Printer p) =>
s -> p -> p -> p -> α -> p
number' s
s p
neg p
z p
pos Integer
n
Optional
Required → s -> p -> p -> p -> Integer -> p
forall s α p.
(PositionalSystem s, Integral α, Printer p) =>
s -> p -> p -> p -> α -> p
number' s
s p
neg p
z p
pos Integer
n
p -> p -> p
forall a. Semigroup a => a -> a -> a
<> p
sep
p -> p -> p
forall a. Semigroup a => a -> a -> a
<> (s -> Char -> p
forall p. Printer p => s -> Char -> p
forall s p. (PositionalSystem s, Printer p) => s -> Char -> p
printDigitIn s
s (Char -> p) -> Char -> p
forall a b. (a -> b) -> a -> b
$! s -> Int -> Char
forall s. PositionalSystem s => s -> Int -> Char
intToDigitIn s
s Int
1)
| Bool
otherwise = if Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0
then p
neg p -> p -> p
forall a. Semigroup a => a -> a -> a
<> s -> Integer -> p
forall s α p.
(PositionalSystem s, Integral α, Printer p) =>
s -> α -> p
nonPositive s
s Integer
n p -> p -> p
forall a. Semigroup a => a -> a -> a
<> p
sep p -> p -> p
forall a. Semigroup a => a -> a -> a
<> s -> Integer -> p
forall s α p.
(PositionalSystem s, Integral α, Printer p) =>
s -> α -> p
nonNegative s
s Integer
d
else p
pos p -> p -> p
forall a. Semigroup a => a -> a -> a
<> s -> Integer -> p
forall s α p.
(PositionalSystem s, Integral α, Printer p) =>
s -> α -> p
nonNegative s
s Integer
n p -> p -> p
forall a. Semigroup a => a -> a -> a
<> p
sep p -> p -> p
forall a. Semigroup a => a -> a -> a
<> s -> Integer -> p
forall s α p.
(PositionalSystem s, Integral α, Printer p) =>
s -> α -> p
nonNegative s
s Integer
d
where r :: Rational
r = α -> Rational
forall a. Real a => a -> Rational
toRational α
a
n :: Integer
n = Rational -> Integer
forall a. Ratio a -> a
numerator Rational
r
d :: Integer
d = Rational -> Integer
forall a. Ratio a -> a
denominator Rational
r
fraction ∷ (Real α, Printer p) ⇒ α → p
fraction :: forall α p. (Real α, Printer p) => α -> p
fraction = Decimal -> p -> p -> p -> p -> Optional -> α -> p
forall s α p.
(PositionalSystem s, Real α, Printer p) =>
s -> p -> p -> p -> p -> Optional -> α -> p
fraction' Decimal
Decimal (Char -> p
forall p. Printer p => Char -> p
char7 Char
'-') (Char -> p
forall p. Printer p => Char -> p
char7 Char
'0')
p
forall a. Monoid a => a
mempty (Char -> p
forall p. Printer p => Char -> p
char7 Char
'/') Optional
Optional