What is the role of 'bottom' (⊥) in Haskell function definitions?

后端 未结 4 1295
醉酒成梦
醉酒成梦 2021-02-05 07:35

I don\'t understand the role played by bottom ( or _|_) in Haskell function definitions.

The definition of zip for example describes it as \"r

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 07:56

    Bottom is essentially the fancy algebraic way of saying undefined.

    If you try this, you can see why zip is lazy for its right-hand argument:

    λ> zip [] undefined
    []
    λ> zip undefined []
    *** Exception: Prelude.undefined
    

    This is because undefined only fails when you try to evaluate it.

    You might be confusing _|_ with _ because of the way it was presented. I will make it clear: the line zip [] _|_ = [] does not act as a pattern match but an equation, stating the equality of zip [] _|_ and []. That is to say, this is not valid Haskell code, but a notational, abstract-algebraic way of saying "I don't care about the second argument."

    In the definition of zip you may of course use _, but that's irrelevant. You could have used any name, just as long as it wasn't a constructor-matching pattern such as (Just x) or (a,b). Values will remain unevaluated until they must be pattern matched in pure code.

    You can read more about lazy evaluation here.

    You can read more about bottom here and here.

提交回复
热议问题