[Haskell-cafe] ANN: unfoldable-0.4.0
Sjoerd Visscher
sjoerd at w3future.com
Thu Apr 26 00:32:28 CEST 2012
I am pleased to announce the 5th version of the unfoldable package. (This is the first announcement, you didn't miss anything.)
http://hackage.haskell.org/package/unfoldable-0.4.0
Just as there's a Foldable class, there should also be an Unfoldable class. This package provides one:
class Unfoldable t where
unfold :: Unfolder f => f a -> f (t a)
Writing instances of Unfoldable is similar to writing Traversable instances. For example, given a data type
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a)
a suitable instance would be
instance Unfoldable Tree where
unfold fa = choose
[ pure Empty
, Leaf <$> fa
, Node <$> unfold fa <*> fa <*> unfold fa
]
The choose function comes from the Unfolder class:
class Applicative f => Unfolder f where
choose :: [f x] -> f x
(If f is an Alternative instance, choose is simply Data.Foldable.asum.)
Different unfolders provide different ways of generating values, for example:
- Random values
- Enumeration of all values (depth-first or breadth-first)
- Convert from a list
- An implementation of QuickCheck's arbitrary should also be possible (still working on that)
Some examples can be found in the examples directory in the github repo:
https://github.com/sjoerdvisscher/unfoldable
Ideas and comments are welcome!
greetings,
Sjoerd
More information about the Haskell-Cafe
mailing list