r/compsci • • Nov 09 '22

Is “x' = f(x)” a programming paradigm?

/r/AskProgramming/comments/yq0uq7/is_x_fx_a_programming_paradigm/
90 Upvotes

36 comments sorted by

19

u/hugogrant Nov 09 '22

r/programminglanguages might have better opinions.

I think it's not really a paradigm, but more like design pattern (or the motivation of many patterns). You could be looking at effects or streams depending on what you want. Effects are a functional programming abstraction around state changes, and the details of what "state change" means actually is up to an effect handler. Streams are lists -- actually a data point and a (stateless) function you can invoke to get the next one. Streams would mean that any reader can manually track the value it cares about and, if it's a stream itself, give a range of values per input.

Simply, the premise that you have a before and after state is fraught. When do readers switch to reading the after state? How are they interacting with each other?

5

u/hellotanjent Nov 09 '22

In a hypothetical language that supports x' syntax, there would be a top-level function or object that the language runtime would be responsible for calling repeatedly until the program signalled an exit. For example:

struct MyProgram {
  void update() {
    /*magical stuff with x' and y' and whatever here */
  }
};

And this would be part of the language runtime and not visible to the program itself:

int main(int argc, char\*\* argv) {
  MyProgram p;
  while(!p.exit) {
    p.update();
  }
  return p.exit_code;
}

5

u/hugogrant Nov 09 '22

That's arguably a stream (like what python and rust call an iterator). If you let the stream thing be composable, you could get a pretty library that stacks up all the state changes in a coherent way.

0

u/[deleted] Nov 09 '22

[deleted]

1

u/hellotanjent Nov 09 '22

This is a good reference, thanks!

8

u/eras Nov 09 '22

TLA+ (Temporal Language of Actions) uses syntax foo' to refer to the next value of foo, but it's not really a programming language but a modeling language. In TLA+ can also only ever have one "assignment" to a variable during an atomic step, so the issue you mention is avoided from the start.

5

u/hellotanjent Nov 09 '22

Correct. I want executable TLA+ that looks like C++.

1

u/eras Nov 09 '22

Maybe you'd be interested in PGo, though it's nothing like you asked for ;-), but at least it's in similar domain. They had a talk at the TLA+ Conference.

1

u/hellotanjent Nov 09 '22

Lol, that's going the opposite direction of what I wanted, C++ to PlusCal or TLA+

11

u/Madsy9 Nov 09 '22 edited Nov 09 '22

Seems to me the replies you got so far misunderstood your question, not taking into account your background in hardware RTL design.

What you're asking for isn't atomic operations; it's the total opposite. You're asking for asynchronous signals and the operator in your example works like a parallel RTL assignment ('<=') in Verilog and SystemVerilog. The closest counterpart in a sequential programming language would not be operations like atomic swap but.. threads.

The computation model that HDLs like Verilog targets is vastly different than the model that languages like C++ targets. Programming languages like C++ execute discrete instructions in sequence where the current instruction is fetched from memory via the address from a program counter register. Hardware Description Languages like Verilog, SystemVerilog and VHDL on the other hand models hardware and hardware signals that run asynchronously by default. They are only synchronized at explicit points via latches and flip-flops. So operations modeled in say Verilog might look "atomic", but in reality it's just a bunch of independent signals that are synchronized at the same time. At a clock rising edge, "wires" are sampled and latched in flip-flops at the same time as the old flip-flop value from the model's perspective looks to be available. That's how D-flipflops work in nature.

It sounds like you're trying to simulate Verilog RTL. Look at how the Verilog event model works in the IEEE standard, for example IEEE Standard 1364-2005. And yes, you of course need separate "state" and "state_next" variables for simulation flip-flops and latches, since software does not work the same way as hardware.

TL;DR:

  • The operator you describe is very similar to asynchronous assignment in HDL languages where signals propagate in parallel and are synched up in latches and flip-flops.

  • The execution model for software running on a CPU execute instructions sequentially; parallelism is limited to threads running on multiple cores. The execution model for hardware description languages work totally different than software; where signal propagation has no well-defined order outside of being synchronized at latches/flip-flops.

  • If you want to simulate latches and flip-flops in software, you need to encapsulate that state, which is two variables, "state" and "state_next".

2

