summaryrefslogtreecommitdiff
path: root/src/theory/rewriterules/theory_rewriterules_preprocess.h
blob: f17e30758717509d3da7b863a67e8d54a2f51081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*********************                                                        */
/*! \file rewriterules.h
 ** \verbatim
 ** Original author: bobot
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010, 2011  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief One utilitise for rewriterules definition
 **
 **/


#ifndef __CVC4__REWRITERULES_H
#define __CVC4__REWRITERULES_H

#include <vector>
#include <hash_set>
#include <hash_map>
#include "expr/expr.h"
#include "expr/node.h"
#include "theory/rewriterules/theory_rewriterules_params.h"
#include "theory/uf/theory_uf.h"

namespace CVC4 {
namespace theory {
namespace rewriterules {

namespace rewriter{

  typedef Node TMPNode;
  typedef std::vector<Node> Subst;
  typedef std::vector<Expr> ESubst;
  typedef std::vector<TMPNode> TSubst;

  struct Step{

    /** match the node and add in Vars the found variables */
    virtual Node run(TMPNode node) = 0;
    virtual bool add(TMPNode pattern, TMPNode body, Subst & pvars, Subst & vars) = 0;
  };

  struct FinalStep : Step {
    Node body;
    TSubst vars;

    Node subst(Subst & subst){
      return body.substitute(vars.begin(), vars.end(),
                             subst.begin(), subst.end());
    }

  };

  typedef std::hash_map< Node, int, NodeHashFunction > PVars;

  struct Pattern : FinalStep{
    Node pattern;
    PVars pattern_vars;

    Node run(TMPNode node){
      Subst vars = Subst(pattern_vars.size(),Node::null());

      typedef std::vector<std::pair<TMPNode,TMPNode> > tstack;
      tstack stack(1,std::make_pair(node,pattern)); // t * pat

      while(!stack.empty()){
        const std::pair<TMPNode,TMPNode> p = stack.back(); stack.pop_back();
        const TMPNode & t = p.first;
        const TMPNode & pat = p.second;

        // pat is a variable
        if( pat.getKind() == kind::INST_CONSTANT ||
            pat.getKind() == kind::VARIABLE){
          PVars::iterator i = pattern_vars.find(pat);
          Assert(i != pattern_vars.end());
          if(vars[i->second].isNull()) vars[i->second] = t;
          if(vars[i->second] == t) continue;
          return Node::null();
        };

        // t is not an UF application
        if( t.getKind() != kind::APPLY_UF ){
          if (t == pat) continue;
          else return Node::null();
        };

        //different UF_application
        if( t.getOperator() != pat.getOperator() ) return Node::null();

        //put the children on the stack
        for( size_t i=0; i < pat.getNumChildren(); i++ ){
          stack.push_back(std::make_pair(t[i],pat[i]));
        };
      }

      // Matching is done
      return subst(vars);
    }

    bool add(TMPNode pattern, TMPNode body, Subst & pvars, Subst & vars){
      return false;
    }
    
  };


  struct Args : Step {
    typedef std::vector<Pattern> Patterns;
    Patterns d_matches;

    Node run(TMPNode node){
      Node n;
      for (Patterns::iterator i = d_matches.begin();
           i != d_matches.end() && n.isNull(); ++i){
        Debug("rewriterules-rewrite") << "test?" << i->pattern << std::endl;
        n = i->run(node);
      }
      return n;
    }

    bool add(TMPNode pattern, TMPNode body, Subst & pvars, Subst & vars){
      Debug("rewriterules-rewrite") << "theoryrewrite::Args::add" << "("
                                    << d_matches.size() << ")"
                                    << pattern << "->" << body << std::endl;
      d_matches.push_back(Pattern());
      Pattern & p = d_matches.back();
      p.body = body;
      p.vars.reserve(vars.size());
      for( size_t i=0; i < vars.size(); i++ ){
        p.vars.push_back(vars[i]);
      };
      p.pattern = pattern;
      for( size_t i=0; i < pvars.size(); i++ ){
        p.pattern_vars[pvars[i]] = i;
      };
      return true;
    };

    void clear(){
      d_matches.clear();
    }
  };

class RRPpRewrite : public uf::TheoryUF::PpRewrite {
  Args d_pattern;
public:
  Node ppRewrite(TNode node){
    Debug("rewriterules-rewrite") << "rewrite?" << node << std::endl;
    Node t = d_pattern.run(node);
    Debug("rewriterules-rewrite") << "rewrite:" << node
                                  << (t.isNull()? " to": " to ")
                                  << t << std::endl;
    if (t.isNull()) return node;
    else return t;
  }

  bool addRewritePattern(TMPNode pattern, TMPNode body,
                                Subst & pvars, Subst & vars){
    return d_pattern.add(pattern,body,pvars,vars);
  }

};



}

}
}
}
#endif /* __CVC4__REWRITERULES_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback