From b3f00a827bcdd0fe406ccf8913cc5fb7cd6e0f2f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 7 May 2019 18:38:54 +0200 Subject: Promote shift and subst_shift to traits --- dhall/src/phase/mod.rs | 69 +++++++++++++++++++++++++------------------- dhall/src/phase/normalize.rs | 1 + dhall/src/phase/typecheck.rs | 1 + 3 files changed, 42 insertions(+), 29 deletions(-) (limited to 'dhall/src/phase') diff --git a/dhall/src/phase/mod.rs b/dhall/src/phase/mod.rs index 97b8083..5c910bc 100644 --- a/dhall/src/phase/mod.rs +++ b/dhall/src/phase/mod.rs @@ -7,7 +7,7 @@ use dhall_syntax::{Const, Import, Span, SubExpr, X}; use crate::core::context::TypecheckContext; use crate::core::thunk::Thunk; use crate::core::value::Value; -use crate::core::var::AlphaVar; +use crate::core::var::{AlphaVar, Shift, Subst}; use crate::error::{Error, ImportError, TypeError, TypeMessage}; use resolve::ImportRoot; @@ -168,26 +168,6 @@ impl Typed { Typed::Const(c) => Ok(Cow::Owned(type_of_const(*c)?)), } } - - pub(crate) fn shift(&self, delta: isize, var: &AlphaVar) -> Self { - match self { - Typed::Value(th, t) => Typed::Value( - th.shift(delta, var), - t.as_ref().map(|x| x.shift(delta, var)), - ), - Typed::Const(c) => Typed::Const(*c), - } - } - - pub(crate) fn subst_shift(&self, var: &AlphaVar, val: &Typed) -> Self { - match self { - Typed::Value(th, t) => Typed::Value( - th.subst_shift(var, val), - t.as_ref().map(|x| x.subst_shift(var, val)), - ), - Typed::Const(c) => Typed::Const(*c), - } - } } impl Type { @@ -226,13 +206,6 @@ impl Type { pub(crate) fn from_const(c: Const) -> Self { Type(Box::new(Typed::from_const(c))) } - - pub(crate) fn shift(&self, delta: isize, var: &AlphaVar) -> Self { - Type(Box::new(self.0.shift(delta, var))) - } - pub(crate) fn subst_shift(&self, var: &AlphaVar, val: &Typed) -> Self { - Type(Box::new(self.0.subst_shift(var, val))) - } } impl Normalized { @@ -259,12 +232,50 @@ impl Normalized { pub(crate) fn get_type(&self) -> Result, TypeError> { self.0.get_type() } +} + +impl Shift for Typed { + fn shift(&self, delta: isize, var: &AlphaVar) -> Self { + match self { + Typed::Value(th, t) => Typed::Value( + th.shift(delta, var), + t.as_ref().map(|x| x.shift(delta, var)), + ), + Typed::Const(c) => Typed::Const(*c), + } + } +} + +impl Shift for Type { + fn shift(&self, delta: isize, var: &AlphaVar) -> Self { + Type(Box::new(self.0.shift(delta, var))) + } +} - pub(crate) fn shift(&self, delta: isize, var: &AlphaVar) -> Self { +impl Shift for Normalized { + fn shift(&self, delta: isize, var: &AlphaVar) -> Self { Normalized(self.0.shift(delta, var)) } } +impl Subst for Typed { + fn subst_shift(&self, var: &AlphaVar, val: &Typed) -> Self { + match self { + Typed::Value(th, t) => Typed::Value( + th.subst_shift(var, val), + t.as_ref().map(|x| x.subst_shift(var, val)), + ), + Typed::Const(c) => Typed::Const(*c), + } + } +} + +impl Subst for Type { + fn subst_shift(&self, var: &AlphaVar, val: &Typed) -> Self { + Type(Box::new(self.0.subst_shift(var, val))) + } +} + macro_rules! derive_traits_for_wrapper_struct { ($ty:ident) => { impl std::cmp::PartialEq for $ty { diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index 5dfcfb6..e9fc570 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -5,6 +5,7 @@ use dhall_syntax::{BinOp, Builtin, ExprF, InterpolatedTextContents, Label, X}; use crate::core::context::NormalizationContext; use crate::core::thunk::{Thunk, TypeThunk}; use crate::core::value::Value; +use crate::core::var::Subst; use crate::phase::{NormalizedSubExpr, ResolvedSubExpr, Typed}; pub(crate) type InputSubExpr = ResolvedSubExpr; diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs index 32c1531..f8ee559 100644 --- a/dhall/src/phase/typecheck.rs +++ b/dhall/src/phase/typecheck.rs @@ -11,6 +11,7 @@ use dhall_syntax::{ use crate::core::context::{NormalizationContext, TypecheckContext}; use crate::core::thunk::{Thunk, TypeThunk}; use crate::core::value::Value; +use crate::core::var::Subst; use crate::error::{TypeError, TypeMessage}; use crate::phase::{Normalized, Resolved, Type, Typed}; -- cgit v1.2.3