3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-23 17:15:31 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2017-07-27 17:02:27 -07:00
commit b482dbd589
379 changed files with 7440 additions and 3352 deletions

80
examples/CMakeLists.txt Normal file
View file

@ -0,0 +1,80 @@
include(ExternalProject)
# Unfortunately `BUILD_ALWAYS` only seems to be supported with the version of ExternalProject
# that shipped with CMake >= 3.1.
if (("${CMAKE_VERSION}" VERSION_EQUAL "3.1") OR ("${CMAKE_VERSION}" VERSION_GREATER "3.1"))
set(EXTERNAL_PROJECT_BUILD_ALWAYS_ARG BUILD_ALWAYS 1)
else()
set(EXTERNAL_PROJECT_BUILD_ALWAYS_ARG "")
endif()
################################################################################
# Build example project using libz3's C API as an external project
################################################################################
ExternalProject_Add(c_example
DEPENDS libz3
# Configure step
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c"
CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}"
# Build step
${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG}
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/c_example_build_dir"
# Install Step
INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
)
set_target_properties(c_example PROPERTIES EXCLUDE_FROM_ALL TRUE)
################################################################################
# Build maxsat example project using libz3's C API as an external project
################################################################################
ExternalProject_Add(c_maxsat_example
DEPENDS libz3
# Configure step
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/maxsat"
CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}"
# Build step
${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG}
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/c_maxsat_example_build_dir"
# Install Step
INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
)
set_target_properties(c_maxsat_example PROPERTIES EXCLUDE_FROM_ALL TRUE)
################################################################################
# Build example project using libz3's C++ API as an external project
################################################################################
ExternalProject_Add(cpp_example
DEPENDS libz3
# Configure step
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/c++"
CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}"
# Build step
${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG}
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/cpp_example_build_dir"
# Install Step
INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
)
set_target_properties(cpp_example PROPERTIES EXCLUDE_FROM_ALL TRUE)
################################################################################
# Build example tptp5 project using libz3's C++ API as an external project
################################################################################
ExternalProject_Add(z3_tptp5
DEPENDS libz3
# Configure step
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tptp"
CMAKE_ARGS "-DZ3_DIR=${CMAKE_BINARY_DIR}"
# Build step
${EXTERNAL_PROJECT_BUILD_ALWAYS_ARG}
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/tptp_build_dir"
# Install Step
INSTALL_COMMAND "${CMAKE_COMMAND}" -E echo "" # Dummy command
)
set_target_properties(z3_tptp5 PROPERTIES EXCLUDE_FROM_ALL TRUE)
################################################################################
# Build Python examples
################################################################################
if (BUILD_PYTHON_BINDINGS)
add_subdirectory(python)
endif()

View file

@ -0,0 +1,38 @@
################################################################################
# Example C++ project
################################################################################
project(Z3_C_EXAMPLE CXX)
cmake_minimum_required(VERSION 2.8.12)
find_package(Z3
REQUIRED
CONFIG
# `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3.
# This should prevent us from accidently picking up an installed
# copy of Z3. This is here to benefit Z3's build sytem when building
# this project. When making your own project you probably shouldn't
# use this option.
NO_DEFAULT_PATH
)
message(STATUS "Z3_FOUND: ${Z3_FOUND}")
message(STATUS "Found Z3 ${Z3_VERSION_STRING}")
message(STATUS "Z3_DIR: ${Z3_DIR}")
add_executable(cpp_example example.cpp)
target_include_directories(cpp_example PRIVATE ${Z3_CXX_INCLUDE_DIRS})
target_link_libraries(cpp_example PRIVATE ${Z3_LIBRARIES})
if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows")
# On Windows we need to copy the Z3 libraries
# into the same directory as the executable
# so that they can be found.
foreach (z3_lib ${Z3_LIBRARIES})
message(STATUS "Adding copy rule for ${z3_lib}")
add_custom_command(TARGET cpp_example
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:${z3_lib}>
$<TARGET_FILE_DIR:cpp_example>
)
endforeach()
endif()

42
examples/c/CMakeLists.txt Normal file
View file

@ -0,0 +1,42 @@
################################################################################
# Example C project
################################################################################
# NOTE: Even though this is a C project, libz3 uses C++. When using libz3
# as a static library if we don't configure this project to also support
# C++ we will use the C linker rather than the C++ linker and will not link
# the C++ standard library in resulting in a link failure.
project(Z3_C_EXAMPLE C CXX)
cmake_minimum_required(VERSION 2.8.12)
find_package(Z3
REQUIRED
CONFIG
# `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3.
# This should prevent us from accidently picking up an installed
# copy of Z3. This is here to benefit Z3's build sytem when building
# this project. When making your own project you probably shouldn't
# use this option.
NO_DEFAULT_PATH
)
message(STATUS "Z3_FOUND: ${Z3_FOUND}")
message(STATUS "Found Z3 ${Z3_VERSION_STRING}")
message(STATUS "Z3_DIR: ${Z3_DIR}")
add_executable(c_example test_capi.c)
target_include_directories(c_example PRIVATE ${Z3_C_INCLUDE_DIRS})
target_link_libraries(c_example PRIVATE ${Z3_LIBRARIES})
if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows")
# On Windows we need to copy the Z3 libraries
# into the same directory as the executable
# so that they can be found.
foreach (z3_lib ${Z3_LIBRARIES})
message(STATUS "Adding copy rule for ${z3_lib}")
add_custom_command(TARGET c_example
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:${z3_lib}>
$<TARGET_FILE_DIR:c_example>
)
endforeach()
endif()

View file

@ -2152,6 +2152,31 @@ namespace test_mapi
Console.WriteLine("OK, model: {0}", s.Model.ToString());
}
public static void TranslationExample()
{
Context ctx1 = new Context();
Context ctx2 = new Context();
Sort s1 = ctx1.IntSort;
Sort s2 = ctx2.IntSort;
Sort s3 = s1.Translate(ctx2);
Console.WriteLine(s1 == s2);
Console.WriteLine(s1.Equals(s2));
Console.WriteLine(s2.Equals(s3));
Console.WriteLine(s1.Equals(s3));
Expr e1 = ctx1.MkIntConst("e1");
Expr e2 = ctx2.MkIntConst("e1");
Expr e3 = e1.Translate(ctx2);
Console.WriteLine(e1 == e2);
Console.WriteLine(e1.Equals(e2));
Console.WriteLine(e2.Equals(e3));
Console.WriteLine(e1.Equals(e3));
}
static void Main(string[] args)
{
try
@ -2225,6 +2250,8 @@ namespace test_mapi
QuantifierExample4(ctx);
}
TranslationExample();
Log.Close();
if (Log.isOpen())
Console.WriteLine("Log is still open!");

View file

@ -281,7 +281,7 @@ class JavaExample
}
void disprove(Context ctx, BoolExpr f, boolean useMBQI)
throws TestFailedException
throws TestFailedException
{
BoolExpr[] a = {};
disprove(ctx, f, useMBQI, a);
@ -2279,6 +2279,29 @@ class JavaExample
System.out.println(my);
}
public void translationExample() {
Context ctx1 = new Context();
Context ctx2 = new Context();
Sort s1 = ctx1.getIntSort();
Sort s2 = ctx2.getIntSort();
Sort s3 = s1.translate(ctx2);
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
System.out.println(s1.equals(s3));
Expr e1 = ctx1.mkIntConst("e1");
Expr e2 = ctx2.mkIntConst("e1");
Expr e3 = e1.translate(ctx2);
System.out.println(e1 == e2);
System.out.println(e1.equals(e2));
System.out.println(e2.equals(e3));
System.out.println(e1.equals(e3));
}
public static void main(String[] args)
{
JavaExample p = new JavaExample();
@ -2300,8 +2323,8 @@ class JavaExample
HashMap<String, String> cfg = new HashMap<String, String>();
cfg.put("model", "true");
Context ctx = new Context(cfg);
p.optimizeExample(ctx);
p.optimizeExample(ctx);
p.basicTests(ctx);
p.castingTest(ctx);
p.sudokuExample(ctx);
@ -2355,7 +2378,9 @@ class JavaExample
Context ctx = new Context(cfg);
p.quantifierExample3(ctx);
p.quantifierExample4(ctx);
}
}
p.translationExample();
Log.close();
if (Log.isOpen())

