category-printf-0.1.0.0: Highbrow approach to type-safe printf format specifications.

Safe HaskellSafe-Inferred
LanguageHaskell2010

Control.Category.Printf

Synopsis

Documentation

type Format m = Cokleisli ((->) m) Source

Handy type synonym for the things we're working with.

printfWith :: (m -> r) -> Format m r b -> b Source

You should regard a value of type Format m a b as something which explains how to write some element of the monoid m (a "string" for our purposes), and which will change the type of printf from a to b. For instance, something which adds a responsibility to provide an additional argument of type t might have type Format m a (t -> a), while a formatter which somehow absolves you of that responsibility would have type Format m (t -> a) a.

We can apply this to something like putStrLn to get a function for formatted printing. Typically you'll have r = IO (), but that needn't be the case.

sprintf :: Format m m b -> b Source

If you just want to build a string / element of your monoid, we have sprintf = printfWith id

c :: Monoid m => m -> Format m a a Source

Formatter for a constant string.

i :: Format m a (m -> a) Source

Inclusion of a string directly.

spliceWith :: Monoid m => (t -> m) -> Format m a (t -> a) Source

Given a way to turn a value of type t into a string, this builds a formatter which demands an additional argument of type t and splices it in.

s :: (Monoid s, IsString s, Show t) => Format s a (t -> a) Source

Splice in anything showable.

generalizeString :: (IsString s, Monoid s) => Format String a b -> Format s a b Source

Generalizes the string type that a formatter uses by applying fromString internally.

intAtBase :: (Real t, Integral t, Show t, Monoid s, IsString s) => t -> (Int -> Char) -> Format s a (t -> a) Source

Show an integral value using the given base, and using the provided function to determine how to display individual digits.

hex :: (Integral t, Show t, Monoid s, IsString s) => Format s a (t -> a) Source

Show an integral value in hexadecimal.

oct :: (Integral t, Show t, Monoid s, IsString s) => Format s a (t -> a) Source

Show an integral value in octal.

eFloat :: (RealFloat t, Monoid s, IsString s) => Maybe Int -> Format s a (t -> a) Source

Show a floating point value in exponential format. (e.g. 2.45e2, -1.5e-3) If digs is Nothing, the value is shown to full precision, if it is Just d then at most d digits after the decimal point are shown.

fFloat :: (RealFloat t, Monoid s, IsString s) => Maybe Int -> Format s a (t -> a) Source

Show a floating point value in standard decimal format. (e.g. 245000, -0.0015) If digs is Nothing, the value is shown to full precision, if it is Just d then at most d digits after the decimal point are shown.

gFloat :: (RealFloat t, Monoid s, IsString s) => Maybe Int -> Format s a (t -> a) Source

Show a floating point value using standard decimal notation for arguments whose absolute value lies between 0.1 and 9,999,999, and scientific notation otherwise. If digs is Nothing, the value is shown to full precision, if it is Just d then at most d digits after the decimal point are shown.

push :: Monoid m => t -> Format m (t -> a) a Source

We can use arr from the Arrow instance for Cokleisli w to produce formatters that manipulate the stack without printing. That is, we have

arr :: (Monoid m) => (s -> s') -> Format m s s'

Push an argument onto the stack to be consumed by subsequent formatters.

dup :: Monoid m => Format m (t -> t -> a) (t -> a) Source

Duplicate an argument on the stack, making it available twice.

swap :: Monoid m => Format m (t -> t' -> a) (t' -> t -> a) Source

Swap the next two arguments on the stack.

skip :: Monoid m => Format m (t -> a) (t' -> t -> a) Source

Skip the next argument on the stack.

apply :: Monoid m => (u -> v) -> Format m (v -> a) (u -> a) Source

Apply a function to the argument on the top of the stack.

apply2 :: Monoid m => (u -> v -> w) -> Format m (w -> a) (u -> v -> a) Source

Apply a binary function to the top two arguments on the stack.