litex-lang 0.9.6-beta

A simple formal proof language and verifier, learnable in 2 hours
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
410
411
412
413
414
415
416
417
418
419
420
421
use crate::prelude::*;
use std::fmt;

#[derive(Debug)]
pub enum RuntimeError {
    ArithmeticError(RuntimeErrorStruct),
    NewAtomicFactError(RuntimeErrorStruct),
    StoreFactError(RuntimeErrorStruct),
    ParseError(RuntimeErrorStruct),
    ExecStmtError(RuntimeErrorStruct),
    WellDefinedError(RuntimeErrorStruct),
    VerifyError(RuntimeErrorStruct),
    UnknownError(RuntimeErrorStruct),
    InferError(RuntimeErrorStruct),
    NameAlreadyUsedError(RuntimeErrorStruct),
    DefineParamsError(RuntimeErrorStruct),
    InstantiateError(RuntimeErrorStruct),
}

#[derive(Debug)]
pub struct RuntimeErrorStruct {
    pub statement: Option<Stmt>,
    pub msg: String,
    pub conflict_with: Option<ConflictMsg>,
    pub line_file: LineFile,
    pub previous_error: Option<Box<RuntimeError>>,
    pub inside_results: Vec<StmtResult>,
}

macro_rules! runtime_error_from_wrapper {
    ($wrapper:ident, $variant:ident) => {
        #[derive(Debug)]
        pub struct $wrapper(pub RuntimeErrorStruct);
        impl From<$wrapper> for RuntimeError {
            fn from(w: $wrapper) -> Self {
                RuntimeError::$variant(w.0)
            }
        }
    };
}

runtime_error_from_wrapper!(ArithmeticRuntimeError, ArithmeticError);
runtime_error_from_wrapper!(NewAtomicFactRuntimeError, NewAtomicFactError);
runtime_error_from_wrapper!(StoreFactRuntimeError, StoreFactError);
runtime_error_from_wrapper!(ParseRuntimeError, ParseError);
runtime_error_from_wrapper!(WellDefinedRuntimeError, WellDefinedError);
runtime_error_from_wrapper!(VerifyRuntimeError, VerifyError);
runtime_error_from_wrapper!(UnknownRuntimeError, UnknownError);
runtime_error_from_wrapper!(InferRuntimeError, InferError);
runtime_error_from_wrapper!(NameAlreadyUsedRuntimeError, NameAlreadyUsedError);
runtime_error_from_wrapper!(DefineParamsRuntimeError, DefineParamsError);
runtime_error_from_wrapper!(InstantiateRuntimeError, InstantiateError);

#[derive(Debug, Clone)]
pub struct ConflictMsg {
    pub msg: String,
    pub line_file: LineFile,
    pub stmt: Option<Stmt>,
}

impl RuntimeErrorStruct {
    pub fn new(
        statement: Option<Stmt>,
        msg: String,
        line_file: LineFile,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        RuntimeErrorStruct::new_with_conflict(
            statement,
            msg,
            line_file,
            None,
            previous_error,
            vec![],
        )
    }

    pub fn new_with_conflict(
        statement: Option<Stmt>,
        msg: String,
        line_file: LineFile,
        conflict_with: Option<ConflictMsg>,
        previous_error: Option<RuntimeError>,
        inside_results: Vec<StmtResult>,
    ) -> Self {
        RuntimeErrorStruct {
            statement,
            msg,
            conflict_with,
            line_file,
            previous_error: boxed_previous_error(previous_error),
            inside_results,
        }
    }

    pub fn new_with_msg_previous_error(msg: String, previous_error: Option<RuntimeError>) -> Self {
        RuntimeErrorStruct::new(None, msg, default_line_file(), previous_error)
    }

    pub fn exec_stmt_new(
        stmt: Option<Stmt>,
        info: String,
        previous_error: Option<RuntimeError>,
        inside_results: Vec<StmtResult>,
    ) -> Self {
        let line_file = if let Some(ref stmt) = stmt {
            stmt.line_file()
        } else {
            default_line_file()
        };
        RuntimeErrorStruct::new_with_conflict(
            stmt,
            info,
            line_file,
            None,
            previous_error,
            inside_results,
        )
    }

