From f76e985c8948334f6e65679491be4f003157f460 Mon Sep 17 00:00:00 2001 From: Aina Niemetz Date: Tue, 20 Mar 2018 08:56:07 -0700 Subject: Add parameterized datatypes example. (#1676) --- examples/api/datatypes.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'examples') diff --git a/examples/api/datatypes.cpp b/examples/api/datatypes.cpp index b6aebde71..b0c176314 100644 --- a/examples/api/datatypes.cpp +++ b/examples/api/datatypes.cpp @@ -98,6 +98,52 @@ int main() { std::cout << " + arg: " << *j << std::endl; } } + std::cout << std::endl; + + // You can also define parameterized datatypes. + // This example builds a simple parameterized list of sort T, with one + // constructor "cons". + Type sort = em.mkSort("T", ExprManager::SORT_FLAG_PLACEHOLDER); + Datatype paramConsListSpec("list", std::vector{sort}); + DatatypeConstructor paramCons("cons"); + paramCons.addArg("head", sort); + paramCons.addArg("tail", DatatypeSelfType()); + paramConsListSpec.addConstructor(paramCons); + + DatatypeType paramConsListType = em.mkDatatypeType(paramConsListSpec); + Type paramConsIntListType = paramConsListType.instantiate(std::vector{em.integerType()}); + + Datatype paramConsList = paramConsListType.getDatatype(); + + std::cout << "parameterized datatype sort is " << std::endl; + for (const DatatypeConstructor& ctor : paramConsList) + { + std::cout << "ctor: " << ctor << std::endl; + for (const DatatypeConstructorArg& stor : ctor) + { + std::cout << " + arg: " << stor << std::endl; + } + } + + Expr a = em.mkVar("a", paramConsIntListType); + std::cout << "Expr " << a << " is of type " << a.getType() << std::endl; + + Expr head_a = em.mkExpr( + kind::APPLY_SELECTOR, + paramConsList["cons"].getSelector("head"), + a); + std::cout << "head_a is " << head_a << " of type " << head_a.getType() + << std::endl + << "type of cons is " + << paramConsList.getConstructor("cons").getType() << std::endl + << std::endl; + + Expr assertion = em.mkExpr(kind::GT, head_a, em.mkConst(Rational(50))); + std::cout << "Assert " << assertion << std::endl; + smt.assertFormula(assertion); + + std::cout << "Expect sat." << std::endl; + std::cout << "CVC4: " << smt.checkSat()<< std::endl; return 0; } -- cgit v1.2.3