forked from bonetblai/mini-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressions.h
223 lines (186 loc) · 7.02 KB
/
expressions.h
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#ifndef EXPRESSIONS_H
#define EXPRESSIONS_H
#include "functions.h"
#include "terms.h"
#include "rational.h"
#include <assert.h>
#include <iostream>
#include <set>
#include <tr1/unordered_map>
class problem_t;
class Application;
/*******************************************************************************
*
* expression
*
******************************************************************************/
class ValueMap : public std::tr1::unordered_map<const Application*,Rational> { };
class Expression
{
mutable size_t ref_count_;
protected:
Expression() : ref_count_(0) { }
public:
virtual ~Expression()
{
assert( ref_count_ == 0 );
}
static void register_use( const Expression* e )
{
#ifdef MEM_DEBUG
if(e) std::cerr << "<exp>: inc-ref-count " << e << " = " << e->ref_count_+1 << std::endl;
#endif
if( e ) ++e->ref_count_;
}
static void unregister_use( const Expression* e )
{
#ifdef MEM_DEBUG
if(e) std::cerr << "<exp>: dec-ref-count " << e << " = " << e->ref_count_-1 << std::endl;
#endif
if( e && (--e->ref_count_ == 0) ) delete e;
}
virtual Rational value( const ValueMap& values ) const = 0;
virtual const Expression& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const = 0;
virtual bool operator==( const Expression &expr ) const = 0;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const = 0;
};
/*******************************************************************************
*
* value
*
******************************************************************************/
class Value : public Expression
{
Rational value_;
public:
Value( const Rational& value ) { value_ = value; }
const Rational& value( void ) const { return( value_ ); }
virtual Rational value( const ValueMap& values ) const;
virtual const Value& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const;
virtual bool operator==( const Expression &expr ) const;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
};
/*******************************************************************************
*
* application
*
******************************************************************************/
class Application : public Expression
{
class ApplicationLess : public std::binary_function<const Application*,const Application*,bool>
{
public:
bool operator()( const Application* a1, const Application* a2 ) const;
};
class ApplicationTable : public std::set<const Application*,ApplicationLess> { };
static ApplicationTable applications;
Function function_;
TermList terms_;
Application( Function function ) : function_(function) { }
void add_term( Term term ) { terms_.push_back( term ); }
public:
virtual ~Application();
static const Application& make_application( Function function,
const TermList& terms );
Function function( void ) const { return( function_ ); }
size_t arity( void ) const { return( terms_.size() ); }
Term term( size_t i ) const { return( terms_[i] ); }
virtual Rational value( const ValueMap& values ) const;
const Application& substitution( const SubstitutionMap& subst ) const;
virtual const Expression& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const;
virtual bool operator==( const Expression &expr ) const;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
void printXML( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
};
/*******************************************************************************
*
* addition
*
******************************************************************************/
class Addition : public Expression
{
const Expression* term1_;
const Expression* term2_;
public:
Addition( const Expression& term1, const Expression& term2 );
virtual ~Addition();
const Expression& term1( void ) const { return( *term1_ ); }
const Expression& term2( void ) const { return( *term2_ ); }
virtual Rational value( const ValueMap& values ) const;
virtual const Expression& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const;
virtual bool operator==( const Expression &expr ) const;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
};
/*******************************************************************************
*
* subtraction
*
******************************************************************************/
class Subtraction : public Expression
{
const Expression* term1_;
const Expression* term2_;
public:
Subtraction( const Expression& term1, const Expression& term2 );
virtual ~Subtraction();
const Expression& term1( void ) const { return( *term1_ ); }
const Expression& term2( void ) const { return( *term2_ ); }
virtual Rational value( const ValueMap& values ) const;
virtual const Expression& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const;
virtual bool operator==( const Expression &expr ) const;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
};
/*******************************************************************************
*
* multiplication
*
******************************************************************************/
class Multiplication : public Expression
{
const Expression* factor1_;
const Expression* factor2_;
public:
Multiplication( const Expression& factor1, const Expression& factor2 );
virtual ~Multiplication();
const Expression& factor1( void ) const { return( *factor1_ ); }
const Expression& factor2( void ) const { return( *factor2_ ); }
virtual Rational value( const ValueMap& values ) const;
virtual const Expression& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const;
virtual bool operator==( const Expression &expr ) const;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
};
/*******************************************************************************
*
* division
*
******************************************************************************/
class Division : public Expression
{
const Expression* factor1_;
const Expression* factor2_;
public:
Division( const Expression& factor1, const Expression& factor2 );
virtual ~Division();
const Expression& factor1( void ) const { return( *factor1_ ); }
const Expression& factor2( void ) const { return( *factor2_ ); }
virtual Rational value( const ValueMap& values ) const;
virtual const Expression& instantiation( const SubstitutionMap& subst,
const problem_t& problem ) const;
virtual bool operator==( const Expression &expr ) const;
virtual void print( std::ostream& os, const FunctionTable& functions,
const TermTable& terms ) const;
};
#endif // EXPRESSIONS_H