GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/evaluator.h Lines: 5 6 83.3 %
Date: 2021-09-29 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
3858988
  EvalResult() : d_tag(INVALID) {}
66
838945
  EvalResult(bool b) : d_tag(BOOL), d_bool(b) {}
67
1599464
  EvalResult(const BitVector& bv) : d_tag(BITVECTOR), d_bv(bv) {}
68
334973
  EvalResult(const Rational& i) : d_tag(RATIONAL), d_rat(i) {}
69
318367
  EvalResult(const String& str) : d_tag(STRING), d_str(str) {}
70
  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
  Evaluator(Rewriter* rr);
94
  /**
95
   * Evaluates node `n` under the substitution described by the variable names
96
   * `args` and the corresponding values `vals`. This method uses evaluation
97
   * for subterms that evaluate to constants supported in the EvalResult
98
   * class and substitution with rewriting for the others.
99
   *
100
   * The nodes in vals are typically intended to be constant, although this
101
   * is not required.
102
   *
103
   * If the parameter useRewriter is true, then we may invoke calls to the
104
   * rewriter for computing the result of this method.
105
   *
106
   * The result of this call is either equivalent to:
107
   * (1) Rewriter::rewrite(n.substitute(args,vars))
108
   * (2) Node::null().
109
   * If d_rr is non-null, then we are always in the first case. If
110
   * useRewriter is null, then we may be in case (2) if computing the
111
   * rewritten, substituted form of n could not be determined by evaluation.
112
   */
113
  Node eval(TNode n,
114
            const std::vector<Node>& args,
115
            const std::vector<Node>& vals) const;
116
  /**
117
   * Same as above, but with a precomputed visited map.
118
   */
119
  Node eval(TNode n,
120
            const std::vector<Node>& args,
121
            const std::vector<Node>& vals,
122
            const std::unordered_map<Node, Node>& visited) const;
123
124
 private:
125
  /**
126
   * Evaluates node `n` under the substitution described by the variable names
127
   * `args` and the corresponding values `vals`. The internal version returns
128
   * an EvalResult which has slightly less overhead for recursive calls.
129
   *
130
   * The method returns an invalid EvalResult if the result of the substitution
131
   * on n does not result in a constant value that is one of those supported in
132
   * the EvalResult class. Notice that e.g. datatype constants are not supported
133
   * in this class.
134
   *
135
   * This method additionally adds subterms of n that could not be evaluated
136
   * (in the above sense) to the map evalAsNode. For each such subterm n', we
137
   * store the node corresponding to the result of applying the substitution
138
   * `args` to `vals` and rewriting. Notice that this map contains an entry
139
   * for n in the case that it cannot be evaluated.
140
   */
141
  EvalResult evalInternal(TNode n,
142
                          const std::vector<Node>& args,
143
                          const std::vector<Node>& vals,
144
                          std::unordered_map<TNode, Node>& evalAsNode,
145
                          std::unordered_map<TNode, EvalResult>& results) const;
146
  /** reconstruct
147
   *
148
   * This function reconstructs the result of evaluating n using a combination
149
   * of evaluation results (eresults) and substitution+rewriting (evalAsNode).
150
   *
151
   * Arguments eresults and evalAsNode are built within the context of the
152
   * above method for some args and vals. This method ensures that the return
153
   * value is equivalent to the rewritten form of n * { args -> vals }.
154
   */
155
  Node reconstruct(TNode n,
156
                   std::unordered_map<TNode, EvalResult>& eresults,
157
                   std::unordered_map<TNode, Node>& evalAsNode) const;
158
  /** The (optional) rewriter to be used */
159
  Rewriter* d_rr;
160
  /** The cardinality of the alphabet of strings */
161
  uint32_t d_alphaCard;
162
};
163
164
}  // namespace theory
165
}  // namespace cvc5
166
167
#endif /* CVC5__THEORY__EVALUATOR_H */