# Day 18 – Formulas: resistance is futile

Today, Perl turns 25: happy birthday Perl! There’s too much to say about this language, its philosophy, its culture, … So here, I would just thank all people who make Perl a success, for such a long time.

## Introduction

A formula is “an entity constructed using the symbols and formation rules of a given language“, according to Wikipedia as of this writing. These words sound really familiar for any Perl 6 users who have already played with grammars, however this is not the purpose of this article. Instead, the aim is to demonstrate how the Perl 6 language can be easily extended in order to use formulas literally in the code.

There are many domains, like Mathematics, Physics, finance, etc., that use their own specific languages. When writing programs for such a domain, it could be less error-prone and simpler to use its specific language instead of using a specific API. For example, someone who has knowledge in electronic may find the formula below:

`4.7kΩ ± 5%`

far more understandable than the following piece of code:

```my \$factory MeasureFactory.getSharedInstance();
my \$resistance = \$factory.createMeasure(value     => 4700,
unit      => Unit::ohm,
precision => 5);```

The formula `4.7kΩ ± 5%` will be used all along this article as an example.

## Symbol `k`: return a modified value

Let’s start with the simplest symbol: `k`. Basically this is just a multiplier placed after a numeric value. To make the Perl 6 language support this new operator, there’s no need to know much about Perl 6 guts: operators are just funny looking sub-routines:

`sub postfix:<k> (\$a) is tighter(&infix:<*>) { \$a * 1000 }`

This just makes `4.7k` return `4.7 * 1000`, for example. To be a little bit picky, such kind of multiplier should not be used without a unit (ex. `Ω`) and not be coupled to another multiplier (ex. `μ`). This would have made this article a little bit more complex, so this is left as an exercise to the reader :) Regarding the `tighter` trait, it is already well explained in three other articles.

## Symbols `%`: return a closure

The next symbol is `%`: it is commonly used to compute a ratio of something, that’s why `5%` shouldn’t naively be transformed into `0.05`. Instead, it creates a closure that computes the given percent of whatever you want:

`sub postfix:<%> (\$a) is tighter(&infix:<*>) { * * \$a / 100 }`

It’s now possible to write `\$f = 5%; \$f(42)` or `5%(42)` directly, and this returns `2.1`. It is worth saying this doesn’t conflict with the `infix:<%>` operator (modulo), that is, `5 % 42` still returns `5`.

## Symbol `Ω`: create a new `Measure` object

Let’s go on with the `Ω` symbol. One possibility is to tie the unit and the value in the same object, as in the `Measure` class defined below. The `ACCEPTS` method is explained later but the idea in this case is that two `Measure` objects with two different units can’t match together:

```enum Unit <volt ampere ohm>;

class Measure {
has Unit \$.unit;
has \$.value;

method ACCEPTS (Measure:D \$a) {
\$!unit == \$a.unit && \$!value.ACCEPTS(\$a.value);
}
}```

Then, one operator per unit can be defined in order to hide the underlying API, that is, to allow `4.7kΩ` as an equivalent of `Measure.new(value => 4.7k, unit => ohm)`:

```sub postfix:<V> (Real:D \$a) is looser(&postfix:<k>) {
Measure.new(value => \$a, unit => volt)
}
sub postfix:<A> (Real:D \$a) is looser(&postfix:<k>) {
Measure.new(value => \$a, unit => ampere)
}
sub postfix:<Ω> (Real:D \$a) is looser(&postfix:<k>) {
Measure.new(value => \$a, unit => ohm)
}```

Regarding the `ACCEPTS` method, it is used by `~~`, the smartmatch operator, to check if the left operand can match the right operand, the one with the `ACCEPTS` method. In other terms, `\$a ~~ \$b` is equivalent to `\$b.ACCEPTS(\$a)`. Typically, this allows the intuitive comparison between two different types, like scalars and containers for example.

In this example, this method is overloaded to ensure two `Measure `objects can match only if they have the same unit and if their values match. That means `4kΩ ~~ 4.0kΩ` is `True` whereas `4kΩ ~~ 4kV` is `False`. Actually, there are many units that can mix altogether, typically currencies (¥€\$) and the ones derived from the International System of Unit. But as usual, when something is a little bit more complex, it is left as an exercise to the reader ;)

## Symbol `±`: create a `Range` object

There’s only one symbol left so far: `±`. In the example, it is used to indicate the tolerance of the resistance. This tolerance could be either absolute (expressed in `Ω`) or relative (expressed in `%`), thus the new `infix:<±>` operator has several signatures and have to be declared with a `multi` keyword. In both cases, the `value` is a new `Range` objects with the right bounds:

