Haskell for LATEX2e Manuel M. T. Chakravarty School of Computer Science and Engineering University of New South Wales, Australia [email protected] www.cse.unsw.edu.au/~chak Version 1.0e (for Version 1.0e of the style file)


What’s it good for?

Setting large pieces of code in verbatim is ugly and complicates the use of subscripts or to set comments in a proportional type face. On the other hand, the use of TEX’s math mode requires additional macros to achieve proper kerning in multi-letter identifiers and to typeset application by juxtaposition. The haskell style provides environments and macros that simplify setting Haskell [H+ 92, P+ 97] programs in LATEX [Lam94]. While the style is specifically geared towards Haskell, it should also be useful for other functional languages like ML or Nesl. The famous map function can be set as follows: map :: (α → β) → [α] → [β] map f [] = [] map f (x : xs) = f x : map f xs

-- type assertion -- [] is the empty list -- : is the infix list constr.

Under this definition, map (+1) [1, 2, 3] evaluates to [2, 3, 4]. The previous example was set using the following input: The famous \ function can be set as follows: % \begin{haskell*} map &::& (\alpha\to\beta)\to[\alpha]\to[\beta] &\hscom{type assertion}\\ map f [] &=& [] &\hscom{\<[]\> is the empty list}\\ map f (x:xs) &=& f x : map f xs &\hscom{\<:\> is the infix list constr.} \end{haskell*} % Under this definition, \ evaluates to \<[2, 3, 4]\>. There are a number of points worth noting. In a Haskell phrase, math mode commands, such as \alpha are available, but nevertheless, space characters can be used to denote juxtaposition of expressions (i.e., application in Haskell) and multi-letter identifiers are set properly. Compare f f oo and ffoo, set by $ffoo$ and \, respectively. The examples in the following sections do not provide the LATEX sources used to typeset the examples in the formatted version. Please look at the source file to see the code. 1

2 2.1

Basic Usage Haskell Mode

The environment haskell as well as the commands \< and \> enter Haskell mode. Keywords can be set in bold face using \hskwd. Within a haskell environment, columns are defined using the alignment character &. Usually, it is not necessary to wrap relations into alignment characters, but instead a single alignment to the right of the relational symbol suffices (just like AMSLATEX’s align environment). However, sometimes we want to align relational symbols of different size (i.e., the :: of a type assertion and the = of the corresponding equations). In this case, the star form haskell* should be used, which centers the material enclosed between the first and second alignment character.



The unary command \hscom is used to set single line comments. data Maybe α = Nothing | Just α

-- No result -- Just a value of type α

The second line shows that in a comment, you can again enter Haskell mode. This is particularly important when identifiers appear in comments or when you want to cite some program fragement, as the font choice in these cases should be identical to the fonts in the displayed program. Sometimes, we like to omit some details of an algorithm and instead give an informal description. This is achieved with \hsinf. main = do config ← hread the configuration filei result ← hprocess the input according to configi putStr result


Features from Math Mode

You can use all sorts of symbols, subscripts, and superscripts from LATEX’s math mode in Haskell mode. This includes TEX’s relational symbols, for example, ≥ or 6=, and greek characters for type variables.


Aligned Material

Larger function bodies usually require the use of aligned subphrases to visually structure the definition. The command \hsalign generates an alignment with an arbitrary number of columns. Consider the function transpose from Haskell’s standard library: transpose :: [[a]] → [[a]] transpose = foldr (λ xs xss → zipWith (:) xs (xss ++ repeat [])) [] For several constructs that require alignments, there are predefined macros internally using \hsalign. For example for let expressions, we have \hslet:


foo a = let x= 1 y= 2 in x +y Another macros is \hsif for if-then-else expression. In the following, example, we see that aligned subphrases can be nested: foo a = if a == 0 then let x = a + 1 in x else a Aligned material frequently gets big enough to require to break the surrounding alignment. This achieved with \hsbody: calculateNextPos :: Pos → Mover → State → [Pos] calculateNextPos oldPos move state = map(λ (lowerHalf , upperHalf ) → (upperHalf , lowerHalf ) (compare oldPos (move state)) A canned use of \hsbody is provided by \hswhere for where clauses: partition :: (a → Bool ) → [a] → ([a], [a]) partition p xs = foldr select ([], []) xs where select x (ts, fs) | p x = (x : ts, fs) | otherwise = (ts, x : fs)


Advanced Features

If you need to set complex documents, you may want to know some of the more subtle points of the Haskell mode. Generally, whenever you attempt to use Haskell mode in sophistcated ways or try to define your own commands, keep in mind that spaces matter in Haskell mode, i.e., Haskell mode uses TEX’s \obeyspaces. This changes TEX’s behaviour in subtle ways.


Defining Commands

Commands that are used in Haskell mode should be defined with \hscommand instead of \newcommand. The arguments expected by \hscommand are the same as those for \newcommand (however, there is no *-version of \hscommand). \hscommand works slightly different, for example, with respect to processing spaces in the body of the command definition, to allow application by juxtaposition.


Entering “Real” Math Mode

Haskell mode is rather similar to math mode, to allow using special characters, subscripts, and so forth. However, sometimes, it is desirable to enter plain math mode. Within a Haskell phrase, the commands \( and \) may be used to enter LATEX’s standard math mode. This is often helpful, when defining macros1 and, for example, the macro \hscom is defined this way. 1 Sometimes it may be necessary to prefix such a use of \( by \relax, when the macro is used behind a &.



Use in Transformation Rules

For example in transformation rules, we want complex expressions, such as let-in constructs to be vertically centered. This is achieved by specifying an optional argument giving the alignment, for example, \hslet[c]{· · ·}. let x= e =⇒ C [e] in C [x ] By default boxes are vertically aligned by the baseline of the first line (corresponding to the option t). Using c the alignment is in the center and with b it is at the baseline of the last line.


TEX’s Formula Parser

TEX is capable of understanding some of the structure of formulae in math mode. The parsed structure influences the space inserted between the elements of a formula. Compare 3+4 =⇒ 3 + 4 3{+}4 =⇒ 3+4


In the first case, TEX groks that + is a binary operator applied to 3 and 4, but not in the second. Details are provided in The TEXbook [Knu86].



The Haskell style is available under a BSD3 license.

References [H+ 92]

P. Hudak et al. Haskell special issue. ACM SIGPLAN Notices, 27(5), May 1992.

[Knu86] Donald E. Knuth. The TEXbook. Addison-Wesley, 1986. [Lam94] Leslie Lamport. LATEX: a Document Preparation System. Addison-Wesley, second edition, 1994. [P+ 97]

John Peterson et al. Haskell 1.4: a non-strict, purely functional language. Research report, Yale University, April 1997.


Haskell for LATEX2e - GitHub

School of Computer Science and Engineering. University of New South Wales, Australia [email protected]. .... Research report, Yale University, April 1997. 4.

176KB Sizes 0 Downloads 392 Views

Recommend Documents

haskell-Tensor haskell-OpenGL haskell-GLUT xmobar ... - GitHub
haskell-blaze-html haskell-lcs haskell-ansi-terminal haskell-ansi-wl-pprint hscolour haskell-haskell-src haskell-hostname haskell-StateVar haskell-temporary.

Categories and Haskell - GitHub
This is often summarized as a side-effect free function. More generally ... The composition g ◦ f is only defined on arrows f and g if the domain of g is equal to the codomain of f. ...... http://files.meetup.com/3866232/foldListProduct.pdf ... Pag

A Haskell Interpreter for Object Calculus - GitHub
Church numerals and encoding are used to represent data and operators in Lambda ... ers have taken to model object-oriented languages, ei- ther adapting or ...

FLTKHS - Easy Native GUIs in Haskell, Today! - GitHub
Feb 5, 2016 - Call. valuatorRef->bounds(1.0, 10.0). In FLTKHS bounds valuatorRef 1.0 10.0. Aditya Siram (@deech). FLTKHS - Easy Native GUIs in Haskell, ...

Applying Type-Level and Generic Programming in Haskell - GitHub
Feb 10, 2018 - (Haskell allows to say deriving Eq on a datatype declaration, invoking compiler magic that conjures up a .... Since type-level programming is a bit peculiar in Haskell, we'll move step by step: from normal lists over .... Before we mov

Page 2 of 15. PROGRAMACIÓ TRIMESTRAL Escola del Mar, curs 2017-18. 5è. 2. SEGON TRIMESTRE. Numeració i càlcul. - Nombres decimals: part sencera i part decimal. - Dècimes, centèsimes i mil·lèsimes. - Descomposició, comparació i ordenació de

Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7. 2.1.1. Simple Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7. 2.1.2. Boolean Algebra . .... haskell-b

Handling Exceptions in Haskell
Jan 19, 1999 - ... less idealistic programmers can write in C, Java, Ada and other useful .... since the Prelude is not just ordinary Haskell code, requires a lot of ...

Handling Exceptions in Haskell
Jan 19, 1999 - Handling Exceptions in Haskell. Alastair Reid. Yale University. Department of Computer Science. New Haven, CT 06520 [email protected].

Haskell as an Introduction to Parallel Computing for ...
May 31, 2013 - parallel programming in today's world of software engineering. ... effective environment in which to teach undergraduate students parallel com- ... runtime systems, like parallel garbage collection[3]. ... Quicksort algorithm in other

Examples of type expressions include the atomic types Integer (in. nite-precision. integers), Char (characters), Integer->Integer (functions mapping Integer to ...

Lodash for President - GitHub
Page 1. LODASH FOR PRESIDENT. Christian Ulbrich, CDO Zalari UG. Page 2. AGENDA PROPAGANDA. • Recap: LoDash. • Why? • Installation.

Uses for Modules - GitHub
In this article, I will walk you through several practical examples of what modules can be used for, ranging from ... that is not the case. Ruby would apply the definitions of Document one after the other, with whatever file was required last taking

For Developing Countries - GitHub
Adhere to perceived affordance of a mobile phone to avoid alienating the user. Target users are unfamiliar to banking terminology. Balance Check. Shows current balance in m-banking account. Top Up. Scratch card or merchant credit transfer. Credit Tra

Clojure for Beginners - GitHub
Preview. Language. Overview. Clojure Basics & .... (clojure.java.io/reader file))]. (doseq [line .... Incremental development via REPL ⇒ less unexpected surprises ...

hacking for sustainability - GitHub
web, is the collection of interconnected hypertext3 documents. 3 Hypertext is a .... the online photo service Flickr hosts now more than 200 ... It is one of the top ten most visited websites ..... best possible delivery route between different store

Directions For Use - GitHub
Page 7 of 46. 4. Using EMPOP to perform mtDNA haplotype frequency estimates. EMPOP follows the revised and extended guidelines for mitochondrial DNA typing issued by the DNA commission of the ISFG (Parson et al. 2014). See document for further detail

Manual for tsRFinder - GitHub
Feb 11, 2015 - Book (running OS X) or laptop (running Linux), typically within 10 min- .... ure (”label/distribution.pdf”) showing the length distributions and base.

The Haskell School of Music - The Yale Haskell Group - Yale University
No part of this publication may be reproduced or distributed in any form ..... You do not need to know much, if any, music theory to read this book, and you do not ...