r/ProgrammingLanguages • • Nov 09 '22

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

/r/AskProgramming/comments/yq0uq7/is_x_fx_a_programming_paradigm/
23 Upvotes

34 comments sorted by

24

u/PurpleUpbeat2820 Nov 09 '22

To put it more concisely, a program in the form "x' = f(x)" has no good representation in the software programming languages we use today.

In purely functional style you can literally write:

let x' = f(x)

There really isn't any software-oriented language out there that allows for global atomic state change to be both performant (the compiler understands the distinction between "old" and "new" and can reorder code to avoid excessive copies or temporaries) and unambiguous (the distinction between an "old" value and a "new" value has some explicit representation in the language syntax).

Depends entirely upon the type of x. If it is a tuple of machine-precision numeric values as in you example then it can be done extremely efficiently. If it is a dictionary you're screwed.

Related ideas are RC with in-place mutation for objects with counts of 1 and Perceus in Koka.

7

u/hellotanjent Nov 09 '22

Sure, but what if I need to apply F to X a few billion times to compute my result?

The Perceus paper is interesting and wasn't already in my research pile, particular the statement "The in-place update optimization is guaranteed, which leads to a new programming paradigm that we call FBIP". Thanks for the link.

3

u/PurpleUpbeat2820 Nov 09 '22

Sure, but what if I need to apply F to X a few billion times to compute my result?

Should be fine. Why do you ask?

3

u/hellotanjent Nov 09 '22

What does the code look like?

2

u/PurpleUpbeat2820 Nov 09 '22

Lots of different ways to write it. Using a nest combinator to compute f(f(f(x))):

nest 3000000000 f x

In my language you might use the for function:

for 0 1 3000000000 x [x, _ → f(x)]

That's like:

for(int64 i=0L; i<3000000000L; i+=1)
  x = f(x);

2

u/hellotanjent Nov 09 '22

Ok, that seems reasonable as an example of the "driver" of the language model. Is your preferred language smart enough to update x in place if you use "nest"?

3

u/PurpleUpbeat2820 Nov 09 '22

Is your preferred language smart enough to update x in place if you use "nest"?

No. None of the languages I like would be smart enough to update in place. This feature isn't a priority for me. Sounds like Koka would do it though.

1

u/jonathancast globalscript Nov 09 '22

iterate f x !! (5 * 109)

3

u/hellotanjent Nov 09 '22

OK, skimmed through the Perceus paper and looked at a few Koka examples. It seems like FBIP is more of a back-end optimization instead of something like an aspect of the language syntax, though I could be misreading it.

3

u/PurpleUpbeat2820 Nov 09 '22

It seems like FBIP is more of a back-end optimization instead of something like an aspect of the language syntax, though I could be misreading it.

Yeah. Its the nearest kind of thing I've heard of but still doesn't quite satisfy the requirements because you're free to use old versions.

2

u/phischu Effekt Nov 09 '22

For some people, tail call elimination is a back-end optimization, but for me tail calls are a language features that confusingly uses the same syntax as function calls.

They argue the same holds for FBIP: it is so reliable and so obvious when it happens that you can structure your program around it.

3

u/sintrastes Nov 09 '22

Maybe I'm misunderstanding the constraints here, but what about Linear Haskell?

Linear base has an API for updating dictionaries (maps) in-place.

1

u/PurpleUpbeat2820 Nov 09 '22

Yes, I think that's exactly it.

1

u/alagris12358 Nov 09 '22

I don't think it's right to compare it to functional programming. It is actually closer to GPAC

https://en.m.wikipedia.org/wiki/General_purpose_analog_computer#:~:text=The%20general%20purpose%20analog%20computer,order%20to%20compute%20some%20function.

It is a continuous model of computation https://arxiv.org/abs/0907.3117

It is the fundamental theory that underlies analog computers, in the same way as turing machines are fundamental to digital computers or like lambda calculus is to functional programming.

Programs can be expressed using differentially algebraic systems of equations https://en.m.wikipedia.org/wiki/Differential-algebraic_system_of_equations

It's also closely related to how neurons operate in the brain (a.k.a spiking neural networks )

2

u/PurpleUpbeat2820 Nov 09 '22

When the OP wrote:

x' = f(x)

I think they meant:

x₂ = f(x₁)

So I'm not sure it is differential or continuous.

1

u/Felix9876543210 Nov 10 '22 edited Nov 10 '22

There is a word for when the compiler reuses a memory location of an argument to store a result, when the argument isn't needed anymore. Then the programmer doesn't need to conserve variables to conserve memory. Do you know how that's called?

I guess "in-place mutation" or "in-place update optimization".

x1 = 2        →  sptr = sptr - 1
                 stack[sptr] = 2
