git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update types.rst
commit9fb3ad590ab5e8f06d7458db4b9b75f80a77e843 authorPhil Freeman <paf31@cantab.net> dateWed 22 Jan 2014, 23:11 (13 years ago) parent4efe14b
1 file changed+108−133
docs/source/types.rst+108−133
docs/source/types.rst+108−133
@@ -1,25 +1,21 @@
1−## Type System
1+Types
2+=====
23
34The type system defines the following types:
45
5−- Primitive Types
6− - Number
7− - String
8− - Boolean
6+- Number
7+- String
8+- Boolean
99- Arrays
10− - E.g. `[String]`, `[[Number]]`
1110- Records
12− - E.g. `{ foo :: String, bar :: Number }`
1311- Tagged Unions
14− - E.g. `data Foo a = Foo | Bar String`
1512- Functions
16− - E.g. `Number -> String`
17− - E.g. `(Number, Number) -> Number`
18− - Functions can have zero or more arguments
19−- Polymorphic types
20− - E.g. `forall a. a -> a`
13+- Polymorphic Types
14+- Constrained Types
15+- Type Synonyms
2116
22−## Primitive Types
17+Primitive Types
18+---------------
2319
2420The three primitive types `String`, `Number` and `Boolean` correspond to their Javascript equivalents at runtime.
2521
@@ -28,15 +24,14 @@ PureScript supports the same binary and unary operations on primitive types as J
2824- String concatenation is denoted `++` to differentiate it from numeric addition, `+`
2925- PureScript's `==` and `!=` correspond to Javascript's strong equality tests `===` and `!==`
3026
31−Examples:
27+Examples::
3228
33−```haskell
34−num = 1 + 2 * 3
35−str = "Hello, " ++ "World!"
36−bool = 1 > 2 || true
37−```
29+ num = 1 + 2 * 3
30+ str = "Hello, " ++ "World!"
31+ bool = 1 > 2 || true
3832
39−## Arrays
33+Arrays
34+------
4035
4136PureScript arrays correspond to Javascript arrays at runtime, but all elements must have the same type.
4237
@@ -44,7 +39,8 @@ Array literals look like Javascript array literals: `[1, 2, 3]`
4439
4540Array elements can be read using array index notation `arr !! index`
4641
47−## Records
42+Records
43+-------
4844
4945PureScript records correspond to Javascript objects.
5046
@@ -52,197 +48,176 @@ Record literals look like Javascript object literals: `{ foo: "Foo" }`
5248
5349Properties can be read by using dot notation: `o.foo`
5450
55−## Tagged Unions
51+Tagged Unions
52+-------------
5653
5754Tagged unions consist of one or more constructors, each of which takes zero or one arguments.
5855
5956Tagged unions can only be created using their constructors, and deconstructed through pattern matching (see later).
6057
61−For example:
58+For example::
6259
63−```haskell
64−data Foo a = Foo | Bar String
65−
66−runFoo Foo = "It's a Foo"
67−runFoo (Bar s) = "It's a Bar. The string is " ++ s
68−
69−test = runFoo Foo ++ runFoo (Bar "Test")
70−```
60+ data Foo a = Foo | Bar String
61+
62+ runFoo Foo = "It's a Foo"
63+ runFoo (Bar s) = "It's a Bar. The string is " ++ s
64+
65+ test = runFoo Foo ++ runFoo (Bar "Test")
7166
7267In the example, Foo is a tagged union type which has two constructors. It's first constructor `Foo` takes no argument, and it's second `Bar` takes one, which must be a String.
7368
7469`runFoo` is an example of pattern matching on a tagged union type to discover its constructor, and the last line shows how `Foo`s are constructed.
7570
76−## Functions
71+Functions
72+---------
7773
7874Functions in PureScript can have zero or more arguments in general, just like in Javascript.
7975
80−Functions are introduced by using a backslash followed by a list of argument names:
76+Functions are introduced by using a backslash followed by a list of argument names::
8177
82−```haskell
83−test1 = \a b -> a + b
84−```
78+ example1 = \a b -> a + b
8579
86−which would correspond to the Javascript
80+which would correspond to the following Javascript::
8781
88−```javascript
89−function test1(a) {
90− return function (b) {
91− return a + b;
82+ function example1(a) {
83+ return function (b) {
84+ return a + b;
85+ }
9286 }
93−}
94−```
9587
9688Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas:
9789
98−```haskell
99−test1 = \(a, b) -> a + b
100−```
90+ example2 = \(a, b) -> a + b
10191
102−which generates
92+which generates::
10393
104−```javascript
105−function test1(a, b) {
106− return a + b;
107−}
108−```
94+ function example2(a, b) {
95+ return a + b;
96+ }
10997
110−In the case of a function with no arguments, the parentheses may be omitted, as follows:
98+In the case of a function with no arguments, the parentheses may be omitted, as follows::
11199
112−```haskell
113−test2 = \ -> 100
114−```
100+ example3 = \ -> 100
115101
116−which would correspond to the Javascript `function test2() { return 100; }`
102+which would correspond to the Javascript::
117103
118−Multiple-argument and single-argument syntax can be mixed, as follows:
119−
120−```haskell
121−test3 = \a (b, c) d -> a + b + c + d
122−```
123−
124−which generates
104+ function example3() {
105+ return 100;
106+ }
125107
126−```javascript
127−function test3(a) {
128− return function (b, c) {
129− return function (d) {
130− return a + b + c + d;
131− }
132− }
133−}
134−```
108+Multiple-argument and single-argument syntax can be mixed, as follows::
135109
136−Functions are applied by providing their arguments inside parentheses:
110+ example4 = \a (b, c) d -> a + b + c + d
137111
138−```haskell
139−test1(1, 2, 3)
140−test2()
141−```
112+which generates::
142113
143−A special case is made in the case of functions with one argument. These functions can be applied without parentheses, and function application associates to the left:
114+ function example4(a) {
115+ return function (b, c) {
116+ return function (d) {
117+ return a + b + c + d;
118+ }
119+ }
120+ }
144121
145−```haskell
146−-- has type Number -> Number -> Number -> Number
147−addThree = \a b c -> a + b + c
122+Functions are applied by providing their arguments inside parentheses::
148123
149−-- has type Number -> Number -> Number
150−addThree 1
124+ test1(1, 2, 3)
125+ test2()
151126
152−-- has type Number -> Number
153−addThree 1 2
127+A special case is made in the case of functions with one argument. These functions can be applied without parentheses, and function application associates to the left::
154128
155−-- has type Number
156−addThree 1 2 3
157−```
129+ -- has type Number -> Number -> Number -> Number
130+ addThree = \a b c -> a + b + c
131+
132+ -- has type Number -> Number -> Number
133+ addThree 1
134+
135+ -- has type Number -> Number
136+ addThree 1 2
137+
138+ -- has type Number
139+ addThree 1 2 3
158140
159−## Polymorphic Types
141+Polymorphic Types
142+-----------------
160143
161144Expressions defined at the top level may have polymorphic types.
162145
163−Here is an example:
146+Here is an example::
164147
165−```haskell
166−identity x = x
167−```
148+ identity x = x
168149
169150`identity` is inferred to have (polymorphic) type `forall t0. t0 -> t0`. This means that for any type `t0`, `identity` can be given a value of type `t0` and will give back a value of the same type.
170151
171−A type annotation can also be provided:
152+A type annotation can also be provided::
172153
173−```haskell
174−identity :: forall a. a -> a
175−identity x = x
176−```
154+ identity :: forall a. a -> a
155+ identity x = x
177156
178−Functions may also be polymorphic in row types or type variables with other kinds (see "Kind System"):
157+Functions may also be polymorphic in row types or type variables with other kinds (see "Kind System")::
179158
180−```haskell
181−addProps o = o.foo + o.bar
182−```
159+ addProps o = o.foo + o.bar
183160
184161Here, `addProps` is inferred to have type `forall r. { foo :: Number, bar :: Number | r } -> Number`. That is, it can take any type which has properties `Foo` and `Bar`, and *any other record properties*.
185162
186−So, the following compiles:
163+So, the following compiles::
187164
188−```haskell
189−addProps { foo: 1, bar: 2, baz: 3 }
190−```
165+ addProps { foo: 1, bar: 2, baz: 3 }
191166
192−but the following does not:
167+but the following does not::
193168
194−```haskell
195−addProps { foo: 1 }
196−```
169+ addProps { foo: 1 }
197170
198171since the `bar` property is missing.
199172
200173Again, a type annotation can be provided if necessary.
201174
202−## Rank N Types
175+Rank N Types
176+------------
203177
204178It is also possible for the `forall` quantifier to appear on the left of a function arrow, inside types record fields and data constructors, and in type synonyms.
205179
206180In most cases, a type annotation is necessary when using this feature.
207181
208−As an example, we can pass a polymorphic function as an argument to another function:
182+As an example, we can pass a polymorphic function as an argument to another function::
209183
210−```haskell
211−poly :: (forall a. a -> a) -> Boolean
212−poly f = (f 0 < 1) == f true
213−```
184+ poly :: (forall a. a -> a) -> Boolean
185+ poly f = (f 0 < 1) == f true
214186
215187Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
216188
217−An argument to `poly` must indeed be polymorphic. For example, the following fails:
189+An argument to `poly` must indeed be polymorphic. For example, the following fails::
218190
219−```haskell
220−test = poly (\n -> n + 1)
221−```
191+ test = poly (\n -> n + 1)
222192
223193since the skolemized type variable `a` does not unify with `Number`.
224194
225−## Type Synonyms
195+Type Synonyms
196+-------------
226197
227198For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.
228199
229−For example:
200+For example::
230201
231−```haskell
232−type Foo = { foo :: Number, bar Number }
202+ type Foo = { foo :: Number, bar Number }
203+
204+ addFoo :: Foo -> Number
205+ addFoo = \o -> o.foo + o.bar
233206
234−addFoo :: Foo -> Number
235−addFoo = \o -> o.foo + o.bar
236−```
207+Type Annotations
208+----------------
237209
238−## Type Inference
210+Most types can be inferred (not including Rank N Types and constrained types), but annotations can optionally be provided using a double-colon::
239211
240−All types can be inferred, but annotations can optionally be provided.
212+ one = 1 :: Number
241213
242−## Kind System
214+Kind System
215+-----------
243216
244217There are two primitive kinds, the kind `*` of types and the kind `!` of effects.
245218
246219For each kind `k` there is also a kind `# k` of rows, with types of kind `k`. For example `# *` is the kind of rows of types, as used to define records, and `# !` is the kind of rows of effects, used to define the monad `Eff` of extensible effects.
247220
248−Higher kinded types are also supported. That is, a type variable can refer to not only a type or a row, but a type constructor, or row constructor etc.
221+Type constructors are given the arrow kind `k1 -> k2` for appropriate kinds `k1`, `k2`.
222+
223+A type variable can refer to not only a type or a row, but a type constructor, or row constructor etc., and type variables with those kinds can be bound inside a `forall` quantifier.