[ prog / sol / mona ]

prog


Stack effect type checking

1 2023-10-25 21:52

Concatenative languages, like Forth, have a convention of annotating word definitions with stack effects. For example, if a word like +== pops two things from the stack as "inputs" and pushes one as an "output," you would write a "type signature" (actually just a comment) such as ==( a a -- a ).

A newcomer to concatenative languages might see this and try to interpret these comments as type signatures that the interpreter can check against existing words every time a word is defined. However, this quickly breaks down for words essential to the DIY character of Forth, and necessary for productivity in most other such languages as well.

How far can such stack effect typechecking go before a language ceases to be concatenative?

2 2023-10-26 06:23

What if it pops or puts a variable amount of elements?

3 2023-10-26 07:38

>>2
Yes, that is part of the problem. Does a type system have to forbid that, or what?

I tried real hard with that formatting. Still haven't mastered it.

4 2023-10-28 07:51

>>3
The types of elements popped by word have to follow some rule. Of course you could probably make a word where the most simple way to describe that rule would be the word itself, and I don't know how a type system would handle that. Checking all possible inputs would quickly break down, so I'd assume it would be similar to proving the theorem that the wrong types could never be passed in given the rest of the program. Maybe someone smarter than me could think of something.

5 2023-10-28 15:52

>>4
I thought of something. The compiler should be able to determine a lower bound for how many stack slots will be popped, and how many will be pushed, for any word defined in terms of primitives with known variable stack effects. It could do something as simple as inserting runtime checks before and after these primitives based on the requirements of whatever precedes and follows the variable effect. Not foolproof, but at least it moves a stack underflow error closer to the word that causes it.

6 2023-10-28 21:07

>>5
Maybe useful, but does that even count as a type system?

7 2023-10-29 04:10

>>6
It needs to be accounted for by any attempt to make a concatenative language that has a type system, especially if the contents of the stack can be said to have types.

8 2023-10-29 22:38

It seems to me that a concatenative language could be expressed as a form of function composition f∘g∘h∘... of words, with a recursive stack as their domain; for example + : Num Num [Stack] -> Num [Stack], so that we can match inputs and outputs of adjacent words. Checking the type would be a matter of keeping track of the type of the stack at al times, and to verify that at any point the word in question matches the input.
I don't think variable arguments and dynamic typing could work in this system. Maybe overloaded type signatures, or union types.

9 2023-10-30 21:11

Doesn't Factor has some sort of type system?

10 2023-11-02 05:49

>>9
Dynamic typing, whereas FORTH is basically untyped

11 2023-12-26 23:06

Interesting.

12 2023-12-28 10:56

Forth is fantastic. I really need to spend some time learning it.
https://github.com/cesarblum/sectorforth/

13 2023-12-29 20:14

Scheme is the Forth of Lisps.

14 2023-12-31 02:04

whenever i read the word <forth> i read <fortran>
i do not know why

15 2023-12-31 05:17

>>14
Forthtran when

16 2024-01-04 23:01

ForthLisp when

17 2024-01-05 15:16

>>16
https://github.com/schani/forthlisp

18 2024-01-06 13:29

>>17
Forth is such a great language!

19 2024-01-06 22:24

>>18
Indeed.

20 2024-01-11 18:15

>>17
Great. Now we need LispForth.

21 2024-01-26 18:01

>>17
Neato.
https://github.com/schani/forthlisp/blob/master/lisp.fs

22


VIP:

do not edit these