Search code examples
c++smtcvc4

How to define predicates using C++ API for CVC4


This is an example in native CVC language:

isblue: STRING -> BOOLEAN;
ASSERT isblue("sky");
ASSERT isblue("water");
QUERY isblue("sky"); //valid
QUERY isblue("pig"); //invalid

How would I write it using the C++ API for CVC4? Couldn't find any documentation on how to do this.


Solution

  • There are some API examples in the source distribution that might help you. In particular, examples/api/combination.cpp creates some functions and predicates and makes some assertions:

    https://github.com/CVC4/CVC4/blob/master/examples/api/combination.cpp

    In your case, you'll create a predicate type with ExprManager::mkFunctionType(), then you construct an "isblue" predicate with ExprManager::mkVar() giving it that type. It will look something like this (assuming you've done "using namespace CVC4" and #included <cvc4/cvc4.h>):

      ExprManager em;
      SmtEngine smt(&em);
    
      Type predType = em.mkFunctionType(em.stringType(), em.booleanType());
      Expr isblue = em.mkVar(predType);
    

    Then you can assert and query applications of your predicate:

      smt.assertFormula(em.mkExpr(kind::APPLY_UF, isblue, em.mkConst(String("sky"))));
      smt.assertFormula(em.mkExpr(kind::APPLY_UF, isblue, em.mkConst(String("water"))));
      smt.query(em.mkExpr(kind::APPLY_UF, isblue, em.mkConst(String("sky"))));
      smt.query(em.mkExpr(kind::APPLY_UF, isblue, em.mkConst(String("pig"))));