git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update docs
3059a14Phil Freeman
README.md+44−27
@@ -74,7 +74,7 @@ As an introductory example, here is the usual "Hello World" written in PureScrip
74```haskell
74```haskell
75foreign import console :: { log :: String -> {} }
75foreign import console :: { log :: String -> {} }
76
76
77−main = \() -> console.log "Hello, World!"
77+main = \ -> console.log "Hello, World!"
78```
78```
79
79
80which compiles to the following Javascript:
80which compiles to the following Javascript:
@@ -95,8 +95,7 @@ data Person = Person { name :: String, age :: Number }
95foreign import numberToString :: Number -> String
95foreign import numberToString :: Number -> String
96
96
97showPerson :: Person -> String
97showPerson :: Person -> String
98−showPerson = \p -> case p of
99− Person o -> o.name ++ ", aged " ++ numberToString(o.age)
98+showPerson (Person o) = o.name ++ ", aged " ++ numberToString(o.age)
100```
99```
101
100
102Line by line, this reads as follows:
101Line by line, this reads as follows:
@@ -105,21 +104,21 @@ Line by line, this reads as follows:
105 - The `Person` constructor takes an object with two properties, `name` which is a `String`, and `age` which is a `Number`
104 - The `Person` constructor takes an object with two properties, `name` which is a `String`, and `age` which is a `Number`
106- The `numberToString` function is written in Javascript, and converts a `Number` to its `String` representation
105- The `numberToString` function is written in Javascript, and converts a `Number` to its `String` representation
107- The `showPerson` function takes a `Person` and returns a `String`
106- The `showPerson` function takes a `Person` and returns a `String`
108−- `showPerson` works by case analysis on its argument `p`, first matching the constructor `Person` and then using string concatenation and object accessors to return its result.
107+- `showPerson` works by case analysis on its argument, first matching the constructor `Person` and then using string concatenation and object accessors to return its result.
109
108
110The generated Javascript looks like this:
109The generated Javascript looks like this:
111
110
112```javascript
111```javascript
113var Person = function (value) {
112var Person = function (value) {
114− return { ctor: 'Person', value: value };
113+ return { ctor: 'Person', value: value };
115};
114};
116
115
117−function showPerson(p) {
116+function showPerson(_1) {
118− if (p.ctor === "Person") {
117+ if (_1.ctor === "Person") {
119− return p.value.name + ", aged " + numberToString(p.value.age)
118+ return _1.value.name + ", aged " + numberToString(_1.value.age);
120− };
119+ };
121− throw "Failed pattern match"
120+ throw "Failed pattern match";
122−}
121+};
123
122
124```
123```
125
124
@@ -141,7 +140,7 @@ The type system defines the following types:
141 - E.g. `Number -> String`
140 - E.g. `Number -> String`
142 - E.g. `(Number, Number) -> Number`
141 - E.g. `(Number, Number) -> Number`
143 - Functions can have zero or more arguments
142 - Functions can have zero or more arguments
144−- Polymorphic types (for top level declarations only)
143+- Polymorphic types
145 - E.g. `forall a. a -> a`
144 - E.g. `forall a. a -> a`
146
145
147## Primitive Types
146## Primitive Types
@@ -188,11 +187,10 @@ For example:
188```haskell
187```haskell
189data Foo a = Foo | Bar String
188data Foo a = Foo | Bar String
190
189
191−runFoo = \foo -> case foo of
192−Foo -> "It's a Foo"
190+runFoo Foo = "It's a Foo"
193−Bar s -> "It's a Bar. The string is " ++ s
191+runFoo (Bar s) = "It's a Bar. The string is " ++ s
194
192
195−runFoo Foo ++ runFoo (Bar "Test")
193+test = runFoo Foo ++ runFoo (Bar "Test")
196```
194```
197
195
198In 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.
196In 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.
@@ -289,7 +287,7 @@ Expressions defined at the top level may have polymorphic types.
289Here is an example:
287Here is an example:
290
288
291```haskell
289```haskell
292−identity = \x -> x
290+identity x = x
293```
291```
294
292
295`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.
293`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.
@@ -298,13 +296,13 @@ A type annotation can also be provided:
298
296
299```haskell
297```haskell
300identity :: forall a. a -> a
298identity :: forall a. a -> a
301−identity = \x -> x
299+identity x = x
302```
300```
303
301
304Functions may also be polymorphic in row types or type variables with other kinds (see "Kind System"):
302Functions may also be polymorphic in row types or type variables with other kinds (see "Kind System"):
305
303
306```haskell
304```haskell
307−addProps = \o -> o.foo + o.bar
305+addProps o = o.foo + o.bar
308```
306```
309
307
310Here, `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*.
308Here, `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*.
@@ -335,7 +333,7 @@ As an example, we can pass a polymorphic function as an argument to another func
335
333
336```haskell
334```haskell
337poly :: (forall a. a -> a) -> Boolean
335poly :: (forall a. a -> a) -> Boolean
338−poly = \f -> (f 0 < 1) == f true
336+poly f = (f 0 < 1) == f true
339```
337```
340
338
341Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
339Notice that the polymorphic function's type argument is instantiated to both `Number` and `Boolean`.
@@ -391,7 +389,7 @@ The following types of statement are supported:
391Here is an example of a power function defined using a block:
389Here is an example of a power function defined using a block:
392
390
393```haskell
391```haskell
394−pow = \n p -> {
392+pow n p = {
395 var m = n;
393 var m = n;
396 for (i <- 0 until p) {
394 for (i <- 0 until p) {
397 m = m * n;
395 m = m * n;
@@ -405,7 +403,7 @@ Blocks enable local mutation of their variables, but mutation is not allowed in
405That is, while the example above is valid, the following does not compile:
403That is, while the example above is valid, the following does not compile:
406
404
407```haskell
405```haskell
408−incr = \n -> {
406+incr n = {
409 n = n + 1;
407 n = n + 1;
410 return n;
408 return n;
411 }
409 }
@@ -416,7 +414,7 @@ The variable `n` is not mutable, and so the assignment in the first line of the
416This function can be rewritten as follows:
414This function can be rewritten as follows:
417
415
418```haskell
416```haskell
419−incr = \n -> {
417+incr n = {
420 var m = n;
418 var m = n;
421 m = m + 1;
419 m = m + 1;
422 return m;
420 return m;
@@ -444,7 +442,7 @@ The bounds `0` and `10` are inclusive and exclusive respectively.
444For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
442For each loops loop over the elements in an array using the `Object.forEach` method. A polyfill may be required for some browsers:
445
443
446```haskell
444```haskell
447−total = \arr -> {
445+total arr = {
448 var n = 0;
446 var n = 0;
449 foreach (i in arr) {
447 foreach (i in arr) {
450 n = n + i;
448 n = n + i;
@@ -458,7 +456,7 @@ total = \arr -> {
458The syntax of a while loop is similar to a foreach loop:
456The syntax of a while loop is similar to a foreach loop:
459
457
460```haskell
458```haskell
461−log2 = \n -> {
459+log2 n = {
462 var count = 0;
460 var count = 0;
463 var m = n;
461 var m = n;
464 while (m > 1) {
462 while (m > 1) {
@@ -474,7 +472,7 @@ log2 = \n -> {
474Else branches are optional, and may contain further `if` statements, just as in Javascript:
472Else branches are optional, and may contain further `if` statements, just as in Javascript:
475
473
476```haskell
474```haskell
477−collatz = \n -> {
475+collatz n = {
478 var count = 0;
476 var count = 0;
479 var m = n;
477 var m = n;
480 while (m > 1) {
478 while (m > 1) {
@@ -494,7 +492,7 @@ collatz = \n -> {
494Any expression with the empty type `{ }` can be used as a statement in a block. For example, method calls which return `{ }`:
492Any expression with the empty type `{ }` can be used as a statement in a block. For example, method calls which return `{ }`:
495
493
496```haskell
494```haskell
497−example = \n m -> {
495+example n m = {
498 console.log "Adding two numbers"
496 console.log "Adding two numbers"
499 return n + m;
497 return n + m;
500 }
498 }
@@ -523,6 +521,18 @@ case value of
523 pattern -> result
521 pattern -> result
524```
522```
525
523
524+Pattern matching can also be used in the declaration of functions, as we have already seen:
525+
526+```haskell
527+fn pattern_1 ... pattern_n = result
528+```
529+
530+Any of the above types of pattern are also valid when introducing functions. In addition, patterns can also be grouped in parentheses to introduce multiple-arugument functions. For example,
531+
532+```haskell
533+example x (y, z) w = x * y + z * w
534+```
535+
526The following pattern types are supported:
536The following pattern types are supported:
527
537
528- Wildcard pattern
538- Wildcard pattern
@@ -653,6 +663,13 @@ evens = \arr -> case arr of
653 _ : xs -> evens xs
663 _ : xs -> evens xs
654```
664```
655
665
666+When defining a function, guards appear after all patterns:
667+
668+```haskell
669+greater x y | x > y = true
670+greater _ _ = false
671+```
672+
656## Type Synonyms
673## Type Synonyms
657
674
658For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.
675For convenience, it is possible to declare a synonym for a type using the `type` keyword. Type synonyms can include type arguments.