git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Organise prelude, add Unit instances
commit4b2c65dc493edfd9ba8efe82c48cd7b1e855d912 authorGary Burgess <gary.burgess@gmail.com> dateMon 30 Mar 2015, 20:29 (12 years ago) parent4dd5c10
2 files changed+447−417
prelude/README.md+202−174 prelude/prelude.purs+245−243
prelude/README.md+202−174
@@ -2,6 +2,26 @@
22
33## Module Prelude
44
5+#### `Unit`
6+
7+``` purescript
8+newtype Unit
9+ = Unit { }
10+```
11+
12+The `Unit` type has a single inhabitant, called `unit`. It represents values with no computational content.
13+
14+`Unit` is often used, wrapped in a monadic type constructor, as the return type of a computation where only
15+the _effects_ are important.
16+
17+#### `unit`
18+
19+``` purescript
20+unit :: Unit
21+```
22+
23+`unit` is the sole inhabitant of the `Unit` type.
24+
525#### `otherwise`
626
727``` purescript
@@ -15,7 +35,6 @@ max x y | x >= y = x
1535 | otherwise = y
1636```
1737
18−
1938#### `flip`
2039
2140``` purescript
@@ -28,7 +47,6 @@ Flips the order of the arguments to a function of two arguments.
2847flip const 1 2 = const 2 1 = 2
2948```
3049
31−
3250#### `const`
3351
3452``` purescript
@@ -41,7 +59,6 @@ Returns its first argument and ignores its second.
4159const 1 "hello" = 1
4260```
4361
44−
4562#### `asTypeOf`
4663
4764``` purescript
@@ -105,7 +122,6 @@ class (Semigroupoid a) <= Category a where
105122- Left Identity: `id <<< p = p`
106123- Right Identity: `p <<< id = p`
107124
108−
109125#### `categoryArr`
110126
111127``` purescript
@@ -134,7 +150,6 @@ length (groupBy productCategory (filter isInStock products))
134150`($)` is different from [`(#)`](#-2) because it is right-infix instead of left, so
135151`a $ b $ c $ d x` = `a $ (b $ (c $ (d $ x)))` = `a (b (c (d x)))`
136152
137−
138153#### `(#)`
139154
140155``` purescript
@@ -156,7 +171,6 @@ length (groupBy productCategory (filter isInStock products))
156171`(#)` is different from [`($)`](#-1) because it is left-infix instead of right, so
157172`x # a # b # c # d` = `(((x # a) # b) # c) # d` = `d (c (b (a x)))`
158173
159−
160174#### `(:)`
161175
162176``` purescript
@@ -193,31 +207,31 @@ The `Show` type class represents those types which can be converted into a human
193207While not required, it is recommended that for any expression `x`, the string `show x` be executable PureScript code
194208which evaluates to the same value as the expression `x`.
195209
196−#### `showUnit`
210+#### `showBoolean`
197211
198212``` purescript
199−instance showUnit :: Show Unit
213+instance showBoolean :: Show Boolean
200214```
201215
202216
203−#### `showString`
217+#### `showNumber`
204218
205219``` purescript
206−instance showString :: Show String
220+instance showNumber :: Show Number
207221```
208222
209223
210−#### `showBoolean`
224+#### `showString`
211225
212226``` purescript
213−instance showBoolean :: Show Boolean
227+instance showString :: Show String
214228```
215229
216230
217−#### `showNumber`
231+#### `showUnit`
218232
219233``` purescript
220−instance showNumber :: Show Number
234+instance showUnit :: Show Unit
221235```
222236
223237
@@ -228,6 +242,13 @@ instance showArray :: (Show a) => Show [a]
228242```
229243
230244
245+#### `showOrdering`
246+
247+``` purescript
248+instance showOrdering :: Show Ordering
249+```
250+
251+
231252#### `Functor`
232253
233254``` purescript
@@ -245,6 +266,12 @@ types use the type constructor `f` to represent some computational context.
245266- Identity: `(<$>) id = id`
246267- Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
247268
269+#### `functorArr`
270+
271+``` purescript
272+instance functorArr :: Functor (Prim.Function r)
273+```
274+
248275
249276#### `(<#>)`
250277
@@ -301,6 +328,13 @@ wrapped with the type constructor `f`.
301328
302329Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
303330
331+#### `applyArr`
332+
333+``` purescript
334+instance applyArr :: Apply (Prim.Function r)
335+```
336+
337+
304338#### `Applicative`
305339
306340``` purescript
@@ -322,6 +356,20 @@ _zero_ arguments. That is, `Applicative` functors support a lifting operation fo
322356- Homomorphism: `(pure f) <*> (pure x) = pure (f x)`
323357- Interchange: `u <*> (pure y) = (pure ($ y)) <*> u`
324358
359+#### `applicativeArr`
360+
361+``` purescript
362+instance applicativeArr :: Applicative (Prim.Function r)
363+```
364+
365+
366+#### `return`
367+
368+``` purescript
369+return :: forall m a. (Applicative m) => a -> m a
370+```
371+
372+`return` is an alias for `pure`.
325373
326374#### `liftA1`
327375
@@ -374,6 +422,13 @@ do x <- m1
374422 m3 x y
375423```
376424
425+#### `bindArr`
426+
427+``` purescript
428+instance bindArr :: Bind (Prim.Function r)
429+```
430+
431+
377432#### `Monad`
378433
379434``` purescript
@@ -393,14 +448,12 @@ Or, expressed using `do` notation:
393448- Left Identity: `do { y <- pure x ; f y } = f x`
394449- Right Identity: `do { y <- x ; pure y } = x`
395450
396−
397−#### `return`
451+#### `monadArr`
398452
399453``` purescript
400−return :: forall m a. (Monad m) => a -> m a
454+instance monadArr :: Monad (Prim.Function r)
401455```
402456
403−`return` is an alias for `pure`.
404457
405458#### `liftM1`
406459
@@ -434,38 +487,54 @@ instance applyF :: Apply F where
434487 (<*>) = ap
435488```
436489
437−#### `functorArr`
490+#### `Semigroup`
438491
439492``` purescript
440−instance functorArr :: Functor (Prim.Function r)
493+class Semigroup a where
494+ (<>) :: a -> a -> a
441495```
442496
497+The `Semigroup` type class identifies an associative operation on a type.
443498
444−#### `applyArr`
499+`Semigroup` instances are required to satisfy the following law:
500+
501+- Associativity: `(x <> y) <> z = x <> (y <> z)`
502+
503+For example, the `String` type is an instance of `Semigroup`, where `(<>)` is defined to be string concatenation.
504+
505+#### `(++)`
445506
446507``` purescript
447−instance applyArr :: Apply (Prim.Function r)
508+(++) :: forall s. (Semigroup s) => s -> s -> s
448509```
449510
511+`(++)` is an alias for `(<>)`.
450512
451−#### `applicativeArr`
513+#### `semigroupString`
452514
453515``` purescript
454−instance applicativeArr :: Applicative (Prim.Function r)
516+instance semigroupString :: Semigroup String
455517```
456518
457519
458−#### `bindArr`
520+#### `semigroupUnit`
459521
460522``` purescript
461−instance bindArr :: Bind (Prim.Function r)
523+instance semigroupUnit :: Semigroup Unit
462524```
463525
464526
465−#### `monadArr`
527+#### `semigroupArr`
466528
467529``` purescript
468−instance monadArr :: Monad (Prim.Function r)
530+instance semigroupArr :: (Semigroup s') => Semigroup (s -> s')
531+```
532+
533+
534+#### `semigroupOrdering`
535+
536+``` purescript
537+instance semigroupOrdering :: Semigroup Ordering
469538```
470539
471540
@@ -486,6 +555,19 @@ Addition and multiplication, satisfying the following laws:
486555- multiplication distributes over addition
487556- multiplication by `zero` annihilates `a`
488557
558+#### `semiringNumber`
559+
560+``` purescript
561+instance semiringNumber :: Semiring Number
562+```
563+
564+
565+#### `semiringUnit`
566+
567+``` purescript
568+instance semiringUnit :: Semiring Unit
569+```
570+
489571
490572#### `ModuloSemiring`
491573
@@ -499,6 +581,19 @@ Addition, multiplication, modulo operation and division, satisfying:
499581
500582- ```a / b * b + (a `mod` b) = a```
501583
584+#### `moduloSemiringNumber`
585+
586+``` purescript
587+instance moduloSemiringNumber :: ModuloSemiring Number
588+```
589+
590+
591+#### `moduloSemiringUnit`
592+
593+``` purescript
594+instance moduloSemiringUnit :: ModuloSemiring Unit
595+```
596+
502597
503598#### `Ring`
504599
@@ -513,60 +608,58 @@ Has the same laws as `Semiring` but additionally satisfying:
513608
514609- `a` is an abelian group under addition
515610
516−
517−#### `negate`
611+#### `ringNumber`
518612
519613``` purescript
520−negate :: forall a. (Ring a) => a -> a
614+instance ringNumber :: Ring Number
521615```
522616
523617
524−#### `DivisionRing`
618+#### `ringUnit`
525619
526620``` purescript
527−class (Ring a, ModuloSemiring a) <= DivisionRing a where
621+instance ringUnit :: Ring Unit
528622```
529623
530−Ring where every nonzero element has a multiplicative inverse so that:
531624
532−- ```a `mod` b = zero```
533−
534−
535−#### `Num`
625+#### `negate`
536626
537627``` purescript
538−class (DivisionRing a) <= Num a where
628+negate :: forall a. (Ring a) => a -> a
539629```
540630
541−A commutative field
542631
543−#### `semiringNumber`
632+#### `DivisionRing`
544633
545634``` purescript
546−instance semiringNumber :: Semiring Number
635+class (Ring a, ModuloSemiring a) <= DivisionRing a where
547636```
548637
638+Ring where every nonzero element has a multiplicative inverse so that:
549639
550−#### `ringNumber`
640+- ```a `mod` b = zero```
641+
642+#### `divisionRingNumber`
551643
552644``` purescript
553−instance ringNumber :: Ring Number
645+instance divisionRingNumber :: DivisionRing Number
554646```
555647
556648
557−#### `moduloSemiringNumber`
649+#### `divisionRingUnit`
558650
559651``` purescript
560−instance moduloSemiringNumber :: ModuloSemiring Number
652+instance divisionRingUnit :: DivisionRing Unit
561653```
562654
563655
564−#### `divisionRingNumber`
656+#### `Num`
565657
566658``` purescript
567−instance divisionRingNumber :: DivisionRing Number
659+class (DivisionRing a) <= Num a where
568660```
569661
662+A commutative field
570663
571664#### `numNumber`
572665
@@ -575,25 +668,12 @@ instance numNumber :: Num Number
575668```
576669
577670
578−#### `Unit`
579−
580−``` purescript
581−newtype Unit
582− = Unit { }
583−```
584−
585−The `Unit` type has a single inhabitant, called `unit`. It represents values with no computational content.
586−
587−`Unit` is often used, wrapped in a monadic type constructor, as the return type of a computation where only
588−the _effects_ are important.
589−
590−#### `unit`
671+#### `numUnit`
591672
592673``` purescript
593−unit :: Unit
674+instance numUnit :: Num Unit
594675```
595676
596−`unit` is the sole inhabitant of the `Unit` type.
597677
598678#### `Eq`
599679
@@ -614,31 +694,31 @@ The `Eq` type class represents types which support decidable equality.
614694
615695`(/=)` may be implemented in terms of `(==)`, but it might give a performance improvement to implement it separately.
616696
617−#### `eqUnit`
697+#### `eqBoolean`
618698
619699``` purescript
620−instance eqUnit :: Eq Unit
700+instance eqBoolean :: Eq Boolean
621701```
622702
623703
624−#### `eqString`
704+#### `eqNumber`
625705
626706``` purescript
627−instance eqString :: Eq String
707+instance eqNumber :: Eq Number
628708```
629709
630710
631−#### `eqNumber`
711+#### `eqString`
632712
633713``` purescript
634−instance eqNumber :: Eq Number
714+instance eqString :: Eq String
635715```
636716
637717
638−#### `eqBoolean`
718+#### `eqUnit`
639719
640720``` purescript
641−instance eqBoolean :: Eq Boolean
721+instance eqUnit :: Eq Unit
642722```
643723
644724
@@ -664,27 +744,6 @@ The `Ordering` data type represents the three possible outcomes of comparing two
664744`GT` - The first value is _greater than_ the second.
665745`EQ` - The first value is _equal to_ or _incomparable to_ the second.
666746
667−#### `eqOrdering`
668−
669−``` purescript
670−instance eqOrdering :: Eq Ordering
671−```
672−
673−
674−#### `showOrdering`
675−
676−``` purescript
677−instance showOrdering :: Show Ordering
678−```
679−
680−
681−#### `semigroupOrdering`
682−
683−``` purescript
684−instance semigroupOrdering :: Semigroup Ordering
685−```
686−
687−
688747#### `Ord`
689748
690749``` purescript
@@ -700,73 +759,79 @@ The `Ord` type class represents types which support comparisons.
700759- Antisymmetry: if `a <= b` and `b <= a` then `a = b`
701760- Transitivity: if `a <= b` and `b <= c` then `a <= c`
702761
762+#### `ordBoolean`
703763
704−#### `(<)`
764+``` purescript
765+instance ordBoolean :: Ord Boolean
766+```
767+
768+
769+#### `ordNumber`
705770
706771``` purescript
707−(<) :: forall a. (Ord a) => a -> a -> Boolean
772+instance ordNumber :: Ord Number
708773```
709774
710−Test whether one value is _strictly less than_ another.
711775
712−#### `(>)`
776+#### `ordString`
713777
714778``` purescript
715−(>) :: forall a. (Ord a) => a -> a -> Boolean
779+instance ordString :: Ord String
716780```
717781
718−Test whether one value is _strictly greater than_ another.
719782
720−#### `(<=)`
783+#### `ordUnit`
721784
722785``` purescript
723−(<=) :: forall a. (Ord a) => a -> a -> Boolean
786+instance ordUnit :: Ord Unit
724787```
725788
726−Test whether one value is _non-strictly less than_ another.
727789
728−#### `(>=)`
790+#### `ordArray`
729791
730792``` purescript
731−(>=) :: forall a. (Ord a) => a -> a -> Boolean
793+instance ordArray :: (Ord a) => Ord [a]
732794```
733795
734−Test whether one value is _non-strictly greater than_ another.
735796
736−#### `ordUnit`
797+#### `eqOrdering`
737798
738799``` purescript
739−instance ordUnit :: Ord Unit
800+instance eqOrdering :: Eq Ordering
740801```
741802
742803
743−#### `ordBoolean`
804+#### `(<)`
744805
745806``` purescript
746−instance ordBoolean :: Ord Boolean
807+(<) :: forall a. (Ord a) => a -> a -> Boolean
747808```
748809
810+Test whether one value is _strictly less than_ another.
749811
750−#### `ordNumber`
812+#### `(>)`
751813
752814``` purescript
753−instance ordNumber :: Ord Number
815+(>) :: forall a. (Ord a) => a -> a -> Boolean
754816```
755817
818+Test whether one value is _strictly greater than_ another.
756819
757−#### `ordString`
820+#### `(<=)`
758821
759822``` purescript
760−instance ordString :: Ord String
823+(<=) :: forall a. (Ord a) => a -> a -> Boolean
761824```
762825
826+Test whether one value is _non-strictly less than_ another.
763827
764−#### `ordArray`
828+#### `(>=)`
765829
766830``` purescript
767−instance ordArray :: (Ord a) => Ord [a]
831+(>=) :: forall a. (Ord a) => a -> a -> Boolean
768832```
769833
834+Test whether one value is _non-strictly greater than_ another.
770835
771836#### `Bounded`
772837
@@ -790,6 +855,13 @@ instance boundedBoolean :: Bounded Boolean
790855```
791856
792857
858+#### `boundedUnit`
859+
860+``` purescript
861+instance boundedUnit :: Bounded Unit
862+```
863+
864+
793865#### `Lattice`
794866
795867``` purescript
@@ -824,6 +896,22 @@ instance latticeBoolean :: Lattice Boolean
824896```
825897
826898
899+#### `(||)`
900+
901+``` purescript
902+(||) :: forall a. (Lattice a) => a -> a -> a
903+```
904+
905+The `sup` operator.
906+
907+#### `(&&)`
908+
909+``` purescript
910+(&&) :: forall a. (Lattice a) => a -> a -> a
911+```
912+
913+The `inf` operator.
914+
827915#### `BoundedLattice`
828916
829917``` purescript
@@ -916,66 +1004,6 @@ instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
9161004```
9171005
9181006
919−#### `(||)`
920−
921−``` purescript
922−(||) :: forall a. (Lattice a) => a -> a -> a
923−```
924−
925−The `sup` operator.
926−
927−#### `(&&)`
928−
929−``` purescript
930−(&&) :: forall a. (Lattice a) => a -> a -> a
931−```
932−
933−The `inf` operator.
934−
935−#### `Semigroup`
936−
937−``` purescript
938−class Semigroup a where
939− (<>) :: a -> a -> a
940−```
941−
942−The `Semigroup` type class identifies an associative operation on a type.
943−
944−`Semigroup` instances are required to satisfy the following law:
945−
946−- Associativity: `(x <> y) <> z = x <> (y <> z)`
947−
948−For example, the `String` type is an instance of `Semigroup`, where `(<>)` is defined to be string concatenation.
949−
950−#### `semigroupUnit`
951−
952−``` purescript
953−instance semigroupUnit :: Semigroup Unit
954−```
955−
956−
957−#### `semigroupString`
958−
959−``` purescript
960−instance semigroupString :: Semigroup String
961−```
962−
963−
964−#### `semigroupArr`
965−
966−``` purescript
967−instance semigroupArr :: (Semigroup s') => Semigroup (s -> s')
968−```
969−
970−
971−#### `(++)`
972−
973−``` purescript
974−(++) :: forall s. (Semigroup s) => s -> s -> s
975−```
976−
977−`(++)` is an alias for `(<>)`.
978−
9791007#### `Bits`
9801008
9811009``` purescript
prelude/prelude.purs+245−243
@@ -1,5 +1,6 @@
11module Prelude
2− ( otherwise
2+ ( Unit(..), unit
3+ , otherwise
34 , flip
45 , const
56 , asTypeOf
@@ -13,15 +14,15 @@ module Prelude
1314 , Applicative, pure, liftA1
1415 , Bind, (>>=)
1516 , Monad, return, liftM1, ap
17+ , Semigroup, (<>), (++)
1618 , Semiring, (+), zero, (*), one
1719 , ModuloSemiring, (/), mod
18− , Ring, (-)
20+ , Ring, (-), negate
1921 , (%)
20− , negate
21− , DivisionRing
2222 , Num
23+ , DivisionRing
2324 , Eq, (==), (/=)
24− , Ord, Ordering(..), compare, (<), (>), (<=), (>=)
25+ , Ordering(..), Ord, compare, (<), (>), (<=), (>=)
2526 , Bounded, top, bottom
2627 , Lattice, sup, inf, (||), (&&)
2728 , BoundedLattice
@@ -29,17 +30,24 @@ module Prelude
2930 , DistributiveLattice
3031 , BooleanAlgebra
3132 , Bits, (.&.), (.|.), (.^.), shl, shr, zshr, complement
32− , Semigroup, (<>), (++)
33− , Unit(..), unit
3433 ) where
3534
35+ -- | The `Unit` type has a single inhabitant, called `unit`. It represents values with no computational content.
36+ -- |
37+ -- | `Unit` is often used, wrapped in a monadic type constructor, as the return type of a computation where only
38+ -- | the _effects_ are important.
39+ newtype Unit = Unit {}
40+
41+ -- | `unit` is the sole inhabitant of the `Unit` type.
42+ unit :: Unit
43+ unit = Unit {}
44+
3645 -- | An alias for `true`, which can be useful in guard clauses:
3746 -- |
3847 -- | ```purescript
3948 -- | max x y | x >= y = x
4049 -- | | otherwise = y
4150 -- | ```
42− -- |
4351 otherwise :: Boolean
4452 otherwise = true
4553
@@ -48,7 +56,6 @@ module Prelude
4856 -- | ```purescript
4957 -- | flip const 1 2 = const 2 1 = 2
5058 -- | ```
51− -- |
5259 flip :: forall a b c. (a -> b -> c) -> b -> a -> c
5360 flip f b a = f a b
5461
@@ -57,7 +64,6 @@ module Prelude
5764 -- | ```purescript
5865 -- | const 1 "hello" = 1
5966 -- | ```
60− -- |
6167 const :: forall a b. a -> b -> a
6268 const a _ = a
6369
@@ -102,7 +108,6 @@ module Prelude
102108 -- |
103109 -- | - Left Identity: `id <<< p = p`
104110 -- | - Right Identity: `p <<< id = p`
105− -- |
106111 class (Semigroupoid a) <= Category a where
107112 id :: forall t. a t t
108113
@@ -126,7 +131,6 @@ module Prelude
126131 -- |
127132 -- | `($)` is different from [`(#)`](#-2) because it is right-infix instead of left, so
128133 -- | `a $ b $ c $ d x` = `a $ (b $ (c $ (d $ x)))` = `a (b (c (d x)))`
129− -- |
130134 ($) :: forall a b. (a -> b) -> a -> b
131135 ($) f x = f x
132136
@@ -144,7 +148,6 @@ module Prelude
144148 -- |
145149 -- | `(#)` is different from [`($)`](#-1) because it is left-infix instead of right, so
146150 -- | `x # a # b # c # d` = `(((x # a) # b) # c) # d` = `d (c (b (a x)))`
147− -- |
148151 (#) :: forall a b. a -> (a -> b) -> b
149152 (#) x f = f x
150153
@@ -179,22 +182,26 @@ module Prelude
179182 class Show a where
180183 show :: a -> String
181184
182− foreign import showStringImpl
183− """
184− function showStringImpl(s) {
185− return JSON.stringify(s);
186− }
187− """ :: String -> String
185+ instance showBoolean :: Show Boolean where
186+ show true = "true"
187+ show false = "false"
188188
189− instance showUnit :: Show Unit where
190− show (Unit {}) = "Unit {}"
189+ instance showNumber :: Show Number where
190+ show = showNumberImpl
191191
192192 instance showString :: Show String where
193193 show = showStringImpl
194194
195− instance showBoolean :: Show Boolean where
196− show true = "true"
197− show false = "false"
195+ instance showUnit :: Show Unit where
196+ show _ = "unit"
197+
198+ instance showArray :: (Show a) => Show [a] where
199+ show = showArrayImpl show
200+
201+ instance showOrdering :: Show Ordering where
202+ show LT = "LT"
203+ show GT = "GT"
204+ show EQ = "EQ"
198205
199206 foreign import showNumberImpl
200207 """
@@ -203,8 +210,12 @@ module Prelude
203210 }
204211 """ :: Number -> String
205212
206− instance showNumber :: Show Number where
207− show = showNumberImpl
213+ foreign import showStringImpl
214+ """
215+ function showStringImpl(s) {
216+ return JSON.stringify(s);
217+ }
218+ """ :: String -> String
208219
209220 foreign import showArrayImpl
210221 """
@@ -219,9 +230,6 @@ module Prelude
219230 }
220231 """ :: forall a. (a -> String) -> [a] -> String
221232
222− instance showArray :: (Show a) => Show [a] where
223− show = showArrayImpl show
224−
225233 infixl 4 <$>
226234 infixl 1 <#>
227235
@@ -234,10 +242,12 @@ module Prelude
234242 -- |
235243 -- | - Identity: `(<$>) id = id`
236244 -- | - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
237− -- |
238245 class Functor f where
239246 (<$>) :: forall a b. (a -> b) -> f a -> f b
240247
248+ instance functorArr :: Functor ((->) r) where
249+ (<$>) = (<<<)
250+
241251 -- | `(<#>)` is `(<$>)` with its arguments reversed. For example:
242252 -- |
243253 -- | ```purescript
@@ -282,6 +292,9 @@ module Prelude
282292 class (Functor f) <= Apply f where
283293 (<*>) :: forall a b. f (a -> b) -> f a -> f b
284294
295+ instance applyArr :: Apply ((->) r) where
296+ (<*>) f g x = f x (g x)
297+
285298 -- | The `Applicative` type class extends the [`Apply`](#apply) type class with a `pure` function, which can be used to
286299 -- | create values of type `f a` from values of type `a`.
287300 -- |
@@ -295,10 +308,16 @@ module Prelude
295308 -- | - Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
296309 -- | - Homomorphism: `(pure f) <*> (pure x) = pure (f x)`
297310 -- | - Interchange: `u <*> (pure y) = (pure ($ y)) <*> u`
298− -- |
299311 class (Apply f) <= Applicative f where
300312 pure :: forall a. a -> f a
301313
314+ instance applicativeArr :: Applicative ((->) r) where
315+ pure = const
316+
317+ -- | `return` is an alias for `pure`.
318+ return :: forall m a. (Applicative m) => a -> m a
319+ return = pure
320+
302321 -- | `liftA1` provides a default implementation of `(<$>)` for any [`Applicative`](#applicative) functor,
303322 -- | without using `(<$>)` as provided by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass relationship.
304323 -- |
@@ -343,6 +362,9 @@ module Prelude
343362 class (Apply m) <= Bind m where
344363 (>>=) :: forall a b. m a -> (a -> m b) -> m b
345364
365+ instance bindArr :: Bind ((->) r) where
366+ (>>=) m f x = f (m x) x
367+
346368 -- | The `Monad` type class combines the operations of the `Bind` and `Applicative` type classes. Therefore, `Monad` instances
347369 -- | represent type constructors which support sequential composition, and also lifting of functions of arbitrary arity.
348370 -- |
@@ -355,12 +377,9 @@ module Prelude
355377 -- |
356378 -- | - Left Identity: `do { y <- pure x ; f y } = f x`
357379 -- | - Right Identity: `do { y <- x ; pure y } = x`
358− -- |
359380 class (Applicative m, Bind m) <= Monad m
360381
361− -- | `return` is an alias for `pure`.
362− return :: forall m a. (Monad m) => a -> m a
363− return = pure
382+ instance monadArr :: Monad ((->) r)
364383
365384 -- | `liftM1` provides a default implementation of `(<$>)` for any [`Monad`](#monad),
366385 -- | without using `(<$>)` as provided by the [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
@@ -391,26 +410,48 @@ module Prelude
391410 a' <- a
392411 return (f' a')
393412
394− instance functorArr :: Functor ((->) r) where
395− (<$>) = (<<<)
413+ infixr 5 <>
414+ infixr 5 ++
396415
397− instance applyArr :: Apply ((->) r) where
398− (<*>) f g x = f x (g x)
416+ -- | The `Semigroup` type class identifies an associative operation on a type.
417+ -- |
418+ -- | `Semigroup` instances are required to satisfy the following law:
419+ -- |
420+ -- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
421+ -- |
422+ -- | For example, the `String` type is an instance of `Semigroup`, where `(<>)` is defined to be string concatenation.
423+ class Semigroup a where
424+ (<>) :: a -> a -> a
399425
400− instance applicativeArr :: Applicative ((->) r) where
401− pure = const
426+ -- | `(++)` is an alias for `(<>)`.
427+ (++) :: forall s. (Semigroup s) => s -> s -> s
428+ (++) = (<>)
402429
403− instance bindArr :: Bind ((->) r) where
404− (>>=) m f x = f (m x) x
430+ instance semigroupString :: Semigroup String where
431+ (<>) = concatString
405432
406− instance monadArr :: Monad ((->) r)
433+ instance semigroupUnit :: Semigroup Unit where
434+ (<>) _ _ = unit
407435
408− infixl 7 *
409− infixl 7 /
410− infixl 7 %
436+ instance semigroupArr :: (Semigroup s') => Semigroup (s -> s') where
437+ (<>) f g = \x -> f x <> g x
438+
439+ instance semigroupOrdering :: Semigroup Ordering where
440+ (<>) LT _ = LT
441+ (<>) GT _ = GT
442+ (<>) EQ y = y
443+
444+ foreign import concatString
445+ """
446+ function concatString(s1) {
447+ return function(s2) {
448+ return s1 + s2;
449+ };
450+ }
451+ """ :: String -> String -> String
411452
412− infixl 6 -
413453 infixl 6 +
454+ infixl 7 *
414455
415456 -- | Addition and multiplication, satisfying the following laws:
416457 -- |
@@ -418,42 +459,79 @@ module Prelude
418459 -- | - `a` is a monoid under multiplication
419460 -- | - multiplication distributes over addition
420461 -- | - multiplication by `zero` annihilates `a`
421− -- |
422462 class Semiring a where
423463 (+) :: a -> a -> a
424464 zero :: a
425465 (*) :: a -> a -> a
426466 one :: a
427467
468+ instance semiringNumber :: Semiring Number where
469+ (+) = numAdd
470+ zero = 0
471+ (*) = numMul
472+ one = 1
473+
474+ instance semiringUnit :: Semiring Unit where
475+ (+) _ _ = unit
476+ zero = unit
477+ (*) _ _ = unit
478+ one = unit
479+
480+ infixl 7 /
481+
428482 -- | Addition, multiplication, modulo operation and division, satisfying:
429483 -- |
430484 -- | - ```a / b * b + (a `mod` b) = a```
431− -- |
432485 class (Semiring a) <= ModuloSemiring a where
433486 (/) :: a -> a -> a
434487 mod :: a -> a -> a
435488
489+ instance moduloSemiringNumber :: ModuloSemiring Number where
490+ (/) = numDiv
491+ mod _ _ = 0
492+
493+ instance moduloSemiringUnit :: ModuloSemiring Unit where
494+ (/) _ _ = unit
495+ mod _ _ = unit
496+
497+ infixl 6 -
498+
436499 -- | Addition, multiplication, and subtraction.
437500 -- |
438501 -- | Has the same laws as `Semiring` but additionally satisfying:
439502 -- |
440503 -- | - `a` is an abelian group under addition
441− -- |
442504 class (Semiring a) <= Ring a where
443505 (-) :: a -> a -> a
444506
507+ instance ringNumber :: Ring Number where
508+ (-) = numSub
509+
510+ instance ringUnit :: Ring Unit where
511+ (-) _ _ = unit
512+
445513 negate :: forall a. (Ring a) => a -> a
446514 negate a = zero - a
447515
448516 -- | Ring where every nonzero element has a multiplicative inverse so that:
449517 -- |
450518 -- | - ```a `mod` b = zero```
451− -- |
452519 class (Ring a, ModuloSemiring a) <= DivisionRing a
453520
521+ instance divisionRingNumber :: DivisionRing Number
522+
523+ instance divisionRingUnit :: DivisionRing Unit
524+
454525 -- | A commutative field
455526 class (DivisionRing a) <= Num a
456527
528+ instance numNumber :: Num Number
529+
530+ instance numUnit :: Num Unit
531+
532+ infixl 7 %
533+ (%) = numMod
534+
457535 foreign import numAdd
458536 """
459537 function numAdd(n1) {
@@ -463,15 +541,6 @@ module Prelude
463541 }
464542 """ :: Number -> Number -> Number
465543
466− foreign import numSub
467− """
468− function numSub(n1) {
469− return function(n2) {
470− return n1 - n2;
471− };
472− }
473− """ :: Number -> Number -> Number
474−
475544 foreign import numMul
476545 """
477546 function numMul(n1) {
@@ -490,6 +559,15 @@ module Prelude
490559 }
491560 """ :: Number -> Number -> Number
492561
562+ foreign import numSub
563+ """
564+ function numSub(n1) {
565+ return function(n2) {
566+ return n1 - n2;
567+ };
568+ }
569+ """ :: Number -> Number -> Number
570+
493571 foreign import numMod
494572 """
495573 function numMod(n1) {
@@ -499,35 +577,6 @@ module Prelude
499577 }
500578 """ :: Number -> Number -> Number
501579
502− (%) = numMod
503−
504− instance semiringNumber :: Semiring Number where
505− (+) = numAdd
506− zero = 0
507− (*) = numMul
508− one = 1
509−
510− instance ringNumber :: Ring Number where
511− (-) = numSub
512−
513− instance moduloSemiringNumber :: ModuloSemiring Number where
514− (/) = numDiv
515− mod _ _ = 0
516−
517− instance divisionRingNumber :: DivisionRing Number
518−
519− instance numNumber :: Num Number
520−
521− -- | The `Unit` type has a single inhabitant, called `unit`. It represents values with no computational content.
522− -- |
523− -- | `Unit` is often used, wrapped in a monadic type constructor, as the return type of a computation where only
524− -- | the _effects_ are important.
525− newtype Unit = Unit {}
526−
527− -- | `unit` is the sole inhabitant of the `Unit` type.
528− unit :: Unit
529− unit = Unit {}
530−
531580 infix 4 ==
532581 infix 4 /=
533582
@@ -545,6 +594,26 @@ module Prelude
545594 (==) :: a -> a -> Boolean
546595 (/=) :: a -> a -> Boolean
547596
597+ instance eqBoolean :: Eq Boolean where
598+ (==) = refEq
599+ (/=) = refIneq
600+
601+ instance eqNumber :: Eq Number where
602+ (==) = refEq
603+ (/=) = refIneq
604+
605+ instance eqString :: Eq String where
606+ (==) = refEq
607+ (/=) = refIneq
608+
609+ instance eqUnit :: Eq Unit where
610+ (==) _ _ = true
611+ (/=) _ _ = false
612+
613+ instance eqArray :: (Eq a) => Eq [a] where
614+ (==) = eqArrayImpl (==)
615+ (/=) xs ys = not (xs == ys)
616+
548617 foreign import refEq
549618 """
550619 function refEq(r1) {
@@ -563,22 +632,6 @@ module Prelude
563632 }
564633 """ :: forall a. a -> a -> Boolean
565634
566− instance eqUnit :: Eq Unit where
567− (==) (Unit {}) (Unit {}) = true
568− (/=) (Unit {}) (Unit {}) = false
569−
570− instance eqString :: Eq String where
571− (==) = refEq
572− (/=) = refIneq
573−
574− instance eqNumber :: Eq Number where
575− (==) = refEq
576− (/=) = refIneq
577−
578− instance eqBoolean :: Eq Boolean where
579− (==) = refEq
580− (/=) = refIneq
581−
582635 foreign import eqArrayImpl
583636 """
584637 function eqArrayImpl(f) {
@@ -594,10 +647,6 @@ module Prelude
594647 }
595648 """ :: forall a. (a -> a -> Boolean) -> [a] -> [a] -> Boolean
596649
597− instance eqArray :: (Eq a) => Eq [a] where
598− (==) xs ys = eqArrayImpl (==) xs ys
599− (/=) xs ys = not (xs == ys)
600−
601650 -- | The `Ordering` data type represents the three possible outcomes of comparing two values:
602651 -- |
603652 -- | `LT` - The first value is _less than_ the second.
@@ -605,23 +654,6 @@ module Prelude
605654 -- | `EQ` - The first value is _equal to_ or _incomparable to_ the second.
606655 data Ordering = LT | GT | EQ
607656
608− instance eqOrdering :: Eq Ordering where
609− (==) LT LT = true
610− (==) GT GT = true
611− (==) EQ EQ = true
612− (==) _ _ = false
613− (/=) x y = not (x == y)
614−
615− instance showOrdering :: Show Ordering where
616− show LT = "LT"
617− show GT = "GT"
618− show EQ = "EQ"
619−
620− instance semigroupOrdering :: Semigroup Ordering where
621− (<>) LT _ = LT
622− (<>) GT _ = GT
623− (<>) EQ y = y
624−
625657 -- | The `Ord` type class represents types which support comparisons.
626658 -- |
627659 -- | `Ord` instances should satisfy the laws of _partially orderings_:
@@ -629,11 +661,43 @@ module Prelude
629661 -- | - Reflexivity: `a <= a`
630662 -- | - Antisymmetry: if `a <= b` and `b <= a` then `a = b`
631663 -- | - Transitivity: if `a <= b` and `b <= c` then `a <= c`
632− -- |
633664 class (Eq a) <= Ord a where
634665 compare :: a -> a -> Ordering
635666
667+ instance ordBoolean :: Ord Boolean where
668+ compare false false = EQ
669+ compare false true = LT
670+ compare true true = EQ
671+ compare true false = GT
672+
673+ instance ordNumber :: Ord Number where
674+ compare = unsafeCompare
675+
676+ instance ordString :: Ord String where
677+ compare = unsafeCompare
678+
679+ instance ordUnit :: Ord Unit where
680+ compare _ _ = EQ
681+
682+ instance ordArray :: (Ord a) => Ord [a] where
683+ compare [] [] = EQ
684+ compare [] _ = LT
685+ compare _ [] = GT
686+ compare (x:xs) (y:ys) = case compare x y of
687+ EQ -> compare xs ys
688+ other -> other
689+
690+ instance eqOrdering :: Eq Ordering where
691+ (==) LT LT = true
692+ (==) GT GT = true
693+ (==) EQ EQ = true
694+ (==) _ _ = false
695+ (/=) x y = not (x == y)
696+
636697 infixl 4 <
698+ infixl 4 >
699+ infixl 4 <=
700+ infixl 4 >=
637701
638702 -- | Test whether one value is _strictly less than_ another.
639703 (<) :: forall a. (Ord a) => a -> a -> Boolean
@@ -641,30 +705,27 @@ module Prelude
641705 LT -> true
642706 _ -> false
643707
644− infixl 4 >
645−
646708 -- | Test whether one value is _strictly greater than_ another.
647709 (>) :: forall a. (Ord a) => a -> a -> Boolean
648710 (>) a1 a2 = case a1 `compare` a2 of
649711 GT -> true
650712 _ -> false
651713
652− infixl 4 <=
653−
654714 -- | Test whether one value is _non-strictly less than_ another.
655715 (<=) :: forall a. (Ord a) => a -> a -> Boolean
656716 (<=) a1 a2 = case a1 `compare` a2 of
657717 GT -> false
658718 _ -> true
659719
660− infixl 4 >=
661−
662720 -- | Test whether one value is _non-strictly greater than_ another.
663721 (>=) :: forall a. (Ord a) => a -> a -> Boolean
664722 (>=) a1 a2 = case a1 `compare` a2 of
665723 LT -> false
666724 _ -> true
667725
726+ unsafeCompare :: forall a. a -> a -> Ordering
727+ unsafeCompare = unsafeCompareImpl LT EQ GT
728+
668729 foreign import unsafeCompareImpl
669730 """
670731 function unsafeCompareImpl(lt) {
@@ -680,32 +741,6 @@ module Prelude
680741 }
681742 """ :: forall a. Ordering -> Ordering -> Ordering -> a -> a -> Ordering
682743
683− unsafeCompare :: forall a. a -> a -> Ordering
684− unsafeCompare = unsafeCompareImpl LT EQ GT
685−
686− instance ordUnit :: Ord Unit where
687− compare (Unit {}) (Unit {}) = EQ
688−
689− instance ordBoolean :: Ord Boolean where
690− compare false false = EQ
691− compare false true = LT
692− compare true true = EQ
693− compare true false = GT
694−
695− instance ordNumber :: Ord Number where
696− compare = unsafeCompare
697−
698− instance ordString :: Ord String where
699− compare = unsafeCompare
700−
701− instance ordArray :: (Ord a) => Ord [a] where
702− compare [] [] = EQ
703− compare [] _ = LT
704− compare _ [] = GT
705− compare (x:xs) (y:ys) = case compare x y of
706− EQ -> compare xs ys
707− other -> other
708−
709744 -- | The `Bounded` type class represents types that are finite partially
710745 -- | ordered sets.
711746 -- |
@@ -720,6 +755,10 @@ module Prelude
720755 top = true
721756 bottom = false
722757
758+ instance boundedUnit :: Bounded Unit where
759+ top = unit
760+ bottom = unit
761+
723762 -- | The `Lattice` type class represents types that are partially ordered
724763 -- | sets with a supremum (`sup` or `||`) and infimum (`inf` or `&&`).
725764 -- |
@@ -746,6 +785,17 @@ module Prelude
746785 sup = boolOr
747786 inf = boolAnd
748787
788+ infixr 2 ||
789+ infixr 3 &&
790+
791+ -- | The `sup` operator.
792+ (||) :: forall a. (Lattice a) => a -> a -> a
793+ (||) = sup
794+
795+ -- | The `inf` operator.
796+ (&&) :: forall a. (Lattice a) => a -> a -> a
797+ (&&) = inf
798+
749799 -- | The `BoundedLattice` type class represents types that are finite
750800 -- | lattices.
751801 -- |
@@ -799,35 +849,23 @@ module Prelude
799849
800850 instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
801851
802− infixr 2 ||
803−
804− -- | The `sup` operator.
805− (||) :: forall a. (Lattice a) => a -> a -> a
806− (||) = sup
807−
808− infixr 3 &&
809−
810− -- | The `inf` operator.
811− (&&) :: forall a. (Lattice a) => a -> a -> a
812− (&&) = inf
813−
814− foreign import boolAnd
852+ foreign import boolOr
815853 """
816− function boolAnd(b1) {
854+ function boolOr(b1) {
817855 return function(b2) {
818− return b1 && b2;
856+ return b1 || b2;
819857 };
820858 }
821− """ :: Boolean -> Boolean -> Boolean
859+ """ :: Boolean -> Boolean -> Boolean
822860
823− foreign import boolOr
861+ foreign import boolAnd
824862 """
825− function boolOr(b1) {
863+ function boolAnd(b1) {
826864 return function(b2) {
827− return b1 || b2;
865+ return b1 && b2;
828866 };
829867 }
830− """ :: Boolean -> Boolean -> Boolean
868+ """ :: Boolean -> Boolean -> Boolean
831869
832870 foreign import boolNot
833871 """
@@ -836,42 +874,6 @@ module Prelude
836874 }
837875 """ :: Boolean -> Boolean
838876
839− infixr 5 <>
840−
841− -- | The `Semigroup` type class identifies an associative operation on a type.
842− -- |
843− -- | `Semigroup` instances are required to satisfy the following law:
844− -- |
845− -- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
846− -- |
847− -- | For example, the `String` type is an instance of `Semigroup`, where `(<>)` is defined to be string concatenation.
848− class Semigroup a where
849− (<>) :: a -> a -> a
850−
851− foreign import concatString
852− """
853− function concatString(s1) {
854− return function(s2) {
855− return s1 + s2;
856− };
857− }
858− """ :: String -> String -> String
859−
860− instance semigroupUnit :: Semigroup Unit where
861− (<>) (Unit {}) (Unit {}) = Unit {}
862−
863− instance semigroupString :: Semigroup String where
864− (<>) = concatString
865−
866− instance semigroupArr :: (Semigroup s') => Semigroup (s -> s') where
867− (<>) f g = \x -> f x <> g x
868−
869− infixr 5 ++
870−
871− -- | `(++)` is an alias for `(<>)`.
872− (++) :: forall s. (Semigroup s) => s -> s -> s
873− (++) = (<>)
874−
875877 infixl 10 .&.
876878 infixl 10 .|.
877879 infixl 10 .^.
@@ -886,56 +888,65 @@ module Prelude
886888 zshr :: b -> Number -> b
887889 complement :: b -> b
888890
889− foreign import numShl
891+ instance bitsNumber :: Bits Number where
892+ (.&.) = numAnd
893+ (.|.) = numOr
894+ (.^.) = numXor
895+ shl = numShl
896+ shr = numShr
897+ zshr = numZshr
898+ complement = numComplement
899+
900+ foreign import numAnd
890901 """
891− function numShl(n1) {
902+ function numAnd(n1) {
892903 return function(n2) {
893− return n1 << n2;
904+ return n1 & n2;
894905 };
895906 }
896907 """ :: Number -> Number -> Number
897908
898− foreign import numShr
909+ foreign import numOr
899910 """
900− function numShr(n1) {
911+ function numOr(n1) {
901912 return function(n2) {
902− return n1 >> n2;
913+ return n1 | n2;
903914 };
904915 }
905916 """ :: Number -> Number -> Number
906917
907− foreign import numZshr
918+ foreign import numXor
908919 """
909− function numZshr(n1) {
920+ function numXor(n1) {
910921 return function(n2) {
911− return n1 >>> n2;
922+ return n1 ^ n2;
912923 };
913924 }
914925 """ :: Number -> Number -> Number
915926
916− foreign import numAnd
927+ foreign import numShl
917928 """
918− function numAnd(n1) {
929+ function numShl(n1) {
919930 return function(n2) {
920− return n1 & n2;
931+ return n1 << n2;
921932 };
922933 }
923934 """ :: Number -> Number -> Number
924935
925− foreign import numOr
936+ foreign import numShr
926937 """
927− function numOr(n1) {
938+ function numShr(n1) {
928939 return function(n2) {
929− return n1 | n2;
940+ return n1 >> n2;
930941 };
931942 }
932943 """ :: Number -> Number -> Number
933944
934− foreign import numXor
945+ foreign import numZshr
935946 """
936− function numXor(n1) {
947+ function numZshr(n1) {
937948 return function(n2) {
938− return n1 ^ n2;
949+ return n1 >>> n2;
939950 };
940951 }
941952 """ :: Number -> Number -> Number
@@ -947,15 +958,6 @@ module Prelude
947958 }
948959 """ :: Number -> Number
949960
950− instance bitsNumber :: Bits Number where
951− (.&.) = numAnd
952− (.|.) = numOr
953− (.^.) = numXor
954− shl = numShl
955− shr = numShr
956− zshr = numZshr
957− complement = numComplement
958−
959961module Data.Function where
960962
961963 -- | The `on` function is used to change the domain of a binary operator.