𝚲

说说「直给风格」编程

如果你讨厌 Monad,你会喜欢它。如果你喜欢 Monad,你依旧会喜欢它。

Noel Welsh,2024 1

RT by Odersky

这个主题我憋了有段时间了,可想的愈多,就愈发难以开头。今天看到 Odersky 的 RT 2,忍不住蹭个热度,只当是给自己挖些坑先。什么时候来填,就看被催更的力度啦。

Monad 与 Effect 的联姻 3

1989年,Moggi 4 将范畴论里的 Monad 5 引入到了编程语言的世界 6。很快,Wadler 7 意识到 Monad 对于纯(Pure)函数式编程的巨大价值,并将其引入到 Haskell 当中,进而构建了一套系统来对付现实编程里的头号劲敌 —— Effect

Monads increase the ease with which programs may be modified. They can mimic the effect of impure features such as exceptions, state, and continuations; and also provide effects not easily achieved with such features.

Philip Wadler,1992 8

程序与真实世界的交互,离不开对 状态文件网络 的读写,有的操作可能需要是 异步 的,有的甚至还会遭遇 异常 情况,这些都是 Effect。当然,Monad 不是编程语言应对 Effect 的唯一办法 9,但 Wadler 认为它是一个更容易的办法。

由于 Haskell 在函数式语言中是旗帜般的存在,也令 Monad 几乎成为了函数式编程的代名词。我想,这大概是劝退不少想要学习函数式语言的人的主要原因吧。

Direct Style Scala

2023年的 Scalar Conference 上,Odersky 给在场的观众一个意外的惊喜,展示了用 Scala 3 的黑魔(语)法调教 Effect 的一些代码示例,这样的代码风格被他称为 Direct Style 10。而我管它叫「直给风格」,主打一个「返璞归真」。

// monadic-style 
val sum =
  for
    x <- Future(c1.read)
    y <- Future(c2.read)
  yield x + y

// direct-style 
val sum = Future:
  val f1 = Future(c1.read)
  val f2 = Future(c2.read)
  f1.value + f2.value

There will be a move away from monads as the primary way of code composition.

Martin Odersky,2023

简言之,Scala 3 能让程序员以「直给风格」代码应对 Effect ,且保留函数组合的优势。像是,标准库内建的 boundary & break、 异步编程库 Gears 11、和聚焦并发与弹性编程的 Ox 12

// Simple compositions
// No traverse or lift is needed
def acrobatics(xs: List[Future[Result[T, E]]]) : 
  Future[Result[List[T], E]] =
    Future:
      Result:
        xs.map(_.value.?)

与其去看 Odersky 的演讲视频 13,不如去看 Wampler 的「Scala 3: What Is “Direct Style”?」14 和 Welsh 的「Direct-style Effects Explained」15,更容易消化一些。

「能力」产生「作用」

// Effect
def f(): T throws E           

// Capability
def f()(using CanThrow[E]): T 

然而,Odersky 的野心远不止于此。

2022年,他在主题 Simple Scala 的演讲里加了一个彩蛋 —— Caprese,这个项目旨在探索一种创新性的类型系统,用 Capabilities(能力)这一概念,来捕获、跟踪、控制程序中的 Resources(资源) 和 Effects(作用)16

2023年底,ACM 上有一篇名为「Capturing Types」文章被发表 17,可视作项目公开的最新成果。

最后

2003 - A drunken Martin Odersky sees a Reese’s Peanut Butter Cup ad featuring somebody’s peanut butter getting on somebody else’s chocolate and has an idea. He creates Scala, a language that unifies constructs from both object oriented and functional languages. This pisses off both groups and each promptly declares jihad.

2003 年 - 醉酒的马丁·奥德斯基 (Martin Odersky) 看到里斯花生酱杯广告,其中有人将花生酱涂在了别人的巧克力上,他产生了一个想法。他创建了 Scala,这是一种统一了面向对象语言和函数式语言构造的语言。这激怒了两个团体,每个团体都立即宣布圣战。

JAMES IRY,2009 18

回首 Scala 这二十年,Odersky 一直致力于让编程语言变得更简洁更实用,也实实在在地引领着一众后继者 19。我很幸运地见证了其中的一大部分,并在学习和使用 Scala 的过程中收获众多编程的乐趣。

明天的 Scala 会是怎样的呢,请同我一起翘首以盼!


  1. https://twitter.com/noelwelsh/status/1783496769394950179↩︎

  2. https://twitter.com/odersky/status/1783885477646405805↩︎

  3. https://homepages.inf.ed.ac.uk/wadler/papers/effectstocl/effectstocl.pdf↩︎

  4. https://en.wikipedia.org/wiki/Eugenio_Moggi↩︎

  5. 中文译为“单子”,其实是个非常贴切的翻译,可以背后的概念太过抽象,中译文的采用反而会提升理解门槛。↩︎

  6. https://www.lfcs.inf.ed.ac.uk/reports/88/ECS-LFCS-88-66/ECS-LFCS-88-66.pdf↩︎

  7. https://en.wikipedia.org/wiki/Philip_Wadler↩︎

  8. https://jgbm.github.io/eecs762f19/papers/wadler-monads.pdf↩︎

  9. https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/history.pdf 参见第七节。↩︎

  10. Direct Style 的说法不是 Odersky 最先提及的,最早及起的可能是 Filinski 1994↩︎

  11. https://blog.nkagami.me/gears-book/introduction.html↩︎

  12. https://ox.softwaremill.com/latest/↩︎

  13. https://www.youtube.com/watch?v=0Fm0y4K4YO8↩︎

  14. https://medium.com/scala-3/scala-3-what-is-direct-style-d9c1bcb1f810↩︎

  15. https://www.inner-product.com/posts/direct-style-effects/↩︎

  16. https://youtu.be/-qf8yteuxPs?t=2468↩︎

  17. https://dl.acm.org/doi/10.1145/3618003↩︎

  18. https://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html↩︎

  19. https://youtu.be/sNos8aGjJMA↩︎