git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update types.rst
commitde7cd27894b07a6629f31e82e0c72b78e77b53eb authorPhil Freeman <paf31@cantab.net> dateThu 23 Jan 2014, 13:58 (13 years ago) parent3ad52a4
1 file changed+22−103
docs/source/types.rst+22−103
docs/source/types.rst+22−103
@@ -3,9 +3,7 @@ Types
33
44The type system defines the following types:
55
6−- Number
7−- String
8−- Boolean
6+- Primitive Types: Number, String, Boolean
97- Arrays
108- Records
119- Tagged Unions
@@ -17,36 +15,17 @@ The type system defines the following types:
1715Primitive Types
1816---------------
1917
20−The three primitive types `String`, `Number` and `Boolean` correspond to their Javascript equivalents at runtime.
21−
22−PureScript supports the same binary and unary operations on primitive types as Javascript, with the following exceptions:
23−
24−- String concatenation is denoted `++` to differentiate it from numeric addition, `+`
25−- PureScript's `==` and `!=` correspond to Javascript's strong equality tests `===` and `!==`
26−
27−Examples::
28−
29− num = 1 + 2 * 3
30− str = "Hello, " ++ "World!"
31− bool = 1 > 2 || true
18+The three primitive types ``String``, ``Number`` and ``Boolean`` correspond to their Javascript equivalents at runtime.
3219
3320Arrays
3421------
3522
36−PureScript arrays correspond to Javascript arrays at runtime, but all elements must have the same type.
37−
38−Array literals look like Javascript array literals: `[1, 2, 3]`
39−
40−Array elements can be read using array index notation `arr !! index`
23+PureScript arrays correspond to Javascript arrays at runtime, but all elements in an array must have the same type.
4124
4225Records
4326-------
4427
45−PureScript records correspond to Javascript objects.
46−
47−Record literals look like Javascript object literals: `{ foo: "Foo" }`
48−
49−Properties can be read by using dot notation: `o.foo`
28+PureScript records correspond to Javascript objects. They may have zero or more named fields, each with their own types.
5029
5130Tagged Unions
5231-------------
@@ -64,79 +43,14 @@ For example::
6443
6544 test = runFoo Foo ++ runFoo (Bar "Test")
6645
67−In 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.
46+In 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.
6847
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.
48+``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.
7049
7150Functions
7251---------
7352
74−Functions in PureScript can have zero or more arguments in general, just like in Javascript.
75−
76−Functions are introduced by using a backslash followed by a list of argument names::
77−
78− example1 = \a b -> a + b
79−
80−which would correspond to the following Javascript::
81−
82− function example1(a) {
83− return function (b) {
84− return a + b;
85− }
86− }
87−
88−Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas:
89−
90− example2 = \(a, b) -> a + b
91−
92−which generates::
93−
94− function example2(a, b) {
95− return a + b;
96− }
97−
98−In the case of a function with no arguments, the parentheses may be omitted, as follows::
99−
100− example3 = \ -> 100
101−
102−which would correspond to the Javascript::
103−
104− function example3() {
105− return 100;
106− }
107−
108−Multiple-argument and single-argument syntax can be mixed, as follows::
109−
110− example4 = \a (b, c) d -> a + b + c + d
111−
112−which generates::
113−
114− function example4(a) {
115− return function (b, c) {
116− return function (d) {
117− return a + b + c + d;
118− }
119− }
120− }
121−
122−Functions are applied by providing their arguments inside parentheses::
123−
124− test1(1, 2, 3)
125− test2()
126−
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::
128−
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
53+Functions in PureScript can have zero or more arguments. A function with multiple arguments is not to be confused with a `curried function`, which takes a single argument, and returns another function.
14054
14155Polymorphic Types
14256-----------------
@@ -147,7 +61,7 @@ Here is an example::
14761
14862 identity x = x
14963
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.
64+`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.
15165
15266A type annotation can also be provided::
15367
@@ -158,7 +72,7 @@ Functions may also be polymorphic in row types or type variables with other kind
15872
15973 addProps o = o.foo + o.bar
16074
161−Here, `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*.
75+Here, `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*.
16276
16377So, the following compiles::
16478
@@ -175,7 +89,7 @@ Again, a type annotation can be provided if necessary.
17589Rank N Types
17690------------
17791
178−It 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.
92+It 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.
17993
18094In most cases, a type annotation is necessary when using this feature.
18195
@@ -186,16 +100,16 @@ As an example, we can pass a polymorphic function as an argument to another func
186100
187101Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
188102
189−An argument to `poly` must indeed be polymorphic. For example, the following fails::
103+An argument to ``poly`` must indeed be polymorphic. For example, the following fails::
190104
191105 test = poly (\n -> n + 1)
192106
193−since the skolemized type variable `a` does not unify with `Number`.
107+since the skolemized type variable ``a`` does not unify with ``Number``.
194108
195109Type Synonyms
196110-------------
197111
198−For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.
112+For convenience, it is possible to declare a synonym for a type using the ``type`` keyword. Type synonyms can include type arguments.
199113
200114For example::
201115
@@ -203,6 +117,11 @@ For example::
203117
204118 addFoo :: Foo -> Number
205119 addFoo = \o -> o.foo + o.bar
120+
121+Constrained Types
122+-----------------
123+
124+Polymorphic types may be predicated on one or more ``constraints``. See the chapter on type classes for more information.
206125
207126Type Annotations
208127----------------
@@ -214,10 +133,10 @@ Most types can be inferred (not including Rank N Types and constrained types), b
214133Kind System
215134-----------
216135
217−There are two primitive kinds, the kind `*` of types and the kind `!` of effects.
136+There are two primitive kinds, the kind ``*`` of types and the kind ``!`` of effects.
218137
219−For 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.
138+For 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.
220139
221−Type constructors are given the arrow kind `k1 -> k2` for appropriate kinds `k1`, `k2`.
140+Type constructors are given the arrow kind ``k1 -> k2`` for appropriate kinds ``k1``, ``k2``.
222141
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.
142+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.