3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-12 04:03:39 +00:00

Dispose of intermediate Z3Objects created in dotnet api. (#5901)

* Dispose of intermediate Z3Objects created in dotnet api.

* Set C# LangVersion to 8.0.

* Fix build errors.

* Fix warning about empty using statement.

* Fix Xor to only dispose of objects that it creates internally.
This commit is contained in:
Matt Thornton 2022-03-17 15:08:05 +00:00 committed by GitHub
parent bdf7de1703
commit 4e0a2f5968
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 360 additions and 142 deletions

View file

@ -1682,6 +1682,7 @@ class DotNetDLLComponent(Component):
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework> <TargetFramework>netstandard1.4</TargetFramework>
<LangVersion>8.0</LangVersion>
<DefineConstants>$(DefineConstants);DOTNET_CORE</DefineConstants> <DefineConstants>$(DefineConstants);DOTNET_CORE</DefineConstants>
<DebugType>portable</DebugType> <DebugType>portable</DebugType>
<AssemblyName>Microsoft.Z3</AssemblyName> <AssemblyName>Microsoft.Z3</AssemblyName>

View file

@ -100,7 +100,7 @@ namespace Microsoft.Z3
{ {
get get
{ {
ASTVector res = new ASTVector(Context, Native.Z3_ast_map_keys(Context.nCtx, NativeObject)); using ASTVector res = new ASTVector(Context, Native.Z3_ast_map_keys(Context.nCtx, NativeObject));
return res.ToArray(); return res.ToArray();
} }
} }

View file

@ -22,7 +22,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
namespace Microsoft.Z3 namespace Microsoft.Z3
{ {
/// <summary> /// <summary>
@ -41,24 +40,48 @@ namespace Microsoft.Z3
#region Operators #region Operators
private static ArithExpr MkNum(ArithExpr e, int i) { return (ArithExpr)e.Context.MkNumeral(i, e.Context.MkIntSort()); } private static ArithExpr MkNum(ArithExpr e, int i)
{
using var sort = e.Context.MkIntSort();
return (ArithExpr)e.Context.MkNumeral(i, sort);
}
private static ArithExpr MkNum(ArithExpr e, double d) { return (ArithExpr)e.Context.MkNumeral(d.ToString(), e.Context.MkRealSort()); } private static ArithExpr MkNum(ArithExpr e, double d)
{
using var sort = e.Context.MkRealSort();
return (ArithExpr)e.Context.MkNumeral(d.ToString(), sort);
}
/// <summary> Operator overloading for arithmetical division operator (over reals) </summary> /// <summary> Operator overloading for arithmetical division operator (over reals) </summary>
public static ArithExpr operator /(ArithExpr a, ArithExpr b) { return a.Context.MkDiv(a, b); } public static ArithExpr operator /(ArithExpr a, ArithExpr b) { return a.Context.MkDiv(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator /(ArithExpr a, int b) { return a / MkNum(a, b); } public static ArithExpr operator /(ArithExpr a, int b)
{
using var denominator = MkNum(a, b);
return a / denominator;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator /(ArithExpr a, double b) { return a / MkNum(a, b); } public static ArithExpr operator /(ArithExpr a, double b)
{
using var denominator = MkNum(a, b);
return a / denominator;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator /(int a, ArithExpr b) { return MkNum(b, a) / b; } public static ArithExpr operator /(int a, ArithExpr b)
{
using var numerator = MkNum(b, a);
return numerator / b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator /(double a, ArithExpr b) { return MkNum(b, a) / b; } public static ArithExpr operator /(double a, ArithExpr b)
{
using var numerator = MkNum(b, a);
return numerator / b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator -(ArithExpr a) { return a.Context.MkUnaryMinus(a); } public static ArithExpr operator -(ArithExpr a) { return a.Context.MkUnaryMinus(a); }
@ -67,106 +90,218 @@ namespace Microsoft.Z3
public static ArithExpr operator -(ArithExpr a, ArithExpr b) { return a.Context.MkSub(a, b); } public static ArithExpr operator -(ArithExpr a, ArithExpr b) { return a.Context.MkSub(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator -(ArithExpr a, int b) { return a - MkNum(a, b); } public static ArithExpr operator -(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a - rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator -(ArithExpr a, double b) { return a - MkNum(a, b); } public static ArithExpr operator -(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a - rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator -(int a, ArithExpr b) { return MkNum(b, a) - b; } public static ArithExpr operator -(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs - b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator -(double a, ArithExpr b) { return MkNum(b, a) - b; } public static ArithExpr operator -(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs - b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator +(ArithExpr a, ArithExpr b) { return a.Context.MkAdd(a, b); } public static ArithExpr operator +(ArithExpr a, ArithExpr b) { return a.Context.MkAdd(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator +(ArithExpr a, int b) { return a + MkNum(a, b); } public static ArithExpr operator +(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a + rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator +(ArithExpr a, double b) { return a + MkNum(a, b); } public static ArithExpr operator +(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a + rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator +(int a, ArithExpr b) { return MkNum(b, a) + b; } public static ArithExpr operator +(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs + b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator +(double a, ArithExpr b) { return MkNum(b, a) + b; } public static ArithExpr operator +(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs + b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator *(ArithExpr a, ArithExpr b) { return a.Context.MkMul(a, b); } public static ArithExpr operator *(ArithExpr a, ArithExpr b) { return a.Context.MkMul(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator *(ArithExpr a, int b) { return a * MkNum(a, b); } public static ArithExpr operator *(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a * rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator *(ArithExpr a, double b) { return a * MkNum(a, b); } public static ArithExpr operator *(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a * rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator *(int a, ArithExpr b) { return MkNum(b, a) * b; } public static ArithExpr operator *(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs * b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static ArithExpr operator *(double a, ArithExpr b) { return MkNum(b, a) * b; } public static ArithExpr operator *(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs * b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <=(ArithExpr a, ArithExpr b) { return a.Context.MkLe(a, b); } public static BoolExpr operator <=(ArithExpr a, ArithExpr b) { return a.Context.MkLe(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <=(ArithExpr a, int b) { return a <= MkNum(a, b); } public static BoolExpr operator <=(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a <= rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <=(ArithExpr a, double b) { return a <= MkNum(a, b); } public static BoolExpr operator <=(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a <= rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <=(int a, ArithExpr b) { return MkNum(b, a) <= b; } public static BoolExpr operator <=(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs <= b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <=(double a, ArithExpr b) { return MkNum(b, a) <= b; } public static BoolExpr operator <=(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs <= b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <(ArithExpr a, ArithExpr b) { return a.Context.MkLt(a, b); } public static BoolExpr operator <(ArithExpr a, ArithExpr b) { return a.Context.MkLt(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <(ArithExpr a, int b) { return a < MkNum(a, b); } public static BoolExpr operator <(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a < rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <(ArithExpr a, double b) { return a < MkNum(a, b); } public static BoolExpr operator <(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a < rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <(int a, ArithExpr b) { return MkNum(b, a) < b; } public static BoolExpr operator <(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs < b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator <(double a, ArithExpr b) { return MkNum(b, a) < b; } public static BoolExpr operator <(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs < b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >(ArithExpr a, ArithExpr b) { return a.Context.MkGt(a, b); } public static BoolExpr operator >(ArithExpr a, ArithExpr b) { return a.Context.MkGt(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >(ArithExpr a, int b) { return a > MkNum(a, b); } public static BoolExpr operator >(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a > rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >(ArithExpr a, double b) { return a > MkNum(a, b); } public static BoolExpr operator >(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a > rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >(int a, ArithExpr b) { return MkNum(b, a) > b; } public static BoolExpr operator >(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs > b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >(double a, ArithExpr b) { return MkNum(b, a) > b; } public static BoolExpr operator >(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs > b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >=(ArithExpr a, ArithExpr b) { return a.Context.MkGe(a, b); } public static BoolExpr operator >=(ArithExpr a, ArithExpr b) { return a.Context.MkGe(a, b); }
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >=(ArithExpr a, int b) { return a >= MkNum(a, b); } public static BoolExpr operator >=(ArithExpr a, int b)
{
using var rhs = MkNum(a, b);
return a >= rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >=(ArithExpr a, double b) { return a >= MkNum(a, b); } public static BoolExpr operator >=(ArithExpr a, double b)
{
using var rhs = MkNum(a, b);
return a >= rhs;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >=(int a, ArithExpr b) { return MkNum(b, a) >= b; } public static BoolExpr operator >=(int a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs >= b;
}
/// <summary> Operator overloading for arithmetical operator </summary> /// <summary> Operator overloading for arithmetical operator </summary>
public static BoolExpr operator >=(double a, ArithExpr b) { return MkNum(b, a) >= b; } public static BoolExpr operator >=(double a, ArithExpr b)
{
using var lhs = MkNum(b, a);
return lhs >= b;
}
#endregion #endregion
} }

View file

@ -18,10 +18,10 @@ Notes:
--*/ --*/
using System; using System;
using System.Diagnostics;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.InteropServices; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
namespace Microsoft.Z3 namespace Microsoft.Z3
{ {
@ -202,7 +202,7 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
public UninterpretedSort MkUninterpretedSort(string str) public UninterpretedSort MkUninterpretedSort(string str)
{ {
using var sym = MkSymbol(str); using var sym = MkSymbol(str);
return MkUninterpretedSort(sym); return MkUninterpretedSort(sym);
} }
@ -312,9 +312,18 @@ namespace Microsoft.Z3
public EnumSort MkEnumSort(string name, params string[] enumNames) public EnumSort MkEnumSort(string name, params string[] enumNames)
{ {
Debug.Assert(enumNames != null); Debug.Assert(enumNames != null);
using var sname = MkSymbol(name);
using var snames = MkSymbols(enumNames); var enumSymbols = MkSymbols(enumNames);
return new EnumSort(this, sname, snames); try
{
using var symbol = MkSymbol(name);
return new EnumSort(this, symbol, enumSymbols);
}
finally
{
foreach (var enumSymbol in enumSymbols)
enumSymbol.Dispose();
}
} }
/// <summary> /// <summary>
@ -338,8 +347,8 @@ namespace Microsoft.Z3
Debug.Assert(elemSort != null); Debug.Assert(elemSort != null);
CheckContextMatch(elemSort); CheckContextMatch(elemSort);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return new ListSort(this, sname, elemSort); return new ListSort(this, symbol, elemSort);
} }
/// <summary> /// <summary>
@ -366,8 +375,8 @@ namespace Microsoft.Z3
/// <param name="size">The size of the sort</param> /// <param name="size">The size of the sort</param>
public FiniteDomainSort MkFiniteDomainSort(string name, ulong size) public FiniteDomainSort MkFiniteDomainSort(string name, ulong size)
{ {
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return new FiniteDomainSort(this, sname, size); return new FiniteDomainSort(this, symbol, size);
} }
@ -401,10 +410,19 @@ namespace Microsoft.Z3
/// <returns></returns> /// <returns></returns>
public Constructor MkConstructor(string name, string recognizer, string[] fieldNames = null, Sort[] sorts = null, uint[] sortRefs = null) public Constructor MkConstructor(string name, string recognizer, string[] fieldNames = null, Sort[] sorts = null, uint[] sortRefs = null)
{ {
using var sname = MkSymbol(name);
using var srecognizer = MkSymbol(recognizer); using var nameSymbol = MkSymbol(name);
using var sfieldNames = MkSymbols(fieldNames); using var recognizerSymbol = MkSymbol(recognizer);
return new Constructor(this, sname, srecognizer, sfieldNames, sorts, sortRefs); var fieldSymbols = MkSymbols(fieldNames);
try
{
return new Constructor(this, nameSymbol, recognizerSymbol, fieldSymbols, sorts, sortRefs);
}
finally
{
foreach (var fieldSymbol in fieldSymbols)
fieldSymbol.Dispose();
}
} }
/// <summary> /// <summary>
@ -431,8 +449,8 @@ namespace Microsoft.Z3
Debug.Assert(constructors.All(c => c != null)); Debug.Assert(constructors.All(c => c != null));
CheckContextMatch<Constructor>(constructors); CheckContextMatch<Constructor>(constructors);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return new DatatypeSort(this, sname, constructors); return new DatatypeSort(this, symbol, constructors);
} }
/// <summary> /// <summary>
@ -480,8 +498,17 @@ namespace Microsoft.Z3
Debug.Assert(names.Length == c.Length); Debug.Assert(names.Length == c.Length);
//Debug.Assert(Contract.ForAll(0, c.Length, j => c[j] != null)); //Debug.Assert(Contract.ForAll(0, c.Length, j => c[j] != null));
//Debug.Assert(names.All(name => name != null)); //Debug.Assert(names.All(name => name != null));
var snames = MkSymbols(names);
return MkDatatypeSorts(snames, c); var symbols = MkSymbols(names);
try
{
return MkDatatypeSorts(symbols, c);
}
finally
{
foreach (var symbol in symbols)
symbol.Dispose();
}
} }
/// <summary> /// <summary>
@ -542,8 +569,8 @@ namespace Microsoft.Z3
CheckContextMatch<Sort>(domain); CheckContextMatch<Sort>(domain);
CheckContextMatch(range); CheckContextMatch(range);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return new FuncDecl(this, sname, domain, range); return new FuncDecl(this, symbol, domain, range);
} }
/// <summary> /// <summary>
@ -556,8 +583,8 @@ namespace Microsoft.Z3
CheckContextMatch<Sort>(domain); CheckContextMatch<Sort>(domain);
CheckContextMatch(range); CheckContextMatch(range);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return new FuncDecl(this, sname, domain, range, true); return new FuncDecl(this, symbol, domain, range, true);
} }
/// <summary> /// <summary>
@ -585,9 +612,9 @@ namespace Microsoft.Z3
CheckContextMatch(domain); CheckContextMatch(domain);
CheckContextMatch(range); CheckContextMatch(range);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
Sort[] q = new Sort[] { domain }; Sort[] q = new Sort[] { domain };
return new FuncDecl(this, sname, q, range); return new FuncDecl(this, symbol, q, range);
} }
/// <summary> /// <summary>
@ -626,8 +653,8 @@ namespace Microsoft.Z3
Debug.Assert(range != null); Debug.Assert(range != null);
CheckContextMatch(range); CheckContextMatch(range);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return new FuncDecl(this, sname, null, range); return new FuncDecl(this, symbol, null, range);
} }
/// <summary> /// <summary>
@ -694,8 +721,9 @@ namespace Microsoft.Z3
public Expr MkConst(string name, Sort range) public Expr MkConst(string name, Sort range)
{ {
Debug.Assert(range != null); Debug.Assert(range != null);
using var sname = MkSymbol(name);
return MkConst(sname, range); using var symbol = MkSymbol(name);
return MkConst(symbol, range);
} }
/// <summary> /// <summary>
@ -736,8 +764,8 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
public BoolExpr MkBoolConst(string name) public BoolExpr MkBoolConst(string name)
{ {
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return (BoolExpr)MkConst(sname, BoolSort); return (BoolExpr)MkConst(symbol, BoolSort);
} }
/// <summary> /// <summary>
@ -785,8 +813,9 @@ namespace Microsoft.Z3
public BitVecExpr MkBVConst(Symbol name, uint size) public BitVecExpr MkBVConst(Symbol name, uint size)
{ {
Debug.Assert(name != null); Debug.Assert(name != null);
using var sort = MkBitVecSort(size);
return (BitVecExpr)MkConst(name, sort); using var sort = MkBitVecSort(size);
return (BitVecExpr)MkConst(name, sort);
} }
/// <summary> /// <summary>
@ -794,7 +823,7 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
public BitVecExpr MkBVConst(string name, uint size) public BitVecExpr MkBVConst(string name, uint size)
{ {
using var sort = MkBitVecSort(size); using var sort = MkBitVecSort(size);
return (BitVecExpr)MkConst(name, sort); return (BitVecExpr)MkConst(name, sort);
} }
#endregion #endregion
@ -956,17 +985,15 @@ namespace Microsoft.Z3
Debug.Assert(ts != null); Debug.Assert(ts != null);
Debug.Assert(ts.All(a => a != null)); Debug.Assert(ts.All(a => a != null));
CheckContextMatch<BoolExpr>(ts); CheckContextMatch<BoolExpr>(ts);
BoolExpr r = null;
foreach (var t in ts) return ts.Any()
{ ? ts
if (r == null) .Aggregate(MkFalse(), (r, t) =>
r = t; {
else using (r)
r = MkXor(r, t); return MkXor(r, t);
} })
if (r == null) : MkTrue();
r = MkTrue();
return r;
} }
/// <summary> /// <summary>
@ -2041,7 +2068,8 @@ namespace Microsoft.Z3
Debug.Assert(domain != null); Debug.Assert(domain != null);
Debug.Assert(range != null); Debug.Assert(range != null);
return (ArrayExpr)MkConst(name, MkArraySort(domain, range)); using var sort = MkArraySort(domain, range);
return (ArrayExpr)MkConst(name, sort);
} }
/// <summary> /// <summary>
@ -2051,9 +2079,10 @@ namespace Microsoft.Z3
{ {
Debug.Assert(domain != null); Debug.Assert(domain != null);
Debug.Assert(range != null); Debug.Assert(range != null);
using var sort = MkArraySort(domain, range);
using var sname = MkSymbol(name); using var symbol = MkSymbol(name);
return (ArrayExpr)MkConst(sname, sort); using var sort = MkArraySort(domain, range);
return (ArrayExpr)MkConst(symbol, sort);
} }
@ -3051,8 +3080,8 @@ namespace Microsoft.Z3
/// <param name="size">the size of the bit-vector</param> /// <param name="size">the size of the bit-vector</param>
public BitVecNum MkBV(string v, uint size) public BitVecNum MkBV(string v, uint size)
{ {
using var sort = MkBitVecSort(size);
return (BitVecNum)MkNumeral(v, MkBitVecSort(size)); return (BitVecNum)MkNumeral(v, sort);
} }
/// <summary> /// <summary>
@ -3062,8 +3091,8 @@ namespace Microsoft.Z3
/// <param name="size">the size of the bit-vector</param> /// <param name="size">the size of the bit-vector</param>
public BitVecNum MkBV(int v, uint size) public BitVecNum MkBV(int v, uint size)
{ {
using var sort = MkBitVecSort(size);
return (BitVecNum)MkNumeral(v, MkBitVecSort(size)); return (BitVecNum)MkNumeral(v, sort);
} }
/// <summary> /// <summary>
@ -3073,8 +3102,8 @@ namespace Microsoft.Z3
/// <param name="size">the size of the bit-vector</param> /// <param name="size">the size of the bit-vector</param>
public BitVecNum MkBV(uint v, uint size) public BitVecNum MkBV(uint v, uint size)
{ {
using var sort = MkBitVecSort(size);
return (BitVecNum)MkNumeral(v, MkBitVecSort(size)); return (BitVecNum)MkNumeral(v, sort);
} }
/// <summary> /// <summary>
@ -3084,8 +3113,8 @@ namespace Microsoft.Z3
/// <param name="size">the size of the bit-vector</param> /// <param name="size">the size of the bit-vector</param>
public BitVecNum MkBV(long v, uint size) public BitVecNum MkBV(long v, uint size)
{ {
using var sort = MkBitVecSort(size);
return (BitVecNum)MkNumeral(v, MkBitVecSort(size)); return (BitVecNum)MkNumeral(v, sort);
} }
/// <summary> /// <summary>
@ -3095,8 +3124,8 @@ namespace Microsoft.Z3
/// <param name="size">the size of the bit-vector</param> /// <param name="size">the size of the bit-vector</param>
public BitVecNum MkBV(ulong v, uint size) public BitVecNum MkBV(ulong v, uint size)
{ {
using var sort = MkBitVecSort(size);
return (BitVecNum)MkNumeral(v, MkBitVecSort(size)); return (BitVecNum)MkNumeral(v, sort);
} }
/// <summary> /// <summary>
@ -3343,7 +3372,7 @@ namespace Microsoft.Z3
uint cd = AST.ArrayLength(decls); uint cd = AST.ArrayLength(decls);
if (csn != cs || cdn != cd) if (csn != cs || cdn != cd)
throw new Z3Exception("Argument size mismatch"); throw new Z3Exception("Argument size mismatch");
ASTVector assertions = new ASTVector(this, Native.Z3_parse_smtlib2_string(nCtx, str, using ASTVector assertions = new ASTVector(this, Native.Z3_parse_smtlib2_string(nCtx, str,
AST.ArrayLength(sorts), Symbol.ArrayToNative(sortNames), AST.ArrayToNative(sorts), AST.ArrayLength(sorts), Symbol.ArrayToNative(sortNames), AST.ArrayToNative(sorts),
AST.ArrayLength(decls), Symbol.ArrayToNative(declNames), AST.ArrayToNative(decls))); AST.ArrayLength(decls), Symbol.ArrayToNative(declNames), AST.ArrayToNative(decls)));
return assertions.ToBoolExprArray(); return assertions.ToBoolExprArray();
@ -3362,7 +3391,7 @@ namespace Microsoft.Z3
uint cd = AST.ArrayLength(decls); uint cd = AST.ArrayLength(decls);
if (csn != cs || cdn != cd) if (csn != cs || cdn != cd)
throw new Z3Exception("Argument size mismatch"); throw new Z3Exception("Argument size mismatch");
ASTVector assertions = new ASTVector(this, Native.Z3_parse_smtlib2_file(nCtx, fileName, using ASTVector assertions = new ASTVector(this, Native.Z3_parse_smtlib2_file(nCtx, fileName,
AST.ArrayLength(sorts), Symbol.ArrayToNative(sortNames), AST.ArrayToNative(sorts), AST.ArrayLength(sorts), Symbol.ArrayToNative(sortNames), AST.ArrayToNative(sorts),
AST.ArrayLength(decls), Symbol.ArrayToNative(declNames), AST.ArrayToNative(decls))); AST.ArrayLength(decls), Symbol.ArrayToNative(declNames), AST.ArrayToNative(decls)));
return assertions.ToBoolExprArray(); return assertions.ToBoolExprArray();
@ -3847,8 +3876,8 @@ namespace Microsoft.Z3
/// <seealso cref="MkSolver(Symbol)"/> /// <seealso cref="MkSolver(Symbol)"/>
public Solver MkSolver(string logic) public Solver MkSolver(string logic)
{ {
using var slogic = MkSymbol(logic); using var symbol = MkSymbol(logic);
return MkSolver(slogic); return MkSolver(symbol);
} }
/// <summary> /// <summary>
@ -4937,16 +4966,16 @@ namespace Microsoft.Z3
Fixedpoint_DRQ.Clear(this); Fixedpoint_DRQ.Clear(this);
Optimize_DRQ.Clear(this); Optimize_DRQ.Clear(this);
if (m_boolSort != null) m_boolSort.Dispose(); if (m_boolSort != null) m_boolSort.Dispose();
if (m_intSort != null) m_intSort.Dispose(); if (m_intSort != null) m_intSort.Dispose();
if (m_realSort != null) m_realSort.Dispose(); if (m_realSort != null) m_realSort.Dispose();
if (m_stringSort != null) m_stringSort.Dispose(); if (m_stringSort != null) m_stringSort.Dispose();
if (m_charSort != null) m_charSort.Dispose(); if (m_charSort != null) m_charSort.Dispose();
m_boolSort = null; m_boolSort = null;
m_intSort = null; m_intSort = null;
m_realSort = null; m_realSort = null;
m_stringSort = null; m_stringSort = null;
m_charSort = null; m_charSort = null;
if (refCount == 0 && m_ctx != IntPtr.Zero) if (refCount == 0 && m_ctx != IntPtr.Zero)
{ {
m_n_err_handler = null; m_n_err_handler = null;

View file

@ -79,7 +79,7 @@ namespace Microsoft.Z3
FuncDecl[][] res = new FuncDecl[n][]; FuncDecl[][] res = new FuncDecl[n][];
for (uint i = 0; i < n; i++) for (uint i = 0; i < n; i++)
{ {
FuncDecl fd = new FuncDecl(Context, Native.Z3_get_datatype_sort_constructor(Context.nCtx, NativeObject, i)); using FuncDecl fd = new FuncDecl(Context, Native.Z3_get_datatype_sort_constructor(Context.nCtx, NativeObject, i));
uint ds = fd.DomainSize; uint ds = fd.DomainSize;
FuncDecl[] tmp = new FuncDecl[ds]; FuncDecl[] tmp = new FuncDecl[ds];
for (uint j = 0; j < ds; j++) for (uint j = 0; j < ds; j++)

View file

@ -74,7 +74,8 @@ namespace Microsoft.Z3
/// <returns></returns> /// <returns></returns>
public Expr Const(uint inx) public Expr Const(uint inx)
{ {
return Context.MkApp(ConstDecl(inx)); using var decl = ConstDecl(inx);
return Context.MkApp(decl);
} }
/// <summary> /// <summary>

View file

@ -255,7 +255,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_get_rules(Context.nCtx, NativeObject)); using ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_get_rules(Context.nCtx, NativeObject));
return av.ToBoolExprArray(); return av.ToBoolExprArray();
} }
} }
@ -268,7 +268,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_get_assertions(Context.nCtx, NativeObject)); using ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_get_assertions(Context.nCtx, NativeObject));
return av.ToBoolExprArray(); return av.ToBoolExprArray();
} }
} }
@ -292,7 +292,7 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
public BoolExpr[] ParseFile(string file) public BoolExpr[] ParseFile(string file)
{ {
ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_from_file(Context.nCtx, NativeObject, file)); using ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_from_file(Context.nCtx, NativeObject, file));
return av.ToBoolExprArray(); return av.ToBoolExprArray();
} }
@ -301,7 +301,7 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
public BoolExpr[] ParseString(string s) public BoolExpr[] ParseString(string s)
{ {
ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_from_string(Context.nCtx, NativeObject, s)); using ASTVector av = new ASTVector(Context, Native.Z3_fixedpoint_from_string(Context.nCtx, NativeObject, s));
return av.ToBoolExprArray(); return av.ToBoolExprArray();
} }

View file

@ -203,8 +203,8 @@ namespace Microsoft.Z3
/// <remarks>Essentially invokes the `simplify' tactic on the goal.</remarks> /// <remarks>Essentially invokes the `simplify' tactic on the goal.</remarks>
public Goal Simplify(Params p = null) public Goal Simplify(Params p = null)
{ {
Tactic t = Context.MkTactic("simplify"); using Tactic t = Context.MkTactic("simplify");
ApplyResult res = t.Apply(this, p); using ApplyResult res = t.Apply(this, p);
if (res.NumSubgoals == 0) if (res.NumSubgoals == 0)
throw new Z3Exception("No subgoals"); throw new Z3Exception("No subgoals");

View file

@ -32,6 +32,8 @@
<Authors>Microsoft</Authors> <Authors>Microsoft</Authors>
<Company>Microsoft</Company> <Company>Microsoft</Company>
<LangVersion>8.0</LangVersion>
</PropertyGroup> </PropertyGroup>
<!-- Code contract & signing properties --> <!-- Code contract & signing properties -->

View file

@ -87,7 +87,8 @@ namespace Microsoft.Z3
if (Native.Z3_is_as_array(Context.nCtx, n) == 0) if (Native.Z3_is_as_array(Context.nCtx, n) == 0)
throw new Z3Exception("Argument was not an array constant"); throw new Z3Exception("Argument was not an array constant");
IntPtr fd = Native.Z3_get_as_array_func_decl(Context.nCtx, n); IntPtr fd = Native.Z3_get_as_array_func_decl(Context.nCtx, n);
return FuncInterp(new FuncDecl(Context, fd)); using var decl = new FuncDecl(Context, fd);
return FuncInterp(decl);
} }
} }
else else
@ -241,7 +242,7 @@ namespace Microsoft.Z3
/// Evaluate expression to a double, assuming it is a numeral already. /// Evaluate expression to a double, assuming it is a numeral already.
/// </summary> /// </summary>
public double Double(Expr t) { public double Double(Expr t) {
var r = Eval(t, true); using var r = Eval(t, true);
return Native.Z3_get_numeral_double(Context.nCtx, r.NativeObject); return Native.Z3_get_numeral_double(Context.nCtx, r.NativeObject);
} }
@ -283,7 +284,7 @@ namespace Microsoft.Z3
{ {
Debug.Assert(s != null); Debug.Assert(s != null);
ASTVector av = new ASTVector(Context, Native.Z3_model_get_sort_universe(Context.nCtx, NativeObject, s.NativeObject)); using ASTVector av = new ASTVector(Context, Native.Z3_model_get_sort_universe(Context.nCtx, NativeObject, s.NativeObject));
return av.ToExprArray(); return av.ToExprArray();
} }

View file

@ -212,7 +212,7 @@ namespace Microsoft.Z3
public Handle AssertSoft(BoolExpr constraint, uint weight, string group) public Handle AssertSoft(BoolExpr constraint, uint weight, string group)
{ {
Context.CheckContextMatch(constraint); Context.CheckContextMatch(constraint);
Symbol s = Context.MkSymbol(group); using Symbol s = Context.MkSymbol(group);
return new Handle(this, Native.Z3_optimize_assert_soft(Context.nCtx, NativeObject, constraint.NativeObject, weight.ToString(), s.NativeObject)); return new Handle(this, Native.Z3_optimize_assert_soft(Context.nCtx, NativeObject, constraint.NativeObject, weight.ToString(), s.NativeObject));
} }
@ -289,7 +289,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector core = new ASTVector(Context, Native.Z3_optimize_get_unsat_core(Context.nCtx, NativeObject)); using ASTVector core = new ASTVector(Context, Native.Z3_optimize_get_unsat_core(Context.nCtx, NativeObject));
return core.ToBoolExprArray(); return core.ToBoolExprArray();
} }
} }
@ -337,7 +337,7 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
private Expr[] GetLowerAsVector(uint index) private Expr[] GetLowerAsVector(uint index)
{ {
ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_lower_as_vector(Context.nCtx, NativeObject, index)); using ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_lower_as_vector(Context.nCtx, NativeObject, index));
return v.ToExprArray(); return v.ToExprArray();
} }
@ -347,7 +347,7 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
private Expr[] GetUpperAsVector(uint index) private Expr[] GetUpperAsVector(uint index)
{ {
ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_upper_as_vector(Context.nCtx, NativeObject, index)); using ASTVector v = new ASTVector(Context, Native.Z3_optimize_get_upper_as_vector(Context.nCtx, NativeObject, index));
return v.ToExprArray(); return v.ToExprArray();
} }
@ -396,7 +396,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector assertions = new ASTVector(Context, Native.Z3_optimize_get_assertions(Context.nCtx, NativeObject)); using ASTVector assertions = new ASTVector(Context, Native.Z3_optimize_get_assertions(Context.nCtx, NativeObject));
return assertions.ToBoolExprArray(); return assertions.ToBoolExprArray();
} }
} }
@ -409,7 +409,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector objectives = new ASTVector(Context, Native.Z3_optimize_get_objectives(Context.nCtx, NativeObject)); using ASTVector objectives = new ASTVector(Context, Native.Z3_optimize_get_objectives(Context.nCtx, NativeObject));
return objectives.ToExprArray(); return objectives.ToExprArray();
} }
} }

View file

@ -62,7 +62,7 @@ namespace Microsoft.Z3
{ {
get get
{ {
IntNum n = Numerator; using IntNum n = Numerator;
return BigInteger.Parse(n.ToString()); return BigInteger.Parse(n.ToString());
} }
} }
@ -74,7 +74,7 @@ namespace Microsoft.Z3
{ {
get get
{ {
IntNum n = Denominator; using IntNum n = Denominator;
return BigInteger.Parse(n.ToString()); return BigInteger.Parse(n.ToString());
} }
} }

View file

@ -58,43 +58,92 @@ namespace Microsoft.Z3
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(string name, bool value) { Parameters = Context.MkParams().Add(name, value); } public void Set(string name, bool value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(string name, uint value) { Parameters = Context.MkParams().Add(name, value); } public void Set(string name, uint value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(string name, double value) { Parameters = Context.MkParams().Add(name, value); } public void Set(string name, double value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(string name, string value) { Parameters = Context.MkParams().Add(name, value); } public void Set(string name, string value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(string name, Symbol value) { Parameters = Context.MkParams().Add(name, value); } public void Set(string name, Symbol value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(Symbol name, bool value) { Parameters = Context.MkParams().Add(name, value); } public void Set(Symbol name, bool value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(Symbol name, uint value) { Parameters = Context.MkParams().Add(name, value); } public void Set(Symbol name, uint value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(Symbol name, double value) { Parameters = Context.MkParams().Add(name, value); } public void Set(Symbol name, double value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(Symbol name, string value) { Parameters = Context.MkParams().Add(name, value); } public void Set(Symbol name, string value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
/// <summary> /// <summary>
/// Sets parameter on the solver /// Sets parameter on the solver
/// </summary> /// </summary>
public void Set(Symbol name, Symbol value) { Parameters = Context.MkParams().Add(name, value); } public void Set(Symbol name, Symbol value)
{
using var parameters = Context.MkParams().Add(name, value);
Parameters = parameters;
}
@ -245,7 +294,7 @@ namespace Microsoft.Z3
{ {
get get
{ {
ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject)); using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
return assertions.Size; return assertions.Size;
} }
} }
@ -258,7 +307,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject)); using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_assertions(Context.nCtx, NativeObject));
return assertions.ToBoolExprArray(); return assertions.ToBoolExprArray();
} }
} }
@ -271,7 +320,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_units(Context.nCtx, NativeObject)); using ASTVector assertions = new ASTVector(Context, Native.Z3_solver_get_units(Context.nCtx, NativeObject));
return assertions.ToBoolExprArray(); return assertions.ToBoolExprArray();
} }
} }
@ -330,9 +379,9 @@ namespace Microsoft.Z3
/// </remarks> /// </remarks>
public Status Consequences(IEnumerable<BoolExpr> assumptions, IEnumerable<Expr> variables, out BoolExpr[] consequences) public Status Consequences(IEnumerable<BoolExpr> assumptions, IEnumerable<Expr> variables, out BoolExpr[] consequences)
{ {
ASTVector result = new ASTVector(Context); using ASTVector result = new ASTVector(Context);
ASTVector asms = new ASTVector(Context); using ASTVector asms = new ASTVector(Context);
ASTVector vars = new ASTVector(Context); using ASTVector vars = new ASTVector(Context);
foreach (var asm in assumptions) asms.Push(asm); foreach (var asm in assumptions) asms.Push(asm);
foreach (var v in variables) vars.Push(v); foreach (var v in variables) vars.Push(v);
Z3_lbool r = (Z3_lbool)Native.Z3_solver_get_consequences(Context.nCtx, NativeObject, asms.NativeObject, vars.NativeObject, result.NativeObject); Z3_lbool r = (Z3_lbool)Native.Z3_solver_get_consequences(Context.nCtx, NativeObject, asms.NativeObject, vars.NativeObject, result.NativeObject);
@ -391,7 +440,7 @@ namespace Microsoft.Z3
get get
{ {
ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject)); using ASTVector core = new ASTVector(Context, Native.Z3_solver_get_unsat_core(Context.nCtx, NativeObject));
return core.ToBoolExprArray(); return core.ToBoolExprArray();
} }
} }
@ -424,14 +473,14 @@ namespace Microsoft.Z3
/// </summary> /// </summary>
public IEnumerable<BoolExpr[]> Cube() public IEnumerable<BoolExpr[]> Cube()
{ {
ASTVector cv = new ASTVector(Context); using ASTVector cv = new ASTVector(Context);
if (CubeVariables != null) if (CubeVariables != null)
foreach (var b in CubeVariables) cv.Push(b); foreach (var b in CubeVariables) cv.Push(b);
while (true) { while (true) {
var lvl = BacktrackLevel; var lvl = BacktrackLevel;
BacktrackLevel = uint.MaxValue; BacktrackLevel = uint.MaxValue;
ASTVector r = new ASTVector(Context, Native.Z3_solver_cube(Context.nCtx, NativeObject, cv.NativeObject, lvl)); using ASTVector r = new ASTVector(Context, Native.Z3_solver_cube(Context.nCtx, NativeObject, cv.NativeObject, lvl));
var v = r.ToBoolExprArray(); var v = r.ToBoolExprArray();
CubeVariables = cv.ToBoolExprArray(); CubeVariables = cv.ToBoolExprArray();
if (v.Length == 1 && v[0].IsFalse) { if (v.Length == 1 && v[0].IsFalse) {