summaryrefslogtreecommitdiff
path: root/dhall/src/phase
diff options
context:
space:
mode:
authorNadrieril2019-05-07 18:38:54 +0200
committerNadrieril2019-05-07 18:38:54 +0200
commitb3f00a827bcdd0fe406ccf8913cc5fb7cd6e0f2f (patch)
tree18d8326069fe94bcb03177c3375fcf453a370759 /dhall/src/phase
parent8cb3046e0920bf24d66c578b1a2b184c741b73fe (diff)
Promote shift and subst_shift to traits
Diffstat (limited to '')
-rw-r--r--dhall/src/phase/mod.rs69
-rw-r--r--dhall/src/phase/normalize.rs1
-rw-r--r--dhall/src/phase/typecheck.rs1
3 files changed, 42 insertions, 29 deletions
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<Cow<'_, Type>, 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<Typed> 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<Typed> 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};