1 /********************* */
4 ** Top contributors (to current version):
5 ** Andrew Reynolds, Morgan Deters, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 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.\endverbatim
12 ** \brief inst match class
15 #include "cvc4_private.h"
17 #ifndef __CVC4__THEORY__QUANTIFIERS__INST_MATCH_H
18 #define __CVC4__THEORY__QUANTIFIERS__INST_MATCH_H
20 #include "util/hash.h"
21 #include "context/cdo.h"
23 #include <ext/hash_set>
26 #include "context/cdlist.h"
27 #include "expr/node.h"
32 class QuantifiersEngine
;
37 /** basic class defining an instantiation */
40 /* map from variable to ground terms */
41 std::vector
< Node
> d_vals
;
44 explicit InstMatch( TNode f
);
45 InstMatch( InstMatch
* m
);
47 /** fill all unfilled values with m */
48 bool add( InstMatch
& m
);
49 /** if compatible, fill all unfilled values with m and return true
50 return false otherwise */
51 bool merge( EqualityQuery
* q
, InstMatch
& m
);
52 /** debug print method */
53 void debugPrint( const char* c
);
56 /** make representative */
57 void makeRepresentative( QuantifiersEngine
* qe
);
63 inline void toStream(std::ostream
& out
) const {
64 out
<< "INST_MATCH( ";
66 for( unsigned i
=0; i
<d_vals
.size(); i
++ ){
67 if( !d_vals
[i
].isNull() ){
68 if( printed
){ out
<< ", "; }
69 out
<< i
<< " -> " << d_vals
[i
];
79 void getTerms( Node f
, std::vector
< Node
>& inst
);
81 void setValue( int i
, TNode n
);
82 bool set( QuantifiersEngine
* qe
, int i
, TNode n
);
83 /* Node used for matching the trigger */
85 };/* class InstMatch */
87 inline std::ostream
& operator<<(std::ostream
& out
, const InstMatch
& m
) {
92 /** trie for InstMatch objects */
97 std::vector
< int > d_order
;
98 };/* class InstMatchTrie ImtIndexOrder */
100 std::map
< Node
, InstMatchTrie
> d_data
;
102 void print( std::ostream
& out
, Node q
, std::vector
< TNode
>& terms
, bool& firstTime
, bool useActive
, std::vector
< Node
>& active
) const;
103 void getInstantiations( std::vector
< Node
>& insts
, Node q
, std::vector
< Node
>& terms
, QuantifiersEngine
* qe
, bool useActive
, std::vector
< Node
>& active
) const;
104 void getExplanationForInstLemmas( Node q
, std::vector
< Node
>& terms
, std::vector
< Node
>& lems
, std::map
< Node
, Node
>& quant
, std::map
< Node
, std::vector
< Node
> >& tvec
) const;
106 void setInstLemma( Node n
){
110 bool hasInstLemma() const { return !d_data
.empty(); }
111 Node
getInstLemma() const { return d_data
.begin()->first
; }
116 /** return true if m exists in this trie
117 modEq is if we check modulo equality
118 modInst is if we return true if m is an instance of a match that exists
120 bool existsInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false,
121 ImtIndexOrder
* imtio
= NULL
, int index
= 0 ) {
122 return !addInstMatch( qe
, f
, m
, modEq
, imtio
, true, index
);
124 bool existsInstMatch( QuantifiersEngine
* qe
, Node f
, std::vector
< Node
>& m
, bool modEq
= false,
125 ImtIndexOrder
* imtio
= NULL
, int index
= 0 ) {
126 return !addInstMatch( qe
, f
, m
, modEq
, imtio
, true, index
);
128 /** add match m for quantifier f, take into account equalities if modEq = true,
129 if imtio is non-null, this is the order to add to trie
130 return true if successful
132 bool addInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false,
133 ImtIndexOrder
* imtio
= NULL
, bool onlyExist
= false, int index
= 0 ){
134 return addInstMatch( qe
, f
, m
.d_vals
, modEq
, imtio
, onlyExist
, index
);
136 bool addInstMatch( QuantifiersEngine
* qe
, Node f
, std::vector
< Node
>& m
, bool modEq
= false,
137 ImtIndexOrder
* imtio
= NULL
, bool onlyExist
= false, int index
= 0 );
138 bool removeInstMatch( QuantifiersEngine
* qe
, Node f
, std::vector
< Node
>& m
, ImtIndexOrder
* imtio
= NULL
, int index
= 0 );
139 bool recordInstLemma( Node q
, std::vector
< Node
>& m
, Node lem
, ImtIndexOrder
* imtio
= NULL
, int index
= 0 );
140 void print( std::ostream
& out
, Node q
, bool& firstTime
, bool useActive
, std::vector
< Node
>& active
) const{
141 std::vector
< TNode
> terms
;
142 print( out
, q
, terms
, firstTime
, useActive
, active
);
144 void getInstantiations( std::vector
< Node
>& insts
, Node q
, QuantifiersEngine
* qe
, bool useActive
, std::vector
< Node
>& active
) {
145 std::vector
< Node
> terms
;
146 getInstantiations( insts
, q
, terms
, qe
, useActive
, active
);
148 void getExplanationForInstLemmas( Node q
, std::vector
< Node
>& lems
, std::map
< Node
, Node
>& quant
, std::map
< Node
, std::vector
< Node
> >& tvec
) const {
149 std::vector
< Node
> terms
;
150 getExplanationForInstLemmas( q
, terms
, lems
, quant
, tvec
);
152 void clear() { d_data
.clear(); }
153 };/* class InstMatchTrie */
155 /** trie for InstMatch objects */
156 class CDInstMatchTrie
{
159 std::map
< Node
, CDInstMatchTrie
* > d_data
;
161 context::CDO
< bool > d_valid
;
163 void print( std::ostream
& out
, Node q
, std::vector
< TNode
>& terms
, bool& firstTime
, bool useActive
, std::vector
< Node
>& active
) const;
164 void getInstantiations( std::vector
< Node
>& insts
, Node q
, std::vector
< Node
>& terms
, QuantifiersEngine
* qe
, bool useActive
, std::vector
< Node
>& active
) const;
165 void getExplanationForInstLemmas( Node q
, std::vector
< Node
>& terms
, std::vector
< Node
>& lems
, std::map
< Node
, Node
>& quant
, std::map
< Node
, std::vector
< Node
> >& tvec
) const;
167 void setInstLemma( Node n
){
171 bool hasInstLemma() const { return !d_data
.empty(); }
172 Node
getInstLemma() const { return d_data
.begin()->first
; }
174 CDInstMatchTrie( context::Context
* c
) : d_valid( c
, false ){}
177 /** return true if m exists in this trie
178 modEq is if we check modulo equality
179 modInst is if we return true if m is an instance of a match that exists
181 bool existsInstMatch( QuantifiersEngine
* qe
, Node q
, InstMatch
& m
, context::Context
* c
, bool modEq
= false,
183 return !addInstMatch( qe
, q
, m
, c
, modEq
, index
, true );
185 bool existsInstMatch( QuantifiersEngine
* qe
, Node q
, std::vector
< Node
>& m
, context::Context
* c
, bool modEq
= false,
187 return !addInstMatch( qe
, q
, m
, c
, modEq
, index
, true );
189 /** add match m for quantifier f, take into account equalities if modEq = true,
190 if imtio is non-null, this is the order to add to trie
191 return true if successful
193 bool addInstMatch( QuantifiersEngine
* qe
, Node q
, InstMatch
& m
, context::Context
* c
, bool modEq
= false,
194 int index
= 0, bool onlyExist
= false ) {
195 return addInstMatch( qe
, q
, m
.d_vals
, c
, modEq
, index
, onlyExist
);
197 bool addInstMatch( QuantifiersEngine
* qe
, Node q
, std::vector
< Node
>& m
, context::Context
* c
, bool modEq
= false,
198 int index
= 0, bool onlyExist
= false );
199 bool removeInstMatch( QuantifiersEngine
* qe
, Node q
, std::vector
< Node
>& m
, int index
= 0 );
200 bool recordInstLemma( Node q
, std::vector
< Node
>& m
, Node lem
, int index
= 0 );
201 void print( std::ostream
& out
, Node q
, bool& firstTime
, bool useActive
, std::vector
< Node
>& active
) const{
202 std::vector
< TNode
> terms
;
203 print( out
, q
, terms
, firstTime
, useActive
, active
);
205 void getInstantiations( std::vector
< Node
>& insts
, Node q
, QuantifiersEngine
* qe
, bool useActive
, std::vector
< Node
>& active
) {
206 std::vector
< Node
> terms
;
207 getInstantiations( insts
, q
, terms
, qe
, useActive
, active
);
209 void getExplanationForInstLemmas( Node q
, std::vector
< Node
>& lems
, std::map
< Node
, Node
>& quant
, std::map
< Node
, std::vector
< Node
> >& tvec
) const {
210 std::vector
< Node
> terms
;
211 getExplanationForInstLemmas( q
, terms
, lems
, quant
, tvec
);
213 };/* class CDInstMatchTrie */
216 class InstMatchTrieOrdered
{
218 InstMatchTrie::ImtIndexOrder
* d_imtio
;
221 InstMatchTrieOrdered( InstMatchTrie::ImtIndexOrder
* imtio
) : d_imtio( imtio
){}
222 ~InstMatchTrieOrdered(){}
224 InstMatchTrie::ImtIndexOrder
* getOrdering() { return d_imtio
; }
226 InstMatchTrie
* getTrie() { return &d_imt
; }
228 /** add match m, return true if successful */
229 bool addInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false, bool modInst
= false ){
230 return d_imt
.addInstMatch( qe
, f
, m
, modEq
, d_imtio
);
232 bool existsInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false, bool modInst
= false ){
233 return d_imt
.existsInstMatch( qe
, f
, m
, modEq
, d_imtio
);
235 };/* class InstMatchTrieOrdered */
237 }/* CVC4::theory::inst namespace */
239 typedef CVC4::theory::inst::InstMatch InstMatch
;
241 }/* CVC4::theory namespace */
242 }/* CVC4 namespace */
244 #endif /* __CVC4__THEORY__QUANTIFIERS__INST_MATCH_H */