Repository navigation
Expand file tree
/
Copy pathsubstitution.cpp
More file actions
168 lines (138 loc) · 6.22 KB
/
Copy pathsubstitution.cpp
File metadata and controls
168 lines (138 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "cppl/kernel/substitution.hpp"
#include "cppl/kernel/proposition.hpp"
#include "cppl/kernel/term.hpp"
#include <cstdint>
#include <utility>
#include <variant>
#include <vector>
namespace cppl::kernel {
Term shift(const Term& term, std::uint32_t amount, std::uint32_t cutoff) {
if (amount == 0) {
return term;
}
if (const auto* variable = std::get_if<Var>(&term.node)) {
if (variable->index.value < cutoff) {
return term;
}
return Term::variable(VarIndex{variable->index.value + amount});
}
if (const auto* call = std::get_if<Call>(&term.node)) {
std::vector<Term> arguments;
arguments.reserve(call->arguments.size());
for (const Term& argument : call->arguments) {
arguments.push_back(shift(argument, amount, cutoff));
}
return Term::call(call->callee, std::move(arguments));
}
if (const auto* primitive = std::get_if<Prim>(&term.node)) {
std::vector<Term> arguments;
arguments.reserve(primitive->arguments.size());
for (const Term& argument : primitive->arguments) {
arguments.push_back(shift(argument, amount, cutoff));
}
return Term::primitive(primitive->op, primitive->type, std::move(arguments));
}
if (const auto* projection = std::get_if<Projection>(&term.node)) {
Projection result = *projection;
for (auto& child : result.arguments)
child = shift(child, amount, cutoff);
return Term{std::move(result)};
}
if (const auto* element = std::get_if<Element>(&term.node)) {
Element result = *element;
for (auto& child : result.arguments)
child = shift(child, amount, cutoff);
return Term{std::move(result)};
}
return term; // a literal denotes the same value under any binder
}
Proposition shift(const Proposition& proposition, std::uint32_t amount, std::uint32_t cutoff) {
if (amount == 0) {
return proposition;
}
if (const auto* quantified = std::get_if<Forall>(&proposition.node)) {
return Proposition::for_all(quantified->binder, shift(*quantified->body, amount, cutoff + 1));
}
if (const auto* implication = std::get_if<Implies>(&proposition.node)) {
return Proposition::implication(shift(*implication->premise, amount, cutoff),
shift(*implication->conclusion, amount, cutoff));
}
if (const auto* conjunction = std::get_if<And>(&proposition.node)) {
return Proposition::conjunction(shift(*conjunction->left, amount, cutoff),
shift(*conjunction->right, amount, cutoff));
}
if (const auto* disjunction = std::get_if<Or>(&proposition.node)) {
return Proposition::disjunction(shift(*disjunction->left, amount, cutoff),
shift(*disjunction->right, amount, cutoff));
}
if (std::holds_alternative<Falsity>(proposition.node)) {
return proposition; // it mentions no variable
}
const auto& equality = std::get<Eq>(proposition.node);
return Proposition::equality(equality.type, shift(equality.lhs, amount, cutoff),
shift(equality.rhs, amount, cutoff));
}
Term instantiate(const Term& body, const Term& argument, std::uint32_t depth) {
if (const auto* variable = std::get_if<Var>(&body.node)) {
if (variable->index.value == depth) {
return shift(argument, depth, 0);
}
if (variable->index.value > depth) {
return Term::variable(VarIndex{variable->index.value - 1});
}
return body;
}
if (const auto* call = std::get_if<Call>(&body.node)) {
std::vector<Term> arguments;
arguments.reserve(call->arguments.size());
for (const Term& nested : call->arguments) {
arguments.push_back(instantiate(nested, argument, depth));
}
return Term::call(call->callee, std::move(arguments));
}
if (const auto* primitive = std::get_if<Prim>(&body.node)) {
std::vector<Term> arguments;
arguments.reserve(primitive->arguments.size());
for (const Term& nested : primitive->arguments) {
arguments.push_back(instantiate(nested, argument, depth));
}
return Term::primitive(primitive->op, primitive->type, std::move(arguments));
}
if (const auto* projection = std::get_if<Projection>(&body.node)) {
Projection result = *projection;
for (auto& child : result.arguments)
child = instantiate(child, argument, depth);
return Term{std::move(result)};
}
if (const auto* element = std::get_if<Element>(&body.node)) {
Element result = *element;
for (auto& child : result.arguments)
child = instantiate(child, argument, depth);
return Term{std::move(result)};
}
return body;
}
Proposition instantiate(const Proposition& body, const Term& argument, std::uint32_t depth) {
if (const auto* quantified = std::get_if<Forall>(&body.node)) {
return Proposition::for_all(quantified->binder, instantiate(*quantified->body, argument, depth + 1));
}
if (const auto* implication = std::get_if<Implies>(&body.node)) {
return Proposition::implication(instantiate(*implication->premise, argument, depth),
instantiate(*implication->conclusion, argument, depth));
}
if (const auto* conjunction = std::get_if<And>(&body.node)) {
return Proposition::conjunction(instantiate(*conjunction->left, argument, depth),
instantiate(*conjunction->right, argument, depth));
}
if (const auto* disjunction = std::get_if<Or>(&body.node)) {
return Proposition::disjunction(instantiate(*disjunction->left, argument, depth),
instantiate(*disjunction->right, argument, depth));
}
if (std::holds_alternative<Falsity>(body.node)) {
return body;
}
const auto& equality = std::get<Eq>(body.node);
return Proposition::equality(equality.type, instantiate(equality.lhs, argument, depth),
instantiate(equality.rhs, argument, depth));
}
} // namespace cppl::kernel