From 55b5be3407a8528bc47482a591b168a7cb0ce91e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 6 May 2019 23:52:15 +0200 Subject: Move main datatypes into their own modules --- dhall/src/api/traits/static_type.rs | 2 +- dhall/src/core/context.rs | 136 +++++ dhall/src/core/mod.rs | 3 + dhall/src/core/thunk.rs | 295 +++++++++++ dhall/src/core/value.rs | 616 ++++++++++++++++++++++ dhall/src/error/mod.rs | 2 +- dhall/src/lib.rs | 8 +- dhall/src/phase/mod.rs | 10 +- dhall/src/phase/normalize.rs | 999 +----------------------------------- dhall/src/phase/typecheck.rs | 345 +++++-------- 10 files changed, 1217 insertions(+), 1199 deletions(-) create mode 100644 dhall/src/core/context.rs create mode 100644 dhall/src/core/mod.rs create mode 100644 dhall/src/core/thunk.rs create mode 100644 dhall/src/core/value.rs (limited to 'dhall') diff --git a/dhall/src/api/traits/static_type.rs b/dhall/src/api/traits/static_type.rs index 8b141a0..e05dfff 100644 --- a/dhall/src/api/traits/static_type.rs +++ b/dhall/src/api/traits/static_type.rs @@ -39,7 +39,7 @@ fn mktype(x: SubExpr) -> SimpleType { impl StaticType for T { fn get_static_type() -> Type { crate::phase::Normalized::from_thunk_and_type( - crate::phase::normalize::Thunk::from_normalized_expr( + crate::core::thunk::Thunk::from_normalized_expr( T::get_simple_static_type().into(), ), Type::const_type(), diff --git a/dhall/src/core/context.rs b/dhall/src/core/context.rs new file mode 100644 index 0000000..9b5beed --- /dev/null +++ b/dhall/src/core/context.rs @@ -0,0 +1,136 @@ +use std::borrow::Cow; +use std::rc::Rc; + +use dhall_syntax::context::Context; +use dhall_syntax::{Label, V}; + +use crate::core::thunk::Thunk; +use crate::core::value::{AlphaVar, Value}; +use crate::phase::{Normalized, Type, Typed}; + +#[derive(Debug, Clone)] +enum NzEnvItem { + Thunk(Thunk), + Skip(AlphaVar), +} + +#[derive(Debug, Clone)] +pub(crate) struct NormalizationContext(Rc>); + +#[derive(Debug, Clone)] +pub(crate) enum TyEnvItem { + Type(AlphaVar, Type), + Value(Normalized), +} + +#[derive(Debug, Clone)] +pub(crate) struct TypecheckContext(pub(crate) Context); + +impl NzEnvItem { + pub(crate) fn shift(&self, delta: isize, var: &AlphaVar) -> Self { + use NzEnvItem::*; + match self { + Thunk(e) => Thunk(e.shift(delta, var)), + Skip(v) => Skip(v.shift(delta, var)), + } + } + + pub(crate) fn subst_shift(&self, var: &AlphaVar, val: &Typed) -> Self { + match self { + NzEnvItem::Thunk(e) => NzEnvItem::Thunk(e.subst_shift(var, val)), + NzEnvItem::Skip(v) if v == var => NzEnvItem::Thunk(val.to_thunk()), + NzEnvItem::Skip(v) => NzEnvItem::Skip(v.shift(-1, var)), + } + } +} + +impl NormalizationContext { + pub(crate) fn new() -> Self { + NormalizationContext(Rc::new(Context::new())) + } + pub(crate) fn skip(&self, x: &Label) -> Self { + NormalizationContext(Rc::new( + self.0 + .map(|_, e| e.shift(1, &x.into())) + .insert(x.clone(), NzEnvItem::Skip(x.into())), + )) + } + pub(crate) fn lookup(&self, var: &V