GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/evaluator.h Lines: 6 6 100.0 %
Date: 2021-11-05 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Andres Noetzli, 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
 * The Evaluator class.
14
 *
15
 * The Evaluator class can be used to evaluate terms with constant leaves
16
 * quickly, without going through the rewriter.
17
 */
18
19
#include "cvc5_private.h"
20
21
#ifndef CVC5__THEORY__EVALUATOR_H
22
#define CVC5__THEORY__EVALUATOR_H
23
24
#include <utility>
25
#include <vector>
26
27
#include "base/output.h"
28
#include "expr/node.h"
29
#include "expr/uninterpreted_constant.h"
30
#include "util/bitvector.h"
31
#include "util/rational.h"
32
#include "util/string.h"
33
34
namespace cvc5 {
35
namespace theory {
36
37
/**
38
 * Struct that holds the result of an evaluation. The actual value is stored in
39
 * a union to avoid the overhead of a class hierarchy with virtual methods.
40
 */
41
struct EvalResult
42
{
43
  /* Describes which type of result is being stored */
44
  enum
45
  {
46
    BOOL,
47
    BITVECTOR,
48
    RATIONAL,
49
    STRING,
50
    UCONST,
51
    INVALID
52
  } d_tag;
53
54
  /* Stores the actual result */
55
  union
56
  {
57
    bool d_bool;
58
    BitVector d_bv;
59
    Rational d_rat;
60
    String d_str;
61
    UninterpretedConstant d_uc;
62
  };
63
64
  EvalResult(const EvalResult& other);
65
6055094
  EvalResult() : d_tag(INVALID) {}
66
1670648
  EvalResult(bool b) : d_tag(BOOL), d_bool(b) {}
67
2109744
  EvalResult(const BitVector& bv) : d_tag(BITVECTOR), d_bv(bv) {}
68
761981
  EvalResult(const Rational& i) : d_tag(RATIONAL), d_rat(i) {}
69
330995
  EvalResult(const String& str) : d_tag(STRING), d_str(str) {}
70
4
  EvalResult(const UninterpretedConstant& u) : d_tag(UCONST), d_uc(u) {}
71
72
  EvalResult& operator=(const EvalResult& other);
73
74
  ~EvalResult();
75
76
  /**
77
   * Converts the result to a Node. If the result is not valid, this function
78
   * returns the null node.
79
   */
80
  Node toNode() const;
81
};
82
83
class Rewriter;
84
85
/**
86
 * The class that performs the actual evaluation of a term under a
87
 * substitution. Right now, the class does not cache anything between different
88
 * calls to `eval` but this might change in the future.
89
 */
90
class Evaluator
91
{
92
 public:
93
  /**
94
   * @param rr (optional) the rewriter to use when a node cannot be evaluated.
95
   * @param strAlphaCard The assumed cardinality of the alphabet for strings.
96
   */
97
  Evaluator(Rewriter* rr, uint32_t strAlphaCard = 196608);
98
  /**
99
   * Evaluates node `n` under the substitution described by the variable names
100
   * `args` and the corresponding values `vals`. This method uses evaluation
101
   * for subterms that evaluate to constants supported in the EvalResult
102
   * class and substitution with rewriting for the others.
103
   *
104
   * The nodes in vals are typically intended to be constant, although this
105
   * is not required.
106
   *
107
   * If the parameter useRewriter is true, then we may invoke calls to the
108
   * rewriter for computing the result of this method.
109
   *
110
   * The result of this call is either equivalent to:
111
   * (1) rewrite(n.substitute(args,vars))
112
   * (2) Node::null().
113
   * If d_rr is non-null, then we are always in the first case. If
114
   * useRewriter is null, then we may be in case (2) if computing the
115
   * rewritten, substituted form of n could not be determined by evaluation.
116
   */
117
  Node eval(TNode n,
118
            const std::vector<Node>& args,
119
            const std::vector<Node>& vals) const;
120
  /**
121
   * Same as above, but with a precomputed visited map.
122
   */
123
  Node eval(TNode n,
124
            const std::vector<Node>& args,
125
            const std::vector<Node>& vals,
126
            const std::unordered_map<Node, Node>& visited) const;
127
128
 private:
129
  /**
130
   * Evaluates node `n` under the substitution described by the variable names
131
   * `args` and the corresponding values `vals`. The internal version returns
132
   * an EvalResult which has slightly less overhead for recursive calls.
133
   *
134
   * The method returns an invalid EvalResult if the result of the substitution
135
   * on n does not result in a constant value that is one of those supported in
136
   * the EvalResult class. Notice that e.g. datatype constants are not supported
137
   * in this class.
138
   *
139
   * This method additionally adds subterms of n that could not be evaluated
140
   * (in the above sense) to the map evalAsNode. For each such subterm n', we
141
   * store the node corresponding to the result of applying the substitution
142
   * `args` to `vals` and rewriting. Notice that this map contains an entry
143
   * for n in the case that it cannot be evaluated.
144
   */
145
  EvalResult evalInternal(TNode n,
146
                          const std::vector<Node>& args,
147
                          const std::vector<Node>& vals,
148
                          std::unordered_map<TNode, Node>& evalAsNode,
149
                          std::unordered_map<TNode, EvalResult>& results) const;
150
  /** reconstruct
151
   *
152
   * This function reconstructs the result of evaluating n using a combination
153
   * of evaluation results (eresults) and substitution+rewriting (evalAsNode).
154
   *
155
   * Arguments eresults and evalAsNode are built within the context of the
156
   * above method for some args and vals. This method ensures that the return
157
   * value is equivalent to the rewritten form of n * { args -> vals }.
158
   */
159
  Node reconstruct(TNode n,
160
                   std::unordered_map<TNode, EvalResult>& eresults,
161
                   std::unordered_map<TNode, Node>& evalAsNode) const;
162
  /** The (optional) rewriter to be used */
163
  Rewriter* d_rr;
164
  /** The cardinality of the alphabet of strings */
165
  uint32_t d_alphaCard;
166
};
167
168
}  // namespace theory
169
}  // namespace cvc5
170
171
#endif /* CVC5__THEORY__EVALUATOR_H */