在类型级别未定义

发布于 2024-12-29 06:26:44 字数 431 浏览 4 评论 0原文

通常,当我使用 Haskell 代码时,我会使用类型注释和 undefined 来存根。

foo :: String -> Int
foo = undefined

是否有一个类型级别的“未定义”,我可以以类似的方式使用?

(理想情况下,与某种注释结合使用)

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

对同一线程的进一步思考:有没有办法让我为以这种方式创建的类型存根类型类实例?比以下理论方法更简单的方法?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

困倦 2025-01-05 06:26:44

您可以使用 EmptyDataDecls 来存根类型,并且使用 KindSignatures 您可以给它一个类型:

{-# LANGUAGE EmptyDataDecls, KindSignatures #-}

data Foo :: * -> *

您还可以存根 Monad 实例,而无需使用此选项向 GHC 发出警告。

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

instance Monad Foo

然后您不需要为 return>>= 保留任何实现。

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 >>=.

☆獨立☆ 2025-01-05 06:26:44

这个问题很久以前就被问过并回答过;此后,最佳实践不断发展。

如今,为了删除代码,您应该使用 类型化洞,以及它们的类型级类似物,部分类型签名

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文