File: Ouroborous.java

package info (click to toggle)
cvc5 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 87,260 kB
  • sloc: cpp: 383,850; java: 12,207; python: 12,090; sh: 5,679; ansic: 4,729; lisp: 763; perl: 208; makefile: 38
file content (145 lines) | stat: -rw-r--r-- 4,706 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/******************************************************************************
 * Top contributors (to current version):
 *   Daniel Larraz
 *
 * This file is part of the cvc5 project.
 *
 * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS
 * in the top-level source directory and their institutional affiliations.
 * All rights reserved.  See the file COPYING in the top-level source
 * directory for licensing information.
 * ****************************************************************************
 *
 * "Ouroborous" test: does cvc5 read its own output?
 *
 * The "Ouroborous" test, named after the serpent that swallows its
 * own tail, ensures that cvc5 can parse some input, output it again
 * (in any of its languages) and then parse it again.  The result of
 * the first parse must be equal to the result of the second parse;
 * both strings and expressions are compared for equality.
 *
 * To add a new test, simply add a call to runTestString() under
 * runTest(), below.  If you don't specify an input language,
 * LANG_SMTLIB_V2 is used.  If your example depends on symbolic constants,
 * you'll need to declare them in the "declarations" global, just
 * below, in SMT-LIBv2 form (but they're good for all languages).
 */

import io.github.cvc5.*;
import io.github.cvc5.modes.*;
import java.io.*;

public class Ouroborous
{
  public static void main(String[] args)
  {
    try
    {
      System.exit(runTest());
    }
    catch (CVC5ApiException e)
    {
      System.err.println(e.getMessage());
    }
    catch (Exception e)
    {
      System.err.println("non-cvc5 exception thrown");
      e.printStackTrace();
    }
    System.exit(1);
  }

  private static String parse(String instr, String inputLanguage, String outputLanguage)
      throws CVC5ApiException
  {
    assert inputLanguage.equals("smt2");
    assert outputLanguage.equals("smt2");

    String declarations = "(set-logic ALL)\n"
        + "(declare-sort U 0)\n"
        + "(declare-fun f (U) U)\n"
        + "(declare-fun x () U)\n"
        + "(declare-fun y () U)\n"
        + "(assert (= (f x) x))\n"
        + "(declare-fun a () (Array U (Array U U)))\n";

    TermManager tm = new TermManager();
    Solver solver = new Solver(tm);

    solver.setOption("input-language", inputLanguage);
    solver.setOption("output-language", outputLanguage);
    SymbolManager symman = new SymbolManager(tm);
    InputParser parser = new InputParser(solver, symman);

    parser.setStringInput(InputLanguage.SMT_LIB_2_6, declarations, "internal-buffer");

    Command c;
    while (true)
    {
      c = parser.nextCommand();
      if (c == null || c.isNull())
      {
        break;
      }
      c.invoke(solver, symman);
    }
    assert parser.done();

    parser.setStringInput(InputLanguage.SMT_LIB_2_6, instr, "internal-buffer");

    Term e = parser.nextTerm();
    String s = e.toString();
    assert parser.nextTerm() == null; // should be no more terms
    return s;
  }

  private static String translate(String instr, String inputLanguage, String outputLanguage)
      throws CVC5ApiException
  {
    assert inputLanguage.equals("smt2");
    assert outputLanguage.equals("smt2");

    System.out.println("==============================================");
    System.out.println(
        "translating from " + inputLanguage + " to " + outputLanguage + " this string:");
    System.out.println(instr);

    String outstr = parse(instr, inputLanguage, outputLanguage);

    System.out.println("got this:");
    System.out.println(outstr);
    System.out.println("reparsing as " + outputLanguage);

    String poutstr = parse(outstr, outputLanguage, outputLanguage);
    assert outstr.equals(poutstr);

    System.out.println("got same expressions " + outstr + " and " + poutstr);
    System.out.println("==============================================");

    return outstr;
  }

  private static void runTestString(String instr, String instrLanguage) throws CVC5ApiException
  {
    System.out.println();
    System.out.println("starting with: " + instr);
    System.out.println("   in language " + instrLanguage);

    String smt2str = translate(instr, instrLanguage, "smt2");
    System.out.println("in SMT2      : " + smt2str);

    String outstr = translate(smt2str, "smt2", "smt2");
    System.out.println("to SMT2 : " + outstr);
    System.out.println();

    assert outstr.equals(smt2str);
  }

  private static int runTest() throws CVC5ApiException
  {
    runTestString("(= (f (f y)) x)", "smt2");
    runTestString("(= ((_ extract 2 1) (bvnot (bvadd #b000 #b011))) #b10)", "smt2");
    runTestString("((_ extract 2 0) (bvnot (bvadd (bvmul #b001 #b011) #b011)))", "smt2");
    return 0;
  }
}