| @@ -278,6 +278,51 @@ fn split_slots(slots: Vec<Option<Atom>>) -> (Vec<bool>, Vec<Atom>) { |
| 278 | 278 | (placed, slots.into_iter().flatten().collect()) |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | +/// A capability a partial application's closure passes its callee: taken |
| 282 | +/// where the partial application is written (captured), or the closure's |
| 283 | +/// `k`-th received one, which its callers supply (ADR 0062, ADR 0010). |
| 284 | +#[derive(Clone)] |
| 285 | +enum CapSource { |
| 286 | + Taken(Atom), |
| 287 | + Received(usize), |
| 288 | +} |
| 289 | + |
| 290 | +/// The capabilities to capture, in order. |
| 291 | +fn taken(caps: &[CapSource]) -> Vec<Atom> { |
| 292 | + caps.iter() |
| 293 | + .filter_map(|c| match c { |
| 294 | + CapSource::Taken(a) => Some(a.clone()), |
| 295 | + CapSource::Received(_) => None, |
| 296 | + }) |
| 297 | + .collect() |
| 298 | +} |
| 299 | + |
| 300 | +/// How many capabilities the closure receives. |
| 301 | +fn received(caps: &[CapSource]) -> usize { |
| 302 | + caps.iter() |
| 303 | + .filter_map(|c| match c { |
| 304 | + CapSource::Received(k) => Some(k + 1), |
| 305 | + CapSource::Taken(_) => None, |
| 306 | + }) |
| 307 | + .max() |
| 308 | + .unwrap_or(0) |
| 309 | +} |
| 310 | + |
| 311 | +/// The capabilities inside the closure, in the callee's order: the taken |
| 312 | +/// ones as captured (from `captured`), the received ones its parameters. |
| 313 | +fn caps_inside( |
| 314 | + caps: &[CapSource], |
| 315 | + captured: &mut impl Iterator<Item = Atom>, |
| 316 | + received: &[Atom], |
| 317 | +) -> Vec<Atom> { |
| 318 | + caps.iter() |
| 319 | + .map(|c| match c { |
| 320 | + CapSource::Taken(_) => captured.next().expect("one per taken capability"), |
| 321 | + CapSource::Received(k) => received[*k].clone(), |
| 322 | + }) |
| 323 | + .collect() |
| 324 | +} |
| 325 | + |
| 281 | 326 | fn holes(placed: &[bool]) -> usize { |
| 282 | 327 | placed.iter().filter(|p| !**p).count() |
| 283 | 328 | } |
| @@ -605,16 +650,18 @@ impl<'a> Lowerer<'a> { |
| 605 | 650 | self.frame().vars.insert(key, v); |
| 606 | 651 | } |
| 607 | 652 | |
| 608 | | − /// A closure over `outer`, atoms of the current frame, taking `holes` |
| 609 | | − /// more arguments (a partial application, ADR 0062, issue #321). Each |
| 610 | | − /// distinct variable of `outer` is captured once, as a parameter of |
| 611 | | − /// the new function; a literal is used as is. `body` builds the result |
| 612 | | − /// from `outer` as the new frame sees it, then the holes. |
| 653 | + /// A closure over `outer`, atoms of the current frame, receiving |
| 654 | + /// `received` capabilities then `holes` more arguments (a partial |
| 655 | + /// application, ADR 0062, issue #321). Each distinct variable of |
| 656 | + /// `outer` is captured once, as a parameter of the new function; a |
| 657 | + /// literal is used as is. `body` builds the result from `outer` as the |
| 658 | + /// new frame sees it, the received capabilities and the holes. |
| 613 | 659 | fn closure_over( |
| 614 | 660 | &mut self, |
| 615 | 661 | outer: Vec<Atom>, |
| 662 | + received: usize, |
| 616 | 663 | holes: usize, |
| 617 | | − body: impl FnOnce(&mut Self, std::vec::IntoIter<Atom>, Vec<Atom>) -> Atom, |
| 664 | + body: impl FnOnce(&mut Self, std::vec::IntoIter<Atom>, Vec<Atom>, Vec<Atom>) -> Atom, |
| 618 | 665 | ) -> Atom { |
| 619 | 666 | let parent = self.frame(); |
| 620 | 667 | parent.lambdas += 1; |
| @@ -639,11 +686,15 @@ impl<'a> Lowerer<'a> { |
| 639 | 686 | other => other, |
| 640 | 687 | }) |
| 641 | 688 | .collect(); |
| 689 | + let received: Vec<VarId> = (0..received).map(|_| self.fresh()).collect(); |
| 642 | 690 | let holes: Vec<VarId> = (0..holes).map(|_| self.fresh()).collect(); |
| 643 | | − let hole_atoms = holes.iter().map(|v| Atom::Var(*v)).collect(); |
| 644 | | − let body = self.block_value(|this| body(this, inner.into_iter(), hole_atoms)); |
| 691 | + let atoms = |vars: &[VarId]| vars.iter().map(|v| Atom::Var(*v)).collect(); |
| 692 | + let (received_atoms, hole_atoms) = (atoms(&received), atoms(&holes)); |
| 693 | + let body = |
| 694 | + self.block_value(|this| body(this, inner.into_iter(), received_atoms, hole_atoms)); |
| 645 | 695 | let frame = self.frames.pop().expect("pushed"); |
| 646 | 696 | let mut params: Vec<VarId> = captured.iter().map(|(_, own)| *own).collect(); |
| 697 | + params.extend(received); |
| 647 | 698 | params.extend(holes); |
| 648 | 699 | self.define( |
| 649 | 700 | fid, |
| @@ -1646,6 +1697,7 @@ impl<'a> Lowerer<'a> { |
| 1646 | 1697 | .results |
| 1647 | 1698 | .map(|r| r.types.exprs[callee.index()].clone()) |
| 1648 | 1699 | .unwrap_or(Ty::Error); |
| 1700 | + let caps = self.cap_sources(callee, caps); |
| 1649 | 1701 | return self.call_value(callee_atom.expect("unnamed callee"), caps, lowered, ty); |
| 1650 | 1702 | }; |
| 1651 | 1703 | let arity = self.arity_of(def); |
| @@ -1696,7 +1748,10 @@ impl<'a> Lowerer<'a> { |
| 1696 | 1748 | args, |
| 1697 | 1749 | }) |
| 1698 | 1750 | } |
| 1699 | | − DefKind::Op(_) if !complete => self.partial_perform(def, slots, caps), |
| 1751 | + DefKind::Op(_) if !complete => { |
| 1752 | + let caps = self.cap_sources(callee, caps); |
| 1753 | + self.partial_perform(def, slots, caps) |
| 1754 | + } |
| 1700 | 1755 | DefKind::Op(_) => { |
| 1701 | 1756 | let (effect, op) = self.effect_and_op(def); |
| 1702 | 1757 | let cap = caps.into_iter().next().unwrap_or(Atom::Unit); |
| @@ -1707,7 +1762,10 @@ impl<'a> Lowerer<'a> { |
| 1707 | 1762 | args, |
| 1708 | 1763 | }) |
| 1709 | 1764 | } |
| 1710 | | − DefKind::Fn(f) if !complete => self.partial_fn(def, slots, caps, callee), |
| 1765 | + DefKind::Fn(_) if !complete => { |
| 1766 | + let caps = self.cap_sources(callee, caps); |
| 1767 | + self.partial_fn(def, slots, caps, callee) |
| 1768 | + } |
| 1711 | 1769 | DefKind::Fn(f) => { |
| 1712 | 1770 | if let Some(sym) = self.extern_symbol(def) { |
| 1713 | 1771 | // An extern's C shim takes its declared arguments only: |
| @@ -1756,6 +1814,26 @@ impl<'a> Lowerer<'a> { |
| 1756 | 1814 | result |
| 1757 | 1815 | } |
| 1758 | 1816 | |
| 1817 | + /// The capabilities of the call at `callee`: for a partial application |
| 1818 | + /// whose expected arrow's callers supply some, those are received by |
| 1819 | + /// its closure (ADR 0062, ADR 0010); the others are taken here. |
| 1820 | + fn cap_sources(&mut self, callee: ExprId, caps: Vec<Atom>) -> Vec<CapSource> { |
| 1821 | + match self |
| 1822 | + .results |
| 1823 | + .and_then(|r| r.caps.partials.get(&callee)) |
| 1824 | + .cloned() |
| 1825 | + { |
| 1826 | + Some(args) => args |
| 1827 | + .iter() |
| 1828 | + .map(|a| match a { |
| 1829 | + CapArg::Param(k) => CapSource::Received(*k), |
| 1830 | + CapArg::Inst(t) => CapSource::Taken(self.cap(t)), |
| 1831 | + }) |
| 1832 | + .collect(), |
| 1833 | + None => caps.into_iter().map(CapSource::Taken).collect(), |
| 1834 | + } |
| 1835 | + } |
| 1836 | + |
| 1759 | 1837 | /// The return type of `ty`'s arrow after `arity` value parameters |
| 1760 | 1838 | /// (its parameters are the flattened chain, ADR 0062), if `ty` is a |
| 1761 | 1839 | /// function of that arity. |
| @@ -1776,7 +1854,7 @@ impl<'a> Lowerer<'a> { |
| 1776 | 1854 | fn call_value( |
| 1777 | 1855 | &mut self, |
| 1778 | 1856 | callee: Atom, |
| 1779 | | − caps: Vec<Atom>, |
| 1857 | + caps: Vec<CapSource>, |
| 1780 | 1858 | args: Vec<(Option<String>, Atom)>, |
| 1781 | 1859 | ty: Ty, |
| 1782 | 1860 | ) -> Atom { |
| @@ -1818,7 +1896,7 @@ impl<'a> Lowerer<'a> { |
| 1818 | 1896 | } |
| 1819 | 1897 | let full = slots.iter().all(Option::is_some); |
| 1820 | 1898 | if full { |
| 1821 | | − let atoms: Vec<Atom> = std::mem::take(&mut caps) |
| 1899 | + let atoms: Vec<Atom> = taken(&std::mem::take(&mut caps)) |
| 1822 | 1900 | .into_iter() |
| 1823 | 1901 | .chain(slots.into_iter().map(|s| s.unwrap())) |
| 1824 | 1902 | .collect(); |
| @@ -1836,16 +1914,24 @@ impl<'a> Lowerer<'a> { |
| 1836 | 1914 | /// a value (ADR 0062). The callee, the capabilities and the placed |
| 1837 | 1915 | /// arguments are captured; `slots` are the arrow's parameters, the |
| 1838 | 1916 | /// placed ones `Some`. |
| 1839 | | − fn eta(&mut self, callee: Atom, slots: Vec<Option<Atom>>, caps: Vec<Atom>) -> Atom { |
| 1840 | | − let n_caps = caps.len(); |
| 1917 | + fn eta(&mut self, callee: Atom, slots: Vec<Option<Atom>>, caps: Vec<CapSource>) -> Atom { |
| 1841 | 1918 | let (placed, outer) = split_slots(slots); |
| 1842 | | − let outer = std::iter::once(callee).chain(caps).chain(outer).collect(); |
| 1843 | | − self.closure_over(outer, holes(&placed), |this, mut inner, holes| { |
| 1844 | | − let callee = inner.next().expect("the callee"); |
| 1845 | | − let caps: Vec<Atom> = inner.by_ref().take(n_caps).collect(); |
| 1846 | | − let args = fill(&placed, inner, holes); |
| 1847 | | − this.emit(Value::Call(callee, caps.into_iter().chain(args).collect())) |
| 1848 | | − }) |
| 1919 | + let outer = std::iter::once(callee) |
| 1920 | + .chain(taken(&caps)) |
| 1921 | + .chain(outer) |
| 1922 | + .collect(); |
| 1923 | + let n_received = received(&caps); |
| 1924 | + self.closure_over( |
| 1925 | + outer, |
| 1926 | + n_received, |
| 1927 | + holes(&placed), |
| 1928 | + |this, mut inner, received, holes| { |
| 1929 | + let callee = inner.next().expect("the callee"); |
| 1930 | + let caps = caps_inside(&caps, &mut inner, &received); |
| 1931 | + let args = fill(&placed, inner, holes); |
| 1932 | + this.emit(Value::Call(callee, caps.into_iter().chain(args).collect())) |
| 1933 | + }, |
| 1934 | + ) |
| 1849 | 1935 | } |
| 1850 | 1936 | |
| 1851 | 1937 | /// `def` with some arguments given: a closure of the rest (ADR 0062). |
| @@ -1855,7 +1941,7 @@ impl<'a> Lowerer<'a> { |
| 1855 | 1941 | &mut self, |
| 1856 | 1942 | def: DefId, |
| 1857 | 1943 | slots: Vec<Option<Atom>>, |
| 1858 | | − caps: Vec<Atom>, |
| 1944 | + caps: Vec<CapSource>, |
| 1859 | 1945 | site: ExprId, |
| 1860 | 1946 | ) -> Atom { |
| 1861 | 1947 | // Dictionaries are found where the partial application is written. |
| @@ -1864,57 +1950,76 @@ impl<'a> Lowerer<'a> { |
| 1864 | 1950 | .iter() |
| 1865 | 1951 | .map(|e| self.materialize(e)) |
| 1866 | 1952 | .collect(); |
| 1867 | | − let (n_caps, n_ev) = (caps.len(), ev.len()); |
| 1953 | + let n_ev = ev.len(); |
| 1868 | 1954 | let (placed, outer) = split_slots(slots); |
| 1869 | | − let outer = caps.into_iter().chain(ev).chain(outer).collect(); |
| 1870 | | − self.closure_over(outer, holes(&placed), |this, mut inner, holes| { |
| 1871 | | − let caps: Vec<Atom> = inner.by_ref().take(n_caps).collect(); |
| 1872 | | − let ev: Vec<Atom> = inner.by_ref().take(n_ev).collect(); |
| 1873 | | − let args = fill(&placed, inner, holes); |
| 1874 | | − let DefKind::Fn(f) = &this.hir.def(def).kind else { |
| 1875 | | − return Atom::Unit; |
| 1876 | | − }; |
| 1877 | | − if let Some(sym) = this.extern_symbol(def) { |
| 1878 | | − return this.emit(Value::CallExtern(sym, args)); |
| 1879 | | − } |
| 1880 | | − let args: Vec<Atom> = caps.into_iter().chain(args).collect(); |
| 1881 | | − if let Some(owner) = f.owner |
| 1882 | | − && matches!(this.hir.def(owner).kind, DefKind::Trait(_)) |
| 1883 | | − { |
| 1884 | | − let Some((first, extra)) = ev.split_first() else { |
| 1955 | + let outer = taken(&caps).into_iter().chain(ev).chain(outer).collect(); |
| 1956 | + let n_received = received(&caps); |
| 1957 | + self.closure_over( |
| 1958 | + outer, |
| 1959 | + n_received, |
| 1960 | + holes(&placed), |
| 1961 | + |this, mut inner, received, holes| { |
| 1962 | + let caps = caps_inside(&caps, &mut inner, &received); |
| 1963 | + let ev: Vec<Atom> = inner.by_ref().take(n_ev).collect(); |
| 1964 | + let args = fill(&placed, inner, holes); |
| 1965 | + let DefKind::Fn(f) = &this.hir.def(def).kind else { |
| 1885 | 1966 | return Atom::Unit; |
| 1886 | 1967 | }; |
| 1887 | | − let index = this.method_index(owner, &this.hir.def(def).name); |
| 1888 | | − return this.method_call( |
| 1889 | | − first.clone(), |
| 1890 | | − index, |
| 1891 | | − extra.iter().cloned().chain(args).collect(), |
| 1892 | | − ); |
| 1893 | | − } |
| 1894 | | − this.emit(Value::CallFn( |
| 1895 | | − this.fn_ids[&def], |
| 1896 | | − ev.into_iter().chain(args).collect(), |
| 1897 | | − )) |
| 1898 | | − }) |
| 1968 | + if let Some(sym) = this.extern_symbol(def) { |
| 1969 | + return this.emit(Value::CallExtern(sym, args)); |
| 1970 | + } |
| 1971 | + let args: Vec<Atom> = caps.into_iter().chain(args).collect(); |
| 1972 | + if let Some(owner) = f.owner |
| 1973 | + && matches!(this.hir.def(owner).kind, DefKind::Trait(_)) |
| 1974 | + { |
| 1975 | + let Some((first, extra)) = ev.split_first() else { |
| 1976 | + return Atom::Unit; |
| 1977 | + }; |
| 1978 | + let index = this.method_index(owner, &this.hir.def(def).name); |
| 1979 | + return this.method_call( |
| 1980 | + first.clone(), |
| 1981 | + index, |
| 1982 | + extra.iter().cloned().chain(args).collect(), |
| 1983 | + ); |
| 1984 | + } |
| 1985 | + this.emit(Value::CallFn( |
| 1986 | + this.fn_ids[&def], |
| 1987 | + ev.into_iter().chain(args).collect(), |
| 1988 | + )) |
| 1989 | + }, |
| 1990 | + ) |
| 1899 | 1991 | } |
| 1900 | 1992 | |
| 1901 | 1993 | /// An operation with some arguments given: a closure performing it |
| 1902 | 1994 | /// once the rest arrive (ADR 0062). |
| 1903 | | − fn partial_perform(&mut self, def: DefId, slots: Vec<Option<Atom>>, caps: Vec<Atom>) -> Atom { |
| 1904 | | − let n_caps = caps.len(); |
| 1995 | + fn partial_perform( |
| 1996 | + &mut self, |
| 1997 | + def: DefId, |
| 1998 | + slots: Vec<Option<Atom>>, |
| 1999 | + caps: Vec<CapSource>, |
| 2000 | + ) -> Atom { |
| 1905 | 2001 | let (placed, outer) = split_slots(slots); |
| 1906 | | − let outer = caps.into_iter().chain(outer).collect(); |
| 1907 | | − self.closure_over(outer, holes(&placed), |this, mut inner, holes| { |
| 1908 | | − let cap = inner.by_ref().take(n_caps).next().unwrap_or(Atom::Unit); |
| 1909 | | − let args = fill(&placed, inner, holes); |
| 1910 | | − let (effect, op) = this.effect_and_op(def); |
| 1911 | | − this.emit(Value::Perform { |
| 1912 | | − cap, |
| 1913 | | − effect, |
| 1914 | | − op, |
| 1915 | | − args, |
| 1916 | | − }) |
| 1917 | | − }) |
| 2002 | + let outer = taken(&caps).into_iter().chain(outer).collect(); |
| 2003 | + let n_received = received(&caps); |
| 2004 | + self.closure_over( |
| 2005 | + outer, |
| 2006 | + n_received, |
| 2007 | + holes(&placed), |
| 2008 | + |this, mut inner, received, holes| { |
| 2009 | + let cap = caps_inside(&caps, &mut inner, &received) |
| 2010 | + .into_iter() |
| 2011 | + .next() |
| 2012 | + .unwrap_or(Atom::Unit); |
| 2013 | + let args = fill(&placed, inner, holes); |
| 2014 | + let (effect, op) = this.effect_and_op(def); |
| 2015 | + this.emit(Value::Perform { |
| 2016 | + cap, |
| 2017 | + effect, |
| 2018 | + op, |
| 2019 | + args, |
| 2020 | + }) |
| 2021 | + }, |
| 2022 | + ) |
| 1918 | 2023 | } |
| 1919 | 2024 | |
| 1920 | 2025 | fn binary(&mut self, e: ExprId, op: hb::BinOp, lhs: ExprId, rhs: ExprId) -> Atom { |