{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
module Formatting.Internal
( Format(..)
, (%)
, (%.)
, now
, bind
, mapf
, later
, format
, sformat
, bprint
, bformat
, fprint
, fprintLn
, hprint
, hprintLn
, formatToString
) where
import Control.Category (Category(..))
import Data.Monoid
import Data.String
import qualified Data.Text as S (Text)
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as T
import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Builder (Builder)
import qualified Data.Text.Lazy.Builder as T
import qualified Data.Text.Lazy.Builder as TLB
import qualified Data.Text.Lazy.IO as T
import Prelude hiding ((.),id)
import System.IO
newtype Format r a =
Format {Format r a -> (Builder -> r) -> a
runFormat :: (Builder -> r) -> a}
instance Functor (Format r) where
fmap :: (a -> b) -> Format r a -> Format r b
fmap f :: a -> b
f (Format k :: (Builder -> r) -> a
k) = ((Builder -> r) -> b) -> Format r b
forall r a. ((Builder -> r) -> a) -> Format r a
Format (a -> b
f (a -> b) -> ((Builder -> r) -> a) -> (Builder -> r) -> b
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Builder -> r) -> a
k)
instance Semigroup (Format r (a -> r)) where
m :: Format r (a -> r)
m <> :: Format r (a -> r) -> Format r (a -> r) -> Format r (a -> r)
<> n :: Format r (a -> r)
n =
((Builder -> r) -> a -> r) -> Format r (a -> r)
forall r a. ((Builder -> r) -> a) -> Format r a
Format (\k :: Builder -> r
k a :: a
a ->
Format r (a -> r) -> (Builder -> r) -> a -> r
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format r (a -> r)
m (\b1 :: Builder
b1 -> Format r (a -> r) -> (Builder -> r) -> a -> r
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format r (a -> r)
n (\b2 :: Builder
b2 -> Builder -> r
k (Builder
b1 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
b2)) a
a) a
a)
instance Monoid (Format r (a -> r)) where
mempty :: Format r (a -> r)
mempty = ((Builder -> r) -> a -> r) -> Format r (a -> r)
forall r a. ((Builder -> r) -> a) -> Format r a
Format (\k :: Builder -> r
k _ -> Builder -> r
k Builder
forall a. Monoid a => a
mempty)
instance (a ~ r) => IsString (Format r a) where
fromString :: String -> Format r a
fromString = Builder -> Format r r
forall r. Builder -> Format r r
now (Builder -> Format r r)
-> (String -> Builder) -> String -> Format r r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> Builder
forall a. IsString a => String -> a
fromString
instance Category Format where
id :: Format a a
id = Builder -> Format a a
forall r. Builder -> Format r r
now Builder
forall a. Monoid a => a
mempty
f :: Format b c
f . :: Format b c -> Format a b -> Format a c
. g :: Format a b
g =
Format b c
f Format b c -> (Builder -> Format a b) -> Format a c
forall r a r'.
Format r a -> (Builder -> Format r' r) -> Format r' a
`bind`
\a :: Builder
a ->
Format a b
g Format a b -> (Builder -> Format a a) -> Format a b
forall r a r'.
Format r a -> (Builder -> Format r' r) -> Format r' a
`bind`
\b :: Builder
b -> Builder -> Format a a
forall r. Builder -> Format r r
now (Builder
a Builder -> Builder -> Builder
forall a. Monoid a => a -> a -> a
`mappend` Builder
b)
(%) :: Format r a -> Format r' r -> Format r' a
% :: Format r a -> Format r' r -> Format r' a
(%) = Format r a -> Format r' r -> Format r' a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
(.)
infixr 9 %
(%.) :: Format r (Builder -> r') -> Format r' a -> Format r a
%. :: Format r (Builder -> r') -> Format r' a -> Format r a
(%.) (Format a :: (Builder -> r) -> Builder -> r'
a) (Format b :: (Builder -> r') -> a
b) = ((Builder -> r) -> a) -> Format r a
forall r a. ((Builder -> r) -> a) -> Format r a
Format ((Builder -> r') -> a
b ((Builder -> r') -> a)
-> ((Builder -> r) -> Builder -> r') -> (Builder -> r) -> a
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Builder -> r) -> Builder -> r'
a)
infixr 8 %.
now :: Builder -> Format r r
now :: Builder -> Format r r
now a :: Builder
a = ((Builder -> r) -> r) -> Format r r
forall r a. ((Builder -> r) -> a) -> Format r a
Format ((Builder -> r) -> Builder -> r
forall a b. (a -> b) -> a -> b
$ Builder
a)
bind :: Format r a -> (Builder -> Format r' r) -> Format r' a
m :: Format r a
m bind :: Format r a -> (Builder -> Format r' r) -> Format r' a
`bind` f :: Builder -> Format r' r
f = ((Builder -> r') -> a) -> Format r' a
forall r a. ((Builder -> r) -> a) -> Format r a
Format (((Builder -> r') -> a) -> Format r' a)
-> ((Builder -> r') -> a) -> Format r' a
forall a b. (a -> b) -> a -> b
$ \k :: Builder -> r'
k -> Format r a -> (Builder -> r) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format r a
m (\a :: Builder
a -> Format r' r -> (Builder -> r') -> r
forall r a. Format r a -> (Builder -> r) -> a
runFormat (Builder -> Format r' r
f Builder
a) Builder -> r'
k)
mapf :: (a -> b) -> Format r (b -> t) -> Format r (a -> t)
mapf :: (a -> b) -> Format r (b -> t) -> Format r (a -> t)
mapf f :: a -> b
f m :: Format r (b -> t)
m = ((Builder -> r) -> a -> t) -> Format r (a -> t)
forall r a. ((Builder -> r) -> a) -> Format r a
Format (\k :: Builder -> r
k -> Format r (b -> t) -> (Builder -> r) -> b -> t
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format r (b -> t)
m Builder -> r
k (b -> t) -> (a -> b) -> a -> t
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
f)
later :: (a -> Builder) -> Format r (a -> r)
later :: (a -> Builder) -> Format r (a -> r)
later f :: a -> Builder
f = ((Builder -> r) -> a -> r) -> Format r (a -> r)
forall r a. ((Builder -> r) -> a) -> Format r a
Format ((Builder -> r) -> (a -> Builder) -> a -> r
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Builder
f)
format :: Format Text a -> a
format :: Format Text a -> a
format m :: Format Text a
m = Format Text a -> (Builder -> Text) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format Text a
m Builder -> Text
T.toLazyText
sformat :: Format S.Text a -> a
sformat :: Format Text a -> a
sformat m :: Format Text a
m = Format Text a -> (Builder -> Text) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format Text a
m (Text -> Text
T.toStrict (Text -> Text) -> (Builder -> Text) -> Builder -> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
T.toLazyText)
bprint :: Format Builder a -> a
bprint :: Format Builder a -> a
bprint m :: Format Builder a
m = Format Builder a -> (Builder -> Builder) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format Builder a
m Builder -> Builder
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
bformat :: Format Builder a -> a
bformat :: Format Builder a -> a
bformat m :: Format Builder a
m = Format Builder a -> (Builder -> Builder) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format Builder a
m Builder -> Builder
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
fprint :: Format (IO ()) a -> a
fprint :: Format (IO ()) a -> a
fprint m :: Format (IO ()) a
m = Format (IO ()) a -> (Builder -> IO ()) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format (IO ()) a
m (Text -> IO ()
T.putStr (Text -> IO ()) -> (Builder -> Text) -> Builder -> IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
T.toLazyText)
fprintLn :: Format (IO ()) a -> a
fprintLn :: Format (IO ()) a -> a
fprintLn m :: Format (IO ()) a
m = Format (IO ()) a -> (Builder -> IO ()) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format (IO ()) a
m (Text -> IO ()
T.putStrLn (Text -> IO ()) -> (Builder -> Text) -> Builder -> IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
T.toLazyText)
hprint :: Handle -> Format (IO ()) a -> a
hprint :: Handle -> Format (IO ()) a -> a
hprint h :: Handle
h m :: Format (IO ()) a
m = Format (IO ()) a -> (Builder -> IO ()) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format (IO ()) a
m (Handle -> Text -> IO ()
T.hPutStr Handle
h (Text -> IO ()) -> (Builder -> Text) -> Builder -> IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
T.toLazyText)
hprintLn :: Handle -> Format (IO ()) a -> a
hprintLn :: Handle -> Format (IO ()) a -> a
hprintLn h :: Handle
h m :: Format (IO ()) a
m = Format (IO ()) a -> (Builder -> IO ()) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format (IO ()) a
m (Handle -> Text -> IO ()
T.hPutStrLn Handle
h (Text -> IO ()) -> (Builder -> Text) -> Builder -> IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
T.toLazyText)
formatToString :: Format String a -> a
formatToString :: Format String a -> a
formatToString m :: Format String a
m = Format String a -> (Builder -> String) -> a
forall r a. Format r a -> (Builder -> r) -> a
runFormat Format String a
m (Text -> String
TL.unpack (Text -> String) -> (Builder -> Text) -> Builder -> String
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Builder -> Text
TLB.toLazyText)