Undefined at the type level

纵然是瞬间 提交于 2019-12-03 14:49:20

问题


Often when I'm playing with Haskell code, I stub things out with a type annotation and undefined.

foo :: String -> Int
foo = undefined

Is there a type-level "undefined" that I could use in a similar way?

(Ideally, in conjunction with a kind annotation)

type Foo :: * -> *
type Foo = Undefined

Further thought on the same thread: is there a way for me to stub out typeclass instances for types created this way? An even easier way than the following theoretical way?

instance Monad Foo where
  return = undefined
  (>>=) = undefined

回答1:


You can use EmptyDataDecls to stub out a type, and with KindSignatures you can give it a kind:

{-# LANGUAGE EmptyDataDecls, KindSignatures #-}

data Foo :: * -> *

You can also stub out the Monad instance without warnings with this option to GHC.

{-# OPTIONS_GHC -fno-warn-missing-methods #-}

instance Monad Foo

And then you don't need to leave any implementation for return and >>=.




回答2:


This question was asked and answered a long time ago; best practices have evolved since.

These days, instead of undefined, for stubbing out code you should be using typed holes, and their type-level analogue, partial type signatures.



来源:https://stackoverflow.com/questions/9036554/undefined-at-the-type-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!