kowainik / stan

🕵️ Haskell STatic ANalyser
https://kowainik.github.io/projects/stan
Mozilla Public License 2.0
565 stars 48 forks source link

STAN-0202 should only apply to right-associative types #343

Closed josephcsible closed 4 years ago

josephcsible commented 4 years ago

foldl is only bad when used on right-associative types like []. It's just as good as foldr on symmetrical types like data Tree a = Leaf | Branch (Tree a) a (Tree a), and on left-associative types like data SnocList a = Nil | Snoc (SnocList a) a, it's better than foldr. We should only fire STAN-0202 for right-associative types.

chshersh commented 4 years ago

The structure of the data type doesn't tell how the foldl is implemented. You still can treat such SnocList as an ordinary list with flipped constructors. To truly understand whether the foldl is efficient or not, you need to analyse the implementation of foldl. But this looks like an unsolvable issue. Usually, it's almost impossible to infer such high-level insights from code analysis due to the unsolvability of such problems.

Additionally, Stan doesn't have access to HIE files of dependencies currently, so if such types and their instances are defined externally, we can't do anything about this. So I believe that the implementation of the algorithm for dealing with right-associative folds will complicate Stan logic a lot without bringing too much benefit. Again, if people use right-associative types specifically for the reasons of using better foldl they know what they are doing and can ignore such inspection.