From 4c159640e5ee77ffa48b85a5bffa56350cf933ef Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 4 May 2019 17:59:05 +0200 Subject: Make SubExpr generic in the variable labels type --- dhall/src/binary.rs | 22 ++++++++------ dhall/src/expr.rs | 32 +++++++++++--------- dhall/src/normalize.rs | 48 ++++++++++++++++-------------- dhall/src/serde.rs | 2 +- dhall/src/traits/static_type.rs | 14 ++++----- dhall/src/typecheck.rs | 65 +++++++++++++++++++++-------------------- 6 files changed, 100 insertions(+), 83 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/binary.rs b/dhall/src/binary.rs index 9c31d4c..0d9f28a 100644 --- a/dhall/src/binary.rs +++ b/dhall/src/binary.rs @@ -2,7 +2,7 @@ use dhall_syntax::*; use itertools::*; use serde_cbor::value::value as cbor; -type ParsedExpr = SubExpr; +type ParsedExpr = SubExpr; #[derive(Debug)] pub enum DecodeError { @@ -18,28 +18,32 @@ pub fn decode(data: &[u8]) -> Result { } fn cbor_value_to_dhall(data: &cbor::Value) -> Result { - use cbor::Value::*; + use cbor::Value::{Array, Bool, Null, Object, String, F64, I64, U64}; use dhall_syntax::{BinOp, Builtin, Const}; - use ExprF::*; + use ExprF::{ + Annot, App, BoolIf, BoolLit, DoubleLit, Embed, EmptyListLit, Field, + IntegerLit, Lam, Let, Merge, NEListLit, NaturalLit, OldOptionalLit, Pi, + RecordLit, RecordType, SomeLit, TextLit, UnionLit, UnionType, + }; Ok(rc(match data { String(s) => match Builtin::parse(s) { Some(b) => ExprF::Builtin(b), None => match s.as_str() { "True" => BoolLit(true), "False" => BoolLit(false), - "Type" => Const(Const::Type), - "Kind" => Const(Const::Kind), - "Sort" => Const(Const::Sort), + "Type" => ExprF::Const(Const::Type), + "Kind" => ExprF::Const(Const::Kind), + "Sort" => ExprF::Const(Const::Sort), _ => Err(DecodeError::WrongFormatError("builtin".to_owned()))?, }, }, - U64(n) => Var(V(Label::from("_"), *n as usize)), + U64(n) => ExprF::Var(Var(Label::from("_"), *n as usize)), F64(x) => DoubleLit((*x).into()), Bool(b) => BoolLit(*b), Array(vec) => match vec.as_slice() { [String(l), U64(n)] => { let l = Label::from(l.as_str()); - Var(V(l, *n as usize)) + ExprF::Var(Var(l, *n as usize)) } [U64(0), f, args..] => { let mut f = cbor_value_to_dhall(&f)?; @@ -92,7 +96,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { Err(DecodeError::WrongFormatError("binop".to_owned()))? } }; - BinOp(op, x, y) + ExprF::BinOp(op, x, y) } [U64(4), t] => { let t = cbor_value_to_dhall(&t)?; diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 5bde68f..efc3928 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -23,15 +23,17 @@ macro_rules! derive_other_traits { }; } +type OutputSubExpr = SubExpr; + #[derive(Debug, Clone)] pub(crate) struct Parsed( - pub(crate) SubExpr, + pub(crate) SubExpr, pub(crate) ImportRoot, ); derive_other_traits!(Parsed); #[derive(Debug, Clone)] -pub(crate) struct Resolved(pub(crate) SubExpr); +pub(crate) struct Resolved(pub(crate) SubExpr); derive_other_traits!(Resolved); pub(crate) use self::typed::TypedInternal; @@ -57,12 +59,12 @@ impl std::fmt::Display for Normalized { } mod typed { - use super::{Type, Typed}; + use super::{OutputSubExpr, Type, Typed}; use crate::normalize::{Thunk, Value}; use crate::typecheck::{ TypeError, TypeInternal, TypeMessage, TypecheckContext, }; - use dhall_syntax::{Const, Label, SubExpr, V, X}; + use dhall_syntax::{Const, Label, Var}; use std::borrow::Cow; #[derive(Debug, Clone)] @@ -90,7 +92,7 @@ mod typed { } } - pub(crate) fn to_expr(&self) -> SubExpr { + pub(crate) fn to_expr(&self) -> OutputSubExpr { self.to_value().normalize_to_expr() } @@ -129,7 +131,7 @@ mod typed { } } - pub(crate) fn shift(&self, delta: isize, var: &V