Skip to content

Commit

Permalink
Merge pull request #175 from purescript/compiler/0.12
Browse files Browse the repository at this point in the history
Update for PureScript 0.12
  • Loading branch information
garyb authored May 22, 2018
2 parents e903fd2 + c276ea0 commit da43e7d
Show file tree
Hide file tree
Showing 41 changed files with 1,067 additions and 60 deletions.
38 changes: 22 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
The MIT License (MIT)
Copyright 2018 PureScript

Copyright (c) 2015 PureScript
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "purescript-prelude",
"homepage": "https://github.com/purescript/purescript-prelude",
"description": "The PureScript Prelude",
"license": "MIT",
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "git://github.com/purescript/purescript-prelude.git"
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "eslint src && pulp build -- --censor-lib --strict",
"test": "pulp test"
"test": "pulp test --no-check-main"
},
"devDependencies": {
"eslint": "^3.17.1",
"purescript-psa": "^0.5.0-rc.1",
"pulp": "^10.0.4",
"rimraf": "^2.6.1"
"eslint": "^4.19.1",
"purescript-psa": "^0.6.0",
"pulp": "^12.2.0",
"rimraf": "^2.6.2"
}
}
2 changes: 1 addition & 1 deletion src/Control/Applicative.purs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Data.Unit (Unit, unit)
-- | Instances must satisfy the following laws in addition to the `Apply`
-- | laws:
-- |
-- | - Identity: `(pure id) <*> v = v`
-- | - Identity: `(pure identity) <*> v = v`
-- | - Composition: `pure (<<<) <*> f <*> g <*> h = f <*> (g <*> h)`
-- | - Homomorphism: `(pure f) <*> (pure x) = pure (f x)`
-- | - Interchange: `u <*> (pure y) = (pure (_ $ y)) <*> u`
Expand Down
4 changes: 2 additions & 2 deletions src/Control/Apply.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Control.Apply

import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
import Data.Function (const)
import Control.Category (id)
import Control.Category (identity)

-- | The `Apply` class provides the `(<*>)` which is used to apply a function
-- | to an argument under a type constructor.
Expand Down Expand Up @@ -53,7 +53,7 @@ infixl 4 applyFirst as <*

-- | Combine two effectful actions, keeping only the result of the second.
applySecond :: forall a b f. Apply f => f a -> f b -> f b
applySecond a b = const id <$> a <*> b
applySecond a b = const identity <$> a <*> b

infixl 4 applySecond as *>

Expand Down
4 changes: 2 additions & 2 deletions src/Control/Bind.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Control.Bind

import Control.Applicative (class Applicative, liftA1, pure, unless, when)
import Control.Apply (class Apply, apply, (*>), (<*), (<*>))
import Control.Category (id)
import Control.Category (identity)

