Skip to content

Commit 6f9f88e

Browse files
authored
Merge pull request #228 from hemanth/jethrolarson-morphism
refined definitions of morphisms #214
2 parents 6797f89 + 1e1ba57 commit 6f9f88e

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

readme.md

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ __Table of Contents__
5656
* [Comonad](#comonad)
5757
* [Applicative Functor](#applicative-functor)
5858
* [Morphism](#morphism)
59+
* [Homomorphism](#homomorphism)
5960
* [Endomorphism](#endomorphism)
6061
* [Isomorphism](#isomorphism)
61-
* [Homomorphism](#homomorphism)
6262
* [Catamorphism](#catamorphism)
6363
* [Anamorphism](#anamorphism)
6464
* [Hylomorphism](#hylomorphism)
@@ -723,11 +723,35 @@ partiallyAppliedAdds.ap(arg2) // [5, 6, 7, 8]
723723

724724
## Morphism
725725

726-
A transformation function.
726+
A relationship between objects within a [category](#category). In the context of functional programming all functions are morphisms.
727+
728+
### Homomorphism
729+
730+
A function where there is a structural property that is the same in the input as well as the output.
731+
732+
For example, in a [Monoid](#monoid) homomorphism both the input and the output are monoids even if their types are different.
733+
734+
```js
735+
// toList :: [number] -> string
736+
const toList = (a) => a.join(', ')
737+
```
738+
739+
`toList` is a homomorphism because:
740+
* array is a monoid - has a `concat` operation and an identity value (`[]`)
741+
* string is a monoid - has a `concat` operation and an identity value (`''`)
742+
743+
In this way, a homomorphism relates to whatever property you care about in the input and output of a transformation.
744+
745+
[Endomorphisms](#endomorphism) and [Isomorphisms](#isomorphism) are examples of homomorphisms.
746+
747+
__Further Reading__
748+
* [Homomorphism | Learning Functional Programming in Go](https://subscription.packtpub.com/book/application-development/9781787281394/11/ch11lvl1sec90/homomorphism#:~:text=A%20homomorphism%20is%20a%20correspondence,pointing%20to%20it%20from%20A.)
749+
750+
727751

728752
### Endomorphism
729753

730-
A function where the input type is the same as the output.
754+
A function where the input type is the same as the output. Since the types are identical, endomorphisms are also [homomorphisms](#homomorphism).
731755

732756
```js
733757
// uppercase :: String -> String
@@ -739,12 +763,12 @@ const decrement = (x) => x - 1
739763

740764
### Isomorphism
741765

742-
A pair of transformations between 2 types of objects that is structural in nature and no data is lost.
766+
A morphism made of a pair of transformations between 2 types of objects that is structural in nature and no data is lost.
743767

744768
For example, 2D coordinates could be stored as an array `[2,3]` or object `{x: 2, y: 3}`.
745769

746770
```js
747-
// Providing functions to convert in both directions makes them isomorphic.
771+
// Providing functions to convert in both directions makes the 2D coordinate structures isomorphic.
748772
const pairToCoords = (pair) => ({ x: pair[0], y: pair[1] })
749773

750774
const coordsToPair = (coords) => [coords.x, coords.y]
@@ -754,29 +778,22 @@ coordsToPair(pairToCoords([1, 2])) // [1, 2]
754778
pairToCoords(coordsToPair({ x: 1, y: 2 })) // {x: 1, y: 2}
755779
```
756780

757-
### Homomorphism
758-
759-
A homomorphism is just a structure preserving map. In fact, a functor is just a homomorphism between categories as it preserves the original category's structure under the mapping.
760-
761-
```js
762-
A.of(f).ap(A.of(x)) == A.of(f(x))
763-
764-
Either.of(_.toUpper).ap(Either.of('oreos')) == Either.of(_.toUpper('oreos'))
765-
```
781+
Isomorphisms are an interesting example of [morphism](#morphism) because more than single function is necessary for it to be satisfied. Isomorphisms are also [homomorphisms](#homomorphism) since both input and output types share the property of being reversable.
766782

767783
### Catamorphism
768784

769-
A `reduceRight` function that applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
785+
A function which deconstructs a structure into a single value. `reduceRight` is an example of a catamorphism for array structures.
770786

771787
```js
788+
// sum is a catamorphism from [Number] -> Number
772789
const sum = xs => xs.reduceRight((acc, x) => acc + x, 0)
773790

774791
sum([1, 2, 3, 4, 5]) // 15
775792
```
776793

777794
### Anamorphism
778795

779-
An `unfold` function. An `unfold` is the opposite of `fold` (`reduce`). It generates a list from a single value.
796+
A function that builds up a structure by repeatedly applying a function to its argument. `unfold` is an example which generates an array by from a function and a seed value. This is the opposite of a [catamorphism](#catamorphism). You can think of this as a anamorphism builds up a structure and catamorphism breaks it down.
780797

781798
```js
782799
const unfold = (f, seed) => {
@@ -798,7 +815,12 @@ countDown(5) // [5, 4, 3, 2, 1]
798815

799816
### Hylomorphism
800817

801-
The combination of anamorphism and catamorphism.
818+
The function which composes a [anamorphism](#anamorphism) followed by a [catamorphism](#catamorphism).
819+
820+
```js
821+
const sumUpToX = (x) => sum(countDown(x))
822+
sumUpToX(5) // 15
823+
```
802824

803825
### Paramorphism
804826

@@ -831,7 +853,7 @@ The third parameter in the reducer (in the above example, `[x, ... xs]`) is kind
831853

832854
### Apomorphism
833855

834-
it's the opposite of paramorphism, just as anamorphism is the opposite of catamorphism. Whereas with paramorphism, you combine with access to the accumulator and what has been accumulated, apomorphism lets you `unfold` with the potential to return early.
856+
The opposite of paramorphism, just as anamorphism is the opposite of catamorphism. With paramorphism, you retain access to the accumulator and what has been accumulated, apomorphism lets you `unfold` with the potential to return early.
835857

836858
## Setoid
837859

0 commit comments

Comments
 (0)