简体   繁体   English

()在Haskell中意味着什么

[英]What does () mean in Haskell

In some Haskell code I came across: 在一些Haskell代码中,我遇到了:

put :: s -> m ()

What does the () mean here? ()在这里意味着什么?

I'd use a search engine, but I can't find one that handles () correctly. 我使用搜索引擎,但我找不到正确处理()的搜索引擎。

() means "Boring". ()表示“无聊”。 It means the boring type which contains one thing, also boring. 这意味着无聊的类型包含一件事,也很无聊。 There is nothing interesting to be gained by comparing one element of the boring type with another, because there is nothing to learn about an element of the boring type by giving it any of your attention. 通过将无聊类型的一个元素与另一个元素进行比较,没有什么有趣的,因为通过给予它任何注意力,没有什么可以学习无聊类型的元素。

It is very different from the empty type, called (by people I wish had chosen a better name like the one I suggested) in Haskell Void . 它与空类型非常不同,在Haskell Void称为(我希望人们选择一个更好的名字,就像我建议的那样)。 The empty type is very exciting, because if somebody ever gives you a value belonging to it, you know that you are already dead and in Heaven and that anything you want is yours. 空的类型是非常令人兴奋的,因为如果有人给你一个属于它的价值,你知道你已经死了,在天堂,你想要的任何东西都是你的。

But if somebody gives you a value in () , don't get excited. 但如果有人给你一个()的价值,不要兴奋。 Just throw it away. 把它扔掉

Sometimes it's fun to take type constructors parametrised by an "element type" and fill the parameter in with () . 有时采用由“元素类型”参数化的类型构造函数并使用()填充参数很有趣。 You can see just what information is inherent in the type constructor, rather than coming from the elements. 您可以看到类型构造函数中固有的信息,而不是来自元素。 Eg, Maybe () is a version of Bool , with Just () or Nothing . 例如, Maybe ()Bool一个版本,有Just ()Nothing Also, [()] amounts to the (possibly infinite) natural numbers: the only information you have is a length. 此外, [()]相当于(可能是无限的)自然数:您拥有的唯一信息是长度。

So, () means "Boring", but it's often a clue that something interesting is happening somewhere else. 所以, ()意思是“无聊”,但这通常是一个有趣的事情发生在其他地方的线索。

If not for the weird special syntax, it could be defined as 如果不是奇怪的特殊语法,它可以定义为

data () = ()

It's about the most boring type there is. 它是关于最无聊的类型。 The syntax is supposed to make you think of tuples: (a,b) is a pair, (a,b,c) is a triple, etc., and () is a 0-tuple. 语法应该让你想到元组: (a,b)是一对, (a,b,c)是三元组等, ()是0元组。 The only thing missing is a 1-tuple, which can't have that syntax because it would clash with the usual use of parentheses. 唯一缺少的是1元组,它不能具有该语法,因为它会与通常使用的括号冲突。

() is very often used as the result of something that has no interesting result. ()经常被用作没有有趣结果的东西的结果。 For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () . 例如,应该执行某些I / O并在不产生结果的情况下终止的IO操作通常具有类型IO () It can also be used when you need some uninteresting input; 它也可以在你需要一些无趣的输入时使用; GHC has special syntax for this, but in Haskell 98 the way to mimic Lisp's cond is like this: GHC有特殊的语法,但在Haskell 98中,模仿Lisp的cond是这样的:

case () of
  () | c1 -> e1
     | c2 -> e2
     ...
     | otherwise -> e3

It's perfectly valid, but also perfectly boring, to ask what value of type () you have; 它是完全有效的,但也非常无聊,要问你有什么类型的值() ; there's only one legitimate one that you could have. 你可以拥有的只有一个合法的。

From the "Haskell-as-almost-category" standpoint, () is a final object . 从“Haskell-as-almost-category”的观点来看, ()最终的对象 That is, for any type X , there is exactly one legitimate function of type X -> () , namely const () . 也就是说,对于任何类型X只有一个类型为X -> ()合法函数,即const () From the other direction, the Void type pigworker mentions is an initial object . 从另一个方向来看, Void型pigworker提到的是一个初始对象 For any type X , there is exactly one legitimate function of type Void -> X , namely absurd . 对于任何类型X只有一个类型为Void -> X合法函数Void -> X ,即absurd If you're in the mood for categorical reasoning, initial and final objects can be useful to have around. 如果您有分类推理的心情,初始和最终对象可能有用。

There are excellent search engines specialised for Haskell. 有专门针对Haskell的优秀搜索引擎。

http://hayoo.fh-wedel.de/?query=() will give you immediately the correct answer: http://hayoo.fh-wedel.de/?query=()会立即给你正确答案:

Data () Data ()
ghc-prim -GHC.Tuple ghc-prim -GHC.Tuple


The unit datatype () has one non-undefined member, the nullary constructor () . unit datatype ()有一个非undefined成员,nullary constructor ()

It's called the "unit type" . 它被称为“单位类型” You can think of it as Haskell's equivalent of void , in some respects. 在某些方面,您可以将其视为Haskell等同于void It's a type that has only one value (also () ). 它是一种只有一个值(也是() )的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM