Implement Data.{List,Foldable}.Linear#126
Conversation
|
It is looking to me that it would be easier to decide what to include if we had a few applications working with linear lists or similar. In the abstract, it is difficult to compare the alternatives (regarding the question about |
|
All good points. I believe I can't think of a @b-mehta do you remember why not? Do you have thoughts on any of the above? |
|
I don't recall completely, but we had a quick discussion here: #48, which could be useful. As for a |
Sorry @b-mehta, I do not understand what you mean. As far as I can understand when implementing @aspiwack, Thanks, makes sense. I will make |
He says that instance Traversable ((,) a)(even linearly) But that you don't have And the argument for This means, by the way, that |
Divesh-Otwani
left a comment
There was a problem hiding this comment.
I don't recall completely, but we had a quick discussion here: #48, which could be useful. As for a
TraversablewithoutConsumable (t ()), doesn't(a,)whereaisn't consumable work? I could be wrong though, it's been a while since I've thought about these things.
Yup. I had this exact same thought after thinking through this. A linear Traversable is not always a linear Foldable. In fact, one could say a linear foldable is basically some representation of [a].
On the PR: In my opinion, it's awesome, careful and high-quality.
Changes: In my opinion, I think we should document the important parts of this discussion in the code as developer comments (not haddock). Beyond that, I have minor organizational edits and requests.
|
Before merging this. I have a thought (it can be addressed in a future PR, but this thread is the perfect venue for this discussion). It occurred to me today. @b-mehta I'd like your opinion on this as well. We've established that being “traversable” by However, being “traversable” by So, here is the thought. Why don't we give a name to the equivalent of It gives a stark distinction between |
|
I played around with a As you mentioned, it is a less common occurence; I couldn't implement instances for: It is quite interesting how big of a difference linearity can make. After spending a few hours thinking about this, it does make sense. On the other hand, it feels a bit daunting at times; having two different But I would also be happy to implement the new typeclass if you think it is worth it. |
|
The heart of my question was, rather, do we lose anything if we add this data-traversal as an additional method in foldable: class Functor t => Foldable t where
…
foldButReallyTraverseWellThisNeedABetterNameButWhatever :: Data.Applicative f => (a #-> f b) -> t a #-> f (t b)Is there any functor of notice which supports Because I have this idea that this would be a good place for it. |
|
[above comment edited after @utdemir pointed out I wrote in several mistakes. Disregard the email and go read the comment on github directly] |
|
We found a few eg. |
|
I just noticed another interesting difference. It looks like the -- Prelude.Linear.Internal.Simple.foldl (removed with this PR)
foldl :: (b #-> a -> b) -> b #-> [a] -> b
-- Data.Foldable.Linear.foldl
foldl :: (b #-> a #-> b) -> b #-> t a #-> b It looks like the old one traverses a non-linear container with a linear accumulator, which might be useful on some cases, but the new one still makes sense to me. Maybe we should have both. Unfortunately that requires us to find different names for them. |
|
For me |
I agree. However, the old signature also does make sense by itself, in fact I just had a case where I required exactly that. I guess it's derivable from the second one by mapping the list elements to |
|
Could you maybe do it with |
|
@sjoerdvisscher Thanks, yes, it looks like it is a job for I now think since the new implementation already covers the old one with some wrapping and unwrapping around it, I am happy with only including the new one for now. This PR is starting to diverge from |
|
To be frank, I lost track of this discussion. What are the conclusions in your opinion? |
|
In my opinion, the biggest open question here is linear
In short, the current state is that we have There are a few other minor discussions, mainly about choosing between alternative signatures; but we made some decisions there, and as @facundominguez said before we have some use cases it's difficult to choose between them (#126 (comment)). One of my concerns we haven't addressed is this:
If all above is good, I think this PR is good to merge (I can fix the conflicts when it's good to go). We can always revisit these decisions. |
I found yesterday that this would have been useful to define instance Dupable a => Dupable (Array a) where
dupV = traverse dupVCurrently fails with |
We are losing instances, yes, but I'm not sure that we concluded that they were useful. Maybe we just need the data-applicative-wise traversable in addition to all this. But, to be honest, I've never regarded |
I agree with this. So here's an idea. Let's side-step this discussion by removing I am convinced that we will end up having a variant of Traversable-with-data-applicatives, so lets make a separate issue to discuss that (or if we indeed want |
Me too. It does seem increasingly unavoidable. Ok for making this a list-only issue and merging fast. |
This PR implements the linear versions of
Data.ListandData.Foldablemodules. I am not a fan of the API yet, so this PR is just to open the discussion. Here are some notes:I found that there are usually a few ways to handle leftovers. Let's think about the function
any:From those three, the last one is the most generic, but unfortunately also the most confusing one. Especially when
tis[]. I think it would be better to go with the generic one, but somehow make it clear that the return value is the leftovers. I am not sure how to do that, but maybe an explicit data type might work.I think I had this issue because I tried to replicate
Data.Foldablefunction-by-function. But most functions there are not very useful in a linear context, such asnull,lengthorelem. So, maybe it would be better to only implement some subset of functions onData.Foldableand changeData.Listfunctions type signatures to be more useful in a linear context.Especially
Data.Listcomes with a lot of rewrite rules and annotations to make sure that the operations fuse. Replicating those might end up being a lot of work, especially since I do not completely understand how they work. In some cases we can't evenUnsafe.toLinearthem since our semantics are a bit different (we need to calllseqto forget the unused elements). So, this PR just implements the simplest versions.I do not think the linear
Foldablecan be derived from linearTraversable; as opposed to their base counterparts. I think the main reason is thatFoldablegives you a way to get rid of the container, whileTraversabledoesn't. So, I could not implementfoldMapDefaultusing onlyData.Traversable. The closest I got was this:which requires that ugly extra
Consumable (t ())constraint. I think we can still makeFoldablea superclass ofTraversable, but I hesitated doing it without a discussion.Do you think this is the right direction?