Literate Programming in Haskell?

andrew@andrewcooke.free-online.co.uk andrew@andrewcooke.free-online.co.uk
Sun, 18 Feb 2001 11:46:54 +0000


--82I3+IH0IqGh5yIs
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline


Hi,

I'm no expert, but I've found a solution that works for me.  I use the

\begin{code}
...
\end{code}

delimiters (rather than >).  Since these aren't standard Latex, I
threw together a style file that called the listings package to handle
this.

The result is a file that ghc and hugs can compile directly, but which
latex can also process.  No need for weaving and tangling, while line
numbers in error messages are correct.  On the other hand, I'm not
doing anything very sophisticated - no labelling of blocks or anything
like that.

I needed to update to the version of tetex in the "testing" Debian
release to get this to work - the listing package in the standard
distribution doesn't support Haskell (not that I really use that
support), so if you get an error message about being unable to load
something, that may be the problem.  (Warning: I wouldn't recommend
moving to Debian testing at the moment; when I last tried the X
support had changed significantly and failed to install, so don't
upgraed anything apart from tetex).

I've included the style file.  I don't have a public example yet
(hopefully later this week), but I can email you a postscript file
directly if you want.

Haskell is the first language where I've managed to work happily with
literate programming - partly because of the direct integration and
partly because the order isn't critical and the code is compact.

Cheers,
Andrew

PS The style file includes lots of stuff unrelated to literate
programming - delete everything but the first block, if you like.

On Sun, Feb 18, 2001 at 01:53:41AM -0500, Tom Moertel wrote:
> In the Haskell community is there a generally accepted best way to
> approach Literate Programming?  The language has support for literate
> comments, but it seems that many common LP tools don't respect it.
> 
> For example, in order to convert some .lhs code into LaTeX via the noweb
> LP tools, I had to write a preprocessor to convert the ">" code blocks
> into something that noweb would respect.  (The preprocessor actually
> does a bit more and, in conjunection with noweb, gives pretty good
> results for little effort.  For an example, see:
> 
>     http://www.ellium.com/~thor/hangman/cheating-hangman.lhs
>     http://www.ellium.com/~thor/hangman/cheating-hangman.pdf
> )
> 
> Yet somehow, I don't think that my homebrew approach is optimal.  Can
> anybody recommend a particularly elegant LP setup for Haskell
> programming?  Or if you have an approach that works well for you, would
> you mind sharing it?
> 
> Cheers,
> Tom
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

-- 
http://www.andrewcooke.free-online.co.uk/index.html

--82I3+IH0IqGh5yIs
Content-Type: text/x-tex; charset=us-ascii
Content-Disposition: attachment; filename="haskell.sty"

\ProvidesPackage{haskell}

%
% Code support
%

% stop warning from listings when used in article mode
\newcounter{chapter}
\usepackage{listings}
\lstloadlanguages{Haskell}
\lstnewenvironment{code}
    {\lstset{}%
     \csname lst@SetFirstLabel\endcsname}
    {\csname lst@SaveFirstLabel\endcsname}
\lstset{
%  basicstyle=\small,
  basicstyle=\small\ttfamily,
%  keywordstyle=\underbar,
%  identifierstyle=\slshape,
%  commentstyle=\underbar
  flexiblecolumns=false,
  basewidth={0.5em,0.45em},
  literate={-}{{$-$}}1 {+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1 {=}{{$=$}}1
           {>}{{$>$}}1 {<}{{$<$}}1 
           {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
           {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2 {\ .}{{ $\circ$}}2
} 

%
% Page size
%

\usepackage[hmargin={6cm,3cm},vmargin={3cm,1cm},offset=0pt,nohead]{geometry}

%
% Titles, layout etc
%

\usepackage{titlesec}

\titleformat{\part}[frame]
{\normalfont}
{\filright\footnotesize\enspace Andrew Cooke \enspace}
{8pt}
{\Large\bfseries\sffamily\filcenter}
\titlespacing{\part}
{-3cm}{-0.5cm}{5ex}

\titleformat{\section}[leftmargin]
{\large\bfseries\sffamily\filleft}
{}{0pt}{}
\titlespacing{\section}
{3cm}{2.5ex plus .1ex minus .2ex}{1pc}

\titleformat{\subsection}[leftmargin]
{\normalfont\bfseries\sffamily\filleft}
{}{0pt}{}
\titlespacing{\subsection}
{3cm}{1.5ex plus .1ex minus .2ex}{1pc}

\setlength{\parindent 0pt}
\setlength{\parskip 1.5ex plus 0.5ex minus 0.2ex}

%
% eps images
%
\usepackage{graphicx}

--82I3+IH0IqGh5yIs--