Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Elm (programming language)

From Wikipedia, the free encyclopedia
Functional programming language
This articlerelies excessively onreferences toprimary sources. Please improve this article by addingsecondary or tertiary sources.
Find sources: "Elm" programming language – news ·newspapers ·books ·scholar ·JSTOR
(May 2019) (Learn how and when to remove this message)
Elm
The Elm tangram
Paradigmfunctional
FamilyHaskell
Designed byEvan Czaplicki
First appearedMarch 30, 2012; 13 years ago (2012-03-30)[1]
Stable release
0.19.1 / October 21, 2019; 6 years ago (2019-10-21)[2]
Typing disciplinestatic,strong,inferred
Platformx86-64,ARM
OSmacOS,Windows,Linux
LicensePermissive (Revised BSD)[3]
Filename extensions.elm
Websiteelm-lang.orgEdit this at Wikidata
Influenced by
Haskell,Standard ML,OCaml,F#
Influenced
Redux,[4]Rust,[5]Vue,[6] Roc,[7] Derw,[8] Gren[9]

Elm is adomain-specificprogramming language fordeclaratively creatingweb browser-basedgraphical user interfaces. Elm ispurely functional, and is developed with emphasis onusability, performance, androbustness. It advertises "noruntimeexceptions in practice",[10] made possible by the Elm compiler'sstatic type checking.

History

[edit]

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 aweb browser.[12] Czaplicki joinedPrezi in 2013 to work on Elm,[13] and in 2016 moved toNoRedInk 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), andJavaScript.[15] The set of core tools has continued to expand, now including aread–eval–print loop (REPL),[16]package manager,[17] time-travelling debugger,[18] and installers formacOS andWindows.[19] Elm also has an ecosystem of community createdlibraries,[20] and Ellie, an advanced online editor that allows saved work and including community libraries.[21]

Features

[edit]

Elm has a small set of language constructs, including traditional if-expressions, let-expressions for storing local values, and case-expressions forpattern matching.[22] As a functional language, it supportsanonymous functions, functions as arguments, and functions can return functions, the latter often by partial application ofcurried 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".

Immutability

[edit]

All values in Elm areimmutable, meaning that a value cannot be modified after it is created. Elm usespersistent data structures to implement its arrays, sets, and dictionaries in the standard library.[23]

Static types

[edit]

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 exampleround : 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 aList Int. Types are always capitalized; lowercase names are type variables. For example, aList a is a list of values of unknown type. It is the type of the empty list and of the argument toList.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 typeMsg.

Rather than allow any value to be implicitly nullable (such as JavaScript'sundefined or anull pointer), Elm's standard library defines aMaybe 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-intype classes:number which includesInt andFloat 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, andappendable 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 (seeLimits).

Module system

[edit]

Elm has amodule 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 asBitwise.and. Third party libraries (or packages) consist of one or more modules, and are available from theElm Public Library. All libraries are versioned according tosemver, 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.

Interoperability with HTML, CSS, and JavaScript

[edit]

Elm uses an abstraction called ports to communicate withJavaScript.[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 virtualDOM approach to make updates efficient.[27]

Backend

[edit]

Elm does not officially support server-side development. Czaplicki does consider it a primary goal at this point, but public progress on this front has been slow. Nevertheless, there are several independent projects which attempt to explore Elm on the backend.

The primary production-ready full-stack Elm platform is Lamdera, an open-core "unfork" of Elm.[28][29][30] Czaplicki has also teased Elm Studio, a potential alternative to Lamdera, but it isn't available to the public yet.[31] Current speculation is that Elm Studio will use a future version of Elm that targets C, uses Emscripten to compile to WASM, and supports type-safePostgres table generation.[32][33]

For full-stack frameworks, as opposed toBaaS products, elm-pages is perhaps the most popular fully open-source option.[34] It does not extend the Elm language, but just runs the compiled JS onNode.js. It also supports scripting. There is also Pine, an Elm to .NET compiler, which allows safe interop with C#, F#, and otherCLR languages.[35]

There were also some attempts in Elm versions prior to 0.19.0 to use theBEAM (Erlang virtual machine) to run Elm, but they are stuck due to the removal of native code in 0.19.0 and changes to the package manager. One of the projects executed Elm directly on the environment,[36] while another one compiled it to Elixir.[37]

Finally, the Gren programming language started out a fork of Elm primarily focused on backend support, although its goals have since shifted.

The Elm Architecture (TEA pattern)

[edit]

The Elm Architecture is asoftware design pattern and as aTLA 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:

  • Model - the state of the application
  • View - a function that turns the model into HTML
  • Update - a function that updates the model based on messages

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.[38] In this case, all we need to store is one number, so our model can be as simple astype alias Model = Int. Theview function would be defined with theHtml library and display the number and button. For the number to be updated, we need to be able to send a message to theupdate function, which is done through a custom type such astype Msg = Increase. TheIncrease value is attached to the button defined in theview function such that when the button is clicked by a user,Increase is passed on to theupdate function, which can update the model by increasing the number.

In the Elm Architecture, sending messages toupdate 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 fromupdate, subscriptions to external events (window resize, system clock, JavaScript interop...) and URL changes and requests.

Limits

[edit]

Elm does not supporthigher-kinded polymorphism,[39] which related languagesHaskell,Scala andPureScript offer, nor does Elm support the creation oftype classes.

This means that, for example, Elm does not have a genericmap function which works across multiple data structures such asList andSet. In Elm, such functions are typically invoked qualified by their module name, for example callingList.map andSet.map. In Haskell or PureScript, there would be only one functionmap. This is a known feature request that is on Czaplicki's rough roadmap since at least 2015.[40] On the other hand, implementations of TEA pattern in advanced languages likeScala does not suffer from such limitations and can benefit fromScala's type classes,type-level andkind-level programming constructs.[41]

Another outcome is a large amount ofboilerplate code in medium to large size projects as illustrated by the author of "Elm in Action," a former Elm core team member, in his single page application example[42] with almost identical fragments being repeated in update, view, subscriptions, route parsing and building functions.

Example code

[edit]
-- 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)

See also

[edit]
  • PureScript – A strongly-typed, purely-functional programming language that compiles to JavaScript
  • Reason – A syntax extension and toolchain for OCaml that can also transpile to JavaScript

References

[edit]
  1. ^Czaplicki, Evan (30 March 2012)."My Thesis is Finally Complete! "Elm: Concurrent FRP for functional GUIs"".Reddit.
  2. ^"Releases: elm/Compiler".GitHub.
  3. ^"elm/compiler".GitHub. 16 October 2021.
  4. ^"Prior Art - Redux".redux.js.org. 28 April 2024.
  5. ^"Shapes of errors to come".Rust Blog. Retrieved2016-10-08.Those of you familiar with the Elm style may recognize that the updated --explain messages draw heavy inspiration from the Elm approach.
  6. ^"Comparison with Other Frameworks — Vue.js".
  7. ^"roc/roc-for-elm-programmers.md at main · roc-lang/roc".GitHub. Retrieved2024-02-17.Roc is a direct descendant of the Elm programming language. The two languages are similar, but not the same!
  8. ^"Why Derw: an Elm-like language that compiles to TypeScript?". 20 December 2021.
  9. ^"Gren 0.1.0 is released".
  10. ^"Elm home page".
  11. ^"Elm: Concurrent FRP for Functional GUIs"(PDF).
  12. ^"Try Elm".elm-lang.org. Retrieved2025-04-26.
  13. ^"elm and prezi".elm-lang.org.
  14. ^"new adventures for elm".elm-lang.org.
  15. ^"elm/compiler".GitHub. 16 October 2021.
  16. ^"repl".elm-lang.org.
  17. ^"package manager".elm-lang.org.
  18. ^"Home".elm-lang.org.
  19. ^"Install".guide.elm-lang.org.
  20. ^"Elm packages".Elm-lang.org.
  21. ^"Ellie".Ellie-app.com.
  22. ^"syntax".elm-lang.org. Retrieved2025-04-26.
  23. ^"elm/core".package.elm-lang.org.
  24. ^"Model The Problem".Elm. Retrieved4 May 2016.
  25. ^"JavaScript interop".elm-lang.org.
  26. ^"elm/html".package.elm-lang.org.
  27. ^"Blazing Fast HTML".elm-lang.org.
  28. ^Elm Europe (2019-11-28).Mario Rogic - Elm as a Service. Retrieved2025-03-27 – via YouTube.
  29. ^Elm Online Meetup (2021-07-23).Building a Meetup clone on Lamdera - Martin Stewart. Retrieved2025-03-27 – via YouTube.
  30. ^"Episode 38: Lamdera".Elm Radio Podcast. Retrieved2025-03-27.
  31. ^"Elm Studio".www.elm.studio. Retrieved2025-03-27.
  32. ^"Status Update - 3 Nov 2021".Elm. 2021-11-03. Retrieved2025-03-27.
  33. ^Cesarini, Francesco (22 May 2023)."@evancz tempting the demo gods…".Twitter. Retrieved26 March 2025.
  34. ^"elm-pages - pull in typed elm data to your pages".elm-pages. Retrieved2025-03-27.
  35. ^"Pine — Run Elm Everywhere".pine-vm.org. Retrieved2025-03-27.
  36. ^"Kofigumbs/Elm-beam".GitHub. 24 September 2021.
  37. ^"What is it?".GitHub. 24 September 2021.
  38. ^"Buttons · An Introduction to Elm".guide.elm-lang.org. Retrieved2020-10-15.
  39. ^"Higher-Kinded types Not Expressible? #396".github.com/elm-lang/elm-compiler. Retrieved6 March 2015.
  40. ^"Higher-Kinded types Not Expressible #396".github.com/elm-lang/elm-compiler. Retrieved19 November 2019.
  41. ^"The Elm Architecture".tyrian.indigoengine.io. Retrieved2024-09-07.
  42. ^"Main.elm".github.com/rtfeldman/elm-spa-example. Retrieved30 June 2020.

External links

[edit]
Haskell programming
Software
Implementations
(features)
Dialects
Electronic
design
Libraries
Package managers
Windowing systems
Web frameworks
Book
Community
Eponym
Retrieved from "https://en.wikipedia.org/w/index.php?title=Elm_(programming_language)&oldid=1307433512"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp