Forth is the wrong way, because it makes no problems

Abstract
The Forth programming language is superior to all other computer architects. The programs runs better and the hardware is more efficient. But for teaching purposes this is not a wishful state. Students learn better if the underlying system isn’t working very well. The UNIX operating system produces a lot of trouble, and this is the reason why it is the better teaching tool. Apart from a datastack and a returnstack UNIX has much more to offer, for example a POSIX API and a high level programming language, called C.

Introduction

The paper is copyright protected. That means, it is not allowed to redistribute it over the internet or in Academic social networks. To get access to the fulltext, the user must proof, that he has reasonable knowledge about Artificial Intelligence.

1. Who has invented the perceptron in 1957?
a) Frank Rosenblatt b) Richard Feynman c) Dennis Ritchie

2. Which programming language is used heavily for Aimbot programming?
a) PHP b) LaTeX c) AutoIt

3. Who is the inventor of the git-version control system?
a) Linus Torvalds b) Bill Gates c) Steven M. LaValle

4. What is PDDL?
a) computergame b) operating system c) symbolic planner

5. Who is the inventor of BEAM robotics?
a) Mark Tilden b) Sebastian Thrun c) Marvin Minsky

The passphrase gets formed by the correct answers in the quiz. For example, if the choice is question1=c, question2=a, 3=a, 4=a, 5=b the following bash command in the Linux operating system is used for decrypting the paper:

gpg --passphrase 1c2a3a4a5b paper.txt.asc

Here comes the fulltext of the paper.

-----BEGIN PGP MESSAGE-----

jA0EBwMCt/yqAiX/5hpg0usBCeoqWDPKD9iMpOIxDIeUpMpRQVSGB+j8whtYLCOq
8w+dukdWBnSL2an5x9kbUtR5my1KAKXA/dJvnEZ5Jt3Sb8TbPYICea9m+pGWySlX
7nwyiOt7mBV1XCg1GfTV7IrmA9ky7ULlen+hZo+syCr/+NMJ0sQ/lfAAqBTDulsR
/Jd7GdFQ5qDVzlcYYoQwCTEB+3PyaZl2uUMGWMtKPsi4pA4TvyeOrAP4cbHdxand
bYgOVQIRGWQnFfJ4mho9VIi4ZJF+KVMBI3uQegogKekDJnBkYvznbQg+jMbExXpa
doGAU50cG1CEx+TQP1V9vwICqWWya5RNIUzCNfWtU+LS8N6/vfIp+kCODoJFNLPY
3s0f+4VnLTSXJjPqbkdtjLFA8wJ1srwKcz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y
7FQENC/LjBfUWxhHziHDATWhr4mj5nJ5Z2tS0xMPqkKADhnd8yrNPtUx+bxFVN7B
FJINg3j247eGH5m1/64aY1j7sD7fPFHTniZejcIJ9kSQKGS1zU66CULfRhNbNsFw
OOrDVZI8/xlDLLdP8/2i62HygjOjlZobm3dlZHayk9Yl2QhzYoJhmHto6uEFM68R
gcEkMtI7nhIN4mPJTc5celubacRTmWY4KlWcKfq61lB2HxMdMkcDp1+dKlm4bCmf
acq34Fq9MUtOdqybeKNV+hWp6Gp7iUajLj4j8QvCzyc/d+yVcCDS5qGRIrnxm0B7
eDFv6CFtoXbXQmkeeaVihJL8QG92Ge7H2Lo6OmxtIbuMvip9LEmRoUxPawgasXrr
voQBuebNl09PRRzMGH3E0FBmG06q7aN3+nqNL2O3mxg06+AvArZ+LTtTBtA7fY5m
o211ni+IHGrBQZ6D7r+9Re5Xcbl3VWI44WBmVg0B+UemHftvM2NWoraL1yXQdF+x
PFnP07Dd4N18rbM2FHEfPX1lMmNlwzZafg9RVyKOG2+EawJJPcf5m1bjkJ0WNnw6
7KX2AzpBBiYxZP9Y/hsKM9Eq4EcKw46WOrkP0mzwtfAqSxyI34UlcU6CXn06opom
Qy/+JH4aez0T9sAnzM6kJgqywKtp580u8cZWa+t2gb+IfE+kmUUewKy3cMErhLYc
fHu9f6sjsl3NQcef9F17IhxNQ1XMcdvRhobFnhvM5mJ4xHmcmx678aDnkdw3sUjJ
1fNiV69yTjLmjGoT12HoEqeaBfSlmHrF3KUJvCi58SkWJM3mV9aQUfg0ISD5Z0GQ
Zfu8UtbdnQ==
=druJ
-----END PGP MESSAGE-----
Advertisements

Programming in Forth – the right way


In the picture above we see an example of using Forth. The screenshot was taken from a japanese Forth forum on the internet. The people there are encouraging each other to learn Forth and everybody is welcome who is interested in writing easy and not so easy programs. The URL of the forum is https://ja.stackoverflow.com/ and the guidelines are very strict in relationship to the japanese language. That means, it is not allowed to posting something in English.

Why Forth makes sense on Stackoverflow

The number of Forth-related questions on Stackoverflow is small. Around 180 are there. Not 180k, but only 0.18k. Today’s programmers have other needs then playing around with the stack or write factorized sourcecode. The major Forth-related online forum is the usenetgroup “comp.lang.forth” which has a high traffic. But for the future it makes sense to push the Forth ideology into Stackoverflow. There are many reasons. At first, Stackoverflow has a unique capabilities of posting images. If the question is more complicated a figure may help the understanding. The second, more important reason is, that Forth is not really about the programming language. Forth was not invented for writing real software in it. It is some kind meta-language which helps to learn other languages. For example, if a C++ programmer wants to write better code. Than he should take a look at forth, because the code examples there have a high-quality. It is clean, efficient code. And if a compiler-designer want’s to improve the Java virtual machine, then at look at Forth helps a lot. Without no doubt, the Stackoverflow community has the largest number of programmers and potential programmers worldwide. So it is the natural place for discussion concatenative programming language.

Sometimes, Forth is described as a lowlevel, assembly like programming language. But in reality, Forth is focussed not on computers but on people. It is a learning tool for teaching cpu-architecture and software-development. The best platform for running forth is not the GA144 cpu or the Intel CPU, the best platform is a computer course at the university. The idea is to use Forth as a dry language inside a pen&paper computer. The main advantage is, that only Forth is minimalistic per default. A Forth CPU is the smallest form of a cpu, a Forth compiler is the smallest piece of software and so on. Holding Forth outside of todays programmer community with the reference, that the language is obsolete makes no sense. Yes, Forth is outdated, and this makes it attractive for teaching. Possible alternatives to Forth are not C++ or Java, but the teaching languages PL/0 (Wirth) and the teaching cpu MMIX (Knuth).

New paper in preparation

I’m currently writing a new paper about the Forth programming language, a small example is given in the image above. The reason is, that the language Forth is very interesting and until now only few papers where published about the topic. The idea is not to propagate the Forth language itself, but to write a Simulator like the “little man computer” to run Forth programs in debug mode. That may be a bit surprising, because normal programming language are used for something meaningful. For example, C++ can be used to write a database application. So the natural idea is to use Forth for programming a hello world application or alternative make a Forth based hardware. But both is not the intention of my paper. Making a Forth chip in FPGA hardware is way to complicated for amateurs, and programming a longer software with Forth is also a pain. So the normal result is, to not using Forth for something useful and research other programming languages like Java, Python or whetever. But there is an alternative. The idea is not to program in Forth, but in C++. The C++ program is some kind of game in which a Forth Virtual machine is executed. The idea is heavily used in the minecraft community who are using the building blocks to build a 8 bit cpu and playing around with it. The interesting aspect it, that it is no longer a Forth implementation but it is more a C++ project in which Forth is part of.

The background is, that Forth is way more useful than using it only as a language. Forth can be understand on a purely theoretical base. That means, without programming the language or without building real CPUs. But I do not want to reveal to much of the coming paper. In contrast to my previous paper about Forth it will be written in English and it will have lots of figures. Perhaps this will increase the audience.

Forth is here to stay

Some while ago i discovered the Forth programming language. The first posting in the blog here is from year 2016. At the beginning Forth makes absolutely no sense, it is so different from a normal programming language and the proposed hardware architecture is also not mainstream compatible. After some introductional paper I get very slowly an understand of Forth. At first, it is not possible to use Forth as an alternative to existing C programming languages. The reason is, that there are no software in Forth available and a hello world program runs in Forth at least 10x slower than with a C compiler. But, Forth main advantage is, that it can be used in computer teaching. If someone wants to know, how a cpu works, and what a compiler is, than Forth is his friend. I would compare Forth not with C but with teaching only languages like PL/0 or PASCAL. Forth has a very clean syntax which is comparable to Assembly.

The best way of formatting sourcecode is given by http://excamera.com/sphinx/fpga-cpu.html Instead of what is used in most books about Forth, the above URL writes the sourcecode from top to down. And a Forth word is equal to an assembly label.

