npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@markandrus/effect-derive

v0.1.1

Published

Derive Covariant (Functor), Foldable, and Traversable instances, as well as base functors, for algebraic data types (ADTs)

Downloads

60

Readme

effect-derive

This project provides a command-line tool for deriving types and type class instances that can be used with Effect. Specifically, it can derive

| Effect type class | Haskell equivalent | |:---------------------------- |:---------------------------- | | Covariant | Functor | | Foldable | Foldable | | Traversable | Traversable |

It can also derive base functors for recursive data types, along with their Recursive and Corecursive type class instances, in the style of recursion-schemes. This unlocks the ability to fold and unfold instances of the recursive data type using functions like cata, para, histo, and ana.

Usage

Derive Covariant, Foldable & Traversable

Assume we have the following definition of a List:

// src/examples/List.ts

export type List<A>
  = { type: 'Nil' }
  | { type: 'Cons', head: A, tail: List<A> }

Then we can generate its Covariant, Foldable, and Traversable instances as follows:

npx @markandrus/effect-derive \
  Covariant Foldable Traversable \
  --for-type List \
  --discriminator type \
  --in-file src/examples/List.ts \
  --out-file src/examples/List.derived.ts

Registering additional type class instances

Assume we have the following definition of a RoseTree:

// src/examples/RoseTree.ts

export type RoseTree<A> = {
  rootLabel: A
  subForest: ReadonlyArray<RoseTree<A>>
}

The tool doesn't know anything about ReadonlyArray, so we must tell it where to find its Covariant, Foldable, and Traversable instances:

npx @markandrus/effect-derive \
  Covariant Foldable Traversable \
  --for-type RoseTree \
  --discriminator type \
  --in-file src/examples/RoseTree.ts \
  --out-file src/examples/RoseTree.derived.ts \
  --covariant '@effect/typeclass/data/Array#Covariant#ReadonlyArray<_>' \
  --foldable '@effect/typeclass/data/Array#Foldable#ReadonlyArray<_>' \
  --traversable '@effect/typeclass/data/Array#Traversable#ReadonlyArray<_>'

You can register as many additional type class instances as needed.

Derive BaseFunctor, Recursive & Corecursive

Many recursive data types can be expressed as the fixed point of some base functor. For example, we can derive the base functor for List by replacing each of its recursive positions with a type variable:

export type ListF<A, X>
  = { type: 'Nil' }
  | { type: 'Cons', head: A, tail: X }

We can derive this base functor, as well as its Recursive and Corecursive instances, as follows:

npx @markandrus/effect-derive \
  BaseFunctor Recursive Corecursive \
  --for-type List \
  --discriminator type \
  --in-file src/examples/List.ts \
  --out-file src/examples/ListF.derived.ts

Note that Covariant is derived automatically with BaseFunctor, but you can also derive Foldable and Traversable, if you like. You can also register additional type class instances, as needed.

Register an existing TypeLambda

Most of the commands will derive TypeLambda instances as necessary; however, if you already have a TypeLambda you want to reuse, you can register it:

--type-lambda './src/examples/List.derived#ListTypeLambda#List'

Limitations

  • Currently, only relatively simple types are supported. We should expand support to tuples and other sorts of types, including function types.
  • Effect's higher-kinded type (HKT) encoding restricts the arity of types this tool can derive instances for to just 2 or 3 type parameters.
  • The tool does not check all the requirements for legal instances that it should; however, these could be added.
  • The tool usually works with the last type parameter, in Haskell-style; however, many Effect types map over the first type parameter. The tool should support specifying type parameter to use.

Developing

Install, generate, and build everything as follows:

pnpm install
pnpm run "/^generate:.*/"
pnpm build
pnpm test

Related work

This project is a loose port of GHC's DeriveFunctor and Edward Kmett's recursion-schemes to TypeScript. It reuses type classes from Effect and depends on its HKT encoding.