-
Notifications
You must be signed in to change notification settings - Fork 0
/
loopyprolog.h
1396 lines (1227 loc) · 46.4 KB
/
loopyprolog.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
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
#pragma once
#include "stdafx.h"
#define OWN_MEMORY_MANAGEMENT
using std::ostream;
using std::cout;
using std::endl;
using std::string;
using boost::any_cast;
using boost::intrusive_ref_counter;
using boost::intrusive_ptr;
using std::function;
class LogicalVariant;
class LCons;
class LVar;
enum UninstanciatedType { UNINSTANCIATED };
enum NilType { NIL };
#define CapturedLambda(...) CapturedVar<std::function<Trampoline (__VA_ARGS__) >>
#define UncountedLambda(...) UncountedVar<std::function<Trampoline (__VA_ARGS__) >>
inline ostream & operator<<(ostream & os, const NilType &)
{
os << "Nil";
return os;
}
inline ostream & operator<<(ostream & os, const UninstanciatedType &)
{
os << "Uninstanciated";
return os;
}
//Much to my surprise Search in C++ is only 1/3 more lines than the lua version.
class Search;
//Note: the search parameter only seems necessary in the final continuation that
//reports when the search has failed, the rest of the time it could be captured... but
//it's simpler to pass than capture especially since other values are captured by value
//and this one would have to be captured by reference.
//The other times it has to be passed are in direct calls not continuations
typedef std::function<void(Search &)> Continuation;
//typedef std::initializer_list<boost::any> Params;
//typedef std::function<void(Search&, Params)> TailWithParams;
template<typename T>
class CapturedVar;
#ifdef OWN_MEMORY_MANAGEMENT
struct FreeList
{
FreeList * next;
};
const int FREE_LIST_BLOCK_SIZE = 32768;
template<typename T> void *allocate_from_freelist()
{
if (T::free_list == nullptr) {
void *block = malloc(FREE_LIST_BLOCK_SIZE);
intptr_t align;
if (T::blocksize > 16) align = 16;
else align = T::blocksize;
intptr_t offset = ((reinterpret_cast<intptr_t>(block) + align - 1)& ~(align - 1)) - reinterpret_cast<intptr_t>(block);
FreeList* p = nullptr;
for (intptr_t i = offset;i <= FREE_LIST_BLOCK_SIZE - T::blocksize; i += T::blocksize) {
FreeList* const f = reinterpret_cast<FreeList *>(i + reinterpret_cast<intptr_t>(block));
f->next = p;
p = f;
}
T::free_list = p;
}
FreeList* r = T::free_list;
T::free_list = r->next;
return static_cast<void *>(r);
}
template <typename T>
void free_to_freelist(void *v)
{
if (v == nullptr) return;
FreeList* r = static_cast<FreeList*>(v);
r->next = T::free_list;
T::free_list = r;
}
#endif
#define DECLARE_MEM_MANAGEMENT(name) \
static intptr_t blocksize; \
static FreeList *free_list; \
void * operator new (size_t size) \
{ \
assert(size == sizeof(name)); \
return allocate_from_freelist<name>(); \
} \
void * operator new (size_t, void *place) \
{ \
return place; \
} \
void operator delete (void *, void *) {} \
\
void operator delete (void * mem) \
{ \
free_to_freelist<name>(mem); \
}
#define DEFINE_MEM_MANAGEMENT(name) \
intptr_t name::blocksize = intptr_t(((sizeof(name) + 15)>>4)<<4); \
FreeList *name::free_list = nullptr;
#define DEFINE_MEM_MANAGEMENT_T(name,...) \
template <__VA_ARGS__>\
intptr_t name::blocksize = intptr_t(((sizeof(name) + 15)>>4)<<4); \
template <__VA_ARGS__>\
FreeList *name::free_list = nullptr;
//very simple not thread safe class compatible with intrusive_ptr
//the point of this class is that you can define both counted and eternal/singleton objects
//and the built in class doesn't have that
class SimpleRefCount
{
protected:
static const int SINGLETON = -1000000;
public:
int _ref;
int use_count() const { return _ref; }
void _inc() { if (_ref != SINGLETON) ++_ref; }
SimpleRefCount * _dec() {
if (_ref != SINGLETON) if (0 == --_ref) return this;
return nullptr;
}
SimpleRefCount() :_ref(0) {}
SimpleRefCount(int initial) :_ref(initial) {}
virtual ~SimpleRefCount()
{}
};
void intrusive_ptr_add_ref(SimpleRefCount *p);
void intrusive_ptr_release(SimpleRefCount *p);
class Trampoline;
class TrampolineLetter :public SimpleRefCount
{
public:
TrampolineLetter() {}
TrampolineLetter(int i) :SimpleRefCount(i) {}
virtual ~TrampolineLetter() {}
virtual Trampoline execute() = 0;
virtual bool isNull() const { return false; }
virtual void _for_retargetting(void *n) { }
};
class CombinableRefCount
{
public:
int _ref;
CombinableRefCount *_next;//in union head , otherwise next member of union
CombinableRefCount *_forward;//in union end of list, otherwise not used
int use_count() const { if (_forward) return _forward->_ref; else return _ref; }
void _inc() { if (_forward) ++_forward->_ref; else ++_ref; }
CombinableRefCount * _dec() {
if (_forward) {
if (0 == --_forward->_ref) return _forward;
}
else {
if (0 == --_ref) return this;
}
return nullptr;
}
CombinableRefCount() :_ref(0), _next(nullptr), _forward(nullptr) {}
virtual ~CombinableRefCount()
{
if (_next) delete _next;
}
template<typename T>
void add_ref(T &one_more)
{
if (_forward == nullptr) {
_forward = new CombinableRefCount;
_forward->_ref = _ref;
_forward->_next = this;
}
CombinableRefCount *o = one_more.get();
_forward->_ref += o->_ref;
o->_forward = _forward;
o->_next = _forward->_next;
_forward->_next = o;
}
#ifdef OWN_MEMORY_MANAGEMENT
static intptr_t blocksize;
static FreeList *free_list;
void * operator new (size_t size)
{
assert(size == sizeof(CombinableRefCount));
return allocate_from_freelist<CombinableRefCount>();
}
void * operator new (size_t, void *place)
{
return place;
}
void operator delete (void *, void *) {}
void operator delete (void * mem)
{
free_to_freelist<CombinableRefCount>(mem);
}
#endif
};
template <typename T>
class CapturedVarLetter;
template<typename T>
class CapturedVar;
class Trampoline : public intrusive_ptr<TrampolineLetter>
{
public:
Trampoline(const CapturedVar<std::function<Trampoline() >> &);
Trampoline(TrampolineLetter * v) :intrusive_ptr(v) {}
Trampoline() :intrusive_ptr() {}
Trampoline(const Trampoline &o) :intrusive_ptr(o.get()) {}
};
extern Trampoline end_search;
template <typename T>
class Trampoline0 : public TrampolineLetter
{
T fn;
public:
Trampoline0(const T &f) :fn(f) { }
virtual Trampoline execute() { return fn(); }
};
class NullTrampoline : public TrampolineLetter
{
public:
NullTrampoline() :TrampolineLetter(SimpleRefCount::SINGLETON) { }
virtual bool isNull() const { return true; }
virtual Trampoline execute() { return this; }
};
template <typename T, typename P1>
class Trampoline1 : public TrampolineLetter
{
T fn;
P1 p1;
public:
Trampoline1(const T &f, const P1 &_p1) :fn(f), p1(_p1) {}
virtual Trampoline execute() { return fn(p1); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2>
class Trampoline2 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
public:
Trampoline2(const T &f, const P1 &_p1, const P2 &_p2) :fn(f), p1(_p1), p2(_p2) {}
virtual Trampoline execute() { return fn(p1, p2); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2, typename P3>
class Trampoline3 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
P3 p3;
public:
Trampoline3(const T &f, const P1 &_p1, const P2 &_p2, const P3 &_p3) :fn(f), p1(_p1), p2(_p2), p3(_p3) {}
virtual Trampoline execute() { return fn(p1, p2, p3); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2, typename P3, typename P4>
class Trampoline4 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
P3 p3;
P4 p4;
public:
Trampoline4(const T &f, const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4) :fn(f), p1(_p1), p2(_p2), p3(_p3), p4(_p4) {}
virtual Trampoline execute() { return fn(p1, p2, p3, p4); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5>
class Trampoline5 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
P3 p3;
P4 p4;
P5 p5;
public:
Trampoline5(const T &f, const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5) :fn(f), p1(_p1), p2(_p2), p3(_p3), p4(_p4), p5(_p5) {}
virtual Trampoline execute() { return fn(p1, p2, p3, p4, p5); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
class Trampoline6 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
P3 p3;
P4 p4;
P5 p5;
P6 p6;
public:
Trampoline6(const T &f, const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6) :fn(f), p1(_p1), p2(_p2), p3(_p3), p4(_p4), p5(_p5), p6(_p6) {}
virtual Trampoline execute() { return fn(p1, p2, p3, p4, p5, p6); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
class Trampoline7 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
P3 p3;
P4 p4;
P5 p5;
P6 p6;
P7 p7;
public:
Trampoline7(const T &f, const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7) :fn(f), p1(_p1), p2(_p2), p3(_p3), p4(_p4), p5(_p5), p6(_p6), p7(_p7) {}
virtual Trampoline execute() { return fn(p1, p2, p3, p4, p5, p6, p7); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
class Trampoline8 : public TrampolineLetter
{
T fn;
P1 p1;
P2 p2;
P3 p3;
P4 p4;
P5 p5;
P6 p6;
P7 p7;
P8 p8;
public:
Trampoline8(const T &f, const P1 &_p1, const P2 &_p2, const P3 &_p3, const P4 &_p4, const P5 &_p5, const P6 &_p6, const P7 &_p7, const P8 &_p8) :fn(f), p1(_p1), p2(_p2), p3(_p3), p4(_p4), p5(_p5), p6(_p6), p7(_p7), p8(_p8) {}
virtual Trampoline execute() { return fn(p1, p2, p3, p4, p5, p6, p7, p8); }
virtual void _for_retargetting(void *n) { *static_cast<T **>(n) = &fn; }
};
/* CombinableRefCount is a replacement for intrusive_ref_counter<_, boost::thread_unsafe_counter >
* With the difference that you can combine a bunches of them to share a reference counter.
* The point of that is to handle the case where a bunch of objects create a cycle of references.
* Note it is assumed that the organization of this cycle is immutable.
* Then by combining the counts, a reference to one object is considered a reference to all for the
* sake of refernce counting. Only when there are no external links to all of the objects will they
* be collected.
* Note it is assumed that the cycle of references either is special cased to not cause the counter
* to increment, or that all of those increments have been manually decremented out.
* The class is hard to understand because CombinableRefCount is used in two separate ways by the
* algorithm and it's just punning that the same code works for both.
* The main way is that CombinableRefCount is subclassed. These subclasses can be used just like
* subclasses of boost::intrusive_ref_counter.
* However, if combine_refs is called on a list of CombinableRefCount* (or on a list of CapturedVar<T> holding
* CapturedVarLetter<T> derived from CombinableRefCount) then a single CombinableRefCount is allocated
* to hold the combined reference count for all those objects. Note that this CombinableRefCount is
* just the raw type, not a subclass.
* For the first kind, the subclassed version, _forward points at the shared count if there is one
* and _next makes a list to facilitate deleting the whole set at once.
* For the second kind, the shared count, _next points to the head of the list of shared objects
* and _forward isn't used.
*/
void intrusive_ptr_add_ref(CombinableRefCount *p);
void intrusive_ptr_release(CombinableRefCount *p);
inline CombinableRefCount * combine_refs()
{
return new CombinableRefCount;
}
template<typename ... Types>
CombinableRefCount * combine_refs(CombinableRefCount *first, Types ... rest)
{
CombinableRefCount *u = combine_refs(rest...);
first->_forward = u;
u->_ref += first->_ref;
first->_next = u->_next;
u->_next = first;
return u;
}
template <typename T>
class CapturedVarLetter :public CombinableRefCount
{
public:
typedef T type;
T value;
CapturedVarLetter(const T& a) :value(a) {}
CapturedVarLetter() {}
T& operator *() { return value; }
T* operator ->() { return &value; }
#ifdef OWN_MEMORY_MANAGEMENT
static intptr_t blocksize;
static FreeList *free_list;
void * operator new (size_t size)
{
assert(size == sizeof(CapturedVarLetter<T>));
return allocate_from_freelist<CapturedVarLetter<T>>();
}
void * operator new (size_t, void *place)
{
return place;
}
void operator delete (void *, void *) {}
void operator delete (void * mem)
{
free_to_freelist<CapturedVarLetter<T>>(mem);
}
#endif
};
#ifdef OWN_MEMORY_MANAGEMENT
template <typename T>
intptr_t CapturedVarLetter<T>::blocksize = intptr_t((sizeof(CapturedVarLetter<T>) + sizeof(CapturedVarLetter<T>) - 1)&~((sizeof(CapturedVarLetter<T>) + sizeof(CapturedVarLetter<T>) - 1) >> 1));
template <typename T>
FreeList *CapturedVarLetter<T>::free_list = nullptr;
#endif
/* CapturedVar has two uses
* it can be used inside of lambdas to give the variable capture semantics of other language ie:
* 1) variables are captured by reference but
* 2) the lifetime of captured variables is controlled by garbage collection - even if the original variable goes out of scope
* the variable exists as long as there is a lambda that references it.
* 3) note that this garbage collection is not thread safe - I decided that speed is more important, do not share lambdas that hold
* CapturedVar across threads
*
* The other use (for CapturedCont) is to speed up copying std::function objects that would otherwise require copying a heap object
* on each copy. Incrementing and decrementing the refernce counter is faster than calling new and delete.
*
* A weirdness with CapturedVar<T> where T is a std function type such as CapturedCont is that while lambdas can be stored in
* std::function<> types you can't match the type of a lambda to automatically convert to std::function as a result I couldn't give
* CapturedCont a shortcut assignment such as "foo=[=](){...};", instead you have to use * to expose the std::function inside and assign
* to that. Ie it's "CapturedCont foo; *foo=[=](){...};" The difference is the "*"
* Avoid assigning to CapturedVar<T> without the *.
*
* Note, there's a bug/complication with the use of reference counters here.
* Lambdas that are held in CapturedConts that can form cycles of ownership will never be collected.
* In normal programs that would rarely come up, but I'm afraid that in logic style programming it will come up quite often, so
* there's a memory leak unless a somewhat complicated fix is used.
* The fix is:
* Any CapturedCont that's part of a cycle (even a self reference) needs a shadow ptr variable thus:
* UncountedCont foo_uncounted = foo;
* and inside the lambdas use foo_uncounted everywhere you would have used foo. foo_uncounted can convert to CapturedCont as needed, say to pass
* as a parameter.
*
* For all cycles of more than one CapturedCont you have to call combine_refs on the set like so:
* CapturedCont foo,bar,baz;
* UncountedCont foo_uncounted = foo,bar_uncounted=bar,baz_uncounted=baz;
* combine_refs(foo,bar,baz);
* and inside of the lambdas that foo,bar and baz are set to use foo_uncounted, bar_uncounted and baz_uncounted instead of foo,bar&baz
* Having done that incantation, the problem of circular references of CapturedConts is solved.
*
* What combine_refs does combine the reference count of the objects it refers to so that for the sake of garbage collection the
* whole set is managed as a single object. A reference to one is a reference to all.
*/
template <typename T>
class UncountedVar;
class TrampolineLetter;
enum CombineRefType { CombineRef };
template<typename T>
class CapturedVar : public intrusive_ptr< CapturedVarLetter<T> >
{
public:
CapturedVar(const CapturedVarLetter<T>& v) :intrusive_ptr(&const_cast<CapturedVarLetter<T>&>(v))
{
}
CapturedVar(const T& v) :intrusive_ptr(new CapturedVarLetter<T>(v)) {}
template<typename U>
CapturedVar(CombineRefType, const CapturedVar<U> &c) : intrusive_ptr(new CapturedVarLetter<T>()) { c.get()->add_ref(*this); }
template<typename U>
CapturedVar(CombineRefType, const UncountedVar<U> &c) : intrusive_ptr(new CapturedVarLetter<T>()) { c.get()->add_ref(*this); }
CapturedVar() :intrusive_ptr(new CapturedVarLetter<T>()) {}
CapturedVar(const CapturedVar<T> &o) :intrusive_ptr(static_cast<const intrusive_ptr< CapturedVarLetter<T> > &>(o)) {}
CapturedVar(const UncountedVar<T> &o);
void clear()
{
*static_cast<intrusive_ptr< CapturedVarLetter<T> > *>(this) = nullptr;
}
auto operator()() { return (this->operator *())(); }
template <typename P1>
auto operator()(P1 &&p1) { return (this->operator *())(p1); }
template <typename P1, typename P2>
auto operator()(P1 &&p1, P2 &&p2) { return (this->operator *())(p1, p2); }
template <typename P1, typename P2, typename P3>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3) { return (this->operator *())(p1, p2, p3); }
template <typename P1, typename P2, typename P3, typename P4>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4) { return (this->operator *())(p1, p2, p3, p4); }
template <typename P1, typename P2, typename P3, typename P4, typename P5>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5) { return (this->operator *())(p1, p2, p3, p4, p5); }
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6) { return (this->operator *())(p1, p2, p3, p4, p5, p6); }
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7) { return (this->operator *())(p1, p2, p3, p4, p5, p6, p7); }
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8) { return (this->operator *())(p1, p2, p3, p4, p5, p6, p7, p8); }
T * operator->() { return &get()->value; }
T * operator->() const { return &get()->value; }
T& operator *() { return get()->value; }
T& operator *() const { return get()->value; }
};
//typedef CapturedVarLetter<Continuation> *UncountedCont;
template <typename T>
class UncountedVar
{
public:
CapturedVarLetter<T> * value;
UncountedVar(const CapturedVar<T> &c) :value(c.get()) {}
template<typename U>
UncountedVar(CombineRefType, const CapturedVar<U> &c) : value(new CapturedVarLetter<T>()) { c.get()->add_ref(*this); }
template<typename U>
UncountedVar(CombineRefType, const UncountedVar<U> &c) : value(new CapturedVarLetter<T>()) { c.get()->add_ref(*this); }
CapturedVarLetter<T> * get() const {
return const_cast<CapturedVarLetter<T> *>(value);
}
T * operator->() { return &get()->value; }
T * operator->() const { return &get()->value; }
T& operator *() { return value->value; }
T& operator *() const { return const_cast<T&>(value->value); }
auto operator()() { return (this->operator *())(); }
template <typename P1>
auto operator()(P1 &&p1) { return (this->operator *())(p1); }
template <typename P1, typename P2>
auto operator()(P1 &&p1, P2 &&p2) { return (this->operator *())(p1, p2); }
template <typename P1, typename P2, typename P3>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3) { return (this->operator *())(p1, p2, p3); }
template <typename P1, typename P2, typename P3, typename P4>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4) { return (this->operator *())(p1, p2, p3, p4); }
template <typename P1, typename P2, typename P3, typename P4, typename P5>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5) { return (this->operator *())(p1, p2, p3, p4, p5); }
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6) { return (this->operator *())(p1, p2, p3, p4, p5, p6); }
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7) { return (this->operator *())(p1, p2, p3, p4, p5, p6, p7); }
template <typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
auto operator()(P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8) { return (this->operator *())(p1, p2, p3, p4, p5, p6, p7, p8); }
};
template<typename T>
CapturedVar<T>::CapturedVar(const UncountedVar<T> &o) :intrusive_ptr(o.value) {}
template<typename T, typename ... Types>
CombinableRefCount * combine_refs(const CapturedVar<T> &_first, Types ... rest)
{
CombinableRefCount *first = _first.get();
CombinableRefCount *u = combine_refs(rest...);
first->_forward = u;
u->_ref += first->_ref;
first->_next = u->_next;
u->_next = first;
return u;
}
template <typename T>
auto make_counted(T && o) {
return o;
}
template <typename T>
CapturedVar<T> make_counted(const UncountedVar<T> &o) {
return *o.get();
}
template <typename T>
CapturedVar<T> make_counted(UncountedVar<T> &o) {
return *o.get();
}
inline std::reference_wrapper<Search> make_counted(Search &o) {
return std::ref(o);
}
template <typename T>
inline TrampolineLetter* new_trampoline(T &&f)
{
return new Trampoline0<T>(f);
}
template <typename T, typename P1>
inline TrampolineLetter* new_trampoline(T &&f, P1 &&p1)
{
return new Trampoline1<T, P1>(f, p1);
}
template <typename T, typename P1, typename P2>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2)
{
return new Trampoline2<T, P1, P2>(f, p1, p2);
}
template <typename T, typename P1, typename P2, typename P3>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3)
{
return new Trampoline3<T, P1, P2, P3>(f, p1, p2, p3);
}
template <typename T, typename P1, typename P2, typename P3, typename P4>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4)
{
return new Trampoline4<T, P1, P2, P3, P4>(f, p1, p2, p3, p4);
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5)
{
return new Trampoline5<T, P1, P2, P3, P4, P5>(f, p1, p2, p3, p4, p5);
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6)
{
return new Trampoline6<T, P1, P2, P3, P4, P5, P6>(f, p1, p2, p3, p4, p5, p6);
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7)
{
return new Trampoline7<T, P1, P2, P3, P4, P5, P6, P7>(f, p1, p2, p3, p4, p5, p6, p7);
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
inline TrampolineLetter * new_trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8)
{
return new Trampoline8<T, P1, P2, P3, P4, P5, P6, P7, P8>(f, p1, p2, p3, p4, p5, p6, p7, p8);
}
template <typename T>
Trampoline trampoline(T &&f)
{
return new_trampoline(make_counted(f));
}
template <typename T, typename P1>
Trampoline trampoline(T &&f, P1 &&p1)
{
return new_trampoline(make_counted(f), make_counted(p1));
}
template <typename T, typename P1, typename P2>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2));
}
template <typename T, typename P1, typename P2, typename P3>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2), make_counted(p3));
}
template <typename T, typename P1, typename P2, typename P3, typename P4>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2), make_counted(p3), make_counted(p4));
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2), make_counted(p3), make_counted(p4), make_counted(p5));
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2), make_counted(p3), make_counted(p4), make_counted(p5), make_counted(p6));
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2), make_counted(p3), make_counted(p4), make_counted(p5), make_counted(p6), make_counted(p7));
}
template <typename T, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
Trampoline trampoline(T &&f, P1 &&p1, P2 &&p2, P3 &&p3, P4 &&p4, P5 &&p5, P6 &&p6, P7 &&p7, P8 &&p8)
{
return new_trampoline(make_counted(f), make_counted(p1), make_counted(p2), make_counted(p3), make_counted(p4), make_counted(p5), make_counted(p6), make_counted(p7), make_counted(p8));
}
typedef CapturedLambda(Search &) CapturedCont;
typedef UncountedLambda(Search &) UncountedCont;
typedef CapturedLambda() Subclause;
typedef UncountedLambda() UncountedSubclause;
//for testing with .which()
enum LVType {
LV_NIL, LV_UNINSTANCIATED, LV_DOUBLE, LV_STRING, LV_LVAR, LV_LIST, LV_CUSTOM, LV_DATA1, LV_DATA2, LV_DATA3, LV_DATA4
};
extern char * const TypeNames[];
//to allow custom logical types
class LogicalData :public intrusive_ref_counter<LogicalData, boost::thread_unsafe_counter >
{
public:
LVType class_type;
void *data;
LogicalData(LVType t, void *d) :class_type(t), data(d) {}
};
typedef boost::flyweight<string> InternedString;
/*
Note: difference from Lua version
boost::variant treats its values as value types, it's not possible to get a reference to the stored value, only a copy
so when you instanciate an LVar it's not the LVar that can be shared, it's the LVariant inside it. You can't instanciate by
setting anLVar = aValue you have to do it as anLVar->value = aValue!
Anyway the point of LVar in the C++ version is to allow reference counting and simple initialization.
All the extra levels of indirection are just ways of dealing with C++ limitations consider an LVar to just be a variable and
the LVariant/LValue to be the logical variable inside it.
*/
typedef boost::variant <
NilType
, UninstanciatedType
, double
, InternedString
, LVar
, intrusive_ptr<LCons>
, intrusive_ptr<LogicalData>
> LValue;
//Note, in this program LVars are never to hold NULL
//rather they can hold a LogicalVariant with the type LV_NIL
//The usual initialization is to be: LVar aVariable(LInit())
//that initializes the variable to UNINSTANCIATED
class LVar : public boost::intrusive_ptr<LogicalVariant>
{
public:
LVar();
LVar(NilType);
LVar(UninstanciatedType);
LVar(const char * c);
LVar(char * c);
LVar(double d);
//copy constructor, not chaining
LVar(const LVar &v);
LVar(LogicalVariant *v);
LVar(LValue v);
LVar(InternedString s);
LVar(LCons *);
LVar(intrusive_ptr<LCons>&);
void chain(LVar&o);
LVar& get_target();
LVType target_type();
bool nullp() { return target_type() == LV_NIL; }
bool listp() { LVType t = target_type(); return t == LV_NIL || t == LV_LIST; }
bool pairp() { return target_type() == LV_LIST; }
void set_car(LVar &t);
void set_cdr(LVar &t);
// LV_NIL,LV_UNINSTANCIATED,LV_DOUBLE,LV_STRING,LV_LVAR,LV_LIST,LV_CUSTOM,LV_DATA1,LV_DATA2,LV_DATA3,LV_DATA4
LVType type() const;
bool uninstanciatedp() { return type() == LV_UNINSTANCIATED; }
bool doublep() { return type() == LV_DOUBLE; }
bool stringp() { return type() == LV_STRING; }
bool lvarp() { return type() == LV_LVAR; }
void operator = (const LVar &o);
bool ground() {
LVType t = type();
if (t == LV_LIST) return car().ground() && cdr().ground();
return t != LV_UNINSTANCIATED;
}
intrusive_ptr<LCons> as_LCons();
double as_double();
InternedString as_IString();
intrusive_ptr<LogicalData> as_LogicalValue();
LVar car();
LVar cdr();
// bool operator ==(LVar &);
#ifdef OWN_MEMORY_MANAGEMENT
static intptr_t blocksize;
static FreeList *free_list;
void * operator new (size_t size)
{
assert(size == sizeof(LVar));
return allocate_from_freelist<LVar>();
}
void * operator new (size_t, void *place)
{
return place;
}
void operator delete (void *, void *) {}
void operator delete (void * mem)
{
free_to_freelist<LVar>(mem);
}
#endif
};
class LogicalVariant :public intrusive_ref_counter<LogicalVariant, boost::thread_unsafe_counter>
{
public:
LogicalVariant() :value(UNINSTANCIATED) {}
LogicalVariant(LValue v) :value(v) {}
LogicalVariant(LogicalVariant &v) :value(v.value) {}
LogicalVariant(LVar &v) :value(v) {}
LogicalVariant(LCons *c) :value(intrusive_ptr<LCons>(c)) {}
LogicalVariant(boost::intrusive_ptr<LCons>&c) :value(c) {}
LValue value;
#ifdef OWN_MEMORY_MANAGEMENT
static intptr_t blocksize;
static FreeList *free_list;
void * operator new (size_t size)
{
assert(size == sizeof(LogicalVariant));
return allocate_from_freelist<LogicalVariant>();
}
void * operator new (size_t, void *place)
{
return place;
}
void operator delete (void *, void *) {}
void operator delete (void * mem)
{
free_to_freelist<LogicalVariant>(mem);
}
#endif
};
inline LVar::LVar() : intrusive_ptr<LogicalVariant>(new LogicalVariant(UNINSTANCIATED)) { }
inline LVar::LVar(NilType) : intrusive_ptr<LogicalVariant>(new LogicalVariant(NIL)) { }
inline LVar::LVar(LValue v) : intrusive_ptr<LogicalVariant>(new LogicalVariant(v)) { }
inline LVar::LVar(UninstanciatedType) : intrusive_ptr<LogicalVariant>(new LogicalVariant(UNINSTANCIATED)) { }
inline LVar::LVar(const char * c) : intrusive_ptr<LogicalVariant>(new LogicalVariant(InternedString(c))) {}
inline LVar::LVar(char * c) : intrusive_ptr<LogicalVariant>(new LogicalVariant(InternedString(c))) {}
inline LVar::LVar(double d) : intrusive_ptr<LogicalVariant>(new LogicalVariant(d)) {}
//Note it's not safe to make this chain because it's used as a copy constructor
inline LVar::LVar(const LVar &v) : intrusive_ptr<LogicalVariant>(v) {}
inline LVar::LVar(LogicalVariant *v) : intrusive_ptr<LogicalVariant>(v) {}
inline LVar::LVar(InternedString s) : intrusive_ptr<LogicalVariant>(new LogicalVariant(s)) {}
inline void LVar::chain(LVar &o) { (*this)->value = o; }
inline LVType LVar::target_type() { return get_target().type(); }
inline void LVar::operator = (const LVar &o) { (*this)->value = o->value; }
struct GetAddress : public boost::static_visitor<>
{
void *address;
template <typename T>
void operator()(T &t) const { address = &t; }
};
;
inline LogicalVariant * LInit()
{
return new LogicalVariant();
}
//ostream & operator<<(ostream & os, const LogicalVariant &v);
ostream & operator<<(ostream & os, const LVar &v);
//typedef CapturedVar<LVar> CLVar;
//typedef UncountedVar<LVar> ULVar;
struct DotHolder
{
LVar cdr;
};
class LCons :public intrusive_ref_counter<LCons, boost::thread_unsafe_counter>
{
public:
static const char *open_paren;
static const char *close_paren;
static const char *display_dot;
static const char *display_nil;
ostream & _out_rest(ostream & os)
{
/*
if (nullp(logical_get(self[2]))) then return ' ' .. tostring(logical_get(self[1])) .. close_paren
elseif (listp(logical_get(self[2]))) then return ' ' .. tostring(logical_get(self[1])) .. logical_get(self[2]):rest_tostring()
else return ' ' .. tostring(logical_get(self[1])) .. display_dot .. tostring(self[2]) ..close_paren
end
*/
if (cdr.nullp()) os << " " << car.get_target() << close_paren;
else if (cdr.listp()) {
os << " "
<< car.get_target();
return cdr.as_LCons()->_out_rest(os);
}
else os << " " << car.get_target() << display_dot << cdr.get_target() << close_paren;
return os;
}
//allocating a new LogicalVariant for NIL allows the cons to be mutable
//Maybe we'd prefer immutable
LCons(LValue first) :car(new LogicalVariant(first)), cdr(new LogicalVariant(NIL)) {}
LCons(LValue first, LValue rest) :car(new LogicalVariant(first)), cdr(new LogicalVariant(rest)) {}
//LCons(LVar& first, DotHolder rest) :car(first), cdr(rest.cdr) {}
LCons(LVar && first, DotHolder && rest) :car(first), cdr(rest.cdr.car()) {}
LCons(LVar& first) :car(first), cdr(new LogicalVariant(NIL)) {}
LCons(LVar& first, LVar& rest) :car(first), cdr(rest) {}
LCons(LValue first, LVar& rest) :car(new LogicalVariant(first)), cdr(rest) {}
LCons(LVar& first, LValue rest) :car(first), cdr(new LogicalVariant(rest)) {}
//Note there is an extra level of indirection here so that there doesn't have to be
//some complicated plumbing for the garbage collection. And remember LVars should never be NULL
LVar car;
LVar cdr;
#ifdef OWN_MEMORY_MANAGEMENT
static intptr_t blocksize;
static FreeList *free_list;
void * operator new (size_t size)
{
assert(size == sizeof(LCons));
return allocate_from_freelist<LCons>();
}
void * operator new (size_t, void *place)
{
return place;
}
void operator delete (void *, void *) {}
void operator delete (void * mem)
{
free_to_freelist<LCons>(mem);
}
#endif
};
inline LVar::LVar(LCons *c) :intrusive_ptr<LogicalVariant>(new LogicalVariant(c)) {}
inline LVar::LVar(intrusive_ptr<LCons> &c) : intrusive_ptr<LogicalVariant>(new LogicalVariant(c)) {}
ostream & operator<<(ostream & os, const LVar &v);
enum DotType { DOT };
extern LogicalVariant NilVariant;
template<typename ... TYPES>
LVar L()
{
return NIL;
}
template<typename T, typename ... TYPES>
LVar L(T a, TYPES ... rest)
{
return LVar(intrusive_ptr<LCons>(new LCons(LVar(a), L(rest ...))));
}
template<typename ... TYPES>
DotHolder L(DotType, TYPES ... rest)
{
return DotHolder{ L(rest ...) };
}