forked from M-Labs/nac3
1
0
Fork 0

core: rename to_var_map to into_var_map

This commit is contained in:
lyken 2024-06-13 16:03:32 +08:00
parent aa94e0c8a4
commit fed1361c6a
7 changed files with 30 additions and 30 deletions

View File

@ -9,7 +9,7 @@ use nac3core::{
}, },
typecheck::{ typecheck::{
type_inferencer::PrimitiveStore, type_inferencer::PrimitiveStore,
typedef::{iter_type_vars, to_var_map, Type, TypeEnum, TypeVar, Unifier, VarMap}, typedef::{iter_type_vars, into_var_map, Type, TypeEnum, TypeVar, Unifier, VarMap},
}, },
}; };
use nac3parser::ast::{self, StrRef}; use nac3parser::ast::{self, StrRef};
@ -719,7 +719,7 @@ impl InnerResolver {
unreachable!("must be tobj") unreachable!("must be tobj")
}; };
let var_map = to_var_map(iter_type_vars(params).map(|tvar| { let var_map = into_var_map(iter_type_vars(params).map(|tvar| {
let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty) let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty)
else { else {
unreachable!() unreachable!()
@ -746,7 +746,7 @@ impl InnerResolver {
} }
(TypeEnum::TObj { params, fields, .. }, false) => { (TypeEnum::TObj { params, fields, .. }, false) => {
self.pyid_to_type.write().insert(py_obj_id, extracted_ty); self.pyid_to_type.write().insert(py_obj_id, extracted_ty);
let var_map = to_var_map(iter_type_vars(params).map(|tvar| { let var_map = into_var_map(iter_type_vars(params).map(|tvar| {
let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty) let TypeEnum::TVar { id, range, name, loc, .. } = &*unifier.get_ty(tvar.ty)
else { else {
unreachable!() unreachable!()

View File

@ -3,7 +3,7 @@ use crate::{
toplevel::DefinitionId, toplevel::DefinitionId,
typecheck::{ typecheck::{
type_inferencer::PrimitiveStore, type_inferencer::PrimitiveStore,
typedef::{to_var_map, FunSignature, FuncArg, Type, TypeEnum, TypeVar, TypeVarId, Unifier}, typedef::{into_var_map, FunSignature, FuncArg, Type, TypeEnum, TypeVar, TypeVarId, Unifier},
}, },
}; };
@ -272,7 +272,7 @@ impl ConcreteTypeStore {
(*name, (self.to_unifier_type(unifier, primitives, cty.0, cache), cty.1)) (*name, (self.to_unifier_type(unifier, primitives, cty.0, cache), cty.1))
}) })
.collect::<HashMap<_, _>>(), .collect::<HashMap<_, _>>(),
params: to_var_map(params.iter().map(|(&id, cty)| { params: into_var_map(params.iter().map(|(&id, cty)| {
let ty = self.to_unifier_type(unifier, primitives, *cty, cache); let ty = self.to_unifier_type(unifier, primitives, *cty, cache);
TypeVar { id, ty } TypeVar { id, ty }
})), })),
@ -287,7 +287,7 @@ impl ConcreteTypeStore {
}) })
.collect(), .collect(),
ret: self.to_unifier_type(unifier, primitives, *ret, cache), ret: self.to_unifier_type(unifier, primitives, *ret, cache),
vars: to_var_map(vars.iter().map(|(&id, cty)| { vars: into_var_map(vars.iter().map(|(&id, cty)| {
let ty = self.to_unifier_type(unifier, primitives, *cty, cache); let ty = self.to_unifier_type(unifier, primitives, *cty, cache);
TypeVar { id, ty } TypeVar { id, ty }
})), })),

View File

@ -25,7 +25,7 @@ use crate::{
}, },
symbol_resolver::SymbolValue, symbol_resolver::SymbolValue,
toplevel::{helper::PrimDef, numpy::make_ndarray_ty}, toplevel::{helper::PrimDef, numpy::make_ndarray_ty},
typecheck::typedef::{iter_type_vars, to_var_map, TypeVar, VarMap}, typecheck::typedef::{iter_type_vars, into_var_map, TypeVar, VarMap},
}; };
use super::*; use super::*;
@ -373,7 +373,7 @@ impl<'a> BuiltinBuilder<'a> {
Some("N".into()), Some("N".into()),
None, None,
); );
let num_var_map = to_var_map([num_ty]); let num_var_map = into_var_map([num_ty]);
let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), None); let ndarray_float = make_ndarray_ty(unifier, primitives, Some(float), None);
let ndarray_float_2d = { let ndarray_float_2d = {
@ -390,11 +390,11 @@ impl<'a> BuiltinBuilder<'a> {
let ndarray_num_ty = make_ndarray_ty(unifier, primitives, Some(num_ty.ty), None); let ndarray_num_ty = make_ndarray_ty(unifier, primitives, Some(num_ty.ty), None);
let float_or_ndarray_ty = let float_or_ndarray_ty =
unifier.get_fresh_var_with_range(&[float, ndarray_float], Some("T".into()), None); unifier.get_fresh_var_with_range(&[float, ndarray_float], Some("T".into()), None);
let float_or_ndarray_var_map = to_var_map([float_or_ndarray_ty]); let float_or_ndarray_var_map = into_var_map([float_or_ndarray_ty]);
let num_or_ndarray_ty = let num_or_ndarray_ty =
unifier.get_fresh_var_with_range(&[num_ty.ty, ndarray_num_ty], Some("T".into()), None); unifier.get_fresh_var_with_range(&[num_ty.ty, ndarray_num_ty], Some("T".into()), None);
let num_or_ndarray_var_map = to_var_map([num_ty, num_or_ndarray_ty]); let num_or_ndarray_var_map = into_var_map([num_ty, num_or_ndarray_ty]);
let list_int32 = unifier.add_ty(TypeEnum::TList { ty: int32 }); let list_int32 = unifier.add_ty(TypeEnum::TList { ty: int32 });
@ -717,7 +717,7 @@ impl<'a> BuiltinBuilder<'a> {
default_value: None, default_value: None,
}], }],
ret: self.primitives.option, ret: self.primitives.option,
vars: to_var_map([self.option_tvar]), vars: into_var_map([self.option_tvar]),
})), })),
var_id: vec![self.option_tvar.id], var_id: vec![self.option_tvar.id],
instance_to_symbol: HashMap::default(), instance_to_symbol: HashMap::default(),
@ -892,7 +892,7 @@ impl<'a> BuiltinBuilder<'a> {
create_fn_by_codegen( create_fn_by_codegen(
self.unifier, self.unifier,
&to_var_map([common_ndim, p0_ty, ret_ty]), &into_var_map([common_ndim, p0_ty, ret_ty]),
prim.name(), prim.name(),
ret_ty.ty, ret_ty.ty,
&[(p0_ty.ty, "n")], &[(p0_ty.ty, "n")],
@ -956,7 +956,7 @@ impl<'a> BuiltinBuilder<'a> {
create_fn_by_codegen( create_fn_by_codegen(
self.unifier, self.unifier,
&to_var_map([common_ndim, p0_ty, ret_ty]), &into_var_map([common_ndim, p0_ty, ret_ty]),
prim.name(), prim.name(),
ret_ty.ty, ret_ty.ty,
&[(p0_ty.ty, "n")], &[(p0_ty.ty, "n")],
@ -1034,7 +1034,7 @@ impl<'a> BuiltinBuilder<'a> {
}, },
], ],
ret: ndarray, ret: ndarray,
vars: to_var_map([tv]), vars: into_var_map([tv]),
})), })),
var_id: vec![tv.id], var_id: vec![tv.id],
instance_to_symbol: HashMap::default(), instance_to_symbol: HashMap::default(),
@ -1054,7 +1054,7 @@ impl<'a> BuiltinBuilder<'a> {
create_fn_by_codegen( create_fn_by_codegen(
self.unifier, self.unifier,
&to_var_map([tv]), &into_var_map([tv]),
prim.name(), prim.name(),
self.primitives.ndarray, self.primitives.ndarray,
// We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a // We are using List[int32] here, as I don't know a way to specify an n-tuple bound on a
@ -1333,7 +1333,7 @@ impl<'a> BuiltinBuilder<'a> {
signature: self.unifier.add_ty(TypeEnum::TFunc(FunSignature { signature: self.unifier.add_ty(TypeEnum::TFunc(FunSignature {
args: vec![FuncArg { name: "ls".into(), ty: arg_ty.ty, default_value: None }], args: vec![FuncArg { name: "ls".into(), ty: arg_ty.ty, default_value: None }],
ret: int32, ret: int32,
vars: to_var_map([tvar, arg_ty]), vars: into_var_map([tvar, arg_ty]),
})), })),
var_id: Vec::default(), var_id: Vec::default(),
instance_to_symbol: HashMap::default(), instance_to_symbol: HashMap::default(),
@ -1516,7 +1516,7 @@ impl<'a> BuiltinBuilder<'a> {
.map(|p| FuncArg { name: p.1.into(), ty: p.0, default_value: None }) .map(|p| FuncArg { name: p.1.into(), ty: p.0, default_value: None })
.collect(), .collect(),
ret: ret_ty.ty, ret: ret_ty.ty,
vars: to_var_map([x1_ty, x2_ty, ret_ty]), vars: into_var_map([x1_ty, x2_ty, ret_ty]),
})), })),
var_id: vec![x1_ty.id, x2_ty.id], var_id: vec![x1_ty.id, x2_ty.id],
instance_to_symbol: HashMap::default(), instance_to_symbol: HashMap::default(),
@ -1745,7 +1745,7 @@ impl<'a> BuiltinBuilder<'a> {
.map(|p| FuncArg { name: p.1.into(), ty: p.0, default_value: None }) .map(|p| FuncArg { name: p.1.into(), ty: p.0, default_value: None })
.collect(), .collect(),
ret: ret_ty.ty, ret: ret_ty.ty,
vars: to_var_map([x1_ty, x2_ty, ret_ty]), vars: into_var_map([x1_ty, x2_ty, ret_ty]),
})), })),
var_id: vec![ret_ty.id], var_id: vec![ret_ty.id],
instance_to_symbol: HashMap::default(), instance_to_symbol: HashMap::default(),

