Elm (programming language)
Paradigm | functional |
---|---|
Designed by | Evan Czaplicki |
First appeared | 2012 |
Stable release |
0.18
/ November 14, 2016 |
Typing discipline | static, strong, inferred |
License | Permissive (Revised BSD) [1] |
Filename extensions | .elm |
Website |
elm-lang |
Influenced by | |
Haskell, Standard ML, OCaml, F# | |
Influenced | |
Redux (application architecture)[2] |
Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice,"[3] made possible by the Elm compiler's static type checking.
History
Elm was initially designed by Evan Czaplicki as his thesis in 2012.[4] The first release of Elm came with many examples and an online editor that made it easy to try out in a web browser.[5] Evan Czaplicki joined Prezi in 2013 to work on Elm,[6] and in 2016 moved to NoRedInk as an Open Source Engineer, also starting the Elm Software Foundation.[7]
The initial implementation of the Elm compiler targets HTML, CSS, and JavaScript.[8] The set of core tools has continued to expand, now including a REPL,[9] package manager,[10] time-traveling debugger,[11] and installers for Mac and Windows.[12] Elm also has an ecosystem of community created libraries.[13]
Features
Elm has a small but expressive set of language constructs, including if-expressions, let-expressions, case-expressions, anonymous functions, and list interpolation.[14][15] From there the key features include immutability, static types, and interoperability with HTML, CSS, and JavaScript.
Immutability
All values in Elm are immutable, meaning that a value cannot be modified after it is created. Elm uses persistent data structures to implement its Array
, Dict
, and Set
libraries.[16]
Static Types
Elm is statically typed. Every definition in Elm can be given a type annotation that describes the exact shape of the value. Types include:
- primitive types such as integers and strings
- basic data structures such as lists, tuples, and extensible records
- custom types called tagged unions that let you build entirely new types[17]
Elm also supports full type inference, so the compiler can verify that a program is type-safe without any type annotations.
Module System
Elm has a module system that allows users to break their code into smaller parts called modules. Users can import and export values, making it possible to hide implementation details that other programmers do not need to think about. Modules form the basis of Elm's community library website, the Elm Public Library.
Interoperability with HTML, CSS, and JavaScript
Elm uses an abstraction called ports to communicate with JavaScript.[18] It allows values to flow in and out of Elm programs, making it possible to communicate between Elm and JavaScript.
Elm has a library called elm-html that a programmer can use to write HTML and CSS within Elm.[19] It uses a virtual DOM approach to make updates efficient.[20]
Limitations
Unlike Haskell, Elm has no support for higher-kinded types, and thus cannot provide generic abstractions for many common operations.[21] For example, there is no generic map
, apply
, fold
, or filter
function. Instead, such names are used prefixed by their module, such as List.map
and Dict.map
.
Tools
- Online editor at elm-lang.org/try for easy experimentation
- Elm Platform for installing the core tools locally
- Time-Traveling Debugger
- Learning resources and examples
- Core Libraries and Community Libraries
Example Code
-- This is a single line comment
{- This is a multi-line comment.
It can span multiple lines.
-}
{- It is possible to {- nest -} multi-line comments -}
-- Here we define a value named ''greeting''. The type is inferred as a String.
greeting =
"Hello World!"
-- It is best to add type annotations to top-level declarations.
hello : String
hello =
"Hi there."
-- Functions are declared the same way, with arguments following the function name.
add x y =
x + y
-- Again, it is best to add type annotations.
hypotenuse : Float -> Float -> Float
hypotenuse a b =
sqrt (a^2 + b^2)
-- If-expressions are used to branch on values
absoluteValue : Int -> Int
absoluteValue number =
if number < 0 then -number else number
-- Records are used to hold values with named fields
book : { title:String, author:String, pages:Int }
book =
{ title = "Steppenwolf"
, author = "Hesse"
, pages = 237
}
-- We can create entirely new types with the `type` keyword.
-- The following value represents a binary tree.
type Tree a
= Empty
| Node a (Tree a) (Tree a)
-- It is possible to inspect these types with case-expressions.
depth : Tree a -> Int
depth tree =
case tree of
Empty -> 0
Node value left right ->
1 + max (depth left) (depth right)
References
- ↑ https://github.com/evancz/Elm/blob/master/LICENSE
- ↑ Redux Prior Art
- ↑ "Elm home page".
- ↑ Elm: Concurrent FRP for Functional GUIs
- ↑ Elm's Online Editor
- ↑ Elm joins Prezi
- ↑ New Adventures for Elm
- ↑ Elm compiler source code
- ↑ Elm REPL announcement
- ↑ Elm Package Manager announcement
- ↑ Elm's Time-Traveling Debugger
- ↑ Elm Platform
- ↑ Elm Public Libraries
- ↑ The Syntax of Elm
- ↑ About Elm Elm features
- ↑ Elm Standard Libraries
- ↑ "Model The Problem". Elm. Retrieved 4 May 2016.
- ↑ Ports
- ↑ elm-html documentation
- ↑ Blazing Fast Html
- ↑ "Higher-Kinded types Not Expressible? #396". github.com/elm-lang/elm-compiler. Retrieved 6 March 2015.