From 833cb91cec6ae708e17a0f9589eba9560e81bd07 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 7 May 2019 19:47:04 +0200 Subject: Unify typecheck and normalization contexts --- dhall/src/core/context.rs | 166 ++++++++++++++++++++++++++----------------- dhall/src/core/var.rs | 12 ++++ dhall/src/error/mod.rs | 6 -- dhall/src/phase/mod.rs | 3 - dhall/src/phase/typecheck.rs | 4 +- 5 files changed, 115 insertions(+), 76 deletions(-) diff --git a/dhall/src/core/context.rs b/dhall/src/core/context.rs index e693317..aeec7fb 100644 --- a/dhall/src/core/context.rs +++ b/dhall/src/core/context.rs @@ -1,66 +1,88 @@ use std::borrow::Cow; use std::rc::Rc; -use dhall_syntax::context::Context; +use dhall_syntax::context::Context as SimpleContext; use dhall_syntax::{Label, V}; use crate::core::thunk::Thunk; use crate::core::value::Value; use crate::core::var::{AlphaVar, Shift, Subst}; -use crate::phase::{Normalized, Type, Typed}; +use crate::error::TypeError; +use crate::phase::{Type, Typed}; #[derive(Debug, Clone)] -enum NzEnvItem { - Thunk(Thunk), - Skip(AlphaVar), +pub(crate) enum CtxItem { + Kept(AlphaVar, T), + Replaced(Thunk, T), } #[derive(Debug, Clone)] -pub(crate) struct NormalizationContext(Rc>); +pub(crate) struct Context(Rc>>); #[derive(Debug, Clone)] -pub(crate) enum TyEnvItem { - Type(AlphaVar, Type), - Value(Normalized), -} +pub(crate) struct NormalizationContext(Context<()>); #[derive(Debug, Clone)] -pub(crate) struct TypecheckContext(pub(crate) Context); +pub(crate) struct TypecheckContext(Context); + +impl CtxItem { + fn forget(&self) -> CtxItem<()> { + match self { + CtxItem::Kept(var, _) => CtxItem::Kept(var.clone(), ()), + CtxItem::Replaced(e, _) => CtxItem::Replaced(e.clone(), ()), + } + } +} + +impl Context { + pub(crate) fn new() -> Self { + Context(Rc::new(SimpleContext::new())) + } + pub(crate) fn forget(&self) -> Context<()> { + let mut ctx = SimpleContext::new(); + for (k, vs) in self.0.iter_keys() { + for v in vs.iter() { + ctx = ctx.insert(k.clone(), v.forget()); + } + } + Context(Rc::new(ctx)) + } + pub(crate) fn insert_kept(&self, x: &Label, t: T) -> Self + where + T: Shift + Clone, + { + Context(Rc::new(self.0.map(|_, e| e.shift(1, &x.into())).insert( + x.clone(), + CtxItem::Kept(x.into(), t.shift(1, &x.into())), + ))) + } + pub(crate) fn insert_replaced(&self, x: &Label, e: Thunk, t: T) -> Self + where + T: Clone, + { + Context(Rc::new(self.0.insert(x.clone(), CtxItem::Replaced(e, t)))) + } + pub(crate) fn lookup(&self, var: &V