Week 1 of the course adds a little lambda calculus to the UOp graph: PARAM is an argument and CALL applies a function body to arguments. Implement evaluation, inlining and node counting for graphs given as nested tuples:
("param", slot): the argument in position slot of the enclosing call (or of the top-level program).int is a constant.("add", a, b) and ("mul", a, b) wrap to signed 32 bits. ("div", a, b) is floor division (its result wraps too) and ("mod", a, b) is floor modulo.("cmplt", a, b) gives 1 or 0, and ("where", c, a, b) gives a when c is non-zero, else b.("call", body, arg0, arg1, ...) evaluates body with its ("param", i) bound to arg_i.Scoping rule. Every body is closed. A call binds the params of its own body, but not those inside the body of a nested call: a nested call's body is untouched, and only its argument expressions are interpreted in the current binding.
Implement:
evaluate(node, args=()): the value of node, with top-level params bound to args.inline(node): an equivalent expression that contains no call node, obtained by substituting each call's (already inlined) argument expressions for its body's params, recursively. Top-level params stay as they are.count_nodes(node): the number of distinct nodes reachable from node, compared structurally (equal tuples count once). Count every tuple and every integer constant, including call bodies and their contents; a ("param", slot) counts as one node and its slot number does not count separately.