Do you remember Bosque from Microsoft? I wrote a book about it
Tags: books, functional, programming, productivity
Published: 2021-09-16 19:28:47

Let's start with a quick explanation what is Bosque, because I believe it's not so well-known topic.

Bosque is a programming language created by Microsoft in 2019. Initially, it was an experiment that was supposed to show what would happen if we removed the sources of accidental complexity from the language. Currently, Bosque is on its road to the first stable version. This project's mission is to start a new paradigm called Regularized Programming.

At this point you may be wondering why on Earth would I write a book about a language which is hardly recognizable? Let me explain how it happend.

## How did I become an author?

It was a bit unexpected. Back in 2019, when Bosque had been initially released to the public, I was curious what this new language looks like and what it can be used for. At the time, I was also very interested in the internal design of various programming languages and was simply curious how to implement a new one from scratch. So, I downloaded the Bosque codebase and started reading it and playing with the language itself. There was a ton of bugs really. But I couldn’t leave that as-is and started fixing them. After my first PR got merged, I was motivated to open more. I was really enjoying it – I was both helping to develop Bosque and learning about how programming languages are designed and implemented. I was thinking that regardless of whether Bosque will be a success or not, it’s still worth doing it – you never know what the future brings, right? And then, a year later, I got an email in my inbox with a proposition to author a book about Bosque. I couldn’t say no and without hesitation agreed. That’s how I become an author and this is the cover of my first ever technical book:

![Cover](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gnpr33ku6junkjcn1iy.jpeg)

Let's get back to Bosque itself. I think it's a good idea to make a little introduction to the language since I decided to write a book about it, right? In the next few sections I describe in more detail what is Bosque and regularized programming as well as cite a few fragments from my book. Let's jump in!

## What is regularized programming?

Let's make a step back to the term "_sources of accidental complexity_". It covers things like loops, mutable state, unexpected behavior or invariants. All of that lead to the whole variety of problems, often quite easy to solve but hard to find. You can imagine a built-in method called `.sort()`. As a programmer you need to think whether the sorting is stable or not. Similarly iterating over a map - you can't be sure about keys order since there's no such concept as "order" in maps. Depending on a language the solutions will differ. Languages following regularized programming paradigm are supposed to work in very clear and predictable way.

Another example refers to the mentioned invariants. Let's say you create a game of chess. These are your invariants:

* a piece (or a pawn) can take only one square
* a piece (or a pawn) disappears from the board when it gets taken

These are the rules that cannot be broken. So we need to create a data structure representing a chessboard. When one of the players make a move we need to do two things: take a piece from one square (1) and put it on another (2). For a very short moment one of our invariants is broken. If we take the piece from a square first and then put it on another one, we break the second invariant because for a brief moment a piece that hasn't been taken is not on the board. If we put the piece on another square first and then take it from its current square, we break the first invariant because for a moment a piece stands on two different squares. A "regulation" here is a mechanism that allows you to update class fields in atomic way so that there's no moment in time when your invariants are broken.

This is what regularized programming has been initiated for. It's a paradigm that gives us tools that take a ton of problems from us. There are more examples really and it's hard to show them all. Using Bosque built-in tools you can create quite complex programs although the language is still under quite a heavy development.

Let's see a few examples from my book to better understand advantages of this language.

## Typed Strings

It's a feature that allows you to create text values of a specific type. Have a look at the fragment from _Chapter 3_, _Bosque Key Features_:

> [...] On the other hand, there are lots of less complex data types that are still schematic, and some checks are required to confirm their validity. Some examples include phone number, ZIP code, email address, credit card number, or even simple types such as mass or distance. All these data types can be described with a finite number of rules that define their structure. The problem is that we usually do not create a class for things such as a mass unit and even if we do, we feel like it's overkill because the implementation hides the true intent of a programmer, which is to simply know what type of data we are dealing with. A solution we usually go for is strings.

> If we want to store a phone number or a distance unit, we simply store it as a string. This is convenient, but at the same time, it brings terrible consequences. If we operate on strings, then how sure can we be that a given string is actually a phone number? What stops us from – accidentally – passing a string representing a mass unit to a function that expects a ZIP code string? The answer is that we can't be sure at all unless we perform a manual check; nothing stops us unless we perform a manual check. It would be much easier for us if there was a way to tell the compiler to treat some strings as if they were representing some data type – **typed strings**. This is where Bosque shines.

So we can define a type of value that is stored in a string. Thanks to that it's impossible to accidentaly pass the wrong value to your variable/parameter. For example if you want to store a phone number you can create a typed string and use it in your code so that there's no doubt what you are looking at:

```
let number = PhoneNumber'123-456-789';
```

There are more interesting ideas from Bosque creators.

## There are no loops in Bosque

No, really, not a single loop construct. But don't worry, you can still perform iteration - just use built-in methods for that. No need to create a "manual" `for` or `while`. Have a quick read:

> The truth is that Bosque did not remove the iteration completely. What the creators did is they removed structures like the ones we mentioned previously: `while`, `for`, `do...while`, and so on. Instead, every iterable data structure (such that we can perform an iteration on it) that is available in Bosque is equipped with a ton of methods that are performing iterative processing internally. The only thing we have to do is to pick the ones that will satisfy our needs and make use of them. This may seem like an impediment because we simply got used to structured loops. But in reality, for the price of some level of flexibility, we are better able to understand the code we write.

Using the built-in methods we can create programs that utilize iteration without having to write any loops. I prove that in the next section.

## Artificial Intelligence in Bosque? You can do that

Although Bosque is on the early stage of development you can create quite interesting projects. One of such projects is an AI classifier. Here are a few citations:

> [...] Our end result should consist of two programs. The first one should be used to train and test our intelligent model and return trained values (we'll talk about these in the next section). The second one should accept those learned values as parameters plus two coordinates – X and Y – and return a string that will indicate which quadrant the point with these coordinates belongs to.
> [...]
> Neural networks are mathematical structures that are designed to perform calculations by using a collection of nodes called neurons. An artificial neuron is an elementary unit of a neural network. Basically, it's a mathematical function that receives input and produces output. The artificial neuron usually has multiple input values (input vectors) that are weighted (weight vectors).
> [...]
> Alright, after this very quick introduction to neural networks, we are ready to put the theory into practice. Our task is a simple classification problem. We want to assign every
point to one of four classes (quadrants). As mentioned earlier, in order to recognize four classes, we need two perceptrons. Each of these two perceptrons will produce only one of
the two possible outputs – 1 or 0, which maps to a certain class. If the output is 1, we can say that the given perceptron has activated.
> [...]
> In order to train the perceptron, we need to perform certain operations iteratively until our training set is fully processed. At each step of iteration, we have to take the training
sample, create an input vector from it, and multiply this and the current weights vector. Then, we need to pass the result to the activation function and use the output to update
the weights. These steps must be repeated for all of the training samples.
> [...]
> The best practice is to always wrap the constantly repeating
code in a function so that it will be easier to maintain. So, let's go ahead and wrap the
repeating piece of code in a classify function. This is what it can look like:

```
function classify(x: Float64, y: Float64, perceptron: Perceptron): Float64 {
	let sum = multiplyVectors(List<Float64>@{ x, y }, perceptron.weights);
	return activation(sum);
}
```

## Summary

It's hard to show all examples here. The whole idea standing behind Bosque, its syntax and approach is a topic for a whole series of articles or... a book :) Anyway, I encourage you to have a look at the language. People at Microsoft have proven that they know how to create good programming lanugages - C# and TypeScript are the best examples. Who knows, maybe one day it will turn out to be a great success?