Laziness

Definitions to support lazy expressions.

Copyright (c) 2024 - Eindhoven University of Technology, The Netherlands

This software is made available under the terms of the MIT License.

class FuPy.laziness.Lazy(value: Callable[[], A] | A, name: str | None = None)[source]

A lazy expression of type A, with memoization upon evaluation.

Thunks are identified by a sequence number. Also keeps track of statistics: * How often its value has been requested. * How deep evaluation unnested lazy expressions.

See: https://en.wikipedia.org/wiki/Lazy_evaluation. A.k.a. as thunk: https://en.wikipedia.org/wiki/Thunk. Sharing of lazy expressions is encouraged, since they need to be evaluated only once.

Usage:

  • Construct by v = Lazy(lambda: expression)

    N.B. The lambda captures local names occurring in expression, in its closure.

  • Get value by evaluate(v); for internal use only: v._get()

FuPy.laziness.IterLazy[A]
FuPy.laziness.evaluate(v: Lazy | A) A[source]

Evaluate v.

FuPy.laziness.lazy(expr: Callable[[A], B] | str) Func[A, Lazy] | Lazy[Any][source]

Make expr lazy.

If expr is callable, then lazy(expr)(a) is equivalent to Lazy(lambda: expr(a)). TODO: split into separate functions?

FuPy.laziness.lazyf(f: Func) Func[Lazy, Lazy][source]

Apply Lazy as functor to f. That is, return a function that applies f inside Lazy.

FuPy.laziness.fpower_lazy(f: Func, n: int) Func[source]

Lazy version of FuPy.basic.fpower().

When is this useful? E.g. when f does not depend on its argument (is a constant function).

FuPy.laziness.fpower_left_lazy(f: Func, n: int) Func[source]

Version of FuPy.basics.fpower_left() using laziness for Tail-Call Elimination.