Lesson 20 of 23 · 3:15
Build, query and validate a CM in code
Build through the public API and validate against a separate scalar oracle.
Keyboard: focus the player, then Space or K to play/pause and arrow keys to seek. Tab moves through the controls. Selectable captions replace the video's caption strip with the same timed text below.
In this lesson
- 0:00 · How do we check a matrix built by code?
- 0:29 · Create the expression object
- 0:48 · Declare the rows and columns
- 1:07 · Make a complete assignment explicit
- 1:29 · Check with an independent scalar oracle
- 1:47 · Three representative balanced partitions
- 2:12 · Reuse compilation when appropriate
- 2:31 · Your turn: address the same input differently
- 2:56 · Answer: row three, column zero, value one
Complete transcript
This transcript includes the answers to prediction prompts.
Show transcript
How do we check a matrix built by code? · 0:00
How do we check a matrix built by code? We will construct a four-input rule through the repository public API, build its matrix, and test every assignment against an independent scalar calculation. Start with four indexed variables: A through D in our explanation, named x zero through x three in the repository. The accompanying Python example is runnable.
Create the expression object · 0:29
Construct AND from A and B, OR from C and D, then place both under XOR. This object records the expression structure. It does not rely on Python’s ordinary Boolean and operator, which would evaluate Python objects instead of building this expression tree.
Declare the rows and columns · 0:48
Choose x zero and x one as row variables, and x two and x three as column variables. The empty fixed dictionary means no input is held constant. With two free bits on each axis, the output has four rows and four columns.
Make a complete assignment explicit · 1:07
For A one, B one, C zero, D zero, pass every repository name in an assignment dictionary. Row bits one-one give index three and column bits zero-zero give index zero. Store the lookup result, then assert that it is one. This is runnable Python, not equation notation.
Check with an independent scalar oracle · 1:29
Compute the expected answer directly from the four bits: A bitwise-AND B, exclusive-or C bitwise-OR D. Compare that with the lookup for every tuple from zero-zero-zero-zero to one-one-one-one. Sixteen checks cover the whole four-input function.
Three representative balanced partitions · 1:47
Here are three representative balanced partitions: A B over C D, A C over B D, and A D over B C. We keep A in the row group and retain the original order within each list. Swapping axes or changing list order produces other valid layouts. The example checks these three representatives across all sixteen assignments.
Reuse compilation when appropriate · 2:12
Import compilation and materialization from cm_ir. Compile the rule once, then materialize its node for each declared layout. Those arrays match direct public-wrapper construction. This distinguishes reusable preparation from the requested final matrix output.
Your turn: address the same input differently · 2:31
For A one, B zero, C one, D zero, use rows A C and columns B D. What row and column indices do you get, and what is the rule’s value? First split the bits according to the lists, then evaluate the scalar expression.
Prediction pause: 10 seconds.
Answer: row three, column zero, value one · 2:56
The row bits A C are one-one, giving three. The column bits B D are zero-zero, giving zero. AND is zero, OR is one, so exclusive-or is one. This is a small reproducible correctness example; it does not make a performance claim.

