foldl icon indicating copy to clipboard operation
foldl copied to clipboard

An idea concerning ST

Open nikita-volkov opened this issue 8 years ago • 4 comments

I've had an interesting idea. There seems to be a way to specify all kinds of pure Fold as a case of FoldM spesialised to ST. This would solve the issue of the divergence of the API into pure and effectful. Also it would allow to use temporary mutable data-structures (those that have an ST API) in the accumulator.

type Fold =
   forall s. FoldM (ST s)

It's hard to predict how that would affect GC and performance, but it surely seems interesting to explore.

I'm posting this primarily as a matter of a discussion.

nikita-volkov avatar Jul 10 '17 09:07 nikita-volkov

If the goal is to eliminate the API divergence, you can also do:

type Fold = FoldM Identity

... and I know that @michaelt has done experiments showing that it gives identical performance

The main reason that I keep the separate APIs is just to preserve simpler inferred types and error messages when all you need is a Fold

Gabriella439 avatar Jul 10 '17 14:07 Gabriella439

In general I feel Haskell is often plagued with multiple APIs (fmap, map, ...). The excuse is often the same (better error messages and so on).

I just want to express the fact that multiple APIs are also very confusing for beginners (at least this is my experience coming from languages where subtype polymorphism is more natural) .

This is just a general comment and it might be not so relevant in this case. Sorry about that.

PierreR avatar Jul 10 '17 19:07 PierreR

I like having both Fold and FoldM as separate things. The decision to go monadic is a big deal.

In this instance, I actually dont know what ST is (I used to know but forget), and since I do know what a Fold is, it seems like a confusing addition.

tonyday567 avatar Jul 12 '17 07:07 tonyday567

I'm +1 for merging Fold and FoldM, if that's up for discussion.

mitchellwrosen avatar Apr 04 '18 04:04 mitchellwrosen