8000 support easy declaration and use of variables · Z80coder/datalog-cpp@9283e60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9283e60

Browse files
committed
support easy declaration and use of variables
1 parent 2d290b7 commit 9283e60

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

src/Datalog.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <limits>
99
#include <cassert>
1010
#include <iostream>
11+
#include <memory>
1112

1213
namespace datalog
1314
{
@@ -39,11 +40,21 @@ struct Variable : optional<T>
3940
}
4041
};
4142

43+
template<typename T>
44+
unique_ptr<Variable<T>> var() {
45+
return make_unique<Variable<T>>();
46+
}
47+
4248
template <typename T>
4349
static void unbind(Variable<T>* t) {
4450
t->unbind();
4551
}
4652

53+
template <typename T>
54+
static void unbind(unique_ptr<Variable<T>>& t) {
55+
unbind(t.get());
56+
}
57+
4758
template <typename T>
4859
static void unbind(const T& t) {}
4960

@@ -67,6 +78,11 @@ static bool bind(const T& a, Variable<T>* b) {
6778
return true;
6879
}
6980

81+
template <typename T>
82+
static bool bind(const T& a, unique_ptr<Variable<T>>& b) {
83+
return bind(a, b.get());
84+
}
85+
7086
template <typename GROUND_TYPE, typename ... Ts, size_t... Is>
7187
static bool bind(const GROUND_TYPE &fact, tuple<Ts...> &atom, index_sequence<Is...>)
7288
{
@@ -86,6 +102,12 @@ static void ground(const Variable<T>* s, T &v)
86102
v = s->value();
87103
}
88104

105+
template <typename T>
106+
static void ground(const unique_ptr<Variable<T>>& s, T &v)
107+
{
108+
ground(s.get(), v);
109+
}
110+
89111
template <typename T>
90112
static void ground(const T &s, T &v)
91113
{
@@ -506,6 +528,7 @@ static bool unseenSlice(size_t iteration, const typename RULE_TYPE::SliceType &s
506528
template <typename RULE_TYPE>
507529
static bool unseenSlice(size_t iteration, const typename RULE_TYPE::SliceType &slice) {
508530
return unseenSlice<RULE_TYPE>(iteration, slice, make_index_sequence<tuple_size<typename RULE_TYPE::BodyRelations>::value>{});
531+
return true;
509532
}
510533

511534
template<size_t I, typename RULE_TYPE, typename STATE_TYPE>
@@ -536,13 +559,14 @@ static RelationSet<typename RULE_TYPE::RuleType::HeadRelationType> applyRule(
536559
{
537560
typedef typename RULE_TYPE::RuleType::HeadRelationType HeadRelationType;
538561
RelationSet<HeadRelationType> derivedFacts;
539-
// does the body of this rule refer to relations with new data?
562+
// does the body of this rule refer to relations with unseen data?
540563
if (unseenSlicePossible<typename RULE_TYPE::RuleType, STATE_TYPE>(stateSizeDelta)) {
564+
// OK, we now exhaustively check all relations for unseen combinations
541565
auto it = state.template it<typename RULE_TYPE::RuleType>();
542566
while (it.hasNext())
543567
{
544568
auto slice = it.next();
545-
// does this slice contain a novel combination of ground atoms?
569+
// does this slice contain an unseen combination of ground atoms?
546570
if (unseenSlice<typename RULE_TYPE::RuleType>(iteration, slice)) {
547571
// try to bind rule body with slice
548572
if (bindBodyAtomsToSlice<RULE_TYPE, typename RULE_TYPE::RuleType>(rule.body, slice))

tests/types_test.cpp

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

3535
// Rule
36-
Variable<Name> x;
36+
auto x = var<Name>();
3737
auto rule1 = Rule<Mortal, Thing>::rule(
38-
Mortal::head(&x),
39-
Thing::clause(&x, person)
38+
Mortal::head(x),
39+
Thing::clause(x, person)
4040
);
4141

4242
State<Thing, Mortal> state{things, {}};
@@ -77,20 +77,23 @@ bool test2()
7777
{rod, alan},
7878
{robin, alan}};
7979

80-
Variable<Name> x, y, z;
80+
auto x = var<Name>();
81+
auto y = var<Name>();
82+
auto z = var<Name>();
83+
8184
auto directAcademicAncestor = Rule<AcademicAncestor, Adviser>::rule(
82-
AcademicAncestor::head(&x, &y),
83-
Adviser::clause(&x, &y)
85+
AcademicAncestor::head(x, y),
86+
Adviser::clause(x, y)
8487
);
8588
auto indirectAcademicAncestor = Rule<AcademicAncestor, Adviser, AcademicAncestor>::rule(
86-
AcademicAncestor::head(&x, &z),
87-
Adviser::clause(&x, &y),
88-
AcademicAncestor::clause(&y, &z)
89+
AcademicAncestor::head(x, z),
90+
Adviser::clause(x, y),
91+
AcademicAncestor::clause(y, z)
8992
);
9093
auto query = Rule<QueryResult, AcademicAncestor, AcademicAncestor>::rule(
91-
QueryResult::head(&x),
92-
AcademicAncestor::clause(robin, &x),
93-
AcademicAncestor::clause(&x, mistral)
94+
QueryResult::head(x),
95+
AcademicAncestor::clause(robin, x),
96+
AcademicAncestor::clause(x, mistral)
9497
);
9598

9699
// Apply rules
@@ -119,24 +122,21 @@ bool po1()
119122

120123
State<Check, In, A> state{check, in, {}};
121124

122-
// TODO
123-
//auto var = []() { return make_unique<Variable<Number>>(); };
124-
auto var = []() { return new Variable<Number>; };
125-
auto a = var();
126-
auto b = var();
127-
auto c = var();
128-
auto d = var();
129-
auto e = var();
130-
auto f = var();
131-
auto i = var();
132-
auto anon1 = var();
133-
auto anon2 = var();
134-
auto anon3 = var();
135-
auto anon4 = var();
136-
auto anon5 = var();
137-
auto anon6 = var();
138-
auto anon7 = var();
139-
auto anon8 = var();
125+
auto a = var<Number>();
126+
auto b = var<Number>();
127+
auto c = var<Number>();
128+
auto d = var<Number>();
129+
auto e = var<Number>();
130+
auto f = var<Number>();
131+
auto i = var<Number>();
132+
auto anon1 = var<Number>();
133+
auto anon2 = var<Number>();
134+
auto anon3 = var<Number>();
135+
auto anon4 = var<Number>();
136+
auto anon5 = var<Number>();
137+
auto anon6 = var<Number>();
138+
auto anon7 = var<Number>();
139+
auto anon8 = var<Number>();
140140

141141
typedef Rule<A, Check, In> P0RuleType;
142142

0 commit comments

Comments
 (0)
0