From 423fdeebe9247b16744fae4b50df415bbd08be04 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 6 May 2019 22:09:55 +0200 Subject: Reorganize dhall into a phase structure --- dhall/src/phase/typecheck.rs | 1276 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1276 insertions(+) create mode 100644 dhall/src/phase/typecheck.rs (limited to 'dhall/src/phase/typecheck.rs') diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs new file mode 100644 index 0000000..6b12077 --- /dev/null +++ b/dhall/src/phase/typecheck.rs @@ -0,0 +1,1276 @@ +#![allow(non_snake_case)] +use std::borrow::Borrow; +use std::borrow::Cow; +use std::collections::BTreeMap; +use std::fmt; + +use crate::phase::normalize::{ + AlphaVar, NormalizationContext, Thunk, TypeThunk, Value, +}; +use crate::phase::*; +use crate::traits::DynamicType; +use dhall_proc_macros as dhall; +use dhall_syntax; +use dhall_syntax::context::Context; +use dhall_syntax::*; + +use self::TypeMessage::*; + +impl TypeThunk { + fn to_type(&self, ctx: &TypecheckContext) -> Result { + match self { + TypeThunk::Type(t) => Ok(t.clone()), + TypeThunk::Thunk(th) => { + // TODO: rule out statically + mktype(ctx, th.normalize_to_expr().embed_absurd().note_absurd()) + } + } + } +} + +#[derive(Debug, Clone)] +pub(crate) enum EnvItem { + Type(AlphaVar, Type), + Value(Normalized), +} + +impl EnvItem { + fn shift(&self, delta: isize, var: &AlphaVar) -> Self { + use EnvItem::*; + match self { + Type(v, e) => Type(v.shift(delta, var), e.shift(delta, var)), + Value(e) => Value(e.shift(delta, var)), + } + } +} + +#[derive(Debug, Clone)] +pub(crate) struct TypecheckContext(pub(crate) Context); + +impl TypecheckContext { + pub(crate) fn new() -> Self { + TypecheckContext(Context::new()) + } + pub(crate) fn insert_type(&self, x: &Label, t: Type) -> Self { + TypecheckContext( + self.0.map(|_, e| e.shift(1, &x.into())).insert( + x.clone(), + EnvItem::Type(x.into(), t.shift(1, &x.into())), + ), + ) + } + pub(crate) fn insert_value(&self, x: &Label, t: Normalized) -> Self { + TypecheckContext(self.0.insert(x.clone(), EnvItem::Value(t))) + } + pub(crate) fn lookup(&self, var: &V