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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! The foundations meta-language knows, as data.
//!
//! `src/data/foundation-models.json` is written from
//! `parity/foundation-models.json` by `js/scripts/build-foundation-models.mjs`;
//! the npm package ships the same file. It records integer, overflow,
//! universe, effect, proof and logic models, the models each language uses,
//! and the explicit correspondences between distinct models. No model is the
//! universal logic every other model reduces to: a consumer brings its own
//! foundation and proof authority and relates it to these models through
//! correspondences.
use std::collections::{BTreeMap, BTreeSet};
use std::sync::OnceLock;
use serde::Deserialize;
use serde_json::{Map, Value};
use crate::SourceAlias;
const FOUNDATION_MODELS_JSON: &str = include_str!("data/foundation-models.json");
/// The kinds of correspondence between two distinct models; none of them claims the models are the same.
pub const CORRESPONDENCE_KINDS: [&str; 4] = ["embedding", "conditional", "encoding", "restatement"];
/// The families whose models are logics or proof authorities.
///
/// A model of one of them that every other model of its family embeds into, or (beyond the
/// booleans every language has) that every language is made to use, would be
/// a hard-coded universal logic.
pub const LOGIC_FAMILIES: [&str; 3] = ["logic-model", "proof-system", "universe-model"];
/// A family of foundation models, such as `integer-model` or `proof-system`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FoundationFamily {
/// Stable identity of the family.
pub id: String,
/// What the family's models describe.
pub definition: String,
}
/// One foundation model: a meaning, not a spelling.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FoundationModel {
/// Stable identity, `<family>.<name>`, such as `overflow-model.wrapping`.
pub id: String,
/// The family the model belongs to.
pub family: String,
/// What the model means.
pub definition: String,
/// The properties that distinguish the model from the other models of its family.
pub properties: Map<String, Value>,
/// The names languages give the model.
pub source_aliases: Vec<SourceAlias>,
}
/// A recorded relation between two distinct models and the condition under which it holds.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FoundationCorrespondence {
/// The model the correspondence starts from.
pub from: String,
/// The model the correspondence leads to.
pub to: String,
/// One of [`CORRESPONDENCE_KINDS`].
pub kind: String,
/// When the two models agree.
pub condition: String,
/// The translation encodings and assumptions this correspondence justifies.
#[serde(default)]
pub translation_encodings: Vec<String>,
}
/// Two models that must never be merged, with the reason.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FoundationDistinction {
/// The two models.
pub models: Vec<String>,
/// What distinguishes them.
pub reason: String,
/// The translation encodings and assumptions this distinction justifies.
#[serde(default)]
pub translation_encodings: Vec<String>,
}
/// The models one language uses.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LanguageFoundation {
/// The language, such as `Rust` or `Lean`.
pub language: String,
/// The identities of the models the language uses, one or more per family.
pub models: Vec<String>,
}
/// A foundation register: the shape of `parity/foundation-models.json`.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FoundationRegister {
/// What the register records.
pub description: String,
/// The model families.
pub families: Vec<FoundationFamily>,
/// The models.
pub models: Vec<FoundationModel>,
/// The correspondences between distinct models.
pub correspondences: Vec<FoundationCorrespondence>,
/// The models that must stay distinct.
pub distinctions: Vec<FoundationDistinction>,
/// The models each language uses.
pub languages: Vec<LanguageFoundation>,
}
/// A way a register fails to keep foundations neutral.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FoundationProblem {
/// The problem kind, the same string the JavaScript runtime reports, such as `universal-model`.
pub kind: &'static str,
/// The model, correspondence or language the problem is about.
pub subject: String,
/// A readable explanation.
pub message: String,
}
/// Returns the embedded foundation register.
#[must_use]
pub fn foundation_register() -> &'static FoundationRegister {
static REGISTER: OnceLock<FoundationRegister> = OnceLock::new();
REGISTER.get_or_init(|| {
serde_json::from_str(FOUNDATION_MODELS_JSON)
.expect("the embedded foundation models are valid")
})
}
/// Returns the model named `id`.
#[must_use]
pub fn foundation_model(id: &str) -> Option<&'static FoundationModel> {
foundation_register()
.models
.iter()
.find(|model| model.id == id)
}
/// Returns the models `language` uses, optionally only those of one family.
#[must_use]
pub fn language_foundation_models(
language: &str,
family: Option<&str>,
) -> Vec<&'static FoundationModel> {
foundation_register()
.languages
.iter()
.filter(|entry| entry.language == language)
.flat_map(|entry| entry.models.iter().filter_map(|id| foundation_model(id)))
.filter(|model| family.is_none_or(|family| model.family == family))
.collect()
}
/// Returns the recorded correspondences between two models, in either direction.
#[must_use]
pub fn foundation_correspondences(a: &str, b: &str) -> Vec<&'static FoundationCorrespondence> {
foundation_register()
.correspondences
.iter()
.filter(|entry| (entry.from == a && entry.to == b) || (entry.from == b && entry.to == a))
.collect()
}
/// Checks that a register keeps foundations neutral.
///
/// Every model belongs to a declared family, distinct models are not merged or
/// claimed equal, every correspondence states its condition, every language
/// names its own models, and no model is a universal logic. Unknown fields such
/// as `universal` are rejected when the register is parsed. An empty result
/// means the register is neutral; the kinds match the JavaScript runtime's
/// `checkFoundationModels`.
#[must_use]
pub fn check_foundation_models(register: &FoundationRegister) -> Vec<FoundationProblem> {
let mut problems = Vec::new();
let mut report = |kind, subject: &str, message: String| {
problems.push(FoundationProblem {
kind,
subject: subject.to_owned(),
message,
});
};
let mut families = BTreeSet::new();
for family in ®ister.families {
if !families.insert(family.id.as_str()) {
report(
"duplicate",
&family.id,
format!("family {} is declared twice", family.id),
);
}
}
let mut models = BTreeMap::new();
for model in ®ister.models {
if models.insert(model.id.as_str(), model).is_some() {
report(
"duplicate",
&model.id,
format!("model {} is declared twice", model.id),
);
}
if !families.contains(model.family.as_str()) {
report(
"unknown-family",
&model.id,
format!("{} belongs to undeclared family {}", model.id, model.family),
);
} else if !model.id.starts_with(&format!("{}.", model.family)) {
report(
"identity",
&model.id,
format!(
"{} is not named under its family {}",
model.id, model.family
),
);
}
if model.definition.is_empty() {
report(
"definition",
&model.id,
format!("{} has no definition", model.id),
);
}
if model.properties.is_empty() {
report(
"properties",
&model.id,
format!(
"{} records no properties, so its distinctness cannot be checked",
model.id
),
);
}
}
for (index, model) in register.models.iter().enumerate() {
for other in ®ister.models[index + 1..] {
// Map equality ignores key order, like the JavaScript runtime's canonical JSON comparison.
if model.family == other.family && model.properties == other.properties {
report(
"indistinct-models",
&model.id,
format!(
"{} and {} record the same properties: one meaning must be one model with source aliases",
model.id, other.id
),
);
}
}
}
let mut pairs = BTreeSet::new();
for entry in ®ister.correspondences {
let subject = format!("{} -> {}", entry.from, entry.to);
for end in [&entry.from, &entry.to] {
if !models.contains_key(end.as_str()) {
report(
"unknown-model",
&subject,
format!("{subject} names undeclared model {end}"),
);
}
}
if entry.from == entry.to {
report(
"self-correspondence",
&subject,
format!("{subject} relates a model to itself"),
);
}
if !CORRESPONDENCE_KINDS.contains(&entry.kind.as_str()) {
report(
"correspondence-kind",
&subject,
format!(
"{subject} has kind {}; two distinct models never correspond exactly, and the same meaning must be one model",
entry.kind
),
);
}
if entry.condition.trim().chars().count() < 20 {
report(
"unconditioned-correspondence",
&subject,
format!("{subject} does not state the condition under which the models agree"),
);
}
let key = if entry.from <= entry.to {
(&entry.from, &entry.to)
} else {
(&entry.to, &entry.from)
};
if !pairs.insert(key) {
report(
"duplicate",
&subject,
format!("{subject} is recorded twice"),
);
}
}
for entry in ®ister.distinctions {
let subject = entry.models.join(" / ");
if entry.models.len() != 2 {
report(
"distinction",
&subject,
"a distinction names exactly two models".to_owned(),
);
}
for id in &entry.models {
if !models.contains_key(id.as_str()) {
report(
"unknown-model",
&subject,
format!("distinction {subject} names undeclared model {id}"),
);
}
}
if entry.reason.trim().chars().count() < 20 {
report(
"distinction",
&subject,
format!("distinction {subject} gives no reason"),
);
}
}
for entry in ®ister.languages {
let mut used = BTreeSet::new();
for id in &entry.models {
match models.get(id.as_str()) {
Some(model) => {
used.insert(model.family.as_str());
}
None => report(
"unknown-model",
&entry.language,
format!("{} uses undeclared model {id}", entry.language),
),
}
}
for family in &families {
if !used.contains(family) {
report(
"language-family",
&entry.language,
format!("{} does not record its {family}", entry.language),
);
}
}
}
for family in LOGIC_FAMILIES
.into_iter()
.filter(|family| families.contains(family))
{
let members: Vec<_> = register
.models
.iter()
.filter(|model| model.family == family)
.collect();
if members.len() < 2 {
continue;
}
for model in &members {
let embedded = members
.iter()
.filter(|other| other.id != model.id)
.all(|other| {
register.correspondences.iter().any(|entry| {
entry.kind == "embedding" && entry.from == other.id && entry.to == model.id
})
});
if embedded {
report(
"universal-model",
&model.id,
format!(
"every other {family} embeds into {}, which makes it a hard-coded universal logic",
model.id
),
);
}
if family != "logic-model"
&& register.languages.len() > 1
&& register
.languages
.iter()
.all(|entry| entry.models.contains(&model.id))
{
report(
"universal-model",
&model.id,
format!(
"every language is made to use {}, which makes it a hard-coded universal foundation",
model.id
),
);
}
}
}
problems
}