mostly-adequate-guide

Searchâ€¦

Chapter 11: Transform Again, Naturally

We are about to discuss *natural transformations* in the context of practical utility in every day code. It just so happens they are a pillar of category theory and absolutely indispensable when applying mathematics to reason about and refactor our code. As such, I believe it is my duty to inform you about the lamentable injustice you are about to witness undoubtedly due to my limited scope. Let's begin.

Curse This Nest

I'd like to address the issue of nesting. Not the instinctive urge felt by soon to be parents wherein they tidy and rearrange with obsessive compulsion, but the...well actually, come to think of it, that isn't far from the mark as we'll see in the coming chapters... In any case, what I mean by *nesting* is to have two or more different types all huddled together around a value, cradling it like a newborn, as it were.

1

Right(Maybe('b'));

2

â€‹

3

IO(Task(IO(1000)));

4

â€‹

5

[Identity('bee thousand')];

Copied!

Until now, we've managed to evade this common scenario with carefully crafted examples, but in practice, as one codes, types tend to tangle themselves up like earbuds in an exorcism. If we don't meticulously keep our types organized as we go along, our code will read hairier than a beatnik in a cat cafÃ©.

A Situational Comedy

1

// getValue :: Selector -> Task Error (Maybe String)

2

// postComment :: String -> Task Error Comment

3

// validate :: String -> Either ValidationError String

4

â€‹

5

// saveComment :: () -> Task Error (Maybe (Either ValidationError (Task Error Comment)))

6

const saveComment = compose(

7

map(map(map(postComment))),

8

map(map(validate)),

9

getValue('#comment'),

10

);

Copied!

The gang is all here, much to our type signature's dismay. Allow me to briefly explain the code. We start by getting the user input with

`getValue('#comment')`

which is an action which retrieves text on an element. Now, it might error finding the element or the value string may not exist so it returns `Task Error (Maybe String)`

. After that, we must `map`

over both the `Task`

and the `Maybe`

to pass our text to `validate`

, which in turn, gives us back `Either`

a `ValidationError`

or our `String`

. Then onto mapping for days to send the `String`

in our current `Task Error (Maybe (Either ValidationError String))`

into `postComment`

which returns our resulting `Task`

.What a frightful mess. A collage of abstract types, amateur type expressionism, polymorphic Pollock, monolithic Mondrian. There are many solutions to this common issue. We can compose the types into one monstrous container, sort and *natural transformations*.

`join`

a few, homogenize them, deconstruct them, and so on. In this chapter, we'll focus on homogenizing them via All Natural

A *Natural Transformation* is a "morphism between functors", that is, a function which operates on the containers themselves. Typewise, it is a function *natural transformation* is any function for which the following holds:

`(Functor f, Functor g) => f a -> g a`

. What makes it special is that we cannot, for any reason, peek at the contents of our functor. Think of it as an exchange of highly classified information - the two parties oblivious to what's in the sealed manila envelope stamped "top secret". This is a structural operation. A functorial costume change. Formally, a natural transformation diagram

or in code:

1

// nt :: (Functor f, Functor g) => f a -> g a

2

compose(map(f), nt) === compose(nt, map(f));

Copied!

Both the diagram and the code say the same thing: We can run our natural transformation then

`map`

or `map`

then run our natural transformation and get the same result. Incidentally, that follows from a free theorem though natural transformations (and functors) are not limited to functions on types.Principled Type Conversions

As programmers we are familiar with type conversions. We transform types like

`Strings`

into `Booleans`

and `Integers`

into `Floats`

(though JavaScript only has `Numbers`

). The difference here is simply that we're working with algebraic containers and we have some theory at our disposal.Let's look at some of these as examples:

1

// idToMaybe :: Identity a -> Maybe a

2

const idToMaybe = x => Maybe.of(x.$value);

3

â€‹

4

// idToIO :: Identity a -> IO a

5

const idToIO = x => IO.of(x.$value);

6

â€‹

7

// eitherToTask :: Either a b -> Task a b

8

const eitherToTask = either(Task.rejected, Task.of);

9

â€‹

10

// ioToTask :: IO a -> Task () a

11

const ioToTask = x => new Task((reject, resolve) => resolve(x.unsafePerform()));

12

â€‹

13

// maybeToTask :: Maybe a -> Task () a

14

const maybeToTask = x => (x.isNothing ? Task.rejected() : Task.of(x.$value));

15

â€‹

16

// arrayToMaybe :: [a] -> Maybe a

17

const arrayToMaybe = x => Maybe.of(x[0]);

Copied!

See the idea? We're just changing one functor to another. We are permitted to lose information along the way so long as the value we'll

`map`

doesn't get lost in the shape shift shuffle. That is the whole point: `map`

must carry on, according to our definition, even after the transformation.One way to look at it is that we are transforming our effects. In that light, we can view

`ioToTask`

as converting synchronous to asynchronous or `arrayToMaybe`

from nondeterminism to possible failure. Note that we cannot convert asynchronous to synchronous in JavaScript so we cannot write `taskToIO`

- that would be a supernatural transformation.Feature Envy

Suppose we'd like to use some features from another type like *Natural transformations* provide a nice way to convert to the target type knowing our

`sortBy`

on a `List`

. `map`

will be sound.1

// arrayToList :: [a] -> List a

2

