1

The programs I am talking about are Haskell(ish).

In the programming world Haskell is the most math-ish of mainstream languages. However when it comes to taking actual Haskell code and typesetting it as math the results are painful (for me so far!)

Just for context

Yeah I know this is unfashionable. Yeah I know the current fashion is to typeset programs as programs ie verbatim/courier.

But there was a time a few decades ago when this was not the established fashion. Eg First Edition of Bird & Wadler's Intro to Functional Programming was typeset as beautiful math with a small appendix tucked away showing how to transcode the math into Miranda. Second edition on, the math-Miranda became courier Haskell. A few "devotees" who say the first ed had something the second lost linger on.

Ive been exploring what it would take to replicate the B&W 1987 feel in 2022.
And so far faring not too well... Sticking at the typesetting stage itself 😕

The Details

Take this code:

\begin{flalign*}
&fib : Int &&-> (Int, Int)\\
&fib.0     &&= (0,1)\\
&fib.(n+1) &&= (b,a+b)\\
     where (a,b) = fib.n\\
\end{flalign*}

The flalign is one of the half-dozen I tried: eqnarray, align, gather $$ etc Likewise the & placement was utterly unsuccessful. Eg I want the first (type) line's alignment to not effect the alignments of the rest. And how to get the where straight (not italic) bold and indented just right has baffled me.

Some takeaways...
To say that functional programming is math is nice when discussing over coffee. But when the rubber hits the road there are a lot of gaps. eg

  1. Even a terse math language will have some keywords. These need to be recognized and typeset accordingly. Editors normally use different colors for variables, keywords, strings etc. It would be nice if tex could do something analogous and suitable.
  2. Beautiful equations in math and beautiful equations in programming are similar but different
  3. The "typeset-math" configuration should be suspendable/resetable inside the environment, particular with respect to alignment

There are other things I would like to do and am more confident of doing. Eg translate:
-> to \rightarrow
\ to \lambda
Int to \mathBB{N}
etc etc

I believe I can achieve these for the most part with the spiffy rename emacs macro from org special blocks extra. But as should be evident I am quite far from using those.

Also I would like to put mathish haskell and python (say) side by side. Tried minipages without much success. This would be a separate question but first need to get past the current sticking points!

So my questions come round to...

  • Ideally if there is some magic environment that will DWIM that'd be best.
  • If not what are the paths of approximation within the Latex world?
  • And if it's simply not do-able (equivalently too much outside the capabilities of Latex novice), I'd like to know!
0

1 Answer 1

3

Most of what you are after can be done in LaTeX's listings package using suitable literate configuration. Haskell's wiki has an example configuration.

Their configuration:

\usepackage{listings}
\lstloadlanguages{Haskell}
\lstnewenvironment{code}
    {\lstset{}%
      \csname lst@SetFirstLabel\endcsname}
    {\csname lst@SaveFirstLabel\endcsname}
    \lstset{
      basicstyle=\small\ttfamily,
      flexiblecolumns=false,
      basewidth={0.5em,0.45em},
      literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1 {=}{{$=$}}1
               {>}{{$>$}}1 {<}{{$<$}}1 {\\}{{$\lambda$}}1
               {\\\\}{{\char`\\\char`\\}}1
               {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
               {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2 
               {\ .}{{$\circ$}}2 {\ .\ }{{$\circ$}}2
               {>>}{{>>}}2 {>>=}{{>>=}}2
               {|}{{$\mid$}}1               
    }

It will replace, e.g., \ with $\lambda$, => with $\Rightarrow$, and so on.

Addendum

If you want to use multiple languages, like Haskell and some "blub" language (e.g., Python), one possible configuration is:

\usepackage{listings}
\lstloadlanguages{Haskell}

\lstnewenvironment{haskell}
    {\lstset{language=Haskell,
      basicstyle=\small\ttfamily,
      flexiblecolumns=false,
      basewidth={0.5em,0.45em},
      literate={+}{{$+$}}1 {/}{{$/$}}1 {*}{{$*$}}1 {=}{{$=$}}1
               {>}{{$>$}}1 {<}{{$<$}}1 {\\}{{$\lambda$}}1
               {\\\\}{{\char`\\\char`\\}}1
               {->}{{$\rightarrow$}}2 {>=}{{$\geq$}}2 {<-}{{$\leftarrow$}}2
               {<=}{{$\leq$}}2 {=>}{{$\Rightarrow$}}2 
               {\ .}{{$\circ$}}2 {\ .\ }{{$\circ$}}2
               {>>}{{>>}}2 {>>=}{{>>=}}2
               {|}{{$\mid$}}1               
    }}{}

\lstnewenvironment{python}
    {\lstset{language=Python,
      basicstyle=\small\ttfamily,
      % further customizations can be inserted here
    }}{}

For Python pretty-printing, there are some other threads which have their own style choices...I'm pretty uncertain what is appealing these days, with Python typography.

Addendum 2: Special Haskell Considerations

There are a number of small adjustments you can make to make the code "prettier".

1. Monadic bind. Haskell uses idiosyncratic notation for monadic "bind" operation. You can typeset this using (I forgot where I picked this up from):

\newcommand{\bind}{\mathbin{>\!\!\!>\mkern-6.7mu=}}

Then in the "literate" section, just replace {>>=}{{>>=}}2 with {>>=}{{$\bind$}}3.

2. Wildcard. Also, "wild card" patterns (the _ which appears in case expressions, for example) could be typeset better using:

\newcommand\haskWildcard{\kern0.06em \vbox{\hrule width .5em}}

Then in the "literate" section for the Haskell code, add {\ _}{{\ \haskWildcard}}2.

3. For Standard ML. If you are typesetting Standard ML, you may want to add {\ o\ }{{\ $\circ$\ }}3 and {*}{$\times$}1 to the literate section, since these are quirky to SML. (I have other suggestions for literate Standard ML, but I don't think many people care.)

4. Other random odds and ends. There are other random choices made in Haskell, like <$>, which could probably be typeset as {<$>}{{$\langle\$\rangle$}}2 in the literate section. I'm sure there are many others I have forgotten.

8
  • Tnx! Not able to try out right now. Upvoted for drawing attention to Listings package. Will get back with more (detailed) questions in a day or two
    – Rushi
    Commented Jan 25, 2022 at 3:44
  • @Rusi I've thought about this topic for some time (I'm writing a manuscript using Standard ML, trying to adhere to a "Bird & Wadler"-style), so it's great to find someone interested in this topic. Feel free to push back with objections, questions, concerns, etc. Commented Jan 26, 2022 at 16:44
  • Ok now I'm stuck on something rather basic: I want to have python and haskell together available in a document (with their individual styles of course). Can you put in some dummy code to (a) have both available and (b) show their respective call? If I can get that much working, I'll add/modify my question to have a mwe. My basic problem is that the latex you have given does not have any (obvious) nesting structure; so if I replicate it twice, replacing the "haskell" by "python" in the second its not clear how the haskell-python settings connect. Just sequence??
    – Rushi
    Commented Jan 27, 2022 at 14:47
  • @Rusi you mean you want something like, one environment \begin{haskell}...\end{haskell} for Haskell code, and another environment \begin{python}...\end{python} for Python code? Commented Jan 27, 2022 at 16:37
  • Should have added: Main thing of course is (implied) my original q: Haskell as B&W style, python in whatever is the norm nowadays
    – Rushi
    Commented Jan 27, 2022 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .