Skip to content

Instantly share code, notes, and snippets.

@agalatan
Last active November 13, 2021 04:04
Show Gist options
  • Save agalatan/6906c233cc46e447b1fe38a1345a4de7 to your computer and use it in GitHub Desktop.
Save agalatan/6906c233cc46e447b1fe38a1345a4de7 to your computer and use it in GitHub Desktop.
The HOM functor for the Flow type category
//@flow
import * as React from 'react';
type Hom<-A, +B> = React.AbstractComponent<A, B>;
// A is the Category of Props: They are represented as objects { [string]: any }
// B is the Category of Elements: They are represented as JSX.
//
// Diverging now into other another Category: the category of Sets
// Fix for now a map
// phi: X => Y
//
// Arrows/maps between objects of a category are called "morphisms" (homomorphisms)
// Hence phi is a member of Hom(X, Y), the set of morphisms/arrows/maps from X to Y
// Hom(X, Y) is the set of Functions from X to Y
//
// Let "any" be just some generic name for some space (no types yes). Let
// F: Y => any
// be a function. Compose to get the following
// x: X => phi(x): Y => F(phi(x)): any
//
// Let's recap what happened: We started with an arbitrary, but fixed
// phi: X => Y
// and we obtained a map (we'll name it later)
// Fun(Y) => Fun(X)
// We have now a functor. Call it Fun:
//
// It is the functor which, to "an input X", associates the "space of functions on
// that input, call it Fun(X)".
// X => Fun(X)
// But also, given phi, a "morphism" (homomorphism aka map aka arrow) from X to Y,
// Fun transformed it into a morphism, but this time from Fun(Y) to Fun(X).
// (X => Y) => (Fun(Y) => Fun(X))
// In fancier language:
// Hom(X, Y) => Hom(Fun(Y), Fun(X))
// Let's denote this operation for now (by abuse of language, but it should be obvious
// from the context when Fun acts on the objects of the category, or on its homomorphisms).
// phi => Fun(phi)
//
//
// Once again, see that the functor NOT ONLY acts on objects of a category, but also acts
// on the morphisms of that category. The functor Fun is called "the pullback functor":
// it pulls functions from Y, to functions from X. Also called the "dual" functor.
//
// phi: Hom(X, Y) => Fun(phi): Hom(Fun(Y), Fun(X))
// IT IS A CONTRAVARIANT FUNCTOR
// BECAUSE IT FLIPS THE ARROWS
// $ExpectError
// type Fun<+X> = X => any
type Fun<-X> = X => any
function pullbackOfHOM<X, Y>(
phi: X => Y,
): (f: (Y => any)) => (X => any) {
return x => f(phi(x))
}
// Now look at this: https://flow.org/en/docs/react/hoc/#toc-hocs-before-0-89-0
// Then at this: https://flow.org/en/docs/react/hoc/#toc-hocs-as-of-0-89-0
// And then, at the top of this Gist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment