Skip to content

Commit b801f78

Browse files
committed
links: add generation of Link instances
1 parent d436bcb commit b801f78

File tree

83 files changed

+1863727
-339
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1863727
-339
lines changed

.github/workflows/rust.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,38 +117,38 @@ jobs:
117117
cd crates
118118
# move-abstract-stack
119119
cd move-abstract-stack
120-
cargo coq-of-rust
120+
cargo coq-of-rust --with-json
121121
touch src/lib.rs
122-
cargo coq-of-rust
123-
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_abstract_stack/ --include='*/' --include='*.v' --exclude='*'
122+
cargo coq-of-rust --with-json
123+
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_abstract_stack/ --include='*/' --include='*.v' --include='*.json' --exclude='*'
124124
cd ..
125125
# move-binary-format
126126
cd move-binary-format
127-
cargo coq-of-rust
127+
cargo coq-of-rust --with-json
128128
touch src/lib.rs
129-
cargo coq-of-rust
130-
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_binary_format/ --include='*/' --include='*.v' --exclude='*'
129+
cargo coq-of-rust --with-json
130+
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_binary_format/ --include='*/' --include='*.v' --include='*.json' --exclude='*'
131131
cd ..
132132
# move-bytecode-verifier
133133
cd move-bytecode-verifier
134-
cargo coq-of-rust
134+
cargo coq-of-rust --with-json
135135
touch src/lib.rs
136-
cargo coq-of-rust
137-
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_bytecode_verifier/ --include='*/' --include='*.v' --exclude='*'
136+
cargo coq-of-rust --with-json
137+
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_bytecode_verifier/ --include='*/' --include='*.v' --include='*.json' --exclude='*'
138138
cd ..
139139
# move-bytecode-verifier-meter
140140
cd move-bytecode-verifier-meter
141-
cargo coq-of-rust
141+
cargo coq-of-rust --with-json
142142
touch src/lib.rs
143-
cargo coq-of-rust
144-
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_bytecode_verifier_meter/ --include='*/' --include='*.v' --exclude='*'
143+
cargo coq-of-rust --with-json
144+
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_bytecode_verifier_meter/ --include='*/' --include='*.v' --include='*.json' --exclude='*'
145145
cd ..
146146
# move-core-types
147147
cd move-core-types
148-
cargo coq-of-rust
148+
cargo coq-of-rust --with-json
149149
touch src/lib.rs
150-
cargo coq-of-rust
151-
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_core_types/ --include='*/' --include='*.v' --exclude='*'
150+
cargo coq-of-rust --with-json
151+
rsync -acv src/ ../../../../CoqOfRust/move_sui/translations/move_core_types/ --include='*/' --include='*.v' --include='*.json' --exclude='*'
152152
cd ..
153153
cd ../../..
154154
endGroup

CoqOfRust/blacklist.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ core/iter/adapters/flatten.v
1818
token-2022
1919
# Links that are not working yet
2020
core/links/cmp.v
21-
move_sui/links/
21+
links/move_sui/move_binary_format/file_format.v
22+
links/move_sui/move_bytecode_verifier/type_safety.v

CoqOfRust/links.py

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@
33
"""
44

55

6-
def find_top_level_item(top_level, condition):
6+
from typing import Any, Tuple
7+
8+
9+
def find_top_level_item(prefix: list[str], top_level, condition) -> Tuple[list[str], Any] | None:
710
for entry in top_level:
811
item = entry["item"]
912
if condition(item):
10-
return item
13+
return (prefix, item)
1114
if item["type"] == "Module":
12-
result = find_top_level_item(item["body"], condition)
15+
result = find_top_level_item(prefix + [item["name"]], item["body"], condition)
1316
if result:
1417
return result
1518

1619

17-
def find_top_level_item_by_name(top_level, name):
18-
return find_top_level_item(
20+
def find_top_level_item_by_name(crate: str, top_level, name: str) -> Tuple[list[str], Any]:
21+
result = find_top_level_item(
22+
[crate],
1923
top_level,
2024
lambda item: "name" in item and item["name"] == name
2125
)
26+
if result:
27+
return result
28+
raise Exception("Item not found: " + name)
2229

2330

2431
def get_header(imports: list[str]) -> str:
@@ -30,8 +37,12 @@ def get_header(imports: list[str]) -> str:
3037
"""
3138

3239

33-
def indent(level: int) -> str:
34-
return " " * level
40+
def indent(s: str) -> str:
41+
return "\n".join(
42+
# We do not indent empty lines
43+
" " + line if len(line) > 0 else ""
44+
for line in s.split("\n")
45+
)
3546

3647

3748
def paren(with_paren: bool, s: str) -> str:
@@ -51,41 +62,98 @@ def pp_type(with_paren: bool, item) -> str:
5162
path = item["path"]
5263
if path == ["*const"]:
5364
return "Ref.t Pointer.Kind.ConstPointer"
65+
if path == ["bool"]:
66+
return "bool"
5467
return pp_path(path) + ".t"
5568
if item["type"] == "Application":
5669
return paren(
57-
with_paren,
70+
with_paren and len(item["tys"]) > 0,
5871
" ".join(pp_type(True, ty) for ty in [item["func"]] + item["tys"])
5972
)
6073
return "Unknown type " + item["type"]
6174

6275

63-
def pp_type_struct_struct(level: int, item) -> str:
64-
result = "Module " + item["name"] + ".\n"
76+
def pp_type_struct_struct(prefix: list[str], item) -> str:
6577
if len(item["ty_params"]) != 0:
6678
ty_params = "(" + " ".join(item["ty_params"]) + ": Set) "
6779
else:
6880
ty_params = ""
6981
ty_params_links = "".join("`{Link " + ty_param + "} " for ty_param in item["ty_params"])
70-
result += indent(level + 1) + "Record t " + ty_params + ty_params_links + ": Set := {\n"
71-
for field in item["fields"]:
72-
result += indent(level + 2) + field[0] + ": " + pp_type(False, field[1]) + ";\n"
73-
result += indent(level + 1) + "}.\n"
74-
result += indent(level) + "End " + item["name"] + "."
75-
return result
82+
return pp_module(item["name"],
83+
"Record t " + ty_params + ty_params_links + ": Set := {\n" +
84+
indent("".join(
85+
field[0] + ": " + pp_type(False, field[1]) + ";\n"
86+
for field in item["fields"]
87+
)) +
88+
"}.\n" +
89+
("Arguments Build_t {" + " ".join(["_ _"] * len(item["ty_params"])) + "}.\n"
90+
if len(item["ty_params"]) > 0
91+
else ""
92+
) +
93+
"\n" +
94+
"Global Instance IsLink " + ty_params + ty_params_links + ": Link " +
95+
paren(len(item["ty_params"]) > 0, " ".join(["t"] + item["ty_params"])) +
96+
" := {\n" +
97+
indent(
98+
"to_ty := Ty.path \"" + "::".join(prefix + [item["name"]]) + "\";\n" +
99+
"to_value '(Build_t" + "".join(" " + field[0] for field in item["fields"]) + ") :=\n" +
100+
indent(
101+
"Value.StructRecord \"" + "::".join(prefix + [item["name"]]) + "\" [\n" +
102+
indent(";\n".join(
103+
"(\"" + field[0] + "\", to_value " + field[0] + ")"
104+
for field in item["fields"]
105+
)) + "\n" +
106+
"];\n"
107+
)
108+
) +
109+
"}."
110+
)
76111

77112

78-
def pp_type_struct_tuple(level: int, item) -> str:
79-
result = "Module " + item["name"] + ".\n"
113+
def pp_type_struct_tuple(prefix: list[str], item) -> str:
80114
if len(item["ty_params"]) != 0:
81115
ty_params = "(" + " ".join(item["ty_params"]) + ": Set) "
82116
else:
83117
ty_params = ""
84118
ty_params_links = "".join("`{Link " + ty_param + "} " for ty_param in item["ty_params"])
85-
result += indent(level + 1) + "Inductive t " + ty_params + ty_params_links + ": Set :=\n"
86-
result += \
87-
indent(level + 1) + "| Make :" + \
88-
"".join(" " + pp_type(False, field) + " ->" for field in item["fields"]) + \
89-
" t" + "".join(" " + ty_param for ty_param in item["ty_params"]) + ".\n"
90-
result += indent(level) + "End " + item["name"] + "."
91-
return result
119+
return pp_module(item["name"],
120+
"Inductive t " + ty_params + ty_params_links + ": Set :=\n" +
121+
"| Make :" +
122+
"".join(
123+
" " + pp_type(False, field) + " ->"
124+
for field in item["fields"]
125+
) +
126+
" t" + "".join(" " + ty_param for ty_param in item["ty_params"]) + ".\n" +
127+
("Arguments Make {" + " ".join(["_ _"] * len(item["ty_params"])) + "}.\n"
128+
if len(item["ty_params"]) > 0
129+
else ""
130+
) +
131+
"\n" +
132+
"Global Instance IsLink " + ty_params + ty_params_links + ": Link " +
133+
paren(len(item["ty_params"]) > 0, " ".join(["t"] + item["ty_params"])) +
134+
" := {\n" +
135+
indent(
136+
"to_ty := Ty.path \"" + "::".join(prefix + [item["name"]]) + "\";\n" +
137+
"to_value '(Make" + "".join(" x" + str(index) for index in range(len(item["fields"]))) + ") :=\n" +
138+
indent(
139+
"Value.StructTuple \"" + "::".join(prefix + [item["name"]]) + "\" [" +
140+
"; ".join("to_value x" + str(index) for index in range(len(item["fields"]))) + "];\n"
141+
)
142+
) +
143+
"}."
144+
)
145+
146+
147+
def pp_top_level_item(prefix: list[str], item) -> str:
148+
if item["type"] == "TypeStructStruct":
149+
return pp_type_struct_struct(prefix, item)
150+
if item["type"] == "TypeStructTuple":
151+
return pp_type_struct_tuple(prefix, item)
152+
return "Unknown item type " + item["type"]
153+
154+
155+
def pp_module(name: str, content: str) -> str:
156+
return \
157+
"Module " + name + ".\n" + \
158+
indent(content) + "\n" + \
159+
"End " + name + "."

CoqOfRust/links/alloc/alloc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import json
2+
import links as l
3+
4+
crate = "alloc"
5+
alloc = json.load(open("alloc/alloc.json"))
6+
7+
print(l.get_header([
8+
]))
9+
print(l.pp_top_level_item(*l.find_top_level_item_by_name(crate, alloc, "Global")))

CoqOfRust/links/alloc/alloc.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(* Generated file for the links. Do not edit. *)
2+
Require Import CoqOfRust.CoqOfRust.
3+
Require Import links.M.
4+
5+
Import Run.
6+
7+
Module Global.
8+
Inductive t : Set :=
9+
| Make : t.
10+
11+
Global Instance IsLink : Link t := {
12+
to_ty := Ty.path "alloc::alloc::Global";
13+
to_value '(Make) :=
14+
Value.StructTuple "alloc::alloc::Global" [];
15+
}.
16+
End Global.

CoqOfRust/links/alloc/raw_vec.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import json
22
import links as l
33

4-
input_file = "alloc/raw_vec.json"
5-
6-
with open(input_file) as f:
7-
input = json.load(f)
4+
crate = "alloc"
5+
raw_vec = json.load(open("alloc/raw_vec.json"))
86

