claimr 0.1.0

Claimr — a constraint logic programming language, Prolog III inspired, parsed with a rustemo-generated LR parser.
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
// SPDX-License-Identifier: Apache-2.0

//! The interactive loop (design: `docs/design/2026-08-18-repl-interaction-model.md`).
//!
//! `claimr> ` prompt; input is exactly file syntax — a clause terminated by
//! `.` (multi-line), `?- …` being a query. Queries are answered Prolog-style —
//! first answer, then `;` for the next, Enter/`.`/`q` to stop; other clauses
//! typed at the prompt extend the session; `:` commands load,
//! list, clear and configure; Ctrl-C interrupts a running query. A pipe on
//! stdin drives the same loop without line editing (prompts suppressed, the
//! query echoed), which is how it is tested.

use std::io::{self, BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use claimr::{parse_program_spanned, Clause, EvalError, ParseError, Program};
use rustyline::error::ReadlineError;
use rustyline::DefaultEditor;

/// Where a session clause came from.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Origin {
    File(PathBuf),
    Prompt,
}

/// The session: the program being built, and how to read input.
pub struct Repl {
    clauses: Vec<(Clause, Origin)>,
    loaded: Vec<PathBuf>,
    program: Program,
    all_mode: bool,
    limit: Option<usize>,
    interrupt: Arc<AtomicBool>,
    input: Input,
    interactive: bool,
    /// Whether the stepping hint has been shown this session.
    hinted: bool,
}

enum Input {
    Editor(Box<DefaultEditor>),
    Pipe(io::StdinLock<'static>),
}

/// What one round of reading produced.
enum Read {
    Line(String),
    Interrupted,
    Eof,
}

const HELP: &str = "\
Enter claimr syntax ending in `.`: `?- goals.` is answered, anything else
(fact, rule, `{ … }.`, `{ … } => head.`) is added to the session.
After an answer: `;` then Enter for the next, Enter or `.` to stop.
  :load FILE   append FILE's clauses to the session and answer its queries
  :reload      re-read the loaded files, dropping clauses typed at the prompt
  :list        print the session's clauses
  :clear       empty the session
  :limit N     cap answers per query in :all mode (0 = unlimited)
  :all         toggle between stepping answers and printing them all
  :help        this text
  :quit        leave (also `exit.`, `halt.`, Ctrl-D, or Ctrl-C twice at an empty prompt)
Ctrl-C interrupts a running query.";

impl Repl {
    /// A REPL with an empty session. `limit` seeds `:limit`.
    pub fn new(limit: Option<usize>) -> Result<Self, String> {
        let interactive = io::stdin().is_terminal();
        let input = if interactive {
            let editor = DefaultEditor::new().map_err(|e| format!("cannot start line editor: {e}"))?;
            Input::Editor(Box::new(editor))
        } else {
            Input::Pipe(io::stdin().lock())
        };
        let interrupt = Arc::new(AtomicBool::new(false));
        {
            let flag = interrupt.clone();
            // A second registration (tests spawn processes, not threads) is
            // not an error worth stopping for.
            let _ = ctrlc::set_handler(move || flag.store(true, Ordering::Relaxed));
        }
        Ok(Repl {
            clauses: Vec::new(),
            loaded: Vec::new(),
            program: Program::compile(&[]).expect("empty program compiles"),
            all_mode: false,
            limit,
            interrupt,
            input,
            interactive,
            hinted: false,
        })
    }

