7

If I'm not mistaken, the concept of a "lazy imperative programming language" makes perfect sense.

For example, I envision that the following code should cause the program to print "7"

a <- 1
b <- a+5
a <- 2
print([b])

while the following code should cause the program to print "6"

a <- 1
b <- [a+5]
a <- 2
print(b)

and the following code should cause the program to print the string "a+5"

a <- 1
b <- a+5
a <- 2
print(b)

The idea is that [..] flattens an expression by performing an evaluation, using the current values of each variable.

Question. Do lazy imperative programming languages exist, and if not, why not? Is there any particular reason why they cannot ever exist?

goblin GONE
  • 540
  • 3
  • 12
  • In some ways Python 3 is a lazy imperative programming language. – John Coleman Apr 20 '16 at 04:36
  • @JohnColeman, I like that you have a background in universal algebra. Can you elaborate a little regarding the laziness of Python 3? – goblin GONE Apr 20 '16 at 04:48
  • It was just an off-hand remark. Python3 is so multi-paradigm that it is difficult to classify, but it is clearly closer to being an imperative language than a functional one (especially if you view object-oriented as a sub-type of imperative). On the other hand, Python makes very heavy use of lazy *iterators*. E.g. `(n**2 for n in range(10))` produces no squares until you try to iterate over it -- or pass it to a function like `sum` which implicitly iterates over it. This was the case even in Python2 but was made more extensive in Python3. The core evaluation strategy is still eager, though. – John Coleman Apr 20 '16 at 10:31
  • Being multi-paradigm is the rule rather than the exception nowadays. I don't see any imperative language following Haskell's footsteps and being lazy by default, but versions of lazy streams/iterators are becoming more common (e.g. Java 8's streams). See this for a bit about laziness in Python: http://stackoverflow.com/q/2249651/4996248 – John Coleman Apr 20 '16 at 10:48
  • The criterion for a language to be lazy is that it needs to be referentially transparent. In that case, it does not matter when an expression is evaluated - the program will still yield the same result. – lsund Feb 08 '18 at 13:14
  • Any language that supports anonymous functions could operate this way. Take Javascript for example: `var a = 1; var b = () => a + 5; a = 2; console.log(b());` which prints "7" because `b` wraps the expression in a lambda that is defined when `a` is 1 but not executed until `a` is set to 2. The `[]` syntax in your example code could just be interpreted as the syntax to execute a function-wrapped expression, so your second example would instead look like `var a = 1; var b = (() => a + 5)(); a = 2; console.log(b);` which would print "6". – Abion47 Feb 25 '20 at 18:31

0 Answers0