View File

@ -2,7 +2,7 @@ use std::convert::TryInto;
use crate::symbol_resolver::SymbolValue; use crate::symbol_resolver::SymbolValue;
use crate::toplevel::numpy::unpack_ndarray_var_tys; use crate::toplevel::numpy::unpack_ndarray_var_tys;
use crate::typecheck::typedef::{to_var_map, Mapping, TypeVarId, VarMap}; use crate::typecheck::typedef::{into_var_map, Mapping, TypeVarId, VarMap};
use nac3parser::ast::{Constant, Location}; use nac3parser::ast::{Constant, Location};
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use strum_macros::EnumIter; use strum_macros::EnumIter;
@ -377,12 +377,12 @@ impl TopLevelComposer {
let is_some_type_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature { let is_some_type_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
args: vec![], args: vec![],
ret: bool, ret: bool,
vars: to_var_map([option_type_var]), vars: into_var_map([option_type_var]),
})); }));
let unwrap_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature { let unwrap_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
args: vec![], args: vec![],
ret: option_type_var.ty, ret: option_type_var.ty,
vars: to_var_map([option_type_var]), vars: into_var_map([option_type_var]),
})); }));
let option = unifier.add_ty(TypeEnum::TObj { let option = unifier.add_ty(TypeEnum::TObj {
obj_id: PrimDef::Option.id(), obj_id: PrimDef::Option.id(),
@ -393,7 +393,7 @@ impl TopLevelComposer {
] ]
.into_iter() .into_iter()
.collect::<HashMap<_, _>>(), .collect::<HashMap<_, _>>(),
params: to_var_map([option_type_var]), params: into_var_map([option_type_var]),
}); });
let size_t_ty = match size_t { let size_t_ty = match size_t {
@ -409,7 +409,7 @@ impl TopLevelComposer {
let ndarray_copy_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature { let ndarray_copy_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
args: vec![], args: vec![],
ret: ndarray_copy_fun_ret_ty.ty, ret: ndarray_copy_fun_ret_ty.ty,
vars: to_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]), vars: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
})); }));
let ndarray_fill_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature { let ndarray_fill_fun_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
args: vec![FuncArg { args: vec![FuncArg {
@ -418,7 +418,7 @@ impl TopLevelComposer {
default_value: None, default_value: None,
}], }],
ret: none, ret: none,
vars: to_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]), vars: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
})); }));
let ndarray = unifier.add_ty(TypeEnum::TObj { let ndarray = unifier.add_ty(TypeEnum::TObj {
obj_id: PrimDef::NDArray.id(), obj_id: PrimDef::NDArray.id(),
@ -426,7 +426,7 @@ impl TopLevelComposer {
(PrimDef::NDArrayCopy.simple_name().into(), (ndarray_copy_fun_ty, true)), (PrimDef::NDArrayCopy.simple_name().into(), (ndarray_copy_fun_ty, true)),
(PrimDef::NDArrayFill.simple_name().into(), (ndarray_fill_fun_ty, true)), (PrimDef::NDArrayFill.simple_name().into(), (ndarray_fill_fun_ty, true)),
]), ]),
params: to_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]), params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
}); });
unifier.unify(ndarray_copy_fun_ret_ty.ty, ndarray).unwrap(); unifier.unify(ndarray_copy_fun_ret_ty.ty, ndarray).unwrap();

