<div dir="ltr">Hello All,<br><br>I have a design question where I could use some of your thoughts and suggestions. Here is the setup. I apologize<br>for the long email but I have resisted asking this question, since it needs a long explanation, until now.<br><br>I have a set of data structures defined as follows<br><br>--  An Item that can be of two types,  one whose value can be changed, one whose value are frozen once created  <br>data Item = FreeToChange {freeToChangeCount:: Int}<br>  | CannotChange {frozenCount:: Int}<br><br>-- The item is part of a basket<br>data Basket = Basket { name:: String, item::Item }<br><br>-- The cart can have both kind of Baskets at the start of the program, or during runtime.<br>data Cart = List Basket<br><br>You can imagine this be a  shopping cart with fixed set of items. Where the count of<br>some of the items in the basket can be changed during shopping but not the count of the<br>items once they are tagged as frozen.<br><br>Therefore, valid operation are:<br><br>1. I can create an Basket with either FreeToChange item or CannotChange item.<br>2. I can update the count for FreeToChange item in the Basket<br>3. But, once I create an instance of the Basket to contain the CannotChange item,<br>   we cannot update the count anymore or update the Basket.<br><br>One approach I have taken is to throw an error if this happens  by pattern matching on type.  But, this is <br>an runtime check.<br><br>addToBasket :: Basket -> Basket<br>addToBasket b = let<br>  i = item b<br>  i' = case i of<br>    FreeToChange f -> FreeToChange {freeToChangeCount = f + 1}<br>    CannotChange f -> error ("This operation is not allowed")<br>  in<br>    b {item=i'}<br><br><br>Here are my questions:<br><br>1. Is there  a way to design the above data structures in such a way I could use the type system.  <br>2. Since, these are runtime changes, is it even a good design pattern to push this responsibility to a type system?<br><br><br>Thanks<br>Guru<br><br><br></div>