Theory of Datatypes

examples/api/cpp/datatypes.cpp

  1/******************************************************************************
  2 * This file is part of the cvc5 project.
  3 *
  4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
  5 * in the top-level source directory and their institutional affiliations.
  6 * All rights reserved.  See the file COPYING in the top-level source
  7 * directory for licensing information.
  8 * ****************************************************************************
  9 *
 10 * An example of using inductive datatypes in cvc5.
 11 */
 12
 13#include <cvc5/cvc5.h>
 14
 15#include <iostream>
 16
 17using namespace cvc5;
 18
 19void test(Solver& slv, Sort& consListSort)
 20{
 21  TermManager& tm = slv.getTermManager();
 22
 23  // Now our old "consListSpec" is useless--the relevant information
 24  // has been copied out, so we can throw that spec away.  We can get
 25  // the complete spec for the datatype from the DatatypeSort, and
 26  // this Datatype object has constructor symbols (and others) filled in.
 27
 28  const Datatype& consList = consListSort.getDatatype();
 29
 30  // t = cons 0 nil
 31  //
 32  // Here, consList["cons"] gives you the DatatypeConstructor.  To get
 33  // the constructor symbol for application, use .getConstructor("cons"),
 34  // which is equivalent to consList["cons"].getConstructor().  Note that
 35  // "nil" is a constructor too, so it needs to be applied with
 36  // APPLY_CONSTRUCTOR, even though it has no arguments.
 37  Term t = tm.mkTerm(Kind::APPLY_CONSTRUCTOR,
 38                     {consList.getConstructor("cons").getTerm(),
 39                      tm.mkInteger(0),
 40                      tm.mkTerm(Kind::APPLY_CONSTRUCTOR,
 41                                {consList.getConstructor("nil").getTerm()})});
 42
 43  std::cout << "t is " << t << std::endl
 44            << "sort of cons is "
 45            << consList.getConstructor("cons").getTerm().getSort() << std::endl
 46            << "sort of nil is "
 47            << consList.getConstructor("nil").getTerm().getSort() << std::endl;
 48
 49  // t2 = head(cons 0 nil), and of course this can be evaluated
 50  //
 51  // Here we first get the DatatypeConstructor for cons (with
 52  // consList["cons"]) in order to get the "head" selector symbol
 53  // to apply.
 54  Term t2 = tm.mkTerm(Kind::APPLY_SELECTOR,
 55                      {consList["cons"].getSelector("head").getTerm(), t});
 56
 57  std::cout << "t2 is " << t2 << std::endl
 58            << "simplify(t2) is " << slv.simplify(t2) << std::endl
 59            << std::endl;
 60
 61  // You can also iterate over a Datatype to get all its constructors,
 62  // and over a DatatypeConstructor to get all its "args" (selectors)
 63  for (Datatype::const_iterator i = consList.begin(); i != consList.end(); ++i)
 64  {
 65    std::cout << "ctor: " << *i << std::endl;
 66    for (DatatypeConstructor::const_iterator j = (*i).begin(); j != (*i).end();
 67         ++j)
 68    {
 69      std::cout << " + arg: " << *j << std::endl;
 70    }
 71  }
 72  std::cout << std::endl;
 73
 74  // Alternatively, you can use for each loops.
 75  for (const auto& c : consList)
 76  {
 77    std::cout << "ctor: " << c << std::endl;
 78    for (const auto& s : c)
 79    {
 80      std::cout << " + arg: " << s << std::endl;
 81    }
 82  }
 83  std::cout << std::endl;
 84
 85  // You can also define a tester term for constructor 'cons': (_ is cons)
 86  Term t_is_cons =
 87      tm.mkTerm(Kind::APPLY_TESTER, {consList["cons"].getTesterTerm(), t});
 88  std::cout << "t_is_cons is " << t_is_cons << std::endl << std::endl;
 89  slv.assertFormula(t_is_cons);
 90  // Updating t at 'head' with value 1 is defined as follows:
 91  Term t_updated = tm.mkTerm(
 92      Kind::APPLY_UPDATER,
 93      {consList["cons"]["head"].getUpdaterTerm(), t, tm.mkInteger(1)});
 94  std::cout << "t_updated is " << t_updated << std::endl << std::endl;
 95  slv.assertFormula(tm.mkTerm(Kind::DISTINCT, {t, t_updated}));
 96
 97  // You can also define parameterized datatypes.
 98  // This example builds a simple parameterized list of sort T, with one
 99  // constructor "cons".
100  Sort sort = tm.mkParamSort("T");
101  DatatypeDecl paramConsListSpec =
102      tm.mkDatatypeDecl("paramlist", {sort});  // give the datatype a name
103  DatatypeConstructorDecl paramCons = tm.mkDatatypeConstructorDecl("cons");
104  DatatypeConstructorDecl paramNil = tm.mkDatatypeConstructorDecl("nil");
105  paramCons.addSelector("head", sort);
106  paramCons.addSelectorSelf("tail");
107  paramConsListSpec.addConstructor(paramCons);
108  paramConsListSpec.addConstructor(paramNil);
109
110  Sort paramConsListSort = tm.mkDatatypeSort(paramConsListSpec);
111  Sort paramConsIntListSort =
112      paramConsListSort.instantiate(std::vector<Sort>{tm.getIntegerSort()});
113
114  const Datatype& paramConsList = paramConsListSort.getDatatype();
115
116  std::cout << "parameterized datatype sort is" << std::endl;
117  for (const DatatypeConstructor& ctor : paramConsList)
118  {
119    std::cout << "ctor: " << ctor << std::endl;
120    for (const DatatypeSelector& stor : ctor)
121    {
122      std::cout << " + arg: " << stor << std::endl;
123    }
124  }
125
126  Term a = tm.mkConst(paramConsIntListSort, "a");
127  std::cout << "term " << a << " is of sort " << a.getSort() << std::endl;
128
129  Term head_a =
130      tm.mkTerm(Kind::APPLY_SELECTOR,
131                {paramConsList["cons"].getSelector("head").getTerm(), a});
132  std::cout << "head_a is " << head_a << " of sort " << head_a.getSort()
133            << std::endl
134            << "sort of cons is "
135            << paramConsList.getConstructor("cons").getTerm().getSort()
136            << std::endl
137            << std::endl;
138
139  Term assertion = tm.mkTerm(Kind::GT, {head_a, tm.mkInteger(50)});
140  std::cout << "Assert " << assertion << std::endl;
141  slv.assertFormula(assertion);
142  std::cout << "Expect sat." << std::endl;
143  std::cout << "cvc5: " << slv.checkSat() << std::endl;
144}
145
146int main()
147{
148  TermManager tm;
149  Solver slv(tm);
150  // This example builds a simple "cons list" of integers, with
151  // two constructors, "cons" and "nil."
152
153  // Building a datatype consists of two steps.
154  // First, the datatype is specified.
155  // Second, it is "resolved" to an actual sort, at which point function
156  // symbols are assigned to its constructors, selectors, and testers.
157
158  DatatypeDecl consListSpec =
159      tm.mkDatatypeDecl("list");  // give the datatype a name
160  DatatypeConstructorDecl cons = tm.mkDatatypeConstructorDecl("cons");
161  cons.addSelector("head", tm.getIntegerSort());
162  cons.addSelectorSelf("tail");
163  consListSpec.addConstructor(cons);
164  DatatypeConstructorDecl nil = tm.mkDatatypeConstructorDecl("nil");
165  consListSpec.addConstructor(nil);
166
167  std::cout << "spec is:" << std::endl << consListSpec << std::endl;
168
169  // Keep in mind that "DatatypeDecl" is the specification class for
170  // datatypes---"DatatypeDecl" is not itself a cvc5 Sort.
171  // Now that our Datatype is fully specified, we can get a Sort for it.
172  // This step resolves the "SelfSort" reference and creates
173  // symbols for all the constructors, etc.
174
175  Sort consListSort = tm.mkDatatypeSort(consListSpec);
176
177  test(slv, consListSort);
178
179  std::cout << std::endl
180            << ">>> Alternatively, use declareDatatype" << std::endl;
181  std::cout << std::endl;
182
183  DatatypeConstructorDecl cons2 = tm.mkDatatypeConstructorDecl("cons");
184  cons2.addSelector("head", tm.getIntegerSort());
185  cons2.addSelectorSelf("tail");
186  DatatypeConstructorDecl nil2 = tm.mkDatatypeConstructorDecl("nil");
187  std::vector<DatatypeConstructorDecl> ctors = {cons2, nil2};
188  Sort consListSort2 = slv.declareDatatype("list2", ctors);
189  test(slv, consListSort2);
190
191  return 0;
192}