View file

@ -0,0 +1,42 @@
################################################################################
# Example maxsat project
################################################################################
# NOTE: Even though this is a C project, libz3 uses C++. When using libz3
# as a static library if we don't configure this project to also support
# C++ we will use the C linker rather than the C++ linker and will not link
# the C++ standard library in resulting in a link failure.
project(Z3_C_MAXSAT_EXAMPLE C CXX)
cmake_minimum_required(VERSION 2.8.12)
find_package(Z3
REQUIRED
CONFIG
# `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3.
# This should prevent us from accidently picking up an installed
# copy of Z3. This is here to benefit Z3's build sytem when building
# this project. When making your own project you probably shouldn't
# use this option.
NO_DEFAULT_PATH
)
message(STATUS "Z3_FOUND: ${Z3_FOUND}")
message(STATUS "Found Z3 ${Z3_VERSION_STRING}")
message(STATUS "Z3_DIR: ${Z3_DIR}")
add_executable(c_maxsat_example maxsat.c)
target_include_directories(c_maxsat_example PRIVATE ${Z3_C_INCLUDE_DIRS})
target_link_libraries(c_maxsat_example PRIVATE ${Z3_LIBRARIES})
if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows")
# On Windows we need to copy the Z3 libraries
# into the same directory as the executable
# so that they can be found.
foreach (z3_lib ${Z3_LIBRARIES})
message(STATUS "Adding copy rule for ${z3_lib}")
add_custom_command(TARGET c_maxsat_example
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:${z3_lib}>
$<TARGET_FILE_DIR:c_maxsat_example>
)
endforeach()
endif()

View file

@ -348,7 +348,15 @@ void assert_at_most_k(Z3_context ctx, Z3_solver s, unsigned n, Z3_ast * lits, un
*/
void assert_at_most_one(Z3_context ctx, Z3_solver s, unsigned n, Z3_ast * lits)
{
assert_at_most_k(ctx, s, n, lits, 1);
assert_at_most_k(ctx, s, n, lits, 1);
}
Z3_solver mk_solver(Z3_context ctx)
{
Z3_solver r = Z3_mk_solver(ctx);
Z3_solver_inc_ref(ctx, r);
return r;
}
/**
@ -357,7 +365,7 @@ void assert_at_most_one(Z3_context ctx, Z3_solver s, unsigned n, Z3_ast * lits)
void tst_at_most_one()
{
Z3_context ctx = mk_context();
Z3_solver s = Z3_mk_solver(ctx);
Z3_solver s = mk_solver(ctx);
Z3_ast k1 = mk_bool_var(ctx, "k1");
Z3_ast k2 = mk_bool_var(ctx, "k2");
Z3_ast k3 = mk_bool_var(ctx, "k3");
@ -376,7 +384,9 @@ void tst_at_most_one()
if (result != Z3_L_TRUE)
error("BUG");
m = Z3_solver_get_model(ctx, s);
Z3_model_inc_ref(ctx, m);
printf("model:\n%s\n", Z3_model_to_string(ctx, m));
Z3_model_dec_ref(ctx, m);
Z3_solver_assert(ctx, s, mk_binary_or(ctx, k2, k3));
Z3_solver_assert(ctx, s, mk_binary_or(ctx, k1, k6));
printf("it must be sat...\n");
@ -384,12 +394,15 @@ void tst_at_most_one()
if (result != Z3_L_TRUE)
error("BUG");
m = Z3_solver_get_model(ctx, s);
Z3_model_inc_ref(ctx, m);
printf("model:\n%s\n", Z3_model_to_string(ctx, m));
Z3_solver_assert(ctx, s, mk_binary_or(ctx, k4, k5));
printf("it must be unsat...\n");
result = Z3_solver_check(ctx, s);
if (result != Z3_L_FALSE)
error("BUG");
Z3_model_dec_ref(ctx, m);
Z3_solver_dec_ref(ctx, s);
Z3_del_context(ctx);
}
@ -454,8 +467,10 @@ int naive_maxsat(Z3_context ctx, Z3_solver s, unsigned num_hard_cnstrs, Z3_ast *
printf("unsat\n");
return num_soft_cnstrs - k - 1;
}
m = Z3_solver_get_model(ctx, s);
m = Z3_solver_get_model(ctx, s);
Z3_model_inc_ref(ctx, m);
num_disabled = get_num_disabled_soft_constraints(ctx, m, num_soft_cnstrs, aux_vars);
Z3_model_dec_ref(ctx, m);
if (num_disabled > k) {
error("BUG");
}
@ -506,6 +521,7 @@ int fu_malik_maxsat_step(Z3_context ctx, Z3_solver s, unsigned num_soft_cnstrs,
}
else {
core = Z3_solver_get_unsat_core(ctx, s);
Z3_ast_vector_inc_ref(ctx, core);
core_size = Z3_ast_vector_size(ctx, core);
block_vars = (Z3_ast*) malloc(sizeof(Z3_ast) * core_size);
k = 0;
@ -514,7 +530,7 @@ int fu_malik_maxsat_step(Z3_context ctx, Z3_solver s, unsigned num_soft_cnstrs,
unsigned j;
// check whether assumption[i] is in the core or not
for (j = 0; j < core_size; j++) {
if (assumptions[i] == Z3_ast_vector_get(ctx, core, j))
if (assumptions[i] == Z3_ast_vector_get(ctx, core, j))
break;
}
if (j < core_size) {
@ -531,6 +547,7 @@ int fu_malik_maxsat_step(Z3_context ctx, Z3_solver s, unsigned num_soft_cnstrs,
}
}
assert_at_most_one(ctx, s, k, block_vars);
Z3_ast_vector_dec_ref(ctx, core);
return 0; // not done.
}
}
@ -597,7 +614,7 @@ int smtlib_maxsat(char * file_name, int approach)
Z3_ast * hard_cnstrs, * soft_cnstrs;
unsigned result = 0;
ctx = mk_context();
s = Z3_mk_solver(ctx);
s = mk_solver(ctx);
Z3_parse_smtlib_file(ctx, file_name, 0, 0, 0, 0, 0, 0);
hard_cnstrs = get_hard_constraints(ctx, &num_hard_cnstrs);
soft_cnstrs = get_soft_constraints(ctx, &num_soft_cnstrs);
@ -615,6 +632,7 @@ int smtlib_maxsat(char * file_name, int approach)
}
free_cnstr_array(hard_cnstrs);
free_cnstr_array(soft_cnstrs);
Z3_solver_dec_ref(ctx, s);
return result;
}

View file

@ -0,0 +1,33 @@
set(python_example_files
all_interval_series.py
complex/complex.py
example.py
hamiltonian/hamiltonian.py
mus/marco.py
mus/mss.py
socrates.py
visitor.py
)
set(z3py_bindings_build_dest "${CMAKE_BINARY_DIR}/python")
set(build_z3_python_examples_target_depends "")
foreach (example_file ${python_example_files})
add_custom_command(OUTPUT "${z3py_bindings_build_dest}/${example_file}"
COMMAND "${CMAKE_COMMAND}" "-E" "copy"
"${CMAKE_CURRENT_SOURCE_DIR}/${example_file}"
# We flatten the hierarchy so that all python files have
# the `z3` directory in their directory so that their import
# statements "just work".
"${z3py_bindings_build_dest}/"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${example_file}"
COMMENT "Copying \"${example_file}\" to ${z3py_bindings_build_dest}/${example_file}"
)
list(APPEND build_z3_python_examples_target_depends "${z3py_bindings_build_dest}/${example_file}")
endforeach()
add_custom_target(build_z3_python_examples
ALL
DEPENDS
${build_z3_python_examples_target_depends}
)

View file

@ -4,7 +4,7 @@
# adjacent entries fall in the range 0,..,n-1
# This is known as the "The All-Interval Series Problem"
# See http://www.csplib.org/Problems/prob007/
from __future__ import print_function
from z3 import *
import time
@ -56,7 +56,7 @@ def process_model(s, xij, n):
block += [xij[i][j]]
k = j
values += [k]
print values
print(values)
sys.stdout.flush()
return block
@ -68,9 +68,9 @@ def all_models(n):
block = process_model(s, xij, n)
s.add(Not(And(block)))
count += 1
print s.statistics()
print time.clock() - start
print count
print(s.statistics())
print(time.clock() - start)
print(count)
set_option(verbose=1)
all_models(12)

View file

@ -6,6 +6,10 @@
#
# Author: Leonardo de Moura (leonardo)
############################################
from __future__ import print_function
import sys
if sys.version_info.major >= 3:
from functools import reduce
from z3 import *
def _to_complex(a):
@ -53,7 +57,7 @@ class ComplexExpr:
return self
if k < 0:
return (self ** (-k)).inv()
return reduce(lambda x, y: x * y, [self for _ in xrange(k)], ComplexExpr(1, 0))
return reduce(lambda x, y: x * y, [self for _ in range(k)], ComplexExpr(1, 0))
def inv(self):
den = self.r*self.r + self.i*self.i
@ -63,6 +67,12 @@ class ComplexExpr:
inv_other = _to_complex(other).inv()
return self.__mul__(inv_other)
if sys.version_info.major >= 3:
# In python 3 the meaning of the '/' operator
# was changed.
def __truediv__(self, other):
return self.__div__(other)
def __rdiv__(self, other):
other = _to_complex(other)
return self.inv().__mul__(other)
@ -113,5 +123,5 @@ print(s.model())
s.add(x.i != 1)
print(s.check())
# print(s.model())
print ((3 + I) ** 2)/(5 - I)
print ((3 + I) ** -3)/(5 - I)
print(((3 + I) ** 2)/(5 - I))
print(((3 + I) ** -3)/(5 - I))

View file

@ -45,11 +45,6 @@ def enumerate_sets(solver):
else:
break
class CompareSetSize():
def __call__(self, s1, s2):
return len(s1) < len(s2)
class MSSSolver:
s = Solver()
varcache = {}
@ -157,7 +152,7 @@ class MSSSolver:
mcs = [x for x in self.orig_soft_vars if not is_true(self.model[x])]
self.s.add(Or(mcs))
core_literals = set([])
cores.sort(CompareSetSize())
cores.sort(key=lambda element: len(element))
for core in cores:
if len(core & core_literals) == 0:
self.relax_core(core)

View file

@ -1,5 +1,5 @@
# Copyright (c) Microsoft Corporation 2015
from __future__ import print_function
from z3 import *
def visitor(e, seen):
@ -22,8 +22,8 @@ fml = x + x + y > 2
seen = {}
for e in visitor(fml, seen):
if is_const(e) and e.decl().kind() == Z3_OP_UNINTERPRETED:
print "Variable", e
print("Variable", e)
else:
print e
print(e)

View file

@ -0,0 +1,39 @@
################################################################################
# TPTP example
################################################################################
project(Z3_TPTP5 CXX)
cmake_minimum_required(VERSION 2.8.12)
find_package(Z3
REQUIRED
CONFIG
# `NO_DEFAULT_PATH` is set so that -DZ3_DIR has to be passed to find Z3.
# This should prevent us from accidently picking up an installed
# copy of Z3. This is here to benefit Z3's build sytem when building
# this project. When making your own project you probably shouldn't
# use this option.
NO_DEFAULT_PATH
)
message(STATUS "Z3_FOUND: ${Z3_FOUND}")
message(STATUS "Found Z3 ${Z3_VERSION_STRING}")
message(STATUS "Z3_DIR: ${Z3_DIR}")
add_executable(z3_tptp5 tptp5.cpp tptp5.lex.cpp)
target_include_directories(z3_tptp5 PRIVATE ${Z3_CXX_INCLUDE_DIRS})
target_link_libraries(z3_tptp5 PRIVATE ${Z3_LIBRARIES})
if ("${CMAKE_SYSTEM_NAME}" MATCHES "[Ww]indows")
# On Windows we need to copy the Z3 libraries
# into the same directory as the executable
# so that they can be found.
foreach (z3_lib ${Z3_LIBRARIES})
message(STATUS "Adding copy rule for ${z3_lib}")
add_custom_command(TARGET z3_tptp5
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:${z3_lib}>
$<TARGET_FILE_DIR:z3_tptp5>
)
endforeach()
endif()