def nbwd
import bend-ml-autograd@0.1.1.0/main.bend as BendMlAutogradimport Base
53# REVERSE-mode derivative: g is the gradient arriving from the output; each node passes on
54# g (sum) or g * value-of-the-other-factor (product) to its children and adds up what
55# arrives at NX.
56def nbwd(e: NE, +x: Nat, +g: Nat) -> Nat:
57 match e:
58 case NCst{n}:
59 0n
60 case NX{}:
61 g
62 case NAdd{a, b}:
63 Nat.add(nbwd(a, x, g), nbwd(b, x, g))
64 case NMul{+a, +b}:
65 Nat.add(nbwd(a, x, Nat.mul(g, nval(b, x))), nbwd(b, x, Nat.mul(g, nval(a, x))))