repr-0.1: Render numeric expressions to their textual representation.




data Repr a Source

Repr a is a value of type a paired with a way to render that value to a string which will contain a representation of the value.

Note that Repr a is overloaded for all the numeric classes provided that a has instances for the respected classes. This allows you to write a numeric expression of type Repr a. For example:

 *Repr> let rd = 1.5 + 2 + (3 + (-4) * (5 - pi / sqrt 6)) :: Repr Double

You can extract the value of rd:

 *Repr> value rd

And you can than render rd to its textual representation:

 *Repr> render rd
 "fromRational (3 % 2) + fromInteger 2 + (fromInteger 3 + negate (fromInteger 4) * (fromInteger 5 - pi / sqrt (fromInteger 6)))"


Enum a => Enum (Repr a) 
Eq a => Eq (Repr a) 
Floating a => Floating (Repr a) 
Fractional a => Fractional (Repr a) 
Integral a => Integral (Repr a) 
Num a => Num (Repr a) 
Ord a => Ord (Repr a) 
Real a => Real (Repr a) 
RealFloat a => RealFloat (Repr a) 
RealFrac a => RealFrac (Repr a) 
Show (Repr a) 
IsString a => IsString (Repr a) 

value :: Repr a -> aSource

Extract the value of the Repr.

renderer :: Repr a -> RendererSource

Extrac the renderer of the Repr.

type Renderer = Precedence -> Fixity -> DStringSource


For more documentation about precedence and fixity see:

The reason the renderer returns a DString instead of for example a String is that the rendering of numeric expression involves lots of left-factored appends i.e.: ((a ++ b) ++ c) ++ d. A DString has a O(1) append operation while a String just has a O(n) append. So choosing a DString is more efficient.

type Precedence = IntSource

The precedence of operators and function application.

  • Function application always has precedence 10.

data Fixity Source

Fixity of operators.



No fixity information.


Left associative operator.


Right associative operator.


render :: Repr a -> StringSource

Render a top-level value to a String.

Note that: render r = toString $ renderer r 0 Non

(<?>) :: Repr a -> DString -> Repr aSource







The rendering will then look like:
