Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

() as empty constraint

how can one represent the empty constraint ?

for the following file

{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE KindSignatures #-}

import Data.Kind(Type, Constraint)

type Empty = (() :: Type -> Constraint)

main :: IO ()
main = return ()

ghc 8.2.2 answers

constraint.hs:6:15: error:
    • Expected kind ‘* -> Constraint’, but ‘()’ has kind ‘*’
    • In the type ‘(() :: Type -> Constraint)’
      In the type declaration for ‘Empty’
  |
6 | type Empty = (() :: Type -> Constraint)
  |  

what do i miss ?

i know about the following solution

{-# LANGUAGE FlexibleInstances #-}

class Empty x
instance Empty x

but i want to know why () does not work

like image 649
libeako Avatar asked Sep 04 '25 03:09

libeako


1 Answers

() has kind * or Constraint, depending on context, never a -> Constraint. Similarly (,) has kind * -> * -> * or Constraint -> Constraint -> Constraint, depending on context.

— Simon Peyton-Jones

It’s just that () is only overloaded for whether it’s a type or a constraint. That is, you’d write () => a, not (() a) => a. So I think this:

class Empty x
instance Empty x

Is the correct solution here. (And perhaps something like that should be in base.)

like image 174
Jon Purdy Avatar answered Sep 05 '25 21:09

Jon Purdy