    pub fn exec_stmt_new_with_stmt(
        stmt: Stmt,
        info: String,
        previous_error: Option<RuntimeError>,
        inside_results: Vec<StmtResult>,
    ) -> Self {
        let line_file = stmt.line_file();
        RuntimeErrorStruct::new_with_conflict(
            Some(stmt),
            info,
            line_file,
            None,
            previous_error,
            inside_results,
        )
    }

    pub fn exec_stmt_with_message_and_cause(
        stmt: Stmt,
        message: String,
        cause: Option<RuntimeError>,
        inside_results: Vec<StmtResult>,
    ) -> Self {
        let line_file = stmt.line_file();
        let previous_error = if message.is_empty() {
            cause
        } else {
            Some(
                RuntimeError::new_unknown_error_with_msg_position_optional_fact_previous_error(
                    message.clone(),
                    line_file,
                    None,
                    cause,
                ),
            )
        };
        RuntimeErrorStruct::exec_stmt_new_with_stmt(stmt, message, previous_error, inside_results)
    }
}

impl std::error::Error for RuntimeError {}

impl RuntimeError {
    pub fn into_struct(self) -> RuntimeErrorStruct {
        match self {
            RuntimeError::ArithmeticError(s) => s,
            RuntimeError::NewAtomicFactError(s) => s,
            RuntimeError::StoreFactError(s) => s,
            RuntimeError::ParseError(s) => s,
            RuntimeError::ExecStmtError(s) => s,
            RuntimeError::WellDefinedError(s) => s,
            RuntimeError::VerifyError(s) => s,
            RuntimeError::UnknownError(s) => s,
            RuntimeError::InferError(s) => s,
            RuntimeError::NameAlreadyUsedError(s) => s,
            RuntimeError::DefineParamsError(s) => s,
            RuntimeError::InstantiateError(s) => s,
        }
    }

    pub fn line_file(&self) -> LineFile {
        match self {
            RuntimeError::ArithmeticError(e) => e.line_file.clone(),
            RuntimeError::NewAtomicFactError(e) => e.line_file.clone(),
            RuntimeError::StoreFactError(e) => e.line_file.clone(),
            RuntimeError::ParseError(e) => e.line_file.clone(),
            RuntimeError::ExecStmtError(e) => e.line_file.clone(),
            RuntimeError::WellDefinedError(e) => e.line_file.clone(),
            RuntimeError::VerifyError(e) => e.line_file.clone(),
            RuntimeError::UnknownError(e) => e.line_file.clone(),
            RuntimeError::InferError(e) => e.line_file.clone(),
            RuntimeError::NameAlreadyUsedError(e) => e.line_file.clone(),
            RuntimeError::DefineParamsError(e) => e.line_file.clone(),
            RuntimeError::InstantiateError(e) => e.line_file.clone(),
        }
    }

