Tilia: the little tree that grew
Published on December 2, 2026
A couple of months ago I announced a new formatter for Haskell—Tilia. The announcement post discussed the high-level design decisions I made, and while some readers appreciated it, many more remained puzzled as to what this new formatter brings to the table—I think this was the top Reddit comment, for example. Shortly after the announcement someone opened an issue on Tilia’s issue tracker asking for a comparison with other formatters. I also received a few questions by email asking about Tilia and the future of formatting Haskell in general. All this convinced me that a follow-up post was in order, this time a much more practical one, ideally with examples. I held off on writing it, though, because I was not yet completely happy with Tilia and wanted to do more work before writing about it again. That work has largely been done now and I have a few exciting updates to share.
Case study: Clash compiler
A few days after the announcement I was contacted by the maintainers of the Clash compiler, who decided to format their code base with Tilia and contributed some fixes. I am thankful to these brave early adopters! It is worth taking a brief look at this story, because it begins to answer the question of why Tilia takes formatting Haskell a step forward, and depending on your situation, it may be a big step. After all, Clash had a Fourmolu PR open since 2024, but it could not land because of CPP.
With a formatter like Fourmolu, the way to tame CPP is to carefully plant
the magic comments {- FOURMOLU_DISABLE -} and {- FOURMOLU_ENABLE -}
around your uses of CPP wherever verbatim preservation of conditionals does
not work or directives fall at particularly awkward positions. You get
something like this:
controlSimulator :: Control -> IO ()
{- FOURMOLU_DISABLE -}
#if defined(VERILOG_2001)
foreign import ccall "vpi_user.h vpi_control"
c_vpi_control_end :: CInt -> CInt -> IO Bool
foreign import ccall "vpi_user.h vpi_control"
c_vpi_control_restart :: CInt -> CInt -> CInt -> CInt -> IO Bool
controlSimulator control = do
(c, s, r, d) <- send control
success <- case control of
Reset{} -> c_vpi_control_restart c s r d
_ -> c_vpi_control_end c d
Monad.unless success $
throwIO $ CouldNotControl control callStack
#else
controlSimulator control =
throwIO $ CouldNotControl control callStack
#endif
{- FOURMOLU_ENABLE -}
There are three problems with this:
- This adds visual noise—the exact thing a formatter should be helping with, not making worse.
- The code inside such a disabled region is not formatted, so the formatting of the code base is no longer consistent.
- If you have a lot of CPP, as Clash does, surrounding half of your code with magic comments is just not worth it, so your project is simply not formattable.
As I explained in the announcement post, CPP is a first-class formattable construct for Tilia, so no magic comments are needed (the following snippet is the formatted output):
controlSimulator :: Control -> IO ()
#if defined(VERILOG_2001)
foreign import ccall "vpi_user.h vpi_control"
c_vpi_control_end :: CInt -> CInt -> IO Bool
foreign import ccall "vpi_user.h vpi_control"
c_vpi_control_restart :: CInt -> CInt -> CInt -> CInt -> IO Bool
controlSimulator control = do
(c, s, r, d) <- send control
success <- case control of
Reset{} -> c_vpi_control_restart c s r d
_ -> c_vpi_control_end c d
Monad.unless success
$ throwIO
$ CouldNotControl control callStack
#else
controlSimulator control =
throwIO $ CouldNotControl control callStack
#endif
Finally, the Clash maintainers also requested a stylistic change: when record fields are spelled out after a data constructor, they asked me to remove the space in front of the curly brace:
f T {fieldOne, fieldTwo} n = fieldOne + fieldTwo + n
^
This is already Fourmolu’s default, they said, and I accepted.
The status of CPP support
CPP handling was my primary focus after the initial release. Let’s take a look at the high-level results of a pre-release Hackage scan with a 0.2.0.0 release candidate. The setup is:
- 5,000 Hackage packages for which a build plan could be found
- 79,052 modules in total
- 628 MB of Haskell source code
Since comparisons were requested, the following table compares Tilia 0.2.0.0 and Fourmolu 0.21.0.0 on a few crucial metrics:
| Metric | Tilia | Fourmolu |
|---|---|---|
| Modules formatted | 99.02% | 95.57% |
| Modules declined | 0.91% | — |
| Modules that resulted in an error | 0.06% | 2.78% |
| Modules broken (AST check failed) | 0.00% | 1.31% |
| Modules non-idempotent | 0.01% | 0.35% |
| Modules with comment defects | 0.02% | 0.41% |
I really wanted to make Tilia reliable and hit these numbers early on, but it took time and a series of redesigns to get there. Ad-hoc fixes could not guarantee convergence at this scale, so I had to return several times to the design of Tilia’s intermediate representations and adjust it so that I could eliminate entire classes of problems elegantly. The end result is well worth it—not only does Tilia handle CPP, it is now also the most reliable formatter in general.

