@@ -0,0 +1,1525 @@
1+module Prelude
2+ ( Unit(..), unit
3+ , ($), (#)
4+ , flip
5+ , const
6+ , asTypeOf
7+ , otherwise
8+ , (:), cons
9+ , Semigroupoid, (<<<), (>>>)
10+ , Category, id
11+ , Functor, (<$>), (<#>), void
12+ , Apply, (<*>)
13+ , Applicative, pure, liftA1
14+ , Bind, (>>=)
15+ , Monad, return, liftM1, ap
16+ , Semigroup, (<>), (++)
17+ , Semiring, (+), zero, (*), one
18+ , ModuloSemiring, (/), mod
19+ , Ring, (-), negate
20+ , Num
21+ , DivisionRing
22+ , Eq, (==), (/=)
23+ , Ordering(..), Ord, compare, (<), (>), (<=), (>=)
24+ , Bounded, top, bottom
25+ , Lattice, sup, inf, (||), (&&)
26+ , BoundedLattice
27+ , ComplementedLattice, not
28+ , DistributiveLattice
29+ , BooleanAlgebra
30+ , Show, show
31+ ) where
32+
33+ -- | The `Unit` type has a single inhabitant, called `unit`. It represents
34+ -- | values with no computational content.
35+ -- |
36+ -- | `Unit` is often used, wrapped in a monadic type constructor, as the
37+ -- | 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+
45+ infixr 0 $
46+ infixl 0 #
47+
48+ -- | Applies a function to its argument.
49+ -- |
50+ -- | ```purescript
51+ -- | length $ groupBy productCategory $ filter isInStock $ products
52+ -- | ```
53+ -- |
54+ -- | is equivalent to:
55+ -- |
56+ -- | ```purescript
57+ -- | length (groupBy productCategory (filter isInStock products))
58+ -- | ```
59+ -- |
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)))`
62+ ($) :: forall a b. (a -> b) -> a -> b
63+ ($) f x = f x
64+
65+ -- | Applies an argument to a function.
66+ -- |
67+ -- | ```purescript
68+ -- | products # filter isInStock # groupBy productCategory # length
69+ -- | ```
70+ -- |
71+ -- | is equivalent to:
72+ -- |
73+ -- | ```purescript
74+ -- | length (groupBy productCategory (filter isInStock products))
75+ -- | ```
76+ -- |
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)))`
79+ (#) :: forall a b. a -> (a -> b) -> b
80+ (#) x f = f x
81+
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
89+
90+ -- | Returns its first argument and ignores its second.
91+ -- |
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
118+
119+ -- | Attaches an element to the front of an array, creating a new array.
120+ -- |
121+ -- | ```purescript
122+ -- | cons 1 [2, 3, 4] = [1, 2, 3, 4]
123+ -- | ```
124+ -- |
125+ -- | Note, the running time of this function is `O(n)`.
126+ foreign import cons
127+ """
128+ function cons(e) {
129+ return function(l) {
130+ return [e].concat(l);
131+ };
132+ }
133+ """ :: forall a. a -> [a] -> [a]
134+
135+ infixr 6 :
136+
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
142+
143+ infixr 9 >>>
144+ infixr 9 <<<
145+
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
157+
158+ instance semigroupoidArr :: Semigroupoid (->) where
159+ (<<<) f g x = f (g x)
160+
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
164+
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
175+
176+ instance categoryArr :: Category (->) where
177+ id x = x
178+
179+ infixl 4 <$>
180+ infixl 1 <#>
181+
182+ -- | A `Functor` is a type constructor which supports a mapping operation
183+ -- | `(<$>)`.
184+ -- |
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.
188+ -- |
189+ -- | Instances must satisfy the following laws:
190+ -- |
191+ -- | - Identity: `(<$>) id = id`
192+ -- | - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)`
193+ class Functor f where
194+ (<$>) :: forall a b. (a -> b) -> f a -> f b
195+
196+ instance functorArr :: Functor ((->) r) where
197+ (<$>) = (<<<)
198+
199+ -- | `(<#>)` is `(<$>)` with its arguments reversed. For example:
200+ -- |
201+ -- | ```purescript
202+ -- | [1, 2, 3] <#> \n -> n * n
203+ -- | ```
204+ (<#>) :: forall f a b. (Functor f) => f a -> (a -> b) -> f b
205+ (<#>) fa f = f <$> fa
206+
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.
210+ -- |
211+ -- | `void` is often useful when using `do` notation to change the return type
212+ -- | of a monadic computation:
213+ -- |
214+ -- | ```purescript
215+ -- | main = forE 1 10 \n -> void do
216+ -- | print n
217+ -- | print (n * n)
218+ -- | ```
219+ void :: forall f a. (Functor f) => f a -> f Unit
220+ void fa = const unit <$> fa
221+
222+ infixl 4 <*>
223+
224+ -- | The `Apply` class provides the `(<*>)` which is used to apply a function
225+ -- | to an argument under a type constructor.
226+ -- |
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:
230+ -- |
231+ -- | ```purescript
232+ -- | lift2 :: forall f a b c. (Apply f) => (a -> b -> c) -> f a -> f b -> f c
233+ -- | lift2 f a b = f <$> a <*> b
234+ -- | ```
235+ -- |
236+ -- | `(<*>)` is recovered from `lift2` as `lift2 ($)`. That is, `(<*>)` lifts
237+ -- | the function application operator `($)` to arguments wrapped with the
238+ -- | type constructor `f`.
239+ -- |
240+ -- | Instances must satisfy the following law in addition to the `Functor`
241+ -- | laws:
242+ -- |
243+ -- | - Associative composition: `(<<<) <$> f <*> g <*> h = f <*> (g <*> h)`
244+ -- |
245+ -- | Formally, `Apply` represents a strong lax semi-monoidal endofunctor.
246+ class (Functor f) <= Apply f where
247+ (<*>) :: forall a b. f (a -> b) -> f a -> f b
248+
249+ instance applyArr :: Apply ((->) r) where
250+ (<*>) f g x = f x (g x)
251+
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`.
255+ -- |
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.
262+ -- |
263+ -- | Instances must satisfy the following laws in addition to the `Apply`
264+ -- | laws:
265+ -- |
266+ -- | - Identity: `(pure id) <*> v = v`
267+ -- | - Composition: `(pure <<<) <*> f <*> g <*> h = f <*> (g <*> h)`
268+ -- | - Homomorphism: `(pure f) <*> (pure x) = pure (f x)`
269+ -- | - Interchange: `u <*> (pure y) = (pure ($ y)) <*> u`
270+ class (Apply f) <= Applicative f where
271+ pure :: forall a. a -> f a
272+
273+ instance applicativeArr :: Applicative ((->) r) where
274+ pure = const
275+
276+ -- | `return` is an alias for `pure`.
277+ return :: forall m a. (Applicative m) => a -> m a
278+ return = pure
279+
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.
284+ -- |
285+ -- | `liftA1` can therefore be used to write [`Functor`](#functor) instances
286+ -- | as follows:
287+ -- |
288+ -- | ```purescript
289+ -- | instance functorF :: Functor F where
290+ -- | (<$>) = liftA1
291+ -- | ```
292+ liftA1 :: forall f a b. (Applicative f) => (a -> b) -> f a -> f b
293+ liftA1 f a = pure f <*> a
294+
295+ infixl 1 >>=
296+
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.
300+ -- |
301+ -- | The `>>=` operator can also be expressed using `do` notation, as follows:
302+ -- |
303+ -- | ```purescript
304+ -- | x >>= f = do y <- x
305+ -- | f y
306+ -- | ```
307+ -- |
308+ -- | where the function argument of `f` is given the name `y`.
309+ -- |
310+ -- | Instances must satisfy the following law in addition to the `Apply`
311+ -- | laws:
312+ -- |
313+ -- | - Associativity: `(x >>= f) >>= g = x >>= (\k => f k >>= g)`
314+ -- |
315+ -- | Associativity tells us that we can regroup operations which use `do`
316+ -- | notation so that we can unambiguously write, for example:
317+ -- |
318+ -- | ```purescript
319+ -- | do x <- m1
320+ -- | y <- m2 x
321+ -- | m3 x y
322+ -- | ```
323+ class (Apply m) <= Bind m where
324+ (>>=) :: forall a b. m a -> (a -> m b) -> m b
325+
326+ instance bindArr :: Bind ((->) r) where
327+ (>>=) m f x = f (m x) x
328+
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.
333+ -- |
334+ -- | Instances must satisfy the following laws in addition to the
335+ -- | `Applicative` and `Bind` laws:
336+ -- |
337+ -- | - Left Identity: `pure x >>= f = f x`
338+ -- | - Right Identity: `x >>= pure = x`
339+ class (Applicative m, Bind m) <= Monad m
340+
341+ instance monadArr :: Monad ((->) r)
342+
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.
346+ -- |
347+ -- | `liftM1` can therefore be used to write [`Functor`](#functor) instances
348+ -- | as follows:
349+ -- |
350+ -- | ```purescript
351+ -- | instance functorF :: Functor F where
352+ -- | (<$>) = liftM1
353+ -- | ```
354+ liftM1 :: forall m a b. (Monad m) => (a -> b) -> m a -> m b
355+ liftM1 f a = do
356+ a' <- a
357+ return (f a')
358+
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.
362+ -- |
363+ -- | `ap` can therefore be used to write [`Apply`](#apply) instances as
364+ -- | follows:
365+ -- |
366+ -- | ```purescript
367+ -- | instance applyF :: Apply F where
368+ -- | (<*>) = ap
369+ -- | ```
370+ ap :: forall m a b. (Monad m) => m (a -> b) -> m a -> m b
371+ ap f a = do
372+ f' <- f
373+ a' <- a
374+ return (f' a')
375+
376+ infixr 5 <>
377+ infixr 5 ++
378+
379+ -- | The `Semigroup` type class identifies an associative operation on a type.
380+ -- |
381+ -- | Instances are required to satisfy the following law:
382+ -- |
383+ -- | - Associativity: `(x <> y) <> z = x <> (y <> z)`
384+ -- |
385+ -- | One example of a `Semigroup` is `String`, with `(<>)` defined as string
386+ -- | concatenation.
387+ class Semigroup a where
388+ (<>) :: a -> a -> a
389+
390+ -- | `(++)` is an alias for `(<>)`.
391+ (++) :: forall s. (Semigroup s) => s -> s -> s
392+ (++) = (<>)
393+
394+ instance semigroupString :: Semigroup String where
395+ (<>) = concatString
396+
397+ instance semigroupUnit :: Semigroup Unit where
398+ (<>) _ _ = unit
399+
400+ instance semigroupArr :: (Semigroup s') => Semigroup (s -> s') where
401+ (<>) f g = \x -> f x <> g x
402+
403+ instance semigroupOrdering :: Semigroup Ordering where
404+ (<>) LT _ = LT
405+ (<>) GT _ = GT
406+ (<>) EQ y = y
407+
408+ foreign import concatString
409+ """
410+ function concatString(s1) {
411+ return function(s2) {
412+ return s1 + s2;
413+ };
414+ }
415+ """ :: String -> String -> String
416+
417+ infixl 6 +
418+ infixl 7 *
419+
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`
436+ class Semiring a where
437+ (+) :: a -> a -> a
438+ zero :: a
439+ (*) :: a -> a -> a
440+ one :: a
441+
442+ instance semiringNumber :: Semiring Number where
443+ (+) = numAdd
444+ zero = 0
445+ (*) = numMul
446+ one = 1
447+
448+ instance semiringUnit :: Semiring Unit where
449+ (+) _ _ = unit
450+ zero = unit
451+ (*) _ _ = unit
452+ one = unit
453+
454+ infixl 6 -
455+
456+ -- | The `Ring` class is for types that support addition, multiplication,
457+ -- | and subtraction operations.
458+ -- |
459+ -- | Instances must satisfy the following law in addition to the `Semiring`
460+ -- | laws:
461+ -- |
462+ -- | - Additive inverse: `a + (-a) = (-a) + a = zero`
463+ class (Semiring a) <= Ring a where
464+ (-) :: a -> a -> a
465+
466+ instance ringNumber :: Ring Number where
467+ (-) = numSub
468+
469+ instance ringUnit :: Ring Unit where
470+ (-) _ _ = unit
471+
472+ negate :: forall a. (Ring a) => a -> a
473+ negate a = zero - a
474+
475+ infixl 7 /
476+
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:
482+ -- |
483+ -- | - Remainder: `a / b * b + (a `mod` b) = a`
484+ class (Semiring a) <= ModuloSemiring a where
485+ (/) :: a -> a -> a
486+ mod :: a -> a -> a
487+
488+ instance moduloSemiringNumber :: ModuloSemiring Number where
489+ (/) = numDiv
490+ mod _ _ = 0
491+
492+ instance moduloSemiringUnit :: ModuloSemiring Unit where
493+ (/) _ _ = unit
494+ mod _ _ = unit
495+
496+ -- | A `Ring` where every nonzero element has a multiplicative inverse.
497+ -- |
498+ -- | Instances must satisfy the following law in addition to the `Ring` and
499+ -- | `ModuloSemiring` laws:
500+ -- |
501+ -- | - Multiplicative inverse: `(one / x) * x = one`
502+ -- |
503+ -- | As a consequence of this ```a `mod` b = zero``` as no divide operation
504+ -- | will have a remainder.
505+ class (Ring a, ModuloSemiring a) <= DivisionRing a
506+
507+ instance divisionRingNumber :: DivisionRing Number
508+
509+ instance divisionRingUnit :: DivisionRing Unit
510+
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`
517+ class (DivisionRing a) <= Num a
518+
519+ instance numNumber :: Num Number
520+
521+ instance numUnit :: Num Unit
522+
523+ foreign import numAdd
524+ """
525+ function numAdd(n1) {
526+ return function(n2) {
527+ return n1 + n2;
528+ };
529+ }
530+ """ :: Number -> Number -> Number
531+
532+ foreign import numMul
533+ """
534+ function numMul(n1) {
535+ return function(n2) {
536+ return n1 * n2;
537+ };
538+ }
539+ """ :: Number -> Number -> Number
540+
541+ foreign import numDiv
542+ """
543+ function numDiv(n1) {
544+ return function(n2) {
545+ return n1 / n2;
546+ };
547+ }
548+ """ :: Number -> Number -> Number
549+
550+ foreign import numSub
551+ """
552+ function numSub(n1) {
553+ return function(n2) {
554+ return n1 - n2;
555+ };
556+ }
557+ """ :: Number -> Number -> Number
558+
559+ infix 4 ==
560+ infix 4 /=
561+
562+ -- | The `Eq` type class represents types which support decidable equality.
563+ -- |
564+ -- | `Eq` instances should satisfy the following laws:
565+ -- |
566+ -- | - Reflexivity: `x == x = true`
567+ -- | - Symmetry: `x == y = y == x`
568+ -- | - Transitivity: if `x == y` and `y == z` then `x == z`
569+ -- | - Negation: `x /= y = not (x == y)`
570+ -- |
571+ -- | `(/=)` may be implemented in terms of `(==)`, but it might give a performance improvement to implement it separately.
572+ class Eq a where
573+ (==) :: a -> a -> Boolean
574+ (/=) :: a -> a -> Boolean
575+
576+ instance eqBoolean :: Eq Boolean where
577+ (==) = refEq
578+ (/=) = refIneq
579+
580+ instance eqNumber :: Eq Number where
581+ (==) = refEq
582+ (/=) = refIneq
583+
584+ instance eqString :: Eq String where
585+ (==) = refEq
586+ (/=) = refIneq
587+
588+ instance eqUnit :: Eq Unit where
589+ (==) _ _ = true
590+ (/=) _ _ = false
591+
592+ instance eqArray :: (Eq a) => Eq [a] where
593+ (==) = eqArrayImpl (==)
594+ (/=) xs ys = not (xs == ys)
595+
596+ foreign import refEq
597+ """
598+ function refEq(r1) {
599+ return function(r2) {
600+ return r1 === r2;
601+ };
602+ }
603+ """ :: forall a. a -> a -> Boolean
604+
605+ foreign import refIneq
606+ """
607+ function refIneq(r1) {
608+ return function(r2) {
609+ return r1 !== r2;
610+ };
611+ }
612+ """ :: forall a. a -> a -> Boolean
613+
614+ foreign import eqArrayImpl
615+ """
616+ function eqArrayImpl(f) {
617+ return function(xs) {
618+ return function(ys) {
619+ if (xs.length !== ys.length) return false;
620+ for (var i = 0; i < xs.length; i++) {
621+ if (!f(xs[i])(ys[i])) return false;
622+ }
623+ return true;
624+ };
625+ };
626+ }
627+ """ :: forall a. (a -> a -> Boolean) -> [a] -> [a] -> Boolean
628+
629+ -- | The `Ordering` data type represents the three possible outcomes of
630+ -- | comparing two values:
631+ -- |
632+ -- | `LT` - The first value is _less than_ the second.
633+ -- | `GT` - The first value is _greater than_ the second.
634+ -- | `EQ` - The first value is _equal to_ or _incomparable to_ the second.
635+ data Ordering = LT | GT | EQ
636+
637+ -- | The `Ord` type class represents types which support comparisons.
638+ -- |
639+ -- | `Ord` instances should satisfy the laws of _partially orderings_:
640+ -- |
641+ -- | - Reflexivity: `a <= a`
642+ -- | - Antisymmetry: if `a <= b` and `b <= a` then `a = b`
643+ -- | - Transitivity: if `a <= b` and `b <= c` then `a <= c`
644+ class (Eq a) <= Ord a where
645+ compare :: a -> a -> Ordering
646+
647+ instance ordBoolean :: Ord Boolean where
648+ compare false false = EQ
649+ compare false true = LT
650+ compare true true = EQ
651+ compare true false = GT
652+
653+ instance ordNumber :: Ord Number where
654+ compare = unsafeCompare
655+
656+ instance ordString :: Ord String where
657+ compare = unsafeCompare
658+
659+ instance ordUnit :: Ord Unit where
660+ compare _ _ = EQ
661+
662+ instance ordArray :: (Ord a) => Ord [a] where
663+ compare [] [] = EQ
664+ compare [] _ = LT
665+ compare _ [] = GT
666+ compare (x:xs) (y:ys) = case compare x y of
667+ EQ -> compare xs ys
668+ other -> other
669+
670+ instance eqOrdering :: Eq Ordering where
671+ (==) LT LT = true
672+ (==) GT GT = true
673+ (==) EQ EQ = true
674+ (==) _ _ = false
675+ (/=) x y = not (x == y)
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
710+ """
711+ function unsafeCompareImpl(lt) {
712+ return function(eq) {
713+ return function(gt) {
714+ return function(x) {
715+ return function(y) {
716+ return x < y ? lt : x > y ? gt : eq;
717+ };
718+ };
719+ };
720+ };
721+ }
722+ """ :: forall a. Ordering -> Ordering -> Ordering -> a -> a -> Ordering
723+
724+ -- | The `Bounded` type class represents types that are finite partially
725+ -- | ordered sets.
726+ -- |
727+ -- | Instances should satisfy the following law in addition to the `Ord` laws:
728+ -- |
729+ -- | - Ordering: `bottom <= a <= top`
730+ class (Ord a) <= Bounded a where
731+ top :: a
732+ bottom :: a
733+
734+ instance boundedBoolean :: Bounded Boolean where
735+ top = true
736+ bottom = false
737+
738+ instance boundedUnit :: Bounded Unit where
739+ top = unit
740+ bottom = unit
741+
742+ -- | The `Lattice` type class represents types that are partially ordered
743+ -- | sets with a supremum (`sup` or `||`) and infimum (`inf` or `&&`).
744+ -- |
745+ -- | Instances should satisfy the following laws in addition to the `Ord`
746+ -- | laws:
747+ -- |
748+ -- | - Associativity:
749+ -- | - `a || (b || c) = (a || b) || c`
750+ -- | - `a && (b && c) = (a && b) && c`
751+ -- | - Commutativity:
752+ -- | - `a || b = b || a`
753+ -- | - `a && b = b && a`
754+ -- | - Absorption:
755+ -- | - `a || (a && b) = a`
756+ -- | - `a && (a || b) = a`
757+ -- | - Idempotent:
758+ -- | - `a || a = a`
759+ -- | - `a && a = a`
760+ class (Ord a) <= Lattice a where
761+ sup :: a -> a -> a
762+ inf :: a -> a -> a
763+
764+ instance latticeBoolean :: Lattice Boolean where
765+ sup = boolOr
766+ inf = boolAnd
767+
768+ infixr 2 ||
769+ infixr 3 &&
770+
771+ -- | The `sup` operator.
772+ (||) :: forall a. (Lattice a) => a -> a -> a
773+ (||) = sup
774+
775+ -- | The `inf` operator.
776+ (&&) :: forall a. (Lattice a) => a -> a -> a
777+ (&&) = inf
778+
779+ -- | The `BoundedLattice` type class represents types that are finite
780+ -- | lattices.
781+ -- |
782+ -- | Instances should satisfy the following law in addition to the `Lattice`
783+ -- | and `Bounded` laws:
784+ -- |
785+ -- | - Identity:
786+ -- | - `a || bottom = a`
787+ -- | - `a && top = a`
788+ -- | - Annihiliation:
789+ -- | - `a || top = top`
790+ -- | - `a && bottom = bottom`
791+ class (Bounded a, Lattice a) <= BoundedLattice a
792+
793+ instance boundedLatticeBoolean :: BoundedLattice Boolean
794+
795+ -- | The `ComplementedLattice` type class represents types that are lattices
796+ -- | where every member is also uniquely complemented.
797+ -- |
798+ -- | Instances should satisfy the following law in addition to the
799+ -- | `BoundedLattice` laws:
800+ -- |
801+ -- | - Complemented:
802+ -- | - `not a || a == top`
803+ -- | - `not a && a == bottom`
804+ -- | - Double negation:
805+ -- | - `not <<< not == id`
806+ class (BoundedLattice a) <= ComplementedLattice a where
807+ not :: a -> a
808+
809+ instance complementedLatticeBoolean :: ComplementedLattice Boolean where
810+ not = boolNot
811+
812+ -- | The `DistributiveLattice` type class represents types that are lattices
813+ -- | where the `&&` and `||` distribute over each other.
814+ -- |
815+ -- | Instances should satisfy the following law in addition to the `Lattice`
816+ -- | laws:
817+ -- |
818+ -- | - Distributivity: `x && (y || z) = (x && y) || (x && z)`
819+ class (Lattice a) <= DistributiveLattice a
820+
821+ instance distributiveLatticeBoolean :: DistributiveLattice Boolean
822+
823+ -- | The `BooleanAlgebra` type class represents types that are Boolean
824+ -- | algebras, also known as Boolean lattices.
825+ -- |
826+ -- | Instances should satisfy the `ComplementedLattice` and
827+ -- | `DistributiveLattice` laws.
828+ class (ComplementedLattice a, DistributiveLattice a) <= BooleanAlgebra a
829+
830+ instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
831+
832+ foreign import boolOr
833+ """
834+ function boolOr(b1) {
835+ return function(b2) {
836+ return b1 || b2;
837+ };
838+ }
839+ """ :: Boolean -> Boolean -> Boolean
840+
841+ foreign import boolAnd
842+ """
843+ function boolAnd(b1) {
844+ return function(b2) {
845+ return b1 && b2;
846+ };
847+ }
848+ """ :: Boolean -> Boolean -> Boolean
849+
850+ foreign import boolNot
851+ """
852+ function boolNot(b) {
853+ return !b;
854+ }
855+ """ :: Boolean -> Boolean
856+
857+ -- | The `Show` type class represents those types which can be converted into
858+ -- | a human-readable `String` representation.
859+ -- |
860+ -- | While not required, it is recommended that for any expression `x`, the
861+ -- | string `show x` be executable PureScript code which evaluates to the same
862+ -- | value as the expression `x`.
863+ class Show a where
864+ show :: a -> String
865+
866+ instance showBoolean :: Show Boolean where
867+ show true = "true"
868+ show false = "false"
869+
870+ instance showNumber :: Show Number where
871+ show = showNumberImpl
872+
873+ instance showString :: Show String where
874+ show = showStringImpl
875+
876+ instance showUnit :: Show Unit where
877+ show _ = "unit"
878+
879+ instance showArray :: (Show a) => Show [a] where
880+ show = showArrayImpl show
881+
882+ instance showOrdering :: Show Ordering where
883+ show LT = "LT"
884+ show GT = "GT"
885+ show EQ = "EQ"
886+
887+ foreign import showNumberImpl
888+ """
889+ function showNumberImpl(n) {
890+ return n.toString();
891+ }
892+ """ :: Number -> String
893+
894+ foreign import showStringImpl
895+ """
896+ function showStringImpl(s) {
897+ return JSON.stringify(s);
898+ }
899+ """ :: String -> String
900+
901+ foreign import showArrayImpl
902+ """
903+ function showArrayImpl(f) {
904+ return function(xs) {
905+ var ss = [];
906+ for (var i = 0, l = xs.length; i < l; i++) {
907+ ss[i] = f(xs[i]);
908+ }
909+ return '[' + ss.join(',') + ']';
910+ };
911+ }
912+ """ :: forall a. (a -> String) -> [a] -> String
913+
914+module Data.Function where
915+
916+ -- | The `on` function is used to change the domain of a binary operator.
917+ -- |
918+ -- | For example, we can create a function which compares two records based on the values of their `x` properties:
919+ -- |
920+ -- | ```purescript
921+ -- | compareX :: forall r. { x :: Number | r } -> { x :: Number | r } -> Ordering
922+ -- | compareX = compare `on` _.x
923+ -- | ```
924+ on :: forall a b c. (b -> b -> c) -> (a -> b) -> a -> a -> c
925+ on f g x y = g x `f` g y
926+
927+ -- | A function of zero arguments
928+ foreign import data Fn0 :: * -> *
929+
930+ -- | A function of one argument
931+ foreign import data Fn1 :: * -> * -> *
932+
933+ -- | A function of two arguments
934+ foreign import data Fn2 :: * -> * -> * -> *
935+
936+ -- | A function of three arguments
937+ foreign import data Fn3 :: * -> * -> * -> * -> *
938+
939+ -- | A function of four arguments
940+ foreign import data Fn4 :: * -> * -> * -> * -> * -> *
941+
942+ -- | A function of five arguments
943+ foreign import data Fn5 :: * -> * -> * -> * -> * -> * -> *
944+
945+ -- | A function of six arguments
946+ foreign import data Fn6 :: * -> * -> * -> * -> * -> * -> * -> *
947+
948+ -- | A function of seven arguments
949+ foreign import data Fn7 :: * -> * -> * -> * -> * -> * -> * -> * -> *
950+
951+ -- | A function of eight arguments
952+ foreign import data Fn8 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> *
953+
954+ -- | A function of nine arguments
955+ foreign import data Fn9 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> *
956+
957+ -- | A function of ten arguments
958+ foreign import data Fn10 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> *
959+
960+ -- | Create a function of no arguments
961+ foreign import mkFn0
962+ """
963+ function mkFn0(fn) {
964+ return function() {
965+ return fn({});
966+ };
967+ }
968+ """ :: forall a. (Unit -> a) -> Fn0 a
969+
970+ -- | Create a function of one argument
971+ foreign import mkFn1
972+ """
973+ function mkFn1(fn) {
974+ return function(a) {
975+ return fn(a);
976+ };
977+ }
978+ """ :: forall a b. (a -> b) -> Fn1 a b
979+
980+ -- | Create a function of two arguments from a curried function
981+ foreign import mkFn2
982+ """
983+ function mkFn2(fn) {
984+ return function(a, b) {
985+ return fn(a)(b);
986+ };
987+ }
988+ """ :: forall a b c. (a -> b -> c) -> Fn2 a b c
989+
990+ -- | Create a function of three arguments from a curried function
991+ foreign import mkFn3
992+ """
993+ function mkFn3(fn) {
994+ return function(a, b, c) {
995+ return fn(a)(b)(c);
996+ };
997+ }
998+ """ :: forall a b c d. (a -> b -> c -> d) -> Fn3 a b c d
999+
1000+ -- | Create a function of four arguments from a curried function
1001+ foreign import mkFn4
1002+ """
1003+ function mkFn4(fn) {
1004+ return function(a, b, c, d) {
1005+ return fn(a)(b)(c)(d);
1006+ };
1007+ }
1008+ """ :: forall a b c d e. (a -> b -> c -> d -> e) -> Fn4 a b c d e
1009+
1010+ -- | Create a function of five arguments from a curried function
1011+ foreign import mkFn5
1012+ """
1013+ function mkFn5(fn) {
1014+ return function(a, b, c, d, e) {
1015+ return fn(a)(b)(c)(d)(e);
1016+ };
1017+ }
1018+ """ :: forall a b c d e f. (a -> b -> c -> d -> e -> f) -> Fn5 a b c d e f
1019+
1020+ -- | Create a function of six arguments from a curried function
1021+ foreign import mkFn6
1022+ """
1023+ function mkFn6(fn) {
1024+ return function(a, b, c, d, e, f) {
1025+ return fn(a)(b)(c)(d)(e)(f);
1026+ };
1027+ }
1028+ """ :: forall a b c d e f g. (a -> b -> c -> d -> e -> f -> g) -> Fn6 a b c d e f g
1029+
1030+ -- | Create a function of seven arguments from a curried function
1031+ foreign import mkFn7
1032+ """
1033+ function mkFn7(fn) {
1034+ return function(a, b, c, d, e, f, g) {
1035+ return fn(a)(b)(c)(d)(e)(f)(g);
1036+ };
1037+ }
1038+ """ :: forall a b c d e f g h. (a -> b -> c -> d -> e -> f -> g -> h) -> Fn7 a b c d e f g h
1039+
1040+ -- | Create a function of eight arguments from a curried function
1041+ foreign import mkFn8
1042+ """
1043+ function mkFn8(fn) {
1044+ return function(a, b, c, d, e, f, g, h) {
1045+ return fn(a)(b)(c)(d)(e)(f)(g)(h);
1046+ };
1047+ }
1048+ """ :: forall a b c d e f g h i. (a -> b -> c -> d -> e -> f -> g -> h -> i) -> Fn8 a b c d e f g h i
1049+
1050+ -- | Create a function of nine arguments from a curried function
1051+ foreign import mkFn9
1052+ """
1053+ function mkFn9(fn) {
1054+ return function(a, b, c, d, e, f, g, h, i) {
1055+ return fn(a)(b)(c)(d)(e)(f)(g)(h)(i);
1056+ };
1057+ }
1058+ """ :: forall a b c d e f g h i j. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j) -> Fn9 a b c d e f g h i j
1059+
1060+ -- | Create a function of ten arguments from a curried function
1061+ foreign import mkFn10
1062+ """
1063+ function mkFn10(fn) {
1064+ return function(a, b, c, d, e, f, g, h, i, j) {
1065+ return fn(a)(b)(c)(d)(e)(f)(g)(h)(i)(j);
1066+ };
1067+ }
1068+ """ :: forall a b c d e f g h i j k. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k) -> Fn10 a b c d e f g h i j k
1069+
1070+ -- | Apply a function of no arguments
1071+ foreign import runFn0
1072+ """
1073+ function runFn0(fn) {
1074+ return fn();
1075+ }
1076+ """ :: forall a. Fn0 a -> a
1077+
1078+ -- | Apply a function of one argument
1079+ foreign import runFn1
1080+ """
1081+ function runFn1(fn) {
1082+ return function(a) {
1083+ return fn(a);
1084+ };
1085+ }
1086+ """ :: forall a b. Fn1 a b -> a -> b
1087+
1088+ -- | Apply a function of two arguments
1089+ foreign import runFn2
1090+ """
1091+ function runFn2(fn) {
1092+ return function(a) {
1093+ return function(b) {
1094+ return fn(a, b);
1095+ };
1096+ };
1097+ }
1098+ """ :: forall a b c. Fn2 a b c -> a -> b -> c
1099+
1100+ -- | Apply a function of three arguments
1101+ foreign import runFn3
1102+ """
1103+ function runFn3(fn) {
1104+ return function(a) {
1105+ return function(b) {
1106+ return function(c) {
1107+ return fn(a, b, c);
1108+ };
1109+ };
1110+ };
1111+ }
1112+ """ :: forall a b c d. Fn3 a b c d -> a -> b -> c -> d
1113+
1114+ -- | Apply a function of four arguments
1115+ foreign import runFn4
1116+ """
1117+ function runFn4(fn) {
1118+ return function(a) {
1119+ return function(b) {
1120+ return function(c) {
1121+ return function(d) {
1122+ return fn(a, b, c, d);
1123+ };
1124+ };
1125+ };
1126+ };
1127+ }
1128+ """ :: forall a b c d e. Fn4 a b c d e -> a -> b -> c -> d -> e
1129+
1130+ -- | Apply a function of five arguments
1131+ foreign import runFn5
1132+ """
1133+ function runFn5(fn) {
1134+ return function(a) {
1135+ return function(b) {
1136+ return function(c) {
1137+ return function(d) {
1138+ return function(e) {
1139+ return fn(a, b, c, d, e);
1140+ };
1141+ };
1142+ };
1143+ };
1144+ };
1145+ }
1146+ """ :: forall a b c d e f. Fn5 a b c d e f -> a -> b -> c -> d -> e -> f
1147+
1148+ -- | Apply a function of six arguments
1149+ foreign import runFn6
1150+ """
1151+ function runFn6(fn) {
1152+ return function(a) {
1153+ return function(b) {
1154+ return function(c) {
1155+ return function(d) {
1156+ return function(e) {
1157+ return function(f) {
1158+ return fn(a, b, c, d, e, f);
1159+ };
1160+ };
1161+ };
1162+ };
1163+ };
1164+ };
1165+ }
1166+ """ :: forall a b c d e f g. Fn6 a b c d e f g -> a -> b -> c -> d -> e -> f -> g
1167+
1168+ -- | Apply a function of seven arguments
1169+ foreign import runFn7
1170+ """
1171+ function runFn7(fn) {
1172+ return function(a) {
1173+ return function(b) {
1174+ return function(c) {
1175+ return function(d) {
1176+ return function(e) {
1177+ return function(f) {
1178+ return function(g) {
1179+ return fn(a, b, c, d, e, f, g);
1180+ };
1181+ };
1182+ };
1183+ };
1184+ };
1185+ };
1186+ };
1187+ }
1188+ """ :: forall a b c d e f g h. Fn7 a b c d e f g h -> a -> b -> c -> d -> e -> f -> g -> h
1189+
1190+ -- | Apply a function of eight arguments
1191+ foreign import runFn8
1192+ """
1193+ function runFn8(fn) {
1194+ return function(a) {
1195+ return function(b) {
1196+ return function(c) {
1197+ return function(d) {
1198+ return function(e) {
1199+ return function(f) {
1200+ return function(g) {
1201+ return function(h) {
1202+ return fn(a, b, c, d, e, f, g, h);
1203+ };
1204+ };
1205+ };
1206+ };
1207+ };
1208+ };
1209+ };
1210+ };
1211+ }
1212+ """ :: forall a b c d e f g h i. Fn8 a b c d e f g h i -> a -> b -> c -> d -> e -> f -> g -> h -> i
1213+
1214+ -- | Apply a function of nine arguments
1215+ foreign import runFn9
1216+ """
1217+ function runFn9(fn) {
1218+ return function(a) {
1219+ return function(b) {
1220+ return function(c) {
1221+ return function(d) {
1222+ return function(e) {
1223+ return function(f) {
1224+ return function(g) {
1225+ return function(h) {
1226+ return function(i) {
1227+ return fn(a, b, c, d, e, f, g, h, i);
1228+ };
1229+ };
1230+ };
1231+ };
1232+ };
1233+ };
1234+ };
1235+ };
1236+ };
1237+ }
1238+ """ :: forall a b c d e f g h i j. Fn9 a b c d e f g h i j -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j
1239+
1240+ -- | Apply a function of ten arguments
1241+ foreign import runFn10
1242+ """
1243+ function runFn10(fn) {
1244+ return function(a) {
1245+ return function(b) {
1246+ return function(c) {
1247+ return function(d) {
1248+ return function(e) {
1249+ return function(f) {
1250+ return function(g) {
1251+ return function(h) {
1252+ return function(i) {
1253+ return function(j) {
1254+ return fn(a, b, c, d, e, f, g, h, i, j);
1255+ };
1256+ };
1257+ };
1258+ };
1259+ };
1260+ };
1261+ };
1262+ };
1263+ };
1264+ };
1265+ }
1266+ """ :: forall a b c d e f g h i j k. Fn10 a b c d e f g h i j k -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k
1267+
1268+module Prelude.Unsafe where
1269+
1270+ -- | Find the element of an array at the specified index.
1271+ -- |
1272+ -- | Note: this function can cause unpredictable failure at runtime if the index is out-of-bounds.
1273+ foreign import unsafeIndex
1274+ """
1275+ function unsafeIndex(xs) {
1276+ return function(n) {
1277+ return xs[n];
1278+ };
1279+ }
1280+ """ :: forall a. [a] -> Number -> a
1281+
1282+module Control.Monad.Eff
1283+ ( Eff()
1284+ , Pure()
1285+ , runPure
1286+ , untilE, whileE, forE, foreachE
1287+ ) where
1288+
1289+ -- | The `Eff` type constructor is used to represent _native_ effects.
1290+ -- |
1291+ -- | See [Handling Native Effects with the Eff Monad](https://github.com/purescript/purescript/wiki/Handling-Native-Effects-with-the-Eff-Monad) for more details.
1292+ -- |
1293+ -- | The first type parameter is a row of effects which represents the contexts in which a computation can be run, and the second type parameter is the return type.
1294+ foreign import data Eff :: # ! -> * -> *
1295+
1296+ foreign import returnE
1297+ """
1298+ function returnE(a) {
1299+ return function() {
1300+ return a;
1301+ };
1302+ }
1303+ """ :: forall e a. a -> Eff e a
1304+
1305+ foreign import bindE
1306+ """
1307+ function bindE(a) {
1308+ return function(f) {
1309+ return function() {
1310+ return f(a())();
1311+ };
1312+ };
1313+ }
1314+ """ :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
1315+
1316+ -- | The `Pure` type synonym represents _pure_ computations, i.e. ones in which all effects have been handled.
1317+ -- |
1318+ -- | The `runPure` function can be used to run pure computations and obtain their result.
1319+ type Pure a = forall e. Eff e a
1320+
1321+ -- | Run a pure computation and return its result.
1322+ -- |
1323+ -- | Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach
1324+ -- | is to use parentheses instead.
1325+ foreign import runPure
1326+ """
1327+ function runPure(f) {
1328+ return f();
1329+ }
1330+ """ :: forall a. Pure a -> a
1331+
1332+ instance functorEff :: Functor (Eff e) where
1333+ (<$>) = liftA1
1334+
1335+ instance applyEff :: Apply (Eff e) where
1336+ (<*>) = ap
1337+
1338+ instance applicativeEff :: Applicative (Eff e) where
1339+ pure = returnE
1340+
1341+ instance bindEff :: Bind (Eff e) where
1342+ (>>=) = bindE
1343+
1344+ instance monadEff :: Monad (Eff e)
1345+
1346+ -- | Loop until a condition becomes `true`.
1347+ -- |
1348+ -- | `untilE b` is an effectful computation which repeatedly runs the effectful computation `b`,
1349+ -- | until its return value is `true`.
1350+ foreign import untilE
1351+ """
1352+ function untilE(f) {
1353+ return function() {
1354+ while (!f());
1355+ return {};
1356+ };
1357+ }
1358+ """ :: forall e. Eff e Boolean -> Eff e Unit
1359+
1360+ -- | Loop while a condition is `true`.
1361+ -- |
1362+ -- | `whileE b m` is effectful computation which runs the effectful computation `b`. If its result is
1363+ -- | `true`, it runs the effectful computation `m` and loops. If not, the computation ends.
1364+ foreign import whileE
1365+ """
1366+ function whileE(f) {
1367+ return function(a) {
1368+ return function() {
1369+ while (f()) {
1370+ a();
1371+ }
1372+ return {};
1373+ };
1374+ };
1375+ }
1376+ """ :: forall e a. Eff e Boolean -> Eff e a -> Eff e Unit
1377+
1378+ -- | Loop over a consecutive collection of numbers.
1379+ -- |
1380+ -- | `forE lo hi f` runs the computation returned by the function `f` for each of the inputs
1381+ -- | between `lo` (inclusive) and `hi` (exclusive).
1382+ foreign import forE
1383+ """
1384+ function forE(lo) {
1385+ return function(hi) {
1386+ return function(f) {
1387+ return function() {
1388+ for (var i = lo; i < hi; i++) {
1389+ f(i)();
1390+ }
1391+ };
1392+ };
1393+ };
1394+ }
1395+ """ :: forall e. Number -> Number -> (Number -> Eff e Unit) -> Eff e Unit
1396+
1397+ -- | Loop over an array of values.
1398+ -- |
1399+ -- | `foreach xs f` runs the computation returned by the function `f` for each of the inputs `xs`.
1400+ foreign import foreachE
1401+ """
1402+ function foreachE(as) {
1403+ return function(f) {
1404+ return function() {
1405+ for (var i = 0; i < as.length; i++) {
1406+ f(as[i])();
1407+ }
1408+ };
1409+ };
1410+ }
1411+ """ :: forall e a. [a] -> (a -> Eff e Unit) -> Eff e Unit
1412+
1413+module Control.Monad.Eff.Unsafe where
1414+
1415+ import Control.Monad.Eff
1416+
1417+ -- | Change the type of an effectful computation, allowing it to be run in another context.
1418+ -- |
1419+ -- | Note: use of this function can result in arbitrary side-effects.
1420+ foreign import unsafeInterleaveEff
1421+ """
1422+ function unsafeInterleaveEff(f) {
1423+ return f;
1424+ }
1425+ """ :: forall eff1 eff2 a. Eff eff1 a -> Eff eff2 a
1426+
1427+module Debug.Trace where
1428+
1429+ import Control.Monad.Eff
1430+
1431+ -- | The `Trace` effect represents those computations which write to the console.
1432+ foreign import data Trace :: !
1433+
1434+ -- | Write a `String` to the console.
1435+ foreign import trace
1436+ """
1437+ function trace(s) {
1438+ return function() {
1439+ console.log(s);
1440+ return {};
1441+ };
1442+ }
1443+ """ :: forall r. String -> Eff (trace :: Trace | r) Unit
1444+
1445+ -- | Write a value to the console, using its `Show` instance to produce a `String`.
1446+ print :: forall a r. (Show a) => a -> Eff (trace :: Trace | r) Unit
1447+ print o = trace (show o)
1448+
1449+module Control.Monad.ST where
1450+
1451+ import Control.Monad.Eff
1452+
1453+ -- | The `ST` effect represents _local mutation_, i.e. mutation which does not "escape" into the surrounding computation.
1454+ -- |
1455+ -- | An `ST` computation is parameterized by a phantom type which is used to restrict the set of reference cells it is allowed to access.
1456+ -- |
1457+ -- | The `runST` function can be used to handle the `ST` effect.
1458+ foreign import data ST :: * -> !
1459+
1460+ -- | The type `STRef s a` represents a mutable reference holding a value of type `a`, which can be used with the `ST s` effect.
1461+ foreign import data STRef :: * -> * -> *
1462+
1463+ -- | Create a new mutable reference.
1464+ foreign import newSTRef
1465+ """
1466+ function newSTRef(val) {
1467+ return function() {
1468+ return { value: val };
1469+ };
1470+ }
1471+ """ :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
1472+
1473+ -- | Read the current value of a mutable reference.
1474+ foreign import readSTRef
1475+ """
1476+ function readSTRef(ref) {
1477+ return function() {
1478+ return ref.value;
1479+ };
1480+ }
1481+ """ :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
1482+
1483+ -- | Modify the value of a mutable reference by applying a function to the current value.
1484+ foreign import modifySTRef
1485+ """
1486+ function modifySTRef(ref) {
1487+ return function(f) {
1488+ return function() {
1489+ return ref.value = f(ref.value);
1490+ };
1491+ };
1492+ }
1493+ """ :: forall a h r. STRef h a -> (a -> a) -> Eff (st :: ST h | r) a
1494+
1495+ -- | Set the value of a mutable reference.
1496+ foreign import writeSTRef
1497+ """
1498+ function writeSTRef(ref) {
1499+ return function(a) {
1500+ return function() {
1501+ return ref.value = a;
1502+ };
1503+ };
1504+ }
1505+ """ :: forall a h r. STRef h a -> a -> Eff (st :: ST h | r) a
1506+
1507+ -- | Run an `ST` computation.
1508+ -- |
1509+ -- | Note: the type of `runST` uses a rank-2 type to constrain the phantom type `s`, such that the computation must not leak any mutable references
1510+ -- | to the surrounding computation.
1511+ -- |
1512+ -- | It may cause problems to apply this function using the `$` operator. The recommended approach is to use parentheses instead.
1513+ foreign import runST
1514+ """
1515+ function runST(f) {
1516+ return f;
1517+ }
1518+ """ :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
1519+
1520+ -- | A convenience function which combines `runST` with `runPure`, which can be used when the only required effect is `ST`.
1521+ -- |
1522+ -- | Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach
1523+ -- | is to use parentheses instead.
1524+ pureST :: forall a. (forall h r. Eff (st :: ST h | r) a) -> a
1525+ pureST st = runPure (runST st)