egglog 2.0.0

egglog is a language that combines the benefits of equality saturation and datalog. It can be used for analysis, optimization, and synthesis of programs. It is the successor to the popular rust library egg.
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
//! Parse a string into egglog.

use crate::util::INTERNAL_SYMBOL_PREFIX;
use crate::*;
use egglog_ast::generic_ast::*;
use egglog_ast::span::{EgglogSpan, Span, SrcFile};
use ordered_float::OrderedFloat;

#[macro_export]
macro_rules! span {
    () => {
        Span::Rust(std::sync::Arc::new(RustSpan {
            file: file!(),
            line: line!(),
            column: column!(),
        }))
    };
}

// We do an unidiomatic thing here by using a struct instead of an enum.
// This is okay because we don't expect client code to respond
// differently to different parse errors. The benefit of this is that
// error messages are defined in the same place that they are created,
// making it easier to improve errors over time.
#[derive(Debug, Error)]
pub struct ParseError(pub Span, pub String);

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}\nparse error: {}", self.0, self.1)
    }
}

macro_rules! error {
    ($span:expr, $($fmt:tt)*) => {
        Err(ParseError($span, format!($($fmt)*)))
    };
}

pub enum Sexp {
    // Will never contain `Literal::Unit`, as this
    // will be parsed as an empty `Sexp::List`.
    Literal(Literal, Span),
    Atom(String, Span),
    List(Vec<Sexp>, Span),
}

impl Sexp {
    pub fn span(&self) -> Span {
        match self {
            Sexp::Literal(_, span) => span.clone(),
            Sexp::Atom(_, span) => span.clone(),
            Sexp::List(_, span) => span.clone(),
        }
    }

    pub fn expect_uint<UInt: TryFrom<u64>>(&self, e: &'static str) -> Result<UInt, ParseError> {
        if let Sexp::Literal(Literal::Int(x), _) = self {
            if *x >= 0 {
                if let Ok(v) = (*x as u64).try_into() {
                    return Ok(v);
                }
            }
        }
        error!(
            self.span(),
            "expected {e} to be a nonnegative integer literal"
        )
    }

    pub fn expect_string(&self, e: &'static str) -> Result<String, ParseError> {
        if let Sexp::Literal(Literal::String(x), _) = self {
            return Ok(x.to_string());
        }
        error!(self.span(), "expected {e} to be a string literal")
    }

    pub fn expect_atom(&self, e: &'static str) -> Result<String, ParseError> {
        if let Sexp::Atom(symbol, _) = self {
            return Ok(symbol.clone());
        }
        error!(self.span(), "expected {e}")
    }

    pub fn expect_list(&self, e: &'static str) -> Result<&[Sexp], ParseError> {
        if let Sexp::List(sexps, _) = self {
            return Ok(sexps);
        }
        error!(self.span(), "expected {e}")
    }

    pub fn expect_call(&self, e: &'static str) -> Result<(String, &[Sexp], Span), ParseError> {
        if let Sexp::List(sexps, span) = self {
            if let [Sexp::Atom(func, _), args @ ..] = sexps.as_slice() {
                return Ok((func.clone(), args, span.clone()));
            }
        }
        error!(self.span(), "expected {e}")
    }
}

// helper for mapping a function that returns `Result`
fn map_fallible<T>(
    slice: &[Sexp],
    parser: &mut Parser,
    func: impl Fn(&mut Parser, &Sexp) -> Result<T, ParseError>,
) -> Result<Vec<T>, ParseError> {
    slice
        .iter()
        .map(|sexp| func(parser, sexp))
        .collect::<Result<_, _>>()
}

pub trait Macro<T>: Send + Sync {
    fn name(&self) -> &str;
    fn parse(&self, args: &[Sexp], span: Span, parser: &mut Parser) -> Result<T, ParseError>;
}

pub struct SimpleMacro<T, F: Fn(&[Sexp], Span, &mut Parser) -> Result<T, ParseError> + Send + Sync>(
    String,
    F,
);

impl<T, F> SimpleMacro<T, F>
where
    F: Fn(&[Sexp], Span, &mut Parser) -> Result<T, ParseError> + Send + Sync,
{
    pub fn new(head: &str, f: F) -> Self {
        Self(head.to_owned(), f)
    }
}

impl<T, F> Macro<T> for SimpleMacro<T, F>
where
    F: Fn(&[Sexp], Span, &mut Parser) -> Result<T, ParseError> + Send + Sync,
{
    fn name(&self) -> &str {
        &self.0
    }

    fn parse(&self, args: &[Sexp], span: Span, parser: &mut Parser) -> Result<T, ParseError> {
        self.1(args, span, parser)
    }
}

#[derive(Clone)]
pub struct Parser {
    commands: HashMap<String, Arc<dyn Macro<Vec<Command>>>>,
    actions: HashMap<String, Arc<dyn Macro<Vec<Action>>>>,
    exprs: HashMap<String, Arc<dyn Macro<Expr>>>,
    user_defined: HashSet<String>,
    pub symbol_gen: SymbolGen,
    pub ensure_no_reserved_symbols: bool,
}

impl Default for Parser {
    fn default() -> Self {
        Self {
            commands: Default::default(),
            actions: Default::default(),
            exprs: Default::default(),
            user_defined: Default::default(),
            symbol_gen: SymbolGen::new(INTERNAL_SYMBOL_PREFIX.to_string()),
            ensure_no_reserved_symbols: true,
        }
    }
}

impl Parser {
    fn ensure_symbol_not_reserved(&self, symbol: &str, span: &Span) -> Result<(), ParseError> {
        if self.symbol_gen.is_reserved(symbol) && self.ensure_no_reserved_symbols {
            return error!(
                span.clone(),
                "symbols starting with '{}' are reserved for egglog internals",
                self.symbol_gen.reserved_prefix()
            );
        }
        Ok(())
    }

    pub fn get_program_from_string(
        &mut self,
        filename: Option<String>,
        input: &str,
    ) -> Result<Vec<Command>, ParseError> {
        let sexps = all_sexps(SexpParser::new(filename, input))?;
        let nested: Vec<Vec<_>> = map_fallible(&sexps, self, Self::parse_command)?;
        Ok(nested.into_iter().flatten().collect())
    }

    // currently only used for testing, but no reason it couldn't be used elsewhere later
    pub fn get_expr_from_string(
        &mut self,
        filename: Option<String>,
        input: &str,
    ) -> Result<Expr, ParseError> {
        let sexp = sexp(&mut SexpParser::new(filename, input))?;
        self.parse_expr(&sexp)
    }

    pub fn get_schedule_from_string(
        &mut self,
        filename: Option<String>,
        input: &str,
    ) -> Result<Schedule, ParseError> {
        let sexp = sexp(&mut SexpParser::new(filename, input))?;
        self.parse_schedule(&sexp)
    }

    // Parse a fact from a string.
    pub fn get_fact_from_string(
        &mut self,
        filename: Option<String>,
        input: &str,
    ) -> Result<Fact, ParseError> {
        let sexp = sexp(&mut SexpParser::new(filename, input))?;
        self.parse_fact(&sexp)
    }

    pub fn add_command_macro(&mut self, ma: Arc<dyn Macro<Vec<Command>>>) {
        self.commands.insert(ma.name().to_owned(), ma);
    }

    pub fn add_action_macro(&mut self, ma: Arc<dyn Macro<Vec<Action>>>) {
        self.actions.insert(ma.name().to_owned(), ma);
    }

    pub fn add_expr_macro(&mut self, ma: Arc<dyn Macro<Expr>>) {
        self.exprs.insert(ma.name().to_owned(), ma);
    }

    pub(crate) fn add_user_defined(&mut self, name: String) -> Result<(), Error> {
        if self.actions.contains_key(&name)
            || self.exprs.contains_key(&name)
            || self.commands.contains_key(&name)
        {
            use egglog_ast::span::{RustSpan, Span};
            return Err(Error::CommandAlreadyExists(name, span!()));
        }
        self.user_defined.insert(name);
        Ok(())
    }

    pub fn parse_command(&mut self, sexp: &Sexp) -> Result<Vec<Command>, ParseError> {
        let (head, tail, span) = sexp.expect_call("command")?;

        if let Some(macr0) = self.commands.get(&head).cloned() {
            return macr0.parse(tail, span, self);
        }

        // This prevents user-defined commands from being parsed as built-in commands.
        if self.user_defined.contains(&head) {
            let args = map_fallible(tail, self, Self::parse_expr)?;
            return Ok(vec![Command::UserDefined(span, head, args)]);
        }

        Ok(match head.as_str() {
            "sort" => match tail {
                [name] => vec![Command::Sort(span, name.expect_atom("sort name")?, None)],
                [name, call] => {
                    let (func, args, _) = call.expect_call("container sort declaration")?;
                    vec![Command::Sort(
                        span,
                        name.expect_atom("sort name")?,
                        Some((func, map_fallible(args, self, Self::parse_expr)?)),
                    )]
                }
                _ => {
                    return error!(
                        span,
                        "usages:\n(sort <name>)\n(sort <name> (<container sort> <argument sort>*))"
                    );
                }
            },
            "datatype" => match tail {
                [name, variants @ ..] => vec![Command::Datatype {
                    span,
                    name: name.expect_atom("sort name")?,
                    variants: map_fallible(variants, self, Self::variant)?,
                }],
                _ => return error!(span, "usage: (datatype <name> <variant>*)"),
            },
            "datatype*" => vec![Command::Datatypes {
                span,
                datatypes: map_fallible(tail, self, Self::rec_datatype)?,
            }],
            "function" => match tail {
                [name, inputs, output, rest @ ..] => vec![Command::Function {
                    name: name.expect_atom("function name")?,
                    schema: self.parse_schema(inputs, output)?,
                    merge: match self.parse_options(rest)?.as_slice() {
                        [(":no-merge", [])] => None,
                        [(":merge", [e])] => Some(self.parse_expr(e)?),
                        [] => {
                            return error!(
                                span,
                                "functions are required to specify merge behaviour"
                            );
                        }
                        _ => return error!(span, "could not parse function options"),
                    },
                    span,
                }],
                _ => {
                    let a = "(function <name> (<input sort>*) <output sort> :merge <expr>)";
                    let b = "(function <name> (<input sort>*) <output sort> :no-merge)";
                    return error!(span, "usages:\n{a}\n{b}");
                }
            },
            "constructor" => match tail {
                [name, inputs, output, rest @ ..] => {
                    let mut cost = None;
                    let mut unextractable = false;
                    match self.parse_options(rest)?.as_slice() {
                        [] => {}
                        [(":unextractable", [])] => unextractable = true,
                        [(":cost", [c])] => cost = Some(c.expect_uint("cost")?),
                        _ => return error!(span, "could not parse constructor options"),
                    }

                    vec![Command::Constructor {
                        span,
                        name: name.expect_atom("constructor name")?,
                        schema: self.parse_schema(inputs, output)?,
                        cost,
                        unextractable,
                    }]
                }
                _ => {
                    let a = "(constructor <name> (<input sort>*) <output sort>)";
                    let b = "(constructor <name> (<input sort>*) <output sort> :cost <cost>)";
                    let c = "(constructor <name> (<input sort>*) <output sort> :unextractable)";
                    return error!(span, "usages:\n{a}\n{b}\n{c}");
                }
            },
            "relation" => match tail {
                [name, inputs] => vec![Command::Relation {
                    span,
                    name: name.expect_atom("relation name")?,
                    inputs: map_fallible(inputs.expect_list("input sorts")?, self, |_, sexp| {
                        sexp.expect_atom("input sort")
                    })?,
                }],
                _ => return error!(span, "usage: (relation <name> (<input sort>*))"),
            },
            "ruleset" => match tail {
                [name] => vec![Command::AddRuleset(span, name.expect_atom("ruleset name")?)],
                _ => return error!(span, "usage: (ruleset <name>)"),
            },
            "unstable-combined-ruleset" => match tail {
                [name, subrulesets @ ..] => vec![Command::UnstableCombinedRuleset(
                    span,
                    name.expect_atom("combined ruleset name")?,
                    map_fallible(subrulesets, self, |_, sexp| {
                        sexp.expect_atom("subruleset name")
                    })?,
                )],
                _ => {
                    return error!(
                        span,
                        "usage: (unstable-combined-ruleset <name> <child ruleset>*)"
                    );
                }
            },
            "rule" => match tail {
                [lhs, rhs, rest @ ..] => {
                    let body =
                        map_fallible(lhs.expect_list("rule query")?, self, Self::parse_fact)?;
                    let head: Vec<Vec<_>> =
                        map_fallible(rhs.expect_list("rule actions")?, self, Self::parse_action)?;
                    let head = GenericActions(head.into_iter().flatten().collect());

                    let mut ruleset = String::new();
                    let mut name = String::new();
                    for option in self.parse_options(rest)? {
                        match option {
                            (":ruleset", [r]) => ruleset = r.expect_atom("ruleset name")?,
                            (":name", [s]) => name = s.expect_string("rule name")?,
                            _ => return error!(span, "could not parse rule option"),
                        }
                    }

                    vec![Command::Rule {
                        rule: Rule {
                            span,
                            head,
                            body,
                            name,
                            ruleset,
                        },
                    }]
                }
                _ => return error!(span, "usage: (rule (<fact>*) (<action>*) <option>*)"),
            },
            "rewrite" => match tail {
                [lhs, rhs, rest @ ..] => {
                    let lhs = self.parse_expr(lhs)?;
                    let rhs = self.parse_expr(rhs)?;

                    let mut ruleset = String::new();
                    let mut conditions = Vec::new();
                    let mut subsume = false;
                    for option in self.parse_options(rest)? {
                        match option {
                            (":ruleset", [r]) => ruleset = r.expect_atom("ruleset name")?,
                            (":subsume", []) => subsume = true,
                            (":when", [w]) => {
                                conditions = map_fallible(
                                    w.expect_list("rewrite conditions")?,
                                    self,
                                    Self::parse_fact,
                                )?
                            }
                            _ => return error!(span, "could not parse rewrite options"),
                        }
                    }

                    vec![Command::Rewrite(
                        ruleset,
                        Rewrite {
                            span,
                            lhs,
                            rhs,
                            conditions,
                        },
                        subsume,
                    )]
                }
                _ => return error!(span, "usage: (rewrite <expr> <expr> <option>*)"),
            },
            "birewrite" => match tail {
                [lhs, rhs, rest @ ..] => {
                    let lhs = self.parse_expr(lhs)?;
                    let rhs = self.parse_expr(rhs)?;

                    let mut ruleset = String::new();
                    let mut conditions = Vec::new();
                    for option in self.parse_options(rest)? {
                        match option {
                            (":ruleset", [r]) => ruleset = r.expect_atom("ruleset name")?,
                            (":when", [w]) => {
                                conditions = map_fallible(
                                    w.expect_list("rewrite conditions")?,
                                    self,
                                    Self::parse_fact,
                                )?
                            }
                            _ => return error!(span, "could not parse birewrite options"),
                        }
                    }

                    vec![Command::BiRewrite(
                        ruleset,
                        Rewrite {
                            span,
                            lhs,
                            rhs,
                            conditions,
                        },
                    )]
                }
                _ => return error!(span, "usage: (birewrite <expr> <expr> <option>*)"),
            },
            "run" => {
                if tail.is_empty() {
                    return error!(span, "usage: (run <ruleset>? <uint> <:until (<fact>*)>?)");
                }

                let has_ruleset = tail.len() >= 2 && tail[1].expect_uint::<u32>("").is_ok();

                let (ruleset, limit, rest) = if has_ruleset {
                    (
                        tail[0].expect_atom("ruleset name")?,
                        tail[1].expect_uint("number of iterations")?,
                        &tail[2..],
                    )
                } else {
                    (
                        String::new(),
                        tail[0].expect_uint("number of iterations")?,
                        &tail[1..],
                    )
                };

                let until = match self.parse_options(rest)?.as_slice() {
                    [] => None,
                    [(":until", facts)] => Some(map_fallible(facts, self, Self::parse_fact)?),
                    _ => return error!(span, "could not parse run options"),
                };

                vec![Command::RunSchedule(Schedule::Repeat(
                    span.clone(),
                    limit,
                    Box::new(Schedule::Run(span, RunConfig { ruleset, until })),
                ))]
            }
            "run-schedule" => vec![Command::RunSchedule(Schedule::Sequence(
                span,
                map_fallible(tail, self, Self::parse_schedule)?,
            ))],
            "extract" => match tail {
                [e] => vec![Command::Extract(
                    span.clone(),
                    self.parse_expr(e)?,
                    Expr::Lit(span, Literal::Int(0)),
                )],
                [e, v] => vec![Command::Extract(
                    span,
                    self.parse_expr(e)?,
                    self.parse_expr(v)?,
                )],
                _ => return error!(span, "usage: (extract <expr> <number of variants>?)"),
            },
            "check" => vec![Command::Check(
                span,
                map_fallible(tail, self, Self::parse_fact)?,
            )],
            "push" => match tail {
                [] => vec![Command::Push(1)],
                [n] => vec![Command::Push(n.expect_uint("number of times to push")?)],
                _ => return error!(span, "usage: (push <uint>?)"),
            },
            "pop" => match tail {
                [] => vec![Command::Pop(span, 1)],
                [n] => vec![Command::Pop(span, n.expect_uint("number of times to pop")?)],
                _ => return error!(span, "usage: (pop <uint>?)"),
            },
            "print-stats" => match tail {
                [] => vec![Command::PrintOverallStatistics(span, None)],
                [Sexp::Atom(o, _), file] if o == ":file" => vec![Command::PrintOverallStatistics(
                    span,
                    Some(file.expect_string("file name")?),
                )],
                _ => {
                    return error!(
                        span,
                        "usages: (print-stats)\n(print-stats :file \"<filename>\")"
                    );
                }
            },
            "print-function" => match tail {
                [name] => vec![Command::PrintFunction(
                    span,
                    name.expect_atom("table name")?,
                    None,
                    None,
                    PrintFunctionMode::Default,
                )],
                [name, rest @ ..] => {
                    let rows: Option<usize> = rest[0].expect_uint("number of rows").ok();
                    let rest = if rows.is_some() { &rest[1..] } else { rest };

                    let mut file = None;
                    let mut mode = PrintFunctionMode::Default;
                    for opt in self.parse_options(rest)? {
                        match opt {
                            (":file", [file_name]) => {
                                file = Some(file_name.expect_string("file name")?);
                            }
                            (":mode", [Sexp::Atom(mode_str, _)]) => {
                                mode = match mode_str.as_str() {
                                    "default" => PrintFunctionMode::Default,
                                    "csv" => PrintFunctionMode::CSV,
                                    _ => {
                                        return error!(
                                            span,
                                            "Unknown print-function mode. Supported modes are `default` and `csv`."
                                        );
                                    }
                                };
                            }
                            _ => {
                                return error!(
                                    span,
                                    "Unknown option to print-function. Supported options are `:mode csv|default` and `:file \"<filename>\"`."
                                );
                            }
                        }
                    }
                    vec![Command::PrintFunction(
                        span,
                        name.expect_atom("table name")?,
                        rows,
                        file,
                        mode,
                    )]
                }
                _ => {
                    return error!(
                        span,
                        "usage: (print-function <table name> <number of rows>? <option>*)"
                    );
                }
            },
            "print-size" => match tail {
                [] => vec![Command::PrintSize(span, None)],
                [name] => vec![Command::PrintSize(
                    span,
                    Some(name.expect_atom("table name")?),
                )],
                _ => return error!(span, "usage: (print-size <table name>?)"),
            },
            "input" => match tail {
                [name, file] => vec![Command::Input {
                    span,
                    name: name.expect_atom("table name")?,
                    file: file.expect_string("file name")?,
                }],
                _ => return error!(span, "usage: (input <table name> \"<file name>\")"),
            },
            "output" => match tail {
                [file, exprs @ ..] => vec![Command::Output {
                    span,
                    file: file.expect_string("file name")?,
                    exprs: map_fallible(exprs, self, Self::parse_expr)?,
                }],
                _ => return error!(span, "usage: (output <file name> <expr>+)"),
            },
            "include" => match tail {
                [file] => vec![Command::Include(span, file.expect_string("file name")?)],
                _ => return error!(span, "usage: (include <file name>)"),
            },
            "fail" => match tail {
                [subcommand] => {
                    let mut cs = self.parse_command(subcommand)?;
                    if cs.len() != 1 {
                        todo!("extend Fail to work with multiple parsed commands")
                    }
                    vec![Command::Fail(span, Box::new(cs.remove(0)))]
                }
                _ => return error!(span, "usage: (fail <command>)"),
            },
            _ => self
                .parse_action(sexp)?
                .into_iter()
                .map(Command::Action)
                .collect(),
        })
    }

    pub fn parse_schedule(&mut self, sexp: &Sexp) -> Result<Schedule, ParseError> {
        if let Sexp::Atom(ruleset, span) = sexp {
            return Ok(Schedule::Run(
                span.clone(),
                RunConfig {
                    ruleset: ruleset.clone(),
                    until: None,
                },
            ));
        }

        let (head, tail, span) = sexp.expect_call("schedule")?;

        Ok(match head.as_str() {
            "saturate" => Schedule::Saturate(
                span.clone(),
                Box::new(Schedule::Sequence(
                    span,
                    map_fallible(tail, self, Self::parse_schedule)?,
                )),
            ),
            "seq" => Schedule::Sequence(span, map_fallible(tail, self, Self::parse_schedule)?),
            "repeat" => match tail {
                [limit, tail @ ..] => Schedule::Repeat(
                    span.clone(),
                    limit.expect_uint("number of iterations")?,
                    Box::new(Schedule::Sequence(
                        span,
                        map_fallible(tail, self, Self::parse_schedule)?,
                    )),
                ),
                _ => return error!(span, "usage: (repeat <number of iterations> <schedule>*)"),
            },
            "run" => {
                let has_ruleset = match tail.first() {
                    None => false,
                    Some(Sexp::Atom(o, _)) if *o == ":until" => false,
                    _ => true,
                };

                let (ruleset, rest) = if has_ruleset {
                    (tail[0].expect_atom("ruleset name")?, &tail[1..])
                } else {
                    (String::new(), tail)
                };

                let until = match self.parse_options(rest)?.as_slice() {
                    [] => None,
                    [(":until", facts)] => Some(map_fallible(facts, self, Self::parse_fact)?),
                    _ => return error!(span, "could not parse run options"),
                };

                Schedule::Run(span, RunConfig { ruleset, until })
            }
            _ => return error!(span, "expected either saturate, seq, repeat, or run"),
        })
    }

    pub fn parse_action(&mut self, sexp: &Sexp) -> Result<Vec<Action>, ParseError> {
        let (head, tail, span) = sexp.expect_call("action")?;

        if let Some(func) = self.actions.get(&head).cloned() {
            return func.parse(tail, span, self);
        }

        Ok(match head.as_str() {
            "let" => match tail {
                [name, value] => {
                    let binding_span = name.span();
                    let binding = name.expect_atom("binding name")?;
                    self.ensure_symbol_not_reserved(&binding, &binding_span)?;
                    vec![Action::Let(span, binding, self.parse_expr(value)?)]
                }
                _ => return error!(span, "usage: (let <name> <expr>)"),
            },
            "set" => match tail {
                [call, value] => {
                    let (func, args, _) = call.expect_call("table lookup")?;
                    let args = map_fallible(args, self, Self::parse_expr)?;
                    let value = self.parse_expr(value)?;
                    vec![Action::Set(span, func, args, value)]
                }
                _ => return error!(span, "usage: (set (<table name> <expr>*) <expr>)"),
            },
            "delete" => match tail {
                [call] => {
                    let (func, args, _) = call.expect_call("table lookup")?;
                    let args = map_fallible(args, self, Self::parse_expr)?;
                    vec![Action::Change(span, Change::Delete, func, args)]
                }
                _ => return error!(span, "usage: (delete (<table name> <expr>*))"),
            },
            "subsume" => match tail {
                [call] => {
                    let (func, args, _) = call.expect_call("table lookup")?;
                    let args = map_fallible(args, self, Self::parse_expr)?;
                    vec![Action::Change(span, Change::Subsume, func, args)]
                }
                _ => return error!(span, "usage: (subsume (<table name> <expr>*))"),
            },
            "union" => match tail {
                [e1, e2] => vec![Action::Union(
                    span,
                    self.parse_expr(e1)?,
                    self.parse_expr(e2)?,
                )],
                _ => return error!(span, "usage: (union <expr> <expr>)"),
            },
            "panic" => match tail {
                [message] => vec![Action::Panic(span, message.expect_string("error message")?)],
                _ => return error!(span, "usage: (panic <string>)"),
            },
            _ => vec![Action::Expr(span, self.parse_expr(sexp)?)],
        })
    }

    pub fn parse_fact(&mut self, sexp: &Sexp) -> Result<Fact, ParseError> {
        let (head, tail, span) = sexp.expect_call("fact")?;

        Ok(match head.as_str() {
            "=" => match tail {
                [e1, e2] => Fact::Eq(span, self.parse_expr(e1)?, self.parse_expr(e2)?),
                _ => return error!(span, "usage: (= <expr> <expr>)"),
            },
            _ => Fact::Fact(self.parse_expr(sexp)?),
        })
    }

    pub fn parse_expr(&mut self, sexp: &Sexp) -> Result<Expr, ParseError> {
        Ok(match sexp {
            Sexp::Literal(literal, span) => Expr::Lit(span.clone(), literal.clone()),
            Sexp::Atom(symbol, span) => Expr::Var(
                span.clone(),
                if *symbol == "_" {
                    self.symbol_gen.fresh(symbol)
                } else {
                    self.ensure_symbol_not_reserved(symbol, span)?;
                    symbol.clone()
                },
            ),
            Sexp::List(list, span) => match list.as_slice() {
                [] => Expr::Lit(span.clone(), Literal::Unit),
                _ => {
                    let (head, tail, span) = sexp.expect_call("call expression")?;

                    if let Some(func) = self.exprs.get(&head).cloned() {
                        return func.parse(tail, span, self);
                    }

                    Expr::Call(
                        span.clone(),
                        head,
                        map_fallible(tail, self, Self::parse_expr)?,
                    )
                }
            },
        })
    }

    pub fn rec_datatype(
        &mut self,
        sexp: &Sexp,
    ) -> Result<(Span, String, Subdatatypes), ParseError> {
        let (head, tail, span) = sexp.expect_call("datatype")?;

        Ok(match head.as_str() {
            "sort" => match tail {
                [name, call] => {
                    let name = name.expect_atom("sort name")?;
                    let (func, args, _) = call.expect_call("container sort declaration")?;
                    let args = map_fallible(args, self, Self::parse_expr)?;
                    (span, name, Subdatatypes::NewSort(func, args))
                }
                _ => {
                    return error!(
                        span,
                        "usage: (sort <name> (<container sort> <argument sort>*))"
                    );
                }
            },
            _ => {
                let variants = map_fallible(tail, self, Self::variant)?;
                (span, head, Subdatatypes::Variants(variants))
            }
        })
    }

    pub fn variant(&mut self, sexp: &Sexp) -> Result<Variant, ParseError> {
        let (name, tail, span) = sexp.expect_call("datatype variant")?;

        let (types, cost, unextractable) = match tail {
            [types @ .., Sexp::Atom(o, _)] if *o == ":unextractable" => (types, None, true),
            [types @ .., Sexp::Atom(o, _), c] if *o == ":cost" => {
                (types, Some(c.expect_uint("cost")?), false)
            }
            types => (types, None, false),
        };

        Ok(Variant {
            span,
            name,
            types: map_fallible(types, self, |_, sexp| {
                sexp.expect_atom("variant argument type")
            })?,
            cost,
            unextractable,
        })
    }

    // helper for parsing a list of options
    pub fn parse_options<'a>(
        &self,
        sexps: &'a [Sexp],
    ) -> Result<Vec<(&'a str, &'a [Sexp])>, ParseError> {
        fn option_name(sexp: &Sexp) -> Option<&str> {
            if let Sexp::Atom(s, _) = sexp {
                if let Some(':') = s.chars().next() {
                    return Some(s);
                }
            }
            None
        }

        let mut out = Vec::new();
        let mut i = 0;
        while i < sexps.len() {
            let Some(key) = option_name(&sexps[i]) else {
                return error!(sexps[i].span(), "option key must start with ':'");
            };
            i += 1;

            let start = i;
            while i < sexps.len() && option_name(&sexps[i]).is_none() {
                i += 1;
            }
            out.push((key, &sexps[start..i]));
        }
        Ok(out)
    }

    pub fn parse_schema(&self, input: &Sexp, output: &Sexp) -> Result<Schema, ParseError> {
        Ok(Schema {
            input: input
                .expect_list("input sorts")?
                .iter()
                .map(|sexp| sexp.expect_atom("input sort"))
                .collect::<Result<_, _>>()?,
            output: output.expect_atom("output sort")?,
        })
    }
}

#[derive(Clone, Debug)]
pub(crate) struct SexpParser {
    source: Arc<SrcFile>,
    index: usize,
}

impl SexpParser {
    pub(crate) fn new(name: Option<String>, contents: &str) -> SexpParser {
        SexpParser {
            source: Arc::new(SrcFile {
                name,
                contents: contents.to_string(),
            }),
            index: 0,
        }
    }

    fn current_char(&self) -> Option<char> {
        self.source.contents[self.index..].chars().next()
    }

    fn advance_char(&mut self) {
        assert!(self.index < self.source.contents.len());
        loop {
            self.index += 1;
            if self.source.contents.is_char_boundary(self.index) {
                break;
            }
        }
    }

    fn advance_past_whitespace(&mut self) {
        let mut in_comment = false;
        loop {
            match self.current_char() {
                None => break,
                Some(';') => in_comment = true,
                Some('\n') => in_comment = false,
                Some(c) if c.is_whitespace() => {}
                Some(_) if in_comment => {}
                Some(_) => break,
            }
            self.advance_char();
        }
    }

    fn is_at_end(&self) -> bool {
        self.index == self.source.contents.len()
    }

    fn next(&mut self) -> Result<(Token, EgglogSpan), ParseError> {
        self.advance_past_whitespace();
        let mut span = EgglogSpan {
            file: self.source.clone(),
            i: self.index,
            j: self.index,
        };

        let Some(c) = self.current_char() else {
            return error!(s(span), "unexpected end of file");
        };
        self.advance_char();

        let token = match c {
            '(' => Token::Open,
            ')' => Token::Close,
            '"' => {
                let mut in_escape = false;
                let mut string = String::new();

                loop {
                    span.j = self.index;
                    match self.current_char() {
                        None => return error!(s(span), "string is missing end quote"),
                        Some('"') if !in_escape => break,
                        Some('\\') if !in_escape => in_escape = true,
                        Some(c) => {
                            string.push(match (in_escape, c) {
                                (false, c) => c,
                                (true, 'n') => '\n',
                                (true, 't') => '\t',
                                (true, '\\') => '\\',
                                (true, '\"') => '\"',
                                (true, c) => {
                                    return error!(s(span), "unrecognized escape character {c}");
                                }
                            });
                            in_escape = false;
                        }
                    }
                    self.advance_char();
                }
                self.advance_char();

                Token::String(string)
            }
            _ => {
                loop {
                    match self.current_char() {
                        Some(c) if c.is_whitespace() => break,
                        Some(';' | '(' | ')') => break,
                        None => break,
                        Some(_) => self.advance_char(),
                    }
                }
                Token::Other
            }
        };

        span.j = self.index;
        self.advance_past_whitespace();

        Ok((token, span))
    }
}

fn s(span: EgglogSpan) -> Span {
    Span::Egglog(Arc::new(span))
}

enum Token {
    Open,
    Close,
    String(String),
    Other,
}

fn sexp(ctx: &mut SexpParser) -> Result<Sexp, ParseError> {
    let mut stack: Vec<(EgglogSpan, Vec<Sexp>)> = vec![];

    loop {
        let (token, span) = ctx.next()?;

        let sexp = match token {
            Token::Open => {
                stack.push((span, vec![]));
                continue;
            }
            Token::Close => {
                if stack.is_empty() {
                    return error!(s(span), "unexpected `)`");
                }
                let (mut list_span, list) = stack.pop().unwrap();
                list_span.j = span.j;
                Sexp::List(list, s(list_span))
            }
            Token::String(sym) => Sexp::Literal(Literal::String(sym), s(span)),
            Token::Other => {
                let span = s(span);
                let s = span.string();

                if s == "true" {
                    Sexp::Literal(Literal::Bool(true), span)
                } else if s == "false" {
                    Sexp::Literal(Literal::Bool(false), span)
                } else if let Ok(int) = s.parse::<i64>() {
                    Sexp::Literal(Literal::Int(int), span)
                } else if s == "NaN" {
                    Sexp::Literal(Literal::Float(OrderedFloat(f64::NAN)), span)
                } else if s == "inf" {
                    Sexp::Literal(Literal::Float(OrderedFloat(f64::INFINITY)), span)
                } else if s == "-inf" {
                    Sexp::Literal(Literal::Float(OrderedFloat(f64::NEG_INFINITY)), span)
                } else if let Ok(float) = s.parse::<f64>() {
                    if float.is_finite() {
                        Sexp::Literal(Literal::Float(OrderedFloat(float)), span)
                    } else {
                        Sexp::Atom(s.into(), span)
                    }
                } else {
                    Sexp::Atom(s.into(), span)
                }
            }
        };

        if stack.is_empty() {
            return Ok(sexp);
        } else {
            stack.last_mut().unwrap().1.push(sexp);
        }
    }
}

pub(crate) fn all_sexps(mut ctx: SexpParser) -> Result<Vec<Sexp>, ParseError> {
    let mut sexps = Vec::new();
    ctx.advance_past_whitespace();
    while !ctx.is_at_end() {
        sexps.push(sexp(&mut ctx)?);
        ctx.advance_past_whitespace();
    }
    Ok(sexps)
}

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

    #[test]
    fn test_parser_display_roundtrip() {
        let s = r#"(f (g a 3) 4.0 (H "hello"))"#;
        let e = Parser::default().get_expr_from_string(None, s).unwrap();
        assert_eq!(format!("{}", e), s);
    }

    #[test]
    #[rustfmt::skip]
    fn rust_span_display() {
        let actual = format!("{}", span!()).replace('\\', "/");
        assert!(actual.starts_with("At "));
        assert!(actual.contains(":"));
        assert!(actual.ends_with("src/ast/parse.rs"));
    }

    #[test]
    fn test_parser_macros() {
        let mut parser = Parser::default();
        let y = "xxxx";
        parser.add_expr_macro(Arc::new(SimpleMacro::new("qqqq", |tail, span, macros| {
            Ok(Expr::Call(
                span,
                y.into(),
                map_fallible(tail, macros, Parser::parse_expr)?,
            ))
        })));
        let s = r#"(f (qqqq a 3) 4.0 (H "hello"))"#;
        let t = r#"(f (xxxx a 3) 4.0 (H "hello"))"#;
        let e = parser.get_expr_from_string(None, s).unwrap();
        assert_eq!(format!("{}", e), t);
    }
}