From a4ef23fd3e7a053def648dca05dfc9a043af9860 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 11 Apr 2019 19:04:19 +0200 Subject: Respect import boundaries Closes #54 --- dhall_core/src/core.rs | 66 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 20 deletions(-) (limited to 'dhall_core/src') diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 1c41acc..d2ecf53 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -324,29 +324,18 @@ impl Expr { } } -impl Expr> { - pub fn squash_embed(&self) -> Expr { - match self { - ExprF::Embed(e) => e.clone(), - e => e.map_shallow( - >>::squash_embed, - S::clone, - |_| unreachable!(), - Label::clone, - ), - } - } -} - -impl Expr> { - pub fn squash_embed(&self) -> SubExpr { +impl Expr { + pub fn squash_embed( + &self, + f: &impl Fn(&E) -> SubExpr, + ) -> SubExpr { match self.as_ref() { - ExprF::Embed(e) => e.clone(), + ExprF::Embed(e) => f(e), e => e .map( - |e| e.as_ref().squash_embed(), - |_, e| e.as_ref().squash_embed(), - S::clone, + |e| e.as_ref().squash_embed(f), + |_, e| e.as_ref().squash_embed(f), + N::clone, |_| unreachable!(), Label::clone, ) @@ -675,6 +664,43 @@ impl SubExpr { } } +impl SubExpr { + pub fn absurd(&self) -> SubExpr { + rc(self.as_ref().absurd_rec()) + } +} + +impl Expr { + // This is all very sad and I hope this can be avoided sometime + pub fn absurd_rec(&self) -> Expr { + self.map_ref( + |e| e.absurd(), + |_, e| e.absurd(), + |_| unreachable!(), + |_| unreachable!(), + Label::clone, + ) + } +} + +impl ExprF +where + SE: Clone, + L: Clone + Ord, + N: Clone, +{ + // When we know there is no Embed + pub fn absurd(&self) -> ExprF { + self.map_ref( + |e| e.clone(), + |_, e| e.clone(), + N::clone, + |_| unreachable!(), + L::clone, + ) + } +} + impl Clone for SubExpr { fn clone(&self) -> Self { SubExpr(Rc::clone(&self.0)) -- cgit v1.2.3