[Haskell-cafe] How to organize code

Jonathan Cast jonathanccast at fastmail.fm
Mon Jan 28 10:40:11 EST 2008


On 27 Jan 2008, at 11:18 PM, L.Guo wrote:

> Hi,
>
> How do you organize code ?
>
> Here is a sample.
> Acturally, I am thinking about using this plan.
>
> Any suggestions ?
>
>> -- BasicalType.hs
>> type Position = (Int,Int)
>> data Box = Box { pos :: Position }
>> data Chain = Chain { pos :: [Position] }
>
>> -- Object.hs
>> import BasicalType
>> class Object o where
>>   pos :: o -> [Position]
>
>> -- Type.hs
>> import BasicalType
>> import Object
>> instance Object Box where
>>   pos = return . BasicalType.pos
>> instance Object Chain where
>>   pos = BasicalType.pos
>
>> -- Main.hs
>> import Type
>> ...

I would recommend against this; Type.hs should define the types  
directly, and define or import the Object class.

In particular, IIRC GHC wants ever instance in the same module as  
either the type or the class definition, for performance reasons  
(this is the most common pattern, so it gets special optimizations).

jcc



More information about the Haskell-Cafe mailing list