tuple-th-0.2.5: Generate (non-recursive) utility functions for tuples of statically known size

Safe Haskell | None
Language | Haskell98

TupleTH

Contents

- Transformation
- Combination
  - ZipWith
- Construction
- Deconstruction
  - Right folds
  - Left folds
  - Predicates
- Monadic/applicative
- Types

Description

Note: One-tuples are currently understood as just the original type by Template Haskell (though this could be an undefined case which is not guaranteed to work this way?), so for example, we get

     $(catTuples 1 2) = \x (y,z) -> (x,y,z)

Synopsis

- mapTuple :: Int -> ExpQ
- mapTuple' :: Int -> ExpQ -> Q Exp
- filterTuple :: Int -> ExpQ
- filterTuple' :: Int -> ExpQ -> ExpQ
- reindexTuple :: Int -> [Int] -> Q Exp
- reverseTuple :: Int -> Q Exp
- rotateTuple :: Int -> Int -> Q Exp
- subtuples :: Int -> Int -> Q Exp
- deleteAtTuple :: Int -> Q Exp
- takeTuple :: Int -> Int -> Q Exp
- dropTuple :: Int -> Int -> Q Exp
- safeDeleteTuple :: Int -> Q Exp
- updateAtN :: Int -> Int -> Q Exp
- zipTuple :: Int -> Q Exp
- catTuples :: Int -> Int -> Q Exp
- uncatTuple :: Int -> Int -> Q Exp
- splitTupleAt :: Int -> Int -> Q Exp
- zipTupleWith :: Int -> ExpQ
- zipTupleWith' :: Int -> ExpQ -> ExpQ
- safeTupleFromList :: Int -> Q Exp
- tupleFromList :: Int -> Q Exp
- constTuple :: Int -> Q Exp
- proj :: Int -> Int -> ExpQ
- proj' :: Int -> Q Exp
- elemTuple :: Int -> Q Exp
- tupleToList :: Int -> Q Exp
- sumTuple :: Int -> Q Exp
- findSuccessiveElementsSatisfying :: Int -> Q Exp
- foldrTuple :: Int -> ExpQ
- foldrTuple' :: Int -> ExpQ -> ExpQ
- foldr1Tuple :: Int -> ExpQ
- foldr1Tuple' :: Int -> ExpQ -> Q Exp
- foldlTuple :: Int -> ExpQ
- foldlTuple' :: Int -> ExpQ -> ExpQ
- foldl1Tuple :: Int -> ExpQ
- foldl1Tuple' :: Int -> ExpQ -> Q Exp
- andTuple :: Int -> Q Exp
- orTuple :: Int -> Q Exp
- anyTuple :: Int -> Q Exp
- anyTuple' :: Int -> Q Exp -> Q Exp
- allTuple :: Int -> Q Exp
- allTuple' :: Int -> Q Exp -> Q Exp
- sequenceTuple :: Int -> Q Exp
- sequenceATuple :: Int -> Q Exp
- htuple :: Int -> TypeQ -> TypeQ

# Transformation

mapTuple :: Int -> ExpQ #

Type of the generated expression:

    (a -> b) -> (a, ..) -> (b, ..)

mapTuple' :: Int -> ExpQ -> Q Exp #

Takes the mapping as a quoted expression. This can sometimes produce an expression that typechecks when the analogous expression using `filterTuple` does not, e.g.:

    $(mapTuple 2) Just        ((),"foo") -- Type error
    $(mapTuple' 2 [| Just |]) ((),"foo") -- OK

filterTuple :: Int -> ExpQ #

Type of the generated expression:

    (a -> Bool) -> (a, ..) -> [a]

filterTuple' :: Int -> ExpQ -> ExpQ #

Takes the predicate as a quoted expression. See `mapTuple'` for how this can be useful.

reindexTuple :: Int -> [Int] -> Q Exp #

`reindexTuple n js` =>

    \(x_0, ..., x_{n-1}) -> (x_{js !! 0}, x_{js !! 1}, ... x_{last js})

For example,

    $(reindexTuple 3 [1,1,0,0]) ('a','b','c') == ('b','b','a','a')

Each element of `js` must be nonnegative and less than `n`.

reverseTuple :: Int -> Q Exp #

Like `reverse`.

rotateTuple :: Int -> Int -> Q Exp #

`rotateTuple n k` creates a function which rotates an `n`-tuple rightwards by `k` positions (`k` may be negative or greater than `n-1`).

subtuples :: Int -> Int -> Q Exp #

Generates the function which maps a tuple `(x_1, ..., x_n)` to the tuple of all its subtuples of the form `(x_{i_1}, ..., x_{i_k})`, where `i_1 < i_2 < ... < i_k`.

deleteAtTuple :: Int -> Q Exp #

Generates a function which takes a `Num` `i` and a homogenous tuple of size `n` and deletes the `i`-th (0-based) element of the tuple.

takeTuple :: Int -> Int -> Q Exp #

    takeTuple n i = \(x_0, ..., x_{n-1}) -> (x_0, ..., x_{i-1})

dropTuple :: Int -> Int -> Q Exp #

    dropTuple n i = \(x_0, ..., x_{n-1}) -> (x_i, ..., x_{n-1})

safeDeleteTuple :: Int -> Q Exp #

`safeDeleteTuple n` generates a function analogous to `delete` that takes an element and an `n`-tuple and maybe returns an `n-1`-tuple (if and only if the element was found).

updateAtN #

Arguments

:: Int | Length of the input tuple
-> Int | 0-based index of the element to be modified
-> Q Exp | (b -> c) -> (a1,a2,b,a3,a4) -> (a1,a2,c,a3,a4)

Generates a function modifying a single element of a tuple.

# Combination

zipTuple :: Int -> Q Exp #

Like `zip`.

Type of the generated expression:

    (a1, a2, ..) -> (b1, b2, ..) -> ((a1,b1), (a2,b2), ..)

catTuples :: Int -> Int -> Q Exp #

Type of the generated expression:

    (a1, ..) -> (b1, ..) -> (a1, .., b1, ..)

uncatTuple :: Int -> Int -> Q Exp #

    uncatTuple n m = splitTupleAt (n+m) n

`uncatTuple n m` is the inverse function of `uncurry (catTuples n m)`.

splitTupleAt :: Int -> Int -> Q Exp #

`splitTupleAt n i` => `\(x_0, ..., x_{n-1}) -> ((x_0, ..., x_{i-1}),(x_i, ..., x_{n-1})`

## ZipWith

zipTupleWith :: Int -> ExpQ #

Like `zipWith`.

Type of the generated expression:

    (a -> b -> c) -> (a, ..) -> (b, ..) -> (c, ..)

zipTupleWith' :: Int -> ExpQ -> ExpQ #

Takes the zipping function as a quoted expression. See `mapTuple'` for how this can be useful.

# Construction

safeTupleFromList :: Int -> Q Exp #

Type of the generated expression:

    [a] -> Maybe (a, ..)

tupleFromList :: Int -> Q Exp #

Type of the generated expression:

    [a] -> (a, ..)

The generated function is partial.

constTuple :: Int -> Q Exp #

# Deconstruction

proj #

Arguments

:: Int | Size of tuple
-> Int | 0-based index of component to retrieve
-> ExpQ |

    Generate a projection (like 'fst' and 'snd').

proj' :: Int -> Q Exp #

elemTuple :: Int -> Q Exp #

Like `elem`.

Type of generated expression:

    Eq a => a -> (a, ..) -> Bool

tupleToList :: Int -> Q Exp #

sumTuple :: Int -> Q Exp #

findSuccessiveElementsSatisfying :: Int -> Q Exp #

## Right folds

foldrTuple :: Int -> ExpQ #

Type of the generated expression:

    (a -> r -> r) -> r -> (a, ..) -> r

foldrTuple' :: Int -> ExpQ -> ExpQ #

Takes the folding function (but not the seed element) as a quoted expression. See `mapTuple'` for how this can be useful.

foldr1Tuple :: Int -> ExpQ #

Type of the generated expression:

    (a -> a -> a) -> (a, ..) -> a

foldr1Tuple' :: Int -> ExpQ -> Q Exp #

Takes the folding function as a quoted expression. See `mapTuple'` for how this can be useful.

## Left folds

foldlTuple :: Int -> ExpQ #

Type of the generated expression:

    (r -> a -> r) -> r -> (a, ..) -> r

foldlTuple' :: Int -> ExpQ -> ExpQ #

Takes the folding function (but not the seed element) as a quoted expression. See `mapTuple'` for how this can be useful.

foldl1Tuple :: Int -> ExpQ #

Type of the generated expression:

    (a -> a -> a) -> (a, ..) -> a

foldl1Tuple' :: Int -> ExpQ -> Q Exp #

Takes the folding function as a quoted expression. See `mapTuple'` for how this can be useful.

## Predicates

andTuple :: Int -> Q Exp #

Like `and`.

orTuple :: Int -> Q Exp #

Like `or`.

anyTuple :: Int -> Q Exp #

Like `any`.

anyTuple' :: Int -> Q Exp -> Q Exp #

allTuple :: Int -> Q Exp #

Like `all`.

allTuple' :: Int -> Q Exp -> Q Exp #

# Monadic/applicative

sequenceTuple :: Int -> Q Exp #

Like `sequence`.

sequenceATuple :: Int -> Q Exp #

Like `sequenceA`.

# Types

htuple :: Int -> TypeQ -> TypeQ #

Makes a homogenous tuple type of the given size and element type

    $(htuple 2) [t| Char |] = (Char,Char)