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
- 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.
- Beautiful equations in math and beautiful equations in programming are similar but different
- 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!