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