Change your perspective
Tags: computerscience
Published: 2018-12-29 18:58:48

---
title: Change your perspective
published: true
description: A Programming Language Thought Experiment 
tags: computerscience
cover_image: https://i.imgur.com/pLXDRLT.png
---

**Let’s imagine a language that asks you to bring your own runtime.**  

To understand what I mean, let’s review a little bit about how programming languages work.

## Start with a Calculator 🧮

When you type things into your ti-89 or whatever overpriced 1980s technology schools have students using these days, you’re typing _a language_. That language has a lexer and a parser and a runtime and everything. 

Take the following:
```
1 + 1
```

When the calculator encounters `1 + 1`, it goes through and converts the string of characters it receives into tokens, something like this:

```
INT(1) PLUS INT(1)
```

Then it runs those tokens through a recursive descent parser that looks something like this:

```js
int() {
  return items.pop()
}

expr() {
	left = int()
	op = items.pop()
	right = int()

    return int() + int()
}
```

Finally, it will call `expr()` and return that to the user. 

## Programming languages end with an expression

At the end of the day, a programming language will evaluate its literals and then call some operating system functions via its runtime. 

So a more “program-y” language, might do something like this:

```
print(1 + 1)
```

Which would transfer into this:

```
PRINT LEFT_PAREN INT(1) PLUS INT(1) RIGHT_PAREN
```

Which would turn into something like this:

```js
root() {
  system.print(expr())
}
```

# Back to our fantasy language 
In existing programming languages, the runtime is hidden far below the compiler. In fact, the whole point of the language was to abstract the runtime away from you. 

You don’t need to know what system command executes printing a function or opening a file because you can just run `print()` or `open()`. 

## What would happen if we made runtimes a first class citizen?
Instead, let’s imagine a language where you were encouraged, supported, and supposed to write an alternative runtime.

For example, if the programming language says `print`, you could define exactly what `print` means.

Instead, your print could call arbitrary text-to-speech code. If it was in the web, it could return a component. Or it could just call some arbitrary function. 

## Change your perspective

Often, we think about software as an ever-changing system that runs on an extremely unchangeable base: the languages runtime. The whole point of most languages is to give you a tool that runs the _same_ on every person's computer. 

The runtime abstracts away multiple operating systems and hardware so you can always be sure that the logic stays constant. 

But by doing so, we often accidentally coupled the _logic_ of the code to the _domain_ it runs on. Countless hours have been wasted rewriting the same “web server that talks to a database” solution. 

Instead, **what if instead of working with a constant runtime, we worked with constant code.** 

What if you just pulled the “web server code” from a package manager and rewrote what lower level pieces did? Rather than rewrite the the whole thing yourself?

Your CRUD app could be rewritten to work on iOS, Android, and web by just rewriting the outputs of the language. 

## Woah, woah, woah isn’t this super insecure?

Probably. Though it could also be a security boon. Imagine being able to give the power of the runtime to an individual user. 

Anyone who _ran_ the program could no-op unsafe calls. No more `chmod` or network calls for untrusted programs. 

## What’s the point of this?

Building something like this would be dumb. Most of the problems that could be solved with a language like this have been solved in other ways. 

Operating systems have better access controls, modularity lets us have our own pseudo-runtime, and this may be too odd to be useful. 

But exploring alternative ways to solve existing problems can work interesting muscles in your brain. It’s also a little fun.