git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Create intro.rst
commit98a2491fea0c5e9f54f8d5621b63095c6e789ab9 authorPhil Freeman <paf31@cantab.net> dateWed 22 Jan 2014, 22:15 (13 years ago) parent6fd6a20
1 file changed+138
docs/source/intro.rst+138
docs/source/intro.rstnew file+138
@@ -0,0 +1,138 @@
1+purescript
2+==========
3+
4+PureScript is a small strongly, statically typed compile-to-JS language with a number of interesting features, such as:
5+
6+- Type Inference
7+- Higher Kinded Polymorphism
8+- Support for basic Javascript types
9+- Extensible records
10+- Extensible effects
11+- Type-safe blocks with for/while/assignment etc.
12+- Functions with multiple arguments
13+- Pattern matching
14+- Simple FFI
15+- Modules
16+- Rank N Types
17+- Do Notation
18+- Optional tail-call elimination
19+- Type Classes
20+
21+## Try It!
22+
23+Try PureScript in the browser [here](http://tryps.functorial.com/).
24+
25+## Installation
26+
27+Install the Haskell Plaform, then `cabal install`
28+
29+## Usage
30+
31+The `psc` executable takes a list of PureScript source files as arguments and by default writes out its errors or output to the console.
32+
33+The following options are supported:
34+
35+ -s --stdin
36+ Read input from standard input instead of from files
37+
38+ -o --output
39+ Write the generated Javascript to the specified file
40+
41+ -e --foreign imports
42+ Write a list of foreign imports declarations to the specified file in addition to generating Javascript output
43+
44+ --runtime-type-checks
45+ Generate simple runtime type checks for function arguments with simple types.
46+
47+ --tco
48+ Perform tail-call elimination on the generated Javascript.
49+
50+ --no-prelude
51+ Do not include the Prelude in the generated Javascript.
52+
53+ --magic-do
54+ Overload the `do` keyword to inline calls to `bind` for the `Eff` monad, to generate more efficient code.
55+
56+ --run-main
57+ Generate a call to `Main.main` after all other generated Javascript.
58+
59+## Motivation
60+
61+I was looking for a simple functional language which would compile to JavaScript and have the following characteristics:
62+
63+- Generate simple readable Javascript
64+- Provide the ability to compile to tight loops if necessary
65+- Reasonable type system
66+- Ideally, written in a programming language I would enjoy working in
67+- Provides a simple interface to existing Javascript code
68+
69+I 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.
70+
71+PureScript is *not* designed to be a general-purpose programming language. The primary use case is as a generator for purely-functional core libraries, with the main application code written in another language.
72+
73+PureScript can also be seen as a trade-off between a theoretically ideal language and one which generates reasonably high performance code.
74+
75+## Hello, PureScript!
76+
77+As an introductory example, here is the usual "Hello World" written in PureScript:
78+
79+```haskell
80+module Main where
81+
82+import Trace
83+
84+main = trace "Hello, World!"
85+```
86+
87+which compiles to the following Javascript, ignoring the Prelude:
88+
89+```javascript
90+var Main;
91+(function (Main) {
92+ var main = trace("Hello, World!");
93+ Main.main = main;
94+})(Main = Main || {});
95+```
96+
97+The following command will compile and execute the PureScript code above:
98+
99+```
100+psc input.purs --run-main | nodejs
101+```
102+
103+## Another Example
104+
105+The following code defines a `Person` data type and a function to generate a string representation for a `Person`:
106+
107+```haskell
108+data Person = Person { name :: String, age :: Number }
109+
110+foreign import numberToString :: Number -> String
111+
112+showPerson :: Person -> String
113+showPerson (Person o) = o.name ++ ", aged " ++ numberToString(o.age)
114+```
115+
116+Line by line, this reads as follows:
117+
118+- `Person` is a data type with one constructor, also called `Person`
119+ - The `Person` constructor takes an object with two properties, `name` which is a `String`, and `age` which is a `Number`
120+- The `numberToString` function is written in Javascript, and converts a `Number` to its `String` representation
121+- The `showPerson` function takes a `Person` and returns a `String`
122+- `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.
123+
124+The generated Javascript looks like this:
125+
126+```javascript
127+var Person = function (value) {
128+ return { ctor: 'Person', value: value };
129+};
130+
131+function showPerson(_1) {
132+ if (_1.ctor === "Person") {
133+ return _1.value.name + ", aged " + numberToString(_1.value.age);
134+ };
135+ throw "Failed pattern match";
136+};
137+
138+```