nac3core: clean up, fix broken test

This commit is contained in:
ychenfo 2021-09-09 02:08:07 +08:00
parent f1013d9a17
commit 917d447605
4 changed files with 35 additions and 31 deletions

View File

@ -551,7 +551,7 @@ impl TopLevelComposer {
} }
} }
println!("type_var_to_concrete_def1: {:?}", type_var_to_concrete_def); // println!("type_var_to_concrete_def1: {:?}", type_var_to_concrete_def);
// handle the inheritanced methods and fields // handle the inheritanced methods and fields
let mut current_ancestor_depth: usize = 2; let mut current_ancestor_depth: usize = 2;
@ -587,19 +587,19 @@ impl TopLevelComposer {
} }
} }
println!("type_var_to_concrete_def3: {:?}\n", type_var_to_concrete_def); // println!("type_var_to_concrete_def3: {:?}\n", type_var_to_concrete_def);
// unification of previously assigned typevar // unification of previously assigned typevar
for (ty, def) in type_var_to_concrete_def { for (ty, def) in type_var_to_concrete_def {
println!( // println!(
"{:?}_{} -> {:?}\n", // "{:?}_{} -> {:?}\n",
ty, // ty,
unifier.stringify(ty, // unifier.stringify(ty,
&mut |id| format!("class{}", id), // &mut |id| format!("class{}", id),
&mut |id| format!("tvar{}", id) // &mut |id| format!("tvar{}", id)
), // ),
def // def
); // );
let target_ty = let target_ty =
get_type_from_type_annotation_kinds(&temp_def_list, unifier, primitives, &def)?; get_type_from_type_annotation_kinds(&temp_def_list, unifier, primitives, &def)?;
unifier.unify(ty, target_ty)?; unifier.unify(ty, target_ty)?;

View File

@ -213,10 +213,13 @@ pub fn get_type_from_type_annotation_kinds(
// NOTE: check for compatible range here // NOTE: check for compatible range here
let mut result: HashMap<u32, Type> = HashMap::new(); let mut result: HashMap<u32, Type> = HashMap::new();
for (tvar, p) in type_vars.iter().zip(param_ty) { for (tvar, p) in type_vars.iter().zip(param_ty) {
if let TypeEnum::TVar { id, range, meta: TypeVarMeta::Generic } = unifier.get_ty(*tvar).as_ref() { if let TypeEnum::TVar { id, range, meta: TypeVarMeta::Generic } =
unifier.get_ty(*tvar).as_ref()
{
let ok: bool = { let ok: bool = {
// NOTE: create a temp type var and unify to check compatibility // NOTE: create a temp type var and unify to check compatibility
let temp = unifier.get_fresh_var_with_range(range.borrow().as_slice()); let temp =
unifier.get_fresh_var_with_range(range.borrow().as_slice());
unifier.unify(temp.0, p).is_ok() unifier.unify(temp.0, p).is_ok()
}; };
if ok { if ok {
@ -230,7 +233,7 @@ pub fn get_type_from_type_annotation_kinds(
&mut |id| format!("tvar{}", id) &mut |id| format!("tvar{}", id)
), ),
*id *id
)) ));
} }
} else { } else {
unreachable!("must be generic type var") unreachable!("must be generic type var")
@ -250,16 +253,16 @@ pub fn get_type_from_type_annotation_kinds(
(name.clone(), subst_ty) (name.clone(), subst_ty)
})); }));
println!("tobj_fields: {:?}", tobj_fields); // println!("tobj_fields: {:?}", tobj_fields);
println!( // println!(
"{:?}: {}\n", // "{:?}: {}\n",
tobj_fields.get("__init__").unwrap(), // tobj_fields.get("__init__").unwrap(),
unifier.stringify( // unifier.stringify(
*tobj_fields.get("__init__").unwrap(), // *tobj_fields.get("__init__").unwrap(),
&mut |id| format!("class{}", id), // &mut |id| format!("class{}", id),
&mut |id| format!("tvar{}", id) // &mut |id| format!("tvar{}", id)
) // )
); // );
Ok(unifier.add_ty(TypeEnum::TObj { Ok(unifier.add_ty(TypeEnum::TObj {
obj_id: *id, obj_id: *id,
@ -342,13 +345,15 @@ pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec<Ty
result.extend(get_type_var_contained_in_type_annotation(p)); result.extend(get_type_var_contained_in_type_annotation(p));
} }
} }
TypeAnnotation::ListKind(ann) => result.extend(get_type_var_contained_in_type_annotation(ann.as_ref())), TypeAnnotation::ListKind(ann) => {
result.extend(get_type_var_contained_in_type_annotation(ann.as_ref()))
}
TypeAnnotation::TupleKind(anns) => { TypeAnnotation::TupleKind(anns) => {
for a in anns { for a in anns {
result.extend(get_type_var_contained_in_type_annotation(a)); result.extend(get_type_var_contained_in_type_annotation(a));
} }
} }
TypeAnnotation::PrimitiveKind( .. ) => {} TypeAnnotation::PrimitiveKind(..) => {}
} }
result result
} }

View File

@ -350,7 +350,7 @@ impl TestEnvironment {
"}, "},
[("a", "fn[[x=bool], bool]"), ("b", "fn[[x=int32], int32]"), ("c", "bool"), [("a", "fn[[x=bool], bool]"), ("b", "fn[[x=int32], int32]"), ("c", "bool"),
("d", "int32"), ("foo1", "Foo[bool]"), ("foo2", "Foo[int32]")].iter().cloned().collect(), ("d", "int32"), ("foo1", "Foo[1->bool]"), ("foo2", "Foo[1->int32]")].iter().cloned().collect(),
&[] &[]
; "obj test")] ; "obj test")]
#[test_case(indoc! {" #[test_case(indoc! {"

View File

@ -714,10 +714,9 @@ impl Unifier {
let name = obj_to_name(obj_id.0); let name = obj_to_name(obj_id.0);
let params = params.borrow(); let params = params.borrow();
if !params.is_empty() { if !params.is_empty() {
let params = let params = params.iter().map(|(id, v)| {
params.iter().map(|(id, v)| format!("{}->{}", *id, self.stringify(*v, obj_to_name, var_to_name))
format!("{}->{}", *id, self.stringify(*v, obj_to_name, var_to_name)) });
);
// NOTE: sort to preserve order // NOTE: sort to preserve order
let mut params = params.sorted(); let mut params = params.sorted();
format!("{}[{}]", name, params.join(", ")) format!("{}[{}]", name, params.join(", "))