[Haskell-cafe] Printing and Referential transparency excuse
Derek Elkins
derek.a.elkins at gmail.com
Tue Dec 25 04:05:59 EST 2007
On Mon, 2007-12-24 at 11:15 +0200, Cristian Baboi wrote:
> While reading the Haskell language report I noticed that function type is
> not an instance of class Read.
>
> I was told that one cannot define them as an instance of class Show
> without breaking "referential transparency" or printing a constant.
>
> f :: (a->b)->String
> f x = "bla bla bla"
>
> How can I define a function to do the inverse operation ?
> g :: String -> ( a -> b )
>
> This time I cannot see how referential transparency will deny it.
> What's the excuse now ?
Referential transparency has nothing to with this. The problem here is
parametricity. However, if you give a non-parametric type (or add
appropriate class constraints on a and b, or add (phantom) type
information to the input), e.g. String -> (Int -> Int) then there is no
trouble at all.
The compiler (or something non-portably using extremely unsafe features)
could provide a primitive String -> a that reads in a string and
interprets it somehow as a value of type a. If the value represented by
the string doesn't match the type that a is instantiated to, something
bad is going to happen. Adding such a primitive would utterly destroy
parametricity.
More information about the Haskell-Cafe
mailing list