Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnecessary type juggling to define Data.Void (package "void" on Hackage)?

Tags:

haskell

The void package claims to provide an uninhabitable type called Void, which is defined as follows -

newtype Void = Void Void

How is this definition any better than using something simpler? Say -

data Void

If my understanding is correct, both the data types contain only bottom values. However the latter is much easier to understand.

EDIT: Okay so I understand Daniel's answer below. However I thought of another possibly simpler way to do this while remaining Haskell98 compatible. We can use an Abstract data type and expose no constructors to the user.

module Data.Void (Void) where
  data Void = Void

Now only code in Data.Void module can construct a Void, however since we know it doesn't, the Void datatype is effectively uninhabited.

Would that work or am I missing something here?

like image 543
Anupam Jain Avatar asked Feb 13 '12 13:02

Anupam Jain


2 Answers

From description for the void package on Hackage: "A Haskell 98 logically uninhabited data type" (my emphasis). Declaring Void as simply data Void would require either Haskell 2010 or the "EmptyDataDecls" language extension and thus would not be "Haskell 98".

EDIT

Here is a page on the Haskell Wiki that describes exactly this situation.

like image 193
Daniel Pratt Avatar answered Oct 21 '22 08:10

Daniel Pratt


Your implementation is still incomplete because you do not have the absurd function:

absurd :: Void -> a

Void is supposed to be an initial object in Haskell, which is why you need the absurd function. I find the implementation of absurd in the void package to be very elegant and appropriate:

absurd (Void a) = absurd a

This definition is reminiscent of the logical analogy of Void to the False premise from which any conclusion can be derived. It even uses the logical fallacy of referencing itself to prove itself, which is logically ... absurd.

like image 22
Gabriella Gonzalez Avatar answered Oct 21 '22 09:10

Gabriella Gonzalez