@@ -1,1478 +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)))` = `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` = `d (c (b (a x)))`
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 reversed. 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, we can create a function which compares two records based on the values of their `x` properties:
872− -- |
873− -- | ```purescript
874− -- | compareX :: forall r. { x :: Number | r } -> { x :: Number | r } -> Ordering
875− -- | compareX = compare `on` _.x
876− -- | ```
877− on :: forall a b c. (b -> b -> c) -> (a -> b) -> a -> a -> c
878− on f g x y = g x `f` g y
879−
880− -- | A function of zero arguments
881− foreign import data Fn0 :: * -> *
882−
883− -- | A function of one argument
884− foreign import data Fn1 :: * -> * -> *
885−
886− -- | A function of two arguments
887− foreign import data Fn2 :: * -> * -> * -> *
888−
889− -- | A function of three arguments
890− foreign import data Fn3 :: * -> * -> * -> * -> *
891−
892− -- | A function of four arguments
893− foreign import data Fn4 :: * -> * -> * -> * -> * -> *
894−
895− -- | A function of five arguments
896− foreign import data Fn5 :: * -> * -> * -> * -> * -> * -> *
897−
898− -- | A function of six arguments
899− foreign import data Fn6 :: * -> * -> * -> * -> * -> * -> * -> *
900−
901− -- | A function of seven arguments
902− foreign import data Fn7 :: * -> * -> * -> * -> * -> * -> * -> * -> *
903−
904− -- | A function of eight arguments
905− foreign import data Fn8 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> *
906−
907− -- | A function of nine arguments
908− foreign import data Fn9 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> *
909−
910− -- | A function of ten arguments
911− foreign import data Fn10 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> *
912−
913− -- | Create a function of no arguments
914− foreign import mkFn0
915− """
916− function mkFn0(fn) {
917− return function() {
918− return fn({});
919− };
920− }
921− """ :: forall a. (Unit -> a) -> Fn0 a
922−
923− -- | Create a function of one argument
924− foreign import mkFn1
925− """
926− function mkFn1(fn) {
927− return function(a) {
928− return fn(a);
929− };
930− }
931− """ :: forall a b. (a -> b) -> Fn1 a b
932−
933− -- | Create a function of two arguments from a curried function
934− foreign import mkFn2
935− """
936− function mkFn2(fn) {
937− return function(a, b) {
938− return fn(a)(b);
939− };
940− }
941− """ :: forall a b c. (a -> b -> c) -> Fn2 a b c
942−
943− -- | Create a function of three arguments from a curried function
944− foreign import mkFn3
945− """
946− function mkFn3(fn) {
947− return function(a, b, c) {
948− return fn(a)(b)(c);
949− };
950− }
951− """ :: forall a b c d. (a -> b -> c -> d) -> Fn3 a b c d
952−
953− -- | Create a function of four arguments from a curried function
954− foreign import mkFn4
955− """
956− function mkFn4(fn) {
957− return function(a, b, c, d) {
958− return fn(a)(b)(c)(d);
959− };
960− }
961− """ :: forall a b c d e. (a -> b -> c -> d -> e) -> Fn4 a b c d e
962−
963− -- | Create a function of five arguments from a curried function
964− foreign import mkFn5
965− """
966− function mkFn5(fn) {
967− return function(a, b, c, d, e) {
968− return fn(a)(b)(c)(d)(e);
969− };
970− }
971− """ :: forall a b c d e f. (a -> b -> c -> d -> e -> f) -> Fn5 a b c d e f
972−
973− -- | Create a function of six arguments from a curried function
974− foreign import mkFn6
975− """
976− function mkFn6(fn) {
977− return function(a, b, c, d, e, f) {
978− return fn(a)(b)(c)(d)(e)(f);
979− };
980− }
981− """ :: forall a b c d e f g. (a -> b -> c -> d -> e -> f -> g) -> Fn6 a b c d e f g
982−
983− -- | Create a function of seven arguments from a curried function
984− foreign import mkFn7
985− """
986− function mkFn7(fn) {
987− return function(a, b, c, d, e, f, g) {
988− return fn(a)(b)(c)(d)(e)(f)(g);
989− };
990− }
991− """ :: 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
992−
993− -- | Create a function of eight arguments from a curried function
994− foreign import mkFn8
995− """
996− function mkFn8(fn) {
997− return function(a, b, c, d, e, f, g, h) {
998− return fn(a)(b)(c)(d)(e)(f)(g)(h);
999− };
1000− }
1001− """ :: 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
1002−
1003− -- | Create a function of nine arguments from a curried function
1004− foreign import mkFn9
1005− """
1006− function mkFn9(fn) {
1007− return function(a, b, c, d, e, f, g, h, i) {
1008− return fn(a)(b)(c)(d)(e)(f)(g)(h)(i);
1009− };
1010− }
1011− """ :: 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
1012−
1013− -- | Create a function of ten arguments from a curried function
1014− foreign import mkFn10
1015− """
1016− function mkFn10(fn) {
1017− return function(a, b, c, d, e, f, g, h, i, j) {
1018− return fn(a)(b)(c)(d)(e)(f)(g)(h)(i)(j);
1019− };
1020− }
1021− """ :: 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
1022−
1023− -- | Apply a function of no arguments
1024− foreign import runFn0
1025− """
1026− function runFn0(fn) {
1027− return fn();
1028− }
1029− """ :: forall a. Fn0 a -> a
1030−
1031− -- | Apply a function of one argument
1032− foreign import runFn1
1033− """
1034− function runFn1(fn) {
1035− return function(a) {
1036− return fn(a);
1037− };
1038− }
1039− """ :: forall a b. Fn1 a b -> a -> b
1040−
1041− -- | Apply a function of two arguments
1042− foreign import runFn2
1043− """
1044− function runFn2(fn) {
1045− return function(a) {
1046− return function(b) {
1047− return fn(a, b);
1048− };
1049− };
1050− }
1051− """ :: forall a b c. Fn2 a b c -> a -> b -> c
1052−
1053− -- | Apply a function of three arguments
1054− foreign import runFn3
1055− """
1056− function runFn3(fn) {
1057− return function(a) {
1058− return function(b) {
1059− return function(c) {
1060− return fn(a, b, c);
1061− };
1062− };
1063− };
1064− }
1065− """ :: forall a b c d. Fn3 a b c d -> a -> b -> c -> d
1066−
1067− -- | Apply a function of four arguments
1068− foreign import runFn4
1069− """
1070− function runFn4(fn) {
1071− return function(a) {
1072− return function(b) {
1073− return function(c) {
1074− return function(d) {
1075− return fn(a, b, c, d);
1076− };
1077− };
1078− };
1079− };
1080− }
1081− """ :: forall a b c d e. Fn4 a b c d e -> a -> b -> c -> d -> e
1082−
1083− -- | Apply a function of five arguments
1084− foreign import runFn5
1085− """
1086− function runFn5(fn) {
1087− return function(a) {
1088− return function(b) {
1089− return function(c) {
1090− return function(d) {
1091− return function(e) {
1092− return fn(a, b, c, d, e);
1093− };
1094− };
1095− };
1096− };
1097− };
1098− }
1099− """ :: forall a b c d e f. Fn5 a b c d e f -> a -> b -> c -> d -> e -> f
1100−
1101− -- | Apply a function of six arguments
1102− foreign import runFn6
1103− """
1104− function runFn6(fn) {
1105− return function(a) {
1106− return function(b) {
1107− return function(c) {
1108− return function(d) {
1109− return function(e) {
1110− return function(f) {
1111− return fn(a, b, c, d, e, f);
1112− };
1113− };
1114− };
1115− };
1116− };
1117− };
1118− }
1119− """ :: forall a b c d e f g. Fn6 a b c d e f g -> a -> b -> c -> d -> e -> f -> g
1120−
1121− -- | Apply a function of seven arguments
1122− foreign import runFn7
1123− """
1124− function runFn7(fn) {
1125− return function(a) {
1126− return function(b) {
1127− return function(c) {
1128− return function(d) {
1129− return function(e) {
1130− return function(f) {
1131− return function(g) {
1132− return fn(a, b, c, d, e, f, g);
1133− };
1134− };
1135− };
1136− };
1137− };
1138− };
1139− };
1140− }
1141− """ :: 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
1142−
1143− -- | Apply a function of eight arguments
1144− foreign import runFn8
1145− """
1146− function runFn8(fn) {
1147− return function(a) {
1148− return function(b) {
1149− return function(c) {
1150− return function(d) {
1151− return function(e) {
1152− return function(f) {
1153− return function(g) {
1154− return function(h) {
1155− return fn(a, b, c, d, e, f, g, h);
1156− };
1157− };
1158− };
1159− };
1160− };
1161− };
1162− };
1163− };
1164− }
1165− """ :: 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
1166−
1167− -- | Apply a function of nine arguments
1168− foreign import runFn9
1169− """
1170− function runFn9(fn) {
1171− return function(a) {
1172− return function(b) {
1173− return function(c) {
1174− return function(d) {
1175− return function(e) {
1176− return function(f) {
1177− return function(g) {
1178− return function(h) {
1179− return function(i) {
1180− return fn(a, b, c, d, e, f, g, h, i);
1181− };
1182− };
1183− };
1184− };
1185− };
1186− };
1187− };
1188− };
1189− };
1190− }
1191− """ :: 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
1192−
1193− -- | Apply a function of ten arguments
1194− foreign import runFn10
1195− """
1196− function runFn10(fn) {
1197− return function(a) {
1198− return function(b) {
1199− return function(c) {
1200− return function(d) {
1201− return function(e) {
1202− return function(f) {
1203− return function(g) {
1204− return function(h) {
1205− return function(i) {
1206− return function(j) {
1207− return fn(a, b, c, d, e, f, g, h, i, j);
1208− };
1209− };
1210− };
1211− };
1212− };
1213− };
1214− };
1215− };
1216− };
1217− };
1218− }
1219− """ :: 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
1220−
1221−module Prelude.Unsafe where
1222−
1223− -- | Find the element of an array at the specified index.
1224− -- |
1225− -- | Note: this function can cause unpredictable failure at runtime if the index is out-of-bounds.
1226− foreign import unsafeIndex
1227− """
1228− function unsafeIndex(xs) {
1229− return function(n) {
1230− return xs[n];
1231− };
1232− }
1233− """ :: forall a. [a] -> Number -> a
1234−
1235−module Control.Monad.Eff
1236− ( Eff()
1237− , Pure()
1238− , runPure
1239− , untilE, whileE, forE, foreachE
1240− ) where
1241−
1242− -- | The `Eff` type constructor is used to represent _native_ effects.
1243− -- |
1244− -- | See [Handling Native Effects with the Eff Monad](https://github.com/purescript/purescript/wiki/Handling-Native-Effects-with-the-Eff-Monad) for more details.
1245− -- |
1246− -- | 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.
1247− foreign import data Eff :: # ! -> * -> *
1248−
1249− foreign import returnE
1250− """
1251− function returnE(a) {
1252− return function() {
1253− return a;
1254− };
1255− }
1256− """ :: forall e a. a -> Eff e a
1257−
1258− foreign import bindE
1259− """
1260− function bindE(a) {
1261− return function(f) {
1262− return function() {
1263− return f(a())();
1264− };
1265− };
1266− }
1267− """ :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
1268−
1269− -- | The `Pure` type synonym represents _pure_ computations, i.e. ones in which all effects have been handled.
1270− -- |
1271− -- | The `runPure` function can be used to run pure computations and obtain their result.
1272− type Pure a = forall e. Eff e a
1273−
1274− -- | Run a pure computation and return its result.
1275− -- |
1276− -- | Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach
1277− -- | is to use parentheses instead.
1278− foreign import runPure
1279− """
1280− function runPure(f) {
1281− return f();
1282− }
1283− """ :: forall a. Pure a -> a
1284−
1285− instance functorEff :: Functor (Eff e) where
1286− (<$>) = liftA1
1287−
1288− instance applyEff :: Apply (Eff e) where
1289− (<*>) = ap
1290−
1291− instance applicativeEff :: Applicative (Eff e) where
1292− pure = returnE
1293−
1294− instance bindEff :: Bind (Eff e) where
1295− (>>=) = bindE
1296−
1297− instance monadEff :: Monad (Eff e)
1298−
1299− -- | Loop until a condition becomes `true`.
1300− -- |
1301− -- | `untilE b` is an effectful computation which repeatedly runs the effectful computation `b`,
1302− -- | until its return value is `true`.
1303− foreign import untilE
1304− """
1305− function untilE(f) {
1306− return function() {
1307− while (!f());
1308− return {};
1309− };
1310− }
1311− """ :: forall e. Eff e Boolean -> Eff e Unit
1312−
1313− -- | Loop while a condition is `true`.
1314− -- |
1315− -- | `whileE b m` is effectful computation which runs the effectful computation `b`. If its result is
1316− -- | `true`, it runs the effectful computation `m` and loops. If not, the computation ends.
1317− foreign import whileE
1318− """
1319− function whileE(f) {
1320− return function(a) {
1321− return function() {
1322− while (f()) {
1323− a();
1324− }
1325− return {};
1326− };
1327− };
1328− }
1329− """ :: forall e a. Eff e Boolean -> Eff e a -> Eff e Unit
1330−
1331− -- | Loop over a consecutive collection of numbers.
1332− -- |
1333− -- | `forE lo hi f` runs the computation returned by the function `f` for each of the inputs
1334− -- | between `lo` (inclusive) and `hi` (exclusive).
1335− foreign import forE
1336− """
1337− function forE(lo) {
1338− return function(hi) {
1339− return function(f) {
1340− return function() {
1341− for (var i = lo; i < hi; i++) {
1342− f(i)();
1343− }
1344− };
1345− };
1346− };
1347− }
1348− """ :: forall e. Number -> Number -> (Number -> Eff e Unit) -> Eff e Unit
1349−
1350− -- | Loop over an array of values.
1351− -- |
1352− -- | `foreach xs f` runs the computation returned by the function `f` for each of the inputs `xs`.
1353− foreign import foreachE
1354− """
1355− function foreachE(as) {
1356− return function(f) {
1357− return function() {
1358− for (var i = 0; i < as.length; i++) {
1359− f(as[i])();
1360− }
1361− };
1362− };
1363− }
1364− """ :: forall e a. [a] -> (a -> Eff e Unit) -> Eff e Unit
1365−
1366−module Control.Monad.Eff.Unsafe where
1367−
1368− import Control.Monad.Eff
1369−
1370− -- | Change the type of an effectful computation, allowing it to be run in another context.
1371− -- |
1372− -- | Note: use of this function can result in arbitrary side-effects.
1373− foreign import unsafeInterleaveEff
1374− """
1375− function unsafeInterleaveEff(f) {
1376− return f;
1377− }
1378− """ :: forall eff1 eff2 a. Eff eff1 a -> Eff eff2 a
1379−
1380−module Debug.Trace where
1381−
1382− import Control.Monad.Eff
1383−
1384− -- | The `Trace` effect represents those computations which write to the console.
1385− foreign import data Trace :: !
1386−
1387− -- | Write a `String` to the console.
1388− foreign import trace
1389− """
1390− function trace(s) {
1391− return function() {
1392− console.log(s);
1393− return {};
1394− };
1395− }
1396− """ :: forall r. String -> Eff (trace :: Trace | r) Unit
1397−
1398− -- | Write a value to the console, using its `Show` instance to produce a `String`.
1399− print :: forall a r. (Show a) => a -> Eff (trace :: Trace | r) Unit
1400− print o = trace (show o)
1401−
1402−module Control.Monad.ST where
1403−
1404− import Control.Monad.Eff
1405−
1406− -- | The `ST` effect represents _local mutation_, i.e. mutation which does not "escape" into the surrounding computation.
1407− -- |
1408− -- | An `ST` computation is parameterized by a phantom type which is used to restrict the set of reference cells it is allowed to access.
1409− -- |
1410− -- | The `runST` function can be used to handle the `ST` effect.
1411− foreign import data ST :: * -> !
1412−
1413− -- | The type `STRef s a` represents a mutable reference holding a value of type `a`, which can be used with the `ST s` effect.
1414− foreign import data STRef :: * -> * -> *
1415−
1416− -- | Create a new mutable reference.
1417− foreign import newSTRef
1418− """
1419− function newSTRef(val) {
1420− return function() {
1421− return { value: val };
1422− };
1423− }
1424− """ :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
1425−
1426− -- | Read the current value of a mutable reference.
1427− foreign import readSTRef
1428− """
1429− function readSTRef(ref) {
1430− return function() {
1431− return ref.value;
1432− };
1433− }
1434− """ :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
1435−
1436− -- | Modify the value of a mutable reference by applying a function to the current value.
1437− foreign import modifySTRef
1438− """
1439− function modifySTRef(ref) {
1440− return function(f) {
1441− return function() {
1442− return ref.value = f(ref.value);
1443− };
1444− };
1445− }
1446− """ :: forall a h r. STRef h a -> (a -> a) -> Eff (st :: ST h | r) a
1447−
1448− -- | Set the value of a mutable reference.
1449− foreign import writeSTRef
1450− """
1451− function writeSTRef(ref) {
1452− return function(a) {
1453− return function() {
1454− return ref.value = a;
1455− };
1456− };
1457− }
1458− """ :: forall a h r. STRef h a -> a -> Eff (st :: ST h | r) a
1459−
1460− -- | Run an `ST` computation.
1461− -- |
1462− -- | 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
1463− -- | to the surrounding computation.
1464− -- |
1465− -- | It may cause problems to apply this function using the `$` operator. The recommended approach is to use parentheses instead.
1466− foreign import runST
1467− """
1468− function runST(f) {
1469− return f;
1470− }
1471− """ :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
1472−
1473− -- | A convenience function which combines `runST` with `runPure`, which can be used when the only required effect is `ST`.
1474− -- |
1475− -- | Note: since this function has a rank-2 type, it may cause problems to apply this function using the `$` operator. The recommended approach
1476− -- | is to use parentheses instead.
1477− pureST :: forall a. (forall h r. Eff (st :: ST h | r) a) -> a
1478− pureST st = runPure (runST st)