Skip to content

Commit

Permalink
Merge pull request #149 from purescript/compiler/0.12
Browse files Browse the repository at this point in the history
Updates for 0.12
  • Loading branch information
garyb committed May 23, 2018
2 parents ab3189f + ea86885 commit 61e9c15
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/bower_components/
/node_modules/
/output/
package-lock.json
70 changes: 20 additions & 50 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,56 +1,26 @@
Copyright (c) 2014 PureScript
Copyright 2018 PureScript

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:
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

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.
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 Glasgow Haskell Compiler License

Copyright 2004, The University Court of the University of Glasgow.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

- 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.

- Neither name of the University nor the names of its contributors may be
used to endorse or promote products derived from this software without
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 UNIVERSITY COURT OF THE UNIVERSITY OF
GLASGOW AND THE 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
UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE 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.
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.
29 changes: 17 additions & 12 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"authors": [
"Phil Freeman <paf31@cantab.net>"
],
"description": "Linked lists",
"license": "MIT",
"license": "BSD-3-Clause",
"repository": {
"type": "git",
"url": "git://github.com/purescript/purescript-lists.git"
Expand All @@ -20,17 +19,23 @@
"package.json"
],
"dependencies": {
"purescript-lazy": "^3.0.0",
"purescript-nonempty": "^4.0.0",
"purescript-tailrec": "^3.3.0",
"purescript-unfoldable": "^3.0.0",
"purescript-partial": "^1.0.0",
"purescript-foldable-traversable": "^3.4.0"
"purescript-bifunctors": "^4.0.0",
"purescript-control": "^4.0.0",
"purescript-foldable-traversable": "^4.0.0",
"purescript-lazy": "^4.0.0",
"purescript-maybe": "^4.0.0",
"purescript-newtype": "^3.0.0",
"purescript-nonempty": "^5.0.0",
"purescript-partial": "^2.0.0",
"purescript-prelude": "^4.0.0",
"purescript-tailrec": "^4.0.0",
"purescript-tuples": "^5.0.0",
"purescript-unfoldable": "^4.0.0"
},
"devDependencies": {
"purescript-arrays": "^4.0.0",
"purescript-assert": "^3.0.0",
"purescript-console": "^3.0.0",
"purescript-math": "^2.0.0"
"purescript-arrays": "^5.0.0",
"purescript-assert": "^4.0.0",
"purescript-console": "^4.0.0",
"purescript-math": "^2.1.1"
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build -- --censor-lib --strict",
"test": "pulp test"
"test": "pulp test --check-main-type Effect.Effect"
},
"devDependencies": {
"pulp": "^11.0.0",
"purescript-psa": "^0.5.1",
"rimraf": "^2.6.1"
"pulp": "^12.2.0",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.2"
}
}
14 changes: 12 additions & 2 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ reverse = go Nil
-- |
-- | Running time: `O(n)`, where `n` is the total number of elements.
concat :: forall a. List (List a) -> List a
concat = (_ >>= id)
concat = (_ >>= identity)

-- | Apply a function to each element in a list, and flatten the results
-- | into a single, new list.
Expand Down Expand Up @@ -423,7 +423,7 @@ mapMaybe f = go Nil
-- | Filter a list of optional values, keeping only the elements which contain
-- | a value.
catMaybes :: forall a. List (Maybe a) -> List a
catMaybes = mapMaybe id
catMaybes = mapMaybe identity


-- | Apply a function to each element and its index in a list starting at 0.
Expand Down Expand Up @@ -617,6 +617,16 @@ partition p xs = foldr select { no: Nil, yes: Nil } xs
then { no, yes: x : yes }
else { no: x : no, yes }

-- | Returns all final segments of the argument, longest first. For example,
-- |
-- | ```purescript
-- | tails (1 : 2 : 3 : Nil) == ((1 : 2 : 3 : Nil) : (2 : 3 : Nil) : (3 : Nil) : (Nil) : Nil)
-- | ```
-- | Running time: `O(n)`
tails :: forall a. List a -> List (List a)
tails Nil = singleton Nil
tails list@(Cons _ tl)= list : tails tl

--------------------------------------------------------------------------------
-- Set-like operations ---------------------------------------------------------
--------------------------------------------------------------------------------
Expand Down
9 changes: 5 additions & 4 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ reverse xs = Z.defer \_ -> foldl (flip cons) nil xs
-- |
-- | Running time: `O(n)`, where `n` is the total number of elements.
concat :: forall a. List (List a) -> List a
concat = (_ >>= id)
concat = (_ >>= identity)

