Skip to main content

logicaffeine_language/
pragmatics.rs

1//! Post-parse pragmatic inference transformations.
2//!
3//! This module applies pragmatic rules to the parsed logical representation,
4//! converting indirect speech acts to their illocutionary force:
5//!
6//! - Polite requests ("Can you X?") → Imperatives
7//! - Permission modals with addressee agent → Imperatives
8//!
9//! These transformations capture the pragmatic insight that modal questions
10//! directed at the addressee often function as requests, not information-seeking.
11
12use crate::ast::{LogicExpr, ModalDomain, Term, ThematicRole};
13use logicaffeine_base::Arena;
14use logicaffeine_base::Interner;
15
16/// Apply pragmatic transformations to convert indirect speech acts.
17pub fn apply_pragmatics<'a>(
18    expr: &'a LogicExpr<'a>,
19    expr_arena: &'a Arena<LogicExpr<'a>>,
20    interner: &Interner,
21) -> &'a LogicExpr<'a> {
22    match expr {
23        LogicExpr::Modal { vector, operand } => {
24            if vector.domain == ModalDomain::Alethic && vector.force > 0.0 && vector.force < 1.0 {
25                if is_addressee_agent(operand, interner) {
26                    return expr_arena.alloc(LogicExpr::Imperative { action: *operand });
27                }
28            }
29            expr
30        }
31        LogicExpr::Question { body, .. } => {
32            if let LogicExpr::Modal { vector, operand } = body {
33                if vector.domain == ModalDomain::Alethic && vector.force > 0.0 && vector.force < 1.0
34                {
35                    if is_addressee_agent(operand, interner) {
36                        return expr_arena.alloc(LogicExpr::Imperative { action: *operand });
37                    }
38                }
39            }
40            expr
41        }
42        LogicExpr::YesNoQuestion { body } => {
43            if let LogicExpr::Modal { vector, operand } = body {
44                if vector.domain == ModalDomain::Alethic && vector.force > 0.0 && vector.force < 1.0
45                {
46                    if is_addressee_agent(operand, interner) {
47                        return expr_arena.alloc(LogicExpr::Imperative { action: *operand });
48                    }
49                }
50            }
51            expr
52        }
53        _ => expr,
54    }
55}
56
57fn is_addressee_agent(expr: &LogicExpr, interner: &Interner) -> bool {
58    match expr {
59        LogicExpr::NeoEvent(data) => {
60            for (role, term) in data.roles.iter() {
61                if *role == ThematicRole::Agent {
62                    if let Term::Constant(sym) = term {
63                        let name = interner.resolve(*sym);
64                        if name == "Addressee" {
65                            return true;
66                        }
67                    }
68                }
69            }
70            false
71        }
72        LogicExpr::Predicate { args, .. } => {
73            if let Some(Term::Constant(sym)) = args.first() {
74                let name = interner.resolve(*sym);
75                return name == "Addressee";
76            }
77            false
78        }
79        _ => false,
80    }
81}