PureScript

{{Short description|Strongly-typed language that compiles to JavaScript}}

{{Primary sources|date=August 2019}}

{{Infobox programming language

| name = PureScript

| logo = PureScript Logo.png

| paradigm = functional

| family = Haskell

| designer = Phil Freeman

| released = {{Start date and age|2013}}

| latest release version = {{wikidata|property|edit|P548=Q2804309|P348}}

| latest release date = {{Start date and age|{{wikidata|qualifier|single|P548=Q2804309|P348|P577}}}}

| typing = Inferred, static, strong

| website = {{official URL}}

| license = BSD 3-clause{{cite web|url=https://github.com/purescript/purescript/blob/master/LICENSE|title=purescript/purescript|website=GitHub|date=3 June 2022}}

| file ext = .purs

| influenced by = Haskell, JavaScript

}}

PureScript is a strongly-typed, purely-functional programming language that transpiles to JavaScript,{{Cite web |url=https://github.com/purescript/purescript |title=PureScript to JavaScript transpiler |website=GitHub |access-date=2023-01-22}} C++11,{{Cite web |url=https://github.com/andyarvanitis/purescript-native/tree/cpp |title=PureScript to C++11 transpiler |website=GitHub |access-date=2023-01-22}} Erlang,{{Citation |title=purerl - the PureScript Erlang backend |date=2023-09-04 |url=https://github.com/purerl/purerl |access-date=2023-09-05 |publisher=purerl}} and Go.{{Cite web |url=https://github.com/andyarvanitis/purescript-native |title=PureScript to Go transpiler |website=GitHub |access-date=2023-01-22}} It can be used to develop web applications, server side apps, and also desktop applications with use of Electron or via C++11 and Go compilers with suitable libraries. Its syntax is mostly comparable to that of Haskell. Also, it introduces row polymorphism and extensible records.{{Cite web|url=https://purescript.org/|title=PureScript|website=purescript.org|access-date=2019-03-14}} Also, contrary to Haskell, the PureScript language is defined as having a strict evaluation strategy,{{Cite web |title=purescript/documentation |date=2023-02-19 |url=https://github.com/purescript/documentation/blob/ddc83562aa1de9d4e2c1067c71bf3d4e3b77b84d/language/Differences-from-Haskell.md |publisher=PureScript |access-date=2023-02-20}} although there are non-conforming back-ends which implement a lazy evaluation strategy.{{Citation |title=Alternative backends |date=2023-02-19 |url=https://github.com/purescript/documentation/blob/ddc83562aa1de9d4e2c1067c71bf3d4e3b77b84d/ecosystem/Alternate-backends.md |publisher=PureScript |access-date=2023-02-20}} It is free and open-source software released under a BSD 3-clause license.

History

PureScript was initially designed by Phil Freeman in 2013. He began work on it because he was unsatisfied by other attempts to transpile Haskell to JavaScript (e.g., using Fay, Haste, or GHCJS).{{Cite web |url=https://leanpub.com/purescript/read#leanpub-auto-about-the-author |title=Read PureScript by Example |website=Leanpub.com |access-date=2019-03-14}}

Since then it has been adopted by the community and is developed on GitHub. Further community-developed core tools include the dedicated build tool Pulp,{{Citation|title=A build tool for PureScript projects|date=2019-03-08|url=https://github.com/purescript-contrib/pulp|publisher=PureScript Contrib|access-date=2019-03-14}} the documentation directory Pursuit,{{Cite web|url=https://pursuit.purescript.org/|title=Pursuit|website=pursuit.purescript.org|access-date=2019-03-14}} and the package manager Spago{{Citation|title=PureScript package manager and build tool powered by Dhall and package-sets: spacchetti/spago|date=2019-03-14|url=https://github.com/spacchetti/spago|publisher=spacchetti|access-date=2019-03-14}}

Features

PureScript features strict evaluation, persistent data structures, and type inference. Its data type system shares many features with those of similar functional languages like Haskell: algebraic data types and pattern matching, higher kinded types, type classes, functional dependencies, and higher-rank polymorphism. Its type system adds support for row polymorphism and extensible records,{{Cite web |title=Documentation for the PureScript language, compiler, and tools: purescript/documentation |date=2019-03-14 |url=https://github.com/purescript/documentation |publisher=PureScript |access-date=2019-03-14}} but does not support some of the more advanced features of Haskell such as the generalized algebraic data type (GADT) and the type family.

The PureScript transpilers attempt to produce readable code, where possible. Through a simple foreign function interface (FFI), it also allows code reuse of extant source code in JavaScript,{{Cite web |title=The Foreign Function Interface |date=2015-07-15 |url=https://github.com/purescript/documentation/blob/master/guides/FFI.md |publisher=PureScript |access-date=2023-01-22}} C++11,{{Cite web |title=FFI |date=2019-07-26 |url=https://github.com/andyarvanitis/purescript-native/wiki/FFI |publisher=PureScript |access-date=2023-01-22}}{{Citation |title=C++ foreign export implementations for the standard library |date=2019-07-26 |url=https://github.com/andyarvanitis/purescript-native-cpp-ffi |publisher=PureScript |access-date=2023-01-22}} and Go,{{Cite web |title=Go foreign export implementations for the standard library |date=2020-12-13 |url=https://github.com/andyarvanitis/purescript-native-go-ffi |publisher=PureScript |access-date=2023-01-22}} usually as an intermediate representation.

PureScript supports incremental compiling, and the transpiler to JavaScript distribution supports building source-code editor plug-ins for iterative development.{{Cite web |last1=Hegemann |first1=Christoph (kritzcreek) |date=2019-04-21 |title=purs ide: Editor and tooling support for the PureScript programming language |url=https://github.com/purescript/purescript/tree/dfbd19da85c92d82ed4c3184f950f74da5332e3f/psc-ide |access-date=2019-04-21}} Editor plug-ins exist for many popular text editors, including Vim, Emacs, Sublime Text, Atom and Visual Studio Code.

PureScript supports type-driven development via its typed holes feature,{{Cite thesis |last1=Hegemann |first1=Christoph |date=2016-10-27 |title=Implementing type directed search for PureScript |degree=B.Sc.}} in which a program can be constructed with missing subexpressions. The JavaScript transpiler will subsequently attempt to infer the types of the missing subexpressions, and report those types to the user. This feature inspired similar work in the Glasgow Haskell Compiler (GHC).{{Cite journal |last1=Gissurarson |first1=Matthías Páll |date=2018-09-27 |title=Suggesting Valid Hole Fits for Typed-Holes (Experience Report) |url=https://dl.acm.org/citation.cfm?id=3242760 |journal=ACM SIGPLAN Notices |volume=53 |issue=7 |pages=179–185 |doi=10.1145/3299711.3242760 |s2cid=218480974|url-access=subscription }}

Examples

Here is a minimal "Hello, World!" program in PureScript:

module Main where

import Effect.Console (log)

main = log "Hello World!"

Here, the type of the program is inferred and checked by the PureScript transpiler. A more verbose version of the same program might include explicit type annotations:

module Main where

import Prelude

import Effect (Effect)

import Effect.Console (log)

main :: Effect Unit

main = log "Hello World!"

See also

{{Portal|Free and open-source software}}

  • {{annotated link|Elm (programming language)}}
  • {{annotated link|Reason (programming language)}}
  • {{annotated link|JS++}}
  • {{annotated link|TypeScript}}

References

{{Reflist|2}}