# Elm（编程语言）

**Elm** 是一门纯函数式、静态类型的编程语言，用于构建网页浏览器用户界面，由埃文·查普利基（Evan Czaplicki）在其2012年哈佛大学本科毕业论文《Elm：面向函数式图形用户界面的并发FRP》中创造<sup>[1](https://elm-lang.org/assets/papers/concurrent-frp.pdf)</sup>。Elm 编译为 [JavaScript](https://www.edgepedia.org/zh/javascript)，其设计围绕两个主张：一是具备完整类型推断的强类型系统在实践中可以消除运行时异常，二是所有交互式程序都可以用同一种模式来组织，即 Elm 架构<sup>[1](https://elm-lang.org/assets/papers/concurrent-frp.pdf)</sup><sup> • </sup><sup>[2](https://guide.elm-lang.org/types/)</sup><sup> • </sup><sup>[3](https://guide.elm-lang.org/architecture/)</sup>。

| 关键事实 | 详情 |
|---|---|
| 起源 | 由埃文·查普利基在《Elm：面向函数式图形用户界面的并发FRP》中提出，哈佛大学本科毕业论文，2012年3月30日<sup>[1](https://elm-lang.org/assets/papers/concurrent-frp.pdf)</sup> |
| 编译目标 | JavaScript，由命令行编译器生成；`elm make` 默认生成 HTML<sup>[1](https://elm-lang.org/assets/papers/concurrent-frp.pdf)</sup><sup> • </sup><sup>[4](https://guide.elm-lang.org/interop/)</sup> |
| 核心模式 | Elm 架构：模型（状态）、视图（状态转为 HTML）、更新（根据消息改变状态）<sup>[3](https://guide.elm-lang.org/architecture/)</sup> |
| 类型系统 | 静态类型并带类型推断；编译器在程序运行前报告诸如记录字段拼写错误之类的不匹配<sup>[2](https://guide.elm-lang.org/types/)</sup> |
| JavaScript 互操作 | 没有传统的外部函数接口；三种机制：标志、端口和自定义元素<sup>[5](https://guide.elm-lang.org/interop/limits)</sup><sup> • </sup><sup>[4](https://guide.elm-lang.org/interop/)</sup> |
| 当前版本 | 0.19.3，自 0.19.1 起语言无变化<sup>[6](https://github.com/elm/compiler/releases)</sup> |
| 生产规模示例 | 乐天（Rakuten）报告称，在投入生产两年多后（2021年），其多个应用中共有约10万行 Elm 代码<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup> |

## Elm 架构

每个 Elm 程序都分为三部分：保存应用状态的**模型**、将状态转为 HTML 的**视图**，以及在消息到达时修订状态的**更新**<sup>[3](https://guide.elm-lang.org/architecture/)</sup>。程序生成 HTML，运行时发回描述所发生事件的消息（按钮点击、文本字段输入），更新函数则产生新的模型。官方指南将这一模式描述为早期 Elm 程序员在自己代码中不断重新发现的东西，而非事先强加的设计<sup>[3](https://guide.elm-lang.org/architecture/)</sup>。

这一模式已经跨越到其他生态系统。Redux 的文档将 Elm 架构列为先行技术，InfoQ 则列出了向 [Rust](https://www.edgepedia.org/zh/rust-programming-language)/Wasm（yew）、JavaScript（ferp、AppRun）和 [TypeScript](https://www.edgepedia.org/zh/typescript)（elm-ts）的移植，以及对 Redux 等库和 SwiftUI 的影响<sup>[8](https://redux.js.org/understanding/history-and-design/prior-art)</sup><sup> • </sup><sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。

## 类型与错误消息

Elm 的类型系统采用 Hindley-Milner 风格的类型推断：编译器分析值在程序中的流动方式，并以指南所称的友好消息报告错误<sup>[2](https://guide.elm-lang.org/types/)</sup>。一个有据可查的例子显示，拼错的记录字段（`fistName` 而非 `firstName`）会产生一个 TYPE MISMATCH 错误，该错误会指出两个字段并提示拼写错误，而等效的 JavaScript 代码则会静默地产生字符串 `"undefined Hesse"`<sup>[2](https://guide.elm-lang.org/types/)</sup>。

类型系统也为包管理器提供支持。由于破坏 API 的变更会被自动检测，Elm 包管理器强制执行语义化版本控制，将破坏性变更提升为主版本更新<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。自定义类型——旧称联合类型，在其他地方被称为标签联合或代数数据类型——让程序员能够枚举一个值的所有可能状态，编译器则强制要求处理每一种情况<sup>[9](https://guide.elm-lang.org/types/custom_types)</sup>。

## JavaScript 互操作及其限制

Elm 有意不提供传统的外部函数接口（FFI），即 Scala、Clojure、[Python](https://www.edgepedia.org/zh/python-programming-language) 和 Haskell 等语言用来直接调用宿主语言函数的机制<sup>[5](https://guide.elm-lang.org/interop/limits)</sup>。取而代之的是三种机制：用于将初始数据传入程序的**标志**、用于与 JavaScript 进行基于消息的通信的**端口**，以及用于嵌入 JavaScript 组件的**自定义元素**<sup>[4](https://guide.elm-lang.org/interop/)</sup>。

端口在 `port module` 中声明为带类型的通道：出站端口如 `port sendMessage : String -> Cmd msg` 将值发送给 JavaScript，入站端口如 `port messageReceiver : (String -> msg) -> Sub msg` 则监听返回的值<sup>[10](https://guide.elm-lang.org/interop/ports)</sup>。指南建议围绕“谁拥有哪个状态”这一问题组织少量端口，而不是为每个 JavaScript 函数设一个端口<sup>[10](https://guide.elm-lang.org/interop/ports)</sup>。

拒绝 FFI 的明确理由是维护保证和避免包泛滥<sup>[5](https://guide.elm-lang.org/interop/limits)</sup>。如果可以调用任意 JavaScript，语言就无法再承诺函数不会修改数据或抛出意外异常；指南还认为，直接照搬 JavaScript API 的绑定会淹没包生态系统，并将这种压力描述为编译到 JavaScript 的语言所特有的<sup>[5](https://guide.elm-lang.org/interop/limits)</sup>。对采用者而言，这一取舍是真实的：在 Elm 0.19 及以上版本中，非核心团队的 Elm 代码无法同步调用 JavaScript 函数，InfoQ 报道称这一限制既促成了无运行时异常的保证，也导致一些开发者放弃了这门语言<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。乐天的评估是，Elm 可能不适合需要与第三方 JavaScript 库大量集成的短期项目<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。

## 编译与优化

编译器用 Haskell 编写，在 elm/compiler 仓库中开发<sup>[11](https://www.ohrg.org/a-comparison-of-elm-and-react)</sup><sup> • </sup><sup>[6](https://github.com/elm/compiler/releases)</sup>。其流水线依次解析源码、规范化变量、运行类型推断、执行杂项检查、优化并生成代码<sup>[12](https://github.com/elm-community/elm-compiler-docs)</sup>。

官方指南强调两个优化目标：资源体积和运行时性能<sup>[13](https://github.com/elm/compiler/blob/master/hints/optimize.md)</sup>。生产构建使用 `--optimize` 标志编译，该标志会缩短记录字段名并对值进行拆箱，然后用带有 `pure_funcs` 和 `unsafe` 等特殊标志的 `uglifyjs` 处理输出；这些转换在普通 JavaScript 中并不可靠，但在 Elm 输出上是安全的，因为 Elm 代码没有副作用<sup>[13](https://github.com/elm/compiler/blob/master/hints/optimize.md)</sup>。0.19 版本通过生成与 JavaScript 压缩器配合更好的代码，以及根据优化级别选择自定义类型的运行时表示，减小了资源体积<sup>[5](https://guide.elm-lang.org/interop/limits)</sup>。

关于运行时性能，官方指南直言不讳地指出时间花在哪里：DOM 是浏览器中最慢的东西，而且差距巨大；作者发现唯一能带来显著差异的技术是使用 `Html.Lazy` 和 `Html.Keyed` 来减少 DOM 操作；更好的数据结构和在模型中缓存计算结果则收效甚微<sup>[14](https://guide.elm-lang.org/optimization/)</sup>。在 Elm 创造者发布于 js-framework-benchmark 的一项比较中，键控变体在性能上 Elm 得分为1.25，原生 JavaScript 为1.00，Svelte 3.23.0 为1.28，React 16.8.6 为2.09，启动时间分别为1.19、1.01、1.00和1.65<sup>[15](https://discourse.elm-lang.org/t/can-the-compiler-skip-virtual-dom/6300/2)</sup>。

一个实现细节会影响性能：编译器生成 JavaScript 时无法访问 Elm 类型信息，这些信息在流水线更早的阶段已被丢弃，因此某些类型上的相等性判断要经过自定义的 `_Utils_eq` 函数，比 JavaScript 内置的 `==` 运算符更慢<sup>[12](https://github.com/elm-community/elm-compiler-docs)</sup><sup> • </sup><sup>[16](https://discourse.elm-lang.org/t/performance-optimization/5105)</sup>。编译器还会在生成的 JavaScript 中按字母顺序排列记录字段，该语言的创造者指出，这是 Elm 代码往往比手写 JavaScript 更快的原因之一<sup>[17](https://discourse.elm-lang.org/t/optimizing-for-javascript-is-hard/8571)</sup>。

## 采用与生态系统

Elm 专门面向单页 Web 应用开发，这使其能够在用户干预最少的情况下针对性能和小资源体积调优输出，与典型 JavaScript 前端工具链中的打包器、插件和配置文件形成对比<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。一份精选列表追踪了在生产中使用 Elm 的公司，涵盖汽车（CARFAX、carwow、Arrival）、银行（CBANC Network、Nordea）等行业<sup>[18](https://github.com/jah2488/elm-companies)</sup>。

乐天的经历体现了正反两面。该公司于2017年夏天在其柏林分部开始采用 Elm，此前一个原生 JavaScript 单页应用变得难以维护；到2021年，其多个应用中共有约10万行 Elm 代码，公司看重函数式 UI 方法、类型系统以及没有运行时异常<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。它报告的缺点来自 Elm 并非主流：谷歌和 Stack Overflow 上可搜索的资源较少，以及需要编写自定义库，例如受 react-jsonschema-form 启发的表单库<sup>[7](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)</sup>。

与 npm 相比，包生态系统很小，但现有的包带有与语言本身相同的保证<sup>[19](https://www.oreilly.com/library/view/why-elm/9781491990728/ch06.html)</sup>。发布节奏缓慢：0.19.0 于2018年8月发布，随后是0.19.1、0.19.2（编译器性能升级）和0.19.3（编译器基础设施升级），均无语言变化<sup>[6](https://github.com/elm/compiler/releases)</sup>。一篇2026年的评论将此描述为 Elm 稳定性承诺的代价，指出旧的 Elm 项目仍能构建，而 JavaScript 项目需要不断更新包，且0.19.2 在0.19.1 之后6.7年才到来<sup>[20](https://www.javacodegeeks.com/2026/10/elm-a-quiet-rebuke-to-javascript-framework-churn-and-the-price-of-its-promise.html)</sup>。

## References

1. [Evan Czaplicki (2012). Elm: Concurrent FRP for Functional GUIs. Harvard University.](https://elm-lang.org/assets/papers/concurrent-frp.pdf)
2. [Types, An Introduction to Elm](https://guide.elm-lang.org/types/)
3. [The Elm Architecture, An Introduction to Elm](https://guide.elm-lang.org/architecture/)
4. [JavaScript Interop, An Introduction to Elm](https://guide.elm-lang.org/interop/)
5. [The Limits of Elm/JS Interop, An Introduction to Elm](https://guide.elm-lang.org/interop/limits)
6. [Releases, elm/compiler, GitHub](https://github.com/elm/compiler/releases)
7. [Bruno Couriol (2021). 100,000+ Lines of Elm Code in Production: Rakuten Shares Lessons Learnt. InfoQ.](https://www.infoq.com/news/2021/10/elm-lessons-learnt-production/)
8. [Prior Art, Redux documentation](https://redux.js.org/understanding/history-and-design/prior-art)
9. [Custom Types, An Introduction to Elm](https://guide.elm-lang.org/types/custom_types)
10. [Ports, An Introduction to Elm](https://guide.elm-lang.org/interop/ports)
11. [A comparison of Elm and React (in code)](https://www.ohrg.org/a-comparison-of-elm-and-react)
12. [elm-compiler-docs, elm-community, GitHub](https://github.com/elm-community/elm-compiler-docs)
13. [How to optimize Elm code, elm/compiler hints, GitHub](https://github.com/elm/compiler/blob/master/hints/optimize.md)
14. [Optimization, An Introduction to Elm](https://guide.elm-lang.org/optimization/)
15. [Can the compiler skip virtual DOM? Elm Discourse](https://discourse.elm-lang.org/t/can-the-compiler-skip-virtual-dom/6300/2)
16. [Performance Optimization, Elm Discourse](https://discourse.elm-lang.org/t/performance-optimization/5105)
17. [Optimizing for JavaScript is hard, Elm Discourse](https://discourse.elm-lang.org/t/optimizing-for-javascript-is-hard/8571)
18. [elm-companies: companies using Elm in production, GitHub](https://github.com/jah2488/elm-companies)
19. [A Tour of the Ecosystem, Why Elm? (O'Reilly)](https://www.oreilly.com/library/view/why-elm/9781491990728/ch06.html)
20. [Elm: A Quiet Rebuke to JavaScript Framework Churn, and the Price of Its Promise, Java Code Geeks (2026)](https://www.javacodegeeks.com/2026/10/elm-a-quiet-rebuke-to-javascript-framework-churn-and-the-price-of-its-promise.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Programming languages › Individual programming languages › Functional, logic, and array languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/elm-programming-language

English original: https://www.edgepedia.org/elm-programming-language
