math_spec.lowering
Lower a parsed YAML schema (typed AST) to a :class:~math_spec.program.Program.
One lowering, whatever builds the result: it reads the typed AST and emits declarations with names resolved and shapes fixed. It lives on the language side, so no consumer needs YAML knowledge and this module reaches no consumer — which is what makes two consumers agreeing about a file structural rather than careful.
Constructs with no lowering raise :class:~math_spec.errors.LanguageError naming
the construct and its rewrite, never a pointer at some other implementation:
a rejection here is a language gap (docs/about/roadmap.md) rather than a
routing decision.
The rules a lowered program then carries:
- a reduction over a dim the operand does not carry is an error, not a silent
identity —
math_spec.dimensionsowns that rule and this module asks it; - a constraint is one rule carrying its own name, so a row is read back by the name the file writes, with no positional suffix to guess;
- a file declares one objective, likewise one expression;
- an objective is scalar, so every reduction in it is one the file wrote and nothing sums on its own behalf.
lower_program(schema)
#
Compile a :class:_ExpandedSpec into a :class:Program.
Takes the expanded model rather than expanding one: a program is built from
declarations, and _ExpandedSpec is the type that guarantees they are all
there. Every caller already held one — the expansion is memoised on the
model — so this moves no work, it only stops the guarantee being a
convention four consumers happened to observe.
A domain: binary variable lowers with fixed 0/1 bounds, so the domain
needs no separate carrier.
| RAISES | DESCRIPTION |
|---|---|
LanguageError
|
A construct outside the streaming language, named with its rewrite. |
Source code in src/math_spec/lowering.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
to_program(spec)
#
spec as a :class:~math_spec.program.Program — the public door.
Takes whatever you have: a YAML path, the YAML itself, a mapping, a loaded model, or a program already. Idempotent, so a caller that does not know which it holds can call this and be sure.
Not memoised. :func:~math_spec.piecewise.expand_piecewise is, because
validators reach for the expansion as well as consumers and the same model
is expanded more than once on one pass; nothing has that shape here. Add it
the day something lowers one model twice.
| PARAMETER | DESCRIPTION |
|---|---|
spec
|
What to read the declarations from. |
| RETURNS | DESCRIPTION |
|---|---|
Program
|
Every declaration the file makes, with names resolved and shapes |
Program
|
fixed. |
| RAISES | DESCRIPTION |
|---|---|
SchemaError
|
The file is not a valid model. |
LanguageError
|
A construct outside the language, named with its rewrite. |