-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclors.cpp
More file actions
1900 lines (1626 loc) · 53.6 KB
/
clors.cpp
File metadata and controls
1900 lines (1626 loc) · 53.6 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012, 2013, 2014 Keean Schupke
// compile with g++ -std=gnu++11
#include <string>
#include <vector>
#include <forward_list>
#include <iostream>
#include <iomanip>
#include <memory>
#include <fstream>
#include <stdexcept>
#include <vector>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <sstream>
#include <type_traits>
#include <ctime>
#include <cassert>
extern "C" {
#include <sys/resource.h>
}
#ifdef DEBUG
#define IF_DEBUG(X) X
#else
#define IF_DEBUG(X)
#endif
using namespace std;
//----------------------------------------------------------------------------
// Profiling
inline uint64_t rtime() {
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
return 1000000 * static_cast<uint64_t>(rusage.ru_utime.tv_sec) + static_cast<uint64_t>(rusage.ru_utime.tv_usec);
}
class profile {
static uint64_t t;
static uint64_t s;
public:
profile() {
s = rtime();
}
~profile() {
t += rtime() - s;
}
static void start() {
s = rtime();
}
static void finish() {
t += rtime() - s;
}
static uint64_t report() {
return t;
}
};
uint64_t profile::t {0};
uint64_t profile::s;
class depth_profile {
static int depth;
public:
depth_profile(int const i) {
depth = i;
profile::start();
}
~depth_profile() {
profile::finish();
}
static int report() {
return depth;
}
};
int depth_profile::depth;
//----------------------------------------------------------------------------
// Character Predicates
struct is_space {
string const name = "space";
bool operator() (int const c) const {
return ::isspace(c) != 0;
}
} is_space;
struct is_digit {
string const name = "digit";
bool operator() (int const c) const {
return ::isdigit(c) != 0;
}
} is_digit;
struct is_upper {
string const name = "uppercase";
bool operator() (int const c) const {
return ::isupper(c) != 0;
}
} is_upper;
struct is_lower {
string const name = "lowercase";
bool operator() (int const c) const {
return ::islower(c) != 0;
}
} is_lower;
struct is_alpha {
string const name = "alphabetic";
bool operator() (int const c) const {
return ::isalpha(c) != 0;
}
} is_alpha;
struct is_alnum {
string const name = "alphanumeric";
bool operator() (int const c) const {
return ::isalnum(c) != 0;
}
} is_alnum;
class is_char {
int const k;
public:
string const name;
explicit is_char(char const c) : k(c), name("'" + string(1, c) + "'") {}
bool operator() (int const c) const {
return k == c;
}
};
template <typename T, typename U> class is_either {
T const& a;
U const& b;
public:
string const name;
is_either(T const& a, U const& b) : a(a), b(b), name("(" + a.name + " or " + b.name + ")") {}
bool operator() (int const c) const {
return a(c) || b(c);
}
};
template <typename T> class is_not {
T const& a;
public:
string const name;
is_not(T const& a) : a(a), name("~" + a.name) {}
bool operator() (int const c) const {
return !a(c);
}
};
//----------------------------------------------------------------------------
// Recursive Descent Parser
struct parse_error : public runtime_error {
int const row;
int const col;
int const sym;
string const exp;
parse_error(string const& what, int row, int col, string exp, int sym)
: runtime_error(what), row(row), col(col), exp(move(exp)), sym(sym) {}
};
class fparse {
fstream *in;
int row;
int col;
int sym;
protected:
void next() {
sym = in->get();
if (sym == '\n') {
++row;
col = 1;
} else {
++col;
}
}
void error(string const& err, string const exp) {
throw parse_error(err, row, col, exp, sym);
}
template <typename Term> bool test(Term const& t) {
return t(sym);
}
template <typename Term> bool accept(Term const& t, string *s = nullptr) {
if (t(sym)) {
if (s != nullptr) {
s->push_back(sym);
}
next();
return true;
}
return false;
}
template <typename Term> void expect(Term const& t, string *s = nullptr) {
if (!t(sym)) {
error("expected", t.name);
}
if (s != nullptr) {
s->push_back(sym);
}
next();
}
void space(string *s = nullptr) {
while (accept(is_space, s));
}
void number(string *s = nullptr) {
expect(is_digit, s);
while (accept(is_digit, s));
}
void name(string *s = nullptr) {
expect(is_alpha, s);
while (accept(is_alnum, s));
}
void set_fstream(fstream *f) {
in = f;
row = 1;
col = 1;
sym = f->get();
}
};
//----------------------------------------------------------------------------
// Clause Environment
class type_expression;
class type_variable;
class type_attrvar;
class type_atom;
class type_struct;
class type_clause;
using env_type = map<type_atom*, vector<type_clause*>>;
using atoms = map<string, type_atom*>;
//----------------------------------------------------------------------------
// Expression Graph
struct ast {
virtual ~ast() {};
};
using union_stack = vector<pair<type_expression *const, bool const>>;
class type_expression : public ast {
type_expression *canonical;
int rank;
protected:
type_expression() : canonical(this), rank(0) {}
public:
virtual void accept(class type_visitor *v) = 0;
void deunion(bool ranked) {
if (ranked) {
--(canonical->rank);
}
canonical = this;
}
// find the canonical type
friend type_expression* find(type_expression* e) {
while (e != e->canonical) {
e = e->canonical;
}
return e;
}
// let the algorithm pick the most efficient substitution
friend void link(type_expression* x, type_expression* y, union_stack& u) {
bool ranked = false;
if (x->rank > y->rank) {
swap(x, y);
} else if (x->rank == y->rank) {
ranked = true;
++(y->rank);
}
x->canonical = y;
u.emplace_back(x, ranked);
}
friend void link2(type_attrvar*& x, type_attrvar*& y, union_stack& u);
void replace_with(type_expression *e, union_stack& u) {
bool const ranked = (rank == e->rank);
if (ranked) {
++(e->rank);
}
canonical = e;
u.emplace_back(this, ranked);
}
};
class type_variable : public type_expression {
friend class heap;
template <typename T>
type_variable(T&& name) : name(forward<T>(name)) {}
public:
string const name;
virtual void accept(class type_visitor *v) override;
};
class type_attrvar : public type_expression {
friend class heap;
type_attrvar(type_variable* var, type_struct* goal) : var(var), goal(goal), next(nullptr) {}
public:
type_variable* const var;
type_struct* const goal;
type_attrvar* next;
virtual void accept(class type_visitor *v) override;
};
class type_atom : public type_expression {
friend class heap;
protected:
type_atom(string const value) : value(move(value)) {}
public:
string const value;
virtual void accept(class type_visitor *v) override;
};
class type_struct : public type_expression {
friend class heap;
template <typename T>
type_struct(type_atom* const functor, T&& args, bool neg)
: functor(functor), args(forward<T>(args)), negated(neg) {}
public:
type_atom* const functor;
vector<type_expression*> const args;
bool const negated;
virtual void accept(class type_visitor *v) override;
};
class type_clause : public type_expression {
friend class heap;
public:
template <typename T, typename U>
type_clause(type_struct *head, T&& cyck, U&& impl, int id)
: head(head), cyck(forward<T>(cyck)), impl(forward<U>(impl)), id(id) {}
int const id;
type_struct *const head;
set<type_variable*> const cyck;
vector<type_struct*> const impl;
virtual void accept(class type_visitor *v) override;
};
struct type_visitor {
virtual void visit(type_variable *t) = 0;
virtual void visit(type_attrvar *t) = 0;
virtual void visit(type_atom *t) = 0;
virtual void visit(type_struct *t) = 0;
virtual void visit(type_clause *t) = 0;
};
void type_variable::accept(class type_visitor *v) {v->visit(this);}
void type_attrvar::accept(class type_visitor *v) {v->visit(this);}
void type_atom::accept(class type_visitor *v) {v->visit(this);}
void type_struct::accept(class type_visitor *v) {v->visit(this);}
void type_clause::accept(class type_visitor *v) {v->visit(this);}
void link2(type_attrvar*& x, type_attrvar*& y, union_stack& u) {
bool ranked = false;
if (x->rank > y->rank) {
swap(x, y);
} else if (x->rank == y->rank) {
ranked = true;
++(y->rank);
}
x->canonical = y;
u.emplace_back(x, ranked);
}
//----------------------------------------------------------------------------
// Heap : The Global Stack
class heap {
vector<unique_ptr<ast>> region;
public:
heap() {};
heap(const heap&) = delete;
heap(heap&&) = default;
heap& operator= (const heap&) = delete;
int checkpoint() {
return region.size();
}
void backtrack(int p) {
while (region.size() > p) {
region.pop_back();
}
}
// Types
template <typename T>
type_variable* new_type_variable(T&& n) {
type_variable *const t = new type_variable(n);
region.emplace_back(t);
return t;
}
type_attrvar* new_type_attrvar(type_variable* v, type_struct* g) {
type_attrvar* const t = new type_attrvar(v, g);
region.emplace_back(t);
return t;
}
type_atom* new_type_atom(string const& value) {
type_atom *const t = new type_atom(value);
region.emplace_back(t);
return t;
}
template <typename T>
type_struct* new_type_struct(type_atom* const functor, T&& args, bool neg) {
type_struct *const t = new type_struct(functor, forward<T>(args), neg);
region.emplace_back(t);
return t;
}
template <typename T = set<type_variable*>, typename U = vector<type_struct*>>
type_clause* new_type_clause(type_struct *head
, T&& cyck = set<type_variable*> {}, U&& goals = vector<type_struct*> {}
, int id = 0) {
type_clause *const t = new type_clause(head, forward<T>(cyck), forward<U>(goals), id);
region.emplace_back(t);
return t;
}
};
//----------------------------------------------------------------------------
// Show Type Graph - assumes no cycles
class var_map {
using map_type = map<type_variable*, int>;
using map_name = map<string, int>;
map_type tmap;
map_name nmap;
public:
void clear() {
tmap.clear();
nmap.clear();
}
int get(type_variable *t) {
map_type::iterator const i = tmap.find(t);
if (i == tmap.end()) {
map_name::iterator const j = nmap.find(t->name);
int id = 1;
if (j == nmap.end()) {
nmap[t->name] = id;
} else {
id = ++(j->second);
}
tmap[t] = id;
return id;
} else {
return i->second;
}
}
};
class type_show : public type_visitor {
var_map tvar_map;
bool debug;
bool top;
bool constraint;
void show_variable(type_variable *const t) {
int const x {tvar_map.get(t)};
stringstream ss;
ss << t->name << x;
cout << ss.str();
}
void show_struct(type_struct *const t) {
if (t->negated) {
cout << "-";
}
cout << t->functor->value;
if (t->args.size() > 0) {
cout << "(";
for (auto i = t->args.begin(); i != t->args.end(); ++i) {
(*i)->accept(this);
if (i + 1 != t->args.end()) {
cout << ", ";
}
}
cout << ")";
}
}
public:
virtual void visit(type_variable *const t) override {
if (top) {
top = false;
show_variable(t);
type_expression *const e = find(t);
if (t != e) {
cout << " = ";
e->accept(this);
}
top = true;
} else {
type_expression *const e = find(t);
if (t != e) {
e->accept(this);
} else {
show_variable(t);
}
}
}
virtual void visit(type_attrvar *const t) override {
show_variable(t->var);
if (!constraint && t->goal != nullptr) {
constraint = true;
cout << "{";
int j = 0;
for (auto i = t; i != nullptr; i = i->next) {
++j;
assert(i->goal != nullptr);
show_struct(i->goal);
if (i->next != nullptr) {
cout << ", ";
}
}
cout << "} ";
constraint = false;
}
}
virtual void visit(type_atom *const t) override {
cout << t->value;
}
virtual void visit(type_struct *const t) override {
show_struct(t);
}
virtual void visit(type_clause *const t) override {
cout << t->id << ".\t";
show_struct(t->head);
IF_DEBUG(
if (t->cyck.size() > 0) {
cout << " [";
for (set<type_variable*>::iterator i = t->cyck.begin(); i != t->cyck.end();) {
show_variable(*i);
++i;
if (i != t->cyck.end()) {
cout << ", ";
}
}
cout << "]";
}
)
if (t->impl.size() > 0) {
cout << " :-\n";
for (auto i = t->impl.begin(); i != t->impl.end(); ++i) {
cout << "\t";
show_struct(*i);
if (i + 1 != t->impl.end()) {
cout << ",\n";
}
}
}
}
explicit type_show(bool debug = false) : debug(debug) {}
void operator() (type_expression *const t) {
if (t != nullptr) {
constraint = false;
top = true;
t->accept(this);
}
}
template <typename T> void range(typename T::const_iterator const begin, typename T::const_iterator const end) {
for (typename T::const_iterator i = begin; i != end; ++i) {
operator() (*i);
if (i + 1 != end) {
cout << ", ";
}
}
}
void reset() {
tvar_map.clear();
}
};
//----------------------------------------------------------------------------
// Test if term is ground - assumes no cycles.
class is_ground : public type_visitor {
vector<type_expression*> todo;
enum result {none, variable, attributed} result;
type_variable* var;
type_attrvar* attr;
virtual void visit(type_variable* const t) override {
var = t;
result = variable;
}
virtual void visit(type_attrvar* const t) override {
attr = t;
result = attributed;
}
virtual void visit(type_atom* const t) override {}
virtual void visit(type_struct* const t) override {
for (type_expression *const u : t->args) {
todo.push_back(u);
}
}
virtual void visit(type_clause* const t) override {
todo.push_back(t->head);
for (type_struct* const u : t->impl) {
todo.push_back(t);
}
}
enum result operator() (type_expression* const t) {
result = none;
todo.clear();
todo.push_back(t);
while ((!todo.empty()) && (result == none)) {
type_expression* const u = find(todo.back());
todo.pop_back();
find(u)->accept(this);
}
return result;
}
};
//----------------------------------------------------------------------------
// Get Vars - assumes no cycles
class get_variables : public type_visitor {
using tvars_type = set<type_variable*>;
tvars_type tvars;
public:
virtual void visit(type_variable *const t) override {
tvars.insert(t);
}
virtual void visit(type_attrvar *const t) override {
tvars.insert(t->var);
}
virtual void visit(type_atom *const t) override {}
virtual void visit(type_struct *const t) override {
for (type_expression *const u : t->args) {
find(u)->accept(this);
}
}
virtual void visit(type_clause *const t) override {
find(t->head)->accept(this);
for (type_struct *const u : t->impl) {
find(u)->accept(this);
}
}
vector<type_expression*> operator() (vector<type_struct *> const ts) {
tvars.clear();
for (auto const &t : ts) {
find(t)->accept(this);
}
vector<type_expression*> args;
args.assign(tvars.begin(), tvars.end());
return move(args);
}
};
//----------------------------------------------------------------------------
// Instantiate Type - assumes no cycles
class type_instantiate : public type_visitor {
using tvar_map_type = map<type_variable*, type_variable*>;
heap& ast;
tvar_map_type tvar_map;
type_expression *exp;
type_struct* inst_struct(type_struct *const t) {
vector<type_expression*> args;
for (type_expression *const e : t->args) {
find(e)->accept(this);
args.push_back(exp);
}
return ast.new_type_struct(t->functor, move(args), t->negated);
}
type_variable* inst_var(type_variable *const t) {
tvar_map_type::iterator const i = tvar_map.find(t);
if (i == tvar_map.end()) { // fresh type variable
type_variable *const n = ast.new_type_variable(t->name);
tvar_map.emplace(t, n);
return n;
}
return i->second;
}
type_attrvar* inst_attr(type_attrvar* const t) {
type_attrvar* const a = ast.new_type_attrvar(inst_var(t->var), inst_struct(t->goal));
if (t->next != nullptr) {
a->next = inst_attr(t->next);
}
return a;
}
public:
type_clause* inst_rule(
type_struct *const h,
set<type_variable*> const& c,
vector<type_struct*> const& i,
int d
) {
tvar_map.clear();
type_struct *const head = inst_struct(h);
set<type_variable*> cyck;
for (type_variable *const v : c) {
tvar_map_type::const_iterator j = tvar_map.find(v);
if (j != tvar_map.end()) {
cyck.insert(j->second);
}
}
vector<type_struct*> impl;
for (type_struct *const s : i) {
impl.push_back(inst_struct(s));
}
return ast.new_type_clause(head, move(cyck), move(impl), d);
}
virtual void visit(type_variable *const t) override {
exp = inst_var(t);
}
virtual void visit(type_attrvar *const t) override {
exp = inst_attr(t);
}
virtual void visit(type_atom *const t) override {
exp = t;
}
virtual void visit(type_struct *const t) override {
exp = inst_struct(t);
}
virtual void visit(type_clause *const t) override {
exp = inst_rule(t->head, t->cyck, t->impl, t->id);
}
explicit type_instantiate(heap& ast) : ast(ast) {}
type_expression* operator() (type_expression *const t) {
tvar_map.clear();
find(t)->accept(this);
return exp;
}
};
//----------------------------------------------------------------------------
// Cycle Check
class no_cycles : public type_visitor {
set<type_expression*> visited;
bool cycle_free;
void check_struct(type_struct *const t) {
pair<set<type_expression*>::const_iterator, bool> p = visited.insert(t);
if (p.second) { // new element
for (type_expression *const e : t->args) {
find(e)->accept(this);
}
visited.erase(p.first);
} else {
cycle_free = false;
}
}
public:
virtual void visit(type_variable *const t) override {}
virtual void visit(type_attrvar *const t) override {}
virtual void visit(type_atom *const t) override {}
virtual void visit(type_struct *const t) override {
check_struct(t);
}
virtual void visit(type_clause *const t) override {
check_struct(t->head);
}
bool operator() (type_expression *const t) {
visited.clear();
cycle_free = true;
find(t)->accept(this);
//if (cycle_free == false) {
// cout << "CYCLIC ";
//}
return cycle_free;
}
};
//----------------------------------------------------------------------------
// Rational Tree Unification
struct trail : public type_visitor {
union_stack unions;
private:
using texp_pair = pair<type_expression*, type_expression*>;
no_cycles nocyc;
vector<texp_pair> todo;
vector<type_attrvar*> deferred_goals;
type_expression *u2;
bool unifies;
inline void queue(type_expression *const t1, type_expression *const t2) {
if (t1 != t2) {
todo.emplace_back(t1, t2);
}
}
inline void struct_struct(type_struct *const t1, type_struct *const t2) {
if ((t1->functor == t2->functor) && (t1->args.size() == t2->args.size())) {
link(t1, t2, unions);
for (int i = 0; i < t1->args.size(); ++i) {
queue(t1->args[i], t2->args[i]);
}
} else {
unifies = false;
}
}
public:
class variable_unify : public type_visitor {
trail &unify;
type_variable *t1;
public:
virtual void visit(type_variable *const t2) override {
link(t1, t2, unify.unions);
}
virtual void visit(type_attrvar* const t2) override {
unify.deferred_goals.push_back(t2);
t1->replace_with(t2, unify.unions);
}
virtual void visit(type_atom *const t2) override {
t1->replace_with(t2, unify.unions);
}
virtual void visit(type_struct *const t2) override {
t1->replace_with(t2, unify.unions);
}
virtual void visit(type_clause *const t2) override {
unify.unifies = false;
}
explicit variable_unify(trail &unify) : unify(unify) {}
void operator() (type_variable *const v1) {
t1 = v1;
unify.u2->accept(this);
}
} variable;
virtual void visit(type_variable *const u1) override {
variable(u1);
}
class attrvar_unify : public type_visitor {
trail &unify;
type_attrvar *t1;
public:
virtual void visit(type_variable *const t2) override {
unify.deferred_goals.push_back(t1);
t2->replace_with(t1, unify.unions);
}
virtual void visit(type_attrvar* t2) override {
//unify.deferred_goals.push_back(t1);
//unify.deferred_goals.push_back(t2);
link2(t1, t2, unify.unions);
type_attrvar* i = t2;
while (i->next != nullptr) {
i = i->next;
}
i->next = t1;
}
virtual void visit(type_atom *const t2) override {
unify.deferred_goals.push_back(t1);
t1->replace_with(t2, unify.unions);
}
virtual void visit(type_struct *const t2) override {
unify.deferred_goals.push_back(t1);
t1->replace_with(t2, unify.unions);
}
virtual void visit(type_clause *const t2) override {
unify.unifies = false;
}
explicit attrvar_unify(trail &unify) : unify(unify) {}
void operator() (type_attrvar *const v1) {
t1 = v1;
unify.u2->accept(this);
}
} attrvar;
virtual void visit(type_attrvar *const u1) override {
attrvar(u1);
}
class atom_unify : public type_visitor {
trail &unify;
type_atom *t1;
public:
virtual void visit(type_variable *const t2) override {
t2->replace_with(t1, unify.unions);
}
virtual void visit(type_attrvar *const t2) override {
unify.deferred_goals.push_back(t2);
t2->replace_with(t1, unify.unions);
}
virtual void visit(type_atom *const t2) override {
if (t1->value != t2->value) {
unify.unifies = false;
}
}
virtual void visit(type_struct *const t2) override {
if (t2->args.size() > 0 || t1->value != t2->functor->value) {
unify.unifies = false;
}
}
virtual void visit(type_clause *const t2) override {
unify.unifies = false;
}
explicit atom_unify(trail &unify) : unify(unify) {}
void operator() (type_atom *const v1) {