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

Safe Haskell | None
Language | Haskell98

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

- 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

data Defaults s a Source #

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.

#### Instances

Instances details

Applicative (Defaults s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods pure :: a -> Defaults s a Source # (<*>) :: Defaults s (a -> b) -> Defaults s a -> Defaults s b Source # liftA2 :: (a -> b -> c) -> Defaults s a -> Defaults s b -> Defaults s c Source # (*>) :: Defaults s a -> Defaults s b -> Defaults s b Source # (<*) :: Defaults s a -> Defaults s b -> Defaults s a Source #
Functor (Defaults s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods fmap :: (a -> b) -> Defaults s a -> Defaults s b Source # (<$) :: a -> Defaults s b -> Defaults s a Source #
Monad (Defaults s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods (>>=) :: Defaults s a -> (a -> Defaults s b) -> Defaults s b Source # (>>) :: Defaults s a -> Defaults s b -> Defaults s b Source # return :: a -> Defaults s a Source #

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 `Implementation`s exist. Defined using the `function` function.

#### Instances

Instances details

Applicative (Function s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods pure :: a -> Function s a Source # (<*>) :: Function s (a -> b) -> Function s a -> Function s b Source # liftA2 :: (a -> b -> c) -> Function s a -> Function s b -> Function s c Source # (*>) :: Function s a -> Function s b -> Function s b Source # (<*) :: Function s a -> Function s b -> Function s a Source #
Functor (Function s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods fmap :: (a -> b) -> Function s a -> Function s b Source # (<$) :: a -> Function s b -> Function s a Source #
Monad (Function s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods (>>=) :: Function s a -> (a -> Function s b) -> Function s b Source # (>>) :: Function s a -> Function s b -> Function s b Source # return :: a -> Function s a Source #

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.

#### Instances

Instances details

Applicative (Implementation s) Source # |

Instance details

Defined in Language.Haskell.TH.FlexibleDefaults.DSL

Methods

pure :: a -> Implementation s a Source #

(<*>) :: Implementation s (a -> b) -> Implementation s a -> Implementation s b Source #

liftA2 :: (a -> b -> c) -> Implementation s a -> Implementation s b -> Implementation s c Source #

(*>) :: Implementation s a -> Implementation s b -> Implementation s b Source #

(<*) :: Implementation s a -> Implementation s b -> Implementation s a Source #

Functor (Implementation s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods fmap :: (a -> b) -> Implementation s a -> Implementation s b Source # (<$) :: a -> Implementation s b -> Implementation s a Source #
Monad (Implementation s) Source # |
Instance details Defined in Language.Haskell.TH.FlexibleDefaults.DSL Methods (>>=) :: Implementation s a -> (a -> Implementation s b) -> Implementation s b Source # (>>) :: Implementation s a -> Implementation s b -> Implementation s b Source # return :: a -> Implementation s a Source #

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.