A code-generation system for complex typeclass default-implementation configurations. There are usage examples in this package's source distribution[1] and in the random-source package[2].

- data Defaults s a
- scoreBy :: (a -> b) -> Defaults a t -> Defaults b t
- data Function s a
- function :: String -> Function s a -> Defaults s a
- requireFunction :: String -> Defaults s ()
- data Implementation s a
- implementation :: Implementation s (Q [Dec]) -> Function s ()
- score :: s -> Implementation s ()
- cost :: Num s => s -> Implementation s ()
- dependsOn :: String -> Implementation s ()
- inline :: Implementation s ()
- noinline :: Implementation s ()
- withDefaults :: (Monoid s, Ord s) => Defaults s () -> Q [Dec] -> Q [Dec]
- implementDefaults :: (Ord s, Monoid s) => Defaults s () -> [Dec] -> Q [Dec]

# Documentation

A description of a system of `Function`

s and default `Implementation`

s
which can be used to complete a partial implementation of some type class.

scoreBy :: (a -> b) -> Defaults a t -> Defaults b tSource

Map a function over all scores. This function's name comes from the
following idiom (where `Sum`

is replaced by whatever monoid-constructor
you want to use to combine scores):

foo = scoreBy Sum $ do ...

A representation of a function for which one or more default
`Implementation`

s exist. Defined using the `function`

function.

function :: String -> Function s a -> Defaults s aSource

Declare a function that must be implemented, and provide a description of any default implementations which can be used.

requireFunction :: String -> Defaults s ()Source

State that a function must be implemented but has no default implementation.

data Implementation s a Source

A representation of a single possible implementation of a `Function`

. Defined
using the `implementation`

function.

Monad (Implementation s) | |

Functor (Implementation s) | |

Applicative (Implementation s) |

implementation :: Implementation s (Q [Dec]) -> Function s ()Source

Describe a default implementation of the current function

score :: s -> Implementation s ()Source

cost :: Num s => s -> Implementation s ()Source

dependsOn :: String -> Implementation s ()Source

Specify that the current implementation must not be used unless the given function is already defined. If this implementation can be used mutually-recursively with _ALL_ potential implementations of some other function, then a dependency need not be declared on that function.

inline :: Implementation s ()Source

Specify that an `Implementation`

should be annotated with an INLINE pragma.
Under GHC versions earlier than 6.12 this is a no-op, because those Template
Haskell implementations do not support pragmas.

noinline :: Implementation s ()Source

Specify that an `Implementation`

should be annotated with a NOINLINE pragma.
Under GHC versions earlier than 6.12 this is a no-op, because those Template
Haskell implementations do not support pragmas.

withDefaults :: (Monoid s, Ord s) => Defaults s () -> Q [Dec] -> Q [Dec]Source

Given a `Q [Dec]`

containing an instance declaration, complete that instance
declaration using the given `Defaults`

specification. Typical usage would be
along the lines of the following:

$(withDefaults fooDefaults [d| instance Foo t where {- ... -} |])