List Machine
In this section we will discuss the ListMachine
. It is used to rationalize the space between LatexExpressions
when they are rendered. A ListMachine
reads a tape from left to right. The tape is aware of the contents of the current cell, and the cell to the immediate left and immediate right. Assume that cell contents have type a
. Then the machine has three registers, before
, current
, and after
, all of which have type Maybe a
. The use of Maybe
accounts for the fact that a register may not be defined when the tape is near its beginning or its end. The main definition is the following
Given a function State a -> b
, one transforms a list of a
's to a list of b
's. A value of type State a
holds the registers before
, current
, and after
, as well as an input tape of type List a
:
Example
Make the following file:
Then do this:
The internals of ListMachine
To use the ListMachine module, it is enough to export the type State a
, the function ListMachine.run
, and to define an output function f : State a -> a
. The code for all this on GitHub. Here we describe brielfly the design of this module. There is a fair amount of internal plumbing consisting of short type and function definitions. The idea is to hide all implementation details behind a very simple API. Here is the definition of run
:
The function makeReducer
transforms a function of type State a -> b
to a function of type
where type alias TotalState a b = {state: State a, outputList: List b}
. Next, we construct a function that returns a function of type TotalState a b -> List a -> TotalState a b
given a Reducer a b
:
With this function in hand, we can define run_
:
For the remaining details, please see the code.
addSpace
The Render2.addSpace
function is the output function for a ListMachine
with type State LatexExpression
. Thus it transforms a tape of LatexExpressions
into another tape of the same kind. The output function given below defines the rules for adding space before or after a LXString String
, or not adding any space at all:
Here are the two small auxilliary functions:
Last updated