# Simple to complex

### From HaskellWiki

(Difference between revisions)
|          |                                                         |                      |                                                                                                                        |
|----------|---------------------------------------------------------|----------------------|------------------------------------------------------------------------------------------------------------------------|
| (init)   |                                                         | (type class methods) |                                                                                                                        |
| Line 5:  |                                                         | Line 5:              |                                                                                                                        |
|          | That leads no longer to a clear hierarchy of functions, |                      | That leads no longer to a clear hierarchy of functions,                                                                |
|          | but to an entangled graph of dependencies.              |                      | but to an entangled graph of dependencies.                                                                             |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         |                      |                                                                                                                        |
|          | == Functions ==                                         |                      | == Functions ==                                                                                                        |
| Line 56: |                                                         | Line 57:             |                                                                                                                        |
|          | Move them as far as possible to leaf modules.           |                      | Move them as far as possible to leaf modules.                                                                          |
|          |                                                         |                      |                                                                                                                        |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | == Type class methods ==                                                                                               |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | There are many methods in <hask>Data.List</hask> that can be generalised to other data structures,                     |
|          |                                                         | +                    | Some people wonder why they aren't replaced by their generalized counterparts, namely the methods in the type classes. |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | First of all, there are didactic reasons.                                                                              |
|          |                                                         | +                    | Higher order functions like <hask>foldr</hask> are hard to grasp for newbies.                                          |
|          |                                                         | +                    | That's why they often stick to manual encoding of recursion.                                                           |
|          |                                                         | +                    | (See [[Things_to_avoid#Avoid_explicit_recursion]])                                                                     |
|          |                                                         | +                    | Now imagine, that <hask>Data.List.foldl</hask> is no longer exposed,                                                   |
|          |                                                         | +                    | but only its generalization, namely the method <hask>Data.Foldable.foldl</hask>                                        |
|          |                                                         | +                    | where not only the element types and the working function are generalized away,                                        |
|          |                                                         | +                    | but there is even no longer a particular data structure you are working on.                                            |
|          |                                                         | +                    | Nothing concrete any longer, only abstract things.                                                                     |
|          |                                                         | +                    | The best thing to explain this abstract function,                                                                      |
|          |                                                         | +                    | is to [http://research.microsoft.com/~simonpj/Papers/giving-a-talk/giving-a-talk-html.html use an example].            |
|          |                                                         | +                    | E.g. <hask>sum xs = List.foldl (+) 0 xs</hask>.                                                                        |
|          |                                                         | +                    | Now, this requires to have a function <hask>List.foldl</hask>.                                                         |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | <!--                                                                                                                   |
|          |                                                         | +                    | Modularity reason:                                                                                                     |
|          |                                                         | +                    | It is sometimes not possible to generalize all the functionality that belongs together.                                |
|          |                                                         | +                    | Example?                                                                                                               |
|          |                                                         | +                    | On the other hand: The generaliation can guide your decision, what belongs together,                                   |
|          |                                                         | +                    | or vice versa, what you think, belongs together, should be generlized together.                                        |
|          |                                                         | +                    | -->                                                                                                                    |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | <!--                                                                                                                   |
|          |                                                         | +                    | Close to didactic reasons there are reasons of comprehensible program code.                                            |
|          |                                                         | +                    | >  And a lot of standard list functions can be                                                                         |
|          |                                                         | +                    | > generalized to MonadPlus, for example you can define                                                                 |
|          |                                                         | +                    | >                                                                                                                      |
|          |                                                         | +                    | > filter :: (MonadPlus m) => (a -> Bool) -> m a -> m a                                                                 |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | Always using the most generalized form is not a good idea. If you know you are working on lists, 'map'                 |
|          |                                                         | +                    | and 'filter' tell the reader, that they are working on lists. The reader of a program doesn't need to                  |
|          |                                                         | +                    | start human type inference to deduce this. Also the type inference of the compiler will fail, if you                   |
|          |                                                         | +                    | write too general. Say, you are in GHCi, have your definition of 'filter' and you write                                |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | Prelude> filter Char.isUpper (return 'a')                                                                              |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | To what monad this shall be specialised? Rely on type defaulting? Ok, you can use type signatures.                     |
|          |                                                         | +                    | -->                                                                                                                    |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | These problems should be taken account, when designing custom type classes.                                            |
|          |                                                         | +                    | See the notes on [[Slim instance declarations]].                                                                       |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    |                                                                                                                        |
|          |                                                         | +                    | See also                                                                                                               |
|          |                                                         |                      |                                                                                                                        |
|          | [[Category:Style]]                                      |                      | [[Category:Style]]                                                                                                     |

## Revision as of 06:32, 5 September 2007

It is generally a good idea to construct complex functions from simpler ones rather than making simple functions special cases of complex functions. Obviously the latter strategy does not work alone, thus using it means mixing it with the former one. That leads no longer to a clear hierarchy of functions, but to an entangled graph of dependencies.

## 1  Functions

The lazy evaluation feature of Haskell tempts you to ignore the principle of building complex functions on simpler ones.

See "Eleven reasons to use Haskell as mathematician" where it is presented as an advantage that lazy evaluation automatically simplifies the computation of a cross product of two 3D vectors if only a single component of the result is requested. However, computing a single component of a cross product means computing the determinant of a $2\times2$-matrix, which is certainly useful of its own. So instead of using laziness for reducing the cross product to a determinant, the better concept is clearly to write a function for computing the $2\times2$-determinant and invoke this three times in the implementation of the cross product.

## 2  Types

Another bad example is the numerical linear algebra package MatLab. Its type hierarchy starts at complex valued matrices from which you can build more complex types. That is, there are no simpler types, no real valued matrices, complex numbers, real numbers, integers nor booleans. They have to be represented by complex valued matrices. This is not very natural since some operations like transcendent powers are not easily ported to matrices. That is many operations must check at run-time, whether the input values have appropriate properties, e.g. being $1\times1$-matrices. Actually, some kinds of integers and booleans (logical values) have been added later, but they interact weirdly with MatLab's base type.

The mistake, that the language designers of MatLab made, is the following: They thought MatLab would remain a special purpose language for numerical linear algebra forever, so they decided to implement only the most complex type that would ever be encountered in this domain. As MatLab grew, they extended the program to fit new needs, image import and export, GUI programming and so on, but the initial decision for a universal type didn't scale well.

It's not a good idea to mimic this in Haskell. Start with simple types and build complexer ones out of it. Make sure that you use fancy type constructs not in the core of a library, if at all. Move them as far as possible to leaf modules.

## 3  Type class methods

There are many methods in
Data.List
that can be generalised to other data structures, like
map
,
filter
,
foldr
,
(++)
.

Some people wonder why they aren't replaced by their generalized counterparts, namely the methods in the type classes.

First of all, there are didactic reasons.

Higher order functions like
foldr
are hard to grasp for newbies.

That's why they often stick to manual encoding of recursion. (See Things_to_avoid#Avoid_explicit_recursion)

Now imagine, that
Data.List.foldl
is no longer exposed, but only its generalization, namely the method
Data.Foldable.foldl

where not only the element types and the working function are generalized away, but there is even no longer a particular data structure you are working on. Nothing concrete any longer, only abstract things. The best thing to explain this abstract function, is to use an example.

E.g.
sum xs = List.foldl (+) 0 xs
. Now, this requires to have a function
List.foldl
.

These problems should be taken account, when designing custom type classes. See the notes on Slim instance declarations.

See also
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 3, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": false, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.902523398399353, "perplexity_flag": "middle"}