Transhare-0.9: A library to apply transformation to containers so as to maximize sharing of unchanged subcomponents.



This module is my answer to the pattern discussed in http:blog.ezyang.com201106a-pattern-for-increasing-sharing about maximizing sharing when transforming an algebraic data type.

The' Transhare class is a kind of degerate case of Traverse building on a new Applicative data type called TransResult defined below. The result transM is a way to lift a parsimonious transformer 'a -> Maybe a', which indicates identity with Nothing, to work on a container with maximized sharing.



type TransM a = a -> Maybe aSource

TransM is a parsimonious transformer that can return Nothing when the transformation is an identity.

If the result is Just t then the result t might or might not be identical to the argument.

type TransR a = a -> TransResult aSource

TransR is a parsimonious transformer that returns (Original x) only if x is the original argument.

This must follow the law that TransMR . TransRM . t = t

The disadvantage of TransR compared to TransM is ensuring the above law and that sharing for Original results is actually being done.

TransR which implement sharing correctly are proper implementations of TransR

transMR :: TransM a -> TransR aSource

transMR creates a proper implementation of transR from any transM

transRM :: TransR a -> TransM aSource

transRM creates a proper implementation of transM only from a proper implementation of transR

fromO :: a -> TransResult a -> TransResult aSource

fromO is a helper function used with Applicative to ensure the TransR computed by transR are proper implementations.