@@ -56,16 +56,18 @@ PureScript can also be seen as a trade-off between a theoretically ideal languag
5656
5757The following code defines a `Person` data type and a function to generate a string representation for a `Person`:
5858
59− data Person = Person
60− { name :: String
61− ; age :: Number
62− }
63−
64− extern itoa :: Number -> String
65−
66− showPerson :: Person -> String
67− showPerson = \p -> case p of
68− Person o -> o.name ++ ", aged " ++ itoa(o.age)
59+```haskell
60+data Person = Person
61+{ name :: String
62+; age :: Number
63+}
64+
65+extern itoa :: Number -> String
66+
67+showPerson :: Person -> String
68+showPerson = \p -> case p of
69+Person o -> o.name ++ ", aged " ++ itoa(o.age)
70+```
6971
7072Line by line, this reads as follows:
7173
@@ -77,20 +79,22 @@ Line by line, this reads as follows:
7779
7880The generated Javascript looks like this:
7981
80− var Person = function (value) {
81− return { ctor: 'Person', value: value };
82− };
83−
84− var showPerson = function (p) {
85− return (function (_0) {
86− if (_0.ctor === "Person") {
87− var _1 = _0.value;
88− var o = _1;
89− return o.name ++ ", aged " ++ itoa(o.age);
90− }
91− throw "Failed pattern match";
92− })(p);
93− };
82+```javascript
83+var Person = function (value) {
84+return { ctor: 'Person', value: value };
85+};
86+
87+var showPerson = function (p) {
88+return (function (_0) {
89+if (_0.ctor === "Person") {
90+ var _1 = _0.value;
91+ var o = _1;
92+ return o.name ++ ", aged " ++ itoa(o.age);
93+}
94+throw "Failed pattern match";
95+})(p);
96+};
97+```
9498
9599## Type System
96100
@@ -124,9 +128,11 @@ PureScript supports the same binary and unary operations on primitive types as J
124128
125129Examples:
126130
127− num = 1 + 2 * 3
128− str = "Hello, " ++ "World!"
129− bool = 1 > 2 || true
131+```haskell
132+num = 1 + 2 * 3
133+str = "Hello, " ++ "World!"
134+bool = 1 > 2 || true
135+```
130136
131137## Arrays
132138
@@ -152,14 +158,16 @@ Tagged unions can only be created using their constructors, and deconstructed th
152158
153159For example:
154160
155− data Foo a = Foo | Bar String
156−
157− runFoo = \foo -> case foo of
158− Foo -> "It's a Foo"
159− Bar s -> "It's a Bar. The string is " ++ s
160−
161− runFoo Foo ++ runFoo (Bar "Test")
162−
161+```haskell
162+data Foo a = Foo | Bar String
163+
164+runFoo = \foo -> case foo of
165+Foo -> "It's a Foo"
166+Bar s -> "It's a Bar. The string is " ++ s
167+
168+runFoo Foo ++ runFoo (Bar "Test")
169+```
170+
163171In 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.
164172
165173`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.
@@ -170,34 +178,42 @@ Functions in PureScript can have zero or more arguments in general, just like in
170178
171179Functions are introduced by using a backslash followed by a comma separated list of argument names:
172180
173− test1 = \a, b, c -> a + b + c
174−
181+```haskell
182+test1 = \a, b, c -> a + b + c
183+```
184+
175185which would correspond to the Javascript `function test1(a, b, c) { return a + b + c; }`
176186
177187A function taking no arguments would look like this:
178188
179− test2 = \ -> 100
180−
189+```haskell
190+test2 = \ -> 100
191+```
192+
181193which would correspond to the Javascript `function test2() { return 100; }`
182194
183195Functions are applied by providing their arguments inside parentheses:
184196
185− test1(1, 2, 3)
186− test2()
187−
197+```haskell
198+test1(1, 2, 3)
199+test2()
200+```
201+
188202A 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:
189203
190− -- has type Number -> Number -> Number -> Number
191− addThree = \a -> \b -> \c -> a + b + c
192−
193− -- has type Number -> Number -> Number
194− addThree 1
195−
196− -- has type Number -> Number
197− addThree 1 2
198−
199− -- has type Number
200− addThree 1 2 3
204+```haskell
205+-- has type Number -> Number -> Number -> Number
206+addThree = \a -> \b -> \c -> a + b + c
207+
208+-- has type Number -> Number -> Number
209+addThree 1
210+
211+-- has type Number -> Number
212+addThree 1 2
213+
214+-- has type Number
215+addThree 1 2 3
216+```
201217
202218## Polymorphic Types
203219
@@ -205,28 +221,38 @@ Expressions defined at the top level may have polymorphic types.
205221
206222Here is an example:
207223
208− identity = \x -> x
209−
224+```haskell
225+identity = \x -> x
226+```
227+
210228`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.
211229
212230A type annotation can also be provided:
213231
214− identity :: forall a. a -> a
215− identity = \x -> x
216−
232+```haskell
233+identity :: forall a. a -> a
234+identity = \x -> x
235+```
236+
217237Functions may also be polymorphic in row types or type variables with other kinds (see "Kind System"):
218238
219− addProps = \o -> o.foo + o.bar
239+```haskell
240+addProps = \o -> o.foo + o.bar
241+```
220242
221243Here, `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*.
222244
223245So, the following compiles:
224246
225− addProps { foo: 1, bar: 2, baz: 3 }
247+```haskell
248+addProps { foo: 1, bar: 2, baz: 3 }
249+```
226250
227251but the following does not:
228252
229− addProps { foo: 1 }
253+```haskell
254+addProps { foo: 1 }
255+```
230256
231257since the `bar` property is missing.
232258
@@ -246,13 +272,17 @@ Syntax is whitespace sensitive. The general rule of thumb is that declarations w
246272
247273That is, the following is valid:
248274
249− foo = bar(x) +
250− baz(x)
275+```haskell
276+foo = bar(x) +
277+ baz(x)
278+```
251279
252280But this is not:
253281
254− foo = bar(x) +
255− baz(x)
282+```haskell
283+foo = bar(x) +
284+baz(x)
285+```
256286
257287## Do Notation
258288
@@ -271,38 +301,46 @@ The following types of statement are supported:
271301
272302Here is an example of a power function defined using a block:
273303
274− pow = \n, p -> do
275− var m = n
276− for i <- 0 until p:
277− m = m * n
278− return m
304+```haskell
305+pow = \n, p -> do
306+ var m = n
307+ for i <- 0 until p:
308+ m = m * n
309+ return m
310+```
279311
280312Blocks enable local mutation of their variables, but mutation is not allowed in general. The type system prevents mutable variables from escaping their scope.
281313
282314That is, while the example above is valid, the following does not compile:
283315
284− incr = \n -> do
285− n = n + 1
286− return n
316+```haskell
317+incr = \n -> do
318+ n = n + 1
319+ return n
320+```
287321
288322The variable `n` is not mutable, and so the assignment in the first line of the `do` block is not allowed.
289323
290324This function can be rewritten as follows:
291325
292− incr = \n -> do
293− var m = n
294− m = m + 1
295− return m
326+```haskell
327+incr = \n -> do
328+ var m = n
329+ m = m + 1
330+ return m
331+```
296332
297333## For Loops
298334
299335For loops look like this:
300336
301− total = do
302− var n = 0
303− for i <- 0 until 10:
304− n = n + i
305− return n
337+```haskell
338+total = do
339+ var n = 0
340+ for i <- 0 until 10:
341+ n = n + i
342+ return n
343+```
306344
307345The bounds `0` and `10` are inclusive and exclusive respectively.
308346
@@ -310,38 +348,44 @@ The bounds `0` and `10` are inclusive and exclusive respectively.
310348
311349For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
312350
313− total = \arr -> do
314− var n = 0
315− foreach i in arr:
316− n = n + i
317− return n
351+```haskell
352+total = \arr -> do
353+ var n = 0
354+ foreach i in arr:
355+ n = n + i
356+ return n
357+```
318358
319359## While Loops
320360
321361The syntax of a while loop is similar to a foreach loop:
322362
323− log2 = \n -> do
324− var count = 0
325− var m = n
326− while m > 1:
327− m = m / 2
328− count = count + 1
329− return count
363+```haskell
364+log2 = \n -> do
365+ var count = 0
366+ var m = n
367+ while m > 1:
368+ m = m / 2
369+ count = count + 1
370+ return count
371+```
330372
331373## If-Then-Else Statements
332374
333375Else branches are optional, and may contain further `if` statements, just as in Javascript:
334376
335− collatz = \n -> do
336− var count = 0
337− var m = n
338− while m > 1:
339− if m % 2 == 0:
340− m = m / 2
341− else:
342− m = m * 3 + 1
343− count = count + 1
344− return count
377+```haskell
378+collatz = \n -> do
379+ var count = 0
380+ var m = n
381+ while m > 1:
382+ if m % 2 == 0:
383+ m = m / 2
384+ else:
385+ m = m * 3 + 1
386+ count = count + 1
387+ return count
388+```
345389
346390## If-Then-Else Expressions
347391
@@ -349,7 +393,9 @@ The `if`, `then` and `else` keywords can also be used to create conditional expr
349393
350394For example,
351395
352− conditional = if 2 > 1 then "ok" else "oops"
396+```haskell
397+conditional = if 2 > 1 then "ok" else "oops"
398+```
353399
354400## Pattern Matching
355401
@@ -357,10 +403,12 @@ Pattern matching deconstructs a value to bring zero or more expressions into sco
357403
358404Pattern matches have the following general form
359405
360− case value of
361− pattern -> result
362− ...
363− pattern -> result
406+```haskell
407+case value of
408+ pattern -> result
409+ -- ...
410+ pattern -> result
411+```
364412
365413The following pattern types are supported:
366414
@@ -378,31 +426,37 @@ Patterns need not be exhaustive. A pattern match failed exception will be thrown
378426
379427The wilcard `_` matches any input and brings nothing into scope:
380428
381− case x of
382− _ -> 0
429+```haskell
430+case x of
431+ _ -> 0
432+```
383433
384434## Literal Patterns
385435
386436Literal patterns are provided to match on primitives:
387437
388− case booleanValue of
389− true -> 0
390− false -> 1
391−
392− case stringValue of
393− "Foo" -> 0
394− _ -> 1
395−
396− case numericValue of
397− 0 -> 0
398− _ -> 1
438+```haskell
439+case booleanValue of
440+ true -> 0
441+ false -> 1
442+
443+case stringValue of
444+ "Foo" -> 0
445+ _ -> 1
446+
447+case numericValue of
448+ 0 -> 0
449+ _ -> 1
450+```
399451
400452## Variable Pattern
401453
402454A variable pattern matches any input and binds that input to its name:
403455
404− case foo(x) of
405− result -> bar(result)
456+```haskell
457+case foo(x) of
458+ result -> bar(result)
459+```
406460
407461## Array Patterns
408462
@@ -410,10 +464,12 @@ Array patterns match an input which is an array, and bring its elements into sco
410464
411465For example:
412466
413− f = \arr -> case arr of
414− [x] -> x
415− [x, y : xs] -> x * y + f(xs)
416− _ -> 0
467+```haskell
468+f = \arr -> case arr of
469+ [x] -> x
470+ [x, y : xs] -> x * y + f(xs)
471+ _ -> 0
472+```
417473
418474Here, the first pattern only matches arrays of length one, and brings the first element of the array into scope.
419475
@@ -423,9 +479,11 @@ The second pattern matches arrays with two or more elements, and brings the firs
423479
424480Record patterns match an input which is a record, and bring its properties into scope.
425481
426− f = \o -> case o of
427− { foo = "Foo" } -> o.bar
428− _ -> 0
482+```haskell
483+f = \o -> case o of
484+ { foo = "Foo" } -> o.bar
485+ _ -> 0
486+```
429487
430488## Nested Patterns
431489
@@ -433,54 +491,65 @@ The patterns above can be combined to create larger patterns.
433491
434492For example:
435493
436− f = \o -> case o of
437− { arr = [x:_], take = "car" } -> x
438− { arr = [_,x:_], take = "cadr" } -> x
439− _ -> 0
494+```haskell
495+f = \o -> case o of
496+ { arr = [x:_], take = "car" } -> x
497+ { arr = [_,x:_], take = "cadr" } -> x
498+ _ -> 0
499+```
440500
441501## Named Patterns
442502
443503Named patterns bring additional names into scope when using nested patterns. Any pattern can be named by using the `@` symbol:
444504
445− f = \arr -> case arr of
446− a@[_,_:_] -> a
447− a -> a
448−
505+```haskell
506+f = \arr -> case arr of
507+ a@[_,_:_] -> a
508+ a -> a
509+```
510+
449511Here, in the first pattern, any array with two or more elements will be matched and bound to the variable `a`.
450512
451513## Guards
452514
453515Guards are used to impose additional constraints inside a pattern using boolean-valued expressions, and are introduced with a pipe after the pattern:
454516
455− evens = \arr -> case arr of
456− [] -> 0
457− [x:xs] | x % 2 == 0 -> 1 + evens xs
458− [_:xs] -> evens xs
459−
517+```haskell
518+evens = \arr -> case arr of
519+ [] -> 0
520+ [x:xs] | x % 2 == 0 -> 1 + evens xs
521+ [_:xs] -> evens xs
522+```
523+
460524## Type Synonyms
461525
462526For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.
463527
464528For example:
465529
466− type Foo = { foo :: Number, bar Number }
467−
468− addFoo :: Foo -> Number
469− addFoo = \o -> o.foo + o.bar
470−
530+```haskell
531+type Foo = { foo :: Number, bar Number }
532+
533+addFoo :: Foo -> Number
534+addFoo = \o -> o.foo + o.bar
535+```
471536## Record Updates
472537
473538Properties on records can be updated using the following syntax: `o { key = value, ..., key = value }`
474539
475540For example, the following function increments the `foo` property on its argument:
476541
477− incr = \o -> o { foo = o.foo + 1 }
542+```haskell
543+incr = \o -> o { foo = o.foo + 1 }
544+```
478545
479546The generated Javascript assumes the existence of a method called `Object.extend` such that `Object.extend(o, p)` takes an object `o` and generates a shallow copy of `o` including the properties of `p`. A simple JQuery implementation of this specification is
480547
481− Object.prototype.extend = function(o, p) {
482− return $.extend({}, o, p);
483− };
548+```haskell
549+Object.prototype.extend = function(o, p) {
550+ return $.extend({}, o, p);
551+};
552+```
484553
485554## Operators
486555
@@ -488,31 +557,40 @@ In addition to the standard operators, user-defined infix operators can be creat
488557
489558E.g. to create a synonym for string concatenation:
490559
491− (<>) = \s1 -> \s2 -> s1 ++ s2
492−
493− greeting = "Hello" <> "World!"
494−
560+```haskell
561+(<>) = \s1 -> \s2 -> s1 ++ s2
562+
563+greeting = "Hello" <> "World!"
564+```
565+
495566Regular functions can be used as operators by enclosing their names in backticks:
496567
497− foo = \x -> \y -> x * y + y
498−
499− test = 10 `foo` 20
568+```haskell
569+foo = \x -> \y -> x * y + y
570+
571+test = 10 `foo` 20
572+```
500573
501574Fixity declarations can associate a precedence level, which is a natural number, to a user-defined operator, and specify which way it associates:
502575
503− infixl 5 <>
504− infixr 7 %%
576+```haskell
577+infixl 5 <>
578+infixr 7 %%
579+```
505580
506581## Foreign Function Interface
507582
508583The `extern` keyword declares a value which is defined in Javascript, and its type:
509584
510− extern pow :: (Number, Number) -> Number
511−
585+```haskell
586+extern pow :: (Number, Number) -> Number
587+```
588+
512589To declare a new type with no constructors, use `extern data` and provide the kind:
513590
514− extern data IO :: * -> *
591+extern data IO :: * -> *
515592
516− extern console :: {
517− log :: String -> IO {}
518− }
593+extern console :: {
594+ log :: String -> IO {}
595+}
596+```