MattWindsor91 / travesty

Haskell-style container traversability and state monads for Core
https://MattWindsor91.github.io/travesty
MIT License
10 stars 1 forks source link

Feature proposal: bifunctors #1

Closed MattWindsor91 closed 5 years ago

MattWindsor91 commented 5 years ago

Bifunctors generalise containers whose items contain two pieces of data, and where we can map over both of them at the same time:

type ('a, 'b) t
val bimap : f1:('a -> 'b) -> f2:('c -> 'd) -> ('a, 'c) t -> ('b, 'd) t

This pattern comes up in several places in OCaml/Core:

It'd be nice if travesty supported this (though I figure the name would be Bi_mappable or something similar). It'd also be nice if travesty supported fixing the type of the first or second item; for example,

type elt
type 'b t (* 'a has become elt *)
val bimap : f1:(elt -> elt) -> f2:('a -> 'b) -> 'a t -> 'b t

and likewise for the other side. (This would be especially helpful for working with Or_error.t—it'd let you combine mapping over results and errors.)

Another possibility is to implement a few of the ideas from the bifunctors Haskell library:

MattWindsor91 commented 5 years ago

Done.