-- | Apply a function to each element in a list, and flatten the results
-- | into a single, new list.
Expand Down Expand Up @@ -463,7 +463,7 @@ mapMaybe f = List <<< map go <<< unwrap
-- | Filter a list of optional values, keeping only the elements which contain
-- | a value.
catMaybes :: forall a. List (Maybe a) -> List a
catMaybes = mapMaybe id
catMaybes = mapMaybe identity

--------------------------------------------------------------------------------
-- Sorting ---------------------------------------------------------------------
Expand Down Expand Up @@ -509,10 +509,11 @@ slice start end xs = take (end - start) (drop start xs)
-- |
-- | Running time: `O(n)` where `n` is the number of elements to take.
take :: forall a. Int -> List a -> List a
take n = List <<< map (go n) <<< unwrap
take n = if n <= 0
then const nil
else List <<< map (go n) <<< unwrap
where
go :: Int -> Step a -> Step a
go i _ | i <= 0 = Nil
go _ Nil = Nil
go n' (Cons x xs) = Cons x (take (n' - 1) xs)

Expand Down
10 changes: 8 additions & 2 deletions src/Data/List/Lazy/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWit
import Data.FunctorWithIndex (class FunctorWithIndex)
import Data.Lazy (Lazy, defer, force)
import Data.Maybe (Maybe(..))
import Data.Monoid (class Monoid, mempty)
import Data.Newtype (class Newtype, unwrap)
import Data.NonEmpty (NonEmpty, (:|))
import Data.NonEmpty as NE
Expand All @@ -25,6 +24,7 @@ import Data.Traversable (class Traversable, traverse, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex)
import Data.Tuple (Tuple(..), snd)
import Data.Unfoldable (class Unfoldable)
import Data.Unfoldable1 (class Unfoldable1)

-- | A lazy linked list.
newtype List a = List (Lazy (Step a))
Expand Down Expand Up @@ -141,6 +141,12 @@ instance foldableWithIndexList :: FoldableWithIndex Int List where
snd <<< foldl (\(Tuple i b) a -> Tuple (i + 1) (f i b a)) (Tuple 0 acc)
foldMapWithIndex f = foldlWithIndex (\i acc -> append acc <<< f i) mempty

instance unfoldable1List :: Unfoldable1 List where
unfoldr1 = go where
go f b = Z.defer \_ -> case f b of
Tuple a (Just b') -> a : go f b'
Tuple a Nothing -> a : nil

instance unfoldableList :: Unfoldable List where
unfoldr = go where
go f b = Z.defer \_ -> case f b of
Expand All @@ -151,7 +157,7 @@ instance traversableList :: Traversable List where
traverse f =
foldr (\a b -> cons <$> f a <*> b) (pure nil)

sequence = traverse id
sequence = traverse identity

instance traversableWithIndexList :: TraversableWithIndex Int List where
traverseWithIndex f =
Expand Down
2 changes: 1 addition & 1 deletion src/Data/List/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ catMaybes :: forall a. NonEmptyList (Maybe a) -> L.List a
catMaybes = lift L.catMaybes

concat :: forall a. NonEmptyList (NonEmptyList a) -> NonEmptyList a
concat = (_ >>= id)
concat = (_ >>= identity)

concatMap :: forall a b. (a -> NonEmptyList b) -> NonEmptyList a -> NonEmptyList b
concatMap = flip bind
Expand Down
19 changes: 11 additions & 8 deletions src/Data/List/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Data.Foldable (class Foldable, foldl, foldr, intercalate)
import Data.FoldableWithIndex (class FoldableWithIndex, foldlWithIndex, foldrWithIndex)
import Data.FunctorWithIndex (class FunctorWithIndex)
import Data.Maybe (Maybe(..))
import Data.Monoid (class Monoid, mempty)
import Data.Newtype (class Newtype)
import Data.NonEmpty (NonEmpty, (:|))
import Data.NonEmpty as NE
Expand All @@ -26,6 +25,7 @@ import Data.Traversable (class Traversable, traverse)
import Data.TraversableWithIndex (class TraversableWithIndex)
import Data.Tuple (Tuple(..), snd)
import Data.Unfoldable (class Unfoldable)
import Data.Unfoldable1 (class Unfoldable1)

data List a = Nil | Cons a (List a)

Expand Down Expand Up @@ -99,6 +99,13 @@ instance foldableWithIndexList :: FoldableWithIndex Int List where
snd <<< foldl (\(Tuple i b) a -> Tuple (i + 1) (f i b a)) (Tuple 0 acc)
foldMapWithIndex f = foldlWithIndex (\i acc -> append acc <<< f i) mempty

instance unfoldable1List :: Unfoldable1 List where
unfoldr1 f b = go b Nil
where
go source memo = case f source of
Tuple one (Just rest) -> go rest (one : memo)
Tuple one Nothing -> foldl (flip (:)) Nil (one : memo)

instance unfoldableList :: Unfoldable List where
unfoldr f b = go b Nil
where
Expand All @@ -108,7 +115,7 @@ instance unfoldableList :: Unfoldable List where

instance traversableList :: Traversable List where
traverse f = map (foldl (flip (:)) Nil) <<< foldl (\acc -> lift2 (flip (:)) acc <<< f) (pure Nil)
sequence = traverse id
sequence = traverse identity

instance traversableWithIndexList :: TraversableWithIndex Int List where
traverseWithIndex f =
Expand Down Expand Up @@ -204,14 +211,10 @@ derive newtype instance foldableNonEmptyList :: Foldable NonEmptyList

derive newtype instance traversableNonEmptyList :: Traversable NonEmptyList

instance foldable1NonEmptyList :: Foldable1 NonEmptyList where
fold1 (NonEmptyList (a :| as)) =
foldl append a as
foldMap1 f (NonEmptyList (a :| as)) =
foldl (\acc -> append acc <<< f) (f a) as
derive newtype instance foldable1NonEmptyList :: Foldable1 NonEmptyList

instance traversable1NonEmptyList :: Traversable1 NonEmptyList where
traverse1 f (NonEmptyList (a :| as)) =
foldl (\acc -> lift2 (flip nelCons) acc <<< f) (pure <$> f a) as
<#> case _ of NonEmptyList (x :| xs) → foldl (flip nelCons) (pure x) xs
sequence1 = traverse1 id
sequence1 = traverse1 identity
6 changes: 3 additions & 3 deletions src/Data/List/ZipList.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ module Data.List.ZipList
) where

