1
$\begingroup$

It seems like a lot of programming is about converting a high-level human intuition into a very precise, ordered set of steps and conditions for the computer to execute correctly. I think programming could match the way people think more if we could write instructions which refer back to previous parts of the program without having to capture them as variables ahead of time. Ideally, the language could be designed to compile the high-level, intuitive instructions into the precise order.

Is there any programming language which relates to these ideas, of being so high-level the program gets significantly reordered upon compiling?

$\endgroup$
3
  • $\begingroup$ I know that there is a branch of "programming" which takes a totally different perspective on the problem: Instead of coding a solution to a problem, you write the target task and the compiler's task is to generate code that solves this task (as opposed to you writing it without specifying the task to the computer). This might be more "intuitive" in some sense of programming - since you don't need to specify all the details of how to solve the problem, but rather the general idea of it. That being said, I only heard of this concept once or twice, so I don't really know anything more about it. $\endgroup$
    – nir shahar
    Oct 2 '21 at 21:38
  • 1
    $\begingroup$ Are you seeking constraint programming? $\endgroup$
    – Guy Coder
    Oct 3 '21 at 7:29
  • $\begingroup$ Part of the problem I see with your question is this line Ideally, the language could be designed to compile the high-level, intuitive instructions into the precise order. You are assuming the work has to be done at compile time. I work with Prolog and other models of problem solving and in many cases the work is done at run time and not at compile time. While quantum computers is not something I do, your question seems to have touch points with quantum computing. $\endgroup$
    – Guy Coder
    Oct 3 '21 at 7:36
3
$\begingroup$

You are probably looking for declarative programming. There are various sorts, but most of them are (mostly) atemporal in the sense that there is no notion of order of execution.

Probably the best main-stream example is Haskell. Programs are not "sequences of instructions" but rather systems of equations. When programming in Haskell, we do not think of how to compute the result, but what equations describe the desired result. Of course, the compiler then generates sequences of instructions in machine code, as there is little else that can be done, given the constraints of the available hardware and the fact that we do happen to live in a universe governed by temporal causality.

$\endgroup$
1
$\begingroup$

It depends on exactly what you mean

I can see three possibilities for what you might be asking about here.

If the problem is that, in a language like C for instance, it's not possible to write things like this:

int f(int x) {
  return g(x + 1);
}
int g(int x) {
  return x + 1;
}

because the compiler will complain that it doesn't know what g is during the definition of f, then the answer is very much yes. Many languages allow functions to be defined out of order, although often it comes at the cost that calls to nonexistant functions won't throw an error until they actually happen while the program is running. Python falls in this group for instance, along with most if not all lisp languages.

If, on the other hand, you want the same to apply to variables - allowing programs like:

x = y + 1
y = 2

Then the answer is also yes, but it comes with even stronger caveats. What you're looking for is a purely or nearly purely functional[1] language like Haskell or Prolog, which can achieve the property you seem to be asking about by virtue of not allowing any modification to a variable once it's been defined. This means the language doesn't have to care where the definition took place - since order doesn't matter, it can shuffle the lines about (kind of) freely. The following is a valid Haskell program that prints 3:

x = y + 1
y = 2
main =
  print(x)

I don't think it's possible to have what (I think) you're asking about and also allow variables to be modified, because if a variable can be modified then the language must have some concept of time in order for there to be a 'before' and 'after' changing it.

[1] Strictly speaking, being a functional language is unrelated to allowing definitions out of order in this way. What is needed is not allowing variables to be changed after definition, which is one part of functional programming but not all of it. However, I don't think any general-purpose language that doesn't allow variables to be updated wouldn't describe itself as at least a bit functional.

$\endgroup$
3
  • $\begingroup$ Thanks. What about adding a break condition to a for loop after writing the for loop? Could you refer back to a named for loop in Haskell later in the program? And, what about writing a program and at the end calling a method, time_me(). It knows to insert a start_timer() at the beginning of the program and print the time at the end. That’s what I mean by a high-level language slotting in the necessary preparatory steps - like a variable declaration - so you can write ideas in an intuitive order and not think exactly how the computer needs to pull it off. $\endgroup$
    – John Smith
    Oct 3 '21 at 8:06
  • $\begingroup$ "Could you refer back to a named for loop in Haskell later in the program?" – This is easy: Haskell doesn't have loops. $\endgroup$ Oct 3 '21 at 8:22
  • $\begingroup$ @john-smith okay, I think I see. That sort of thing is generally frowned upon because it makes debugging difficult if you have to understand the entire program to understand one part of it because any part of the program could modify the behaviour of the part you want to understand. I don't know any general purpose language that allows it to the extent you describe, but I'm sure they exist - you might want to check out esolangs.org/wiki/Category:Self-modifying $\endgroup$
    – Silver
    Oct 3 '21 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.