Hacker News new | past | comments | ask | show | jobs | submit login
Writing a Forth in Haskell (reinvanderwoerd.nl)
127 points by reinvdwoerd on June 8, 2017 | hide | past | favorite | 52 comments



Surely you just take an existing LISP implementation and call "reverse" at the correct location in the code? :p


Forth uses a stack machine. In this way, since calls to functions are really simple and easy, higher performance can be achieved.

I love Lisp, but what you suggest is not really Forth and will also not give you the performance benefits and simplicity of implementation that Forth has.


I think he was joking.


:)


Even at the most basic level, Forth and lisp are quite different. Lisp has grouping and evaluates using a tree. forth has no grouping* and evaluates completely linearly.

* well you can hack it to give it grouping, but usually it doesn't.


Lisp's abstract model doesn't use stacks to pass arguments. Arguments are just lexical variables and they live in environments like other lexical variables: completely foreign stuff to Forth. Environments often have to survive the termination of a lexical scope's execution; it is required when a closure has escaped. So things can't even be compiled to a stack-based machine, using a "pop everything when returning" strategy: only in some cases.

(ANSI) Lisp function calls are safe w.r.t. wrong number of arguments, and support optional and variadic arguments also. This is true of compiled code without source: when we load someone's compiled file and call a function in it with the wrong number of arguments, it is diagnosed.

Forth and Lisp are so miles apart, that I have to scratch my head why a comparison comes up in discussion from time to time.

(It is pretty much always from someone who uses Forth and doesn't know Lisp (or, worse, anything that isn't Forth). "Hey, I heard Lisp is also interactive and meta-programmable, so it must just be a Forth with postfix switched to prefix and parentheses added ...").


I had this dumb idea of actually evaluating LISP in a forth inspired way. It would be a stack of subroutine/accumulator pairs. so given

    (+ 1 2 3)
"(" would pop an pair on a stack, + would set its subroutine, and 1 2 3 would be fed to the subroutine and accumulate a value. when ) is encountered, the data is fed into the subroutine/accumulator pair lower on the stack, so you could still have nesting.

Horribly inefficient, but I thought the idea was neat. I'm sure I'm not the first to think of it though.


some antique famous lisper (Henry .. forgot last name) did write a linear lisp that works by stack evaluation. From the few that I grasped, since everything is mostly duped, it's like a new value each time; free borrowing ?


Higher-level concatenative languages like Factor do feel somewhat Lisp-like because most code is structured with quotations and combinators. But typical Forth isn’t very much like typical Factor or Joy at all, even though they’re all nominally concatenative.


Or in other descriptive terms, Forth could be called "point-free" or "tacit". There is no structure given by the code's context, no named arguments to describe what is in scope - all you have is what is on the stack at the moment of execution, and global variables that the program might access by convention.

Point-free can be a very concise and flexible strategy but also error-prone since (in Forth) it allows the stack to leak and consume/return an unbalanced quantity of arguments. This class of error is eliminated within the syntax of Algol style languages languages but can be reproduced easily if you build your own stack machine.


Can anyone point me to a Forth implementation where the absolute bare minimum is written in (C/assembly/whatever) and everything possible is then bootstrapped in Forth itself?

I recall reading what the minimal word set needed to be able to write the rest of standard Forth (Fig Forth if I recall) but I seem to remember that most implementations don't push the purity quite that far for performance reasons.



Self promotion: https://github.com/zevv/zForth

zForth is yet another Forth, but with some special features not found in most other forths. Note that zForth was written for engineers, not for language purists or Forth aficionados. Its main intention is to be a lightweight scripting language for extending embedded applications on small microprocessors. It is not particularly fast, but should be easy to integrate on any platform with a few kB's of ROM and RAM.


https://github.com/tehologist/forthkit

I did exactly this, was curious if I could write an outer and inner interpreter in pure c in under 500 lines with only 5 c functions. I then used that to boot strap an image that can run using only the inner interpreter.

http://chiselapp.com/user/tehologist/repository/compc/index

Also several experimental versions. Final version includes a pdf that documents how the c code works.


In addition to what others have linked, look up "moving forth," which is a series on porting forth... It covers the low-level bits that you'd need to port to run Forth on a new platform.


https://github.com/anse1/firmforth

has not been mentioned on hn afaik.


The very neat part about firmForth is that if you compile the firmForth JIT with cparser (libfirm compiler), it can inline C functions into the JITted code.


On an unrelated note, this is a pretty nicely designed website, OP.


I think it's also valuable to take a look at how Joy handles quoting program fragments. I just wish there were better online resources around the language.


Links? When I tried I just used "[" to construct an empty dynamic array, and pushed words into it until I found "]". Very inefficient, but simple.


Doesn't really give Forth the credit it deserves. Totally misses the point of Forth: the simplicity of the environment and it's relationship to the underlying hardware, defining primitives like : and VARIABLE with CREATE and DOES>, threaded code, , (COMMA) , ... Nice promotion of Haskell though


You're right of course, this is an ongoing exploration. I hope to learn the deeper truths in the process.


I would suggest re-doing the exercise on an 8 bit cpu with < 8 K of RAM. That will open your eyes to what is so special about Forth.


One good place to look for that is <http://git.annexia.org/?p=jonesforth.git;a=summary>, previously discussed at <https://news.ycombinator.com/item?id=942684>.


Thanks for the links, had not seen this before.

(though your links are slightly broken)


[Disclosure: I'm the author of JONESFORTH]

If you search on github there are several clones and indeed forks on JONESFORTH. It is public domain, so I welcome that:

https://github.com/search?utf8=%E2%9C%93&q=jonesforth&type=

There are also versions ported to several other architectures, which I occasionally announce on my blog (when people email me about it :-)

https://rwmj.wordpress.com/?s=jonesforth


Thank you as JonesForth keeps getting hard to find. Any chance you will ever do an even more in-depth tutorial? Put it on lulu and I will buy. I'd like to write my own Forth, but need a bit more handholding.


If the tutorial is hard to find, please feel free to clone it on the git sites of your choice.

In depth: Likely not. I've actually been working on another literate language tutorial (completely unrelated to FORTH), so ... watch this space (or my blog).


Good advice...yea I remember you saying you weren't using Forth anymore a year ago. What new language are you looking at?


The new language is also a toy, but it's based on SML, Lisp and others.


Gotcha. I was originally assuming OCaml based off of your site.


I'm curious about Forth, as it's probably the most important language I've never learned. I've done a fair bit of low-level, micro-optimised coding in C and assembly, but don't get much time for that kind of thing these days.

How would you describe Forth in relation to those? What makes it stand out and what are its weaknesses?


Forth is probably the smallest true high level language there is. It is entirely self-contained, needs just about nothing in terms of hardware support and can run in as little as 2K of memory.

It is well suited to things like real-time control, you'll probably have a hard time getting used to stack manipulation (especially in the beginning) and it tends to keep you up all night (not sure if that is a strength or a weakness ;) ).

It will also be the most fun you've had with a computer in a long time and it will likely make you look at the rest of what we do with computers as clunky in the extreme.

What Forth is not really suitable for is large projects and things built with a team. It is more of an artisanal thing, something closer to watchmaking or jewelery than major construction.

If you want to know more about why Forth is the way it is you could do worse than to start with studying the life of Chuck Moore for a bit, the language and its author are roughly equally interesting and for want of a better word peculiar.

One thing Forth is not: wasteful.


I remember writing a fairly trivial program in Forth on a ZX81 for a school open day. You needed the 16K RAM pack thing with the usual lump of Blu Tac to stop it wobbling.

When I "discovered" Forth, it seemed like science fiction (soo fast!) to me after BASIC juddering around the screen.


Probably worth mentioning the Jupiter Ace here[0] - a ZX81/Spectrum era computer which had Forth in ROM, instead of BASIC which was common at the time. The basic model only had 1K of RAM. I never used one but I remember when it came out, reviewers commented on its speed and memory efficiency.

[0]: https://en.wikipedia.org/wiki/Jupiter_Ace


"It will also be the most fun you've had with a computer in a long time and it will likely make you look at the rest of what we do with computers as clunky in the extreme."

Double true if one looks at the hardware and software Chuck Moore makes with it.


Can you tell me the link?



http://www.greenarraychips.com/ is his current company, mostly notable for extremely low-power chips using asynchronous circuits (i.e. there's no central clock). IIRC he develops the CAD software he uses to design chips on his own chips.


Read Chuck talk about OKad...when the hardware wasn't to his liking he built his own chips...after writing his own CAD software in only like 5k loc that runs waay faster than traditional CAD software and uses almost no resources.


Do you think that a concatenative language could be suitable for a team to use?


Last time i did some coding in forth, it was Fig-Forth on the 8-bit Atari 800XL. It was really fun and I was amazing on how fast it could run despite being in such limited hardware.

I would say Forth is easier to use than assembly, and perhaps much more flexible/extensible than C, although in some senses lower level since C gives you a very nice, ordered way of specifying what are the arguments to your functions/etc.


In addition to other comments:

Forth is a really cool language, but has the pitfall of trading time for space. It's a very lean language, and incredibly dynamic, but its heavy reliance on jumps makes it run slowly on modern hardware and thrashes most branch predictors.


The branch prediction problem is true for every language that doesn't JIT (pronounced: "cheat"). Furthermore, Forth is not limited to threaded code. You can have some simple code generation (subroutine threaded code+some primitive inlining) if you really want to prioritize speed. Finally, Forth is a simple language with simple implementations so it's often easy to add the primitive(s) you need in order to deal with the bottlenecks.


Modern CPUs are optimized for C code. This is not the fault of FORTH, but it certainly is a problem for its adoption.


This is why Chuck Moore designed his own chips like GreenArrays I believe. There is no impedance mismatch between software and hardware and the entire thing is optimized for the problem domain and massively parallel. I'm sure you can get more speed out of C, but it no other language (even Lisp or Smalltalk) can the user understand the entirety of the system. Lisp machines and Smalltalk machines probably are about as close as possible, but I have zero experience with them personally. Perhaps the project to put PicoLisp on bare-metal qualifies if one uses a simple enough microcontroller as the language itself is about as simple as you can get a practical lisp.


Over the last twenty years or so, I have written serious Forth code generators for six CPU architectures. There's only one code generation algorithm that's not regularly present in most Pasgol family compilers. The results are the VFX Forth systems at http://www.mpeforth.com.


I've looked over your products over the years and been super impressed by your results. Thanks for all your hard work!


Not really, modern Lisps have done a fine job optimizing performance. FORTH suffers from treating the hardware too granularly, and thus not being able to take advantage of any of the advances in hardware in the last 2 decades.


There is no a priori reason FORTH can't take advantage of modern hardware, since in the extreme case the compiler can be completely replaced by redefining the meaning of ':' and ';'.


Feel free to have fun with Haskell and Forth all you like. Your project. :) However, you need to learn C and/or ASM then implement Forth and some system codeto get to the deeper truths. Probably. Quite a bit of people using it in embedded systems.


If you can get DOES> working, you really understand how Forth works. I have a toy Forth implementation where I got DOES> working, and I still have a hard time understanding how I got it working.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: