metanohi/site/magicng/index.md

292 lines
11 KiB
Markdown
Raw Normal View History

2016-09-02 11:47:33 +02:00
---
abstract: The Next Generation Programming Language
---
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
# MagicNG
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
*From 2012.*
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
# Chapter 1: The Future of Yesterday
*Software.*
*Magic.*
2016-08-30 21:46:31 +02:00
Two seemingly unrelated words. Yet together they form the essentials of the new
programming language which has already changed how computers and humans interact
with each other: *MagicNG* (short for Magic: The Next Generation).
2016-09-02 11:47:33 +02:00
**MagicNG** is not your common programming language; it is not biased towards one
2016-08-30 21:46:31 +02:00
or two paradigms, in fact it uses none of the existing ways of programming:
+ Functional programming is, naturally, much too functional for any magic-based
2016-09-02 11:47:33 +02:00
programming language, as magic is not *functional*, but
*magical*. Magic-based software does not require functionality as in something
being able to do something else, because it depends on magic which *does*
stuff instead of making sure that *stuff* can be done; this makes using a
2016-08-30 21:46:31 +02:00
computer much faster.
+ Object-oriented programming does not fit very well with the ideas behind
2016-09-02 11:47:33 +02:00
**MagicNG** either. While the magic-oriented approach *is* able to properly
2016-08-30 21:46:31 +02:00
emulate object-oriented ways of doing things, such emulations will often
2016-09-02 11:47:33 +02:00
require CMU-expensive ('Central Magical Unit') conversions from spell
2016-08-30 21:46:31 +02:00
scrolls to object-based representations.
+ Procedural programming conflicts with the nature of magic; since in the case
of magic, advanced heuristics are used run a program, the simple approach of
procedurally executing commands pales in comparison to the magical
approach. The same is true for assembly programming and machine programming.
+ Logic programming is mute. Magic is always above logic. Logic is based on
magic. Everyone knows that.
+ Declarative programming is actually the only one of the existing widespread
paradigms which is even slightly like *magical programming*. Declarative
programming languages do, however, still base their entire existence on
deduction and knowledge, both of which are infinitely long below the
standards of magical programming languages. Deduction and knowledge are for
2016-09-02 11:47:33 +02:00
programming languages that do not *see* how everything works, whose authors
2016-08-30 21:46:31 +02:00
do not understand the connectionabilities of everything.
2016-09-02 11:47:33 +02:00
In short, **MagicNG** employs the *magical programming* paradigm because it is
2016-08-30 21:46:31 +02:00
superior to non-magical approaches.
2016-09-02 11:47:33 +02:00
## But how do I program in it?
2016-08-30 21:46:31 +02:00
The reader should now be comfortable thinking about programming in terms of
magic and be able to see what a big mistake it was to learn all those outdated,
logic-based programming languages. The reader might also be a little
overwhelmed and perhaps confused; but fear not, we will soon take a look at an
example of MagicNG programming.
Before we get to that, we will go over a few (yes, there are many more) of the
applications of magical programming, to pique the reader's curiosity.
1. First, there is the temporal improvement. Since magic is independent of the
flow of time, a program can be run not just everywhere, but
2016-09-02 11:47:33 +02:00
every*when*. This is the sole reason MagicNG is already so popular ---
2016-08-30 21:46:31 +02:00
someone spread its use many years ago, through several temporal-offset
MagicNG runs.
2. Speed. The Central Magical Unit runs programs magically many times faster
than any CPU in existence. This is possible due both to the previously
2016-09-02 11:47:33 +02:00
explained effects of magic in programming *and* what we will come to refer
2016-08-30 21:46:31 +02:00
as "magic downleveling" in later chapters (since magic cannot be optimized,
other approaches such as the downleveling approach have been developed).
3. Code maintenance also becomes much easier, as you will see in the examples
and excercises.
Now, to our first example. We wish to print to a computer console (on a magical
computer) the magical string "Hello, world!". This is often accomplished in
anything from 1 to 100 conventional lines in conventional programming
2016-09-02 11:47:33 +02:00
languages; in MagicNG, it is not *that* simple. It's another kind of simple,
namely the *magic simple* way, from now on referred to as the *magple* way.
2016-08-30 21:46:31 +02:00
To write the magple MagicNG code for this example, we first fire up our MagicNG
interactive interpreter:
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
MagicNG vX
?
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
(MagicNG is not being developed because it relies on magic for updates, hence
the 'X' version.)
(The correct way to program in MagicNG is to write on pergament scrolls, but
for the beginner it is often easier to write the magical expressions on one's
computer and then print afterwards.)
The =?= at the prompt means we can type something. Let us try typing the
following:
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
? one frog eye
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
What this tells the MagicNG interpreter is that if *it is not the case* that no
2016-08-30 21:46:31 +02:00
frog eyes exist in the programmer's vicinity, it will print "Hello,
world!". The frog-eye detection is only a formality, included in MagicNG to
make the proofs of magical truth be magically true, and so the sentence can be
excluded if the programmer wishes so. That is, you could write
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
t
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
which is short for "one frog eye", and it would have the same effects.
MagicNG programs can be compiled (printed) to scrolls, which can then be used
independently of a running magical computer.
(Trivia: This was in fact one of the major reasons why the text-based nature of
MagicNG was chosen over the two other proposed ways of programming: speech and
jumping. While speech had the advantage that it worked well for people who had
difficulties writing, history had shown that the human memory was not a good
place to store information not meant to be changed. And while jumping was good
for people who were only good at jumping, it was too radical an approach for
many of the language designers.)
Now, it may not be obvious to the casual non-magician why and how both "one
frog eye" and "t" eventually prints "Hello, world!". In fact, these two
expressions are not at all the only ways to print "Hello, world!" --- one can
also write "traveller with seven legs", "spider queen", or something
2016-09-02 11:47:33 +02:00
similar. The thing to remember is that it is *the intention* that counts. If
you *feel* that the expression "bonsai of Norway" prints the sum of all
prime numbers below 4012, then *that is what it does*.
2016-08-30 21:46:31 +02:00
In essence, MagicNG is an *intention-based programming language*. This might at
first sound like all other programming languages: You have an intention on
2016-09-02 11:47:33 +02:00
which you base your programming. The difference here is that the intention *is*
2016-08-30 21:46:31 +02:00
the programming and not just part of it. Once you have figured out what you
want and written it down in your inner language, magic takes care of the boring
stuff.
This has the side-effect that all magic is somewhat personal; a scroll written
by an individual A might yield different results if used by an individual B
with different intentions than A.
This means that MagicNG has *built-in encryption*.
At this point some might note that this is not good for interoperability. At
the core of MagicNG, this is true. However, one extraordinary extension has
been made to MagicNG that makes interoperability both possible and easy:
MagicIS (short for Magic Intention Serializer).
MagicIS encodes your intention in a magical format, after which the intention
is compiled along with your program. With MagicIS, a compiled program that
prints "My name is Niels" might look like this (imagine it compiled on a
scroll):
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
Holy oak of honey
,si:pmnin34_=UUe
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
where the =,si:= part denotes the start of the serialized intention.
The MagicIS extension can be enabled on most MagicNG compilers with a
command-line option. Note that it will need to be connected to your brain with
a special magical interface to work.
2016-09-02 11:47:33 +02:00
# Chapter 2: Real World Examples
2016-08-30 21:46:31 +02:00
"This is all very nice," you might think, "but what is it any good for?"
MagicNG can be used for everything a typical, non-magical language can be used
for, the major differences being that MagicNG is per definition faster, better
and easier to program in. This has been magically proven.
2016-09-02 11:47:33 +02:00
## cat
2016-08-30 21:46:31 +02:00
Consider the UNIX =cat= program. If written in C (even if you look at the Plan
9 version), there are many lines of source code. If written in Python 3, there
are 2 lines:
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
for line in open(filename):
print(line)
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
If written in MagicNG, there is _one_ line (*and* it is magical):
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
cauldron of candy
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
(without MagicIS enabled.)
2016-09-02 11:47:33 +02:00
## Finding a value when a key is known
2016-08-30 21:46:31 +02:00
In a non-magical programming language, you could use a hash map. Or you could
sort the elements by their keys and use binary search. Or you could look
through every element one by one.
2016-09-02 11:47:33 +02:00
In MagicNG, no such algorithms are used; *magic* finds the value. In fact, this
2016-08-30 21:46:31 +02:00
reveals a large, fascinating and unavoidable part of MagicNG: it does not
support algorithms.
"No algorithms? But how, then, can I program?" you think. The answer is simple,
2016-09-02 11:47:33 +02:00
and it has been explained before, but to stress it one last time: *magic*.
2016-08-30 21:46:31 +02:00
This leads us to another important part of MagicNG: its use of the *black box
2016-09-02 11:47:33 +02:00
model*. The *black box model* has been revered all over the known universe for
2016-08-30 21:46:31 +02:00
its unchangeability (it is absolute), unworsenability (since you cannot change
it, you cannot make it worse), high surprise factor ("who knew my program could
2016-09-02 11:47:33 +02:00
do *that*?!"), and lack of meaningful error messages (no errors, no worries).
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
## Calculating the sum of a list of numbers
2016-08-30 21:46:31 +02:00
In C:
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
int sum(int xs[], int xs_len) {
int fin_sum = 0;
int i;
for (i = 0; i < xs_len; i++) {
fin_sum += xs[i];
}
return fin_sum;
}
[...]
sum({1, 3, -2, 9}, 4);
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
In Python:
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
sum([1, 3, -2, 9])
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
In MagicNG:
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
head of Macbeth
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
## A flight simulator
2016-08-30 21:46:31 +02:00
Up until now, we have only looked at MagicNG programs spanning single
lines. One can create a very powerful MagicNG program in one line, but
sometimes it can be necessary to use two, or even three lines.
As a flight operator, you may wish to have a very durable and efficient 3D
flight simulator; in MagicNG such a system can be written concisely in just
three lines of magical code (four lines with MagicIS enabled).
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
dragon claw polished with golden mead
mead in cauldron
tastebuds of 23 pigs
,si:3=)uUUn!2aa
2016-09-02 11:47:33 +02:00
```
2016-08-30 21:46:31 +02:00
(Notice the indentation and the explicit number.)
2016-09-02 11:47:33 +02:00
*[Chapters 3 through 88 have been excluded in this preview. Only members of
the Magically Magical Magic Community (MMMC) have access to these chapters.]*
2016-08-30 21:46:31 +02:00
2016-09-02 11:47:33 +02:00
# Chapter 89: How to Learn More
2016-08-30 21:46:31 +02:00
To recap: MagicNG is an easy language to learn, since not only does it not
require the user to learn about algorithms, data structures and related wastes
2016-09-02 11:47:33 +02:00
of time, but actually discourages that; MagicNG encourages its users to *not
think* which *reduces errors*.
2016-08-30 21:46:31 +02:00
To expand your knowledge of MagicNG, both in theory and practice, do not look
at the examples of other MagicNG programmers, unless you are *absolutely sure*
that their intentions are the same as yours. You can of course deduce the
intention from a MagicIS line, but then, if you wish to base a new program on
the old program, you will have to convert that intention to your own along with
the actual program, which can be tedious and very difficult (MagicNG is
actually a very complex language).
What you should do is lean back in a comfy chair and look into nothingness and
not strain your mind. That way, everything you need to know will come to you
eventually. That's how this book was written.