|
2 | 2 | #include <cstring>
|
3 | 3 | #include <functional>
|
4 | 4 | #include <iostream>
|
5 |
| -#include <map> |
6 |
| -#include <set> |
7 | 5 |
|
8 | 6 | #include <ds/ds.hh>
|
| 7 | +#include <ds/search.hh> |
9 | 8 | #include <ds/utility.hh>
|
10 | 9 |
|
11 |
| -struct PointerLess { |
12 |
| - template<typename T> |
13 |
| - bool operator()(const T& lhs, const T& rhs) const { |
14 |
| - if (lhs->data_size() < rhs->data_size()) { |
15 |
| - return true; |
16 |
| - } |
17 |
| - if (lhs->data_size() > rhs->data_size()) { |
18 |
| - return false; |
19 |
| - } |
20 |
| - ds::length_t data_size = lhs->data_size(); |
21 |
| - const std::byte* lhs_data = reinterpret_cast<const std::byte*>(lhs.get()); |
22 |
| - const std::byte* rhs_data = reinterpret_cast<const std::byte*>(rhs.get()); |
23 |
| - for (ds::length_t index = 0; index < data_size; ++index) { |
24 |
| - if (lhs_data[index] < rhs_data[index]) { |
25 |
| - return true; |
26 |
| - } |
27 |
| - if (lhs_data[index] > rhs_data[index]) { |
28 |
| - return false; |
29 |
| - } |
30 |
| - } |
31 |
| - return false; |
32 |
| - } |
33 |
| -}; |
34 |
| - |
35 | 10 | void run() {
|
36 | 11 | int temp_data_size = 1000;
|
37 | 12 | int temp_text_size = 1000;
|
38 | 13 | int single_result_size = 10000;
|
39 | 14 |
|
| 15 | + auto search = ds::search_t(temp_data_size, single_result_size); |
| 16 | + |
40 | 17 | // P -> Q, P |- Q
|
41 |
| - auto mp = ds::text_to_rule( |
42 |
| - "(`P -> `Q)\n" |
43 |
| - "`P\n" |
44 |
| - "----------\n" |
45 |
| - "`Q", |
46 |
| - temp_data_size |
47 |
| - ); |
| 18 | + search.add("(`P -> `Q)\n" |
| 19 | + "`P\n" |
| 20 | + "----------\n" |
| 21 | + "`Q\n"); |
48 | 22 | // p -> (q -> p)
|
49 |
| - auto axiom1 = ds::text_to_rule( |
50 |
| - "------------------\n" |
51 |
| - "(`p -> (`q -> `p))\n", |
52 |
| - temp_data_size |
53 |
| - ); |
| 23 | + search.add("(`p -> (`q -> `p))"); |
54 | 24 | // (p -> (q -> r)) -> ((p -> q) -> (p -> r))
|
55 |
| - auto axiom2 = ds::text_to_rule( |
56 |
| - "--------------------------------------------------\n" |
57 |
| - "((`p -> (`q -> `r)) -> ((`p -> `q) -> (`p -> `r)))\n", |
58 |
| - temp_data_size |
59 |
| - ); |
| 25 | + search.add("((`p -> (`q -> `r)) -> ((`p -> `q) -> (`p -> `r)))"); |
60 | 26 | // (!p -> !q) -> (q -> p)
|
61 |
| - auto axiom3 = ds::text_to_rule( |
62 |
| - "----------------------------------\n" |
63 |
| - "(((! `p) -> (! `q)) -> (`q -> `p))\n", |
64 |
| - temp_data_size |
65 |
| - ); |
66 |
| - |
67 |
| - auto premise = ds::text_to_rule("(! (! X))", temp_data_size); |
68 |
| - auto target = ds::text_to_rule("X", temp_data_size); |
| 27 | + search.add("(((! `p) -> (! `q)) -> (`q -> `p))"); |
69 | 28 |
|
70 |
| - std::map<std::unique_ptr<ds::rule_t>, ds::length_t, PointerLess> rules; |
71 |
| - std::map<std::unique_ptr<ds::rule_t>, ds::length_t, PointerLess> facts; |
| 29 | + // premise |
| 30 | + search.add("(! (! X))"); |
72 | 31 |
|
73 |
| - std::set<std::unique_ptr<ds::rule_t>, PointerLess> temp_rules; |
74 |
| - std::set<std::unique_ptr<ds::rule_t>, PointerLess> temp_facts; |
75 |
| - |
76 |
| - ds::length_t cycle = -1; |
77 |
| - rules.emplace(std::move(mp), cycle); |
78 |
| - facts.emplace(std::move(axiom1), cycle); |
79 |
| - facts.emplace(std::move(axiom2), cycle); |
80 |
| - facts.emplace(std::move(axiom3), cycle); |
81 |
| - facts.emplace(std::move(premise), cycle); |
82 |
| - |
83 |
| - auto buffer = std::unique_ptr<ds::rule_t>(reinterpret_cast<ds::rule_t*>(operator new(single_result_size))); |
84 |
| - |
85 |
| - auto less = PointerLess(); |
| 32 | + auto target = ds::text_to_rule("X", temp_data_size); |
86 | 33 |
|
87 | 34 | while (true) {
|
88 |
| - temp_rules.clear(); |
89 |
| - temp_facts.clear(); |
90 |
| - |
91 |
| - for (auto& [rule, rules_cycle] : rules) { |
92 |
| - for (auto& [fact, facts_cycle] : facts) { |
93 |
| - if (rules_cycle != cycle && facts_cycle != cycle) { |
94 |
| - continue; |
95 |
| - } |
96 |
| - buffer->match(rule.get(), fact.get(), reinterpret_cast<std::byte*>(buffer.get()) + single_result_size); |
97 |
| - if (!buffer->valid()) { |
98 |
| - continue; |
99 |
| - } |
100 |
| - if (buffer->premises_count() != 0) { |
101 |
| - // rule |
102 |
| - if (rules.find(buffer) != rules.end() || temp_rules.find(buffer) != temp_rules.end()) { |
103 |
| - continue; |
104 |
| - } |
105 |
| - auto new_rule = std::unique_ptr<ds::rule_t>(reinterpret_cast<ds::rule_t*>(operator new(buffer->data_size()))); |
106 |
| - memcpy(new_rule.get(), buffer.get(), buffer->data_size()); |
107 |
| - temp_rules.emplace(std::move(new_rule)); |
108 |
| - } else { |
109 |
| - // fact |
110 |
| - if (facts.find(buffer) != facts.end() || temp_facts.find(buffer) != temp_facts.end()) { |
111 |
| - continue; |
112 |
| - } |
113 |
| - auto new_fact = std::unique_ptr<ds::rule_t>(reinterpret_cast<ds::rule_t*>(operator new(buffer->data_size()))); |
114 |
| - memcpy(new_fact.get(), buffer.get(), buffer->data_size()); |
115 |
| - if ((!less(new_fact, target)) && (!less(target, new_fact))) { |
116 |
| - printf("Found!\n"); |
117 |
| - printf("%s", ds::rule_to_text(new_fact.get(), temp_text_size).get()); |
118 |
| - return; |
119 |
| - } |
120 |
| - temp_facts.emplace(std::move(new_fact)); |
121 |
| - } |
| 35 | + bool success = false; |
| 36 | + auto callback = [&target, &success, &temp_text_size](ds::rule_t* candidate) { |
| 37 | + if (candidate->data_size() != target->data_size()) { |
| 38 | + return false; |
122 | 39 | }
|
123 |
| - } |
124 |
| - |
125 |
| - ++cycle; |
126 |
| - for (auto& rule : temp_rules) { |
127 |
| - auto& movable_rule = const_cast<std::unique_ptr<ds::rule_t>&>(rule); |
128 |
| - rules.emplace(std::move(movable_rule), cycle); |
129 |
| - } |
130 |
| - for (auto& fact : temp_facts) { |
131 |
| - auto& movable_fact = const_cast<std::unique_ptr<ds::rule_t>&>(fact); |
132 |
| - facts.emplace(std::move(movable_fact), cycle); |
| 40 | + auto data_size = candidate->data_size(); |
| 41 | + auto equal = memcmp(candidate->head(), target->head(), data_size) == 0; |
| 42 | + if (equal) { |
| 43 | + printf("Found!\n"); |
| 44 | + printf("%s", ds::rule_to_text(candidate, temp_text_size).get()); |
| 45 | + success = true; |
| 46 | + return true; |
| 47 | + } |
| 48 | + return false; |
| 49 | + }; |
| 50 | + search.execute(callback); |
| 51 | + if (success) { |
| 52 | + break; |
133 | 53 | }
|
134 | 54 | }
|
135 | 55 | }
|
|
0 commit comments