Feature idea: functions evaluating $ ... $ blocks

Let’s assume we want to write a book on maths, and we have some prose like this:

#lorem(20)

$ sum_(k=0)^n k
    &= 1 + ... + n \
    &= (n(n+1)) / 2 $

#lorem(30)

It would be nice if Typst had a maths engine (something similar to Mathematica or MATLab) and we could define a function like this:

// pesudo code

#let f(n) = $ sum_(k=0)^n k $

And then we could call f:

#let f(n) = $ sum_(k=0)^n k $

#lorem(20)

$ sum_(k=0)^n k
    &= 1 + ... + n \
    &= (n(n+1)) / 2 $

#lorem(30)

#f(5) // evaluates to 15

Or have I gone insane?

I realize this avoids actually answering the question, but in your example f(n) is never displayed. It being written in Typst’s math notation is only important to the author. The reason I mention it is because there already exists a tool available to the author for computing such things: code mode.

#let f(n) = {(n * (n + 1)) / 2}

#lorem(10)

$ sum_(k=0)^n k
    &= 1 + ... + n \
    &= (n(n+1)) / 2 $

#lorem(10)

#f(5) // evaluates to 15

Taking this idea even further, a system could be created that would collect display and calculation capabilities into one variable:

#let create-pair(short, full, code) = (
  short: short(),
  full: full,
  code: code,
  evaluate: (n) => [#short(n: n)$= #code(n)$]  //Display inline and the result
)

#let custom-sum = create-pair(
  (n: $n$) => $sum_(k=0)^#n k$, //Function to display inline math
  $ sum_(k=0)^n k               //Plain block math
    &= 1 + ... + n \
    &= (n(n+1)) / 2 $,
  (n) => {(n * (n + 1)) / 2}    //Function to calculate the result (code mode)
)

A summation can be expanded as such:
#custom-sum.full

A few examples of summing for various $n$'s:

#(custom-sum.evaluate)(1)
#(custom-sum.evaluate)(5)
#(custom-sum.evaluate)(10)