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