What is the kind of Void?

前端 未结 4 1673
醉话见心
醉话见心 2021-02-01 14:41

Seeing as the type of Void is uninhabited, can it be regarded as a type \"constructor\"? Or is this just a quick \"hack\" to be able to safely disregard / disable f

4条回答
  •  太阳男子
    2021-02-01 15:12

    Another angle on this question: suppose I asked you to write a guaranteed-terminating function of type a -> b:

    aintGonnaWork :: a -> b
    aintGonnaWork a = _
    

    As hopefully you can tell, it's impossible to write such a function. It follows from this that the type a -> b has no defined values. Note also that the kind of a -> b is *:

    (->)   :: * -> * -> *
    a      :: *
    b      :: *
    ---------------------
    a -> b :: *
    

    And there we have it: a type of kind *, built from "vanilla" Haskell elements (no "hacks"), but which nevertheless has no defined values. So the existence of types like Void is already implicit in "vanilla" Haskell; all that the explicit Void type does is provide a standard, named one.

    I'll close with a simple implementation of the Void type in terms of the above; the only extension necessary is RankNTypes.

    {-# LANGUAGE RankNTypes #-}
    
    newtype Void = Void (forall a b. a -> b)
    
    absurd :: Void -> a
    absurd (Void f) = f f
    

提交回复
热议问题