<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<blockquote
cite="mid:CAAq9Y10cnbVq+ERjH_TNt4QJNVegOi6b-U3+RHgpicJ5E_Ky9A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex"><i>If
all carts behave the same and contents don't matter</i>
just store <i>PlainBasket</i>s, i.e. a <i>Basket</i>
without the annotation.</blockquote>
<div> </div>
<div>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?<br>
</div>
</div>
</div>
</div>
</blockquote>
<br>
Well <i>contents don't matter, </i>so there is no annotation once
you enter a cart.<br>
<pre> -- A <i>PlainBasket</i> is to a <i>Basket</i> as a <i>PlainItem</i> is to an <i>Item.</i>
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
}
</pre>
<p>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.<br>
</p>
<p>Of course you could add a changeability field to <i>PlainBasket</i>s
and thus store the information at runtime again. But Theodores <i>Either</i>-approach
or variations of the class I propose in my the third option are
more elegant forms of that.<br>
</p>
</body>
</html>