fixed virtual unification

This commit is contained in:
pca006132 2021-07-23 16:19:00 +08:00
parent ddcf4b7e39
commit d140164a38
1 changed files with 30 additions and 20 deletions

View File

@ -278,11 +278,24 @@ impl Unifier {
self.check_var_range(*id, b, &range.borrow())?; self.check_var_range(*id, b, &range.borrow())?;
self.set_a_to_b(a, b); self.set_a_to_b(a, b);
} }
(TVar { meta: Record(_), id, range, .. }, TVirtual { ty }) => { (TVar { meta: Record(map), id, range, .. }, TVirtual { ty }) => {
// TODO: look at this rule
self.occur_check(a, b)?; self.occur_check(a, b)?;
let ty = self.get_ty(*ty);
if let TObj { fields, .. } = ty.as_ref() {
for (k, v) in map.borrow().iter() {
if let Some(ty) = fields.get(k) {
if !matches!(self.get_ty(*ty).as_ref(), TFunc { .. }) {
return Err(format!("Cannot access field {} for virtual type", k));
}
self.unify(*v, *ty)?;
}
}
} else {
// require annotation...
return Err("Requires type annotation for virtual".to_string());
}
self.check_var_range(*id, b, &range.borrow())?; self.check_var_range(*id, b, &range.borrow())?;
self.unify(a, *ty)?; self.unify(a, b)?;
} }
( (
TObj { obj_id: id1, params: params1, .. }, TObj { obj_id: id1, params: params1, .. },
@ -394,21 +407,23 @@ impl Unifier {
match ty.as_ref() { match ty.as_ref() {
TypeEnum::TVar { id, meta: Generic, .. } => var_to_name(*id), TypeEnum::TVar { id, meta: Generic, .. } => var_to_name(*id),
TypeEnum::TVar { meta: Sequence(map), .. } => { TypeEnum::TVar { meta: Sequence(map), .. } => {
let fields = map.borrow().iter().map(|(k, v)| { let fields = map
format!("{}={}", k, self.stringify(*v, obj_to_name, var_to_name)) .borrow()
}).join(", "); .iter()
.map(|(k, v)| format!("{}={}", k, self.stringify(*v, obj_to_name, var_to_name)))
.join(", ");
format!("seq[{}]", fields) format!("seq[{}]", fields)
} }
TypeEnum::TVar { meta: Record(fields), .. } => { TypeEnum::TVar { meta: Record(fields), .. } => {
let fields = fields.borrow().iter().map(|(k, v)| { let fields = fields
format!("{}={}", k, self.stringify(*v, obj_to_name, var_to_name)) .borrow()
}).join(", "); .iter()
.map(|(k, v)| format!("{}={}", k, self.stringify(*v, obj_to_name, var_to_name)))
.join(", ");
format!("record[{}]", fields) format!("record[{}]", fields)
} }
TypeEnum::TTuple { ty } => { TypeEnum::TTuple { ty } => {
let mut fields = ty let mut fields = ty.iter().map(|v| self.stringify(*v, obj_to_name, var_to_name));
.iter()
.map(|v| self.stringify(*v, obj_to_name, var_to_name));
format!("tuple[{}]", fields.join(", ")) format!("tuple[{}]", fields.join(", "))
} }
TypeEnum::TList { ty } => { TypeEnum::TList { ty } => {
@ -420,9 +435,8 @@ impl Unifier {
TypeEnum::TObj { obj_id, params, .. } => { TypeEnum::TObj { obj_id, params, .. } => {
let name = obj_to_name(*obj_id); let name = obj_to_name(*obj_id);
if !params.is_empty() { if !params.is_empty() {
let mut params = params let mut params =
.values() params.values().map(|v| self.stringify(*v, obj_to_name, var_to_name));
.map(|v| self.stringify(*v, obj_to_name, var_to_name));
format!("{}[{}]", name, params.join(", ")) format!("{}[{}]", name, params.join(", "))
} else { } else {
name name
@ -434,11 +448,7 @@ impl Unifier {
.args .args
.iter() .iter()
.map(|arg| { .map(|arg| {
format!( format!("{}={}", arg.name, self.stringify(arg.ty, obj_to_name, var_to_name))
"{}={}",
arg.name,
self.stringify(arg.ty, obj_to_name, var_to_name)
)
}) })
.join(", "); .join(", ");
let ret = self.stringify(signature.ret, obj_to_name, var_to_name); let ret = self.stringify(signature.ret, obj_to_name, var_to_name);