[Haskell-cafe] Do people use visitor pattern in Haskell...

Phil Ruffwind rf at rufflewind.com
Fri Jun 19 18:36:26 UTC 2015

>     class Wibble a where
>       visit :: (Foo -> b) -> (Bar -> b) -> a -> b

The type `forall b. (X -> b) -> (Y -> b) -> b` is isomorphic to
`Either X Y`.  If you rearrange the signature of `visit` a little bit,

    visit_ :: a -> forall b . (Foo -> b) -> (Bar -> b) -> b

you can see that you're really just defining a function `a -> Either
Foo Bar`.  So the class can be simplified to:

    class Wibble' a where
      visit' :: a -> Either Foo Bar

It follows that `WibblePackage'` is equivalent to

    type WibblePackage' = forall a . (WibbleInstance a, a)


    type WibbleInstance a = a -> Either Foo Bar

Because of the `forall`, there's nothing you can do with the second
component of the tuple (`a`) except to apply it to the first (`a ->
Either Foo Bar`), so it's just a long-winded way of saying

    type WibblePackage'' = Either Foo Bar

In a non-strict language like Haskell, there's no reason to use such a
complicated type signature to delay the application of `a -> Either
Foo Bar` to `a`.

More information about the Haskell-Cafe mailing list