Search code examples
haskellhaskell-snap-framework

What is <|> in haskell?


I am still learning haskell and looking at the snap web framework. In their hello world example there is a thingy that looks like <|>

site :: Snap ()
site =
    ifTop (writeBS "hello world") <|>
    route [ ("foo", writeBS "bar")
          , ("echo/:echoparam", echoHandler)
          ] <|>
    dir "static" (serveDirectory ".")

Googling for this is surprisingly challenging, and the snap documentation simply uses <|> as a noun. What is it, and what does it do?


Solution

  • It's a method in the Alternative typeclass in the module Control.Applicative in the base package.

    Typically it means that you're dealing with a kind of computation which can fail and continue. If both x and y are typed as m a where m tags this kind of computation we're talking about then

    x <|> y :: m a
    

    is a computation which "tries" x and if it fails then "tries" y. Such kinds of computation instantiate Alternative.