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.
1 Answer
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.
-
$\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 fora
and put the computed result at that location. It will do the same forb
and print it out. In Haskell, the equivalent program will allocate some memory (thunks) for both but it will never actually compute the value ofb
because it is never used since we only print the value ofa
. I was looking for languages where you can 'force' that computation. It seems it is indeed possible with!
operators in Haskell. $\endgroup$– wildcatCommented May 8, 2023 at 15:56
!
,seq
anddeepseq
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$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 fora
and a value of 50 forb
. $\endgroup$