diff options
author | Nadrieril | 2019-03-08 22:46:39 +0100 |
---|---|---|
committer | Nadrieril | 2019-03-08 22:46:39 +0100 |
commit | d3f4a32d1e3d39c8d42306e5ca5ad4bb256edcd8 (patch) | |
tree | 113e9a9aaac158a5c0d110b3ce93c2a531957c27 | |
parent | 66bed8dbc7249e17a89adcbb19406f4126a434de (diff) |
Rename Expr back to its true name
Diffstat (limited to '')
-rw-r--r-- | dhall/src/imports.rs | 6 | ||||
-rw-r--r-- | dhall/src/lib.rs | 2 | ||||
-rw-r--r-- | dhall/src/main.rs | 2 | ||||
-rw-r--r-- | dhall/src/normalize.rs | 10 | ||||
-rw-r--r-- | dhall/src/typecheck.rs | 146 | ||||
-rw-r--r-- | dhall/tests/macros.rs | 2 | ||||
-rw-r--r-- | dhall_core/src/core.rs | 175 | ||||
-rw-r--r-- | dhall_core/src/grammar_util.rs | 2 | ||||
-rw-r--r-- | dhall_core/src/parser.rs | 76 | ||||
-rw-r--r-- | dhall_generator/src/lib.rs | 12 |
10 files changed, 216 insertions, 217 deletions
diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 4240b5e..ad0ae0f 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -1,11 +1,11 @@ // use dhall_core::{Expr, FilePrefix, Import, ImportLocation, ImportMode, X}; -use dhall_core::{Expr_, StringLike, Import, X}; +use dhall_core::{Expr, StringLike, Import, X}; // use std::path::Path; // use std::path::PathBuf; pub fn resolve_imports<Label: StringLike, S: Clone>( - expr: &Expr_<Label, S, Import>, -) -> Expr_<Label, S, X> { + expr: &Expr<Label, S, Import>, +) -> Expr<Label, S, X> { let no_import = |_: &Import| -> X { panic!("ahhh import") }; expr.map_embed(&no_import) } diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 66d132e..95f7f6f 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -44,7 +44,7 @@ pub fn load_dhall_file<'i, 'a: 'i>( f: &Path, source_pool: &'a mut Vec<String>, _resolve_imports: bool, -) -> Result<Expr_<String, X, X>, DhallError> { +) -> Result<Expr<String, X, X>, DhallError> { source_pool.push(String::new()); let mut buffer = source_pool.last_mut().unwrap(); File::open(f)?.read_to_string(&mut buffer)?; diff --git a/dhall/src/main.rs b/dhall/src/main.rs index b571996..23c8108 100644 --- a/dhall/src/main.rs +++ b/dhall/src/main.rs @@ -65,7 +65,7 @@ fn main() { } }; - let expr: Expr_<String, _, _> = imports::resolve_imports(&expr.take_ownership_of_labels()); + let expr: Expr<String, _, _> = imports::resolve_imports(&expr.take_ownership_of_labels()); let type_expr = match typecheck::type_of(&expr) { Err(e) => { diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 4f07d9a..3b8099a 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -13,8 +13,8 @@ use std::fmt; /// leave ill-typed sub-expressions unevaluated. /// pub fn normalize<Label: StringLike, S, T, A>( - e: &Expr_<Label, S, A>, -) -> Expr_<Label, T, A> + e: &Expr<Label, S, A>, +) -> Expr<Label, T, A> where S: Clone + fmt::Debug, T: Clone + fmt::Debug, @@ -22,7 +22,7 @@ where { use dhall_core::BinOp::*; use dhall_core::Builtin::*; - use dhall_core::Expr_::*; + use dhall_core::Expr::*; match e { // Matches that don't normalize everything right away Let(f, _, r, b) => { @@ -96,7 +96,7 @@ where // normalize(&dhall!(k (List a0) (λ(a : a0) -> λ(as : List a1) -> [ a ] # as) ([] : List a0))) // } (App(box App(box App(box App(box Builtin(ListFold), _), box ListLit(_, xs)), _), cons), nil) => { - let e2: Expr_<_, _, _> = xs.into_iter().rev().fold(nil, |y, ys| { + let e2: Expr<_, _, _> = xs.into_iter().rev().fold(nil, |y, ys| { let y = bx(y); let ys = bx(ys); dhall!(cons y ys) @@ -133,7 +133,7 @@ where ] */ (App(box App(box App(box App(box Builtin(OptionalFold), _), box OptionalLit(_, xs)), _), just), nothing) => { - let e2: Expr_<_, _, _> = xs.into_iter().fold(nothing, |y, _| { + let e2: Expr<_, _, _> = xs.into_iter().fold(nothing, |y, _| { let y = bx(y); dhall!(just y) }); diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index 1c15d88..b5bdee8 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -8,9 +8,9 @@ use dhall_core::context::Context; use dhall_core::core; use dhall_core::core::Builtin::*; use dhall_core::core::Const::*; -use dhall_core::core::Expr_::*; +use dhall_core::core::Expr::*; use dhall_core::core::{app, pi}; -use dhall_core::core::{bx, shift, subst, Expr, Expr_, V, X, StringLike}; +use dhall_core::core::{bx, shift, subst, Expr, V, X, StringLike}; use self::TypeMessage::*; @@ -48,15 +48,15 @@ fn match_vars<L: Clone + Eq>(vl: &V<L>, vr: &V<L>, ctx: &[(L, L)]) -> bool { } } -fn prop_equal<L: StringLike, S, T>(eL0: &Expr_<L, S, X>, eR0: &Expr_<L, T, X>) -> bool +fn prop_equal<L: StringLike, S, T>(eL0: &Expr<L, S, X>, eR0: &Expr<L, T, X>) -> bool where S: Clone + ::std::fmt::Debug, T: Clone + ::std::fmt::Debug, { fn go<L: StringLike, S, T>( ctx: &mut Vec<(L, L)>, - el: &Expr_<L, S, X>, - er: &Expr_<L, T, X>, + el: &Expr<L, S, X>, + er: &Expr<L, T, X>, ) -> bool where S: Clone + ::std::fmt::Debug, @@ -146,16 +146,16 @@ where } fn op2_type<Label: StringLike + From<String>, S, EF>( - ctx: &Context<Label, Expr_<Label, S, X>>, - e: &Expr_<Label, S, X>, + ctx: &Context<Label, Expr<Label, S, X>>, + e: &Expr<Label, S, X>, t: core::Builtin, ef: EF, - l: &Expr_<Label, S, X>, - r: &Expr_<Label, S, X>, -) -> Result<Expr_<Label, S, X>, TypeError<Label, S>> + l: &Expr<Label, S, X>, + r: &Expr<Label, S, X>, +) -> Result<Expr<Label, S, X>, TypeError<Label, S>> where S: Clone + ::std::fmt::Debug, - EF: FnOnce(Expr_<Label, S, X>, Expr_<Label, S, X>) -> TypeMessage<Label, S>, + EF: FnOnce(Expr<Label, S, X>, Expr<Label, S, X>) -> TypeMessage<Label, S>, { let tl = normalize(&type_with(ctx, l)?); match tl { @@ -179,14 +179,14 @@ where /// is not necessary for just type-checking. If you actually care about the /// returned type then you may want to `normalize` it afterwards. pub fn type_with<Label: StringLike + From<String>, S>( - ctx: &Context<Label, Expr_<Label, S, X>>, - e: &Expr_<Label, S, X>, -) -> Result<Expr_<Label, S, X>, TypeError<Label, S>> + ctx: &Context<Label, Expr<Label, S, X>>, + e: &Expr<Label, S, X>, +) -> Result<Expr<Label, S, X>, TypeError<Label, S>> where S: Clone + ::std::fmt::Debug, { use dhall_core::BinOp::*; - use dhall_core::Expr_; + use dhall_core::Expr; match *e { Const(c) => axiom(c).map(Const), //.map(Cow::Owned), Var(V(ref x, n)) => { @@ -418,7 +418,7 @@ where } ListLit(ref t, ref xs) => { let mut iter = xs.iter().enumerate(); - let t: Box<Expr_<_, _, _>> = match t { + let t: Box<Expr<_, _, _>> = match t { Some(t) => t.clone(), None => { let (_, first_x) = iter.next().unwrap(); @@ -488,14 +488,14 @@ where pi("_", app(List, "a"), app(Optional, "a")), ).take_ownership_of_labels()), Builtin(ListIndexed) => { - let mut m: BTreeMap<Label, Expr_<Label, _, _>> = BTreeMap::new(); + let mut m: BTreeMap<Label, Expr<Label, _, _>> = BTreeMap::new(); m.insert("index".to_owned().into(), Builtin(Natural)); - let var: Expr_<Label, _, _> = Var(V(Label::from("a".to_owned()), 0)); + let var: Expr<Label, _, _> = Var(V(Label::from("a".to_owned()), 0)); m.insert("value".to_owned().into(), var.clone()); let underscore: Label = Label::from("_".to_owned()); - let innerinner: Expr_<Label, _, _> = app(List, Record(m)); - let innerinner2: Expr_<Label, _, _> = app(List, var); - let inner: Expr_<Label, _, _> = Pi(underscore, bx(innerinner2), bx(innerinner)); + let innerinner: Expr<Label, _, _> = app(List, Record(m)); + let innerinner2: Expr<Label, _, _> = app(List, var); + let inner: Expr<Label, _, _> = Pi(underscore, bx(innerinner2), bx(innerinner)); Ok(Pi( Label::from("a".to_owned()), bx(Const(Type)), @@ -509,7 +509,7 @@ where ).take_ownership_of_labels()), OptionalLit(ref t, ref xs) => { let mut iter = xs.iter(); - let t: Box<Expr_<_, _, _>> = match t { + let t: Box<Expr<_, _, _>> = match t { Some(t) => t.clone(), None => { let first_x = iter.next().unwrap(); @@ -714,8 +714,8 @@ where /// expression must be closed (i.e. no free variables), otherwise type-checking /// will fail. pub fn type_of<Label: StringLike + From<String>, S: Clone + ::std::fmt::Debug>( - e: &Expr_<Label, S, X>, -) -> Result<Expr_<Label, S, X>, TypeError<Label, S>> { + e: &Expr<Label, S, X>, +) -> Result<Expr<Label, S, X>, TypeError<Label, S>> { let ctx = Context::new(); type_with(&ctx, e) //.map(|e| e.into_owned()) } @@ -724,83 +724,83 @@ pub fn type_of<Label: StringLike + From<String>, S: Clone + ::std::fmt::Debug>( #[derive(Debug)] pub enum TypeMessage<Label: std::hash::Hash + Eq, S> { UnboundVariable, - InvalidInputType(Expr_<Label, S, X>), - InvalidOutputType(Expr_<Label, S, X>), - NotAFunction(Expr_<Label, S, X>, Expr_<Label, S, X>), + InvalidInputType(Expr<Label, S, X>), + InvalidOutputType(Expr<Label, S, X>), + NotAFunction(Expr<Label, S, X>, Expr<Label, S, X>), TypeMismatch( - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, ), - AnnotMismatch(Expr_<Label, S, X>, Expr_<Label, S, X>, Expr_<Label, S, X>), + AnnotMismatch(Expr<Label, S, X>, Expr<Label, S, X>, Expr<Label, S, X>), Untyped, InvalidListElement( usize, - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, ), - InvalidListType(Expr_<Label, S, X>), + InvalidListType(Expr<Label, S, X>), InvalidOptionalElement( - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, ), InvalidOptionalLiteral(usize), - InvalidOptionalType(Expr_<Label, S, X>), - InvalidPredicate(Expr_<Label, S, X>, Expr_<Label, S, X>), + InvalidOptionalType(Expr<Label, S, X>), + InvalidPredicate(Expr<Label, S, X>, Expr<Label, S, X>), IfBranchMismatch( - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, ), IfBranchMustBeTerm( bool, - Expr_<Label, S, X>, - Expr_<Label, S, X>, - Expr_<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, + Expr<Label, S, X>, ), - InvalidField(Label, Expr_<Label, S, X>), - InvalidFieldType(Label, Expr_<Label, S, X>), - InvalidAlternative(Label, Expr_<Label, S, X>), - InvalidAlternativeType(Label, Expr_<Label, S, X>), + InvalidField(Label, Expr<Label, S, X>), + InvalidFieldType(Label, Expr<Label, S, X>), + InvalidAlternative(Label, Expr<Label, S, X>), + InvalidAlternativeType(Label, Expr<Label, S, X>), DuplicateAlternative(Label), - MustCombineARecord(Expr_<Label, S, X>, Expr_<Label, S, X>), + MustCombineARecord(Expr<Label, S, X>, Expr<Label, S, X>), FieldCollision(Label), - MustMergeARecord(Expr_<Label, S, X>, Expr_<Label, S, X>), - MustMergeUnion(Expr_<Label, S, X>, Expr_<Label, S, X>), + MustMergeARecord(Expr<Label, S, X>, Expr<Label, S, X>), + MustMergeUnion(Expr<Label, S, X>, Expr<Label, S, X>), UnusedHandler(HashSet<Label>), MissingHandler(HashSet<Label>), - HandlerInputTypeMismatch(Label, Expr_<Label, S, X>, Expr_<Label, S, X>), - HandlerOutputTypeMismatch(Label, Expr_<Label, S, X>, Expr_<Label, S, X>), - HandlerNotAFunction(Label, Expr_<Label, S, X>), - NotARecord(Label, Expr_<Label, S, X>, Expr_<Label, S, X>), - MissingField(Label, Expr_<Label, S, X>), - CantAnd(Expr_<Label, S, X>, Expr_<Label, S, X>), - CantOr(Expr_<Label, S, X>, Expr_<Label, S, X>), - CantEQ(Expr_<Label, S, X>, Expr_<Label, S, X>), - CantNE(Expr_<Label, S, X>, Expr_<Label, S, X>), - CantTextAppend(Expr_<Label, S, X>, Expr_<Label, S, X>), - CantAdd(Expr_<Label, S, X>, Expr_<Label, S, X>), - CantMultiply(Expr_<Label, S, X>, Expr_<Label, S, X>), - NoDependentLet(Expr_<Label, S, X>, Expr_<Label, S, X>), - NoDependentTypes(Expr_<Label, S, X>, Expr_<Label, S, X>), + HandlerInputTypeMismatch(Label, Expr<Label, S, X>, Expr<Label, S, X>), + HandlerOutputTypeMismatch(Label, Expr<Label, S, X>, Expr<Label, S, X>), + HandlerNotAFunction(Label, Expr<Label, S, X>), + NotARecord(Label, Expr<Label, S, X>, Expr<Label, S, X>), + MissingField(Label, Expr<Label, S, X>), + CantAnd(Expr<Label, S, X>, Expr<Label, S, X>), + CantOr(Expr<Label, S, X>, Expr<Label, S, X>), + CantEQ(Expr<Label, S, X>, Expr<Label, S, X>), + CantNE(Expr<Label, S, X>, Expr<Label, S, X>), + CantTextAppend(Expr<Label, S, X>, Expr<Label, S, X>), + CantAdd(Expr<Label, S, X>, Expr<Label, S, X>), + CantMultiply(Expr<Label, S, X>, Expr<Label, S, X>), + NoDependentLet(Expr<Label, S, X>, Expr<Label, S, X>), + NoDependentTypes(Expr<Label, S, X>, Expr<Label, S, X>), } /// A structured type error that includes context #[derive(Debug)] pub struct TypeError<Label: std::hash::Hash + Eq, S> { - pub context: Context<Label, Expr_<Label, S, X>>, - pub current: Expr_<Label, S, X>, + pub context: Context<Label, Expr<Label, S, X>>, + pub current: Expr<Label, S, X>, pub type_message: TypeMessage<Label, S>, } impl<Label: StringLike, S: Clone> TypeError<Label, S> { pub fn new( - context: &Context<Label, Expr_<Label, S, X>>, - current: &Expr_<Label, S, X>, + context: &Context<Label, Expr<Label, S, X>>, + current: &Expr<Label, S, X>, type_message: TypeMessage<Label, S>, ) -> Self { TypeError { diff --git a/dhall/tests/macros.rs b/dhall/tests/macros.rs index 619742e..83510a3 100644 --- a/dhall/tests/macros.rs +++ b/dhall/tests/macros.rs @@ -80,7 +80,7 @@ pub enum ExpectedResult { pub fn read_dhall_file<'i>( file_path: &str, mut buffer: &'i mut String, -) -> Result<Box<Expr_<String, X, Import>>, ParseError> { +) -> Result<Box<Expr<String, X, Import>>, ParseError> { let mut file = File::open(&file_path).unwrap(); file.read_to_string(&mut buffer).unwrap(); let expr = parser::parse_expr(&*buffer)?; diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 2465629..04c3961 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -136,9 +136,8 @@ pub enum BinOp { } /// Syntax tree for expressions -pub type Expr<'i, S, A> = Expr_<&'i str, S, A>; #[derive(Debug, Clone, PartialEq)] -pub enum Expr_<Label, Note, Embed> { +pub enum Expr<Label, Note, Embed> { /// `Const c ~ c` Const(Const), /// `Var (V x 0) ~ x`<br> @@ -147,49 +146,49 @@ pub enum Expr_<Label, Note, Embed> { /// `Lam x A b ~ λ(x : A) -> b` Lam( Label, - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// `Pi "_" A B ~ A -> B` /// `Pi x A B ~ ∀(x : A) -> B` Pi( Label, - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// `App f A ~ f A` App( - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// `Let x Nothing r e ~ let x = r in e` /// `Let x (Just t) r e ~ let x : t = r in e` Let( Label, - Option<Box<Expr_<Label, Note, Embed>>>, - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Option<Box<Expr<Label, Note, Embed>>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// `Annot x t ~ x : t` Annot( - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// Built-in values Builtin(Builtin), // Binary operations BinOp( BinOp, - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// `BoolLit b ~ b` BoolLit(bool), /// `BoolIf x y z ~ if x then y else z` BoolIf( - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, ), /// `NaturalLit n ~ +n` NaturalLit(Natural), @@ -201,37 +200,37 @@ pub enum Expr_<Label, Note, Embed> { TextLit(Builder), /// `ListLit t [x, y, z] ~ [x, y, z] : List t` ListLit( - Option<Box<Expr_<Label, Note, Embed>>>, - Vec<Expr_<Label, Note, Embed>>, + Option<Box<Expr<Label, Note, Embed>>>, + Vec<Expr<Label, Note, Embed>>, ), /// `OptionalLit t [e] ~ [e] : Optional t` /// `OptionalLit t [] ~ [] : Optional t` OptionalLit( - Option<Box<Expr_<Label, Note, Embed>>>, - Vec<Expr_<Label, Note, Embed>>, + Option<Box<Expr<Label, Note, Embed>>>, + Vec<Expr<Label, Note, Embed>>, ), /// `Record [(k1, t1), (k2, t2)] ~ { k1 : t1, k2 : t1 }` - Record(BTreeMap<Label, Expr_<Label, Note, Embed>>), + Record(BTreeMap<Label, Expr<Label, Note, Embed>>), /// `RecordLit [(k1, v1), (k2, v2)] ~ { k1 = v1, k2 = v2 }` - RecordLit(BTreeMap<Label, Expr_<Label, Note, Embed>>), + RecordLit(BTreeMap<Label, Expr<Label, Note, Embed>>), /// `Union [(k1, t1), (k2, t2)] ~ < k1 : t1, k2 : t2 >` - Union(BTreeMap<Label, Expr_<Label, Note, Embed>>), + Union(BTreeMap<Label, Expr<Label, Note, Embed>>), /// `UnionLit (k1, v1) [(k2, t2), (k3, t3)] ~ < k1 = t1, k2 : t2, k3 : t3 >` UnionLit( Label, - Box<Expr_<Label, Note, Embed>>, - BTreeMap<Label, Expr_<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + BTreeMap<Label, Expr<Label, Note, Embed>>, ), /// `Merge x y t ~ merge x y : t` Merge( - Box<Expr_<Label, Note, Embed>>, - Box<Expr_<Label, Note, Embed>>, - Option<Box<Expr_<Label, Note, Embed>>>, + Box<Expr<Label, Note, Embed>>, + Box<Expr<Label, Note, Embed>>, + Option<Box<Expr<Label, Note, Embed>>>, ), /// `Field e x ~ e.x` - Field(Box<Expr_<Label, Note, Embed>>, Label), + Field(Box<Expr<Label, Note, Embed>>, Label), /// Annotation on the AST. Unused for now but could hold e.g. file location information - Note(Note, Box<Expr_<Label, Note, Embed>>), + Note(Note, Box<Expr<Label, Note, Embed>>), /// Embeds an import or the result of resolving the import Embed(Embed), } @@ -291,32 +290,32 @@ impl<Label> From<Label> for V<Label> { } } -impl<'i, S, A> From<&'i str> for Expr<'i, S, A> { +impl<'i, S, A> From<&'i str> for Expr<&'i str, S, A> { fn from(s: &'i str) -> Self { - Expr_::Var(s.into()) + Expr::Var(s.into()) } } -impl<L, S, A> From<Builtin> for Expr_<L, S, A> { +impl<L, S, A> From<Builtin> for Expr<L, S, A> { fn from(t: Builtin) -> Self { - Expr_::Builtin(t) + Expr::Builtin(t) } } -impl<Label: StringLike, S, A> Expr_<Label, S, A> { +impl<Label: StringLike, S, A> Expr<Label, S, A> { pub fn map_shallow<T, B, Label2, F1, F2, F3, F4>( &self, map_expr: F1, map_note: F2, map_embed: F3, map_label: F4, - ) -> Expr_<Label2, T, B> + ) -> Expr<Label2, T, B> where A: Clone, T: Clone, S: Clone, Label2: StringLike, - F1: Fn(&Self) -> Expr_<Label2, T, B>, + F1: Fn(&Self) -> Expr<Label2, T, B>, F2: FnOnce(&S) -> T, F3: FnOnce(&A) -> B, F4: Fn(&Label) -> Label2, @@ -324,13 +323,13 @@ impl<Label: StringLike, S, A> Expr_<Label, S, A> { map_shallow(self, map_expr, map_note, map_embed, map_label) } - pub fn map_embed<B, F>(&self, map_embed: &F) -> Expr_<Label, S, B> + pub fn map_embed<B, F>(&self, map_embed: &F) -> Expr<Label, S, B> where A: Clone, S: Clone, F: Fn(&A) -> B, { - let recurse = |e: &Expr_<Label, S, A>| -> Expr_<Label, S, B> { + let recurse = |e: &Expr<Label, S, A>| -> Expr<Label, S, B> { e.map_embed(map_embed) }; self.map_shallow(recurse, |x| x.clone(), map_embed, |x| x.clone()) @@ -338,30 +337,30 @@ impl<Label: StringLike, S, A> Expr_<Label, S, A> { pub fn bool_lit(&self) -> Option<bool> { match *self { - Expr_::BoolLit(v) => Some(v), + Expr::BoolLit(v) => Some(v), _ => None, } } pub fn natural_lit(&self) -> Option<usize> { match *self { - Expr_::NaturalLit(v) => Some(v), + Expr::NaturalLit(v) => Some(v), _ => None, } } pub fn text_lit(&self) -> Option<String> { match *self { - Expr_::TextLit(ref t) => Some(t.clone()), // FIXME? + Expr::TextLit(ref t) => Some(t.clone()), // FIXME? _ => None, } } } -impl<'i, S: Clone, A: Clone> Expr_<&'i str, S, A> { - pub fn take_ownership_of_labels<L: StringLike + From<String>>(&self) -> Expr_<L, S, A> { +impl<'i, S: Clone, A: Clone> Expr<&'i str, S, A> { + pub fn take_ownership_of_labels<L: StringLike + From<String>>(&self) -> Expr<L, S, A> { let recurse = - |e: &Expr_<&'i str, S, A>| -> Expr_<L, S, A> { e.take_ownership_of_labels() }; + |e: &Expr<&'i str, S, A>| -> Expr<L, S, A> { e.take_ownership_of_labels() }; map_shallow(self, recurse, |x| x.clone(), |x| x.clone(), |x: &&str| -> L { (*x).to_owned().into() }) } } @@ -376,10 +375,10 @@ impl<'i, S: Clone, A: Clone> Expr_<&'i str, S, A> { // you add a new constructor to the syntax tree without adding a matching // case the corresponding builder. -impl<Label: Display, S, A: Display> Display for Expr_<Label, S, A> { +impl<Label: Display, S, A: Display> Display for Expr<Label, S, A> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { // buildExprA - use crate::Expr_::*; + use crate::Expr::*; match self { &Annot(ref a, ref b) => { a.fmt_b(f)?; @@ -392,9 +391,9 @@ impl<Label: Display, S, A: Display> Display for Expr_<Label, S, A> { } } -impl<Label: Display, S, A: Display> Expr_<Label, S, A> { +impl<Label: Display, S, A: Display> Expr<Label, S, A> { fn fmt_b(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::Expr_::*; + use crate::Expr::*; match self { &Lam(ref a, ref b, ref c) => { write!(f, "λ({} : ", a)?; @@ -479,7 +478,7 @@ impl<Label: Display, S, A: Display> Expr_<Label, S, A> { fn fmt_c(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use crate::BinOp::*; - use crate::Expr_::*; + use crate::Expr::*; match self { // FIXME precedence &BinOp(BoolOr, ref a, ref b) => { @@ -528,7 +527,7 @@ impl<Label: Display, S, A: Display> Expr_<Label, S, A> { } fn fmt_d(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::Expr_::*; + use crate::Expr::*; match self { &App(ref a, ref b) => { a.fmt_d(f)?; @@ -541,7 +540,7 @@ impl<Label: Display, S, A: Display> Expr_<Label, S, A> { } fn fmt_e(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::Expr_::*; + use crate::Expr::*; match self { &Field(ref a, ref b) => { a.fmt_e(f)?; @@ -553,7 +552,7 @@ impl<Label: Display, S, A: Display> Expr_<Label, S, A> { } fn fmt_f(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::Expr_::*; + use crate::Expr::*; match &self { &Var(a) => a.fmt(f), &Const(k) => k.fmt(f), @@ -703,21 +702,21 @@ pub fn pi<Label, S, A, Name, Et, Ev>( var: Name, ty: Et, value: Ev, -) -> Expr_<Label, S, A> +) -> Expr<Label, S, A> where Name: Into<Label>, - Et: Into<Expr_<Label, S, A>>, - Ev: Into<Expr_<Label, S, A>>, + Et: Into<Expr<Label, S, A>>, + Ev: Into<Expr<Label, S, A>>, { - Expr_::Pi(var.into(), bx(ty.into()), bx(value.into())) + Expr::Pi(var.into(), bx(ty.into()), bx(value.into())) } -pub fn app<Label, S, A, Ef, Ex>(f: Ef, x: Ex) -> Expr_<Label, S, A> +pub fn app<Label, S, A, Ef, Ex>(f: Ef, x: Ex) -> Expr<Label, S, A> where - Ef: Into<Expr_<Label, S, A>>, - Ex: Into<Expr_<Label, S, A>>, + Ef: Into<Expr<Label, S, A>>, + Ex: Into<Expr<Label, S, A>>, { - Expr_::App(bx(f.into()), bx(x.into())) + Expr::App(bx(f.into()), bx(x.into())) } pub type Builder = String; @@ -749,26 +748,26 @@ fn add_ui(u: usize, i: isize) -> usize { } pub fn map_shallow<Label1, Label2, S, T, A, B, F1, F2, F3, F4>( - e: &Expr_<Label1, S, A>, + e: &Expr<Label1, S, A>, map: F1, map_note: F2, map_embed: F3, map_label: F4, -) -> Expr_<Label2, T, B> +) -> Expr<Label2, T, B> where A: Clone, S: Clone, T: Clone, Label1: Display + fmt::Debug + Clone + Hash + Ord + Eq + Into<String> + Default, Label2: StringLike, - F1: Fn(&Expr_<Label1, S, A>) -> Expr_<Label2, T, B>, + F1: Fn(&Expr<Label1, S, A>) -> Expr<Label2, T, B>, F2: FnOnce(&S) -> T, F3: FnOnce(&A) -> B, F4: Fn(&Label1) -> Label2, { - use crate::Expr_::*; + use crate::Expr::*; let bxmap = - |x: &Expr_<Label1, S, A>| -> Box<Expr_<Label2, T, B>> { bx(map(x)) }; + |x: &Expr<Label1, S, A>| -> Box<Expr<Label2, T, B>> { bx(map(x)) }; let opt = |x| map_opt_box(x, &map); match *e { Const(k) => Const(k), @@ -930,12 +929,12 @@ where pub fn shift<Label, S, T, A: Clone>( d: isize, v: &V<Label>, - e: &Expr_<Label, S, A>, -) -> Expr_<Label, T, A> + e: &Expr<Label, S, A>, +) -> Expr<Label, T, A> where Label: StringLike, { - use crate::Expr_::*; + use crate::Expr::*; let V(x, n) = v; match e { Const(a) => Const(*a), @@ -1013,14 +1012,14 @@ fn shift_op2<Label, S, T, A, F>( f: F, d: isize, v: &V<Label>, - a: &Expr_<Label, S, A>, - b: &Expr_<Label, S, A>, -) -> Expr_<Label, T, A> + a: &Expr<Label, S, A>, + b: &Expr<Label, S, A>, +) -> Expr<Label, T, A> where F: FnOnce( - Box<Expr_<Label, T, A>>, - Box<Expr_<Label, T, A>>, - ) -> Expr_<Label, T, A>, + Box<Expr<Label, T, A>>, + Box<Expr<Label, T, A>>, + ) -> Expr<Label, T, A>, A: Clone, Label: StringLike, { @@ -1035,14 +1034,14 @@ where /// pub fn subst<Label: StringLike, S, T, A>( v: &V<Label>, - e: &Expr_<Label, S, A>, - b: &Expr_<Label, T, A>, -) -> Expr_<Label, S, A> + e: &Expr<Label, S, A>, + b: &Expr<Label, T, A>, +) -> Expr<Label, S, A> where S: Clone, A: Clone, { - use crate::Expr_::*; + use crate::Expr::*; let V(x, n) = v; match b { Const(a) => Const(*a), @@ -1122,15 +1121,15 @@ where fn subst_op2<Label: StringLike, S, T, A, F>( f: F, v: &V<Label>, - e: &Expr_<Label, S, A>, - a: &Expr_<Label, T, A>, - b: &Expr_<Label, T, A>, -) -> Expr_<Label, S, A> + e: &Expr<Label, S, A>, + a: &Expr<Label, T, A>, + b: &Expr<Label, T, A>, +) -> Expr<Label, S, A> where F: FnOnce( - Box<Expr_<Label, S, A>>, - Box<Expr_<Label, S, A>>, - ) -> Expr_<Label, S, A>, + Box<Expr<Label, S, A>>, + Box<Expr<Label, S, A>>, + ) -> Expr<Label, S, A>, S: Clone, A: Clone, { diff --git a/dhall_core/src/grammar_util.rs b/dhall_core/src/grammar_util.rs index 73f935f..2547e94 100644 --- a/dhall_core/src/grammar_util.rs +++ b/dhall_core/src/grammar_util.rs @@ -1,4 +1,4 @@ use crate::core::{Expr, Import, X}; -pub type ParsedExpr<'i> = Expr<'i, X, Import>; +pub type ParsedExpr<'i> = Expr<&'i str, X, Import>; pub type BoxExpr<'i> = Box<ParsedExpr<'i>>; diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index d72b6bd..9d0a88b 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -539,7 +539,7 @@ rule!(import_raw<BoxExpr<'a>>; // TODO: handle "as Text" children!(import: import_hashed_raw) => { let (location, hash) = import; - bx(Expr_::Embed(Import { + bx(Expr::Embed(Import { mode: ImportMode::Code, hash, location, @@ -586,19 +586,19 @@ rule_group!(expression<BoxExpr<'a>>; rule!(lambda_expression<BoxExpr<'a>>; children!(label: str, typ: expression, body: expression) => { - bx(Expr_::Lam(label, typ, body)) + bx(Expr::Lam(label, typ, body)) } ); rule!(ifthenelse_expression<BoxExpr<'a>>; children!(cond: expression, left: expression, right: expression) => { - bx(Expr_::BoolIf(cond, left, right)) + bx(Expr::BoolIf(cond, left, right)) } ); rule!(let_expression<BoxExpr<'a>>; children!(bindings*: let_binding, final_expr: expression) => { - bindings.fold(final_expr, |acc, x| bx(Expr_::Let(x.0, x.1, x.2, acc))) + bindings.fold(final_expr, |acc, x| bx(Expr::Let(x.0, x.1, x.2, acc))) } ); @@ -608,27 +608,27 @@ rule!(let_binding<(&'a str, Option<BoxExpr<'a>>, BoxExpr<'a>)>; rule!(forall_expression<BoxExpr<'a>>; children!(label: str, typ: expression, body: expression) => { - bx(Expr_::Pi(label, typ, body)) + bx(Expr::Pi(label, typ, body)) } ); rule!(arrow_expression<BoxExpr<'a>>; children!(typ: expression, body: expression) => { - bx(Expr_::Pi("_", typ, body)) + bx(Expr::Pi("_", typ, body)) } ); rule!(merge_expression<BoxExpr<'a>>; children!(x: expression, y: expression, z?: expression) => { - bx(Expr_::Merge(x, y, z)) + bx(Expr::Merge(x, y, z)) } ); rule!(empty_collection<BoxExpr<'a>>; children!(x: str, y: expression) => { match x { - "Optional" => bx(Expr_::OptionalLit(Some(y), vec![])), - "List" => bx(Expr_::ListLit(Some(y), vec![])), + "Optional" => bx(Expr::OptionalLit(Some(y), vec![])), + "List" => bx(Expr::ListLit(Some(y), vec![])), _ => unreachable!(), } } @@ -636,7 +636,7 @@ rule!(empty_collection<BoxExpr<'a>>; rule!(non_empty_optional<BoxExpr<'a>>; children!(x: expression, _y: str, z: expression) => { - bx(Expr_::OptionalLit(Some(z), vec![*x])) + bx(Expr::OptionalLit(Some(z), vec![*x])) } ); @@ -644,7 +644,7 @@ macro_rules! binop { ($rule:ident, $op:ident) => { rule!($rule<BoxExpr<'a>>; children!(first: expression, rest*: expression) => { - rest.fold(first, |acc, e| bx(Expr_::BinOp(BinOp::$op, acc, e))) + rest.fold(first, |acc, e| bx(Expr::BinOp(BinOp::$op, acc, e))) } ); }; @@ -652,7 +652,7 @@ macro_rules! binop { rule!(annotated_expression<BoxExpr<'a>>; children!(e: expression, annot: expression) => { - bx(Expr_::Annot(e, annot)) + bx(Expr::Annot(e, annot)) }, children!(e: expression) => e, ); @@ -672,61 +672,61 @@ binop!(not_equal_expression, BoolNE); rule!(application_expression<BoxExpr<'a>>; children!(first: expression, rest*: expression) => { - rest.fold(first, |acc, e| bx(Expr_::App(acc, e))) + rest.fold(first, |acc, e| bx(Expr::App(acc, e))) } ); rule!(selector_expression_raw<BoxExpr<'a>>; children!(first: expression, rest*: str) => { - rest.fold(first, |acc, e| bx(Expr_::Field(acc, e))) + rest.fold(first, |acc, e| bx(Expr::Field(acc, e))) } ); rule!(literal_expression_raw<BoxExpr<'a>>; - children!(n: double_literal_raw) => bx(Expr_::DoubleLit(n)), - children!(n: minus_infinity_literal) => bx(Expr_::DoubleLit(std::f64::NEG_INFINITY)), - children!(n: plus_infinity_literal) => bx(Expr_::DoubleLit(std::f64::INFINITY)), - children!(n: NaN_raw) => bx(Expr_::DoubleLit(std::f64::NAN)), - children!(n: natural_literal_raw) => bx(Expr_::NaturalLit(n)), - children!(n: integer_literal_raw) => bx(Expr_::IntegerLit(n)), - children!(s: double_quote_literal) => bx(Expr_::TextLit(s)), - children!(s: single_quote_literal) => bx(Expr_::TextLit(s)), + children!(n: double_literal_raw) => bx(Expr::DoubleLit(n)), + children!(n: minus_infinity_literal) => bx(Expr::DoubleLit(std::f64::NEG_INFINITY)), + children!(n: plus_infinity_literal) => bx(Expr::DoubleLit(std::f64::INFINITY)), + children!(n: NaN_raw) => bx(Expr::DoubleLit(std::f64::NAN)), + children!(n: natural_literal_raw) => bx(Expr::NaturalLit(n)), + children!(n: integer_literal_raw) => bx(Expr::IntegerLit(n)), + children!(s: double_quote_literal) => bx(Expr::TextLit(s)), + children!(s: single_quote_literal) => bx(Expr::TextLit(s)), children!(e: expression) => e, ); rule!(identifier_raw<BoxExpr<'a>>; children!(name: str, idx?: natural_literal_raw) => { match Builtin::parse(name) { - Some(b) => bx(Expr_::Builtin(b)), + Some(b) => bx(Expr::Builtin(b)), None => match name { - "True" => bx(Expr_::BoolLit(true)), - "False" => bx(Expr_::BoolLit(false)), - "Type" => bx(Expr_::Const(Const::Type)), - "Kind" => bx(Expr_::Const(Const::Kind)), - name => bx(Expr_::Var(V(name, idx.unwrap_or(0)))), + "True" => bx(Expr::BoolLit(true)), + "False" => bx(Expr::BoolLit(false)), + "Type" => bx(Expr::Const(Const::Type)), + "Kind" => bx(Expr::Const(Const::Kind)), + name => bx(Expr::Var(V(name, idx.unwrap_or(0)))), } } } ); rule!(empty_record_literal<BoxExpr<'a>>; - children!() => bx(Expr_::RecordLit(BTreeMap::new())) + children!() => bx(Expr::RecordLit(BTreeMap::new())) ); rule!(empty_record_type<BoxExpr<'a>>; - children!() => bx(Expr_::Record(BTreeMap::new())) + children!() => bx(Expr::Record(BTreeMap::new())) ); rule!(non_empty_record_type_or_literal<BoxExpr<'a>>; children!(first_label: str, rest: non_empty_record_type) => { let (first_expr, mut map) = rest; map.insert(first_label, *first_expr); - bx(Expr_::Record(map)) + bx(Expr::Record(map)) }, children!(first_label: str, rest: non_empty_record_literal) => { let (first_expr, mut map) = rest; map.insert(first_label, *first_expr); - bx(Expr_::RecordLit(map)) + bx(Expr::RecordLit(map)) }, ); @@ -754,12 +754,12 @@ rule!(non_empty_record_literal<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)> rule!(union_type_or_literal<BoxExpr<'a>>; children!(_e: empty_union_type) => { - bx(Expr_::Union(BTreeMap::new())) + bx(Expr::Union(BTreeMap::new())) }, children!(x: non_empty_union_type_or_literal) => { match x { - (Some((l, e)), entries) => bx(Expr_::UnionLit(l, e, entries)), - (None, entries) => bx(Expr_::Union(entries)), + (Some((l, e)), entries) => bx(Expr::UnionLit(l, e, entries)), + (None, entries) => bx(Expr::Union(entries)), } }, ); @@ -799,7 +799,7 @@ rule!(union_type_entry<(&'a str, BoxExpr<'a>)>; rule!(non_empty_list_literal_raw<BoxExpr<'a>>; children!(items*: expression) => { - bx(Expr_::ListLit(None, items.map(|x| *x).collect())) + bx(Expr::ListLit(None, items.map(|x| *x).collect())) } ); @@ -811,13 +811,13 @@ pub fn parse_expr<'i>(s: &'i str) -> ParseResult<BoxExpr<'i>> { let pairs = DhallParser::parse(Rule::final_expression, s)?; // Match the only item in the pairs iterator match_iter!(@panic; pairs; (p) => expression(p)) - // Ok(bx(Expr_::BoolLit(false))) + // Ok(bx(Expr::BoolLit(false))) } #[test] fn test_parse() { use crate::core::BinOp::*; - use crate::core::Expr_::*; + use crate::core::Expr::*; // let expr = r#"{ x = "foo", y = 4 }.x"#; // let expr = r#"(1 + 2) * 3"#; let expr = r#"if True then 1 + 3 * 5 else 2"#; diff --git a/dhall_generator/src/lib.rs b/dhall_generator/src/lib.rs index d84bea4..75912d0 100644 --- a/dhall_generator/src/lib.rs +++ b/dhall_generator/src/lib.rs @@ -8,7 +8,7 @@ use quote::quote; #[proc_macro] pub fn dhall(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let input_str = input.to_string(); - let expr: Box<Expr<X, Import>> = parser::parse_expr(&input_str).unwrap(); + let expr: Box<Expr<_, X, Import>> = parser::parse_expr(&input_str).unwrap(); let no_import = |_: &Import| -> X { panic!("Don't use import in dhall!()") }; let expr = expr.take_ownership_of_labels().map_embed(&no_import); @@ -19,10 +19,10 @@ pub fn dhall(input: proc_macro::TokenStream) -> proc_macro::TokenStream { // Returns an expression of type Expr<_, _>. Expects input variables // to be of type Box<Expr<_, _>> (future-proof for structural sharing). fn dhall_to_tokenstream<L: StringLike>( - expr: &Expr_<L, X, X>, + expr: &Expr<L, X, X>, ctx: &Context<L, ()>, ) -> TokenStream { - use dhall_core::Expr_::*; + use dhall_core::Expr::*; match expr { e @ Var(_) => { let v = dhall_to_tokenstream_bx(e, ctx); @@ -75,10 +75,10 @@ fn dhall_to_tokenstream<L: StringLike>( // Returns an expression of type Box<Expr<_, _>> fn dhall_to_tokenstream_bx<L: StringLike>( - expr: &Expr_<L, X, X>, + expr: &Expr<L, X, X>, ctx: &Context<L, ()>, ) -> TokenStream { - use dhall_core::Expr_::*; + use dhall_core::Expr::*; match expr { Var(V(s, n)) => { match ctx.lookup(&s, *n) { @@ -93,7 +93,7 @@ fn dhall_to_tokenstream_bx<L: StringLike>( // TODO: insert appropriate shifts ? let v: TokenStream = s.parse().unwrap(); quote! { { - let x: Box<Expr_<_, _, _>> = #v.clone(); + let x: Box<Expr<_, _, _>> = #v.clone(); x } } } |