Do lazy imperative programming languages exist? If not, why not?
Asked Answered
W

0

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?

Willy answered 20/4, 2016 at 4:14 Comment(6)
In some ways Python 3 is a lazy imperative programming language.Hobard
@JohnColeman, I like that you have a background in universal algebra. Can you elaborate a little regarding the laziness of Python 3?Willy
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.Hobard
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: https://mcmap.net/q/1297284/-python-lazy-iterator/4996248Hobard
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.Gwenngwenneth
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".Sisterly

© 2022 - 2024 — McMap. All rights reserved.