git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Further update prelude docs, remove (%)
2fd349cGary Burgess
examples/passing/Collatz.purs+12−1
@@ -1,9 +1,20 @@
1module Main where
1module Main where
2
2
3−import Prelude
4import Control.Monad.Eff
3import Control.Monad.Eff
5import Control.Monad.ST
4import Control.Monad.ST
6
5
6+foreign import jsMod
7+ """
8+ function jsMod(x) {
9+ return function (y) {
10+ return x % y;
11+ };
12+ }
13+ """ :: Number -> Number -> Number
14+
15+infixl 7 %
16+(%) = jsMod
17+
7collatz :: Number -> Number
18collatz :: Number -> Number
8collatz n = runPure (runST (do
19collatz n = runPure (runST (do
9 r <- newSTRef n
20 r <- newSTRef n
examples/passing/Guards.purs+31−21
@@ -1,29 +1,39 @@
1module Main where
1module Main where
2
2
3− import Prelude
3+foreign import jsMod
4+ """
5+ function jsMod(x) {
6+ return function (y) {
7+ return x % y;
8+ };
9+ }
10+ """ :: Number -> Number -> Number
4
11
5− collatz = \x -> case x of
12+infixl 7 %
6− y | y % 2 == 0 -> y / 2
13+(%) = jsMod
7− y -> y * 3 + 1
8
14
9− -- Guards have access to current scope
10− collatz2 = \x y -> case x of
15+collatz = \x -> case x of
11− z | y > 0 -> z / 2
16+ y | y % 2 == 0 -> y / 2
12− z -> z * 3 + 1
17+ y -> y * 3 + 1
13
18
14− min :: forall a. (Ord a) => a -> a -> a
19+-- Guards have access to current scope
15− min n m | n < m = n
20+collatz2 = \x y -> case x of
16− | otherwise = m
21+ z | y > 0 -> z / 2
22+ z -> z * 3 + 1
17
23
18− max :: forall a. (Ord a) => a -> a -> a
24+min :: forall a. (Ord a) => a -> a -> a
19− max n m = case unit of
25+min n m | n < m = n
20− _ | m < n -> n
21− | otherwise -> m
26+ | otherwise = m
22
27
23− testIndentation :: Number -> Number -> Number
28+max :: forall a. (Ord a) => a -> a -> a
24− testIndentation x y | x > 0
29+max n m = case unit of
25− = x + y
30+ _ | m < n -> n
26− | otherwise
31+ | otherwise -> m
27− = y - x
28
32
33+testIndentation :: Number -> Number -> Number
34+testIndentation x y | x > 0
35+ = x + y
36+ | otherwise
37+ = y - x
38+
29− main = Debug.Trace.trace $ min "Done" "ZZZZ"
39+main = Debug.Trace.trace $ min "Done" "ZZZZ"
examples/passing/OperatorAssociativity.purs−6
@@ -35,12 +35,6 @@ main = do
35 assert (6 / (3 * 2) == 1) "6 / (3 * 2) == 1"
35 assert (6 / (3 * 2) == 1) "6 / (3 * 2) == 1"
36 assert ((6 / 3) * 2 == 4) "(6 / 3) * 2 == 4"
36 assert ((6 / 3) * 2 == 4) "(6 / 3) * 2 == 4"
37
37
38− assert (6 % (2 * 2) == 2) "6 % (2 * 2) == 2"
39− assert ((6 % 2) * 2 == 0) "(6 % 2) * 2 == 0"
40−
41− assert (4 % (9 / 3) == 1) "4 % (9 / 3) == 1"
42− assert ((4 % 9) / 2 == 2) "(4 % 9) / 2 == 2"
43−
44 assert (not (1 < 0) == true) "not (1 < 0) == true"
38 assert (not (1 < 0) == true) "not (1 < 0) == true"
45 assert (not ((negate 1) < 0) == false) "not ((negate 1) < 0) == false"
39 assert (not ((negate 1) < 0) == false) "not ((negate 1) < 0) == false"
46
40
examples/passing/TopLevelCase.purs+21−11
@@ -1,18 +1,28 @@
1module Main where
1module Main where
2
2
3− import Prelude
3+foreign import jsMod
4+ """
5+ function jsMod(x) {
6+ return function (y) {
7+ return x % y;
8+ };
9+ }
10+ """ :: Number -> Number -> Number
4
11
5− gcd :: Number -> Number -> Number
12+infixl 7 %
6− gcd 0 x = x
13+(%) = jsMod
7− gcd x 0 = x
8− gcd x y | x > y = gcd (x % y) y
9− gcd x y = gcd (y % x) x
10
14
11− guardsTest (x:xs) | x > 0 = guardsTest xs
15+gcd :: Number -> Number -> Number
12− guardsTest xs = xs
16+gcd 0 x = x
17+gcd x 0 = x
18+gcd x y | x > y = gcd (x % y) y
19+gcd x y = gcd (y % x) x
13
20
14− data A = A
21+guardsTest (x:xs) | x > 0 = guardsTest xs
22+guardsTest xs = xs
15
23
16− parseTest A 0 = 0
24+data A = A
17
25
26+parseTest A 0 = 0
27+
18− main = Debug.Trace.trace "Done"
28+main = Debug.Trace.trace "Done"
prelude/README.md+246−217
@@ -9,9 +9,11 @@ newtype Unit
9 = Unit { }
9 = Unit { }
10```
10```
11
11
12−The `Unit` type has a single inhabitant, called `unit`. It represents values with no computational content.
12+The `Unit` type has a single inhabitant, called `unit`. It represents
13+values with no computational content.
13
14
14−`Unit` is often used, wrapped in a monadic type constructor, as the return type of a computation where only
15+`Unit` is often used, wrapped in a monadic type constructor, as the
16+return type of a computation where only
15the _effects_ are important.
17the _effects_ are important.
16
18
17#### `unit`
19#### `unit`
@@ -22,165 +24,101 @@ unit :: Unit
22
24
23`unit` is the sole inhabitant of the `Unit` type.
25`unit` is the sole inhabitant of the `Unit` type.
24
26
25−#### `otherwise`
27+#### `($)`
26
28
27``` purescript
29``` purescript
28−otherwise :: Boolean
30+($) :: forall a b. (a -> b) -> a -> b
29```
31```
30
32
31−An alias for `true`, which can be useful in guard clauses:
33+Applies a function to its argument.
32
34
33```purescript
35```purescript
34−max x y | x >= y = x
36+length $ groupBy productCategory $ filter isInStock $ products
35− | otherwise = y
36−```
37−
38−#### `flip`
39−
40−``` purescript
41−flip :: forall a b c. (a -> b -> c) -> b -> a -> c
42```
37```
43
38
44−Flips the order of the arguments to a function of two arguments.
39+is equivalent to:
45
40
46```purescript
41```purescript
47−flip const 1 2 = const 2 1 = 2
42+length (groupBy productCategory (filter isInStock products))
48−```
49−
50−#### `const`
51−
52−``` purescript
53−const :: forall a b. a -> b -> a
54```
43```
55
44
56−Returns its first argument and ignores its second.
45+`($)` is different from [`(#)`](#-2) because it is right-infix instead of
46+left: `a $ b $ c $ d x = a $ (b $ (c $ (d $ x))) = a (b (c (d x)))`
57
47
58−```purescript
59−const 1 "hello" = 1
60−```
61−
62−#### `asTypeOf`
48+#### `(#)`
63
49
64``` purescript
50``` purescript
65−asTypeOf :: forall a. a -> a -> a
51+(#) :: forall a b. a -> (a -> b) -> b
66```
52```
67
53
68−This function returns its first argument, and can be used to assert type equalities.
54+Applies an argument to a function.
69−This can be useful when types are otherwise ambiguous.
70
55
71```purescript
56```purescript
72−main = print $ [] `asTypeOf` [0]
57+products # filter isInStock # groupBy productCategory # length
73```
58```
74
59
75−If instead, we had written `main = print []`, the type of the argument `[]` would have
60+is equivalent to:
76−been ambiguous, resulting in a compile-time error.
77
61
78−#### `Semigroupoid`
79−
80−``` purescript
62+```purescript
81−class Semigroupoid a where
63+length (groupBy productCategory (filter isInStock products))
82− (<<<) :: forall b c d. a c d -> a b c -> a b d
83```
64```
84
65
85−A `Semigroupoid` is similar to a [`Category`](#category) but does not require an identity
66+`(#)` is different from [`($)`](#-1) because it is left-infix instead of
86−element `id`, just composable morphisms.
67+right: `x # a # b # c # d = (((x # a) # b) # c) # d = d (c (b (a x)))`
87−
88−`Semigroupoid`s should obey the following rule:
89−
90−- Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
91−
92−One example of a `Semigroupoid` is the function type constructor `(->)`, with `(<<<)` defined
93−as function composition.
94
68
95−#### `semigroupoidArr`
69+#### `flip`
96
70
97``` purescript
71``` purescript
98−instance semigroupoidArr :: Semigroupoid Prim.Function
72+flip :: forall a b c. (a -> b -> c) -> b -> a -> c
99```
73```
100
74
75+Flips the order of the arguments to a function of two arguments.
101
76
102−#### `(>>>)`
103−
104−``` purescript
77+```purescript
105−(>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
78+flip const 1 2 = const 2 1 = 2
106```
79```
107
80
108−Forwards composition, or `(<<<)` with its arguments reversed.
81+#### `const`
109−
110−#### `Category`
111
82
112``` purescript
83``` purescript
113−class (Semigroupoid a) <= Category a where
114− id :: forall t. a t t
84+const :: forall a b. a -> b -> a
115```
85```
116
86
117−`Category`s consist of objects and composable morphisms between them, and as such are
87+Returns its first argument and ignores its second.
118−[`Semigroupoids`](#semigroupoid), but unlike `semigroupoids` must have an identity element.
119−
120−`Category`s should obey the following rules.
121−
122−- Left Identity: `id <<< p = p`
123−- Right Identity: `p <<< id = p`
124−
125−#### `categoryArr`
126
88
127−``` purescript
89+```purescript
128−instance categoryArr :: Category Prim.Function
90+const 1 "hello" = 1
129```
91```
130
92
131−
132−#### `($)`
93+#### `asTypeOf`
133
94
134``` purescript
95``` purescript
135−($) :: forall a b. (a -> b) -> a -> b
96+asTypeOf :: forall a. a -> a -> a
136−```
137−
138−Applies a function to its argument.
139−
140−```purescript
141−length $ groupBy productCategory $ filter isInStock $ products
142```
97```
143
98
144−is equivalent to
99+This function returns its first argument, and can be used to assert type
100+equalities. This can be useful when types are otherwise ambiguous.
145
101
146```purescript
102```purescript
147−length (groupBy productCategory (filter isInStock products))
103+main = print $ [] `asTypeOf` [0]
148```
104```
149
105
150−`($)` is different from [`(#)`](#-2) because it is right-infix instead of left, so
106+If instead, we had written `main = print []`, the type of the argument
151−`a $ b $ c $ d x` = `a $ (b $ (c $ (d $ x)))` = `a (b (c (d x)))`
107+`[]` would have been ambiguous, resulting in a compile-time error.
152
108
153−#### `(#)`
109+#### `otherwise`
154
110
155``` purescript
111``` purescript
156−(#) :: forall a b. a -> (a -> b) -> b
112+otherwise :: Boolean
157−```
158−
159−Applies an argument to a function.
160−
161−```purescript
162−products # filter isInStock # groupBy productCategory # length
163```
113```
164
114
165−is equivalent to
115+An alias for `true`, which can be useful in guard clauses:
166
116
167```purescript
117```purescript
168−length (groupBy productCategory (filter isInStock products))
118+max x y | x >= y = x
169−```
119+ | otherwise = y
170−
171−`(#)` is different from [`($)`](#-1) because it is left-infix instead of right, so
172−`x # a # b # c # d` = `(((x # a) # b) # c) # d` = `d (c (b (a x)))`
173−
174−#### `(:)`
175−
176−``` purescript
177−(:) :: forall a. a -> [a] -> [a]
178```
120```
179
121
180−An infix alias for `cons`.
181−
182−Note, the running time of this function is `O(n)`.
183−
184#### `cons`
122#### `cons`
185
123
186``` purescript
124``` purescript
@@ -195,57 +133,68 @@ cons 1 [2, 3, 4] = [1, 2, 3, 4]
195
133
196Note, the running time of this function is `O(n)`.
134Note, the running time of this function is `O(n)`.
197
135
198−#### `Show`
136+#### `(:)`
199
137
200``` purescript
138``` purescript
201−class Show a where
139+(:) :: forall a. a -> [a] -> [a]
202− show :: a -> String
203```
140```
204
141
205−The `Show` type class represents those types which can be converted into a human-readable `String` representation.
142+An infix alias for `cons`.
206
143
207−While not required, it is recommended that for any expression `x`, the string `show x` be executable PureScript code
144+Note, the running time of this function is `O(n)`.
208−which evaluates to the same value as the expression `x`.
209
145
210−#### `showBoolean`
146+#### `Semigroupoid`
211
147
212``` purescript
148``` purescript
213−instance showBoolean :: Show Boolean
149+class Semigroupoid a where
150+ (<<<) :: forall b c d. a c d -> a b c -> a b d
214```
151```
215
152
153+A `Semigroupoid` is similar to a [`Category`](#category) but does not
154+require an identity element `id`, just composable morphisms.
216
155
217−#### `showNumber`
156+`Semigroupoid`s must satisfy the following law:
218
157
219−``` purescript
158+- Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
220−instance showNumber :: Show Number
221−```
222
159
160+One example of a `Semigroupoid` is the function type constructor `(->)`,
161+with `(<<<)` defined as function composition.
223
162
224−#### `showString`
163+#### `semigroupoidArr`
225
164
226``` purescript
165``` purescript
227−instance showString :: Show String
166+instance semigroupoidArr :: Semigroupoid Prim.Function
228```
167```
229
168
230
169
231−#### `showUnit`
170+#### `(>>>)`
232
171
233``` purescript
172``` purescript
234−instance showUnit :: Show Unit
173+(>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
235```
174```
236
175
176+Forwards composition, or `(<<<)` with its arguments reversed.
237
177
238−#### `showArray`
178+#### `Category`
239
179
240``` purescript
180``` purescript
241−instance showArray :: (Show a) => Show [a]
181+class (Semigroupoid a) <= Category a where
182+ id :: forall t. a t t
242```
183```
243
184
185+`Category`s consist of objects and composable morphisms between them, and
186+as such are [`Semigroupoids`](#semigroupoid), but unlike `semigroupoids`
187+must have an identity element.
244
188
245−#### `showOrdering`
189+Instances must satisfy the following law in addition to the
190+`Semigroupoid` law:
191+
192+- Identity: `id <<< p = p <<< id = p`
193+
194+#### `categoryArr`
246
195
247``` purescript
196``` purescript
248−instance showOrdering :: Show Ordering
197+instance categoryArr :: Category Prim.Function
249```
198```
250
199
251
200
@@ -256,12 +205,14 @@ class Functor f where
256 (<$>) :: forall a b. (a -> b) -> f a -> f b
205 (<$>) :: forall a b. (a -> b) -> f a -> f b
257```
206```
258
207
259−A `Functor` is a type constructor which supports a mapping operation `(<$>)`.
208+A `Functor` is a type constructor which supports a mapping operation
209+`(<$>)`.
260
210
261−`(<$>)` can be used to turn functions `a -> b` into functions `f a -> f b` whose argument and return
211+`(<$>)` can be used to turn functions `a -> b` into functions
212+`f a -> f b` whose argument and return types use the type constructor `f`
262−types use the type constructor `f` to represent some computational context.
213+to represent some computational context.
263
214
264−`Functor` instances should satisfy the following laws:
215+Instances must satisfy the following laws:
265
216
266- Identity: `(<$>) id = id`
217- Identity: `(<$>) id = id`
267- Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
218- Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
@@ -291,10 +242,12 @@ instance functorArr :: Functor (Prim.Function r)
291void :: forall f a. (Functor f) => f a -> f Unit
242void :: forall f a. (Functor f) => f a -> f Unit
292```
243```
293
244
294−The `void` function is used to ignore the type wrapped by a [`Functor`](#functor), replacing it with `Unit` and
245+The `void` function is used to ignore the type wrapped by a
246+[`Functor`](#functor), replacing it with `Unit` and keeping only the type
295−keeping only the type information provided by the type constructor itself.
247+information provided by the type constructor itself.
296
248
297−`void` is often useful when using `do` notation to change the return type of a monadic computation:
249+`void` is often useful when using `do` notation to change the return type
250+of a monadic computation:
298
251
299```purescript
252```purescript
300main = forE 1 10 \n -> void do
253main = forE 1 10 \n -> void do
@@ -309,22 +262,26 @@ class (Functor f) <= Apply f where
309 (<*>) :: forall a b. f (a -> b) -> f a -> f b
262 (<*>) :: forall a b. f (a -> b) -> f a -> f b
310```
263```
311
264
312−The `Apply` class provides the `(<*>)` which is used to apply a function to an argument under a type constructor.
265+The `Apply` class provides the `(<*>)` which is used to apply a function
266+to an argument under a type constructor.
313
267
314−`Apply` can be used to lift functions of two or more arguments to work on values wrapped with the type constructor `f`.
268+`Apply` can be used to lift functions of two or more arguments to work on
269+values wrapped with the type constructor `f`. It might also be understood
315−It might also be understood in terms of the `lift2` function:
270+in terms of the `lift2` function:
316
271
317```purescript
272```purescript
318lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
273lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
319lift2 f a b = f <$> a <*> b
274lift2 f a b = f <$> a <*> b
320```
275```
321
276
322−`(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts the function application operator `($)` to arguments
277+`(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
278+the function application operator `($)` to arguments wrapped with the
323−wrapped with the type constructor `f`.
279+type constructor `f`.
324
280
325−`Apply` instances should satisfy the following law:
281+Instances must satisfy the following law in addition to the `Functor`
282+laws:
326
283
327−- Associative Composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
284+- Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
328
285
329Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
286Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
330
287
@@ -342,14 +299,19 @@ class (Apply f) <= Applicative f where
342 pure :: forall a. a -> f a
299 pure :: forall a. a -> f a
343```
300```
344
301
345−The `Applicative` type class extends the [`Apply`](#apply) type class with a `pure` function, which can be used to
302+The `Applicative` type class extends the [`Apply`](#apply) type class
303+with a `pure` function, which can be used to create values of type `f a`
346−create values of type `f a` from values of type `a`.
304+from values of type `a`.
347
305
348−Where [`Apply`](#apply) provides the ability to lift functions of two or more arguments to functions whose arguments are wrapped using `f`,
306+Where [`Apply`](#apply) provides the ability to lift functions of two or
307+more arguments to functions whose arguments are wrapped using `f`, and
308+[`Functor`](#functor) provides the ability to lift functions of one
349−and [`Functor`](#functor) provides the ability to lift functions of one argument, `pure` can be seen as the function which lifts functions of
309+argument, `pure` can be seen as the function which lifts functions of
350−_zero_ arguments. That is, `Applicative` functors support a lifting operation for any number of function arguments.
310+_zero_ arguments. That is, `Applicative` functors support a lifting
311+operation for any number of function arguments.
351
312
352−`Applicative` instances should satisfy the following laws:
313+Instances must satisfy the following laws in addition to the `Apply`
314+laws:
353
315
354- Identity: `(pure id) <*> v = v`
316- Identity: `(pure id) <*> v = v`
355- Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
317- Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
@@ -377,10 +339,13 @@ return :: forall m a. (Applicative m) => a -> m a
377liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
339liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
378```
340```
379
341
380−`liftA1` provides a default implementation of `(<$>)` for any [`Applicative`](#applicative) functor,
342+`liftA1` provides a default implementation of `(<$>)` for any
343+[`Applicative`](#applicative) functor, without using `(<$>)` as provided
381−without using `(<$>)` as provided by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass relationship.
344+by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
345+relationship.
382
346
383−`liftA1` can therefore be used to write [`Functor`](#functor) instances as follows:
347+`liftA1` can therefore be used to write [`Functor`](#functor) instances
348+as follows:
384
349
385```purescript
350```purescript
386instance functorF :: Functor F where
351instance functorF :: Functor F where
@@ -394,8 +359,9 @@ class (Apply m) <= Bind m where
394 (>>=) :: forall a b. m a -> (a -> m b) -> m b
359 (>>=) :: forall a b. m a -> (a -> m b) -> m b
395```
360```
396
361
397−The `Bind` type class extends the [`Apply`](#apply) type class with a "bind" operation `(>>=)` which composes computations
362+The `Bind` type class extends the [`Apply`](#apply) type class with a
363+"bind" operation `(>>=)` which composes computations in sequence, using
398−in sequence, using the return value of one computation to determine the next computation.
364+the return value of one computation to determine the next computation.
399
365
400The `>>=` operator can also be expressed using `do` notation, as follows:
366The `>>=` operator can also be expressed using `do` notation, as follows:
401
367
@@ -406,15 +372,13 @@ x >>= f = do y <- x
406
372
407where the function argument of `f` is given the name `y`.
373where the function argument of `f` is given the name `y`.
408
374
409−`Bind` instances should satisfy the following law:
375+Instances must satisfy the following law in addition to the `Apply`
376+laws:
410
377
411- Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
378- Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
412
379
413−Or, expressed using `do` notation:
414−
415−- Associativity: `do { z <- do { y <- x ; f y } ; g z } = do { k <- x ; do { y <- f k ; g y } }`
416−
417−Associativity tells us that we can regroup operations which use do-notation, so that we can unambiguously write, for example:
380+Associativity tells us that we can regroup operations which use `do`
381+notation so that we can unambiguously write, for example:
418
382
419```purescript
383```purescript
420do x <- m1
384do x <- m1
@@ -435,19 +399,17 @@ instance bindArr :: Bind (Prim.Function r)
435class (Applicative m, Bind m) <= Monad m where
399class (Applicative m, Bind m) <= Monad m where
436```
400```
437
401
438−The `Monad` type class combines the operations of the `Bind` and `Applicative` type classes. Therefore, `Monad` instances
402+The `Monad` type class combines the operations of the `Bind` and
403+`Applicative` type classes. Therefore, `Monad` instances represent type
439−represent type constructors which support sequential composition, and also lifting of functions of arbitrary arity.
404+constructors which support sequential composition, and also lifting of
405+functions of arbitrary arity.
440
406
441−`Monad` instances should satisfy the following laws:
407+Instances must satisfy the following laws in addition to the
408+`Applicative` and `Bind` laws:
442
409
443- Left Identity: `pure x >>= f = f x`
410- Left Identity: `pure x >>= f = f x`
444- Right Identity: `x >>= pure = x`
411- Right Identity: `x >>= pure = x`
445
412
446−Or, expressed using `do` notation:
447−
448−- Left Identity: `do { y <- pure x ; f y } = f x`
449−- Right Identity: `do { y <- x ; pure y } = x`
450−
451#### `monadArr`
413#### `monadArr`
452
414
453``` purescript
415``` purescript
@@ -461,10 +423,12 @@ instance monadArr :: Monad (Prim.Function r)
461liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
423liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
462```
424```
463
425
464−`liftM1` provides a default implementation of `(<$>)` for any [`Monad`](#monad),
426+`liftM1` provides a default implementation of `(<$>)` for any
427+[`Monad`](#monad), without using `(<$>)` as provided by the
465−without using `(<$>)` as provided by the [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
428+[`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
466
429
467−`liftM1` can therefore be used to write [`Functor`](#functor) instances as follows:
430+`liftM1` can therefore be used to write [`Functor`](#functor) instances
431+as follows:
468
432
469```purescript
433```purescript
470instance functorF :: Functor F where
434instance functorF :: Functor F where
@@ -477,10 +441,12 @@ instance functorF :: Functor F where
477ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
441ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
478```
442```
479
443
480−`ap` provides a default implementation of `(<*>)` for any [`Monad`](#monad),
444+`ap` provides a default implementation of `(<*>)` for any
445+[`Monad`](#monad), without using `(<*>)` as provided by the
481−without using `(<*>)` as provided by the [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
446+[`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
482
447
483−`ap` can therefore be used to write [`Apply`](#apply) instances as follows:
448+`ap` can therefore be used to write [`Apply`](#apply) instances as
449+follows:
484
450
485```purescript
451```purescript
486instance applyF :: Apply F where
452instance applyF :: Apply F where
@@ -496,11 +462,12 @@ class Semigroup a where
496
462
497The `Semigroup` type class identifies an associative operation on a type.
463The `Semigroup` type class identifies an associative operation on a type.
498
464
499−`Semigroup` instances are required to satisfy the following law:
465+Instances are required to satisfy the following law:
500
466
501- Associativity: `(x <> y) <> z = x <> (y <> z)`
467- Associativity: `(x <> y) <> z = x <> (y <> z)`
502
468
503−For example, the `String` type is an instance of `Semigroup`, where `(<>)` is defined to be string concatenation.
469+One example of a `Semigroup` is `String`, with `(<>)` defined as string
470+concatenation.
504
471
505#### `(++)`
472#### `(++)`
506
473
@@ -548,12 +515,22 @@ class Semiring a where
548 one :: a
515 one :: a
549```
516```
550
517
551−Addition and multiplication, satisfying the following laws:
518+The `Semiring` class is for types that support an addition and
519+multiplication operation.
520+
521+Instances must satisfy the following laws:
552
522
553−- `a` is a commutative monoid under addition
523+- Commutative monoid under addition:
554−- `a` is a monoid under multiplication
524+ - Associativity: `(a + b) + c = a + (b + c)`
525+ - Identity: `zero + a = a + zero = a`
526+ - Commutative: `a + b = b + a`
527+- Monoid under multiplication:
528+ - Associativity: `(a * b) * c = a * (b * c)`
529+ - Identity: `one * a = a * one = a`
555−- multiplication distributes over addition
530+- Multiplication distributes over addition:
556−- multiplication by `zero` annihilates `a`
531+ - Left distributivity: `a * (b + c) = (a * b) + (a * c)`
532+ - Right distributivity: `(a + b) * c = (a * c) + (b * c)`
533+- Annihiliation: `zero * a = a * zero = zero`
557
534
558#### `semiringNumber`
535#### `semiringNumber`
559
536
@@ -569,63 +546,69 @@ instance semiringUnit :: Semiring Unit
569```
546```
570
547
571
548
572−#### `ModuloSemiring`
549+#### `Ring`
573
550
574``` purescript
551``` purescript
575−class (Semiring a) <= ModuloSemiring a where
552+class (Semiring a) <= Ring a where
576− (/) :: a -> a -> a
577− mod :: a -> a -> a
553+ (-) :: a -> a -> a
578```
554```
579
555
580−Addition, multiplication, modulo operation and division, satisfying:
556+The `Ring` class is for types that support addition, multiplication,
557+and subtraction operations.
581
558
582−- ```a / b * b + (a `mod` b) = a```
559+Instances must satisfy the following law in addition to the `Semiring`
560+laws:
583
561
584−#### `moduloSemiringNumber`
562+- Additive inverse: `a + (−a) = (−a) + a = zero`
563+
564+#### `ringNumber`
585
565
586``` purescript
566``` purescript
587−instance moduloSemiringNumber :: ModuloSemiring Number
567+instance ringNumber :: Ring Number
588```
568```
589
569
590
570
591−#### `moduloSemiringUnit`
571+#### `ringUnit`
592
572
593``` purescript
573``` purescript
594−instance moduloSemiringUnit :: ModuloSemiring Unit
574+instance ringUnit :: Ring Unit
595```
575```
596
576
597
577
598−#### `Ring`
578+#### `negate`
599
579
600``` purescript
580``` purescript
601−class (Semiring a) <= Ring a where
581+negate :: forall a. (Ring a) => a -> a
602− (-) :: a -> a -> a
603```
582```
604
583
605−Addition, multiplication, and subtraction.
606−
607−Has the same laws as `Semiring` but additionally satisfying:
608
584
609−- `a` is an abelian group under addition
585+#### `ModuloSemiring`
610−
611−#### `ringNumber`
612
586
613``` purescript
587``` purescript
614−instance ringNumber :: Ring Number
588+class (Semiring a) <= ModuloSemiring a where
589+ (/) :: a -> a -> a
590+ mod :: a -> a -> a
615```
591```
616
592
593+The `ModuloSemiring` class is for types that support addition,
594+multiplication, division, and modulo (division remainder) operations.
617
595
618−#### `ringUnit`
596+Instances must satisfy the following law in addition to the `Semiring`
597+laws:
598+
599+- Remainder: `a / b * b + (a `mod` b) = a`
600+
601+#### `moduloSemiringNumber`
619
602
620``` purescript
603``` purescript
621−instance ringUnit :: Ring Unit
604+instance moduloSemiringNumber :: ModuloSemiring Number
622```
605```
623
606
624
607
625−#### `negate`
608+#### `moduloSemiringUnit`
626
609
627``` purescript
610``` purescript
628−negate :: forall a. (Ring a) => a -> a
611+instance moduloSemiringUnit :: ModuloSemiring Unit
629```
612```
630
613
631
614
@@ -635,9 +618,15 @@ negate :: forall a. (Ring a) => a -> a
635class (Ring a, ModuloSemiring a) <= DivisionRing a where
618class (Ring a, ModuloSemiring a) <= DivisionRing a where
636```
619```
637
620
638−Ring where every nonzero element has a multiplicative inverse so that:
621+A `Ring` where every nonzero element has a multiplicative inverse.
639
622
640−- ```a `mod` b = zero```
623+Instances must satisfy the following law in addition to the `Ring` and
624+`ModuloSemiring` laws:
625+
626+- Multiplicative inverse: `(one / x) * x = one`
627+
628+As a consequence of this ```a `mod` b = zero``` as no divide operation
629+will have a remainder.
641
630
642#### `divisionRingNumber`
631#### `divisionRingNumber`
643
632
@@ -659,7 +648,12 @@ instance divisionRingUnit :: DivisionRing Unit
659class (DivisionRing a) <= Num a where
648class (DivisionRing a) <= Num a where
660```
649```
661
650
662−A commutative field
651+The `Num` class is for types that are commutative fields.
652+
653+Instances must satisfy the following law in addition to the
654+`DivisionRing` laws:
655+
656+- Commutative multiplication: `a * b = b * a`
663
657
664#### `numNumber`
658#### `numNumber`
665
659
@@ -738,7 +732,8 @@ data Ordering
738 | EQ
732 | EQ
739```
733```
740
734
741−The `Ordering` data type represents the three possible outcomes of comparing two values:
735+The `Ordering` data type represents the three possible outcomes of
736+comparing two values:
742
737
743`LT` - The first value is _less than_ the second.
738`LT` - The first value is _less than_ the second.
744`GT` - The first value is _greater than_ the second.
739`GT` - The first value is _greater than_ the second.
@@ -1004,25 +999,59 @@ instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
1004```
999```
1005
1000
1006
1001
1007−#### `Bits`
1002+#### `Show`
1003+
1004+``` purescript
1005+class Show a where
1006+ show :: a -> String
1007+```
1008+
1009+The `Show` type class represents those types which can be converted into
1010+a human-readable `String` representation.
1011+
1012+While not required, it is recommended that for any expression `x`, the
1013+string `show x` be executable PureScript code which evaluates to the same
1014+value as the expression `x`.
1015+
1016+#### `showBoolean`
1017+
1018+``` purescript
1019+instance showBoolean :: Show Boolean
1020+```
1021+
1022+
1023+#### `showNumber`
1024+
1025+``` purescript
1026+instance showNumber :: Show Number
1027+```
1028+
1029+
1030+#### `showString`
1031+
1032+``` purescript
1033+instance showString :: Show String
1034+```
1035+
1036+
1037+#### `showUnit`
1008
1038
1009``` purescript
1039``` purescript
1010−class Bits b where
1040+instance showUnit :: Show Unit
1011− (.&.) :: b -> b -> b
1012− (.|.) :: b -> b -> b
1013− (.^.) :: b -> b -> b
1014− shl :: b -> Number -> b
1015− shr :: b -> Number -> b
1016− zshr :: b -> Number -> b
1017− complement :: b -> b
1018```
1041```
1019
1042
1020−The `Bits` type class identifies types which support bitwise operations.
1021
1043
1022−#### `bitsNumber`
1044+#### `showArray`
1023
1045
1024``` purescript
1046``` purescript
1025−instance bitsNumber :: Bits Number
1047+instance showArray :: (Show a) => Show [a]
1048+```
1049+
1050+
1051+#### `showOrdering`
1052+
1053+``` purescript
1054+instance showOrdering :: Show Ordering
1026```
1055```
1027
1056
1028
1057
prelude/images/Prelude.png

Binary file not shown.

prelude/prelude.purs+263−225
@@ -1,14 +1,13 @@
1module Prelude
1module Prelude
2 ( Unit(..), unit
2 ( Unit(..), unit
3− , otherwise
3+ , ($), (#)
4 , flip
4 , flip
5 , const
5 , const
6 , asTypeOf
6 , asTypeOf
7+ , otherwise
8+ , (:), cons
7 , Semigroupoid, (<<<), (>>>)
9 , Semigroupoid, (<<<), (>>>)
8 , Category, id
10 , Category, id
9− , ($), (#)
10− , (:), cons
11− , Show, show
12 , Functor, (<$>), (<#>), void
11 , Functor, (<$>), (<#>), void
13 , Apply, (<*>)
12 , Apply, (<*>)
14 , Applicative, pure, liftA1
13 , Applicative, pure, liftA1
@@ -18,7 +17,6 @@ module Prelude
18 , Semiring, (+), zero, (*), one
17 , Semiring, (+), zero, (*), one
19 , ModuloSemiring, (/), mod
18 , ModuloSemiring, (/), mod
20 , Ring, (-), negate
19 , Ring, (-), negate
21− , (%)
22 , Num
20 , Num
23 , DivisionRing
21 , DivisionRing
24 , Eq, (==), (/=)
22 , Eq, (==), (/=)
@@ -29,11 +27,14 @@ module Prelude
29 , ComplementedLattice, not
27 , ComplementedLattice, not
30 , DistributiveLattice
28 , DistributiveLattice
31 , BooleanAlgebra
29 , BooleanAlgebra
30+ , Show, show
32 ) where
31 ) where
33
32
34− -- | The `Unit` type has a single inhabitant, called `unit`. It represents values with no computational content.
33+ -- | The `Unit` type has a single inhabitant, called `unit`. It represents
34+ -- | values with no computational content.
35 -- |
35 -- |
36− -- | `Unit` is often used, wrapped in a monadic type constructor, as the return type of a computation where only
36+ -- | `Unit` is often used, wrapped in a monadic type constructor, as the
37+ -- | return type of a computation where only
37 -- | the _effects_ are important.
38 -- | the _effects_ are important.
38 newtype Unit = Unit {}
39 newtype Unit = Unit {}
39
40
@@ -41,78 +42,6 @@ module Prelude
41 unit :: Unit
42 unit :: Unit
42 unit = Unit {}
43 unit = Unit {}
43
44
44− -- | An alias for `true`, which can be useful in guard clauses:
45− -- |
46− -- | ```purescript
47− -- | max x y | x >= y = x
48− -- | | otherwise = y
49− -- | ```
50− otherwise :: Boolean
51− otherwise = true
52−
53− -- | Flips the order of the arguments to a function of two arguments.
54− -- |
55− -- | ```purescript
56− -- | flip const 1 2 = const 2 1 = 2
57− -- | ```
58− flip :: forall a b c. (a -> b -> c) -> b -> a -> c
59− flip f b a = f a b
60−
61− -- | Returns its first argument and ignores its second.
62− -- |
63− -- | ```purescript
64− -- | const 1 "hello" = 1
65− -- | ```
66− const :: forall a b. a -> b -> a
67− const a _ = a
68−
69− -- | This function returns its first argument, and can be used to assert type equalities.
70− -- | This can be useful when types are otherwise ambiguous.
71− -- |
72− -- | ```purescript
73− -- | main = print $ [] `asTypeOf` [0]
74− -- | ```
75− -- |
76− -- | If instead, we had written `main = print []`, the type of the argument `[]` would have
77− -- | been ambiguous, resulting in a compile-time error.
78− asTypeOf :: forall a. a -> a -> a
79− asTypeOf x _ = x
80−
81− infixr 9 >>>
82− infixr 9 <<<
83−
84− -- | A `Semigroupoid` is similar to a [`Category`](#category) but does not require an identity
85− -- | element `id`, just composable morphisms.
86− -- |
87− -- | `Semigroupoid`s should obey the following rule:
88− -- |
89− -- | - Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
90− -- |
91− -- | One example of a `Semigroupoid` is the function type constructor `(->)`, with `(<<<)` defined
92− -- | as function composition.
93− class Semigroupoid a where
94− (<<<) :: forall b c d. a c d -> a b c -> a b d
95−
96− instance semigroupoidArr :: Semigroupoid (->) where
97− (<<<) f g x = f (g x)
98−
99− -- | Forwards composition, or `(<<<)` with its arguments reversed.
100− (>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
101− (>>>) f g = g <<< f
102−
103− -- | `Category`s consist of objects and composable morphisms between them, and as such are
104− -- | [`Semigroupoids`](#semigroupoid), but unlike `semigroupoids` must have an identity element.
105− -- |
106− -- | `Category`s should obey the following rules.
107− -- |
108− -- | - Left Identity: `id <<< p = p`
109− -- | - Right Identity: `p <<< id = p`
110− class (Semigroupoid a) <= Category a where
111− id :: forall t. a t t
112−
113− instance categoryArr :: Category (->) where
114− id x = x
115−
116 infixr 0 $
45 infixr 0 $
117 infixl 0 #
46 infixl 0 #
118
47
@@ -122,14 +51,14 @@ module Prelude
122 -- | length $ groupBy productCategory $ filter isInStock $ products
51 -- | length $ groupBy productCategory $ filter isInStock $ products
123 -- | ```
52 -- | ```
124 -- |
53 -- |
125− -- | is equivalent to
54+ -- | is equivalent to:
126 -- |
55 -- |
127 -- | ```purescript
56 -- | ```purescript
128 -- | length (groupBy productCategory (filter isInStock products))
57 -- | length (groupBy productCategory (filter isInStock products))
129 -- | ```
58 -- | ```
130 -- |
59 -- |
131− -- | `($)` is different from [`(#)`](#-2) because it is right-infix instead of left, so
60+ -- | `($)` is different from [`(#)`](#-2) because it is right-infix instead of
132− -- | `a $ b $ c $ d x` = `a $ (b $ (c $ (d $ x)))` = `a (b (c (d x)))`
61+ -- | left: `a $ b $ c $ d x = a $ (b $ (c $ (d $ x))) = a (b (c (d x)))`
133 ($) :: forall a b. (a -> b) -> a -> b
62 ($) :: forall a b. (a -> b) -> a -> b
134 ($) f x = f x
63 ($) f x = f x
135
64
@@ -139,24 +68,53 @@ module Prelude
139 -- | products # filter isInStock # groupBy productCategory # length
68 -- | products # filter isInStock # groupBy productCategory # length
140 -- | ```
69 -- | ```
141 -- |
70 -- |
142− -- | is equivalent to
71+ -- | is equivalent to:
143 -- |
72 -- |
144 -- | ```purescript
73 -- | ```purescript
145 -- | length (groupBy productCategory (filter isInStock products))
74 -- | length (groupBy productCategory (filter isInStock products))
146 -- | ```
75 -- | ```
147 -- |
76 -- |
148− -- | `(#)` is different from [`($)`](#-1) because it is left-infix instead of right, so
77+ -- | `(#)` is different from [`($)`](#-1) because it is left-infix instead of
149− -- | `x # a # b # c # d` = `(((x # a) # b) # c) # d` = `d (c (b (a x)))`
78+ -- | right: `x # a # b # c # d = (((x # a) # b) # c) # d = d (c (b (a x)))`
150 (#) :: forall a b. a -> (a -> b) -> b
79 (#) :: forall a b. a -> (a -> b) -> b
151 (#) x f = f x
80 (#) x f = f x
152
81
153− infixr 6 :
82+ -- | Flips the order of the arguments to a function of two arguments.
83+ -- |
84+ -- | ```purescript
85+ -- | flip const 1 2 = const 2 1 = 2
86+ -- | ```
87+ flip :: forall a b c. (a -> b -> c) -> b -> a -> c
88+ flip f b a = f a b
154
89
155− -- | An infix alias for `cons`.
90+ -- | Returns its first argument and ignores its second.
156 -- |
91 -- |
157− -- | Note, the running time of this function is `O(n)`.
92+ -- | ```purescript
93+ -- | const 1 "hello" = 1
94+ -- | ```
95+ const :: forall a b. a -> b -> a
96+ const a _ = a
97+
98+ -- | This function returns its first argument, and can be used to assert type
99+ -- | equalities. This can be useful when types are otherwise ambiguous.
100+ -- |
101+ -- | ```purescript
102+ -- | main = print $ [] `asTypeOf` [0]
103+ -- | ```
104+ -- |
105+ -- | If instead, we had written `main = print []`, the type of the argument
106+ -- | `[]` would have been ambiguous, resulting in a compile-time error.
158− (:) :: forall a. a -> [a] -> [a]
107+ asTypeOf :: forall a. a -> a -> a
159− (:) = cons
108+ asTypeOf x _ = x
109+
110+ -- | An alias for `true`, which can be useful in guard clauses:
111+ -- |
112+ -- | ```purescript
113+ -- | max x y | x >= y = x
114+ -- | | otherwise = y
115+ -- | ```
116+ otherwise :: Boolean
117+ otherwise = true
160
118
161 -- | Attaches an element to the front of an array, creating a new array.
119 -- | Attaches an element to the front of an array, creating a new array.
162 -- |
120 -- |
@@ -174,70 +132,61 @@ module Prelude
174 }
132 }
175 """ :: forall a. a -> [a] -> [a]
133 """ :: forall a. a -> [a] -> [a]
176
134
177− -- | The `Show` type class represents those types which can be converted into a human-readable `String` representation.
135+ infixr 6 :
178− -- |
179− -- | While not required, it is recommended that for any expression `x`, the string `show x` be executable PureScript code
180− -- | which evaluates to the same value as the expression `x`.
181− class Show a where
182− show :: a -> String
183−
184− instance showBoolean :: Show Boolean where
185− show true = "true"
186− show false = "false"
187−
188− instance showNumber :: Show Number where
189− show = showNumberImpl
190
136
191− instance showString :: Show String where
137+ -- | An infix alias for `cons`.
192− show = showStringImpl
138+ -- |
139+ -- | Note, the running time of this function is `O(n)`.
140+ (:) :: forall a. a -> [a] -> [a]
141+ (:) = cons
193
142
194− instance showUnit :: Show Unit where
143+ infixr 9 >>>
195− show _ = "unit"
144+ infixr 9 <<<
196
145
197− instance showArray :: (Show a) => Show [a] where
146+ -- | A `Semigroupoid` is similar to a [`Category`](#category) but does not
198− show = showArrayImpl show
147+ -- | require an identity element `id`, just composable morphisms.
148+ -- |
149+ -- | `Semigroupoid`s must satisfy the following law:
150+ -- |
151+ -- | - Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
152+ -- |
153+ -- | One example of a `Semigroupoid` is the function type constructor `(->)`,
154+ -- | with `(<<<)` defined as function composition.
155+ class Semigroupoid a where
156+ (<<<) :: forall b c d. a c d -> a b c -> a b d
199
157
200− instance showOrdering :: Show Ordering where
158+ instance semigroupoidArr :: Semigroupoid (->) where
201− show LT = "LT"
159+ (<<<) f g x = f (g x)
202− show GT = "GT"
203− show EQ = "EQ"
204
160
205− foreign import showNumberImpl
161+ -- | Forwards composition, or `(<<<)` with its arguments reversed.
206− """
162+ (>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
207− function showNumberImpl(n) {
163+ (>>>) f g = g <<< f
208− return n.toString();
209− }
210− """ :: Number -> String
211
164
212− foreign import showStringImpl
165+ -- | `Category`s consist of objects and composable morphisms between them, and
213− """
166+ -- | as such are [`Semigroupoids`](#semigroupoid), but unlike `semigroupoids`
214− function showStringImpl(s) {
167+ -- | must have an identity element.
215− return JSON.stringify(s);
168+ -- |
216− }
169+ -- | Instances must satisfy the following law in addition to the
217− """ :: String -> String
170+ -- | `Semigroupoid` law:
171+ -- |
172+ -- | - Identity: `id <<< p = p <<< id = p`
173+ class (Semigroupoid a) <= Category a where
174+ id :: forall t. a t t
218
175
219− foreign import showArrayImpl
176+ instance categoryArr :: Category (->) where
220− """
177+ id x = x
221− function showArrayImpl(f) {
222− return function(xs) {
223− var ss = [];
224− for (var i = 0, l = xs.length; i < l; i++) {
225− ss[i] = f(xs[i]);
226− }
227− return '[' + ss.join(',') + ']';
228− };
229− }
230− """ :: forall a. (a -> String) -> [a] -> String
231
178
232 infixl 4 <$>
179 infixl 4 <$>
233 infixl 1 <#>
180 infixl 1 <#>
234
181
235− -- | A `Functor` is a type constructor which supports a mapping operation `(<$>)`.
182+ -- | A `Functor` is a type constructor which supports a mapping operation
183+ -- | `(<$>)`.
236 -- |
184 -- |
237− -- | `(<$>)` can be used to turn functions `a -> b` into functions `f a -> f b` whose argument and return
185+ -- | `(<$>)` can be used to turn functions `a -> b` into functions
186+ -- | `f a -> f b` whose argument and return types use the type constructor `f`
238− -- | types use the type constructor `f` to represent some computational context.
187+ -- | to represent some computational context.
239 -- |
188 -- |
240− -- | `Functor` instances should satisfy the following laws:
189+ -- | Instances must satisfy the following laws:
241 -- |
190 -- |
242 -- | - Identity: `(<$>) id = id`
191 -- | - Identity: `(<$>) id = id`
243 -- | - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
192 -- | - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
@@ -255,10 +204,12 @@ module Prelude
255 (<#>) :: forall f a b. (Functor f) => f a -> (a -> b) -> f b
204 (<#>) :: forall f a b. (Functor f) => f a -> (a -> b) -> f b
256 (<#>) fa f = f <$> fa
205 (<#>) fa f = f <$> fa
257
206
258− -- | The `void` function is used to ignore the type wrapped by a [`Functor`](#functor), replacing it with `Unit` and
207+ -- | The `void` function is used to ignore the type wrapped by a
208+ -- | [`Functor`](#functor), replacing it with `Unit` and keeping only the type
259− -- | keeping only the type information provided by the type constructor itself.
209+ -- | information provided by the type constructor itself.
260 -- |
210 -- |
261− -- | `void` is often useful when using `do` notation to change the return type of a monadic computation:
211+ -- | `void` is often useful when using `do` notation to change the return type
212+ -- | of a monadic computation:
262 -- |
213 -- |
263 -- | ```purescript
214 -- | ```purescript
264 -- | main = forE 1 10 \n -> void do
215 -- | main = forE 1 10 \n -> void do
@@ -270,22 +221,26 @@ module Prelude
270
221
271 infixl 4 <*>
222 infixl 4 <*>
272
223
273− -- | The `Apply` class provides the `(<*>)` which is used to apply a function to an argument under a type constructor.
224+ -- | The `Apply` class provides the `(<*>)` which is used to apply a function
225+ -- | to an argument under a type constructor.
274 -- |
226 -- |
275− -- | `Apply` can be used to lift functions of two or more arguments to work on values wrapped with the type constructor `f`.
227+ -- | `Apply` can be used to lift functions of two or more arguments to work on
228+ -- | values wrapped with the type constructor `f`. It might also be understood
276− -- | It might also be understood in terms of the `lift2` function:
229+ -- | in terms of the `lift2` function:
277 -- |
230 -- |
278 -- | ```purescript
231 -- | ```purescript
279 -- | lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
232 -- | lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
280 -- | lift2 f a b = f <$> a <*> b
233 -- | lift2 f a b = f <$> a <*> b
281 -- | ```
234 -- | ```
282 -- |
235 -- |
283− -- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts the function application operator `($)` to arguments
236+ -- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
237+ -- | the function application operator `($)` to arguments wrapped with the
284− -- | wrapped with the type constructor `f`.
238+ -- | type constructor `f`.
285 -- |
239 -- |
286− -- | `Apply` instances should satisfy the following law:
240+ -- | Instances must satisfy the following law in addition to the `Functor`
241+ -- | laws:
287 -- |
242 -- |
288− -- | - Associative Composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
243+ -- | - Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
289 -- |
244 -- |
290 -- | Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
245 -- | Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
291 class (Functor f) <= Apply f where
246 class (Functor f) <= Apply f where
@@ -294,14 +249,19 @@ module Prelude
294 instance applyArr :: Apply ((->) r) where
249 instance applyArr :: Apply ((->) r) where
295 (<*>) f g x = f x (g x)
250 (<*>) f g x = f x (g x)
296
251
297− -- | The `Applicative` type class extends the [`Apply`](#apply) type class with a `pure` function, which can be used to
252+ -- | The `Applicative` type class extends the [`Apply`](#apply) type class
253+ -- | with a `pure` function, which can be used to create values of type `f a`
298− -- | create values of type `f a` from values of type `a`.
254+ -- | from values of type `a`.
299 -- |
255 -- |
300− -- | Where [`Apply`](#apply) provides the ability to lift functions of two or more arguments to functions whose arguments are wrapped using `f`,
256+ -- | Where [`Apply`](#apply) provides the ability to lift functions of two or
257+ -- | more arguments to functions whose arguments are wrapped using `f`, and
258+ -- | [`Functor`](#functor) provides the ability to lift functions of one
301− -- | and [`Functor`](#functor) provides the ability to lift functions of one argument, `pure` can be seen as the function which lifts functions of
259+ -- | argument, `pure` can be seen as the function which lifts functions of
302− -- | _zero_ arguments. That is, `Applicative` functors support a lifting operation for any number of function arguments.
260+ -- | _zero_ arguments. That is, `Applicative` functors support a lifting
261+ -- | operation for any number of function arguments.
303 -- |
262 -- |
304− -- | `Applicative` instances should satisfy the following laws:
263+ -- | Instances must satisfy the following laws in addition to the `Apply`
264+ -- | laws:
305 -- |
265 -- |
306 -- | - Identity: `(pure id) <*> v = v`
266 -- | - Identity: `(pure id) <*> v = v`
307 -- | - Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
267 -- | - Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
@@ -317,10 +277,13 @@ module Prelude
317 return :: forall m a. (Applicative m) => a -> m a
277 return :: forall m a. (Applicative m) => a -> m a
318 return = pure
278 return = pure
319
279
320− -- | `liftA1` provides a default implementation of `(<$>)` for any [`Applicative`](#applicative) functor,
280+ -- | `liftA1` provides a default implementation of `(<$>)` for any
281+ -- | [`Applicative`](#applicative) functor, without using `(<$>)` as provided
321− -- | without using `(<$>)` as provided by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass relationship.
282+ -- | by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
283+ -- | relationship.
322 -- |
284 -- |
323− -- | `liftA1` can therefore be used to write [`Functor`](#functor) instances as follows:
285+ -- | `liftA1` can therefore be used to write [`Functor`](#functor) instances
286+ -- | as follows:
324 -- |
287 -- |
325 -- | ```purescript
288 -- | ```purescript
326 -- | instance functorF :: Functor F where
289 -- | instance functorF :: Functor F where
@@ -331,8 +294,9 @@ module Prelude
331
294
332 infixl 1 >>=
295 infixl 1 >>=
333
296
334− -- | The `Bind` type class extends the [`Apply`](#apply) type class with a "bind" operation `(>>=)` which composes computations
297+ -- | The `Bind` type class extends the [`Apply`](#apply) type class with a
298+ -- | "bind" operation `(>>=)` which composes computations in sequence, using
335− -- | in sequence, using the return value of one computation to determine the next computation.
299+ -- | the return value of one computation to determine the next computation.
336 -- |
300 -- |
337 -- | The `>>=` operator can also be expressed using `do` notation, as follows:
301 -- | The `>>=` operator can also be expressed using `do` notation, as follows:
338 -- |
302 -- |
@@ -343,15 +307,13 @@ module Prelude
343 -- |
307 -- |
344 -- | where the function argument of `f` is given the name `y`.
308 -- | where the function argument of `f` is given the name `y`.
345 -- |
309 -- |
346− -- | `Bind` instances should satisfy the following law:
310+ -- | Instances must satisfy the following law in addition to the `Apply`
311+ -- | laws:
347 -- |
312 -- |
348 -- | - Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
313 -- | - Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
349 -- |
314 -- |
350− -- | Or, expressed using `do` notation:
351− -- |
352− -- | - Associativity: `do { z <- do { y <- x ; f y } ; g z } = do { k <- x ; do { y <- f k ; g y } }`
353− -- |
354− -- | Associativity tells us that we can regroup operations which use do-notation, so that we can unambiguously write, for example:
315+ -- | Associativity tells us that we can regroup operations which use `do`
316+ -- | notation so that we can unambiguously write, for example:
355 -- |
317 -- |
356 -- | ```purescript
318 -- | ```purescript
357 -- | do x <- m1
319 -- | do x <- m1
@@ -364,26 +326,26 @@ module Prelude
364 instance bindArr :: Bind ((->) r) where
326 instance bindArr :: Bind ((->) r) where
365 (>>=) m f x = f (m x) x
327 (>>=) m f x = f (m x) x
366
328
367− -- | The `Monad` type class combines the operations of the `Bind` and `Applicative` type classes. Therefore, `Monad` instances
329+ -- | The `Monad` type class combines the operations of the `Bind` and
330+ -- | `Applicative` type classes. Therefore, `Monad` instances represent type
368− -- | represent type constructors which support sequential composition, and also lifting of functions of arbitrary arity.
331+ -- | constructors which support sequential composition, and also lifting of
332+ -- | functions of arbitrary arity.
369 -- |
333 -- |
370− -- | `Monad` instances should satisfy the following laws:
334+ -- | Instances must satisfy the following laws in addition to the
335+ -- | `Applicative` and `Bind` laws:
371 -- |
336 -- |
372 -- | - Left Identity: `pure x >>= f = f x`
337 -- | - Left Identity: `pure x >>= f = f x`
373 -- | - Right Identity: `x >>= pure = x`
338 -- | - Right Identity: `x >>= pure = x`
374− -- |
375− -- | Or, expressed using `do` notation:
376− -- |
377− -- | - Left Identity: `do { y <- pure x ; f y } = f x`
378− -- | - Right Identity: `do { y <- x ; pure y } = x`
379 class (Applicative m, Bind m) <= Monad m
339 class (Applicative m, Bind m) <= Monad m
380
340
381 instance monadArr :: Monad ((->) r)
341 instance monadArr :: Monad ((->) r)
382
342
383− -- | `liftM1` provides a default implementation of `(<$>)` for any [`Monad`](#monad),
343+ -- | `liftM1` provides a default implementation of `(<$>)` for any
344+ -- | [`Monad`](#monad), without using `(<$>)` as provided by the
384− -- | without using `(<$>)` as provided by the [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
345+ -- | [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
385 -- |
346 -- |
386− -- | `liftM1` can therefore be used to write [`Functor`](#functor) instances as follows:
347+ -- | `liftM1` can therefore be used to write [`Functor`](#functor) instances
348+ -- | as follows:
387 -- |
349 -- |
388 -- | ```purescript
350 -- | ```purescript
389 -- | instance functorF :: Functor F where
351 -- | instance functorF :: Functor F where
@@ -394,10 +356,12 @@ module Prelude
394 a' <- a
356 a' <- a
395 return (f a')
357 return (f a')
396
358
397− -- | `ap` provides a default implementation of `(<*>)` for any [`Monad`](#monad),
359+ -- | `ap` provides a default implementation of `(<*>)` for any
360+ -- | [`Monad`](#monad), without using `(<*>)` as provided by the
398− -- | without using `(<*>)` as provided by the [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
361+ -- | [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
399 -- |
362 -- |
400− -- | `ap` can therefore be used to write [`Apply`](#apply) instances as follows:
363+ -- | `ap` can therefore be used to write [`Apply`](#apply) instances as
364+ -- | follows:
401 -- |
365 -- |
402 -- | ```purescript
366 -- | ```purescript
403 -- | instance applyF :: Apply F where
367 -- | instance applyF :: Apply F where
@@ -414,11 +378,12 @@ module Prelude
414
378
415 -- | The `Semigroup` type class identifies an associative operation on a type.
379 -- | The `Semigroup` type class identifies an associative operation on a type.
416 -- |
380 -- |
417− -- | `Semigroup` instances are required to satisfy the following law:
381+ -- | Instances are required to satisfy the following law:
418 -- |
382 -- |
419 -- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
383 -- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
420 -- |
384 -- |
421− -- | For example, the `String` type is an instance of `Semigroup`, where `(<>)` is defined to be string concatenation.
385+ -- | One example of a `Semigroup` is `String`, with `(<>)` defined as string
386+ -- | concatenation.
422 class Semigroup a where
387 class Semigroup a where
423 (<>) :: a -> a -> a
388 (<>) :: a -> a -> a
424
389
@@ -452,12 +417,22 @@ module Prelude
452 infixl 6 +
417 infixl 6 +
453 infixl 7 *
418 infixl 7 *
454
419
455− -- | Addition and multiplication, satisfying the following laws:
420+ -- | The `Semiring` class is for types that support an addition and
421+ -- | multiplication operation.
422+ -- |
423+ -- | Instances must satisfy the following laws:
456− -- |
424+ -- |
457− -- | - `a` is a commutative monoid under addition
425+ -- | - Commutative monoid under addition:
458− -- | - `a` is a monoid under multiplication
426+ -- | - Associativity: `(a + b) + c = a + (b + c)`
427+ -- | - Identity: `zero + a = a + zero = a`
428+ -- | - Commutative: `a + b = b + a`
429+ -- | - Monoid under multiplication:
430+ -- | - Associativity: `(a * b) * c = a * (b * c)`
431+ -- | - Identity: `one * a = a * one = a`
459− -- | - multiplication distributes over addition
432+ -- | - Multiplication distributes over addition:
460− -- | - multiplication by `zero` annihilates `a`
433+ -- | - Left distributivity: `a * (b + c) = (a * b) + (a * c)`
434+ -- | - Right distributivity: `(a + b) * c = (a * c) + (b * c)`
435+ -- | - Annihiliation: `zero * a = a * zero = zero`
461 class Semiring a where
436 class Semiring a where
462 (+) :: a -> a -> a
437 (+) :: a -> a -> a
463 zero :: a
438 zero :: a
@@ -476,11 +451,36 @@ module Prelude
476 (*) _ _ = unit
451 (*) _ _ = unit
477 one = unit
452 one = unit
478
453
454+ infixl 6 -
455+
456+ -- | The `Ring` class is for types that support addition, multiplication,
457+ -- | and subtraction operations.
458+ -- |
459+ -- | Instances must satisfy the following law in addition to the `Semiring`
460+ -- | laws:
461+ -- |
462+ -- | - Additive inverse: `a + (-a) = (-a) + a = zero`
463+ class (Semiring a) <= Ring a where
464+ (-) :: a -> a -> a
465+
466+ instance ringNumber :: Ring Number where
467+ (-) = numSub
468+
469+ instance ringUnit :: Ring Unit where
470+ (-) _ _ = unit
471+
472+ negate :: forall a. (Ring a) => a -> a
473+ negate a = zero - a
474+
479 infixl 7 /
475 infixl 7 /
480
476
477+ -- | The `ModuloSemiring` class is for types that support addition,
481− -- | Addition, multiplication, modulo operation and division, satisfying:
478+ -- | multiplication, division, and modulo (division remainder) operations.
479+ -- |
480+ -- | Instances must satisfy the following law in addition to the `Semiring`
481+ -- | laws:
482 -- |
482 -- |
483− -- | - ```a / b * b + (a `mod` b) = a```
483+ -- | - Remainder: `a / b * b + (a `mod` b) = a`
484 class (Semiring a) <= ModuloSemiring a where
484 class (Semiring a) <= ModuloSemiring a where
485 (/) :: a -> a -> a
485 (/) :: a -> a -> a
486 mod :: a -> a -> a
486 mod :: a -> a -> a
@@ -493,44 +493,33 @@ module Prelude
493 (/) _ _ = unit
493 (/) _ _ = unit
494 mod _ _ = unit
494 mod _ _ = unit
495
495
496− infixl 6 -
496+ -- | A `Ring` where every nonzero element has a multiplicative inverse.
497−
498− -- | Addition, multiplication, and subtraction.
499 -- |
497 -- |
500− -- | Has the same laws as `Semiring` but additionally satisfying:
498+ -- | Instances must satisfy the following law in addition to the `Ring` and
499+ -- | `ModuloSemiring` laws:
501 -- |
500 -- |
502− -- | - `a` is an abelian group under addition
501+ -- | - Multiplicative inverse: `(one / x) * x = one`
503− class (Semiring a) <= Ring a where
504− (-) :: a -> a -> a
505−
506− instance ringNumber :: Ring Number where
507− (-) = numSub
508−
509− instance ringUnit :: Ring Unit where
510− (-) _ _ = unit
511−
512− negate :: forall a. (Ring a) => a -> a
513− negate a = zero - a
514−
515− -- | Ring where every nonzero element has a multiplicative inverse so that:
516 -- |
502 -- |
517− -- | - ```a `mod` b = zero```
503+ -- | As a consequence of this ```a `mod` b = zero``` as no divide operation
504+ -- | will have a remainder.
518 class (Ring a, ModuloSemiring a) <= DivisionRing a
505 class (Ring a, ModuloSemiring a) <= DivisionRing a
519
506
520 instance divisionRingNumber :: DivisionRing Number
507 instance divisionRingNumber :: DivisionRing Number
521
508
522 instance divisionRingUnit :: DivisionRing Unit
509 instance divisionRingUnit :: DivisionRing Unit
523
510
524− -- | A commutative field
511+ -- | The `Num` class is for types that are commutative fields.
512+ -- |
513+ -- | Instances must satisfy the following law in addition to the
514+ -- | `DivisionRing` laws:
515+ -- |
516+ -- | - Commutative multiplication: `a * b = b * a`
525 class (DivisionRing a) <= Num a
517 class (DivisionRing a) <= Num a
526
518
527 instance numNumber :: Num Number
519 instance numNumber :: Num Number
528
520
529 instance numUnit :: Num Unit
521 instance numUnit :: Num Unit
530
522
531− infixl 7 %
532− (%) = numMod
533−
534 foreign import numAdd
523 foreign import numAdd
535 """
524 """
536 function numAdd(n1) {
525 function numAdd(n1) {
@@ -567,15 +556,6 @@ module Prelude
567 }
556 }
568 """ :: Number -> Number -> Number
557 """ :: Number -> Number -> Number
569
558
570− foreign import numMod
571− """
572− function numMod(n1) {
573− return function(n2) {
574− return n1 % n2;
575− };
576− }
577− """ :: Number -> Number -> Number
578−
579 infix 4 ==
559 infix 4 ==
580 infix 4 /=
560 infix 4 /=
581
561
@@ -646,7 +626,8 @@ module Prelude
646 }
626 }
647 """ :: forall a. (a -> a -> Boolean) -> [a] -> [a] -> Boolean
627 """ :: forall a. (a -> a -> Boolean) -> [a] -> [a] -> Boolean
648
628
649− -- | The `Ordering` data type represents the three possible outcomes of comparing two values:
629+ -- | The `Ordering` data type represents the three possible outcomes of
630+ -- | comparing two values:
650 -- |
631 -- |
651 -- | `LT` - The first value is _less than_ the second.
632 -- | `LT` - The first value is _less than_ the second.
652 -- | `GT` - The first value is _greater than_ the second.
633 -- | `GT` - The first value is _greater than_ the second.
@@ -873,6 +854,63 @@ module Prelude
873 }
854 }
874 """ :: Boolean -> Boolean
855 """ :: Boolean -> Boolean
875
856
857+ -- | The `Show` type class represents those types which can be converted into
858+ -- | a human-readable `String` representation.
859+ -- |
860+ -- | While not required, it is recommended that for any expression `x`, the
861+ -- | string `show x` be executable PureScript code which evaluates to the same
862+ -- | value as the expression `x`.
863+ class Show a where
864+ show :: a -> String
865+
866+ instance showBoolean :: Show Boolean where
867+ show true = "true"
868+ show false = "false"
869+
870+ instance showNumber :: Show Number where
871+ show = showNumberImpl
872+
873+ instance showString :: Show String where
874+ show = showStringImpl
875+
876+ instance showUnit :: Show Unit where
877+ show _ = "unit"
878+
879+ instance showArray :: (Show a) => Show [a] where
880+ show = showArrayImpl show
881+
882+ instance showOrdering :: Show Ordering where
883+ show LT = "LT"
884+ show GT = "GT"
885+ show EQ = "EQ"
886+
887+ foreign import showNumberImpl
888+ """
889+ function showNumberImpl(n) {
890+ return n.toString();
891+ }
892+ """ :: Number -> String
893+
894+ foreign import showStringImpl
895+ """
896+ function showStringImpl(s) {
897+ return JSON.stringify(s);
898+ }
899+ """ :: String -> String
900+
901+ foreign import showArrayImpl
902+ """
903+ function showArrayImpl(f) {
904+ return function(xs) {
905+ var ss = [];
906+ for (var i = 0, l = xs.length; i < l; i++) {
907+ ss[i] = f(xs[i]);
908+ }
909+ return '[' + ss.join(',') + ']';
910+ };
911+ }
912+ """ :: forall a. (a -> String) -> [a] -> String
913+
876module Data.Function where
914module Data.Function where
877
915
878 -- | The `on` function is used to change the domain of a binary operator.
916 -- | The `on` function is used to change the domain of a binary operator.