Source

z3 / src / muz_qe / dl_vector_relation.h

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*++
Copyright (c) 2010 Microsoft Corporation

Module Name:

    dl_vector_relation.h

Abstract:

    Basic relation with equivalences.

Author:

    Nikolaj Bjorner (nbjorner) 2010-2-11

Revision History:

--*/
#ifndef _DL_VECTOR_RELATION_H_
#define _DL_VECTOR_RELATION_H_

#include "ast_pp.h"
#include "dl_context.h"
#include "union_find.h"

namespace datalog {
   
    typedef std::pair<unsigned, unsigned> u_pair;

    template<typename T>
    class vector_relation_helper {
    public:
        static void mk_project_t(T& t, unsigned_vector const& renaming) {}
    };

    template<typename T, typename Helper = vector_relation_helper<T> > 
    class vector_relation : public relation_base {       
    protected:
        T                  m_default;
        vector<T>*         m_elems;
        bool               m_empty;
        union_find_default_ctx m_ctx;
        union_find<>*      m_eqs;

        friend class vector_relation_plugin;

    public:
        vector_relation(relation_plugin& p, relation_signature const& s, bool is_empty, T const& t = T()):
            relation_base(p, s),
            m_default(t),
            m_elems(alloc(vector<T>)),
            m_empty(is_empty), 
            m_eqs(alloc(union_find<>, m_ctx)) {
            m_elems->resize(s.size(), t);
            for (unsigned i = 0; i < s.size(); ++i) {
                m_eqs->mk_var();
            }
        }

        virtual ~vector_relation() {
            dealloc(m_eqs);
            dealloc(m_elems);
        }

        virtual bool can_swap() const { return true; }

        virtual void swap(relation_base& other) {
            vector_relation& o = dynamic_cast<vector_relation&>(other);
            if (&o == this) return;
            std::swap(o.m_eqs, m_eqs);
            std::swap(o.m_empty, m_empty);
            std::swap(o.m_elems, m_elems);
        }

        void copy(vector_relation const& other) {
            SASSERT(get_signature() == other.get_signature());
            if (other.empty()) {
                set_empty();
                return;
            }
            m_empty = false;
            for (unsigned i = 0; i < m_elems->size(); ++i) {
                (*this)[i] = other[i];            
                SASSERT(find(i) == i);
            }
            for (unsigned i = 0; i < m_elems->size(); ++i) {
                merge(i, find(i));
            }        
        }


        virtual bool empty() const { return m_empty; }

        T& operator[](unsigned i) { return (*m_elems)[find(i)]; }

        T const& operator[](unsigned i) const { return (*m_elems)[find(i)]; }

        virtual void display_index(unsigned i, T const& t, std::ostream& out) const = 0;

        virtual void display(std::ostream & out) const {
            if (empty()) {
                out << "empty\n";
                return;
            }
            for (unsigned i = 0; i < m_elems->size(); ++i) {
                if (i == find(i)) {
                    display_index(i, (*m_elems)[i], out);
                }
                else {
                    out << i << " = " << find(i) << "\n";
                }
            }
        }


        bool is_subset_of(vector_relation const& other) const {
            if (empty()) return true;
            if (other.empty()) return false;
            for (unsigned i = 0; i < get_signature().size(); ++i) {
                if (!is_subset_of((*this)[i], other[i])) {
                    return false;
                }
            }
            return true;
        }

        void set_empty() { 
            unsigned sz = m_elems->size();
            m_empty = true; 
            m_elems->reset();
            m_elems->resize(sz, m_default);
            dealloc(m_eqs);
            m_eqs = alloc(union_find<>,m_ctx);
            for (unsigned i = 0; i < sz; ++i) {
                m_eqs->mk_var();
            }
        }


        virtual T mk_intersect(T const& t1, T const& t2, bool& is_empty) const = 0;

        virtual T mk_widen(T const& t1, T const& t2) const = 0;

        virtual T mk_unite(T const& t1, T const& t2) const = 0;

        virtual bool is_subset_of(T const& t1, T const& t2) const = 0;

        virtual bool is_full(T const& t) const = 0;

        virtual bool is_empty(unsigned i, T const& t) const = 0;

        virtual void mk_rename_elem(T& t, unsigned col_cnt, unsigned const* cycle) = 0;

        virtual T mk_eq(union_find<> const& old_eqs, union_find<> const& neq_eqs, T const& t) const { return t; }

        void equate(unsigned i, unsigned j) {
            SASSERT(i < get_signature().size());
            SASSERT(j < get_signature().size());
            if (!empty() && find(i) != find(j)) {
                bool isempty;
                T r = mk_intersect((*this)[i], (*this)[j], isempty);
                if (isempty || is_empty(find(i),r)) {
                    m_empty = true;
                }
                else {
                    merge(i, j);
                    (*this)[i] = r;
                }
            }
        }

        bool is_full() const { 
            for (unsigned i = 0; i < m_elems->size(); ++i) {
                if (!is_full((*this)[i])) {
                    return false;
                }
            }
            return true;
        }

        void mk_join(vector_relation const& r1, vector_relation const& r2,
                     unsigned num_cols, unsigned const* cols1, unsigned const* cols2) {
            SASSERT(is_full());
            bool is_empty = r1.empty() || r2.empty();          
            if (is_empty) {
                m_empty = true;               
                return;
            }
            unsigned sz1 = r1.get_signature().size();
            unsigned sz2 = r2.get_signature().size();  
            for (unsigned i = 0; i < sz1; ++i) {
                (*this)[i] = r1[i];
            }
            for (unsigned i = 0; i < sz2; ++i) {
                (*this)[sz1+i] = r2[i];
            }
            for (unsigned i = 0; i < num_cols; ++i) {
                unsigned col1 = cols1[i];
                unsigned col2 = cols2[i];                
                equate(col1, sz1 + col2);
            }

            TRACE("dl_relation",                   
                  r1.display(tout << "r1:\n");
                  r2.display(tout << "r2:\n");
                  display(tout << "dst:\n");
                  );
        }

        void mk_project(vector_relation const& r, unsigned col_cnt, unsigned const* removed_cols) {
            SASSERT(is_full());
            unsigned_vector classRep, repNode;
            unsigned result_size = get_signature().size();
            unsigned input_size = r.get_signature().size();
            repNode.resize(input_size, UINT_MAX);

            // initialize vector entries and set class representatives.
            for (unsigned i = 0, j = 0, c = 0; i < input_size; ++i) {
                if (c < col_cnt && removed_cols[c] == i) {
                    ++c;
                }
                else {
                    (*this)[j] = r[i];
                    classRep.push_back(r.find(i));
                    ++j;
                }
            }

            // merge remaining equivalence classes.
            for (unsigned i = 0; i < result_size; ++i) {
                unsigned rep = classRep[i];
                if (repNode[rep] == UINT_MAX) {
                    repNode[rep] = i;
                }
                else {
                    merge(repNode[rep], i);
                }
            }

            // rename columns in image of vector relation.
            unsigned_vector renaming;
            for (unsigned i = 0, j = 0, c = 0; i < input_size; ++i) {
                if (c < col_cnt && removed_cols[c] == i) {
                    renaming.push_back(UINT_MAX);
                    ++c;
                }
                else {
                    renaming.push_back(find(j));
                    ++j;
                }
            }
            for (unsigned k = 0; k < result_size; ++k) {
                Helper::mk_project_t((*this)[k], renaming);
            }


            TRACE("dl_relation",
                  ast_manager& m = r.get_plugin().get_ast_manager();
                  tout << "Signature: ";
                  for (unsigned i = 0; i < r.get_signature().size(); ++i) {
                      tout << mk_pp(r.get_signature()[i], m) << " ";
                  }
                  tout << "Remove: ";
                  for (unsigned i = 0; i < col_cnt; ++i) {
                      tout << removed_cols[i] << " ";
                  }
                  tout << "\n";
                  r.display(tout);
                  tout << " --> \n";
                  display(tout););
        }

