Debian Codegraph @

test/api/python/ouroborous.py

raw
1 ###############################################################################
2 # Top contributors (to current version):
3 # Daniel Larraz
4 #
5 # This file is part of the cvc5 project.
6 #
7 # Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
8 # in the top-level source directory and their institutional affiliations.
9 # All rights reserved. See the file COPYING in the top-level source
10 # directory for licensing information.
11 # #############################################################################
12 #
13 # "Ouroborous" test: does cvc5 read its own output?
14 #
15 # The "Ouroborous" test, named after the serpent that swallows its
16 # own tail, ensures that cvc5 can parse some input, output it again
17 # (in any of its languages) and then parse it again. The result of
18 # the first parse must be equal to the result of the second parse;
19 # both strings and expressions are compared for equality.
20 #
21 # To add a new test, simply add a call to run_test_string() under
22 # run_test(), below. If you don't specify an input language,
23 # LANG_SMTLIB_V2 is used. If your example depends on symbolic constants,
24 # you'll need to declare them in the "declarations" global, just
25 # below, in SMT-LIBv2 form (but they're good for all languages).
26 ##
27
28 import cvc5
29
30 def parse(instr: str, input_language: str, output_language: str) -> str:
31 assert input_language == "smt2"
32 assert output_language == "smt2"
33
34 declarations = """(set-logic ALL)
35 (declare-sort U 0)
36 (declare-fun f (U) U)
37 (declare-fun x () U)
38 (declare-fun y () U)
39 (assert (= (f x) x))
40 (declare-fun a () (Array U (Array U U)))
41 """
42
43 tm = cvc5.TermManager()
44 solver = cvc5.Solver(tm)
45
46 solver.setOption("input-language", input_language)
47 solver.setOption("output-language", output_language)
48
49 sm = cvc5.SymbolManager(tm)
50 parser = cvc5.InputParser(solver, sm)
51
52 # Parse declarations to bind symbols
53 parser.setStringInput(cvc5.InputLanguage.SMT_LIB_2_6, declarations, "internal-buffer")
54 while True:
55 cmd = parser.nextCommand()
56 if cmd.isNull():
57 break
58 # Invoke the command, which may bind symbols
59 cmd.invoke(solver, sm)
60
61 assert parser.done() # parser should be done
62
63 parser.setStringInput(cvc5.InputLanguage.SMT_LIB_2_6, instr, "internal-buffer")
64 term = parser.nextTerm()
65 s = str(term)
66 assert parser.nextTerm().isNull() # next expr should be null
67 return s
68
69 def translate(instr: str, input_language: str, output_language: str) -> str:
70 print("="*50)
71 print(f"translating from {input_language} to {output_language} this string:")
72 print(instr)
73 outstr = parse(instr, input_language, output_language)
74 print("got this:")
75 print(outstr)
76 print(f"reparsing as {output_language}")
77 poutstr = parse(outstr, output_language, output_language)
78 assert outstr == poutstr
79 print(f"got same expressions {outstr} and {poutstr}")
80 print("="*50)
81 return outstr
82
83 def run_test_string(instr: str, instr_language: str):
84 print(f"\nstarting with: {instr}\n in language {instr_language}")
85 smt2str = translate(instr, instr_language, "smt2")
86 print(f"in SMT2 : {smt2str}")
87 outstr = translate(smt2str, "smt2", "smt2")
88 print(f"to SMT2 : {outstr}\n")
89 assert outstr == smt2str
90
91 def run_test():
92 run_test_string("(= (f (f y)) x)", "smt2")
93 run_test_string("(= ((_ extract 2 1) (bvnot (bvadd #b000 #b011))) #b10)", "smt2")
94 run_test_string("((_ extract 2 0) (bvnot (bvadd (bvmul #b001 #b011) #b011)))", "smt2")
95
96 run_test()