Skip to content

Add a primitive for laziness/call-by-need semantics #5564

Open
@anka-213

Description

Is your feature request related to a problem? Please describe.
Some algorithms and tricks are only possible in pure languages when you have access to laziness/memoized thunks. See e.g. Okasaki's "Purely Functional Data Structures", many of which depend on laziness to get their desired asymptomatics. Or tricks like using info about the final data structure to put info in leaves

Describe the solution you'd like
I would like to have some kind of primitive to create a memoized thunk/lazy value. A signature that was proposed on discord is the following:

memo : '{} a -> '{} a

The lazy value needs to be pure, since it would be a complete mess otherwise.

It might also be the case that some of the rules on recursive definitions would need to be loosened if the primitive is used, to actually allow self-referential lazy definitions, but I'm not completely sure.

Describe alternatives you've considered
There are other possible interfaces that could be used to introduce laziness. I haven't completely thought through the options.

Without some way to get laziness, one would have to make do without the improved asymptomatics of those algorithms.

Additional context

This post by Edward Kmett lists a few more things that are difficult or impossible to do without laziness: https://www.reddit.com/r/haskell/comments/5xge0v/comment/deia53t/

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions