A parser value is not a parse in progress
Consider the type of expr:
expr : Parser Expr
expr is a value of type Parser Expr. It describes how to parse source text and produce an Expr.
Defining expr doesn’t itself parse a program. It constructs a parser value that can later be run on some source text.
We can see the difference by comparing expr with P.run:
<code class="tut-inline-code">P.run : Parser a -> String -> Result (List P.DeadEnd) a</code>
P.run receives a parser and the source text to parse:
P.run expr "-(2, 1)"
Here, expr describes how to recognize an expression, while "-(2, 1)" is the source text being parsed.
Combinators such as P.oneOf, P.succeed, |., and |= let us define larger parser values in terms of smaller ones. In our case, expr is defined in terms of constExpr and diffExpr.
No input is being consumed while these parser values are being constructed. Elm finds the problem before we ever try to run the parser on some source text.
The cycle is not caused by recursively parsing a nested DIFF program.
It’s a cycle among the parser values themselves.
A Parser value is a recipe, not a running parse. Referring to it inside its own definition means the recipe never finishes being written.
Following the dependency cycle
Let’s follow the definitions.
expr includes diffExpr as one of its alternatives, so expr depends on diffExpr.
But diffExpr uses expr to parse both operands, so diffExpr also depends on expr.
That gives us the dependency cycle:
expr → diffExpr → expr
Neither parser value can be constructed without the other one already being available.
The recursive grammar itself is not the problem. A difference expression really does contain two expressions, and those expressions may be difference expressions too.
The problem is how we translated that recursion into Elm:
The parser values depend on each other immediately.
We need to keep the recursive relationship while preventing Elm from following it as soon as the parser values are constructed.
expr uses diffExpr and diffExpr uses expr, so each value depends on the other. Elm reports CYCLIC DEFINITION because neither can be evaluated first.
What does the CYCLIC DEFINITION error report?
Put the recursive reference behind a function
Compare these two expressions:
expr
<code class="tut-inline-code">\_ -> expr</code>The first uses the expr parser directly, so Elm needs that parser immediately.
The second defines a function that returns the expr parser.
Elm is a strict, call-by-value language, but a function is already a value. Constructing the function doesn’t evaluate its body, so Elm doesn’t need to obtain the expr parser yet. The parser is only obtained when the function is called.
In programming-language terminology, this function is a thunk: a function used to delay a computation.
The thunk lets us keep the recursive reference without requiring Elm to obtain expr while the parser values are being constructed.
Putting expr behind a function also changes its type.
expr has this type:
Parser Expr
The thunk has this type:
<code class="tut-inline-code">() -> Parser Expr</code>
It’s a function that accepts () and returns the expr parser.
We cannot pass that function directly to |=. The right-hand side of |= must be a parser. Here, because we’re parsing an operand expression, it must be a Parser Expr, but our thunk has type () -> Parser Expr.
We need something that accepts the thunk and gives us a parser value again.
That’s what P.lazy does:
<code class="tut-inline-code">P.lazy : (() -> Parser a) -> Parser a</code>
Its argument matches the type of our thunk, so:
<code class="tut-inline-code">P.lazy (\_ -> expr)</code>
gives us the Parser Expr required by |=.
Now let’s see when P.lazy calls the thunk.
Putting the recursive reference behind a lambda breaks the cycle: the lambda is a finished value that only looks up expr when called.
When does P.lazy call the thunk?
Consider this program:
-(2, -(4, 3))
When we run expr on this program, it selects diffExpr, which first recognizes the beginning of the outer difference expression:
-(
When parsing reaches the first operand, P.lazy calls the thunk:
<code class="tut-inline-code">\_ -> expr</code>and obtains the expr parser needed at that position.
The first operand is:
2
so expr selects constExpr.
Parsing then continues to the second operand:
-(4, 3)
Again, P.lazy calls the thunk and obtains expr. This time, expr selects diffExpr, whose operands eventually require expr again.
The recursion ends when expr selects constExpr for 2, 4, and 3. Because constExpr does not depend on expr, it provides the base case.
The important distinction is that Elm no longer needs to obtain expr while the parser values are being constructed. It obtains expr later, when parsing reaches a position where another expression is required, turning what was an immediate dependency between parser values into a recursive dependency followed while parsing the input.
P.lazy runs the thunk every time parsing reaches that point, so recursion depth follows the input, not the parser's size.
When does P.lazy evaluate \_ -> expr?
When should you use P.lazy in a recursive parser?
P.lazy is useful when a recursive parser reference would otherwise create an immediate dependency cycle among parser values.
In DIFF, putting the recursive uses of expr behind functions breaks the immediate dependency cycle.
Not every recursive parser needs P.lazy, though. If the recursive use of the parser is already behind a function, the immediate dependency has already been broken.
For example, P.andThen takes a function:
<code class="tut-inline-code">P.andThen : (a -> Parser b) -> Parser a -> Parser b</code>
If the recursive parser is referred to inside that function, Elm doesn’t need to obtain it until the function is called. In that situation, P.andThen already places the recursive reference behind a function, so P.lazy isn’t needed to break the immediate dependency cycle.
What matters, then, is not simply whether a parser is recursive. The important question is whether constructing the parser creates an immediate dependency cycle.
If it does, put the recursive use behind a function so Elm doesn’t need that parser while constructing the surrounding parser value. Use P.lazy when the surrounding parser combinator requires a Parser a, but the recursive parser is behind a () -> Parser a thunk.
You don’t have to spot every cycle before compiling the code. If you forget P.lazy where it is needed, the Elm compiler will detect the cyclic definition and show you the chain of parser values involved.
Once you understand why recursive parser values can create this cycle, that error becomes much more useful. Instead of seeing a mysterious infinite-loop warning, you can look for the recursive parser use that is creating the immediate dependency.
Use P.lazy whenever a parser refers to itself, directly or through a group of mutually recursive parsers.
The key idea
Recursive parsers are not the problem. The problem is an immediate dependency cycle among parser values; putting the recursive reference behind a function breaks that cycle, and P.lazy lets us use that thunk where a parser value is required.
In Elm, recursion inside a value definition must hide behind a function. That is all P.lazy does.
Which parser needs P.lazy?
Keep building the language
The language keeps growing one feature at a time. Next up: Lexeme Parsers in Elm.
Part of the Tiny Interpreters tutorial series, adapted from blog.tinyinterpreters.dev.