It's been about a month since my previous post about
lyre, and the language has changed very
substantially (to the point that the current main branch is no longer
compatible with the version I used to write First, Second, and Third
Persons.
Probably the most striking change is that I "caved" and made the language an
island grammar; that is, the source file is interpreted as player-facing text
by default and "code" is demarcated within it, as is the case in ChoiceScript
and Ink.
=storylet paris_1886
# Paris, 1886
You still recall that heady summer of '86, which stretched on for far too
long…
~ recalling_parisian_memories++
But the interesting thing about this is that this is not the product of just a
syntactical change where I realized it's more ergonomic to write text without
quotes, it's the product of a deep semantic change that has almost entirely
replaced the guts of the language.
Originally, lyre conceptualized storylets and branches as having a "body" and a
"title" of text, and each was a single expression that would evaluate to the
text that's presented to the player.
Like any narrative scripting language, lyre needs some way to evaluate
arbitrary expressions; predicates like language proficiency > 10 or
times_spoken_to_vicar < 7, but also calculations like time_elapsed = time_left - time entered. Yarn uses a stack-based virtual machine; for lyre, I
wrote a very basic interpreter that's conceptually similar to a Lisp interpreter,
which has the advantage of being simple and easier to inspect while the
language is still actively changing, and required less binding architectural
decisions early on.
In this early form, the expression evaluator sat in its own corner and
evaluated small short expressions, but most of the core logic was left to the
narrative engine—which is just straightforward code that runs a storylet
system, figuring out which branches to display and how game state changes. The
expression engine was relegated to calculating values that would plug into the
decisions the narrative engine made.
However: the expression engine has, as the language evolved, gradually eaten
the narrative engine, to the point that what remains of the latter is now
really kind of a very simple orchestrating layer whose job is just delegating
the right things to the right expression engine components.
Which is to say, storylets used to be made of distinct kinds of statements,
some of which could contain expressions, but which were distinct among
themselves. The text body of a storylet was one specified property of that
storylet, a single string value (or a string with variable text, which is in
fact just an expression).
Now, most of the different kinds of statements that make up storylets (text and
effects) have been consolidated into the expression language. Storylets don't
have "a text body" as a distinct component, and execution is more similar to
something like Yarn; each line of text is its own statement. This allows
interleaving text and effects in ways that work how you expect:
=storylet recruitment
~ new_sailors = d(6)
Your crew of { sailors } sailors expands with the addition of
{ new_sailors } new able seamen,
~ sailors = sailors + new_sailors
for a total of { sailors }.
Lyre was originally conceptualized more as a data format than a programming
language, closer to YAML than anything. All the storylets in Voyageur were in
fact just written as YAML files.
But, as it evolves to develop new and more generalizable functionality, it
starts to resemble a programming language more, and specifically the arc has
been that it starts to resemble Haskell more and more, which is very funny.
This isn't because I love Haskell—though I have a sort of guarded fondness for
it—or because I think it's the best possible design for a programming language,
let alone a specialist tool like lyre. This is because it turns out the problem
lyre is designed to solve is actually very similar to the space Haskell is
designed to explore.
Lyre's central idea is the notion of narrative explicitness; state only ever
changes through quality changes, values that are like messages emitted out
of the narrative engine, meant to be made visible to the player or used as the
origin point of events for things in a game's UI and world.
This is sort of the aesthetic and structural heart of StoryNexus and games in
that mold. Lyre is designed around it. And if you have a certain kind of
programming background, you may already see where this is going. "No silent or
implicit changes to story state" starts to look a lot like functional purity.
"The result of a state change is itself a value" starts to look a lot like a
monad.
I'm not going to define what a "monad" is here because it wouldn't help you in
the slightest if you don't already know. In this context, really, what a monad
is is just a way of chaining together different computations while keeping
track of their relationships; you can think of the "monad" here as being kind
of like a combine harvester, going over all the individual expressions that
make up a storylet, sorting text from quality changes from branches, and
storing them in the hopper to be read as the combined result of the storylet.
Lyre is designed to have a stateless runtime, meaning that the runtime doesn't
store any internal state. This is useful in a narrative engine because it makes
it easy to implement things like full undo or redo, or traversal between
narrative states, or the ability to look ahead at the result of taking a branch
without changing any internal state. It also kind of naturally flows from the
idea of the language not having any implicit state changes anyway, and makes
the runtime both smaller and less likely to contain subtle bugs.
I also wanted lyre to be viably something you could use to build a server-based
live game, and statelessness is really desirable there, because for a game
like that to scale it has to work in a way where you can't guarantee that a
given player's requests will all be answered by the same server process; there
can only be one source of truth, and that has to be a database that lives apart
from the game logic.
So the way the lyre API works is that every time you call it (to advance the
game state), you have to supply the game state along with your call; what you
get back is a representation of how the state changes, but the original game
state object you passed isn't mutated. The difficulty here though is that
people very strongly expect lyre to behave as if it keeps track of internal
state.
That is, you expect to be able to write this:
=storylet gain_wealth
~ wealth_roll = d(20)
~ gems = gems + wealth_roll
~ cash = cash + 100 * wealth_roll
...and have it work as you expect.
And indeed it does; the way lyre interprets a storylet is by evaluating each
statement in the context of the previous ones, essentially, and this is done
through an implementation of something analogous to the State monad in Haskell.
Conceptually, a storylet in lyre is a function that maps from game states to
other game states, and not just a series of instructions for doing something.
But ergonomically, you can write it as a series of instructions just like you
would in Ink or Yarn.
This is all to illustrate how path-dependent things can be in realizing a tool
like this. I started pulling on the thread of "what if we allowed unquoted
text" and where that ended was totally rethinking the language conceptually as
a prerequisite for allowing unquoted text in a sane way.
Previously, interleaving effects and text didn't make sense, because evaluating
the body text and evaluating changes were two distinct steps orchestrated by
the narrative engine, and one happened strictly before the other. Now it's all
one process.
There are still some open questions about lyre's syntax and semantics, and the
language is still evolving. One questionable area right now that lyre has a
very regimented structure and specifically limits how entities can nest in the
source code. Storylets contain branches; branches can contain results, which
are really just anonymous storylets meant as the "leaves" of the story tree.
This very much maps onto how StoryNexus works, and it is intended to make it so
the "grain" of the language is towards a flatter structure that's more at home in
a storylet narrative.
However, as the language becomes more expression-based, the idea of branches
and results also being syntactically construed as expressions rears its head...
But if that were to happen, you'd need some kind of delimitation for them,
whereas lyre currently literally has none—it looks a bit like an indentation
language, but it actually isn't at all. Blocks simply last until the parser
runs into the start of the next block at the same or higher level.
Indentation languages have a bunch of annoying downsides, and I also find them
aesthetically objectionable, so I don't want to go in this direction;
especially because it would be specifically to support use cases that are
likely to lead to gory and hard to understand use of indentation! I am not
sure, yet, of how to address this, or if it is even possible or desirable to
address. It may be that it's just not practical to fold the syntax that creates
branches/results into the expression syntax.
To concretize this, results look like this currently:
=storylet root
* A branch looks like this now—like in Ink.
An arrow points to another storylet by name.
-> elsewhere
=> And a "fat arrow" is a result, ie a storylet written inline here.
A result can have multiple statements, lines of text, etc.
* The parser knows that this new branch must mean the end of the result.
Indentation does not matter.
And it'd be nice if you could do this:
=storylet root
* Branch
~ if (some_condition) => This result is part of an expression!
But it still can have multiple lines, as it would need to to be
useful.
But the problem is that once results have wormed their way into the expression
language, that implies you can write...
=storylet root
* Branch
~ if (some_condition) => Result
~ if (another_condition) => This expression statement (everything from
the ~ on) is actually *nested* inside the other result. The parser
has no way of placing this on the "outer" level.
Lyre currently doesn't have a type checker, but it will eventually, and as
things stand it would have to choke on this because you'd have an expression
statement within a storylet that has the type Route; a route is the type of
statements like -> elsewhere, a description of which storylet to go to next;
they only have meaning as part of a branch.
Thus breaking lyre's nice and neat "blocks go on forever until they must stop"
syntax. One way of addressing this is to give lyre semantic indentation... but
again, I don't want to do that because it is annoying across multiple vectors,
even if the construct is less Python-like "brackets are indents" and more
Haskell-like "indentation is expression continuation".
Possibly the easiest version of this kind of syntax to build is the
"two-dimensional," truly Haskellish one where everything more indented than the
arrow itself becomes part of the result block that encompasses the arrow, but
that is so cursed.
An alternative would of course be just adding explicit delimiters, like {},
but of that's less than ideal for something highly ergonomic like a narrative
scripting language. It may just be the most practical solution though.
Ultimately building lyre has been a very gratifying exercise that has me
doing a lot of deep consideration of the aesthetics and design of this kind of
tool, and I look forward to continuing to refine it towards being more useful.
I am finding increasingly that there's this law of equivalent exchange to
designing something like this where often building regularities and meeting
likely user expectations involves building more complex or more sophisticated
inner systems that are then veiled from the user—again, the fact that lyre
increasingly resembles a functionally pure ML is actively something the
language is designed to conceal from the user.