4PureScript is a small strongly, statically typed compile-to-JS language with a number of interesting features, such as:
7- Higher Kinded Polymorphism
8- Support for basic Javascript types
11- Optimizer rules for generation of efficient Javascript
17- Optional tail-call elimination
23If you have the Haskell Plaform installed, then you can install the latest released version from Hackage::
26 cabal install purescript
28If you would like to build the latest version of the code, clone this repository and build::
30 git clone git://github.com:paf31/purescript.git
31 cabal configure --enable-tests
39The `psc` executable takes a list of PureScript source files as arguments and by default writes out its errors or output to the console.
41The following options are supported:
43--stdin Read input from standard input instead of from files
44--output Write the generated Javascript to the specified file
45--externs Write a list of foreign imports declarations to the specified file in addition to generating Javascript output
46--runtime-type-checks Generate simple runtime type checks for function arguments with simple types.
47--tco Perform tail-call elimination on the generated Javascript.
48--no-prelude Do not include the Prelude in the generated Javascript.
49--magic-do Overload the `do` keyword to inline calls to `bind` for the `Eff` monad, to generate more efficient code.
50--main Generate a call to `main` in the specified module after all other generated Javascript. Defaults to `Main` if the option is used but no value is provided.
51--module If specified, any code which is not referenced transitively from this module will be removed. This argument can be used multiple times.
52--browser-namespace Specify the namespace that PureScript modules will be exported to when running in the browser.
57I was looking for a simple functional language which would compile to JavaScript and have the following characteristics:
59- Generate simple readable Javascript
60- Provide the ability to compile to tight loops if necessary
61- Reasonable type system
62- Ideally, written in a programming language I would enjoy working in
63- Provides a simple interface to existing Javascript code
65I didn't find exactly what I was looking for, so I wrote PureScript. It doesn't have everything right now, but it should serve as a simple core on which to develop new ideas.
67PureScript was originally designed to implement purely functional core logic. However, recent additions to the compiler and libraries now also make it a good option for general-purpose programming.
69PureScript can also be seen as a trade-off between a theoretically ideal language and one which generates reasonably high performance code.
74As an introductory example, here is the usual "Hello World" written in PureScript::
80 main = trace "Hello, World!"
82which compiles to the following Javascript, ignoring the Prelude::
86 var main = trace("Hello, World!");
88 })(Main = Main || {});
90The following command will compile and execute the PureScript code above::
92 psc input.purs --main | node
97The following code defines a `Person` data type and a function to generate a string representation for a `Person`::
99 data Person = Person { name :: String, age :: Number }
101 foreign import numberToString :: Number -> String
103 showPerson :: Person -> String
104 showPerson (Person o) = o.name ++ ", aged " ++ numberToString(o.age)
106 examplePerson :: Person
107 examplePerson = Person {name: "Bonnie", age: 26}
110Line by line, this reads as follows:
112- `Person` is a data type with one constructor, also called `Person`
113- The `Person` constructor takes an object with two properties, `name` which is a `String`, and `age` which is a `Number`
114- The `numberToString` function is written in Javascript, and converts a `Number` to its `String` representation
115- The `showPerson` function takes a `Person` and returns a `String`
116- `showPerson` works by case analysis on its argument, first matching the constructor `Person` and then using string concatenation and object accessors to return its result.
117- `examplePerson` is a Person object, made with the Person constructor and given the String "Bonnie" for the name value and the Number 26 for the age value.
119The generated Javascript looks like this::
121 var Person = function (value) {
122 return { ctor: 'Person', values: [value] };
125 function showPerson(_1) {
126 return _1.values[0].name + ", aged " + numberToString(_1.values[0].age);
129 var examplePerson = Person({
138PureScript might be compared to other AltJS projects such as Roy, Haste, Fay, Elm and GHCJS. Certainly, there is a lot of overlap in terms of syntax, but the goals of PureScript listed above separate it in one or more ways from each of these languages.
140Roy is probably the most similar language on the list, and was a large influence on the development of PureScript. There are however, key differences in the foreign function interface, the type system and the choice of development language (Haskell vs. Javascript)
142Projects such as Haste, Fay and GHCJS aim to use some combination of the GHC compiler itself and/or its intermediate representation, Core, to perform some of the tasks involved in compilation such as parsing and type checking. This usually gives the advantage that tools and libraries can be shared with Haskell, but often at the cost of the size of the generated Javascript. This is the main practical difference between PureScript and these projects.
144Elm also shares a lot in terms of functionality with PureScript. Elm is designed for functional reactive programming, and focusses on tools and language features suitable for that domain, while PureScript focusses on the development of purely functional core application logic. Another difference between PureScript and Elm is PureScript's lack of a runtime system.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list