97
print(l.get_header([
108
"links.core.ptr.unique",
119
]))
12-
print(l.pp_type_struct_tuple(0, l.find_top_level_item_by_name(input, "Cap")))
10+
print(l.pp_top_level_item(*l.find_top_level_item_by_name(crate, raw_vec, "Cap")))
1311
print()
14-
print(l.pp_type_struct_struct(0, l.find_top_level_item_by_name(input, "RawVecInner")))
12+
print(l.pp_top_level_item(*l.find_top_level_item_by_name(crate, raw_vec, "RawVecInner")))
1513
print()
16-
print(l.pp_type_struct_struct(0, l.find_top_level_item_by_name(input, "RawVec")))
14+
print(l.pp_top_level_item(*l.find_top_level_item_by_name(crate, raw_vec, "RawVec")))

CoqOfRust/links/alloc/raw_vec.v

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Import Run.
88
Module Cap.
99
Inductive t : Set :=
1010
| Make : usize.t -> t.
11+
12+
Global Instance IsLink : Link t := {
13+
to_ty := Ty.path "alloc::raw_vec::Cap";
14+
to_value '(Make x0) :=
15+
Value.StructTuple "alloc::raw_vec::Cap" [to_value x0];
16+
}.
1117
End Cap.
1218

1319
Module RawVecInner.
@@ -16,11 +22,32 @@ Module RawVecInner.
1622
cap: alloc.raw_vec.Cap.t;
1723
alloc: A;
1824
}.
25+
Arguments Build_t {_ _}.
26+
27+
Global Instance IsLink (A: Set) `{Link A} : Link (t A) := {
28+
to_ty := Ty.path "alloc::raw_vec::RawVecInner";
29+
to_value '(Build_t ptr cap alloc) :=
30+
Value.StructRecord "alloc::raw_vec::RawVecInner" [
31+
("ptr", to_value ptr);
32+
("cap", to_value cap);
33+
("alloc", to_value alloc)
34+
];
35+
}.
1936
End RawVecInner.
2037

2138
Module RawVec.
2239
Record t (T A: Set) `{Link T} `{Link A} : Set := {
2340
inner: alloc.raw_vec.RawVecInner.t A;
2441
_marker: core.marker.PhantomData.t T;
2542
}.
43+
Arguments Build_t {_ _ _ _}.
44+
45+
Global Instance IsLink (T A: Set) `{Link T} `{Link A} : Link (t T A) := {
46+
to_ty := Ty.path "alloc::raw_vec::RawVec";
47+
to_value '(Build_t inner _marker) :=
48+
Value.StructRecord "alloc::raw_vec::RawVec" [
49+
("inner", to_value inner);
50+
("_marker", to_value _marker)
51+
];
52+
}.
2653
End RawVec.

CoqOfRust/links/alloc/vec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
import links as l
3+
4+
crate = "alloc"
5+
vec_mod = json.load(open("alloc/vec/mod.json"))
6+
7+
print(l.get_header([
8+
"links.alloc.raw_vec",
9+
]))
10+
print(l.pp_top_level_item(*l.find_top_level_item_by_name(crate, vec_mod, "Vec")))

CoqOfRust/links/alloc/vec.v

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(* Generated file for the links. Do not edit. *)
2+
Require Import CoqOfRust.CoqOfRust.
3+
Require Import links.M.
4+
Require links.alloc.raw_vec.
5+
6+
Import Run.
7+
8+
Module Vec.
9+
Record t (T A: Set) `{Link T} `{Link A} : Set := {
10+
buf: alloc.raw_vec.RawVec.t T A;
11+
len: usize.t;
12+
}.
13+
Arguments Build_t {_ _ _ _}.
14+
15+
Global Instance IsLink (T A: Set) `{Link T} `{Link A} : Link (t T A) := {
16+
to_ty := Ty.path "alloc::vec::Vec";
17+
to_value '(Build_t buf len) :=
18+
Value.StructRecord "alloc::vec::Vec" [
19+
("buf", to_value buf);
20+
("len", to_value len)
21+
];
22+
}.
23+
End Vec.

CoqOfRust/links/alloc/vec/mod.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)