{-# LANGUAGE Safe #-}{-# LANGUAGE GADTs #-}{-|
The @set-monad@ library exports the @Set@ abstract data type and
set-manipulating functions. These functions behave exactly as their namesakes
from the @Data.Set@ module of the @containers@ library. In addition, the
@set-monad@ library extends @Data.Set@ by providing @Functor@, @Applicative@,
@Alternative@, @Monad@, and @MonadPlus@ instances for sets.
In other words, you can use the @set-monad@ library as a drop-in replacement
for the @Data.Set@ module of the @containers@ library and, in addition, you
will also get the aforementioned instances which are not available in the
@containers@ package.
It is not possible to directly implement instances for the aforementioned
standard Haskell type classes for the @Set@ data type from the @containers@
library. This is because the key operations @map@ and @union@, are constrained
with @Ord@ as follows.
> map :: (Ord a, Ord b) => (a -> b) -> Set a -> Set b
> union :: (Ord a) => Set a -> Set a -> Set a
The @set-monad@ library provides the type class instances by wrapping the
constrained @Set@ type into a data type that has unconstrained constructors
corresponding to monadic combinators. The data type constructors that
represent monadic combinators are evaluated with a constrained run function.
This elevates the need to use the constraints in the instance definitions
(this is what prevents a direct definition). The wrapping and unwrapping
happens internally in the library and does not affect its interface.
For details, see the rather compact definitions of the @run@ function and
type class instances. The left identity and associativity monad laws play a
crucial role in the definition of the @run@ function. The rest of the code
should be self explanatory.
The technique is not new. This library was inspired by [1]. To my knowledge,
the original, systematic presentation of the idea to represent monadic
combinators as data is given in [2]. There is also a Haskell library that
provides a generic infrastructure for the aforementioned wrapping and
unwrapping [3].
The @set-monad@ library is particularly useful for writing set-oriented code
using the do and/or monad comprehension notations. For example, the following
definitions now type check.
> s1 :: Set (Int,Int)
> s1 = do a <- fromList [1 .. 4]
> b <- fromList [1 .. 4]
> return (a,b)
> -- with -XMonadComprehensions
> s2 :: Set (Int,Int)
> s2 = [ (a,b) | (a,b) <- s1, even a, even b ]
> s3 :: Set Int
> s3 = fmap (+1) (fromList [1 .. 4])
As noted in [1], the implementation technique can be used for monadic
libraries and EDSLs with restricted types (compiled EDSLs often restrict the
types that they can handle). Haskell's standard monad type class can be used
for restricted monad instances. There is no need to resort to GHC extensions
that rebind the standard monadic combinators with the library or EDSL specific
ones.
@[@1@]@ CSDL Blog: The home of applied functional programming at KU. Monad
Reification in Haskell and the Sunroof Javascript compiler.
<http://www.ittc.ku.edu/csdlblog/?p=88>
@[@2@]@ Chuan-kai Lin. 2006. Programming monads operationally with Unimo. In
Proceedings of the eleventh ACM SIGPLAN International Conference on Functional
Programming (ICFP '06). ACM.
@[@3@]@ Heinrich Apfelmus. The operational package.
<http://hackage.haskell.org/package/operational>
-}moduleData.Set.Monad(-- * Set typeSet-- * Operators,(\\)-- * Query,null,size,member,notMember,isSubsetOf,isProperSubsetOf-- * Construction,empty,singleton,insert,delete-- * Combine,union,unions,difference,intersection-- * Filter,filter,partition,split,splitMember-- * Map,map,mapMonotonic-- * Folds,foldr,foldl-- ** Strict folds,foldr',foldl'-- ** Legacy folds,fold-- * Min\/Max,findMin,findMax,deleteMin,deleteMax,deleteFindMin,deleteFindMax,maxView,minView-- * Conversion-- ** List,elems,toList,fromList-- ** Ordered list,toAscList,fromAscList,fromDistinctAscList-- * Debugging,showTree,showTreeWith,valid)whereimportPreludehiding(null,filter,map,foldr,foldl)importqualifiedData.ListasLimportqualifiedData.SetasSimportqualifiedData.FunctorasFimportqualifiedControl.ApplicativeasAimportData.MonoidimportControl.ArrowimportControl.MonadimportControl.DeepSeqdataSetawherePrim::(Orda)=>S.Seta->SetaReturn::a->SetaBind::Seta->(a->Setb)->SetbZero::SetaPlus::Seta->Seta->Setarun::(Orda)=>Seta->S.Setarun(Prims)=srun(Returna)=S.singletonarun(Zero)=S.emptyrun(Plusmamb)=S.union(runma)(runmb)run(Bind(Prims)f)=S.foldl'S.unionS.empty(S.map(run.f)s)run(Bind(Returna)f)=run(fa)run(BindZero_)=S.emptyrun(Bind(Plusmamb)f)=run(Plus(Bindmaf)(Bindmbf))run(Bind(Bindmf)g)=run(Bindm(\a->Bind(fa)g))instanceF.FunctorSetwherefmap=liftMinstanceA.ApplicativeSetwherepure=return(<*>)=apinstanceA.AlternativeSetwhereempty=mzero(<|>)=mplusinstanceMonadSetwherereturn=Return(>>=)=BindinstanceMonadPlusSetwheremzero=Zeromplus=Plusinstance(Orda)=>Monoid(Seta)wheremempty=emptymappend=unionmconcat=unionsinstance(Orda)=>Eq(Seta)wheres1==s2=runs1==runs2instance(Orda)=>Ord(Seta)wherecompares1s2=compare(runs1)(runs2)instance(Showa,Orda)=>Show(Seta)whereshow=show.runinstance(Reada,Orda)=>Read(Seta)wherereadsPrecis=L.map(firstPrim)(readsPrecis)instance(NFDataa,Orda)=>NFData(Seta)wherernf=rnf.runinfixl9\\(\\)::(Orda)=>Seta->Seta->Setam1\\m2=differencem1m2null::(Orda)=>Seta->Boolnull=S.null.runsize::(Orda)=>Seta->Intsize=S.size.runmember::(Orda)=>a->Seta->Boolmemberas=S.membera(runs)notMember::(Orda)=>a->Seta->BoolnotMemberat=not(memberat)isSubsetOf::Orda=>Seta->Seta->BoolisSubsetOfs1s2=S.isSubsetOf(runs1)(runs2)isProperSubsetOf::Orda=>Seta->Seta->BoolisProperSubsetOfs1s2=S.isProperSubsetOf(runs1)(runs2)empty::(Orda)=>Setaempty=PrimS.emptysingleton::(Orda)=>a->Setasingletona=Prim(S.singletona)insert::(Orda)=>a->Seta->Setainsertas=Prim(S.inserta(runs))delete::(Orda)=>a->Seta->Setadeleteas=Prim(S.deletea(runs))union::(Orda)=>Seta->Seta->Setaunions1s2=Prim(S.union(runs1)(runs2))unions::(Orda)=>[Seta]->Setaunionsss=Prim(S.unions(L.maprunss))difference::(Orda)=>Seta->Seta->Setadifferences1s2=Prim(S.difference(runs1)(runs2))intersection::(Orda)=>Seta->Seta->Setaintersections1s2=Prim(S.intersection(runs1)(runs2))filter::(Orda)=>(a->Bool)->Seta->Setafilterfs=Prim(S.filterf(runs))partition::(Orda)=>(a->Bool)->Seta->(Seta,Seta)partitionfs=(Prim***Prim)(S.partitionf(runs))split::(Orda)=>a->Seta->(Seta,Seta)splitas=(Prim***Prim)(S.splita(runs))splitMember::(Orda)=>a->Seta->(Seta,Bool,Seta)splitMemberas=(\(s1,b,s2)->(Prims1,b,Prims2))(S.splitMembera(runs))map::(Orda,Ordb)=>(a->b)->Seta->Setbmapfs=Prim(S.mapf(runs))mapMonotonic::(Orda,Ordb)=>(a->b)->Seta->SetbmapMonotonicfs=Prim(S.mapMonotonicf(runs))foldr::(Orda)=>(a->b->b)->b->Seta->bfoldrfzs=S.foldrfz(runs)foldl::(Orda)=>(b->a->b)->b->Seta->bfoldlfzs=S.foldlfz(runs)foldr'::(Orda)=>(a->b->b)->b->Seta->bfoldr'fzs=S.foldr'fz(runs)foldl'::(Orda)=>(b->a->b)->b->Seta->bfoldl'fzs=S.foldl'fz(runs)fold::(Orda)=>(a->b->b)->b->Seta->bfold=foldrfindMin::(Orda)=>Seta->afindMin=S.findMin.runfindMax::(Orda)=>Seta->afindMax=S.findMax.rundeleteMin::(Orda)=>Seta->SetadeleteMin=Prim.S.deleteMin.rundeleteMax::(Orda)=>Seta->SetadeleteMax=Prim.S.deleteMax.rundeleteFindMin::(Orda)=>Seta->(a,Seta)deleteFindMins=secondPrim(S.deleteFindMin(runs))deleteFindMax::(Orda)=>Seta->(a,Seta)deleteFindMaxs=secondPrim(S.deleteFindMax(runs))maxView::(Orda)=>Seta->Maybe(a,Seta)maxView=fmap(secondPrim).S.maxView.runminView::(Orda)=>Seta->Maybe(a,Seta)minView=fmap(secondPrim).S.minView.runelems::(Orda)=>Seta->[a]elems=toListtoList::(Orda)=>Seta->[a]toList=S.toList.runfromList::(Orda)=>[a]->SetafromListas=Prim(S.fromListas)toAscList::(Orda)=>Seta->[a]toAscList=S.toAscList.runfromAscList::(Orda)=>[a]->SetafromAscList=Prim.S.fromAscListfromDistinctAscList::(Orda)=>[a]->SetafromDistinctAscList=Prim.S.fromDistinctAscListshowTree::(Showa,Orda)=>Seta->StringshowTree=S.showTree.runshowTreeWith::(Showa,Orda)=>Bool->Bool->Seta->StringshowTreeWithb1b2s=S.showTreeWithb1b2(runs)valid::(Orda)=>Seta->Boolvalid=S.valid.run