Skip to main content

logicaffeine_web/ui/components/
proof_panel.rs

1//! The proof drawer for Logic mode: the Studio's bottom drawer, risen from the dock bar.
2//!
3//! The drawer docks below the panel row, on the Studio page and in a frame alike, and is dragged
4//! by the grip on its top edge the way the debugger's drawer is. Its bar names it and says how the
5//! proof stands ([`ProofStatus`]); its body is the derivation once there is one, drawn by the
6//! Studio's own tree (`components::tree`, the Files tree's and the Syntax Tree's): each step a row,
7//! its rule in a pill and its conclusion beside it, its premises its parts, down to the givens
8//! ([`ProofNode`]). Until there is one, the walkthrough: the three steps a first visit takes, each
9//! marked done, next, or still to come ([`Walkthrough`]). The one action is Prove, in the dock bar
10//! and nowhere else: the prover searches on its own (`auto_view`), so the panel offers no tactic a
11//! reader would have to choose.
12//!
13//! # Props
14//!
15//! - `proof_text` - Formatted derivation tree HTML
16//! - `status` - Current proof state
17//! - `steps` - What the reader has done so far, for the walkthrough
18//! - `height` - The drawer's height, which its grip changes
19//! - `on_prove` - Prove was pressed
20//! - `on_close` - The drawer's close was pressed
21
22use dioxus::prelude::*;
23
24use crate::ui::components::tree::{Tree, TreeNode};
25
26pub(crate) const PROOF_PANEL_STYLE: &str = r#"
27.proof-drawer {
28    display: flex;
29    flex-direction: column;
30    flex: 0 1 auto;
31    min-height: 44px;
32    overflow: hidden;
33    background: var(--studio-panel-bg, #12161c);
34    border-top: 1px solid var(--studio-border, rgba(255, 255, 255, 0.08));
35    color: var(--studio-text, #e8eaed);
36    font-size: 13px;
37}
38
39/* The grab bar is the column bars turned on their side: solid 8px, in their grey. */
40.proof-grip {
41    flex: none;
42    height: 8px;
43    background: var(--studio-resizer, #22272b);
44    cursor: row-resize;
45    touch-action: none;
46    transition: background 0.2s ease;
47}
48
49.proof-grip:hover,
50.proof-grip.active {
51    background: var(--studio-accent, #667eea);
52}
53
54/* Under a finger the grip takes a finger: its grab area reaches 18px past what it draws. */
55@media (pointer: coarse) {
56    .proof-grip { position: relative; }
57    .proof-grip::before { content: ""; position: absolute; left: 0; right: 0; top: -18px; bottom: -18px; z-index: 1; }
58}
59
60.proof-bar {
61    display: flex;
62    align-items: center;
63    gap: 12px;
64    min-height: 36px;
65    padding: 4px 12px;
66    border-bottom: 1px solid var(--studio-border, rgba(255, 255, 255, 0.08));
67}
68
69.proof-title {
70    font-weight: 600;
71}
72
73.proof-status {
74    font-size: 13px;
75    color: rgba(255, 255, 255, 0.7);
76}
77
78.proof-status.success { color: #98c379; }
79.proof-status.error { color: #e06c75; }
80.proof-status.pending { color: #e5c07b; }
81.proof-status.uncertified { color: #e5c07b; }
82
83.proof-bar-space { flex: 1 1 auto; }
84
85.proof-close {
86    padding: 3px 8px;
87    border: 1px solid var(--studio-border, rgba(255, 255, 255, 0.08));
88    border-radius: 6px;
89    background: rgba(255, 255, 255, 0.05);
90    color: var(--studio-text, #e8eaed);
91    font-size: 13px;
92    line-height: 1;
93    cursor: pointer;
94}
95
96.proof-close:hover { background: rgba(255, 255, 255, 0.1); }
97
98.proof-body {
99    flex: 1 1 auto;
100    min-height: 0;
101    overflow: auto;
102}
103
104/* The derivation, as the Studio's trees are drawn: a note on how to read it, then the rows. The
105   tree runs the drawer's whole width, so its first line stands one step from the edge. */
106.proof-derivation {
107    padding: 12px var(--studio-inset, 16px) 0;
108}
109
110.proof-tree-note {
111    margin: 0;
112    font-size: 13px;
113    line-height: 1.5;
114    color: var(--studio-text-secondary, rgba(255, 255, 255, 0.6));
115}
116
117.proof-tree {
118    --tree-pad-top: 10px;
119    --tree-pad-bottom: 12px;
120    margin: 0 calc(-1 * var(--studio-inset, 16px));
121    overflow-x: auto;
122}
123
124/* A step's rule, a pill as the Syntax Tree's kinds are: the word on a tint of its colour. */
125.proof-rule {
126    flex: none;
127    margin-right: 8px;
128    padding: 2px 6px;
129    border-radius: 4px;
130    font-size: 13px;
131    line-height: 16px;
132    white-space: nowrap;
133    color: var(--studio-accent, #667eea);
134    background: color-mix(in srgb, var(--studio-accent, #667eea) 18%, transparent);
135}
136
137.proof-rule.given {
138    color: #98c379;
139    background: color-mix(in srgb, #98c379 18%, transparent);
140}
141
142.proof-rule-detail {
143    flex: none;
144    margin-right: 8px;
145    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
146    font-size: 13px;
147    color: var(--studio-text-secondary, rgba(255, 255, 255, 0.6));
148    white-space: nowrap;
149}
150
151.proof-conclusion {
152    flex: none;
153    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
154    font-size: 13px;
155    color: var(--studio-text, #e8eaed);
156    white-space: nowrap;
157}
158
159/* What else a proof shows: a grid's answer, or why a search failed, as the prover wrote it. */
160.derivation-tree {
161    padding: var(--studio-inset, 16px);
162    white-space: pre-wrap;
163    font-family: 'SF Mono', 'Fira Code', monospace;
164    font-size: 13px;
165    line-height: 1.8;
166    color: #e8eaed;
167}
168
169.derivation-tree .rule {
170    color: #667eea;
171    font-weight: 600;
172}
173
174.derivation-tree .conclusion {
175    color: #98c379;
176}
177
178/* The walkthrough: the three steps across the drawer, each what to do and what it shows. The
179   step to take now is lit in the dock bar's amber; a step taken is ticked. */
180.proof-steps {
181    display: grid;
182    grid-template-columns: repeat(3, minmax(0, 1fr));
183    gap: 12px;
184    margin: 0;
185    padding: 14px 16px;
186    list-style: none;
187}
188
189.proof-step {
190    display: flex;
191    gap: 10px;
192    min-width: 0;
193    padding: 10px 12px;
194    border: 1px solid var(--studio-border, rgba(255, 255, 255, 0.08));
195    border-radius: 8px;
196    background: rgba(255, 255, 255, 0.02);
197}
198
199.proof-step.now {
200    border-color: rgba(245, 158, 11, 0.6);
201    background: rgba(245, 158, 11, 0.08);
202}
203
204.proof-step.done { opacity: 0.75; }
205
206.proof-step-n {
207    flex: none;
208    display: inline-flex;
209    align-items: center;
210    justify-content: center;
211    width: 22px;
212    height: 22px;
213    border-radius: 50%;
214    background: rgba(255, 255, 255, 0.08);
215    font-size: 12px;
216    font-weight: 700;
217}
218
219.proof-step.now .proof-step-n { background: #f59e0b; color: #111; }
220.proof-step.done .proof-step-n { background: #22c55e; color: #111; }
221
222.proof-step-title {
223    margin-bottom: 2px;
224    font-weight: 600;
225}
226
227.proof-step-line {
228    color: var(--studio-text-secondary, rgba(255, 255, 255, 0.6));
229    line-height: 1.45;
230}
231
232@media (max-width: 768px) {
233    .proof-steps { grid-template-columns: minmax(0, 1fr); }
234    .derivation-tree { padding: 12px; font-size: 12px; }
235}
236"#;
237
238/// One step of a derivation, as the drawer draws it: the rule's word, what the rule was applied
239/// with (the term a universal was instantiated at, a witness), the conclusion it reached, and the
240/// steps it rests on.
241#[derive(Clone, Debug, PartialEq)]
242pub struct ProofNode {
243    pub rule: String,
244    pub detail: Option<String>,
245    pub conclusion: String,
246    pub premises: Vec<ProofNode>,
247}
248
249impl ProofNode {
250    /// A premise taken as given: the leaf a derivation rests on.
251    pub fn is_given(&self) -> bool {
252        self.rule == "Given"
253    }
254}
255
256/// What the note above the derivation says.
257pub const DERIVATION_NOTE: &str =
258    "Read it from the top: the first row is what was proved, and each row below is a step it rests on, down to the givens. Press a row with a chevron to fold its steps away.";
259
260/// Proof status for the UI
261#[derive(Clone, PartialEq, Default)]
262pub enum ProofStatus {
263    #[default]
264    Idle,
265    Proving,
266    Success,
267    /// The prover found a derivation the kernel did not certify — shown, but not a proof.
268    /// Carries where verification broke.
269    Uncertified(String),
270    Failed(String),
271}
272
273impl ProofStatus {
274    /// The status header's class and text.
275    pub fn line(&self) -> (&'static str, String) {
276        match self {
277            ProofStatus::Idle => ("proof-status", "Ready to prove".to_string()),
278            ProofStatus::Proving => ("proof-status pending", "Searching for proof...".to_string()),
279            ProofStatus::Success => ("proof-status success", "\u{2713} Proof found".to_string()),
280            ProofStatus::Uncertified(_) => ("proof-status uncertified", "\u{26A0} Derivation not certified".to_string()),
281            ProofStatus::Failed(msg) => ("proof-status error", format!("\u{2717} {}", msg)),
282        }
283    }
284}
285
286/// The three steps a first visit takes, in order: what to do, and what it shows.
287pub const STEPS: [(&str, &str); 3] = [
288    ("Write English", "Given lines, then what to prove, one sentence each, as in the example."),
289    ("Execute", "Turns every sentence into logic, shown in Logic Output."),
290    ("Prove", "Asks the kernel whether it follows from the givens. Plain sentences prove the first one."),
291];
292
293/// Where a step stands for this reader.
294#[derive(Clone, Copy, Debug, PartialEq, Eq)]
295pub enum StepState {
296    Done,
297    Now,
298    Next,
299}
300
301impl StepState {
302    /// The class a step is painted with.
303    pub fn class(self) -> &'static str {
304        match self {
305            StepState::Done => "proof-step done",
306            StepState::Now => "proof-step now",
307            StepState::Next => "proof-step",
308        }
309    }
310}
311
312/// What the reader has done so far: written something, and run Execute on it.
313#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
314pub struct Walkthrough {
315    pub wrote: bool,
316    pub executed: bool,
317}
318
319impl Walkthrough {
320    /// Each step's state: the first two follow what was done, and Prove is next once both are.
321    pub fn states(self) -> [StepState; 3] {
322        let done = [self.wrote, self.wrote && self.executed, false];
323        let now = done.iter().position(|d| !d).unwrap_or(STEPS.len());
324        std::array::from_fn(|i| {
325            if done[i] {
326                StepState::Done
327            } else if i == now {
328                StepState::Now
329            } else {
330                StepState::Next
331            }
332        })
333    }
334}
335
336/// The dock bar's line: the step to take now, or, once Execute has run, what the prover last said
337/// (`hint`), else the invitation to prove.
338pub fn dock_prompt(steps: Walkthrough, hint: Option<&str>) -> String {
339    if !steps.wrote {
340        "Write a sentence in English to begin.".to_string()
341    } else if !steps.executed {
342        "Press Execute to turn your sentences into logic.".to_string()
343    } else {
344        hint.unwrap_or("Press Prove to check it follows from the givens.").to_string()
345    }
346}
347
348/// The proof drawer: its bar with the proof's status, and the derivation tree or the walkthrough.
349#[component]
350pub fn ProofPanel(
351    /// What else the proof shows, as HTML: a grid's answer, or why a search failed.
352    proof_text: String,
353    /// The derivation, once there is one.
354    tree: Option<ProofNode>,
355    /// Current proof status
356    status: ProofStatus,
357    /// What the reader has done so far, for the walkthrough
358    steps: Walkthrough,
359    /// The drawer's height, which its grip changes
360    height: Signal<f64>,
361    /// Prove was pressed
362    on_prove: EventHandler<()>,
363    /// The drawer's close was pressed
364    on_close: EventHandler<()>,
365) -> Element {
366    // Dragging the grip on the drawer's top edge paints each frame straight onto the drawer's
367    // height and commits it once, on release (see ui::drag). The panel row above gives way.
368    let start_grip_drag = move |evt: PointerEvent| {
369        #[cfg(target_arch = "wasm32")]
370        {
371            use crate::ui::pages::studio_layout::proof_dragged;
372            use dioxus::html::input_data::MouseButton;
373            use wasm_bindgen::JsCast;
374            let mut height = height;
375            if evt.data().trigger_button().is_some_and(|b| b != MouseButton::Primary) {
376                return;
377            }
378            evt.prevent_default();
379            let Some(document) = web_sys::window().and_then(|w| w.document()) else {
380                return;
381            };
382            let find = |selector: &str| document.query_selector(selector).ok().flatten();
383            let (Some(grip), Some(drawer), Some(panels)) = (
384                find(".proof-grip"),
385                find(".proof-drawer").and_then(|el| el.dyn_into::<web_sys::HtmlElement>().ok()),
386                find(".studio-content"),
387            ) else {
388                return;
389            };
390            let start = drawer.get_bounding_client_rect().height();
391            let room = start + panels.get_bounding_client_rect().height();
392            let point = evt.data().client_coordinates();
393            let _ = grip.class_list().add_1("active");
394            let released = grip.clone();
395            crate::ui::drag::begin(
396                grip,
397                evt.data().pointer_id(),
398                (point.x, point.y),
399                crate::ui::drag::Axis::Rows,
400                move |_, dy| {
401                    let _ = drawer.style().set_property("height", &format!("{}px", proof_dragged(start, dy, room)));
402                },
403                move |_, dy| {
404                    let _ = released.class_list().remove_1("active");
405                    height.set(proof_dragged(start, dy, room));
406                },
407            );
408        }
409        #[cfg(not(target_arch = "wasm32"))]
410        let _ = (evt, height);
411    };
412    let drawer_style = format!("height: {}px;", height());
413    let has_proof = tree.is_some() || !proof_text.is_empty();
414    let (status_class, status_text) = status.line();
415    let states = steps.states();
416
417    rsx! {
418        style { "{PROOF_PANEL_STYLE}" }
419
420        div { class: "proof-drawer", style: "{drawer_style}",
421            div { class: "proof-grip", onpointerdown: move |evt| start_grip_drag(evt) }
422            div { class: "proof-bar",
423                span { class: "proof-title", "Proof" }
424                span { class: "{status_class}", "{status_text}" }
425                span { class: "proof-bar-space" }
426                button { class: "proof-close", title: "Hide the proof", onclick: move |_| on_close.call(()), "\u{2715}" }
427            }
428            div { class: "proof-body",
429                if let Some(root) = tree.clone() {
430                    div { class: "proof-derivation",
431                        p { class: "proof-tree-note", "{DERIVATION_NOTE}" }
432                        Tree { kind: "proof-tree", label: "Derivation",
433                            ProofTreeNode { node: root, depth: 0, last: true }
434                        }
435                    }
436                }
437                if !proof_text.is_empty() {
438                    div { class: "derivation-tree", dangerous_inner_html: "{proof_text}" }
439                }
440                if !has_proof {
441                    ol { class: "proof-steps", "aria-label": "How to prove a sentence",
442                        for (i, (title, line)) in STEPS.iter().enumerate() {
443                            li { key: "{i}", class: states[i].class(), "aria-current": if states[i] == StepState::Now { "step" } else { "false" },
444                                span { class: "proof-step-n", "aria-hidden": "true",
445                                    if states[i] == StepState::Done { "\u{2713}" } else { "{i + 1}" }
446                                }
447                                div {
448                                    div { class: "proof-step-title", "{title}" }
449                                    div { class: "proof-step-line", "{line}" }
450                                }
451                            }
452                        }
453                    }
454                }
455            }
456        }
457    }
458}
459
460/// One step and the steps it rests on. Every level opens as the derivation opens.
461#[component]
462fn ProofTreeNode(node: ProofNode, depth: usize, last: bool) -> Element {
463    let mut open = use_signal(|| true);
464    let expandable = !node.premises.is_empty();
465    let count = node.premises.len();
466    let rule_class = if node.is_given() { "proof-rule given" } else { "proof-rule" };
467
468    rsx! {
469        TreeNode {
470            depth,
471            last,
472            expandable,
473            open: open(),
474            stop: depth == 0,
475            on_press: if expandable {
476                Some(EventHandler::new(move |_| {
477                    let now = open();
478                    open.set(!now);
479                }))
480            } else {
481                None
482            },
483            row: rsx! {
484                span { class: "{rule_class}", "{node.rule}" }
485                if let Some(detail) = &node.detail {
486                    span { class: "proof-rule-detail", "{detail}" }
487                }
488                span { class: "proof-conclusion", "{node.conclusion}" }
489            },
490            for (i, premise) in node.premises.iter().enumerate() {
491                ProofTreeNode { key: "{i}", node: premise.clone(), depth: depth + 1, last: i + 1 == count }
492            }
493        }
494    }
495}
496
497#[cfg(test)]
498#[path = "../../../../../crates/logicaffeine_tests/unit/logicaffeine_web/ui/components/proof_panel/status_line_tests.rs"]
499mod status_line_tests;
500
501#[cfg(test)]
502#[path = "../../../../../crates/logicaffeine_tests/unit/logicaffeine_web/ui/components/proof_panel/walkthrough_tests.rs"]
503mod walkthrough_tests;