-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathunification.hpp
391 lines (325 loc) · 8.82 KB
/
unification.hpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#pragma once
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <stdexcept>
#include <boost/variant.hpp>
#include <boost/variant/recursive_wrapper.hpp>
namespace unification
{
class type_variable;
class type_operator;
typedef boost::variant<
type_variable,
boost::recursive_wrapper<type_operator>
> type;
class type_variable
{
public:
inline type_variable()
: m_id()
{}
inline type_variable(const std::size_t i)
: m_id(i)
{}
inline std::size_t id() const
{
return m_id;
} // end id()
inline bool operator==(const type_variable &other) const
{
return id() == other.id();
} // end operator==()
inline bool operator!=(const type_variable &other) const
{
return !(*this == other);
} // end operator!=()
inline bool operator<(const type_variable &other) const
{
return id() < other.id();
} // end operator<()
inline operator std::size_t (void) const
{
return id();
} // end operator size_t
private:
std::size_t m_id;
}; // end type_variable
class type_operator
: private std::vector<type>
{
public:
typedef std::size_t kind_type;
private:
typedef std::vector<type> super_t;
std::vector<type> m_types;
kind_type m_kind;
public:
using super_t::begin;
using super_t::end;
using super_t::size;
using super_t::operator[];
inline type_operator(const type_operator &other)
: super_t(other),
m_types(other.m_types),
m_kind(other.m_kind)
{}
inline type_operator(const kind_type &kind)
: m_kind(kind)
{}
template<typename Iterator>
type_operator(const kind_type &kind,
Iterator first,
Iterator last)
: super_t(first, last),
m_kind(kind)
{}
template<typename Range>
inline type_operator(const kind_type &kind,
const Range &rng)
: super_t(rng.begin(), rng.end()),
m_kind(kind)
{}
inline type_operator(const kind_type &kind,
std::initializer_list<type> &&types)
: super_t(types),
m_kind(kind)
{}
inline type_operator(type_operator &&other)
: super_t(std::move(other)),
m_kind(std::move(other.m_kind))
{}
inline type_operator &operator=(const type_operator &other)
{
super_t::operator=(other);
m_types = other.m_types;
m_kind = other.m_kind;
return *this;
}
inline const kind_type &kind(void) const
{
return m_kind;
} // end kind()
inline bool compare_kind(const type_operator &other) const
{
return kind() == other.kind() && size() == other.size();
} // end operator==()
inline bool operator==(const type_operator &other) const
{
return compare_kind(other) & std::equal(begin(), end(), other.begin());
} // end operator==()
}; // end type_operator
typedef std::pair<type, type> constraint;
struct type_mismatch
: std::runtime_error
{
inline type_mismatch(const type &xx, const type &yy)
: std::runtime_error("type mismatch"),
x(xx),
y(yy)
{}
inline virtual ~type_mismatch(void) throw()
{}
type x;
type y;
};
struct recursive_unification
: std::runtime_error
{
inline recursive_unification(const type &xx, const type &yy)
: std::runtime_error("recursive unification"),
x(xx),
y(yy)
{}
inline virtual ~recursive_unification(void) throw()
{}
type x, y;
};
namespace detail
{
inline void replace(type &x, const type_variable &replace_me, const type &replacement)
{
if(x.which())
{
auto &op = boost::get<type_operator>(x);
auto f = std::bind(replace, std::placeholders::_1, replace_me, replacement);
std::for_each(op.begin(), op.end(), f);
} // end if
else
{
auto &var = boost::get<type_variable>(x);
if(var == replace_me)
{
x = replacement;
} // end if
} // end else
} // end replace()
inline bool occurs(const type &haystack, const type_variable &needle)
{
bool result = false;
if(haystack.which())
{
auto &op = boost::get<type_operator>(haystack);
auto f = std::bind(occurs, std::placeholders::_1, needle);
result = std::any_of(op.begin(), op.end(), f);
} // end end if
else
{
auto &var = boost::get<type_variable>(haystack);
result = (var == needle);
} // end else
return result;
} // end occurs()
struct equals_variable
: boost::static_visitor<bool>
{
inline equals_variable(const type_variable &xx)
: m_x(xx)
{}
inline bool operator()(const type_variable &y)
{
return m_x == y;
} // end operator()()
inline bool operator()(const type_operator &y)
{
return false;
} // end operator()()
const type_variable &m_x;
}; // end equals_variable
struct replacer
: boost::static_visitor<>
{
inline replacer(const type_variable &replace_me)
: m_replace_me(replace_me)
{}
inline void operator()(type_variable &var, const type_variable &replacement)
{
if(var == m_replace_me)
{
var = replacement;
} // end if
} // end operator()()
template<typename T>
inline void operator()(type_operator &op, const T &replacement)
{
auto v = boost::apply_visitor(*this);
auto f = std::bind(v, std::placeholders::_2, replacement);
std::for_each(op.begin(), op.end(), f);
} // end operator()()
const type_variable &m_replace_me;
}; // end replacer
class unifier
: public boost::static_visitor<>
{
inline void eliminate(const type_variable &x, const type &y)
{
// replace all occurrances of x with y in the stack and the substitution
for(auto i = m_stack.begin();
i != m_stack.end();
++i)
{
replace(i->first, x, y);
replace(i->second, x, y);
} // end for i
for(auto i = m_substitution.begin();
i != m_substitution.end();
++i)
{
replace(i->second, x, y);
} // end for i
// add x = y to the substitution
m_substitution[x] = y;
} // end eliminate()
std::vector<constraint> m_stack;
std::map<type_variable, type> &m_substitution;
public:
// apply_visitor requires that these functions be public
inline void operator()(const type_variable &x, const type_variable &y)
{
if(x != y)
{
eliminate(x,y);
} // end if
} // end operator()()
inline void operator()(const type_variable &x, const type_operator &y)
{
if(occurs(y,x))
{
throw recursive_unification(x,y);
} // end if
eliminate(x,y);
} // end operator()()
inline void operator()(const type_operator &x, const type_variable &y)
{
if(occurs(x,y))
{
throw recursive_unification(y,x);
} // end if
eliminate(y,x);
} // end operator()()
inline void operator()(const type_operator &x, const type_operator &y)
{
if(!x.compare_kind(y))
{
throw type_mismatch(x,y);
} // end if
// push (xi,yi) onto the stack
for(auto xi = x.begin(), yi = y.begin();
xi != x.end();
++xi, ++yi)
{
m_stack.push_back(std::make_pair(*xi, *yi));
} // end for xi, yi
} // end operator()()
template<typename Iterator>
inline unifier(Iterator first_constraint, Iterator last_constraint, std::map<type_variable,type> &substitution)
: m_stack(first_constraint, last_constraint),
m_substitution(substitution)
{
// add the current substitution to the stack
// XXX this step might be unnecessary
m_stack.insert(m_stack.end(), m_substitution.begin(), m_substitution.end());
m_substitution.clear();
} // end unifier()
inline void operator()(void)
{
while(!m_stack.empty())
{
type x = std::move(m_stack.back().first);
type y = std::move(m_stack.back().second);
m_stack.pop_back();
boost::apply_visitor(*this, x, y);
} // end while
} // end operator()()
}; // unifier()
} // end detail
template<typename Iterator>
void unify(Iterator first_constraint, Iterator last_constraint, std::map<type_variable,type> &substitution)
{
detail::unifier u(first_constraint, last_constraint, substitution);
u();
} // end unify()
template<typename Range>
void unify(const Range &rng, std::map<type_variable,type> &substitution)
{
return unify(rng.begin(), rng.end(), substitution);
} // end unify()
// often our system has only a single constraint
void unify(const type &x, const type &y, std::map<type_variable,type> &substitution)
{
auto c = constraint(x,y);
return unify(&c, &c + 1, substitution);
} // end unify()
template<typename Range>
std::map<type_variable,type>
unify(const Range &rng)
{
std::map<type_variable,type> solutions;
unify(rng, solutions);
return std::move(solutions);
} // end unify()
} // end unification