let

Contents

let#

discopy.closed.let(expression, body)[source]#

Bind the result of an expression term inside the body of a Python function, whose variable names are given by introspection and whose types are the factors of the codomain of expression.

Parameters:
  • expression (Term) – The term that is evaluated.

  • body (Callable) – A function from the bound variables to a term.

Return type:

Let

Example

The term for the self-attention block of the CatGPT benchmark, where an embedded token is packed into a query, key and value before attention and a feed-forward layer are applied:

>>> E = Ty("E")
>>> query, key, value = [(E >> E)(name) for name in (
...     "query", "key", "value")]
>>> attention = (E.product(E, E) >> E)("attention")
>>> feed_forward = (E >> E)("feed_forward")
>>> block = E(lambda x: let(Tuple(query(x), key(x), value(x)),
...     lambda q, k, v: let(attention(Tuple(q, k, v)),
...         lambda a: feed_forward(a))))
>>> assert block.cod == E >> E
>>> block.draw(doctest="docs/_static/closed/catgpt-block.svg",
...     aspect="auto", figsize=(6, 8), margins=(0.2, 0))
../_images/catgpt-block.svg