git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Further update prelude docs, remove (%)
commit2fd349c5b55243cb8e47b46fbcb0351c7fb1f44e authorGary Burgess <gary.burgess@gmail.com> dateTue 31 Mar 2015, 19:52 (12 years ago) parentbbc5cbc
7 files changed+573−481
examples/passing/Collatz.purs+12−1 examples/passing/Guards.purs+31−21 examples/passing/OperatorAssociativity.purs−6 examples/passing/TopLevelCase.purs+21−11 prelude/README.md+246−217 prelude/images/Prelude.png prelude/prelude.purs+263−225
examples/passing/Collatz.purs+12−1
@@ -1,9 +1,20 @@
11module Main where
22
3−import Prelude
43import Control.Monad.Eff
54import Control.Monad.ST
65
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+
718collatz :: Number -> Number
819collatz n = runPure (runST (do
920 r <- newSTRef n
examples/passing/Guards.purs+31−21
@@ -1,29 +1,39 @@
11module Main where
22
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
411
5− collatz = \x -> case x of
6− y | y % 2 == 0 -> y / 2
7− y -> y * 3 + 1
12+infixl 7 %
13+(%) = jsMod
814
9− -- Guards have access to current scope
10− collatz2 = \x y -> case x of
11− z | y > 0 -> z / 2
12− z -> z * 3 + 1
15+collatz = \x -> case x of
16+ y | y % 2 == 0 -> y / 2
17+ y -> y * 3 + 1
1318
14− min :: forall a. (Ord a) => a -> a -> a
15− min n m | n < m = n
16− | otherwise = m
19+-- Guards have access to current scope
20+collatz2 = \x y -> case x of
21+ z | y > 0 -> z / 2
22+ z -> z * 3 + 1
1723
18− max :: forall a. (Ord a) => a -> a -> a
19− max n m = case unit of
20− _ | m < n -> n
21− | otherwise -> m
24+min :: forall a. (Ord a) => a -> a -> a
25+min n m | n < m = n
26+ | otherwise = m
2227
23− testIndentation :: Number -> Number -> Number
24− testIndentation x y | x > 0
25− = x + y
26− | otherwise
27− = y - x
28+max :: forall a. (Ord a) => a -> a -> a
29+max n m = case unit of
30+ _ | m < n -> n
31+ | otherwise -> m
2832
29− main = Debug.Trace.trace $ min "Done" "ZZZZ"
33+testIndentation :: Number -> Number -> Number
34+testIndentation x y | x > 0
35+ = x + y
36+ | otherwise
37+ = y - x
38+
39+main = Debug.Trace.trace $ min "Done" "ZZZZ"
examples/passing/OperatorAssociativity.purs−6
@@ -35,12 +35,6 @@ main = do
3535 assert (6 / (3 * 2) == 1) "6 / (3 * 2) == 1"
3636 assert ((6 / 3) * 2 == 4) "(6 / 3) * 2 == 4"
3737
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−
4438 assert (not (1 < 0) == true) "not (1 < 0) == true"
4539 assert (not ((negate 1) < 0) == false) "not ((negate 1) < 0) == false"
4640
examples/passing/TopLevelCase.purs+21−11
@@ -1,18 +1,28 @@
11module Main where
22
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
411
5− gcd :: Number -> Number -> Number
6− gcd 0 x = x
7− gcd x 0 = x
8− gcd x y | x > y = gcd (x % y) y
9− gcd x y = gcd (y % x) x
12+infixl 7 %
13+(%) = jsMod
1014
11− guardsTest (x:xs) | x > 0 = guardsTest xs
12− guardsTest xs = xs
15+gcd :: Number -> Number -> Number
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
1320
14− data A = A
21+guardsTest (x:xs) | x > 0 = guardsTest xs
22+guardsTest xs = xs
1523
16− parseTest A 0 = 0
24+data A = A
1725
18− main = Debug.Trace.trace "Done"
26+parseTest A 0 = 0
27+
28+main = Debug.Trace.trace "Done"
prelude/README.md+246−217
@@ -9,9 +9,11 @@ newtype Unit
99 = Unit { }
1010```
1111
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.
1314
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
1517the _effects_ are important.
1618
1719#### `unit`
@@ -22,165 +24,101 @@ unit :: Unit
2224
2325`unit` is the sole inhabitant of the `Unit` type.
2426
25−#### `otherwise`
27+#### `($)`
2628
2729``` purescript
28−otherwise :: Boolean
30+($) :: forall a b. (a -> b) -> a -> b
2931```
3032
31−An alias for `true`, which can be useful in guard clauses:
33+Applies a function to its argument.
3234
3335```purescript
34−max x y | x >= y = x
35− | otherwise = y
36−```
37−
38−#### `flip`
39−
40−``` purescript
41−flip :: forall a b c. (a -> b -> c) -> b -> a -> c
36+length $ groupBy productCategory $ filter isInStock $ products
4237```
4338
44−Flips the order of the arguments to a function of two arguments.
39+is equivalent to:
4540
4641```purescript
47−flip const 1 2 = const 2 1 = 2
48−```
49−
50−#### `const`
51−
52−``` purescript
53−const :: forall a b. a -> b -> a
42+length (groupBy productCategory (filter isInStock products))
5443```
5544
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)))`
5747
58−```purescript
59−const 1 "hello" = 1
60−```
61−
62−#### `asTypeOf`
48+#### `(#)`
6349
6450``` purescript
65−asTypeOf :: forall a. a -> a -> a
51+(#) :: forall a b. a -> (a -> b) -> b
6652```
6753
68−This function returns its first argument, and can be used to assert type equalities.
69−This can be useful when types are otherwise ambiguous.
54+Applies an argument to a function.
7055
7156```purescript
72−main = print $ [] `asTypeOf` [0]
57+products # filter isInStock # groupBy productCategory # length
7358```
7459
75−If instead, we had written `main = print []`, the type of the argument `[]` would have
76−been ambiguous, resulting in a compile-time error.
60+is equivalent to:
7761
78−#### `Semigroupoid`
79−
80−``` purescript
81−class Semigroupoid a where
82− (<<<) :: forall b c d. a c d -> a b c -> a b d
62+```purescript
63+length (groupBy productCategory (filter isInStock products))
8364```
8465
85−A `Semigroupoid` is similar to a [`Category`](#category) but does not require an identity
86−element `id`, just composable morphisms.
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.
66+`(#)` is different from [`($)`](#-1) because it is left-infix instead of
67+right: `x # a # b # c # d = (((x # a) # b) # c) # d = d (c (b (a x)))`
9468
95−#### `semigroupoidArr`
69+#### `flip`
9670
9771``` purescript
98−instance semigroupoidArr :: Semigroupoid Prim.Function
72+flip :: forall a b c. (a -> b -> c) -> b -> a -> c
9973```
10074
75+Flips the order of the arguments to a function of two arguments.
10176
102−#### `(>>>)`
103−
104−``` purescript
105−(>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
77+```purescript
78+flip const 1 2 = const 2 1 = 2
10679```
10780
108−Forwards composition, or `(<<<)` with its arguments reversed.
109−
110−#### `Category`
81+#### `const`
11182
11283``` purescript
113−class (Semigroupoid a) <= Category a where
114− id :: forall t. a t t
84+const :: forall a b. a -> b -> a
11585```
11686
117−`Category`s consist of objects and composable morphisms between them, and as such are
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`
87+Returns its first argument and ignores its second.
12688
127−``` purescript
128−instance categoryArr :: Category Prim.Function
89+```purescript
90+const 1 "hello" = 1
12991```
13092
131−
132−#### `($)`
93+#### `asTypeOf`
13394
13495``` purescript
135−($) :: forall a b. (a -> b) -> a -> b
136−```
137−
138−Applies a function to its argument.
139−
140−```purescript
141−length $ groupBy productCategory $ filter isInStock $ products
96+asTypeOf :: forall a. a -> a -> a
14297```
14398
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.
145101
146102```purescript
147−length (groupBy productCategory (filter isInStock products))
103+main = print $ [] `asTypeOf` [0]
148104```
149105
150−`($)` is different from [`(#)`](#-2) because it is right-infix instead of left, so
151−`a $ b $ c $ d x` = `a $ (b $ (c $ (d $ x)))` = `a (b (c (d x)))`
106+If instead, we had written `main = print []`, the type of the argument
107+`[]` would have been ambiguous, resulting in a compile-time error.
152108
153−#### `(#)`
109+#### `otherwise`
154110
155111``` purescript
156−(#) :: forall a b. a -> (a -> b) -> b
157−```
158−
159−Applies an argument to a function.
160−
161−```purescript
162−products # filter isInStock # groupBy productCategory # length
112+otherwise :: Boolean
163113```
164114
165−is equivalent to
115+An alias for `true`, which can be useful in guard clauses:
166116
167117```purescript
168−length (groupBy productCategory (filter isInStock products))
169−```
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]
118+max x y | x >= y = x
119+ | otherwise = y
178120```
179121
180−An infix alias for `cons`.
181−
182−Note, the running time of this function is `O(n)`.
183−
184122#### `cons`
185123
186124``` purescript
@@ -195,57 +133,68 @@ cons 1 [2, 3, 4] = [1, 2, 3, 4]
195133
196134Note, the running time of this function is `O(n)`.
197135
198−#### `Show`
136+#### `(:)`
199137
200138``` purescript
201−class Show a where
202− show :: a -> String
139+(:) :: forall a. a -> [a] -> [a]
203140```
204141
205−The `Show` type class represents those types which can be converted into a human-readable `String` representation.
142+An infix alias for `cons`.
206143
207−While not required, it is recommended that for any expression `x`, the string `show x` be executable PureScript code
208−which evaluates to the same value as the expression `x`.
144+Note, the running time of this function is `O(n)`.
209145
210−#### `showBoolean`
146+#### `Semigroupoid`
211147
212148``` 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
214151```
215152
153+A `Semigroupoid` is similar to a [`Category`](#category) but does not
154+require an identity element `id`, just composable morphisms.
216155
217−#### `showNumber`
156+`Semigroupoid`s must satisfy the following law:
218157
219−``` purescript
220−instance showNumber :: Show Number
221−```
158+- Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
222159
160+One example of a `Semigroupoid` is the function type constructor `(->)`,
161+with `(<<<)` defined as function composition.
223162
224−#### `showString`
163+#### `semigroupoidArr`
225164
226165``` purescript
227−instance showString :: Show String
166+instance semigroupoidArr :: Semigroupoid Prim.Function
228167```
229168
230169
231−#### `showUnit`
170+#### `(>>>)`
232171
233172``` purescript
234−instance showUnit :: Show Unit
173+(>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
235174```
236175
176+Forwards composition, or `(<<<)` with its arguments reversed.
237177
238−#### `showArray`
178+#### `Category`
239179
240180``` purescript
241−instance showArray :: (Show a) => Show [a]
181+class (Semigroupoid a) <= Category a where
182+ id :: forall t. a t t
242183```
243184
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.
244188
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`
246195
247196``` purescript
248−instance showOrdering :: Show Ordering
197+instance categoryArr :: Category Prim.Function
249198```
250199
251200
@@ -256,12 +205,14 @@ class Functor f where
256205 (<$>) :: forall a b. (a -> b) -> f a -> f b
257206```
258207
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+`(<$>)`.
260210
261−`(<$>)` can be used to turn functions `a -> b` into functions `f a -> f b` whose argument and return
262−types use the type constructor `f` to represent some computational context.
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`
213+to represent some computational context.
263214
264−`Functor` instances should satisfy the following laws:
215+Instances must satisfy the following laws:
265216
266217- Identity: `(<$>) id = id`
267218- Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
@@ -291,10 +242,12 @@ instance functorArr :: Functor (Prim.Function r)
291242void :: forall f a. (Functor f) => f a -> f Unit
292243```
293244
294−The `void` function is used to ignore the type wrapped by a [`Functor`](#functor), replacing it with `Unit` and
295−keeping only the type information provided by the type constructor itself.
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
247+information provided by the type constructor itself.
296248
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:
298251
299252```purescript
300253main = forE 1 10 \n -> void do
@@ -309,22 +262,26 @@ class (Functor f) <= Apply f where
309262 (<*>) :: forall a b. f (a -> b) -> f a -> f b
310263```
311264
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.
313267
314−`Apply` can be used to lift functions of two or more arguments to work on values wrapped with the type constructor `f`.
315−It might also be understood in terms of the `lift2` function:
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
270+in terms of the `lift2` function:
316271
317272```purescript
318273lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
319274lift2 f a b = f <$> a <*> b
320275```
321276
322−`(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts the function application operator `($)` to arguments
323−wrapped with the type constructor `f`.
277+`(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
278+the function application operator `($)` to arguments wrapped with the
279+type constructor `f`.
324280
325−`Apply` instances should satisfy the following law:
281+Instances must satisfy the following law in addition to the `Functor`
282+laws:
326283
327−- Associative Composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
284+- Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
328285
329286Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
330287
@@ -342,14 +299,19 @@ class (Apply f) <= Applicative f where
342299 pure :: forall a. a -> f a
343300```
344301
345−The `Applicative` type class extends the [`Apply`](#apply) type class with a `pure` function, which can be used to
346−create values of type `f a` from values of type `a`.
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`
304+from values of type `a`.
347305
348−Where [`Apply`](#apply) provides the ability to lift functions of two or more arguments to functions whose arguments are wrapped using `f`,
349−and [`Functor`](#functor) provides the ability to lift functions of one 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.
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
309+argument, `pure` can be seen as the function which lifts functions of
310+_zero_ arguments. That is, `Applicative` functors support a lifting
311+operation for any number of function arguments.
351312
352−`Applicative` instances should satisfy the following laws:
313+Instances must satisfy the following laws in addition to the `Apply`
314+laws:
353315
354316- Identity: `(pure id) <*> v = v`
355317- Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
@@ -377,10 +339,13 @@ return :: forall m a. (Applicative m) => a -> m a
377339liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
378340```
379341
380−`liftA1` provides a default implementation of `(<$>)` for any [`Applicative`](#applicative) functor,
381−without using `(<$>)` as provided by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass relationship.
342+`liftA1` provides a default implementation of `(<$>)` for any
343+[`Applicative`](#applicative) functor, without using `(<$>)` as provided
344+by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
345+relationship.
382346
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:
384349
385350```purescript
386351instance functorF :: Functor F where
@@ -394,8 +359,9 @@ class (Apply m) <= Bind m where
394359 (>>=) :: forall a b. m a -> (a -> m b) -> m b
395360```
396361
397−The `Bind` type class extends the [`Apply`](#apply) type class with a "bind" operation `(>>=)` which composes computations
398−in sequence, using the return value of one computation to determine the next computation.
362+The `Bind` type class extends the [`Apply`](#apply) type class with a
363+"bind" operation `(>>=)` which composes computations in sequence, using
364+the return value of one computation to determine the next computation.
399365
400366The `>>=` operator can also be expressed using `do` notation, as follows:
401367
@@ -406,15 +372,13 @@ x >>= f = do y <- x
406372
407373where the function argument of `f` is given the name `y`.
408374
409−`Bind` instances should satisfy the following law:
375+Instances must satisfy the following law in addition to the `Apply`
376+laws:
410377
411378- Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
412379
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:
418382
419383```purescript
420384do x <- m1
@@ -435,19 +399,17 @@ instance bindArr :: Bind (Prim.Function r)
435399class (Applicative m, Bind m) <= Monad m where
436400```
437401
438−The `Monad` type class combines the operations of the `Bind` and `Applicative` type classes. Therefore, `Monad` instances
439−represent type constructors which support sequential composition, and also lifting of functions of arbitrary arity.
402+The `Monad` type class combines the operations of the `Bind` and
403+`Applicative` type classes. Therefore, `Monad` instances represent type
404+constructors which support sequential composition, and also lifting of
405+functions of arbitrary arity.
440406
441−`Monad` instances should satisfy the following laws:
407+Instances must satisfy the following laws in addition to the
408+`Applicative` and `Bind` laws:
442409
443410- Left Identity: `pure x >>= f = f x`
444411- Right Identity: `x >>= pure = x`
445412
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−
451413#### `monadArr`
452414
453415``` purescript
@@ -461,10 +423,12 @@ instance monadArr :: Monad (Prim.Function r)
461423liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
462424```
463425
464−`liftM1` provides a default implementation of `(<$>)` for any [`Monad`](#monad),
465−without using `(<$>)` as provided by the [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
426+`liftM1` provides a default implementation of `(<$>)` for any
427+[`Monad`](#monad), without using `(<$>)` as provided by the
428+[`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
466429
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:
468432
469433```purescript
470434instance functorF :: Functor F where
@@ -477,10 +441,12 @@ instance functorF :: Functor F where
477441ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
478442```
479443
480−`ap` provides a default implementation of `(<*>)` for any [`Monad`](#monad),
481−without using `(<*>)` as provided by the [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
444+`ap` provides a default implementation of `(<*>)` for any
445+[`Monad`](#monad), without using `(<*>)` as provided by the
446+[`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
482447
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:
484450
485451```purescript
486452instance applyF :: Apply F where
@@ -496,11 +462,12 @@ class Semigroup a where
496462
497463The `Semigroup` type class identifies an associative operation on a type.
498464
499−`Semigroup` instances are required to satisfy the following law:
465+Instances are required to satisfy the following law:
500466
501467- Associativity: `(x <> y) <> z = x <> (y <> z)`
502468
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.
504471
505472#### `(++)`
506473
@@ -548,12 +515,22 @@ class Semiring a where
548515 one :: a
549516```
550517
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:
552522
553−- `a` is a commutative monoid under addition
554−- `a` is a monoid under multiplication
555−- multiplication distributes over addition
556−- multiplication by `zero` annihilates `a`
523+- Commutative monoid under addition:
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`
530+- Multiplication distributes over addition:
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`
557534
558535#### `semiringNumber`
559536
@@ -569,63 +546,69 @@ instance semiringUnit :: Semiring Unit
569546```
570547
571548
572−#### `ModuloSemiring`
549+#### `Ring`
573550
574551``` purescript
575−class (Semiring a) <= ModuloSemiring a where
576− (/) :: a -> a -> a
577− mod :: a -> a -> a
552+class (Semiring a) <= Ring a where
553+ (-) :: a -> a -> a
578554```
579555
580−Addition, multiplication, modulo operation and division, satisfying:
556+The `Ring` class is for types that support addition, multiplication,
557+and subtraction operations.
581558
582−- ```a / b * b + (a `mod` b) = a```
559+Instances must satisfy the following law in addition to the `Semiring`
560+laws:
583561
584−#### `moduloSemiringNumber`
562+- Additive inverse: `a + (−a) = (−a) + a = zero`
563+
564+#### `ringNumber`
585565
586566``` purescript
587−instance moduloSemiringNumber :: ModuloSemiring Number
567+instance ringNumber :: Ring Number
588568```
589569
590570
591−#### `moduloSemiringUnit`
571+#### `ringUnit`
592572
593573``` purescript
594−instance moduloSemiringUnit :: ModuloSemiring Unit
574+instance ringUnit :: Ring Unit
595575```
596576
597577
598−#### `Ring`
578+#### `negate`
599579
600580``` purescript
601−class (Semiring a) <= Ring a where
602− (-) :: a -> a -> a
581+negate :: forall a. (Ring a) => a -> a
603582```
604583
605−Addition, multiplication, and subtraction.
606−
607−Has the same laws as `Semiring` but additionally satisfying:
608584
609−- `a` is an abelian group under addition
610−
611−#### `ringNumber`
585+#### `ModuloSemiring`
612586
613587``` purescript
614−instance ringNumber :: Ring Number
588+class (Semiring a) <= ModuloSemiring a where
589+ (/) :: a -> a -> a
590+ mod :: a -> a -> a
615591```
616592
593+The `ModuloSemiring` class is for types that support addition,
594+multiplication, division, and modulo (division remainder) operations.
617595
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`
619602
620603``` purescript
621−instance ringUnit :: Ring Unit
604+instance moduloSemiringNumber :: ModuloSemiring Number
622605```
623606
624607
625−#### `negate`
608+#### `moduloSemiringUnit`
626609
627610``` purescript
628−negate :: forall a. (Ring a) => a -> a
611+instance moduloSemiringUnit :: ModuloSemiring Unit
629612```
630613
631614
@@ -635,9 +618,15 @@ negate :: forall a. (Ring a) => a -> a
635618class (Ring a, ModuloSemiring a) <= DivisionRing a where
636619```
637620
638−Ring where every nonzero element has a multiplicative inverse so that:
621+A `Ring` where every nonzero element has a multiplicative inverse.
639622
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.
641630
642631#### `divisionRingNumber`
643632
@@ -659,7 +648,12 @@ instance divisionRingUnit :: DivisionRing Unit
659648class (DivisionRing a) <= Num a where
660649```
661650
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`
663657
664658#### `numNumber`
665659
@@ -738,7 +732,8 @@ data Ordering
738732 | EQ
739733```
740734
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:
742737
743738`LT` - The first value is _less than_ the second.
744739`GT` - The first value is _greater than_ the second.
@@ -1004,25 +999,59 @@ instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
1004999```
10051000
10061001
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`
10081038
10091039``` purescript
1010−class Bits b where
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
1040+instance showUnit :: Show Unit
10181041```
10191042
1020−The `Bits` type class identifies types which support bitwise operations.
10211043
1022−#### `bitsNumber`
1044+#### `showArray`
10231045
10241046``` 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
10261055```
10271056
10281057
prelude/images/Prelude.png

Binary file not shown.

prelude/prelude.purs+263−225
@@ -1,14 +1,13 @@
11module Prelude
22 ( Unit(..), unit
3− , otherwise
3+ , ($), (#)
44 , flip
55 , const
66 , asTypeOf
7+ , otherwise
8+ , (:), cons
79 , Semigroupoid, (<<<), (>>>)
810 , Category, id
9− , ($), (#)
10− , (:), cons
11− , Show, show
1211 , Functor, (<$>), (<#>), void
1312 , Apply, (<*>)
1413 , Applicative, pure, liftA1
@@ -18,7 +17,6 @@ module Prelude
1817 , Semiring, (+), zero, (*), one
1918 , ModuloSemiring, (/), mod
2019 , Ring, (-), negate
21− , (%)
2220 , Num
2321 , DivisionRing
2422 , Eq, (==), (/=)
@@ -29,11 +27,14 @@ module Prelude
2927 , ComplementedLattice, not
3028 , DistributiveLattice
3129 , BooleanAlgebra
30+ , Show, show
3231 ) where
3332
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.
3535 -- |
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
3738 -- | the _effects_ are important.
3839 newtype Unit = Unit {}
3940
@@ -41,78 +42,6 @@ module Prelude
4142 unit :: Unit
4243 unit = Unit {}
4344
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−
11645 infixr 0 $
11746 infixl 0 #
11847
@@ -122,14 +51,14 @@ module Prelude
12251 -- | length $ groupBy productCategory $ filter isInStock $ products
12352 -- | ```
12453 -- |
125− -- | is equivalent to
54+ -- | is equivalent to:
12655 -- |
12756 -- | ```purescript
12857 -- | length (groupBy productCategory (filter isInStock products))
12958 -- | ```
13059 -- |
131− -- | `($)` is different from [`(#)`](#-2) because it is right-infix instead of left, so
132− -- | `a $ b $ c $ d x` = `a $ (b $ (c $ (d $ x)))` = `a (b (c (d x)))`
60+ -- | `($)` is different from [`(#)`](#-2) because it is right-infix instead of
61+ -- | left: `a $ b $ c $ d x = a $ (b $ (c $ (d $ x))) = a (b (c (d x)))`
13362 ($) :: forall a b. (a -> b) -> a -> b
13463 ($) f x = f x
13564
@@ -139,24 +68,53 @@ module Prelude
13968 -- | products # filter isInStock # groupBy productCategory # length
14069 -- | ```
14170 -- |
142− -- | is equivalent to
71+ -- | is equivalent to:
14372 -- |
14473 -- | ```purescript
14574 -- | length (groupBy productCategory (filter isInStock products))
14675 -- | ```
14776 -- |
148− -- | `(#)` is different from [`($)`](#-1) because it is left-infix instead of right, so
149− -- | `x # a # b # c # d` = `(((x # a) # b) # c) # d` = `d (c (b (a x)))`
77+ -- | `(#)` is different from [`($)`](#-1) because it is left-infix instead of
78+ -- | right: `x # a # b # c # d = (((x # a) # b) # c) # d = d (c (b (a x)))`
15079 (#) :: forall a b. a -> (a -> b) -> b
15180 (#) x f = f x
15281
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
15489
155− -- | An infix alias for `cons`.
90+ -- | Returns its first argument and ignores its second.
15691 -- |
157− -- | Note, the running time of this function is `O(n)`.
158− (:) :: forall a. a -> [a] -> [a]
159− (:) = cons
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.
107+ asTypeOf :: forall a. a -> a -> a
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
160118
161119 -- | Attaches an element to the front of an array, creating a new array.
162120 -- |
@@ -174,70 +132,61 @@ module Prelude
174132 }
175133 """ :: forall a. a -> [a] -> [a]
176134
177− -- | The `Show` type class represents those types which can be converted into a human-readable `String` representation.
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
135+ infixr 6 :
190136
191− instance showString :: Show String where
192− show = showStringImpl
137+ -- | An infix alias for `cons`.
138+ -- |
139+ -- | Note, the running time of this function is `O(n)`.
140+ (:) :: forall a. a -> [a] -> [a]
141+ (:) = cons
193142
194− instance showUnit :: Show Unit where
195− show _ = "unit"
143+ infixr 9 >>>
144+ infixr 9 <<<
196145
197− instance showArray :: (Show a) => Show [a] where
198− show = showArrayImpl show
146+ -- | A `Semigroupoid` is similar to a [`Category`](#category) but does not
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
199157
200− instance showOrdering :: Show Ordering where
201− show LT = "LT"
202− show GT = "GT"
203− show EQ = "EQ"
158+ instance semigroupoidArr :: Semigroupoid (->) where
159+ (<<<) f g x = f (g x)
204160
205− foreign import showNumberImpl
206− """
207− function showNumberImpl(n) {
208− return n.toString();
209− }
210− """ :: Number -> String
161+ -- | Forwards composition, or `(<<<)` with its arguments reversed.
162+ (>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
163+ (>>>) f g = g <<< f
211164
212− foreign import showStringImpl
213− """
214− function showStringImpl(s) {
215− return JSON.stringify(s);
216− }
217− """ :: String -> String
165+ -- | `Category`s consist of objects and composable morphisms between them, and
166+ -- | as such are [`Semigroupoids`](#semigroupoid), but unlike `semigroupoids`
167+ -- | must have an identity element.
168+ -- |
169+ -- | Instances must satisfy the following law in addition to the
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
218175
219− foreign import showArrayImpl
220− """
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
176+ instance categoryArr :: Category (->) where
177+ id x = x
231178
232179 infixl 4 <$>
233180 infixl 1 <#>
234181
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+ -- | `(<$>)`.
236184 -- |
237− -- | `(<$>)` can be used to turn functions `a -> b` into functions `f a -> f b` whose argument and return
238− -- | types use the type constructor `f` to represent some computational context.
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`
187+ -- | to represent some computational context.
239188 -- |
240− -- | `Functor` instances should satisfy the following laws:
189+ -- | Instances must satisfy the following laws:
241190 -- |
242191 -- | - Identity: `(<$>) id = id`
243192 -- | - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
@@ -255,10 +204,12 @@ module Prelude
255204 (<#>) :: forall f a b. (Functor f) => f a -> (a -> b) -> f b
256205 (<#>) fa f = f <$> fa
257206
258− -- | The `void` function is used to ignore the type wrapped by a [`Functor`](#functor), replacing it with `Unit` and
259− -- | keeping only the type information provided by the type constructor itself.
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
209+ -- | information provided by the type constructor itself.
260210 -- |
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:
262213 -- |
263214 -- | ```purescript
264215 -- | main = forE 1 10 \n -> void do
@@ -270,22 +221,26 @@ module Prelude
270221
271222 infixl 4 <*>
272223
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.
274226 -- |
275− -- | `Apply` can be used to lift functions of two or more arguments to work on values wrapped with the type constructor `f`.
276− -- | It might also be understood in terms of the `lift2` function:
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
229+ -- | in terms of the `lift2` function:
277230 -- |
278231 -- | ```purescript
279232 -- | lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
280233 -- | lift2 f a b = f <$> a <*> b
281234 -- | ```
282235 -- |
283− -- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts the function application operator `($)` to arguments
284− -- | wrapped with the type constructor `f`.
236+ -- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
237+ -- | the function application operator `($)` to arguments wrapped with the
238+ -- | type constructor `f`.
285239 -- |
286− -- | `Apply` instances should satisfy the following law:
240+ -- | Instances must satisfy the following law in addition to the `Functor`
241+ -- | laws:
287242 -- |
288− -- | - Associative Composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
243+ -- | - Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
289244 -- |
290245 -- | Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
291246 class (Functor f) <= Apply f where
@@ -294,14 +249,19 @@ module Prelude
294249 instance applyArr :: Apply ((->) r) where
295250 (<*>) f g x = f x (g x)
296251
297− -- | The `Applicative` type class extends the [`Apply`](#apply) type class with a `pure` function, which can be used to
298− -- | create values of type `f a` from values of type `a`.
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`
254+ -- | from values of type `a`.
299255 -- |
300− -- | Where [`Apply`](#apply) provides the ability to lift functions of two or more arguments to functions whose arguments are wrapped using `f`,
301− -- | and [`Functor`](#functor) provides the ability to lift functions of one 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.
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
259+ -- | argument, `pure` can be seen as the function which lifts functions of
260+ -- | _zero_ arguments. That is, `Applicative` functors support a lifting
261+ -- | operation for any number of function arguments.
303262 -- |
304− -- | `Applicative` instances should satisfy the following laws:
263+ -- | Instances must satisfy the following laws in addition to the `Apply`
264+ -- | laws:
305265 -- |
306266 -- | - Identity: `(pure id) <*> v = v`
307267 -- | - Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
@@ -317,10 +277,13 @@ module Prelude
317277 return :: forall m a. (Applicative m) => a -> m a
318278 return = pure
319279
320− -- | `liftA1` provides a default implementation of `(<$>)` for any [`Applicative`](#applicative) functor,
321− -- | without using `(<$>)` as provided by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass relationship.
280+ -- | `liftA1` provides a default implementation of `(<$>)` for any
281+ -- | [`Applicative`](#applicative) functor, without using `(<$>)` as provided
282+ -- | by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
283+ -- | relationship.
322284 -- |
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:
324287 -- |
325288 -- | ```purescript
326289 -- | instance functorF :: Functor F where
@@ -331,8 +294,9 @@ module Prelude
331294
332295 infixl 1 >>=
333296
334− -- | The `Bind` type class extends the [`Apply`](#apply) type class with a "bind" operation `(>>=)` which composes computations
335− -- | in sequence, using the return value of one computation to determine the next computation.
297+ -- | The `Bind` type class extends the [`Apply`](#apply) type class with a
298+ -- | "bind" operation `(>>=)` which composes computations in sequence, using
299+ -- | the return value of one computation to determine the next computation.
336300 -- |
337301 -- | The `>>=` operator can also be expressed using `do` notation, as follows:
338302 -- |
@@ -343,15 +307,13 @@ module Prelude
343307 -- |
344308 -- | where the function argument of `f` is given the name `y`.
345309 -- |
346− -- | `Bind` instances should satisfy the following law:
310+ -- | Instances must satisfy the following law in addition to the `Apply`
311+ -- | laws:
347312 -- |
348313 -- | - Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
349314 -- |
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:
355317 -- |
356318 -- | ```purescript
357319 -- | do x <- m1
@@ -364,26 +326,26 @@ module Prelude
364326 instance bindArr :: Bind ((->) r) where
365327 (>>=) m f x = f (m x) x
366328
367− -- | The `Monad` type class combines the operations of the `Bind` and `Applicative` type classes. Therefore, `Monad` instances
368− -- | represent type constructors which support sequential composition, and also lifting of functions of arbitrary arity.
329+ -- | The `Monad` type class combines the operations of the `Bind` and
330+ -- | `Applicative` type classes. Therefore, `Monad` instances represent type
331+ -- | constructors which support sequential composition, and also lifting of
332+ -- | functions of arbitrary arity.
369333 -- |
370− -- | `Monad` instances should satisfy the following laws:
334+ -- | Instances must satisfy the following laws in addition to the
335+ -- | `Applicative` and `Bind` laws:
371336 -- |
372337 -- | - Left Identity: `pure x >>= f = f x`
373338 -- | - 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`
379339 class (Applicative m, Bind m) <= Monad m
380340
381341 instance monadArr :: Monad ((->) r)
382342
383− -- | `liftM1` provides a default implementation of `(<$>)` for any [`Monad`](#monad),
384− -- | without using `(<$>)` as provided by the [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
343+ -- | `liftM1` provides a default implementation of `(<$>)` for any
344+ -- | [`Monad`](#monad), without using `(<$>)` as provided by the
345+ -- | [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
385346 -- |
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:
387349 -- |
388350 -- | ```purescript
389351 -- | instance functorF :: Functor F where
@@ -394,10 +356,12 @@ module Prelude
394356 a' <- a
395357 return (f a')
396358
397− -- | `ap` provides a default implementation of `(<*>)` for any [`Monad`](#monad),
398− -- | without using `(<*>)` as provided by the [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
359+ -- | `ap` provides a default implementation of `(<*>)` for any
360+ -- | [`Monad`](#monad), without using `(<*>)` as provided by the
361+ -- | [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
399362 -- |
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:
401365 -- |
402366 -- | ```purescript
403367 -- | instance applyF :: Apply F where
@@ -414,11 +378,12 @@ module Prelude
414378
415379 -- | The `Semigroup` type class identifies an associative operation on a type.
416380 -- |
417− -- | `Semigroup` instances are required to satisfy the following law:
381+ -- | Instances are required to satisfy the following law:
418382 -- |
419383 -- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
420384 -- |
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.
422387 class Semigroup a where
423388 (<>) :: a -> a -> a
424389
@@ -452,12 +417,22 @@ module Prelude
452417 infixl 6 +
453418 infixl 7 *
454419
455− -- | Addition and multiplication, satisfying the following laws:
456− -- |
457− -- | - `a` is a commutative monoid under addition
458− -- | - `a` is a monoid under multiplication
459− -- | - multiplication distributes over addition
460− -- | - multiplication by `zero` annihilates `a`
420+ -- | The `Semiring` class is for types that support an addition and
421+ -- | multiplication operation.
422+ -- |
423+ -- | Instances must satisfy the following laws:
424+ -- |
425+ -- | - Commutative monoid under addition:
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`
432+ -- | - Multiplication distributes over addition:
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`
461436 class Semiring a where
462437 (+) :: a -> a -> a
463438 zero :: a
@@ -476,11 +451,36 @@ module Prelude
476451 (*) _ _ = unit
477452 one = unit
478453
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+
479475 infixl 7 /
480476
481− -- | Addition, multiplication, modulo operation and division, satisfying:
477+ -- | The `ModuloSemiring` class is for types that support addition,
478+ -- | multiplication, division, and modulo (division remainder) operations.
479+ -- |
480+ -- | Instances must satisfy the following law in addition to the `Semiring`
481+ -- | laws:
482482 -- |
483− -- | - ```a / b * b + (a `mod` b) = a```
483+ -- | - Remainder: `a / b * b + (a `mod` b) = a`
484484 class (Semiring a) <= ModuloSemiring a where
485485 (/) :: a -> a -> a
486486 mod :: a -> a -> a
@@ -493,44 +493,33 @@ module Prelude
493493 (/) _ _ = unit
494494 mod _ _ = unit
495495
496− infixl 6 -
497−
498− -- | Addition, multiplication, and subtraction.
496+ -- | A `Ring` where every nonzero element has a multiplicative inverse.
499497 -- |
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:
501500 -- |
502− -- | - `a` is an abelian group under addition
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:
501+ -- | - Multiplicative inverse: `(one / x) * x = one`
516502 -- |
517− -- | - ```a `mod` b = zero```
503+ -- | As a consequence of this ```a `mod` b = zero``` as no divide operation
504+ -- | will have a remainder.
518505 class (Ring a, ModuloSemiring a) <= DivisionRing a
519506
520507 instance divisionRingNumber :: DivisionRing Number
521508
522509 instance divisionRingUnit :: DivisionRing Unit
523510
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`
525517 class (DivisionRing a) <= Num a
526518
527519 instance numNumber :: Num Number
528520
529521 instance numUnit :: Num Unit
530522
531− infixl 7 %
532− (%) = numMod
533−
534523 foreign import numAdd
535524 """
536525 function numAdd(n1) {
@@ -567,15 +556,6 @@ module Prelude
567556 }
568557 """ :: Number -> Number -> Number
569558
570− foreign import numMod
571− """
572− function numMod(n1) {
573− return function(n2) {
574− return n1 % n2;
575− };
576− }
577− """ :: Number -> Number -> Number
578−
579559 infix 4 ==
580560 infix 4 /=
581561
@@ -646,7 +626,8 @@ module Prelude
646626 }
647627 """ :: forall a. (a -> a -> Boolean) -> [a] -> [a] -> Boolean
648628
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:
650631 -- |
651632 -- | `LT` - The first value is _less than_ the second.
652633 -- | `GT` - The first value is _greater than_ the second.
@@ -873,6 +854,63 @@ module Prelude
873854 }
874855 """ :: Boolean -> Boolean
875856
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+
876914module Data.Function where
877915
878916 -- | The `on` function is used to change the domain of a binary operator.