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