GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/sort_inference.h Lines: 5 6 83.3 %
Date: 2021-08-16 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Paul Meng, Mathias Preiner
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 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
 * Pre-process step for performing sort inference.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__SORT_INFERENCE_H
19
#define CVC5__SORT_INFERENCE_H
20
21
#include <map>
22
#include <vector>
23
24
#include "expr/node.h"
25
#include "expr/type_node.h"
26
27
namespace cvc5 {
28
namespace theory {
29
30
/** sort inference
31
 *
32
 * This class implements sort inference techniques, which rewrites a
33
 * formula F into an equisatisfiable formula F', where the symbols g in F are
34
 * replaced by others g', possibly of different types. For details, see e.g.:
35
 *   "Sort it out with Monotonicity" Claessen 2011
36
 *   "Non-Cyclic Sorts for First-Order Satisfiability" Korovin 2013.
37
 */
38
class SortInference {
39
private:
40
  //all subsorts
41
  std::vector< int > d_sub_sorts;
42
  std::map< int, bool > d_non_monotonic_sorts;
43
  std::map< TypeNode, std::vector< int > > d_type_sub_sorts;
44
  void recordSubsort( TypeNode tn, int s );
45
public:
46
20
  class UnionFind {
47
  public:
48
20
    UnionFind(){}
49
    UnionFind( UnionFind& c ){
50
      set( c );
51
    }
52
    std::map< int, int > d_eqc;
53
    //pairs that must be disequal
54
    std::vector< std::pair< int, int > > d_deq;
55
    void print(const char * c);
56
20
    void clear() { d_eqc.clear(); d_deq.clear(); }
57
    void set( UnionFind& c );
58
    int getRepresentative( int t );
59
    void setEqual( int t1, int t2 );
60
    void setDisequal( int t1, int t2 ){ d_deq.push_back( std::pair< int, int >( t1, t2 ) ); }
61
    bool areEqual( int t1, int t2 ) { return getRepresentative( t1 )==getRepresentative( t2 ); }
62
    bool isValid();
63
  };
64
65
 private:
66
  /** the id count for all subsorts we have allocated */
67
  int d_sortCount;
68
  UnionFind d_type_union_find;
69
  std::map< int, TypeNode > d_type_types;
70
  std::map< TypeNode, int > d_id_for_types;
71
  //for apply uf operators
72
  std::map< Node, int > d_op_return_types;
73
  std::map< Node, std::vector< int > > d_op_arg_types;
74
  std::map< Node, int > d_equality_types;
75
  //for bound variables
76
  std::map< Node, std::map< Node, int > > d_var_types;
77
  //get representative
78
  void setEqual( int t1, int t2 );
79
  int getIdForType( TypeNode tn );
80
  void printSort( const char* c, int t );
81
  //process
82
  int process( Node n, std::map< Node, Node >& var_bound, std::map< Node, int >& visited );
83
  // for monotonicity inference
84
 private:
85
  void processMonotonic( Node n, bool pol, bool hasPol, std::map< Node, Node >& var_bound, std::map< Node, std::map< int, bool > >& visited, bool typeMode = false );
86
87
//for rewriting
88
private:
89
  //mapping from old symbols to new symbols
90
  std::map< Node, Node > d_symbol_map;
91
  //mapping from constants to new symbols
92
  std::map< TypeNode, std::map< Node, Node > > d_const_map;
93
  //helper functions for simplify
94
  TypeNode getOrCreateTypeForId( int t, TypeNode pref );
95
  TypeNode getTypeForId( int t );
96
  Node getNewSymbol( Node old, TypeNode tn );
97
  //simplify
98
  Node simplifyNode(Node n,
99
                    std::map<Node, Node>& var_bound,
100
                    TypeNode tnn,
101
                    std::map<Node, Node>& model_replace_f,
102
                    std::map<Node, std::map<TypeNode, Node> >& visited);
103
  //make injection
104
  Node mkInjection( TypeNode tn1, TypeNode tn2 );
105
  //reset
106
  void reset();
107
108
 public:
109
20
  SortInference() : d_sortCount(1) {}
110
20
  ~SortInference(){}
111
112
  /** initialize
113
   *
114
   * This initializes this class. The input formula is indicated by assertions.
115
   */
116
  void initialize(const std::vector<Node>& assertions);
117
  /** simplify
118
   *
119
   * This returns the simplified form of formula n, based on the information
120
   * computed during initialization. The argument model_replace_f stores the
121
   * mapping between functions and their analog in the sort-inferred signature.
122
   * The argument visited is a cache of the internal results of simplifying
123
   * previous nodes with this class.
124
   *
125
   * Must call initialize() before this function.
126
   */
127
  Node simplify(Node n,
128
                std::map<Node, Node>& model_replace_f,
129
                std::map<Node, std::map<TypeNode, Node> >& visited);
130
  /** get new constraints
131
   *
132
   * This adds constraints to new_asserts that ensure the following.
133
   * Let F be the conjunction of assertions from the input. Let F' be the
134
   * conjunction of the simplified form of each conjunct in F. Let C be the
135
   * conjunction of formulas adding to new_asserts. Then, F and F' ^ C are
136
   * equisatisfiable.
137
   */
138
  void getNewAssertions(std::vector<Node>& new_asserts);
139
  /** compute monotonicity
140
   *
141
   * This computes whether sorts are monotonic (see e.g. Claessen 2011). If
142
   * this function is called, then calls to isMonotonic() can subsequently be
143
   * used to query whether sorts are monotonic.
144
   */
145
  void computeMonotonicity(const std::vector<Node>& assertions);
146
  /** return true if tn was inferred to be monotonic */
147
  bool isMonotonic(TypeNode tn);
148
  //get sort id for term n
149
  int getSortId( Node n );
150
  //get sort id for variable of quantified formula f
151
  int getSortId( Node f, Node v );
152
  //set that sk is the skolem variable of v for quantifier f
153
  void setSkolemVar( Node f, Node v, Node sk );
154
public:
155
  //is well sorted
156
  bool isWellSortedFormula( Node n );
157
  bool isWellSorted( Node n );
158
private:
159
  // store monotonicity for original sorts as well
160
 std::map<TypeNode, bool> d_non_monotonic_sorts_orig;
161
 /**
162
  * Returns true if k is the APPLY_UF kind and we are not using higher-order
163
  * techniques. This is called in places where we want to know whether to
164
  * treat a term as uninterpreted function.
165
  */
166
 bool isHandledApplyUf(Kind k) const;
167
};
168
169
}  // namespace theory
170
}  // namespace cvc5
171
172
#endif