我正在尝试option
使用 Java API 在 CVC4 中定义参数化数据类型。
DATATYPE option[T] =
None
| Some(elem: T)
END;
我的问题是我不知道如何调用None
构造函数。我尝试了以下代码:
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;
}
}
这会导致以下错误:
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)
当我删除类型归属行时,它不会推断出正确的类型,所以我认为类型归属是必要的。这是我在没有类型说明的情况下得到的错误:
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)
如何使用 Java API 正确创建此数据类型和公式?