Skip to content

Commit c04e179

Browse files
committed
:q
ºPrint statements
1 parent c19ba00 commit c04e179

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

src/axiomatic/pic_helpers.py

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ipywidgets import interactive, IntSlider # type: ignore
44
from typing import List, Optional
55

6-
from . import Parameter
6+
from . import Parameter, StatementDictionary, StatementValidationDictionary, StatementValidation
77

88

99
def plot_circuit(component):
@@ -201,3 +201,101 @@ def plot_parameter_history(parameters: List[Parameter], parameter_history: List[
201201
]
202202
)
203203
plt.show()
204+
205+
206+
def print_statements(statements: StatementDictionary, validation: Optional[StatementValidationDictionary] = None):
207+
"""
208+
Print a list of statements in nice readable format.
209+
"""
210+
211+
validation = StatementValidationDictionary(
212+
cost_functions=(validation.cost_functions if validation is not None else None) or [StatementValidation()]*len(statements.cost_functions or []),
213+
parameter_constraints=(validation.parameter_constraints if validation is not None else None) or [StatementValidation()]*len(statements.parameter_constraints or []),
214+
structure_constraints=(validation.structure_constraints if validation is not None else None) or [StatementValidation()]*len(statements.structure_constraints or []),
215+
unformalizable_statements=(validation.unformalizable_statements if validation is not None else None) or [StatementValidation()]*len(statements.unformalizable_statements or [])
216+
)
217+
218+
if len(validation.cost_functions or []) != len(statements.cost_functions or []):
219+
raise ValueError("Number of cost functions and validations do not match.")
220+
if len(validation.parameter_constraints or []) != len(statements.parameter_constraints or []):
221+
raise ValueError("Number of parameter constraints and validations do not match.")
222+
if len(validation.structure_constraints or []) != len(statements.structure_constraints or []):
223+
raise ValueError("Number of structure constraints and validations do not match.")
224+
if len(validation.unformalizable_statements or []) != len(statements.unformalizable_statements or []):
225+
raise ValueError("Number of unformalizable statements and validations do not match.")
226+
227+
# (statements.parameter_constraints or []),(validation.parameter_constraints or []))
228+
print("-----------------------------------\n")
229+
for cost_stmt, cost_val in zip(statements.cost_functions or [], validation.cost_functions or []):
230+
print("Type:", cost_stmt.type)
231+
print("Statement:", cost_stmt.text)
232+
print("Formalization:", end=" ")
233+
if cost_stmt.formalization is None:
234+
print("UNFORMALIZED")
235+
else:
236+
code = cost_stmt.formalization.code
237+
if cost_stmt.formalization.mapping is not None:
238+
for var_name, computation in cost_stmt.formalization.mapping.items():
239+
if computation is not None:
240+
args_str = ", ".join(
241+
[
242+
f"{argname}="
243+
+ (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
244+
for argname, argvalue in computation.arguments.items()
245+
]
246+
)
247+
code = code.replace(var_name, f"{computation.name}({args_str})")
248+
print(code)
249+
val = cost_stmt.validation or cost_val
250+
print(f"Satisfiable: {val.satisfiable}")
251+
print(val.message)
252+
print("\n-----------------------------------\n")
253+
for param_stmt, param_val in zip(statements.cost_functions or [], validation.cost_functions or []):
254+
print("Type:", param_stmt.type)
255+
print("Statement:", param_stmt.text)
256+
print("Formalization:", end=" ")
257+
if param_stmt.formalization is None:
258+
print("UNFORMALIZED")
259+
else:
260+
code = param_stmt.formalization.code
261+
if param_stmt.formalization.mapping is not None:
262+
for var_name, computation in param_stmt.formalization.mapping.items():
263+
if computation is not None:
264+
args_str = ", ".join(
265+
[
266+
f"{argname}="
267+
+ (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
268+
for argname, argvalue in computation.arguments.items()
269+
]
270+
)
271+
code = code.replace(var_name, f"{computation.name}({args_str})")
272+
print(code)
273+
val = param_stmt.validation or param_val
274+
print(f"Satisfiable: {val.satisfiable}")
275+
print(f"Holds: {val.holds} ({val.message})")
276+
print("\n-----------------------------------\n")
277+
for struct_stmt, struct_val in zip(statements.structure_constraints or [], validation.structure_constraints or []):
278+
print("Type:", struct_stmt.type)
279+
print("Statement:", struct_stmt.text)
280+
print("Formalization:", end=" ")
281+
if struct_stmt.formalization is None:
282+
print("UNFORMALIZED")
283+
else:
284+
func_constr = struct_stmt.formalization
285+
args_str = ", ".join(
286+
[
287+
f"{argname}=" + (f"'{argvalue}'" if isinstance(argvalue, str) else str(argvalue))
288+
for argname, argvalue in func_constr.arguments.items()
289+
]
290+
)
291+
func_str = f"{func_constr.function_name}({args_str}) == {func_constr.expected_result}"
292+
print(func_str)
293+
val = struct_stmt.validation or struct_val
294+
print(f"Satisfiable: {val.satisfiable}")
295+
print(f"Holds: {val.holds}")
296+
print("\n-----------------------------------\n")
297+
for unf_stmt in statements.unformalizable_statements or []:
298+
print("Type:", unf_stmt.type)
299+
print("Statement:", unf_stmt.text)
300+
print("Formalization: UNFORMALIZABLE")
301+
print("\n-----------------------------------\n")

0 commit comments

Comments
 (0)