javacvc4

Invoking no-arg constructors of custom data types with type parameters in CVC4


I am trying to define a parameterized datatype option in CVC4 using the Java API.

 DATATYPE option[T] =
        None
      | Some(elem: T)
 END;

My problem is that I don't know how to invoke the None constructor. I tried the following code:

class Cvc4Test3 {

    public static void main(String... args) {
        Cvc4Solver.loadLibrary();
        ExprManager em = new ExprManager();
        SmtEngine smt = new SmtEngine(em);

        DatatypeType dt = createOptionDatatype(em);
        // instantiate to int
        DatatypeType dtInt = dt.instantiate(vector(em.integerType()));

        // x is an integer variable
        Expr x = em.mkVar("x", em.integerType());

        // create equation: None::option[INT] = Some(x)
        Expr l = em.mkExpr(Kind.APPLY_CONSTRUCTOR, dtInt.getConstructor("None"));
        Expr la = em.mkExpr(Kind.APPLY_TYPE_ASCRIPTION, em.mkConst(new AscriptionType(dtInt)), l);
        Expr r = em.mkExpr(Kind.APPLY_CONSTRUCTOR, dtInt.getConstructor("Some"), x);
        Expr eq = em.mkExpr(Kind.EQUAL, la, r);

        // Try to solve equation:
        smt.setOption("produce-models", new SExpr(true));
        smt.assertFormula(eq);
        Result res = smt.checkSat();
        System.out.println("res = " + res);
    }

    /**
     * DATATYPE option[T] =
     * None
     * | Some(elem: T)
     * END;
     */
    private static DatatypeType createOptionDatatype(ExprManager em) {
        Type t = em.mkSort("T");
        vectorType types = vector(t);
        Datatype dt = new Datatype("option", types);
        DatatypeConstructor cNone = new DatatypeConstructor("None");
        dt.addConstructor(cNone);
        DatatypeConstructor cSome = new DatatypeConstructor("Some");
        cSome.addArg("elem", t);
        dt.addConstructor(cSome);
        return em.mkDatatypeType(dt);
    }

    private static vectorType vector(Type... types) {
        vectorType res = new vectorType();
        for (Type t : types) {
            res.add(t);
        }
        return res;
    }
}

This results in the following error:

Exception in thread "main" java.lang.RuntimeException: matching failed for type ascription argument of parameterized datatype: matching failed for type ascription argument of parameterized datatype
    at edu.nyu.acsys.CVC4.CVC4JNI.SmtEngine_assertFormula__SWIG_1(Native Method)
    at edu.nyu.acsys.CVC4.SmtEngine.assertFormula(SmtEngine.java:149)

When I remove the type ascription line, it does not infer the correct type, so I assume the type ascription is necessary. Here i the error I get without the type ascription:

Exception in thread "main" java.lang.RuntimeException: Subexpressions must have a common base type:
Equation: None = Some(x)
Type 1: option[T]
Type 2: option[INT]
: Subexpressions must have a common base type:
Equation: None = Some(x)
Type 1: option[T]
Type 2: option[INT]

    at edu.nyu.acsys.CVC4.CVC4JNI.SmtEngine_assertFormula__SWIG_1(Native Method)
    at edu.nyu.acsys.CVC4.SmtEngine.assertFormula(SmtEngine.java:149)

How do I create this datatype and formula correctly using the Java API?


Solution

  • Got an answer from Andrew Reynolds on the CVC4 mailing list:

    First, notice that we have updated to a new API (https://github.com/CVC4/CVC4/blob/master/src/api/cvc4cpp.h).
    Coincidentally, I just submitted a PR that adds the interface for getting the required constructor you are looking for, in the new API: https://github.com/CVC4/CVC4/pull/4817

    If you are interested in the old API, your code is almost correct, however, there is a subtle difference in what we expect to be cast.

    In particular, in SMT-LIB casts are applied to constructor operators, not terms (you may be interested in this discussion https://github.com/Z3Prover/z3/issues/2135). This means that the AST of a casted nil term in CVC4 is: (APPLY_CONSTRUCTOR (APPLY_TYPE_ASCRIPTION None T)) not (APPLY_TYPE_ASCRIPTION (APPLY_CONSTRUCTOR None) option[Int]) Unfortunately, there is a complication in the old API about what T is. It is not "option[Int]", instead it is "the type of constructors of type option[Int]", or what we call a "constructor type".

    Here is the corrected code for creating the expression:

        // create equation: None::option[INT] = Some(x)
        Type noneInt = dtInt.getDatatype().get("None").getSpecializedConstructorType(dtInt);
        Expr la = em.mkExpr(Kind.APPLY_TYPE_ASCRIPTION, em.mkConst(new AscriptionType(noneInt)), dtInt.getConstructor("None"));
        Expr l = em.mkExpr(Kind.APPLY_CONSTRUCTOR, la);
        Expr r = em.mkExpr(Kind.APPLY_CONSTRUCTOR, dtInt.getConstructor("Some"), x);
        Expr eq = em.mkExpr(Kind.EQUAL, l, r);
    
    1. There is a difference between the type option[INT] and the constructor type option[INT]. The type ascription needs to use the constructor type.
    2. The ascription must go on the constructor and not on the applied constructor.