meta-language 1.0.0

A self-describing links-network core for lossless language representation
Documentation
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
407
408
409
//! Portable proof plans.
//!
//! A source proof keeps its structure: which variable is split by induction or case analysis,
//! and the names of fields and induction hypotheses. It also keeps its hints: definitions to
//! unfold, lemmas and hypotheses to use, and whether arithmetic or computation closes a goal.
//! Each target emits its own tactics from the plan and its kernel re-checks the result, so a
//! proof is never copied as text into a language that cannot check it.
//!
//! Mirrors `js/src/translation/proof.js`.

use std::collections::HashMap;

use super::Language;
use super::diagnostics::{Result, type_error, unsupported};
use super::ir::{Binder, Ctor, Expr, Field, Hints, Node, Plan, PlanCase, Proof, Prop, Split};
use super::surface::{SProof, SSplit, SStep};
use super::types::Type;

/// What a name in a proof refers to.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ProofName {
    Function(String),
    Theorem(String),
    Other,
}

/// The checker's view of the program, as a proof sees it.
pub trait ProofContext {
    /// Resolves a (dotted) name from the theorem's module.
    ///
    /// # Errors
    /// When the name climbs above the crate root.
    fn lookup(&self, name: &str) -> Result<Option<ProofName>>;

    /// The constructors of a data type.
    fn ctors(&self, data: &str) -> Vec<Ctor>;
}

/// The theorem a proof belongs to, with its binders already hoisted.
pub struct TheoremHead<'a> {
    pub full_name: &'a str,
    pub binders: &'a [Binder],
}

/// Normalises a surface proof into a portable plan.
///
/// # Errors
/// On steps outside the portable proof model, unknown constructors and self-reference.
pub fn normalise_proof(
    proof: &SProof,
    theorem: &TheoremHead<'_>,
    context: &dyn ProofContext,
) -> Result<Proof> {
    let mut hints = Hints::default();
    let locals: HashMap<String, Type> = theorem
        .binders
        .iter()
        .map(|binder| (binder.name.clone(), binder.ty.clone()))
        .collect();
    let normaliser = Normaliser {
        theorem,
        context,
        language: proof.source_language,
    };
    let plan = normaliser.sequence(&proof.steps, &mut hints, &locals)?;
    Ok(Proof {
        plan,
        source: proof.source.clone().unwrap_or_default(),
        source_language: proof.source_language,
    })
}

fn merge_hints(into: &mut Hints, from: &Hints) {
    let merge = |into: &mut Vec<String>, from: &[String]| {
        for value in from {
            if !into.contains(value) {
                into.push(value.clone());
            }
        }
    };
    merge(&mut into.unfold, &from.unfold);
    merge(&mut into.lemmas, &from.lemmas);
    merge(&mut into.hyps, &from.hyps);
    merge(&mut into.library, &from.library);
    into.arith |= from.arith;
    into.compute |= from.compute;
}

struct Normaliser<'a> {
    theorem: &'a TheoremHead<'a>,
    context: &'a dyn ProofContext,
    language: Language,
}

