Elm (programming language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Add Vue.js to list of influenced, direct statement from Vue.js about Elm's influence
Revise features section
Line 2: Line 2:
| name = Elm
| name = Elm
| logo = [[File:Elm logo.svg|120px|Logo of Elm|The Elm tangram]]
| logo = [[File:Elm logo.svg|120px|Logo of Elm|The Elm tangram]]
| paradigm = [[Functional programming|functional]]
| paradigm = [[Functional programming|Functional]]
| year = 2012
| year = 2012
| latest release version = 0.18
| latest release version = 0.18
Line 8: Line 8:
| influenced_by = [[Haskell (programming language)|Haskell]], [[Standard ML]], [[OCaml]], [[F Sharp (programming language)|F#]]
| influenced_by = [[Haskell (programming language)|Haskell]], [[Standard ML]], [[OCaml]], [[F Sharp (programming language)|F#]]
| influenced = [[Redux (JavaScript library)|Redux]]<ref>[http://redux.js.org/docs/introduction/PriorArt.html Redux Prior Art]</ref>, [[Vue.js|Vue.js]]<ref>[https://vuejs.org/v2/guide/comparison.html#Scale Elm Influenced Vue.js]</ref>
| influenced = [[Redux (JavaScript library)|Redux]]<ref>[http://redux.js.org/docs/introduction/PriorArt.html Redux Prior Art]</ref>, [[Vue.js|Vue.js]]<ref>[https://vuejs.org/v2/guide/comparison.html#Scale Elm Influenced Vue.js]</ref>
| typing = [[static typing|static]], [[strong typing|strong]], [[type inference|inferred]]
| typing = [[static typing|Static]], [[strong typing|Strong]], [[type inference|Inferred]]
| designer = Evan Czaplicki
| designer = Evan Czaplicki
| website = {{URL|elm-lang.org}}
| website = {{URL|elm-lang.org}}
Line 23: Line 23:


== Features ==
== Features ==
Elm has a small but expressive set of language constructs, including if-expressions, let-expressions, case-expressions, anonymous functions, and list interpolation.<ref name="syntax">[http://elm-lang.org/learn/Syntax.elm The Syntax of Elm]</ref><ref>[http://elm-lang.org/About.elm About Elm] Elm features</ref> From there the key features include immutability, static types, and interoperability with HTML, CSS, and JavaScript.
Elm has a small but expressive set of language constructs, including traditional if-expressions, let-expressions for local state, and case-expressions for [[pattern matching]].<ref name="syntax">[http://elm-lang.org/learn/Syntax.elm The Syntax of Elm]</ref> As a functional language, it supports anonymous functions, functions as arguments, and partial application ([[currying]]) by default. Its semantics include immutable values, [[pure function|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".


=== Immutability ===
=== Immutability ===
Line 29: Line 29:


=== Static types ===
=== Static types ===
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".
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 union]]s that let you build entirely new types<ref>{{cite web|title=Model The Problem|url=http://elm-lang.org/guide/model-the-problem|website=Elm|accessdate=4 May 2016|ref=tagged-unions}}</ref>


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 <code>round : Float -> Int</code>. [[tagged union|Union types]] allow the programmer to create custom types to represent data in a way that matches the problem domain.<ref>{{cite web|title=Model The Problem|url=http://elm-lang.org/guide/model-the-problem|website=Elm|accessdate=4 May 2016|ref=tagged-unions}}</ref>
Elm also supports full type inference, so the compiler can verify that a program is type-safe without any type annotations.

Types can refer to other types, for example a <code>List Int</code>. Types are always capitalized; lowercase names are type variables. For example a <code>List a</code> is a list of values of unknown type. It is the type of the empty list and of the argument to <code>List.length</code>, 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, <code>Html Msg</code> represents a (virtual) DOM tree whose event handlers all produce messages of type <code>Msg</code>.

Rather than allow any value to be implicitly nullable (such a JavaScript's <code>undefined</code> or a [[null pointer]]), Elm's standard library defines a <code>Maybe a</code> 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.
<!-- Would be good to note number, comparable, and appendable here. -->


=== Module system ===
=== Module system ===
Elm has a [[Modular programming|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 [http://package.elm-lang.org/packages/ Elm Public Library].
Elm has a [[Modular programming|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 [http://package.elm-lang.org/packages/ Elm Public Library].
<!-- enforced semver -->


=== Interoperability with HTML, CSS, and JavaScript ===
=== Interoperability with HTML, CSS, and JavaScript ===

Revision as of 15:04, 23 June 2017

Elm
The Elm tangram
ParadigmFunctional
Designed byEvan Czaplicki
First appeared2012
Stable release
0.18 / November 14, 2016; 7 years ago (2016-11-14)
Typing disciplineStatic, Strong, Inferred
LicensePermissive (Revised BSD) [1]
Filename extensions.elm
Websiteelm-lang.org
Influenced by
Haskell, Standard ML, OCaml, F#
Influenced
Redux[2], Vue.js[3]

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,"[4] made possible by the Elm compiler's static type checking.

History

Elm was initially designed by Evan Czaplicki as his thesis in 2012.[5] The first release of Elm came with many examples and an online editor that made it easy to try out in a web browser.[6] Evan Czaplicki joined Prezi in 2013 to work on Elm,[7] and in 2016 moved to NoRedInk as an Open Source Engineer, also starting the Elm Software Foundation.[8]

The initial implementation of the Elm compiler targets HTML, CSS, and JavaScript.[9] The set of core tools has continued to expand, now including a REPL,[10] package manager,[11] time-traveling debugger,[12] and installers for Mac and Windows.[13] Elm also has an ecosystem of community created libraries.[14]

Features

Elm has a small but expressive set of language constructs, including traditional if-expressions, let-expressions for local state, and case-expressions for pattern matching.[15] As a functional language, it supports anonymous functions, functions as arguments, and partial application (currying) by default. 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".

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. 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. Union types allow the programmer to create custom types to represent data in a way that matches the problem domain.[17]

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 a 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.

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 or PureScript, 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

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)

-- Functions are also curried; here we've curried the multiplication 
-- infix operator with a `2`
multiplyBy2 : number -> number
multiplyBy2 =
    (*) 2

-- If-expressions are used to branch on values
absoluteValue : number -> number
absoluteValue number =
    if number < 0 then negate 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 
    }

-- Record access is done with `.`
title : String
title =
    book.title

-- Record access `.` can also be used as a function
author : String
author =
    .author book

-- 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

  1. ^ https://github.com/evancz/Elm/blob/master/LICENSE
  2. ^ Redux Prior Art
  3. ^ Elm Influenced Vue.js
  4. ^ "Elm home page".
  5. ^ Elm: Concurrent FRP for Functional GUIs
  6. ^ Elm's Online Editor
  7. ^ Elm joins Prezi
  8. ^ New Adventures for Elm
  9. ^ Elm compiler source code
  10. ^ Elm REPL announcement
  11. ^ Elm Package Manager announcement
  12. ^ Elm's Time-Traveling Debugger
  13. ^ Elm Platform
  14. ^ Elm Public Libraries
  15. ^ The Syntax of Elm
  16. ^ Elm Standard Libraries
  17. ^ "Model The Problem". Elm. Retrieved 4 May 2016.
  18. ^ Ports
  19. ^ elm-html documentation
  20. ^ Blazing Fast Html
  21. ^ "Higher-Kinded types Not Expressible? #396". github.com/elm-lang/elm-compiler. Retrieved 6 March 2015.

External links