编程与软件工程conal.net
C 语言是纯函数式的(2009)
The C language is purely functional (2009)
2009 — 2019,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 Conal Elliott 的一篇半开玩笑却意有所指的文章:C 程序员其实是在用纯函数式语言 cpp(C 预处理器)编程——cpp 表达式求值后产生一个类型为 C 的值(即命令式程序),再由 C 的运行时系统去执行它。作者借此讽刺 Haskell 社区:既然这种论证成立,那么 Haskell 的 monadic IO 也谈不上真正'纯粹',他呼吁大家重拾 monadic IO 出现之前那些更具想象力的函数式 I/O 研究。
为什么经久不衰:它用一个巧妙的归谬把'纯度'的边界问题摆上台面,至今仍是关于函数式编程、IO monad 与语言纯度讨论的经典思想实验。
HN 评论精选
点明文章真实意图:批评 Haskell 社区因 monadic IO 太舒适而放弃了更纯粹的函数式 I/O 探索。
如果有人没看懂文章的真正意图(我一开始就没看懂),作者在评论里明说了:可悲的是,许多 Haskell 程序员相信做'真正的编程'必须用 IO,于是把 Haskell 当 C 来用,把大量工作丢进 IO。monadic IO 作为函数式语言处理 I/O 的'解决方案'太舒适了,以至于几乎没人再去寻找真正(而非技术意义上)函数式的方案。在 monadic IO 出现之前,函数式 I/O 领域曾有大量充满活力的探索。monadic 命令式编程被接受后,Haskell 社区就像走进了鸦片馆,至今还躺在迷雾里。把 Haskell+IO 类比 cpp+C,不是贬低前者或抬高后者,而是想唤醒函数式程序员,提醒他们真正函数式编程有多有趣、多有创造力。
the_af查看英文原文
In case someone misses the point of the article -- I know I did, originally! -- the author explicitly lays it out in a comment: > "Sadly–and here is the real intent behind my post–many Haskell programmers believe that IO is necessary to do “real programming”, and they use Haskell as if it were C (relegating lots of work to IO). In other words, monadic IO has proved to be such a comfortable “solution” to I/O in a functional language, that very few folks are still searching for a genuinely (not merely technically) functional solution. Before monadic IO, there was a lot of vibrant and imaginative work on functional I/O. It hadn’t arrived yet, but was still in touch with the Spirit of functional programming. With the invention and acceptance of monadic imperative programming, it’s like the Haskell community wandered into an opium den and are still lying there in a fog. > [...] > "By relating Haskell+IO to cpp+C, I’m not trying to lower the former or elevate the latter. Instead, I’m trying to waken functional programmers out of the opium fog and remind them how fun and creative genuinely functional programming can be." I love Haskell but I'm not well-versed enough to know why other alternatives to monadic IO were discarded. I do know Haskell predates the IO monad.
提供历史背景:作者 Conal 是 FRP 先驱,一直主张用流/FRP 方式而非 monad 处理 I/O。
补充背景:Conal 在 Haskell 还没转向 monadic IO 之前,就与 Hudak 做过'经典'函数式响应式编程(FRP)的开创性工作。早期 Haskell 的 IO 基于 Miranda 式的流:程序是把操作系统事件流变换为操作系统命令流的变换,两者都是数据,这样写程序时可以 mock 操作系统而不必真正接触现实世界。Conal 不认为 monadic IO 是 IO 问题的最佳方案——'monadic IO 是纯的、指称式的与现实交互方式'这个说法相当站不住脚,这正是本文的论点:把这个想法推到逻辑终点,就会得出 cpp 也是纯的。Conal 转而研究 FRP 式的方案:写下的就是关于时间和带时间戳事件流的函数。
Chattered查看英文原文
For some context, conal did the pioneering work on "classic" Functional Reactive Programming with Hudak, before the Haskell language had moved to monadic IO. Originally, IO in Haskell was done based on streams as in Miranda from which it was inspired. A program is then a transformation on a stream of operating system events into a stream of operating system commands, both represented as data. That's pretty cool, because it makes it easy to write your program and then mock the operating system rather than actually touching the real world. The closest we have to this now is the "interact" function, which allows you to model your IO as a transformation of a stdin stream to a stdout stream. Conal isn't much convinced that monadic IO was the best solution to the IO problem. The idea that monadic IO is a "pure" or "denotational" way to interact with the real world is arguably pretty weak, and that's the point of conal's post here: if you take the idea to its logical conclusion, we might end up just concluding that cpp is pure and denotational. Instead, conal has worked on FRP approaches to interacting with the real world which are pure and have a very denotational feel to them: you're just writing down functions from time and a time-stamped stream of events. I still hold out enormous hope for this approach, but I'm not placing any bets on it.
指出论证的局限:把'纯'的定义推广到一切代码,反而让这个概念失去意义。
这是极端的夸张,但它说明了一点:C 程序员写的源代码只在运行时产生效果,编译器可以看作一个纯函数。关键是,产生副作用的是运行时系统,正如 Haskell 的 RTS 一样;唯一区别是 Haskell 用类型系统标注了效果。但另一方面,这种论证完全模糊了'纯代码'与'有副作用代码'的界限——如果一个术语适用于世上所有源代码,那它还有什么意义?
dmytrish查看英文原文
This is an extreme exaggeration, but it illustrates the point that a C programmer writes some source code that produces effects only at runtime and the compiler may be thought of as a pure function (though, with a bit of external state, but that is manageable in Haskell too). The point is, it is the runtime system that produces the side effects, just as Haskell RTS does; the only difference is that Haskell annotates effects with its type system. On the other hand, it completely blurs the line between "pure code" and "effectful code". Though the definition of "pure code" is rather tricky (we used to it in the Haskell meaning, with memory management completely abstracted away and explicit signatures of side-effect presence), but what's the point of a term that applies to all source code in existence?
用同样的论证结构套在 Java 上,幽默地展示该论证的普适性与荒谬之处。
Java 语言也是纯函数式的。'Java 程序员'其实不是在用 Java 编程,而是在用一种叫 cat 的纯函数式语言。cat 只由表达式组成,求值后产生一个类型为 Java 的纯值——一个表示命令式程序的抽象数据类型,再由 cat 的运行时系统执行。作为巧妙优化,cat 的 RTS 还内置了代码生成器,性能远超 Java 抽象数据类型的朴素实现。
earenndil查看英文原文
The Java language is purely functional. “Java programmers” really program not in Java, but in a purely functional language called 'cat'. As with any purely functional language, cat consists only of expressions, not statements. Now here’s the clever part: when evaluated (not executed), a cat expression yields a (pure) value of type Java, which is an ADT (abstract data type) that represents imperative programs. That value of type Java is then executed (not evaluated) by the cat language’s RTS (run-time system). As a clever optimization, cat’s RTS actually includes a code generator, considerably improving performance over more naïve implementations of the Java abstract data type.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选