tuple-morph-0.1.0.0: Morph between tuples with the same "flattened" representation

Copyright(c) Paweł Nowak
LicenseMIT
MaintainerPaweł Nowak <pawel834@gmail.com>
Stabilityprovisional
Safe HaskellNone
LanguageHaskell2010

Data.Tuple.Morph

Contents

Description

Allows you to flatten, unflatten and morph tuples of matching types.

Note: by design units are ignored. For example (Int, (), Char) is the same as (Int, Char).

Synopsis

Morphing tuples.

morph :: forall a b. (HFoldable a, HUnfoldable b, Rep a ~ Rep b) => a -> b Source

Morph a tuple to some isomorphic tuple with the same order of types.

Works with arbitrary nested tuples, each tuple can have size up to sizeLimit.

>>> morph ("a", ("b", "c")) :: (String, String, String)
("a","b","c")
>>> morph ((1 :: Int, 2 :: Int), 3 :: Double) :: (Int, (Int, Double))
(1,(2,3.0))
>>> morph ("a", (), (5 :: Int, (), "c")) :: ((), (String, Int), String)
((),("a",5),"c")
>>> morph (((("a", "b"), "c"), "d"), "e") :: ((String, String), (String, (String, String)))
(("a","b"),("c",("d","e")))

sizeLimit :: Int Source

Size of the largest tuple that this library will work with. Equal to 13.

Note that size of ((((((1, 1), 1), 1), 1), 1), 1) is 2, not 7.

Converting between tuples and HLists.

type family Rep tuple :: [*] Source

Recurisvely break down a tuple type, representing it as a type list.

Equations

Rep (a, b, c, d, e, f, g, h, i, j, k, l, m) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) ((++) (Rep g) ((++) (Rep h) ((++) (Rep i) ((++) (Rep j) ((++) (Rep k) ((++) (Rep l) (Rep m)))))))))))) 
Rep (a, b, c, d, e, f, g, h, i, j, k, l) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) ((++) (Rep g) ((++) (Rep h) ((++) (Rep i) ((++) (Rep j) ((++) (Rep k) (Rep l))))))))))) 
Rep (a, b, c, d, e, f, g, h, i, j, k) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) ((++) (Rep g) ((++) (Rep h) ((++) (Rep i) ((++) (Rep j) (Rep k)))))))))) 
Rep (a, b, c, d, e, f, g, h, i, j) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) ((++) (Rep g) ((++) (Rep h) ((++) (Rep i) (Rep j))))))))) 
Rep (a, b, c, d, e, f, g, h, i) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) ((++) (Rep g) ((++) (Rep h) (Rep i)))))))) 
Rep (a, b, c, d, e, f, g, h) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) ((++) (Rep g) (Rep h))))))) 
Rep (a, b, c, d, e, f, g) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) ((++) (Rep f) (Rep g)))))) 
Rep (a, b, c, d, e, f) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) ((++) (Rep e) (Rep f))))) 
Rep (a, b, c, d, e) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) ((++) (Rep d) (Rep e)))) 
Rep (a, b, c, d) = (++) (Rep a) ((++) (Rep b) ((++) (Rep c) (Rep d))) 
Rep (a, b, c) = (++) (Rep a) ((++) (Rep b) (Rep c)) 
Rep (a, b) = (++) (Rep a) (Rep b) 
Rep () = `[]` 
Rep a = `[a]` 

class HFoldable t where Source

Types that can be flattened to a heterogenous list.

Methods

toHList :: t -> HList (Rep t) Source

Converts a structure to a heterogenous list.

Instances

HFoldable () 
(~) [*] (Rep a) ((:) * a ([] *)) => HFoldable a 
(HFoldable a, HFoldable b) => HFoldable (a, b) 
(HFoldable a, HFoldable b, HFoldable c) => HFoldable (a, b, c) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d) => HFoldable (a, b, c, d) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e) => HFoldable (a, b, c, d, e) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f) => HFoldable (a, b, c, d, e, f) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g) => HFoldable (a, b, c, d, e, f, g) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g, HFoldable h) => HFoldable (a, b, c, d, e, f, g, h) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g, HFoldable h, HFoldable i) => HFoldable (a, b, c, d, e, f, g, h, i) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g, HFoldable h, HFoldable i, HFoldable j) => HFoldable (a, b, c, d, e, f, g, h, i, j) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g, HFoldable h, HFoldable i, HFoldable j, HFoldable k) => HFoldable (a, b, c, d, e, f, g, h, i, j, k) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g, HFoldable h, HFoldable i, HFoldable j, HFoldable k, HFoldable l) => HFoldable (a, b, c, d, e, f, g, h, i, j, k, l) 
(HFoldable a, HFoldable b, HFoldable c, HFoldable d, HFoldable e, HFoldable f, HFoldable g, HFoldable h, HFoldable i, HFoldable j, HFoldable k, HFoldable l, HFoldable m) => HFoldable (a, b, c, d, e, f, g, h, i, j, k, l, m) 

class HUnfoldable t where Source

Types that can be built from a heterogenous list.

Minimal complete definition

hListParser

Methods

fromHList :: HList (Rep t) -> t Source

Build a structure from a heterogenous list.

hListParser :: HParser (Rep t) t Source

Builds a structure from a heterogenous list and yields the leftovers.

Instances

HUnfoldable () 
(~) [*] (Rep a) ((:) * a ([] *)) => HUnfoldable a 
(HUnfoldable a, HUnfoldable b) => HUnfoldable (a, b) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c) => HUnfoldable (a, b, c) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d) => HUnfoldable (a, b, c, d) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e) => HUnfoldable (a, b, c, d, e) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f) => HUnfoldable (a, b, c, d, e, f) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g) => HUnfoldable (a, b, c, d, e, f, g) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g, HUnfoldable h) => HUnfoldable (a, b, c, d, e, f, g, h) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g, HUnfoldable h, HUnfoldable i) => HUnfoldable (a, b, c, d, e, f, g, h, i) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g, HUnfoldable h, HUnfoldable i, HUnfoldable j) => HUnfoldable (a, b, c, d, e, f, g, h, i, j) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g, HUnfoldable h, HUnfoldable i, HUnfoldable j, HUnfoldable k) => HUnfoldable (a, b, c, d, e, f, g, h, i, j, k) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g, HUnfoldable h, HUnfoldable i, HUnfoldable j, HUnfoldable k, HUnfoldable l) => HUnfoldable (a, b, c, d, e, f, g, h, i, j, k, l) 
(HUnfoldable a, HUnfoldable b, HUnfoldable c, HUnfoldable d, HUnfoldable e, HUnfoldable f, HUnfoldable g, HUnfoldable h, HUnfoldable i, HUnfoldable j, HUnfoldable k, HUnfoldable l, HUnfoldable m) => HUnfoldable (a, b, c, d, e, f, g, h, i, j, k, l, m) 

HList parser.

newtype HParser rep val Source

A function that parses some value val with representation rep from a heterogenous list and returns the parsed value and leftovers.

Constructors

HParser 

Fields

runHParser :: forall leftover. HList (rep ++ leftover) -> (val, HList leftover)

Run the parser.

class MonoidIndexedMonad m where Source

An indexed monad on a monoid.

Associated Types

type Empty :: k Source

type Append x y :: k Source

Methods

returnMI :: a -> m Empty a Source

bindMI :: m x a -> (a -> m y b) -> m (Append x y) b Source