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 */
101 std::map
< Node
, InstMatchTrie
> d_data
;
103 void print( std::ostream
& out
, Node q
, std::vector
< TNode
>& terms
) const;
104 void getInstantiations( std::vector
< Node
>& insts
, Node q
, std::vector
< Node
>& terms
, QuantifiersEngine
* qe
) const;
109 /** return true if m exists in this trie
110 modEq is if we check modulo equality
111 modInst is if we return true if m is an instance of a match that exists
113 bool existsInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false,
114 ImtIndexOrder
* imtio
= NULL
, int index
= 0 ) {
115 return !addInstMatch( qe
, f
, m
, modEq
, imtio
, true, index
);
117 bool existsInstMatch( QuantifiersEngine
* qe
, Node f
, std::vector
< Node
>& m
, bool modEq
= false,
118 ImtIndexOrder
* imtio
= NULL
, int index
= 0 ) {
119 return !addInstMatch( qe
, f
, m
, modEq
, imtio
, true, index
);
121 /** add match m for quantifier f, take into account equalities if modEq = true,
122 if imtio is non-null, this is the order to add to trie
123 return true if successful
125 bool addInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false,
126 ImtIndexOrder
* imtio
= NULL
, bool onlyExist
= false, int index
= 0 ){
127 return addInstMatch( qe
, f
, m
.d_vals
, modEq
, imtio
, onlyExist
, index
);
129 bool addInstMatch( QuantifiersEngine
* qe
, Node f
, std::vector
< Node
>& m
, bool modEq
= false,
130 ImtIndexOrder
* imtio
= NULL
, bool onlyExist
= false, int index
= 0 );
131 bool removeInstMatch( QuantifiersEngine
* qe
, Node f
, std::vector
< Node
>& m
, ImtIndexOrder
* imtio
= NULL
, int index
= 0 );
132 void print( std::ostream
& out
, Node q
) const{
133 std::vector
< TNode
> terms
;
134 print( out
, q
, terms
);
136 void getInstantiations( std::vector
< Node
>& insts
, Node q
, QuantifiersEngine
* qe
) {
137 std::vector
< Node
> terms
;
138 getInstantiations( insts
, q
, terms
, qe
);
140 void clear() { d_data
.clear(); }
141 };/* class InstMatchTrie */
143 /** trie for InstMatch objects */
144 class CDInstMatchTrie
{
147 std::map
< Node
, CDInstMatchTrie
* > d_data
;
149 context::CDO
< bool > d_valid
;
151 void print( std::ostream
& out
, Node q
, std::vector
< TNode
>& terms
) const;
152 void getInstantiations( std::vector
< Node
>& insts
, Node q
, std::vector
< Node
>& terms
, QuantifiersEngine
* qe
) const;
154 CDInstMatchTrie( context::Context
* c
) : d_valid( c
, false ){}
157 /** return true if m exists in this trie
158 modEq is if we check modulo equality
159 modInst is if we return true if m is an instance of a match that exists
161 bool existsInstMatch( QuantifiersEngine
* qe
, Node q
, InstMatch
& m
, context::Context
* c
, bool modEq
= false,
163 return !addInstMatch( qe
, q
, m
, c
, modEq
, index
, true );
165 bool existsInstMatch( QuantifiersEngine
* qe
, Node q
, std::vector
< Node
>& m
, context::Context
* c
, bool modEq
= false,
167 return !addInstMatch( qe
, q
, m
, c
, modEq
, index
, true );
169 /** add match m for quantifier f, take into account equalities if modEq = true,
170 if imtio is non-null, this is the order to add to trie
171 return true if successful
173 bool addInstMatch( QuantifiersEngine
* qe
, Node q
, InstMatch
& m
, context::Context
* c
, bool modEq
= false,
174 int index
= 0, bool onlyExist
= false ) {
175 return addInstMatch( qe
, q
, m
.d_vals
, c
, modEq
, index
, onlyExist
);
177 bool addInstMatch( QuantifiersEngine
* qe
, Node q
, std::vector
< Node
>& m
, context::Context
* c
, bool modEq
= false,
178 int index
= 0, bool onlyExist
= false );
179 bool removeInstMatch( QuantifiersEngine
* qe
, Node q
, std::vector
< Node
>& m
, int index
= 0 );
180 void print( std::ostream
& out
, Node q
) const{
181 std::vector
< TNode
> terms
;
182 print( out
, q
, terms
);
184 void getInstantiations( std::vector
< Node
>& insts
, Node q
, QuantifiersEngine
* qe
) {
185 std::vector
< Node
> terms
;
186 getInstantiations( insts
, q
, terms
, qe
);
188 };/* class CDInstMatchTrie */
191 class InstMatchTrieOrdered
{
193 InstMatchTrie::ImtIndexOrder
* d_imtio
;
196 InstMatchTrieOrdered( InstMatchTrie::ImtIndexOrder
* imtio
) : d_imtio( imtio
){}
197 ~InstMatchTrieOrdered(){}
199 InstMatchTrie::ImtIndexOrder
* getOrdering() { return d_imtio
; }
201 InstMatchTrie
* getTrie() { return &d_imt
; }
203 /** add match m, return true if successful */
204 bool addInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false, bool modInst
= false ){
205 return d_imt
.addInstMatch( qe
, f
, m
, modEq
, d_imtio
);
207 bool existsInstMatch( QuantifiersEngine
* qe
, Node f
, InstMatch
& m
, bool modEq
= false, bool modInst
= false ){
208 return d_imt
.existsInstMatch( qe
, f
, m
, modEq
, d_imtio
);
210 };/* class InstMatchTrieOrdered */
212 }/* CVC4::theory::inst namespace */
214 typedef CVC4::theory::inst::InstMatch InstMatch
;
216 }/* CVC4::theory namespace */
217 }/* CVC4 namespace */
219 #endif /* __CVC4__THEORY__QUANTIFIERS__INST_MATCH_H */