Python的“与”一元? [英] Is Python's "with" monadic?

查看:108
本文介绍了Python的“与”一元?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像许多在我之前的傻瓜先锋一样,我正在努力穿越理解Monad的无轨荒地。

我仍然stag through不前,但我可以对于使用语句的Python <没有帮助。考虑这个片段:

  with open(input_filename,'r')as f:
for line in f:
process(line)

考虑 open()称为单元,块本身称为绑定。实际的monad没有公开(呃,除非 f 是monad),但模式在那里。不是吗?或者我只是把所有的FP误认为monadry?或者仅仅是早上3点,而且看起来似乎有道理?

一个相关的问题:如果我们有单子,我们是否需要例外?



在上面的片段中,I / O中的任何故障都可以从代码中隐藏。磁盘损坏,缺少指定文件和空文件都可以被视为相同。因此,不需要可见的IO异常。



当然,Scala的 Option typeclass已经消除了可怕的空指针异常。如果你重新考虑Monads(作为特殊情况, NaN DivideByZero )...



像我说的,早上3点。 / strong>



在定义下方, Wikipedia says
$ b


在面向对象的编程术语中,类型构造将对应于monadic类型的声明,单元函数承担构造函数方法的角色,并且绑定操作包含执行其注册回调(monadic函数)所必需的逻辑。


这听起来与上下文管理器协议完全相同,对象实现上下文管理器协议,语句。



来自@Owen in a comm在这篇文章中:


Monads在他们最基本的层面上,或多或少地是一种使用延续传球风格的很酷的方式:> > =需要一个制作者和一个回调;这也基本上是这样的:一个生产者像打开(...)和一个代码块,一旦它被创建。


完整的维基百科定义:


一种类型构造,为每种基础类型定义如何获得相应的一元类型。在Haskell的表示法中,monad的名称代表类型构造函数。如果M是monad的名称并且t是数据类型,那么M t是monad中的相应类型。


这听起来像我的上下文管理器协议


一个单位函数,将基础类型的值映射到相应一元类型的值。结果是相应类型中最简单的值,它完全保留了原始值(简单性被理解为适合单声道)。在Haskell中,这个函数被称为return,因为它在后面的描述中被使用。单位函数具有多态类型t→M t。

该对象实际执行上下文管理器协议。


多态类型(M t)→(t→M u)→(M u)的绑定操作,Haskell由中缀运算符表示> > =。它的第一个参数是一个monadic类型的值,第二个参数是一个函数,它从第一个参数的基础类型映射到另一个monadic类型,其结果在另一个monadic类型中。


这对应于 with 语句及其套件。

所以是的,我会说是一个monad。我搜索了 PEP 343 以及所有相关的被拒绝和撤回的PEP,并且没有他们提到了单词monad。它当然适用,但似乎语句的目标是资源管理,monad只是获取它的有用方法。 / p>

Like many a foolhardy pioneer before me, I'm endeavoring to cross the trackless wasteland that is Understanding Monads.

I'm still staggering through, but I can't help noticing a certain monad-like quality about Python's with statement. Consider this fragment:

with open(input_filename, 'r') as f:
   for line in f:
       process(line)

Consider the open() call as the "unit" and the block itself as the "bind". The actual monad isn't exposed (uh, unless f is the monad), but the pattern is there. Isn't it? Or am I just mistaking all of FP for monadry? Or is it just 3 in the morning and anything seems plausible?

A related question: if we have monads, do we need exceptions?

In the above fragment, any failure in the I/O can be hidden from the code. Disk corruption, the absence of the named file, and an empty file can all be treated the same. So no need for a visible IO Exception.

Certainly, Scala's Option typeclass has eliminated the dreaded Null Pointer Exception. If you rethought numbers as Monads (with NaN and DivideByZero as the special cases)...

Like I said, 3 in the morning.

解决方案

Yes.

Right below the definition, Wikipedia says:

In object-oriented programming terms, the type construction would correspond to the declaration of the monadic type, the unit function takes the role of a constructor method, and the binding operation contains the logic necessary to execute its registered callbacks (the monadic functions).

This sounds to me exactly like the context manager protocol, the implementation of the context manager protocol by the object, and the with statement.

From @Owen in a comment on this post:

Monads, at their most basic level, are more or less a cool way to use continuation-passing style: >>= takes a "producer" and a "callback"; this is also basically what with is: a producer like open(...) and a block of code to be called once it's created.

The full Wikipedia definition:

A type construction that defines, for every underlying type, how to obtain a corresponding monadic type. In Haskell's notation, the name of the monad represents the type constructor. If M is the name of the monad and t is a data type, then "M t" is the corresponding type in the monad.

This sounds like the context manager protocol to me.

A unit function that maps a value in an underlying type to a value in the corresponding monadic type. The result is the "simplest" value in the corresponding type that completely preserves the original value (simplicity being understood appropriately to the monad). In Haskell, this function is called return due to the way it is used in the do-notation described later. The unit function has the polymorphic type t→M t.

The actual implementation of the context manager protocol by the object.

A binding operation of polymorphic type (M t)→(t→M u)→(M u), which Haskell represents by the infix operator >>=. Its first argument is a value in a monadic type, its second argument is a function that maps from the underlying type of the first argument to another monadic type, and its result is in that other monadic type.

This corresponds to the with statement and its suite.

So yes, I'd say with is a monad. I searched PEP 343 and all the related rejected and withdrawn PEPs, and none of them mentioned the word "monad". It certainly applies, but it seems the goal of the with statement was resource management, and a monad is just a useful way to get it.

这篇关于Python的“与”一元?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