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
//! A [Hindley-Milner polymorphic typing system]. //! //! For brevity, the documentation heavily uses the three provided macros when //! creating types. //! //! A [`TypeSchema`] is a type that may have universally quantified type //! variables. A [`Context`] keeps track of assignments made to type variables //! so that you may manipulate [`Type`]s, which are unquantified and concrete. //! Hence a `TypeSchema` can be instantiated, using [`TypeSchema::instantiate`], //! into a `Context` in order to produce a corresponding `Type`. Two `Type`s //! under a particular `Context` can be unified using [`Context::unify`], which //! may record new type variable assignments in the `Context`. //! //! # Examples //! //! The basics: //! //! ``` //! # #[macro_use] extern crate polytype; //! use polytype::Context; //! //! # fn main() { //! // filter: ∀α. (α → bool) → [α] → [α] //! let t = ptp!(0; @arrow[ //! tp!(@arrow[tp!(0), tp!(bool)]), //! tp!(list(tp!(0))), //! tp!(list(tp!(0))), //! ]); //! //! // Quantified type schemas provide polymorphic behavior. //! assert_eq!(format!("{}", &t), "∀t0. (t0 → bool) → list(t0) → list(t0)"); //! //! // We can instantiate type schemas to remove quantifiers //! let mut ctx = Context::default(); //! let t = t.instantiate(&mut ctx); //! assert_eq!(format!("{}", &t), "(t0 → bool) → list(t0) → list(t0)"); //! //! // We can register a substiution for t0 in the context: //! ctx.extend(0, tp!(int)); //! let t = t.apply(&ctx); //! assert_eq!(format!("{}", &t), "(int → bool) → list(int) → list(int)"); //! # } //! ``` //! //! Extended example: //! //! ``` //! # #[macro_use] extern crate polytype; //! use polytype::Context; //! //! # fn main() { //! // reduce: ∀α. ∀β. (β → α → β) → β → [α] → β //! // We can represent the type schema of reduce using the included macros: //! let t = ptp!(0, 1; @arrow[ //! tp!(@arrow[tp!(1), tp!(0), tp!(1)]), //! tp!(1), //! tp!(list(tp!(0))), //! tp!(1), //! ]); //! assert_eq!(format!("{}", &t), "∀t0. ∀t1. (t1 → t0 → t1) → t1 → list(t0) → t1"); //! //! // Let's consider reduce when applied to a function that adds two ints //! //! // First, we create a new typing context to manage typing bookkeeping. //! let mut ctx = Context::default(); //! //! // Let's create a type representing binary addition. //! let tplus = tp!(@arrow[tp!(int), tp!(int), tp!(int)]); //! assert_eq!(format!("{}", &tplus), "int → int → int"); //! //! // We instantiate the type schema of reduce within our context //! // so new type variables will be distinct //! let t = t.instantiate(&mut ctx); //! assert_eq!(format!("{}", &t), "(t1 → t0 → t1) → t1 → list(t0) → t1"); //! //! // By unifying, we can ensure function applications obey type requirements. //! let treturn = ctx.new_variable(); //! let targ1 = ctx.new_variable(); //! let targ2 = ctx.new_variable(); //! ctx.unify( //! &t, //! &tp!(@arrow[ //! tplus.clone(), //! targ1.clone(), //! targ2.clone(), //! treturn.clone(), //! ]), //! ).expect("unifies"); //! //! // We can also now infer what arguments are needed and what gets returned //! assert_eq!(targ1.apply(&ctx), tp!(int)); // inferred arg 1: int //! assert_eq!(targ2.apply(&ctx), tp!(list(tp!(int)))); // inferred arg 2: list(int) //! assert_eq!(treturn.apply(&ctx), tp!(int)); // inferred return: int //! //! // Finally, we can see what form reduce takes by applying the new substitution //! let t = t.apply(&ctx); //! assert_eq!(format!("{}", &t), "(int → int → int) → int → list(int) → int"); //! # } //! ``` //! //! [`Context`]: struct.Context.html //! [`Context::unify`]: struct.Context.html#method.unify //! [`Type`]: enum.Type.html //! [`TypeSchema::instantiate`]: enum.TypeSchema.html#method.instantiate //! [`TypeSchema`]: enum.TypeSchema.html //! [Hindley-Milner polymorphic typing system]: https://en.wikipedia.org/wiki/Hindley–Milner_type_system extern crate itertools; #[macro_use] extern crate nom; #[macro_use] mod macros; use itertools::Itertools; use std::collections::{HashMap, VecDeque}; use std::fmt; /// Represents a [type variable][1] (an unknown type). /// /// [1]: https://en.wikipedia.org/wiki/Hindley–Milner_type_system#Free_type_variables pub type Variable = u32; /// Represents [polytypes][1] (uninstantiated, universally quantified types). /// /// The primary ways of creating a `TypeSchema` are with the [`ptp!`] macro or /// with [`Type::generalize`]. /// /// [1]: https://en.wikipedia.org/wiki/Hindley–Milner_type_system#Polytype /// [`ptp!`]: macro.ptp.html /// [`Type::generalize`]: enum.Type.html#method.generalize #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub enum TypeSchema { /// Non-polymorphic types (e.g. `α → β`, `int → bool`) Monotype(Type), /// Polymorphic types (e.g. `∀α. α → α`, `∀α. ∀β. α → β`) Polytype { /// The [`Variable`] being bound /// /// [`Variable`]: type.Variable.html variable: Variable, /// The type in which `variable` is bound body: Box<TypeSchema>, }, } impl TypeSchema { /// Returns a set of each [`Variable`] bound by the [`TypeSchema`]. /// /// [`Variable`]: type.Variable.html /// [`TypeSchema`]: enum.TypeSchema.html pub fn bound_variables(&self) -> Vec<Variable> { match *self { TypeSchema::Monotype(_) => vec![], TypeSchema::Polytype { variable, ref body } => { let mut bvs = body.bound_variables(); bvs.push(variable); bvs } } } pub fn is_bound(&self, v: Variable) -> bool { match *self { TypeSchema::Monotype(_) => false, TypeSchema::Polytype { variable, .. } if variable == v => true, TypeSchema::Polytype { ref body, .. } => body.is_bound(v), } } /// Returns a set of each free [`Variable`] in the [`TypeSchema`]. /// /// [`Variable`]: type.Variable.html /// [`TypeSchema`]: enum.TypeSchema.html pub fn free_vars(&self, ctx: &Context) -> Vec<Variable> { match *self { TypeSchema::Monotype(ref t) => t.free_vars(ctx), TypeSchema::Polytype { variable, ref body } => { let mut fvs = body.free_vars(ctx); fvs.retain(|&v| v != variable); fvs } } } /// The work of instantiation happens here. fn instantiate_helper( &self, ctx: &mut Context, substitution: &mut HashMap<Variable, Type>, ) -> Type { match *self { TypeSchema::Monotype(ref t) => t.substitute(substitution), TypeSchema::Polytype { variable, ref body } => { if let Type::Variable(v) = ctx.new_variable() { substitution.insert(variable, Type::Variable(v)); } body.instantiate_helper(ctx, substitution) } } } /// Instantiate a [`TypeSchema`] in the context by removing quantifiers. /// /// All type variables will be replaced with fresh type variables. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::Context; /// let mut ctx = Context::default(); /// /// let t1 = ptp!(3; list(tp!(3))); /// let t2 = ptp!(3; list(tp!(3))); /// /// let t1 = t1.instantiate(&mut ctx); /// let t2 = t2.instantiate(&mut ctx); /// assert_eq!(format!("{}", &t1), "list(t0)"); /// assert_eq!(format!("{}", &t2), "list(t1)"); /// # } /// ``` /// /// [`TypeSchema`]: enum.TypeSchema.html pub fn instantiate(&self, ctx: &mut Context) -> Type { self.instantiate_helper(ctx, &mut HashMap::new()) } /// Parse a [`TypeSchema`] from a string. This round-trips with [`Display`]. /// This is a **leaky** operation and should be avoided wherever possible: /// names of constructed types will remain until program termination. /// /// The "for-all" `∀` is optional. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::TypeSchema; /// let t_par = TypeSchema::parse("∀t0. t0 -> t0").expect("valid type"); /// let t_lit = ptp!(0; @arrow[tp!(0), tp!(0)]); /// assert_eq!(t_par, t_lit); /// /// let s = "∀t0. ∀t1. (t1 → t0 → t1) → t1 → list(t0) → t1"; /// let t = TypeSchema::parse(s).expect("valid type"); /// let round_trip = format!("{}", &t); /// assert_eq!(s, round_trip); /// # } /// ``` /// /// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html /// [`TypeSchema`]: enum.TypeSchema.html pub fn parse(s: &str) -> Result<TypeSchema, ()> { parser::parsep(s) } } impl fmt::Display for TypeSchema { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { TypeSchema::Polytype { variable, ref body } => write!(f, "∀t{}. {}", variable, body), TypeSchema::Monotype(ref t) => t.fmt(f), } } } /// Represents [monotypes][1] (fully instantiated, unquantified types). /// /// The primary ways of creating a `Type` are with the [`tp!`] macro or with /// [`TypeSchema::instantiate`]. /// /// [`tp!`]: macro.tp.html /// [`TypeSchema::instantiate`]: enum.TypeSchema.html#method.instantiate /// [1]: https://en.wikipedia.org/wiki/Hindley–Milner_type_system#Monotypes #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub enum Type { /// Primitive or composite types (e.g. `int`, `List(α)`, `α → β`) /// /// # Examples /// /// Primitives have no associated types: /// /// ``` /// # use polytype::Type; /// let tint = Type::Constructed("int", vec![]); /// assert_eq!(format!("{}", &tint), "int") /// ``` /// /// Composites have associated types: /// /// ``` /// # use polytype::Type; /// let tint = Type::Constructed("int", vec![]); /// let tlist_of_ints = Type::Constructed("list", vec![tint]); /// assert_eq!(format!("{}", &tlist_of_ints), "list(int)"); /// ``` /// /// With the macros: /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// let t = tp!(list(tp!(int))); /// assert_eq!(format!("{}", &t), "list(int)"); /// # } /// ``` Constructed(&'static str, Vec<Type>), /// Type variables (e.g. `α`, `β`) identified by de Bruin indices. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::Type; /// // any function: α → β /// let t = tp!(@arrow[Type::Variable(0), Type::Variable(1)]); /// assert_eq!(format!("{}", &t), "t0 → t1"); /// # } /// ``` /// /// With the macros: /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// // map: (α → β) → [α] → [β] /// let t = tp!(@arrow[ /// tp!(@arrow[tp!(0), tp!(1)]), /// tp!(list(tp!(0))), /// tp!(list(tp!(1))), /// ]); /// assert_eq!(format!("{}", &t), "(t0 → t1) → list(t0) → list(t1)"); /// # } /// ``` Variable(Variable), } impl Type { /// Construct a function type (i.e. `alpha` → `beta`). pub fn arrow(alpha: Type, beta: Type) -> Type { Type::Constructed("→", vec![alpha, beta]) } /// If the type is an arrow, get its associated argument and return types. pub fn as_arrow(&self) -> Option<(&Type, &Type)> { if let Type::Constructed("→", ref args) = *self { Some((&args[0], &args[1])) } else { None } } fn occurs(&self, v: Variable) -> bool { match *self { Type::Constructed(_, ref args) => args.iter().any(|t| t.occurs(v)), Type::Variable(n) => n == v, } } /// Supplying `is_return` helps arrows look cleaner. fn show(&self, is_return: bool) -> String { match *self { Type::Variable(v) => format!("t{}", v), Type::Constructed(name, ref args) => { if args.is_empty() { String::from(name) } else if name == "→" { Type::arrow_show(args, is_return) } else { format!("{}({})", name, args.iter().map(|t| t.show(true)).join(",")) } } } } /// Show specifically for arrow types fn arrow_show(args: &[Type], is_return: bool) -> String { if is_return { format!("{} → {}", args[0].show(false), args[1].show(true)) } else { format!("({} → {})", args[0].show(false), args[1].show(true)) } } /// If the type is an arrow, recursively get all curried function arguments. pub fn args(&self) -> Option<VecDeque<&Type>> { match *self { Type::Constructed("→", ref args) => { let mut tps = args[1].args().unwrap_or_default(); tps.push_front(&args[0]); Some(tps) } Type::Variable(_) | Type::Constructed(..) => None, } } /// If the type is an arrow, get its ultimate return type. pub fn returns(&self) -> Option<&Type> { match *self { Type::Constructed("→", ref args) => args[1].returns().or_else(|| Some(&args[1])), Type::Variable(_) | Type::Constructed(..) => None, } } /// Applies the type in a [`Context`]. /// /// This will substitute type variables for the values associated with them /// by the context. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::Context; /// let mut ctx = Context::default(); /// ctx.unify(&tp!(0), &tp!(int)).expect("unifies"); /// /// let t = tp!(list(tp!(0))); /// assert_eq!(format!("{}", &t), "list(t0)"); /// let t = t.apply(&ctx); /// assert_eq!(format!("{}", &t), "list(int)"); /// # } /// ``` /// /// [`Context`]: struct.Context.html pub fn apply(&self, ctx: &Context) -> Type { match *self { Type::Constructed(name, ref args) => { let args = args.iter().map(|t| t.apply(ctx)).collect(); Type::Constructed(name, args) } Type::Variable(v) => ctx.substitution .get(&v) .cloned() .unwrap_or_else(|| Type::Variable(v)), } } /// Like [`apply`], but works in-place. /// /// [`apply`]: #method.apply pub fn apply_mut(&mut self, ctx: &Context) { match *self { Type::Constructed(_, ref mut args) => for ref mut t in args { t.apply_mut(ctx) }, Type::Variable(v) => { *self = ctx.substitution .get(&v) .cloned() .unwrap_or_else(|| Type::Variable(v)); } } } /// Generalizes the type by binding free variables in a [`TypeSchema`]. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::{Context, Type}; /// let t = tp!(@arrow[tp!(0), tp!(1)]); /// assert_eq!(format!("{}", &t), "t0 → t1"); /// /// let mut ctx = Context::default(); /// ctx.extend(0, tp!(int)); /// let t_gen = t.apply(&ctx).generalize(&ctx); /// /// assert_eq!(format!("{}", t_gen), "∀t1. int → t1"); /// # } /// ``` /// /// [`TypeSchema`]: enum.TypeSchema.html pub fn generalize(&self, ctx: &Context) -> TypeSchema { let fvs = self.free_vars(ctx); let mut t = TypeSchema::Monotype(self.clone()); for v in &fvs { t = TypeSchema::Polytype { variable: *v, body: Box::new(t), }; } t } /// Compute all the free variables in a type. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::{Context, Type}; /// let t = tp!(@arrow[tp!(0), tp!(1)]); /// assert_eq!(format!("{}", &t), "t0 → t1"); /// /// let mut ctx = Context::default(); /// ctx.extend(0, tp!(int)); /// let fvs_computed = t.free_vars(&ctx); /// let fvs_expected = vec![1]; /// /// assert_eq!(fvs_computed, fvs_expected); /// # } /// ``` pub fn free_vars(&self, ctx: &Context) -> Vec<Variable> { match *self { Type::Constructed(_, ref args) => args.iter() .flat_map(|a| a.free_vars(ctx).into_iter()) .collect(), Type::Variable(v) => { if !ctx.substitution().contains_key(&v) { vec![v] } else { vec![] } } } } /// Perform a substitution. This is analogous to [`apply`]. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::Type; /// # use std::collections::HashMap; /// let t = tp!(@arrow[tp!(0), tp!(1)]); /// assert_eq!(format!("{}", &t), "t0 → t1"); /// /// let mut substitution = HashMap::new(); /// substitution.insert(0, tp!(int)); /// substitution.insert(1, tp!(bool)); /// let t = t.substitute(&substitution); /// /// assert_eq!(format!("{}", t), "int → bool"); /// # } /// ``` /// /// [`apply`]: #method.apply pub fn substitute(&self, substitution: &HashMap<Variable, Type>) -> Type { match *self { Type::Constructed(name, ref args) => { let args = args.iter().map(|t| t.substitute(substitution)).collect(); Type::Constructed(name, args) } Type::Variable(v) => substitution .get(&v) .cloned() .unwrap_or_else(|| Type::Variable(v)), } } /// Parse a type from a string. This round-trips with [`Display`]. This is a /// **leaky** operation and should be avoided wherever possible: names of /// constructed types will remain until program termination. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::Type; /// let t_par = Type::parse("int -> hashmap(str, list(bool))").expect("valid type"); /// let t_lit = tp!(@arrow[ /// tp!(int), /// tp!(hashmap( /// tp!(str), /// tp!(list(tp!(bool))), /// )), /// ]); /// assert_eq!(t_par, t_lit); /// /// let s = "(t1 → t0 → t1) → t1 → list(t0) → t1"; /// let t = Type::parse(s).expect("valid type"); /// let round_trip = format!("{}", &t); /// assert_eq!(s, round_trip); /// # } /// ``` /// /// [`Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html pub fn parse(s: &str) -> Result<Type, ()> { parser::parse(s) } } impl fmt::Display for Type { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { write!(f, "{}", self.show(true)) } } impl From<VecDeque<Type>> for Type { fn from(mut tps: VecDeque<Type>) -> Type { match tps.len() { 0 => panic!("cannot create a type from nothing"), 1 => tps.pop_front().unwrap(), 2 => { let alpha = tps.pop_front().unwrap(); let beta = tps.pop_front().unwrap(); Type::arrow(alpha, beta) } _ => { let alpha = tps.pop_front().unwrap(); Type::arrow(alpha, tps.into()) } } } } impl From<Vec<Type>> for Type { fn from(tps: Vec<Type>) -> Type { Type::from(VecDeque::from(tps)) } } /// Errors during unification. #[derive(Debug, Clone, PartialEq)] pub enum UnificationError { /// `Occurs` happens when occurs checks fail (i.e. a type variable is /// unified recursively). The id of the bad type variable is supplied. Occurs(Variable), /// `Failure` happens when symbols or type variants don't unify because of /// structural differences. Failure(Type, Type), } impl fmt::Display for UnificationError { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { match *self { UnificationError::Occurs(v) => write!(f, "Occurs({})", v), UnificationError::Failure(ref t1, ref t2) => { write!(f, "Failure({}, {})", t1.show(false), t2.show(false)) } } } } impl std::error::Error for UnificationError { fn description(&self) -> &str { "could not unify" } } /// A type environment. Useful for reasoning about [`Type`]s (e.g unification, /// type inference). /// /// Contexts track substitutions and generate fresh type variables. /// /// [`Type`]: enum.Type.html #[derive(Debug, Clone)] pub struct Context { substitution: HashMap<Variable, Type>, next: Variable, } impl Default for Context { fn default() -> Self { Context { substitution: HashMap::new(), next: 0, } } } impl Context { /// The substitution managed by the context. pub fn substitution(&self) -> &HashMap<Variable, Type> { &self.substitution } /// Create a new substitution for [`Type::Variable`] number `v` to the /// [`Type`] `t`. /// /// [`Type`]: enum.Type.html /// [`Type::Variable`]: enum.Type.html#variant.Variable pub fn extend(&mut self, v: Variable, t: Type) { self.substitution.insert(v, t); } /// Create a new [`Type::Variable`] from the next unused number. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::{Type, Context}; /// let mut ctx = Context::default(); /// /// // Get a fresh variable /// let t0 = ctx.new_variable(); /// assert_eq!(t0, Type::Variable(0)); /// /// // Instantiating a polytype will yield new variables /// let t = ptp!(0, 1; @arrow[tp!(0), tp!(1), tp!(1)]); /// let t = t.instantiate(&mut ctx); /// assert_eq!(format!("{}", t), "t1 → t2 → t2"); /// /// // Get another fresh variable /// let t3 = ctx.new_variable(); /// assert_eq!(t3, Type::Variable(3)); /// # } /// ``` /// /// [`Type::Variable`]: enum.Type.html#variant.Variable pub fn new_variable(&mut self) -> Type { self.next += 1; Type::Variable(self.next - 1) } /// Create constraints within the context that ensure `t1` and `t2` /// unify. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::Context; /// let mut ctx = Context::default(); /// /// let t1 = tp!(@arrow[tp!(int), tp!(0)]); /// let t2 = tp!(@arrow[tp!(1), tp!(bool)]); /// ctx.unify(&t1, &t2).expect("unifies"); /// /// let t1 = t1.apply(&ctx); /// let t2 = t2.apply(&ctx); /// assert_eq!(t1, t2); // int → bool /// # } /// ``` /// /// Unification errors leave the context unaffected. A /// [`UnificationError::Failure`] error happens when symbols don't match: /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::{Context, UnificationError}; /// let mut ctx = Context::default(); /// /// let t1 = tp!(@arrow[tp!(int), tp!(0)]); /// let t2 = tp!(@arrow[tp!(bool), tp!(1)]); /// let res = ctx.unify(&t1, &t2); /// /// if let Err(UnificationError::Failure(left, right)) = res { /// // failed to unify t1 with t2. /// assert_eq!(left, tp!(int)); /// assert_eq!(right, tp!(bool)); /// } else { unreachable!() } /// # } /// ``` /// /// An [`UnificationError::Occurs`] error happens when the same type /// variable occurs in both types in a circular way. Ensure you /// [`instantiate`][] your types properly, so type variables don't overlap /// unless you mean them to. /// /// ``` /// # #[macro_use] extern crate polytype; /// # fn main() { /// # use polytype::{Context, UnificationError}; /// let mut ctx = Context::default(); /// /// let t1 = tp!(1); /// let t2 = tp!(@arrow[tp!(bool), tp!(1)]); /// let res = ctx.unify(&t1, &t2); /// /// if let Err(UnificationError::Occurs(v)) = res { /// // failed to unify t1 with t2 because of circular type variable occurrence. /// // t1 would have to be bool -> bool -> ... ad infinitum. /// assert_eq!(v, 1); /// } else { unreachable!() } /// # } /// ``` /// /// [`UnificationError::Failure`]: enum.UnificationError.html#variant.Failure /// [`UnificationError::Occurs`]: enum.UnificationError.html#variant.Occurs /// [`instantiate`]: enum.Type.html#method.instantiate pub fn unify(&mut self, t1: &Type, t2: &Type) -> Result<(), UnificationError> { let mut ctx = self.clone(); ctx.unify_internal(t1, t2)?; *self = ctx; Ok(()) } /// unify_internal may mutate the context even with an error. The context on /// which it's called should be discarded if there's an error. fn unify_internal(&mut self, t1: &Type, t2: &Type) -> Result<(), UnificationError> { if t1 == t2 { return Ok(()); } match (t1, t2) { (&Type::Variable(v), _) => { if t2.occurs(v) { Err(UnificationError::Occurs(v)) } else { self.extend(v, t2.clone()); Ok(()) } } (_, &Type::Variable(v)) => { if t1.occurs(v) { Err(UnificationError::Occurs(v)) } else { self.extend(v, t1.clone()); Ok(()) } } (&Type::Constructed(n1, ref a1), &Type::Constructed(n2, ref a2)) => { if n1 != n2 { Err(UnificationError::Failure( Type::Constructed(n1, vec![]), Type::Constructed(n2, vec![]), )) } else { for (t1, t2) in a1.into_iter().zip(a2) { let mut t1 = t1.clone(); let mut t2 = t2.clone(); t1.apply_mut(self); t2.apply_mut(self); self.unify_internal(&t1, &t2)?; } Ok(()) } } } } } mod parser { use std::num::ParseIntError; use nom::types::CompleteStr; use nom::{alpha, digit}; use super::{Type, TypeSchema}; fn nom_u32(inp: CompleteStr) -> Result<u32, ParseIntError> { inp.0.parse() } named!(var<CompleteStr, Type>, do_parse!(tag!("t") >> num: map_res!(digit, nom_u32) >> (Type::Variable(num))) ); named!(constructed_simple<CompleteStr, Type>, do_parse!( name: alpha >> (Type::Constructed(leaky_str(name.0), vec![]))) ); named!(constructed_complex<CompleteStr, Type>, do_parse!( name: alpha >> args: delimited!( tag!("("), separated_list!(tag!(","), ws!(monotype)), tag!(")") ) >> (Type::Constructed(leaky_str(name.0), args))) ); named!(arrow<CompleteStr, Type>, do_parse!(alpha: ws!(alt!(parenthetical | var | constructed_complex | constructed_simple)) >> alt!(tag!("→") | tag!("->")) >> beta: ws!(monotype) >> (Type::arrow(alpha, beta))) ); named!(parenthetical<CompleteStr, Type>, delimited!(tag!("("), arrow, tag!(")")) ); named!(binding<CompleteStr, TypeSchema>, do_parse!(opt!(tag!("∀")) >> tag!("t") >> variable: map_res!(digit, nom_u32) >> ws!(tag!(".")) >> body: map!(polytype, Box::new) >> (TypeSchema::Polytype{variable, body})) ); named!(monotype<CompleteStr, Type>, alt!(arrow | var | constructed_complex | constructed_simple) ); named!(polytype<CompleteStr, TypeSchema>, alt!(map!(monotype, TypeSchema::Monotype) | binding) ); pub fn parse(input: &str) -> Result<Type, ()> { parsem(input) } pub fn parsem(input: &str) -> Result<Type, ()> { match monotype(CompleteStr(input)) { Ok((_, t)) => Ok(t), _ => Err(()), } } pub fn parsep(input: &str) -> Result<TypeSchema, ()> { match polytype(CompleteStr(input)) { Ok((_, t)) => Ok(t), _ => Err(()), } } fn leaky_str(s: &str) -> &'static str { unsafe { &mut *Box::into_raw(s.to_string().into_boxed_str()) } } }