To my example-question on using existential type

Michael Hobbs <[EMAIL PROTECTED]>  writes

> [...]
> But my real point in replying to this message is to see if you actually
> need a multi-type list. Do you really *need* to have separate types for
> DMeat, DVine, etc.? For example, instead of having:
>
> data DMeat      = DMeat      {mName  :: String  -- ,...several fields
>                              } 
> data DVegetable = DVegetable { different fields }
> data DVine      = DVine      { different fields }
>  ...
>  -- 50 kinds of dishes, each described in individual manner
>  data Dish = Meat' DMeat | Vegetable' DVegetable | ... -- 50 items
>
> would it be possible to use:
>
>  data Dish = DMeat {mName :: String, ...}
>    | DVegetable {...}
>    | DVine {...}
>    | ...
>
> This would eliminate the need to have a multi-type list, for this
> particular example anyway.


No, the matter is that both are used intensively:
 * separately, the items data  DMeat, DVine ...
 * and the other parts of this large program operate with items of
   type Dinner - a collection of items of the above types.



------------------
Sergey Mechveliani
[EMAIL PROTECTED]



-------------------------------------------------------------------------
>> ... how to get free of extra constructors that are used to organise a
>> new type from several types?  ...
>>  data Name = Meat | Vegetable | Vine  | ...    deriving(Eq)
>>
>>  data DMeat      = DMeat      {mName  :: String ...} 
>>  data DVegetable = DVegetable { different fields   }
>>  data DVine      = DVine      { different fields   }  ... 
>>  type Dinner     = [(Name,Dish)]
>> ... for this, we have to join the above dish-types to one new type
>>
>>  data Dish = Meat' DMeat | Vegetable' DVegetable | ... -- 50 items
>> ...
>>  get from the dinner its meat part. If it is absent then put there
>>  such and such shashlyk.
>>  fixMeat :: Dinner -> (Dinner, DMeat)
>>  fixMeat    dn     = case  lookup Meat dn
>>                      of
>>                        Just (Meat' d) -> (dn               , d)
>>                        _              -> ((Meat,Meat' s):dn, s)
>>                                      where
>>                                      s = DMeat {mName = "shashlyk"}
>>
>> We have here the parasitic constructors  Meat', Vine'...
>> With existentials, i hoped to achieve the effect of simplification,
>> something like ...
>>               Just d -> (dn  , d)
>>               _      -> (s:dn, s)  where
>>                                    s = DMeat {name = "shashlyk"...}
>> Is this possible?






Reply via email to