[Haskell-cafe] ANN: unfoldable-0.4.0

Sjoerd Visscher sjoerd at w3future.com
Sat Apr 28 18:10:09 CEST 2012

On Apr 28, 2012, at 2:40 AM, wren ng thornton wrote:

> On 4/26/12 3:52 PM, Roman Cheplyaka wrote:
>> * Tillmann Rendel<rendel at informatik.uni-marburg.de>  [2012-04-26 21:34:21+0200]
>>> Hi,
>>> Sjoerd Visscher wrote:
>>>> 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)
>>> Just to be sure: That's not a generalization of Data.List.unfoldr, or
>>> is it somehow?
>> It seems to be -- see
>> https://github.com/sjoerdvisscher/unfoldable/blob/master/src/Data/Unfoldable.hs#L84
>> (although that is much more complicated than Data.List.unfoldr)
> I must admit I'm a bit weirded out by the (Bounded a, Enum a) restriction on the Either, tuple, and Constant instances. Why not just use Unfoldable a, or have a class specifically devoted to unfolding * types?

I don't like the (Bounded a, Enum a) restrictions very much either. That was basically a quick hack in the first version and I haven't given it much thought after that.

The most generic solution would be Biunfoldable I think.

class Biunfoldable t where
  biunfold :: Unfolder f => f a -> f b -> f (t a b)

instance Biunfoldable (,) where
  biunfold fa fb = choose [(,) <$> fa <*> fb]
instance Biunfoldable Either where
  biunfold fa fb = choose [Left <$> fa, Right <$> fb]
instance Biunfoldable Constant where
  biunfold fa _ = choose [Constant <$> fa]

But I don't think an unfoldable class for * types is that interesting. Any type that would be an instance could also be in instance of Bounded and Enum:

class Unfoldable0 a where
  unfold0 :: Unfolder f => f a

minBoundDef :: Unfoldable0 a => a
minBoundDef = fromJust unfold0

maxBoundDef :: Unfoldable0 a => a
maxBoundDef = fromJust (getDualA unfold0)

toEnumDef :: Unfoldable0 a => Int -> a
toEnumDef i = unfold0 !! i

fromEnumDef :: (Unfoldable0 a, Eq a) => a -> Int
fromEnumDef a = fromJust (elemIndex a unfold0)

so having boundedEnum is good enough I think.
Sjoerd Visscher

More information about the Haskell-Cafe mailing list