formal-ai 0.199.0

Formal symbolic AI implementation with OpenAI-compatible APIs
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Finite propositional decision procedure.

use std::collections::{BTreeMap, BTreeSet};

use super::sat::{CnfFormula, Literal, SatOutcome};
use crate::proof_engine::types::{Proof, ProofMethod, ProofOutcome, ProofStep, StepKind};

/// Largest variable count the exhaustive truth-table audit will enumerate.
/// Up to this width the procedure renders every row of the table as its
/// witness; beyond it the model space is too large to print, so the claim is
/// delegated to the DPLL satisfiability backend instead.
const TRUTH_TABLE_VARIABLE_LIMIT: usize = 8;

/// Largest variable count the DPLL fallback will accept before declining the
/// claim. This keeps the worst-case search bounded and the engine
/// deterministic; wider formulas fall through to the generic proof planner.
const MAX_SAT_VARIABLES: usize = 20;

enum BoolExpr {
    Var(String),
    Not(Box<Self>),
    And(Box<Self>, Box<Self>),
    Or(Box<Self>, Box<Self>),
    Implies(Box<Self>, Box<Self>),
}

impl BoolExpr {
    fn variables(&self, output: &mut BTreeSet<String>) {
        match self {
            Self::Var(name) => {
                output.insert(name.clone());
            }
            Self::Not(inner) => inner.variables(output),
            Self::And(left, right) | Self::Or(left, right) | Self::Implies(left, right) => {
                left.variables(output);
                right.variables(output);
            }
        }
    }

