@@ -1,872 +0,0 @@
1−module Prelude
2− ( Unit(), unit
3− , ($), (#)
4− , flip
5− , const
6− , asTypeOf
7− , otherwise
8− , Semigroupoid, compose, (<<<), (>>>)
9− , Category, id
10− , Functor, map, (<$>), (<#>), void
11− , Apply, apply, (<*>)
12− , Applicative, pure, liftA1
13− , Bind, bind, (>>=)
14− , Monad, return, liftM1, ap
15− , Semigroup, append, (<>), (++)
16− , Semiring, add, zero, mul, one, (+), (*)
17− , ModuloSemiring, div, mod, (/)
18− , Ring, sub, negate, (-)
19− , Num
20− , DivisionRing
21− , Eq, eq, (==), (/=)
22− , Ordering(..), Ord, compare, (<), (>), (<=), (>=)
23− , unsafeCompare
24− , Bounded, top, bottom
25− , BoundedOrd
26− , BooleanAlgebra, conj, disj, not, (&&), (||)
27− , Show, show
28− ) where
29−
30−-- | The `Unit` type has a single inhabitant, called `unit`. It represents
31−-- | values with no computational content.
32−-- |
33−-- | `Unit` is often used, wrapped in a monadic type constructor, as the
34−-- | return type of a computation where only
35−-- | the _effects_ are important.
36−newtype Unit = Unit {}
37−
38−-- | `unit` is the sole inhabitant of the `Unit` type.
39−unit :: Unit
40−unit = Unit {}
41−
42−infixr 0 $
43−infixl 1 #
44−
45−-- | Applies a function to its argument.
46−-- |
47−-- | ```purescript
48−-- | length $ groupBy productCategory $ filter isInStock $ products
49−-- | ```
50−-- |
51−-- | is equivalent to:
52−-- |
53−-- | ```purescript
54−-- | length (groupBy productCategory (filter isInStock products))
55−-- | ```
56−-- |
57−-- | `($)` is different from [`(#)`](#-2) because it is right-infix instead of
58−-- | left: `a $ b $ c $ d x = a $ (b $ (c $ (d $ x))) = a (b (c (d x)))`
59−($) :: forall a b. (a -> b) -> a -> b
60−($) f x = f x
61−
62−-- | Applies an argument to a function.
63−-- |
64−-- | ```purescript
65−-- | products # filter isInStock # groupBy productCategory # length
66−-- | ```
67−-- |
68−-- | is equivalent to:
69−-- |
70−-- | ```purescript
71−-- | length (groupBy productCategory (filter isInStock products))
72−-- | ```
73−-- |
74−-- | `(#)` is different from [`($)`](#-1) because it is left-infix instead of
75−-- | right: `x # a # b # c # d = (((x # a) # b) # c) # d = d (c (b (a x)))`
76−(#) :: forall a b. a -> (a -> b) -> b
77−(#) x f = f x
78−
79−-- | Flips the order of the arguments to a function of two arguments.
80−-- |
81−-- | ```purescript
82−-- | flip const 1 2 = const 2 1 = 2
83−-- | ```
84−flip :: forall a b c. (a -> b -> c) -> b -> a -> c
85−flip f b a = f a b
86−
87−-- | Returns its first argument and ignores its second.
88−-- |
89−-- | ```purescript
90−-- | const 1 "hello" = 1
91−-- | ```
92−const :: forall a b. a -> b -> a
93−const a _ = a
94−
95−-- | This function returns its first argument, and can be used to assert type
96−-- | equalities. This can be useful when types are otherwise ambiguous.
97−-- |
98−-- | ```purescript
99−-- | main = print $ [] `asTypeOf` [0]
100−-- | ```
101−-- |
102−-- | If instead, we had written `main = print []`, the type of the argument
103−-- | `[]` would have been ambiguous, resulting in a compile-time error.
104−asTypeOf :: forall a. a -> a -> a
105−asTypeOf x _ = x
106−
107−-- | An alias for `true`, which can be useful in guard clauses:
108−-- |
109−-- | ```purescript
110−-- | max x y | x >= y = x
111−-- | | otherwise = y
112−-- | ```
113−otherwise :: Boolean
114−otherwise = true
115−
116−-- | A `Semigroupoid` is similar to a [`Category`](#category) but does not
117−-- | require an identity element `id`, just composable morphisms.
118−-- |
119−-- | `Semigroupoid`s must satisfy the following law:
120−-- |
121−-- | - Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
122−-- |
123−-- | One example of a `Semigroupoid` is the function type constructor `(->)`,
124−-- | with `(<<<)` defined as function composition.
125−class Semigroupoid a where
126− compose :: forall b c d. a c d -> a b c -> a b d
127−
128−instance semigroupoidFn :: Semigroupoid (->) where
129− compose f g x = f (g x)
130−
131−infixr 9 >>>
132−infixr 9 <<<
133−
134−-- | `(<<<)` is an alias for `compose`.
135−(<<<) :: forall a b c d. (Semigroupoid a) => a c d -> a b c -> a b d
136−(<<<) = compose
137−
138−-- | Forwards composition, or `(<<<)` with its arguments reversed.
139−(>>>) :: forall a b c d. (Semigroupoid a) => a b c -> a c d -> a b d
140−(>>>) = flip compose
141−
142−-- | `Category`s consist of objects and composable morphisms between them, and
143−-- | as such are [`Semigroupoids`](#semigroupoid), but unlike `semigroupoids`
144−-- | must have an identity element.
145−-- |
146−-- | Instances must satisfy the following law in addition to the
147−-- | `Semigroupoid` law:
148−-- |
149−-- | - Identity: `id <<< p = p <<< id = p`
150−class (Semigroupoid a) <= Category a where
151− id :: forall t. a t t
152−
153−instance categoryFn :: Category (->) where
154− id x = x
155−
156−-- | A `Functor` is a type constructor which supports a mapping operation
157−-- | `(<$>)`.
158−-- |
159−-- | `(<$>)` can be used to turn functions `a -> b` into functions
160−-- | `f a -> f b` whose argument and return types use the type constructor `f`
161−-- | to represent some computational context.
162−-- |
163−-- | Instances must satisfy the following laws:
164−-- |
165−-- | - Identity: `(<$>) id = id`
166−-- | - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
167−class Functor f where
168− map :: forall a b. (a -> b) -> f a -> f b
169−
170−instance functorFn :: Functor ((->) r) where
171− map = compose
172−
173−instance functorArray :: Functor Array where
174− map = arrayMap
175−
176−foreign import arrayMap :: forall a b. (a -> b) -> Array a -> Array b
177−
178−infixl 4 <$>
179−infixl 1 <#>
180−
181−-- | `(<$>)` is an alias for `map`
182−(<$>) :: forall f a b. (Functor f) => (a -> b) -> f a -> f b
183−(<$>) = map
184−
185−-- | `(<#>)` is `(<$>)` with its arguments reversed. For example:
186−-- |
187−-- | ```purescript
188−-- | [1, 2, 3] <#> \n -> n * n
189−-- | ```
190−(<#>) :: forall f a b. (Functor f) => f a -> (a -> b) -> f b
191−(<#>) fa f = f <$> fa
192−
193−-- | The `void` function is used to ignore the type wrapped by a
194−-- | [`Functor`](#functor), replacing it with `Unit` and keeping only the type
195−-- | information provided by the type constructor itself.
196−-- |
197−-- | `void` is often useful when using `do` notation to change the return type
198−-- | of a monadic computation:
199−-- |
200−-- | ```purescript
201−-- | main = forE 1 10 \n -> void do
202−-- | print n
203−-- | print (n * n)
204−-- | ```
205−void :: forall f a. (Functor f) => f a -> f Unit
206−void fa = const unit <$> fa
207−
208−-- | The `Apply` class provides the `(<*>)` which is used to apply a function
209−-- | to an argument under a type constructor.
210−-- |
211−-- | `Apply` can be used to lift functions of two or more arguments to work on
212−-- | values wrapped with the type constructor `f`. It might also be understood
213−-- | in terms of the `lift2` function:
214−-- |
215−-- | ```purescript
216−-- | lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
217−-- | lift2 f a b = f <$> a <*> b
218−-- | ```
219−-- |
220−-- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
221−-- | the function application operator `($)` to arguments wrapped with the
222−-- | type constructor `f`.
223−-- |
224−-- | Instances must satisfy the following law in addition to the `Functor`
225−-- | laws:
226−-- |
227−-- | - Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
228−-- |
229−-- | Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
230−class (Functor f) <= Apply f where
231− apply :: forall a b. f (a -> b) -> f a -> f b
232−
233−instance applyFn :: Apply ((->) r) where
234− apply f g x = f x (g x)
235−
236−instance applyArray :: Apply Array where
237− apply = ap
238−
239−infixl 4 <*>
240−
241−-- | `(<*>)` is an alias for `apply`.
242−(<*>) :: forall f a b. (Apply f) => f (a -> b) -> f a -> f b
243−(<*>) = apply
244−
245−-- | The `Applicative` type class extends the [`Apply`](#apply) type class
246−-- | with a `pure` function, which can be used to create values of type `f a`
247−-- | from values of type `a`.
248−-- |
249−-- | Where [`Apply`](#apply) provides the ability to lift functions of two or
250−-- | more arguments to functions whose arguments are wrapped using `f`, and
251−-- | [`Functor`](#functor) provides the ability to lift functions of one
252−-- | argument, `pure` can be seen as the function which lifts functions of
253−-- | _zero_ arguments. That is, `Applicative` functors support a lifting
254−-- | operation for any number of function arguments.
255−-- |
256−-- | Instances must satisfy the following laws in addition to the `Apply`
257−-- | laws:
258−-- |
259−-- | - Identity: `(pure id) <*> v = v`
260−-- | - Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
261−-- | - Homomorphism: `(pure f) <*> (pure x) = pure (f x)`
262−-- | - Interchange: `u <*> (pure y) = (pure ($ y)) <*> u`
263−class (Apply f) <= Applicative f where
264− pure :: forall a. a -> f a
265−
266−instance applicativeFn :: Applicative ((->) r) where
267− pure = const
268−
269−instance applicativeArray :: Applicative Array where
270− pure x = [x]
271−
272−-- | `return` is an alias for `pure`.
273−return :: forall m a. (Applicative m) => a -> m a
274−return = pure
275−
276−-- | `liftA1` provides a default implementation of `(<$>)` for any
277−-- | [`Applicative`](#applicative) functor, without using `(<$>)` as provided
278−-- | by the [`Functor`](#functor)-[`Applicative`](#applicative) superclass
279−-- | relationship.
280−-- |
281−-- | `liftA1` can therefore be used to write [`Functor`](#functor) instances
282−-- | as follows:
283−-- |
284−-- | ```purescript
285−-- | instance functorF :: Functor F where
286−-- | map = liftA1
287−-- | ```
288−liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
289−liftA1 f a = pure f <*> a
290−
291−-- | The `Bind` type class extends the [`Apply`](#apply) type class with a
292−-- | "bind" operation `(>>=)` which composes computations in sequence, using
293−-- | the return value of one computation to determine the next computation.
294−-- |
295−-- | The `>>=` operator can also be expressed using `do` notation, as follows:
296−-- |
297−-- | ```purescript
298−-- | x >>= f = do y <- x
299−-- | f y
300−-- | ```
301−-- |
302−-- | where the function argument of `f` is given the name `y`.
303−-- |
304−-- | Instances must satisfy the following law in addition to the `Apply`
305−-- | laws:
306−-- |
307−-- | - Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
308−-- |
309−-- | Associativity tells us that we can regroup operations which use `do`
310−-- | notation so that we can unambiguously write, for example:
311−-- |
312−-- | ```purescript
313−-- | do x <- m1
314−-- | y <- m2 x
315−-- | m3 x y
316−-- | ```
317−class (Apply m) <= Bind m where
318− bind :: forall a b. m a -> (a -> m b) -> m b
319−
320−instance bindFn :: Bind ((->) r) where
321− bind m f x = f (m x) x
322−
323−instance bindArray :: Bind Array where
324− bind = arrayBind
325−
326−foreign import arrayBind :: forall a b. Array a -> (a -> Array b) -> Array b
327−
328−infixl 1 >>=
329−
330−-- | `(>>=)` is an alias for `bind`.
331−(>>=) :: forall m a b. (Bind m) => m a -> (a -> m b) -> m b
332−(>>=) = bind
333−
334−-- | The `Monad` type class combines the operations of the `Bind` and
335−-- | `Applicative` type classes. Therefore, `Monad` instances represent type
336−-- | constructors which support sequential composition, and also lifting of
337−-- | functions of arbitrary arity.
338−-- |
339−-- | Instances must satisfy the following laws in addition to the
340−-- | `Applicative` and `Bind` laws:
341−-- |
342−-- | - Left Identity: `pure x >>= f = f x`
343−-- | - Right Identity: `x >>= pure = x`
344−class (Applicative m, Bind m) <= Monad m
345−
346−instance monadFn :: Monad ((->) r)
347−instance monadArray :: Monad Array
348−
349−-- | `liftM1` provides a default implementation of `(<$>)` for any
350−-- | [`Monad`](#monad), without using `(<$>)` as provided by the
351−-- | [`Functor`](#functor)-[`Monad`](#monad) superclass relationship.
352−-- |
353−-- | `liftM1` can therefore be used to write [`Functor`](#functor) instances
354−-- | as follows:
355−-- |
356−-- | ```purescript
357−-- | instance functorF :: Functor F where
358−-- | map = liftM1
359−-- | ```
360−liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
361−liftM1 f a = do
362− a' <- a
363− return (f a')
364−
365−-- | `ap` provides a default implementation of `(<*>)` for any
366−-- | [`Monad`](#monad), without using `(<*>)` as provided by the
367−-- | [`Apply`](#apply)-[`Monad`](#monad) superclass relationship.
368−-- |
369−-- | `ap` can therefore be used to write [`Apply`](#apply) instances as
370−-- | follows:
371−-- |
372−-- | ```purescript
373−-- | instance applyF :: Apply F where
374−-- | apply = ap
375−-- | ```
376−ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
377−ap f a = do
378− f' <- f
379− a' <- a
380− return (f' a')
381−
382−-- | The `Semigroup` type class identifies an associative operation on a type.
383−-- |
384−-- | Instances are required to satisfy the following law:
385−-- |
386−-- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
387−-- |
388−-- | One example of a `Semigroup` is `String`, with `(<>)` defined as string
389−-- | concatenation.
390−class Semigroup a where
391− append :: a -> a -> a
392−
393−infixr 5 <>
394−infixr 5 ++
395−
396−-- | `(<>)` is an alias for `append`.
397−(<>) :: forall s. (Semigroup s) => s -> s -> s
398−(<>) = append
399−
400−-- | `(++)` is an alternative alias for `append`.
401−(++) :: forall s. (Semigroup s) => s -> s -> s
402−(++) = append
403−
404−instance semigroupString :: Semigroup String where
405− append = concatString
406−
407−instance semigroupUnit :: Semigroup Unit where
408− append _ _ = unit
409−
410−instance semigroupFn :: (Semigroup s') => Semigroup (s -> s') where
411− append f g = \x -> f x <> g x
412−
413−instance semigroupOrdering :: Semigroup Ordering where
414− append LT _ = LT
415− append GT _ = GT
416− append EQ y = y
417−
418−instance semigroupArray :: Semigroup (Array a) where
419− append = concatArray
420−
421−foreign import concatString :: String -> String -> String
422−foreign import concatArray :: forall a. Array a -> Array a -> Array a
423−
424−-- | The `Semiring` class is for types that support an addition and
425−-- | multiplication operation.
426−-- |
427−-- | Instances must satisfy the following laws:
428−-- |
429−-- | - Commutative monoid under addition:
430−-- | - Associativity: `(a + b) + c = a + (b + c)`
431−-- | - Identity: `zero + a = a + zero = a`
432−-- | - Commutative: `a + b = b + a`
433−-- | - Monoid under multiplication:
434−-- | - Associativity: `(a * b) * c = a * (b * c)`
435−-- | - Identity: `one * a = a * one = a`
436−-- | - Multiplication distributes over addition:
437−-- | - Left distributivity: `a * (b + c) = (a * b) + (a * c)`
438−-- | - Right distributivity: `(a + b) * c = (a * c) + (b * c)`
439−-- | - Annihiliation: `zero * a = a * zero = zero`
440−class Semiring a where
441− add :: a -> a -> a
442− zero :: a
443− mul :: a -> a -> a
444− one :: a
445−
446−instance semiringInt :: Semiring Int where
447− add = intAdd
448− zero = 0
449− mul = intMul
450− one = 1
451−
452−instance semiringNumber :: Semiring Number where
453− add = numAdd
454− zero = 0.0
455− mul = numMul
456− one = 1.0
457−
458−instance semiringUnit :: Semiring Unit where
459− add _ _ = unit
460− zero = unit
461− mul _ _ = unit
462− one = unit
463−
464−infixl 6 +
465−infixl 7 *
466−
467−-- | `(+)` is an alias for `add`.
468−(+) :: forall a. (Semiring a) => a -> a -> a
469−(+) = add
470−
471−-- | `(*)` is an alias for `mul`.
472−(*) :: forall a. (Semiring a) => a -> a -> a
473−(*) = mul
474−
475−foreign import intAdd :: Int -> Int -> Int
476−foreign import intMul :: Int -> Int -> Int
477−foreign import numAdd :: Number -> Number -> Number
478−foreign import numMul :: Number -> Number -> Number
479−
480−-- | The `Ring` class is for types that support addition, multiplication,
481−-- | and subtraction operations.
482−-- |
483−-- | Instances must satisfy the following law in addition to the `Semiring`
484−-- | laws:
485−-- |
486−-- | - Additive inverse: `a - a = (zero - a) + a = zero`
487−class (Semiring a) <= Ring a where
488− sub :: a -> a -> a
489−
490−instance ringInt :: Ring Int where
491− sub = intSub
492−
493−instance ringNumber :: Ring Number where
494− sub = numSub
495−
496−instance ringUnit :: Ring Unit where
497− sub _ _ = unit
498−
499−infixl 6 -
500−
501−-- | `(-)` is an alias for `sub`.
502−(-) :: forall a. (Ring a) => a -> a -> a
503−(-) = sub
504−
505−-- | `negate x` can be used as a shorthand for `zero - x`.
506−negate :: forall a. (Ring a) => a -> a
507−negate a = zero - a
508−
509−foreign import intSub :: Int -> Int -> Int
510−foreign import numSub :: Number -> Number -> Number
511−
512−-- | The `ModuloSemiring` class is for types that support addition,
513−-- | multiplication, division, and modulo (division remainder) operations.
514−-- |
515−-- | Instances must satisfy the following law in addition to the `Semiring`
516−-- | laws:
517−-- |
518−-- | - Remainder: ``a / b * b + (a `mod` b) = a``
519−class (Semiring a) <= ModuloSemiring a where
520− div :: a -> a -> a
521− mod :: a -> a -> a
522−
523−instance moduloSemiringInt :: ModuloSemiring Int where
524− div = intDiv
525− mod = intMod
526−
527−instance moduloSemiringNumber :: ModuloSemiring Number where
528− div = numDiv
529− mod _ _ = 0.0
530−
531−instance moduloSemiringUnit :: ModuloSemiring Unit where
532− div _ _ = unit
533− mod _ _ = unit
534−
535−infixl 7 /
536−
537−-- | `(/)` is an alias for `div`.
538−(/) :: forall a. (ModuloSemiring a) => a -> a -> a
539−(/) = div
540−
541−foreign import intDiv :: Int -> Int -> Int
542−foreign import numDiv :: Number -> Number -> Number
543−foreign import intMod :: Int -> Int -> Int
544−
545−-- | A `Ring` where every nonzero element has a multiplicative inverse.
546−-- |
547−-- | Instances must satisfy the following law in addition to the `Ring` and
548−-- | `ModuloSemiring` laws:
549−-- |
550−-- | - Multiplicative inverse: `(one / x) * x = one`
551−-- |
552−-- | As a consequence of this ```a `mod` b = zero``` as no divide operation
553−-- | will have a remainder.
554−class (Ring a, ModuloSemiring a) <= DivisionRing a
555−
556−instance divisionRingNumber :: DivisionRing Number
557−instance divisionRingUnit :: DivisionRing Unit
558−
559−-- | The `Num` class is for types that are commutative fields.
560−-- |
561−-- | Instances must satisfy the following law in addition to the
562−-- | `DivisionRing` laws:
563−-- |
564−-- | - Commutative multiplication: `a * b = b * a`
565−class (DivisionRing a) <= Num a
566−
567−instance numNumber :: Num Number
568−instance numUnit :: Num Unit
569−
570−-- | The `Eq` type class represents types which support decidable equality.
571−-- |
572−-- | `Eq` instances should satisfy the following laws:
573−-- |
574−-- | - Reflexivity: `x == x = true`
575−-- | - Symmetry: `x == y = y == x`
576−-- | - Transitivity: if `x == y` and `y == z` then `x == z`
577−class Eq a where
578− eq :: a -> a -> Boolean
579−
580−infix 4 ==
581−infix 4 /=
582−
583−-- | `(==)` is an alias for `eq`. Tests whether one value is equal to another.
584−(==) :: forall a. (Eq a) => a -> a -> Boolean
585−(==) = eq
586−
587−-- | `(/=)` tests whether one value is _not equal_ to another. Shorthand for
588−-- | `not (x == y)`.
589−(/=) :: forall a. (Eq a) => a -> a -> Boolean
590−(/=) x y = not (x == y)
591−
592−instance eqBoolean :: Eq Boolean where
593− eq = refEq
594−
595−instance eqInt :: Eq Int where
596− eq = refEq
597−
598−instance eqNumber :: Eq Number where
599− eq = refEq
600−
601−instance eqChar :: Eq Char where
602− eq = refEq
603−
604−instance eqString :: Eq String where
605− eq = refEq
606−
607−instance eqUnit :: Eq Unit where
608− eq _ _ = true
609−
610−instance eqArray :: (Eq a) => Eq (Array a) where
611− eq = eqArrayImpl (==)
612−
613−instance eqOrdering :: Eq Ordering where
614− eq LT LT = true
615− eq GT GT = true
616− eq EQ EQ = true
617− eq _ _ = false
618−
619−foreign import refEq :: forall a. a -> a -> Boolean
620−foreign import refIneq :: forall a. a -> a -> Boolean
621−foreign import eqArrayImpl :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Boolean
622−
623−-- | The `Ordering` data type represents the three possible outcomes of
624−-- | comparing two values:
625−-- |
626−-- | `LT` - The first value is _less than_ the second.
627−-- | `GT` - The first value is _greater than_ the second.
628−-- | `EQ` - The first value is _equal to_ the second.
629−data Ordering = LT | GT | EQ
630−
631−-- | The `Ord` type class represents types which support comparisons with a
632−-- | _total order_.
633−-- |
634−-- | `Ord` instances should satisfy the laws of total orderings:
635−-- |
636−-- | - Reflexivity: `a <= a`
637−-- | - Antisymmetry: if `a <= b` and `b <= a` then `a = b`
638−-- | - Transitivity: if `a <= b` and `b <= c` then `a <= c`
639−class (Eq a) <= Ord a where
640− compare :: a -> a -> Ordering
641−
642−instance ordBoolean :: Ord Boolean where
643− compare = unsafeCompare
644−
645−instance ordInt :: Ord Int where
646− compare = unsafeCompare
647−
648−instance ordNumber :: Ord Number where
649− compare = unsafeCompare
650−
651−instance ordString :: Ord String where
652− compare = unsafeCompare
653−
654−instance ordChar :: Ord Char where
655− compare = unsafeCompare
656−
657−instance ordUnit :: Ord Unit where
658− compare _ _ = EQ
659−
660−instance ordArray :: (Ord a) => Ord (Array a) where
661− compare xs ys = compare 0 $ ordArrayImpl (\x y -> case compare x y of
662− EQ -> 0
663− LT -> 1
664− GT -> -1) xs ys
665−
666−foreign import ordArrayImpl :: forall a. (a -> a -> Int) -> Array a -> Array a -> Int
667−
668−instance ordOrdering :: Ord Ordering where
669− compare LT LT = EQ
670− compare EQ EQ = EQ
671− compare GT GT = EQ
672− compare LT _ = LT
673− compare EQ LT = GT
674− compare EQ GT = LT
675− compare GT _ = GT
676−
677−infixl 4 <
678−infixl 4 >
679−infixl 4 <=
680−infixl 4 >=
681−
682−-- | Test whether one value is _strictly less than_ another.
683−(<) :: forall a. (Ord a) => a -> a -> Boolean
684−(<) a1 a2 = case a1 `compare` a2 of
685− LT -> true
686− _ -> false
687−
688−-- | Test whether one value is _strictly greater than_ another.
689−(>) :: forall a. (Ord a) => a -> a -> Boolean
690−(>) a1 a2 = case a1 `compare` a2 of
691− GT -> true
692− _ -> false
693−
694−-- | Test whether one value is _non-strictly less than_ another.
695−(<=) :: forall a. (Ord a) => a -> a -> Boolean
696−(<=) a1 a2 = case a1 `compare` a2 of
697− GT -> false
698− _ -> true
699−
700−-- | Test whether one value is _non-strictly greater than_ another.
701−(>=) :: forall a. (Ord a) => a -> a -> Boolean
702−(>=) a1 a2 = case a1 `compare` a2 of
703− LT -> false
704− _ -> true
705−
706−unsafeCompare :: forall a. a -> a -> Ordering
707−unsafeCompare = unsafeCompareImpl LT EQ GT
708−
709−foreign import unsafeCompareImpl :: forall a. Ordering -> Ordering -> Ordering -> a -> a -> Ordering
710−
711−-- | The `Bounded` type class represents types that are finite.
712−-- |
713−-- | Although there are no "internal" laws for `Bounded`, every value of `a`
714−-- | should be considered less than or equal to `top` by some means, and greater
715−-- | than or equal to `bottom`.
716−-- |
717−-- | The lack of explicit `Ord` constraint allows flexibility in the use of
718−-- | `Bounded` so it can apply to total and partially ordered sets, boolean
719−-- | algebras, etc.
720−class Bounded a where
721− top :: a
722− bottom :: a
723−
724−instance boundedBoolean :: Bounded Boolean where
725− top = true
726− bottom = false
727−
728−instance boundedUnit :: Bounded Unit where
729− top = unit
730− bottom = unit
731−
732−instance boundedOrdering :: Bounded Ordering where
733− top = GT
734− bottom = LT
735−
736−instance boundedInt :: Bounded Int where
737− top = topInt
738− bottom = bottomInt
739−
740−-- | Characters fall within the Unicode range.
741−instance boundedChar :: Bounded Char where
742− top = topChar
743− bottom = bottomChar
744−
745−instance boundedFn :: (Bounded b) => Bounded (a -> b) where
746− top _ = top
747− bottom _ = bottom
748−
749−foreign import topInt :: Int
750−foreign import bottomInt :: Int
751−
752−foreign import topChar :: Char
753−foreign import bottomChar :: Char
754−
755−-- | The `BoundedOrd` type class represents totally ordered finite data types.
756−-- |
757−-- | Instances should satisfy the following law in addition to the `Ord` laws:
758−-- |
759−-- | - Ordering: `bottom <= a <= top`
760−class (Bounded a, Ord a) <= BoundedOrd a
761−
762−instance boundedOrdBoolean :: BoundedOrd Boolean where
763−instance boundedOrdUnit :: BoundedOrd Unit where
764−instance boundedOrdOrdering :: BoundedOrd Ordering where
765−instance boundedOrdInt :: BoundedOrd Int where
766−instance boundedOrdChar :: BoundedOrd Char where
767−
768−-- | The `BooleanAlgebra` type class represents types that behave like boolean
769−-- | values.
770−-- |
771−-- | Instances should satisfy the following laws in addition to the `Bounded`
772−-- | laws:
773−-- |
774−-- | - Associativity:
775−-- | - `a || (b || c) = (a || b) || c`
776−-- | - `a && (b && c) = (a && b) && c`
777−-- | - Commutativity:
778−-- | - `a || b = b || a`
779−-- | - `a && b = b && a`
780−-- | - Distributivity:
781−-- | - `a && (b || c) = (a && b) || (a && c)`
782−-- | - `a || (b && c) = (a || b) && (a || c)`
783−-- | - Identity:
784−-- | - `a || bottom = a`
785−-- | - `a && top = a`
786−-- | - Idempotent:
787−-- | - `a || a = a`
788−-- | - `a && a = a`
789−-- | - Absorption:
790−-- | - `a || (a && b) = a`
791−-- | - `a && (a || b) = a`
792−-- | - Annhiliation:
793−-- | - `a || top = top`
794−-- | - Complementation:
795−-- | - `a && not a = bottom`
796−-- | - `a || not a = top`
797−class (Bounded a) <= BooleanAlgebra a where
798− conj :: a -> a -> a
799− disj :: a -> a -> a
800− not :: a -> a
801−
802−instance booleanAlgebraBoolean :: BooleanAlgebra Boolean where
803− conj = boolAnd
804− disj = boolOr
805− not = boolNot
806−
807−instance booleanAlgebraUnit :: BooleanAlgebra Unit where
808− conj _ _ = unit
809− disj _ _ = unit
810− not _ = unit
811−
812−instance booleanAlgebraFn :: (BooleanAlgebra b) => BooleanAlgebra (a -> b) where
813− conj fx fy a = fx a `conj` fy a
814− disj fx fy a = fx a `disj` fy a
815− not fx a = not (fx a)
816−
817−infixr 3 &&
818−infixr 2 ||
819−
820−-- | `(&&)` is an alias for `conj`.
821−(&&) :: forall a. (BooleanAlgebra a) => a -> a -> a
822−(&&) = conj
823−
824−-- | `(||)` is an alias for `disj`.
825−(||) :: forall a. (BooleanAlgebra a) => a -> a -> a
826−(||) = disj
827−
828−foreign import boolOr :: Boolean -> Boolean -> Boolean
829−foreign import boolAnd :: Boolean -> Boolean -> Boolean
830−foreign import boolNot :: Boolean -> Boolean
831−
832−-- | The `Show` type class represents those types which can be converted into
833−-- | a human-readable `String` representation.
834−-- |
835−-- | While not required, it is recommended that for any expression `x`, the
836−-- | string `show x` be executable PureScript code which evaluates to the same
837−-- | value as the expression `x`.
838−class Show a where
839− show :: a -> String
840−
841−instance showBoolean :: Show Boolean where
842− show true = "true"
843− show false = "false"
844−
845−instance showInt :: Show Int where
846− show = showIntImpl
847−
848−instance showNumber :: Show Number where
849− show = showNumberImpl
850−
851−instance showChar :: Show Char where
852− show = showCharImpl
853−
854−instance showString :: Show String where
855− show = showStringImpl
856−
857−instance showUnit :: Show Unit where
858− show _ = "unit"
859−
860−instance showArray :: (Show a) => Show (Array a) where
861− show = showArrayImpl show
862−
863−instance showOrdering :: Show Ordering where
864− show LT = "LT"
865− show GT = "GT"
866− show EQ = "EQ"
867−
868−foreign import showIntImpl :: Int -> String
869−foreign import showNumberImpl :: Number -> String
870−foreign import showCharImpl :: Char -> String
871−foreign import showStringImpl :: String -> String
872−foreign import showArrayImpl :: forall a. (a -> String) -> Array a -> String