This is an automated archive made by the Lemmit Bot.

The original was posted on /r/programminglanguages by /u/poemsavvy on 2024-08-22 21:07:38+00:00.


So I had the idea for this language last night where the language itself models state machine interactions and event-based programming in its syntax as opposed to implementing those things yourself in a language.

I came up with a hypothetical language and made a truth machine as an example of what I mean:

def TruthMachine withref inp :: File, out :: File:
    value :: UInt = 0

    Start {
        mut:
            uint(inp.lineRead) -> value_str
        on:
            true -> HandleInput
    }
    HandleInput {
        on:
            value == 0 -> HandleZero
            value == 1 -> HandleOne
            otherwise -> Start // Re-get an input
    }
    HandleZero {
        mut:
            "0" -> out.lineWritten
        on:
            true -> Finish
    }
    HandleOne {
        mut:
            "1" -> out.line
    }
end

TruthMachine tm(stdin, stdout)

Explanation:

  1. Define a type of machine with 4 states and one bit of data. Start in the “Start” state and give its data a default value of 0.
  2. The result of being in the start state is that the data “value” is mutated to be set to the data “lineRead” from the reference to a “File” machine.
    • Note: “File” is a built-in machine that when the line data is requested, freezes the requesting machine’s operation and reads a line.
    • Note: Typically machines function in parallel to each other. This is an exception to allow certain external behaviors.
  3. On “true” in Start (read: immediately after mutation) the machine will transition into the HandleInput state.
  4. The result of being in the HandleInput state is non-existant.
  5. When the data “value” is 0 in the HandleInput state, transition to HandleZero. When the data value is 1, transition to HandleOne. If no transition occurs, then go back to start to change value.
  6. The result of being in HandleZero is that the data “lineWritten” in the reference File “out” is changed to be the string “0”.
    • Note: As stated before, “File” is built-in and adds certain external behaviors. In this case, we write a “0\n” to our passed in File. Inverse of reading.
  7. On “true” in HandleZero, the machine will end functionality - “Finish” state
  8. The result of being in HandleOne is that the data “lineWritten” is changed to be the string “1”
  9. HandleOne state does not transition out of itself, constantly mutating
  10. Create one of these machines and pass a reference to the built-in stdin and stdout files. It will automatically run its code as it will start in its start state.

This gives the typical truth machine behavior of “Input a 0 -> print 0 and stop, input a 1 -> print 1 forever”

This is not quite imperative in terms of how you think about it while programming. It’s also not quite OOP either even though it has references to what could be described as objects in one sense. It is similar to both. However, it’s more declarative - you’re describing machine transitions and “outputs” (data mutation really), and everything acts parallel and independently of each other and automatically runs and figures itself out.

There are ways to do this in various languages, especially functional ones, but is there any language that exists that functions somewhat similar to this in terms of the syntax itself?

The only thing I can think of is hardware descriptor languages like Verilog which describe connections between things rather than the things themselves.