    fn evaluate(&self, assignment: &BTreeMap<String, bool>) -> bool {
        match self {
            Self::Var(name) => assignment.get(name).copied().unwrap_or(false),
            Self::Not(inner) => !inner.evaluate(assignment),
            Self::And(left, right) => left.evaluate(assignment) && right.evaluate(assignment),
            Self::Or(left, right) => left.evaluate(assignment) || right.evaluate(assignment),
            Self::Implies(left, right) => !left.evaluate(assignment) || right.evaluate(assignment),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum BoolToken {
    Var(String),
    Not,
    And,
    Or,
    Implies,
    LParen,
    RParen,
}

pub(super) fn attempt_boolean_claim(claim: &str, _language: &str) -> Option<ProofOutcome> {
    let rewritten = rewrite_boolean_if_then(claim);
    let tokens = tokenize_boolean(&rewritten)?;
    let expression = BoolParser::new(tokens).parse()?;
    let mut variables = BTreeSet::new();
    expression.variables(&mut variables);
    if variables.is_empty() {
        return None;
    }
    let variable_list = variables.into_iter().collect::<Vec<_>>();
    if variable_list.len() > TRUTH_TABLE_VARIABLE_LIMIT {
        return attempt_boolean_via_sat(&expression, &variable_list);
    }
    let mut rows = Vec::new();
    let mut first_false = None;
    for mask in 0..(1usize << variable_list.len()) {
        let assignment = variable_list
            .iter()
            .enumerate()
            .map(|(index, name)| (name.clone(), (mask & (1usize << index)) != 0))
            .collect::<BTreeMap<_, _>>();
        let value = expression.evaluate(&assignment);
        if !value && first_false.is_none() {
            first_false = Some(assignment.clone());
        }
        rows.push((assignment, value));
    }
    let formula = format_bool_expr(&expression);
    if let Some(counterexample) = first_false {
        return Some(ProofOutcome::Disproven {
            counterexample: format!(
                "{} makes {formula} false.",
                format_bool_assignment(&counterexample)
            ),
            method: ProofMethod::DecisionProcedure,
            partial_proof: Some(boolean_disproof(&formula, &rows, &counterexample)),
        });
    }
    Some(ProofOutcome::Proven {
        proof: boolean_tautology_proof(&formula, &rows),
    })
}

/// Discharge a wide propositional claim through the DPLL satisfiability
/// backend instead of an exhaustive truth table.
///
/// The goal `F` is a tautology exactly when its negation `¬F` is
/// unsatisfiable, so the formula is Tseitin-encoded into CNF, the root gate is
/// asserted false, and the result is handed to [`CnfFormula::solve`]. An
/// `Unsatisfiable` answer proves the tautology; a `Satisfiable` answer hands
/// back the named-variable assignment as a printed countermodel.
fn attempt_boolean_via_sat(
    expression: &BoolExpr,
    variable_list: &[String],
) -> Option<ProofOutcome> {
    if variable_list.len() > MAX_SAT_VARIABLES {
        return None;
    }
    let variable_index = variable_list
        .iter()
        .enumerate()
        .map(|(index, name)| (name.as_str(), index))
        .collect::<BTreeMap<_, _>>();
    let mut encoder = TseitinEncoder::new(variable_list.len(), &variable_index);
    let root = encoder.encode(expression);
    // Search for a model of `¬F`: assert the formula's root gate is false.
    encoder.assert_literal(root.negated());
    let formula = format_bool_expr(expression);
    let cnf = encoder.into_formula();
    let clause_count = cnf.clauses().len();
    let variable_count = variable_list.len();
    match cnf.solve() {
        SatOutcome::Unsatisfiable => Some(ProofOutcome::Proven {
            proof: sat_tautology_proof(&formula, variable_count, clause_count),
        }),
        SatOutcome::Satisfiable(model) => {
            let counterexample = decode_named_model(variable_list, &model);
            Some(ProofOutcome::Disproven {
                counterexample: format!(
                    "{} makes {formula} false.",
                    format_bool_assignment(&counterexample)
                ),
                method: ProofMethod::DecisionProcedure,
                partial_proof: Some(sat_disproof(
                    &formula,
                    &counterexample,
                    variable_count,
                    clause_count,
                )),
            })
        }
    }
}

/// Project a CNF model back onto the named propositional variables, dropping
/// the Tseitin auxiliary gates that occupy the higher indices.
fn decode_named_model(variable_list: &[String], model: &[bool]) -> BTreeMap<String, bool> {
    variable_list
        .iter()
        .enumerate()
        .map(|(index, name)| (name.clone(), model[index]))
        .collect()
}

/// A Tseitin transformer from a [`BoolExpr`] into an equisatisfiable CNF.
///
/// Named variables keep their caller-assigned indices; every binary gate gets
/// a fresh auxiliary variable defined by the standard three-clause Tseitin
/// pattern. Negation is folded directly into literal polarity, so `not` never
/// allocates a gate.
struct TseitinEncoder<'a> {
    variable_index: &'a BTreeMap<&'a str, usize>,
    next_variable: usize,
    clauses: Vec<Vec<Literal>>,
}

impl<'a> TseitinEncoder<'a> {
    const fn new(named_count: usize, variable_index: &'a BTreeMap<&'a str, usize>) -> Self {
        Self {
            variable_index,
            next_variable: named_count,
            clauses: Vec::new(),
        }
    }

    fn fresh_gate(&mut self) -> Literal {
        let variable = self.next_variable;
        self.next_variable += 1;
        Literal::positive(variable)
    }

    fn assert_literal(&mut self, literal: Literal) {
        self.clauses.push(vec![literal]);
    }

    /// Encode `expression`, returning the literal that represents its truth.
    fn encode(&mut self, expression: &BoolExpr) -> Literal {
        match expression {
            BoolExpr::Var(name) => Literal::positive(self.variable_index[name.as_str()]),
            BoolExpr::Not(inner) => self.encode(inner).negated(),
            BoolExpr::And(left, right) => {
                let a = self.encode(left);
                let b = self.encode(right);
                self.define_and(a, b)
            }
            BoolExpr::Or(left, right) => {
                let a = self.encode(left);
                let b = self.encode(right);
                self.define_or(a, b)
            }
            BoolExpr::Implies(left, right) => {
                // `a → b` is `¬a ∨ b`; fold the antecedent's negation into its
                // literal so the gate is a plain disjunction.
                let a = self.encode(left).negated();
                let b = self.encode(right);
                self.define_or(a, b)
            }
        }
    }

    fn define_and(&mut self, a: Literal, b: Literal) -> Literal {
        let gate = self.fresh_gate();
        // gate ↔ (a ∧ b)
        self.clauses.push(vec![gate.negated(), a]);
        self.clauses.push(vec![gate.negated(), b]);
        self.clauses.push(vec![gate, a.negated(), b.negated()]);
        gate
    }

    fn define_or(&mut self, a: Literal, b: Literal) -> Literal {
        let gate = self.fresh_gate();
        // gate ↔ (a ∨ b)
        self.clauses.push(vec![gate.negated(), a, b]);
        self.clauses.push(vec![gate, a.negated()]);
        self.clauses.push(vec![gate, b.negated()]);
        gate
    }

    fn into_formula(self) -> CnfFormula {
        let mut formula = CnfFormula::new(self.next_variable);
        for clause in self.clauses {
            formula.add_clause(clause);
        }
        formula
    }
}

fn sat_tautology_proof(formula: &str, variable_count: usize, clause_count: usize) -> Proof {
    Proof {
        statement: formula.to_owned(),
        steps: vec![
            ProofStep {
                kind: StepKind::Hypothesis,
                text: format!(
                    "Delegate the formula to the relative-meta-logic / SMT decision procedure. \
                     With {variable_count} variables an exhaustive truth table (2^{variable_count} \
                     rows) is infeasible, so the verified backend is a DPLL satisfiability search."
                ),
            },
            ProofStep {
                kind: StepKind::Definition,
                text: format!(
                    "Negate the goal and Tseitin-encode ¬({formula}) into conjunctive normal \
                     form: {clause_count} clauses over the propositional variables plus gate \
                     auxiliaries."
                ),
            },
            ProofStep {
                kind: StepKind::Inference,
                text: String::from(
                    "DPLL with unit propagation, pure-literal elimination, and backtracking \
                     finds the negation unsatisfiable.",
                ),
            },
        ],
        conclusion: format!(
            "Because ¬({formula}) is unsatisfiable, every assignment satisfies {formula}, \
             so it is a tautology. ∎"
        ),
        method: ProofMethod::DecisionProcedure,
    }
}

fn sat_disproof(
    formula: &str,
    counterexample: &BTreeMap<String, bool>,
    variable_count: usize,
    clause_count: usize,
) -> Proof {
    Proof {
        statement: formula.to_owned(),
        steps: vec![
            ProofStep {
                kind: StepKind::Hypothesis,
                text: format!(
                    "Delegate the formula to the relative-meta-logic / SMT decision procedure. \
                     With {variable_count} variables a truth table is infeasible, so the verified \
                     backend is a DPLL satisfiability search."
                ),
            },
            ProofStep {
                kind: StepKind::Definition,
                text: format!(
                    "Tseitin-encode ¬({formula}) into {clause_count} CNF clauses and search for \
                     an assignment that makes the formula false."
                ),
            },
            ProofStep {
                kind: StepKind::Inference,
                text: format!(
                    "DPLL returns the satisfying assignment {}, a countermodel.",
                    format_bool_assignment(counterexample)
                ),
            },
        ],
        conclusion: format!("Therefore {formula} is not a tautology. ∎"),
        method: ProofMethod::DecisionProcedure,
    }
}

fn rewrite_boolean_if_then(claim: &str) -> String {
    if let Some(rest) = claim.strip_prefix("if ") {
        if let Some(index) = rest.find(" then ") {
            let premise = rest[..index].trim();
            let conclusion = rest[index + " then ".len()..].trim();
            return format!("({premise}) implies ({conclusion})");
        }
    }
    claim.to_owned()
}

fn tokenize_boolean(text: &str) -> Option<Vec<BoolToken>> {
    let mut tokens = Vec::new();
    let mut position = 0;
    while position < text.len() {
        let ch = text[position..].chars().next()?;
        if ch.is_whitespace() {
            position += ch.len_utf8();
            continue;
        }
        match ch {
            '(' => {
                tokens.push(BoolToken::LParen);
                position += 1;
            }
            ')' => {
                tokens.push(BoolToken::RParen);
                position += 1;
            }
            '!' | '¬' => {
                tokens.push(BoolToken::Not);
                position += ch.len_utf8();
            }
            '&' => {
                tokens.push(BoolToken::And);
                position += if text[position..].starts_with("&&") {
                    2
                } else {
                    1
                };
            }
            '|' => {
                tokens.push(BoolToken::Or);
                position += if text[position..].starts_with("||") {
                    2
                } else {
                    1
                };
            }
            '-' if text[position..].starts_with("->") => {
                tokens.push(BoolToken::Implies);
                position += 2;
            }
            '=' if text[position..].starts_with("=>") => {
                tokens.push(BoolToken::Implies);
                position += 2;
            }
            _ if ch.is_ascii_alphabetic() => {
                let start = position;
                position += ch.len_utf8();
                while position < text.len() {
                    let next = text[position..].chars().next()?;
                    if next.is_ascii_alphanumeric() || next == '_' {
                        position += next.len_utf8();
                    } else {
                        break;
                    }
                }
                let word = &text[start..position];
                tokens.push(match word {
                    "not" => BoolToken::Not,
                    "and" => BoolToken::And,
                    "or" => BoolToken::Or,
                    "implies" | "then" => BoolToken::Implies,
                    "if" => continue,
                    _ => BoolToken::Var(word.to_owned()),
                });
            }
            _ => return None,
        }
    }
    Some(tokens)
}

struct BoolParser {
    tokens: Vec<BoolToken>,
    position: usize,
}

impl BoolParser {
    const fn new(tokens: Vec<BoolToken>) -> Self {
        Self {
            tokens,
            position: 0,
        }
    }

    fn parse(mut self) -> Option<BoolExpr> {
        let expression = self.parse_implication()?;
        (self.position == self.tokens.len()).then_some(expression)
    }

    fn parse_implication(&mut self) -> Option<BoolExpr> {
        let left = self.parse_or()?;
        if self.consume(&BoolToken::Implies) {
            let right = self.parse_implication()?;
            return Some(BoolExpr::Implies(Box::new(left), Box::new(right)));
        }
        Some(left)
    }

    fn parse_or(&mut self) -> Option<BoolExpr> {
        let mut value = self.parse_and()?;
        while self.consume(&BoolToken::Or) {
            value = BoolExpr::Or(Box::new(value), Box::new(self.parse_and()?));
        }
        Some(value)
    }

    fn parse_and(&mut self) -> Option<BoolExpr> {
        let mut value = self.parse_not()?;
        while self.consume(&BoolToken::And) {
            value = BoolExpr::And(Box::new(value), Box::new(self.parse_not()?));
        }
        Some(value)
    }

    fn parse_not(&mut self) -> Option<BoolExpr> {
        if self.consume(&BoolToken::Not) {
            return Some(BoolExpr::Not(Box::new(self.parse_not()?)));
        }
        self.parse_primary()
    }

    fn parse_primary(&mut self) -> Option<BoolExpr> {
        match self.tokens.get(self.position)?.clone() {
            BoolToken::Var(name) => {
                self.position += 1;
                Some(BoolExpr::Var(name))
            }
            BoolToken::LParen => {
                self.position += 1;
                let expression = self.parse_implication()?;
                self.consume(&BoolToken::RParen).then_some(expression)
            }
            _ => None,
        }
    }

    fn consume(&mut self, expected: &BoolToken) -> bool {
        if self.tokens.get(self.position) == Some(expected) {
            self.position += 1;
            true
        } else {
            false
        }
    }
}

fn boolean_tautology_proof(formula: &str, rows: &[(BTreeMap<String, bool>, bool)]) -> Proof {
    Proof {
        statement: formula.to_owned(),
        steps: vec![
            ProofStep {
                kind: StepKind::Hypothesis,
                text: String::from(
                    "Delegate the formula to the relative-meta-logic / SMT decision procedure. \
                     For the propositional fragment, the verified backend is an exhaustive \
                     truth-table audit over every assignment.",
                ),
            },
            ProofStep {
                kind: StepKind::Definition,
                text: format!("Normalized formula: {formula}."),
            },
            ProofStep {
                kind: StepKind::Inference,
                text: format!("Truth table: {}.", format_truth_rows(rows)),
            },
        ],
        conclusion: format!(
            "Every assignment makes {formula} true, so the formula is a tautology. ∎"
        ),
        method: ProofMethod::DecisionProcedure,
    }
}

fn boolean_disproof(
    formula: &str,
    rows: &[(BTreeMap<String, bool>, bool)],
    counterexample: &BTreeMap<String, bool>,
) -> Proof {
    Proof {
        statement: formula.to_owned(),
        steps: vec![
            ProofStep {
                kind: StepKind::Hypothesis,
                text: String::from(
                    "Delegate the formula to the relative-meta-logic / SMT decision procedure \
                     and enumerate the finite Boolean model space.",
                ),
            },
            ProofStep {
                kind: StepKind::Inference,
                text: format!("Truth table: {}.", format_truth_rows(rows)),
            },
            ProofStep {
                kind: StepKind::Inference,
                text: format!(
                    "The assignment {} is a countermodel.",
                    format_bool_assignment(counterexample)
                ),
            },
        ],
        conclusion: format!("Therefore {formula} is not a tautology. ∎"),
        method: ProofMethod::DecisionProcedure,
    }
}

fn format_truth_rows(rows: &[(BTreeMap<String, bool>, bool)]) -> String {
    rows.iter()
        .map(|(assignment, value)| {
            format!(
                "{} -> {}",
                format_bool_assignment(assignment),
                if *value { "true" } else { "false" }
            )
        })
        .collect::<Vec<_>>()
        .join("; ")
}

fn format_bool_assignment(assignment: &BTreeMap<String, bool>) -> String {
    assignment
        .iter()
        .map(|(name, value)| format!("{name} = {}", if *value { "true" } else { "false" }))
        .collect::<Vec<_>>()
        .join(", ")
}

fn format_bool_expr(expression: &BoolExpr) -> String {
    format_bool_expr_prec(expression, 0)
}

fn format_bool_expr_prec(expression: &BoolExpr, parent_precedence: u8) -> String {
    let (precedence, rendered) = match expression {
        BoolExpr::Var(name) => (5, name.clone()),
        BoolExpr::Not(inner) => (4, format!("not {}", format_bool_expr_prec(inner, 4))),
        BoolExpr::And(left, right) => (
            3,
            format!(
                "{} and {}",
                format_bool_expr_prec(left, 3),
                format_bool_expr_prec(right, 3)
            ),
        ),
        BoolExpr::Or(left, right) => (
            2,
            format!(
                "{} or {}",
                format_bool_expr_prec(left, 2),
                format_bool_expr_prec(right, 2)
            ),
        ),
        BoolExpr::Implies(left, right) => (
            1,
            format!(
                "{} implies {}",
                format_bool_expr_prec(left, 2),
                format_bool_expr_prec(right, 1)
            ),
        ),
    };
    if precedence < parent_precedence {
        format!("({rendered})")
    } else {
        rendered
    }
}