1pub const LANGUAGE_REPRESENTATION_SCHEMA_VERSION: u32 = 2;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum RepresentationLevel {
9 Preserved,
11 ConcreteSyntax,
13 Parsed,
15 Resolved,
17 Elaborated,
19 Opaque,
21 NotApplicable,
23 Unavailable,
25}
26
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub struct LanguageSupport {
30 pub schema_version: u32,
32 pub name: &'static str,
34 pub aliases: &'static [&'static str],
36 pub version: &'static str,
38 pub edition: &'static str,
40 pub extensions: &'static [&'static str],
42 pub source_bytes: RepresentationLevel,
44 pub concrete_syntax: RepresentationLevel,
46 pub binding_resolution: RepresentationLevel,
48 pub type_elaboration: RepresentationLevel,
50 pub dynamic_extensions: RepresentationLevel,
52 pub proof_syntax: RepresentationLevel,
54 pub emitter: &'static str,
56}
57
58const fn support(
59 name: &'static str,
60 aliases: &'static [&'static str],
61 version: &'static str,
62 edition: &'static str,
63 extensions: &'static [&'static str],
64 proof_syntax: RepresentationLevel,
65) -> LanguageSupport {
66 LanguageSupport {
67 schema_version: LANGUAGE_REPRESENTATION_SCHEMA_VERSION,
68 name,
69 aliases,
70 version,
71 edition,
72 extensions,
73 source_bytes: RepresentationLevel::Preserved,
74 concrete_syntax: RepresentationLevel::ConcreteSyntax,
75 binding_resolution: RepresentationLevel::Parsed,
76 type_elaboration: RepresentationLevel::Unavailable,
77 dynamic_extensions: RepresentationLevel::Parsed,
78 proof_syntax,
79 emitter: "ordered source-token emitter",
80 }
81}
82
83pub const FOUR_LANGUAGE_SUPPORT: [LanguageSupport; 4] = [
85 support(
86 "JavaScript",
87 &["javascript", "js", "ecmascript"],
88 "ECMAScript 2026",
89 "ECMA-262, 17th edition",
90 &[".js", ".mjs", ".cjs"],
91 RepresentationLevel::NotApplicable,
92 ),
93 support(
94 "Rust",
95 &["rust", "rs"],
96 "Rust 1.99.0",
97 "2024",
98 &[".rs"],
99 RepresentationLevel::NotApplicable,
100 ),
101 support(
102 "Lean",
103 &["lean", "lean4"],
104 "Lean 4.34.1",
105 "Lean 4",
106 &[".lean"],
107 RepresentationLevel::Parsed,
108 ),
109 support(
110 "Rocq",
111 &["rocq", "coq"],
112 "Rocq 9.3",
113 "Vernacular",
114 &[".v"],
115 RepresentationLevel::Parsed,
116 ),
117];
118
119#[must_use]
121pub fn language_support(language: &str) -> Option<&'static LanguageSupport> {
122 FOUR_LANGUAGE_SUPPORT.iter().find(|support| {
123 support
124 .aliases
125 .iter()
126 .any(|alias| alias.eq_ignore_ascii_case(language))
127 })
128}
129
130#[derive(Clone, Copy, Debug, PartialEq, Eq)]
132pub enum TranslationSupport {
133 PortableEncoding,
135 SemanticSubset,
137 SemanticTranslation,
139}
140
141#[derive(Clone, Debug, PartialEq, Eq)]
143pub struct TranslationContract {
144 pub schema_version: u32,
146 pub source: &'static str,
148 pub target: &'static str,
150 pub support: TranslationSupport,
152 pub observation: &'static str,
154 pub required_runtime: &'static str,
156 pub encoding: &'static str,
158 pub assumptions: &'static [&'static str],
160 pub obligation: Option<String>,
162}
163
164#[must_use]
166pub fn translation_contracts() -> Vec<TranslationContract> {
167 FOUR_LANGUAGE_SUPPORT
168 .iter()
169 .flat_map(|source| {
170 FOUR_LANGUAGE_SUPPORT
171 .iter()
172 .filter(move |target| target.name != source.name)
173 .map(move |target| contract(source, target))
174 })
175 .collect()
176}
177
178#[must_use]
180pub fn translation_contract(
181 source_language: &str,
182 target_language: &str,
183) -> Option<TranslationContract> {
184 let source = language_support(source_language)?;
185 let target = language_support(target_language)?;
186 (source.name != target.name).then(|| contract(source, target))
187}
188
189fn contract(source: &LanguageSupport, target: &LanguageSupport) -> TranslationContract {
190 TranslationContract {
191 schema_version: LANGUAGE_REPRESENTATION_SCHEMA_VERSION,
192 source: source.name,
193 target: target.name,
194 support: TranslationSupport::PortableEncoding,
195 observation: "exact source bytes after decoding; target behavior is not preserved",
196 required_runtime: runtime_for(target.name),
197 encoding: "meta-language portable source envelope v1 (UTF-8 hexadecimal payload)",
198 assumptions: &["the source-language runtime is required to execute decoded source"],
199 obligation: Some(
200 "semantic translation is not implemented for arbitrary source programs".to_string(),
201 ),
202 }
203}
204
205fn runtime_for(language: &str) -> &'static str {
206 match language {
207 "Lean" => "Lean 4.34.1 kernel and project environment",
208 "Rocq" => "Rocq 9.3 kernel and project environment",
209 "Rust" => "Rust 1.99.0, edition 2024",
210 _ => "ECMAScript 2026 host",
211 }
212}