use crate::imports::ImportRoot; use crate::normalize::{Thunk, Value}; use dhall_syntax::*; use std::marker::PhantomData; macro_rules! derive_other_traits { ($ty:ident) => { impl<'a> std::cmp::PartialEq for $ty<'a> { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl<'a> std::cmp::Eq for $ty<'a> {} impl<'a> std::fmt::Display for $ty<'a> { fn fmt( &self, f: &mut std::fmt::Formatter, ) -> Result<(), std::fmt::Error> { self.0.fmt(f) } } }; } #[derive(Debug, Clone)] pub(crate) struct Parsed<'a>( pub(crate) SubExpr, Import>, pub(crate) ImportRoot, ); derive_other_traits!(Parsed); #[derive(Debug, Clone)] pub(crate) struct Resolved<'a>( pub(crate) SubExpr, Normalized<'static>>, ); derive_other_traits!(Resolved); pub(crate) use self::typed::TypedInternal; #[derive(Debug, Clone)] pub(crate) struct Typed<'a>( pub(crate) TypedInternal, pub(crate) PhantomData<&'a ()>, ); #[derive(Debug, Clone)] pub(crate) struct Normalized<'a>( pub(crate) TypedInternal, pub(crate) PhantomData<&'a ()>, ); impl<'a> std::cmp::PartialEq for Normalized<'a> { fn eq(&self, other: &Self) -> bool { self.to_expr() == other.to_expr() } } impl<'a> std::cmp::Eq for Normalized<'a> {} impl<'a> std::fmt::Display for Normalized<'a> { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { self.to_expr().fmt(f) } } mod typed { use super::{Type, Typed}; use crate::normalize::{Thunk, Value}; use crate::typecheck::{ TypeError, TypeInternal, TypeMessage, TypecheckContext, }; use dhall_syntax::{Const, Label, SubExpr, V, X}; use std::borrow::Cow; use std::marker::PhantomData; #[derive(Debug, Clone)] pub(crate) enum TypedInternal { // The `Sort` higher-kinded type doesn't have a type Sort, // Any other value, along with its type Value(Thunk, Option>), } impl TypedInternal { pub(crate) fn from_thunk_and_type(th: Thunk, t: Type<'static>) -> Self { TypedInternal::Value(th, Some(t)) } pub(crate) fn from_thunk_untyped(th: Thunk) -> Self { TypedInternal::Value(th, None) } // TODO: Avoid cloning if possible pub(crate) fn to_value(&self) -> Value { match self { TypedInternal::Value(th, _) => th.to_value(), TypedInternal::Sort => Value::Const(Const::Sort), } } pub(crate) fn to_expr(&self) -> SubExpr { self.to_value().normalize_to_expr() } pub(crate) fn to_thunk(&self) -> Thunk { match self { TypedInternal::Value(th, _) => th.clone(), TypedInternal::Sort => { Thunk::from_value(Value::Const(Const::Sort)) } } } pub(crate) fn to_type(&self) -> Type<'static> { match self { TypedInternal::Sort => Type(TypeInternal::Const(Const::Sort)), TypedInternal::Value(th, _) => match &*th.as_value() { Value::Const(c) => Type(TypeInternal::Const(*c)), _ => Type(TypeInternal::Typed(Box::new(Typed( self.clone(), PhantomData, )))), }, } } pub(crate) fn get_type( &self, ) -> Result>, TypeError> { match self { TypedInternal::Value(_, Some(t)) => Ok(Cow::Borrowed(t)), TypedInternal::Value(_, None) => Err(TypeError::new( &TypecheckContext::new(), TypeMessage::Untyped, )), TypedInternal::Sort => Err(TypeError::new( &TypecheckContext::new(), TypeMessage::Sort, )), } } pub(crate) fn shift(&self, delta: isize, var: &V