Skip to content

Commit 941aa50

Browse files
committed
Make binary operator distillation less confusing
1 parent 481b91b commit 941aa50

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

fathom/src/surface/distillation.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,6 @@ impl<'interner, 'arena, 'env> Context<'interner, 'arena, 'env> {
515515
)
516516
}
517517
}
518-
519518
core::Term::FunLit(..) => {
520519
let initial_local_len = self.local_len();
521520
let mut params = Vec::new();
@@ -542,34 +541,38 @@ impl<'interner, 'arena, 'env> Context<'interner, 'arena, 'env> {
542541
self.scope.to_scope(body_expr),
543542
)
544543
}
545-
core::Term::FunApp(
546-
..,
547-
core::Term::FunApp(.., core::Term::Prim(_, prim), lhs),
548-
arg_expr,
549-
) if prim_to_bin_op(prim).is_some() => {
550-
self.synth_bin_op(lhs, arg_expr, prim_to_bin_op(prim).unwrap())
551-
}
552-
553544
core::Term::FunApp(..) => {
554545
let mut head_expr = core_term;
555546
let mut args = Vec::new();
556547

557-
while let core::Term::FunApp(_, plicity, next_head_expr, arg_expr) = head_expr {
548+
// Collect a spine of arguments in reverse order
549+
while let core::Term::FunApp(_, plicity, next_head_expr, arg_expr) = *head_expr {
558550
head_expr = next_head_expr;
559-
args.push(Arg {
560-
plicity: *plicity,
561-
term: self.check(arg_expr),
562-
});
551+
args.push((plicity, arg_expr));
563552
}
564553

565-
let head_expr = self.synth(head_expr);
554+
// Distill appropriate primitives to binary operator expressions
555+
if let (core::Term::Prim(_, prim), [(_, rhs), (_, lhs)]) = (head_expr, &args[..]) {
556+
if let Some(binop) = prim_to_bin_op(prim) {
557+
let lhs = self.scope.to_scope(self.synth(lhs));
558+
let rhs = self.scope.to_scope(self.synth(rhs));
559+
return Term::BinOp((), lhs, binop, rhs);
560+
}
561+
}
566562

563+
// Otherwise distill to a function application
567564
Term::App(
568565
(),
569-
self.scope.to_scope(head_expr),
570-
self.scope.to_scope_from_iter(args.into_iter().rev()),
566+
self.scope.to_scope(self.synth(head_expr)),
567+
self.scope.to_scope_from_iter(args.into_iter().rev().map(
568+
|(plicity, arg_expr)| Arg {
569+
plicity,
570+
term: self.check(arg_expr),
571+
},
572+
)),
571573
)
572574
}
575+
573576
core::Term::RecordType(_, labels, types)
574577
if is_tuple_type(&mut self.interner.borrow_mut(), labels, types) =>
575578
{
@@ -737,22 +740,6 @@ impl<'interner, 'arena, 'env> Context<'interner, 'arena, 'env> {
737740
}
738741
}
739742

740-
fn synth_bin_op(
741-
&mut self,
742-
lhs: &core::Term<'_>,
743-
rhs: &core::Term<'_>,
744-
op: BinOp<()>,
745-
) -> Term<'arena, ()> {
746-
let lhs = self.synth(lhs);
747-
let rhs = self.synth(rhs);
748-
Term::BinOp(
749-
(),
750-
self.scope.to_scope(lhs),
751-
op,
752-
self.scope.to_scope(self.scope.to_scope(rhs)),
753-
)
754-
}
755-
756743
fn synth_format_fields(
757744
&mut self,
758745
labels: &[StringId],

0 commit comments

Comments
 (0)