Skip to content

Commit

Permalink
Backport functions introduced in base-4.15
Browse files Browse the repository at this point in the history
Checks off the `base-4.15.0.0` boxes in #24.
  • Loading branch information
RyanGlScott committed Sep 30, 2020
1 parent 32b9028 commit b103647
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 2 deletions.
5 changes: 5 additions & 0 deletions base-compat-batteries/CHANGES.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Changes in ?.??.? [????.??.??]
- This coincides with the `base-compat-?.??.?` release. Refer to the
[`base-compat` changelog](https://github.com/haskell-compat/base-compat/blob/master/base-compat/CHANGES.markdown#changes-in-????-????????)
for more details.

## Changes in 0.11.1 [2020.01.27]
- This coincides with the `base-compat-0.11.1` release. Refer to the
[`base-compat` changelog](https://github.com/haskell-compat/base-compat/blob/master/base-compat/CHANGES.markdown#changes-in-0111-20200127)
Expand Down
2 changes: 2 additions & 0 deletions base-compat-batteries/base-compat-batteries.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ library
Prelude.Compat
System.Environment.Compat
System.Exit.Compat
System.IO.Compat
System.IO.Error.Compat
System.IO.Unsafe.Compat
Text.Read.Compat
Expand Down Expand Up @@ -203,6 +204,7 @@ library
Prelude.Compat.Repl.Batteries
System.Environment.Compat.Repl.Batteries
System.Exit.Compat.Repl.Batteries
System.IO.Compat.Repl.Batteries
System.IO.Error.Compat.Repl.Batteries
System.IO.Unsafe.Compat.Repl.Batteries
Text.Read.Compat.Repl.Batteries
Expand Down
11 changes: 9 additions & 2 deletions base-compat-batteries/src/Data/List/NonEmpty/Compat.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{-# LANGUAGE CPP, NoImplicitPrelude, PackageImports #-}
-- | This backports the modern "Data.Semigroup" interface back to
-- @base-4.9@/GHC 8.0.
module Data.List.NonEmpty.Compat (
-- * The type of non-empty streams
NonEmpty(..)
Expand All @@ -21,6 +19,7 @@ module Data.List.NonEmpty.Compat (
, tail
, last
, init
, singleton
, (<|), cons
, uncons
, unfoldr
Expand Down Expand Up @@ -76,3 +75,11 @@ import "base-compat" Data.List.NonEmpty.Compat
#else
import "semigroups" Data.List.NonEmpty
#endif

#if !(MIN_VERSION_base(4,9,0))
-- | Construct a 'NonEmpty' list from a single element.
--
-- /Since: 4.15/
singleton :: a -> NonEmpty a
singleton a = a :| []
#endif
6 changes: 6 additions & 0 deletions base-compat-batteries/src/System/IO/Compat.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{-# LANGUAGE CPP, NoImplicitPrelude, PackageImports #-}
module System.IO.Compat (
module Base
) where

import "base-compat" System.IO.Compat as Base
8 changes: 8 additions & 0 deletions base-compat-batteries/src/System/IO/Compat/Repl/Batteries.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE PackageImports #-}
{-# OPTIONS_GHC -fno-warn-dodgy-exports -fno-warn-unused-imports #-}
-- | Reexports "System.IO.Compat"
-- from a globally unique namespace.
module System.IO.Compat.Repl.Batteries (
module System.IO.Compat
) where
import "this" System.IO.Compat
5 changes: 5 additions & 0 deletions base-compat/CHANGES.markdown
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Changes in ?.??.?? [????.??.??]
- Sync with `base-4.15`/GHC 9.0
- Backport `singleton` to `Data.List` and `Data.List.NonEmpty`
- Backport `hGetContents'`, `getContents'`, and `readFile'` added to `System.IO`

## Changes in 0.11.1 [2020.01.27]
- Sync with `base-4.14`/GHC 8.10
- Backport `isResourceVanishedError`, `resourceVanishedErrorType`, and
Expand Down
3 changes: 3 additions & 0 deletions base-compat/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ So far the following is covered.
* `RuntimeRep`-polymorphic `throw` to `Control.Exception.Compat`
* `isResourceVanishedError`, `resourceVanishedErrorType`, and
`isResourceVanishedErrorType` to `System.IO.Error.Compat`
* `singleton` to `Data.List.Compat` and `Data.List.NonEmpty.Compat`
* `hGetContents'`, `getContents'`, and `readFile'` to `System.IO`

## What is not covered

Expand Down Expand Up @@ -304,6 +306,7 @@ on, paired with the things that each library backports:

## Supported versions of GHC/`base`

* `ghc-9.0.*` / `base-4.15.*`
* `ghc-8.10.*` / `base-4.14.*`
* `ghc-8.8.*` / `base-4.13.*`
* `ghc-8.6.*` / `base-4.12.*`
Expand Down
2 changes: 2 additions & 0 deletions base-compat/base-compat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ library
Prelude.Compat
System.Environment.Compat
System.Exit.Compat
System.IO.Compat
System.IO.Error.Compat
System.IO.Unsafe.Compat
Text.Read.Compat
Expand Down Expand Up @@ -193,6 +194,7 @@ library
Prelude.Compat.Repl
System.Environment.Compat.Repl
System.Exit.Compat.Repl
System.IO.Compat.Repl
System.IO.Error.Compat.Repl
System.IO.Unsafe.Compat.Repl
Text.Read.Compat.Repl
Expand Down
16 changes: 16 additions & 0 deletions base-compat/src/Data/List/Compat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#endif
module Data.List.Compat (
module Base
#if !(MIN_VERSION_base(4,15,0))
, singleton
#endif

#if !(MIN_VERSION_base(4,11,0))
, iterate'
#endif
Expand Down Expand Up @@ -235,3 +239,15 @@ iterate'FB c f x0 = go x0
"iterate'FB" [1] iterate'FB (:) = iterate'
#-}
#endif

#if !(MIN_VERSION_base(4,15,0))
-- | Produce singleton list.
--
-- >>> singleton True
-- [True]
--
-- /Since: 4.14.0.0/
--
singleton :: a -> [a]
singleton x = [x]
#endif
9 changes: 9 additions & 0 deletions base-compat/src/Data/List/NonEmpty/Compat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Data.List.NonEmpty.Compat (
, tail
, last
, init
, singleton
, (<|), cons
, uncons
, unfoldr
Expand Down Expand Up @@ -76,3 +77,11 @@ module Data.List.NonEmpty.Compat (
#if MIN_VERSION_base(4,9,0)
import Data.List.NonEmpty
#endif

#if MIN_VERSION_base(4,9,0) && !(MIN_VERSION_base(4,15,0))
-- | Construct a 'NonEmpty' list from a single element.
--
-- /Since: 4.15/
singleton :: a -> NonEmpty a
singleton a = a :| []
#endif
46 changes: 46 additions & 0 deletions base-compat/src/System/IO/Compat.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{-# LANGUAGE CPP, NoImplicitPrelude #-}
module System.IO.Compat (
module Base
, getContents'
, hGetContents'
, readFile'
) where

import System.IO as Base

#if !(MIN_VERSION_base(4,15,0))
import Prelude.Compat

-- | The 'getContents'' operation returns all user input as a single string,
-- which is fully read before being returned
-- (same as 'hGetContents'' 'stdin').
--
-- /Since: 4.15.0.0/

getContents' :: IO String
getContents' = hGetContents' stdin

-- | The 'readFile'' function reads a file and
-- returns the contents of the file as a string.
-- The file is fully read before being returned, as with 'getContents''.
--
-- /Since: 4.15.0.0/

readFile' :: FilePath -> IO String
readFile' name = openFile name ReadMode >>= hGetContents'

-- | The 'hGetContents'' operation reads all input on the given handle
-- before returning it as a 'String' and closing the handle.
--
-- /Since: 4.15.0.0/

hGetContents' :: Handle -> IO String
hGetContents' h = hGetContents h >>= \s -> length s `seq` return s
-- NB: The actual implementation of hGetContents' in `base` uses a lot of
-- low-level code from GHC.IO.Handle.Text. What's worse, a lot of this
-- low-level code isn't exported, so we'd have to reimplement large chunks
-- of it in base-compat if we wanted to backport it. For now, I've opted for
-- the simpler approach of simply defining hGetContents' in terms of
-- hGetContents, which is the approach that the `extra` and `strict` libraries
-- use. (Indeed, the code above is taken from `strict`.)
#endif
8 changes: 8 additions & 0 deletions base-compat/src/System/IO/Compat/Repl.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{-# LANGUAGE PackageImports #-}
{-# OPTIONS_GHC -fno-warn-dodgy-exports -fno-warn-unused-imports #-}
-- | Reexports "System.IO.Compat"
-- from a globally unique namespace.
module System.IO.Compat.Repl (
module System.IO.Compat
) where
import "this" System.IO.Compat

0 comments on commit b103647

Please sign in to comment.