[ art / civ / cult / cyb / diy / drg / feels / layer / lit / λ / q / r / sci / sec / tech / w / zzz ] archive provided by lainchan.jp

lainchan archive - /λ/ - 21102

File: 1482722504797.png (35.27 KB, 300x225, cartridge_handic_forth_c64_01.jpg)


So many people like Forth in this site, yet there is no Forth general.
Let's talk about Forth!
Share code, ask questions, talk about Forth

What do you like about Forth?
What dialect are you using?
What implementation do you use?
What have to made in Forth?



>What do you like about Forth?

Haven't used it much. Typed in a few things on a Sparcstation 10 boot prom to mess around, and I did write a few programs in the old RedPower mod for minecraft (it had forth-based computers), but that's it.

>What dialect are you using?

>What implementation do you use?

I was using a machine forth for a virtual 6502 in Minecraft.

>What have to made in Forth?

I made several different little automation things. A basic nuclear reactor controller (using IndustrialCraft's reactors), a sorting system, a frame-based door, and a few other things. Mostly I just played around with building more complex functions based on simpler ones. I usually program top-down, but forth is great for bottom-up programming.

It beats the hell out of Lua, which is what every other Minecraft computer mod seems to use.

I've got a few old Sparcstations in the closet, maybe I should pull one out and play with the forth on it.


>So many people like Forth in this site, yet there is no Forth general.
I was using the lower levels thread and discussing it in other threads, but the urge to make a general did come up every now and again.

>What do you like about Forth?

I mostly like Forth's simplicity. I also like that it exemplifies different ideas in programming, namely the stack machine.
All of my (good) calculators are stack machines, including several with RPL, which is similar to Forth in many ways.
>What dialect are you using?
I usually use ANSI Forth for things, but it has many complexity issues I don't care for. I don't like CHARS and CELLS, but feel that I should attempt to write portable code if I can, which makes my programs ugly and inelegant to my eyes.
I intend to buy a GA144 soon and use Colorforth on it, which would be much more fun to use and post programs for.
>What implementation do you use?
I halfheartedly use Gforth, but it's complex; SEE is basically useless. I want to use the aforementioned GA144 Colorforth.
>What have to made in Forth?
I don't have any hardware I can target with a Forth program, so I'm stuck implementing esoteric languages I've made efficiently. I'm working on a development tool and want to release a version of it written in a Forth, along with a few other languages, soon. For my sanity, I'll probably target a subset of the core and core extension wordsets of ANSI Forth while also assuming eight bit characters with thirty two bit cell sizes.


Hah. I was also first introduced to Forth by RedPower. The pun on it being stack-based always amuses me.

I like how different reasoning on a stack is. I've spent a little bit of time solving generic exercises just for the brain gymnastics of it.
I used ANSI, GForth. Never really made anything concrete with it, tbh I don't think it's really worthwhile to do so.

Is Forth still in use today? I read that it used to be relatively common once upon a time, especially for embedded systems, but those are all on C nowadays, if not on higher level languages even.


Yes, but its use is very limited iirc. However, it does see some use in Aerospace. The Rosetta probe's Philae lander which landed on a comet was entirely programmed in Forth.


Forth is unlike any other programming language, you can write your own implementation of Forth in less than a thousand lines of asm. So its really stupid to ask about dialect or implementation of a language which is so low level that it does not obey any high level abstractions.

Forth's window of opportunity to become great came and went. In the late 70s to late 80s Forth would have been the perfect shell language for personal computers. Instead BASIC was the defacto shell language, which is really a shame because everything about BASIC is gimped. BASIC is just a high level simulator of assembly. Using goto's as the main construct in a high level language is/was moronic. Forth is able to run in tiny memory (4k) like BASIC but has high level abstractions on the same level as Lisp but directly accessed hardware memory like asm. It truely was an operating system as much as it was a programming language or shell language. Forth is still used by embedded programmers to test out electronics because of its high level ease and operating system-like capabilities. We dont use computers anymore that directly access hardware so we really dont need Forth anymore unless you do systems/embedded programming.


>So its really stupid to ask about dialect or implementation of a language which is so low level that it does not obey any high level abstractions.
There ARE different dialects of forth you know that? colorforth and 8th to name a few. Just like there were dialects of lisp where lisp was a model for languages past 1.5
However I am aware that forth is best used as a model for embedded systems and low-level programming. ANS Forth is too complex, even Moore decided it grew way too complex so he moved on to colorforth. That's part of the beauty of it, you can implement it in your device without the overhead of the extension features and portability issues.

>We dont use computers anymore that directly access hardware so we really dont need Forth anymore unless you do systems/embedded programming.

I beg to differ. Sure, most of the time we deal with high level abstractions, but sometimes the libraries available just won't do the job. I like the fact that it can interact closely with the operating system where it's hosted, as opposed to being isolated from it and needing layers of FFIs on top. If I can interact with a library with native C-style data, then I can bend it to work with the application at hand. Kind of how all libraries are directly available to C.
Sure enough, this is not directly accessing hardware, but the scope of forth shines in this kind of problem.


File: 1482992105460.png (1.44 MB, 200x200, PRG76.pdf)

Oh sweet, I just bought a ga144 a couple days ago. It's nice because you don't have to pay $400 for a board or solder anything with:

although I'll still have to power it and do some housekeeping.

I don't use forth much but I've been growing more interested in greenarrays processors because of how parallel they are. 144 asynchronous nodes is just what I've been needing to try implementing a computing system based on concurrent graph rewriting and Jose Meseguer's work on rewriting logic. Also reading about all the crazy power saving decisions that they make is crazy.


Where the fuarrrk is Chuck Moore?

His website is dead.


colorforth site has been down a whiles now.


I bought the board-and-GA144 combo a few months ago and soldered it myself with this [0] soldering iron. I used a toy microscope to check the joints afterwards and redo some connections that I missed the first time around. It took me about 30-45 minutes to do.

I haven't touched it since, so I'm not sure that I did it right. Overall, it's probably worthwhile to buy the soldering service.


Forgot my soldering iron link. Here it is:


Here's a list of currently-maintained linux-compatible Forths and Forth-alikes that I know of:
gforth: https://www.gnu.org/software/gforth/
4th: https://thebeez.home.xs4all.nl/4tH/index.html
muFORTH: http://muforth.nimblemachines.com/
retro: http://forthworks.com/retro/12/
factor: http://factorcode.org/
pforth: http://www.softsynth.com/pforth/


How much was the board? Did you buy it directly from GreenArrays?


I didn't buy the development board from GreenArrays, I bought the bare breakout board and GA144f18a from Schmartboard. [0] It costs 35 USD. This is the same thing that >21171 bought. For an extra 3 USD, SchmartBoard will solder the IC for you.

At a minimum the board still needs power, memory, and a serial line attached to it.

[0] http://schmartboard.com/schmartboard-ez-qfn-88-pins-0-4mm-pitch-2-x-2-grid-bundled-with-a-greenarrays-ga144-ic-202-0048-02/


kitten: https://github.com/evincarofautumn/kitten
HN discussion: https://news.ycombinator.com/item?id=13345832

Concatenative functional language with syntax more like the ALGOL family. (i.e. C, C++, Java, etc.)


gForth wins the cake for me, Factor it's unnecessary complex and the others are useless.
I always wanted to try colorforth, but it is dead now?


File: 1486263771377.png (545.22 KB, 163x200, 1363439772199.png)


challenge: make the case why forth should be someone's first language and why it shouldn't be.


>why it should be
Easy to start, easy to learn, easy to use
>why it shouldn't
Stack machines are almost unexistant today.


It's very simple, dialects such as colorForth make it even simpler.
It can be made high level, but begins at a very low level and so it's a so-called wide language.


It doesn't resemble other languages, which some see as a bad thing.
It's probably not going to beat a domain-specific language if one already exists and is suitable. As an example, I wouldn't use Forth for heavy mathematical operations if I could use a language that already implemented arbitrary length arithmetic and whatnot.



Hey am making my own forth os due to how easy it is, no ansi because it overcomplicates things.

Also, have you guys checked out ultratechnology page? It has tons of forth articles from some guy who worked with Chuck Moore before he went to do GreenArrays.


Almost every modern VM is based on a stack machine.


Will you release the source? I'm very interested in OS development and Forth. It would be awesome to take a look at your work.



Probably will after am done.
If you're interested in Forth OS' check out these:

Its actually amazing how back then people wrote their own OS and just rolled their own Forth, kinda sad looking at how little resources there are on OS development and the ones out there are just a confusing mess(looking at you OSdev) when in Forth its just get your bootloader, jump into protected mode and implement the 20 or so primitives to get the compiler, interpreter and debugger done.


So I've been using forth but I still don't know how the metaprogramming thing works.
I ran into a problem where I need multiple exits from a loop, like so
( stuff )
( flag ) if ( I want out of the loop here ) then
( another flag ) if ( another exit here ) then
I've tried with begin..while..while..repeat but it's not legal, and I looked at words like exit and abort but those don't seem to work for what I need (exit breaks out of the function (word), next only works in a do..loop and abort signals an error).
Any pointers on how I might get this behaviour?


begin ( stuff ) ( flag ) if ( I want out of the loop here ) then ( another flag ) if ( another exit here ) then again

>I've tried with begin..while..while..repeat but it's not legal

Control flow words have compile time stack effects, yes.

>and I looked at words like exit and abort but those don't seem to work for what I need (exit breaks out of the function (word), next only works in a do..loop and abort signals an error).

Have you considered splitting this word into more words, which may allow you to use exit?
Have you looked into using begin ... until?


Like >>21898 said or use leave if you're using any ansi forth.
It works like a break in C.


>Have you looked into using begin ... until?
yes but that still uses only one conditional, I need two.
I could also wrap the loop into a function and just use exit, but I think it'd obscure what's going on and that's not how these super flexible programming languages are supposed to be used right?


>yes but that still uses only one conditional, I need two.
It's possible to design your own conditional words. You could, as an example, create a word like until, except it leaves information on the compile stack for another word like it.

>I could also wrap the loop into a function and just use exit, but I think it'd obscure what's going on

Does all of this fit on a short line? If not, it's already becoming obscured. Good Forth is written with small words composed.

>that's not how these super flexible programming languages are supposed to be used right?

It would greatly help if you would actually show your program or a part of it. It's not easy to tell exactly what you're wanting to do.


As I learned to program, I couldn't wrap my head around the common languages, so I tried to learn forth, it made sense, its very hard to learn to program in though, because small community and no libraries etc, so you can't do any cool stuff easily, so its discouraging. But I learned a lot, it was a huge missing piece in my understanding of concepts in other languages, how they were designed. I 100% believe everyone should read a forth book, the simple one from Charles Moore(?) should do.

I don't entirely understand your question, but according to docs "again" is not a word in forth.

>that's not how these super flexible programming languages are supposed to be used right?
do what works for you unless someone is paying you :D


I don't remember the exact details rn but it was a sieve.
create #found 1 cells allot ( number of primes found so far )
create primelist 1000 cells allot
\ stuff
: prime? ( n -- flag )
( some preparation words )
N dup * i > if ( it's a prime, put 1 and leave ) then
N i mod 0= if ( not a prime put 0 and leave ) then
( next prime goes in i )
( cleanup ) ;
I simplified it to N and i though that was kind of dealt with some stack boilerplate which rendered the preparation and cleanup.
For some context, i would be the prime, taken from an offset from primelist, updating looks something like
( with the pointer to the current prime at TOS )
cell+ dup @
( or rather cell+ here and dup @ when I need the value itself )
On hindsight, I can see that without such boilerplate I could just have used exit. But I don't want to create variables N and i for just a function you know?
If you have better ideas let me know.

sorry, s/again/repeat/
>do what works for you unless someone is paying you :D
no m8, I insist of writing the best code I can :)


Is there any way at all to have a multi-threaded flow of execution in FORTH? Has anybody ever done any speculation on parallel FORTH?


I've tried to trick gforth using ' ( and failed miserably.
Rethinking about the problem, maybe I've not fully understand your question: what's N; where do you put 1 and 0; you need two if? if it's prime, how can it be not prime? why checking?; why do you want primes in i when you can use primelist?.
Sorry if I didn't understood because of the english (2nd language here)


Bump, I'm interested too


sorry, I didn't realize I was that unclear. I wrote the function from memory and it seems I omitted important details that were clear for me. I also had some errors in my code.
Anyway, thinking about the problem (it certainly helps to forget about it for a while!) and I realized that indeed I could just use `exit' to break out of the whole function, I'd just been carrying issues that arised from previous solution attempts. Here is the `prime?' word as it stands now:
create primelist 100 cells allot ( space for 100 primes )
2 primelist ! ( our first prime yay! )
: prime? ( N -- )
primelist ( N addr )
over over @ ( N addr N i )
dup * < if 2drop -1 exit then \ it's prime!
over over @
mod 0= if 2drop 0 exit then \ it's composite
cell+ ( N addr+1 )
again ;
So now
3 prime .   \ prints -1
4 prime . \ prints 0
5 prime . \ prints -1
15 prime . \ prints -1 whoopsie!
last case is because I'll be using another function to add the primes
\ This goes at the top of the code
1 #found !

: addprime ( n -- )
primelist #found @ cells + !
1 #found +! ;
: dummy ( n -- ) 
10 3 do
i prime? if i addprime then
loop ;
\ primelist shoudl now hold 2 3 5 7
15 prime? . \ prints 0, yay!
And that's my sieve.


That code is far too long to be good Forth. You should split that into helper words.

>On hindsight, I can see that without such boilerplate I could just have used exit. But I don't want to create variables N and i for just a function you know?

If you have better ideas let me know.
I'm not seeing why you'd need to create variables to do this.

Refactoring is a big part of writing good Forth. Don't salvage, rewrite.


I don't see any more succint way to make a sieve honestly.
I could either factor the `over over @' part that gets repeated into a word but that'll save exactly 0 lines.
I could instead drop the `dup square' conditional and save 2 lines and sacrifice quite a deal of efficiency, I'd rather have those 2 lines.
I know that the forth culture is to have the source in 4 lines of code but, honestly, fuarrrk that.
In sum, I'm using 16 lines for the helper functions and I'd be using about 5 lines for the loop that'll find N primes, and 1 to fetch the Nth prime:
: Ineedthis
dup prime? if dup addprime then
#found @ 1000 > until ;
primelist #found @ 1 - cells + @ . \ get the last prime
total: 22 lines to get the 1000th prime.
Again, if you have any better idea, it'd be great.


nevermind, I added some stuff to make the solution less obscure (I also had an error in that last listing) and it added up to 28 LOC, a disgrace of Forth code lol, I may refactor but I'm happy to have solved it anyway.



Minimal Forth is broken, too bad, I was very curious...



The Koopman book is really interesting, it provides with a good mental model of the stack machine. It also answered some questions that always stayed at the back of my head ("how can you add two numbers if you only have access to the top element of the stack?" answer is that you actually can access the top two elements at any time, and even if you didn't, you can use the return stack as temp storage).


>What do you like about Forth?
The way of abstraction.Each instruction(word) is the building block for another instruction.An instruction can be whatever you want to define .
>What dialect are you using?
>What implementation do you use?
gforth on linux
>What have to made in Forth?
Just playing around for brain gymnastics.Implemented a few data structures(arrays, dynamic arrays, linked lists,arrays of strings....) small stuff.


>What do you like about Forth?
RPN notation and colon definitions.
>What dialect are you using?
Mine, and that's awesome!
>What implementation do you use?
gForth on Arch Linux and Android
>What have to made in Forth?
A bulls & cows solver, a Shannon entropy calculator for books and various mental gymnastic.

>What do you want for Forth?

Much clear documentation, tutorial and libraries. I want a modern, general use, dialect of Forth, that it's used extensively.


>why it should be the first language
Forth will teach(force) you to break the problem in smaller ones. As a beginner you will learn better how to grasp the notions of "computation" and "computer instruction."(forth case: "word")
>why forth shouldn't be the first language:
Implementing algorithms in gForth can be a pain in butt .In fact, implementing simple notions as "collections of data"(arrays) can be very hard.No, declaring a variable and moving the stack pointer with 'allot' it's not a real array, but it can be used as an array if you need only one array.



Chances are if forth seems hard to you its because you're doing it wrong, something like trying to make forth behave like C or some other language.


I'm not a Forth expert, in fact I'm just a beginner but, I think Forth encourages using abstractions for those things through words. In that sense an array would indeed be referenced by an address (pointer in C parlance) but words would make this transparent.
10 0 array \ this could allocate an array of 10 zeroes
dup 5 th 8 ! \ store an 8 at position 5
dup 3 th \ get the third element
dup .arr \ print the contents
\ and so on
Only problem I have with forth in this respect is that you don't get to allocate things temporarily without making a mess, or so I've come to believe.


Re-read my post as in response for the initial question.Forth is not hard to learn.It's hard to do stuff with it.Especially taught to beginners who need to learn about algorithmic thinking, data collections, common sense stuff . As a first language to learn, it has advantages and also disadvantages.


wrote an implementation similar to this.was something like:
array myarray \ declaring myarray as an array
12 myarray 0 <- \ store 12 at the first cell
25 myarray 1 <- \ store 25 at the second cell
myarray 1 -> \ get the value from the second cell
34 myarray 2 <- \ store 34


\ That's how I make array, they works fine!
: array ( dim — )
create dup , cells allot
does> ( n array — *array[n] )
swap 1+ cells + ;
\ Array starts at 0, -1 is the length


it doesn't have a rich culture of libraries. It has only:
1. theforth.net - could be great, one day. not today.
2. FFL. Good for whippiuptidude actually. Some code is quite poor.
3. FSL.
but if you are willing to write the library with the app, without getting dragged into too general of a library, you can do stuff with Forth just fine. Data structures are not hard. They just aren't given to you. Try this: whenever you want a data structure, use a homogeneous linked list. Do that all the time. When it becomes so inappropriate that you're willing to replace it even at the expense of learning how to write the more useful structure yourself, do that -- and be amazed at how placidly your code accepts the change. A slow ass randomly-accessed assoc list got replaced with a julia table? And you rewrote none of the code that used it? wtf you like Forth now.


I am highly interested in how forth behaves under the pressure of more serious work (so far, I've done nothing but solve Project Euler problems).
One thing that I've noted about Forth is that it has in it's nature to be better tailored to specific solutions rather than more general ones.
Of course, your specific solution could be to make a general version of a data structure, but that'd only be warranted when you actually need such generality.
I like that part about the culture of Forth, to adress the problem itself, rather than use the way of the library that is kind of plaguing programming (looking at you python)


having a lot of fun with durexforth under vice

i highly recommend it to anyone wanting to learn forth


Why would you want to use a Forth in an emulator?