The use of `show' for converting objects to their print representations

The need often arises to convert an arbitrary Miranda value to its printable representation as a string. For numbers the function `shownum' (of type num->[char]) can be used for this purpose. To be able to do this conversion for any type of object would seemingly require an infinite number of functions, one for each type. As a solution to this problem Miranda provides a special keyword, `show'.

For any object x

        show x
is a string containing the printable representation of x. For example, if x is a number the above expression is equivalent to `shownum x'. In the general case, however, x could be a structured object of arbitrary complexity. Note that show is a reserved word, not an identifier.

In fact `show' behaves under most circumstances as though it was the name of a function, of type *->[char]. For example it can be passed as a parameter, so that say,

        map show [a,b,c,d,e]
is a legal expression of type [[char]].

There are three important restrictions on the universality of show.

(i) You cannot `show' functions in any useful sense. (That would be a violation of referential transparency.) The result of applying show to any kind of function is just the string "<function>".

(ii) You cannot `show' an abstract object unless an appropriate show-function was included when the type was defined (see manual section on Abstract types). The result of applying show to such an object is by default just the string "<abstract ob>".

(iii) When it occurs in a script the context in which show is used must be such as to determine its type monomorphically. An example:

        my_show x = "hello\n"++show x++"goodbye\n"
In the absence of any other type information, the compiler will infer that my_show has type *->[char], and that x is of type `*'. The use of show is therefore polymorphic, and will be rejected as illegal.

If however we intend that my_show will be used only on objects of type `tree', say, and we add to the script the declaration `my_show::tree->[char]', then the above use of show becomes monomorphic, and will be accepted.

The essence of restriction (iii) is that show is not a true polymorphic function of type *->[char], but rather a family of monomorphic functions with the types T->[char] for each possible monotype T. The context must be sufficient for the compiler to determine which member of the family is required.

(For technical reasons this restriction applies only in scripts. In command-level expressions show behaves as if it were a genuine polymorphic function.)