Skip to content

Instantly share code, notes, and snippets.

@ehrenmurdick
Created August 16, 2021 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehrenmurdick/e5fff5900ff62274e42fa3c3613f5c07 to your computer and use it in GitHub Desktop.
Save ehrenmurdick/e5fff5900ff62274e42fa3c3613f5c07 to your computer and use it in GitHub Desktop.
Peano numbers in facebook flow!
// @flow
type Zero = {}
type Succ<M> = { succ: M } // doesn't matter what the type actually has in it
type P = Zero | Succ<P>
type Increment = <P>(P) => Succ<P>
type Prev = <P>(Succ<P>) => P
type One = $Call<Increment, Zero>
type Two = $Call<Increment, One>
type Three = $Call<Increment, Three>
// the ("": any) is because I just have to have an inhabitant. casting it through any
// lets me cast it to anything else, but once it's gone through the first
// cast it has a definite type. Flow makes no changes at all to the actual code,
// it only checks the type. "Compilation" is to just delete all the flow annotations.
// An actual inhabitant of Three would just be the object { succ: {succ: {succ: {}}}}
;((("": any): $Call<Prev, Three>): Two)
// Delete this line and the error goes away.
;((("": any): $Call<Prev, Three>): One)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment