1pub const PERMITTED_AXIOMS: [&str; 3] = ["propext", "Classical.choice", "Quot.sound"];
21
22pub const EXPORTER: &str = "leanexport";
24
25#[derive(Clone, Copy, Debug, Eq, PartialEq)]
27pub enum KernelInput {
28 ExportPath,
30 NanodaConfig,
32}
33
34#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40pub struct Kernel {
41 pub name: &'static str,
42 pub binary: &'static str,
43 pub args: &'static [&'static str],
44 pub input: KernelInput,
45 pub admits_sorry: bool,
46}
47
48pub const PARANOID_KERNELS: [Kernel; 6] = [
51 Kernel {
52 name: "Lean",
53 binary: "leanchecker",
54 args: &["--silent", "--from-export"],
55 input: KernelInput::ExportPath,
56 admits_sorry: true,
57 },
58 Kernel {
59 name: "Lean (hardened allocator)",
60 binary: "leanchecker-paranoid",
61 args: &["--silent", "--from-export"],
62 input: KernelInput::ExportPath,
63 admits_sorry: true,
64 },
65 Kernel {
66 name: "lean4lean",
67 binary: "lean4lean",
68 args: &["--import"],
69 input: KernelInput::ExportPath,
70 admits_sorry: true,
71 },
72 Kernel {
73 name: "nanoda",
74 binary: "nanoda_bin",
75 args: &[],
76 input: KernelInput::NanodaConfig,
77 admits_sorry: false,
78 },
79 Kernel {
80 name: "con-leche",
81 binary: "con-leche",
82 args: &[],
83 input: KernelInput::ExportPath,
84 admits_sorry: false,
85 },
86 Kernel {
87 name: "con-ron",
88 binary: "con-ron",
89 args: &[],
90 input: KernelInput::ExportPath,
91 admits_sorry: false,
92 },
93];
94
95impl Kernel {
96 pub fn export<'a>(&self, whole: &'a str, sorry_free: &'a str) -> &'a str {
99 if self.admits_sorry { whole } else { sorry_free }
100 }
101
102 pub fn arguments(&self, export_path: &str, nanoda_config_path: &str) -> Vec<String> {
105 let mut arguments: Vec<String> = self.args.iter().map(|a| (*a).to_string()).collect();
106 arguments.push(match self.input {
107 KernelInput::ExportPath => export_path.to_string(),
108 KernelInput::NanodaConfig => nanoda_config_path.to_string(),
109 });
110 arguments
111 }
112}
113
114pub fn nanoda_config(export_path: &str) -> String {
118 let axioms = PERMITTED_AXIOMS
119 .iter()
120 .map(|axiom| json_string(axiom))
121 .collect::<Vec<_>>()
122 .join(",");
123 format!(
124 "{{\"use_stdin\":false,\"export_file_path\":{},\"permitted_axioms\":[{axioms}],\
125 \"unpermitted_axiom_hard_error\":false,\"num_threads\":4,\
126 \"nat_extension\":true,\"string_extension\":true}}",
127 json_string(export_path)
128 )
129}
130
131fn json_string(text: &str) -> String {
132 let mut out = String::with_capacity(text.len() + 2);
133 out.push('"');
134 for ch in text.chars() {
135 match ch {
136 '"' => out.push_str("\\\""),
137 '\\' => out.push_str("\\\\"),
138 ch if (ch as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", ch as u32)),
139 ch => out.push(ch),
140 }
141 }
142 out.push('"');
143 out
144}
145
146pub fn export_mentions(export: &[u8], name_component: &str) -> bool {
150 let needle = json_string(name_component);
151 export
152 .windows(needle.len())
153 .any(|window| window == needle.as_bytes())
154}
155
156#[cfg(test)]
157mod tests {
158 use super::*;
159
160 #[test]
161 fn every_kernel_gets_the_export_or_its_configuration_last() {
162 for kernel in PARANOID_KERNELS {
163 let arguments = kernel.arguments("/x/export.ndjson", "/x/nanoda.json");
164 let last = arguments.last().unwrap();
165 match kernel.input {
166 KernelInput::ExportPath => assert_eq!(last, "/x/export.ndjson"),
167 KernelInput::NanodaConfig => assert_eq!(last, "/x/nanoda.json"),
168 }
169 }
170 }
171
172 #[test]
173 fn the_set_is_lean_first_and_has_no_duplicates() {
174 assert_eq!(PARANOID_KERNELS[0].binary, "leanchecker");
175 let mut binaries: Vec<_> = PARANOID_KERNELS.iter().map(|k| k.binary).collect();
176 binaries.sort_unstable();
177 binaries.dedup();
178 assert_eq!(binaries.len(), PARANOID_KERNELS.len());
179 }
180
181 #[test]
182 fn nanoda_config_names_the_export_and_the_standard_axioms() {
183 let config = nanoda_config("/tmp/a \"b\".ndjson");
184 assert!(config.contains("\"export_file_path\":\"/tmp/a \\\"b\\\".ndjson\""));
185 assert!(
186 config
187 .contains("\"permitted_axioms\":[\"propext\",\"Classical.choice\",\"Quot.sound\"]")
188 );
189 assert!(config.contains("\"use_stdin\":false"));
190 }
191
192 #[test]
193 fn export_mentions_matches_a_whole_json_string_only() {
194 let export = br#"{"str":{"pre":1,"str":"CheckerWitness"}}"#;
195 assert!(export_mentions(export, "CheckerWitness"));
196 assert!(!export_mentions(export, "Checker"));
197 assert!(!export_mentions(b"", "CheckerWitness"));
198 }
199}