[commit: packages/array] master: Add a 'Read (UArray i e)' instance (2845463)

git at git.haskell.org git at git.haskell.org
Thu Nov 8 15:23:17 UTC 2018


Repository : ssh://git@git.haskell.org/array

On branch  : master
Link       : http://git.haskell.org/packages/array.git/commitdiff/28454638ca681c4625f7c2fcf127821399d9ded3

>---------------------------------------------------------------

commit 28454638ca681c4625f7c2fcf127821399d9ded3
Author: Alec Theriault <alec.theriault at gmail.com>
Date:   Thu Nov 8 10:21:12 2018 -0500

    Add a 'Read (UArray i e)' instance
    
    Summary:
    This matches exactly the 'Read (Array i e)' instance defined in base.
    Note that the same thing is being done for 'Show (UArray i e)'.
    
    Reviewers: RyanGlScott, bgamari
    
    GHC Trac Issues: #11335
    
    Differential Revision: https://phabricator.haskell.org/D5156


>---------------------------------------------------------------

28454638ca681c4625f7c2fcf127821399d9ded3
 Data/Array/Base.hs | 23 +++++++++++++++++++++--
 changelog.md       |  4 ++++
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/Data/Array/Base.hs b/Data/Array/Base.hs
index 80e91ea..ed014cb 100644
--- a/Data/Array/Base.hs
+++ b/Data/Array/Base.hs
@@ -43,12 +43,16 @@ import GHC.ST           ( ST(..), runST )
 import GHC.Base         ( IO(..), divInt# )
 import GHC.Exts
 import GHC.Ptr          ( nullPtr, nullFunPtr )
+import GHC.Show         ( appPrec )
 import GHC.Stable       ( StablePtr(..) )
+import GHC.Read         ( expectP, parens, Read(..) )
 import GHC.Int          ( Int8(..),  Int16(..),  Int32(..),  Int64(..) )
 import GHC.Word         ( Word8(..), Word16(..), Word32(..), Word64(..) )
 import GHC.IO           ( stToIO )
 import GHC.IOArray      ( IOArray(..),
                           newIOArray, unsafeReadIOArray, unsafeWriteIOArray )
+import Text.Read.Lex    ( Lexeme(Ident) )
+import Text.ParserCombinators.ReadPrec ( prec, ReadPrec, step )
 
 #include "MachDeps.h"
 
@@ -479,7 +483,7 @@ cmpIntUArray arr1@(UArray l1 u1 n1 _) arr2@(UArray l2 u2 n2 _) =
 {-# RULES "cmpUArray/Int" cmpUArray = cmpIntUArray #-}
 
 -----------------------------------------------------------------------------
--- Showing IArrays
+-- Showing and Reading IArrays
 
 {-# SPECIALISE
     showsIArray :: (IArray UArray e, Ix i, Show i, Show e) =>
@@ -488,12 +492,24 @@ cmpIntUArray arr1@(UArray l1 u1 n1 _) arr2@(UArray l2 u2 n2 _) =
 
 showsIArray :: (IArray a e, Ix i, Show i, Show e) => Int -> a i e -> ShowS
 showsIArray p a =
-    showParen (p > 9) $
+    showParen (p > appPrec) $
     showString "array " .
     shows (bounds a) .
     showChar ' ' .
     shows (assocs a)
 
+{-# SPECIALISE
+    readIArray :: (IArray UArray e, Ix i, Read i, Read e) =>
+                   ReadPrec (UArray i e)
+  #-}
+
+readIArray :: (IArray a e, Ix i, Read i, Read e) => ReadPrec (a i e)
+readIArray = parens $ prec appPrec $
+               do expectP (Ident "array")
+                  theBounds <- step readPrec
+                  vals   <- step readPrec
+                  return (array theBounds vals)
+
 -----------------------------------------------------------------------------
 -- Flat unboxed arrays: instances
 
@@ -785,6 +801,9 @@ instance (Ix ix, Ord e, IArray UArray e) => Ord (UArray ix e) where
 instance (Ix ix, Show ix, Show e, IArray UArray e) => Show (UArray ix e) where
     showsPrec = showsIArray
 
+instance (Ix ix, Read ix, Read e, IArray UArray e) => Read (UArray ix e) where
+    readPrec = readIArray
+
 -----------------------------------------------------------------------------
 -- Mutable arrays
 
diff --git a/changelog.md b/changelog.md
index 8a3d149..0fd9289 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,9 @@
 # Changelog for [`array` package](http://hackage.haskell.org/package/array)
 
+## Next
+
+* Add a `Read` instance for `UArray`
+
 ## 0.5.3.0  *Oct 2018*
 
   * Bundled with GHC 8.6.2



More information about the ghc-commits mailing list