@@ -3,9 +3,7 @@ Types
4The type system defines the following types:
4The type system defines the following types:
6−- Number
6+- Primitive Types: Number, String, Boolean
11- Tagged Unions
9- Tagged Unions
@@ -17,36 +15,17 @@ The type system defines the following types:
17Primitive Types
15Primitive Types
18---------------
16---------------
20−The three primitive types `String`, `Number` and `Boolean` correspond to their Javascript equivalents at runtime.
18+The three primitive types ``String``, ``Number`` and ``Boolean`` correspond to their Javascript equivalents at runtime.
22−PureScript supports the same binary and unary operations on primitive types as Javascript, with the following exceptions:
24−- String concatenation is denoted `++` to differentiate it from numeric addition, `+`
25−- PureScript's `==` and `!=` correspond to Javascript's strong equality tests `===` and `!==`
30− str = "Hello, " ++ "World!"
36−PureScript arrays correspond to Javascript arrays at runtime, but all elements must have the same type.
23+PureScript arrays correspond to Javascript arrays at runtime, but all elements in an array must have the same type.
38−Array literals look like Javascript array literals: `[1, 2, 3]`
40−Array elements can be read using array index notation `arr !! index`
45−PureScript records correspond to Javascript objects.
28+PureScript records correspond to Javascript objects. They may have zero or more named fields, each with their own types.
47−Record literals look like Javascript object literals: `{ foo: "Foo" }`
49−Properties can be read by using dot notation: `o.foo`
51Tagged Unions
30Tagged Unions
52-------------
31-------------
@@ -64,79 +43,14 @@ For example::
65 test = runFoo Foo ++ runFoo (Bar "Test")
44 test = runFoo Foo ++ runFoo (Bar "Test")
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.
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.
74−Functions in PureScript can have zero or more arguments in general, just like in Javascript.
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.
76−Functions are introduced by using a backslash followed by a list of argument names::
78− example1 = \a b -> a + b
80−which would correspond to the following Javascript::
82− function example1(a) {
83− return function (b) {
88−Multiple argument functions can be introduced by wrapping the arguments in parentheses, and separating them with commas:
90− example2 = \(a, b) -> a + b
94− function example2(a, b) {
98−In the case of a function with no arguments, the parentheses may be omitted, as follows::
102−which would correspond to the Javascript::
104− function example3() {
108−Multiple-argument and single-argument syntax can be mixed, as follows::
110− example4 = \a (b, c) d -> a + b + c + d
114− function example4(a) {
115− return function (b, c) {
116− return function (d) {
117− return a + b + c + d;
122−Functions are applied by providing their arguments inside parentheses::
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::
129− -- has type Number -> Number -> Number -> Number
130− addThree = \a b c -> a + b + c
132− -- has type Number -> Number -> Number
135− -- has type Number -> Number
141Polymorphic Types
55Polymorphic Types
142-----------------
56-----------------
@@ -147,7 +61,7 @@ Here is an example::
148 identity x = x
62 identity x = x
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.
152A type annotation can also be provided::
66A type annotation can also be provided::
@@ -158,7 +72,7 @@ Functions may also be polymorphic in row types or type variables with other kind
159 addProps o = o.foo + o.bar
73 addProps o = o.foo + o.bar
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*.
163So, the following compiles::
77So, the following compiles::
@@ -175,7 +89,7 @@ Again, a type annotation can be provided if necessary.
175Rank N Types
89Rank N Types
176------------
90------------
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.
180In most cases, a type annotation is necessary when using this feature.
94In most cases, a type annotation is necessary when using this feature.
@@ -186,16 +100,16 @@ As an example, we can pass a polymorphic function as an argument to another func
187Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
101Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
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::
191 test = poly (\n -> n + 1)
105 test = poly (\n -> n + 1)
193−since the skolemized type variable `a` does not unify with `Number`.
107+since the skolemized type variable ``a`` does not unify with ``Number``.
195Type Synonyms
109Type Synonyms
196-------------
110-------------
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.
200For example::
114For example::
@@ -203,6 +117,11 @@ For example::
204 addFoo :: Foo -> Number
118 addFoo :: Foo -> Number
205 addFoo = \o -> o.foo + o.bar
119 addFoo = \o -> o.foo + o.bar
124+Polymorphic types may be predicated on one or more ``constraints``. See the chapter on type classes for more information.
207Type Annotations
126Type Annotations
208----------------
127----------------
@@ -214,10 +133,10 @@ Most types can be inferred (not including Rank N Types and constrained types), b
214Kind System
133Kind System
215-----------
134-----------
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.
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.
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``.
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.