eqlog 0.8.0

Datalog with equality
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use crate::grammar_util::*;
use crate::source_display::*;
use lalrpop_util::{lexer::Token, ParseError};
use std::cmp::Ordering;
use std::error::Error;
use std::fmt::{self, Display};
use std::path::PathBuf;

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum SymbolKindEnum {
    Type,
    Pred,
    Func,
    Rule,
    Enum,
    Ctor,
}

impl Display for SymbolKindEnum {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use SymbolKindEnum::*;
        f.write_str(match self {
            Type => "type",
            Pred => "predicate",
            Func => "function",
            Rule => "rule",
            Enum => "enum",
            Ctor => "constructor",
        })
    }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum CompileError {
    InvalidToken {
        location: Location,
    },
    UnrecognizedEOF {
        location: Location,
        expected: Vec<String>,
    },
    UnrecognizedToken {
        location: Location,
        expected: Vec<String>,
    },
    ExtraToken {
        location: Location,
    },
    SymbolNotCamelCase {
        name: String,
        location: Location,
        symbol_kind: SymbolKindEnum,
    },
    SymbolNotSnakeCase {
        name: String,
        location: Location,
        symbol_kind: SymbolKindEnum,
    },
    VariableNotSnakeCase {
        name: String,
        location: Location,
    },
    VariableOccursOnlyOnce {
        name: String,
        location: Location,
    },
    FunctionArgumentNumber {
        expected: usize,
        got: usize,
        location: Location,
    },
    PredicateArgumentNumber {
        expected: usize,
        got: usize,
        location: Location,
    },
    UndeclaredSymbol {
        name: String,
        used_at: Location,
    },
    BadSymbolKind {
        name: String,
        expected: SymbolKindEnum,
        found: SymbolKindEnum,
        used_at: Location,
        declared_at: Location,
    },
    SymbolDeclaredTwice {
        name: String,
        first_declaration: Location,
        second_declaration: Location,
    },
    UndeterminedTermType {
        location: Location,
    },
    ConflictingTermType {
        types: Vec<String>,
        location: Location,
    },
    VariableIntroducedInThenStmt {
        location: Location,
    },
    WildcardInThenStmt {
        location: Location,
    },
    SurjectivityViolation {
        location: Location,
    },
    ThenDefinedNotVar {
        location: Location,
    },
    ThenDefinedVarNotNew {
        location: Location,
    },
    EnumCtorsNotSurjective {
        term_location: Location,
        enum_name: String,
        enum_location: Location,
    },
    MatchPatternIsVariable {
        location: Location,
    },
    MatchPatternIsWildcard {
        location: Location,
    },
    MatchPatternCtorArgIsApp {
        location: Location,
    },
    MatchPatternArgVarIsNotFresh {
        location: Location,
    },
    MatchConflictingEnum {
        match_stmt_location: Location,
        first_ctor_decl_location: Location,
        second_ctor_decl_location: Location,
    },
    MatchNotExhaustive {
        match_location: Location,
        missing_ctor_decl_location: Location,
    },
}

impl CompileError {
    pub fn primary_location(&self) -> Location {
        match self {
            CompileError::InvalidToken { location } => *location,
            CompileError::UnrecognizedEOF { location, .. } => *location,
            CompileError::UnrecognizedToken { location, .. } => *location,
            CompileError::ExtraToken { location } => *location,
            CompileError::SymbolNotCamelCase { location, .. } => *location,
            CompileError::SymbolNotSnakeCase { location, .. } => *location,
            CompileError::VariableNotSnakeCase { location, .. } => *location,
            CompileError::VariableOccursOnlyOnce { location, .. } => *location,
            CompileError::FunctionArgumentNumber { location, .. } => *location,
            CompileError::PredicateArgumentNumber { location, .. } => *location,
            CompileError::UndeclaredSymbol { used_at, .. } => *used_at,
            CompileError::BadSymbolKind { used_at, .. } => *used_at,
            CompileError::SymbolDeclaredTwice {
                second_declaration, ..
            } => *second_declaration,
            CompileError::UndeterminedTermType { location } => *location,
            CompileError::ConflictingTermType { location, .. } => *location,
            CompileError::VariableIntroducedInThenStmt { location } => *location,
            CompileError::WildcardInThenStmt { location } => *location,
            CompileError::SurjectivityViolation { location } => *location,
            CompileError::ThenDefinedNotVar { location } => *location,
            CompileError::ThenDefinedVarNotNew { location } => *location,
            CompileError::EnumCtorsNotSurjective {
                term_location,
                enum_name: _,
                enum_location: _,
            } => *term_location,
            CompileError::MatchPatternIsVariable { location } => *location,
            CompileError::MatchConflictingEnum {
                match_stmt_location,
                ..
            } => *match_stmt_location,
            CompileError::MatchPatternIsWildcard { location } => *location,
            CompileError::MatchPatternCtorArgIsApp { location } => *location,
            CompileError::MatchPatternArgVarIsNotFresh { location } => *location,
            CompileError::MatchNotExhaustive { match_location, .. } => *match_location,
        }
    }
}

/// [CompileError] is sorted according to *reverse* priority with respect to reporting:
/// A smaller [CompileError] should be reported preferably over a larger one.
///
/// TODO: This is confusing, should revert the order. It is currently this way to be compatible
/// with location ordering, where lower locations take precedence over greater ones.
impl Ord for CompileError {
    fn cmp(&self, other: &Self) -> Ordering {
        use CompileError::*;
        if self == other {
            return Ordering::Equal;
        }

        // An earlier (lower) location has higher priority.
        let loc_cmp = self.primary_location().1.cmp(&other.primary_location().1);

        // TODO: Far from complete. We probably want to group errors into e.g. parse errors, symbol
        // lookup errors, inference errors etc and define orders separately and then implement the
        // ordering hierarchically: First within a group, then among groups.
        match (self, other) {
            (UndeclaredSymbol { .. }, UndeclaredSymbol { .. }) => loc_cmp,
            (UndeclaredSymbol { .. }, _) => Ordering::Less,
            (SymbolDeclaredTwice { .. }, SymbolDeclaredTwice { .. }) => loc_cmp,
            (SymbolDeclaredTwice { .. }, _) => Ordering::Less,
            (BadSymbolKind { .. }, BadSymbolKind { .. }) => loc_cmp,
            (BadSymbolKind { .. }, _) => Ordering::Less,
            (UndeterminedTermType { .. }, UndeterminedTermType { .. }) => loc_cmp,
            // TODO: Is that what we want necessarily? This is here because when there are
            // conflicting enum constructors in a match statement, then we necessarily infer
            // different types for the discriminee (it must be equal to both enums). The root cause
            // is the conflicting enum constructors though, so it's better to report that one. But
            // if the ConflictingTermType is not the discriminee but something unrelated, then it'd
            // be better to compare by location.
            (ConflictingTermType { .. }, MatchConflictingEnum { .. }) => Ordering::Greater,
            (_, _) => loc_cmp,
        }
    }
}

