Safe Haskell | None |
---|---|

Language | Haskell2010 |

This module exports various functions that work with n-ary tuples.

Generally speaking all functions can be applied to tuples of different sizes.

However, to improve type inference we have limited our implementation to only only work on tuples of at most size 10.

For functions working with indexes we have also added some convenience functions to avoid usage of Proxy's.

- sel :: Select s t => s -> t
- selN :: SelectN s n t => s -> Proxy n -> t
- sel1 :: SelectN s 0 t => s -> t
- sel2 :: SelectN s 1 t => s -> t
- sel3 :: SelectN s 2 t => s -> t
- sel4 :: SelectN s 3 t => s -> t
- sel5 :: SelectN s 4 t => s -> t
- sel6 :: SelectN s 5 t => s -> t
- sel7 :: SelectN s 6 t => s -> t
- sel8 :: SelectN s 7 t => s -> t
- sel9 :: SelectN s 8 t => s -> t
- sel10 :: SelectN s 9 t => s -> t
- lastT :: forall s n t. (LengthT s ~ n, SelectN s (n - 1) t) => s -> t
- app :: App f s t => f -> s -> t
- appPoly :: App (Poly a b) s t => (a -> b) -> s -> t
- appN :: AppN f s n t => f -> s -> Proxy n -> t
- appF :: AppF f s t => f -> s -> t
- mapT :: MapT f s t => f -> s -> t
- mapPolyT :: MapT (Poly a b) s t => (a -> b) -> s -> t
- app1 :: AppN f s 0 t => f -> s -> t
- app2 :: AppN f s 1 t => f -> s -> t
- app3 :: AppN f s 2 t => f -> s -> t
- app4 :: AppN f s 3 t => f -> s -> t
- app5 :: AppN f s 4 t => f -> s -> t
- app6 :: AppN f s 5 t => f -> s -> t
- app7 :: AppN f s 6 t => f -> s -> t
- app8 :: AppN f s 7 t => f -> s -> t
- app9 :: AppN f s 8 t => f -> s -> t
- app10 :: AppN f s 9 t => f -> s -> t
- consT :: ConsT a s t => a -> s -> t
- snocT :: SnocT a s t => a -> s -> t
- appendT :: AppendT s r t => s -> r -> t
- reverseT :: ReverseT s t => s -> t
- initT :: InitT s t => s -> t
- tailT :: TailT s t => s -> t
- ins :: Insert a s t => a -> s -> t
- insN :: InsertN a s n t => a -> s -> Proxy n -> t
- ins1 :: InsertN a s 0 t => a -> s -> t
- ins2 :: InsertN a s 1 t => a -> s -> t
- ins3 :: InsertN a s 2 t => a -> s -> t
- ins4 :: InsertN a s 3 t => a -> s -> t
- ins5 :: InsertN a s 4 t => a -> s -> t
- ins6 :: InsertN a s 5 t => a -> s -> t
- ins7 :: InsertN a s 6 t => a -> s -> t
- ins8 :: InsertN a s 7 t => a -> s -> t
- ins9 :: InsertN a s 8 t => a -> s -> t
- ins10 :: InsertN a s 9 t => a -> s -> t
- del :: Delete s t => s -> t
- delN :: DeleteN s n t => s -> Proxy n -> t
- del1 :: DeleteN s 0 t => s -> t
- del2 :: DeleteN s 1 t => s -> t
- del3 :: DeleteN s 2 t => s -> t
- del4 :: DeleteN s 3 t => s -> t
- del5 :: DeleteN s 4 t => s -> t
- del6 :: DeleteN s 5 t => s -> t
- del7 :: DeleteN s 6 t => s -> t
- del8 :: DeleteN s 7 t => s -> t
- del9 :: DeleteN s 8 t => s -> t
- del10 :: DeleteN s 9 t => s -> t
- foldlT :: FoldLeft f s t => f -> t -> s -> t
- foldrT :: FoldRight f s t => f -> t -> s -> t
- uncurryT :: UnCurryT s t b => s -> t -> b
- curryT :: CurryT s t => s -> t

# Selection

sel :: Select s t => s -> t Source #

Takes an n-ary tuple and returns the first element of the requested type

`>>>`

'd'`sel (1,'d','c') :: Char`

## convenience functions

sel1 :: SelectN s 0 t => s -> t Source #

Selects the first element in an n-ary tuple

`>>>`

0`sel1 (0,'d','c')`

lastT :: forall s n t. (LengthT s ~ n, SelectN s (n - 1) t) => s -> t Source #

Selects the last element of any n-ary tuple

`>>>`

4`lastT (1,2,3,4)`

`>>>`

3`lastT (1,2,3)`

# Application

app :: App f s t => f -> s -> t Source #

Applies a monomorphic function to the first element of an n-ary tuple that matches the type of the argument of the function.

`>>>`

('d',False)`app not ('d',True)`

Sometimes it is necessary to specify the result type, such that the function becomes monomorphic >>> app (+1) (True,5) :: (Bool,Integer) (True,6)

One may also use `appPoly`

, which doesn't require specifying the result type. However it can only apply functions
to the first element of an n-ary tuple.

appPoly :: App (Poly a b) s t => (a -> b) -> s -> t Source #

Applies a polymorphic function to the first element of an n-ary tuple. Since the function is polymorphic in its argument it can always be applied to the first element of a tuple.

`>>>`

("5",False)`appPoly show (5,False)`

