Advanced Forth Programming Topics
mosaic-industries.com
mosaic-industries.com
0 = IF foo ELSE bar THEN
Kitten allows you to say: -> x;
if (x = 0):
foo
else:
bar
which I personally find much easier to follow.So, you get local variables, infix expressions, and some amount of non-RPN syntax, without sacrificing any of the power you'd get from another concatenative language.
I've talked to Jon (the creator) a bit off-and-on, and from what I gather there are some really neat things being planned for Kitten in the near future :)
Author of Kitten gives some explanations comparing it with Haskell at https://www.reddit.com/r/programming/comments/4mzlvi/10_less...
I've been thinking about how they could be used with a term-rewriting system for certain kinds of optimization. Some rewrite rules only apply under certain conditions that may or may not be lexically apparent in the absence of an effects system. As a simple example:
rewrite {f map} {g map} compose => {f} {g} compose map ;
This transformation will only work if `f` and `g` are pure functions: if `f` and `g` have side-effects, then the transformation is no longer equivalent. But using the effects system, we can give rewrite rules a sort of context that lets the rewrite system know the extra conditions necessary for a rule to apply.Think that's right. I've been doing a lot of oK, J, Pyth, Forth (working on my own for Z80) for fun and I find it actually very easy to read terse code after a few months. So much that the way you (and most) prefer actually annoys me. It's so much space taken in my field of vision while with terse code I can read entire, quite full and complex programs on 1 screen without scrolling.
Don't worry; I would not say that this is a good idea for programming in teams or maintainable software. I think it is if you have a group of like minded people who prefer this. For me personally however, it makes me very productive and I use the above languages for prototyping now as I get stuff done quicker and with less bugs. I think it's very personal though.
Hooray for kitten lang by the way.
Looking at projects like LuaJIT I'd say programming in teams is, to say the least, an overrated idea per se in so many cases.
<condition> IF <consequent> ELSE <alternative> THEN
is something I still struggle with, even after studying forth (via Leo Brodie's "Starting Forth" and "Thinking Forth") for about 3 months. Something like this I have no trouble with: IF <condition> THEN <consequent> ELSE <alternative>
but the addition of the (optional) local variables helps me to keep track of the stack more easily (if you could even consider it the stack at that point).In general, I don't have much trouble with RPN, but a few of the constructs in forth have eluded me...
<condition> THEN <consequent> ELSE <alternative> IF
would probably be a better order, in the same structure, keeping the same keywords. FORTH ?KNOW IF
HONK!
ELSE
FORTH LEARN!
THENhttp://www.hpmuseum.org/rpl.htm
Not sure if Kitten is directly inspired by this or if they're both building on a common ancestor I haven't heard of.
Having said that, there have historically been attempts to have libraries and create build environment, etc, etc. I'm not aware of any that achieved any universal popularity.
Check this blog out for some cool things you could do with Factor: http://re-factor.blogspot.com/
As pointed out below, FIG Forth has a great deal of support.
All my interactions using Forth have been on underpowered systems pounding on the bare metal. So lots of the words created supported that environment. Math words and geometry words came from FIG Forth.
The standard library is amazing and they (used to) follow a model that has worked well for them (IRRC): the best libraries from the community become part of the distribution. If they aren't that well written, the core guys would help you express the idea better. And then it could make it into the standard library. Or the core guys would just rewrite it for you (Factor is very compact).
On the other hand, if something solving that same problem was already in the standard library, there was no discussion: that problem is already solved.
Happy to see that Factor still lives!
In the open source world, gForth [1] is pretty well-known which comes with a comprehensive set of words [2] many are not part of the ANS Forth standard [3,4]. You could call that gForth's "standard library". For stuff that is missing, gForth has an interface to native system libraries [5].
For math and scientific computing there is the Forth Scientific Library [6] which is written in standard-conforming forth and can be loaded and used on most of the forth systems currently in use.
But then Forth coders somehow live by the philosophy "if you want it to be done right you have to do it yourself", so use of library code is frowned upon and people usually tend to reinvent the wheel and writing code from scratch :)
[1] https://www.gnu.org/software/gforth/
[2] http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/
[3] https://www.taygeta.com/forth/dpans.html
[4] http://www.forth200x.org/documents/forth14-5.pdf
[5] http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/C-In...
[0]: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.5.38...
I dislike syntax which uses the <> symbols, but I'm all for RPN and postfix (in theory anyway).
For me I think the challenge is that the stack-effects are 'hidden' in the code. However, this seems like it could be solvable by compiler (typed stack effects for instance) and tooling/IDE.
I just don't think many programmers fully take into account all the trouble a stack-based language is. You're constantly rearranging the stack to get values stored on it in position to call functions and such. You know stack manipulations are inherently wasteful, and you worry about doing too many of them. So you end up micro-managing them, and often end up sacrificing readability for efficiency. And the readability of stack languages is already woefully bad. I don't really understand why they continue to exist.
It can create image files, but those are not directly executable; they need to be loaded by the Gforth runtime.