flexible-defaults-0.0.1.2: Generate default function implementations for complex type classes.

Safe HaskellNone
LanguageHaskell98

Language.Haskell.TH.FlexibleDefaults

Description

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].

  1. https://github.com/mokus0/flexible-defaults/tree/master/examples
  2. https://github.com/mokus0/random-fu/blob/master/random-source/src/Data/Random/Internal/TH.hs

Synopsis

Documentation

data Defaults s a Source

A description of a system of Functions and default Implementations which can be used to complete a partial implementation of some type class.

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

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
   ...

data Function s a Source

A representation of a function for which one or more default Implementations exist. Defined using the function function.

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

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.

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

Describe a default implementation of the current function

score :: s -> Implementation s () Source

Specify the score associated with the current implementation. Only one invocation of either score or cost may be used per implementation.

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

Specify the cost (negated score) associated with the current implementation. Only one invocation of either score or cost may be used per implementation.

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 {- ... -} |])

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

Given a partial list of function declarations, complete that list based on the Defaults specification given.