GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/theory/quantifiers/alpha_equivalence.cpp Lines: 51 53 96.2 %
Date: 2021-09-15 Branches: 106 242 43.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Aina Niemetz
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
 * Alpha equivalence checking.
14
 */
15
16
#include "theory/quantifiers/alpha_equivalence.h"
17
18
using namespace cvc5::kind;
19
20
namespace cvc5 {
21
namespace theory {
22
namespace quantifiers {
23
24
struct sortTypeOrder {
25
  expr::TermCanonize* d_tu;
26
21876
  bool operator() (TypeNode i, TypeNode j) {
27
21876
    return d_tu->getIdForType( i )<d_tu->getIdForType( j );
28
  }
29
};
30
31
26871
Node AlphaEquivalenceTypeNode::registerNode(
32
    Node q,
33
    Node t,
34
    std::vector<TypeNode>& typs,
35
    std::map<TypeNode, size_t>& typCount)
36
{
37
26871
  AlphaEquivalenceTypeNode* aetn = this;
38
26871
  size_t index = 0;
39
107185
  while (index < typs.size())
40
  {
41
80314
    TypeNode curr = typs[index];
42
40157
    Assert(typCount.find(curr) != typCount.end());
43
40157
    Trace("aeq-debug") << "[" << curr << " " << typCount[curr] << "] ";
44
80314
    std::pair<TypeNode, size_t> key(curr, typCount[curr]);
45
40157
    aetn = &(aetn->d_children[key]);
46
40157
    index = index + 1;
47
  }
48
26871
  Trace("aeq-debug") << " : ";
49
26871
  std::map<Node, Node>::iterator it = aetn->d_quant.find(t);
50
26871
  if (it != aetn->d_quant.end())
51
  {
52
1795
    return it->second;
53
  }
54
25076
  aetn->d_quant[t] = q;
55
25076
  return q;
56
}
57
58
26871
Node AlphaEquivalenceDb::addTerm(Node q)
59
{
60
26871
  Assert(q.getKind() == FORALL);
61
26871
  Trace("aeq") << "Alpha equivalence : register " << q << std::endl;
62
  //construct canonical quantified formula
63
53742
  Node t = d_tc->getCanonicalTerm(q[1], true);
64
26871
  Trace("aeq") << "  canonical form: " << t << std::endl;
65
  //compute variable type counts
66
53742
  std::map<TypeNode, size_t> typCount;
67
53742
  std::vector< TypeNode > typs;
68
89539
  for( unsigned i=0; i<q[0].getNumChildren(); i++ ){
69
125336
    TypeNode tn = q[0][i].getType();
70
62668
    typCount[tn]++;
71
62668
    if( std::find( typs.begin(), typs.end(), tn )==typs.end() ){
72
40157
      typs.push_back( tn );
73
    }
74
  }
75
  sortTypeOrder sto;
76
26871
  sto.d_tu = d_tc;
77
26871
  std::sort( typs.begin(), typs.end(), sto );
78
26871
  Trace("aeq-debug") << "  ";
79
26871
  Node ret = d_ae_typ_trie.registerNode(q, t, typs, typCount);
80
26871
  Trace("aeq") << "  ...result : " << ret << std::endl;
81
53742
  return ret;
82
}
83
84
6839
AlphaEquivalence::AlphaEquivalence() : d_termCanon(), d_aedb(&d_termCanon) {}
85
86
26871
Node AlphaEquivalence::reduceQuantifier(Node q)
87
{
88
26871
  Assert(q.getKind() == FORALL);
89
26871
  Trace("aeq") << "Alpha equivalence : register " << q << std::endl;
90
53742
  Node ret = d_aedb.addTerm(q);
91
26871
  Node lem;
92
26871
  if (ret != q)
93
  {
94
    // lemma ( q <=> d_quant )
95
    // Notice that we infer this equivalence regardless of whether q or ret
96
    // have annotations (e.g. user patterns, names, etc.).
97
1795
    Trace("alpha-eq") << "Alpha equivalent : " << std::endl;
98
1795
    Trace("alpha-eq") << "  " << q << std::endl;
99
1795
    Trace("alpha-eq") << "  " << ret << std::endl;
100
1795
    lem = q.eqNode(ret);
101
1795
    if (q.getNumChildren() == 3)
102
    {
103
78
      Notice() << "Ignoring annotated quantified formula based on alpha "
104
                  "equivalence: "
105
               << q << std::endl;
106
    }
107
  }
108
53742
  return lem;
109
}
110
111
}  // namespace quantifiers
112
}  // namespace theory
113
29577
}  // namespace cvc5