Closed josephcsible closed 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.
foldl
is only bad when used on right-associative types like[]
. It's just as good asfoldr
on symmetrical types likedata Tree a = Leaf | Branch (Tree a) a (Tree a)
, and on left-associative types likedata SnocList a = Nil | Snoc (SnocList a) a
, it's better thanfoldr
. We should only fire STAN-0202 for right-associative types.