Skip to content

Commit 04dd60e

Browse files
Baltolitothtamas28
andauthored
Add support for rawTerm to Python bindings (#908)
~~Blocked on #907 This PR follows up #907 by having the Python bindings strip patterns of the form `rawTerm{}(inj{...}(...)})` when deserializing from binary KORE; this will allow Pyk and subsequently the proof checker's parser to soundly load function arguments from proof traces and pass them to the function evaluator in #905. --------- Co-authored-by: Tamás Tóth <[email protected]>
1 parent f43af65 commit 04dd60e

File tree

7 files changed

+2238
-9
lines changed

7 files changed

+2238
-9
lines changed

bindings/python/ast.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,13 @@ void bind_ast(py::module_ &m) {
294294
py::kw_only(), py::arg("emit_size") = false)
295295
.def_static(
296296
"deserialize",
297-
[](py::bytes const &bytes) {
297+
[](py::bytes const &bytes, bool strip_raw_term) {
298298
auto str = std::string(bytes);
299-
return deserialize_pattern(str.begin(), str.end());
300-
})
299+
return deserialize_pattern(
300+
str.begin(), str.end(), strip_raw_term);
301+
},
302+
py::arg("bytes"), py::kw_only(),
303+
py::arg("strip_raw_term") = true)
301304
.def_static("read_from", &read_pattern_from_file);
302305

303306
py::class_<KORECompositePattern, std::shared_ptr<KORECompositePattern>>(

bindings/python/package/kllvm/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def to_pattern(self):
3232
def serialize(self, emit_size=False):
3333
return self._block.serialize(emit_size=emit_size)
3434

35+
# Used to implement backend integration tests; should not be bound
36+
# onwards to Pyk without rethinking the underlying API.
37+
def _serialize_raw(self, filename, sort):
38+
self._block._serialize_raw(filename, sort)
39+
3540
@staticmethod
3641
def deserialize(bs):
3742
return mod.InternalTerm.deserialize(bs)

bindings/python/runtime.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,17 @@ void bind_runtime(py::module_ &m) {
8080
return py::bytes(std::string(data, data + size));
8181
},
8282
py::kw_only(), py::arg("emit_size") = false)
83-
.def("deserialize", [](py::bytes const &bytes) {
84-
auto str = std::string(bytes);
85-
return deserializeConfiguration(str.data(), str.size());
86-
});
83+
.def(
84+
"deserialize",
85+
[](py::bytes const &bytes) {
86+
auto str = std::string(bytes);
87+
return deserializeConfiguration(str.data(), str.size());
88+
})
89+
.def(
90+
"_serialize_raw", [](block *term, std::string const &filename,
91+
std::string const &sort) {
92+
serializeRawTermToFile(filename.c_str(), term, sort.c_str());
93+
});
8794
}
8895

8996
PYBIND11_MODULE(_kllvm_runtime, m) {

include/kllvm/binary/deserializer.h

+27-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ sptr<KOREPattern> read(It &ptr, It end, binary_version version) {
254254
std::string file_contents(std::string const &fn, int max_bytes = -1);
255255

256256
template <typename It>
257-
sptr<KOREPattern> deserialize_pattern(It begin, It end) {
257+
sptr<KOREPattern>
258+
deserialize_pattern(It begin, It end, bool strip_raw_term = true) {
258259
// Try to parse the file even if the magic header isn't correct; by the time
259260
// we're here we already know that we're trying to parse a binary KORE file.
260261
// The header itself gets used by the application when detecting binary vs.
@@ -270,7 +271,31 @@ sptr<KOREPattern> deserialize_pattern(It begin, It end) {
270271
end = std::next(begin, total_size);
271272
}
272273

273-
return detail::read(begin, end, version);
274+
auto pattern = detail::read(begin, end, version);
275+
276+
// When strip_raw_term is set, we're looking for special patterns that have
277+
// the form:
278+
//
279+
// rawTerm{}(inj{S, SortKItem{}}(X))
280+
//
281+
// and extracting the inner term X.
282+
if (strip_raw_term) {
283+
if (auto composite
284+
= std::dynamic_pointer_cast<KORECompositePattern>(pattern)) {
285+
if (composite->getConstructor()->getName() == "rawTerm"
286+
&& composite->getArguments().size() == 1) {
287+
if (auto inj = std::dynamic_pointer_cast<KORECompositePattern>(
288+
composite->getArguments()[0])) {
289+
if (inj->getConstructor()->getName() == "inj"
290+
&& inj->getArguments().size() == 1) {
291+
return inj->getArguments()[0];
292+
}
293+
}
294+
}
295+
}
296+
}
297+
298+
return pattern;
274299
}
275300

276301
bool has_binary_kore_header(std::string const &filename);

0 commit comments

Comments
 (0)