What's the absurd function in Data.Void useful for?

前端 未结 6 1829
孤街浪徒
孤街浪徒 2020-12-04 07:42

The absurd function in Data.Void has the following signature, where Void is the logically uninhabited type exported by that package:



        
6条回答
  •  [愿得一人]
    2020-12-04 08:17

    I'm thinking that perhaps it's useful in some cases as a type-safe way of exhaustively handling "can't happen" cases

    This is precisely right.

    You could say that absurd is no more useful than const (error "Impossible"). However, it is type restricted, so that its only input can be something of type Void, a data type which is intentionally left uninhabited. This means that there is no actual value that you can pass to absurd. If you ever end up in a branch of code where the type checker thinks that you have access to something of type Void, then, well, you are in an absurd situation. So you just use absurd to basically mark that this branch of code should never be reached.

    "Ex falso quodlibet" literally means "from [a] false [proposition], anything follows". So when you find that you are holding a piece of data whose type is Void, you know you have false evidence in your hands. You can therefore fill any hole you want (via absurd), because from a false proposition, anything follows.

    I wrote a blog post about the ideas behind Conduit which has an example of using absurd.

    http://unknownparallel.wordpress.com/2012/07/30/pipes-to-conduits-part-6-leftovers/#running-a-pipeline

提交回复
热议问题