8000 wire-up slice to lower-level bind · Z80coder/datalog-cpp@89cb8d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89cb8d1

Browse files
author
wright
committed
wire-up slice to lower-level bind
1 parent 59bcf6c commit 89cb8d1

File tree

2 files changed

+69
-38
lines changed

2 files changed

+69
-38
lines changed

src/Datalog.h

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ operator<<(ostream &out, const SymbolOrValue<T> &s)
9090
template <typename... Ts>
9191
struct Relation : tuple<Ts...>
9292
{
93-
typedef tuple<Ts...> Type;
93+
typedef tuple<Ts...> TupleType;
9494
typedef tuple<SymbolOrValue<Ts>...> Atom;
9595
using tuple<Ts...>::tuple;
9696
struct compare
@@ -103,6 +103,24 @@ struct Relation : tuple<Ts...>
103103
typedef set<Relation, compare> Set;
104104
};
105105

106+
#if 0
107+
template <std::size_t I, typename ...Ts>
108+
decltype(auto) get(Relation<Ts...>&& v)
109+
{
110+
return std::get<I>(static_cast<std::tuple<Ts...>&&>(v));
111+
}
112+
template <std::size_t I, typename ...Ts>
113+
decltype(auto) get(Relation<Ts...>& v)
114+
{
115+
return std::get<I>(static_cast<std::tuple<Ts...>&>(v));
116+
}
117+
template <std::size_t I, typename ...Ts>
118+
decltype(auto) get(Relation<Ts...> const& v)
119+
{
120+
return std::get<I>(static_cast<std::tuple<Ts...> const&>(v));
121+
}
122+
#endif
123+
106124
template <typename TUPLE_TYPE>
107125
static ostream &
108126
print(ostream &out, const TUPLE_TYPE &s)
@@ -150,9 +168,8 @@ bool bind(SymbolOrValue<VALUE_TYPE> &s, const VALUE_TYPE &v)
150168
return true;
151169
}
152170

153-
template <typename RELATION_TYPE, size_t... Is>
154-
static bool bind(typename RELATION_TYPE::Atom &atom,
155-
const typename RELATION_TYPE::Type &fact, index_sequence<Is...>)
171+
template <typename ATOM_TYPE, typename GROUND_TYPE, size_t... Is>
172+
static bool bind(ATOM_TYPE &atom, const GROUND_TYPE &fact, index_sequence<Is...>)
156173
{
157174
bool success = ((bind<Is>(get<Is>(atom), get<Is>(fact))) and ...);
158175
if (success)
@@ -177,16 +194,23 @@ static bool bind(typename RELATION_TYPE::Atom &atom,
177194
}
178195

179196
// bind 1 atom with 1 fact (of the same relation type)
197+
#if 0
198+
template <typename ATOM_TYPE, typename GROUND_TYPE>
199+
static bool bind(ATOM_TYPE &atom, const GROUND_TYPE &fact)
200+
{
201+
//unbind<RELATION_TYPE>(atom);
202+
constexpr size_t tupleSize = tuple_size<typename GROUND_TYPE::TupleType>::value;
203+
return bind<ATOM_TYPE, typename GROUND_TYPE::TupleType>(atom, fact, make_index_sequence<tupleSize>{});
204+
}
205+
#else
180206
template <typename RELATION_TYPE>
181-
static bool bind(typename RELATION_TYPE::Atom &atom,
182-
//const typename RELATION_TYPE::GroundType& fact
183-
const RELATION_TYPE &fact)
207+
static bool bind(typename RELATION_TYPE::Atom &atom, const RELATION_TYPE &fact)
184208
{
185209
//unbind<RELATION_TYPE>(atom);
186-
//constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::GroundType::Type>::value;
187-
constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::Type>::value;
188-
return bind<RELATION_TYPE>(atom, fact, make_index_sequence<tupleSize>{});
210+
constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::TupleType>::value;
211+
return bind<typename RELATION_TYPE::Atom, typename RELATION_TYPE::TupleType>(atom, fact, make_index_sequence<tupleSize>{});
189212
}
213+
#endif
190214

191215
#if 0
192216
// bind 1 atom with a relation (n facts)
@@ -211,18 +235,18 @@ struct Rule
211235
{
212236
typedef Rule Define;
213237
typedef typename HEAD_RELATION::Atom HeadType;
214-
const HeadType head;
238+
HeadType head;
215239
typedef tuple<BODY_RELATIONs...> BodyRelations;
216240
typedef tuple<typename BODY_RELATIONs::Atom...> BodyType;
217-
const BodyType body;
241+
BodyType body;
218242
};
219243

220244
template <typename... RELATIONs>
221245
struct State
222246
{
223247
typedef tuple<typename RELATIONs::Set...> RelationsType;
224248
RelationsType relations;
225-
typedef tuple<RELATIONs const *...> SliceType;
249+
typedef tuple<const RELATIONs*...> SliceType;
226250
typedef tuple<typename RELATIONs::Set::const_iterator...> IteratorsArrayType;
227251

228252
template <typename TUPLE_TYPE>
@@ -231,7 +255,7 @@ struct State
231255
{
232256
if (p)
233257
{
234-
datalog::print<typename TUPLE_TYPE::Type>(out, *p);
258+
datalog::print<typename TUPLE_TYPE::TupleType>(out, *p);
235259
}
236260
return out;
237261
}
@@ -252,13 +276,13 @@ struct State
252276
private:
253277
template <size_t I, typename RELATION_TYPE>
254278
void pick(const typename RELATION_TYPE::Set &relation,
255-
RELATION_TYPE const *&sliceElement)
279+
const RELATION_TYPE*&sliceElement)
256280
{
257281
const auto &it = get<I>(iterators);
258282
if (it != relation.end())
259283
{
260-
// NASTY: fix this
261-
sliceElement = reinterpret_cast<RELATION_TYPE const *>(&*it);
284+
// TODO: avoid cast if possible
285+
sliceElement = reinterpret_cast<const RELATION_TYPE*>(&*it);
262286
}
263287
else
264288
{
@@ -288,7 +312,6 @@ struct State
288312
it++;
289313
if (it == end)
290314
{
291-
//it = get<I>(relations).tuples.begin();
292315
it = get<I>(relations).begin();
293316
if (I == tuple_size<RelationsType>::value - 1)
294317
{
@@ -361,8 +384,7 @@ struct State
361384

362385
Iterator iterator() const
363386
{
364-
Iterator it{
365-
relations};
387+
Iterator it{relations};
366388
return it;
367389
}
368390
};
@@ -381,34 +403,43 @@ static void unbind(const typename RULE_TYPE::BodyType &atoms)
381403
make_index_sequence<tuple_size<typename RULE_TYPE::BodyType>::value>{});
382404
}
383405

406+
#if 0
407+
// bind 1 atom with 1 fact (of the same relation type)
408+
template <typename RELATION_TYPE>
409+
static bool bind(typename RELATION_TYPE::Atom &atom,
410+
const RELATION_TYPE &fact)
411+
{
412+
//unbind<RELATION_TYPE>(atom);
413+
//constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::Type>::value;
414+
constexpr size_t tupleSize = tuple_size<typename RELATION_TYPE::TupleType>::value;
415+
return bind<RELATION_TYPE>(atom, fact, make_index_sequence<tupleSize>{});
416+
}
417+
#endif
418+
384419
template <size_t I, typename RULE_TYPE, typename... RELATIONs>
385-
static void bind(const typename RULE_TYPE::BodyType &atoms,
420+
static void bind(typename RULE_TYPE::BodyType &atoms, // typedef tuple<typename BODY_RELATIONs::Atom...> BodyType;
386421
const tuple<RELATIONs const *...> &slice)
387422
{
388-
auto &atom = get<I>(atoms);
423+
// get the fact in the slice that can potentially bind
389424
typedef typename tuple_element<I, typename RULE_TYPE::BodyRelations>::type BodyRelationI;
390-
const auto &factPtr = get<BodyRelationI const *>(slice);
391-
//((bind()), ...)
392-
393-
// TODO: atom type is SymbolOrValue but ground type is not
394-
#if 0
395-
((bind<Relation<typename tuple_element<Is, const typename RULE_TYPE::BodyType>::type>>(
396-
get<Is>(atoms),
397-
(get<typename Relation<typename tuple_element<Is, const typename RULE_TYPE::BodyType>::type>::GroundType>(slice))
398-
)), ...);
399-
#endif
425+
auto factPtr = get<BodyRelationI const *>(slice);
426+
const BodyRelationI& fact = *factPtr;
427+
// get the atom
428+
typename BodyRelationI::Atom &atom = get<I>(atoms);
429+
// try to bind the atom with the fact
430+
bind(atom, fact);
400431
}
401432

402433
template <typename RULE_TYPE, typename... RELATIONs, size_t... Is>
403-
static void bind(const typename RULE_TYPE::BodyType &atoms,
434+
static void bind(typename RULE_TYPE::BodyType &atoms,
404435
const tuple<RELATIONs const *...> &slice, index_sequence<Is...>)
405436
{
406437
((bind<Is, RULE_TYPE, RELATIONs...>(atoms, slice)), ...);
407438
}
408439

409440
// bind n atoms (of multiple relation types) with 1 slice (of multiple relation types)
410441
template <typename RULE_TYPE, typename... RELATIONs>
411-
static bool bind(const typename RULE_TYPE::BodyType &atoms,
442+
static bool bind(typename RULE_TYPE::BodyType &atoms,
412443
const tuple<RELATIONs const *...> &slice)
413444
{
414445
// unbind all the symbols
@@ -421,7 +452,7 @@ static bool bind(const typename RULE_TYPE::BodyType &atoms,
421452

422453
// apply a rule to state
423454
template <typename RULE_TYPE, typename... RELATIONs>
424-
static void apply(const RULE_TYPE &rule, const State<RELATIONs...> &state)
455+
static void apply(RULE_TYPE &rule, const State<RELATIONs...> &state)
425456
{
426457
auto it = state.iterator();
427458
while (it.hasNext())

tests/types_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int main()
9999

100100
// Bind 1 atom with 1 fact
101101
Adviser fact1{{"Andrew Rice", "Mistral Contrastin"}};
102-
if (bind<Adviser>(clause1, fact1))
102+
if (bind(clause1, fact1))
103103
{
104104
cout << "successful bind" << endl;
105105
}
@@ -110,7 +110,7 @@ int main()
110110

111111
// Bind 1 atom with 1 fact
112112
Adviser::Atom dummyClause{sym(x), sym(x)};
113-
if (bind<Adviser>(dummyClause, fact1))
113+
if (bind(dummyClause, fact1))
114114
{
115115
cout << "successful bind" << endl;
116116
}
@@ -121,7 +121,7 @@ int main()
121121

122122
// Bind 1 atom with 1 fact
123123
Adviser fact2{{"Mistral Contrastin", "Mistral Contrastin"}};
124-
if (bind<Adviser>(dummyClause, fact2))
124+
if (bind(dummyClause, fact2))
125125
{
126126
cout << "successful bind" << endl;
127127
}

0 commit comments

Comments
 (0)
0