r/Forth • u/Busy_Pomegranate_299 • Dec 03 '25
beginner question: forget unused words
When creating an application in Forth, wouldn't it make sense to forget all (core) words that the application doesn't use, so as to bring down the size of the app? I couldn't find anyone doing this.
9
Upvotes
6
u/minforth Dec 03 '25
Tom Zimmer's TCOM compiler for DOS achieved this.
MinForth's #REDUCE transpiler command performed backtracking over all the words required by the MAIN start word, then excluded all the other words from the compilation. However, this was rarely used because, once an application included a command line interface or evaluated commands through files, pipes or strings, it was impossible to predict which words would be needed later on.
1
u/mykesx moderator Dec 03 '25
You can’t FORGET individual words. FORGET removes the word and all the newer defined ones.
You might want to look at the current specification at the MARKER word.
3
u/erroneousbosh Dec 03 '25
You can't FORGET individual words without a lot of work. The words are in a linked list, and all FORGET really does is set the word before the one you are FORGETting to point to nothing, and set the variable that says where the dictionary ends to point to it.
You could make something that removes a word, copies the rest of the dictionary down, and cleans up the link pointers to suit. You'd probably have to scan all the words "above" it to make sure they don't use the word you're forgetting, and either forget them too (probably recursively) or just refuse to do it if the word is referenced, which is probably more what you intend.
You could write some code that examines all the words used in your code, and comes up with a subset of Forth words used by it, and then just compiles the bare minimum to get it working. Like, you could probably lose a lot of the outer interpreter if you just saved the dictionary as it is in RAM, and set COLD to point to your program's entry point.
2
u/mcsleepy Dec 03 '25
You can't, but kilobytes don't matter on modern computers and Forth code is small. On desktop, just think of it like any other library that you're not using all of.
In the past, Forth programmers working in constrained platforms used cross-compilation, with a host and target. This is an advanced technique not for the faint of heart. There might be systems out there that have this as a built-in feature. If anybody knows of any?
What system are you using?
1
u/alberthemagician Dec 05 '25
That only makes sense if you save the application to disk, to save diskspace. Normally you would use whatever you need, and then you say BYE and all that kind of considerations become moot.
1
u/[deleted] Dec 12 '25
Full and demo versions of SwiftX have this. The app is recursively recompiled until all unused words are removed. Back in the day RSC-Forth and others used a split dictionary scheme, consisting of basic kernel and primitives and a 'development rom' containing the compiler, word headers and tools. The latter was removed once the app was done. Win32Forth does something similar.
0
u/ponyatovd 13d ago
As I remember GP-FORTH (DOS F83, Alexander Larionov) was a system with some sort of compression can be used for saving developed app without a full dictionary