| @@ -32,7 +32,7 @@ mod html; |
| 32 | 32 | mod pattern; |
| 33 | 33 | mod template; |
| 34 | 34 | |
| 35 | | −use std::collections::HashMap; |
| 35 | +use std::collections::{HashMap, VecDeque}; |
| 36 | 36 | |
| 37 | 37 | use louise_hir::body::{self as hb, Body, ExprId, LocalId, PatId}; |
| 38 | 38 | use louise_hir::{DefId, DefKind, Hir, TypeBody}; |
| @@ -43,6 +43,9 @@ use louise_types::{ |
| 43 | 43 | |
| 44 | 44 | use crate::ir::*; |
| 45 | 45 | |
| 46 | +/// A lowered argument: its `~` label and its value (ADR 0061). |
| 47 | +type LabelledAtom = (Option<String>, Atom); |
| 48 | + |
| 46 | 49 | /// Names in the scope of the function being built. |
| 47 | 50 | #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] |
| 48 | 51 | enum Key { |
| @@ -56,6 +59,9 @@ enum Key { |
| 56 | 59 | /// The `i`-th dictionary stored in the value a refined constructor |
| 57 | 60 | /// pattern matched (ADR 0041). |
| 58 | 61 | Stored(PatId, u32), |
| 62 | + /// A plain variable captured by a partial application's closure |
| 63 | + /// (ADR 0061), by its id. |
| 64 | + Capture(VarId), |
| 59 | 65 | } |
| 60 | 66 | |
| 61 | 67 | fn cap_key(instance: &Ty) -> Option<Key> { |
| @@ -572,6 +578,55 @@ impl<'a> Lowerer<'a> { |
| 572 | 578 | self.frame().vars.insert(key, v); |
| 573 | 579 | } |
| 574 | 580 | |
| 581 | + /// `v` as a variable of the current frame, captured into it and every |
| 582 | + /// frame between it and its owner (a partial application's closure, |
| 583 | + /// ADR 0061). Keyed unlike a local: a variable is captured by identity. |
| 584 | + fn lookup_var(&mut self, v: VarId) -> VarId { |
| 585 | + let top = self.frames.len() - 1; |
| 586 | + if self.frames[top].vars.values().any(|&id| id == v) { |
| 587 | + return v; |
| 588 | + } |
| 589 | + let Some(owner) = (0..top) |
| 590 | + .rev() |
| 591 | + .find(|i| self.frames[*i].vars.values().any(|&id| id == v)) |
| 592 | + else { |
| 593 | + // A global (a definition's frame): visible as is. |
| 594 | + return v; |
| 595 | + }; |
| 596 | + let mut out = v; |
| 597 | + for i in owner + 1..=top { |
| 598 | + let frame = &mut self.frames[i]; |
| 599 | + out = frame.fresh(); |
| 600 | + frame.vars.insert(Key::Capture(v), out); |
| 601 | + frame.captures.push((Key::Capture(v), out)); |
| 602 | + } |
| 603 | + out |
| 604 | + } |
| 605 | + |
| 606 | + /// The atom captured into the current frame's closure: a variable is |
| 607 | + /// threaded through the frames; a literal is recomputed. |
| 608 | + fn capture_atom(&mut self, a: Atom) -> Atom { |
| 609 | + match a { |
| 610 | + Atom::Var(v) => Atom::Var(self.lookup_var(v)), |
| 611 | + // Atoms are values (a literal or unit is pure). |
| 612 | + other => other, |
| 613 | + } |
| 614 | + } |
| 615 | + |
| 616 | + /// The outer atom a `Key::Capture` of the frame's captures names. |
| 617 | + fn captured_atom(&mut self, key: Key) -> Atom { |
| 618 | + match key { |
| 619 | + Key::Capture(v) => { |
| 620 | + if self.frame().vars.values().any(|&id| id == v) { |
| 621 | + Atom::Var(v) |
| 622 | + } else { |
| 623 | + Atom::Var(self.lookup(key)) |
| 624 | + } |
| 625 | + } |
| 626 | + other => Atom::Var(self.lookup(other)), |
| 627 | + } |
| 628 | + } |
| 629 | + |
| 575 | 630 | // ---- functions --------------------------------------------------------- |
| 576 | 631 | |
| 577 | 632 | fn lower_fn(&mut self, def: DefId, n_dicts: usize) -> FnDecl { |
| @@ -604,6 +659,11 @@ impl<'a> Lowerer<'a> { |
| 604 | 659 | params.push((v, *p)); |
| 605 | 660 | self.frame().params.push(v); |
| 606 | 661 | } |
| 662 | + // A parameterless function takes the unit argument (ADR 0061). |
| 663 | + if body.params.is_empty() { |
| 664 | + let v = self.fresh(); |
| 665 | + self.frame().params.push(v); |
| 666 | + } |
| 607 | 667 | let expr = self.block_value(|this| { |
| 608 | 668 | for (v, p) in params { |
| 609 | 669 | this.bind_pattern(p, v); |
| @@ -794,6 +854,10 @@ impl<'a> Lowerer<'a> { |
| 794 | 854 | let v = self.fresh(); |
| 795 | 855 | own.push((v, *p)); |
| 796 | 856 | } |
| 857 | + // A parameterless lambda takes the unit argument (ADR 0061); a |
| 858 | + // clause for a parameterless operation gets it before its |
| 859 | + // resumption. |
| 860 | + let unit = params.is_empty().then(|| self.fresh()); |
| 797 | 861 | let extra_var = extra.map(|local| { |
| 798 | 862 | let v = self.fresh(); |
| 799 | 863 | self.bind(Key::Local(local), v); |
| @@ -809,6 +873,7 @@ impl<'a> Lowerer<'a> { |
| 809 | 873 | let mut all_params: Vec<VarId> = frame.captures.iter().map(|(_, v)| *v).collect(); |
| 810 | 874 | all_params.extend(&frame.params); |
| 811 | 875 | all_params.extend(own.iter().map(|(v, _)| *v)); |
| 876 | + all_params.extend(unit); |
| 812 | 877 | all_params.extend(extra_var); |
| 813 | 878 | self.define( |
| 814 | 879 | fid, |
| @@ -858,6 +923,11 @@ impl<'a> Lowerer<'a> { |
| 858 | 923 | args, |
| 859 | 924 | } |
| 860 | 925 | } |
| 926 | + // An extern's C shim takes its declared arguments only: a |
| 927 | + // parameterless one's unit argument is dropped (ADR 0061). |
| 928 | + DefKind::Fn(f) if f.sig.params.is_empty() => { |
| 929 | + Value::CallExtern(self.extern_symbol(def).unwrap_or_default(), Vec::new()) |
| 930 | + } |
| 861 | 931 | _ => Value::CallExtern(self.extern_symbol(def).unwrap_or_default(), args), |
| 862 | 932 | }; |
| 863 | 933 | let result = arity as VarId; |
| @@ -898,8 +968,9 @@ impl<'a> Lowerer<'a> { |
| 898 | 968 | fn arity_of(&self, def: DefId) -> usize { |
| 899 | 969 | match &self.hir.def(def).kind { |
| 900 | 970 | DefKind::Ctor(c) => c.fields.len(), |
| 901 | | − DefKind::Fn(f) => f.sig.params.len(), |
| 902 | | − DefKind::Op(o) => o.sig.params.len(), |
| 971 | + // `fn() -> T` takes the unit argument (ADR 0061). |
| 972 | + DefKind::Fn(f) => f.sig.params.len().max(1), |
| 973 | + DefKind::Op(o) => o.sig.params.len().max(1), |
| 903 | 974 | _ => 0, |
| 904 | 975 | } |
| 905 | 976 | } |
| @@ -1019,8 +1090,8 @@ impl<'a> Lowerer<'a> { |
| 1019 | 1090 | callee, |
| 1020 | 1091 | args, |
| 1021 | 1092 | spread, |
| 1022 | | − .. |
| 1023 | | − } => self.call(*callee, args, *spread), |
| 1093 | + parens, |
| 1094 | + } => self.call(*callee, args, *spread, *parens), |
| 1024 | 1095 | hb::Expr::Field { base, .. } => { |
| 1025 | 1096 | let index = self.field_index(e); |
| 1026 | 1097 | let b = self.expr(*base); |
| @@ -1471,16 +1542,30 @@ impl<'a> Lowerer<'a> { |
| 1471 | 1542 | } |
| 1472 | 1543 | |
| 1473 | 1544 | /// Labels of a named callee's parameters (fields for constructors). |
| 1474 | | − fn param_labels(&self, def: DefId) -> Vec<Option<String>> { |
| 1545 | + /// A parameterless function or operation takes the unit argument |
| 1546 | + /// (ADR 0061): one unlabelled slot. A component call (parenthesised, |
| 1547 | + /// constructor-like) fills the component's parameters by name. |
| 1548 | + fn param_labels(&self, def: DefId, parens: bool) -> Vec<Option<String>> { |
| 1475 | 1549 | match &self.hir.def(def).kind { |
| 1476 | 1550 | DefKind::Ctor(c) => c.fields.iter().map(|f| f.label.clone()).collect(), |
| 1477 | | − DefKind::Fn(f) => f.sig.params.iter().map(|p| Some(p.name.clone())).collect(), |
| 1478 | | − DefKind::Op(o) => o.sig.params.iter().map(|p| Some(p.name.clone())).collect(), |
| 1551 | + DefKind::Fn(f) if f.sig.params.is_empty() => vec![None], |
| 1552 | + DefKind::Op(o) if o.sig.params.is_empty() => vec![None], |
| 1553 | + DefKind::Fn(f) if parens => f.sig.params.iter().map(|p| Some(p.name.clone())).collect(), |
| 1554 | + // Labels are declared (`~name`, ADR 0061); names are not |
| 1555 | + // labels. |
| 1556 | + DefKind::Fn(f) => f.sig.params.iter().map(|p| p.label.clone()).collect(), |
| 1557 | + DefKind::Op(o) => o.sig.params.iter().map(|p| p.label.clone()).collect(), |
| 1479 | 1558 | _ => Vec::new(), |
| 1480 | 1559 | } |
| 1481 | 1560 | } |
| 1482 | 1561 | |
| 1483 | | − fn call(&mut self, callee: ExprId, args: &[hb::Arg], spread: Option<ExprId>) -> Atom { |
| 1562 | + fn call( |
| 1563 | + &mut self, |
| 1564 | + callee: ExprId, |
| 1565 | + args: &[hb::Arg], |
| 1566 | + spread: Option<ExprId>, |
| 1567 | + parens: bool, |
| 1568 | + ) -> Atom { |
| 1484 | 1569 | let body = self.body(); |
| 1485 | 1570 | let named = match &body.exprs[callee] { |
| 1486 | 1571 | hb::Expr::Def(d) => Some(*d), |
| @@ -1491,7 +1576,7 @@ impl<'a> Lowerer<'a> { |
| 1491 | 1576 | Some(_) => None, |
| 1492 | 1577 | None => Some(self.expr(callee)), |
| 1493 | 1578 | }; |
| 1494 | | − let lowered: Vec<(Option<String>, Atom)> = args |
| 1579 | + let lowered: Vec<LabelledAtom> = args |
| 1495 | 1580 | .iter() |
| 1496 | 1581 | .map(|a| (a.label.clone(), self.expr(a.expr))) |
| 1497 | 1582 | .collect(); |
| @@ -1499,7 +1584,9 @@ impl<'a> Lowerer<'a> { |
| 1499 | 1584 | // The slot rule is the type checker's (issue #161): labels take |
| 1500 | 1585 | // their slots first, then unlabelled arguments fill the remaining |
| 1501 | 1586 | // slots in order — one function, so the two can't drift. |
| 1502 | | − let positional = |labels: &[Option<String>]| -> Vec<Option<Atom>> { |
| 1587 | + let positional = |labels: &[Option<String>], |
| 1588 | + lowered: &[(Option<String>, Atom)]| |
| 1589 | + -> Vec<Option<Atom>> { |
| 1503 | 1590 | let arg_labels: Vec<Option<String>> = lowered.iter().map(|(l, _)| l.clone()).collect(); |
| 1504 | 1591 | let assignment = louise_hir::order_args(labels, &arg_labels) |
| 1505 | 1592 | .expect("arguments were placed by the type checker"); |
| @@ -1513,18 +1600,31 @@ impl<'a> Lowerer<'a> { |
| 1513 | 1600 | }; |
| 1514 | 1601 | let caps = self.caps_at(callee); |
| 1515 | 1602 | let Some(def) = named else { |
| 1516 | | − let args = caps |
| 1517 | | − .into_iter() |
| 1518 | | − .chain(lowered.into_iter().map(|(_, a)| a)) |
| 1519 | | − .collect(); |
| 1520 | | − return self.emit(Value::Call(callee_atom.expect("unnamed callee"), args)); |
| 1603 | + // A call through a value: the type says the arity (ADR 0061). |
| 1604 | + let ty = self |
| 1605 | + .results |
| 1606 | + .map(|r| r.types.exprs[callee.index()].clone()) |
| 1607 | + .unwrap_or(Ty::Error); |
| 1608 | + return self.call_value(callee_atom.expect("unnamed callee"), caps, lowered, ty); |
| 1521 | 1609 | }; |
| 1522 | | − let slots = positional(&self.param_labels(def)); |
| 1610 | + let arity = self.arity_of(def); |
| 1611 | + // A call that outlasts the arrow continues into the result's own |
| 1612 | + // arrow (ADR 0061): the first `arity` value arguments belong here. |
| 1613 | + let (head, over): (Vec<LabelledAtom>, Vec<LabelledAtom>) = |
| 1614 | + if lowered.len() > arity && !matches!(self.hir.def(def).kind, DefKind::Ctor(_)) { |
| 1615 | + let mut it = lowered; |
| 1616 | + let over = it.split_off(arity); |
| 1617 | + (it, over) |
| 1618 | + } else { |
| 1619 | + (lowered, Vec::new()) |
| 1620 | + }; |
| 1621 | + let slots = positional(&self.param_labels(def, parens), &head); |
| 1622 | + let complete = slots.iter().all(Option::is_some); |
| 1523 | 1623 | let args: Vec<Atom> = slots |
| 1524 | 1624 | .iter() |
| 1525 | 1625 | .map(|s| s.clone().unwrap_or(Atom::Unit)) |
| 1526 | 1626 | .collect(); |
| 1527 | | − match &self.hir.def(def).kind { |
| 1627 | + let mut result = match &self.hir.def(def).kind { |
| 1528 | 1628 | DefKind::Ctor(c) => { |
| 1529 | 1629 | // Record update: the base is a cell of this type, whose |
| 1530 | 1630 | // unchanged fields a reuse can keep (ADR 0040). |
| @@ -1555,6 +1655,7 @@ impl<'a> Lowerer<'a> { |
| 1555 | 1655 | args, |
| 1556 | 1656 | }) |
| 1557 | 1657 | } |
| 1658 | + DefKind::Op(_) if !complete => self.partial_perform(def, slots, caps, callee), |
| 1558 | 1659 | DefKind::Op(_) => { |
| 1559 | 1660 | let (effect, op) = self.effect_and_op(def); |
| 1560 | 1661 | let cap = caps.into_iter().next().unwrap_or(Atom::Unit); |
| @@ -1565,8 +1666,17 @@ impl<'a> Lowerer<'a> { |
| 1565 | 1666 | args, |
| 1566 | 1667 | }) |
| 1567 | 1668 | } |
| 1669 | + DefKind::Fn(f) if !complete => self.partial_fn(def, slots, caps, callee), |
| 1568 | 1670 | DefKind::Fn(f) => { |
| 1569 | 1671 | if let Some(sym) = self.extern_symbol(def) { |
| 1672 | + // An extern's C shim takes its declared arguments only: |
| 1673 | + // the unit argument of a parameterless one is dropped |
| 1674 | + // (ADR 0061). |
| 1675 | + let args = if f.sig.params.is_empty() { |
| 1676 | + Vec::new() |
| 1677 | + } else { |
| 1678 | + args |
| 1679 | + }; |
| 1570 | 1680 | return self.emit(Value::CallExtern(sym, args)); |
| 1571 | 1681 | } |
| 1572 | 1682 | let args: Vec<Atom> = caps.into_iter().chain(args).collect(); |
| @@ -1589,9 +1699,314 @@ impl<'a> Lowerer<'a> { |
| 1589 | 1699 | )) |
| 1590 | 1700 | } |
| 1591 | 1701 | _ => Atom::Unit, |
| 1702 | + }; |
| 1703 | + // Arguments past the arrow apply to the result's own arrows. |
| 1704 | + if !over.is_empty() { |
| 1705 | + let ty = self |
| 1706 | + .results |
| 1707 | + .map(|r| r.types.exprs[callee.index()].clone()) |
| 1708 | + .unwrap_or(Ty::Error); |
| 1709 | + let ret = match self.ctx_shallow_fn(ty, arity) { |
| 1710 | + Some(ret) => ret, |
| 1711 | + None => Ty::Error, |
| 1712 | + }; |
| 1713 | + result = self.call_value(result, Vec::new(), over.to_vec(), ret); |
| 1714 | + } |
| 1715 | + result |
| 1716 | + } |
| 1717 | + |
| 1718 | + /// The return type of `ty`'s arrow after `arity` value parameters |
| 1719 | + /// (its parameters are the flattened chain, ADR 0061), if `ty` is a |
| 1720 | + /// function of that arity. |
| 1721 | + fn ctx_shallow_fn(&self, ty: Ty, arity: usize) -> Option<Ty> { |
| 1722 | + match ty { |
| 1723 | + Ty::Fn(params, ret, _, _) if params.len() == arity => Some(*ret), |
| 1724 | + _ => None, |
| 1592 | 1725 | } |
| 1593 | 1726 | } |
| 1594 | 1727 | |
| 1728 | + /// A call through a value, curried (ADR 0061): a full application is a |
| 1729 | + /// direct call; a partial one builds a closure of the parameters left; |
| 1730 | + /// one that outlasts the arrow continues into the result's own arrow. |
| 1731 | + /// `caps` are the first arrow's capabilities (the callee's row's); a |
| 1732 | + /// returned closure's own capabilities are not known here (an |
| 1733 | + /// over-application of one with a bounded row is not yet compiled; |
| 1734 | + /// open for review). |
| 1735 | + fn call_value( |
| 1736 | + &mut self, |
| 1737 | + callee: Atom, |
| 1738 | + caps: Vec<Atom>, |
| 1739 | + args: Vec<(Option<String>, Atom)>, |
| 1740 | + ty: Ty, |
| 1741 | + ) -> Atom { |
| 1742 | + let mut cur = callee; |
| 1743 | + let mut ty = ty; |
| 1744 | + let mut caps = caps; |
| 1745 | + let mut args: VecDeque<LabelledAtom> = args.into(); |
| 1746 | + while !args.is_empty() { |
| 1747 | + let (labels, n, ret) = match &ty { |
| 1748 | + Ty::Fn(params, ret, _, _) => ( |
| 1749 | + params.iter().map(|p| p.label.clone()).collect(), |
| 1750 | + params.len(), |
| 1751 | + (**ret).clone(), |
| 1752 | + ), |
| 1753 | + // Unknown: applied in full. |
| 1754 | + _ => (Vec::new(), args.len(), Ty::Error), |
| 1755 | + }; |
| 1756 | + let chunk: Vec<LabelledAtom> = args.drain(..n.min(args.len())).collect(); |
| 1757 | + let slots: Vec<Option<Atom>> = if chunk.iter().all(|(l, _)| l.is_none()) { |
| 1758 | + chunk.into_iter().map(|(_, a)| Some(a)).collect() |
| 1759 | + } else { |
| 1760 | + let arg_labels: Vec<Option<String>> = |
| 1761 | + chunk.iter().map(|(l, _)| l.clone()).collect(); |
| 1762 | + match louise_hir::order_args(&labels, &arg_labels) { |
| 1763 | + Ok(assignment) => { |
| 1764 | + let mut slots: Vec<Option<Atom>> = vec![None; labels.len()]; |
| 1765 | + for ((_, atom), slot) in chunk.into_iter().zip(assignment) { |
| 1766 | + slots[slot] = Some(atom); |
| 1767 | + } |
| 1768 | + slots |
| 1769 | + } |
| 1770 | + // Placed by the type checker; defensive only. |
| 1771 | + Err(_) => chunk.into_iter().map(|(_, a)| Some(a)).collect(), |
| 1772 | + } |
| 1773 | + }; |
| 1774 | + let full = slots.len() == n && slots.iter().all(Option::is_some); |
| 1775 | + if full { |
| 1776 | + let atoms: Vec<Atom> = std::mem::take(&mut caps) |
| 1777 | + .into_iter() |
| 1778 | + .chain(slots.into_iter().map(|s| s.unwrap())) |
| 1779 | + .collect(); |
| 1780 | + cur = self.emit(Value::Call(cur, atoms)); |
| 1781 | + } else { |
| 1782 | + // Partial: a closure of the parameters left. |
| 1783 | + cur = self.eta(cur, slots, std::mem::take(&mut caps), n); |
| 1784 | + } |
| 1785 | + ty = ret; |
| 1786 | + } |
| 1787 | + cur |
| 1788 | + } |
| 1789 | + |
| 1790 | + /// `fn(rest) { callee(caps, placed, rest) }`: a partial application of |
| 1791 | + /// a value (ADR 0061). The callee, the capabilities and the placed |
| 1792 | + /// arguments are captured; `slots` are the arrow's parameters, the |
| 1793 | + /// placed ones `Some`. |
| 1794 | + fn eta( |
| 1795 | + &mut self, |
| 1796 | + callee: Atom, |
| 1797 | + slots: Vec<Option<Atom>>, |
| 1798 | + caps: Vec<Atom>, |
| 1799 | + _arity: usize, |
| 1800 | + ) -> Atom { |
| 1801 | + let parent = self.frame(); |
| 1802 | + parent.lambdas += 1; |
| 1803 | + let name = format!("{}$partial{}", parent.name, parent.lambdas); |
| 1804 | + let fid = self.placeholder(name.clone()); |
| 1805 | + self.frames.push(Frame::new(name)); |
| 1806 | + let callee = self.capture_atom(callee); |
| 1807 | + let caps: Vec<Atom> = caps.into_iter().map(|c| self.capture_atom(c)).collect(); |
| 1808 | + let slots: Vec<Option<Atom>> = slots |
| 1809 | + .into_iter() |
| 1810 | + .map(|s| s.map(|a| self.capture_atom(a))) |
| 1811 | + .collect(); |
| 1812 | + let vars: Vec<VarId> = slots |
| 1813 | + .iter() |
| 1814 | + .filter(|s| s.is_none()) |
| 1815 | + .map(|_| self.fresh()) |
| 1816 | + .collect(); |
| 1817 | + let body = self.block_value(|this| { |
| 1818 | + let mut vars = vars.iter(); |
| 1819 | + let args: Vec<Atom> = slots |
| 1820 | + .into_iter() |
| 1821 | + .map(|s| s.unwrap_or_else(|| Atom::Var(*vars.next().expect("one per hole")))) |
| 1822 | + .collect(); |
| 1823 | + this.emit(Value::Call(callee, caps.into_iter().chain(args).collect())) |
| 1824 | + }); |
| 1825 | + let frame = self.frames.pop().expect("pushed"); |
| 1826 | + let mut params: Vec<VarId> = frame.captures.iter().map(|(_, v)| *v).collect(); |
| 1827 | + params.extend(&vars); |
| 1828 | + self.define( |
| 1829 | + fid, |
| 1830 | + FnDecl { |
| 1831 | + name: frame.name, |
| 1832 | + params, |
| 1833 | + body, |
| 1834 | + n_vars: frame.n_vars, |
| 1835 | + borrowed: Default::default(), |
| 1836 | + span: self.span, |
| 1837 | + module: None, |
| 1838 | + shapes: Default::default(), |
| 1839 | + fip: false, |
| 1840 | + cpr_arity: None, |
| 1841 | + unbox_sig: None, |
| 1842 | + }, |
| 1843 | + ); |
| 1844 | + let captures = frame |
| 1845 | + .captures |
| 1846 | + .iter() |
| 1847 | + .map(|(key, _)| self.captured_atom(*key)) |
| 1848 | + .collect(); |
| 1849 | + self.emit(Value::Closure(fid, captures)) |
| 1850 | + } |
| 1851 | + |
| 1852 | + /// `def` with some arguments given: a closure of the rest (ADR 0061). |
| 1853 | + /// Capabilities and dictionaries are taken at this first application; |
| 1854 | + /// the closure captures them. |
| 1855 | + fn partial_fn( |
| 1856 | + &mut self, |
| 1857 | + def: DefId, |
| 1858 | + slots: Vec<Option<Atom>>, |
| 1859 | + caps: Vec<Atom>, |
| 1860 | + site: ExprId, |
| 1861 | + ) -> Atom { |
| 1862 | + let parent = self.frame(); |
| 1863 | + parent.lambdas += 1; |
| 1864 | + let name = format!("{}$partial{}", parent.name, parent.lambdas); |
| 1865 | + let fid = self.placeholder(name.clone()); |
| 1866 | + // Dictionaries are found where the partial application is written. |
| 1867 | + let ev: Vec<Atom> = self |
| 1868 | + .evidence(site) |
| 1869 | + .iter() |
| 1870 | + .map(|e| self.materialize(e)) |
| 1871 | + .collect(); |
| 1872 | + self.frames.push(Frame::new(name)); |
| 1873 | + let caps: Vec<Atom> = caps.into_iter().map(|c| self.capture_atom(c)).collect(); |
| 1874 | + let ev: Vec<Atom> = ev.into_iter().map(|e| self.capture_atom(e)).collect(); |
| 1875 | + let slots: Vec<Option<Atom>> = slots |
| 1876 | + .into_iter() |
| 1877 | + .map(|s| s.map(|a| self.capture_atom(a))) |
| 1878 | + .collect(); |
| 1879 | + let vars: Vec<VarId> = slots |
| 1880 | + .iter() |
| 1881 | + .filter(|s| s.is_none()) |
| 1882 | + .map(|_| self.fresh()) |
| 1883 | + .collect(); |
| 1884 | + let body = self.block_value(|this| { |
| 1885 | + let mut vars = vars.iter(); |
| 1886 | + let args: Vec<Atom> = slots |
| 1887 | + .into_iter() |
| 1888 | + .map(|s| s.unwrap_or_else(|| Atom::Var(*vars.next().expect("one per hole")))) |
| 1889 | + .collect(); |
| 1890 | + match &this.hir.def(def).kind { |
| 1891 | + DefKind::Fn(f) => { |
| 1892 | + if let Some(sym) = this.extern_symbol(def) { |
| 1893 | + return this.emit(Value::CallExtern(sym, args)); |
| 1894 | + } |
| 1895 | + let args: Vec<Atom> = caps.into_iter().chain(args).collect(); |
| 1896 | + if let Some(owner) = f.owner |
| 1897 | + && matches!(this.hir.def(owner).kind, DefKind::Trait(_)) |
| 1898 | + { |
| 1899 | + let Some((first, extra)) = ev.split_first() else { |
| 1900 | + return Atom::Unit; |
| 1901 | + }; |
| 1902 | + let index = this.method_index(owner, &this.hir.def(def).name); |
| 1903 | + return this.method_call( |
| 1904 | + first.clone(), |
| 1905 | + index, |
| 1906 | + extra.iter().cloned().chain(args).collect(), |
| 1907 | + ); |
| 1908 | + } |
| 1909 | + this.emit(Value::CallFn( |
| 1910 | + this.fn_ids[&def], |
| 1911 | + ev.into_iter().chain(args).collect(), |
| 1912 | + )) |
| 1913 | + } |
| 1914 | + _ => Atom::Unit, |
| 1915 | + } |
| 1916 | + }); |
| 1917 | + let frame = self.frames.pop().expect("pushed"); |
| 1918 | + let mut params: Vec<VarId> = frame.captures.iter().map(|(_, v)| *v).collect(); |
| 1919 | + params.extend(&vars); |
| 1920 | + self.define( |
| 1921 | + fid, |
| 1922 | + FnDecl { |
| 1923 | + name: frame.name, |
| 1924 | + params, |
| 1925 | + body, |
| 1926 | + n_vars: frame.n_vars, |
| 1927 | + borrowed: Default::default(), |
| 1928 | + span: self.span, |
| 1929 | + module: None, |
| 1930 | + shapes: Default::default(), |
| 1931 | + fip: false, |
| 1932 | + cpr_arity: None, |
| 1933 | + unbox_sig: None, |
| 1934 | + }, |
| 1935 | + ); |
| 1936 | + let captures = frame |
| 1937 | + .captures |
| 1938 | + .iter() |
| 1939 | + .map(|(key, _)| self.captured_atom(*key)) |
| 1940 | + .collect(); |
| 1941 | + self.emit(Value::Closure(fid, captures)) |
| 1942 | + } |
| 1943 | + |
| 1944 | + /// An operation with some arguments given: a closure performing it |
| 1945 | + /// once the rest arrive (ADR 0061). |
| 1946 | + fn partial_perform( |
| 1947 | + &mut self, |
| 1948 | + def: DefId, |
| 1949 | + slots: Vec<Option<Atom>>, |
| 1950 | + caps: Vec<Atom>, |
| 1951 | + _site: ExprId, |
| 1952 | + ) -> Atom { |
| 1953 | + let parent = self.frame(); |
| 1954 | + parent.lambdas += 1; |
| 1955 | + let name = format!("{}$partial{}", parent.name, parent.lambdas); |
| 1956 | + let fid = self.placeholder(name.clone()); |
| 1957 | + self.frames.push(Frame::new(name)); |
| 1958 | + let caps: Vec<Atom> = caps.into_iter().map(|c| self.capture_atom(c)).collect(); |
| 1959 | + let slots: Vec<Option<Atom>> = slots |
| 1960 | + .into_iter() |
| 1961 | + .map(|s| s.map(|a| self.capture_atom(a))) |
| 1962 | + .collect(); |
| 1963 | + let vars: Vec<VarId> = slots |
| 1964 | + .iter() |
| 1965 | + .filter(|s| s.is_none()) |
| 1966 | + .map(|_| self.fresh()) |
| 1967 | + .collect(); |
| 1968 | + let body = self.block_value(|this| { |
| 1969 | + let mut vars = vars.iter(); |
| 1970 | + let args: Vec<Atom> = slots |
| 1971 | + .into_iter() |
| 1972 | + .map(|s| s.unwrap_or_else(|| Atom::Var(*vars.next().expect("one per hole")))) |
| 1973 | + .collect(); |
| 1974 | + let (effect, op) = this.effect_and_op(def); |
| 1975 | + let cap = caps.into_iter().next().unwrap_or(Atom::Unit); |
| 1976 | + this.emit(Value::Perform { |
| 1977 | + cap, |
| 1978 | + effect, |
| 1979 | + op, |
| 1980 | + args, |
| 1981 | + }) |
| 1982 | + }); |
| 1983 | + let frame = self.frames.pop().expect("pushed"); |
| 1984 | + let mut params: Vec<VarId> = frame.captures.iter().map(|(_, v)| *v).collect(); |
| 1985 | + params.extend(&vars); |
| 1986 | + self.define( |
| 1987 | + fid, |
| 1988 | + FnDecl { |
| 1989 | + name: frame.name, |
| 1990 | + params, |
| 1991 | + body, |
| 1992 | + n_vars: frame.n_vars, |
| 1993 | + borrowed: Default::default(), |
| 1994 | + span: self.span, |
| 1995 | + module: None, |
| 1996 | + shapes: Default::default(), |
| 1997 | + fip: false, |
| 1998 | + cpr_arity: None, |
| 1999 | + unbox_sig: None, |
| 2000 | + }, |
| 2001 | + ); |
| 2002 | + let captures = frame |
| 2003 | + .captures |
| 2004 | + .iter() |
| 2005 | + .map(|(key, _)| self.captured_atom(*key)) |
| 2006 | + .collect(); |
| 2007 | + self.emit(Value::Closure(fid, captures)) |
| 2008 | + } |
| 2009 | + |
| 1595 | 2010 | fn binary(&mut self, e: ExprId, op: hb::BinOp, lhs: ExprId, rhs: ExprId) -> Atom { |
| 1596 | 2011 | use hb::BinOp::*; |
| 1597 | 2012 | match op { |