The interesting aspect is here again, that the projects done with Forth are pointless. Writing a program which calculates the greatest divisor can be done in C more easily, and the FPGA software is not able to boot the Linux kernel. Again, Forth was never created to make real processor with it, but it is comparable to the MMIX cpu from Donald E. Knuth. His cpu was also not designed to work as a physical cpu for run real software, but as a tool to explain how a cpu works. In this specific domain of teaching computer science, Forth is top of the pops. That means, there is no better alternative out there. Forth is minimal and powerful at the same time.

Whats next?

The youtube video in the beginning of this blogpost shows a running Forth simulator. It was written not in Forth but in normal C++ together with SFML. This simulator can be extended. It has today many bugs and the layout is not really good. I think, it would make sense to improve the simulator a bit. Not because he can run a program faster than the GCC compiler can, but because of the opposite. The above simulator has currently a speed with around 2 instructions per second. The reason is, that the game loop waits in every iteration for 500 msec. A faster execution makes no sense, because the user want’s to see the program flow. I think, the future of the simulator is not a higher performance, but more visual feedback. The idea is to explain the stackmachine for newbies. The best way to do so, is to understand the simulator as a game. It uses SFML for drawing a window, has a dedicated gameloop and can be used interactively. For example, the user can move the instruction pointer with cursor keypress.

Another problem with the simulator is, that it misses the hardware details. The so called “execution unit” is realized usually with AND, OR, NOT gates. The number of a simple Forth processor is high. The Mup21 cpu has for example 7000 transistors, while the MSL16 (another Forth chips) needs around 175 CLBs. (A CLB is the smallest unit in an FPGA.) In theory, the simulator has to be extended with a zoom function to show all the single transistors. But that is a bit difficult to implement.

# Assembly
main:
  mov ax,bx   ; do something
  mov bx,ax   ; do something
  jnz show
show:
  mov 0,ax
  mov 2,bx


# Forth
: main
  5 1
  +
  .    \ print
: show
  temp @  \ fetch variable
  .       \ print

In the above example listing, Forth is formatted in the same way, Assembly is formatted usually. Not from left to right, but from topdown. Assembly and Forth have much in common. Forth can be seen as some kind of stackmachine assembly language with an educational purpose.

Why Forth?

There are many definitions out there was Forth is. Instead of explaining the language itself, I want to describe Forth as an education tool for understanding assembly language. Let us investigate first, what the mainstream way is to become familiar with the AMD64 architecture. Under the URL https://developer.amd.com/resources/developer-guides-manuals/ there is the official “AMD64 Architecture Programmer’s Manual Volume 1 ..5” which has each around 600 pages. A look into the manual shows us, that it is very complicated. The next option is to read not the reference manual itself, but some of the “Assembly language tuturials” which are explaining how to use NASM for compiling a hello world program in assembly. But after a while the above cited reference manual will also be needed.

The good news is, that there is an alternative to this. It is called Forth, and the introduction is done with the Jonesforth tutorial: https://github.com/AlexandreAbreu/jonesforth/blob/master/jonesforth.S Jonesforth is some kind of tutorial of how to write a Forth system from scratch. The basic idea is to implement in assembly language some high-level macros like “dup” and “NEXT”. That means, after calling NEXT around 3 assembly commands are executed on the PC. The overall system is a Forth system.

From a user perspective, it is not possible to do any useful task with Forth, which can’t be done with normal C programming or normal assembly language too. The advantage of the Forth way of life is, that the language supports the learning of the advanced programmer. I would suggest that it is possible to become familiar with AMD64 in a shorter timeframe, if the idea is to learn Forth and not Assembly. The most important aspect of Forth is, that it is ignoring completly existing architecture. That means, in studing all the 16 registers of current AMD64 architecture and become familiar with the around 1200 opcodes, the programmer learns only the 20 Forth commands and this knowledge can be used to understand any CPU out there. If this positive vision is reachable in reality is unclear, but the idea behind Forth is to do so.

But in one point I want to critize Jonesforth and most other projects. They are to focussed on programming in reality. The goal seems to be to get sourcecode running on real machines. I would go a step back and implement the Forth system not on hardware but on a pen&paper computer, called pushdown automaton. That means, to “boot” Forth not an x86 or on a microcontroller but as a game in Minecraft and SFML. What do i mean with it? The idea is, to draw a pushdown automaton on the screen. This can be called “Animation of a running computer”. It means to visualize the register, or in the case of Forth the stack. And everything what is not in the image visible is simply not existing. That means, there is no hidden Forth interpreter in the background, which takes a word and searches in the dictionary, but there is only the pushdown automaton as a graphical notation.

The reason is, that using Forth for real programs is not the intention. Instead, Forth is a “learning only” computersystem. It makes sense, to use it in a dry run without booting a real microcontroller or a real PC.