const arrayToList = List.of;

3

â€‹

4

const doListyThings = compose(sortBy(h), filter(g), arrayToList, map(f));

5

const doListyThings_ = compose(sortBy(h), filter(g), map(f), arrayToList); // law applied

Copied!

A wiggle of our nose, three taps of our wand, drop in

`arrayToList`

, and voilÃ ! Our `[a]`

is a `List a`

and we can `sortBy`

if we please.Also, it becomes easier to optimize / fuse operations by moving *natural transformation* as shown in

`map(f)`

to the left of `doListyThings_`

.Isomorphic JavaScript

When we can completely go back and forth without losing any information, that is considered an *isomorphism*. That's just a fancy word for "holds the same data". We say that two types are *isomorphic* if we can provide the "to" and "from" *natural transformations* as proof:

1

// promiseToTask :: Promise a b -> Task a b

2

const promiseToTask = x => new Task((reject, resolve) => x.then(resolve).catch(reject));

3

â€‹

4

// taskToPromise :: Task a b -> Promise a b

5

const taskToPromise = x => new Promise((resolve, reject) => x.fork(reject, resolve));

6

â€‹

7

const x = Promise.resolve('ring');

8

taskToPromise(promiseToTask(x)) === x;

9

â€‹

10

const y = Task.of('rabbit');

11

promiseToTask(taskToPromise(y)) === y;

Copied!

Q.E.D. *isomorphic*. We can also write a *isomorphism* since it loses information:

`Promise`

and `Task`

are `listToArray`

to complement our `arrayToList`

and show that they are too. As a counter example, `arrayToMaybe`

is not an 1

// maybeToArray :: Maybe a -> [a]

2

const maybeToArray = x => (x.isNothing ? [] : [x.$value]);

3

â€‹

4

// arrayToMaybe :: [a] -> Maybe a

5

const arrayToMaybe = x => Maybe.of(x[0]);

6

â€‹

7

const x = ['elvis costello', 'the attractions'];

8

â€‹

9

// not isomorphic

10

maybeToArray(arrayToMaybe(x)); // ['elvis costello']

11

â€‹

12

// but is a natural transformation

13

compose(arrayToMaybe, map(replace('elvis', 'lou')))(x); // Just('lou costello')

14

// ==

15

compose(map(replace('elvis', 'lou'), arrayToMaybe))(x); // Just('lou costello')

Copied!

They are indeed *natural transformations*, however, since *isomorphisms* here, mid-chapter while we're on the subject, but don't let that fool you, they are an enormously powerful and pervasive concept. Anyways, let's move on.

`map`

on either side yields the same result. I mention A Broader Definition

These structural functions aren't limited to type conversions by any means.

Here are a few different ones:

1

reverse :: [a] -> [a]

2

â€‹

3

join :: (Monad m) => m (m a) -> m a

4

â€‹

5

head :: [a] -> a

6

â€‹

7

of :: a -> f a

Copied!

The natural transformation laws hold for these functions too. One thing that might trip you up is that *isomorphisms* were pervasive).

`head :: [a] -> a`

can be viewed as `head :: [a] -> Identity a`

. We are free to insert `Identity`

wherever we please whilst proving laws since we can, in turn, prove that `a`

is isomorphic to `Identity a`

(see, I told you One Nesting Solution

Back to our comedic type signature. We can sprinkle in some *natural transformations* throughout the calling code to coerce each varying type so they are uniform and, therefore,

`join`

able.1

// getValue :: Selector -> Task Error (Maybe String)

2

// postComment :: String -> Task Error Comment

3

// validate :: String -> Either ValidationError String

4

â€‹

5

// saveComment :: () -> Task Error Comment

6

const saveComment = compose(

7

chain(postComment),

8

chain(eitherToTask),

9

map(validate),

10

chain(maybeToTask),

11

getValue('#comment'),

12

);

Copied!

So what do we have here? We've simply added

`chain(maybeToTask)`

and `chain(eitherToTask)`

. Both have the same effect; they naturally transform the functor our `Task`

is holding into another `Task`

then `join`

the two. Like pigeon spikes on a window ledge, we avoid nesting right at the source. As they say in the city of light, "Mieux vaut prÃ©venir que guÃ©rir" - an ounce of prevention is worth a pound of cure.In Summary

`Task`

in most cases).This continual and tedious sorting of types is the price we pay for having materialized them - summoned them from the ether. Of course, implicit effects are much more insidious and so here we are fighting the good fight. We'll need a few more tools in our tackle before we can reel in the larger type amalgamations. Next up, we'll look at reordering our types with *Traversable*.

Exercises

Write a natural transformation that converts

`Either b a`

to `Maybe a`

1

// eitherToMaybe :: Either b a -> Maybe a

2

const eitherToMaybe = undefined;

Copied!

1

// eitherToTask :: Either a b -> Task a b

2

const eitherToTask = either(Task.rejected, Task.of);

Copied!

Using

`eitherToTask`

, simplify `findNameById`

to remove the nested `Either`

.1

// findNameById :: Number -> Task Error (Either Error User)

2

const findNameById = compose(map(map(prop('name'))), findUserById);

Copied!

As a reminder, the following functions are available in the exercise's context:

1

split :: String -> String -> [String]

2

intercalate :: String -> [String] -> String

Copied!

Last modified 1yr ago