Skip to content

Issues with mappend of SavedConfig. #2160

Closed

Description

@erikd

I'm currently working on implementing (and writing tests for) a cabal saved-config update command (which read's the user's ~/.cabal/config and the writes it back with comments from the current version).

During this exercise, I found some odd issues around using mappend on the SavedConfig structure. In particular where elements of that structure are lists, eg extra-prog-path, remote-repo and build-summary some opertations with mappend resulted in duplicate entries in the above variables.

On IRC @dcoutts said that all three of these do in fact need to be lists, but for all of them they should be de-duplicated lists.

My proposal is the replace the list for the above three config elements with a NubList type, a list where every element in the list is unique to the list but which also maintains the original list ortder. I would then define a Monoid instance for NubList (the properties of identity, associativity, and closure are all satisifed)

 (NubList xs) `mappend` (NubList ys) = NubList . nub $ xs ++ ys

so that mappend-ing SavedConfig will then work in a sensible manner. Obviously I would be adding QuickCheck properties for the Monoid NubList instance.

Comments?

Activity

  1. 23Skidoo commented on Oct 14, 2014

    @23Skidoo
    Member

    See also #2111.

  2. erikd commented on Oct 14, 2014

    @erikd
    MemberAuthor

    @23Skidoo That's interesting. One thing I notice is that what you have there requires the Ord typeclass so that the list can be sorted. For things like extra-prog-path the original ordering may be important and should be maintained which is not possible when using your ordNubRight function.

    I actualy have my fixes for this very close to working. I just need to add some more tests before I push it to github so we can discuss it futher.

  3. added a commit that references this issue on Oct 15, 2014
    2efe525
  4. 23Skidoo commented on Oct 16, 2014

    @23Skidoo
    Member

    @erikd ordNub does preserve the original ordering and is equivalent to nub. The reason I also added a right-biased variant is that with program options you want ["-XNoFoo", "-XFoo"] mappend ["-XNoFoo"] to result in ["-XFoo", "-XNoFoo"]. Not sure if that's the case with paths, but you can of course use the left-biased ordNub.

  5. erikd commented on Oct 16, 2014

    @erikd
    MemberAuthor

    Ah ok. I just assumed that because ordNub required the Ord typeclass constraint that it was returned the list ordered.

    With paths, I think left-biased makes more sense.

  6. erikd commented on Oct 16, 2014

    @erikd
    MemberAuthor

    Maybe we could define both left and right biased versions of NubList, NubListL and NubListR.

    The nice thing about using a type rather than just defining functions to do this is that the types can enforce the invariants while maintaining the ability to just use mappend.

  7. 23Skidoo commented on Oct 17, 2014

    @23Skidoo
    Member

    The nice thing about using a type rather than just defining functions to do this is that the types can enforce the invariants while maintaining the ability to just use mappend.

    I'm +0 on using types for this. Usually we're not mappending NubLists themselves, but records that include them as members, for which one has to write boilerplate Monoid instances anyway.

  8. erikd commented on Oct 17, 2014

    @erikd
    MemberAuthor

    The fact that the records are mappened-ed is exactly why you need to components inside the record to do the right thing by default. That is why you want to encode the right bechaviour in the types.

  9. bitemyapp commented on Oct 17, 2014

    @bitemyapp
    Contributor

    @23Skidoo in lieu of a much more time intensive and possibly harder to understand data structure that probably wouldn't be worth it, NubList is the right way to do this in my opinion. They're typically small lists and fixing the correctness problem in this manner is more "by construction" than the code it replaced.

    I'm +1 on NubList, for whatever that's worth.

  10. cartazio commented on Oct 17, 2014

    @cartazio
    Contributor

    I like newtypes because they document invariants.
    edit: document and enforce

  11. 23Skidoo commented on Oct 18, 2014

    @23Skidoo
    Member

    OK, let's add a newtype (NubList/NubListR), but make it use ordNub and listUnion from #2111.

    in lieu of a much more time intensive and possibly harder to understand data structure that probably wouldn't be worth it,

    No-one was suggesting adding a different data structure.

  12. 23Skidoo commented on Oct 18, 2014

    @23Skidoo
    Member

    @erikd Can you please split NubList-related commits from #2159 into a separate pull request so that I could merge them more quickly?

  13. erikd commented on Oct 19, 2014

    @erikd
    MemberAuthor

    The problem is that the tests I wrote for the feature I added were failing because of the existing behaviour mappend on the SavedConfig type. The NubList code therefore needs to be applied before adding the new features.

    I've lost count of how many times I rebased this set of commits, but I will do it once more.

  14. added a commit that references this issue on Oct 19, 2014
    efd5543
  15. erikd commented on Oct 19, 2014

    @erikd
    MemberAuthor

    The NubList changes by themselves are in: #2167

    The saved/user-config features require NubList.

  16. 23Skidoo commented on Oct 19, 2014

    @23Skidoo
    Member

    Fixed by #2167.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions