Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:37:11+00:00 2026-06-10T22:37:11+00:00

I want a Bag container which hides its ‘real’ order from its clients. It

  • 0

I want a Bag container which hides its ‘real’ order from its clients.

It also must be fully polymorphic, that is shouldn’t require any constraints over its element type.

I found at least three implementations of bags: Bag module from ghc package, Data.Bag from bag and Math.Combinatorics.Multiset from multiset-comb.

However, they all have toList and fold* operations which expose the internal order of elements which may depend on implementation details or the order of bag construction.

toList is impossible, at least with type Bag a -> [a]. However, folding does not always expose the order.

For example, fold (+) 0 does not expose.

The question is, how should I design the folding interface? Is there a necessary and sufficient condition for safety of the a -> a -> a folding function? As fmap does not expose the order, is there a loss of genericity from folding with a -> b -> b?

I’m thinking of commutative monoids – they seem sufficient, but I’m not sure if associativity and identity element are necessary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:37:13+00:00Added an answer on June 10, 2026 at 10:37 pm

    An identity is probably necessary if your bags can be empty – you have to return something in that case, and if you want your fold to be a homomorphism (so that combining the results of folding some bags is the same as folding a bag made by combining the bags, which is a pretty natural property to expect), it must be an identity element.

    Associativity, likewise, is a good idea. Suppose I have a type and operation like so:

    data T a = Zero | One a | Two (T a) (T a)
      deriving (Eq, Ord, Show)
    
    (+-+) :: Ord a => T a -> T a -> T a
    Zero +-+ x = x
    x +-+ Zero = x
    a +-+ b = Two (min a b) (max a b)
    

    Clearly (+-+) is commutative and has an identity, but is non-associative. Suppose I then implement a bag as a list:

    newtype Bag a = Bag [a]
    
    -- pre-condition: f is commutative and z is an identity for it
    foldBag :: (a -> a -> a) -> a -> Bag a -> a
    foldBag f z (Bag xs) = foldr f z xs
    
    foldNonAssoc :: (Ord a) => Bag (T a) -> T a
    foldNonAssoc = foldBag (+-+) Zero
    

    Even if I demand the stated precondition, I can still use my foldNonAssoc to distinguish between Bag [One 1,One 2,One 3], which will fold to Two (One 1) (Two (One 2) (One 3)) and Bag [One 3,One 2,One 1], which will fold to Two (One 3) (Two (One 1) (One 2)) (notice that not all of the structure is preserved, but on a long list I’ll get the entire list order back except for the ordering of the last two elements).

    A priori, if you combine all your items with an operation, you’ll have a tree of applications, something like a +-+ (b +-+ (c +-+ d)). Commutativity will let you do some rearrangement, but no matter what you do, c will always be combined with d. So if you want that to be the same as (a +-+ c) +-+ (b +-+ d), you really need associativity, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to progamatically get the grid bag constraints of a panel which is
Want to run javascript function from parent window in child window Example I have
Want to open firefox from terminal at linux with firebug enabled // Terminal $
want to have a Hyperlink-Button in a gridView in which I can display a
I have an class which has a collection, mapped as a bag in my
Here's a fairly normal encapsulation of an STL container which allows the user of
I have a class, Container<T>, which has a ContainerContents<T>. The Container actually takes two
I want to implement bag of words in opencv. after detector->detect(img, keypoint); detects keypoints,
I want to have a way in which I can add all my script
I have some data I'm getting from a datasource which is a bunch of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.