15

Does Literate Haskell support indexing function names, typeclasses and variable references? Is there a filter I can run on Literate Haskell source that will do this and give me either a nice PDF manual or a hyperlinked HTML document.

These are a really nice features of noweb and CWEB which I think it would spur widespread adoption of Literate Haskell.

As an example, look at the word count program written in CWEB. The code chunk on the first page in item #4 is footnoted with where that code is used. LHS doesn't support chunks but I'd like to know where the code is being used:

  1. Comment describing func.

    func = id

    Used in: (X.Y.Z.f, A.B.C.g, Section 1.5)

    func2 = indefined

    Used in: (A.B.C.x, Section 2.1)

And additionally an index that aggregates all the function names and variables along where they're referenced in the document and by other functions etc.

jkoshy
  • 1,793
  • 15
  • 23
Deech
  • 2,223
  • 15
  • 20
  • I think it should be easy to emulate this through a creative use of LaTeX. – fuz Jul 18 '11 at 17:04
  • Currently Haddock and the ":info" command shows me instances of a typeclass. But I can't see where functions and variables are used. – Deech Jul 18 '11 at 17:12
  • By "indexing," do you mean an index that links each name to its documentation, such as Haddock produces? – Heatsink Jul 18 '11 at 17:44
  • @Heatsink: I've clarified my question. – Deech Jul 18 '11 at 18:18
  • This looks like the same problem Racket has solved in Scribble (a markup reader extension for Racket designed for creating documentation.) – Theo Belaire Sep 25 '11 at 15:27

1 Answers1

1

There are some possibilities in Latex, the following uses the package listings, together with makeindex to create a list of all functions. Furthermore \label is used to create cross-references between different sections:

\documentclass[a4paper,11pt,reqno,twoside,pdflatex,makeidx]{amsart}

\usepackage[a4paper]{geometry}

\usepackage{listings}
\lstloadlanguages{Haskell}

\lstset{
    flexiblecolumns=false,
    basewidth={0.5em,0.45em},
    basicstyle=\ttfamily,
    language=haskell,
    % numbers=left, % optional numbering of code lines
    firstnumber=last,
    numberstyle=\tiny,
    stepnumber=2,  
    numbersep=5pt,
    index={fac,fac2}
}

\lstnewenvironment{code}{}{}

\usepackage{hyperref}

\title{The factorial function}
\author{Federico Squartini}
\date{}


\makeindex

\begin{document}
\maketitle
\section{Factorial function}
\label{code:fac1}
The factorial function can be defined as:

\begin{code}

fac 0 = 1
fac n = n * fac (n-1)

\end{code}
  
\section{Factorial function in constant space}
The code for the factorial defined section~\ref{code:fac1} uses $o(n)$ stack
space. The following function uses constant space:

\begin{code}

fac2 n = go 1 1
    where
      go !acc i| i <= n = go (acc*i) (i+1)
               | otherwise = acc

\end{code}

\printindex
\end{document}

Compile with:

pdflatex example.tex

makeindex example.idx

pdflatex example.tex

pdflatex example.tex

Resulting pdf is here. This is great for producing pdf files. For other kind of outputs (e.g. html) you should use latex together with pandoc.

Another option is to use pandoc's markdown syntax, mixed with ad hoc latex commands (\label and makeindex). This should simplify the task as well as producing less syntactic noise in the source files.

Community
  • 1
  • 1
Federico Squartini
  • 1,188
  • 8
  • 14
  • This is incredibly cool but ultimately not what I'm looking for because it requires manual intervention using label to determine the names of functions. Thanks for the Latex lesson! – Deech Jul 23 '11 at 20:20