diff --git a/docs/Prelude.md b/docs/Prelude.md index 27ea9cee..086d1603 100644 --- a/docs/Prelude.md +++ b/docs/Prelude.md @@ -160,10 +160,10 @@ require an identity element `id`, just composable morphisms. One example of a `Semigroupoid` is the function type constructor `(->)`, with `(<<<)` defined as function composition. -#### `semigroupoidArr` +#### `semigroupoidFn` ``` purescript -instance semigroupoidArr :: Semigroupoid Prim.Function +instance semigroupoidFn :: Semigroupoid Prim.Function ``` @@ -198,10 +198,10 @@ Instances must satisfy the following law in addition to the - Identity: `id <<< p = p <<< id = p` -#### `categoryArr` +#### `categoryFn` ``` purescript -instance categoryArr :: Category Prim.Function +instance categoryFn :: Category Prim.Function ``` @@ -224,10 +224,17 @@ Instances must satisfy the following laws: - Identity: `(<$>) id = id` - Composition: `(<$>) (f <<< g) = (f <$>) <<< (g <$>)` -#### `functorArr` +#### `functorFn` ``` purescript -instance functorArr :: Functor (Prim.Function r) +instance functorFn :: Functor (Prim.Function r) +``` + + +#### `functorArray` + +``` purescript +instance functorArray :: Functor Prim.Array ``` @@ -299,10 +306,17 @@ laws: Formally, `Apply` represents a strong lax semi-monoidal endofunctor. -#### `applyArr` +#### `applyFn` + +``` purescript +instance applyFn :: Apply (Prim.Function r) +``` + + +#### `applyArray` ``` purescript -instance applyArr :: Apply (Prim.Function r) +instance applyArray :: Apply Prim.Array ``` @@ -339,10 +353,17 @@ laws: - Homomorphism: `(pure f) <*> (pure x) = pure (f x)` - Interchange: `u <*> (pure y) = (pure ($ y)) <*> u` -#### `applicativeArr` +#### `applicativeFn` ``` purescript -instance applicativeArr :: Applicative (Prim.Function r) +instance applicativeFn :: Applicative (Prim.Function r) +``` + + +#### `applicativeArray` + +``` purescript +instance applicativeArray :: Applicative Prim.Array ``` @@ -407,10 +428,17 @@ do x <- m1 m3 x y ``` -#### `bindArr` +#### `bindFn` + +``` purescript +instance bindFn :: Bind (Prim.Function r) +``` + + +#### `bindArray` ``` purescript -instance bindArr :: Bind (Prim.Function r) +instance bindArray :: Bind Prim.Array ``` @@ -438,10 +466,17 @@ Instances must satisfy the following laws in addition to the - Left Identity: `pure x >>= f = f x` - Right Identity: `x >>= pure = x` -#### `monadArr` +#### `monadFn` + +``` purescript +instance monadFn :: Monad (Prim.Function r) +``` + + +#### `monadArray` ``` purescript -instance monadArr :: Monad (Prim.Function r) +instance monadArray :: Monad Prim.Array ``` @@ -527,10 +562,10 @@ instance semigroupUnit :: Semigroup Unit ``` -#### `semigroupArr` +#### `semigroupFn` ``` purescript -instance semigroupArr :: (Semigroup s') => Semigroup (s -> s') +instance semigroupFn :: (Semigroup s') => Semigroup (s -> s') ``` @@ -541,6 +576,13 @@ instance semigroupOrdering :: Semigroup Ordering ``` +#### `semigroupArray` + +``` purescript +instance semigroupArray :: Semigroup [a] +``` + + #### `Semiring` ``` purescript @@ -568,6 +610,13 @@ Instances must satisfy the following laws: - Right distributivity: `(a + b) * c = (a * c) + (b * c)` - Annihiliation: `zero * a = a * zero = zero` +#### `semiringInt` + +``` purescript +instance semiringInt :: Semiring Int +``` + + #### `semiringNumber` ``` purescript @@ -611,6 +660,13 @@ laws: - Additive inverse: `a + (-a) = (-a) + a = zero` +#### `ringInt` + +``` purescript +instance ringInt :: Ring Int +``` + + #### `ringNumber` ``` purescript @@ -655,6 +711,13 @@ laws: - Remainder: `a / b * b + (a `mod` b) = a` +#### `moduloSemiringInt` + +``` purescript +instance moduloSemiringInt :: ModuloSemiring Int +``` + + #### `moduloSemiringNumber` ``` purescript @@ -770,6 +833,13 @@ instance eqBoolean :: Eq Boolean ``` +#### `eqInt` + +``` purescript +instance eqInt :: Eq Int +``` + + #### `eqNumber` ``` purescript @@ -777,6 +847,13 @@ instance eqNumber :: Eq Number ``` +#### `eqChar` + +``` purescript +instance eqChar :: Eq Char +``` + + #### `eqString` ``` purescript @@ -843,6 +920,13 @@ instance ordBoolean :: Ord Boolean ``` +#### `ordInt` + +``` purescript +instance ordInt :: Ord Int +``` + + #### `ordNumber` ``` purescript @@ -857,6 +941,13 @@ instance ordString :: Ord String ``` +#### `ordChar` + +``` purescript +instance ordChar :: Ord Char +``` + + #### `ordUnit` ``` purescript @@ -1144,6 +1235,13 @@ instance showBoolean :: Show Boolean ``` +#### `showInt` + +``` purescript +instance showInt :: Show Int +``` + + #### `showNumber` ``` purescript @@ -1151,6 +1249,13 @@ instance showNumber :: Show Number ``` +#### `showChar` + +``` purescript +instance showChar :: Show Char +``` + + #### `showString` ``` purescript diff --git a/src/Prelude.purs b/src/Prelude.purs index 9d62d0b8..ea2dd8da 100644 --- a/src/Prelude.purs +++ b/src/Prelude.purs @@ -155,7 +155,7 @@ infixr 9 <<< class Semigroupoid a where compose :: forall b c d. a c d -> a b c -> a b d -instance semigroupoidArr :: Semigroupoid (->) where +instance semigroupoidFn :: Semigroupoid (->) where compose f g x = f (g x) (<<<) :: forall a b c d. (Semigroupoid a) => a c d -> a b c -> a b d @@ -176,7 +176,7 @@ instance semigroupoidArr :: Semigroupoid (->) where class (Semigroupoid a) <= Category a where id :: forall t. a t t -instance categoryArr :: Category (->) where +instance categoryFn :: Category (->) where id x = x infixl 4 <$> @@ -196,9 +196,26 @@ infixl 1 <#> class Functor f where map :: forall a b. (a -> b) -> f a -> f b -instance functorArr :: Functor ((->) r) where +instance functorFn :: Functor ((->) r) where map = compose +instance functorArray :: Functor [] where + map = arrayMap + +foreign import arrayMap + """ + function arrayMap(f) { + return function (arr) { + var l = arr.length; + var result = new Array(l); + for (var i = 0; i < l; i++) { + result[i] = f(arr[i]); + } + return result; + }; + } + """ :: forall a b. (a -> b) -> [a] -> [b] + (<$>) :: forall f a b. (Functor f) => (a -> b) -> f a -> f b (<$>) = map @@ -252,9 +269,12 @@ infixl 4 <*> class (Functor f) <= Apply f where apply :: forall a b. f (a -> b) -> f a -> f b -instance applyArr :: Apply ((->) r) where +instance applyFn :: Apply ((->) r) where apply f g x = f x (g x) +instance applyArray :: Apply [] where + apply = ap + (<*>) :: forall f a b. (Apply f) => f (a -> b) -> f a -> f b (<*>) = apply @@ -279,9 +299,12 @@ instance applyArr :: Apply ((->) r) where class (Apply f) <= Applicative f where pure :: forall a. a -> f a -instance applicativeArr :: Applicative ((->) r) where +instance applicativeFn :: Applicative ((->) r) where pure = const +instance applicativeArray :: Applicative [] where + pure x = [x] + -- | `return` is an alias for `pure`. return :: forall m a. (Applicative m) => a -> m a return = pure @@ -332,9 +355,25 @@ infixl 1 >>= class (Apply m) <= Bind m where bind :: forall a b. m a -> (a -> m b) -> m b -instance bindArr :: Bind ((->) r) where +instance bindFn :: Bind ((->) r) where bind m f x = f (m x) x +instance bindArray :: Bind [] where + bind = arrayBind + +foreign import arrayBind + """ + function arrayBind (arr) { + return function (f) { + var result = []; + for (var i = 0, l = arr.length; i < l; i++) { + Array.prototype.push.apply(result, f(arr[i])); + } + return result; + }; + } + """ :: forall a b. [a] -> (a -> [b]) -> [b] + (>>=) :: forall m a b. (Monad m) => m a -> (a -> m b) -> m b (>>=) = bind @@ -350,7 +389,9 @@ instance bindArr :: Bind ((->) r) where -- | - Right Identity: `x >>= pure = x` class (Applicative m, Bind m) <= Monad m -instance monadArr :: Monad ((->) r) +instance monadFn :: Monad ((->) r) + +instance monadArray :: Monad [] -- | `liftM1` provides a default implementation of `(<$>)` for any -- | [`Monad`](#monad), without using `(<$>)` as provided by the @@ -413,7 +454,7 @@ instance semigroupString :: Semigroup String where instance semigroupUnit :: Semigroup Unit where append _ _ = unit -instance semigroupArr :: (Semigroup s') => Semigroup (s -> s') where +instance semigroupFn :: (Semigroup s') => Semigroup (s -> s') where append f g = \x -> f x <> g x instance semigroupOrdering :: Semigroup Ordering where @@ -421,6 +462,9 @@ instance semigroupOrdering :: Semigroup Ordering where append GT _ = GT append EQ y = y +instance semigroupArray :: Semigroup [a] where + append = concatArray + foreign import concatString """ function concatString(s1) { @@ -430,6 +474,15 @@ foreign import concatString } """ :: String -> String -> String +foreign import concatArray + """ + function concatArray (xs) { + return function (ys) { + return xs.concat(ys); + }; + } + """ :: forall a. [a] -> [a] -> [a] + infixl 6 + infixl 7 * @@ -455,11 +508,17 @@ class Semiring a where mul :: a -> a -> a one :: a +instance semiringInt :: Semiring Int where + add = intAdd + zero = 0 + mul = intMul + one = 1 + instance semiringNumber :: Semiring Number where add = numAdd - zero = 0 + zero = 0.0 mul = numMul - one = 1 + one = 1.0 instance semiringUnit :: Semiring Unit where add _ _ = unit @@ -485,6 +544,9 @@ infixl 6 - class (Semiring a) <= Ring a where sub :: a -> a -> a +instance ringInt :: Ring Int where + sub = intSub + instance ringNumber :: Ring Number where sub = numSub @@ -510,9 +572,13 @@ class (Semiring a) <= ModuloSemiring a where div :: a -> a -> a mod :: a -> a -> a +instance moduloSemiringInt :: ModuloSemiring Int where + div = intDiv + mod = intMod + instance moduloSemiringNumber :: ModuloSemiring Number where div = numDiv - mod _ _ = 0 + mod _ _ = 0.0 instance moduloSemiringUnit :: ModuloSemiring Unit where div _ _ = unit @@ -548,6 +614,51 @@ instance numNumber :: Num Number instance numUnit :: Num Unit +foreign import intAdd + """ + function intAdd(x) { + return function(y) { + return (x + y)|0; + }; + } + """ :: Int -> Int -> Int + +foreign import intMul + """ + function intMul(x) { + return function(y) { + return (x * y)|0; + }; + } + """ :: Int -> Int -> Int + +foreign import intDiv + """ + function intDiv(x) { + return function(y) { + return (x / y)|0; + }; + } + """ :: Int -> Int -> Int + +foreign import intMod + """ + function intMod(x) { + return function(y) { + return x % y; + }; + } + """ :: Int -> Int -> Int + +foreign import intSub + """ + function intSub(x) { + return function(y) { + return (x - y)|0; + }; + } + """ :: Int -> Int -> Int + foreign import numAdd """ function numAdd(n1) { @@ -607,9 +718,15 @@ class Eq a where instance eqBoolean :: Eq Boolean where eq = refEq +instance eqInt :: Eq Int where + eq = refEq + instance eqNumber :: Eq Number where eq = refEq +instance eqChar :: Eq Char where + eq = refEq + instance eqString :: Eq String where eq = refEq @@ -677,10 +794,10 @@ class (Eq a) <= Ord a where compare :: a -> a -> Ordering instance ordBoolean :: Ord Boolean where - compare false false = EQ - compare false true = LT - compare true true = EQ - compare true false = GT + compare = unsafeCompare + +instance ordInt :: Ord Int where + compare = unsafeCompare instance ordNumber :: Ord Number where compare = unsafeCompare @@ -688,6 +805,9 @@ instance ordNumber :: Ord Number where instance ordString :: Ord String where compare = unsafeCompare +instance ordChar :: Ord Char where + compare = unsafeCompare + instance ordUnit :: Ord Unit where compare _ _ = EQ @@ -918,9 +1038,15 @@ instance showBoolean :: Show Boolean where show true = "true" show false = "false" +instance showInt :: Show Int where + show = showIntImpl + instance showNumber :: Show Number where show = showNumberImpl +instance showChar :: Show Char where + show = showCharImpl + instance showString :: Show String where show = showStringImpl @@ -935,13 +1061,27 @@ instance showOrdering :: Show Ordering where show GT = "GT" show EQ = "EQ" +foreign import showIntImpl + """ + function showIntImpl(n) { + return n.toString(); + } + """ :: Int -> String + foreign import showNumberImpl """ function showNumberImpl(n) { - return n.toString(); + return n === (n|0) ? n + ".0" : n.toString(); } """ :: Number -> String +foreign import showCharImpl + """ + function showCharImpl(c) { + return c === "'" ? "'\\''" : "'" + c + "'"; + } + """ :: Char -> String + foreign import showStringImpl """ function showStringImpl(s) {