    /// Load a file into the session and answer its queries (as `-i` does).
    pub fn load(&mut self, path: &Path) -> bool {
        let source = match std::fs::read_to_string(path) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("claimr: cannot read {}: {e}", path.display());
                return false;
            }
        };
        let clauses = match parse_program_spanned(&source) {
            Ok(c) => c,
            Err(e) => {
                eprintln!("{}:{e}", path.display());
                return false;
            }
        };
        let saved = self.clauses.clone();
        let mut queries = Vec::new();
        for (clause, _) in clauses {
            if matches!(clause, Clause::Query(_)) {
                queries.push(clause);
            } else {
                self.clauses.push((clause, Origin::File(path.to_path_buf())));
            }
        }
        if let Err(e) = self.recompile() {
            eprintln!("{}: {e}", path.display());
            self.clauses = saved;
            self.recompile().expect("previous session compiled");
            return false;
        }
        if !self.loaded.contains(&path.to_path_buf()) {
            self.loaded.push(path.to_path_buf());
        }
        for q in queries {
            println!("{q}");
            self.answer(&q, true);
        }
        true
    }

    fn recompile(&mut self) -> Result<(), EvalError> {
        let clauses: Vec<Clause> = self.clauses.iter().map(|(c, _)| c.clone()).collect();
        self.program = Program::compile(&clauses)?;
        Ok(())
    }

    /// Run the loop until `:quit` or end of input.
    pub fn run(&mut self) {
        if self.interactive {
            println!("claimr {} — :help for commands; :quit, exit. or Ctrl-D to leave.", env!("CARGO_PKG_VERSION"));
        }
        let mut buffer = String::new();
        let mut interrupted_at_prompt = false;
        loop {
            let prompt = if buffer.is_empty() { "claimr> " } else { "    ... " };
            match self.read_line(prompt) {
                Read::Eof => {
                    if self.interactive {
                        println!();
                    }
                    return;
                }
                Read::Interrupted => {
                    self.interrupt.store(false, Ordering::Relaxed);
                    if !buffer.is_empty() {
                        buffer.clear();
                        println!("(input discarded)");
                        interrupted_at_prompt = false;
                    } else if interrupted_at_prompt {
                        // Second Ctrl-C in a row at an empty prompt: leave.
                        return;
                    } else {
                        println!("(Ctrl-C interrupts a running query; to leave: :quit, exit. or Ctrl-D — or Ctrl-C again)");
                        interrupted_at_prompt = true;
                    }
                    continue;
                }
                Read::Line(line) => {
                    interrupted_at_prompt = false;
                    if buffer.is_empty() {
                        let trimmed = line.trim();
                        if trimmed.is_empty() || trimmed.starts_with('%') {
                            continue;
                        }
                        if let Some(cmd) = trimmed.strip_prefix(':') {
                            if !self.command(cmd) {
                                return;
                            }
                            continue;
                        }
                        // The words people reach for to leave, with or without a `.`.
                        if matches!(trimmed.trim_end_matches('.').trim(), "exit" | "quit" | "halt") {
                            return;
                        }
                    }
                    buffer.push_str(&line);
                    buffer.push('\n');
                    match completeness(&buffer) {
                        Completeness::Incomplete => continue,
                        Completeness::Error(e) => {
                            eprintln!("error: {e}");
                            buffer.clear();
                        }
                        Completeness::Complete(clauses) => {
                            buffer.clear();
                            for clause in clauses {
                                self.handle_clause(clause);
                            }
                        }
                    }
                }
            }
        }
    }

    fn read_line(&mut self, prompt: &str) -> Read {
        match &mut self.input {
            Input::Editor(ed) => match ed.readline(prompt) {
                Ok(line) => {
                    let _ = ed.add_history_entry(line.as_str());
                    Read::Line(line)
                }
                Err(ReadlineError::Interrupted) => Read::Interrupted,
                Err(ReadlineError::Eof) => Read::Eof,
                Err(e) => {
                    eprintln!("input error: {e}");
                    Read::Eof
                }
            },
            Input::Pipe(stdin) => {
                let mut line = String::new();
                match stdin.read_line(&mut line) {
                    Ok(0) => Read::Eof,
                    Ok(_) => Read::Line(line.trim_end_matches(['\n', '\r']).to_string()),
                    Err(_) => Read::Eof,
                }
            }
        }
    }

    /// Dispatch a `:` command. Returns false to leave the loop.
    fn command(&mut self, cmd: &str) -> bool {
        let mut parts = cmd.splitn(2, char::is_whitespace);
        let name = parts.next().unwrap_or("");
        let arg = parts.next().map(str::trim).unwrap_or("");
        match name {
            "quit" | "q" | "exit" => return false,
            "help" | "h" | "?" => println!("{HELP}"),
            "load" | "l" => {
                if arg.is_empty() {
                    eprintln!("usage: :load FILE");
                } else {
                    self.load(Path::new(arg));
                }
            }
            "reload" | "r" => {
                let files = std::mem::take(&mut self.loaded);
                self.clauses.clear();
                self.recompile().expect("empty program compiles");
                for f in files {
                    self.load(&f);
                }
            }
            "list" => {
                let mut ordered: Vec<&(Clause, Origin)> = self.clauses.iter().collect();
                ordered.sort_by_key(|(_, o)| matches!(o, Origin::Prompt));
                for (c, _) in ordered {
                    println!("{c}");
                }
            }
            "clear" => {
                self.clauses.clear();
                self.loaded.clear();
                self.recompile().expect("empty program compiles");
            }
            "limit" => match arg.parse::<usize>() {
                Ok(0) => self.limit = None,
                Ok(n) => self.limit = Some(n),
                Err(_) => eprintln!("usage: :limit N  (0 = unlimited)"),
            },
            "all" => {
                self.all_mode = !self.all_mode;
                println!("{}", if self.all_mode { "printing all answers" } else { "stepping answers" });
            }
            _ => eprintln!("unknown command `:{name}` — :help for the list"),
        }
        true
    }

    /// A complete clause typed at the prompt: answer a query, extend the
    /// session with anything else.
    fn handle_clause(&mut self, clause: Clause) {
        if matches!(clause, Clause::Query(_)) {
            if !self.interactive {
                println!("{clause}");
            }
            let all = self.all_mode;
            self.answer(&clause, all);
            return;
        }
        self.clauses.push((clause, Origin::Prompt));
        if let Err(e) = self.recompile() {
            eprintln!("error: {e}");
            self.clauses.pop();
            self.recompile().expect("previous session compiled");
        }
    }

    /// Answer a query against the session; stepping unless `all`.
    fn answer(&mut self, query: &Clause, all: bool) {
        // Compile the session plus this query (programs are small).
        let mut clauses: Vec<Clause> = self.clauses.iter().map(|(c, _)| c.clone()).collect();
        clauses.push(query.clone());
        let program = match Program::compile(&clauses) {
            Ok(p) => p,
            Err(e) => {
                eprintln!("error: {e}");
                return;
            }
        };
        let q = program.queries().last().expect("the query").clone();
        self.interrupt.store(false, Ordering::Relaxed);
        let mut sols = program.solve(&q).with_interrupt(self.interrupt.clone());
        let mut count = 0usize;
        loop {
            match sols.next() {
                Some(a) => {
                    count += 1;
                    if all {
                        println!("{a}");
                        if self.limit.is_some_and(|l| count >= l) {
                            break;
                        }
                        continue;
                    }
                    if !sols.may_continue() {
                        println!("{a}.");
                        break;
                    }
                    // More answers may follow: wait for `;` (next) or Enter (stop).
                    // On a terminal the user's keystrokes are echoed, so the line
                    // ends up reading `W = socrates ;` by itself; on a pipe we
                    // print the markers.
                    print!("{a}");
                    if self.interactive && !self.hinted {
                        print!("   (more may follow: type ; then Enter for the next, Enter alone to stop)");
                        self.hinted = true;
                    }
                    let _ = io::stdout().flush();
                    match self.read_line(" ") {
                        Read::Line(l) if matches!(l.trim(), ";" | "n" | "next") => {
                            if !self.interactive {
                                println!(" ;");
                            }
                        }
                        Read::Interrupted => {
                            if !self.interactive {
                                println!(".");
                            }
                            self.interrupt.store(false, Ordering::Relaxed);
                            break;
                        }
                        _ => {
                            if !self.interactive {
                                println!(".");
                            }
                            break;
                        }
                    }
                }
                None => {
                    if sols.interrupted() {
                        println!("interrupted.");
                        self.interrupt.store(false, Ordering::Relaxed);
                    } else if let Some(e) = sols.error() {
                        eprintln!("in `{}`: {e}", q.text());
                    } else if count == 0 || !all {
                        println!("false.");
                    }
                    break;
                }
            }
        }
    }
}