    pub fn display_label(&self) -> &'static str {
        match self {
            RuntimeError::ArithmeticError(_) => "ArithmeticError",
            RuntimeError::NewAtomicFactError(_) => "NewAtomicFactError",
            RuntimeError::StoreFactError(_) => "StoreFactError",
            RuntimeError::ParseError(_) => "ParseError",
            RuntimeError::ExecStmtError(_) => "ExecStmtError",
            RuntimeError::WellDefinedError(_) => "WellDefinedError",
            RuntimeError::VerifyError(_) => "VerifyError",
            RuntimeError::UnknownError(_) => "UnknownError",
            RuntimeError::InferError(_) => "InferError",
            RuntimeError::NameAlreadyUsedError(_) => "NameAlreadyUsedError",
            RuntimeError::DefineParamsError(_) => "DefineParamsError",
            RuntimeError::InstantiateError(_) => "InstantiateError",
        }
    }

    pub fn message_text_for_duplicate_used_name_without_line_file(name: &str) -> String {
        format!(
            "name `{}` is already used, cannot be used again for other definitions",
            name
        )
    }

    pub fn new_infer_error_with_msg_position_previous_error(
        msg: String,
        line_file: LineFile,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        InferRuntimeError(RuntimeErrorStruct::new(
            None,
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_define_params_error_with_msg_previous_error_position(
        msg: String,
        previous_error: Option<RuntimeError>,
        line_file: LineFile,
    ) -> Self {
        DefineParamsRuntimeError(RuntimeErrorStruct::new(
            None,
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_parse_error_with_msg_position_previous_error(
        msg: String,
        line_file: LineFile,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        ParseRuntimeError(RuntimeErrorStruct::new(
            None,
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_parse_error_for_block_unexpected_indent_at_line_file(line_file: LineFile) -> Self {
        let (line_no, path) = (line_file.0, line_file.1.as_ref());
        Self::new_parse_error_with_msg_position_previous_error(
            format!("unexpected indent at line {} in {}", line_no, path),
            line_file,
            None,
        )
    }

    pub fn new_parse_error_for_block_expected_indent_at_line_file(line_file: LineFile) -> Self {
        let (line_no, path) = (line_file.0, line_file.1.as_ref());
        Self::new_parse_error_with_msg_position_previous_error(
            format!("expected indent at line {} in {}", line_no, path),
            line_file,
            None,
        )
    }

    pub fn new_parse_error_for_block_missing_body_at_line_file(line_file: LineFile) -> Self {
        let (line_no, path) = (line_file.0, line_file.1.as_ref());
        Self::new_parse_error_with_msg_position_previous_error(
            format!("block header missing body at line {} in {}", line_no, path),
            line_file,
            None,
        )
    }

    pub fn new_parse_error_for_block_inconsistent_indent_at_line_file(line_file: LineFile) -> Self {
        let (line_no, path) = (line_file.0, line_file.1.as_ref());
        Self::new_parse_error_with_msg_position_previous_error(
            format!("inconsistent indent at line {} in {}", line_no, path),
            line_file,
            None,
        )
    }

    pub fn new_verify_error_with_fact_msg_position_previous_error(
        fact: Fact,
        msg: String,
        line_file: LineFile,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        VerifyRuntimeError(RuntimeErrorStruct::new(
            Some(fact.into_stmt()),
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_verify_error_with_msg_position_previous_error(
        msg: String,
        line_file: LineFile,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        VerifyRuntimeError(RuntimeErrorStruct::new(
            None,
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_unknown_error_with_msg_position_optional_fact_previous_error(
        msg: String,
        line_file: LineFile,
        fact: Option<Fact>,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        UnknownRuntimeError(RuntimeErrorStruct::new(
            if let Some(fact) = fact {
                Some(fact.into_stmt())
            } else {
                None
            },
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_verify_result_unknown_with_fact_previous_error(
        fact: Fact,
        msg: String,
        previous_error: Option<RuntimeError>,
    ) -> Self {
        let line_file = fact.line_file();
        RuntimeError::new_unknown_error_with_msg_position_optional_fact_previous_error(
            msg,
            line_file,
            Some(fact),
            previous_error,
        )
    }

    pub fn new_well_defined_error_with_msg_previous_error_position(
        msg: String,
        previous_error: Option<RuntimeError>,
        line_file: LineFile,
    ) -> Self {
        WellDefinedRuntimeError(RuntimeErrorStruct::new(
            None,
            msg,
            line_file,
            previous_error,
        ))
        .into()
    }

    pub fn new_well_defined_error_wrapping_verify_runtime_error(e: RuntimeError) -> RuntimeError {
        match e {
            RuntimeError::VerifyError(inner) => {
                let line_file = inner.line_file.clone();
                let msg_for_well_defined = if inner.msg.is_empty() {
                    "verify fact error:".to_string()
                } else {
                    inner.msg.clone()
                };
                WellDefinedRuntimeError(RuntimeErrorStruct::new(
                    None,
                    msg_for_well_defined,
                    line_file,
                    Some(VerifyRuntimeError(inner).into()),
                ))
                .into()
            }
            _ => e,
        }
    }
}

// Display outputs a short placeholder; JSON: `display_runtime_error_json` in `crate::pipeline`.
impl fmt::Display for RuntimeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", "error")
    }
}

impl fmt::Display for RuntimeErrorStruct {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

impl std::error::Error for RuntimeErrorStruct {}

impl From<RuntimeErrorStruct> for RuntimeError {
    fn from(runtime_error_struct: RuntimeErrorStruct) -> Self {
        runtime_error_struct.into()
    }
}

fn boxed_previous_error(previous_error: Option<RuntimeError>) -> Option<Box<RuntimeError>> {
    previous_error.map(Box::new)
}