Tilia absolutely refuses to guess fixities or to format input it knows it
cannot handle well, and that is what the 0.91% of declined modules in the
table above are. The lens command in the screenshot gives a taste of what
this looks like in practice: the decline there is a use of a CPP
function-like macro (which is even worse than CPP conditionals):
lessSimplePoly :: forall KVS(k1 k2) (a :: k1) (b :: k2) .
Equality a b a b
lessSimplePoly f = f
QuickCheck and yesod also use macros like this, but not in the middle of
a type signature, where the code no longer parses as Haskell. With more
ad-hoc code, lens‘s use case can be made to work too. This is a question
of where to draw the line.
Tilia’s fixity inference in practice
One question I received by email is why knowing operator fixities is so important. Let me answer with an example. Servant is formatted with Fourmolu and is notorious for its many operators, most interestingly at the type level, so let’s take an example from it:
type TestApi =
"test"
:> Header "header" [String]
:> QueryFlag "flag"
:> Get '[JSON] Int
:<|> "test"
:> QueryParam "param" Int
:> ReqBody '[JSON] [String]
:> Post '[JSON] NoContent
:<|> "test"
:> QueryParams "params" Int
:> ReqBody '[JSON] String
:> Put '[JSON] NoContent
:<|> "test"
:> Capture "id" Int
:> Delete '[JSON] NoContent
:<|> "test"
:> CaptureAll "ids" Int
:> Get '[JSON] [Int]
:<|> "test"
:> EmptyAPI
Here, the result looks wrong. In an operator chain that spans multiple
lines, fixities determine how the operators nest, and the layout should show
it. :> binds tighter than :<|> (infixr 4 vs infixr 3), so each
:<|> separates whole endpoints, yet all the operators come out at one
level, as if they had the same precedence.
How does Fourmolu know fixities? During development, a database of fixities
is assembled by scraping Hoogle, and it is then shipped with Fourmolu. The
database does know :> and :<|>: had this module imported them from
Servant.API, Fourmolu would have nested the chain correctly. But the
module gets them from Servant.Foreign, a module of the package being
formatted, which re-exports Servant.API. Fourmolu resolves a fixity only
through the modules that are hardcoded in its database, and it is not aware
of this re-export, so it falls back to the default fixity, infixl 9. The
fix is to declare the re-export manually, in the reexports field of
fourmolu.yaml.
Operator chains are very common in Haskell, and plenty of different operators come from both third-party and first-party modules, so this is not a corner case. The database approach has several problems:
- The database goes out of date.
- The database is incomplete because it only scans the most popular packages, and as the number of packages on Hackage grows, the lacunae will become more and more obvious.
- Haskell’s import system is more complex than a lookup in a map.
- There is no support for inferring the fixities of first-party operators.
- Once Hoogle changes the format of the pages it serves or removes some information about re-exports, you are in trouble. Hoogle did in fact once drop some of the information on re-exports it used to provide—I discovered this back in August, before I started Tilia. I fixed the problem then, in a different formatter.
These problems motivated Tilia’s design, which infers operator fixities directly from source files and, where available, interface files. It is much harder, but it is the correct thing to do. Was that complexity worth it? I think so. Tilia correctly rewrites the example snippet from above as:
type TestApi =
"test"
:> Header "header" [String]
:> QueryFlag "flag"
:> Get '[JSON] Int
:<|> "test"
:> QueryParam "param" Int
:> ReqBody '[JSON] [String]
:> Post '[JSON] NoContent
:<|> "test"
:> QueryParams "params" Int
:> ReqBody '[JSON] String
:> Put '[JSON] NoContent
:<|> "test"
:> Capture "id" Int
:> Delete '[JSON] NoContent
:<|> "test"
:> CaptureAll "ids" Int
:> Get '[JSON] [Int]
:<|> "test"
:> EmptyAPI
The same goes for hundreds of other cases I found during my comparative Hackage scans: Tilia got the fixities right every time.
An added benefit is that Tilia knows your first-party operators
automatically, so there are no fixities or reexports fields to maintain
manually. With Fourmolu, you would add them to fourmolu.yaml as needed.
We can ask Tilia for information about operators with --debug-fixity:

