If all carts behave the same and contents don't matter just store PlainBaskets, i.e. a Basket without the annotation.Can you clarify the quoted statement. If I were to make store PlainBaskets in the Cart, where would be annotating the Item as Changeble/Unchangable?
-- A PlainBasket is to a Basket as a PlainItem is to an Item. data PlainBasket = PlainBasket { plainBasketName :: String, plainBasketItem :: PlainItem } data Basket c = Basket { basketName :: String, basketItem :: Item c } newtype Cart = Cart [PlainBasket] addBasketToCart :: Basket c -> Cart -> Cart addBasketToCart Basket{..} (Cart cart) = Cart $ newPlainBasket:cart where newPlainBasket = PlainBasket { plainBasketName = basketName , plainBasketItem = plainItem basketItem }
The newtype protects your cart from arbitrary changes, so frozen
baskets are still protected. But the annotations are discarded. So
naturally, you can not recover changeability and you shouldn't
allow arbitrary mapping. If you need one of these, this is not the
right option. It really depends on what you actually need.
Of course you could add a changeability field to PlainBaskets
and thus store the information at runtime again. But Theodores Either-approach
or variations of the class I propose in my the third option are
more elegant forms of that.