git.iko.soy/rowtype-yoga/purescript-native
on e82e607 help sign in
143 lines 6.5 kB history raw
1Introduction
2============
4PureScript is a small strongly, statically typed compile-to-JS language with a number of interesting features, such as:
6- Type Inference
7- Higher Kinded Polymorphism
8- Support for basic Javascript types
9- Extensible records
10- Extensible effects
11- Optimizer rules for generation of efficient Javascript
12- Pattern matching
13- Simple FFI
14- Modules
15- Rank N Types
16- Do Notation
17- Tail-call elimination
18- Type Classes
20Installation
21------------
23If you have the Haskell Plaform installed, then you can install the latest released version from Hackage::
25 cabal update
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
32 cabal build
33 cabal test
34 cabal install
36Usage
37-----
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--no-tco Turn off tail-call elimination.
48--no-prelude Do not include the Prelude in the generated Javascript.
49--no-magic-do Turn off optimizations which inline calls to ``>>=`` for the ``Eff`` monad.
50--no-opts Disable all optimizations.
51--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.
52--module If specified, any code which is not referenced transitively from this module will be removed. This argument can be used multiple times.
53--codegen A list of modules for which Javascript and externs should be generated. This argument can be used multiple times.
54--browser-namespace Specify the namespace that PureScript modules will be exported to when running in the browser.
55--verbose-errors Generate verbose error messages
57Motivation
58----------
60I was looking for a simple functional language which would compile to JavaScript and have the following characteristics:
62- Generate simple readable Javascript
63- Provide the ability to compile to tight loops if necessary
64- Reasonable type system
65- Ideally, written in a programming language I would enjoy working in
66- Provides a simple interface to existing Javascript code
68I 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.
70PureScript 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.
72PureScript can also be seen as a trade-off between a theoretically ideal language and one which generates reasonably high performance code.
74Hello, PureScript!
75------------------
77As an introductory example, here is the usual "Hello World" written in PureScript::
79 module Main where
81 import Debug.Trace
83 main = trace "Hello, World!"
85which compiles to the following Javascript, ignoring the Prelude::
87 var Main;
88 (function (Main) {
89 var main = trace("Hello, World!");
90 Main.main = main;
91 })(Main = Main || {});
93The following command will compile and execute the PureScript code above::
95 psc input.purs --main | node
97Another Example
98---------------
100The following code defines a ``Person`` data type and a function to generate a string representation for a ``Person``::
102 data Person = Person { name :: String, age :: Number }
104 showPerson :: Person -> String
105 showPerson (Person o) = o.name ++ ", aged " ++ show o.age
107 examplePerson :: Person
108 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 ``showPerson`` function takes a ``Person`` and returns a ``String``
115- ``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.
116- ``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.
118The generated Javascript looks like this::
120 var Person = function (value) {
121 return { ctor: 'Person', values: [value] };
122 };
124 function showPerson(_1) {
125 return _1.values[0].name + ", aged " + numberToString(_1.values[0].age);
126 };
128 var examplePerson = Person({
129 name: "Bonnie",
130 age: 26
131 });
133Related Projects
134----------------
136PureScript 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.
138Roy 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)
140Projects 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.
142Elm 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.