/// Is the accumulated input a complete clause (or several), incomplete, or
/// wrong? Incompleteness is a syntax error at the very end of the input.
enum Completeness {
    Complete(Vec<Clause>),
    Incomplete,
    Error(ParseError),
}

fn completeness(buffer: &str) -> Completeness {
    match parse_program_spanned(buffer) {
        Ok(cs) => Completeness::Complete(cs.into_iter().map(|(c, _)| c).collect()),
        Err(e) => {
            let at_end = e.offset.is_some_and(|o| o >= buffer.trim_end().len());
            if at_end { Completeness::Incomplete } else { Completeness::Error(e) }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn completeness_detection() {
        assert!(matches!(completeness("human(socrates)."), Completeness::Complete(_)));
        assert!(matches!(completeness("mortal(X) :- human(X)"), Completeness::Incomplete));
        assert!(matches!(completeness("mortal(X) :-\n"), Completeness::Incomplete));
        assert!(matches!(completeness("?- p(X)"), Completeness::Incomplete));
        assert!(matches!(completeness("?- { X = 1.5 }."), Completeness::Complete(_)));
        assert!(matches!(completeness("?- { X = 1 }. % done"), Completeness::Complete(_)));
        assert!(matches!(completeness("?- p(X) q."), Completeness::Error(_)));
        assert!(matches!(completeness("{ age(x) > 3 }."), Completeness::Complete(_)));
        // `1.` at the end of a query is a number then the terminator.
        assert!(matches!(completeness("?- { X = 1 }."), Completeness::Complete(_)));
        assert!(matches!(completeness("?- p(1"), Completeness::Incomplete));
    }

    #[test]
    fn file_syntax_at_the_prompt() {
        let Completeness::Complete(cs) = completeness("?- p(X), q(X).") else { panic!() };
        assert!(matches!(cs[0], Clause::Query(_)));
        let Completeness::Complete(cs) = completeness("p(a).") else { panic!() };
        assert!(matches!(cs[0], Clause::Fact(_)));
        let Completeness::Complete(cs) = completeness("p(X) :- q(X).") else { panic!() };
        assert!(matches!(cs[0], Clause::Rule { .. }));
    }
}