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