feat: bidi for containers

This commit is contained in:
Shunsuke Shibayama 2023-09-05 17:21:59 +09:00
parent 0f430199ab
commit 712d4e2b73
3 changed files with 138 additions and 20 deletions

View file

@ -1790,6 +1790,38 @@ impl Context {
self.convert_tp_into_value(fv.crack().clone())
}
TyParam::Value(v) => Ok(v),
TyParam::Array(arr) => {
let mut new = vec![];
for elem in arr {
let elem = self.convert_tp_into_value(elem)?;
new.push(elem);
}
Ok(ValueObj::Array(new.into()))
}
TyParam::Tuple(tys) => {
let mut new = vec![];
for elem in tys {
let elem = self.convert_tp_into_value(elem)?;
new.push(elem);
}
Ok(ValueObj::Tuple(new.into()))
}
TyParam::Record(rec) => {
let mut new = dict! {};
for (name, elem) in rec {
let elem = self.convert_tp_into_value(elem)?;
new.insert(name, elem);
}
Ok(ValueObj::Record(new))
}
TyParam::Set(set) => {
let mut new = set! {};
for elem in set {
let elem = self.convert_tp_into_value(elem)?;
new.insert(elem);
}
Ok(ValueObj::Set(new))
}
other => self.convert_tp_into_type(other).map(ValueObj::builtin_type),
}
}
@ -1931,12 +1963,12 @@ impl Context {
}
}
fn _convert_type_to_dict_type(&self, ty: Type) -> Result<Dict<Type, Type>, ()> {
pub(crate) fn convert_type_to_dict_type(&self, ty: Type) -> Result<Dict<Type, Type>, ()> {
match ty {
Type::FreeVar(fv) if fv.is_linked() => {
self._convert_type_to_dict_type(fv.crack().clone())
self.convert_type_to_dict_type(fv.crack().clone())
}
Type::Refinement(refine) => self._convert_type_to_dict_type(*refine.t),
Type::Refinement(refine) => self.convert_type_to_dict_type(*refine.t),
Type::Poly { name, params } if &name[..] == "Dict" => {
let dict = Dict::try_from(params[0].clone())?;
let mut new_dict = dict! {};
@ -1951,6 +1983,25 @@ impl Context {
}
}
pub(crate) fn convert_type_to_tuple_type(&self, ty: Type) -> Result<Vec<Type>, ()> {
match ty {
Type::FreeVar(fv) if fv.is_linked() => {
self.convert_type_to_tuple_type(fv.crack().clone())
}
Type::Refinement(refine) => self.convert_type_to_tuple_type(*refine.t),
Type::Poly { name, params } if &name[..] == "Tuple" => {
let tps = Vec::try_from(params[0].clone())?;
let mut tys = vec![];
for elem in tps.into_iter() {
let elem = self.convert_tp_into_type(elem).map_err(|_| ())?;
tys.push(elem);
}
Ok(tys)
}
_ => Err(()),
}
}
pub(crate) fn convert_type_to_array(&self, ty: Type) -> Result<Vec<ValueObj>, Type> {
match ty {
Type::FreeVar(fv) if fv.is_linked() => self.convert_type_to_array(fv.crack().clone()),