Instances of Read and Show

Dean Herington heringto@cs.unc.edu
Fri, 30 Mar 2001 10:28:15 -0500


--------------74E5E9480E30AA4DD6791F2C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Ingo Sander wrote:

> I have a question concerning the Read and Show classes.
>
> I have read the Haskell 98 report (section 6.3.3 The Read and Show
> Classes), but I still do not figure out how to make instances.
>
> I have a simple data type:
>
> data TVal a =  Abst
>              | Prst a
>
> I managed to define the function show, so that I can use it with the data
> type TVal
>
> instance (Show a) => Show (TVal a) where
>          show Abst     = "_"
>          show (Prst x) = show x
>
> Signals> [Prst 1, Abst, Prst 2]
> [1,_,2] :: [TVal Integer]
>
> which is what I want to have.
>
> But how do I make a Read instance?
>
> I still do not understand the meaning of 'readsPrec' and
> 'readList' (and not of 'showPrec' and 'showList') either.
>
> Can somebody explain to me or give me a good reference so that I can make
> TVal an instance of the classes Read and Show.
>
> Thanks in advance for your help!
>
> Ingo Sander
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

I might do it this way:

> data TVal a = Abst | Prst a

> instance (Show a) => Show (TVal a) where
>     showsPrec p Abst = ('_' :)
>     showsPrec p (Prst x) = showsPrec p x

> instance (Read a) => Read (TVal a) where
>     readsPrec p s = case s' of ('_':t) -> (Abst,t) : readsA
>                                _ -> readsA
>       where s' = dropWhile isSpace s
>             readsA = map (\(a,t)-> (Prst a,t)) (readsPrec p s')

See section D.4 of the Haskell report for information on 'readsPrec',
'readList', 'showPrec', and 'showList'.

Dean Herington

--------------74E5E9480E30AA4DD6791F2C
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Ingo Sander wrote:
<blockquote TYPE=CITE>I have a question concerning the Read and Show classes.
<p>I have read the Haskell 98 report (section 6.3.3 The Read and Show
<br>Classes), but I still do not figure out how to make instances.
<p>I have a simple data type:
<p>data TVal a =&nbsp; Abst
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
| Prst a
<p>I managed to define the function show, so that I can use it with the
data
<br>type TVal
<p>instance (Show a) => Show (TVal a) where
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; show Abst&nbsp;&nbsp;&nbsp;&nbsp;
= "_"
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; show (Prst x) = show
x
<p>Signals> [Prst 1, Abst, Prst 2]
<br>[1,_,2] :: [TVal Integer]
<p>which is what I want to have.
<p>But how do I make a Read instance?
<p>I still do not understand the meaning of 'readsPrec' and
<br>'readList' (and not of 'showPrec' and 'showList') either.
<p>Can somebody explain to me or give me a good reference so that I can
make
<br>TVal an instance of the classes Read and Show.
<p>Thanks in advance for your help!
<p>Ingo Sander
<p>_______________________________________________
<br>Haskell-Cafe mailing list
<br>Haskell-Cafe@haskell.org
<br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a></blockquote>

<p><br>I might do it this way:
<p><tt>> data TVal a = Abst | Prst a</tt>
<br><tt>&nbsp;</tt>
<br><tt>> instance (Show a) => Show (TVal a) where</tt>
<br><tt>>&nbsp;&nbsp;&nbsp;&nbsp; showsPrec p Abst = ('_' :)</tt>
<br><tt>>&nbsp;&nbsp;&nbsp;&nbsp; showsPrec p (Prst x) = showsPrec p x</tt><tt></tt>
<p><tt>> instance (Read a) => Read (TVal a) where</tt>
<br><tt>>&nbsp;&nbsp;&nbsp;&nbsp; readsPrec p s = case s' of ('_':t) ->
(Abst,t) : readsA</tt>
<br><tt>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
_ -> readsA</tt>
<br><tt>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where s' = dropWhile isSpace
s</tt>
<br><tt>>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
readsA = map (\(a,t)-> (Prst a,t)) (readsPrec p s')</tt>
<p>See section D.4 of the Haskell report for information on 'readsPrec',
'readList', 'showPrec', and 'showList'.
<p>Dean Herington</html>

--------------74E5E9480E30AA4DD6791F2C--