0
$\begingroup$

As far as I am aware, most functional programming languages today use a call-by-value eager evaluation strategy with some exceptions like Haskell. I am curious if it is possible for a language to have a mixed evaluation strategy: for example, a language where everything is evaluated lazily by default but some functions can be marked as eager and are evaluated as such. I am not interested about emulating different evaluation semantics (like using thunks to create laziness) but where the language itself allows for mixed modes.

$\endgroup$
3
  • 1
    $\begingroup$ wiki.haskell.org/Performance/… - when you want eager behavior in Haskell, you can have it $\endgroup$
    – Dmitry
    Commented May 8, 2023 at 4:10
  • $\begingroup$ Thanks a lot @Dmitry! It seems like the !, seq and deepseq does what I am thinking of. However, it looks likeseq introduces a dependency between two expressions so you're forced to evaluate one. To me, this is similar to how in OCaml (an eager language) you can create laziness by taking advantage of the fact that the body of a function is not evaluated when the function is defined, but only when it is applied. My question is if its possible for a language to be defined so that it can switch completely between eager and lazy evaluation based on certain contexts. $\endgroup$
    – wildcat
    Commented May 8, 2023 at 16:24
  • $\begingroup$ For example, consider the following hypothetical language where leteg can be a reserved keyword that forces the language to evaluate its expression. let a = 10 + 10; leteg b = 20 + 30; print a. Here, we should expect that right before the program executes the print statement the reduction graph contains a thunk for a and a value of 50 for b. $\endgroup$
    – wildcat
    Commented May 8, 2023 at 16:28

1 Answer 1

0
$\begingroup$

Almost every mainstream programming language mixes strict and eager evaluation. In particular, in almost all mainstream programming languages, subroutine calls, function applications, and operator applications are evaluated eagerly, except conditional operations.

For example, in C, foo ? x : y will only evaluate y if foo is false and only evaluate x if foo is true. foo is evaluated eagerly, but x and y are evaluated lazily.

There are some programming languages which eagerly evaluate both branches, but they are very rare, and not mainstream.

$\endgroup$
1
  • $\begingroup$ Perhaps I wasn't clear in my post. For example, in C we can have int a = 10 + 10; int b = 20 + 20; printf("%d\n", a); The program will allocate memory for a and put the computed result at that location. It will do the same for b and print it out. In Haskell, the equivalent program will allocate some memory (thunks) for both but it will never actually compute the value of b because it is never used since we only print the value of a. I was looking for languages where you can 'force' that computation. It seems it is indeed possible with ! operators in Haskell. $\endgroup$
    – wildcat
    Commented May 8, 2023 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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