```multi sub infix:<±> (Measure:D \$a, Measure:D \$b) is looser(&postfix:<Ω>) {
die if \$a.unit != \$b.unit;
Measure.new(value => Range.new(\$a.value - \$b.value,
\$a.value + \$b.value),
unit => \$a.unit);
}

multi sub infix:<±> (Measure:D \$a, Callable:D \$b) is looser(&postfix:<Ω>) {
Measure.new(value => Range.new(\$a.value - \$b(\$a.value),
\$a.value + \$b(\$a.value)),
unit => \$a.unit);
}```

Actually, any `Callable` object could be used in the second variant, not only the closures created by the `%` operators.

So far, so good! It’s time to check in the Perl6 REPL interface if everything works fine:

```> 4.7kΩ ± 1kΩ
Measure.new(unit => Unit::ohm, value => 3700/1..5700/1)

> 4.7kΩ ± 5%
Measure.new(unit => Unit::ohm, value => 4465/1..4935/1)```

It looks good, so all the code above ought to be moved into a dedicated module in order to be re-used at will. Then, a customer could load it and write literally:

```my \$resistance = 4321Ω;
die "resistance is futile" if !(\$resistance ~~ 4.7kΩ ± 5%);```

As of this writing, this works both in Niecza and Rakudo, the two most advanced implementations of Perl 6.

## Symbols that aren’t operators

Symbols in a formula are not always operators, they can be symbolic constants too, like π. In many languages, constants are just read-only variables, which sounds definitely weird: a variable isn’t supposed to be … variable? In Perl 6, a constant can be a read-only variable too (hmm) or a read-only term (this sounds better). For example, to define the constant term `φ`:

`constant φ = (1 + sqrt(5)) / 2;`

## Conclusion

In this article the Perl 6 language was slightly extended with several new symbols in order to embed simple formulas. Although it is possible to go further by changing the Perl 6 grammar in order to embed more specific languages, that is, languages that don’t have the same grammar rules. Indeed, there are already two such languages supported by Perl 6: regexp and quotes. The same way, Niecza use a custom language to connect its portable parts to the unportable.

## Bonus: How to type these exotic symbols?

Most of the Unicode symbols can be type in Xorg — the most used interface system on Linux — thanks to the `Compose` key, also named `Multi` key. When this special key is pressed, all the following key-strokes are somewhat merged in order to compose a symbol.

There’s plenty of documentation about this support elsewhere on Internet, so only the minimal information is provided here. First, to map the `Compose` key to the `Caps Lock` key, write in a X terminal:

`sh> setxkbmap -option compose:caps`

Some compositions are likely already defined, for instance `<caps>` followed by `+` then `-` should now produce `±`, but both `Ω` and `φ` are likely not defined. One solution is to write a
`~/.XCompose` file with the following content:

```include "%L" # Don't discard the current locale setting.

<Multi_key> <o> <h> <m>      : "Ω"  U03A9
<Multi_key> <O> <underscore> : "Ω"  U03A9
<Multi_key> <underscore> <O> : "Ω"  U03A9

<Multi_key> <p> <h> <y> : "φ"  U03C6
<Multi_key> <o> <bar>   : "φ"  U03C6
<Multi_key> <bar> <o>   : "φ"  U03C6```

This takes effect for each newly started applications. Feel free to leave a comment if you know how to add such a support on other
systems.

# Day 3 – Whatever the layout manager is

## Introduction

This article aims to demonstrate how `Whatever` — one of the many interesting Perl 6 curiosities — could be useful to easily implement and use complex things like a layout manager. In a couple of words, a layout manager is the part of a graphical interface in charge of the spatial arrangement of objects like windows or widgets. For the sake of simplicity, the layout manager implemented in this article will comply with the following three rules:

• there are only two kinds of widgets: terminal or container, the latter can contain other widgets of either kind;
• a widget cannot be overlapped, except for containers which fully contain their sub-widgets; and
• only the height can be adjusted, this size can be either static, dynamic, or intentionally left unspecified.

## Usage

From the user's point-of-view, this layout manager aims to be as easy to use as possible. For example, it shouldn't be hard to specify such typical interface below, inspired from a text-based program. In this example, the interface and body widgets are containers, all the others are terminals:

```interface (X lines)
+----> +------------------------------------------+
|      | menu bar (1 line)                        |  body (remaining space)
|      +------------------------------------------+ <----+
|      | subpart 1 (1/3 of the remaining space)   |      |
|      |                                          |      |
|      |                                          |      |
|      +------------------------------------------+      |
|      | subpart 2 (remaining space)              |      |
|      |                                          |      |
|      |                                          |      |
|      |                                          |      |
|      |                                          |      |
|      |                                          |      |
|      +------------------------------------------+ <----+
|      | status bar (1 line)                      |
+----> +------------------------------------------+```

The user don't know what the remaining space is in advance because such an interface is arbitrary resizable. As a consequence it should be specified as a non-predefined value; this is where `*` — the `Whatever` object — comes in handy. This object is interesting for two reasons:

• from the user's point-of-view, the definition of non-static sizes is as simple as: `* / 3` for the subpart 1 (dynamic) and just `*` for the subpart 2 (unspecified); and
• from the developer's point-of-view, Perl 6 transforms automatically things like `\$size = * / 3` into a closure: `x → x / 3`. Then, it could be called like a regular function: `\$size(\$x)`.

That way, the previous GUI can be transliterated into the following lines of code:

```my \$interface =
Widget.new(name => 'interface', size => \$x, sub-widgets => (
Widget.new(name => 'menu bar', size => 1),
Widget.new(name => 'main part', size => *, sub-widgets => (
Widget.new(name => 'subpart 1', size => * / 3),
Widget.new(name => 'subpart 2', size => *))),
Widget.new(name => 'status bar', size => 1)));```

## Implementation

The drawing of terminal widgets is straightforward since most of the work is done by containers. Those are in charge to compute the remaining space as well as to uniformly distribute widgets that have an unspecified size:

```class Widget {
has \$.name;
has \$.size is rw;
has Widget @.sub-widgets;

method compute-layout(\$remaining-space? is copy, \$unspecified-size? is copy) {
\$remaining-space //= \$!size;

if @!sub-widgets == 0 {  # Terminal
my \$computed-size = do given \$!size {
when Real     { \$_                  };
when Callable { .(\$remaining-space) };
when Whatever { \$unspecified-size   };
}

self.draw(\$computed-size);
}
else {  # Container
my @static-sizes   =  grep Real,     @!sub-widgets».size;
my @dynamic-sizes  =  grep Callable, @!sub-widgets».size;
my \$nb-unspecified = +grep Whatever, @!sub-widgets».size;

\$remaining-space -= [+] @static-sizes;

\$unspecified-size = ([-] \$remaining-space, @dynamic-sizes».(\$remaining-space))
/ \$nb-unspecified;

.compute-layout(\$remaining-space, \$unspecified-size) for @!sub-widgets;
}
}

method draw(Real \$size is copy) {
"+{'-' x 25}+".say;
"\$!name (\$size lines)".fmt("| %-23s |").say;
"|{' ' x 25}|".say while --\$size > 0;
}
}```

Here, any `Callable` object can be used to specify a dynamic size, as far as it takes the computed remaining space as argument. That means it is possible to specify more sophisticated dynamic size by passing a code `Block`. For example, `{ max(5, \$^x / 3) }` ensures the widget has a proportional size that can't decrease below `5`.

## Conclusion

It's time to check if this trivial layout manager works correctly both in Rakudo and Niecza, the two most advanced implementations of Perl 6. The following test is rather simple, it creates and draws an interface, then resize it and draws it again:

```my \$interface =
Widget.new(name => 'interface', size => 11, sub-widgets => (
Widget.new(name => 'menu bar', size => 1),
Widget.new(name => 'main part', size => *, sub-widgets => (
Widget.new(name => 'subpart 1', size => * / 3),
Widget.new(name => 'subpart 2', size => *))),
Widget.new(name => 'status bar', size => 1)));

\$interface.compute-layout;  # Draw
\$interface.size += 3;       # Resize
\$interface.compute-layout;  # Redraw```

The results before and after resizing are respectively displayed below. They are close enough from the initial mockup, n'est-ce pas?

```+-------------------------+            +-------------------------+
| menu bar (1 lines)      |            | menu bar (1 lines)      |
+-------------------------+            +-------------------------+
| subpart 1 (3 lines)     |            | subpart 1 (4 lines)     |
|                         |            |                         |
|                         |            |                         |
+-------------------------+            |                         |
| subpart 2 (6 lines)     |            +-------------------------+
|                         |            | subpart 2 (8 lines)     |
|                         |            |                         |
|                         |            |                         |
|                         |            |                         |
|                         |            |                         |
+-------------------------+            |                         |
| status bar (1 lines)    |            |                         |
|                         |
+-------------------------+
| status bar (1 lines)    |```

Finally, the implementation of such a flexible program is really simple in Perl 6: everything is already there, in the core language. Obviously, this trivial layout manager isn't ready for prime-time since a lot of things are missing: sanity checks, multiple dimensions, … but those are left as exercises to you, the reader ;) For any questions or comments, feel free to meet Perl 6 fellows on IRC (#perl6 on freenode).

## Bonus

As seen previously, `\$!size` can be `Whatever`, but it can't be whatever you want. For example, a negative `Real` or a string are not correct values. Once again Perl 6 provides a simple yet powerful feature: constrained types. In a couple of words this permits to define new types from a set of constraints:

```subset PosReal of Real where * >= 0;
subset Size where {   .does(PosReal)
or .does(Callable) and .signature ~~ :(PosReal --> PosReal)
or .does(Whatever) };

has Size \$.size is rw;```