6#include "../include/solver.h"
8std::unordered_map<EquivLaws::EquivLaw, std::string>
equivalences = {};
13void preprocess(std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs)
20 std::string lhsString = lhs->toStringTree();
21 std::string rhsString = rhs->toStringTree();
23 if (lhsString.find(
"->") != std::string::npos || rhsString.find(
"->") != std::string::npos)
28 if (lhsString.find(
"<=>") != std::string::npos || rhsString.find(
"<=>") != std::string::npos)
34std::vector<std::vector<std::string>>
proveEquivalence(std::shared_ptr<Expression> lhs, std::shared_ptr<Expression> rhs)
36 if (lhs->compare(rhs))
37 return {{
"",
"Given"}};
39 std::vector<std::vector<std::string>> steps;
41 std::queue<std::shared_ptr<Expression>> queue;
42 std::unordered_map<std::string, std::pair<std::string, std::string>> visited;
45 visited[lhs->toStringTree()] = {
"",
"Given"};
49 while (!queue.empty())
54 steps.push_back({
"",
"Too many steps :("});
57 std::shared_ptr<Expression> expr = queue.front();
60 if (expr->compare(rhs))
63 std::string currentExprString = expr->toStringTree();
64 while (currentExprString !=
"")
66 steps.push_back({currentExprString, visited[currentExprString].second});
67 currentExprString = visited[currentExprString].first;
69 std::reverse(steps.begin(), steps.end());
75 return {{
"",
"Couldn't find a solution :("}};
78void generateNextSteps(std::shared_ptr<Expression> expr, std::shared_ptr<Expression> end,
bool &found, std::queue<std::shared_ptr<Expression>> &queue, std::unordered_map<std::string, std::pair<std::string, std::string>> &visited)
85 auto funct = equiv.first;
86 auto lawName = equiv.second;
88 std::shared_ptr<Expression> newExpr = expr->cloneTree();
92 std::string newExprString = newExpr->toStringTree();
97 if (visited.find(newExprString) == visited.end())
99 visited[newExprString] = {expr->toStringTree(), lawName};
100 if (newExpr->compareTree(end))
102 while (newExpr->getParent())
103 newExpr = newExpr->getParent();
111 if (expr->getLeft() && !expr->getLeft()->isVar())
113 if (expr->getRight() && !expr->getRight()->isVar())
static std::unordered_map< EquivLaw, std::string > implications
Map of implication equivalences to their string representations.
static std::unordered_map< EquivLaw, std::string > laws
Map of equivalence laws to their string representations.
static std::unordered_map< EquivLaw, std::string > bidirectionalImplications
Map of biimplication equivalences to their string representations.
int MAX_EXPRESSION_LENGTH
Maximum length of the expressions in the queue.
void generateNextSteps(std::shared_ptr< Expression > expr, std::shared_ptr< Expression > end, bool &found, std::queue< std::shared_ptr< Expression > > &queue, std::unordered_map< std::string, std::pair< std::string, std::string > > &visited)
Generate the next expressions from the current expression. Creates a new expression for each law that...
std::vector< std::vector< std::string > > proveEquivalence(std::shared_ptr< Expression > lhs, std::shared_ptr< Expression > rhs)
Prove the expressions are equivalence using equivalence laws by solving the left hand side to the rig...
std::unordered_map< EquivLaws::EquivLaw, std::string > equivalences
Map of equivalences to be applied to the expressions.
int MAX_QUEUE_SIZE
Maximum size of the queue of expressions to be processed.
void preprocess(std::shared_ptr< Expression > lhs, std::shared_ptr< Expression > rhs)
Fill the map of equivalences to be applied to the expressions. Must be called before any other functi...