instance Read Dynamic ?

Brian Huffman
Mon, 25 Mar 2002 12:55:48 -0800

On Monday 25 March 2002 12:00 pm, Dean Herington wrote:
> The type `Dynamic` is an instance of `Show` but not of `Read`.  Is there
> some reason `Dynamic` could not be made an instance of `Read`?  Has
> anyone extended `Dynamic` in that way?

Here is a snippet of code from the GHC Dynamic library; you will notice that 
Dynamic objects contain a TypeRep and a data object, but the data object is 
ignored by show (as it must be, since the Obj could be anything, showable or 

data Dynamic = Dynamic TypeRep Obj

instance Show Dynamic where
   -- the instance just prints the type representation.
   showsPrec _ (Dynamic t _) =
          showString "<<" .
	  showsPrec 0 t   .
	  showString ">>"

It is impossible to make a matching Read instance, because you lose 
information when you show a Dynamic object; there is no way a read function 
can reconstruct the data object's value.

It probably wouldn't be too difficult to make a readable version of Dynamic, 
but it would only be able to hold datatypes that were instances of Read and 
Show. I'd be interested to see if anyone else has implemented anything like 
this; it seems like it could be pretty useful.

- Brian Huffman