Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Literate Haskell: References And Indexing

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.

like image 376
Deech Avatar asked Jul 18 '11 16:07

Deech


1 Answers

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.

like image 77
Federico Squartini Avatar answered Nov 13 '22 22:11

Federico Squartini