Skip to content

Commit

Permalink
Remove dependency on Protolude
Browse files Browse the repository at this point in the history
The upstream maintainers for Protolude are not very active and it is
ALWAYS slow to get updated for new compilers.

Lets do without it.
  • Loading branch information
erikd committed May 14, 2024
1 parent 44538c8 commit 5251313
Show file tree
Hide file tree
Showing 25 changed files with 95 additions and 94 deletions.
2 changes: 0 additions & 2 deletions cardano-prelude-test/cardano-prelude-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ library
, text
, time
default-language: Haskell2010
default-extensions: NoImplicitPrelude
ghc-options: -Wall

if (!flag(development))
Expand Down Expand Up @@ -78,7 +77,6 @@ test-suite prelude-tests
, hedgehog
, text
default-language: Haskell2010
default-extensions: NoImplicitPrelude
ghc-options: -threaded
-rtsopts
-Wall
Expand Down
1 change: 1 addition & 0 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Base16.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import qualified Data.Attoparsec.ByteString.Lazy as PLB
import qualified Data.ByteString.Base16.Lazy as B16
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy.Char8 as LB
import Prelude hiding ((.))
import Text.Printf (printf)

--------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import Hedgehog (Gen)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

import Prelude hiding ((.))

genBytes :: Int -> Gen ByteString
genBytes n = Gen.bytes (Range.singleton n)

Expand Down
1 change: 1 addition & 0 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Golden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import qualified Data.Text as T
import qualified Data.Text.Lazy as LT
import Data.Text.Lazy.Builder (toLazyText)
import Formatting.Buildable (build)
import Prelude hiding ((.))
import qualified Text.JSON.Canonical as Canonical

