Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * Top contributors (to current version): 3 : : * Andrew Reynolds 4 : : * 5 : : * This file is part of the cvc5 project. 6 : : * 7 : : * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS 8 : : * in the top-level source directory and their institutional affiliations. 9 : : * All rights reserved. See the file COPYING in the top-level source 10 : : * directory for licensing information. 11 : : * **************************************************************************** 12 : : * 13 : : * sygus_term_enumerator 14 : : */ 15 : : 16 : : #include "cvc5_private.h" 17 : : 18 : : #ifndef CVC5__EXPR__SYGUS_TERM_ENUMERATOR_H 19 : : #define CVC5__EXPR__SYGUS_TERM_ENUMERATOR_H 20 : : 21 : : #include <unordered_set> 22 : : 23 : : #include "expr/node.h" 24 : : #include "expr/type_node.h" 25 : : #include "smt/env_obj.h" 26 : : 27 : : namespace cvc5::internal { 28 : : 29 : : namespace theory::quantifiers { 30 : : class SygusEnumerator; 31 : : } 32 : : /** 33 : : * A virtual callback for whether to consider terms in an enumeration. 34 : : */ 35 : : class SygusTermEnumeratorCallback 36 : : { 37 : : public: 38 : 1525 : SygusTermEnumeratorCallback() {} 39 : 1487 : virtual ~SygusTermEnumeratorCallback() {} 40 : : /** 41 : : * Add term, return true if the term should be considered in the enumeration. 42 : : * Notice that returning false indicates that n should not be considered as a 43 : : * subterm of any other term in the enumeration. 44 : : * 45 : : * @param n The SyGuS term, which is a deep embedding of the next term to 46 : : * enumerate. 47 : : * @param bterms The (rewritten, builtin) terms we have already enumerated 48 : : * @return true if n should be considered in the enumeration. 49 : : */ 50 : : virtual bool addTerm(const Node& n, std::unordered_set<Node>& bterms) = 0; 51 : : }; 52 : : 53 : : /** 54 : : * SygusTermEnumerator 55 : : * 56 : : * This class is used for enumerating all terms of a sygus datatype type. At 57 : : * a high level, it is used as an alternative approach to sygus datatypes 58 : : * solver as a candidate generator in a synthesis loop. It filters terms based 59 : : * on redundancy criteria, for instance, it does not generate two terms that can 60 : : * be shown to be equivalent via rewriting. It enumerates terms whose deep 61 : : * embedding is considered in order of sygus term size 62 : : * (TermDb::getSygusTermSize). 63 : : * 64 : : * It also can be configured to enumerate sygus terms with free variables, 65 : : * (as opposed to variables bound in the formal arguments list of the 66 : : * function-to-synthesize), where each free variable appears in exactly one 67 : : * subterm. For grammar: 68 : : * S -> 0 | 1 | x | S+S 69 : : * this enumerator will generate the stream: 70 : : * z1, 0, 1, x, z1+z2, z1+1, 1+1 ... 71 : : * and so on, where z1 and z2 are variables of type Int. We call 72 : : * these "shapes". This feature can be enabled by setting enumShapes to true 73 : : * in the constructor below. 74 : : * 75 : : * In detail to generate all terms in a grammar: 76 : : * 77 : : * SygusTermEnumerator enum(...); 78 : : * std::vector<Node> terms; 79 : : * do 80 : : * { 81 : : * terms.push_back(enum.getCurrent()); 82 : : * } while (enum.increment()); 83 : : */ 84 : : class SygusTermEnumerator 85 : : { 86 : : public: 87 : : /** 88 : : * @param env Reference to the environment 89 : : * @param tn The sygus datatype that encodes the grammar 90 : : * @param sec Pointer to an (optional) callback, required e.g. if we wish to 91 : : * do conjecture-specific symmetry breaking 92 : : * @param enumShapes If true, this enumerator will generate terms having any 93 : : * number of free variables 94 : : * @param enumAnyConstHoles If true, this enumerator will generate terms where 95 : : * free variables are the arguments to any-constant constructors. 96 : : * @param numConstants The number of interpreted constants to consider for 97 : : * each size in any-constant constructors. 98 : : */ 99 : : SygusTermEnumerator(Env& env, 100 : : const TypeNode& tn, 101 : : SygusTermEnumeratorCallback* sec = nullptr, 102 : : bool enumShapes = false, 103 : : bool enumAnyConstHoles = false, 104 : : size_t numConstants = 5); 105 : 136 : ~SygusTermEnumerator() {} 106 : : /** 107 : : * Increment. If true, we ensure that getCurrent() is non-null. If false, 108 : : * there are no more terms in the enumeration. 109 : : */ 110 : : bool increment(); 111 : : /** 112 : : * Increment partial. Same as above but does not guarantee that getCurrent 113 : : * is non-null. This method can be used if the user wants the enumerator 114 : : * to not put too much effort into trying to enumerate the next value. 115 : : */ 116 : : bool incrementPartial(); 117 : : /** 118 : : * Get the current term generated by this class. This can be non-null 119 : : * prior to the first call to increment. 120 : : * 121 : : * In particular, this returns the builtin equivalent of the current sygus 122 : : * datatype term that was enumerated. 123 : : */ 124 : : Node getCurrent(); 125 : : 126 : : private: 127 : : /** The internal implementation */ 128 : : std::unique_ptr<theory::quantifiers::SygusEnumerator> d_internal; 129 : : /** The enumerator, a dummy skolem passed to the above class */ 130 : : Node d_enum; 131 : : }; 132 : : 133 : : } // namespace cvc5::internal 134 : : 135 : : #endif /* CVC5__EXPR__SYGUS_TERM_ENUMERATOR_H */