Search code examples
purescript

What is () defined as in Purescript? What does it mean?


What is () defined as? I see it in type signatures, I thought it was the unit type same as in Haskell but it seems instead Purescript uses Unit. So then what is ()?

Using spago repl was not helpful either:

> :t ()                                                              
Unexpected token ')' at line 1, column 2
> :i ()
Unrecognized directive. Type :? for help.
> :k ()
Error found:
at :1:2 - 1:3 (line 1, column 2 - line 1, column 3)

  Type variable k is undefined.

while inferring the kind of k
while checking that type k
  has kind Type
while inferring the kind of () @k

See https://github.com/purescript/documentation/blob/master/errors/UndefinedTypeVariable.md for more information,
or to contribute content related to this error.

Solution

  • () is of kind Row Type. As one commentor mentioned, it is in fact the empty row type. I think that the parser struggles with it, if it is encountered randomly without context (e.g. after :k) because it could also be the start of a pair of parenthesis. It could probably be considered a bug. If you give it context, it works. It just completely defeats the point of using :k in the first place.

    > :k () :: Row Type
    Row Type
    

    It can be used where row types are expected:

    > :k Record
    Row Type -> Type
    
    > :k Record ()
    Type
    

    Record () is the type of the empty record {}.

    > :t {}
    Record (() @Type)