# multiletter names in lambda calculus

First, I am new to TeX and use it only for mathematical expressions. IMHO the most striking feature is that TeX does not respect whitespaces.

1. In lambda calculus, application is denoted by juxtaposition. In programming languages based on lambda calculus (ML, Haskell) names may be several letters long, therefore, to separate names in application, a whitespace is placed between names. I want to use function names from standard libraries of those programming languages, therefore I can not just give up multiletter names. Currently I use “\ ” in application, but it seems hackish to me and clutters the code. What is the best way to do this? Example: You can take just any functional code, like $foldr\ f\ z\ (x:xs) = f\ x\ (foldr\ f\ z\ xs)$.

2. BTW, is it regular in lambda calculus to embrace multiletter names in “operatorname”?

• \operatorname{func} will indeed add a thin space when the string is followed by another similar object, but not before an open parenthesis. Would you please add some examples to your question? And maybe some reference for us to look at. Sep 14 '11 at 8:49
• Can you give an example of the sort of thing you would like to be able to write and the output you would like it to produce? Sep 14 '11 at 8:50
• @egreg: I added an example. Sep 14 '11 at 9:20
• For other readers: the most popular answer is to generate the needed code with lhs2TeX. Jul 19 '17 at 2:37

The semantic package can be helpful here. Among other things, semantic provides a reserved words feature that allows you to do the following:

% Preamble:
\usepackage{semantic}
\reservestyle{\concretesyntax}{\mathtt}
\concretesyntax{let[let\;],in[\;in\;]}

% Later:
$\<let> x = 5 \<in> x$


Produces this image:

You could define foldr, f, etc the same way (possibly exchanging \mathtt for something else).

Beyond that, semantic provides ligatures which provide a way to fix the spacing of : and .. (Both should have a space after, but not before.)

% Preamble:
\mathlig{:}{\!:}
\mathlig{.}{.\:}

% Later:
\lambda x: nat. x + 1


Produces this image:

Note: These styles attempt to mimick those from John Mitchell's textbook Foundations for Programming Languages. (Strangely enough, the formatting in my physical copy of his book differs from the downloadable chapter online, so keep that in mind.) In my opinion, my methods above do a much better job of reproducing his formatting than egreg's answer, which destroys spacing around : and removes italics on the variable names.

It's not quite clear what symbols should be followed by a space and what not.

Something like

\newcommand{\lname}[1]{\mathop{\kern0pt\mathit{#1}}}


and

$\lname{foldr}\lname{f}\lname{z}(x:xs) = \lname{f}x(\lname{foldr}\lname{f}\lname{z}xs)$


might give good results. In other words, put each symbol you want to be spaced as argument of \lname. One might choose a different name for it: \? for example. It's quite hard to think how to render such formulas automatically, without explicit markup.

A similar effect as Andrew Stacey's can be obtained in standard LaTeX: the following lines go in the preamble

\newenvironment{lambdac}
{\catcode =12 \setupspace
\makeordinary{:}% to make the colon an ordinary symbol
%%% possible other \makeordinary declarations
$\mathgroup0 } {$}
{\catcode =\active\gdef\setupspace{\def {\;}}}
\makeatletter
\newcommand{\makeordinary}[1]{\@tempcnta=\mathcode#1
\@tempcntb=\@tempcnta
\divide\@tempcntb by "1000
\multiply\@tempcntb by "1000
\mathcode#1=\@tempcnta}
\makeatother


In the document one can use

\begin{lambdac}
foldr f z (x:xs)=f x (foldr f z xs)
\end{lambdac}


One should put spaces only where they must appear in the output.

Note: the space has mathcode "8000, but this mathcode is ignored when the space has category code different from 11 or 12. The indirect definition of the active space avoids a global definition that could interfere with the \verb command.

Note: the \makeordinary macro strips off the first hexadecimal digit from a mathcode.

Result:

• “It's not quite clear what symbols should be followed by a space and what not.” Every lambda application corresponds to 1 space. I.e. there is a space between “foldr f z” and “(x:xs)”, unlike in the common function application. (IMHO this method yields the best visual results.) Sep 14 '11 at 9:47
• Wish I could vote this up again! Now I see why the mathcode bit didn't work. What does the \mathgroup0 do? Also, that's an important point about necessary spaces. My version has spaces at the start of the line which shouldn't be there! Sep 14 '11 at 10:48
• @Andrew Saying \mathgroup0 is equivalent to enclose the whole formula in \mathrm, as it chooses math family 0 that always contains upright letters. \mathgroup and \fam are synonymous. Sep 14 '11 at 10:55
• In that case, if you put in the redeclaration of : then your answer becomes a superset of mine! And with more concise code. Could you add that in? Sep 14 '11 at 11:30
• Just tried compiling your code (to produce a screenshot - the one thing my answer still has that yours doesn't!) and discovered a problem. \DeclareMathSymbol can only be used in the preamble (unicode-math removes this prohibition). So either you need to set : as \mathord for the whole document, or you need to do something more wizardry. Sep 14 '11 at 12:14

I've struggled with the same thing myself, and ended up with two possible solutions; first of which is basically egreg's solution (though you could just make the functions straight to macros, ie. \foldr). The other one is making math-mode obey spaces, but that has its own problems.

In the end, you basically won't save any typing with either of those.

To expand a little, what I've done is to define macros (these are in plain-format, latex-format would look different):

\def\defun#1{\mathop{\hbox{\it#1}}} % define functions as math operators,
% so they get the spacing (note: not a "full" space, but a '\,'-space.)
\def\foldl{\defun{foldl}}
\def\foldr{\defun{foldr}}
\def\map{\defun{map}}
\def\filter{\defun{filter}}
% etc. Silly, I know.
\def\f{\mathop f} % I don't want to remove the math italic kerning from 'f',
% because it has some additional space
$$\foldr \f z (x:xs) = \f x (\foldr \f z\, xs)$$
% Note: I didn't want to remove the spacing of ':', and also, I used
% a manual spacing ('\,') to separate parameters.
\bye


The output looks like this:

It's a little tedious, but I think it looks good.

Please note that unlike here, the convention is to have a space before the opening parens. I am hereby humbly challenging that convention. :)

• Since egreg's answer is now a superset of mine, I've deleted mine. That might mean that you don't see my reply to your comment (I don't know). Just in case not: I agree that \foo{}bar could quickly get very tedious! Maybe in this particular case it wouldn't be too annoying as the questioner doesn't seem to use any macros in this setup. Sep 14 '11 at 12:11
• Having no space before the opening parenthesis is unfortunately wrong typesetting in this domain, the OP has such spaces (more specifically: ICFP papers typeset with lhs2TeX will put a space before parenthesis). lhs2TeX also uses \; rather than \,, but the latter can work as well. Jul 19 '17 at 2:40
• @Blaisorblade wait, which authority has decided on that and where can I read about it? Jul 19 '17 at 4:49
• @Blaisorblade some more thoughts, remarks and questions: 1) are the things inside parentheses a) function application, b) function arguments, or c) tuples? A concise explanation on the use of parentheses in general on our sister site. And finally, to my understanding, Haskell's syntax was made to mimic mathematical notation, so depending on the answer on the question above, it would be more bracing to that idea to not have the space. It would interesting to know how much of the convention was born from the work of Ralf Hinze and Andres Loeh. Jul 19 '17 at 9:10
• @Blaisorblade Thank you for taking the time to answer my queries. If it's just a stylistic thing, i.e. lexically unambiguous, I'd like to leave my answer as-is just to play the devils advocate, but I'll leave a note about the convention. Jul 19 '17 at 19:39