GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/datatypes_rewriter.h Lines: 1 1 100.0 %
Date: 2021-09-07 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
 * Rewriter for the theory of (co)inductive datatypes.
14
 */
15
16
#include "cvc5_private.h"
17
18
#ifndef CVC5__THEORY__DATATYPES__DATATYPES_REWRITER_H
19
#define CVC5__THEORY__DATATYPES__DATATYPES_REWRITER_H
20
21
#include "theory/theory_rewriter.h"
22
23
namespace cvc5 {
24
namespace theory {
25
namespace datatypes {
26
27
19849
class DatatypesRewriter : public TheoryRewriter
28
{
29
 public:
30
  RewriteResponse postRewrite(TNode in) override;
31
  RewriteResponse preRewrite(TNode in) override;
32
33
  /** normalize codatatype constant
34
   *
35
   * This returns the normal form of the codatatype constant n. This runs a
36
   * DFA minimization algorithm based on the private functions below.
37
   *
38
   * In particular, we first call collectRefs to setup initial information
39
   * about what terms occur in n. Then, we run a DFA minimization algorithm to
40
   * partition these subterms in equivalence classes. Finally, we call
41
   * normalizeCodatatypeConstantEqc to construct the normalized codatatype
42
   * constant that is equivalent to n.
43
   */
44
  static Node normalizeCodatatypeConstant(Node n);
45
  /** normalize constant
46
   *
47
   * This method returns the normal form of n, which calls the above function
48
   * on all top-level codatatype subterms of n.
49
   */
50
  static Node normalizeConstant(Node n);
51
  /**
52
   * Expand an APPLY_SELECTOR term n, return its expanded form. If n is
53
   *   (APPLY_SELECTOR selC x)
54
   * its expanded form is
55
   *   (ITE (APPLY_TESTER is-C x)
56
   *     (APPLY_SELECTOR_TOTAL selC' x)
57
   *     (f x))
58
   * where f is a skolem function with id SELECTOR_WRONG, and selC' is the
59
   * internal selector function for selC (possibly a shared selector).
60
   */
61
  static Node expandApplySelector(Node n);
62
  /** expand defintions */
63
  TrustNode expandDefinition(Node n) override;
64
65
 private:
66
  /** rewrite constructor term in */
67
  static RewriteResponse rewriteConstructor(TNode in);
68
  /** rewrite selector term in */
69
  static RewriteResponse rewriteSelector(TNode in);
70
  /** rewrite tester term in */
71
  static RewriteResponse rewriteTester(TNode in);
72
  /** rewrite updater term in */
73
  static RewriteResponse rewriteUpdater(TNode in);
74
75
  /** collect references
76
   *
77
   * This function, given as input a codatatype term n, collects the necessary
78
   * information for constructing a (canonical) codatatype constant that is
79
   * equivalent to n if one exists, or null otherwise.
80
   *
81
   * In particular it returns a term ret such that all non-codatatype datatype
82
   * subterms of n are replaced by a constant that is equal to them via a
83
   * (mutually) recursive call to normalizeConstant above. Additionally, this
84
   * function replaces references to mu-binders with fresh variables.
85
   * In detail, mu-terms are represented by uninterpreted constants of datatype
86
   * type that carry their Debruijn index.
87
   *
88
   * Consider the example of a codatatype representing a stream of integers:
89
   *   Stream := cons( head : Int, tail : Stream )
90
   * The stream 1,0,1,0,1,0... when written in mu-notation is the term:
91
   *   mu x. cons( 1, mu y. cons( 0, x ) )
92
   * This is represented in cvc5 by the Node:
93
   *   cons( 1, cons( 0, c[1] ) )
94
   * where c[1] is a uninterpreted constant datatype with Debruijn index 1,
95
   * indicating that c[1] is nested underneath 1 level on the path to the
96
   * term which it binds. On the other hand, the stream 1,0,0,0,0,... is
97
   * represented by the codatatype term:
98
   *   cons( 1, cons( 0, c[0] ) )
99
   *
100
   * Subterms that are references to mu-binders in n are replaced by a new
101
   * variable. If n contains any subterm that is a reference to a mu-binder not
102
   * bound in n, then we return null. For example we return null when n is:
103
   *   cons( 1, cons( 0, c[2] ) )
104
   * since c[2] is not bound by this codatatype term.
105
   *
106
   * All valid references to mu-binders are replaced by a variable that is
107
   * unique for the term it references. For example, for the infinite tree
108
   * codatatype: Tree : node( data : Int, left : Tree, right : Tree ) If n is
109
   * the term: node( 0, c[0], node( 1, c[0], c[1] ) ) then the return value ret
110
   * of this function is: node( 0, x, node( 1, y, x ) ) where x refers to the
111
   * root of the term and y refers to the right tree of the root.
112
   *
113
   * The argument sk stores the current set of node that we are traversing
114
   * beneath. The argument rf_pending stores, for each node that we are
115
   * traversing beneath either null or the free variable that we are using to
116
   * refer to its mu-binder. The remaining arguments store information that is
117
   * relevant when performing normalization of n using the value of ret:
118
   *
119
   * rf : maps subterms of n to the corresponding term in ret for all subterms
120
   * where the corresponding term in ret is different.
121
   * terms : stores all subterms of ret.
122
   * cdts : for each term t in terms, stores whether t is a codatatype.
123
   */
124
  static Node collectRef(Node n,
125
                         std::vector<Node>& sk,
126
                         std::map<Node, Node>& rf,
127
                         std::vector<Node>& rf_pending,
128
                         std::vector<Node>& terms,
129
                         std::map<Node, bool>& cdts);
130
  /** normalize codatatype constant eqc
131
   *
132
   * This recursive function returns a codatatype constant that is equivalent to
133
   * n based on a pre-computed partition of the subterms of n into equivalence
134
   * classes, as stored in the mapping eqc, which maps the subterms of n to
135
   * equivalence class ids. The arguments eqc_stack and depth store information
136
   * about the traversal in a term we have recursed, where
137
   *
138
   * eqc_stack : maps the depth of each term we have traversed to its
139
   * equivalence class id. depth : the number of levels which we have traversed.
140
   */
141
  static Node normalizeCodatatypeConstantEqc(Node n,
142
                                             std::map<int, int>& eqc_stack,
143
                                             std::map<Node, int>& eqc,
144
                                             int depth);
145
  /** replace debruijn
146
   *
147
   * This function, given codatatype term n, returns a node
148
   * where all subterms of n that have Debruijn indices that refer to a
149
   * term of input depth are replaced by orig. For example, for the infinite
150
   * Tree datatype, replaceDebruijn( node( 0, c[0], node( 1, c[0], c[1] ) ), t,
151
   * Tree, 0 ) returns node( 0, t, node( 1, c[0], t ) ).
152
   */
153
  static Node replaceDebruijn(Node n,
154
                              Node orig,
155
                              TypeNode orig_tn,
156
                              unsigned depth);
157
}; /* class DatatypesRewriter */
158
159
}  // namespace datatypes
160
}  // namespace theory
161
}  // namespace cvc5
162
163
#endif /* CVC5__THEORY__DATATYPES__DATATYPES_REWRITER_H */