|
| 1 | +/* kitty: C++ truth table library |
| 2 | + * Copyright (C) 2017-2018 EPFL |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person |
| 5 | + * obtaining a copy of this software and associated documentation |
| 6 | + * files (the "Software"), to deal in the Software without |
| 7 | + * restriction, including without limitation the rights to use, |
| 8 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the |
| 10 | + * Software is furnished to do so, subject to the following |
| 11 | + * conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be |
| 14 | + * included in all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 24 | + */ |
| 25 | + |
| 26 | +/*! |
| 27 | + \file properties.hpp |
| 28 | + \brief Implements property checks for Boolean function |
| 29 | +
|
| 30 | + \author Mathias Soeken |
| 31 | +*/ |
| 32 | + |
| 33 | +#pragma once |
| 34 | + |
| 35 | +#include <cinttypes> |
| 36 | +#include <utility> |
| 37 | +#include <vector> |
| 38 | + |
| 39 | +#include "bit_operations.hpp" |
| 40 | + |
| 41 | +namespace kitty |
| 42 | +{ |
| 43 | + |
| 44 | +/*! \brief Returns the Chow parameter of a function |
| 45 | +
|
| 46 | + The Chow parameters is a set of values \f$N(f), \Sigma(f)\f$, where \f$N(f)\f$ |
| 47 | + is the size of the ON-set, and \f$\Sigma(f)\f$ is the sum of all input |
| 48 | + assignments in the ON-set. For example for \f$f = x_1 \lor x_2\f$ the |
| 49 | + function returns \f$(3, (2,2))\f$. |
| 50 | +
|
| 51 | + \param tt Truth table |
| 52 | +*/ |
| 53 | +template<typename TT> |
| 54 | +std::pair<uint32_t, std::vector<uint32_t>> chow_parameters( const TT& tt ) |
| 55 | +{ |
| 56 | + assert( tt.num_vars() <= 32 ); |
| 57 | + |
| 58 | + const auto n = tt.num_vars(); |
| 59 | + const auto nf = count_ones( tt ); |
| 60 | + |
| 61 | + std::vector<uint32_t> sf( n, 0u ); |
| 62 | + for_each_one_bit( tt, [n, &sf]( auto minterm ) { |
| 63 | + for ( auto i = 0u; minterm; ++i ) |
| 64 | + { |
| 65 | + if ( minterm & 1 ) |
| 66 | + { |
| 67 | + ++sf[i]; |
| 68 | + } |
| 69 | + minterm >>= 1; |
| 70 | + } |
| 71 | + } ); |
| 72 | + |
| 73 | + return {nf, sf}; |
| 74 | +} |
| 75 | + |
| 76 | +/*! \brief Checks whether a function is canalizing |
| 77 | +
|
| 78 | + \param tt Truth table |
| 79 | +*/ |
| 80 | +template<typename TT> |
| 81 | +bool is_canalizing( const TT& tt ) |
| 82 | +{ |
| 83 | + uint32_t f1or{}, f0or{}; |
| 84 | + uint32_t f1and, f0and; |
| 85 | + |
| 86 | + uint32_t max = static_cast<uint32_t>( ( uint64_t( 1 ) << tt.num_vars() ) - 1 ); |
| 87 | + f1and = f0and = max; |
| 88 | + |
| 89 | + for ( uint32_t i = 0u; i < tt.num_bits(); ++i ) |
| 90 | + { |
| 91 | + if ( get_bit( tt, i ) == 0 ) |
| 92 | + { |
| 93 | + f0and &= i; |
| 94 | + f0or |= i; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + f1and &= i; |
| 99 | + f1or |= i; |
| 100 | + } |
| 101 | + |
| 102 | + if ( f0and == 0 && f1and == 0 && f0or == max && f1or == max ) |
| 103 | + { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 111 | +/*! \brief Checks whether a function is Horn |
| 112 | +
|
| 113 | + A function is Horn, if it can be represented using Horn clauses. |
| 114 | +
|
| 115 | + \param tt Truth table |
| 116 | +*/ |
| 117 | +template<typename TT> |
| 118 | +bool is_horn( const TT& tt ) |
| 119 | +{ |
| 120 | + for ( uint32_t i = 1u; i < tt.num_bits(); ++i ) |
| 121 | + { |
| 122 | + for ( uint32_t j = 0u; j < i; ++j ) |
| 123 | + { |
| 124 | + if ( get_bit( tt, j ) && get_bit( tt, i ) && !get_bit( tt, i & j ) ) |
| 125 | + { |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
| 132 | +} |
| 133 | + |
| 134 | +/*! \brief Checks whether a function is Krom |
| 135 | +
|
| 136 | + A function is Krom, if it can be represented using Krom clauses. |
| 137 | +
|
| 138 | + \param tt Truth table |
| 139 | +*/ |
| 140 | +template<typename TT> |
| 141 | +bool is_krom( const TT& tt ) |
| 142 | +{ |
| 143 | + for ( uint32_t i = 2u; i < tt.num_bits(); ++i ) |
| 144 | + { |
| 145 | + for ( uint32_t j = 1u; j < i; ++j ) |
| 146 | + { |
| 147 | + for ( uint32_t k = 0u; k < j; ++k ) |
| 148 | + { |
| 149 | + const auto maj = ( i & j ) | ( i & k ) | ( j & k ); |
| 150 | + if ( get_bit( tt, k ) && get_bit( tt, j ) && get_bit( tt, i ) && !get_bit( tt, maj ) ) |
| 151 | + { |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return true; |
| 159 | +} |
| 160 | + |
| 161 | +} // namespace kitty |
0 commit comments