From 9741e3280ed03920732430e7994e1f8482c9ddd6 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 17:48:13 +0200 Subject: s/DhallError/ImportError/ --- dhall/tests/common/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'dhall/tests/common') diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index 397a8ee..e748cf0 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -44,13 +44,13 @@ pub enum Feature { TypeInferenceFailure, } -pub fn read_dhall_file<'i>(file_path: &str) -> Result, DhallError> { +pub fn read_dhall_file<'i>(file_path: &str) -> Result, ImportError> { load_dhall_file(&PathBuf::from(file_path), true) } pub fn read_dhall_file_no_resolve_imports<'i>( file_path: &str, -) -> Result { +) -> Result { load_dhall_file_no_resolve_imports(&PathBuf::from(file_path)) } @@ -93,7 +93,7 @@ pub fn run_test(base_path: &str, feature: Feature) { let err = read_dhall_file_no_resolve_imports(&file_path).unwrap_err(); match err { - DhallError::ParseError(_) => {} + ImportError::ParseError(_) => {} e => panic!("Expected parse error, got: {:?}", e), } } -- cgit v1.3.1 From 396ec334bac1e8d10a2d2b2d683c93e3b2ff4d8d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 20:04:04 +0200 Subject: Massage import loading into new API Closes #9 --- dhall/src/expr.rs | 30 ++++++++++----- dhall/src/imports.rs | 95 ++++++++++++++++++++++++++++------------------- dhall/tests/common/mod.rs | 9 ++++- dhall_core/src/core.rs | 39 +++++++++++++++++++ 4 files changed, 122 insertions(+), 51 deletions(-) (limited to 'dhall/tests/common') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 0d093cb..72633ea 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,17 +1,27 @@ +use crate::imports::ImportError; +use crate::imports::ImportRoot; use crate::typecheck::TypeError; use dhall_core::*; -pub struct Parsed(SubExpr); -pub struct Resolved(SubExpr); -pub struct Typed(SubExpr, Type); -pub struct Type(Box); -pub struct Normalized(SubExpr); +#[derive(Debug, Clone)] +pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); +#[derive(Debug, Clone)] +pub struct Resolved(pub(crate) SubExpr); +#[derive(Debug, Clone)] +pub struct Typed(pub(crate) SubExpr, Type); +#[derive(Debug, Clone)] +pub struct Type(pub(crate) Box); +#[derive(Debug, Clone)] +pub struct Normalized(pub(crate) SubExpr); -// impl Parsed { -// pub fn resolve(self) -> Result { -// Ok(Resolved(crate::imports::resolve(self.0)?)) -// } -// } +impl Parsed { + pub fn resolve(self) -> Result { + crate::imports::resolve_expr(self, true) + } + pub fn resolve_no_imports(self) -> Result { + crate::imports::resolve_expr(self, false) + } +} impl Resolved { pub fn typecheck(self) -> Result> { let typ = Type(Box::new(Normalized(crate::typecheck::type_of( diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 95cf6fa..bc38bb6 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -1,6 +1,7 @@ // use dhall_core::{Expr, FilePrefix, Import, ImportLocation, ImportMode, X}; use dhall_core::{Expr, Import, X}; // use std::path::Path; +use crate::expr::*; use dhall_core::*; use std::fmt; use std::fs::File; @@ -8,6 +9,34 @@ use std::io::Read; use std::path::Path; use std::path::PathBuf; +#[derive(Debug)] +pub enum ImportError { + ParseError(ParseError), + IOError(std::io::Error), + UnexpectedImportError(Import), +} +impl From for ImportError { + fn from(e: ParseError) -> Self { + ImportError::ParseError(e) + } +} +impl From for ImportError { + fn from(e: std::io::Error) -> Self { + ImportError::IOError(e) + } +} +impl fmt::Display for ImportError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use self::ImportError::*; + match self { + ParseError(e) => e.fmt(f), + IOError(e) => e.fmt(f), + UnexpectedImportError(e) => e.fmt(f), + } + } +} + +// Deprecated pub fn panic_imports(expr: &Expr) -> Expr { let no_import = |i: &Import| -> X { panic!("ahhh import: {:?}", i) }; expr.map_embed(&no_import) @@ -42,55 +71,43 @@ fn resolve_import( } } -#[derive(Debug)] -pub enum ImportError { - ParseError(ParseError), - IOError(std::io::Error), -} -impl From for ImportError { - fn from(e: ParseError) -> Self { - ImportError::ParseError(e) - } -} -impl From for ImportError { - fn from(e: std::io::Error) -> Self { - ImportError::IOError(e) - } -} -impl fmt::Display for ImportError { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use self::ImportError::*; - match self { - ParseError(e) => e.fmt(f), - IOError(e) => e.fmt(f), +pub(crate) fn resolve_expr( + Parsed(expr, root): Parsed, + allow_imports: bool, +) -> Result { + let resolve = |import: &Import| -> Result, ImportError> { + if allow_imports { + let expr = resolve_import(import, &root)?; + Ok(expr.roll()) + } else { + Err(ImportError::UnexpectedImportError(import.clone())) } - } + }; + let expr = expr.as_ref().traverse_embed(&resolve)?; + Ok(Resolved(expr.squash_embed())) +} + +pub fn load_from_file(f: &Path) -> Result { + let mut buffer = String::new(); + File::open(f)?.read_to_string(&mut buffer)?; + let expr = parse_expr(&*buffer)?; + let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); + Ok(Parsed(expr, root)) } +// Deprecated pub fn load_dhall_file( f: &Path, resolve_imports: bool, ) -> Result, ImportError> { - let mut buffer = String::new(); - File::open(f)?.read_to_string(&mut buffer)?; - let expr = parse_expr(&*buffer)?; - let expr = if resolve_imports { - let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); - let resolve = |import: &Import| -> Expr { - resolve_import(import, &root).unwrap() - }; - expr.as_ref().map_embed(&resolve).squash_embed() - } else { - panic_imports(expr.as_ref()) - }; - Ok(expr) + let expr = load_from_file(f)?; + let expr = resolve_expr(expr, resolve_imports)?; + Ok(expr.0.unroll()) } +// Deprecated pub fn load_dhall_file_no_resolve_imports( f: &Path, ) -> Result { - let mut buffer = String::new(); - File::open(f)?.read_to_string(&mut buffer)?; - let expr = parse_expr(&*buffer)?; - Ok(expr) + Ok(load_from_file(f)?.0) } diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index e748cf0..70b7d81 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -54,6 +54,12 @@ pub fn read_dhall_file_no_resolve_imports<'i>( load_dhall_file_no_resolve_imports(&PathBuf::from(file_path)) } +pub fn load_from_file_str<'i>( + file_path: &str, +) -> Result { + load_from_file(&PathBuf::from(file_path)) +} + pub fn run_test(base_path: &str, feature: Feature) { use self::Feature::*; let base_path_prefix = match feature { @@ -90,8 +96,7 @@ pub fn run_test(base_path: &str, feature: Feature) { } ParserFailure => { let file_path = base_path + ".dhall"; - let err = - read_dhall_file_no_resolve_imports(&file_path).unwrap_err(); + let err = load_from_file_str(&file_path).unwrap_err(); match err { ImportError::ParseError(_) => {} e => panic!("Expected parse error, got: {:?}", e), diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index a56c5a3..d832cd5 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -285,6 +285,28 @@ impl Expr { self.map_shallow(recurse, |x| x.clone(), map_embed, |x| x.clone()) } + #[inline(always)] + pub fn traverse_embed( + &self, + map_embed: &F, + ) -> Result, Err> + where + S: Clone, + B: Clone, + F: Fn(&A) -> Result, + { + let recurse = |e: &SubExpr| -> Result, Err> { + Ok(e.as_ref().traverse_embed(map_embed)?.roll()) + }; + self.as_ref().traverse( + |e| recurse(e), + |_, e| recurse(e), + |x| Ok(S::clone(x)), + map_embed, + |x| Ok(Label::clone(x)), + ) + } + pub fn map_label(&self, map_label: &F) -> Self where A: Clone, @@ -319,6 +341,23 @@ impl Expr> { } } +impl Expr> { + pub fn squash_embed(&self) -> SubExpr { + match self.as_ref() { + ExprF::Embed(e) => e.clone(), + e => e + .map( + |e| e.as_ref().squash_embed(), + |_, e| e.as_ref().squash_embed(), + S::clone, + |_| unreachable!(), + Label::clone, + ) + .roll(), + } + } +} + impl ExprF { #[inline(always)] pub fn as_ref(&self) -> ExprF<&SE, &L, &N, &E> -- cgit v1.3.1 From 412d0fac51b7b51aabcb049e3d6ba52f3dda1529 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 20:32:25 +0200 Subject: Move binary decoding to new API --- dhall/src/expr.rs | 22 +++++++++++++-------- dhall/src/imports.rs | 49 ++++++++++++++++++++++++++++++++++++++--------- dhall/src/lib.rs | 2 +- dhall/tests/common/mod.rs | 25 +++++++++++------------- 4 files changed, 66 insertions(+), 32 deletions(-) (limited to 'dhall/tests/common') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 72633ea..ae52e4d 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,27 +1,33 @@ -use crate::imports::ImportError; use crate::imports::ImportRoot; use crate::typecheck::TypeError; use dhall_core::*; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq)] pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); -#[derive(Debug, Clone)] + +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Resolved(pub(crate) SubExpr); + #[derive(Debug, Clone)] pub struct Typed(pub(crate) SubExpr, Type); + #[derive(Debug, Clone)] pub struct Type(pub(crate) Box); + #[derive(Debug, Clone)] pub struct Normalized(pub(crate) SubExpr); -impl Parsed { - pub fn resolve(self) -> Result { - crate::imports::resolve_expr(self, true) +impl PartialEq for Parsed { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 } - pub fn resolve_no_imports(self) -> Result { - crate::imports::resolve_expr(self, false) +} +impl std::fmt::Display for Parsed { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + self.0.fmt(f) } } + impl Resolved { pub fn typecheck(self) -> Result> { let typ = Type(Box::new(Normalized(crate::typecheck::type_of( diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index bc38bb6..5d94b6a 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -1,6 +1,7 @@ // use dhall_core::{Expr, FilePrefix, Import, ImportLocation, ImportMode, X}; use dhall_core::{Expr, Import, X}; // use std::path::Path; +use crate::binary::DecodeError; use crate::expr::*; use dhall_core::*; use std::fmt; @@ -12,6 +13,7 @@ use std::path::PathBuf; #[derive(Debug)] pub enum ImportError { ParseError(ParseError), + BinaryDecodeError(DecodeError), IOError(std::io::Error), UnexpectedImportError(Import), } @@ -20,6 +22,11 @@ impl From for ImportError { ImportError::ParseError(e) } } +impl From for ImportError { + fn from(e: DecodeError) -> Self { + ImportError::BinaryDecodeError(e) + } +} impl From for ImportError { fn from(e: std::io::Error) -> Self { ImportError::IOError(e) @@ -30,6 +37,7 @@ impl fmt::Display for ImportError { use self::ImportError::*; match self { ParseError(e) => e.fmt(f), + BinaryDecodeError(_) => unimplemented!(), IOError(e) => e.fmt(f), UnexpectedImportError(e) => e.fmt(f), } @@ -71,7 +79,7 @@ fn resolve_import( } } -pub(crate) fn resolve_expr( +fn resolve_expr( Parsed(expr, root): Parsed, allow_imports: bool, ) -> Result { @@ -87,12 +95,35 @@ pub(crate) fn resolve_expr( Ok(Resolved(expr.squash_embed())) } -pub fn load_from_file(f: &Path) -> Result { - let mut buffer = String::new(); - File::open(f)?.read_to_string(&mut buffer)?; - let expr = parse_expr(&*buffer)?; - let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); - Ok(Parsed(expr, root)) +impl Parsed { + pub fn load_from_file(f: &Path) -> Result { + let mut buffer = String::new(); + File::open(f)?.read_to_string(&mut buffer)?; + let expr = parse_expr(&*buffer)?; + let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); + Ok(Parsed(expr, root)) + } + + pub fn load_from_str(s: &str) -> Result { + let expr = parse_expr(s)?; + let root = ImportRoot::LocalDir(std::env::current_dir()?); + Ok(Parsed(expr, root)) + } + + pub fn load_from_binary_file(f: &Path) -> Result { + let mut buffer = Vec::new(); + File::open(f)?.read_to_end(&mut buffer)?; + let expr = crate::binary::decode(&buffer)?; + let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); + Ok(Parsed(expr, root)) + } + + pub fn resolve(self) -> Result { + crate::imports::resolve_expr(self, true) + } + pub fn resolve_no_imports(self) -> Result { + crate::imports::resolve_expr(self, false) + } } // Deprecated @@ -100,7 +131,7 @@ pub fn load_dhall_file( f: &Path, resolve_imports: bool, ) -> Result, ImportError> { - let expr = load_from_file(f)?; + let expr = Parsed::load_from_file(f)?; let expr = resolve_expr(expr, resolve_imports)?; Ok(expr.0.unroll()) } @@ -109,5 +140,5 @@ pub fn load_dhall_file( pub fn load_dhall_file_no_resolve_imports( f: &Path, ) -> Result { - Ok(load_from_file(f)?.0) + Ok(Parsed::load_from_file(f)?.0) } diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index fee5ba8..28b43ba 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -9,7 +9,7 @@ mod normalize; pub use crate::normalize::*; -pub mod binary; +mod binary; pub mod imports; mod traits; pub mod typecheck; diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index 70b7d81..861df63 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -44,20 +44,20 @@ pub enum Feature { TypeInferenceFailure, } -pub fn read_dhall_file<'i>(file_path: &str) -> Result, ImportError> { +fn read_dhall_file<'i>(file_path: &str) -> Result, ImportError> { load_dhall_file(&PathBuf::from(file_path), true) } -pub fn read_dhall_file_no_resolve_imports<'i>( +fn load_from_file_str<'i>( file_path: &str, -) -> Result { - load_dhall_file_no_resolve_imports(&PathBuf::from(file_path)) +) -> Result { + Parsed::load_from_file(&PathBuf::from(file_path)) } -pub fn load_from_file_str<'i>( +fn load_from_binary_file_str<'i>( file_path: &str, ) -> Result { - load_from_file(&PathBuf::from(file_path)) + Parsed::load_from_binary_file(&PathBuf::from(file_path)) } pub fn run_test(base_path: &str, feature: Feature) { @@ -77,21 +77,18 @@ pub fn run_test(base_path: &str, feature: Feature) { ParserSuccess => { let expr_file_path = base_path.clone() + "A.dhall"; let expected_file_path = base_path + "B.dhallb"; - let expr = read_dhall_file_no_resolve_imports(&expr_file_path) + let expr = load_from_file_str(&expr_file_path) .map_err(|e| println!("{}", e)) .unwrap(); - use std::fs::File; - use std::io::Read; - let mut file = File::open(expected_file_path).unwrap(); - let mut data = Vec::new(); - file.read_to_end(&mut data).unwrap(); - let expected = dhall::binary::decode(&data).unwrap(); + let expected = load_from_binary_file_str(&expected_file_path) + .map_err(|e| println!("{}", e)) + .unwrap(); assert_eq_pretty!(expr, expected); // Round-trip pretty-printer - let expr = parse_expr(&expr.to_string()).unwrap(); + let expr = Parsed::load_from_str(&expr.to_string()).unwrap(); assert_eq!(expr, expected); } ParserFailure => { -- cgit v1.3.1 From 619290e208f59bb15fc4f9d4b6dae2c19bb7b711 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Apr 2019 01:41:08 +0200 Subject: Augment stack size for typecheck tests --- dhall/src/typecheck.rs | 12 ++++++++++++ dhall/tests/common/mod.rs | 20 +++++++++++++++----- dhall/tests/typecheck.rs | 6 +++--- 3 files changed, 30 insertions(+), 8 deletions(-) (limited to 'dhall/tests/common') diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index becabbd..fe9f68c 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -17,40 +17,50 @@ impl Resolved { } } impl Typed { + #[inline(always)] fn as_expr(&self) -> &SubExpr { &self.0 } + #[inline(always)] fn into_expr(self) -> SubExpr { self.0 } + #[inline(always)] pub fn get_type<'a>(&'a self) -> Type<'a> { self.1.reborrow() } } impl Normalized { + #[inline(always)] fn as_expr(&self) -> &SubExpr { &self.0 } + #[inline(always)] fn into_expr(self) -> SubExpr { self.0 } + #[inline(always)] pub fn get_type<'a>(&'a self) -> Type<'a> { self.1.reborrow() } + #[inline(always)] fn into_type(self) -> Type<'static> { crate::expr::Type(Cow::Owned(TypeInternal::Expr(Box::new(self)))) } } impl<'a> Type<'a> { + #[inline(always)] fn into_owned(self) -> Type<'static> { Type(Cow::Owned(self.0.into_owned())) } + #[inline(always)] fn reborrow<'b>(&'b self) -> Type<'b> { match &self.0 { Cow::Owned(x) => crate::expr::Type(Cow::Borrowed(x)), Cow::Borrowed(x) => crate::expr::Type(Cow::Borrowed(x)), } } + #[inline(always)] fn as_expr(&'a self) -> Result<&'a SubExpr, TypeError> { use TypeInternal::*; match self.0.as_ref() { @@ -62,6 +72,7 @@ impl<'a> Type<'a> { )), } } + #[inline(always)] fn into_expr(self) -> Result, TypeError> { use TypeInternal::*; match self.0.into_owned() { @@ -73,6 +84,7 @@ impl<'a> Type<'a> { )), } } + #[inline(always)] pub fn get_type<'b>(&'b self) -> Type<'b> { use TypeInternal::*; match self.0.as_ref() { diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index 861df63..3c2fc3c 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -113,11 +113,21 @@ pub fn run_test(base_path: &str, feature: Feature) { typecheck::type_of(expr).unwrap_err(); } TypecheckSuccess => { - let expr_file_path = base_path.clone() + "A.dhall"; - let expected_file_path = base_path + "B.dhall"; - let expr = rc(read_dhall_file(&expr_file_path).unwrap()); - let expected = rc(read_dhall_file(&expected_file_path).unwrap()); - typecheck::type_of(rc(ExprF::Annot(expr, expected))).unwrap(); + // Many tests stack overflow in debug mode + std::thread::Builder::new() + .stack_size(4 * 1024 * 1024) + .spawn(|| { + let expr_file_path = base_path.clone() + "A.dhall"; + let expected_file_path = base_path + "B.dhall"; + let expr = rc(read_dhall_file(&expr_file_path).unwrap()); + let expected = + rc(read_dhall_file(&expected_file_path).unwrap()); + typecheck::type_of(rc(ExprF::Annot(expr, expected))) + .unwrap(); + }) + .unwrap() + .join() + .unwrap(); } TypeInferenceFailure => { let file_path = base_path + ".dhall"; diff --git a/dhall/tests/typecheck.rs b/dhall/tests/typecheck.rs index 56a8823..6e05a87 100644 --- a/dhall/tests/typecheck.rs +++ b/dhall/tests/typecheck.rs @@ -87,8 +87,8 @@ tc_success!(spec_typecheck_success_prelude_List_replicate_0, "prelude/List/repli tc_success!(spec_typecheck_success_prelude_List_replicate_1, "prelude/List/replicate/1"); tc_success!(spec_typecheck_success_prelude_List_reverse_0, "prelude/List/reverse/0"); tc_success!(spec_typecheck_success_prelude_List_reverse_1, "prelude/List/reverse/1"); -// tc_success!(spec_typecheck_success_prelude_List_shifted_0, "prelude/List/shifted/0"); -// tc_success!(spec_typecheck_success_prelude_List_shifted_1, "prelude/List/shifted/1"); +tc_success!(spec_typecheck_success_prelude_List_shifted_0, "prelude/List/shifted/0"); +tc_success!(spec_typecheck_success_prelude_List_shifted_1, "prelude/List/shifted/1"); tc_success!(spec_typecheck_success_prelude_List_unzip_0, "prelude/List/unzip/0"); tc_success!(spec_typecheck_success_prelude_List_unzip_1, "prelude/List/unzip/1"); tc_success!(spec_typecheck_success_prelude_Monoid_00, "prelude/Monoid/00"); @@ -96,7 +96,7 @@ tc_success!(spec_typecheck_success_prelude_Monoid_01, "prelude/Monoid/01"); tc_success!(spec_typecheck_success_prelude_Monoid_02, "prelude/Monoid/02"); tc_success!(spec_typecheck_success_prelude_Monoid_03, "prelude/Monoid/03"); tc_success!(spec_typecheck_success_prelude_Monoid_04, "prelude/Monoid/04"); -// tc_success!(spec_typecheck_success_prelude_Monoid_05, "prelude/Monoid/05"); +tc_success!(spec_typecheck_success_prelude_Monoid_05, "prelude/Monoid/05"); tc_success!(spec_typecheck_success_prelude_Monoid_06, "prelude/Monoid/06"); tc_success!(spec_typecheck_success_prelude_Monoid_07, "prelude/Monoid/07"); tc_success!(spec_typecheck_success_prelude_Monoid_08, "prelude/Monoid/08"); -- cgit v1.3.1 From c461548c32f8cb3ee2db5ade88ae4f91b3838ab5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Apr 2019 11:09:57 +0200 Subject: Avoid constructing exprs manually when possible --- dhall/src/typecheck.rs | 38 ++++++++++++++++++++------------------ dhall/tests/common/mod.rs | 2 +- dhall/tests/normalization.rs | 1 + dhall/tests/parser.rs | 1 + dhall/tests/typecheck.rs | 1 + dhall_core/src/parser.rs | 2 ++ dhall_generator/src/quote.rs | 3 +++ 7 files changed, 29 insertions(+), 19 deletions(-) (limited to 'dhall/tests/common') diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index 3815f9f..5be43cf 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -370,7 +370,9 @@ pub fn type_with( } Let(f, mt, r, b) => { let r = if let Some(t) = mt { - rc(Annot(SubExpr::clone(r), SubExpr::clone(t))) + let r = SubExpr::clone(r); + let t = SubExpr::clone(t); + dhall::subexpr!(r: t) } else { SubExpr::clone(r) }; @@ -409,8 +411,8 @@ pub fn type_with( Lam(_, _, _) => unreachable!(), Pi(_, _, _) => unreachable!(), Let(_, _, _, _) => unreachable!(), - Const(Type) => Ok(RetExpr(Const(Kind))), - Const(Kind) => Ok(RetExpr(Const(Sort))), + Const(Type) => Ok(RetExpr(dhall::expr!(Kind))), + Const(Kind) => Ok(RetExpr(dhall::expr!(Sort))), Const(Sort) => Ok(RetType(TYPE_OF_SORT)), Var(V(x, n)) => match ctx.lookup(&x, n) { Some(e) => Ok(RetExpr(e.unroll())), @@ -531,7 +533,7 @@ pub fn type_with( InvalidFieldType(k.clone(), t.clone()), )?; } - Ok(RetExpr(Const(Type))) + Ok(RetExpr(dhall::expr!(Type))) } RecordLit(kvs) => { let kts = kvs @@ -554,25 +556,25 @@ pub fn type_with( _ => Err(mkerr(NotARecord(x.clone(), r.clone()))), }, Builtin(b) => Ok(RetExpr(type_of_builtin(b))), - BoolLit(_) => Ok(RetExpr(Builtin(Bool))), - NaturalLit(_) => Ok(RetExpr(Builtin(Natural))), - IntegerLit(_) => Ok(RetExpr(Builtin(Integer))), - DoubleLit(_) => Ok(RetExpr(Builtin(Double))), + BoolLit(_) => Ok(RetExpr(dhall::expr!(Bool))), + NaturalLit(_) => Ok(RetExpr(dhall::expr!(Natural))), + IntegerLit(_) => Ok(RetExpr(dhall::expr!(Integer))), + DoubleLit(_) => Ok(RetExpr(dhall::expr!(Double))), // TODO: check type of interpolations - TextLit(_) => Ok(RetExpr(Builtin(Text))), + TextLit(_) => Ok(RetExpr(dhall::expr!(Text))), BinOp(o, l, r) => { let t = mktype( ctx, - rc(Builtin(match o { - BoolAnd => Bool, - BoolOr => Bool, - BoolEQ => Bool, - BoolNE => Bool, - NaturalPlus => Natural, - NaturalTimes => Natural, - TextAppend => Text, + match o { + BoolAnd => dhall::subexpr!(Bool), + BoolOr => dhall::subexpr!(Bool), + BoolEQ => dhall::subexpr!(Bool), + BoolNE => dhall::subexpr!(Bool), + NaturalPlus => dhall::subexpr!(Natural), + NaturalTimes => dhall::subexpr!(Natural), + TextAppend => dhall::subexpr!(Text), _ => panic!("Unimplemented typecheck case: {:?}", e), - })), + }, )?; ensure_equal(&l.get_type(), &t, mkerr, || { diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index 3c2fc3c..fc5aa5b 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -122,7 +122,7 @@ pub fn run_test(base_path: &str, feature: Feature) { let expr = rc(read_dhall_file(&expr_file_path).unwrap()); let expected = rc(read_dhall_file(&expected_file_path).unwrap()); - typecheck::type_of(rc(ExprF::Annot(expr, expected))) + typecheck::type_of(dhall::subexpr!(expr: expected)) .unwrap(); }) .unwrap() diff --git a/dhall/tests/normalization.rs b/dhall/tests/normalization.rs index 5ecc02f..a36a6ac 100644 --- a/dhall/tests/normalization.rs +++ b/dhall/tests/normalization.rs @@ -1,3 +1,4 @@ +#![feature(proc_macro_hygiene)] #![feature(custom_inner_attributes)] #![rustfmt::skip] mod common; diff --git a/dhall/tests/parser.rs b/dhall/tests/parser.rs index 0b5e2d6..3969dc9 100644 --- a/dhall/tests/parser.rs +++ b/dhall/tests/parser.rs @@ -1,3 +1,4 @@ +#![feature(proc_macro_hygiene)] #![feature(custom_inner_attributes)] #![rustfmt::skip] mod common; diff --git a/dhall/tests/typecheck.rs b/dhall/tests/typecheck.rs index 6e05a87..8d72313 100644 --- a/dhall/tests/typecheck.rs +++ b/dhall/tests/typecheck.rs @@ -1,3 +1,4 @@ +#![feature(proc_macro_hygiene)] #![feature(custom_inner_attributes)] #![rustfmt::skip] mod common; diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index 67bcf77..41a2ce7 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -764,6 +764,7 @@ make_parser! { "False" => BoolLit(false), "Type" => Const(crate::Const::Type), "Kind" => Const(crate::Const::Kind), + "Sort" => Const(crate::Const::Sort), _ => Var(V(l, idx)), } } @@ -777,6 +778,7 @@ make_parser! { "False" => BoolLit(false), "Type" => Const(crate::Const::Type), "Kind" => Const(crate::Const::Kind), + "Sort" => Const(crate::Const::Sort), _ => Var(V(l, 0)), } } diff --git a/dhall_generator/src/quote.rs b/dhall_generator/src/quote.rs index d0c5733..8fce89d 100644 --- a/dhall_generator/src/quote.rs +++ b/dhall_generator/src/quote.rs @@ -63,6 +63,9 @@ where let a = quote_vec(a); quote! { dhall_core::ExprF::App(#f, #a) } } + Annot(x, t) => { + quote! { dhall_core::ExprF::Annot(#x, #t) } + } Const(c) => { let c = quote_const(c); quote! { dhall_core::ExprF::Const(#c) } -- cgit v1.3.1 From 4bebcd96b6e76b9b8ae7877af91d2ae571e617a9 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 7 Apr 2019 16:45:30 +0200 Subject: Restrict public API Closes #20 --- dhall/src/expr.rs | 48 ++++++++++++++++++++++-------------- dhall/src/imports.rs | 15 +---------- dhall/src/lib.rs | 12 ++++----- dhall/src/main.rs | 20 +++++++-------- dhall/src/normalize.rs | 8 ++++-- dhall/src/typecheck.rs | 9 ++++--- dhall/tests/common/mod.rs | 63 +++++++++++++++++++++++++++++++++++------------ 7 files changed, 104 insertions(+), 71 deletions(-) (limited to 'dhall/tests/common') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 555db2f..7baf628 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,34 +1,46 @@ use crate::imports::ImportRoot; use dhall_core::*; +macro_rules! derive_other_traits { + ($ty:ident) => { + impl std::cmp::PartialEq for $ty { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } + } + + impl std::fmt::Display for $ty { + fn fmt( + &self, + f: &mut std::fmt::Formatter, + ) -> Result<(), std::fmt::Error> { + self.0.fmt(f) + } + } + }; +} + #[derive(Debug, Clone, Eq)] pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); +derive_other_traits!(Parsed); -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Eq)] pub struct Resolved(pub(crate) SubExpr); +derive_other_traits!(Resolved); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq)] pub struct Typed(pub(crate) SubExpr, pub(crate) Type); +derive_other_traits!(Typed); + +#[derive(Debug, Clone, Eq)] +pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); +derive_other_traits!(Normalized); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Type(pub(crate) TypeInternal); -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum TypeInternal { Expr(Box), Untyped, } - -#[derive(Debug, Clone)] -pub struct Normalized(pub(crate) SubExpr, pub(crate) Type); - -impl PartialEq for Parsed { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } -} -impl std::fmt::Display for Parsed { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - self.0.fmt(f) - } -} diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 5d94b6a..fdde8c3 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -44,12 +44,6 @@ impl fmt::Display for ImportError { } } -// Deprecated -pub fn panic_imports(expr: &Expr) -> Expr { - let no_import = |i: &Import| -> X { panic!("ahhh import: {:?}", i) }; - expr.map_embed(&no_import) -} - /// A root from which to resolve relative imports. #[derive(Debug, Clone, PartialEq, Eq)] pub enum ImportRoot { @@ -121,7 +115,7 @@ impl Parsed { pub fn resolve(self) -> Result { crate::imports::resolve_expr(self, true) } - pub fn resolve_no_imports(self) -> Result { + pub fn skip_resolve(self) -> Result { crate::imports::resolve_expr(self, false) } } @@ -135,10 +129,3 @@ pub fn load_dhall_file( let expr = resolve_expr(expr, resolve_imports)?; Ok(expr.0.unroll()) } - -// Deprecated -pub fn load_dhall_file_no_resolve_imports( - f: &Path, -) -> Result { - Ok(Parsed::load_from_file(f)?.0) -} diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 28b43ba..f00e5b6 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -7,16 +7,14 @@ clippy::many_single_char_names )] -mod normalize; -pub use crate::normalize::*; mod binary; -pub mod imports; -mod traits; +mod imports; +mod normalize; +pub mod traits; pub mod typecheck; -pub use crate::imports::*; -pub use crate::traits::*; +pub use crate::imports::{load_dhall_file, ImportError}; +pub use crate::traits::StaticType; pub use dhall_generator::expr; pub use dhall_generator::subexpr; pub use dhall_generator::StaticType; pub mod expr; -pub use crate::expr::*; diff --git a/dhall/src/main.rs b/dhall/src/main.rs index 77f558c..2881d5a 100644 --- a/dhall/src/main.rs +++ b/dhall/src/main.rs @@ -2,9 +2,6 @@ use std::error::Error; use std::io::{self, Read}; use term_painter::ToStyle; -use dhall::*; -use dhall_core::*; - const ERROR_STYLE: term_painter::Color = term_painter::Color::Red; const BOLD: term_painter::Attr = term_painter::Attr::Bold; @@ -57,17 +54,19 @@ fn print_error(message: &str, source: &str, start: usize, end: usize) { fn main() { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).unwrap(); - let expr = match parse_expr(&buffer) { - Ok(e) => e, + + let expr = match dhall::expr::Parsed::load_from_str(&buffer) { + Ok(expr) => expr, Err(e) => { print_error(&format!("Parse error {}", e), &buffer, 0, 0); return; } }; - let expr: SubExpr<_, _> = rc(imports::panic_imports(expr.as_ref())); + let expr = expr.resolve().unwrap(); - let type_expr = match typecheck::type_of(expr.clone()) { + let expr = match expr.typecheck() { + Ok(expr) => expr, Err(e) => { let explain = ::std::env::args().any(|s| s == "--explain"); if !explain { @@ -84,10 +83,9 @@ fn main() { // FIXME Print source position return; } - Ok(type_expr) => type_expr, }; - println!("{}", type_expr); - println!(); - println!("{}", normalize(expr)); + let expr = expr.normalize(); + + println!("{}", expr); } diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index f9633fb..c07d3cb 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -8,6 +8,10 @@ impl Typed { pub fn normalize(self) -> Normalized { Normalized(normalize(self.0), self.1) } + /// Pretends this expression is normalized. Use with care. + pub fn skip_normalize(self) -> Normalized { + Normalized(self.0, self.1) + } } fn apply_builtin(b: Builtin, args: &Vec>) -> WhatNext @@ -209,7 +213,7 @@ enum WhatNext<'a, S, A> { DoneAsIs, } -pub fn normalize_ref(expr: &Expr) -> Expr +fn normalize_ref(expr: &Expr) -> Expr where S: fmt::Debug + Clone, A: fmt::Debug + Clone, @@ -313,7 +317,7 @@ where /// However, `normalize` will not fail if the expression is ill-typed and will /// leave ill-typed sub-expressions unevaluated. /// -pub fn normalize(e: SubExpr) -> SubExpr +fn normalize(e: SubExpr) -> SubExpr where S: fmt::Debug + Clone, A: fmt::Debug + Clone, diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index a0782f8..babfad0 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -11,8 +11,11 @@ use self::TypeMessage::*; impl Resolved { pub fn typecheck(self) -> Result> { - let typ = crate::typecheck::type_of_(self.0.clone())?; - Ok(typ) + type_of_(self.0.clone()) + } + /// Pretends this expression has been typechecked. Use with care. + pub fn skip_typecheck(self) -> Typed { + Typed(self.0, UNTYPE) } } impl Typed { @@ -63,7 +66,7 @@ impl Normalized { } impl Type { #[inline(always)] - fn as_normalized(&self) -> Result<&Normalized, TypeError> { + pub fn as_normalized(&self) -> Result<&Normalized, TypeError> { use TypeInternal::*; match &self.0 { Expr(e) => Ok(e), diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index fc5aa5b..5f16d2c 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -44,20 +44,21 @@ pub enum Feature { TypeInferenceFailure, } +// Deprecated fn read_dhall_file<'i>(file_path: &str) -> Result, ImportError> { - load_dhall_file(&PathBuf::from(file_path), true) + dhall::load_dhall_file(&PathBuf::from(file_path), true) } fn load_from_file_str<'i>( file_path: &str, -) -> Result { - Parsed::load_from_file(&PathBuf::from(file_path)) +) -> Result { + dhall::expr::Parsed::load_from_file(&PathBuf::from(file_path)) } fn load_from_binary_file_str<'i>( file_path: &str, -) -> Result { - Parsed::load_from_binary_file(&PathBuf::from(file_path)) +) -> Result { + dhall::expr::Parsed::load_from_binary_file(&PathBuf::from(file_path)) } pub fn run_test(base_path: &str, feature: Feature) { @@ -88,7 +89,8 @@ pub fn run_test(base_path: &str, feature: Feature) { assert_eq_pretty!(expr, expected); // Round-trip pretty-printer - let expr = Parsed::load_from_str(&expr.to_string()).unwrap(); + let expr = + dhall::expr::Parsed::load_from_str(&expr.to_string()).unwrap(); assert_eq!(expr, expected); } ParserFailure => { @@ -102,15 +104,29 @@ pub fn run_test(base_path: &str, feature: Feature) { Normalization => { let expr_file_path = base_path.clone() + "A.dhall"; let expected_file_path = base_path + "B.dhall"; - let expr = rc(read_dhall_file(&expr_file_path).unwrap()); - let expected = rc(read_dhall_file(&expected_file_path).unwrap()); + let expr = load_from_file_str(&expr_file_path) + .unwrap() + .resolve() + .unwrap() + .skip_typecheck() + .normalize(); + let expected = load_from_file_str(&expected_file_path) + .unwrap() + .resolve() + .unwrap() + .skip_typecheck() + .normalize(); - assert_eq_display!(normalize(expr), normalize(expected)); + assert_eq_display!(expr, expected); } TypecheckFailure => { let file_path = base_path + ".dhall"; - let expr = rc(read_dhall_file(&file_path).unwrap()); - typecheck::type_of(expr).unwrap_err(); + load_from_file_str(&file_path) + .unwrap() + .skip_resolve() + .unwrap() + .typecheck() + .unwrap_err(); } TypecheckSuccess => { // Many tests stack overflow in debug mode @@ -131,15 +147,30 @@ pub fn run_test(base_path: &str, feature: Feature) { } TypeInferenceFailure => { let file_path = base_path + ".dhall"; - let expr = rc(read_dhall_file(&file_path).unwrap()); - typecheck::type_of(expr).unwrap_err(); + load_from_file_str(&file_path) + .unwrap() + .skip_resolve() + .unwrap() + .typecheck() + .unwrap_err(); } TypeInferenceSuccess => { let expr_file_path = base_path.clone() + "A.dhall"; let expected_file_path = base_path + "B.dhall"; - let expr = rc(read_dhall_file(&expr_file_path).unwrap()); - let expected = rc(read_dhall_file(&expected_file_path).unwrap()); - assert_eq_display!(typecheck::type_of(expr).unwrap(), expected); + let expr = load_from_file_str(&expr_file_path) + .unwrap() + .skip_resolve() + .unwrap() + .typecheck() + .unwrap(); + let ty = expr.get_type().as_normalized().unwrap(); + let expected = load_from_file_str(&expected_file_path) + .unwrap() + .skip_resolve() + .unwrap() + .skip_typecheck() + .skip_normalize(); + assert_eq_display!(ty, &expected); } } } -- cgit v1.3.1