Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Describe orphan instance examples with detail.

  • Loading branch information...
commit 1a109f392b65f85c7e7fed65f299ae47dd40cd82 1 parent e6d82d0
Marco Túlio Gontijo authored

Showing 1 changed file with 36 additions and 15 deletions. Show diff stats Hide diff stats

  1. +36 15 marcot.tex
51 marcot.tex
@@ -656,21 +656,42 @@ \section{Orphan instances}
656 656 \texttt{I1} or \texttt{I2} is not always possible in practice because they
657 657 may be part of a third-party library.
658 658
659   -It is worth noticing that these are not only potential problems. They
660   -happen in real world uses of the language. For example, the Monad
661   -instance of Either is defined in both packages \texttt{mtl} and
662   -\texttt{transformers}\footnote{This example is on the wiki page at
663   - \url{http://www.haskell.org/haskellwiki/Orphan_instance} .}. There
664   -are examples where orphan instances would be desirable, involving
665   -pretty printing and JSON\footnote{This example was presented by
666   - Lennart Augustsson in
667   - \url{http://lukepalmer.wordpress.com/2009/01/25/a-world-without-orphans/\#comment-601}
668   - .}. Also, a situation has been reported where instances created
669   -with Template Haskell could not be defined in the same module of the
670   -data type or type class\footnote{Johan Tibell gives a detailed
671   - description of the situation in an e-mail at
672   - \url{http://www.haskell.org/pipermail/glasgow-haskell-users/2010-August/019052.html}
673   - .}.
  659 +\subsection{Real world cases}
  660 +
  661 +It is worth noticing that these are not only potential problems.
  662 +They happen in real world uses of the language.
  663 +For example, the instance of the type class \texttt{Monad} for the data type \texttt{Either} is not defined neither in the module where \texttt{Monad} is defined, \texttt{Control.Monad}, nor in the module where \texttt{Either} is defined, \texttt{Data.Either}.
  664 +Instead it is defined in the module \texttt{Control.Monad.Error} from the package \texttt{mtl}, and also in module \texttt{Control.Monad.Trans.Error} from the package \texttt{transformers}\footnote{This example is on the wiki page at \url{http://www.haskell.org/haskellwiki/Orphan_instance} .}.
  665 +If these two modules are directly or indirectly imported by a module, it would not be possible to use this instance on this module.
  666 +
  667 +Lennart Augustsson presented ``a concrete example'' of a case where orphan instances would be desirable\footnote{\url{http://lukepalmer.wordpress.com/2009/01/25/a-world-without-orphans/\#comment-601} .}.
  668 +There are libraries from pretty printing data types, with type classes that include ``instances for all relevant prelude types''.
  669 +There are also packages with a data type for dealing with JSON data.
  670 +It would be good to write an instance for pretty printing JSON data in yet another package, since those packages are unrelated and should not necessarily know about each other and implement instances about the other.
  671 +But, if this instance is written on another package, it will be an orphan instance, which would cause all the problems described in this section.
  672 +
  673 +Johan Tibell proposes a generic map implementation using a type class for the common map functions and an associated data type for that type class\footnote{\url{http://www.haskell.org/pipermail/glasgow-haskell-users/2010-August/019052.html} .}.
  674 +The code proposed is on Figure \ref{johan}.
  675 +This would require an instance for each pair of key and value of the map.
  676 +A lot of instances would have to be generated for the prelude types, probably using Template Haskell, a mechanism for automatic code generation.
  677 +Even so, for library types the user would have to generate the instance.
  678 +This way, it would not be possible for the user to generate the instance of data type defined on another package without creating an orphan instance.
  679 +
  680 +\begin{figure}
  681 +\caption{Generic map using a type class and an associated data type.\label{johan}}
  682 +\begin{tabular}{|p{\textwidth}|}
  683 +\hline
  684 +\begin{verbatim}
  685 +class Unbox k v where
  686 + data Map k v :: *
  687 + empty :: Map k v
  688 + lookup :: k -> Map k v -> Maybe v
  689 + insert :: k -> v -> Map k v -> Map k v
  690 +\end{verbatim}
  691 +\\
  692 +\hline
  693 +\end{tabular}
  694 +\end{figure}
674 695
675 696 \chapter{Solution}
676 697 \label{solution}

0 comments on commit 1a109f3

Please sign in to comment.
Something went wrong with that request. Please try again.