Monad“拆箱”(Monad “unboxing”)

编程入门 行业动态 更新时间:2024-10-28 00:19:03
Monad“拆箱”(Monad “unboxing”)

我的问题出现在教程Functors,Applicatives和Monads In Pictures及其JavaScript版本之后 。

当文本说functor从上下文中展开值时,我理解正在发生Just 5 - > 5转换。 根据Haskell中“Just”语法的含义? ,Just是在Maybe monad中“定义范围”。

我的问题是整个展开的东西是如此神奇? 我的意思是,有一些语言规则自动解开“范围”变量的问题是什么? 在我看来,这个动作只是在某种表格中查找,其中符号Just 5对应于整数5 。

我的问题受到JavaScript版本的启发,其中Just 5是原型数组实例。 因此,实际上,解开根本不是火箭科学。

这是“计算”类型的理由还是“程序员”理由? 为什么我们在编程语言层面上将Just 5与5区分开来?

My question came up while following the tutorial Functors, Applicatives, And Monads In Pictures and its JavaScript version.

When the text says that functor unwraps value from the context, I understand that a Just 5 -> 5 transformation is happening. As per What does the "Just" syntax mean in Haskell? , Just is "defined in scope" of the Maybe monad.

My question is what is so magical about the whole unwrapping thing? I mean, what is the problem of having some language rule which automatically unwraps the "scoped" variables? It looks to me that this action is merely a lookup in some kind of a table where the symbol Just 5 corresponds to the integer 5.

My question is inspired by the JavaScript version, where Just 5 is prototype array instance. So unwrapping is, indeed, not rocket science at all.

Is this a "for-computation" type of reason or a "for-programmer" one? Why do we distinguish Just 5 from 5 on the programming language level?

最满意答案

首先,我不认为你可以理解Monads之类的东西而不理解类似Haskell的类型系统(即没有学习像Haskell这样的语言)。 是的,有许多教程声称不然,但我在学习Haskell之前已经阅读了很多这些教程,但我没有得到它。 所以我的建议是:如果你想了解Monads至少学习一些Haskell。

对于你的问题“为什么我们在编程语言层面上区分Just 5和5 ?”。 为了类型安全。 在大多数不是Haskell null语言中, nil , whatever ,通常用于表示缺少值。 然而,这通常会导致NullPointerExceptions事情,因为您没有预料到某个值可能不存在。

在Haskell中没有null 。 因此,如果您具有Int或其他任何类型的值,则该值不能为null 。 你保证有价值。 大! 但有时你实际上想要/需要编码缺少值。 在Haskell中,我们使用Maybe 。 所以类似Maybe Int东西可以是Just 5或Nothing 。 这种方式明确表示该值可能不存在,并且您不会意外忘记它可能是Nothing因为您必须显式解包该值。

这与Monads没有任何关系,除了Maybe恰好实现Monad类型类(如果你熟悉Java,类型类有点像Java接口)。 那也许主要不是Monad,但恰好也是Monad。

First of all, I don't think you can understand Monads and the like without understanding a Haskell like type system (i.e. without learning a language like Haskell). Yes, there are many tutorials that claim otherwise, but I've read a lot of them before learning Haskell and I didn't get it. So my advice: If you want to understand Monads learn at least some Haskell.

To your question "Why do we distinguish Just 5 from 5 on the programming language level?". For type safety. In most languages that happen not to be Haskell null, nil, whatever, is often used to represent the absence of a value. This however often results in things like NullPointerExceptions, because you didn't anticipate that a value may not be there.

In Haskell there is no null. So if you have a value of type Int, or anything else, that value can not be null. You are guarantied that there is a value. Great! But sometimes you actually want/need to encode the absence of a value. In Haskell we use Maybe for that. So something of type Maybe Int can either be something like Just 5 or Nothing. This way it is explicit that the value may not be there and you can not accidentally forget that it might be Nothing because you have to explicitly unwrap the value.

This has nothing really to do with Monads, except that Maybe happens to implement the Monad type class (a type class is a bit like a Java interface, if you are familiar with Java). That is Maybe is not primarily a Monad, but just happens to also be a Monad.

更多推荐

本文发布于:2023-08-02 07:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1370059.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Monad   拆箱   unboxing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!