3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-27 02:45:51 +00:00

ML/Java: Proper use of Datatype API for List/Enum/Constructor

Signed-off-by: Christoph M. Wintersteiger <cwinter@microsoft.com>
This commit is contained in:
Christoph M. Wintersteiger 2013-02-20 19:40:48 +00:00
parent 18bae81731
commit 6075ae28fc
6 changed files with 109 additions and 189 deletions

View file

@ -16,8 +16,7 @@ public class Constructor extends Z3Object
* @throws Z3Exception
**/
public int getNumFields() throws Z3Exception
{
init();
{
return n;
}
@ -27,8 +26,11 @@ public class Constructor extends Z3Object
**/
public FuncDecl ConstructorDecl() throws Z3Exception
{
init();
return m_constructorDecl;
Native.LongPtr constructor = new Native.LongPtr();
Native.LongPtr tester = new Native.LongPtr();
long[] accessors = new long[n];
Native.queryConstructor(getContext().nCtx(), getNativeObject(), n, constructor, tester, accessors);
return new FuncDecl(getContext(), constructor.value);
}
/**
@ -37,8 +39,11 @@ public class Constructor extends Z3Object
**/
public FuncDecl getTesterDecl() throws Z3Exception
{
init();
return m_testerDecl;
Native.LongPtr constructor = new Native.LongPtr();
Native.LongPtr tester = new Native.LongPtr();
long[] accessors = new long[n];
Native.queryConstructor(getContext().nCtx(), getNativeObject(), n, constructor, tester, accessors);
return new FuncDecl(getContext(), tester.value);
}
/**
@ -47,8 +52,14 @@ public class Constructor extends Z3Object
**/
public FuncDecl[] getAccessorDecls() throws Z3Exception
{
init();
return m_accessorDecls;
Native.LongPtr constructor = new Native.LongPtr();
Native.LongPtr tester = new Native.LongPtr();
long[] accessors = new long[n];
Native.queryConstructor(getContext().nCtx(), getNativeObject(), n, constructor, tester, accessors);
FuncDecl[] t = new FuncDecl[n];
for (int i = 0; i < n; i++)
t[i] = new FuncDecl(getContext(), accessors[i]);
return t;
}
/**
@ -60,9 +71,6 @@ public class Constructor extends Z3Object
}
private int n = 0;
private FuncDecl m_testerDecl = null;
private FuncDecl m_constructorDecl = null;
private FuncDecl[] m_accessorDecls = null;
Constructor(Context ctx, Symbol name, Symbol recognizer,
Symbol[] fieldNames, Sort[] sorts, int[] sortRefs)
@ -87,21 +95,4 @@ public class Constructor extends Z3Object
Sort.arrayToNative(sorts), sortRefs));
}
private void init() throws Z3Exception
{
if (m_testerDecl != null)
return;
Native.LongPtr constructor = new Native.LongPtr();
Native.LongPtr tester = new Native.LongPtr();
long[] accessors = new long[n];
Native.queryConstructor(getContext().nCtx(), getNativeObject(), n,
constructor, tester, accessors);
m_constructorDecl = new FuncDecl(getContext(), constructor.value);
m_testerDecl = new FuncDecl(getContext(), tester.value);
m_accessorDecls = new FuncDecl[n];
for (int i = 0; i < n; i++)
m_accessorDecls[i] = new FuncDecl(getContext(), accessors[i]);
}
}

View file

@ -14,30 +14,39 @@ public class EnumSort extends Sort
/**
* The function declarations of the constants in the enumeration.
**/
public FuncDecl[] getConstDecls()
public FuncDecl[] getConstDecls() throws Z3Exception
{
return _constdecls;
int n = Native.getDatatypeSortNumConstructors(getContext().nCtx(), getNativeObject());
FuncDecl[] t = new FuncDecl[n];
for (int i = 0; i < n; i++)
t[i] = new FuncDecl(getContext(), Native.getDatatypeSortConstructor(getContext().nCtx(), getNativeObject(), i));
return t;
}
/**
* The constants in the enumeration.
**/
public Expr[] getConsts()
{
return _consts;
public Expr[] getConsts() throws Z3Exception
{
FuncDecl[] cds = getConstDecls();
Expr[] t = new Expr[cds.length];
for (int i = 0; i < t.length; i++)
t[i] = getContext().mkApp(cds[i]);
return t;
}
/**
* The test predicates for the constants in the enumeration.
**/
public FuncDecl[] getTesterDecls()
public FuncDecl[] getTesterDecls() throws Z3Exception
{
return _testerdecls;
int n = Native.getDatatypeSortNumConstructors(getContext().nCtx(), getNativeObject());
FuncDecl[] t = new FuncDecl[n];
for (int i = 0; i < n; i++)
t[i] = new FuncDecl(getContext(), Native.getDatatypeSortRecognizer(getContext().nCtx(), getNativeObject(), i));
return t;
}
private FuncDecl[] _constdecls = null, _testerdecls = null;
private Expr[] _consts = null;
EnumSort(Context ctx, Symbol name, Symbol[] enumNames) throws Z3Exception
{
super(ctx);
@ -47,15 +56,6 @@ public class EnumSort extends Sort
long[] n_testers = new long[n];
setNativeObject(Native.mkEnumerationSort(ctx.nCtx(),
name.getNativeObject(), (int) n, Symbol.arrayToNative(enumNames),
n_constdecls, n_testers));
_constdecls = new FuncDecl[n];
for (int i = 0; i < n; i++)
_constdecls[i] = new FuncDecl(ctx, n_constdecls[i]);
_testerdecls = new FuncDecl[n];
for (int i = 0; i < n; i++)
_testerdecls[i] = new FuncDecl(ctx, n_testers[i]);
_consts = new Expr[n];
for (int i = 0; i < n; i++)
_consts[i] = ctx.mkApp(_constdecls[i], (Expr[])null);
n_constdecls, n_testers));
}
};

View file

@ -13,65 +13,68 @@ public class ListSort extends Sort
{
/**
* The declaration of the nil function of this list sort.
* @throws Z3Exception
**/
public FuncDecl getNilDecl()
public FuncDecl getNilDecl() throws Z3Exception
{
return nilDecl;
return new FuncDecl(getContext(), Native.getDatatypeSortConstructor(getContext().nCtx(), getNativeObject(), 0));
}
/**
* The empty list.
* @throws Z3Exception
**/
public Expr getNil()
public Expr getNil() throws Z3Exception
{
return nilConst;
return getContext().mkApp(getNilDecl());
}
/**
* The declaration of the isNil function of this list sort.
* @throws Z3Exception
**/
public FuncDecl getIsNilDecl()
public FuncDecl getIsNilDecl() throws Z3Exception
{
return isNilDecl;
return new FuncDecl(getContext(), Native.getDatatypeSortRecognizer(getContext().nCtx(), getNativeObject(), 0));
}
/**
* The declaration of the cons function of this list sort.
* @throws Z3Exception
**/
public FuncDecl getConsDecl()
public FuncDecl getConsDecl() throws Z3Exception
{
return consDecl;
return new FuncDecl(getContext(), Native.getDatatypeSortConstructor(getContext().nCtx(), getNativeObject(), 1));
}
/**
* The declaration of the isCons function of this list sort.
* @throws Z3Exception
*
**/
public FuncDecl getIsConsDecl()
public FuncDecl getIsConsDecl() throws Z3Exception
{
return isConsDecl;
return new FuncDecl(getContext(), Native.getDatatypeSortRecognizer(getContext().nCtx(), getNativeObject(), 1));
}
/**
* The declaration of the head function of this list sort.
* @throws Z3Exception
**/
public FuncDecl getHeadDecl()
public FuncDecl getHeadDecl() throws Z3Exception
{
return headDecl;
return new FuncDecl(getContext(), Native.getDatatypeSortConstructorAccessor(getContext().nCtx(), getNativeObject(), 1, 0));
}
/**
* The declaration of the tail function of this list sort.
* @throws Z3Exception
**/
public FuncDecl getTailDecl()
public FuncDecl getTailDecl() throws Z3Exception
{
return tailDecl;
return new FuncDecl(getContext(), Native.getDatatypeSortConstructorAccessor(getContext().nCtx(), getNativeObject(), 1, 1));
}
private FuncDecl nilDecl, isNilDecl, consDecl, isConsDecl, headDecl,
tailDecl;
private Expr nilConst;
ListSort(Context ctx, Symbol name, Sort elemSort) throws Z3Exception
{
super(ctx);
@ -83,12 +86,5 @@ public class ListSort extends Sort
setNativeObject(Native.mkListSort(ctx.nCtx(), name.getNativeObject(),
elemSort.getNativeObject(), inil, iisnil, icons, iiscons, ihead,
itail));
nilDecl = new FuncDecl(ctx, inil.value);
isNilDecl = new FuncDecl(ctx, iisnil.value);
consDecl = new FuncDecl(ctx, icons.value);
isConsDecl = new FuncDecl(ctx, iiscons.value);
headDecl = new FuncDecl(ctx, ihead.value);
tailDecl = new FuncDecl(ctx, itail.value);
nilConst = ctx.mkConst(nilDecl);
}
};