mirror of
https://github.com/Z3Prover/z3
synced 2025-04-23 17:15:31 +00:00
sketch cardinality plugin module
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
acb26d0cf9
commit
2853b322ca
5 changed files with 506 additions and 1 deletions
72
src/ast/card_decl_plugin.cpp
Normal file
72
src/ast/card_decl_plugin.cpp
Normal file
|
@ -0,0 +1,72 @@
|
|||
/*++
|
||||
Copyright (c) 2013 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
card_decl_plugin.cpp
|
||||
|
||||
Abstract:
|
||||
|
||||
Cardinality Constraints plugin
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2013-05-11
|
||||
|
||||
Revision History:
|
||||
|
||||
--*/
|
||||
|
||||
#include "card_decl_plugin.h"
|
||||
|
||||
card_decl_plugin::card_decl_plugin():
|
||||
m_at_most_sym("at_most")
|
||||
{}
|
||||
|
||||
func_decl * card_decl_plugin::mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters,
|
||||
unsigned arity, sort * const * domain, sort * range) {
|
||||
SASSERT(m_manager);
|
||||
ast_manager& m = *m_manager;
|
||||
for (unsigned i = 0; i < arity; ++i) {
|
||||
if (!m.is_bool(domain[i])) {
|
||||
m.raise_exception("invalid non-Boolean sort applied to 'at_most_k'");
|
||||
}
|
||||
}
|
||||
if (num_parameters != 1 || !parameters[0].is_int() || parameters[0].get_int() < 0) {
|
||||
m.raise_exception("function 'at_most_k' expects one non-negative integer parameter");
|
||||
}
|
||||
func_decl_info info(m_family_id, OP_AT_MOST_K, 1, parameters);
|
||||
return m.mk_func_decl(m_at_most_sym, arity, domain, m.mk_bool_sort(), info);
|
||||
}
|
||||
|
||||
void card_decl_plugin::get_op_names(svector<builtin_name> & op_names, symbol const & logic) {
|
||||
if (logic == symbol::null) {
|
||||
op_names.push_back(builtin_name("at-most-k", OP_AT_MOST_K));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
app * card_util::mk_at_most_k(unsigned num_args, expr * const * args, unsigned k) {
|
||||
parameter param(1);
|
||||
return m.mk_app(m_fid, OP_AT_MOST_K, 1, ¶m, num_args, args, m.mk_bool_sort());
|
||||
}
|
||||
|
||||
bool card_util::is_at_most_k(app *a) const {
|
||||
return is_app_of(a, m_fid, OP_AT_MOST_K);
|
||||
}
|
||||
|
||||
bool card_util::is_at_most_k(app *a, unsigned& k) const {
|
||||
if (is_at_most_k(a)) {
|
||||
k = get_k(a);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned card_util::get_k(app *a) const {
|
||||
SASSERT(is_at_most_k(a));
|
||||
return static_cast<unsigned>(a->get_decl()->get_parameter(0).get_int());
|
||||
}
|
||||
|
83
src/ast/card_decl_plugin.h
Normal file
83
src/ast/card_decl_plugin.h
Normal file
|
@ -0,0 +1,83 @@
|
|||
/*++
|
||||
Copyright (c) 2013 Microsoft Corporation
|
||||
|
||||
Module Name:
|
||||
|
||||
card_decl_plugin.h
|
||||
|
||||
Abstract:
|
||||
|
||||
Cardinality Constraints plugin
|
||||
|
||||
Author:
|
||||
|
||||
Nikolaj Bjorner (nbjorner) 2013-05-11
|
||||
|
||||
Notes:
|
||||
|
||||
|
||||
(at-most-k x1 .... x_n) means x1 + ... + x_n <= k
|
||||
|
||||
hence:
|
||||
|
||||
(not (at-most-k x1 .... x_n)) means x1 + ... + x_n >= k + 1
|
||||
|
||||
|
||||
--*/
|
||||
#ifndef _CARD_DECL_PLUGIN_H_
|
||||
#define _CARD_DECL_PLUGIN_H_
|
||||
|
||||
#include"ast.h"
|
||||
|
||||
|
||||
|
||||
enum card_op_kind {
|
||||
OP_AT_MOST_K,
|
||||
LAST_CARD_OP
|
||||
};
|
||||
|
||||
|
||||
class card_decl_plugin : public decl_plugin {
|
||||
symbol m_at_most_sym;
|
||||
func_decl * mk_at_most(unsigned arity, unsigned k);
|
||||
public:
|
||||
card_decl_plugin();
|
||||
virtual ~card_decl_plugin() {}
|
||||
|
||||
virtual sort * mk_sort(decl_kind k, unsigned num_parameters, parameter const * parameters) {
|
||||
UNREACHABLE();
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual decl_plugin * mk_fresh() {
|
||||
return alloc(card_decl_plugin);
|
||||
}
|
||||
|
||||
//
|
||||
// Contract for func_decl:
|
||||
// parameters[0] - integer (at most k elements)
|
||||
// all sorts are Booleans
|
||||
virtual func_decl * mk_func_decl(decl_kind k, unsigned num_parameters, parameter const * parameters,
|
||||
unsigned arity, sort * const * domain, sort * range);
|
||||
virtual void get_op_names(svector<builtin_name> & op_names, symbol const & logic);
|
||||
virtual void get_sort_names(svector<builtin_name> & sort_names, symbol const & logic);
|
||||
virtual expr * get_some_value(sort * s);
|
||||
virtual bool is_fully_interp(sort const * s) const;
|
||||
};
|
||||
|
||||
|
||||
class card_util {
|
||||
ast_manager & m;
|
||||
family_id m_fid;
|
||||
public:
|
||||
card_util(ast_manager& m):m(m), m_fid(m.mk_family_id("card")) {}
|
||||
ast_manager & get_manager() const { return m; }
|
||||
app * mk_at_most_k(unsigned num_args, expr * const * args, unsigned k);
|
||||
bool is_at_most_k(app *a) const;
|
||||
bool is_at_most_k(app *a, unsigned& k) const;
|
||||
unsigned get_k(app *a) const;
|
||||
};
|
||||
|
||||
|
||||
#endif /* _CARD_DECL_PLUGIN_H_ */
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue