1use crate::{
4 env::{Binder, Hints, QuotKind},
5 error::{corrupt, Result},
6 record::{
7 Binding, Counts, Decl, Head, IndCtor, IndRec, IndRule, IndType, Record, Safety, Sink,
8 },
9};
10use serde::Deserialize;
11use serde_json::value::RawValue;
12use std::io::{BufRead, Write};
13
14#[derive(Debug)]
16pub struct Ndjson<W: Write> {
17 out: W,
18 buf: Vec<u8>,
19 next: Counts,
20}
21
22impl<W: Write> Ndjson<W> {
23 pub const fn new(out: W) -> Self {
24 Self {
25 out,
26 buf: Vec::new(),
27 next: Counts {
28 names: 1,
29 levels: 1,
30 exprs: 0,
31 },
32 }
33 }
34
35 fn start(&mut self) -> &mut Self {
36 self.buf.clear();
37 self
38 }
39
40 fn emit(&mut self) -> Result<()> {
41 self.buf.push(b'\n');
42 self.out.write_all(&self.buf)?;
43 Ok(())
44 }
45
46 fn s(&mut self, s: &str) -> &mut Self {
47 self.buf.extend_from_slice(s.as_bytes());
48 self
49 }
50
51 fn n(&mut self, n: impl itoa::Integer) -> &mut Self {
52 self.s(itoa::Buffer::new().format(n))
53 }
54
55 fn b(&mut self, b: bool) -> &mut Self {
56 self.s(if b { "true" } else { "false" })
57 }
58
59 fn list(&mut self, ids: &[u32]) -> &mut Self {
60 self.s("[");
61 for (i, &id) in ids.iter().enumerate() {
62 if i > 0 {
63 self.s(",");
64 }
65 self.n(id);
66 }
67 self.s("]")
68 }
69
70 fn q(&mut self, s: &str) -> &mut Self {
71 self.s("\"");
72 for c in s.chars() {
73 match c {
74 '"' => self.s("\\\""),
75 '\\' => self.s("\\\\"),
76 '\n' => self.s("\\n"),
77 '\r' => self.s("\\r"),
78 '\t' => self.s("\\t"),
79 c if (c as u32) < 0x20 || c == '\u{7f}' => {
80 self.s("\\u00").s(&format!("{:02x}", c as u32))
81 }
82 c => self.s(c.encode_utf8(&mut [0; 4])),
83 };
84 }
85 self.s("\"")
86 }
87
88 fn lp_name(&mut self, h: &Head) -> &mut Self {
90 self.s(r#""levelParams":"#)
91 .list(&h.level_params)
92 .s(r#","name":"#)
93 .n(h.name)
94 }
95
96 fn unsafe_(&mut self, u: bool) -> &mut Self {
97 self.s(r#""isUnsafe":"#).b(u)
98 }
99
100 fn ctor(&mut self, c: &IndCtor) {
101 self.s(r#"{"cidx":"#)
102 .n(c.cidx)
103 .s(r#","induct":"#)
104 .n(c.induct)
105 .s(",")
106 .unsafe_(c.is_unsafe)
107 .s(",")
108 .lp_name(&c.head)
109 .s(r#","numFields":"#)
110 .n(c.num_fields)
111 .s(r#","numParams":"#)
112 .n(c.num_params)
113 .s(r#","type":"#)
114 .n(c.head.ty)
115 .s("}");
116 }
117
118 fn rec(&mut self, r: &IndRec) {
119 self.s(r#"{"all":"#)
120 .list(&r.all)
121 .s(",")
122 .unsafe_(r.is_unsafe)
123 .s(r#","k":"#)
124 .b(r.k)
125 .s(",")
126 .lp_name(&r.head)
127 .s(r#","numIndices":"#)
128 .n(r.num_indices)
129 .s(r#","numMinors":"#)
130 .n(r.num_minors)
131 .s(r#","numMotives":"#)
132 .n(r.num_motives)
133 .s(r#","numParams":"#)
134 .n(r.num_params)
135 .s(r#","rules":["#);
136 for (i, rule) in r.rules.iter().enumerate() {
137 if i > 0 {
138 self.s(",");
139 }
140 self.s(r#"{"ctor":"#)
141 .n(rule.ctor)
142 .s(r#","nfields":"#)
143 .n(rule.nfields)
144 .s(r#","rhs":"#)
145 .n(rule.rhs)
146 .s("}");
147 }
148 self.s(r#"],"type":"#).n(r.head.ty).s("}");
149 }
150
151 fn ty(&mut self, t: &IndType) {
152 self.s(r#"{"all":"#)
153 .list(&t.all)
154 .s(r#","ctors":"#)
155 .list(&t.ctors)
156 .s(r#","isRec":"#)
157 .b(t.is_rec)
158 .s(r#","isReflexive":"#)
159 .b(t.is_reflexive)
160 .s(",")
161 .unsafe_(t.is_unsafe)
162 .s(",")
163 .lp_name(&t.head)
164 .s(r#","numIndices":"#)
165 .n(t.num_indices)
166 .s(r#","numNested":"#)
167 .n(t.num_nested)
168 .s(r#","numParams":"#)
169 .n(t.num_params)
170 .s(r#","type":"#)
171 .n(t.head.ty)
172 .s("}");
173 }
174
175 fn all_opt(&mut self, all: Option<&Vec<u32>>) -> &mut Self {
176 if let Some(all) = all {
177 self.s(r#""all":"#).list(all).s(",");
178 }
179 self
180 }
181}
182
183const fn binder(b: Binder) -> &'static str {
184 match b {
185 Binder::Default => "default",
186 Binder::Implicit => "implicit",
187 Binder::StrictImplicit => "strictImplicit",
188 Binder::InstImplicit => "instImplicit",
189 }
190}
191
192impl<W: Write> Sink for Ndjson<W> {
193 type Output = W;
194
195 fn record(&mut self, r: &Record<'_>) -> Result<()> {
196 let Some(id) = self.next.assign(r) else {
197 self.start();
198 match r {
199 Record::Meta(json) => {
200 self.start().s(r#"{"meta":"#).s(json).s("}");
201 }
202 Record::Decl(d) => self.decl(d),
203 _ => return Ok(()),
204 }
205 return self.emit();
206 };
207 self.start();
208 let level = |this: &mut Self, key: &str| {
209 this.s(r#"{"il":"#).n(id).s(r#",""#).s(key).s(r#"":"#);
210 };
211 let binding = |this: &mut Self, b: &Binding| {
212 this.s(r#"{"binderInfo":""#)
213 .s(binder(b.info))
214 .s(r#"","body":"#)
215 .n(b.body)
216 .s(r#","name":"#)
217 .n(b.name)
218 .s(r#","type":"#)
219 .n(b.ty)
220 .s("}");
221 };
222 match r {
223 Record::NameStr { pre, str } => {
224 self.s(r#"{"in":"#)
225 .n(id)
226 .s(r#","str":{"pre":"#)
227 .n(*pre)
228 .s(r#","str":"#)
229 .q(str)
230 .s("}}");
231 }
232 Record::NameNum { pre, i } => {
233 self.s(r#"{"in":"#)
234 .n(id)
235 .s(r#","num":{"i":"#)
236 .n(*i)
237 .s(r#","pre":"#)
238 .n(*pre)
239 .s("}}");
240 }
241 Record::Succ(a) => {
242 level(self, "succ");
243 self.n(*a).s("}");
244 }
245 Record::Param(a) => {
246 level(self, "param");
247 self.n(*a).s("}");
248 }
249 Record::Max(a, b) | Record::IMax(a, b) => {
250 level(
251 self,
252 if matches!(r, Record::Max(..)) {
253 "max"
254 } else {
255 "imax"
256 },
257 );
258 self.s("[").n(*a).s(",").n(*b).s("]}");
259 }
260 Record::BVar(i) => {
261 self.s(r#"{"bvar":"#).n(*i).s(r#","ie":"#).n(id).s("}");
262 }
263 Record::Sort(l) => {
264 self.s(r#"{"ie":"#).n(id).s(r#","sort":"#).n(*l).s("}");
265 }
266 Record::Const { name, us } => {
267 self.s(r#"{"const":{"name":"#)
268 .n(*name)
269 .s(r#","us":"#)
270 .list(us)
271 .s(r#"},"ie":"#)
272 .n(id)
273 .s("}");
274 }
275 Record::App { fun, arg } => {
276 self.s(r#"{"app":{"arg":"#)
277 .n(*arg)
278 .s(r#","fn":"#)
279 .n(*fun)
280 .s(r#"},"ie":"#)
281 .n(id)
282 .s("}");
283 }
284 Record::Lam(b) => {
285 self.s(r#"{"ie":"#).n(id).s(r#","lam":"#);
286 binding(self, b);
287 self.s("}");
288 }
289 Record::Pi(b) => {
290 self.s(r#"{"forallE":"#);
291 binding(self, b);
292 self.s(r#","ie":"#).n(id).s("}");
293 }
294 Record::Let {
295 name,
296 ty,
297 value,
298 body,
299 nondep,
300 } => {
301 self.s(r#"{"ie":"#)
302 .n(id)
303 .s(r#","letE":{"body":"#)
304 .n(*body)
305 .s(r#","name":"#)
306 .n(*name)
307 .s(r#","nondep":"#)
308 .b(*nondep)
309 .s(r#","type":"#)
310 .n(*ty)
311 .s(r#","value":"#)
312 .n(*value)
313 .s("}}");
314 }
315 Record::Nat(v) => {
316 self.s(r#"{"ie":"#)
317 .n(id)
318 .s(r#","natVal":""#)
319 .s(v)
320 .s(r#""}"#);
321 }
322 Record::Str(v) => {
323 self.s(r#"{"ie":"#).n(id).s(r#","strVal":"#).q(v).s("}");
324 }
325 Record::Proj {
326 type_name,
327 idx,
328 struct_,
329 } => {
330 self.s(r#"{"ie":"#)
331 .n(id)
332 .s(r#","proj":{"idx":"#)
333 .n(*idx)
334 .s(r#","struct":"#)
335 .n(*struct_)
336 .s(r#","typeName":"#)
337 .n(*type_name)
338 .s("}}");
339 }
340 Record::MData(x) => {
341 self.s(r#"{"ie":"#)
342 .n(id)
343 .s(r#","mdata":{"expr":"#)
344 .n(*x)
345 .s("}}");
346 }
347 Record::Meta(_) | Record::Decl(_) | Record::End(_) => unreachable!(),
348 }
349 self.emit()
350 }
351
352 fn finish(mut self) -> Result<W> {
353 self.out.flush()?;
354 Ok(self.out)
355 }
356}
357
358impl<W: Write> Ndjson<W> {
359 fn decl(&mut self, d: &Decl) {
360 match d {
361 Decl::Axiom {
362 head,
363 is_unsafe,
364 all,
365 } => {
366 self.s(r#"{"axiom":{"#)
367 .all_opt(all.as_ref())
368 .unsafe_(*is_unsafe)
369 .s(",")
370 .lp_name(head)
371 .s(r#","type":"#)
372 .n(head.ty)
373 .s("}}");
374 }
375 Decl::Def {
376 head,
377 value,
378 hints,
379 safety,
380 all,
381 } => {
382 self.s(r#"{"def":{"all":"#).list(all).s(r#","hints":"#);
383 match hints {
384 Hints::Opaque => self.s(r#""opaque""#),
385 Hints::Abbrev => self.s(r#""abbrev""#),
386 Hints::Regular(h) => self.s(r#"{"regular":"#).n(*h).s("}"),
387 };
388 let safety = match safety {
389 Safety::Safe => "safe",
390 Safety::Unsafe => "unsafe",
391 Safety::Partial => "partial",
392 };
393 self.s(",")
394 .lp_name(head)
395 .s(r#","safety":""#)
396 .s(safety)
397 .s(r#"","type":"#)
398 .n(head.ty)
399 .s(r#","value":"#)
400 .n(*value)
401 .s("}}");
402 }
403 Decl::Thm { head, value, all } => {
404 self.s(r#"{"thm":{"all":"#)
405 .list(all)
406 .s(",")
407 .lp_name(head)
408 .s(r#","type":"#)
409 .n(head.ty)
410 .s(r#","value":"#)
411 .n(*value)
412 .s("}}");
413 }
414 Decl::Opaque {
415 head,
416 value,
417 is_unsafe,
418 all,
419 } => {
420 self.s(r#"{"opaque":{"#)
421 .all_opt(all.as_ref())
422 .unsafe_(*is_unsafe)
423 .s(",")
424 .lp_name(head)
425 .s(r#","type":"#)
426 .n(head.ty)
427 .s(r#","value":"#)
428 .n(*value)
429 .s("}}");
430 }
431 Decl::Quot { head, kind } => {
432 let kind = match kind {
433 QuotKind::Type => "type",
434 QuotKind::Ctor => "ctor",
435 QuotKind::Lift => "lift",
436 QuotKind::Ind => "ind",
437 };
438 self.s(r#"{"quot":{"kind":""#)
439 .s(kind)
440 .s(r#"","#)
441 .lp_name(head)
442 .s(r#","type":"#)
443 .n(head.ty)
444 .s("}}");
445 }
446 Decl::Inductive { types, ctors, recs } => {
447 self.s(r#"{"inductive":{"ctors":["#);
448 for (i, c) in ctors.iter().enumerate() {
449 self.sep(i).ctor(c);
450 }
451 self.s(r#"],"recs":["#);
452 for (i, r) in recs.iter().enumerate() {
453 self.sep(i).rec(r);
454 }
455 self.s(r#"],"types":["#);
456 for (i, t) in types.iter().enumerate() {
457 self.sep(i).ty(t);
458 }
459 self.s("]}}");
460 }
461 }
462 }
463
464 fn sep(&mut self, i: usize) -> &mut Self {
465 if i > 0 {
466 self.s(",");
467 }
468 self
469 }
470}
471
472pub fn read<S: Sink>(input: impl BufRead, mut sink: S) -> Result<S::Output> {
474 let mut next = Counts::default();
475 for (n, line) in input.lines().enumerate() {
476 let line = line?;
477 if line.is_empty() {
478 continue;
479 }
480 let at = |e: serde_json::Error| corrupt(format!("line {}: {e}", n + 1));
481 let (id, r) = if line.starts_with(r#"{"meta":"#) {
482 let m: Meta<'_> = serde_json::from_str(&line).map_err(at)?;
483 (None, Record::Meta(m.meta.get().into()))
484 } else {
485 serde_json::from_str::<Line>(&line).map_err(at)?.record()
486 };
487 if let (Some(id), Some(want)) = (id, next.assign(&r)) {
488 if id != want {
489 return Err(corrupt(format!("line {}: id {id}, expected {want}", n + 1)));
490 }
491 }
492 sink.record(&r)?;
493 }
494 sink.record(&Record::End(next))?;
495 sink.finish()
496}
497
498#[derive(Deserialize)]
499struct Meta<'a> {
500 #[serde(borrow)]
501 meta: &'a RawValue,
502}
503
504#[derive(Deserialize)]
505#[serde(untagged)]
506enum Line {
507 Name {
508 #[serde(rename = "in")]
509 id: u32,
510 #[serde(flatten)]
511 name: JName,
512 },
513 Level {
514 il: u32,
515 #[serde(flatten)]
516 level: JLevel,
517 },
518 Expr {
519 ie: u32,
520 #[serde(flatten)]
521 expr: JExpr,
522 },
523 Decl(JDecl),
524}
525
526#[derive(Deserialize)]
527#[serde(rename_all = "lowercase")]
528enum JName {
529 Str { pre: u32, str: String },
530 Num { pre: u32, i: u64 },
531}
532
533#[derive(Deserialize)]
534#[serde(rename_all = "lowercase")]
535enum JLevel {
536 Succ(u32),
537 Max(u32, u32),
538 IMax(u32, u32),
539 Param(u32),
540}
541
542#[derive(Deserialize)]
543#[serde(rename_all = "camelCase")]
544struct JBinding {
545 binder_info: Binder,
546 name: u32,
547 #[serde(rename = "type")]
548 ty: u32,
549 body: u32,
550}
551
552#[derive(Deserialize)]
553#[serde(rename_all = "camelCase")]
554enum JExpr {
555 Bvar(u64),
556 Sort(u32),
557 Const {
558 name: u32,
559 us: Vec<u32>,
560 },
561 App {
562 #[serde(rename = "fn")]
563 fun: u32,
564 arg: u32,
565 },
566 Lam(JBinding),
567 ForallE(JBinding),
568 LetE {
569 name: u32,
570 #[serde(rename = "type")]
571 ty: u32,
572 value: u32,
573 body: u32,
574 #[serde(default)]
575 nondep: bool,
576 },
577 NatVal(String),
578 StrVal(String),
579 #[serde(rename_all = "camelCase")]
580 Proj {
581 type_name: u32,
582 idx: u64,
583 #[serde(rename = "struct")]
584 struct_: u32,
585 },
586 Mdata {
587 expr: u32,
588 },
589}
590
591#[derive(Deserialize)]
592#[serde(rename_all = "camelCase")]
593struct JHead {
594 name: u32,
595 level_params: Vec<u32>,
596 #[serde(rename = "type")]
597 ty: u32,
598}
599
600impl From<JHead> for Head {
601 fn from(h: JHead) -> Self {
602 Self {
603 name: h.name,
604 level_params: h.level_params,
605 ty: h.ty,
606 }
607 }
608}
609
610#[derive(Deserialize)]
611#[serde(rename_all = "lowercase")]
612enum JSafety {
613 Safe,
614 Unsafe,
615 Partial,
616}
617
618#[derive(Deserialize)]
619#[serde(rename_all = "camelCase")]
620struct JUnsafe {
621 #[serde(flatten)]
622 head: JHead,
623 #[serde(default)]
624 is_unsafe: bool,
625}
626
627#[derive(Deserialize)]
628#[serde(rename_all = "lowercase")]
629enum JDecl {
630 Axiom {
631 #[serde(flatten)]
632 head: JUnsafe,
633 all: Option<Vec<u32>>,
634 },
635 Def {
636 #[serde(flatten)]
637 head: JHead,
638 value: u32,
639 hints: Hints,
640 safety: JSafety,
641 all: Vec<u32>,
642 },
643 Thm {
644 #[serde(flatten)]
645 head: JHead,
646 value: u32,
647 all: Vec<u32>,
648 },
649 Opaque {
650 #[serde(flatten)]
651 head: JUnsafe,
652 value: u32,
653 all: Option<Vec<u32>>,
654 },
655 Quot {
656 #[serde(flatten)]
657 head: JHead,
658 kind: QuotKind,
659 },
660 Inductive {
661 types: Vec<JType>,
662 ctors: Vec<JCtor>,
663 recs: Vec<JRec>,
664 },
665}
666
667#[derive(Deserialize)]
668#[serde(rename_all = "camelCase")]
669struct JType {
670 #[serde(flatten)]
671 head: JUnsafe,
672 num_params: u64,
673 num_indices: u64,
674 all: Vec<u32>,
675 ctors: Vec<u32>,
676 num_nested: u64,
677 is_rec: bool,
678 is_reflexive: bool,
679}
680
681#[derive(Deserialize)]
682#[serde(rename_all = "camelCase")]
683struct JCtor {
684 #[serde(flatten)]
685 head: JUnsafe,
686 induct: u32,
687 cidx: u64,
688 num_params: u64,
689 num_fields: u64,
690}
691
692#[derive(Deserialize)]
693#[serde(rename_all = "camelCase")]
694struct JRec {
695 #[serde(flatten)]
696 head: JUnsafe,
697 all: Vec<u32>,
698 num_params: u64,
699 num_indices: u64,
700 num_motives: u64,
701 num_minors: u64,
702 rules: Vec<IndRule>,
703 k: bool,
704}
705
706impl Line {
707 fn record(self) -> (Option<u32>, Record<'static>) {
708 match self {
709 Self::Name { id, name } => (
710 Some(id),
711 match name {
712 JName::Str { pre, str } => Record::NameStr {
713 pre,
714 str: str.into(),
715 },
716 JName::Num { pre, i } => Record::NameNum { pre, i },
717 },
718 ),
719 Self::Level { il, level } => (
720 Some(il),
721 match level {
722 JLevel::Succ(a) => Record::Succ(a),
723 JLevel::Max(a, b) => Record::Max(a, b),
724 JLevel::IMax(a, b) => Record::IMax(a, b),
725 JLevel::Param(n) => Record::Param(n),
726 },
727 ),
728 Self::Expr { ie, expr } => (Some(ie), expr.into()),
729 Self::Decl(d) => (None, Record::Decl(d.into())),
730 }
731 }
732}
733
734impl From<JBinding> for Binding {
735 fn from(b: JBinding) -> Self {
736 Self {
737 info: b.binder_info,
738 name: b.name,
739 ty: b.ty,
740 body: b.body,
741 }
742 }
743}
744
745impl From<JExpr> for Record<'_> {
746 fn from(e: JExpr) -> Self {
747 match e {
748 JExpr::Bvar(i) => Self::BVar(i),
749 JExpr::Sort(l) => Self::Sort(l),
750 JExpr::Const { name, us } => Self::Const { name, us },
751 JExpr::App { fun, arg } => Self::App { fun, arg },
752 JExpr::Lam(b) => Self::Lam(b.into()),
753 JExpr::ForallE(b) => Self::Pi(b.into()),
754 JExpr::LetE {
755 name,
756 ty,
757 value,
758 body,
759 nondep,
760 } => Self::Let {
761 name,
762 ty,
763 value,
764 body,
765 nondep,
766 },
767 JExpr::NatVal(v) => Self::Nat(v.into()),
768 JExpr::StrVal(v) => Self::Str(v.into()),
769 JExpr::Proj {
770 type_name,
771 idx,
772 struct_,
773 } => Self::Proj {
774 type_name,
775 idx,
776 struct_,
777 },
778 JExpr::Mdata { expr } => Self::MData(expr),
779 }
780 }
781}
782
783impl From<JDecl> for Decl {
784 fn from(d: JDecl) -> Self {
785 match d {
786 JDecl::Axiom { head, all } => Self::Axiom {
787 head: head.head.into(),
788 is_unsafe: head.is_unsafe,
789 all,
790 },
791 JDecl::Def {
792 head,
793 value,
794 hints,
795 safety,
796 all,
797 } => Self::Def {
798 head: head.into(),
799 value,
800 hints,
801 safety: match safety {
802 JSafety::Safe => Safety::Safe,
803 JSafety::Unsafe => Safety::Unsafe,
804 JSafety::Partial => Safety::Partial,
805 },
806 all,
807 },
808 JDecl::Thm { head, value, all } => Self::Thm {
809 head: head.into(),
810 value,
811 all,
812 },
813 JDecl::Opaque { head, value, all } => Self::Opaque {
814 head: head.head.into(),
815 value,
816 is_unsafe: head.is_unsafe,
817 all,
818 },
819 JDecl::Quot { head, kind } => Self::Quot {
820 head: head.into(),
821 kind,
822 },
823 JDecl::Inductive { types, ctors, recs } => Self::Inductive {
824 types: types
825 .into_iter()
826 .map(|t| IndType {
827 head: t.head.head.into(),
828 is_unsafe: t.head.is_unsafe,
829 num_params: t.num_params,
830 num_indices: t.num_indices,
831 all: t.all,
832 ctors: t.ctors,
833 num_nested: t.num_nested,
834 is_rec: t.is_rec,
835 is_reflexive: t.is_reflexive,
836 })
837 .collect(),
838 ctors: ctors
839 .into_iter()
840 .map(|c| IndCtor {
841 head: c.head.head.into(),
842 is_unsafe: c.head.is_unsafe,
843 induct: c.induct,
844 cidx: c.cidx,
845 num_params: c.num_params,
846 num_fields: c.num_fields,
847 })
848 .collect(),
849 recs: recs
850 .into_iter()
851 .map(|r| IndRec {
852 head: r.head.head.into(),
853 is_unsafe: r.head.is_unsafe,
854 all: r.all,
855 num_params: r.num_params,
856 num_indices: r.num_indices,
857 num_motives: r.num_motives,
858 num_minors: r.num_minors,
859 rules: r.rules,
860 k: r.k,
861 })
862 .collect(),
863 },
864 }
865 }
866}