[Haskell-cafe] Mutually recursive modules

Francesco Mazzoli f at mazzo.li
Wed May 8 09:51:12 CEST 2013


At Wed, 8 May 2013 09:46:08 +0300,
Roman Cheplyaka wrote:
> 
> I wonder whether it's always possible to break cycles using GHC's
> .hs-boot files.
> 
> Consider the following schematic example:
> 
>   module A where
> 
>   import B
> 
>   data A
> 
>   f :: B -> A
>   f = undefined B.g
> 
>   module B where
> 
>   import A
> 
>   data B
> 
>   g :: A -> B
>   g = undefined A.f
> 
> A.hs-boot must give a type signature for f, and since the signature
> contains 'B', it must import 'B'. Ditto for B.hs-boot — it must import
> 'A'.
> 
> Even if we treat all imports as {-# SOURCE #-}, there is still a cycle
> between the hs-boot files.
> 
> So, am I right in understanding that these recursive modules cannot be
> compiled by GHC at all?

This configuration works for me:

A.hs:

    module A where
    import B
    
    data A
    
    f :: B -> A
    f = undefined B.g

A.hs-boot:

    module A where
    
    import {-# SOURCE #-} B
    
    data A
    
    f :: B -> A

B.hs:

    module B where
    import {-# SOURCE #-} A
    
    data B
    
    g :: A -> B
    g = undefined A.f

B.hs-boot:

    module B where
    data B

Then I can compile them:

    bitonic at clay /tmp % ghc -c B.hs-boot
    bitonic at clay /tmp % ghc -c A.hs-boot
    bitonic at clay /tmp % ghc -c B.hs 
    bitonic at clay /tmp % ghc -c A.hs 

Francesco



More information about the Haskell-Cafe mailing list