View File

@ -143,7 +143,7 @@ impl TestEnvironment {
let ndarray = unifier.add_ty(TypeEnum::TObj { let ndarray = unifier.add_ty(TypeEnum::TObj {
obj_id: PrimDef::NDArray.id(), obj_id: PrimDef::NDArray.id(),
fields: HashMap::new(), fields: HashMap::new(),
params: to_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]), params: into_var_map([ndarray_dtype_tvar, ndarray_ndims_tvar]),
}); });
let primitives = PrimitiveStore { let primitives = PrimitiveStore {
int32, int32,
@ -323,7 +323,7 @@ impl TestEnvironment {
let foo_ty = unifier.add_ty(TypeEnum::TObj { let foo_ty = unifier.add_ty(TypeEnum::TObj {
obj_id: DefinitionId(defs + 1), obj_id: DefinitionId(defs + 1),
fields: [("a".into(), (tvar.ty, true))].iter().cloned().collect::<HashMap<_, _>>(), fields: [("a".into(), (tvar.ty, true))].iter().cloned().collect::<HashMap<_, _>>(),
params: to_var_map([tvar]), params: into_var_map([tvar]),
}); });
top_level_defs.push( top_level_defs.push(
RwLock::new(TopLevelDef::Class { RwLock::new(TopLevelDef::Class {
@ -345,7 +345,7 @@ impl TestEnvironment {
unifier.add_ty(TypeEnum::TFunc(FunSignature { unifier.add_ty(TypeEnum::TFunc(FunSignature {
args: vec![], args: vec![],
ret: foo_ty, ret: foo_ty,
vars: to_var_map([tvar]), vars: into_var_map([tvar]),
})), })),
); );

View File

@ -61,7 +61,7 @@ pub type VarMap = IndexMapping<TypeVarId>;
/// Build a [`VarMap`] from an iterator of [`TypeVar`] /// Build a [`VarMap`] from an iterator of [`TypeVar`]
/// ///
/// The resulting [`VarMap`] wil have the same order as the input iterator. /// The resulting [`VarMap`] wil have the same order as the input iterator.
pub fn to_var_map<I>(vars: I) -> VarMap pub fn into_var_map<I>(vars: I) -> VarMap
where where
I: IntoIterator<Item = TypeVar>, I: IntoIterator<Item = TypeVar>,
{ {

View File

@ -116,7 +116,7 @@ impl TestEnvironment {
unifier.add_ty(TypeEnum::TObj { unifier.add_ty(TypeEnum::TObj {
obj_id: DefinitionId(3), obj_id: DefinitionId(3),
fields: [("a".into(), (tvar.ty, true))].iter().cloned().collect::<HashMap<_, _>>(), fields: [("a".into(), (tvar.ty, true))].iter().cloned().collect::<HashMap<_, _>>(),
params: to_var_map([tvar]), params: into_var_map([tvar]),
}), }),
); );