kongware / scriptum

Functional Programming Unorthodoxly Adjusted to Client-/Server-side Javascript
MIT License
383 stars 21 forks source link

Make thunks more lazy #343

Closed ivenmarquardt closed 3 years ago

ivenmarquardt commented 3 years ago

The following program should be lazy by stopping the evaluation after the first iteration, but instead it causes a stack overflow:

A.foldk(
  fun(x => y => Cont(fun(
    k => x + y > 10000 ? x : thunk(() => k(x + y), "() => Number"),
    "(Number => Number) => Number")),
  "Number => Number => Cont<Number, Number>")) (0) (Array(1e5).fill(1));
ivenmarquardt commented 3 years ago

thunks are now evaluated a single layer deep in some cases, not all the way down to weak head normal form. This allows foldk to be stack-safe by using the strictRec trampoline.