Paradigm | functional |
---|---|
Family | Haskell |
Designed by | Evan Czaplicki |
First appeared | March 30, 2012 [1] |
Stable release | 0.19.1 / October 21, 2019 [2] |
Typing discipline | static, strong, inferred |
Platform | x86-64 |
OS | macOS, Windows |
License | Permissive (Revised BSD) [3] |
Filename extensions | .elm |
Website | elm-lang |
Influenced by | |
Haskell, Standard ML, OCaml, F# | |
Influenced | |
Redux, [4] Rust, [5] Vue, [6] Roc, [7] Derw, [8] Gren [9] |
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", [10] made possible by the Elm compiler's static type checking.
Elm was initially designed by Evan Czaplicki as his thesis in 2012. [11] The first release of Elm came with many examples and an online editor that made it easy to try out in a web browser. [12] Mr. Czaplicki joined Prezi in 2013 to work on Elm, [13] and in 2016 moved to NoRedInk as an Open Source Engineer, also starting the Elm Software Foundation. [14]
The initial implementation of the Elm compiler targets HyperText Markup Language (HTML), Cascading Style Sheets (CSS), and JavaScript. [15] The set of core tools has continued to expand, now including a read–eval–print loop (REPL), [16] package manager, [17] time-travelling debugger, [18] and installers for macOS and Windows. [19] Elm also has an ecosystem of community created libraries, [20] and Ellie, an advanced online editor that allows saved work and including community libraries. [21]
Elm has a small set of language constructs, including traditional if-expressions, let-expressions for storing local values, and case-expressions for pattern matching. [22] As a functional language, it supports anonymous functions, functions as arguments, and functions can return functions, the latter often by partial application of curried functions. Functions are called by value. Its semantics include immutable values, stateless functions, and static typing with type inference. Elm programs render HTML through a virtual DOM, and may interoperate with other code by using "JavaScript as a service".
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 arrays, sets, and dictionaries in the standard library. [23]
Elm is statically typed. Type annotations are optional (due to type inference) but strongly encouraged. Annotations exist on the line above the definition (unlike C-family languages where types and names are interspersed). Elm uses a single colon to mean "has type".
Types include primitives like integers and strings, and basic data structures such as lists, tuples, and records. Functions have types written with arrows, for example round : Float -> Int
. Custom types allow the programmer to create custom types to represent data in a way that matches the problem domain. [24]
Types can refer to other types, for example a List Int
. Types are always capitalized; lowercase names are type variables. For example, a List a
is a list of values of unknown type. It is the type of the empty list and of the argument to List.length
, which is agnostic to the list's elements. There are a few special types that programmers create to interact with the Elm runtime. For example, Html Msg
represents a (virtual) DOM tree whose event handlers all produce messages of type Msg
.
Rather than allow any value to be implicitly nullable (such as JavaScript's undefined
or a null pointer), Elm's standard library defines a Maybe a
type. Code that produces or handles an optional value does so explicitly using this type, and all other code is guaranteed a value of the claimed type is actually present.
Elm provides a limited number of built-in type classes: number
which includes Int
and Float
to facilitate the use of numeric operators such as (+)
or (*)
, comparable
which includes numbers, characters, strings, lists of comparable things, and tuples of comparable things to facilitate the use of comparison operators, and appendable
which includes strings and lists to facilitate concatenation with (++)
. Elm does not provide a mechanism to include custom types into these type classes or create new type classes (see Limits).
Elm has a module system that allows users to break their code into smaller parts called modules. Modules can hide implementation details such as helper functions, and group related code together. Modules serve as a namespace for imported code, such as Bitwise.and
. Third party libraries (or packages) consist of one or more modules, and are available from the Elm Public Library. All libraries are versioned according to semver, which is enforced by the compiler and other tools. That is, removing a function or changing its type can only be done in a major release.
Elm uses an abstraction called ports to communicate with JavaScript. [25] 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. [26] It uses a virtual DOM approach to make updates efficient. [27]
Elm does not officially support server-side development. The core development team does not consider it as their primary goal and prefers to focus development on the enhancement of front-end development experience. Nevertheless, there are several independent projects, which attempt to explore possibilities to use Elm for the back-end. The projects are mainly stuck on Elm version 0.18.0 since newer ones do not support "native" code and some other utilized features. There are two attempts to use Elm with BEAM (Erlang virtual machine). One of the projects executes Elm directly on the environment [28] while another one compiles it to Elixir. [29] Also, there was an attempt to create a back-end framework for Elm powered by Node.js infrastructure. [30] None of the projects are production-ready.
The Elm Architecture is a software design pattern and as a TLA called TEA pattern for building interactive web applications. Elm applications are naturally constructed in that way, but other projects may find the concept useful.
An Elm program is always split into three parts:
Those are the core of the Elm Architecture.
For example, imagine an application that displays a number and a button that increments the number when pressed. [31] In this case, all we need to store is one number, so our model can be as simple as type alias Model = Int
. The view
function would be defined with the Html
library and display the number and button. For the number to be updated, we need to be able to send a message to the update
function, which is done through a custom type such as type Msg = Increase
. The Increase
value is attached to the button defined in the view
function such that when the button is clicked by a user, Increase
is passed on to the update
function, which can update the model by increasing the number.
In the Elm Architecture, sending messages to update
is the only way to change the state. In more sophisticated applications, messages may come from various sources: user interaction, initialization of the model, internal calls from update
, subscriptions to external events (window resize, system clock, JavaScript interop...) and URL changes and requests.
Elm does not support higher-kinded polymorphism, [32] which related languages Haskell, Scala and PureScript offer, nor does Elm support the creation of type classes.
This means that, for example, Elm does not have a generic map
function which works across multiple data structures such as List
and Set
. In Elm, such functions are typically invoked qualified by their module name, for example calling List.map
and Set.map
. In Haskell or PureScript, there would be only one function map
. This is a known feature request that is on Mr. Czaplicki's rough roadmap since at least 2015. [33] On the other hand, implementations of TEA pattern in advanced languages like Scala does not suffer from such limitations and can benefit from Scala's type classes, type-level and kind-level programming constructs. [34]
Another outcome is a large amount of boilerplate code in medium to large size projects as illustrated by the author of "Elm in Action" in their single page application example [35] with almost identical fragments being repeated in update, view, subscriptions, route parsing and building functions.
-- This is a single line comment.{-This is a multi-line comment.It is {- nestable. -}-}-- 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:Stringhello="Hi there."-- Functions are declared the same way, with arguments following the function name.addxy=x+y-- Again, it is best to add type annotations.hypotenuse:Float->Float->Floathypotenuseab=sqrt(a^2+b^2)-- We can create lambda functions with the `\[arg] -> [expression]` syntax.hello:String->Stringhello=\s->"Hi, "++s-- Function declarations may have the anonymous parameter names denoted by `_`, -- which are matched but not used in the body. const:a->b->aconstk_=k-- Functions are also curried; here we've curried the multiplication -- infix operator with a `2`multiplyBy2:number->numbermultiplyBy2=(*)2-- If-expressions are used to branch on `Bool` valuesabsoluteValue:number->numberabsoluteValuenumber=ifnumber<0thennegatenumberelsenumber-- Records are used to hold values with named fieldsbook:{title:String,author:String,pages:Int}book={title="Steppenwolf",author="Hesse",pages=237}-- Record access is done with `.`title:Stringtitle=book.title-- Record access `.` can also be used as a functionauthor:Stringauthor=.authorbook-- We can create tagged unions with the `type` keyword.-- The following value represents a binary tree.typeTreea=Empty|Nodea(Treea)(Treea)-- It is possible to inspect these types with case-expressions.depth:Treea->Intdepthtree=casetreeofEmpty->0Node_leftright->1+max(depthleft)(depthright)
A quine is a computer program that takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".
OCaml is a general-purpose, high-level, multi-paradigm programming language which extends the Caml dialect of ML with object-oriented features. OCaml was created in 1996 by Xavier Leroy, Jérôme Vouillon, Damien Doligez, Didier Rémy, Ascánder Suárez, and others.
F# is a general-purpose, high-level, strongly typed, multi-paradigm programming language that encompasses functional, imperative, and object-oriented programming methods. It is most often used as a cross-platform Common Language Infrastructure (CLI) language on .NET, but can also generate JavaScript and graphics processing unit (GPU) code.
D, also known as dlang, is a multi-paradigm system programming language created by Walter Bright at Digital Mars and released in 2001. Andrei Alexandrescu joined the design and development effort in 2007. Though it originated as a re-engineering of C++, D is now a very different language. As it has developed, it has drawn inspiration from other high-level programming languages. Notably, it has been influenced by Java, Python, Ruby, C#, and Eiffel.
In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context.
In some programming languages, eval
, short for evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval
. The input to eval
is not necessarily a string; it may be structured representation of code, such as an abstract syntax tree, or of special type such as code
. The analog for a statement is exec, which executes a string as if it were a statement; in some languages, such as Python, both are present, while in other languages only one of either eval
or exec
is.
In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.
Scala is a strong statically typed high-level general-purpose programming language that supports both object-oriented programming and functional programming. Designed to be concise, many of Scala's design decisions are intended to address criticisms of Java.
A webform, web form or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields. For example, forms can be used to enter shipping or credit card data to order a product, or can be used to retrieve search results from a search engine.
Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under an MIT License. The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.
TypeScript is a free and open-source high-level programming language developed by Microsoft that adds static typing with optional type annotations to JavaScript. It is designed for the development of large applications and transpiles to JavaScript.
JSDoc is a markup language used to annotate JavaScript source code files. Using comments containing JSDoc, programmers can add documentation describing the application programming interface of the code they're creating. This is then processed, by various tools, to produce documentation in accessible formats like HTML and Rich Text Format. The JSDoc specification is released under CC BY-SA 3.0, while its companion documentation generator and parser library is free software under the Apache License 2.0.
In computer programming, string interpolation is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing or, in formal terms, a form of quasi-quotation. The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current context.
Dart is a programming language designed by Lars Bak and Kasper Lund and developed by Google. It can be used to develop web and mobile apps as well as server and desktop applications.
asm.js is a subset of JavaScript designed to allow computer software written in languages such as C to be run as web applications while maintaining performance characteristics considerably better than standard JavaScript, which is the typical language used for such applications.
Idris is a purely-functional programming language with dependent types, optional lazy evaluation, and features such as a totality checker. Idris may be used as a proof assistant, but is designed to be a general-purpose programming language similar to Haskell.
Swift is a high-level general-purpose, multi-paradigm, compiled programming language created by Chris Lattner in 2010 for Apple Inc. and maintained by the open-source community. Swift compiles to machine code and uses an LLVM-based compiler. Swift was first released in June 2014 and the Swift toolchain has shipped in Xcode since Xcode version 6, released in September 2014.
Nim is a general-purpose, multi-paradigm, statically typed, compiled high-level system programming language, designed and developed by a team around Andreas Rumpf. Nim is designed to be "efficient, expressive, and elegant", supporting metaprogramming, functional, message passing, procedural, and object-oriented programming styles by providing several features such as compile time code generation, algebraic data types, a foreign function interface (FFI) with C, C++, Objective-C, and JavaScript, and supporting compiling to those same languages as intermediate representations.
Reason, also known as ReasonML, is a general-purpose, high-level, multi-paradigm, functional and object-oriented programming language and syntax extension and toolchain for OCaml created by Jordan Walke, who also created the React framework, at Facebook. Reason uses many syntax elements from JavaScript, compiles to native code using OCaml's compiler toolchain, and can compile to JavaScript using the ReScript compiler.
Those of you familiar with the Elm style may recognize that the updated --explain messages draw heavy inspiration from the Elm approach.
Roc is a direct descendant of the Elm programming language. The two languages are similar, but not the same!