hm-inference #6

Merged
sb10q merged 136 commits from hm-inference into master 2021-08-19 11:46:50 +08:00
2 changed files with 8 additions and 0 deletions
Showing only changes of commit f4121b570d - Show all commits

View File

@ -177,6 +177,7 @@ mod test {
] ]
; "record obj merge" ; "record obj merge"
)] )]
/// Test cases for valid unifications.
fn test_unify( fn test_unify(
variable_count: u32, variable_count: u32,
unify_pairs: &[(&'static str, &'static str)], unify_pairs: &[(&'static str, &'static str)],
@ -249,6 +250,7 @@ mod test {
(("v1", "v2"), "Recursive type is prohibited.") (("v1", "v2"), "Recursive type is prohibited.")
; "recursive type for lists" ; "recursive type for lists"
)] )]
/// Test cases for invalid unifications.
fn test_invalid_unification( fn test_invalid_unification(
variable_count: u32, variable_count: u32,
unify_pairs: &[(&'static str, &'static str)], unify_pairs: &[(&'static str, &'static str)],

View File

@ -225,6 +225,7 @@ impl Unifier {
table.probe_value(a).0 table.probe_value(a).0
} }
/// Unify two types, i.e. a = b.
pub fn unify(&self, mut a: Type, mut b: Type) -> Result<(), String> { pub fn unify(&self, mut a: Type, mut b: Type) -> Result<(), String> {
let (mut ty_a_cell, mut ty_b_cell) = { let (mut ty_a_cell, mut ty_b_cell) = {
let mut table = self.unification_table.borrow_mut(); let mut table = self.unification_table.borrow_mut();
@ -467,6 +468,10 @@ impl Unifier {
Ok(()) Ok(())
} }
/// Substitute type variables within a type into other types.
/// If this returns Some(T), T would be the substituted type.
/// If this returns None, the result type would be the original type
/// (no substitution has to be done).
pub fn subst(&self, a: Type, mapping: &VarMap) -> Option<Type> { pub fn subst(&self, a: Type, mapping: &VarMap) -> Option<Type> {
let ty_cell = self.unification_table.borrow_mut().probe_value(a); let ty_cell = self.unification_table.borrow_mut().probe_value(a);
let ty = ty_cell.borrow(); let ty = ty_cell.borrow();
@ -577,6 +582,7 @@ impl Unifier {
map2 map2
} }
/// Check whether two types are equal.
pub fn eq(&self, a: Type, b: Type) -> bool { pub fn eq(&self, a: Type, b: Type) -> bool {
if a == b { if a == b {
return true; return true;