How to #include into .lhs files?

Daniel Fischer daniel.is.fischer at googlemail.com
Thu Feb 3 11:51:37 CET 2011


On Thursday 03 February 2011 10:33:23, Conal Elliott wrote:
> Does anyone have a working example of #include'ing Haskell code into a
> bird-tracks-style .lhs file with GHC? Every way I try leads to parsing
> errors. Is there documentation about how it's supposed to work?
>
> Help much appreciated.   - Conal

Stupid example:

-- Main:

> {-# LANGUAGE CPP #-}
> module Main (main) where

#include "MachDeps.h"

> main :: IO ()
> main = do

#if WORD_SIZE_IN_BITS == 32

>     putStrLn "32 bits"

#include "Stuff32"

# else

>     putStrLn "64 bits"

#include "Stuff64"
#endif

-- Stuff32:

      putStrLn "Included from Stuff32"

-- Stuff64:

      putStrLn "Included from Stuff64"


It's a bit tricky. Since the C preprocessor is run after the unlit, the 
included code should not have bird-tracks, also you have to get the 
indentation right. There's probably a way to run cpp before unlit, which 
would allow you to have bird-tracks in the #include'd code.

Much easier with LaTeX-style literate code.

Cheers,
Daniel



More information about the Glasgow-haskell-users mailing list