camxes-rs 1.1.1

Lojban PEG parser with semantic analysis - integrated camxes parser and tersmu semantic engine
Documentation
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
//! CLI eval/show bridge for semantic analysis from `jbo_parse`.
//!
//! Ports the Haskell path `ParseText.hs` → `JboParse.hs :: evalText` → `JboShow.hs` by formatting
//! `SemanticResult` values into the logical and canonical output strings consumed by the Rust CLI.
//! Display helpers mirror the `JboShow`/`LogShow` instances while using explicit Rust formatter
//! context instead of `Bindful.hs` state.

use crate::jbo_parse::{eval_text, eval_text_with_options, SemanticResult};
use crate::jbo_prop::{
    JboFragment, JboMex, JboModalOp, JboOperator, JboProp, JboQuantifier, JboRel, JboTerm,
    Texticule,
};
use crate::jbo_syntax::{Lerfu, SumtiAtom, Text};
use crate::logic::Prop;

// Rust-only: Inverse of jbonum for parsing PA digits back to numeric strings
fn format_pa_digit(pa: &str) -> &str {
    match pa {
        "no" => "0",
        "pa" => "1",
        "re" => "2",
        "ci" => "3",
        "vo" => "4",
        "mu" => "5",
        "xa" => "6",
        "ze" => "7",
        "bi" => "8",
        "so" => "9",
        _ => pa,
    }
}

// Ported from: JboShow.hs :: instance JboShow LerfuString (logshow method)
fn format_lerfu_string(ls: &[Lerfu]) -> String {
    ls.iter().map(format_lerfu).collect::<String>()
}

// Ported from: JboShow.hs :: instance JboShow Lerfu (logshow method)
fn format_lerfu(l: &Lerfu) -> String {
    match l {
        Lerfu::LerfuChar(c) => c.to_string(),
        Lerfu::LerfuPA(p) | Lerfu::LerfuValsi(p) | Lerfu::LerfuShift(p) => format!("{{{}}}", p),
        Lerfu::LerfuShifted(lau, l) => format!("{{{}}}({})", lau, format_lerfu(l)),
        Lerfu::LerfuComposite(ls) => format!(
            "[{}]",
            ls.iter().map(format_lerfu).collect::<Vec<_>>().join(",")
        ),
    }
}

// Rust-only: Debug helper for Lerfu, not present in Haskell
fn format_lerfu_debug(l: &Lerfu) -> String {
    match l {
        Lerfu::LerfuChar(c) => format!("LerfuChar '{}'", c),
        Lerfu::LerfuPA(p) => format!("LerfuPA {:?}", p),
        Lerfu::LerfuValsi(v) => format!("LerfuValsi {:?}", v),
        Lerfu::LerfuShift(s) => format!("LerfuShift {:?}", s),
        Lerfu::LerfuShifted(lau, l) => {
            format!("LerfuShifted {:?} ({})", lau, format_lerfu_debug(l))
        }
        Lerfu::LerfuComposite(ls) => format!(
            "LerfuComposite [{}]",
            ls.iter()
                .map(format_lerfu_debug)
                .collect::<Vec<_>>()
                .join(",")
        ),
    }
}

// Ported from: JboShow.hs :: instance JboShow Lerfu (jboshow method)
fn format_lerfu_jbo(l: &Lerfu) -> String {
    match l {
        Lerfu::LerfuChar(c) if matches!(c, 'a' | 'e' | 'i' | 'o' | 'u') => format!("{}bu", c),
        Lerfu::LerfuChar('y') => "y bu".to_string(),
        Lerfu::LerfuChar('h') => "y'y".to_string(),
        Lerfu::LerfuChar(c) if c.is_ascii_digit() => jbonum(c.to_digit(10).unwrap() as i32),
        Lerfu::LerfuChar(c) => format!("{}y", c),
        Lerfu::LerfuPA(p) => p.clone(),
        Lerfu::LerfuValsi(v) => format!("{} bu", v),
        Lerfu::LerfuShift(s) => s.clone(),
        Lerfu::LerfuShifted(lau, l) => format!("{} {}", lau, format_lerfu_jbo(l)),
        Lerfu::LerfuComposite(ls) => format!(
            "tei {} foi",
            ls.iter()
                .map(format_lerfu_jbo)
                .collect::<Vec<_>>()
                .join(" ")
        ),
    }
}

// Ported from: JboShow.hs :: vowelnum
fn vowelnum(n: i32) -> String {
    match n {
        1 => "a".to_string(),
        2 => "e".to_string(),
        3 => "i".to_string(),
        4 => "o".to_string(),
        5 => "u".to_string(),
        _ => n.to_string(),
    }
}

// Wrapper for jbo_show::jbonum
fn jbonum(n: i32) -> String {
    crate::jbo_show::jbonum(n)
}

// Ported from: JboShow.hs :: instance JboShow ShowBindable (jboshow method)
// Covers SAss (ko'a/ko'e/..., fo'a/fo'e/...) and MainBridiSumbasti (vo'a/vo'e/...)
fn format_sumti_atom(atom: &SumtiAtom) -> String {
    let jbo = match atom {
        SumtiAtom::Assignable(n) if *n <= 5 => format!("ko'{}", vowelnum(*n)),
        SumtiAtom::Assignable(n) if *n <= 10 => format!("fo'{}", vowelnum(*n - 5)),
        SumtiAtom::Assignable(n) => format!("ko'a xi {}", jbonum(*n)),
        SumtiAtom::Ri(0 | 1) => "ri".to_string(),
        SumtiAtom::Ri(n) => format!("ri xi {}", jbonum(*n)),
        SumtiAtom::Ra(r) => r.clone(),
        SumtiAtom::MainBridiSumbasti(n) if *n <= 5 => format!("vo'{}", vowelnum(*n)),
        SumtiAtom::MainBridiSumbasti(n) => format!("vo'a xi {}", jbonum(*n)),
        SumtiAtom::LerfuString(ls) => return format_lerfu_string(ls),
        _ => format!("{:?}", atom),
    };
    format!("{{{}}}", jbo)
}

// Rust-only: Debug helper for syntax-level Operator, not present in Haskell
fn format_syntax_operator(op: &crate::jbo_syntax::Operator) -> String {
    match op {
        crate::jbo_syntax::Operator::OpVUhU(s) => format!("OpVUhU \"{}\"", s),
        _ => format!("{:?}", op),
    }
}

// Rust-only: Debug helper for syntax-level Mex, not present in Haskell
fn format_syntax_mex(mex: &crate::jbo_syntax::Mex) -> String {
    match mex {
        crate::jbo_syntax::Mex::Operation(op, args) => {
            let args = args
                .iter()
                .map(format_syntax_mex)
                .collect::<Vec<_>>()
                .join(",");
            format!("Operation ({}) [{}]", format_syntax_operator(op), args)
        }
        crate::jbo_syntax::Mex::MexNumeralString(ns) => {
            let parts = ns
                .iter()
                .map(|n| match n {
                    crate::jbo_syntax::Numeral::PA(s) => s.clone(),
                    crate::jbo_syntax::Numeral::NumeralLerfu(l) => format!("{:?}", l),
                })
                .collect::<Vec<_>>();
            let digits = parts.join("");
            match digits.as_str() {
                "no" => "MexInt 0".to_string(),
                "pa" => "MexInt 1".to_string(),
                "re" => "MexInt 2".to_string(),
                "ci" => "MexInt 3".to_string(),
                "vo" => "MexInt 4".to_string(),
                "mu" => "MexInt 5".to_string(),
                "xa" => "MexInt 6".to_string(),
                "ze" => "MexInt 7".to_string(),
                "bi" => "MexInt 8".to_string(),
                "so" => "MexInt 9".to_string(),
                _ => parts.join(" "),
            }
        }
        crate::jbo_syntax::Mex::MexInt(n) => format!("MexInt {}", n),
        crate::jbo_syntax::Mex::MexLerfuString(ls) => format!(
            "MexLerfuString [{}]",
            ls.iter()
                .map(format_lerfu_debug)
                .collect::<Vec<_>>()
                .join(",")
        ),
        _ => format!("{:?}", mex),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboMex (logshow method)
fn format_mex(mex: &JboMex) -> String {
    match mex {
        JboMex::MexNumeralString(ns) => format!(
            "({})",
            ns.iter()
                .map(|n| match n {
                    crate::jbo_syntax::Numeral::PA(s) => s.clone(),
                    crate::jbo_syntax::Numeral::NumeralLerfu(l) => format_lerfu(l),
                })
                .collect::<Vec<_>>()
                .join(" ")
        ),
        JboMex::MexInt(n) => n.to_string(),
        JboMex::MexLerfuString(ls) => format!(
            "({})",
            ls.iter()
                .map(format_lerfu_jbo)
                .collect::<Vec<_>>()
                .join(" ")
        ),
        JboMex::Operation(op, args) => {
            let args = args.iter().map(format_mex).collect::<Vec<_>>().join(",");
            format!("{}({})", format_operator(op), args)
        }
        JboMex::ConnectedMex(_, con, m1, m2) => format!(
            "({}){}({})",
            format_mex(m1),
            format_connective(".", con),
            format_mex(m2)
        ),
        JboMex::QualifiedMex(qual, m) => {
            let qualifier = match qual {
                crate::jbo_syntax::SumtiQualifier::LAhE(l) => l.clone(),
                crate::jbo_syntax::SumtiQualifier::NAhE_BO(n) => format!("{} bo", n),
            };
            format!("{{{}}}({})", qualifier, format_mex(m))
        }
        JboMex::MexSelbri(s) => format!("[{}]", format_seltau_prop(&s(&[JboTerm::BoundVar(0)]))),
        JboMex::MexArray(ms) => format!(
            "[{}]",
            ms.iter().map(format_mex).collect::<Vec<_>>().join(",")
        ),
        JboMex::MexSumti(t) => format!("[{}]", format_term(t)),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboMex (logshow method for numeric cases)
fn format_mex_number_for_rel(mex: &JboMex) -> String {
    match mex {
        JboMex::MexNumeralString(ns) => format!(
            "({})",
            ns.iter()
                .map(|n| match n {
                    crate::jbo_syntax::Numeral::PA(s) => s.clone(),
                    crate::jbo_syntax::Numeral::NumeralLerfu(l) => format_lerfu(l),
                })
                .collect::<Vec<_>>()
                .join(" ")
        ),
        JboMex::MexLerfuString(ls) => format!(
            "({})",
            ls.iter()
                .map(format_lerfu_jbo)
                .collect::<Vec<_>>()
                .join(" ")
        ),
        JboMex::MexInt(n) => n.to_string(),
        _ => format_mex(mex),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboQuantifier (logshow method)
fn format_quantifier(q: &JboQuantifier) -> String {
    match q {
        JboQuantifier::QuestionQuantifier => "?".to_string(),
        JboQuantifier::LojQuantifier(q) => q.to_string(),
        JboQuantifier::MexQuantifier(m) => format_mex(m),
        JboQuantifier::RelQuantifier(q) => format!("RelQuantifier({})", format_quantifier(q)),
    }
}

// Rust-only: Helper for formatting quantified variable prefixes in logical output
fn format_quantified_prefix(q: &JboQuantifier, name: &str) -> String {
    match q {
        JboQuantifier::LojQuantifier(crate::logic::LojQuantifier::Exists)
        | JboQuantifier::LojQuantifier(crate::logic::LojQuantifier::Forall) => {
            format!("{}{}", format_quantifier(q), name)
        }
        _ => format!("{} {}", format_quantifier(q), name),
    }
}

// Ported from: JboShow.hs :: instance JboShow Texticule (logshow method)
fn format_texticule(texticule: &Texticule) -> String {
    match texticule {
        Texticule::TexticuleFrag(fragment) => match fragment {
            JboFragment::JboFragTerms(terms) => format!(
                "[Fragment: {}]",
                terms.iter().map(format_term).collect::<Vec<_>>().join(",")
            ),
            JboFragment::JboFragUnparsed(_) => "[Fragment]".to_string(),
        },
        Texticule::TexticuleProp(prop) => format_prop(prop),
        Texticule::TexticuleSide(_, inner) => format_texticule(inner),
    }
}

// Ported from: JboShow.hs :: instance JboShow [Texticule] (logshow method)
// Includes term-attached side texticules from JboParse.hs
fn format_text(text: &[Texticule]) -> String {
    let mut lines = text.iter().map(format_texticule).collect::<Vec<_>>();
    let mut side_lines = Vec::new();
    for texticule in text {
        collect_term_side_texticules_from_texticule(texticule, &mut side_lines);
    }
    let side_lines = side_lines
        .into_iter()
        .map(|texticule| format_texticule(&texticule))
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>();
    if !side_lines.is_empty() {
        let insert_at = lines
            .iter()
            .take_while(|line| line.starts_with("non-veridical: "))
            .count();
        for (i, line) in side_lines.into_iter().enumerate() {
            lines.insert(insert_at + i, line);
        }
    }
    lines.join("\n")
}

// Ported from: JboShow.hs :: instance JboShow JboTerm (logshow method)
fn format_term(term: &JboTerm) -> String {
    match term {
        JboTerm::NonAnaph(s) => s.clone(),
        JboTerm::Named(s) => format!("\"{}\"", s),
        JboTerm::PredNamed(pred) => {
            let prop = pred(&JboTerm::Named("_".to_string()));
            format!("[Name: {} ]", format_prop(&prop).replace("\"_\"", "_"))
        }
        JboTerm::Constant(n, args) if args.is_empty() => format!("c{}", n),
        JboTerm::Constant(n, args) => {
            let arg_strs = args.iter().map(format_term).collect::<Vec<_>>().join(",");
            format!("f{}({})", n, arg_strs)
        }
        JboTerm::Var(_) => "[UNBOUND Var]".to_string(),
        JboTerm::BoundVar(n) if *n < 0 => format!("\\{}", n.abs()),
        JboTerm::BoundVar(n) => format!("x{}", n),
        JboTerm::Unfilled => " ".to_string(),
        JboTerm::UnboundSumbasti(atom) => format_sumti_atom(atom),
        JboTerm::Value(m) => format_mex(m),
        JboTerm::QualifiedTerm(qual, t) => {
            let qualifier = match qual {
                crate::jbo_syntax::SumtiQualifier::LAhE(l) => l.clone(),
                crate::jbo_syntax::SumtiQualifier::NAhE_BO(n) => format!("{} bo", n),
            };
            format!("{{{}}}({})", qualifier, format_term(t))
        }
        JboTerm::TheMex(m) => format!("[MEX: {}]", format_syntax_mex(m)),
        JboTerm::JoikedTerms(joik, t1, t2) => {
            format!("({} {{{}}} {})", format_term(t1), joik, format_term(t2))
        }
        JboTerm::JboQuote(text) => format!("«{}»", format_text(text)),
        JboTerm::JboErrorQuote(words) => format!("<{{< {} >}}>", words.join(" ")),
        JboTerm::JboNonJboQuote(s) => format!("<[< {} >]>", s),
        JboTerm::Valsi(s) => format!("{{{}}}", s),
        JboTerm::TermWithSides(t, _) => format_term(t),
    }
}

// Ported from: JboShow.hs :: instance JboShow Connective (logshow method for LogJboConnective)
fn format_log_connective(prefix: &str, con: &crate::jbo_syntax::LogJboConnective) -> String {
    let mut parts = Vec::new();
    if !con.b1 {
        parts.push("na".to_string());
    }
    if con.c == 'U' {
        parts.push(format!("se {}u", prefix));
    } else {
        parts.push(format!("{}{}", prefix, con.c));
    }
    if !con.b2 {
        parts.push("nai".to_string());
    }
    parts.join(" ")
}

// Ported from: JboShow.hs :: instance JboShow Connective (logshow method)
fn format_connective(prefix: &str, con: &crate::jbo_syntax::Connective) -> String {
    let (mut lojban, mtag) = match con {
        crate::jbo_syntax::Connective::JboConnLog(mtag, lcon) => {
            (format_log_connective(prefix, lcon), mtag.as_deref())
        }
        crate::jbo_syntax::Connective::JboConnJoik(mtag, joik) => {
            let base = if joik == "??" {
                if prefix == "." {
                    "ji".to_string()
                } else {
                    "je'i".to_string()
                }
            } else {
                joik.clone()
            };
            (base, mtag.as_deref())
        }
    };
    if let Some(tag) = mtag {
        lojban.push(' ');
        lojban.push_str(&format_tag_syntax(tag));
        lojban.push_str(" bo");
    }
    format!("{{{}}}", lojban)
}

// Ported from: JboShow.hs :: instance JboShow Tag (jboshow method for syntax-level Tag)
fn format_tag_syntax(tag: &crate::jbo_syntax::Tag) -> String {
    match tag {
        crate::jbo_syntax::Tag::DecoratedTagUnits(dtus) => dtus
            .iter()
            .map(|dtu| match &dtu.tag_unit {
                crate::jbo_syntax::TagUnit::BAI(s)
                | crate::jbo_syntax::TagUnit::TenseCmavo(s)
                | crate::jbo_syntax::TagUnit::CAhA(s)
                | crate::jbo_syntax::TagUnit::CUhE(s) => s.clone(),
                crate::jbo_syntax::TagUnit::FAhA { faha_cmavo, .. } => faha_cmavo.clone(),
                crate::jbo_syntax::TagUnit::TAhE_ZAhO {
                    tahe_zaho_cmavo, ..
                } => tahe_zaho_cmavo.clone(),
                crate::jbo_syntax::TagUnit::ROI {
                    roiroi,
                    roi_quantifier,
                    ..
                } => format!("{} {}", format_syntax_mex(roi_quantifier), roiroi),
                crate::jbo_syntax::TagUnit::FIhO(_) => "fi'o".to_string(),
                crate::jbo_syntax::TagUnit::KI => "ki".to_string(),
            })
            .collect::<Vec<_>>()
            .join(" "),
        crate::jbo_syntax::Tag::ConnectedTag(con, tag1, tag2) => format!(
            "{} {} {}",
            format_tag_syntax(tag1),
            format_connective("j", con),
            format_tag_syntax(tag2)
        ),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboOperator (logshow method)
fn format_operator(op: &JboOperator) -> String {
    match op {
        JboOperator::ConnectedOperator(_, con, o1, o2) => format!(
            "<{}>{}<{}>",
            format_operator(o1),
            format_connective(".", con),
            format_operator(o2)
        ),
        JboOperator::OpPermuted(s, op) => {
            format!("{} {}", crate::jbo_show::se_to_str(*s), format_operator(op))
        }
        JboOperator::OpScalarNegated(n, op) => format!("{{{}}}({})", n, format_operator(op)),
        JboOperator::OpMex(m) => format!("[{}]", format_mex(m)),
        JboOperator::OpSelbri(s) => {
            format!("[{}]", format_seltau_prop(&s(&[JboTerm::BoundVar(0)])))
        }
        JboOperator::OpVUhU(s) => format!("{{{}}}", s),
    }
}

// Ported from: JboShow.hs :: instance JboShow Connective (logshow method for proposition connectives)
fn format_prop_log_connective(prefix: &str, con: &crate::jbo_syntax::LogJboConnective) -> String {
    format!(
        "{}{}{}",
        if con.b1 { "" } else { "na " },
        if con.c == 'U' {
            format!("se {}u", prefix)
        } else {
            format!("{}{}", prefix, con.c)
        },
        if con.b2 { "" } else { " nai" }
    )
}

// Ported from: JboShow.hs :: instance JboShow JboConnective (logshow method for semantic connectives)
fn format_prop_connective(
    prefix: &str,
    con: &crate::jbo_prop::JboConnective,
    logical: bool,
) -> String {
    let shown = match con {
        crate::jbo_prop::JboConnective::JboConnLog(mtag, lcon) => {
            let mut s = format_prop_log_connective(prefix, lcon);
            if let Some(tag) = mtag {
                s.push(' ');
                s.push_str(&format_tag_with_mode(tag, false));
                s.push_str(" bo");
            }
            s
        }
        crate::jbo_prop::JboConnective::JboConnJoik(mtag, joik) => {
            let mut s = if joik == "??" {
                match prefix {
                    "." => "ji".to_string(),
                    "j" => "je'i".to_string(),
                    _ => "BUG".to_string(),
                }
            } else {
                joik.clone()
            };
            if let Some(tag) = mtag {
                s.push(' ');
                s.push_str(&format_tag_with_mode(tag, false));
                s.push_str(" bo");
            }
            s
        }
    };
    if logical {
        format!("{{{}}}", shown)
    } else {
        shown
    }
}

// Ported from: JboShow.hs :: instance JboShow JboTag (logshow/jboshow method with mode parameter)
fn format_tag_with_mode(tag: &crate::jbo_prop::JboTag, logical: bool) -> String {
    match tag {
        crate::jbo_prop::JboTag::DecoratedTagUnits(dtus) => dtus
            .iter()
            .map(|dtu| {
                let unit = match &dtu.tag_unit {
                    crate::jbo_prop::JboTagUnit::BAI(s)
                    | crate::jbo_prop::JboTagUnit::TenseCmavo(s)
                    | crate::jbo_prop::JboTagUnit::CAhA(s)
                    | crate::jbo_prop::JboTagUnit::CUhE(s) => s.clone(),
                    crate::jbo_prop::JboTagUnit::FAhA(Some(true), s) => format!("mo'i {}", s),
                    crate::jbo_prop::JboTagUnit::FAhA(_, s) => s.clone(),
                    crate::jbo_prop::JboTagUnit::TAhE_ZAhO(true, s) => format!("fe'e {}", s),
                    crate::jbo_prop::JboTagUnit::TAhE_ZAhO(_, s) => s.clone(),
                    crate::jbo_prop::JboTagUnit::ROI(s, true, q) => {
                        format!("fe'e {} {}", format_mex(q), s)
                    }
                    crate::jbo_prop::JboTagUnit::ROI(s, _, q) => format!("{} {}", format_mex(q), s),
                    crate::jbo_prop::JboTagUnit::FIhO(p) => format!(
                        "fi'o {}",
                        format_seltau_prop(&p(&[crate::jbo_prop::JboTerm::BoundVar(0)]))
                    ),
                    crate::jbo_prop::JboTagUnit::KI => "ki".to_string(),
                };
                format!(
                    "{}{}{}{}",
                    dtu.nahe
                        .as_ref()
                        .map(|s| format!("{} ", s))
                        .unwrap_or_default(),
                    dtu.se
                        .map(|se| format!("{} ", crate::jbo_show::se_to_str(se)))
                        .unwrap_or_default(),
                    unit,
                    if dtu.nai { " nai" } else { "" }
                )
            })
            .collect::<Vec<_>>()
            .join(" "),
        crate::jbo_prop::JboTag::ConnectedTag(con, tag1, tag2) => {
            let connective = format_prop_connective("j", con, logical);
            if logical {
                format!(
                    "{}({},{})",
                    connective,
                    format_tag_with_mode(tag1, false),
                    format_tag_with_mode(tag2, false)
                )
            } else {
                format!(
                    "{} {} {}",
                    format_tag_with_mode(tag1, false),
                    connective,
                    format_tag_with_mode(tag2, false)
                )
            }
        }
    }
}

// Ported from: JboShow.hs :: instance JboShow JboTag (logshow method)
fn format_tag(tag: &crate::jbo_prop::JboTag) -> String {
    format_tag_with_mode(tag, true)
}

// Ported from: JboShow.hs :: instance JboShow JboModalOp (logshow method for modal relations)
fn format_modal_rel(modal: &JboModalOp, rel: &JboRel) -> String {
    match modal {
        JboModalOp::Tagged(tag, term) => {
            let term = term.as_ref().map(format_term).unwrap_or_default();
            format!("({})({}). {}(_)", format_tag(tag), term, format_rel(rel))
        }
        _ => format!("Modal({:?}). {}(_)", modal, format_rel(rel)),
    }
}

// Ported from: JboShow.hs :: logjboshow' Prop (quantifier restriction term rendering)
fn format_restriction_term(term: &JboTerm, var: i32) -> String {
    match term {
        JboTerm::TermWithSides(inner, _) => format_restriction_term(inner, var),
        JboTerm::BoundVar(n) if *n == var => "_".to_string(),
        JboTerm::Constant(n, args) if !args.is_empty() => {
            let arg_strs: Vec<String> = args
                .iter()
                .map(|t| format_restriction_term(t, var))
                .collect();
            format!("f{}({})", n, arg_strs.join(","))
        }
        JboTerm::QualifiedTerm(qual, t) => {
            let qualifier = match qual {
                crate::jbo_syntax::SumtiQualifier::LAhE(l) => l.clone(),
                crate::jbo_syntax::SumtiQualifier::NAhE_BO(n) => format!("{} bo", n),
            };
            format!("{{{}}}({})", qualifier, format_restriction_term(t, var))
        }
        JboTerm::JoikedTerms(joik, t1, t2) => format!(
            "({} {{{}}} {})",
            format_restriction_term(t1, var),
            joik,
            format_restriction_term(t2, var)
        ),
        _ => format_term(term),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboTerm (logshow method for lambda terms)
fn format_lambda_term(term: &JboTerm) -> String {
    match term {
        JboTerm::BoundVar(n) if *n < 0 => format!("\\{}", n.abs()),
        _ => format_term(term),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboRel (logshow method for lambda relations)
fn format_lambda_rel(rel: &JboRel) -> String {
    match rel {
        JboRel::AppliedRel(r, terms) => {
            format!(
                "{}({})",
                format_rel(r),
                terms
                    .iter()
                    .map(format_lambda_term)
                    .collect::<Vec<_>>()
                    .join(",")
            )
        }
        _ => format_rel(rel),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboTerm (logshow method for lambda restriction terms)
fn format_lambda_restriction_term(term: &JboTerm, var: i32) -> String {
    match term {
        JboTerm::BoundVar(n) if *n == var => "_".to_string(),
        _ => format_lambda_term(term),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboProp (logshow method for lambda restriction propositions)
fn format_lambda_restriction_prop(
    prop: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
    var: i32,
) -> String {
    match prop {
        Prop::Rel(JboRel::Among(t), terms) if matches!(terms.as_slice(), [JboTerm::BoundVar(n)] if *n == var) =>
        {
            format!("(_ ≤ {})", format_lambda_restriction_term(t, var))
        }
        Prop::Rel(JboRel::AppliedRel(r, applied_terms), terms)
            if terms.is_empty()
                && matches!(r.as_ref(), JboRel::Among(_))
                && matches!(applied_terms.as_slice(), [JboTerm::BoundVar(n)] if *n == var) =>
        {
            if let JboRel::Among(t) = r.as_ref() {
                format!("(_ ≤ {})", format_lambda_restriction_term(t, var))
            } else {
                unreachable!()
            }
        }
        Prop::Rel(rel, terms) => {
            format!(
                "{}({})",
                format_lambda_rel(rel),
                terms
                    .iter()
                    .map(|t| format_lambda_restriction_term(t, var))
                    .collect::<Vec<_>>()
                    .join(",")
            )
        }
        _ => format_lambda_prop(prop),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboRel (logshow method for restriction relations)
fn format_restriction_rel(rel: &JboRel, var: i32) -> String {
    match rel {
        JboRel::AppliedRel(r, terms) => format!(
            "{}({})",
            format_rel(r),
            terms
                .iter()
                .map(|t| format_restriction_term(t, var))
                .collect::<Vec<_>>()
                .join(",")
        ),
        _ => format_rel(rel),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboProp (logshow method for restriction propositions)
fn format_restriction_prop(
    prop: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
    var: i32,
) -> String {
    match prop {
        Prop::Rel(rel, terms) => {
            if matches!(rel, JboRel::Equal) {
                return format!(
                    "({})",
                    terms
                        .iter()
                        .map(|t| format_restriction_term(t, var))
                        .collect::<Vec<_>>()
                        .join(" == ")
                );
            }
            if let JboRel::Among(t) = rel {
                if matches!(terms.as_slice(), [JboTerm::BoundVar(n)] if *n == var) {
                    return format!("(_ ≤ {})", format_restriction_term(t, var));
                }
                let terms_str = terms
                    .iter()
                    .map(|term| format_restriction_term(term, var))
                    .collect::<Vec<_>>()
                    .join(",");
                return format!("({} ≤ {})", terms_str, format_restriction_term(t, var));
            }
            if let JboRel::AppliedRel(r, applied_terms) = rel {
                if let JboRel::Among(t) = r.as_ref() {
                    if terms.is_empty()
                        && matches!(applied_terms.as_slice(), [JboTerm::BoundVar(n)] if *n == var)
                    {
                        return format!("(_ ≤ {})", format_restriction_term(t, var));
                    }
                }
            }
            format!(
                "{}({})",
                format_restriction_rel(rel, var),
                terms
                    .iter()
                    .map(|t| format_restriction_term(t, var))
                    .collect::<Vec<_>>()
                    .join(",")
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::NonVeridical, inner) => {
            format!("non-veridical: {}", format_restriction_prop(inner, var))
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::Tagged(tag, mt), inner) => {
            let term = mt
                .as_ref()
                .map(|t| format_restriction_term(t, var))
                .unwrap_or_default();
            format!(
                "({})({}). {}",
                format_tag(tag),
                term,
                format_restriction_prop(inner, var)
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::WithEventAs(t), inner) => {
            format!(
                "{}=. {}",
                format_restriction_term(t, var),
                format_restriction_prop(inner, var)
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::QTruthModal, inner) => {
            format!("?. {}", format_restriction_prop(inner, var))
        }
        Prop::Not(inner) => format!("¬{}", format_restriction_prop(inner, var)),
        Prop::Connected(conn, p1, p2) => {
            format!(
                "({} {} {})",
                format_restriction_prop(p1, var),
                conn,
                format_restriction_prop(p2, var)
            )
        }
        Prop::NonLogConnected(joik, p1, p2) => {
            format!(
                "({} {{{}}} {})",
                format_restriction_prop(p1, var),
                joik,
                format_restriction_prop(p2, var)
            )
        }
        Prop::Quantified(q, restriction, body) => {
            let n = var + 1;
            let mut prefix = match q {
                JboQuantifier::RelQuantifier(inner_q) => {
                    format_quantified_prefix(inner_q, &format!("R{}", n))
                }
                _ => format_quantified_prefix(q, &format!("x{}", n)),
            };
            if let Some(r) = restriction {
                prefix.push_str(":(");
                prefix.push_str(&format_restriction_prop(&r(n), n));
                prefix.push_str("). ");
            } else {
                prefix.push_str(". ");
            }
            format!("{}{}", prefix, format_restriction_prop(&body(n), var))
        }
        Prop::Eet => "_|_".to_string(),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboProp (logshow method for lambda propositions)
fn format_lambda_prop(
    prop: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
) -> String {
    format_lambda_prop_at(prop, 1)
}

// Ported from: JboShow.hs :: instance JboShow JboProp (logshow method for lambda propositions with variable tracking)
fn format_lambda_prop_at(
    prop: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
    next_var: i32,
) -> String {
    match prop {
        Prop::Rel(rel, terms) => {
            format!(
                "{}({})",
                format_lambda_rel(rel),
                terms
                    .iter()
                    .map(format_lambda_term)
                    .collect::<Vec<_>>()
                    .join(",")
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::Tagged(tag, mt), inner) => {
            if let Some(expanded) = expand_connected_tag_modal(tag, mt, inner) {
                return format_lambda_prop_at(&expanded, next_var);
            }
            let term = mt.as_ref().map(format_lambda_term).unwrap_or_default();
            format!(
                "({})({}). {}",
                format_tag(tag),
                term,
                format_lambda_prop_at(inner, next_var)
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::WithEventAs(term), inner) => {
            format!(
                "{}=. {}",
                format_lambda_term(term),
                format_lambda_prop_at(inner, next_var)
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::QTruthModal, inner) => {
            format!("?. {}", format_lambda_prop_at(inner, next_var))
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::NonVeridical, inner) => {
            format!("non-veridical: {}", format_lambda_prop_at(inner, next_var))
        }
        Prop::Not(inner) => format!("¬{}", format_lambda_prop_at(inner, next_var)),
        Prop::Connected(conn, p1, p2) => {
            format!(
                "({} {} {})",
                format_lambda_prop_at(p1, next_var),
                conn,
                format_lambda_prop_at(p2, next_var)
            )
        }
        Prop::NonLogConnected(joik, p1, p2) => {
            format!(
                "({} {{{}}} {})",
                format_lambda_prop_at(p1, next_var),
                joik,
                format_lambda_prop_at(p2, next_var)
            )
        }
        Prop::Quantified(q, restriction, body) => {
            let n = next_var;
            let mut prefix = match q {
                JboQuantifier::RelQuantifier(inner_q) => {
                    format_quantified_prefix(inner_q, &format!("R{}", n))
                }
                _ => format_quantified_prefix(q, &format!("x{}", n)),
            };
            if let Some(r) = restriction {
                prefix.push_str(":(");
                prefix.push_str(&format_lambda_restriction_prop(&r(n), n));
                prefix.push_str("). ");
            } else {
                prefix.push_str(". ");
            }
            let body_next_var = if matches!(q, JboQuantifier::RelQuantifier(_)) {
                n
            } else {
                n + 1
            };
            format!(
                "{}{}",
                prefix,
                format_lambda_prop_at(&body(n), body_next_var)
            )
        }
        Prop::Eet => "_|_".to_string(),
    }
}

// Ported from: JboShow.hs :: logjboshow' Rel (scalar negation case)
// Mirrors scalar NAhE display after parsedSelbriToNewSelbri creates applied rel with skipped x2
fn format_scalar_negated_rel_inner(rel: &JboRel) -> String {
    match rel {
        JboRel::AppliedRel(r, terms)
            if terms.len() > 2 && matches!(terms.get(1), Some(JboTerm::Unfilled)) =>
        {
            let terms = std::iter::once(terms[0].clone())
                .chain(terms.iter().skip(2).cloned())
                .collect::<Vec<_>>();
            format!(
                "{}({})",
                format_rel(r),
                terms.iter().map(format_term).collect::<Vec<_>>().join(",")
            )
        }
        _ => format_rel(rel),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboRel (logshow method)
fn format_rel(rel: &JboRel) -> String {
    match rel {
        JboRel::Brivla(s) => s.clone(),
        JboRel::Equal => "==".to_string(),
        JboRel::Among(t) => format!("( ≤ {})", format_term(t)),
        JboRel::Tanru(r1, r2) => {
            let seltau_str = format_seltau(r1);
            let tertau_str = format_rel(r2);
            format!("<{}><{}>", seltau_str, tertau_str)
        }
        JboRel::AppliedRel(r, terms) => {
            format!(
                "{}({})",
                format_rel(r),
                terms.iter().map(format_term).collect::<Vec<_>>().join(",")
            )
        }
        JboRel::TanruConnective(con, r1, r2) => {
            format!(
                "<{}>{}<{}>",
                format_seltau(r1),
                format_connective("j", con),
                format_seltau(r2)
            )
        }
        JboRel::PermutedRel(n, r) => {
            format!("{} {}", crate::jbo_show::se_to_str(*n), format_rel(r))
        }
        JboRel::RVar(_) => "[UNBOUND RVar]".to_string(),
        JboRel::BoundRVar(n) => format!("R{}", n),
        JboRel::RAss(n) => format!("R{}", n),
        JboRel::UnboundBribasti(tu) => match tu {
            crate::jbo_syntax::TanruUnit::TUGOhA(g, n) => {
                if *n == 1 {
                    g.clone()
                } else {
                    format!("{}{}", g, n)
                }
            }
            crate::jbo_syntax::TanruUnit::TUBrivla(s) => s.clone(),
            _ => format!("unbound({:?})", tu),
        },
        JboRel::Moi(t, moi) => match t.as_ref() {
            JboTerm::Value(
                m @ (JboMex::MexInt(_) | JboMex::MexNumeralString(_) | JboMex::MexLerfuString(_)),
            ) => format!("{} {}", format_mex_number_for_rel(m), moi),
            _ => format!("[{}] {}", format_term(t), moi),
        },
        JboRel::OperatorRel(op) => format!("[{}]", format_operator(op)),
        JboRel::ScalarNegatedRel(nahe, r) => {
            format!("{{{}}}({})", nahe, format_scalar_negated_rel_inner(r))
        }
        JboRel::VPredRel(vpred) => format_seltau_prop(&vpred(&[JboTerm::BoundVar(0)])),
        JboRel::AbsPred(abs, npred) => {
            let args: Vec<JboTerm> = (1..=npred.arity as i32)
                .map(|n| JboTerm::BoundVar(-n))
                .collect();
            format!("{}[{}]", abs, format_lambda_prop(&(npred.pred)(&args)))
        }
        JboRel::AbsProp(abs, prop) => format!("{}[{}]", abs, format_prop(prop)),
        JboRel::TagRel(tag) => format!("{{{}}}", format_tag(tag)),
        JboRel::ModalRel(modal, r) => format_modal_rel(modal, r),
    }
}

// Ported from: JboShow.hs :: logjboshow for JboVPred with Bindful.hs :: withShuntedRelVar
// Helper for modal seltau inner proposition formatting
fn format_modal_seltau_inner_prop(prop: &JboProp) -> String {
    match prop {
        Prop::Rel(rel, terms) if terms.is_empty() => format!("{}()", format_rel(rel)),
        _ => format_seltau_prop(prop),
    }
}

// Ported from: JboShow.hs :: logjboshow for JboVPred with Bindful.hs :: withShuntedRelVar
// Formats seltau (tanru left component) with placeholder variable for logical output
fn format_seltau_prop(prop: &JboProp) -> String {
    match prop {
        Prop::Rel(JboRel::Among(t), terms)
            if terms.is_empty() || matches!(terms.as_slice(), [JboTerm::BoundVar(0)]) =>
        {
            format!("(_ ≤ {})", format_term(t))
        }
        Prop::Rel(JboRel::AppliedRel(r, _), terms)
            if terms.is_empty() && matches!(r.as_ref(), JboRel::Among(_)) =>
        {
            if let JboRel::Among(t) = r.as_ref() {
                format!("(_ ≤ {})", format_term(t))
            } else {
                unreachable!()
            }
        }
        Prop::Rel(rel, terms) if terms.is_empty() => format!("{}(_)", format_rel(rel)),
        Prop::Rel(rel, terms) => format!(
            "{}({})",
            format_rel(rel),
            terms
                .iter()
                .map(|term| {
                    if matches!(term, JboTerm::BoundVar(0)) {
                        "_".to_string()
                    } else {
                        format_term(term)
                    }
                })
                .collect::<Vec<_>>()
                .join(",")
        ),
        Prop::Modal(crate::jbo_prop::JboModalOp::Tagged(tag, mt), inner) => {
            let term = mt
                .as_ref()
                .map(|term| {
                    if matches!(term, JboTerm::BoundVar(0)) {
                        "_".to_string()
                    } else {
                        format_term(term)
                    }
                })
                .unwrap_or_default();
            format!(
                "({})({}). {}",
                format_tag(tag),
                term,
                format_modal_seltau_inner_prop(inner)
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::WithEventAs(t), inner) => {
            format!("{}=. {}", format_term(t), format_seltau_prop(inner))
        }
        Prop::Modal(_, inner) => format_seltau_prop(inner),
        _ => "<vpred>".to_string(),
    }
}

// Ported from: JboShow.hs :: logjboshow for JboVPred with Bindful.hs :: withShuntedRelVar
// Formats seltau (tanru left component) with placeholder variable for logical output
fn format_seltau(rel: &JboRel) -> String {
    match rel {
        JboRel::Among(t) => format!("(_ ≤ {})", format_term(t)),
        JboRel::AppliedRel(r, _terms) if matches!(r.as_ref(), JboRel::Among(_)) => {
            if let JboRel::Among(t) = r.as_ref() {
                format!("(_ ≤ {})", format_term(t))
            } else {
                unreachable!()
            }
        }
        JboRel::ModalRel(_, _) => format_rel(rel),
        JboRel::AppliedRel(r, terms) => {
            format!(
                "{}({})",
                format_rel(r),
                terms
                    .iter()
                    .map(|term| {
                        if matches!(term, JboTerm::BoundVar(0)) {
                            "_".to_string()
                        } else {
                            format_term(term)
                        }
                    })
                    .collect::<Vec<_>>()
                    .join(",")
            )
        }
        JboRel::VPredRel(vpred) => format_seltau_prop(&vpred(&[JboTerm::BoundVar(0)])),
        JboRel::Tanru(_, _) => {
            let inner = format_rel(rel);
            format!("{}(_)", inner)
        }
        _ => format!("{}(_)", format_rel(rel)),
    }
}

// Ported from: JboShow.hs :: instance JboShow JboProp (logshow method)
fn format_prop(
    prop: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
) -> String {
    format_prop_at(prop, 1)
}

// Ported from: JboParse.hs :: parseTag + JboProp.hs :: connToFol
// Expands connected logical tags into FOL connectives
fn expand_connected_tag_modal(
    tag: &crate::jbo_prop::JboTag,
    mt: &Option<JboTerm>,
    inner: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
) -> Option<
    Prop<JboRel, JboTerm, String, crate::jbo_prop::JboModalOp, crate::jbo_prop::JboQuantifier>,
> {
    if let crate::jbo_prop::JboTag::ConnectedTag(con, tag1, tag2) = tag {
        if let crate::jbo_prop::JboConnective::JboConnLog(_, lcon) = con.as_ref() {
            let left = Prop::Modal(
                crate::jbo_prop::JboModalOp::Tagged((**tag1).clone(), mt.clone()),
                Box::new(inner.clone()),
            );
            let right = Prop::Modal(
                crate::jbo_prop::JboModalOp::Tagged((**tag2).clone(), mt.clone()),
                Box::new(inner.clone()),
            );
            Some(crate::jbo_prop::conn_to_fol(lcon, left, right))
        } else {
            None
        }
    } else {
        None
    }
}

// Ported from: JboShow.hs :: positionallyTaggedTerms (scalar negation term reordering)
fn scalar_negated_terms(terms: &[JboTerm]) -> Vec<JboTerm> {
    if terms.len() > 3 && matches!(terms.get(1), Some(JboTerm::Unfilled)) {
        std::iter::once(terms[0].clone())
            .chain(terms.iter().skip(3).cloned())
            .chain(std::iter::once(terms[2].clone()))
            .collect()
    } else {
        terms.to_vec()
    }
}

// Ported from: JboShow.hs :: logjboshow' Prop (main proposition formatting with variable tracking)
fn format_prop_at(
    prop: &Prop<
        JboRel,
        JboTerm,
        String,
        crate::jbo_prop::JboModalOp,
        crate::jbo_prop::JboQuantifier,
    >,
    next_var: i32,
) -> String {
    match prop {
        Prop::Rel(rel, terms) => {
            let terms = if matches!(rel, JboRel::ScalarNegatedRel(_, _)) {
                scalar_negated_terms(terms)
            } else {
                terms.clone()
            };
            // Special handling for fragments
            if let JboRel::Brivla(s) = rel {
                if s == "fragment" {
                    let terms_str = terms.iter().map(format_term).collect::<Vec<_>>().join(",");
                    return format!("[Fragment: {}]", terms_str);
                }
                if s == "unparsed-fragment" {
                    return "[Fragment]".to_string();
                }
            }

            // Ported from: JboShow.hs lines 598-604
            if matches!(rel, JboRel::Equal) {
                return format!(
                    "({})",
                    terms
                        .iter()
                        .map(format_term)
                        .collect::<Vec<_>>()
                        .join(" == ")
                );
            }
            if let JboRel::Among(t) = rel {
                if matches!(terms.as_slice(), [JboTerm::BoundVar(0)]) {
                    return format!("(_ ≤ {})", format_term(t));
                }
                let terms_str = terms.iter().map(format_term).collect::<Vec<_>>().join(",");
                return format!("({} ≤ {})", terms_str, format_term(t));
            }
            if let JboRel::AppliedRel(r, applied_terms) = rel {
                if let JboRel::Among(t) = r.as_ref() {
                    if terms.is_empty()
                        && matches!(applied_terms.as_slice(), [JboTerm::BoundVar(0)])
                    {
                        return format!("(_ ≤ {})", format_term(t));
                    }
                }
            }

            // Ported from: JboShow.hs lines 605-609
            // logjboshow' False [] (Rel r ts) =
            //     do s <- logshow r
            //        tss <- mapM logshow ts
            //        return $ [s ++ "(" ++ (intercalate "," tss) ++ ")"]
            let rel_str = format_rel(rel);
            let terms_str = terms.iter().map(format_term).collect::<Vec<_>>().join(",");
            format!("{}({})", rel_str, terms_str)
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::NonVeridical, inner) => {
            if let Prop::Quantified(q, restriction, body) = inner.as_ref() {
                let n = next_var;
                let mut prefix = match q {
                    JboQuantifier::RelQuantifier(inner_q) => {
                        format_quantified_prefix(inner_q, &format!("R{}", n))
                    }
                    _ => format_quantified_prefix(q, &format!("x{}", n)),
                };
                if let Some(r) = restriction {
                    prefix.push_str(":(");
                    prefix.push_str(&format_restriction_prop(&r(n), n));
                    prefix.push_str("). ");
                } else {
                    prefix.push_str(". ");
                }
                let body_next_var = if matches!(q, JboQuantifier::QuestionQuantifier) {
                    n
                } else {
                    n + 1
                };
                format!(
                    "non-veridical: {}{}",
                    prefix,
                    format_prop_at(&body(n), body_next_var)
                )
            } else {
                format!("non-veridical: {}", format_prop_at(inner, next_var))
            }
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::Tagged(tag, mt), inner) => {
            if let Some(expanded) = expand_connected_tag_modal(tag, mt, inner) {
                return format_prop_at(&expanded, next_var);
            }
            let term = mt.as_ref().map(format_term).unwrap_or_default();
            format!(
                "({})({}). {}",
                format_tag(tag),
                term,
                format_prop_at(inner, next_var)
            )
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::WithEventAs(t), inner) => {
            format!("{}=. {}", format_term(t), format_prop_at(inner, next_var))
        }
        Prop::Modal(crate::jbo_prop::JboModalOp::QTruthModal, inner) => {
            format!("?. {}", format_prop_at(inner, next_var))
        }
        Prop::Not(inner) => format!("¬{}", format_prop_at(inner, next_var)),
        Prop::Connected(conn, p1, p2) => {
            format!(
                "({} {} {})",
                format_prop_at(p1, next_var),
                conn,
                format_prop_at(p2, next_var)
            )
        }
        Prop::NonLogConnected(joik, p1, p2) => {
            format!(
                "({} {{{}}} {})",
                format_prop_at(p1, next_var),
                joik,
                format_prop_at(p2, next_var)
            )
        }
        Prop::Quantified(q, restriction, body) => {
            let n = next_var;
            let mut prefix = match q {
                JboQuantifier::RelQuantifier(inner_q) => {
                    format_quantified_prefix(inner_q, &format!("R{}", n))
                }
                _ => format_quantified_prefix(q, &format!("x{}", n)),
            };
            let body_prop = body(n);
            if let Some(r) = restriction {
                prefix.push_str(":(");
                prefix.push_str(&format_restriction_prop(&r(n), n));
                prefix.push_str("). ");
            } else {
                prefix.push_str(". ");
            }
            if let Prop::Modal(crate::jbo_prop::JboModalOp::NonVeridical, inner) = body_prop {
                format!("non-veridical: {}{}", prefix, format_prop_at(&inner, n + 1))
            } else {
                format!("{}{}", prefix, format_prop_at(&body_prop, n + 1))
            }
        }
        Prop::Eet => "_|_".to_string(),
    }
}

// Ported from: JboShow.hs :: jboshow (canonical output with .i statement joining)
fn push_canonical_line(lines: &mut Vec<String>, lojban: &str) {
    for line in lojban.lines().filter(|line| !line.is_empty()) {
        if lines.is_empty() || line.starts_with(".i ") {
            lines.push(line.to_string());
        } else {
            lines.push(format!(".i {}", line));
        }
    }
}

// Ported from: JboShow.hs :: instance JboShow Texticule (side texticule collection)
// Mirrors JboParse.hs :: replaceLastTermInProp storing discursive frees on TermWithSides
fn collect_term_side_texticules_from_texticule(texticule: &Texticule, out: &mut Vec<Texticule>) {
    match texticule {
        Texticule::TexticuleFrag(JboFragment::JboFragTerms(terms)) => {
            for term in terms {
                collect_term_side_texticules_from_term(term, out);
            }
        }
        Texticule::TexticuleProp(prop) => collect_term_side_texticules(prop, out),
        Texticule::TexticuleSide(_, inner) => {
            collect_term_side_texticules_from_texticule(inner, out)
        }
        Texticule::TexticuleFrag(JboFragment::JboFragUnparsed(_)) => {}
    }
}

// Rust adaptation: Helper for collect_term_side_texticules_from_texticule
fn collect_term_side_texticules_from_term(term: &JboTerm, out: &mut Vec<Texticule>) {
    match term {
        JboTerm::TermWithSides(inner, sides) => {
            out.extend(sides.iter().cloned());
            collect_term_side_texticules_from_term(inner, out);
        }
        JboTerm::Constant(_, args) => {
            for arg in args {
                collect_term_side_texticules_from_term(arg, out);
            }
        }
        JboTerm::PredNamed(pred) => collect_term_side_texticules(&pred(&JboTerm::Unfilled), out),
        JboTerm::Value(m) => collect_term_side_texticules_from_mex(m, out),
        JboTerm::QualifiedTerm(_, inner) => collect_term_side_texticules_from_term(inner, out),
        JboTerm::JoikedTerms(_, left, right) => {
            collect_term_side_texticules_from_term(left, out);
            collect_term_side_texticules_from_term(right, out);
        }
        JboTerm::JboQuote(_) => {}
        _ => {}
    }
}

// Rust adaptation: Helper for collect_term_side_texticules_from_term
fn collect_term_side_texticules_from_rel(rel: &JboRel, out: &mut Vec<Texticule>) {
    match rel {
        JboRel::Among(term) | JboRel::Moi(term, _) => {
            collect_term_side_texticules_from_term(term, out)
        }
        JboRel::Tanru(left, right) | JboRel::TanruConnective(_, left, right) => {
            collect_term_side_texticules_from_rel(left, out);
            collect_term_side_texticules_from_rel(right, out);
        }
        JboRel::AppliedRel(rel, terms) => {
            collect_term_side_texticules_from_rel(rel, out);
            for term in terms {
                collect_term_side_texticules_from_term(term, out);
            }
        }
        JboRel::PermutedRel(_, rel)
        | JboRel::ScalarNegatedRel(_, rel)
        | JboRel::ModalRel(_, rel) => {
            collect_term_side_texticules_from_rel(rel, out);
        }
        JboRel::OperatorRel(op) => collect_term_side_texticules_from_operator(op, out),
        JboRel::TagRel(tag) => collect_term_side_texticules_from_tag(tag, out),
        JboRel::AbsPred(_, npred) => {
            collect_term_side_texticules(&(npred.pred)(&vec![JboTerm::Unfilled; npred.arity]), out)
        }
        JboRel::AbsProp(_, prop) => collect_term_side_texticules(prop, out),
        JboRel::VPredRel(vpred) => collect_term_side_texticules(&vpred(&[JboTerm::Unfilled]), out),
        _ => {}
    }
}

// Rust adaptation: Helper for collect_term_side_texticules_from_rel
fn collect_term_side_texticules_from_mex(mex: &crate::jbo_prop::JboMex, out: &mut Vec<Texticule>) {
    match mex {
        crate::jbo_prop::JboMex::Operation(op, args) => {
            collect_term_side_texticules_from_operator(op, out);
            for arg in args {
                collect_term_side_texticules_from_mex(arg, out);
            }
        }
        crate::jbo_prop::JboMex::ConnectedMex(_, _, left, right) => {
            collect_term_side_texticules_from_mex(left, out);
            collect_term_side_texticules_from_mex(right, out);
        }
        crate::jbo_prop::JboMex::QualifiedMex(_, inner) => {
            collect_term_side_texticules_from_mex(inner, out)
        }
        crate::jbo_prop::JboMex::MexSelbri(vpred) => {
            collect_term_side_texticules(&vpred(&[JboTerm::Unfilled]), out)
        }
        crate::jbo_prop::JboMex::MexSumti(term) => {
            collect_term_side_texticules_from_term(term, out)
        }
        crate::jbo_prop::JboMex::MexArray(ms) => {
            for m in ms {
                collect_term_side_texticules_from_mex(m, out);
            }
        }
        _ => {}
    }
}

// Rust adaptation: Helper for collect_term_side_texticules_from_mex
fn collect_term_side_texticules_from_operator(
    op: &crate::jbo_prop::JboOperator,
    out: &mut Vec<Texticule>,
) {
    match op {
        crate::jbo_prop::JboOperator::ConnectedOperator(_, _, left, right) => {
            collect_term_side_texticules_from_operator(left, out);
            collect_term_side_texticules_from_operator(right, out);
        }
        crate::jbo_prop::JboOperator::OpPermuted(_, op)
        | crate::jbo_prop::JboOperator::OpScalarNegated(_, op) => {
            collect_term_side_texticules_from_operator(op, out);
        }
        crate::jbo_prop::JboOperator::OpMex(mex) => collect_term_side_texticules_from_mex(mex, out),
        crate::jbo_prop::JboOperator::OpSelbri(vpred) => {
            collect_term_side_texticules(&vpred(&[JboTerm::Unfilled]), out)
        }
        crate::jbo_prop::JboOperator::OpVUhU(_) => {}
    }
}

// Rust adaptation: Helper for collect_term_side_texticules_from_operator
fn collect_term_side_texticules_from_tag(tag: &crate::jbo_prop::JboTag, out: &mut Vec<Texticule>) {
    match tag {
        crate::jbo_prop::JboTag::DecoratedTagUnits(units) => {
            for unit in units {
                match &unit.tag_unit {
                    crate::jbo_prop::JboTagUnit::ROI(_, _, mex) => {
                        collect_term_side_texticules_from_mex(mex, out)
                    }
                    crate::jbo_prop::JboTagUnit::FIhO(vpred) => {
                        collect_term_side_texticules(&vpred(&[JboTerm::Unfilled]), out)
                    }
                    _ => {}
                }
            }
        }
        crate::jbo_prop::JboTag::ConnectedTag(_, left, right) => {
            collect_term_side_texticules_from_tag(left, out);
            collect_term_side_texticules_from_tag(right, out);
        }
    }
}

// Ported from: JboShow.hs :: jboshow for side texticules (sei/to markers)
fn jboshow_side_texticule(texticule: &Texticule) -> String {
    match texticule {
        Texticule::TexticuleSide(crate::jbo_prop::SideType::SideDiscursive, inner) => {
            format!("sei {} se'u", crate::jbo_show::jboshow_texticule(inner))
        }
        Texticule::TexticuleSide(crate::jbo_prop::SideType::SideBracketed, inner) => {
            format!("to {} toi", crate::jbo_show::jboshow_texticule(inner))
        }
        _ => crate::jbo_show::jboshow_texticule(texticule),
    }
}

// Rust-only: Helper to check if proposition starts with NonVeridical modal
fn is_leading_nonveridical_prop(prop: &JboProp) -> bool {
    match prop {
        Prop::Modal(crate::jbo_prop::JboModalOp::NonVeridical, _) => true,
        Prop::Connected(crate::logic::Connective::And, left, _) => {
            is_leading_nonveridical_prop(left)
        }
        _ => false,
    }
}

// Rust-only: Helper to count leading NonVeridical propositions for side insertion
fn leading_nonveridical_count(result: &SemanticResult) -> usize {
    result
        .side_props
        .iter()
        .filter(|prop| is_leading_nonveridical_prop(prop))
        .count()
        + usize::from(is_leading_nonveridical_prop(&result.prop))
}

// Ported from: JboShow.hs :: jboshow for fragment terms
fn push_fragment_terms_line(lines: &mut Vec<String>, terms: &[JboTerm]) {
    if !terms.is_empty() {
        let frag = terms
            .iter()
            .map(crate::jbo_show::jboshow_term)
            .collect::<Vec<_>>()
            .join(" ");
        push_canonical_line(lines, &frag);
    } else if !lines.is_empty() {
        lines.push(".i ".to_string());
    }
}

// Rust adaptation: Helper for collect_term_side_texticules_from_texticule
fn collect_term_side_texticules(prop: &JboProp, out: &mut Vec<Texticule>) {
    match prop {
        Prop::Rel(rel, terms) => {
            collect_term_side_texticules_from_rel(rel, out);
            for term in terms {
                collect_term_side_texticules_from_term(term, out);
            }
        }
        Prop::Not(inner) | Prop::Modal(_, inner) => collect_term_side_texticules(inner, out),
        Prop::Connected(_, left, right) | Prop::NonLogConnected(_, left, right) => {
            collect_term_side_texticules(left, out);
            collect_term_side_texticules(right, out);
        }
        Prop::Quantified(_, _, body) => {
            collect_term_side_texticules(&body(1), out);
        }
        Prop::Eet => {}
    }
}

// Ported from: JboShow.hs :: jboshow + logshow (CLI output formatting)
// Combines logical LogShow lines with canonical JboShow text
fn format_result(result: &SemanticResult, preserve_term_side_markers: bool) -> (String, String) {
    let mut logical_lines = vec![];
    let mut term_sides = Vec::new();
    collect_term_side_texticules(&result.prop, &mut term_sides);

    if result.side_texticules.is_empty() {
        for prop in &result.side_props {
            logical_lines.push(format_prop(prop));
        }
    } else {
        for texticule in &result.side_texticules {
            logical_lines.push(format_texticule(texticule));
        }
    }
    for texticule in &term_sides {
        logical_lines.push(format_texticule(texticule));
    }

    logical_lines.push(format_prop(&result.prop));

    let mut canonical_lines = Vec::new();
    if preserve_term_side_markers && !result.side_texticules.is_empty() {
        for texticule in &result.side_texticules {
            push_canonical_line(&mut canonical_lines, &jboshow_side_texticule(texticule));
        }
        match &result.prop {
            Prop::Rel(JboRel::Brivla(name), terms) if name == "fragment" => {
                push_fragment_terms_line(&mut canonical_lines, terms);
            }
            _ => push_canonical_line(
                &mut canonical_lines,
                &crate::jbo_show::prop_to_lojban(&result.prop),
            ),
        }
    } else {
        push_canonical_line(&mut canonical_lines, &result.lojban_output);
    }
    let side_lines = term_sides
        .into_iter()
        .flat_map(|texticule| {
            let shown = if preserve_term_side_markers {
                jboshow_side_texticule(&texticule)
            } else {
                crate::jbo_show::jboshow_texticule(&texticule)
            };
            shown
                .lines()
                .filter(|line| !line.is_empty())
                .map(str::to_string)
                .collect::<Vec<_>>()
        })
        .collect::<Vec<_>>();
    if !side_lines.is_empty() {
        let insert_at = leading_nonveridical_count(result).min(canonical_lines.len());
        for (i, line) in side_lines.into_iter().enumerate() {
            let line = if insert_at + i == 0 || line.starts_with(".i ") {
                line
            } else {
                format!(".i {}", line)
            };
            canonical_lines.insert(insert_at + i, line);
        }
    }

    let logical = logical_lines.join("\n");
    let canonical = canonical_lines.join("\n");
    (logical, canonical)
}

// Ported from: Main.hs :: evalText output formatting
/// Return `(logical, canonical, graph_json)` for CLI/JSON output.
pub fn eval_text_to_outputs_with_options(
    text: &Text,
    include_term_sides: bool,
) -> (String, String, String) {
    eval_text_to_outputs_with_runtime_options(text, include_term_sides, false)
}

pub fn eval_text_to_outputs_with_runtime_options(
    text: &Text,
    include_term_sides: bool,
    indicator_texticules: bool,
) -> (String, String, String) {
    let results = eval_text_with_options(text, indicator_texticules);

    if results.is_empty() {
        return (String::new(), String::new(), "{}".to_string());
    }

    let mut logical_lines = Vec::new();
    let mut canonical_lines = Vec::new();

    for result in results.iter() {
        let (logical, canonical) = format_result(result, include_term_sides);
        logical_lines.push(logical);
        for line in canonical.lines().filter(|line| !line.is_empty()) {
            if canonical_lines.is_empty() || line.starts_with(".i ") {
                canonical_lines.push(line.to_string());
            } else {
                canonical_lines.push(format!(".i {}", line));
            }
        }
    }

    let logical = logical_lines.join("\n");
    let canonical = canonical_lines.join("\n");
    let mut props = Vec::new();
    for result in &results {
        props.extend(result.side_props.iter().cloned());
        let mut term_sides = Vec::new();
        collect_term_side_texticules(&result.prop, &mut term_sides);
        props.extend(
            term_sides
                .into_iter()
                .filter_map(|texticule| match texticule {
                    Texticule::TexticuleProp(prop) => Some(prop),
                    _ => None,
                }),
        );
        props.push(result.prop.clone());
    }
    let graph = crate::jbo_tree::jbo_props_to_graph(&props);
    let graph_json = serde_json::to_string(&graph).unwrap_or_else(|_| "{}".to_string());

    (logical, canonical, graph_json)
}

// Ported from: Main.hs :: evalText output formatting
pub fn eval_text_to_outputs(text: &Text) -> (String, String, String) {
    eval_text_to_outputs_with_options(text, false)
}

/// Return Prolog source code for the parsed text.
pub fn eval_text_to_prolog(text: &Text) -> String {
    let results = eval_text(text);
    crate::jbo_prolog::semantic_results_to_prolog(&results)
}