8000 simply to raw ptrs to vars for simplicity for now · Z80coder/datalog-cpp@c2b8469 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2b8469

Browse files
author
wright
committed
simply to raw ptrs to vars for simplicity for now
1 parent c26c7ec commit c2b8469

File tree

2 files changed

+66
-89
lines changed

2 files changed

+66
-89
lines changed

src/Datalog.h

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <functional>
1010
#include <cassert>
1111
#include <iostream>
12-
#include <memory>
1312

1413
namespace datalog
1514
{
@@ -41,29 +40,14 @@ struct Variable : optional<T>
4140
}
4241
};
4342

44-
template<typename T>
45-
T val(unique_ptr<Variable<T>>& var) {
46-
return var->value();
47-
}
48-
49-
template<typename T>
50-
unique_ptr<Variable<T>> var() {
51-
return make_unique<Variable<T>>();
52-
}
43+
template <typename T>
44+
static void unbind(const T& t) {}
5345

5446
template <typename T>
5547
static void unbind(Variable<T>* t) {
5648
t->unbind();
5749
}
5850

59-
template <typename T>
60-
static void unbind(unique_ptr<Variable<T>>& t) {
61-
unbind(t.get());
62-
}
63-
64-
template <typename T>
65-
static void unbind(const T& t) {}
66-
6751
template <typename... Ts>
6852
static void unbind(const tuple<Ts...> &tuple)
6953
{
@@ -84,11 +68,6 @@ static bool bind(const T& a, Variable<T>* b) {
8468
return true;
8569
}
8670

87-
template <typename T>
88-
static bool bind(const T& a, unique_ptr<Variable<T>>& b) {
89-
return bind(a, b.get());
90-
}
91-
9271
template <typename GROUND_TYPE, typename ... Ts, size_t... Is>
9372
static bool bind(const GROUND_TYPE &fact, tuple<Ts...> &atom, index_sequence<Is...>)
9473
{
@@ -108,12 +87,6 @@ static void ground(const Variable<T>* s, T &v)
10887
v = s->value();
10988
}
11089

111-
template <typename T>
112-
static void ground(const unique_ptr<Variable<T>>& s, T &v)
113-
{
114-
ground(s.get(), v);
115-
}
116-
11790
template <typename T>
11891
static void ground(const T &s, T &v)
11992
{
@@ -137,7 +110,6 @@ static typename RELATION_TYPE::Ground ground(const tuple<Ts...> &atom)
137110
template<typename RELATION_TYPE, typename ... Ts>
138111
struct AtomTypeSpecifier {
139112
typedef RELATION_TYPE RelationType;
140-
// TODO: why not references?
141113
typedef tuple<Ts...> AtomType;
142114
AtomType atom;
143115
};
@@ -182,8 +154,6 @@ struct Rule
182154

183155
template<typename ... EXTERNAL_TYPEs>
184156
struct Externals {
185-
// N.B. We need to store the ExternalFunctions
186-
//typedef tuple<const EXTERNAL_TYPEs&...> ExternalsTupleType;
187157
typedef tuple<const EXTERNAL_TYPEs...> ExternalsTupleType;
188158
ExternalsTupleType externals;
189159
};
@@ -193,8 +163,6 @@ struct BodyAtoms {
193163
tuple<typename BODY_ATOM_SPECIFIERs::AtomType...> body;
194164
};
195165

196-
// Note that RuleInstances store atoms (and therefore atoms specified during construction are copied).
197-
// This is OK since all copies of atoms still refer to the same shared variables
198166
template <typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs>
199167
struct RuleInstance {
200168
typedef Rule<typename HEAD_ATOM_SPECIFIER::RelationType, typename BODY_ATOM_SPECIFIERs::RelationType...> RuleType;
@@ -204,12 +172,6 @@ struct RuleInstance {
204172
BodyType body;
205173
};
206174

207-
#if 0
208-
template <typename EXTERNALS_TYPE, typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs>
209-
struct ExternalRuleInstance : RuleInstance<HEAD_ATOM_SPECIFIER, BODY_ATOM_SPECIFIERs...> {
210-
const EXTERNALS_TYPE& externals;
211-
};
212-
#else
213175
template <typename EXTERNALS_TYPE, typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs>
214176
struct ExternalRuleInstance {
215177
typedef Rule<typename HEAD_ATOM_SPECIFIER::RelationType, typename BODY_ATOM_SPECIFIERs::RelationType...> RuleType;
@@ -219,7 +181,6 @@ struct ExternalRuleInstance {
219181
BodyType body;
220182
const EXTERNALS_TYPE& externals;
221183
};
222-
#endif
223184

224185
template <typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs>
225186
static RuleInstance<HEAD_ATOM_SPECIFIER, BODY_ATOM_SPECIFIERs...> rule(
@@ -246,35 +207,23 @@ static RuleInstance<HEAD_ATOM_SPECIFIER, BODY_ATOM_SPECIFIERs...> rule(
246207

247208
template<typename T>
248209
struct ExternalFunction {
249-
Variable<T>& bindVariable;
210+
Variable<T>* bindVariable;
250211
typedef function<T()> ExternalFunctionType;
251212
ExternalFunctionType externalFunction;
252213
};
253214

254215
template<typename T>
255216
static ExternalFunction<T> lambda(
256-
unique_ptr<Variable<T>>& bindVariable,
217+
Variable<T>* bindVariable,
257218
typename ExternalFunction<T>::ExternalFunctionType externalFunction) {
258-
return ExternalFunction<T> {*bindVariable, externalFunction};
219+
return ExternalFunction<T> {bindVariable, externalFunction};
259220
}
260221

261-
#if 0
262222
template<typename ... BODY_ATOM_SPECIFIERs>
263223
static BodyAtoms<BODY_ATOM_SPECIFIERs...> body(BODY_ATOM_SPECIFIERs&&... bodyAtoms) {
264224
return BodyAtoms<BODY_ATOM_SPECIFIERs...>{{bodyAtoms.atom...}};
265225
}
266226

267-
template<typename ... BODY_ATOM_SPECIFIERs>
268-
static BodyAtoms<BODY_ATOM_SPECIFIERs...> body(BODY_ATOM_SPECIFIERs&... bodyAtoms) {
269-
return BodyAtoms<BODY_ATOM_SPECIFIERs...>{{bodyAtoms.atom...}};
270-
}
271-
#else
272-
template<typename ... BODY_ATOM_SPECIFIERs>
273-
static BodyAtoms<BODY_ATOM_SPECIFIERs...> body(BODY_ATOM_SPECIFIERs... bodyAtoms) {
274-
return BodyAtoms<BODY_ATOM_SPECIFIERs...>{{bodyAtoms.atom...}};
275-
}
276-
#endif
277-
278227
template <typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs, typename... EXTERNAL_TYPEs>
279228
static ExternalRuleInstance<Externals<EXTERNAL_TYPEs...>, HEAD_ATOM_SPECIFIER, BODY_ATOM_SPECIFIERs...> rule(
280229
const HEAD_ATOM_SPECIFIER& h,
@@ -664,7 +613,7 @@ static bool bindExternal(const ExternalRuleInstance<Externals<Ts...>, HEAD_ATOM_
664613
auto value = external.externalFunction();
665614
//cout << "external function returned " << value << endl;
666615
auto& bindVariable = external.bindVariable;
667-
return datalog::bind(value, &bindVariable);
616+
return datalog::bind(value, bindVariable);
668617
}
669618

670619
template <typename... Ts, typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs, size_t ... Is>
@@ -684,7 +633,7 @@ template<size_t I, typename... Ts, typename HEAD_ATOM_SPECIFIER, typename... BOD
684633
static void unbindExternal(const ExternalRuleInstance<Externals<Ts...>, HEAD_ATOM_SPECIFIER, BODY_ATOM_SPECIFIERs...>& rule) {
685634
auto& external = get<I>(rule.externals.externals);
686635
auto& bindVariable = external.bindVariable;
687-
bindVariable.unbind();
636+
bindVariable->unbind();
688637
}
689638

690639
template <typename... Ts, typename HEAD_ATOM_SPECIFIER, typename... BODY_ATOM_SPECIFIERs, size_t ... Is>

tests/types_test.cpp

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ bool test1()
3333
{thor, god}};
3434

3535
// Rule
36-
auto x = var<Name>();
37-
auto rule1 = rule(atom<Mortal>(x), atom<Thing>(x, person));
36+
Variable<Name> x;
37+
auto rule1 = rule(atom<Mortal>(&x), atom<Thing>(&x, person));
3838

3939
State<Thing, Mortal> state{things, {}};
4040

@@ -44,6 +44,7 @@ bool test1()
4444
cout << "before = " << state << endl;
4545
state = fixPoint(rules, state);
4646
cout << "after = " << state << endl;
47+
4748
return true;
4849
}
4950

@@ -74,9 +75,9 @@ bool test2()
7475
{rod, alan},
7576
{robin, alan}};
7677

77-
auto x = var<Name>();
78-
auto y = var<Name>();
79-
auto z = var<Name>();
78+
auto x = new Variable<Name>();
79+
auto y = new Variable<Name>();
80+
auto z = new Variable<Name>();
8081

8182
// TODO
8283
//auto inDirectAcademicAncestor = atom<AcademicAncestor>(x, z) <= atom<Adviser>(x, y) && atom<AcademicAncestor>(y, z);
@@ -101,6 +102,10 @@ bool test2()
101102
state = fixPoint(rules, state);
102103
cout << "after = " << state << endl;
103104

105+
delete x;
106+
delete y;
107+
delete z;
108+
104109
return true;
105110
}
106111

@@ -117,21 +122,21 @@ bool po1()
117122

118123
State<Check, In, A> state{check, in, {}};
119124

120-
auto a = var<Number>();
121-
auto b = var<Number>();
122-
auto c = var<Number>();
123-
auto d = var<Number>();
124-
auto e = var<Number>();
125-
auto f = var<Number>();
126-
auto i = var<Number>();
127-
auto anon1 = var<Number>();
128-
auto anon2 = var<Number>();
129-
auto anon3 = var<Number>();
130-
auto anon4 = var<Number>();
131-
auto anon5 = var<Number>();
132-
auto anon6 = var<Number>();
133-
auto anon7 = var<Number>();
134-
auto anon8 = var<Number>();
125+
auto a = new Variable<Number>();
126+
auto b = new Variable<Number>();
127+
auto c = new Variable<Number>();
128+
auto d = new Variable<Number>();
129+
auto e = new Variable<Number>();
130+
auto f = new Variable<Number>();
131+
auto i = new Variable<Number>();
132+
auto anon1 = new Variable<Number>();
133+
auto anon2 = new Variable<Number>();
134+
auto anon3 = new Variable<Number>();
135+
auto anon4 = new Variable<Number>();
136+
auto anon5 = new Variable<Number>();
137+
auto anon6 = new Variable<Number>();
138+
auto anon7 = new Variable<Number>();
139+
auto anon8 = new Variable<Number>();
135140

136141
// A(1,i) :- Check(_, b, c, d, e, f), In(_, b, c, d, e, f, i).
137142
auto rule1 = rule(atom<A>(1u, i), atom<Check>(anon1, b, c, d, e, f), atom<In>(anon2, b, c, d, e, f, i));
@@ -188,6 +193,22 @@ bool po1()
188193
operator<< <A>(cout, computedA);
189194
cout << endl;
190195

196+
delete a;
197+
delete b;
198+
delete c;
199+
delete d;
200+
delete e;
201+
delete f;
202+
delete i;
203+
delete anon1;
204+
delete anon2;
205+
delete anon3;
206+
delete anon4;
207+
delete anon5;
208+
delete anon6;
209+
delete anon7;
210+
delete anon8;
211+
191212
return computedA == aOut;
192213
}
193214
#endif
@@ -220,10 +241,10 @@ bool test4()
220241
{sally, 40u, female, netherlands}
221242
};
222243

223-
auto name = var<Name>();
224-
auto age = var<Age>();
225-
auto gender = var<Gender>();
226-
auto country = var<Country>();
244+
auto name = new Variable<Name>();
245+
auto age = new Variable<Age>();
246+
auto gender = new Variable<Gender>();
247+
auto country = new Variable<Country>();
227248

228249
struct Female : Relation<Name>{};
229250
auto females = rule(
@@ -234,7 +255,7 @@ bool test4()
234255
typedef float Metres;
235256
struct Height : Relation<Name, Metres>{};
236257

237-
auto height = var<Metres>();
258+
auto height = new Variable<Metres>();
238259

239260
auto heights = rule(
240261
atom<Height>(name, 1.0f),
@@ -243,16 +264,16 @@ bool test4()
243264
)
244265
);
245266

246-
#if 0
267+
247268
// Use this pattern to get at values too
248269
auto anyPerson = atom<Person>(name, age, gender, country);
249270

250-
auto externalHeights = rule(
271+
auto externalHeights1 = rule(
251272
atom<Height>(name, height),
252273
body(
253274
atom<Person>(name, age, gender, country)
254275
),
255-
external(
276+
lambda(
256277
height,
257278
[&anyPerson]() {
258279
cout << "hello world!" << endl;
@@ -262,14 +283,13 @@ bool test4()
262283
}
263284
)
264285
);
265-
#else
286+
266287
// Use this pattern to get at variables (values can't be got directly)
267288
auto externalHeights = rule(
268289
atom<Height>(name, height),
269290
body(atom<Person>(name, age, female, country)),
270-
lambda(height, [&age]() { return val(age) * 3.0f; } )
291+
lambda(height, [&age]() { return age->value() * 3.0f; } )
271292
);
272-
#endif
273293

274294
// Apply rules
275295
State<Person, Female, Height> state{people, {}, {}};
@@ -281,13 +301,20 @@ bool test4()
281301
state = fixPoint(rules, state);
282302
cout << "after = " << state << endl;
283303

304+
delete name;
305+
delete age;
306+
delete gender;
307+
delete country;
308+
delete height;
309+
284310
return true;
285311
}
286312

287313
int main()
288314
{
289315
bool ok1 = test1();
290316
bool ok2 = test2();
317+
#if 1
291318
bool ok3 = po1();
292319
bool ok4 = test4();
293320

@@ -298,5 +325,6 @@ int main()
298325
cout << "PASSED" << endl;
299326
return 0;
300327
}
328+
#endif
301329
return 1;
302330
}

0 commit comments

Comments
 (0)
0