{-# LANGUAGE
MultiParamTypeClasses,
FlexibleInstances,
FlexibleContexts,
Rank2Types
#-}-- | -- Module : Control.Monad.Sharing.Classes-- Copyright : Chung-chieh Shan, Oleg Kiselyov, and Sebastian Fischer-- License : PublicDomain-- Maintainer : Sebastian Fischer <mailto:sebf@informatik.uni-kiel.de>-- Stability : experimental-- -- This library provides type classes for explicit sharing of monadic-- effects. Usually you don't need to import this library as it is-- reexported by the module 'Control.Monad.Sharing'. You may want to-- do so, however, when writing your own implementation of explicit-- sharing.moduleControl.Monad.Sharing.Classes(Sharing(..),Shareable(..),Convertible(..),convert)where-- | Interface of monads that support explicit sharing.classSharingmwhere-- | -- Yields an action that returns the same results as the given-- action but whose effects are only executed once. Especially, when-- the resulting action is duplicated it returns the same result at-- every occurrence.share::Shareablema=>ma->m(ma)-- |-- Interface of shareable nested monadic data types. The provided-- function 'shareArgs' is supposed to map the given function on every-- monadic argument.-- -- We provide instances of the 'Shareable' class for some predefined-- Haskell types. For flat types the function 'shareArgs' just returns-- its argument which has no arguments to which the given function-- could be applied.classShareablemawhereshareArgs::Monadn=>(forallb.Shareablemb=>mb->n(mb))->a->nainstanceMonadm=>ShareablemBoolwhereshareArgs_=returninstanceMonadm=>ShareablemIntwhereshareArgs_=returninstanceMonadm=>ShareablemCharwhereshareArgs_=returninstanceMonadm=>Shareablem[Bool]whereshareArgs_=returninstanceMonadm=>Shareablem[Int]whereshareArgs_=returninstanceMonadm=>Shareablem[Char]whereshareArgs_=return-- | An instance for lists with monadic elements.instance(Monadm,Shareablema)=>Shareablem[ma]whereshareArgsf=mapMf-- |-- Interface for convertible datatypes. The provided function-- 'convArgs' is supposed to map the given function on every argument-- of the given value and combine the results to give the converted-- value.-- -- We provide instances of the 'Convertible' class for some predefined-- Haskell types. For flat types the function 'convArgs' just returns-- its argument which has no arguments to which the given function-- could be applied.classConvertiblemabwhereconvArgs::(forallcd.Convertiblemcd=>c->md)->a->mb-- | Converts a convertible value recursively.convert::Convertiblemab=>a->mbconvert=convArgsconvertinstanceMonadm=>ConvertiblemBoolBoolwhereconvArgs_=returninstanceMonadm=>ConvertiblemIntIntwhereconvArgs_=returninstanceMonadm=>ConvertiblemCharCharwhereconvArgs_=returninstanceMonadm=>Convertiblem[Bool][Bool]whereconvArgs_=returninstanceMonadm=>Convertiblem[Int][Int]whereconvArgs_=returninstanceMonadm=>Convertiblem[Char][Char]whereconvArgs_=return-- |-- An instance to convert ordinary lists into lists with monadic-- elements.instance(Monadm,Convertiblemab)=>Convertiblem[a][mb]whereconvArgsf=return.mapf-- |-- An instance to convert lists with monadic elements into ordinary-- lists.instance(Monadm,Convertiblemab)=>Convertiblem[ma][b]whereconvArgsf=mapM(>>=f)