import Hedgehog (
Expand Down
2 changes: 2 additions & 0 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Cardano.Prelude

import qualified Crypto.Random as Rand

import Prelude hiding ((.))

import Test.QuickCheck (Gen)
import qualified Test.QuickCheck as QC

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import Cardano.Prelude

import Data.ByteString (pack)
import qualified Data.ByteString.Lazy as BL (ByteString, pack)
import qualified Data.Text as Text
import Formatting (build, sformat)
import Prelude hiding ((.))
import Test.QuickCheck (Arbitrary (..), Gen, listOf, scale, shuffle, vector)
import Test.QuickCheck.Gen (unGen)
import Test.QuickCheck.Instances.ByteString ()
Expand Down Expand Up @@ -57,10 +59,10 @@ instance Arbitrary a => Arbitrary (SmallGenerator a) where
-- there's not enough elements.
sublistN :: Int -> [a] -> Gen [a]
sublistN n xs = do
let len = length xs
let len = Prelude.length xs
if len < n
then
panic $
error . Text.unpack $
sformat
( "sublistN: requested "
. build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ where

import Cardano.Prelude

import Control.Monad (fail)
import qualified Data.Semigroup as Semigroup
import qualified Data.Text as Text
import Prelude hiding ((.))

import qualified Test.Hspec as Hspec
import Test.QuickCheck (Property, counterexample, property, (.&&.), (===))
Expand All @@ -63,15 +64,15 @@ qcIsJust Nothing = qcFail "expected Just, got Nothing"

qcIsNothing :: Show a => Maybe a -> Property
qcIsNothing Nothing = property True
qcIsNothing (Just x) = qcFail ("expected Nothing, got Just (" <> show x <> ")")
qcIsNothing (Just x) = qcFail ("expected Nothing, got Just (" <> Text.pack (show x) <> ")")

qcIsLeft :: Show b => Either a b -> Property
qcIsLeft (Left _) = property True
qcIsLeft (Right x) = qcFail ("expected Left, got Right (" <> show x <> ")")
qcIsLeft (Right x) = qcFail ("expected Left, got Right (" <> Text.pack (show x) <> ")")

qcIsRight :: Show a => Either a b -> Property
qcIsRight (Right _) = property True
qcIsRight (Left x) = qcFail ("expected Right, got Left (" <> show x <> ")")
qcIsRight (Left x) = qcFail ("expected Right, got Left (" <> Text.pack (show x) <> ")")

qcElem :: (Show a, Eq a, Show (t a), Foldable t) => a -> t a -> Property
qcElem x xs =
Expand All @@ -87,7 +88,7 @@ qcNotElem x xs =

-- | A property that is always false
qcFail :: Text -> Property
qcFail s = counterexample (toS s) False
qcFail s = counterexample (Text.unpack s) False

--------------------------------------------------------------------------------
-- Monadic testing
Expand All @@ -104,7 +105,7 @@ assertProperty st text = unless st $ stopProperty text

-- | Stop 'PropertyM' execution with given reason. The property will fail.
stopProperty :: Monad m => Text -> PropertyM m a
stopProperty msg = stop failed {reason = toS msg}
stopProperty msg = stop failed {reason = Text.unpack msg}

-- | Use 'stopProperty' if the value is 'Nothing' or return something
-- it the value is 'Just'.
Expand All @@ -116,7 +117,7 @@ maybeStopProperty msg = \case
-- | Split given list into chunks with size up to given value.
-- TODO: consider using `sumEquals maxSize (length items)`
splitIntoChunks :: Monad m => Word -> [a] -> PropertyM m [NonEmpty a]
splitIntoChunks 0 _ = panic "splitIntoChunks: maxSize is 0"
splitIntoChunks 0 _ = error "splitIntoChunks: maxSize is 0"
splitIntoChunks maxSize items = do
sizeMinus1 <- pick $ choose (0, maxSize - 1)
let (chunk, rest) = splitAt (fromIntegral sizeMinus1 + 1) items
Expand All @@ -141,7 +142,7 @@ expectedOne desc = \case
splitWord :: Word64 -> Word64 -> Gen [Word64]
splitWord total parts
| total < parts =
panic $
error $
"splitWord: can't split "
<> show total
<> " into "
Expand All @@ -166,7 +167,7 @@ sumEquals maxEl restSum = do
(el :) <$> sumEquals maxEl (restSum - el)

expectationError :: Text -> Hspec.Expectation
expectationError = fail . toS
expectationError = fail . Text.unpack

--------------------------------------------------------------------------------
-- Monoid/Semigroup laws
Expand Down
9 changes: 6 additions & 3 deletions cardano-prelude-test/src/Test/Cardano/Prelude/Tripping.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
Expand All @@ -20,10 +21,12 @@ where
import Cardano.Prelude

import Data.Aeson (FromJSON, ToJSON, decode, encode, fromJSON, toJSON)
import qualified Data.Map as Map
import Data.String (String, unlines)
import Data.Map qualified as Map
import Data.String (unlines)
import Data.Text.Internal.Builder (toLazyText)
import Data.Text.Lazy qualified as LazyText
import Formatting.Buildable (Buildable (..))
import Prelude hiding ((.))
import System.IO (hSetEncoding, utf8)
import qualified Text.JSON.Canonical as CanonicalJSON
import Text.Show.Pretty (Value (..), parseValue)
Expand Down Expand Up @@ -182,4 +185,4 @@ instance (Buildable e, Buildable a) => Buildable (Either e a) where
build (Right a) = build a

buildValue :: Buildable a => a -> Maybe Value
buildValue = parseValue . toS . toLazyText . build
buildValue = parseValue . LazyText.unpack . toLazyText . build
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ prop_Word8ListClosureTreeDepth =
failure
Just ct -> do
annotate $ unpack (renderTree ct renderClosure)
depth ct === (length xs) + 1
depth ct === (Cardano.Prelude.length xs) + 1

-- | Property: Specifying a 'TreeDepth' other than 'AnyDepth' should
-- appropriately limit the maximum depth of the 'Closure' 'Tree' generated.
Expand All @@ -74,7 +74,7 @@ prop_ClosureTreeHasSpecifiedDepth = withTests 500 $ property $ do
Just ct -> do
annotate $ unpack (renderTree ct renderClosure)
if depth ct < maxDepth
then depth ct === length xs + 1
then depth ct === Cardano.Prelude.length xs + 1
else depth ct === maxDepth

tests :: IO Bool
Expand Down
1 change: 0 additions & 1 deletion cardano-prelude-test/test/test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Main (
main,
) where

import Cardano.Prelude
import Test.Cardano.Prelude

import qualified Test.Cardano.Prelude.GHC.Heap.NormalFormSpec
Expand Down
4 changes: 1 addition & 3 deletions cardano-prelude/cardano-prelude.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ library
exposed-modules: Cardano.Prelude
Data.Semigroup.Action
other-modules: Cardano.Prelude.Base
Cardano.Prelude.Compat
Cardano.Prelude.Compat.ByteString.Short
Cardano.Prelude.Error
Cardano.Prelude.Formatting
Expand All @@ -47,18 +46,17 @@ library
, canonical-json >= 0.6.0.1
, cborg
, containers
, deepseq
, formatting
, ghc-heap
, ghc-prim
, integer-gmp
, microlens
, mtl
, protolude
, tagged
, text
, time
default-language: Haskell2010
default-extensions: NoImplicitPrelude
c-sources: cbits/hashset.c
cbits/worklist.c
cbits/closure_size.c
Expand Down
3 changes: 1 addition & 2 deletions cardano-prelude/src/Cardano/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module Cardano.Prelude (
)
where

import Cardano.Prelude.Base as X hiding (readEither)
import Cardano.Prelude.Compat as X (readEither)
import Cardano.Prelude.Base as X
import Cardano.Prelude.Compat.ByteString.Short as X
import Cardano.Prelude.Error as X
import Cardano.Prelude.Formatting as X
Expand Down
63 changes: 40 additions & 23 deletions cardano-prelude/src/Cardano/Prelude/Base.hs
Original file line number Diff line number Diff line change
@@ -1,61 +1,78 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExplicitNamespaces #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE Safe #-}

module Cardano.Prelude.Base (
module X,
HasLength (..),
identity,
putTextLn,
length,
scanl',
Cardano.Prelude.Base.length,
#if __GLASGOW_HASKELL__ >= 906
type (~)
#endif
)
where

import Protolude as X hiding (
Hashable,
Map,
hash,
hashUsing,
hashWithSalt,
identity,
length,
witness,
(.),
)
import qualified Protolude as Y

import Data.Map.Strict as X (Map)
import qualified Data.Text as T
import Data.List (scanl')
import Data.Text as X (Text)
import Data.Text qualified as Text
import Data.Text.IO qualified as Text

import Control.Category (id)
import Control.Category qualified as Category
import Control.Category as X hiding (id)
import Numeric.Natural as X

#if __GLASGOW_HASKELL__ >= 906
import Prelude (type (~))
#endif
import Control.Applicative as X (many)
import Control.Concurrent.MVar as X (MVar, newMVar)
import Control.DeepSeq as X (NFData (..), ($!!), force)
import Control.Exception as X (Exception, bracket)
import Control.Monad as X (liftM, unless)
import Control.Monad.Except as X (MonadError, throwError)
import Control.Monad.IO.Class as X (MonadIO (..))
import Data.ByteString as X (ByteString)
import Data.Bifunctor as X (first)
import Data.Functor.Identity as X (Identity, runIdentity)
import Data.Int as X (Int8, Int16, Int32, Int64)
import Data.Ord as X (Ord (..), comparing)
import Data.List as X (sortBy)
import Data.List.NonEmpty as X (NonEmpty (..), nonEmpty)
import Data.Maybe as X (catMaybes)
import Data.Proxy as X (Proxy (..))
import Data.Ratio as X ((%), denominator, numerator)
import Data.Semigroup as X (Semigroup (..), Any, diff)
import Data.Typeable as X (Typeable, typeRep)
import Data.Word as X (Word8, Word16, Word32, Word64)
import Foreign.Ptr as X (Ptr)
import GHC.Generics as X (Generic)
import GHC.Stack as X
import System.Exit as X
import System.IO as X (Handle, stderr, stdout)
import Text.Read as X (readEither)

-- | Rename `id` to `identity` to allow `id` as a variable name
identity :: Category cat => cat a a
identity = id
identity = Category.id

-- | Explicit output with @Text@ since that is what we want most of the time.
-- We don't want to look at the type errors or warnings arising.
putTextLn :: Text -> IO ()
putTextLn = putStrLn
putTextLn = Text.putStrLn

-- Length which includes @Text@ as well as @Foldable@.
class HasLength a where
length' :: a -> Int

instance HasLength Text where
length' = T.length
length' = Text.length

instance Foldable t => HasLength (t a) where
length' = Y.length
length' = Prelude.length

-- | We can pass several things here, as long as they have length.
length :: HasLength a => a -> Int
Expand Down
18 changes: 0 additions & 18 deletions cardano-prelude/src/Cardano/Prelude/Compat.hs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module Cardano.Prelude.Compat.ByteString.Short (
unsafeShortByteStringIndex,
) where

import Cardano.Prelude.Base

-- GHC >= 9.0 does not export unsafeIndex for ShortByteString
-- GHC < 9.0 does not export the ShortByteString constructor SBS.
-- Coniditional compile to work around this.

#if __GLASGW_HASKELL__ >= 900
import Data.ByteString.Short (ShortByteString(..))
import Data.Int (Int)
import Data.Word (Word8)
import GHC.Exts (indexWord8Array#)

unsafeShortByteStringIndex :: ShortByteString -> Int -> Word8
Expand All @@ -21,8 +21,6 @@ unsafeShortByteStringIndex (SBS ba#) (I# i#) = W8# (indexWord8Array# ba# i#)
#else

import Data.ByteString.Short (ShortByteString, index)
import Data.Int (Int)
import Data.Word (Word8)

unsafeShortByteStringIndex :: ShortByteString -> Int -> Word8
unsafeShortByteStringIndex = index
Expand Down
2 changes: 1 addition & 1 deletion cardano-prelude/src/Cardano/Prelude/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ where
import Cardano.Prelude.Base

import qualified Codec.CBOR.Decoding as CBOR
import Control.Monad (fail)
import Control.Monad.Except (liftEither)
import qualified Data.Aeson.Types as A
import Formatting (build, formatToString)
import Formatting.Buildable (Buildable)
import Prelude hiding ((.))

-- | Convert an 'Either'-encoded error to an 'aeson' parser error
toAesonError :: Buildable e => Either e a -> A.Parser a
Expand Down
Loading

0 comments on commit 5251313

Please sign in to comment.