Skip to main content

logicaffeine_web/ui/components/
symbol_dictionary.rs

1//! The Symbol Dictionary: what each symbol of a First-Order Logic formula means.
2//!
3//! [`symbols_in`] reads the symbols a formula shows, named as the view that printed it names them;
4//! [`SymbolGroups`] lists them, each kind of symbol a group. The Studio sets the groups in a
5//! collapsible block of its Logic column.
6
7use crate::ui::components::logic_output::OutputFormat;
8use dioxus::prelude::*;
9use logicaffeine_language::formatter::{KripkeFormatter, SimpleFOLFormatter, UnicodeFormatter};
10use logicaffeine_language::symbol_dict::{
11    extract_symbols_for, group_symbols_by_kind, SymbolEntry, SymbolKind,
12};
13
14const SYMBOL_GROUPS_STYLE: &str = r#"
15.symbol-groups {
16    display: flex;
17    flex-direction: column;
18    gap: 14px;
19}
20
21/* Each kind is one group: its name, then every symbol beside what it means. */
22.symbol-group-title {
23    font-size: 11px;
24    font-weight: 600;
25    text-transform: uppercase;
26    letter-spacing: 0.5px;
27    margin-bottom: 6px;
28}
29
30.symbol-entries {
31    display: grid;
32    grid-template-columns: max-content 1fr;
33    column-gap: 16px;
34    row-gap: 4px;
35    align-items: baseline;
36}
37
38.symbol-entry {
39    display: contents;
40}
41
42.symbol-glyph {
43    font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace;
44    font-size: 14px;
45    white-space: nowrap;
46}
47
48.symbol-desc {
49    font-size: 13px;
50    line-height: 1.45;
51    color: rgba(229, 231, 235, 0.72);
52}
53
54.quantifier { color: #60a5fa; }
55.connective { color: #f472b6; }
56.modal { color: #c084fc; }
57.temporal { color: #22d3ee; }
58.predicate { color: #4ade80; }
59.role { color: #fda4af; }
60.variable { color: #fbbf24; }
61.constant { color: #fb923c; }
62"#;
63
64/// The symbols `logic` shows, named as `view` — the view that printed it — names them: each view
65/// names its tense and aspect operators its own way.
66pub fn symbols_in(logic: &str, view: OutputFormat) -> Vec<SymbolEntry> {
67    match view {
68        OutputFormat::Unicode | OutputFormat::LaTeX => {
69            extract_symbols_for(logic, &UnicodeFormatter)
70        }
71        OutputFormat::SimpleFOL => extract_symbols_for(logic, &SimpleFOLFormatter),
72        OutputFormat::Kripke => extract_symbols_for(logic, &KripkeFormatter),
73    }
74}
75
76/// A group's heading and colour class.
77fn group_look(kind: SymbolKind) -> (&'static str, &'static str) {
78    match kind {
79        SymbolKind::Quantifier => ("Quantifiers", "quantifier"),
80        SymbolKind::Connective => ("Connectives", "connective"),
81        SymbolKind::Modal => ("Modality", "modal"),
82        SymbolKind::Temporal => ("Tense & aspect", "temporal"),
83        SymbolKind::Identity => ("Identity", "connective"),
84        SymbolKind::Predicate => ("Predicates", "predicate"),
85        SymbolKind::Role => ("Thematic roles", "role"),
86        SymbolKind::Variable => ("Variables", "variable"),
87        SymbolKind::Constant => ("Individuals", "constant"),
88        SymbolKind::Punctuation => ("Punctuation", "variable"),
89    }
90}
91
92/// The dictionary's entries: each kind of symbol a group, its name over every symbol beside what
93/// it means.
94#[component]
95pub fn SymbolGroups(symbols: Vec<SymbolEntry>) -> Element {
96    let grouped = group_symbols_by_kind(&symbols);
97    rsx! {
98        style { "{SYMBOL_GROUPS_STYLE}" }
99        div { class: "symbol-groups",
100            for (kind, entries) in grouped {
101                {
102                    let (heading, kind_class) = group_look(kind);
103                    rsx! {
104                        div { class: "symbol-group",
105                            key: "{kind_class}-{heading}",
106                            div { class: "symbol-group-title {kind_class}", "{heading}" }
107                            div { class: "symbol-entries",
108                                for entry in entries {
109                                    div { class: "symbol-entry",
110                                        key: "{entry.symbol}",
111                                        span { class: "symbol-glyph {kind_class}", "{entry.usage}" }
112                                        span { class: "symbol-desc", "{entry.description}" }
113                                    }
114                                }
115                            }
116                        }
117                    }
118                }
119            }
120        }
121    }
122}
123
124#[cfg(test)]
125#[path = "../../../../../crates/logicaffeine_tests/unit/logicaffeine_web/ui/components/symbol_dictionary/tests.rs"]
126mod tests;