@@ -0,0 +1,872 @@
8+ , Semigroupoid, compose, (<<<), (>>>)
10+ , Functor, map, (<$>), (<#>), void
11+ , Apply, apply, (<*>)
12+ , Applicative, pure, liftA1
14+ , Monad, return, liftM1, ap
15+ , Semigroup, append, (<>), (++)
16+ , Semiring, add, zero, mul, one, (+), (*)
17+ , ModuloSemiring, div, mod, (/)
18+ , Ring, sub, negate, (-)
22+ , Ordering(..), Ord, compare, (<), (>), (<=), (>=)
24+ , Bounded, top, bottom
26+ , BooleanAlgebra, conj, disj, not, (&&), (||)
30+-- | The `Unit` type has a single inhabitant, called `unit`. It represents
31+-- | values with no computational content.
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 {}
38+-- | `unit` is the sole inhabitant of the `Unit` type.
45+-- | Applies a function to its argument.
48+-- | length $ groupBy productCategory $ filter isInStock $ products
51+-- | is equivalent to:
54+-- | length (groupBy productCategory (filter isInStock products))
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
62+-- | Applies an argument to a function.
65+-- | products # filter isInStock # groupBy productCategory # length
68+-- | is equivalent to:
71+-- | length (groupBy productCategory (filter isInStock products))
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
79+-- | Flips the order of the arguments to a function of two arguments.
82+-- | flip const 1 2 = const 2 1 = 2
84+flip :: forall a b c. (a -> b -> c) -> b -> a -> c
87+-- | Returns its first argument and ignores its second.
90+-- | const 1 "hello" = 1
92+const :: forall a b. a -> b -> a
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.
99+-- | main = print $ [] `asTypeOf` [0]
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
107+-- | An alias for `true`, which can be useful in guard clauses:
110+-- | max x y | x >= y = x
116+-- | A `Semigroupoid` is similar to a [`Category`](#category) but does not
117+-- | require an identity element `id`, just composable morphisms.
119+-- | `Semigroupoid`s must satisfy the following law:
121+-- | - Associativity: `p <<< (q <<< r) = (p <<< q) <<< r`
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
128+instance semigroupoidFn :: Semigroupoid (->) where
129+ compose f g x = f (g x)
134+-- | `(<<<)` is an alias for `compose`.
135+(<<<) :: forall a b c d. (Semigroupoid a) => a c d -> a b c -> a b d
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
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.
146+-- | Instances must satisfy the following law in addition to the
147+-- | `Semigroupoid` law:
149+-- | - Identity: `id <<< p = p <<< id = p`
150+class (Semigroupoid a) <= Category a where
151+ id :: forall t. a t t
153+instance categoryFn :: Category (->) where
156+-- | A `Functor` is a type constructor which supports a mapping operation
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.
163+-- | Instances must satisfy the following laws:
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
170+instance functorFn :: Functor ((->) r) where
173+instance functorArray :: Functor Array where
176+foreign import arrayMap :: forall a b. (a -> b) -> Array a -> Array b
181+-- | `(<$>)` is an alias for `map`
182+(<$>) :: forall f a b. (Functor f) => (a -> b) -> f a -> f b
185+-- | `(<#>)` is `(<$>)` with its arguments reversed. For example:
188+-- | [1, 2, 3] <#> \n -> n * n
190+(<#>) :: forall f a b. (Functor f) => f a -> (a -> b) -> f b
191+(<#>) fa f = f <$> fa
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.
197+-- | `void` is often useful when using `do` notation to change the return type
198+-- | of a monadic computation:
201+-- | main = forE 1 10 \n -> void do
205+void :: forall f a. (Functor f) => f a -> f Unit
206+void fa = const unit <$> fa
208+-- | The `Apply` class provides the `(<*>)` which is used to apply a function
209+-- | to an argument under a type constructor.
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:
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
220+-- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
221+-- | the function application operator `($)` to arguments wrapped with the
222+-- | type constructor `f`.
224+-- | Instances must satisfy the following law in addition to the `Functor`
227+-- | - Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
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
233+instance applyFn :: Apply ((->) r) where
234+ apply f g x = f x (g x)
236+instance applyArray :: Apply Array where
241+-- | `(<*>)` is an alias for `apply`.
242+(<*>) :: forall f a b. (Apply f) => f (a -> b) -> f a -> f b
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`.
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.
256+-- | Instances must satisfy the following laws in addition to the `Apply`
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
266+instance applicativeFn :: Applicative ((->) r) where
269+instance applicativeArray :: Applicative Array where
272+-- | `return` is an alias for `pure`.
273+return :: forall m a. (Applicative m) => a -> m a
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
281+-- | `liftA1` can therefore be used to write [`Functor`](#functor) instances
285+-- | instance functorF :: Functor F where
288+liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
289+liftA1 f a = pure f <*> a
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.
295+-- | The `>>=` operator can also be expressed using `do` notation, as follows:
298+-- | x >>= f = do y <- x
302+-- | where the function argument of `f` is given the name `y`.
304+-- | Instances must satisfy the following law in addition to the `Apply`
307+-- | - Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
309+-- | Associativity tells us that we can regroup operations which use `do`
310+-- | notation so that we can unambiguously write, for example:
317+class (Apply m) <= Bind m where
318+ bind :: forall a b. m a -> (a -> m b) -> m b
320+instance bindFn :: Bind ((->) r) where
321+ bind m f x = f (m x) x
323+instance bindArray :: Bind Array where
326+foreign import arrayBind :: forall a b. Array a -> (a -> Array b) -> Array b
330+-- | `(>>=)` is an alias for `bind`.
331+(>>=) :: forall m a b. (Bind m) => m a -> (a -> m b) -> m b
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.
339+-- | Instances must satisfy the following laws in addition to the
340+-- | `Applicative` and `Bind` laws:
342+-- | - Left Identity: `pure x >>= f = f x`
343+-- | - Right Identity: `x >>= pure = x`
344+class (Applicative m, Bind m) <= Monad m
346+instance monadFn :: Monad ((->) r)
347+instance monadArray :: Monad Array
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.
353+-- | `liftM1` can therefore be used to write [`Functor`](#functor) instances
357+-- | instance functorF :: Functor F where
360+liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
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.
369+-- | `ap` can therefore be used to write [`Apply`](#apply) instances as
373+-- | instance applyF :: Apply F where
376+ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
382+-- | The `Semigroup` type class identifies an associative operation on a type.
384+-- | Instances are required to satisfy the following law:
386+-- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
388+-- | One example of a `Semigroup` is `String`, with `(<>)` defined as string
390+class Semigroup a where
391+ append :: a -> a -> a
396+-- | `(<>)` is an alias for `append`.
397+(<>) :: forall s. (Semigroup s) => s -> s -> s
400+-- | `(++)` is an alternative alias for `append`.
401+(++) :: forall s. (Semigroup s) => s -> s -> s
404+instance semigroupString :: Semigroup String where
405+ append = concatString
407+instance semigroupUnit :: Semigroup Unit where
410+instance semigroupFn :: (Semigroup s') => Semigroup (s -> s') where
411+ append f g = \x -> f x <> g x
413+instance semigroupOrdering :: Semigroup Ordering where
418+instance semigroupArray :: Semigroup (Array a) where
419+ append = concatArray
421+foreign import concatString :: String -> String -> String
422+foreign import concatArray :: forall a. Array a -> Array a -> Array a
424+-- | The `Semiring` class is for types that support an addition and
425+-- | multiplication operation.
427+-- | Instances must satisfy the following laws:
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
446+instance semiringInt :: Semiring Int where
452+instance semiringNumber :: Semiring Number where
458+instance semiringUnit :: Semiring Unit where
467+-- | `(+)` is an alias for `add`.
468+(+) :: forall a. (Semiring a) => a -> a -> a
471+-- | `(*)` is an alias for `mul`.
472+(*) :: forall a. (Semiring a) => a -> a -> a
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
480+-- | The `Ring` class is for types that support addition, multiplication,
481+-- | and subtraction operations.
483+-- | Instances must satisfy the following law in addition to the `Semiring`
486+-- | - Additive inverse: `a - a = (zero - a) + a = zero`
487+class (Semiring a) <= Ring a where
490+instance ringInt :: Ring Int where
493+instance ringNumber :: Ring Number where
496+instance ringUnit :: Ring Unit where
501+-- | `(-)` is an alias for `sub`.
502+(-) :: forall a. (Ring a) => a -> a -> a
505+-- | `negate x` can be used as a shorthand for `zero - x`.
506+negate :: forall a. (Ring a) => a -> a
509+foreign import intSub :: Int -> Int -> Int
510+foreign import numSub :: Number -> Number -> Number
512+-- | The `ModuloSemiring` class is for types that support addition,
513+-- | multiplication, division, and modulo (division remainder) operations.
515+-- | Instances must satisfy the following law in addition to the `Semiring`
518+-- | - Remainder: ``a / b * b + (a `mod` b) = a``
519+class (Semiring a) <= ModuloSemiring a where
523+instance moduloSemiringInt :: ModuloSemiring Int where
527+instance moduloSemiringNumber :: ModuloSemiring Number where
531+instance moduloSemiringUnit :: ModuloSemiring Unit where
537+-- | `(/)` is an alias for `div`.
538+(/) :: forall a. (ModuloSemiring a) => a -> a -> a
541+foreign import intDiv :: Int -> Int -> Int
542+foreign import numDiv :: Number -> Number -> Number
543+foreign import intMod :: Int -> Int -> Int
545+-- | A `Ring` where every nonzero element has a multiplicative inverse.
547+-- | Instances must satisfy the following law in addition to the `Ring` and
548+-- | `ModuloSemiring` laws:
550+-- | - Multiplicative inverse: `(one / x) * x = one`
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
556+instance divisionRingNumber :: DivisionRing Number
557+instance divisionRingUnit :: DivisionRing Unit
559+-- | The `Num` class is for types that are commutative fields.
561+-- | Instances must satisfy the following law in addition to the
562+-- | `DivisionRing` laws:
564+-- | - Commutative multiplication: `a * b = b * a`
565+class (DivisionRing a) <= Num a
567+instance numNumber :: Num Number
568+instance numUnit :: Num Unit
570+-- | The `Eq` type class represents types which support decidable equality.
572+-- | `Eq` instances should satisfy the following laws:
574+-- | - Reflexivity: `x == x = true`
575+-- | - Symmetry: `x == y = y == x`
576+-- | - Transitivity: if `x == y` and `y == z` then `x == z`
578+ eq :: a -> a -> Boolean
583+-- | `(==)` is an alias for `eq`. Tests whether one value is equal to another.
584+(==) :: forall a. (Eq a) => a -> a -> Boolean
587+-- | `(/=)` tests whether one value is _not equal_ to another. Shorthand for
589+(/=) :: forall a. (Eq a) => a -> a -> Boolean
590+(/=) x y = not (x == y)
592+instance eqBoolean :: Eq Boolean where
595+instance eqInt :: Eq Int where
598+instance eqNumber :: Eq Number where
601+instance eqChar :: Eq Char where
604+instance eqString :: Eq String where
607+instance eqUnit :: Eq Unit where
610+instance eqArray :: (Eq a) => Eq (Array a) where
611+ eq = eqArrayImpl (==)
613+instance eqOrdering :: Eq Ordering where
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
623+-- | The `Ordering` data type represents the three possible outcomes of
624+-- | comparing two values:
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
631+-- | The `Ord` type class represents types which support comparisons with a
634+-- | `Ord` instances should satisfy the laws of total orderings:
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
642+instance ordBoolean :: Ord Boolean where
643+ compare = unsafeCompare
645+instance ordInt :: Ord Int where
646+ compare = unsafeCompare
648+instance ordNumber :: Ord Number where
649+ compare = unsafeCompare
651+instance ordString :: Ord String where
652+ compare = unsafeCompare
654+instance ordChar :: Ord Char where
655+ compare = unsafeCompare
657+instance ordUnit :: Ord Unit where
660+instance ordArray :: (Ord a) => Ord (Array a) where
661+ compare xs ys = compare 0 $ ordArrayImpl (\x y -> case compare x y of
666+foreign import ordArrayImpl :: forall a. (a -> a -> Int) -> Array a -> Array a -> Int
668+instance ordOrdering :: Ord Ordering where
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
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
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
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
706+unsafeCompare :: forall a. a -> a -> Ordering
707+unsafeCompare = unsafeCompareImpl LT EQ GT
709+foreign import unsafeCompareImpl :: forall a. Ordering -> Ordering -> Ordering -> a -> a -> Ordering
711+-- | The `Bounded` type class represents types that are finite.
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`.
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
720+class Bounded a where
724+instance boundedBoolean :: Bounded Boolean where
728+instance boundedUnit :: Bounded Unit where
732+instance boundedOrdering :: Bounded Ordering where
736+instance boundedInt :: Bounded Int where
740+-- | Characters fall within the Unicode range.
741+instance boundedChar :: Bounded Char where
745+instance boundedFn :: (Bounded b) => Bounded (a -> b) where
749+foreign import topInt :: Int
750+foreign import bottomInt :: Int
752+foreign import topChar :: Char
753+foreign import bottomChar :: Char
755+-- | The `BoundedOrd` type class represents totally ordered finite data types.
757+-- | Instances should satisfy the following law in addition to the `Ord` laws:
759+-- | - Ordering: `bottom <= a <= top`
760+class (Bounded a, Ord a) <= BoundedOrd a
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
768+-- | The `BooleanAlgebra` type class represents types that behave like boolean
771+-- | Instances should satisfy the following laws in addition to the `Bounded`
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)`
784+-- | - `a || bottom = a`
785+-- | - `a && top = a`
790+-- | - `a || (a && b) = a`
791+-- | - `a && (a || b) = a`
793+-- | - `a || top = top`
794+-- | - Complementation:
795+-- | - `a && not a = bottom`
796+-- | - `a || not a = top`
797+class (Bounded a) <= BooleanAlgebra a where
802+instance booleanAlgebraBoolean :: BooleanAlgebra Boolean where
807+instance booleanAlgebraUnit :: BooleanAlgebra Unit where
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)
820+-- | `(&&)` is an alias for `conj`.
821+(&&) :: forall a. (BooleanAlgebra a) => a -> a -> a
824+-- | `(||)` is an alias for `disj`.
825+(||) :: forall a. (BooleanAlgebra a) => a -> a -> a
828+foreign import boolOr :: Boolean -> Boolean -> Boolean
829+foreign import boolAnd :: Boolean -> Boolean -> Boolean
830+foreign import boolNot :: Boolean -> Boolean
832+-- | The `Show` type class represents those types which can be converted into
833+-- | a human-readable `String` representation.
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`.
841+instance showBoolean :: Show Boolean where
843+ show false = "false"
845+instance showInt :: Show Int where
848+instance showNumber :: Show Number where
849+ show = showNumberImpl
851+instance showChar :: Show Char where
854+instance showString :: Show String where
855+ show = showStringImpl
857+instance showUnit :: Show Unit where
860+instance showArray :: (Show a) => Show (Array a) where
861+ show = showArrayImpl show
863+instance showOrdering :: Show Ordering where
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