autoapply: Template Haskell to automatically pass values to functions

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Warnings:

See readme.md


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.2.0.0, 0.3, 0.4, 0.4.1, 0.4.1.1
Change log changelog.md
Dependencies base (>=4.13 && <5), logict, mtl, template-haskell, th-desugar (>=1.10), transformers, unification-fd [details]
License BSD-3-Clause
Copyright (c) 2020 Joe Hermaszewski
Author
Maintainer Joe Hermaszewski <if.it.fits.i.sits@monoid.al>
Category Template Haskell
Home page https://github.com/expipiplus1/autoapply#readme
Bug tracker https://github.com/expipiplus1/autoapply/issues
Source repo head: git clone https://github.com/expipiplus1/autoapply
Uploaded by jophish at 2020-04-26T07:51:19Z

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for autoapply-0.1.0.0

[back to package description]

autoapply

A Template-Haskell program to automatically pass arguments to functions wherever the type fits.

TL;DR

You have the following values and want to stir them together and see what sticks.

$(autoApply ['getA, 'myC] 'foo) will create \b -> getA >>= \a -> foo a b myC which has type B -> App D

or

autoApplyDecs reverse ['getA, 'myC] ['foo] will create oof :: B -> App D; oof b = do { a <- getA; foo a b myC }

Why to use it

One nice use-case is to avoiding writing boilerplate wrappers for using an API in your Monad stack. For instance imagine the following API.

data Instance; data ExtraOpenInfo; data Foo; data Bar; data Handle
openHandle  :: MonadIO m => Instance -> Maybe ExtraOpenInfo -> m Handle
closeHandle :: MonadIO m => Instance -> Handle -> m ()
useHandle   :: MonadIO m => Instance -> Handle -> Foo -> m Bar

You'd like to use this in your polysemy application, using the Input effect to pass the Instance handle around, and always passing Nothing for ExtraOpenInfo because you don't use that functionality and getting a Foo from some other constraint MyConstraint. You define the following values.

myExtraOpenInfo :: Maybe ExtraOpenInfo
myExtraOpenInfo = Nothing
getInstance :: Member (Input Instance) r => Sem r Instance
getInstance = input
getFoo :: MyConstraint m => m Foo
getFoo = ...

You then create the wrapped API thusly:

autoapplyDecs
  (<> "'") -- Function to transform the names of the wrapped functions
  ['myExtraOpenInfo, 'getInstance, 'getFoo] -- Potential arguments to pass
  ['openHandle, 'closeHandle, 'useHandle] -- Functions to wrap

Which creates the following declarations:

openHandle'
  :: (Member (Input Instance) r, MonadIO (Sem r)) => Sem r Handle
closeHandle'
  :: (Member (Input Instance) r, MonadIO (Sem r)) => Handle -> Sem r ()
useHandle'
  :: (Member (Input Instance) r, MyConstraint (Sem r), MonadIO (Sem r))
  => Handle -> Sem r Bar

Notice:

To see the generated code (it's exactly what you'd expect) compile test/Types.hs with -ddump-splices.

How to use this

To generate a new top-level declaration you'll need:

The new declaration will be generated, equal to the wrapped one but using the supplied arguments wherever possible.

Arguments can be used in two ways:

It's important to note that Monad instance checking only goes as far as template-haskell's reifyInstances. i.e. only the instance heads are checked.

Aside for checking for a Monad instance, no constraints are checked. So autoapply will happily pass reverse to (+) yielding a value of type Num ([a] -> [a]) => [a] -> [a].

Monadic binds are performed in the order of arguments passed to the wrapped function, and will be performed more than once if the argument is used multiple times.

You may want to either type your generated declarations manually (putting the type after the splice) or turn on -XNoMonomorphismRestriction if your arguments have polymorphic constraints.

Where to use it

See also

This has a similar feel to some other programs which also generate Haskell expressions based on types.

There are a couple of differences here: