git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update types.rst
9fb3ad5Phil Freeman
docs/source/types.rst+108−133
@@ -1,25 +1,21 @@
1−## Type System
1+Types
2+=====
2
3
3The type system defines the following types:
4The type system defines the following types:
4
5
5−- Primitive Types
6− - Number
6+- Number
7− - String
7+- String
8− - Boolean
8+- Boolean
9- Arrays
9- Arrays
10− - E.g. `[String]`, `[[Number]]`
11- Records
10- Records
12− - E.g. `{ foo :: String, bar :: Number }`
13- Tagged Unions
11- Tagged Unions
14− - E.g. `data Foo a = Foo | Bar String`
15- Functions
12- Functions
16− - E.g. `Number -> String`
17− - E.g. `(Number, Number) -> Number`
18− - Functions can have zero or more arguments
19−- Polymorphic types
13+- Polymorphic Types
20− - E.g. `forall a. a -> a`
14+- Constrained Types
15+- Type Synonyms
21
16
22−## Primitive Types
17+Primitive Types
18+---------------
23
19
24The three primitive types `String`, `Number` and `Boolean` correspond to their Javascript equivalents at runtime.
20The three primitive types `String`, `Number` and `Boolean` correspond to their Javascript equivalents at runtime.
25
21
@@ -28,15 +24,14 @@ PureScript supports the same binary and unary operations on primitive types as J
28- String concatenation is denoted `++` to differentiate it from numeric addition, `+`
24- String concatenation is denoted `++` to differentiate it from numeric addition, `+`
29- PureScript's `==` and `!=` correspond to Javascript's strong equality tests `===` and `!==`
25- PureScript's `==` and `!=` correspond to Javascript's strong equality tests `===` and `!==`
30
26
31−Examples:
27+Examples::
32
28
33−```haskell
34−num = 1 + 2 * 3
29+ num = 1 + 2 * 3
35−str = "Hello, " ++ "World!"
30+ str = "Hello, " ++ "World!"
36−bool = 1 > 2 || true
31+ bool = 1 > 2 || true
37−```
38
32
39−## Arrays
33+Arrays
34+------
40
35
41PureScript arrays correspond to Javascript arrays at runtime, but all elements must have the same type.
36PureScript arrays correspond to Javascript arrays at runtime, but all elements must have the same type.
42
37
@@ -44,7 +39,8 @@ Array literals look like Javascript array literals: `[1, 2, 3]`
44
39
45Array elements can be read using array index notation `arr !! index`
40Array elements can be read using array index notation `arr !! index`
46
41
47−## Records
42+Records
43+-------
48
44
49PureScript records correspond to Javascript objects.
45PureScript records correspond to Javascript objects.
50
46
@@ -52,197 +48,176 @@ Record literals look like Javascript object literals: `{ foo: "Foo" }`
52
48
53Properties can be read by using dot notation: `o.foo`
49Properties can be read by using dot notation: `o.foo`
54
50
55−## Tagged Unions
51+Tagged Unions
52+-------------
56
53
57Tagged unions consist of one or more constructors, each of which takes zero or one arguments.
54Tagged unions consist of one or more constructors, each of which takes zero or one arguments.
58
55
59Tagged unions can only be created using their constructors, and deconstructed through pattern matching (see later).
56Tagged unions can only be created using their constructors, and deconstructed through pattern matching (see later).
60
57
61−For example:
58+For example::
62
59
63−```haskell
64−data Foo a = Foo | Bar String
60+ data Foo a = Foo | Bar String
65−
61+
66−runFoo Foo = "It's a Foo"
62+ runFoo Foo = "It's a Foo"
67−runFoo (Bar s) = "It's a Bar. The string is " ++ s
63+ runFoo (Bar s) = "It's a Bar. The string is " ++ s
68−
64+
69−test = runFoo Foo ++ runFoo (Bar "Test")
65+ test = runFoo Foo ++ runFoo (Bar "Test")
70−```
71
66
72In 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.
67In 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.
73
68
74`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.
69`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.
75
70
76−## Functions
71+Functions
72+---------
77
73
78Functions in PureScript can have zero or more arguments in general, just like in Javascript.
74Functions in PureScript can have zero or more arguments in general, just like in Javascript.
79
75
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::
81
77
82−```haskell
78+ example1 = \a b -> a + b
83−test1 = \a b -> a + b
84−```
85
79
86−which would correspond to the Javascript
80+which would correspond to the following Javascript::
87
81
88−```javascript
82+ function example1(a) {
89−function test1(a) {
90− return function (b) {
83+ return function (b) {
91− return a + b;
84+ return a + b;
85+ }
92 }
86 }
93−}
94−```
95
87
96Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas:
88Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas:
97
89
98−```haskell
90+ example2 = \(a, b) -> a + b
99−test1 = \(a, b) -> a + b
100−```
101
91
102−which generates
92+which generates::
103
93
104−```javascript
105−function test1(a, b) {
94+ function example2(a, b) {
106− return a + b;
95+ return a + b;
107−}
96+ }
108−```
109
97
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::
111
99
112−```haskell
100+ example3 = \ -> 100
113−test2 = \ -> 100
114−```
115
101
116−which would correspond to the Javascript `function test2() { return 100; }`
102+which would correspond to the Javascript::
117
103
118−Multiple-argument and single-argument syntax can be mixed, as follows:
104+ function example3() {
119−
105+ return 100;
120−```haskell
106+ }
121−test3 = \a (b, c) d -> a + b + c + d
122−```
123−
124−which generates
125
107
126−```javascript
108+Multiple-argument and single-argument syntax can be mixed, as follows::
127−function test3(a) {
128− return function (b, c) {
129− return function (d) {
130− return a + b + c + d;
131− }
132− }
133−}
134−```
135
109
136−Functions are applied by providing their arguments inside parentheses:
110+ example4 = \a (b, c) d -> a + b + c + d
137
111
138−```haskell
112+which generates::
139−test1(1, 2, 3)
140−test2()
141−```
142
113
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+ }
144
121
145−```haskell
122+Functions are applied by providing their arguments inside parentheses::
146−-- has type Number -> Number -> Number -> Number
147−addThree = \a b c -> a + b + c
148
123
149−-- has type Number -> Number -> Number
124+ test1(1, 2, 3)
150−addThree 1
125+ test2()
151
126
152−-- has type Number -> Number
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::
153−addThree 1 2
154
128
155−-- has type Number
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
156−addThree 1 2 3
139+ addThree 1 2 3
157−```
158
140
159−## Polymorphic Types
141+Polymorphic Types
142+-----------------
160
143
161Expressions defined at the top level may have polymorphic types.
144Expressions defined at the top level may have polymorphic types.
162
145
163−Here is an example:
146+Here is an example::
164
147
165−```haskell
166−identity x = x
148+ identity x = x
167−```
168
149
169`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.
150`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.
170
151
171−A type annotation can also be provided:
152+A type annotation can also be provided::
172
153
173−```haskell
174−identity :: forall a. a -> a
154+ identity :: forall a. a -> a
175−identity x = x
155+ identity x = x
176−```
177
156
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")::
179
158
180−```haskell
181−addProps o = o.foo + o.bar
159+ addProps o = o.foo + o.bar
182−```
183
160
184Here, `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*.
161Here, `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*.
185
162
186−So, the following compiles:
163+So, the following compiles::
187
164
188−```haskell
189−addProps { foo: 1, bar: 2, baz: 3 }
165+ addProps { foo: 1, bar: 2, baz: 3 }
190−```
191
166
192−but the following does not:
167+but the following does not::
193
168
194−```haskell
195−addProps { foo: 1 }
169+ addProps { foo: 1 }
196−```
197
170
198since the `bar` property is missing.
171since the `bar` property is missing.
199
172
200Again, a type annotation can be provided if necessary.
173Again, a type annotation can be provided if necessary.
201
174
202−## Rank N Types
175+Rank N Types
176+------------
203
177
204It 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.
178It 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.
205
179
206In most cases, a type annotation is necessary when using this feature.
180In most cases, a type annotation is necessary when using this feature.
207
181
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::
209
183
210−```haskell
211−poly :: (forall a. a -> a) -> Boolean
184+ poly :: (forall a. a -> a) -> Boolean
212−poly f = (f 0 < 1) == f true
185+ poly f = (f 0 < 1) == f true
213−```
214
186
215Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
187Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
216
188
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::
218
190
219−```haskell
220−test = poly (\n -> n + 1)
191+ test = poly (\n -> n + 1)
221−```
222
192
223since the skolemized type variable `a` does not unify with `Number`.
193since the skolemized type variable `a` does not unify with `Number`.
224
194
225−## Type Synonyms
195+Type Synonyms
196+-------------
226
197
227For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.
198For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.
228
199
229−For example:
200+For example::
230
201
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
233
206
234−addFoo :: Foo -> Number
207+Type Annotations
235−addFoo = \o -> o.foo + o.bar
208+----------------
236−```
237
209
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::
239
211
240−All types can be inferred, but annotations can optionally be provided.
212+ one = 1 :: Number
241
213
242−## Kind System
214+Kind System
215+-----------
243
216
244There are two primitive kinds, the kind `*` of types and the kind `!` of effects.
217There are two primitive kinds, the kind `*` of types and the kind `!` of effects.
245
218
246For 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.
219For 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.
247
220
221+Type constructors are given the arrow kind `k1 -> k2` for appropriate kinds `k1`, `k2`.
222+
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.
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.