appN :: AppN f s n t => f -> s -> Proxy n -> t Source #

Applies a function to the element at index `n`

in an n-ary tuple.

`>>>`

(False,True,True)`appN not (Proxy 2) (False,True,False)`

`appN`

also works for polymorphic functions

`>>>`

(5,'c',"False")`appN show (5,'c',False) (Proxy :: Proxy 2)`

appF :: AppF f s t => f -> s -> t Source #

Apply an n-ary function to an n-ary tuple. The function takes an argument for each component of the tuple in left-to-right order.

`>>>`

2`appF (\a b c -> if a then b else c) (False,1,2)`

mapT :: MapT f s t => f -> s -> t Source #

Maps a monomorphic function over each element in an n-ary tuple that matches the type of the argument of the function

`>>>`

(False,'c',True)`mapT not (True,'c',False)`

Sometimes it is necessary to specify the result type.

`>>>`

(6,7,8,False)`mapT (+1) (5,6,7,False) :: (Integer,Integer,Integer,Bool)`

Using `mapPolyT`

this is not necessary. However, to use `mapPolyT`

the tuple may only contains elements of a single type.

mapPolyT :: MapT (Poly a b) s t => (a -> b) -> s -> t Source #

Applies a polymorphic function to each element in an n-ary tuple. Requires all elements in the tuple to be of the same type.

`>>>`

(6,7,8,9)`mapPolyT (+1) (5,6,7,8)`

`>>>`

No instance for (Num Bool) arising from the literal `5'`mapPolyT (+1) (5,6,7,False)`

## convenience functions

app1 :: AppN f s 0 t => f -> s -> t Source #

Applies a function to the first element of an n-ary tuple

`>>>`

(6,6,7)`app1 (+1) (5,6,7)`

# Constructing tuples

consT :: ConsT a s t => a -> s -> t Source #

Adds an element to the head of an n-ary tuple

`>>>`

(5,True,'c')`consT 5 (True,'c')`

snocT :: SnocT a s t => a -> s -> t Source #

Adds an element to the back of an n-ary tuple

`>>>`

(True,'c',5)`snocT 5 (True,'c')`

appendT :: AppendT s r t => s -> r -> t Source #

Appends two n-ary tuple into one larger tuple

`>>>`

(5,'c','d',False)`appendT (5,'c') ('d',False)`

reverseT :: ReverseT s t => s -> t Source #

Reverses the order of elements in an n-ary tuple

`>>>`

(4,3,2,1)`reverseT (1,2,3,4)`

initT :: InitT s t => s -> t Source #

Takes an n-ary tuple and returns the same tuple minus the first element.

`>>>`

(1,2,3)`initT (1,2,3,4)`

Works only only tuples of size at least 3

`>>>`

Couldn't match type `2 ':<= 3' with `2 ':>= 3'`initT (1,2)`

tailT :: TailT s t => s -> t Source #

Takes an n-ary tuple and returns the same tuple minus the first element.

`>>>`

(2,3,4)`tailT (1,2,3,4)`

Works only only tuples of size at least 3

`>>>`

Couldn't match type `2 ':<= 3' with `2 ':>= 3'`tailT (1,2)`

# Insertion

ins :: Insert a s t => a -> s -> t Source #

Inserts an element into an n-ary tuple. Its position is determined by the target type

`>>>`

('c',5,False)`ins 5 ('c', False) :: (Char, Integer, Bool)`

An element will only be inserted at a specific position if:

- The element type matches the component type
- The tail of the tuple remains well-typed

`>>>`

('c',1,False,5)`ins 5 ('c',1,False) :: (Char, Integer, Bool, Integer)`

In the above example, inserting 5 after `c`

would make the tail of the tuple have type (Integer, Bool) when the target type asks for (Bool,Integer)

When attempting to insert an element before or after a component of the same type the element is always inserted in front

`>>>`

('c',5,1,False)`ins 5 ('c',1,False) :: (Char, Integer, Integer, Bool)`

insN :: InsertN a s n t => a -> s -> Proxy n -> t Source #

Inserts an element at an index specified position into an n-ary tuple

`>>>`

('c',5,1,False)`insN 5 ('c',1,False) (Proxy :: Proxy 1)`

ins1 :: InsertN a s 0 t => a -> s -> t Source #

Inserts an element in head position into an n-ary tuple

`>>>`

(5,'c',1,False)`ins1 5 ('c',1,False)`

# Deletion

del :: Delete s t => s -> t Source #

Deletes the first element in an n-ary tuple whose type does not exist in the target type

`>>>`

('c',False)`del ('c',False,5) :: (Char,Bool)`

delN :: DeleteN s n t => s -> Proxy n -> t Source #

Deletes an element specified by an index in an n-ary tuple

`>>>`

('c',5)`delN ('c',False,5) (Proxy :: Proxy 1)`

## convenience functions

del1 :: DeleteN s 0 t => s -> t Source #

Deletes the first element of an n-ary tuple

`>>>`

(False,5)`del1 ('c',False,5)`

# Folding

foldlT :: FoldLeft f s t => f -> t -> s -> t Source #

Fold left for n-ary tuples

`>>>`

-10`foldlT (-) 0 (4,3,2,1)`

foldrT :: FoldRight f s t => f -> t -> s -> t Source #

Fold right for n-ary tuples

`>>>`

2`foldrT (-) 0 (4,3,2,1)`