        void mk_rename(vector_relation const& r, unsigned col_cnt, unsigned const* cycle) {
            unsigned col1, col2;
            SASSERT(is_full());
           
            // roundabout way of creating permuted relation.
            unsigned_vector classRep, repNode;
            for (unsigned i = 0; i < r.m_elems->size(); ++i) {
                classRep.push_back(r.find(i));
                repNode.push_back(UINT_MAX);
                (*this)[i] = r[i];
            }            
            for (unsigned i = 0; i + 1 < col_cnt; ++i) {
                col1 = cycle[i];
                col2 = cycle[i+1];
                (*this)[col2] = (*r.m_elems)[col1];
                classRep[col2] = r.find(col1);
            }
            col1 = cycle[col_cnt-1];
            col2 = cycle[0];
            (*this)[col2] = (*r.m_elems)[col1];
            classRep[col2] = r.find(col1);

            for (unsigned i = 0; i < r.m_elems->size(); ++i) {
                unsigned rep = classRep[i];
                if (repNode[rep] == UINT_MAX) {
                    repNode[rep] = i;
                }
                else {
                    merge(repNode[rep], i);
                }
            }

            for (unsigned i = 0; i < r.m_elems->size(); ++i) {
                mk_rename_elem((*m_elems)[i], col_cnt, cycle);
            }            

            TRACE("dl_relation", 
                  ast_manager& m = r.get_plugin().get_ast_manager();
                  tout << "cycle: ";
                  for (unsigned i = 0; i < col_cnt; ++i) {
                      tout << cycle[i] << " ";
                  }
                  tout << "\nold_sig: ";
                  for (unsigned i = 0; i < r.get_signature().size(); ++i) {
                      tout << mk_pp(r.get_signature()[i], m) << " ";
                  }            
                  tout << "\nnew_sig: ";
                  for (unsigned i = 0; i < get_signature().size(); ++i) {
                        tout << mk_pp(get_signature()[i], m) << " ";
                  }                    
                  tout << "\n";
                  r.display(tout << "src:\n");
                  );         
        }

        void mk_union(vector_relation const& src, vector_relation* delta, bool is_widen) {
            TRACE("dl_relation", display(tout << "dst:\n"); src.display(tout  << "src:\n"););

            if (src.empty()) {
                if (delta) {
                    delta->copy(src);
                }
                return;
            }

            if (empty()) {
                copy(src);
                if (delta) {
                    delta->copy(src);
                }
                return;
            }

            // find coarsest equivalence class containing joint equalities
            union_find<>* uf = alloc(union_find<>, m_ctx);
            unsigned size = get_signature().size();
            map<u_pair, unsigned, pair_hash<unsigned_hash, unsigned_hash>, default_eq<u_pair> > mp;
            bool change = false;
            bit_vector finds;
            finds.resize(size, false);
            for (unsigned i = 0; i < size; ++i) {
                uf->mk_var();
                unsigned w;
                u_pair p(std::make_pair(find(i), src.find(i)));
                if (mp.find(p, w)) {
                    uf->merge(i, w);
                }
                else {
                    mp.insert(p, i);  
                    // detect change
                    if (finds.get(find(i))) {
                        change = true;
                    }
                    else {
                        finds.set(find(i), true);
                    }
                }
            }
            vector<T>* elems = alloc(vector<T>);
            for (unsigned i = 0; i < size; ++i) {                
                T t1 = mk_eq(*m_eqs, *uf, (*this)[i]);
                T t2 = mk_eq(*src.m_eqs, *uf, src[i]);
                if (is_widen) {
                    elems->push_back(mk_widen(t1, t2));
                }
                else {
                    elems->push_back(mk_unite(t1, t2));
                }
                TRACE("dl_relation", tout << t1 << " u " << t2 << " = " << elems->back() << "\n";);
                change = delta && (change || !((*elems)[i] == (*this)[i]));
            }
            dealloc(m_eqs);
            dealloc(m_elems);
            m_eqs = uf;
            m_elems = elems;
            if (delta && change) {
                delta->copy(*this);
            }
            TRACE("dl_relation", display(tout << "dst':\n"););
        }

        unsigned find(unsigned i) const { 
            return m_eqs->find(i);
        }

        void merge(unsigned i, unsigned j) { 
            m_eqs->merge(i, j);
        }

    };
        
};

#endif