| @@ -35,6 +35,11 @@ pub enum Ty { |
| 35 | 35 | Proj(DefId, Vec<Ty>), |
| 36 | 36 | /// The empty effect row `<>`: total. |
| 37 | 37 | RowEmpty, |
| 38 | + /// The end of a closed row that an evidence variable stands for (ADR |
| 39 | + /// 0066): empty like [`Ty::RowEmpty`], but an arrow whose row ends in |
| 40 | + /// it receives its handlers bundled, as one evidence argument. The two |
| 41 | + /// ends never unify. |
| 42 | + RowEvidence, |
| 38 | 43 | /// An effect row `<label | rest>`; the label is `Con(effect, args)`. |
| 39 | 44 | /// Labels may repeat (scoped labels): the leftmost one is innermost. |
| 40 | 45 | RowExtend(Box<Ty>, Box<Ty>), |
| @@ -172,7 +177,12 @@ impl Ty { |
| 172 | 177 | || caps.contains_error() |
| 173 | 178 | } |
| 174 | 179 | Ty::RowExtend(l, r) => l.contains_error() || r.contains_error(), |
| 175 | | − Ty::Var(_) | Ty::Rigid(_) | Ty::Gen(_) | Ty::Bound(_) | Ty::RowEmpty => false, |
| 180 | + Ty::Var(_) |
| 181 | + | Ty::Rigid(_) |
| 182 | + | Ty::Gen(_) |
| 183 | + | Ty::Bound(_) |
| 184 | + | Ty::RowEmpty |
| 185 | + | Ty::RowEvidence => false, |
| 176 | 186 | } |
| 177 | 187 | } |
| 178 | 188 | |
| @@ -216,9 +226,13 @@ impl Ty { |
| 216 | 226 | Ty::RowExtend(l, r) => Ty::RowExtend(Box::new(l.map(f)), Box::new(r.map(f))), |
| 217 | 227 | // Leaves. Listed so that a new variant with children cannot be |
| 218 | 228 | // skipped by every substitution and walk. |
| 219 | | − Ty::Var(_) | Ty::Rigid(_) | Ty::Gen(_) | Ty::Bound(_) | Ty::RowEmpty | Ty::Error => { |
| 220 | | − self.clone() |
| 221 | | − } |
| 229 | + Ty::Var(_) |
| 230 | + | Ty::Rigid(_) |
| 231 | + | Ty::Gen(_) |
| 232 | + | Ty::Bound(_) |
| 233 | + | Ty::RowEmpty |
| 234 | + | Ty::RowEvidence |
| 235 | + | Ty::Error => self.clone(), |
| 222 | 236 | } |
| 223 | 237 | } |
| 224 | 238 | |
| @@ -255,6 +269,7 @@ impl Ty { |
| 255 | 269 | | Ty::Gen(_) |
| 256 | 270 | | Ty::Bound(_) |
| 257 | 271 | | Ty::RowEmpty |
| 272 | + | Ty::RowEvidence |
| 258 | 273 | | Ty::Error => {} |
| 259 | 274 | } |
| 260 | 275 | } |
| @@ -279,7 +294,13 @@ impl Ty { |
| 279 | 294 | l.visit(f); |
| 280 | 295 | r.visit(f); |
| 281 | 296 | } |
| 282 | | − Ty::Var(_) | Ty::Rigid(_) | Ty::Gen(_) | Ty::Bound(_) | Ty::RowEmpty | Ty::Error => {} |
| 297 | + Ty::Var(_) |
| 298 | + | Ty::Rigid(_) |
| 299 | + | Ty::Gen(_) |
| 300 | + | Ty::Bound(_) |
| 301 | + | Ty::RowEmpty |
| 302 | + | Ty::RowEvidence |
| 303 | + | Ty::Error => {} |
| 283 | 304 | } |
| 284 | 305 | } |
| 285 | 306 | } |
| @@ -328,6 +349,9 @@ pub struct Scheme { |
| 328 | 349 | /// (ADR 0047). |
| 329 | 350 | pub equalities: Vec<Equality>, |
| 330 | 351 | pub ty: Ty, |
| 352 | + /// The bound variables that are evidence variables (ADR 0066), by |
| 353 | + /// index: they may stand for handlers a caller supplies. |
| 354 | + pub evidence: Vec<u32>, |
| 331 | 355 | } |
| 332 | 356 | |
| 333 | 357 | /// `Elem<c> = Int`, or, quantified, `Mapped<c, x> = List<x>` for every |
| @@ -372,6 +396,7 @@ impl Scheme { |
| 372 | 396 | markers: Vec::new(), |
| 373 | 397 | equalities: Vec::new(), |
| 374 | 398 | ty, |
| 399 | + evidence: Vec::new(), |
| 375 | 400 | } |
| 376 | 401 | } |
| 377 | 402 | |
| @@ -387,6 +412,33 @@ enum VarState { |
| 387 | 412 | Bound(Ty), |
| 388 | 413 | } |
| 389 | 414 | |
| 415 | +/// Whether a row variable may stand for handlers a caller supplies |
| 416 | +/// (`Bound` labels): an evidence variable may, a plain one may not (ADR |
| 417 | +/// 0066). A plain variable's arrows receive those handlers one by one, an |
| 418 | +/// evidence variable's bundled, so neither becomes the other: binding one |
| 419 | +/// to the other, or to the other's closed end, fails. An undecided |
| 420 | +/// variable (made while inferring a body) takes the mark of what it is |
| 421 | +/// bound to, and refuses `Bound` labels like a plain one. |
| 422 | +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 423 | +pub enum EvidenceMark { |
| 424 | + #[default] |
| 425 | + Undecided, |
| 426 | + Evidence, |
| 427 | + Plain, |
| 428 | +} |
| 429 | + |
| 430 | +/// How a variable instantiating the scheme's `i`-th bound variable is |
| 431 | +/// marked: as the scheme says. One that is not an evidence variable is |
| 432 | +/// plain, never undecided: the scheme's code receives its handlers one by |
| 433 | +/// one (ADR 0066). |
| 434 | +pub fn scheme_mark(scheme: &Scheme, i: u32) -> EvidenceMark { |
| 435 | + if scheme.evidence.contains(&i) { |
| 436 | + EvidenceMark::Evidence |
| 437 | + } else { |
| 438 | + EvidenceMark::Plain |
| 439 | + } |
| 440 | +} |
| 441 | + |
| 390 | 442 | /// The effect a row label refers to. |
| 391 | 443 | pub fn effect_head(label: &Ty) -> Option<DefId> { |
| 392 | 444 | match label { |
| @@ -408,6 +460,10 @@ pub enum UnifyError { |
| 408 | 460 | /// supply (`Bound`), of this effect: whoever calls a function of that |
| 409 | 461 | /// row would not know to supply it (ADR 0010). |
| 410 | 462 | Receives(DefId), |
| 463 | + /// An arrow that receives its handlers bundled (its row ends in an |
| 464 | + /// evidence variable or `RowEvidence`) would be one that receives them |
| 465 | + /// one by one, or the reverse (ADR 0066). |
| 466 | + Bundled, |
| 411 | 467 | /// A projection whose arguments are not known yet, so it cannot be |
| 412 | 468 | /// computed (ADR 0047). |
| 413 | 469 | Stuck, |
| @@ -498,9 +554,17 @@ impl AssocTable { |
| 498 | 554 | pub struct InferCtx { |
| 499 | 555 | vars: Vec<VarState>, |
| 500 | 556 | var_levels: Vec<u32>, |
| 557 | + /// Which variables are evidence variables (ADR 0066). |
| 558 | + var_marks: Vec<EvidenceMark>, |
| 501 | 559 | rigid_names: Vec<String>, |
| 502 | 560 | /// 0 for signature variables (never out of scope). |
| 503 | 561 | rigid_levels: Vec<u32>, |
| 562 | + /// Which rigid row variables are evidence variables: plain unless the |
| 563 | + /// signature says so. |
| 564 | + rigid_marks: Vec<EvidenceMark>, |
| 565 | + /// Whether any variable is an evidence variable: if not, no row |
| 566 | + /// argument needs resolving early. |
| 567 | + evidence_seen: bool, |
| 504 | 568 | /// Local type equalities (ADR 0041): inside a match arm on a GADT |
| 505 | 569 | /// constructor, a rigid variable may stand for a type (`a` is `Int` |
| 506 | 570 | /// in the `IntLit` arm). [`Self::shallow`] follows them; variables are |
| @@ -531,19 +595,80 @@ pub struct InferCtx { |
| 531 | 595 | |
| 532 | 596 | impl InferCtx { |
| 533 | 597 | pub fn fresh(&mut self) -> Ty { |
| 598 | + self.fresh_marked(EvidenceMark::Undecided) |
| 599 | + } |
| 600 | + |
| 601 | + /// A fresh variable that is, or is not, an evidence variable. |
| 602 | + pub fn fresh_marked(&mut self, mark: EvidenceMark) -> Ty { |
| 603 | + self.evidence_seen |= mark == EvidenceMark::Evidence; |
| 534 | 604 | self.vars.push(VarState::Unbound); |
| 535 | 605 | self.var_levels.push(self.level); |
| 606 | + self.var_marks.push(mark); |
| 536 | 607 | Ty::Var(self.vars.len() as u32 - 1) |
| 537 | 608 | } |
| 538 | 609 | |
| 539 | 610 | /// A rigid variable that is in scope everywhere (a signature's). |
| 540 | 611 | pub fn fresh_rigid(&mut self, name: &str) -> Ty { |
| 612 | + self.push_rigid(name, 0) |
| 613 | + } |
| 614 | + |
| 615 | + fn push_rigid(&mut self, name: &str, level: u32) -> Ty { |
| 541 | 616 | self.rigid_names.push(name.to_owned()); |
| 542 | | − self.rigid_levels.push(0); |
| 617 | + self.rigid_levels.push(level); |
| 543 | 618 | self.rigid_eqs.push(None); |
| 619 | + self.rigid_marks.push(EvidenceMark::Plain); |
| 544 | 620 | Ty::Rigid(self.rigid_names.len() as u32 - 1) |
| 545 | 621 | } |
| 546 | 622 | |
| 623 | + /// Mark the rigid row variable `r` an evidence variable (a signature's |
| 624 | + /// that occurs in a row argument, ADR 0066). |
| 625 | + pub fn mark_rigid_evidence(&mut self, r: u32) { |
| 626 | + self.evidence_seen = true; |
| 627 | + self.rigid_marks[r as usize] = EvidenceMark::Evidence; |
| 628 | + } |
| 629 | + |
| 630 | + /// Mark the row variable `ty` an evidence variable if it is still |
| 631 | + /// undecided (an annotation's that occurs in a row argument). |
| 632 | + pub fn mark_evidence(&mut self, ty: &Ty) { |
| 633 | + if let Ty::Var(v) = self.shallow(ty) |
| 634 | + && self.var_marks[v as usize] == EvidenceMark::Undecided |
| 635 | + { |
| 636 | + self.evidence_seen = true; |
| 637 | + self.var_marks[v as usize] = EvidenceMark::Evidence; |
| 638 | + } |
| 639 | + } |
| 640 | + |
| 641 | + /// The mark of the unbound variable or rigid `ty`, or of the closed |
| 642 | + /// end of a row: what a row ending there may stand for (ADR 0066). |
| 643 | + /// `None` for anything else. |
| 644 | + pub fn end_mark(&self, ty: &Ty) -> Option<EvidenceMark> { |
| 645 | + match ty { |
| 646 | + Ty::Var(v) => Some(self.var_marks[*v as usize]), |
| 647 | + Ty::Rigid(r) => Some(self.rigid_marks[*r as usize]), |
| 648 | + Ty::RowEmpty => Some(EvidenceMark::Plain), |
| 649 | + Ty::RowEvidence => Some(EvidenceMark::Evidence), |
| 650 | + _ => None, |
| 651 | + } |
| 652 | + } |
| 653 | + |
| 654 | + /// Whether `ty` is a bundled arrow: one whose row ends in an evidence |
| 655 | + /// variable or `RowEvidence`, which receives the handlers its callers |
| 656 | + /// supply as one evidence argument (ADR 0066). |
| 657 | + pub fn is_bundled(&self, ty: &Ty) -> bool { |
| 658 | + match self.shallow(ty) { |
| 659 | + Ty::Fn(_, _, eff, _) => { |
| 660 | + let (_, end) = self.row_labels(&eff); |
| 661 | + self.end_mark(&end) == Some(EvidenceMark::Evidence) |
| 662 | + } |
| 663 | + _ => false, |
| 664 | + } |
| 665 | + } |
| 666 | + |
| 667 | + /// The scope level the next variable is made at. |
| 668 | + pub fn level(&self) -> u32 { |
| 669 | + self.level |
| 670 | + } |
| 671 | + |
| 547 | 672 | /// Enter a scope that introduces scoped rigids (`region`, `handle`, a |
| 548 | 673 | /// lambda receiving capabilities). |
| 549 | 674 | pub fn enter(&mut self) { |
| @@ -557,10 +682,14 @@ impl InferCtx { |
| 557 | 682 | /// A rigid variable visible only inside the current scope: a region, a |
| 558 | 683 | /// handler instance, a capability received by a lambda. |
| 559 | 684 | pub fn fresh_scoped(&mut self, name: &str) -> Ty { |
| 560 | | − self.rigid_names.push(name.to_owned()); |
| 561 | | − self.rigid_levels.push(self.level); |
| 562 | | − self.rigid_eqs.push(None); |
| 563 | | − Ty::Rigid(self.rigid_names.len() as u32 - 1) |
| 685 | + self.push_rigid(name, self.level) |
| 686 | + } |
| 687 | + |
| 688 | + /// A rigid variable visible only inside the scope at `level`, made |
| 689 | + /// from deeper in it: a handler a lambda receives through its evidence |
| 690 | + /// (ADR 0066), known only once the lambda's row is. |
| 691 | + pub fn fresh_scoped_at(&mut self, name: &str, level: u32) -> Ty { |
| 692 | + self.push_rigid(name, level) |
| 564 | 693 | } |
| 565 | 694 | |
| 566 | 695 | pub fn rigid_names(&self) -> &[String] { |
| @@ -736,7 +865,9 @@ impl InferCtx { |
| 736 | 865 | |
| 737 | 866 | /// A fresh instance of `scheme`. |
| 738 | 867 | pub fn instantiate(&mut self, scheme: &Scheme) -> Instance { |
| 739 | | − let args: Vec<Ty> = scheme.vars.iter().map(|_| self.fresh()).collect(); |
| 868 | + let args: Vec<Ty> = (0..scheme.vars.len() as u32) |
| 869 | + .map(|i| self.fresh_marked(scheme_mark(scheme, i))) |
| 870 | + .collect(); |
| 740 | 871 | let inst = |cs: &[Constraint]| cs.iter().map(|c| c.subst_gen(&args)).collect(); |
| 741 | 872 | Instance { |
| 742 | 873 | ty: scheme.ty.subst_gen(&args), |
| @@ -754,13 +885,19 @@ impl InferCtx { |
| 754 | 885 | /// visible at `v`'s level; lowers the level of `ty`'s variables to |
| 755 | 886 | /// `v`'s (they are now reachable from there). |
| 756 | 887 | fn bind(&mut self, v: u32, ty: Ty) -> Result<(), UnifyError> { |
| 757 | | − if let Ty::RowExtend(..) = ty { |
| 888 | + let mark = self.var_marks[v as usize]; |
| 889 | + // Only an evidence variable stands for handlers a caller supplies |
| 890 | + // (ADR 0066). |
| 891 | + if let Ty::RowExtend(..) = ty |
| 892 | + && mark != EvidenceMark::Evidence |
| 893 | + { |
| 758 | 894 | for l in self.row_labels(&ty).0 { |
| 759 | 895 | if let (Some(Ty::Bound(_)), Some(head)) = (self.instance(&l), effect_head(&l)) { |
| 760 | 896 | return Err(UnifyError::Receives(head)); |
| 761 | 897 | } |
| 762 | 898 | } |
| 763 | 899 | } |
| 900 | + let end_var = self.meet_end(mark, &ty)?; |
| 764 | 901 | let level = self.var_levels[v as usize]; |
| 765 | 902 | let mut result = Ok(()); |
| 766 | 903 | let mut lower = Vec::new(); |
| @@ -786,10 +923,33 @@ impl InferCtx { |
| 786 | 923 | let l = &mut self.var_levels[u as usize]; |
| 787 | 924 | *l = (*l).min(level); |
| 788 | 925 | } |
| 926 | + if let Some(u) = end_var { |
| 927 | + self.var_marks[u as usize] = mark; |
| 928 | + } |
| 789 | 929 | self.vars[v as usize] = VarState::Bound(ty); |
| 790 | 930 | Ok(()) |
| 791 | 931 | } |
| 792 | 932 | |
| 933 | + /// Whether a variable marked `mark` may stand for the row `ty`, by the |
| 934 | + /// mark of its end. A decided mark passes to an undecided end variable: |
| 935 | + /// returns that variable, to mark once the binding is made. |
| 936 | + fn meet_end(&self, mark: EvidenceMark, ty: &Ty) -> Result<Option<u32>, UnifyError> { |
| 937 | + if mark == EvidenceMark::Undecided |
| 938 | + || !matches!( |
| 939 | + ty, |
| 940 | + Ty::RowExtend(..) | Ty::RowEmpty | Ty::RowEvidence | Ty::Var(_) | Ty::Rigid(_) |
| 941 | + ) |
| 942 | + { |
| 943 | + return Ok(None); |
| 944 | + } |
| 945 | + let (_, end) = self.row_labels(ty); |
| 946 | + match (self.end_mark(&end), &end) { |
| 947 | + (Some(EvidenceMark::Undecided), Ty::Var(u)) => Ok(Some(*u)), |
| 948 | + (Some(m), _) if m != mark && m != EvidenceMark::Undecided => Err(UnifyError::Bundled), |
| 949 | + _ => Ok(None), |
| 950 | + } |
| 951 | + } |
| 952 | + |
| 793 | 953 | /// Unify `a` and `b`. A projection that cannot be computed yet makes |
| 794 | 954 | /// it fail (`Stuck`); inference sets those aside instead, with |
| 795 | 955 | /// [`Self::unify_deferring`]. |
| @@ -882,7 +1042,10 @@ impl InferCtx { |
| 882 | 1042 | (Ty::App(h, args), Ty::Con(d, cargs)) | (Ty::Con(d, cargs), Ty::App(h, args)) => { |
| 883 | 1043 | self.unify_app_con(h, args, *d, cargs) |
| 884 | 1044 | } |
| 885 | | − (Ty::RowEmpty, Ty::RowEmpty) => Ok(()), |
| 1045 | + (Ty::RowEmpty, Ty::RowEmpty) | (Ty::RowEvidence, Ty::RowEvidence) => Ok(()), |
| 1046 | + (Ty::RowEmpty, Ty::RowEvidence) | (Ty::RowEvidence, Ty::RowEmpty) => { |
| 1047 | + Err(UnifyError::Bundled) |
| 1048 | + } |
| 886 | 1049 | (Ty::RowExtend(label, rest), other) | (other, Ty::RowExtend(label, rest)) => { |
| 887 | 1050 | self.unify_row(label, rest, other) |
| 888 | 1051 | } |
| @@ -1001,6 +1164,45 @@ impl InferCtx { |
| 1001 | 1164 | } |
| 1002 | 1165 | } |
| 1003 | 1166 | |
| 1167 | + /// Labels and tail of a row, as [`Self::row_labels`], each with |
| 1168 | + /// whether it was reached through a bound evidence variable: then a |
| 1169 | + /// `Bound` instance is a handler supplied through that variable's |
| 1170 | + /// evidence (ADR 0066). |
| 1171 | + pub fn row_labels_through(&self, row: &Ty) -> (Vec<(Ty, bool)>, Ty) { |
| 1172 | + let mut labels = Vec::new(); |
| 1173 | + let mut through = false; |
| 1174 | + let mut cur = row.clone(); |
| 1175 | + loop { |
| 1176 | + if let Ty::Var(v) = cur |
| 1177 | + && let VarState::Bound(t) = &self.vars[v as usize] |
| 1178 | + { |
| 1179 | + through |= self.var_marks[v as usize] == EvidenceMark::Evidence; |
| 1180 | + cur = t.clone(); |
| 1181 | + continue; |
| 1182 | + } |
| 1183 | + match self.shallow(&cur) { |
| 1184 | + Ty::RowExtend(l, r) => { |
| 1185 | + labels.push((*l, through)); |
| 1186 | + cur = *r; |
| 1187 | + } |
| 1188 | + tail => return (labels, tail), |
| 1189 | + } |
| 1190 | + } |
| 1191 | + } |
| 1192 | + |
| 1193 | + /// Whether `ty` mentions an unbound evidence variable. |
| 1194 | + pub fn mentions_open_evidence(&self, ty: &Ty) -> bool { |
| 1195 | + if !self.evidence_seen { |
| 1196 | + return false; |
| 1197 | + } |
| 1198 | + let mut found = false; |
| 1199 | + self.zonk(ty).visit(&mut |t| { |
| 1200 | + found |= |
| 1201 | + matches!(t, Ty::Var(v) if self.var_marks[*v as usize] == EvidenceMark::Evidence) |
| 1202 | + }); |
| 1203 | + found |
| 1204 | + } |
| 1205 | + |
| 1004 | 1206 | /// Labels and tail of a row, with variables substituted. |
| 1005 | 1207 | pub fn row_parts(&self, row: &Ty) -> (Vec<Ty>, Ty) { |
| 1006 | 1208 | let mut labels = Vec::new(); |
| @@ -1176,7 +1378,7 @@ impl Printer<'_> { |
| 1176 | 1378 | // Unresolved inference variables in effect tails are noise |
| 1177 | 1379 | // (every use opens a row); named row variables are shown. |
| 1178 | 1380 | match &**eff { |
| 1179 | | − Ty::RowEmpty | Ty::Error | Ty::Var(_) => {} |
| 1381 | + Ty::RowEmpty | Ty::RowEvidence | Ty::Error | Ty::Var(_) => {} |
| 1180 | 1382 | row @ Ty::RowExtend(..) => { |
| 1181 | 1383 | out.push_str(" / "); |
| 1182 | 1384 | self.write(row, out); |
| @@ -1189,7 +1391,7 @@ impl Printer<'_> { |
| 1189 | 1391 | } |
| 1190 | 1392 | } |
| 1191 | 1393 | } |
| 1192 | | − Ty::RowEmpty | Ty::RowExtend(..) => self.write_row(ty, out), |
| 1394 | + Ty::RowEmpty | Ty::RowEvidence | Ty::RowExtend(..) => self.write_row(ty, out), |
| 1193 | 1395 | } |
| 1194 | 1396 | } |
| 1195 | 1397 | |
| @@ -1208,7 +1410,7 @@ impl Printer<'_> { |
| 1208 | 1410 | } |
| 1209 | 1411 | self.write(l, out); |
| 1210 | 1412 | } |
| 1211 | | − if !matches!(cur, Ty::RowEmpty | Ty::Var(_)) { |
| 1413 | + if !matches!(cur, Ty::RowEmpty | Ty::RowEvidence | Ty::Var(_)) { |
| 1212 | 1414 | out.push_str(if labels.is_empty() { "" } else { " | " }); |
| 1213 | 1415 | self.write(cur, out); |
| 1214 | 1416 | } |
| @@ -1476,6 +1678,80 @@ mod tests { |
| 1476 | 1678 | } |
| 1477 | 1679 | |
| 1478 | 1680 | #[test] |
| 1681 | + fn only_an_evidence_variable_stands_for_handlers_a_caller_supplies() { |
| 1682 | + // `<a | r>` where the caller supplies `a`'s handler (ADR 0066). |
| 1683 | + let d = defs(); |
| 1684 | + let mut cx = InferCtx::default(); |
| 1685 | + cx.instanced.insert(d.a); |
| 1686 | + let supplied = |cx: &mut InferCtx| { |
| 1687 | + let tail = cx.fresh(); |
| 1688 | + Ty::row(vec![Ty::Con(d.a, vec![Ty::Bound(0)])], tail) |
| 1689 | + }; |
| 1690 | + let evidence = cx.fresh_marked(EvidenceMark::Evidence); |
| 1691 | + let row = supplied(&mut cx); |
| 1692 | + assert!(cx.unify(&evidence, &row).is_ok()); |
| 1693 | + for mark in [EvidenceMark::Plain, EvidenceMark::Undecided] { |
| 1694 | + let plain = cx.fresh_marked(mark); |
| 1695 | + let row = supplied(&mut cx); |
| 1696 | + assert!(matches!( |
| 1697 | + cx.unify(&plain, &row), |
| 1698 | + Err(UnifyError::Receives(e)) if e == d.a |
| 1699 | + )); |
| 1700 | + } |
| 1701 | + } |
| 1702 | + |
| 1703 | + #[test] |
| 1704 | + fn evidence_rows_and_plain_rows_never_unify() { |
| 1705 | + // Their arrows receive handlers differently (ADR 0066). |
| 1706 | + let d = defs(); |
| 1707 | + let mut cx = InferCtx::default(); |
| 1708 | + let pairs = [ |
| 1709 | + (EvidenceMark::Evidence, Ty::RowEmpty), |
| 1710 | + (EvidenceMark::Plain, Ty::RowEvidence), |
| 1711 | + ]; |
| 1712 | + for (mark, end) in pairs { |
| 1713 | + let v = cx.fresh_marked(mark); |
| 1714 | + let row = Ty::row(vec![con(d.a)], end); |
| 1715 | + assert!(matches!(cx.unify(&v, &row), Err(UnifyError::Bundled))); |
| 1716 | + } |
| 1717 | + let (e, p) = ( |
| 1718 | + cx.fresh_marked(EvidenceMark::Evidence), |
| 1719 | + cx.fresh_marked(EvidenceMark::Plain), |
| 1720 | + ); |
| 1721 | + assert!(matches!(cx.unify(&e, &p), Err(UnifyError::Bundled))); |
| 1722 | + assert!(matches!( |
| 1723 | + cx.unify(&Ty::RowEvidence, &Ty::RowEmpty), |
| 1724 | + Err(UnifyError::Bundled) |
| 1725 | + )); |
| 1726 | + assert!(cx.unify(&Ty::RowEvidence, &Ty::RowEvidence).is_ok()); |
| 1727 | + // An undecided variable takes either. |
| 1728 | + let u = cx.fresh(); |
| 1729 | + assert!( |
| 1730 | + cx.unify(&u, &Ty::row(vec![con(d.b)], Ty::RowEvidence)) |
| 1731 | + .is_ok() |
| 1732 | + ); |
| 1733 | + } |
| 1734 | + |
| 1735 | + #[test] |
| 1736 | + fn an_evidence_row_keeps_its_mark_through_its_tail() { |
| 1737 | + let d = defs(); |
| 1738 | + let mut cx = InferCtx::default(); |
| 1739 | + cx.instanced.insert(d.a); |
| 1740 | + let e = cx.fresh_marked(EvidenceMark::Evidence); |
| 1741 | + let tail = cx.fresh(); |
| 1742 | + cx.unify(&e, &Ty::row(vec![con(d.b)], tail.clone())) |
| 1743 | + .unwrap(); |
| 1744 | + // The tail is an evidence variable now: it takes a supplied |
| 1745 | + // handler, and the arrow it ends is bundled. |
| 1746 | + let supplied = Ty::row(vec![Ty::Con(d.a, vec![Ty::Bound(0)])], Ty::RowEvidence); |
| 1747 | + assert!(cx.unify(&tail, &supplied).is_ok()); |
| 1748 | + let arrow = Ty::func_eff(vec![], Ty::unit(), e); |
| 1749 | + assert!(cx.is_bundled(&arrow)); |
| 1750 | + let plain = cx.fresh_marked(EvidenceMark::Plain); |
| 1751 | + assert!(!cx.is_bundled(&Ty::func_eff(vec![], Ty::unit(), plain))); |
| 1752 | + } |
| 1753 | + |
| 1754 | + #[test] |
| 1479 | 1755 | fn a_constructor_variable_takes_a_partial_application() { |
| 1480 | 1756 | // `f<Int> ~ List<Int>` gives `f = List` (ADR 0018). |
| 1481 | 1757 | let d = defs(); |