impl PartialOrd for CompileError {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<'a> From<ParseError<usize, Token<'a>, CompileError>> for CompileError {
    fn from(parse_error: ParseError<usize, Token<'_>, CompileError>) -> CompileError {
        match parse_error {
            ParseError::InvalidToken { location } => CompileError::InvalidToken {
                location: Location(location, location + 1),
            },
            ParseError::UnrecognizedEof { location, expected } => CompileError::UnrecognizedEOF {
                location: Location(location, location + 1),
                expected,
            },
            ParseError::UnrecognizedToken { token, expected } => CompileError::UnrecognizedToken {
                location: Location(token.0, token.2),
                expected,
            },
            ParseError::ExtraToken { token } => CompileError::ExtraToken {
                location: Location(token.0, token.2),
            },
            ParseError::User { error } => error,
        }
    }
}

impl<'a> From<ParseError<usize, Token<'a>, NeverType>> for CompileError {
    fn from(parse_error: ParseError<usize, Token<'_>, NeverType>) -> CompileError {
        match parse_error {
            ParseError::InvalidToken { location } => CompileError::InvalidToken {
                location: Location(location, location + 1),
            },
            ParseError::UnrecognizedEof { location, expected } => CompileError::UnrecognizedEOF {
                location: Location(location, location + 1),
                expected,
            },
            ParseError::UnrecognizedToken { token, expected } => CompileError::UnrecognizedToken {
                location: Location(token.0, token.2),
                expected,
            },
            ParseError::ExtraToken { token } => CompileError::ExtraToken {
                location: Location(token.0, token.2),
            },
            ParseError::User { error } => match error {},
        }
    }
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct CompileErrorWithContext {
    pub error: CompileError,
    pub source_path: PathBuf,
    pub source: String,
}

impl Display for CompileErrorWithContext {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use CompileError::*;
        let CompileErrorWithContext {
            error,
            source_path,
            source,
        } = self;
        let write_loc = |f: &mut fmt::Formatter, loc: Location| -> fmt::Result {
            let src_displ = SourceDisplay {
                source_path: Some(source_path),
                underlined: true,
                ..SourceDisplay::new(source, loc)
            };
            write!(f, "{}", src_displ)?;
            Ok(())
        };
        write!(f, "Error: ")?;
        match error {
            InvalidToken { location } => {
                write!(f, "invalid token\n")?;
                write_loc(f, *location)?;
            }
            UnrecognizedEOF {
                location,
                expected: _,
            } => {
                write!(f, "unexpected end of file\n")?;
                write_loc(f, *location)?;
            }
            UnrecognizedToken {
                location,
                expected: _,
            } => {
                write!(f, "unrecognized token\n")?;
                write_loc(f, *location)?;
            }
            ExtraToken { location } => {
                write!(f, "unexpected token\n")?;
                write_loc(f, *location)?;
            }
            SymbolNotCamelCase {
                name,
                location,
                symbol_kind,
            } => {
                write!(f, "{symbol_kind} \"{name}\" is not UpperCamelCase\n")?;
                write_loc(f, *location)?;
            }
            SymbolNotSnakeCase {
                name,
                location,
                symbol_kind,
            } => {
                write!(f, "{symbol_kind} \"{name}\" is not lower_snake_case\n")?;
                write_loc(f, *location)?;
            }
            VariableNotSnakeCase { name, location } => {
                write!(f, "variable \"{name}\" is not lower_snake_case\n")?;
                write_loc(f, *location)?;
            }
            VariableOccursOnlyOnce { name, location } => {
                write!(f, "variable \"{name}\" occurs only once\n")?;
                write_loc(f, *location)?;
            }
            FunctionArgumentNumber {
                expected,
                got,
                location,
            } => {
                write!(
                    f,
                    "function takes {expected} arguments but {got} were supplied\n"
                )?;
                write_loc(f, *location)?;
            }
            PredicateArgumentNumber {
                expected,
                got,
                location,
            } => {
                write!(
                    f,
                    "predicate takes {expected} arguments but {got} were supplied\n"
                )?;
                write_loc(f, *location)?;
            }
            UndeclaredSymbol { name, used_at } => {
                write!(f, "undeclared symbol \"{name}\"\n")?;
                write_loc(f, *used_at)?;
            }
            BadSymbolKind {
                name,
                expected,
                found,
                used_at,
                declared_at,
            } => {
                write!(f, "expected {expected}, found {found} \"{name}\"\n")?;
                write_loc(f, *used_at)?;
                write!(f, "\"{name}\" declared as {found} here:\n")?;
                write_loc(f, *declared_at)?;
            }
            SymbolDeclaredTwice {
                name: _,
                first_declaration,
                second_declaration,
            } => {
                write!(f, "symbol declared multiple times\n")?;
                write_loc(f, *second_declaration)?;
                write!(f, "Previously declared here:\n")?;
                write_loc(f, *first_declaration)?;
            }
            UndeterminedTermType { location } => {
                write!(f, "sort of term undetermined\n")?;
                write_loc(f, *location)?;
            }
            ConflictingTermType { types: _, location } => {
                write!(f, "term has conflicting types\n")?;
                write_loc(f, *location)?;
            }
            VariableIntroducedInThenStmt { location } => {
                write!(f, "variable introduced in then statement\n")?;
                write_loc(f, *location)?;
            }
            WildcardInThenStmt { location } => {
                write!(f, "wildcards must not appear in then statements\n")?;
                write_loc(f, *location)?;
            }
            SurjectivityViolation { location } => {
                write!(f, "term does not appear earlier in this rule\n")?;
                write_loc(f, *location)?;
            }
            ThenDefinedNotVar { location } => {
                write!(f, "expected a variable\n")?;
                write_loc(f, *location)?;
            }
            ThenDefinedVarNotNew { location } => {
                write!(f, "variable has already been introduced earlier\n")?;
                write_loc(f, *location)?;
            }
            EnumCtorsNotSurjective {
                term_location,
                enum_name,
                enum_location,
            } => {
                write!(
                    f,
                    "term of enum type \"{enum_name}\" is not introduced with constructor \n"
                )?;
                write_loc(f, *term_location)?;
                write!(f, "Enum \"{enum_name}\" declared here:\n")?;
                write_loc(f, *enum_location)?;
            }
            MatchPatternIsVariable { location } => {
                write!(f, "Pattern is a variable\n")?;
                write_loc(f, *location)?;
                write!(
                    f,
                    "Patterns must be given by constructors of an enum type\n"
                )?;
            }
            MatchPatternIsWildcard { location } => {
                write!(f, "Pattern is a wildcard\n")?;
                write_loc(f, *location)?;
                write!(
                    f,
                    "Patterns must be given by constructors of an enum type\n"
                )?;
            }
            MatchPatternCtorArgIsApp { location } => {
                write!(f, "Nested patterns are not supported yet\n")?;
                write_loc(f, *location)?;
                write!(f, "Only variables may occur as arguments\n")?;
            }
            MatchPatternArgVarIsNotFresh { location } => {
                write!(f, "Variable in pattern has been used before\n")?;
                write_loc(f, *location)?;
                write!(f, "Variables in patterns must be fresh\n")?;
            }
            MatchConflictingEnum {
                match_stmt_location,
                first_ctor_decl_location,
                second_ctor_decl_location,
            } => {
                write!(f, "Conflicting pattern types\n")?;
                write_loc(f, *match_stmt_location)?;
                write!(f, "Constructors belong to different enums:\n")?;
                write_loc(f, *first_ctor_decl_location)?;
                write_loc(f, *second_ctor_decl_location)?;
            }
            MatchNotExhaustive {
                match_location,
                missing_ctor_decl_location,
            } => {
                write!(f, "Missing match case:\n")?;
                write_loc(f, *match_location)?;
                write!(f, "This constructor is not covered:\n")?;
                write_loc(f, *missing_ctor_decl_location)?;
            }
        }
        Ok(())
    }
}

impl Error for CompileErrorWithContext {}