summaryrefslogtreecommitdiff
path: root/dhall/src
diff options
context:
space:
mode:
authorNadrieril2020-03-18 21:37:14 +0000
committerNadrieril2020-03-31 21:44:01 +0100
commitfa89e9cb319b353332c9e835944e7f86a6604c42 (patch)
tree669c879e1231ce898dd83c786eb17976a2ba6171 /dhall/src
parent7848c8e8d3147ebe428290558aa6c0efcbf59ee5 (diff)
Move Value, SimpleValue and SimpleType to serde
Diffstat (limited to '')
-rw-r--r--dhall/src/lib.rs66
-rw-r--r--dhall/src/semantics/tck/typecheck.rs5
-rw-r--r--dhall/src/simple.rs163
3 files changed, 4 insertions, 230 deletions
diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs
index 34a9bac..0f4b623 100644
--- a/dhall/src/lib.rs
+++ b/dhall/src/lib.rs
@@ -9,7 +9,6 @@ mod tests;
pub mod error;
pub mod semantics;
-pub mod simple;
pub mod syntax;
use std::fmt::Display;
@@ -23,8 +22,6 @@ use crate::semantics::resolve::ImportLocation;
use crate::semantics::{typecheck, typecheck_with, Hir, Nir, Tir, Type};
use crate::syntax::Expr;
-pub use crate::simple::{STyKind, SValKind, SimpleType, SimpleValue};
-
#[derive(Debug, Clone)]
pub struct Parsed(Expr, ImportLocation);
@@ -47,16 +44,6 @@ pub struct Typed {
#[derive(Debug, Clone)]
pub struct Normalized(Nir);
-/// A Dhall value.
-#[derive(Debug, Clone)]
-pub struct Value {
- /// Invariant: in normal form
- pub hir: Hir,
- /// Cached conversions because they are annoying to construct from Hir.
- pub as_simple_val: Option<SimpleValue>,
- pub as_simple_ty: Option<SimpleType>,
-}
-
/// Controls conversion from `Nir` to `Expr`
#[derive(Copy, Clone, Default)]
pub struct ToExprOptions {
@@ -131,14 +118,6 @@ impl Typed {
}
impl Normalized {
- pub fn to_value(&self) -> Value {
- Value {
- hir: self.to_hir(),
- as_simple_val: SimpleValue::from_nir(&self.0),
- as_simple_ty: SimpleType::from_nir(&self.0),
- }
- }
-
/// Converts a value back to the corresponding AST expression.
pub fn to_expr(&self) -> Expr {
self.0.to_expr(ToExprOptions::default())
@@ -147,42 +126,15 @@ impl Normalized {
pub fn to_hir(&self) -> Hir {
self.0.to_hir_noenv()
}
+ pub fn as_nir(&self) -> &Nir {
+ &self.0
+ }
/// Converts a value back to the corresponding AST expression, alpha-normalizing in the process.
pub fn to_expr_alpha(&self) -> Expr {
self.0.to_expr(ToExprOptions { alpha: true })
}
}
-impl Value {
- /// Parse a string into a Value, and optionally ensure that the value matches the provided type
- /// annotation.
- pub fn from_str_with_annot(
- s: &str,
- ty: Option<&Self>,
- ) -> Result<Self, Error> {
- let resolved = Parsed::parse_str(s)?.resolve()?;
- let typed = match ty {
- None => resolved.typecheck()?,
- Some(ty) => resolved.typecheck_with(&ty.hir)?,
- };
- Ok(typed.normalize().to_value())
- }
-
- /// Converts a Value into a SimpleValue.
- pub fn to_simple_value(&self) -> Option<SimpleValue> {
- self.as_simple_val.clone()
- }
- /// Converts a Value into a SimpleType.
- pub fn to_simple_type(&self) -> Option<SimpleType> {
- self.as_simple_ty.clone()
- }
-
- /// Converts a value back to the corresponding AST expression.
- pub fn to_expr(&self) -> Expr {
- self.hir.to_expr(ToExprOptions::default())
- }
-}
-
macro_rules! derive_traits_for_wrapper_struct {
($ty:ident) => {
impl std::cmp::PartialEq for $ty {
@@ -246,15 +198,3 @@ impl Display for Normalized {
self.to_expr().fmt(f)
}
}
-
-impl Eq for Value {}
-impl PartialEq for Value {
- fn eq(&self, other: &Self) -> bool {
- self.hir == other.hir
- }
-}
-impl Display for Value {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
- self.to_expr().fmt(f)
- }
-}
diff --git a/dhall/src/semantics/tck/typecheck.rs b/dhall/src/semantics/tck/typecheck.rs
index 205185f..c3334b5 100644
--- a/dhall/src/semantics/tck/typecheck.rs
+++ b/dhall/src/semantics/tck/typecheck.rs
@@ -57,10 +57,7 @@ pub fn mkerr<T, S: ToString>(msg: S) -> Result<T, TypeError> {
Err(TypeError::new(TypeMessage::Custom(msg.to_string())))
}
-pub fn mk_span_err<T, S: ToString>(
- span: Span,
- msg: S,
-) -> Result<T, TypeError> {
+pub fn mk_span_err<T, S: ToString>(span: Span, msg: S) -> Result<T, TypeError> {
mkerr(
ErrorBuilder::new(msg.to_string())
.span_err(span, msg.to_string())
diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs
deleted file mode 100644
index c00380c..0000000
--- a/dhall/src/simple.rs
+++ /dev/null
@@ -1,163 +0,0 @@
-use std::collections::BTreeMap;
-
-use crate::semantics::{Hir, HirKind, Nir, NirKind};
-use crate::syntax::{Builtin, ExprKind, NumKind, Span};
-use crate::Value;
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct SimpleValue {
- kind: Box<SValKind>,
-}
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum SValKind {
- Num(NumKind),
- Text(String),
- Optional(Option<SimpleValue>),
- List(Vec<SimpleValue>),
- Record(BTreeMap<String, SimpleValue>),
- Union(String, Option<SimpleValue>),
-}
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct SimpleType {
- kind: Box<STyKind>,
-}
-
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum STyKind {
- Bool,
- Natural,
- Integer,
- Double,
- Text,
- Optional(SimpleType),
- List(SimpleType),
- Record(BTreeMap<String, SimpleType>),
- Union(BTreeMap<String, Option<SimpleType>>),
-}
-
-impl SimpleValue {
- pub fn new(kind: SValKind) -> Self {
- SimpleValue {
- kind: Box::new(kind),
- }
- }
- pub fn from_nir(nir: &Nir) -> Option<Self> {
- 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::<Option<_>>()?,
- ),
- NirKind::RecordLit(kvs) => SValKind::Record(
- kvs.iter()
- .map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
- .collect::<Option<_>>()?,
- ),
- 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,
- }))
- }
-
- pub fn kind(&self) -> &SValKind {
- self.kind.as_ref()
- }
-}
-
-impl SimpleType {
- pub fn new(kind: STyKind) -> Self {
- SimpleType {
- kind: Box::new(kind),
- }
- }
- pub fn from_nir(nir: &Nir) -> Option<Self> {
- Some(SimpleType::new(match nir.kind() {
- NirKind::BuiltinType(b) => match b {
- Builtin::Bool => STyKind::Bool,
- Builtin::Natural => STyKind::Natural,
- Builtin::Integer => STyKind::Integer,
- Builtin::Double => STyKind::Double,
- Builtin::Text => STyKind::Text,
- _ => unreachable!(),
- },
- NirKind::OptionalType(t) => STyKind::Optional(Self::from_nir(t)?),
- NirKind::ListType(t) => STyKind::List(Self::from_nir(t)?),
- NirKind::RecordType(kts) => STyKind::Record(
- kts.iter()
- .map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
- .collect::<Option<_>>()?,
- ),
- NirKind::UnionType(kts) => STyKind::Union(
- kts.iter()
- .map(|(k, v)| {
- Some((
- k.into(),
- v.as_ref()
- .map(|v| Ok(Self::from_nir(v)?))
- .transpose()?,
- ))
- })
- .collect::<Option<_>>()?,
- ),
- _ => return None,
- }))
- }
-
- pub fn kind(&self) -> &STyKind {
- self.kind.as_ref()
- }
- pub fn to_value(&self) -> Value {
- Value {
- hir: self.to_hir(),
- as_simple_val: None,
- as_simple_ty: Some(self.clone()),
- }
- }
- pub fn to_hir(&self) -> Hir {
- let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial);
- hir(match self.kind() {
- STyKind::Bool => ExprKind::Builtin(Builtin::Bool),
- STyKind::Natural => ExprKind::Builtin(Builtin::Natural),
- STyKind::Integer => ExprKind::Builtin(Builtin::Integer),
- STyKind::Double => ExprKind::Builtin(Builtin::Double),
- STyKind::Text => ExprKind::Builtin(Builtin::Text),
- STyKind::Optional(t) => ExprKind::App(
- hir(ExprKind::Builtin(Builtin::Optional)),
- t.to_hir(),
- ),
- STyKind::List(t) => {
- ExprKind::App(hir(ExprKind::Builtin(Builtin::List)), t.to_hir())
- }
- STyKind::Record(kts) => ExprKind::RecordType(
- kts.into_iter()
- .map(|(k, t)| (k.as_str().into(), t.to_hir()))
- .collect(),
- ),
- STyKind::Union(kts) => ExprKind::UnionType(
- kts.into_iter()
- .map(|(k, t)| {
- (k.as_str().into(), t.as_ref().map(|t| t.to_hir()))
- })
- .collect(),
- ),
- })
- }
-}