Haskell程序可以类似于将消息传递给彼此的对象吗?

Haskell是一种纯函数式语言,它突破了传统的面向对象语言.但是,请考虑Alan Kay关于OOP“真实”含义的以下引用:

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I’m not aware of them. — Alan Kay

后来:

I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages (so messaging came at the very beginning — it took a while to see how to do messaging in a programming language efficiently enough to be useful).

我很好奇在Haskell中可以在多大程度上实现这种编程风格.特别是,是否可以将Haskell程序构造为一系列(类似的)封装对象,它们来回传递消息?

注意:我正在寻找特定于Haskell的示例,而不是大型函数语言(当发生冲突时).

最佳答案

only messaging, local retention and protection and hiding of
state-process

like biological cells and/or individual computers on a network, only
able to communicate with messages

我相信某些Haskell编程模式在某种程度上确实类似于Kay的描述.

在像conduit,pipesstreaming这样的流式库中,通常将计算构建为由不同阶段组成的管道.管道的每个部分都完全独立于其他部分,并且可以保持其自己的私有状态(您也可以在管道中具有“共享”状态).

拓扑往往是线性和单向的.也就是说,像管道ZipSink这样的抽象 – 以及foldl包中的Folds的Applicative实例 – 让你构建分支出来的“树状”拓扑.管道可以是bidirectional,虽然我没有看到很多使用它的例子.

然后是arrowized functional reactive programming.它可以让你构建自动机箭头的“电路”can even include loops.电路的每个部分都可以保持自己的状态.正如netwire FRP库的描述所述:

This library provides interfaces for and implements wire arrows useful
both for functional reactive programming (FRP) and locally stateful
programming (LSP).

auto库的文档:

auto works by providing a type that encapsulates value stream
transformers, or locally stateful functions; by specifying your
program as a (potentially cyclic) graph of relationships between value
streams, you create a way of declaring a system based simply on static
relationships between quantities.

Instead of a state monad type solution, where all functions have
access to a rigid global state, auto works by specifying relationships
which each exist independently and on their own, without any global
state.

点赞