u/hellotanjent Nov 09 '22

Yeah, most replies didn't read the whole post.

You seem to have missed the "I wrote a C++ to Verilog translator" bit - I am already deeply familiar with Verilog and non-blocking assignments. "Doing the equivalent of non-blocking assignments in C++ is hard" would be a good summary of a lot of my research.

1

u/Madsy9 Nov 09 '22

I didn't miss it at all, what makes you think I did? On the contrary I focused on that part. I assumed you were very familiar with Verilog, not the opposite. I did not assume you were equally proficient at software programming, though.

1

u/umop_aplsdn Nov 09 '22

1

u/hellotanjent Nov 09 '22

That's the second reference to that paper I've gotten. I'll have to read it in detail this afternoon.

1

u/umop_aplsdn Nov 09 '22

Who gave you the first reference?

1

u/hellotanjent Nov 09 '22

I'd have to dig it out of my reddit inbox.

1

u/umop_aplsdn Nov 09 '22

Thank you. Reason why I am curious is that I am somewhat working on a project to implement a Dedalus compiler, and we would be interested to know who might use it

1

u/hellotanjent Nov 09 '22

Oh, nevermind - reddit somehow gave me two "new reply" notifications for your one comment.

1

u/umop_aplsdn Nov 09 '22

I see, all good

1

u/pigeon768 Nov 09 '22

I think you've discovered Single Static Assignment (SSA) and is a typical pattern in compilers. Let's say we have code that looks like this:

// evaluate ax^2 + bx + c
float f(float a, float b, float c, float x) {
  c += x * b;
  x *= x;
  c += x * a;
  return c;
}

The compiler will take that and convert it into code that looks something like this:

// evaluate ax^2 + bx + c
float f(const float a_1, const float b_1, const float c_1, const float x_1) {
  const float _1 = x_1 * b_1;
  const float c_2 = c_1 + _1;
  const float x_2 = x_1 * x_1;
  const float _2 = x_2 * a_1;
  const float c_3 = c_2 + _2;
  return c_3;
}

Then it does optimizations on that, and only then translate it into assembly or machine code. Lots of optimizations which might be hard are a lot easier when you have SSA form. Dead code elimination is trivial; you form a DAG for return values or assignments to out-of-scope memory addresses, and if an expression isn't in the DAG, you yeet it. Constant propagation is easy. Care has to be taken in loops, but once you've got something that works with a loop, now you can hoist non-loop invariant code out of a loop.

I don't necessarily think it's got a place within the language itself. Many languages already have immutable variables; I'm not going to write x' = x+1; I'm going to write const auto x1 = x+1; or whatever.

1

u/hellotanjent Nov 09 '22

Already familiar with ssa, it works well in single functions but not across whole applications.

1

u/PostFunktionalist Nov 09 '22

I guess: what’s the ask here? There’s nothing that looks like it does it natively but there’s plenty of creative ways to implement it.

In the particular use case of an emulator I dunno tho!!

1

u/AkaiMegami Nov 10 '22 edited Nov 10 '22

In a general case you must discretize the problem (the function f precisely, with a series expansion) to make a sum appear in a program.

If the function is not with a mathematic expression but an input output feedback you may also use the finite difference method.

1

u/agumonkey Nov 11 '22

i'm probably the least educated person here but many concurrency systems separate computation from previous values with effect (mutation or else)

in clojure you send a pure function that expresses the relationship (a,b) (b+1, a) and if the transaction system considers it ready to be accepted then it's executed

0

u/poopatroopa3 Nov 09 '22

Can you elaborate?

-2

u/sylvek Nov 09 '22

a bunch of grizzled old software and hardware veterans

I don't know where you were asking those question, but it seems like neither you nor the veterans are familiar with the state-of-the-art computer science from mid-twentieth century.

You seem to be trying to reinvent the https://en.wikipedia.org/wiki/Moore_machine (published in 1956) or https://en.wikipedia.org/wiki/Mealy_machine (published in 1955). You are just insisting on using different notation that is an outgrowth of https://en.wikipedia.org/wiki/Von_Neumann_architecture (as published in 1945).

Please re-read your computer architecture textbook, you must have missed it or forgot it after the exams.

There is also another answer nearby mentioning Verilog and VHDL. Those are the wheel you are trying to re-invent.

Edit: fixed the links to the Wikipedia

3

u/hellotanjent Nov 09 '22

I appreciate your concern, but I have been programming professionally for over 25 years and literally wrote a tool to translate C++ into Verilog - I am deeply familiar with all the examples you cite and am looking for feedback about the overarching idea of naming the programming paradigm represented by "x' = f(x)" more than implementation details.

0

u/sylvek Nov 09 '22

I don't get it. You wrote 8 dense paragraphs to describe the well-known concept. You could even feed your C++ samples through your C++ to Verilog translator and see that it would normally generate synchronous signal assignments or give an error about asynchronous feedback loop/race condition/undefined behavior.

I don't know that you're trying to sell. Maybe say explicitly what's wrong with the conventional, old-fashioned names already in use. I understand that sometimes the old-style mathematical/algebraical/theoretical vocabulary may be off-putting, but exactly why are you looking for a new name?

1

u/hellotanjent Nov 09 '22

I'm not trying to "sell" anything, though I understand your confusion.

For the last three years I've been researching languages and systems that straddle the software-hardware boundary, and I'm currently writing a paper that will summarize that research and go into much more detail than I'm able to fit in a Reddit post.

As part of that, I feel it's a good idea to ping various programming communities to see if they know of any research or ideas I missed. I don't want to bias the feedback too much by saying "I already have a pile of ideas about this", so I try and phrase the questions to be fairly open-ended. This one might've been too open ended, as there's a lot of confusion about what exactly I'm asking.

0

u/sylvek Nov 09 '22

Thanks for clarification. So, you are basically pre-selling your research paper/monograph about "languages and systems that straddle the software-hardware boundary."

I can't get a feel for your requirements, but I'll give you some more suggestions to read for an inspiration. Check out https://en.wikipedia.org/wiki/Very_long_instruction_word and the related marketing literature. Somebody at Intel essentially re-invented and re-named it EPIC https://en.wikipedia.org/wiki/Explicitly_parallel_instruction_computing and they quite successfully sold it as a "new paradigm."

Personally, the ones I liked the most were from https://en.wikipedia.org/wiki/Multiflow and https://en.wikipedia.org/wiki/Cydrome . They were more convincing than Intel, not only to me but also to numerous other people I used to know and work with.

1

u/hellotanjent Nov 09 '22

Yeah, I remember the whole Itanium and EPIC mess, and was kinda disappointed it didn't work out. I think it would make more sense in the modern world of fast JITs, where a generic assembly language could be rescheduled for the particular pipeline configuration of an EPIC processor.

Look into 'Explicit Data Graph Execution' for another similar architecture that never went anywhere.

1

u/WikiSummarizerBot Nov 09 '22

Moore machine

In the theory of computation, a Moore machine is a finite-state machine whose current output values are determined only by its current state. This is in contrast to a Mealy machine, whose output values are determined both by its current state and by the values of its inputs. Like other finite state machines, in Moore machines, the input typically influences the next state. Thus the input may indirectly influence subsequent outputs, but not the current or immediate output.

Mealy machine

In the theory of computation, a Mealy machine is a finite-state machine whose output values are determined both by its current state and the current inputs. This is in contrast to a Moore machine, whose output values are determined solely by its current state. A Mealy machine is a deterministic finite-state transducer: for each state and input, at most one transition is possible.

[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

-4

u/milleniumsentry Nov 09 '22

Couldn't you store both the old and the new value in one variable? Use half of the bits for one, half for the other, and convert from there..

1

u/[deleted] Nov 09 '22

[removed] — view removed comment

1

u/hellotanjent Nov 09 '22

You've got the basic idea correct. The "replace old with new" step should happen after the entire top-level function has been evaluated, and after the swap the language runtime just evaluates the whole program again. So there's no NewOld, just old and new.

1

u/[deleted] Nov 10 '22

[removed] — view removed comment

1

u/hellotanjent Nov 10 '22

For code intended to run as hardware on FPGAs, loops are forbidden anyhow (there's no hardware equivalent of a for loop).

For code intended only to run as software, loops are OK as long as they don't break old/newness (no writing to an array, then reading it, then writing it again etcetera).

I'll have a paper written eventually that spells out the system in more detail.