impl Normaliser<'_> {
    fn sequence(
        &self,
        steps: &[SStep],
        hints: &mut Hints,
        locals: &HashMap<String, Type>,
    ) -> Result<Plan> {
        for (index, step) in steps.iter().enumerate() {
            match step {
                SStep::Intro { names } => {
                    // Leading binders are hoisted into the theorem's parameters, where the
                    // target introduces them; later `intro`s have nothing left to name.
                    if names.iter().any(|name| !locals.contains_key(name)) {
                        return Err(unsupported(
                            "intro",
                            "introducing hypotheses beyond the theorem binders is outside the portable proof model",
                            None,
                        ));
                    }
                }
                SStep::Unfold { names, .. } => {
                    for name in names {
                        self.add_rule(hints, name, locals, false)?;
                    }
                }
                SStep::Rewrite { rules, .. } => {
                    for rule in rules {
                        self.add_rule(hints, &rule.name, locals, rule.reverse)?;
                    }
                }
                SStep::Simp { rules, .. } => {
                    for rule in rules {
                        self.add_rule(hints, &rule.name, locals, rule.reverse)?;
                    }
                    hints.compute = true;
                }
                SStep::Compute { .. } => hints.compute = true,
                SStep::Arith { .. } => hints.arith = true,
                SStep::Induction(split) => {
                    return self.split(split, true, &steps[index + 1..], hints, locals);
                }
                SStep::Cases(split) => {
                    return self.split(split, false, &steps[index + 1..], hints, locals);
                }
            }
        }
        Ok(Plan::Close {
            hints: hints.clone(),
        })
    }

    fn add_rule(
        &self,
        hints: &mut Hints,
        name: &str,
        locals: &HashMap<String, Type>,
        reverse: bool,
    ) -> Result<()> {
        let push = |list: &mut Vec<String>, value: &str| {
            if !list.iter().any(|item| item == value) {
                list.push(value.to_owned());
            }
        };
        if locals.contains_key(name) {
            push(&mut hints.hyps, name);
            return Ok(());
        }
        match self.context.lookup(name)? {
            Some(ProofName::Function(full_name)) => {
                push(&mut hints.unfold, &full_name);
                return Ok(());
            }
            Some(ProofName::Theorem(full_name)) => {
                if full_name == self.theorem.full_name {
                    return Err(type_error(format!("{name} uses itself"), None));
                }
                push(&mut hints.lemmas, &full_name);
                return Ok(());
            }
            _ => {}
        }
        // Library facts such as `Nat.mul_add` or `Nat.add_comm`: every target
        // closes arithmetic with its own decision procedure, so the name is kept
        // for provenance only.
        if !hints.library.iter().any(|item| item == name) {
            hints.library.push(if reverse {
                format!("<-{name}")
            } else {
                name.to_owned()
            });
        }
        hints.arith = true;
        Ok(())
    }

    fn split(
        &self,
        step: &SSplit,
        induction: bool,
        trailing: &[SStep],
        hints: &Hints,
        locals: &HashMap<String, Type>,
    ) -> Result<Plan> {
        let tactic = if induction { "induction" } else { "cases" };
        let Some(ty) = locals.get(&step.variable) else {
            return Err(unsupported(
                &format!("{tactic} on {}", step.variable),
                "only theorem binders can be split",
                None,
            ));
        };
        let Some(ctors) = self.constructors_of(ty) else {
            return Err(unsupported(
                &format!("{tactic} on {}", ty.key()),
                "only natural numbers and data types can be split",
                None,
            ));
        };
        // Rocq names cases by position (`as [| k ih]` and one bullet per
        // subgoal, in constructor order); Lean names them by constructor.
        if step.positional && step.cases.len() > ctors.len() {
            return Err(type_error(
                format!(
                    "{tactic} on {} has {} cases but {} has {} constructors",
                    step.variable,
                    step.cases.len(),
                    ty.key(),
                    ctors.len()
                ),
                None,
            ));
        }
        let mut cases = Vec::new();
        for (ctor_index, ctor) in ctors.iter().enumerate() {
            let written = if step.positional {
                step.cases.get(ctor_index)
            } else {
                step.cases
                    .iter()
                    .find(|kase| canonical_ctor(kase.ctor.as_deref(), ty) == ctor.name)
            };
            let recursive: Vec<bool> = ctor
                .fields
                .iter()
                .map(|field| field.ty.key() == ty.key())
                .collect();
            let binds = written.map_or(&[][..], |kase| &kase.binds[..]);
            let (fields, ihs) = assign_binds(binds, ctor, &recursive, induction, self.language)?;
            let mut inner = locals.clone();
            for (field, name) in ctor.fields.iter().zip(&fields) {
                inner.insert(name.clone(), field.ty.clone());
            }
            for ih in &ihs {
                inner.insert(ih.clone(), Type::Hypothesis);
            }
            let mut case_hints = Hints::default();
            merge_hints(&mut case_hints, hints);
            let mut steps: Vec<SStep> = written.map(|kase| kase.steps.clone()).unwrap_or_default();
            steps.extend(trailing.iter().cloned());
            let plan = self.sequence(&steps, &mut case_hints, &inner)?;
            cases.push(PlanCase {
                ctor: ctor.name.clone(),
                fields,
                ihs,
                recursive,
                plan,
            });
        }
        if !step.positional {
            for kase in &step.cases {
                let name = canonical_ctor(kase.ctor.as_deref(), ty);
                if !ctors.iter().any(|ctor| ctor.name == name) {
                    return Err(type_error(
                        format!(
                            "{} has no constructor {}",
                            ty.key(),
                            kase.ctor.as_deref().unwrap_or("undefined")
                        ),
                        None,
                    ));
                }
            }
        }
        let split = Split {
            variable: step.variable.clone(),
            ty: ty.clone(),
            cases,
        };
        Ok(if induction {
            Plan::Induction(split)
        } else {
            Plan::Cases(split)
        })
    }

    fn constructors_of(&self, ty: &Type) -> Option<Vec<Ctor>> {
        match ty {
            Type::Nat => Some(vec![
                Ctor {
                    name: "zero".to_owned(),
                    fields: Vec::new(),
                },
                Ctor {
                    name: "succ".to_owned(),
                    fields: vec![Field {
                        name: "pred".to_owned(),
                        ty: ty.clone(),
                    }],
                },
            ]),
            Type::Data { name } => Some(self.context.ctors(name)),
            _ => None,
        }
    }
}

fn canonical_ctor(name: Option<&str>, ty: &Type) -> String {
    let name = name.unwrap_or("undefined");
    if matches!(ty, Type::Nat) {
        return match name {
            "zero" | "O" | "Nat.zero" => "zero",
            "succ" | "S" | "Nat.succ" => "succ",
            other => other,
        }
        .to_owned();
    }
    name.rsplit('.').next().unwrap_or(name).to_owned()
}

/// Lean lists all fields first and then one hypothesis per recursive field;
/// Rocq's `as` patterns put each hypothesis right after its field.
fn assign_binds(
    binds: &[Option<String>],
    ctor: &Ctor,
    recursive: &[bool],
    induction: bool,
    language: Language,
) -> Result<(Vec<String>, Vec<String>)> {
    let mut fields = Vec::new();
    let mut ihs = Vec::new();
    let mut cursor = 0;
    let mut fresh = 0;
    let mut take = |prefix: &str| {
        fresh += 1;
        let fallback = format!("ml_{prefix}{fresh}");
        let name = binds.get(cursor).cloned().flatten();
        cursor += 1;
        match name {
            Some(name) if !name.is_empty() && name != "_" => name,
            _ => fallback,
        }
    };
    if language == Language::Rocq {
        for &is_recursive in recursive.iter().take(ctor.fields.len()) {
            fields.push(take("x"));
            if induction && is_recursive {
                ihs.push(take("ih"));
            }
        }
    } else {
        for _ in &ctor.fields {
            fields.push(take("x"));
        }
        if induction {
            for &is_recursive in recursive {
                if is_recursive {
                    ihs.push(take("ih"));
                }
            }
        }
    }
    if cursor < binds.len() {
        return Err(type_error(
            format!("{} case names {} variables", ctor.name, binds.len()),
            None,
        ));
    }
    Ok((fields, ihs))
}

/// Functions, in first-use order, that a proposition mentions.
#[must_use]
pub fn prop_functions(prop: &Prop, mut into: Vec<String>) -> Vec<String> {
    fn visit(expr: &Expr, into: &mut Vec<String>) {
        if let Node::Call { func, .. } = &expr.node
            && !into.contains(func)
        {
            into.push(func.clone());
        }
        for child in expr.children() {
            visit(child, into);
        }
    }
    fn visit_prop(prop: &Prop, into: &mut Vec<String>) {
        match prop {
            Prop::Forall { body, .. } => visit_prop(body, into),
            Prop::And { left, right }
            | Prop::Or { left, right }
            | Prop::Implies { left, right } => {
                visit_prop(left, into);
                visit_prop(right, into);
            }
            Prop::Not { arg } => visit_prop(arg, into),
            Prop::Bool { expr } => visit(expr, into),
            other => {
                if let Some((_, comparison)) = other.comparison() {
                    visit(&comparison.left, into);
                    visit(&comparison.right, into);
                }
            }
        }
    }
    visit_prop(prop, &mut into);
    into
}