srghma
srghma

Reputation: 5303

Best way to convert ShowS to String?

How to to prettify

(displayS $ renderPretty 0.8 80 $ _errDoc ei <> linebreak) ""

why I cant

show $ displayS $ renderPretty 0.8 80 $ _errDoc ei <> linebreak

Upvotes: 1

Views: 697

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152682

You can't show $ displayS blah because ShowS isn't an instance of Show. Recall that

type ShowS = String -> String

so if we wanted an instance of Show we would have to write:

instance Show (String -> String) where
    show f = ...

And now we have a bit of a problem. After all, the only thing we can do with the function f, really, is call it with String arguments. There's a couple choices that spring to mind readily, but they each have drawbacks:

  1. Try to describe the behavior of f on every possible input. This isn't going to work very well -- there's infinitely many inputs. Reading and understanding the infinite output would take a longish time.
  2. Don't bother calling f at all. Pick a single String to be the output of show, as in show f = "<it's a function, lol>". This isn't terribly useful because it doesn't actually give you any information about f.
  3. Call f with the distinguished string "". This isn't useful because there are lots of different functions that give the same output for that input, and you won't be able to distinguish between them. (Maybe it so happens that your library for cooking up ShowS values won't have that problem, but if we're going to have the instance it ought to work for values that come from anywhere, not just some tiny subset of the values!)

Because of these considerations, an instance of Show for functions is not included in the standard libraries, though approaches (1) and (2) are available for installation from Hackage.

Upvotes: 3

Related Questions