GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/datatypes/datatypes_rewriter.h Lines: 1 1 100.0 %
Date: 2021-05-22 Branches: 7 16 43.8 %

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
420840
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
  /** expand defintions */
52
  TrustNode expandDefinition(Node n) override;
53
54
 private:
55
  /** rewrite constructor term in */
56
  static RewriteResponse rewriteConstructor(TNode in);
57
  /** rewrite selector term in */
58
  static RewriteResponse rewriteSelector(TNode in);
59
  /** rewrite tester term in */
60
  static RewriteResponse rewriteTester(TNode in);
61
  /** rewrite updater term in */
62
  static RewriteResponse rewriteUpdater(TNode in);
63
64
  /** collect references
65
   *
66
   * This function, given as input a codatatype term n, collects the necessary
67
   * information for constructing a (canonical) codatatype constant that is
68
   * equivalent to n if one exists, or null otherwise.
69
   *
70
   * In particular it returns a term ret such that all non-codatatype datatype
71
   * subterms of n are replaced by a constant that is equal to them via a
72
   * (mutually) recursive call to normalizeConstant above. Additionally, this
73
   * function replaces references to mu-binders with fresh variables.
74
   * In detail, mu-terms are represented by uninterpreted constants of datatype
75
   * type that carry their Debruijn index.
76
   *
77
   * Consider the example of a codatatype representing a stream of integers:
78
   *   Stream := cons( head : Int, tail : Stream )
79
   * The stream 1,0,1,0,1,0... when written in mu-notation is the term:
80
   *   mu x. cons( 1, mu y. cons( 0, x ) )
81
   * This is represented in cvc5 by the Node:
82
   *   cons( 1, cons( 0, c[1] ) )
83
   * where c[1] is a uninterpreted constant datatype with Debruijn index 1,
84
   * indicating that c[1] is nested underneath 1 level on the path to the
85
   * term which it binds. On the other hand, the stream 1,0,0,0,0,... is
86
   * represented by the codatatype term:
87
   *   cons( 1, cons( 0, c[0] ) )
88
   *
89
   * Subterms that are references to mu-binders in n are replaced by a new
90
   * variable. If n contains any subterm that is a reference to a mu-binder not
91
   * bound in n, then we return null. For example we return null when n is:
92
   *   cons( 1, cons( 0, c[2] ) )
93
   * since c[2] is not bound by this codatatype term.
94
   *
95
   * All valid references to mu-binders are replaced by a variable that is
96
   * unique for the term it references. For example, for the infinite tree
97
   * codatatype: Tree : node( data : Int, left : Tree, right : Tree ) If n is
98
   * the term: node( 0, c[0], node( 1, c[0], c[1] ) ) then the return value ret
99
   * of this function is: node( 0, x, node( 1, y, x ) ) where x refers to the
100
   * root of the term and y refers to the right tree of the root.
101
   *
102
   * The argument sk stores the current set of node that we are traversing
103
   * beneath. The argument rf_pending stores, for each node that we are
104
   * traversing beneath either null or the free variable that we are using to
105
   * refer to its mu-binder. The remaining arguments store information that is
106
   * relevant when performing normalization of n using the value of ret:
107
   *
108
   * rf : maps subterms of n to the corresponding term in ret for all subterms
109
   * where the corresponding term in ret is different.
110
   * terms : stores all subterms of ret.
111
   * cdts : for each term t in terms, stores whether t is a codatatype.
112
   */
113
  static Node collectRef(Node n,
114
                         std::vector<Node>& sk,
115
                         std::map<Node, Node>& rf,
116
                         std::vector<Node>& rf_pending,
117
                         std::vector<Node>& terms,
118
                         std::map<Node, bool>& cdts);
119
  /** normalize codatatype constant eqc
120
   *
121
   * This recursive function returns a codatatype constant that is equivalent to
122
   * n based on a pre-computed partition of the subterms of n into equivalence
123
   * classes, as stored in the mapping eqc, which maps the subterms of n to
124
   * equivalence class ids. The arguments eqc_stack and depth store information
125
   * about the traversal in a term we have recursed, where
126
   *
127
   * eqc_stack : maps the depth of each term we have traversed to its
128
   * equivalence class id. depth : the number of levels which we have traversed.
129
   */
130
  static Node normalizeCodatatypeConstantEqc(Node n,
131
                                             std::map<int, int>& eqc_stack,
132
                                             std::map<Node, int>& eqc,
133
                                             int depth);
134
  /** replace debruijn
135
   *
136
   * This function, given codatatype term n, returns a node
137
   * where all subterms of n that have Debruijn indices that refer to a
138
   * term of input depth are replaced by orig. For example, for the infinite
139
   * Tree datatype, replaceDebruijn( node( 0, c[0], node( 1, c[0], c[1] ) ), t,
140
   * Tree, 0 ) returns node( 0, t, node( 1, c[0], t ) ).
141
   */
142
  static Node replaceDebruijn(Node n,
143
                              Node orig,
144
                              TypeNode orig_tn,
145
                              unsigned depth);
146
}; /* class DatatypesRewriter */
147
148
}  // namespace datatypes
149
}  // namespace theory
150
}  // namespace cvc5
151
152
#endif /* CVC5__THEORY__DATATYPES__DATATYPES_REWRITER_H */