x2 = x1 * 10  →  stack[sptr] = stack[sptr] * 10
x3 = x2 + 4   →  stack[sptr] = stack[sptr] + 4

Anyway, there still could be an interesting notation for manipulating arguments if you don't want to force a functional programming style. Maybe the compiler can't always guarantee to find optimal memory locations for new variables.

3

u/moon-chilled sstm, j, grand unified... Nov 09 '22

Sounds like TLA+.

I was also working on a game engine based on a similar paradigm (plus message-passing) at one point, but I lost interest, and also decided that that is the wrong paradigm for games—time is continuous, not discrete.

2

u/hellotanjent Nov 09 '22

"I want executable TLA+ that looks and performs like C++" would be a reasonable description of my long-term project goals. :D

I did game programming professionally during the N64 to XBox 360 era - "for (x in game_objects) x.update(delta_time);" was then and is still basically the model for most game simulations - discrete, but with an elapsed time parameter.

2

u/moon-chilled sstm, j, grand unified... Nov 09 '22

for (x in game_objects) x.update(delta_time)

Yes - the main problem with this is the implicit interdependencies between objects; this is what I wanted to avoid. A nice side effect of my design is that it admits efficient implementation--simply double buffer the game state--and high parallelism. And given such an architecture, it's easy to go from 2 buffers to n, for first-class time travel, or multiplayer, or ...

Afaik, deltas are no longer in vogue because they make the physics dependent on the framerate. Especially bad in a multiplayer context. I think interpolation is a common strategy for when the physics and graphics tick rates don't match.

1

u/hellotanjent Nov 09 '22

Yep, sounds like we had basically the same ideas. Double-buffering the game state works but does cost memory and CPU (due to the copies).

Deltas are better than explicit ticks for the special-effects/rendering update part of the engine (otherwise you get jitter on GSync-type displays), but fixed ticks are better for netcode. Physics theoretically shouldn't care about the tick rate as long as collisions are detected regardless of step size, but physics engines are notoriously wonky.

2

u/Molossus-Spondee Nov 09 '22

It sounds a little like you want functional reactive programming.

But you probably want it to use linear types and stuff for performance.

1

u/hellotanjent Nov 09 '22

Functional reactive programming really doesn't work for something like the gate simulation I do in GateBoy, where I'm trying to get as close to realtime as possible.

2

u/Rabbit_Brave Nov 09 '22

Temporal logic programming?

1

u/hellotanjent Nov 09 '22

Yeah, I usually call it "Temporal Programming" (with a nod to Lamport's TLA+).

2

u/ericbb Nov 09 '22

There really isn't any software-oriented language out there that ...

What do you think about Cell? Make sure to read the State updates section of the Relational Automata chapter.

1

u/hellotanjent Nov 09 '22

Seems very similar. I recall reading a bit about Cell a few years ago; I'll have to revisit it.

1

u/alagris12358 Nov 09 '22

There actually is. It's called GPAC https://en.m.wikipedia.org/wiki/General_purpose_analog_computer#:~:text=The%20general%20purpose%20analog%20computer,order%20to%20compute%20some%20function.

It is a continuous model of computation https://arxiv.org/abs/0907.3117

It is the fundamental theory that underlies analog computers, in the same way as turing machines are fundamental to digital computers

3

u/hellotanjent Nov 09 '22

Analog computers are super cool - you should google "The Analog Thing" for a modern example that you can buy and play with - but they're out of scope for this particular topic.

1

u/alagris12358 Nov 09 '22

That's cool af. I was always looking for something like this. I wonder if there are bigger machines. Like FPGAs or something

1

u/hellotanjent Nov 09 '22

There are definitely "analog fpgas" out there, but they're a niche of a niche product. I believe Cypress had a programmable analog block on its PSoC 5 chips, but there are also larger sea-of-opamps kind of things out there.

7

u/jediknight Nov 09 '22

Why not write it like (a,b) = (b+1, a+1) ?

As for "a program is a sequence of operations", I believe we should get away from manually mutating things. It is much better to give the compiler enough information to do the mutation itself in the most optimal way. It is better to program in a denotational language where things are predictable.

1

u/o11c Nov 09 '22

FYI that's std::tie in C++

1

u/phischu Effekt Nov 09 '22

What could also be related is clash, a language (embedded into Haskell) that has first-class support for what they call signals. Here are some examples. They synthesize hardware from a given program.

1

u/hiljusti dt Nov 18 '22

I'd say that the important consideration here (from the C/C++ perspective) is a given CPU's instruction set will not necessarily have a way to map this to actual operations it can perform on registers. At least 3 stored values will be required to perform your example computations, or possibly using a stack and more operations