问题描述
感谢,我通常理解(清楚地以有限的方式)Haskell的也许,它的定义是
data也许a = Nothing |只是
然而我并不清楚为什么 Just 是这个定义的一部分。据我所知,这是 Just 本身的定义,但相关文档没有多说这个。
我是否认为在 Maybe 的定义中使用 Just 的主要好处是,而不是简单的
data也许a = Nothing | a
是它允许与进行模式匹配Just _ 和有用的功能,如 isJust 和 fromJust ?
为什么 Maybe 以前者的方式定义而不是后者? div>
Haskell的代数数据类型是标记的联合。按照设计,当你将两种不同的类型合并为另一种类型时,他们 有构造函数来消除它们的歧义。
你的定义不适合代数数据类型如何工作。
data也许a = Nothing | a
a 这里。我们如何告诉也许一个,除了正常的,未包装的 a 你的情况?
有一个只是构造函数,因为它 有设计构造 。
其他语言有,它可以像你想象的那样工作,但是它们不适合Haskell。他们在实践中表现出不同的结果,并且倾向于容易出错。
有一些强烈的设计理由让喜欢标记的联合会成为普通联合类型。他们在类型推断中表现良好。无论如何¹,实际代码中的工会通常都有标签。而且,从优雅的角度来看,标记的联合对语言来说是天生的适合,因为它们是产品(即元组和记录)的双重对象。如果您好奇,我会在。
脚注
在TypeScript和C中使用了联合类型:TypeScript编译为动态类型的JavaScript,这意味着它在运行时跟踪一个值的类型 - 基本上是一个标签。
C实际上并没有,但是类似90%的联合类型的使用或者具有标签或者有效地模拟结构子类型。我的一位教授实际上就如何在真实的C代码中使用工会做了一次实证研究,但我不记得它手头有什么纸。
Thanks to some excellent answers here, I generally understand (clearly in a limited way) the purpose of Haskell's Maybe and that its definition is
data Maybe a = Nothing | Just a
however I'm not entity clear exactly why Just is a part of this definition. As near as I can tell, this is where Just itself is defined, but the the relevant documentation doesn't say much about it.
Am I correct is thinking that the primary benefit of using Just in the definition of Maybe, rather than simply
data Maybe a = Nothing | a
is that it allows for pattern matching to with Just _ and for useful functionality like isJust and fromJust?
Why is Maybe defined in the former way rather than the latter?
Haskell's algebraic data types are tagged unions. By design, when you combine two different types into another type, they have to have constructors to disambiguate them.
Your definition does not fit with how algebraic data types work.
data Maybe a = Nothing | a
There's no "tag" for a here. How would we tell an Maybe a apart from a normal, unwrapped a in your case?
Maybe has a Just constructor because it has to have a constructor by design.
Other languages do have union types which could work like what you imagine, but they would not be a good fit for Haskell. They play out differently in practice and tend to be somewhat error-prone.
There are some strong design reasons for preferring tagged unions to normal union types. They play well with type inference. Unions in real code often have a tag anyhow¹. And, from the point of view of elegance, tagged unions are a natural fit to the language because they are the dual of products (ie tuples and records). If you're curious, I wrote about this in a blog post introducing and motivating algebraic data types.
footnotes
¹ I've played with union types in two places: TypeScript and C. TypeScript compiles to JavaScript which is dynamically typed, meaning it keeps track of the type of a value at runtime—basically a tag.
C doesn't but, in practice, something like 90% of the uses of union types either have a tag or effectively emulate struct subtyping. One of my professors actually did an empirical study on how unions are used in real C code, but I don't remember what paper it was in off-hand.
这篇关于为什么Maybe包含Just?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!