summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNadrieril2020-03-13 16:39:45 +0000
committerNadrieril2020-03-31 21:44:01 +0100
commitf175ea7860cc1dd614b17912140ab7f5400c6bff (patch)
tree7a8a4e8b15d7d51fced0b47ac0c3ff6cb75c5058
parent6fdae6f8026fe9357de63c5557676f4def3eaf92 (diff)
Limit dependency on Normalized
-rw-r--r--dhall/src/lib.rs76
-rw-r--r--dhall/src/simple.rs50
2 files changed, 42 insertions, 84 deletions
diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs
index 7300d88..0e2ec82 100644
--- a/dhall/src/lib.rs
+++ b/dhall/src/lib.rs
@@ -16,15 +16,12 @@ use std::fmt::Display;
use std::path::Path;
use url::Url;
-use crate::error::{EncodeError, Error, TypeError};
+use crate::error::{Error, TypeError};
use crate::semantics::parse;
use crate::semantics::resolve;
use crate::semantics::resolve::ImportLocation;
-use crate::semantics::{
- typecheck, typecheck_with, Hir, Nir, NirKind, Tir, Type,
-};
-use crate::syntax::binary;
-use crate::syntax::{Builtin, Expr};
+use crate::semantics::{typecheck, typecheck_with, Hir, Nir, Tir, Type};
+use crate::syntax::Expr;
pub(crate) type ParsedExpr = Expr;
pub(crate) type DecodedExpr = Expr;
@@ -57,8 +54,9 @@ pub(crate) struct Normalized(Nir);
pub struct Value {
/// Invariant: in normal form
pub(crate) hir: Hir,
- simple_val: Option<SimpleValue>,
- simple_ty: Option<SimpleType>,
+ /// Cached conversions because they are annoying to construct from Hir.
+ pub(crate) as_simple_val: Option<SimpleValue>,
+ pub(crate) as_simple_ty: Option<SimpleType>,
}
/// Controls conversion from `Nir` to `Expr`
@@ -135,14 +133,11 @@ impl Typed {
}
impl Normalized {
- pub fn encode(&self) -> Result<Vec<u8>, EncodeError> {
- binary::encode(&self.to_expr())
- }
pub fn to_value(&self) -> Value {
Value {
hir: self.to_hir(),
- simple_val: self.0.to_simple_value(),
- simple_ty: self.0.to_simple_type(),
+ as_simple_val: self.0.to_simple_value(),
+ as_simple_ty: self.0.to_simple_type(),
}
}
@@ -158,46 +153,6 @@ impl Normalized {
pub(crate) fn to_expr_alpha(&self) -> NormalizedExpr {
self.0.to_expr(ToExprOptions { alpha: true })
}
- pub(crate) fn to_nir(&self) -> Nir {
- self.0.clone()
- }
- pub(crate) fn into_nir(self) -> Nir {
- self.0
- }
-
- pub(crate) fn from_kind(v: NirKind) -> Self {
- Normalized(Nir::from_kind(v))
- }
- pub(crate) fn from_nir(th: Nir) -> Self {
- Normalized(th)
- }
-
- pub(crate) fn make_builtin_type(b: Builtin) -> Self {
- Normalized::from_nir(Nir::from_builtin(b))
- }
- pub(crate) fn make_optional_type(t: Normalized) -> Self {
- Normalized::from_nir(
- Nir::from_builtin(Builtin::Optional).app(t.to_nir()),
- )
- }
- pub(crate) fn make_list_type(t: Normalized) -> Self {
- Normalized::from_nir(Nir::from_builtin(Builtin::List).app(t.to_nir()))
- }
- pub(crate) fn make_record_type(
- kts: impl Iterator<Item = (String, Normalized)>,
- ) -> Self {
- Normalized::from_kind(NirKind::RecordType(
- kts.map(|(k, t)| (k.into(), t.into_nir())).collect(),
- ))
- }
- pub(crate) fn make_union_type(
- kts: impl Iterator<Item = (String, Option<Normalized>)>,
- ) -> Self {
- Normalized::from_kind(NirKind::UnionType(
- kts.map(|(k, t)| (k.into(), t.map(|t| t.into_nir())))
- .collect(),
- ))
- }
}
impl Value {
@@ -217,11 +172,11 @@ impl Value {
/// Converts a Value into a SimpleValue.
pub fn to_simple_value(&self) -> Option<SimpleValue> {
- self.simple_val.clone()
+ self.as_simple_val.clone()
}
/// Converts a Value into a SimpleType.
pub fn to_simple_type(&self) -> Option<SimpleType> {
- self.simple_ty.clone()
+ self.as_simple_ty.clone()
}
/// Converts a value back to the corresponding AST expression.
@@ -253,17 +208,6 @@ macro_rules! derive_traits_for_wrapper_struct {
derive_traits_for_wrapper_struct!(Parsed);
-impl std::hash::Hash for Normalized {
- fn hash<H>(&self, state: &mut H)
- where
- H: std::hash::Hasher,
- {
- if let Ok(vec) = self.encode() {
- vec.hash(state)
- }
- }
-}
-
impl From<Parsed> for NormalizedExpr {
fn from(other: Parsed) -> Self {
other.to_expr()
diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs
index 014053e..7e7e459 100644
--- a/dhall/src/simple.rs
+++ b/dhall/src/simple.rs
@@ -1,7 +1,8 @@
use std::collections::BTreeMap;
-use crate::syntax::{Builtin, LitKind};
-use crate::{Normalized, Value};
+use crate::semantics::{Hir, HirKind};
+use crate::syntax::{Builtin, ExprKind, LitKind, Span};
+use crate::Value;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleValue {
@@ -57,26 +58,39 @@ impl SimpleType {
self.kind.as_ref()
}
pub fn to_value(&self) -> Value {
- self.clone().into_normalized().to_value()
+ Value {
+ hir: self.to_hir(),
+ as_simple_val: None,
+ as_simple_ty: Some(self.clone()),
+ }
}
- fn into_normalized(self) -> Normalized {
- match *self.kind {
- STyKind::Bool => Normalized::make_builtin_type(Builtin::Bool),
- STyKind::Natural => Normalized::make_builtin_type(Builtin::Natural),
- STyKind::Integer => Normalized::make_builtin_type(Builtin::Integer),
- STyKind::Double => Normalized::make_builtin_type(Builtin::Double),
- STyKind::Text => Normalized::make_builtin_type(Builtin::Text),
- STyKind::Optional(t) => {
- Normalized::make_optional_type(t.into_normalized())
+ pub(crate) 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::List(t) => Normalized::make_list_type(t.into_normalized()),
- STyKind::Record(kts) => Normalized::make_record_type(
- kts.into_iter().map(|(k, t)| (k, t.into_normalized())),
+ STyKind::Record(kts) => ExprKind::RecordType(
+ kts.into_iter()
+ .map(|(k, t)| (k.as_str().into(), t.to_hir()))
+ .collect(),
),
- STyKind::Union(kts) => Normalized::make_union_type(
+ STyKind::Union(kts) => ExprKind::UnionType(
kts.into_iter()
- .map(|(k, t)| (k, t.map(|t| t.into_normalized()))),
+ .map(|(k, t)| {
+ (k.as_str().into(), t.as_ref().map(|t| t.to_hir()))
+ })
+ .collect(),
),
- }
+ })
}
}