set-monad: Set monad
set-monad library exports the
Set abstract data type and
set-manipulating functions. These functions behave exactly as their namesakes
Data.Set module of the
containers library. In addition, the
set-monad library extends
Data.Set by providing
MonadPlus instances for sets.
In other words, you can use the
set-monad library as a drop-in replacement
Data.Set module of the
containers library and, in addition, you
will also get the aforementioned instances which are not available in the
It is not possible to directly implement instances for the aforementioned
standard Haskell type classes for the
Set data type from the
library. This is because the key operations
union, are constrained
Ord as follows.
map :: (Ord a, Ord b) => (a -> b) -> Set a -> Set b union :: (Ord a) => Set a -> Set a -> Set a
set-monad library provides the type class instances by wrapping the
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 . To my knowledge, the original, systematic presentation of the idea to represent monadic combinators as data is given in . There is also a Haskell library that provides a generic infrastructure for the aforementioned wrapping and unwrapping .
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 , 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.
] CSDL Blog: The home of applied functional programming at KU. Monad
] Chuan-kai Lin. 2006. Programming monads operationally with Unimo. In
Proceedings of the eleventh ACM SIGPLAN International Conference on Functional
Programming (ICFP '06). ACM.
] Heinrich Apfelmus. The operational package.
|Dependencies||base (==4.*), containers, deepseq [details]|
|Source repo||head: git clone https://github.com/giorgidze/set-monad.git|
|Uploaded||by GeorgeGiorgidze at Sun Jan 4 01:11:11 UTC 2015|
|Distributions||LTSHaskell:0.2.0.0, NixOS:0.2.0.0, openSUSE:0.2.0.0|
|Downloads||1845 total (24 in the last 30 days)|
|Rating||(no votes yet) [estimated by rule of succession]|
|Status||Docs available [build log]
Last success reported on 2015-01-04 [all 1 reports]
Hackage Matrix CI
For package maintainers and hackage trustees