From 7901220c7cc18b740ba2b1d7f786ebc1d97167db Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Mar 2020 22:02:43 +0000 Subject: Start overengineering SimpleVal/Ty --- dhall/src/simple.rs | 151 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 120 insertions(+), 31 deletions(-) (limited to 'dhall') diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs index 45fa656..6f44183 100644 --- a/dhall/src/simple.rs +++ b/dhall/src/simple.rs @@ -37,6 +37,125 @@ pub enum STyKind { Union(BTreeMap>), } +pub trait SimpleValueFolder: Sized { + fn num(x: NumKind) -> Self; + fn text(x: String) -> Self; + fn optional(x: Option) -> Self; + fn list(x: impl Iterator) -> Self; + fn record(values: impl Iterator) -> Self; + fn union(variant: String, content: Option) -> Self; +} + +pub trait SimpleTypeFolder: Sized { + fn bool() -> Self; + fn natural() -> Self; + fn integer() -> Self; + fn double() -> Self; + fn text() -> Self; + fn optional(x: Self) -> Self; + fn list(x: Self) -> Self; + fn record(types: impl Iterator) -> Self; + fn union(types: impl Iterator)>) -> Self; +} + +pub(crate) fn fold_simple_value(nir: &Nir) -> Option +where + T: SimpleValueFolder, +{ + // TODO: check the type first and remove error handling + Some(match nir.kind() { + NirKind::Num(lit) => T::num(lit.clone()), + NirKind::TextLit(x) => T::text( + x.as_text() + .expect("Normal form should ensure the text is a string"), + ), + NirKind::EmptyOptionalLit(_) => T::optional(None), + NirKind::NEOptionalLit(x) => T::optional(Some(fold_simple_value(x)?)), + NirKind::EmptyListLit(_) => T::list(std::iter::empty()), + NirKind::NEListLit(xs) => T::list( + xs.iter() + .map(|v| fold_simple_value(v)) + .collect::>>()? + .into_iter(), + ), + NirKind::RecordLit(kvs) => T::record( + kvs.iter() + .map(|(k, v)| Some((k.into(), fold_simple_value(v)?))) + .collect::>>()? + .into_iter(), + ), + NirKind::UnionLit(field, x, _) => { + T::union(field.into(), Some(fold_simple_value(x)?)) + } + NirKind::UnionConstructor(field, ty) + if ty.get(field).map(|f| f.is_some()) == Some(false) => + { + T::union(field.into(), None) + } + _ => return None, + }) +} + +pub(crate) fn fold_simple_type(nir: &Nir) -> Option +where + T: SimpleTypeFolder, +{ + // TODO: avoid unnecessary allocations + Some(match nir.kind() { + NirKind::BuiltinType(b) => match b { + Builtin::Bool => T::bool(), + Builtin::Natural => T::natural(), + Builtin::Integer => T::integer(), + Builtin::Double => T::double(), + Builtin::Text => T::text(), + _ => unreachable!(), + }, + NirKind::OptionalType(t) => T::optional(fold_simple_type(t)?), + NirKind::ListType(t) => T::list(fold_simple_type(t)?), + NirKind::RecordType(kts) => T::record( + kts.iter() + .map(|(k, v)| Some((k.into(), fold_simple_type(v)?))) + .collect::>>()? + .into_iter(), + ), + NirKind::UnionType(kts) => T::union( + kts.iter() + .map(|(k, v)| { + Some(( + k.into(), + v.as_ref() + .map(|v| Ok(fold_simple_type(v)?)) + .transpose()?, + )) + }) + .collect::>>()? + .into_iter(), + ), + _ => return None, + }) +} + +impl SimpleValueFolder for SimpleValue { + fn num(x: NumKind) -> Self { + SimpleValue::new(SValKind::Num(x)) + } + fn text(x: String) -> Self { + SimpleValue::new(SValKind::Text(x)) + } + fn optional(x: Option) -> Self { + SimpleValue::new(SValKind::Optional(x)) + } + fn list(xs: impl Iterator) -> Self { + SimpleValue::new(SValKind::List(xs.collect())) + } + fn record(values: impl Iterator) -> Self { + SimpleValue::new(SValKind::Record(values.collect())) + } + fn union(variant: String, content: Option) -> Self { + SimpleValue::new(SValKind::Union(variant, content)) + } +} + impl SimpleValue { pub(crate) fn new(kind: SValKind) -> Self { SimpleValue { @@ -44,37 +163,7 @@ impl SimpleValue { } } pub(crate) fn from_nir(nir: &Nir) -> Option { - Some(SimpleValue::new(match nir.kind() { - NirKind::Num(lit) => SValKind::Num(lit.clone()), - NirKind::TextLit(x) => SValKind::Text( - x.as_text() - .expect("Normal form should ensure the text is a string"), - ), - NirKind::EmptyOptionalLit(_) => SValKind::Optional(None), - NirKind::NEOptionalLit(x) => { - SValKind::Optional(Some(Self::from_nir(x)?)) - } - NirKind::EmptyListLit(_) => SValKind::List(vec![]), - NirKind::NEListLit(xs) => SValKind::List( - xs.iter() - .map(|v| Self::from_nir(v)) - .collect::>()?, - ), - NirKind::RecordLit(kvs) => SValKind::Record( - kvs.iter() - .map(|(k, v)| Some((k.into(), Self::from_nir(v)?))) - .collect::>()?, - ), - NirKind::UnionLit(field, x, _) => { - SValKind::Union(field.into(), Some(Self::from_nir(x)?)) - } - NirKind::UnionConstructor(field, ty) - if ty.get(field).map(|f| f.is_some()) == Some(false) => - { - SValKind::Union(field.into(), None) - } - _ => return None, - })) + fold_simple_value::(nir) } pub fn kind(&self) -> &SValKind { -- cgit v1.2.3