Week 2 adds memory to the UOp graph. A graph has no statement order, so the order of reads and writes must be encoded in the graph itself: AFTER(buf, store, ...) is the buffer after those stores have happened. Implement a checker and evaluator for such programs.
A program is a dict from node name to a tuple:
("buffer", size): a buffer with initial contents init[name] (a list of ints).("load", version, index): the value at index of a buffer version. A version is the name of a buffer node or of an after node.("store", version, index, value): write value at index into that version.("after", version, store1, store2, ...): the new version once the listed stores have been applied to version.("add", a, b) and ("mul", a, b): integer arithmetic. Every value and operand is a node name or a plain int literal; every index is a plain int literal.check_effects(program, init) first validates the program with these rules, in this order. For each rule, scan in the stated order and report the first offender as ("invalid", reason, name):
"out_of_bounds": a load or store whose index lies outside of its buffer (scan all node names sorted)."bad_target": a store listed in an after whose version differs from that after's version (scan the after nodes sorted by name, each store in listed order; report the store)."dangling" for a store listed in no after, "duplicated" for one listed more than once (scan stores sorted by name)."fork": two after nodes with the same base version, so the buffer would have two futures (scan the after nodes sorted; report the second one)."conflict": one after lists two stores to the same index, which leaves their order unspecified (scan after nodes sorted; report the after).A valid program returns ("ok", loads, final). Here loads maps every load name, sorted, to the value it reads: the contents of its version, where an after version is its base with its stores applied in listed order, each store's value computed from the graph. final maps each buffer name, sorted, to the contents (a list) of the last version in its chain.