deriving (Show, Read)
deriving (Show, Read)
Back in the old glorious times of the golden age, some Lisp guys have re-discovered this universal idea. Every expression (which implicitly has a type) can be rendered into a unique “printable form” (to be printed by the REPL).
Eventually they have arrived at an invariant, that the unique “printable form” has to be the same as a corresponding “readable form” (the one a REPL can read in).
Remember that in a proper LISP every expression is not a plain text but
an actual nested structure made out of cons
-es, and it goes through the read
function which
converts nested lists into particular data and syntactic forms.
This is the basics for LISP’s unique and rich macros - arbitrary
syntactic forms (which eval
never seen before but would “understand”)
can be generated by turning the read
function on and off at certain parts of an
expression (a list-structure).
Anyway, a complex nested expression can thus be printed (and read in) just by simple traversal, and the whole “printable and REPL-readable representation” became the source and an inspiration for SGML and HTML standards, and for the latter notion of “on-the-wire” data representation - a whole compound, deeply nested immutable value is just “right there”.
The fundamental principle, however, is in that the concept of a “number 42” can
be represented (written down on a piece of paper) as just 42
and every occurrence of these written symbols
can be “read back” (into the mind) as a whole number.
This is not just a traditional convention, it reflects on the fact that every H2O
molecule is the same
as every other, and this holds for all simple molecules of the same
kind, which are Life’s “stable” building blocks.
Nowadays all decent mostly-functional languages support this facility, along with libraries for converting between different human- or machine-readable representations of the data.
Haskell’s deriving (Show, Read)
is, obviously, the culmination of what
LISP guys have discovered.
Just remember that data formats matter, and that all data must be /immutable.