Ode to Fourmolu
So, CPP, comments, and fixities are solved. What’s next? With all the hard problems of formatting out of the way, I decided to work on aligning Tilia better with how Haskellers write their code. After all, what good is it to have all these principled solutions if you cannot have leading commas? ;-)
It is time for Tilia to become just a little bit less opinionated. I decided to do it right and fair, which means doing some statistical analysis of Haskell code found in the wild to discover the natural formatting preferences of the community. Thankfully, that is not even necessary, because we have Fourmolu.
Let’s take a moment to appreciate that project. Fourmolu evolved over the
years in an organic and democratic fashion. Every single configuration
option in Fourmolu (and it has 27!) is there because someone really wanted
it badly enough to take the trouble of implementing it. And then, others
enabled those features in their fourmolu.yaml files because they thought
that it was a good idea.
Before I tell you which configuration options Tilia now has, I want to explain why I feel pressure to have as few of them as possible, each with as few values as possible. This has to do with testing. You see, I am paranoid about the correctness of my formatters. Tilia’s test suite alone, which runs on every commit, currently has 28k test cases of many different kinds. That is not enough, though, so I have a collection of techniques that go far beyond the test suite, such as those Hackage scans, which I run fairly regularly to make sure I have not missed anything. A single Boolean switch means I need to test twice as much, so I’d rather have as few of them as possible.
The key idea that made it possible to have configuration options in Tilia without a combinatorial explosion is what I call the orthogonality of options. Option A is orthogonal to option B iff enabling them together introduces no new bugs and does not remove or hide any bugs present when A or B is enabled on its own.
Now suppose I have some options, each of them with only two possible values. I can then test with the default config and the alternative config, which is obtained by flipping each option in the default config. Provided all options are orthogonal, I can test everything by running the renderer only twice. That’s affordable. But how do I know that the options are orthogonal? Well, I can reason about it, but that’s not good enough. At the end of the day it is an empirical question. It has to be. So, if the default and the alternative configurations are both green on master, and a nightly job that tests every possible combination finds them all green as well, then, as far as my test suite is concerned, my options are orthogonal. This is what I settled on.
Leading commas
Based on a sample of 1,455 fourmolu.yaml configurations found in the wild,
by far the most popular option (93% of users) is comma-style: leading.
Indeed, as of 2026, based on my scans of actual source code from Hackage,
most Haskell code is still written with leading commas. This style is now
supported in Tilia, though its default remains trailing commas. You can
enable it by creating a tilia.toml file with this:
leading-commas = true
This amounts to Fourmolu’s comma-style: leading as well as
import-export-style: leading, which felt stylistically inseparable to me.
Leading arrows
Leading arrows, enabled in Fourmolu with function-arrows: leading, are
selected by 40% of users, while 57% prefer trailing ones. The remaining 3%
choose leading-args, which I did not even know existed. Trailing arrows
are Tilia’s default, while leading ones can be enabled like this:
leading-arrows = true
Indentation step
Despite the formatter’s name, and even though 4-space indentation is Fourmolu’s default, it is not the most popular choice among its users. Only 28% of users choose 4 spaces, while 72% opt for 2. I will make no comment on which is better, only point out that the indentation step can now be configured:
indentation = 4 # 2 is the default
Only 2 and 4 are accepted: it turned out that every indentation step brings its own spectacular bugs, so 3-space indentation is not supported.
While making 4-space indentation work in Tilia, I used Fourmolu as my guiding light, since I have no strong opinion on what Haskell code should look like with this indentation step and there are some unique challenges with it. I figured that whatever Fourmolu does is probably the right thing to do for Tilia as well. That worked nicely, but when I applied my usual testing process, I also discovered a few cases where Fourmolu breaks code at that indentation step, for example:
escape = do
char '\\'
do gap; return Nothing
<|> do code <- escapeCode; return (Just code)
this formats as
escape = do
char '\\'
do gap; return Nothing
<|> do code <- escapeCode; return (Just code)
That is a different program. Fourmolu’s AST check catches this and refuses
to write the result unless you pass --unsafe.
In those cases I had to use my own judgement about the right output for Tilia. If there is interest in fixing these in Fourmolu, please reach out and I will share my collection of failing examples.
How many configuration options is enough?
So far there are 3 options, each with 2 possible values, which makes 8 combinations in total. I think this is a good middle ground. In the next sections I describe other changes, which are clear improvements and therefore unconditional. As long as all the options stay orthogonal, more options could perhaps be added, but I do not currently see strong candidates—at least none as strong as leading commas, leading arrows, and the indentation step.
More intelligent preservation of declaration grouping
Even before looking at Fourmolu, I noticed that authors often group single-line top-level declarations without Haddocks together:
infixr 5 <|, `cons`
infixl 5 |>, `snoc`
infixr 4 <|~, |>~, <<|~, <|>~, <<<|~, <<|>~
infix 4 <|=, |>=, <<|=, <|>=, <<<|=, <<|>=
This is a consistent pattern, and it is worth respecting. Sure enough,
Fourmolu already does that. With respectful: true, its default, Fourmolu
keeps together any declarations written without an empty line between them
unless one has a Haddock, so a function spanning several lines stays glued
to a data declaration under it. With respectful: false it puts an empty
line between every two. Tilia takes a middle course: one-line declarations
of one kind written together stay together, while a declaration spanning
several lines or having a Haddock, and declarations of different kinds, get
an empty line between them. I found that with this rule, every blank line
Tilia inserts looks natural and justified.
Automatic breaking of excessively long lines
I have grown tired of “nudging” my formatters to break long lines. Not only
is it annoying to do by hand, but you can also never be quite sure that your
preferred column limit is applied consistently everywhere. Really, this is
something a formatter should be able to do. Again, I looked at Fourmolu and
found that the feature has existed there since version 0.12.0.0. The setting
is called column-limit but its documentation page
says:
WARNING: Setting this option may break idempotence.
So I said to myself, “challenge accepted, I will add breaking of long lines in Tilia without compromising idempotence and with the added challenge of making it work with CPP”. First things first, I looked at how this feature is implemented in Fourmolu.
Fourmolu lays out every construct either on one line or across several, and
takes the decision from the input: a multi-line construct is printed across
several lines, and one written on a single line stays on a single line.
column-limit adds one more condition: a construct written on a single line
is also printed across several lines when its span in the input is wider
than the limit.
The difficulty with idempotence is that every construct the limit breaks on
the first run is then written across several lines, while many decisions of
the renderer depend on how things were written in the original input. Take
this line, with column-limit: 80:
pipeline = filter isRelevant . map normalize . concatMap splitIntoWords . lines . Text.toLower $ inputText
Fourmolu 0.21.0.0 formats it as:
pipeline =
filter isRelevant
. map normalize
. concatMap splitIntoWords
. lines
. Text.toLower $
inputText
Run it again and $ moves:
pipeline =
filter isRelevant
. map normalize
. concatMap splitIntoWords
. lines
. Text.toLower
$ inputText
Fourmolu puts $ at the end of a line only when the expression to its left
is written on one line. In the original input it is, so the first run puts
$ at the end, and only then does the limit break the expression to its
left. In the second run that expression is written across five lines, so $
goes to the start of the line. Fourmolu’s issue tracker has more examples of
the kind, such as #400 and #409.
Tilia’s engine works differently in a way that matters here. A renderer does not print anything; it builds a document in which line breaks are not decided yet. Parts of the document are grouped, and a group is laid out as a whole: flat, with every break in it printed as a space, or broken, with every break printed as a newline. The engine decides which when it prints the document.
To this I added a third layout, frangible: flat where it fits within the column limit, broken where it does not. Three properties of the engine make breaking long lines idempotent:
- The decision is taken while printing, so it is made on the output: from the column the group actually starts at, with its contents as they are going to be printed. A group breaks only when it runs past the limit flat and its first line ends sooner broken, so a group whose contents cannot be shortened is left alone.
- A group the engine breaks comes out exactly as if it had been written across several lines, so the next run reproduces it.
- Decisions are taken from the outside in. A group laid out flat keeps every frangible group inside it flat, so the layout of an inner group never depends on something decided after it.
The feature is tested in several ways. First, it is always enabled, so it is exercised extensively during all normal Tilia testing and Hackage scans. Second, a property test runs every vendored example in the test suite with 5 different random column limits. This caught some early bugs, but now it is consistently green.
The default column limit of 80 can be overridden in the configuration file:
column-limit = 100
Separate first- and third-party sections in imports
How do Haskell programmers group their imports? I looked at the newest release of 2,134 packages on Hackage and took the 24,507 modules that import both modules of their own package and modules of other packages:
| How the imports are grouped | Modules |
|---|---|
| One block, no empty lines | 53% |
| Groups that mix own and other modules | 19% |
| Other packages, then own modules, in two groups | 12% |
| Other packages, then own modules, in more groups | 7% |
| Own modules first | 6% |
| Own and other modules in alternating groups | 4% |
So about half of the code does not group imports at all, and the other half groups them in many different ways. Still, one line stands out: of the modules that do split their imports, 60% separate their own modules from those of other packages, two thirds of them put their own modules last, and two groups is the most common way to do it.
That is what Tilia does now. Whatever groups the imports are written in, it
prints them in two: imports from other packages first, then the project’s
own modules, each group sorted, with an empty line between them. For those
who write one block, this adds a single empty line, and for those who split
their imports further, it keeps the distinction that matters most. This
seems benign enough, so this behavior is always on. It is analogous to
Fourmolu’s import-grouping: by-scope, except Tilia takes all packages in
your cabal.project to be first-party, not only the current package that
is being formatted.
HLS plugin
Last but not least, Tilia now has an HLS plugin. The most interesting feature of this plugin is that it sources fixity info directly from the GHC session HLS already has. In this mode of operation it is just as precise but significantly faster, because it does not need to work out fixities on its own. This is the first HLS formatting plugin to take advantage of this kind of information. I am thankful to @fendor for nudging me in this direction, since at first I did not recognize this opportunity myself.
Here is how long a formatting request takes in the same build of HLS:
| Module | Lines | Tilia | Fourmolu |
|---|---|---|---|
Control.Monad.Accum (mtl) | 328 | 14 ms | 41 ms |
Text.Megaparsec.Char.Lexer (megaparsec) | 552 | 26 ms | 66 ms |
Control.Monad.Catch (exceptions) | 925 | 130 ms | 190 ms |
Control.Lens.Lens (lens) | 1,523 | 83 ms | 460 ms |
Data.Vector.Generic (vector) | 2,740 | 350 ms | 1,000 ms |
Conclusion
Tilia’s new engine overcomes many difficulties that historically troubled Haskell formatters. It is now robust and extensively tested. Comments, fixities, and CPP are all solved. With these problems out of the way, the direction I am taking now is towards eliminating friction between what Tilia outputs and what people tend to write. This requires a balance: adding a single Boolean configuration switch doubles the space Tilia needs to test. Leading commas, leading arrows, and the indentation step seem worth it; time will tell what else is.
Let’s take a moment to see many of the features mentioned in this post working together:
{- FOURMOLU_DISABLE -}
type API auths
= Auth auths User :>
( Get '[JSON] Int
:<|> ReqBody '[JSON] Int :> Post '[JSON] Int
:<|> "named" :> NamedRoutes DummyRoutes
:<|> "header" :> Get '[JSON] (Headers '[Header "Blah" Int] Int)
#if MIN_VERSION_servant_server(0,15,0)
:<|> "stream" :> StreamGet NoFraming OctetStream (SourceIO BS.ByteString)
#endif
:<|> "raw" :> Raw
)
:<|> "uverb" :> Auth auths User :> UVerb 'GET '[JSON] '[WithStatus 200 Int, WithStatus 401 Text, WithStatus 403 Text]
:<|> "login" :> ReqBody '[JSON] User :> Post '[JSON] (Headers '[ Header "Set-Cookie" SetCookie
, Header "Set-Cookie" SetCookie ] NoContent)
:<|> "logout" :> Get '[JSON] (Headers '[ Header "Set-Cookie" SetCookie
, Header "Set-Cookie" SetCookie ] NoContent)
{- FOURMOLU_ENABLE -}
with leading-commas = true and the column limit set to its default, 80,
this formats as:
type API auths =
Auth auths User
:> ( Get '[JSON] Int
:<|> ReqBody '[JSON] Int :> Post '[JSON] Int
:<|> "named" :> NamedRoutes DummyRoutes
:<|> "header" :> Get '[JSON] (Headers '[Header "Blah" Int] Int)
#if MIN_VERSION_servant_server(0,15,0)
:<|> "stream"
:> StreamGet NoFraming OctetStream (SourceIO BS.ByteString)
#endif
:<|> "raw" :> Raw
)
:<|> "uverb"
:> Auth auths User
:> UVerb
'GET
'[JSON]
'[WithStatus 200 Int, WithStatus 401 Text, WithStatus 403 Text]
:<|> "login"
:> ReqBody '[JSON] User
:> Post
'[JSON]
( Headers
'[ Header "Set-Cookie" SetCookie
, Header "Set-Cookie" SetCookie
]
NoContent
)
:<|> "logout"
:> Get
'[JSON]
( Headers
'[ Header "Set-Cookie" SetCookie
, Header "Set-Cookie" SetCookie
]
NoContent
)
It is this kind of result that keeps me passionate about working on Haskell formatters. It took a little more work than expected to get here, but I am very happy with how far this young project has come.
Tilia will continue to actively improve and advance. That said, I believe
its output has stabilized now and I do not expect significant changes in the
way it lays out the code. Changes will be in other, less obvious areas.
Finally, in anticipation of GHC 10.0, I want to say that a new version of
Tilia with support for GHC 10.0 syntactic features will be available as soon
as ghc-lib-parser allows.