From 20f01b79378a41c6e063d33c584d04c756419a26 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 21 Apr 2019 19:38:09 +0200 Subject: Factor out context handling --- dhall/src/traits/dynamic_type.rs | 11 ++++++----- dhall/src/typecheck.rs | 42 ++++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 19 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/traits/dynamic_type.rs b/dhall/src/traits/dynamic_type.rs index d4faf45..479bed6 100644 --- a/dhall/src/traits/dynamic_type.rs +++ b/dhall/src/traits/dynamic_type.rs @@ -1,7 +1,8 @@ use crate::expr::*; use crate::traits::StaticType; -use crate::typecheck::{type_of_const, TypeError, TypeMessage}; -use dhall_core::context::Context; +use crate::typecheck::{ + type_of_const, TypeError, TypeMessage, TypecheckContext, +}; use dhall_core::{Const, ExprF}; use std::borrow::Cow; @@ -21,7 +22,7 @@ impl<'a> DynamicType for Type<'a> { TypeInternal::Expr(e) => e.get_type(), TypeInternal::Const(c) => Ok(Cow::Owned(type_of_const(*c))), TypeInternal::SuperType => Err(TypeError::new( - &Context::new(), + &TypecheckContext::new(), dhall_core::rc(ExprF::Const(Const::Sort)), TypeMessage::Untyped, )), @@ -34,7 +35,7 @@ impl<'a> DynamicType for Normalized<'a> { match &self.1 { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( - &Context::new(), + &TypecheckContext::new(), self.0.embed_absurd(), TypeMessage::Untyped, )), @@ -47,7 +48,7 @@ impl<'a> DynamicType for Typed<'a> { match &self.1 { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( - &Context::new(), + &TypecheckContext::new(), self.0.clone(), TypeMessage::Untyped, )), diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index b0ea63e..a183944 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -35,7 +35,7 @@ impl<'a> Typed<'a> { fn get_type_move(self) -> Result, TypeError> { let (expr, ty) = (self.0, self.1); ty.ok_or_else(|| { - TypeError::new(&Context::new(), expr, TypeMessage::Untyped) + TypeError::new(&TypecheckContext::new(), expr, TypeMessage::Untyped) }) } } @@ -66,7 +66,7 @@ impl<'a> Type<'a> { TypeInternal::Expr(e) => Ok(Cow::Borrowed(e)), TypeInternal::Const(c) => Ok(Cow::Owned(const_to_normalized(*c))), TypeInternal::SuperType => Err(TypeError::new( - &Context::new(), + &TypecheckContext::new(), rc(ExprF::Const(Const::Sort)), TypeMessage::Untyped, )), @@ -129,7 +129,7 @@ impl<'a> TypeInternal<'a> { TypeInternal::Expr(e) => Ok(*e), TypeInternal::Const(c) => Ok(const_to_normalized(c)), TypeInternal::SuperType => Err(TypeError::new( - &Context::new(), + &TypecheckContext::new(), rc(ExprF::Const(Const::Sort)), TypeMessage::Untyped, )), @@ -137,6 +137,22 @@ impl<'a> TypeInternal<'a> { } } +#[derive(Debug, Clone)] +pub(crate) struct TypecheckContext(Context>); + +impl TypecheckContext { + pub(crate) fn new() -> Self { + TypecheckContext(Context::new()) + } + pub(crate) fn insert_and_shift(&self, x: &Label, t: Type<'static>) -> Self { + TypecheckContext(self.0.insert(x.clone(), t).map(|_, e| e.shift0(1, x))) + } + pub(crate) fn lookup(&self, var: &V