| 1 |
--- |
| 2 |
layout: default |
| 3 |
title: Language Design |
| 4 |
--- |
| 5 |
|
| 6 |
<div class="hgroup"> |
| 7 |
<div class="hgroup-inline"> |
| 8 |
<div class="panel"> |
| 9 |
<h1 id="language_design">Language Design</h1> |
| 10 |
</div> |
| 11 |
<div style="clear: both"></div> |
| 12 |
</div> |
| 13 |
</div> |
| 14 |
|
| 15 |
<div class="hgroup"> |
| 16 |
<div class="hgroup-inline"> |
| 17 |
<div class="panel"> |
| 18 |
<p>An exposition of the basic thinking that underpinned Jsonnet's design.</p> |
| 19 |
</div> |
| 20 |
<div style="clear: both"></div> |
| 21 |
</div> |
| 22 |
</div> |
| 23 |
|
| 24 |
|
| 25 |
<div class="hgroup"> |
| 26 |
<div class="hgroup-inline"> |
| 27 |
<div class="panel"> |
| 28 |
<h2 id="objectives">Objectives</h2> |
| 29 |
</div> |
| 30 |
<div style="clear: both"></div> |
| 31 |
</div> |
| 32 |
</div> |
| 33 |
|
| 34 |
<div class="hgroup"> |
| 35 |
<div class="hgroup-inline"> |
| 36 |
<div class="panel"> |
| 37 |
<p> |
| 38 |
<a href="https://www.json.org">JSON</a> has emerged as the defacto standard for communication |
| 39 |
of structured data, both between machines and at the human / machine boundary. However, in |
| 40 |
large quantities JSON can be unwieldy for humans, especially when duplication needs to be |
| 41 |
kept in sync between different parts of the data structure. Many people address this by |
| 42 |
writing scripts that generate JSON. Typically these are written in general purpose |
| 43 |
programming languages like Python. However, maintaining these scripts can be non-trivial, |
| 44 |
especially for persons unfamiliar with the generation code. |
| 45 |
</p> |
| 46 |
|
| 47 |
<p> |
| 48 |
Jsonnet attempts to solve this problem in a specialized and principled way. Its design was |
| 49 |
guided by the following criteria: |
| 50 |
</p> |
| 51 |
|
| 52 |
<ul> |
| 53 |
<li> |
| 54 |
<b>Hermeticity</b>: Code may be treated as data. The same JSON should be generated |
| 55 |
regardless of the environment, i.e. without non-deterministic or system-dependent |
| 56 |
behaviors. |
| 57 |
</li> |
| 58 |
<li> |
| 59 |
<b>Templating language</b>: Code should be interleaved with verbatim data so that it is |
| 60 |
easy to maintain the two in synchrony. The success of templating languages proves their |
| 61 |
effectiveness. |
| 62 |
</li> |
| 63 |
<li> |
| 64 |
<b>Variants</b>: It should be simple and intuitive to derive variants of any existing |
| 65 |
configurations that override attributes for special or ad-hoc purposes. |
| 66 |
</li> |
| 67 |
<li> |
| 68 |
<b>Modularity</b>: As configurations grow, it must be possible to manage the complexity |
| 69 |
using standard techniques for <a |
| 70 |
href="https://dl.acm.org/doi/10.1145/800027.808431">programming in the large</a>. |
| 71 |
Configurations may span many files and many developers. Errors should provide stack |
| 72 |
traces that describe the nested context. |
| 73 |
</li> |
| 74 |
<li> |
| 75 |
<b>Familiarity</b>: Raw data should be specified as JSON. Computation constructs should |
| 76 |
behave in standard ways and compose predictably. |
| 77 |
</li> |
| 78 |
<li> |
| 79 |
<b>Powerful yet simple</b>: Trivial cases should be trivial. More complex cases should be |
| 80 |
approachable, with localized additional complexity. Everything must be <i>possible</i>, |
| 81 |
yet the language must still have a small footprint for learning and future tooling. |
| 82 |
</li> |
| 83 |
<li> |
| 84 |
<b>Wide scope</b>: The same language should be able to configure anything. Ideally, all |
| 85 |
components of a system should be managed via a well-maintained centralized configuration, |
| 86 |
written in a common language. |
| 87 |
</li> |
| 88 |
<li> |
| 89 |
<b>Formal rigor</b>: There must be an authoritative specification that is complete and |
| 90 |
simple enough to understand, and a comprehensive set of tests. This allows new |
| 91 |
implementations to be developed without compatibility hurdles. The language should |
| 92 |
<i>not</i> be defined by its first implementation. |
| 93 |
</li> |
| 94 |
</ul> |
| 95 |
<p> |
| 96 |
Before Jsonnet, no existing configuration languages or DSLs satisfied these criteria. In |
| 97 |
fact, the vast majority only satisfied one or two of them. |
| 98 |
</p> |
| 99 |
</div> |
| 100 |
<div style="clear: both"></div> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
|
| 104 |
|
| 105 |
<div class="hgroup"> |
| 106 |
<div class="hgroup-inline"> |
| 107 |
<div class="panel"> |
| 108 |
<h2 id="json">Rationale for extending JSON</h2> |
| 109 |
</div> |
| 110 |
<div style="clear: both"></div> |
| 111 |
</div> |
| 112 |
</div> |
| 113 |
|
| 114 |
<div class="hgroup"> |
| 115 |
<div class="hgroup-inline"> |
| 116 |
<div class="panel"> |
| 117 |
<p> |
| 118 |
Extending JSON means building a language that fits within the existing constraints of the |
| 119 |
JSON specification. This means that all JSON data must behave the same in Jsonnet (i.e. be |
| 120 |
emitted unchanged), with additional behaviors being expressible using constructs that would |
| 121 |
be syntax errors in standard JSON. This choice did constrain us substantially, but we |
| 122 |
believe it was the correct one for the following reasons: |
| 123 |
</p> |
| 124 |
|
| 125 |
<p> |
| 126 |
A common case of Jsonnet program is a program that is mostly JSON data, but has a few |
| 127 |
occurrences of Jsonnet constructs. In this case, someone who knows JSON can maintain the |
| 128 |
file with no additional knowledge. |
| 129 |
</p> |
| 130 |
|
| 131 |
<p> |
| 132 |
Systems that accept JSON can be transparently modified to accept Jsonnet instead by |
| 133 |
inserting a step to convert the Jsonnet to JSON. If you want to be extra paranoid, you can |
| 134 |
invoke Jsonnet only if the JSON parsing failed, but this is not actually necessary. |
| 135 |
</p> |
| 136 |
</div> |
| 137 |
<div style="clear: both"></div> |
| 138 |
</div> |
| 139 |
</div> |
| 140 |
|
| 141 |
|
| 142 |
<div class="hgroup"> |
| 143 |
<div class="hgroup-inline"> |
| 144 |
<div class="panel"> |
| 145 |
<h2 id="reflection">Rationale for Reflection</h2> |
| 146 |
</div> |
| 147 |
<div style="clear: both"></div> |
| 148 |
</div> |
| 149 |
</div> |
| 150 |
|
| 151 |
<div class="hgroup"> |
| 152 |
<div class="hgroup-inline"> |
| 153 |
<div class="panel"> |
| 154 |
<p> |
| 155 |
In JSON, there is an object construct, that is used as a dictionary, and like an object. |
| 156 |
Jsonnet is the same, the object construct is often used as a dictionary as well as a |
| 157 |
conventional object. The importance of this is that it is necessary to iterate over |
| 158 |
dictionaries, test for the existence of a key, and build them with computed key names. |
| 159 |
These features when applied to objects give us reflection. So naturally, Jsonnet has |
| 160 |
reflection because objects and dictionaries are the same thing. |
| 161 |
</p> |
| 162 |
</div> |
| 163 |
<div style="clear: both"></div> |
| 164 |
</div> |
| 165 |
</div> |
| 166 |
|
| 167 |
|
| 168 |
<div class="hgroup"> |
| 169 |
<div class="hgroup-inline"> |
| 170 |
<div class="panel"> |
| 171 |
<h2 id="turing_completeness">Rationale for Turing-Completeness</h2> |
| 172 |
</div> |
| 173 |
<div style="clear: both"></div> |
| 174 |
</div> |
| 175 |
</div> |
| 176 |
|
| 177 |
<div class="hgroup"> |
| 178 |
<div class="hgroup-inline"> |
| 179 |
<div class="panel"> |
| 180 |
<p> |
| 181 |
Jsonnet is Turing-complete as it is possible to write non-terminating programs. |
| 182 |
Configurations should always terminate, so ideally we would consider non-termination as an |
| 183 |
error. However, doing so in general is <a |
| 184 |
href="https://en.wikipedia.org/wiki/Halting_problem">impossible</a> and even when constrained |
| 185 |
to practical use cases, it remains impractical. Typical approaches for enforcing |
| 186 |
termination either restrict the language (e.g. to primitive recursion), which makes some |
| 187 |
programs impossible / difficult to write, or, alternatively, require the programmer to |
| 188 |
provide evidence that the program terminates, via some sort of annotation / <a |
| 189 |
href="https://turingarchive.kings.cam.ac.uk/publications-lectures-and-talks-amtb/amt-b-8" |
| 190 |
>energy function</a>. Both of these make the programmer's life more difficult. |
| 191 |
</p> |
| 192 |
<p> |
| 193 |
Furthermore, non-Turing complete languages can take arbitrary CPU or RAM by running |
| 194 |
intensive algorithms on large input. So enforcing termination is firstly not practical, and |
| 195 |
secondly does not actually solve the more practical problem of bounding resource consumption |
| 196 |
during execution. |
| 197 |
</p> |
| 198 |
<p> |
| 199 |
For Jsonnet we decided restricting termination would create more problems than it would |
| 200 |
solve. |
| 201 |
</p> |
| 202 |
</div> |
| 203 |
<div style="clear: both"></div> |
| 204 |
</div> |
| 205 |
</div> |
| 206 |
|
| 207 |
|
| 208 |
<div class="hgroup"> |
| 209 |
<div class="hgroup-inline"> |
| 210 |
<div class="panel"> |
| 211 |
<h2 id="dynamic_typing">Rationale for Dynamic Typing</h2> |
| 212 |
</div> |
| 213 |
<div style="clear: both"></div> |
| 214 |
</div> |
| 215 |
</div> |
| 216 |
|
| 217 |
<div class="hgroup"> |
| 218 |
<div class="hgroup-inline"> |
| 219 |
<div class="panel"> |
| 220 |
<p> |
| 221 |
Modern <a href="https://en.wikipedia.org/wiki/Type_system">type systems</a> typically either |
| 222 |
require annotations, use type inference, or are dynamic. The former two approaches have two |
| 223 |
advantages: Firstly, some errors can be detected before execution (some correct programs are |
| 224 |
also rejected). Secondly, they can be implemented more efficiently since the additional |
| 225 |
knowledge about the program means special memory representations can be used, and also some |
| 226 |
instructions can be elided. However annotations are additional bureaucracy and type |
| 227 |
inference produces unification errors that the programmer has to understand and fix. |
| 228 |
</p> |
| 229 |
<p> |
| 230 |
Dynamic typing means checking types at run-time, and raising errors via the language's |
| 231 |
existing error reporting mechanism. This is conceptually much simpler for the programmer. |
| 232 |
In a configuration language, runtime performance is not as important as it is in most other |
| 233 |
contexts, and additionally programs tend to execute quickly so testing is much easier. On |
| 234 |
the other hand, simplicity is of the utmost importance. |
| 235 |
</p> |
| 236 |
<p> |
| 237 |
For Jsonnet, i.e. in the context of configuration languages, we decided that simplicity was |
| 238 |
far more important than the minimal benefits of runtime efficiency and static error |
| 239 |
detection. |
| 240 |
</p> |
| 241 |
</div> |
| 242 |
<div style="clear: both"></div> |
| 243 |
</div> |
| 244 |
</div> |
| 245 |
|
| 246 |
|
| 247 |
<div class="hgroup"> |
| 248 |
<div class="hgroup-inline"> |
| 249 |
<div class="panel"> |
| 250 |
<h2 id="oo">Rationale for Object-Oriented Semantics</h2> |
| 251 |
</div> |
| 252 |
<div style="clear: both"></div> |
| 253 |
</div> |
| 254 |
</div> |
| 255 |
|
| 256 |
<div class="hgroup"> |
| 257 |
<div class="hgroup-inline"> |
| 258 |
<div class="panel"> |
| 259 |
<p> |
| 260 |
Object-oriented semantics (specifically, late binding), are ideal for deriving variants from |
| 261 |
existing data. Most modern object-oriented languages distinguish between classes and |
| 262 |
objects. Classes can be extended, or instantiated into objects, and objects cannot be |
| 263 |
extended. However some are <a |
| 264 |
href="https://en.wikipedia.org/wiki/Prototype-based_programming">prototype-based</a>, where |
| 265 |
the concept of class and object are essentially fused. In Jsonnet, we decided to use the |
| 266 |
latter form because JSON does not have classes, and it's simpler to only have one concept |
| 267 |
instead of two. |
| 268 |
</p> |
| 269 |
<p> |
| 270 |
Furthermore, we decided to use mixin semantics for inheritance, because they are the most |
| 271 |
powerful and flexible form of inheritance that has been widely studied, yet remain |
| 272 |
remarkably simple to define and use. |
| 273 |
</p> |
| 274 |
<p> |
| 275 |
One novelty is our notion of a first class <code>super</code> construct, which can be used |
| 276 |
outside the context of field lookup, i.e. <code>super.f</code>. The behavior of |
| 277 |
<code>super</code> by itself is given formally in the <a |
| 278 |
href="/language/spec.html">specification</a>. We decided not to restrict <code>super</code> |
| 279 |
in order to keep the language simple (fewer rules). |
| 280 |
</p> |
| 281 |
</div> |
| 282 |
<div style="clear: both"></div> |
| 283 |
</div> |
| 284 |
</div> |
| 285 |
|
| 286 |
|
| 287 |
<div class="hgroup"> |
| 288 |
<div class="hgroup-inline"> |
| 289 |
<div class="panel"> |
| 290 |
<h2 id="functional">Rationale for Pure Functional Semantics</h2> |
| 291 |
</div> |
| 292 |
<div style="clear: both"></div> |
| 293 |
</div> |
| 294 |
</div> |
| 295 |
|
| 296 |
<div class="hgroup"> |
| 297 |
<div class="hgroup-inline"> |
| 298 |
<div class="panel"> |
| 299 |
<p> |
| 300 |
There are ideological and practical reasons for designing Jsonnet as a pure functional |
| 301 |
language. On the practical side, pure functional languages have no side-effects, which |
| 302 |
(together with determinism) gives us the property of hermeticity. This allows code to be |
| 303 |
treated as data, because the code will always evaluate to the same thing, and it is not |
| 304 |
allowed to make changes to its environment (e.g. by writing to files). By analogy, |
| 305 |
compressed data can be decompressed on demand because it will always produce the same data |
| 306 |
and the decompression process doesn't have any external side-effects. |
| 307 |
</p> |
| 308 |
<p> |
| 309 |
Furthermore, the property of <a |
| 310 |
href="https://en.wikipedia.org/wiki/Referential_transparency">referential |
| 311 |
transparency</a> essentially extends the hermeticity property into submodules of the Jsonnet |
| 312 |
code itself, which means that different parts of the code cannot interfere with each other |
| 313 |
and everything composes in a very predictable way. In a language whose purpose is simply to |
| 314 |
define data, the property of referential transparency is very natural. Ideologically |
| 315 |
speaking, functional programming language advocates have always claimed that functional |
| 316 |
languages are excellent at defining, translating, and refining structured data. |
| 317 |
</p> |
| 318 |
</div> |
| 319 |
<div style="clear: both"></div> |
| 320 |
</div> |
| 321 |
</div> |
| 322 |
|
| 323 |
|
| 324 |
<div class="hgroup"> |
| 325 |
<div class="hgroup-inline"> |
| 326 |
<div class="panel"> |
| 327 |
<h2 id="lazy">Rationale for Lazy Semantics</h2> |
| 328 |
</div> |
| 329 |
<div style="clear: both"></div> |
| 330 |
</div> |
| 331 |
</div> |
| 332 |
|
| 333 |
<div class="hgroup"> |
| 334 |
<div class="hgroup-inline"> |
| 335 |
<div class="panel"> |
| 336 |
<p> |
| 337 |
The late binding from the object oriented semantics already embodies some of the features of |
| 338 |
a lazy language. Errors do not occur unless a field is actually dereferenced, and cyclic |
| 339 |
structures can be created. For example the following is valid even in an eager version of |
| 340 |
the language: |
| 341 |
</p> |
| 342 |
<pre class="large">local x = {a: "apple", b: y.b}, |
| 343 |
y = {a: x.a, b: "banana"}; |
| 344 |
x</pre> |
| 345 |
<p> |
| 346 |
It would therefore be confusing if the following was not also valid, which leads us to lazy |
| 347 |
semantics for arrays. |
| 348 |
</p> |
| 349 |
<pre class="large">local x = ["apple", y[1]], |
| 350 |
y = [x[0], "banana"]; |
| 351 |
x</pre> |
| 352 |
<p> |
| 353 |
Therefore, for consistency, the whole language is lazy. It does not harm the language to be |
| 354 |
lazy: Performance is not significantly affected, stack traces are still possible, and it |
| 355 |
doesn't interfere with I/O (because there is no I/O). There is also a precedent for |
| 356 |
laziness, e.g. in Makefiles and the Nix expression language. |
| 357 |
</p> |
| 358 |
<p> |
| 359 |
Arguably, laziness brings real benefits in terms of abstraction and modularity. It is |
| 360 |
possible to build infinite data-structures, and there is unrestricted beta expansion. For |
| 361 |
example, the following 2 snippets of code are only equivalent in a lazy language. |
| 362 |
</p> |
| 363 |
<pre class="large">if x == 0 then 0 else if x > 0 then 1 / x else -1/x</pre> |
| 364 |
<pre class="large">local r = 1 / x; |
| 365 |
if x == 0 then 0 else if x > 0 then r else -r |
| 366 |
</pre> |
| 367 |
</div> |
| 368 |
<div style="clear: both"></div> |
| 369 |
</div> |
| 370 |
</div> |
| 371 |
|
| 372 |
|
| 373 |
<div class="hgroup"> |
| 374 |
<div class="hgroup-inline"> |
| 375 |
<div class="panel"> |
| 376 |
<h2 id="modularity">Modularity and Encapsulation</h2> |
| 377 |
</div> |
| 378 |
<div style="clear: both"></div> |
| 379 |
</div> |
| 380 |
</div> |
| 381 |
|
| 382 |
<div class="hgroup"> |
| 383 |
<div class="hgroup-inline"> |
| 384 |
<div class="panel"> |
| 385 |
<p> |
| 386 |
In Jsonnet, a <dfn>module</dfn> is typically a Jsonnet file that defines an object whose |
| 387 |
fields contain useful values, such as functions or objects that can specialized for a |
| 388 |
particular purpose via extension. Using an object at the top level of the module allows |
| 389 |
adding other fields later on, without having to alter user code. When writing such a |
| 390 |
module, it is advisable to expose only the interface to the module, and not its |
| 391 |
implementation. This is called encapsulation, and it allows changing the implementation |
| 392 |
later, despite the module being imported by many other Jsonnet files. |
| 393 |
</p> |
| 394 |
<p> |
| 395 |
Jsonnet's primary feature for encapsulation is the <code>local</code> keyword. This makes |
| 396 |
it possible to define variables that are visible only to the module, and impossible to |
| 397 |
access from outside. The following is a simple example. Other code can import |
| 398 |
<tt>util.jsonnet</tt> but will not be able to see the <code>internal</code> object, and |
| 399 |
therefore not the function <code>square</code>. |
| 400 |
</p> |
| 401 |
<pre class="large"><code>// util.jsonnet |
| 402 |
local internal = { |
| 403 |
square(x):: x*x, |
| 404 |
}; |
| 405 |
{ |
| 406 |
euclidianDistance(x1, y1, x2, y2):: |
| 407 |
std.sqrt(internal.square(x2-x1) + internal.square(y2-y1)), |
| 408 |
} |
| 409 |
</code></pre> |
| 410 |
<p> |
| 411 |
It is also possible to store <code>square</code> in a field, which exposes it to those |
| 412 |
importing the module: |
| 413 |
</p> |
| 414 |
<pre class="large">// util2.jsonnet |
| 415 |
{ |
| 416 |
square(x):: x*x, |
| 417 |
euclidianDistance(x1, y1, x2, y2):: |
| 418 |
std.sqrt(self.square(x2-x1) + self.square(y2-y1)), |
| 419 |
}</pre> |
| 420 |
<p> |
| 421 |
This allows users to redefine the square function, as shown in the very strange code below. |
| 422 |
In some cases, this is actually what you want and is very useful. But it does make it |
| 423 |
harder to maintain backwards compatibility. For example, if you later change the |
| 424 |
implementation of <code>euclidianDistance</code> to inline the square call, then user code |
| 425 |
will behave differently. |
| 426 |
</p> |
| 427 |
<pre class="large">// myfile.jsonnet |
| 428 |
local util2 = import "util2.jsonnet" { square(x):: x*x*x }; |
| 429 |
{ |
| 430 |
crazy: util2.euclidianDistance(1,2,3,4) |
| 431 |
}</pre> |
| 432 |
<p> |
| 433 |
In conclusion, Jsonnet allows you to either expose these details or hide them. So choose |
| 434 |
wisely, and know that everything you expose will potentially be used in ways that you didn't |
| 435 |
expect. Keep your interface small if possible. |
| 436 |
</p> |
| 437 |
<p> |
| 438 |
A common belief is that languages should make <code>local</code> the default state, with an |
| 439 |
explicit construct to allow outside access. This ensures that things are not accidentally |
| 440 |
(or apathetically) exposed. In the case of Jsonnet, backwards compatibility with JSON |
| 441 |
prohibits that design since in JSON everything is a visible field. |
| 442 |
</p> |
| 443 |
</div> |
| 444 |
<div style="clear: both"></div> |
| 445 |
</div> |
| 446 |
</div> |
| 447 |
|
| 448 |
|
| 449 |
<div class="hgroup"> |
| 450 |
<div class="hgroup-inline"> |
| 451 |
<div class="panel"> |
| 452 |
<h2 id="misc">Miscellaneous Design Choices</h2> |
| 453 |
</div> |
| 454 |
<div style="clear: both"></div> |
| 455 |
</div> |
| 456 |
</div> |
| 457 |
|
| 458 |
<div class="hgroup"> |
| 459 |
<div class="hgroup-inline"> |
| 460 |
<div class="panel"> |
| 461 |
<p> |
| 462 |
The language is designed to be implementable via desugaring, i.e. there is a simple core |
| 463 |
language and the other constructs are translated down to this core language before |
| 464 |
interpretation. This technique allows a language to have considerable expressive power, |
| 465 |
while remaining easy to implement. </p> <p> The string formatting operator <code>%</code> is |
| 466 |
defined to behave identically to the Python equivalent, and therefore very similarly to the |
| 467 |
C <tt>printf</tt> micro-language. We decided it would be better to be compatible with an |
| 468 |
existing popular language wherever possible, and Python is often the language of choice for |
| 469 |
operations people. |
| 470 |
</p> |
| 471 |
<p> |
| 472 |
In contrast, we decided that the semantics of Python's <tt>and</tt> and <tt>or</tt> |
| 473 |
operators, which have meaning beyond boolean logic, might be confusing for those without |
| 474 |
prior knowledge of such languages. We therefore implemented the simpler |
| 475 |
<code>&&</code> and <code>||</code> operators, which only operate on booleans. More |
| 476 |
complex cases can be implemented explicitly with the <code>if</code> construct. |
| 477 |
</p> |
| 478 |
</div> |
| 479 |
<div style="clear: both"></div> |
| 480 |
</div> |
| 481 |
</div> |