import Data.Function (flip)
import Data.Functor (class Functor, map, void, ($>), (<#>), (<$), (<$>))
Expand Down Expand Up @@ -81,7 +81,7 @@ instance discardUnit :: Discard Unit where

-- | Collapse two applications of a monadic type constructor into one.
join :: forall a m. Bind m => m (m a) -> m a
join m = m >>= id
join m = m >>= identity

-- | Forwards Kleisli composition.
-- |
Expand Down
8 changes: 4 additions & 4 deletions src/Control/Category.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Control.Category
( class Category, id
( class Category, identity
, module Control.Semigroupoid
) where

Expand All @@ -12,9 +12,9 @@ import Control.Semigroupoid (class Semigroupoid, compose, (<<<), (>>>))
-- | Instances must satisfy the following law in addition to the
-- | `Semigroupoid` law:
-- |
-- | - Identity: `id <<< p = p <<< id = p`
-- | - Identity: `identity <<< p = p <<< identity = p`
class Semigroupoid a <= Category a where
id :: forall t. a t t
identity :: forall t. a t t

instance categoryFn :: Category (->) where
id x = x
identity x = x
2 changes: 1 addition & 1 deletion src/Control/Semigroupoid.purs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Control.Semigroupoid where

-- | A `Semigroupoid` is similar to a [`Category`](#category) but does not
-- | require an identity element `id`, just composable morphisms.
-- | require an identity element `identity`, just composable morphisms.
-- |
-- | `Semigroupoid`s must satisfy the following law:
-- |
Expand Down
21 changes: 20 additions & 1 deletion src/Data/BooleanAlgebra.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
module Data.BooleanAlgebra
( class BooleanAlgebra
, module Data.HeytingAlgebra
, class BooleanAlgebraRecord
) where

import Data.HeytingAlgebra (class HeytingAlgebra, ff, tt, implies, conj, disj, not, (&&), (||))
import Data.HeytingAlgebra (class HeytingAlgebra, class HeytingAlgebraRecord, ff, tt, implies, conj, disj, not, (&&), (||))
import Data.Symbol (class IsSymbol)
import Data.Unit (Unit)
import Prim.Row as Row
import Prim.RowList as RL

-- | The `BooleanAlgebra` type class represents types that behave like boolean
-- | values.
Expand All @@ -19,3 +23,18 @@ class HeytingAlgebra a <= BooleanAlgebra a
instance booleanAlgebraBoolean :: BooleanAlgebra Boolean
instance booleanAlgebraUnit :: BooleanAlgebra Unit
instance booleanAlgebraFn :: BooleanAlgebra b => BooleanAlgebra (a -> b)
instance booleanAlgebraRecord :: (RL.RowToList row list, BooleanAlgebraRecord list row row) => BooleanAlgebra (Record row)

-- | A class for records where all fields have `BooleanAlgebra` instances, used
-- | to implement the `BooleanAlgebra` instance for records.
class HeytingAlgebraRecord rowlist row subrow <= BooleanAlgebraRecord rowlist row subrow | rowlist -> subrow

instance booleanAlgebraRecordNil :: BooleanAlgebraRecord RL.Nil row ()

instance booleanAlgebraRecordCons
:: ( IsSymbol key
, Row.Cons key focus subrowTail subrow
, BooleanAlgebraRecord rowlistTail row subrowTail
, BooleanAlgebra focus
)
=> BooleanAlgebraRecord (RL.Cons key focus rowlistTail) row subrow
21 changes: 20 additions & 1 deletion src/Data/CommutativeRing.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ module Data.CommutativeRing
( class CommutativeRing
, module Data.Ring
, module Data.Semiring
, class CommutativeRingRecord
) where

import Data.Ring (class Ring)
import Data.Ring (class Ring, class RingRecord)
import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))
import Data.Symbol (class IsSymbol)
import Data.Unit (Unit)
import Prim.Row as Row
import Prim.RowList as RL

-- | The `CommutativeRing` class is for rings where multiplication is
-- | commutative.
Expand All @@ -21,3 +25,18 @@ instance commutativeRingInt :: CommutativeRing Int
instance commutativeRingNumber :: CommutativeRing Number
instance commutativeRingUnit :: CommutativeRing Unit
instance commutativeRingFn :: CommutativeRing b => CommutativeRing (a -> b)
instance commutativeRingRecord :: (RL.RowToList row list, CommutativeRingRecord list row row) => CommutativeRing (Record row)

-- | A class for records where all fields have `CommutativeRing` instances, used
-- | to implement the `CommutativeRing` instance for records.
class RingRecord rowlist row subrow <= CommutativeRingRecord rowlist row subrow | rowlist -> subrow

instance commutativeRingRecordNil :: CommutativeRingRecord RL.Nil row ()

instance commutativeRingRecordCons
:: ( IsSymbol key
, Row.Cons key focus subrowTail subrow
, CommutativeRingRecord rowlistTail row subrowTail
, CommutativeRing focus
)
=> CommutativeRingRecord (RL.Cons key focus rowlistTail) row subrow
2 changes: 1 addition & 1 deletion src/Data/DivisionRing.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Data.DivisionRing
, module Data.Semiring
) where

import Data.EuclideanRing ((/))
import Data.Ring (class Ring, negate, sub)
import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))
import Data.EuclideanRing ((/))

-- | The `DivisionRing` class is for non-zero rings in which every non-zero
-- | element has a multiplicative inverse. Division rings are sometimes also
Expand Down
31 changes: 31 additions & 0 deletions src/Data/Eq.purs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
module Data.Eq
( class Eq, eq, (==), notEq, (/=)
, class Eq1, eq1, notEq1
, class EqRecord, eqRecord
) where

import Data.HeytingAlgebra ((&&))
import Data.Symbol (class IsSymbol, SProxy(..), reflectSymbol)
import Data.Unit (Unit)
import Data.Void (Void)
import Prim.Row as Row
import Prim.RowList as RL
import Record.Unsafe (unsafeGet)
import Type.Data.RowList (RLProxy(..))

-- | The `Eq` type class represents types which support decidable equality.
-- |
Expand Down Expand Up @@ -54,6 +61,9 @@ instance eqVoid :: Eq Void where
instance eqArray :: Eq a => Eq (Array a) where
eq = eqArrayImpl eq

instance eqRec :: (RL.RowToList row list, EqRecord list row) => Eq (Record row) where
eq = eqRecord (RLProxy :: RLProxy list)

foreign import refEq :: forall a. a -> a -> Boolean
foreign import eqArrayImpl :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Boolean

Expand All @@ -66,3 +76,24 @@ instance eq1Array :: Eq1 Array where

notEq1 :: forall f a. Eq1 f => Eq a => f a -> f a -> Boolean
notEq1 x y = (x `eq1` y) == false

-- | A class for records where all fields have `Eq` instances, used to implement
-- | the `Eq` instance for records.
class EqRecord rowlist row where
eqRecord :: RLProxy rowlist -> Record row -> Record row -> Boolean

instance eqRowNil :: EqRecord RL.Nil row where
eqRecord _ _ _ = true

instance eqRowCons
:: ( EqRecord rowlistTail row
, Row.Cons key focus rowTail row
, IsSymbol key
, Eq focus
)
=> EqRecord (RL.Cons key focus rowlistTail) row where
eqRecord _ ra rb = (get ra == get rb) && tail
where
key = reflectSymbol (SProxy :: SProxy key)
get = unsafeGet key :: Record row -> focus
tail = eqRecord (RLProxy :: RLProxy rowlistTail) ra rb
10 changes: 7 additions & 3 deletions src/Data/EuclideanRing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ exports.intDegree = function (x) {
return Math.min(Math.abs(x), 2147483647);
};

// See the Euclidean definition in
// https://en.m.wikipedia.org/wiki/Modulo_operation.
exports.intDiv = function (x) {
return function (y) {
/* jshint bitwise: false */
return x / y | 0;
if (y === 0) return 0;
return y > 0 ? Math.floor(x / y) : -Math.floor(x / -y);
};
};

exports.intMod = function (x) {
return function (y) {
return x % y;
if (y === 0) return 0;
var yy = Math.abs(y);
return ((x % yy) + yy) % yy;
};
};

Expand Down
19 changes: 19 additions & 0 deletions src/Data/EuclideanRing.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))
-- | for `degree` is simply `const 1`. In fact, unless there's a specific
-- | reason not to, `Field` types should normally use this definition of
-- | `degree`.
-- |
-- | The `EuclideanRing Int` instance is one of the most commonly used
-- | `EuclideanRing` instances and deserves a little more discussion. In
-- | particular, there are a few different sensible law-abiding implementations
-- | to choose from, with slightly different behaviour in the presence of
-- | negative dividends or divisors. The most common definitions are "truncating"
-- | division, where the result of `a / b` is rounded towards 0, and "Knuthian"
-- | or "flooring" division, where the result of `a / b` is rounded towards
-- | negative infinity. A slightly less common, but arguably more useful, option
-- | is "Euclidean" division, which is defined so as to ensure that ``a `mod` b``
-- | is always nonnegative. With Euclidean division, `a / b` rounds towards
-- | negative infinity if the divisor is positive, and towards positive infinity
-- | if the divisor is negative. Note that all three definitions are identical if
-- | we restrict our attention to nonnegative dividends and divisors.
-- |
-- | In versions 1.x, 2.x, and 3.x of the Prelude, the `EuclideanRing Int`
-- | instance used truncating division. As of 4.x, the `EuclideanRing Int`
-- | instance uses Euclidean division. Additional functions `quot` and `rem` are
-- | supplied if truncating division is desired.
class CommutativeRing a <= EuclideanRing a where
degree :: a -> Int
div :: a -> a -> a
Expand Down
29 changes: 21 additions & 8 deletions src/Data/Field.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ import Data.Semiring (class Semiring, add, mul, one, zero, (*), (+))

-- | The `Field` class is for types that are (commutative) fields.
-- |
-- | Instances must satisfy the following law in addition to the
-- | `EuclideanRing` laws:
-- | Mathematically, a field is a ring which is commutative and in which every
-- | nonzero element has a multiplicative inverse; these conditions correspond
-- | to the `CommutativeRing` and `DivisionRing` classes in PureScript
-- | respectively. However, the `Field` class has `EuclideanRing` and
-- | `DivisionRing` as superclasses, which seems like a stronger requirement
-- | (since `CommutativeRing` is a superclass of `EuclideanRing`). In fact, it
-- | is not stronger, since any type which has law-abiding `CommutativeRing`
-- | and `DivisionRing` instances permits exactly one law-abiding
-- | `EuclideanRing` instance. We use a `EuclideanRing` superclass here in
-- | order to ensure that a `Field` constraint on a function permits you to use
-- | `div` on that type, since `div` is a member of `EuclideanRing`.
-- |
-- | - Non-zero multiplicative inverse: ``a `mod` b = zero`` for all `a` and `b`
-- | This class has no laws or members of its own; it exists as a convenience,
-- | so a single constraint can be used when field-like behaviour is expected.
-- |
-- | If a type has a `Field` instance, it should also have a `DivisionRing`
-- | instance. In a future release, `DivisionRing` may become a superclass of
-- | `Field`.
class EuclideanRing a <= Field a
-- | This module also defines a single `Field` instance for any type which has
-- | both `EuclideanRing` and `DivisionRing` instances. Any other instance
-- | would overlap with this instance, so no other `Field` instances should be
-- | defined in libraries. Instead, simply define `EuclideanRing` and
-- | `DivisionRing` instances, and this will permit your type to be used with a
-- | `Field` constraint.
class (EuclideanRing a, DivisionRing a) <= Field a

instance fieldNumber :: Field Number
instance field :: (EuclideanRing a, DivisionRing a) => Field a
2 changes: 1 addition & 1 deletion src/Data/Function.purs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Data.Function
, module Control.Category
) where

import Control.Category (id, compose, (<<<), (>>>))
import Control.Category (identity, compose, (<<<), (>>>))
import Data.Boolean (otherwise)
import Data.Ord ((<=))
import Data.Ring ((-))
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Functor.purs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Data.Unit (Unit, unit)
-- |
-- | Instances must satisfy the following laws:
-- |
-- | - Identity: `map id = id`
-- | - Identity: `map identity = identity`
-- | - Composition: `map (f <<< g) = map f <<< map g`
class Functor f where
map :: forall a b. (a -> b) -> f a -> f b
Expand Down
Loading

0 comments on commit da43e7d

Please sign in to comment.