import Prelude
import Prim.TypeError (class Fail, Text)
import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Plus (class Plus)
import Data.Foldable (class Foldable)
import Data.List.Lazy (List, repeat, zipWith)
import Data.Monoid (class Monoid, mempty)
import Data.Newtype (class Newtype)
import Data.Traversable (class Traversable)
import Partial.Unsafe (unsafeCrashWith)
Expand Down Expand Up @@ -54,12 +54,12 @@ instance plusZipList :: Plus ZipList where
instance alternativeZipList :: Alternative ZipList

instance zipListIsNotBind
:: Fail """
:: Fail (Text """
ZipList is not Bind. Any implementation would break the associativity law.

Possible alternatives:
Data.List.List
Data.List.Lazy.List
"""
""")
=> Bind ZipList where
bind = unsafeCrashWith "bind: unreachable"
18 changes: 12 additions & 6 deletions test/Test/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module Test.Data.List (testList) where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Foldable (foldMap, foldl)
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
import Data.List (List(..), (..), stripPrefix, Pattern(..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, partition, span, dropWhile, drop, dropEnd, takeWhile, take, takeEnd, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
Expand All @@ -15,10 +13,13 @@ import Data.Traversable (traverse)
import Data.TraversableWithIndex (traverseWithIndex)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (replicate, replicateA, unfoldr)
import Data.Unfoldable1 (unfoldr1)
import Effect (Effect)
import Effect.Console (log)
import Partial.Unsafe (unsafePartial)
import Test.Assert (ASSERT, assert)
import Test.Assert (assert)

testList :: forall eff. Eff (assert :: ASSERT, console :: CONSOLE | eff) Unit
testList :: Effect Unit
testList = do
let l = fromFoldable

Expand Down Expand Up @@ -357,8 +358,11 @@ testList = do
log "unfoldr should maintain order"
assert $ (1..5) == unfoldr step 1

log "unfoldr1 should maintain order"
assert $ (1..5) == unfoldr1 step1 1

log "map should maintain order"
assert $ (1..5) == map id (1..5)
assert $ (1..5) == map identity (1..5)

log "transpose"
assert $ transpose (l [l [1,2,3], l[4,5,6], l [7,8,9]]) ==
Expand Down Expand Up @@ -388,11 +392,13 @@ testList = do
log "append should be stack-safe"
void $ pure $ xs <> xs


step :: Int -> Maybe (Tuple Int Int)
step 6 = Nothing
step n = Just (Tuple n (n + 1))

step1 :: Int -> Tuple Int (Maybe Int)
step1 n = Tuple n (if n >= 5 then Nothing else Just (n + 1))

nil :: List Int
nil = Nil

Expand Down
Loading

0 comments on commit 61e9c15

Please sign in to comment.