Commit 28454638 authored by Alec Theriault's avatar Alec Theriault Committed by Ryan Scott
Browse files

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
parent 8af2f620
......@@ -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
......
# 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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment