From 05454ab9936514409e1b3c97e36f3fb476d532ba Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 16 Mar 2019 23:21:51 +0100 Subject: Use Rc instead of Box in AST to allow structural sharing Closes #29 --- dhall_core/src/core.rs | 80 +++++++++++++++++++++++++----------------------- dhall_core/src/parser.rs | 67 ++++++++++++++++++++-------------------- 2 files changed, 75 insertions(+), 72 deletions(-) (limited to 'dhall_core') diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 34738bb..f5da1df 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -176,13 +176,13 @@ pub enum BinOp { #[derive(Debug, Clone, PartialEq)] pub struct InterpolatedText { head: String, - tail: Vec<(Box>, String)>, + tail: Vec<(Rc>, String)>, } -impl From<(String, Vec<(Box>, String)>)> +impl From<(String, Vec<(Rc>, String)>)> for InterpolatedText { - fn from(x: (String, Vec<(Box>, String)>)) -> Self { + fn from(x: (String, Vec<(Rc>, String)>)) -> Self { InterpolatedText { head: x.0, tail: x.1, @@ -203,7 +203,7 @@ impl From for InterpolatedText { // This one is needed when parsing, because we need to own the Expr pub enum OwnedInterpolatedTextContents<'a, Note, Embed> { Text(&'a str), - Expr(Box>), + Expr(Rc>), } // This one is needed everywhere else, because we don't want Clone traits bounds @@ -231,7 +231,7 @@ impl<'a, N: Clone + 'a, E: Clone + 'a> impl InterpolatedText { pub fn map(&self, mut f: F) -> InterpolatedText where - F: FnMut(&Box>) -> Box>, + F: FnMut(&Rc>) -> Rc>, { InterpolatedText { head: self.head.clone(), @@ -299,33 +299,33 @@ pub enum Expr { /// `Var (V x n) ~ x@n` Var(V), /// `Lam x A b ~ λ(x : A) -> b` - Lam(Label, Box>, Box>), + Lam(Label, Rc>, Rc>), /// `Pi "_" A B ~ A -> B` /// `Pi x A B ~ ∀(x : A) -> B` - Pi(Label, Box>, Box>), + Pi(Label, Rc>, Rc>), /// `App f A ~ f A` - App(Box>, Vec>>), + App(Rc>, Vec>>), /// `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>, - Box>, + Option>>, + Rc>, + Rc>, ), /// `Annot x t ~ x : t` - Annot(Box>, Box>), + Annot(Rc>, Rc>), /// Built-in values Builtin(Builtin), // Binary operations - BinOp(BinOp, Box>, Box>), + BinOp(BinOp, Rc>, Rc>), /// `BoolLit b ~ b` BoolLit(bool), /// `BoolIf x y z ~ if x then y else z` BoolIf( - Box>, - Box>, - Box>, + Rc>, + Rc>, + Rc>, ), /// `NaturalLit n ~ +n` NaturalLit(Natural), @@ -336,35 +336,35 @@ pub enum Expr { /// `TextLit t ~ t` TextLit(InterpolatedText), /// `ListLit t [x, y, z] ~ [x, y, z] : List t` - ListLit(Option>>, Vec>>), + ListLit(Option>>, Vec>>), /// `OptionalLit t [e] ~ [e] : Optional t` /// `OptionalLit t [] ~ [] : Optional t` OptionalLit( - Option>>, - Option>>, + Option>>, + Option>>, ), /// `Record [(k1, t1), (k2, t2)] ~ { k1 : t1, k2 : t1 }` - Record(BTreeMap>>), + Record(BTreeMap>>), /// `RecordLit [(k1, v1), (k2, v2)] ~ { k1 = v1, k2 = v2 }` - RecordLit(BTreeMap>>), + RecordLit(BTreeMap>>), /// `Union [(k1, t1), (k2, t2)] ~ < k1 : t1, k2 : t2 >` - Union(BTreeMap>>), + Union(BTreeMap>>), /// `UnionLit (k1, v1) [(k2, t2), (k3, t3)] ~ < k1 = t1, k2 : t2, k3 : t3 >` UnionLit( Label, - Box>, - BTreeMap>>, + Rc>, + BTreeMap>>, ), /// `Merge x y t ~ merge x y : t` Merge( - Box>, - Box>, - Option>>, + Rc>, + Rc>, + Option>>, ), /// `Field e x ~ e.x` - Field(Box>, Label), + Field(Rc>, Label), /// Annotation on the AST. Unused for now but could hold e.g. file location information - Note(Note, Box>), + Note(Note, Rc>), /// Embeds an import or the result of resolving the import Embed(Embed), } @@ -848,14 +848,13 @@ where Expr::Pi(var.into(), bx(ty.into()), bx(value.into())) } -pub fn app(f: Ef, x: Vec>) -> Expr +pub fn app(f: Ef, x: Vec>>) -> Expr where Ef: Into>, - Ex: Into>, { Expr::App( bx(f.into()), - x.into_iter().map(|x| bx((*x).into())).collect(), + x, ) } @@ -874,8 +873,11 @@ impl Display for X { } } -pub fn bx(x: T) -> Box { - Box::new(x) +pub fn bx(x: T) -> Rc { + Rc::new(x) +} +pub fn rc(x: T) -> Rc { + Rc::new(x) } fn add_ui(u: usize, i: isize) -> usize { @@ -904,8 +906,8 @@ where F4: Fn(&Label) -> Label, { use crate::Expr::*; - let bxmap = |x: &Expr| -> Box> { bx(map(x)) }; - let bxbxmap = |x: &Box>| -> Box> { bx(map(&**x)) }; + let bxmap = |x: &Expr| -> Rc> { bx(map(x)) }; + let bxbxmap = |x: &Rc>| -> Rc> { bx(map(&**x)) }; let opt = |x| map_opt_box(x, &map); match *e { Const(k) => Const(k), @@ -980,7 +982,7 @@ where it.into_iter().map(|(k, v)| (g(k), f(v))).collect() } -pub fn map_opt_box(x: &Option>, f: F) -> Option> +pub fn map_opt_box(x: &Option>, f: F) -> Option> where F: FnOnce(&T) -> U, { @@ -1153,7 +1155,7 @@ fn shift_op2( b: &Expr, ) -> Expr where - F: FnOnce(Box>, Box>) -> Expr, + F: FnOnce(Rc>, Rc>) -> Expr, A: Clone, { map_op2(f, |x| bx(shift(d, v, x)), a, b) @@ -1258,7 +1260,7 @@ fn subst_op2( b: &Expr, ) -> Expr where - F: FnOnce(Box>, Box>) -> Expr, + F: FnOnce(Rc>, Rc>) -> Expr, S: Clone, A: Clone, { diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index b2955fb..682e081 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -2,6 +2,7 @@ use pest::iterators::Pair; use pest::Parser; use std::collections::BTreeMap; use std::path::PathBuf; +use std::rc::Rc; use dhall_parser::{DhallParser, Rule}; @@ -16,7 +17,7 @@ use crate::core::*; pub type ParsedExpr = Expr; pub type ParsedText = InterpolatedText; pub type ParsedTextContents<'a> = OwnedInterpolatedTextContents<'a, X, Import>; -pub type BoxExpr = Box; +pub type RcExpr = Rc; pub type ParseError = pest::error::Error; @@ -455,7 +456,7 @@ rule!(escaped_quote_pair<&'a str>; rule!(escaped_interpolation<&'a str>; children!() => "${" ); -rule!(interpolation; +rule!(interpolation; children!(e: expression) => e ); @@ -556,7 +557,7 @@ rule!(import_hashed_raw<(ImportLocation, Option<()>)>; } ); -rule!(import_raw; +rule!(import_raw; // TODO: handle "as Text" children!(import: import_hashed_raw) => { let (location, hash) = import; @@ -568,7 +569,7 @@ rule!(import_raw; } ); -rule_group!(expression; +rule_group!(expression; identifier_raw, lambda_expression, ifthenelse_expression, @@ -605,47 +606,47 @@ rule_group!(expression; final_expression ); -rule!(lambda_expression; +rule!(lambda_expression; children!(l: label, typ: expression, body: expression) => { bx(Expr::Lam(l, typ, body)) } ); -rule!(ifthenelse_expression; +rule!(ifthenelse_expression; children!(cond: expression, left: expression, right: expression) => { bx(Expr::BoolIf(cond, left, right)) } ); -rule!(let_expression; +rule!(let_expression; children!(bindings*: let_binding, final_expr: expression) => { bindings.fold(final_expr, |acc, x| bx(Expr::Let(x.0, x.1, x.2, acc))) } ); -rule!(let_binding<(Label, Option, BoxExpr)>; +rule!(let_binding<(Label, Option, RcExpr)>; children!(name: label, annot?: expression, expr: expression) => (name, annot, expr) ); -rule!(forall_expression; +rule!(forall_expression; children!(l: label, typ: expression, body: expression) => { bx(Expr::Pi(l, typ, body)) } ); -rule!(arrow_expression; +rule!(arrow_expression; children!(typ: expression, body: expression) => { bx(Expr::Pi("_".into(), typ, body)) } ); -rule!(merge_expression; +rule!(merge_expression; children!(x: expression, y: expression, z?: expression) => { bx(Expr::Merge(x, y, z)) } ); -rule!(empty_collection; +rule!(empty_collection; children!(x: str, y: expression) => { match x { "Optional" => bx(Expr::OptionalLit(Some(y), None)), @@ -655,7 +656,7 @@ rule!(empty_collection; } ); -rule!(non_empty_optional; +rule!(non_empty_optional; children!(x: expression, _y: str, z: expression) => { bx(Expr::OptionalLit(Some(z), Some(x))) } @@ -686,7 +687,7 @@ fn can_be_shortcutted(rule: Rule) -> bool { macro_rules! binop { ($rule:ident, $op:ident) => { - rule!($rule; + rule!($rule; raw_pair!(pair) => { // This all could be a trivial fold, but to avoid stack explosion // we try to cut down on the recursion level here, by consuming @@ -737,14 +738,14 @@ binop!(times_expression, NaturalTimes); binop!(equal_expression, BoolEQ); binop!(not_equal_expression, BoolNE); -rule!(annotated_expression; +rule!(annotated_expression; children!(e: expression, annot: expression) => { bx(Expr::Annot(e, annot)) }, children!(e: expression) => e, ); -rule!(application_expression; +rule!(application_expression; children!(first: expression, rest*: expression) => { let rest: Vec<_> = rest.collect(); if rest.is_empty() { @@ -755,13 +756,13 @@ rule!(application_expression; } ); -rule!(selector_expression_raw; +rule!(selector_expression_raw; children!(first: expression, rest*: label) => { rest.fold(first, |acc, e| bx(Expr::Field(acc, e))) } ); -rule!(literal_expression_raw; +rule!(literal_expression_raw; 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)), @@ -773,7 +774,7 @@ rule!(literal_expression_raw; children!(e: expression) => e, ); -rule!(identifier_raw; +rule!(identifier_raw; children!(name: str, idx?: natural_literal_raw) => { match Builtin::parse(name) { Some(b) => bx(Expr::Builtin(b)), @@ -788,15 +789,15 @@ rule!(identifier_raw; } ); -rule!(empty_record_literal; +rule!(empty_record_literal; children!() => bx(Expr::RecordLit(BTreeMap::new())) ); -rule!(empty_record_type; +rule!(empty_record_type; children!() => bx(Expr::Record(BTreeMap::new())) ); -rule!(non_empty_record_type_or_literal; +rule!(non_empty_record_type_or_literal; children!(first_label: label, rest: non_empty_record_type) => { let (first_expr, mut map) = rest; map.insert(first_label, first_expr); @@ -809,25 +810,25 @@ rule!(non_empty_record_type_or_literal; }, ); -rule!(non_empty_record_type<(BoxExpr, BTreeMap)>; +rule!(non_empty_record_type<(RcExpr, BTreeMap)>; self!(x: partial_record_entries) => x ); -named!(partial_record_entries<(BoxExpr, BTreeMap)>; +named!(partial_record_entries<(RcExpr, BTreeMap)>; children!(expr: expression, entries*: record_entry) => { (expr, entries.collect()) } ); -named!(record_entry<(Label, BoxExpr)>; +named!(record_entry<(Label, RcExpr)>; children!(name: label, expr: expression) => (name, expr) ); -rule!(non_empty_record_literal<(BoxExpr, BTreeMap)>; +rule!(non_empty_record_literal<(RcExpr, BTreeMap)>; self!(x: partial_record_entries) => x ); -rule!(union_type_or_literal; +rule!(union_type_or_literal; children!(_e: empty_union_type) => { bx(Expr::Union(BTreeMap::new())) }, @@ -842,7 +843,7 @@ rule!(union_type_or_literal; rule!(empty_union_type<()>; children!() => ()); rule!(non_empty_union_type_or_literal - <(Option<(Label, BoxExpr)>, BTreeMap)>; + <(Option<(Label, RcExpr)>, BTreeMap)>; children!(l: label, e: expression, entries: union_type_entries) => { (Some((l, e)), entries) }, @@ -858,27 +859,27 @@ rule!(non_empty_union_type_or_literal }, ); -rule!(union_type_entries>; +rule!(union_type_entries>; children!(entries*: union_type_entry) => { entries.collect() } ); -rule!(union_type_entry<(Label, BoxExpr)>; +rule!(union_type_entry<(Label, RcExpr)>; children!(name: label, expr: expression) => (name, expr) ); -rule!(non_empty_list_literal_raw; +rule!(non_empty_list_literal_raw; children!(items*: expression) => { bx(Expr::ListLit(None, items.collect())) } ); -rule!(final_expression; +rule!(final_expression; children!(e: expression, _eoi: EOI) => e ); -pub fn parse_expr(s: &str) -> ParseResult { +pub fn parse_expr(s: &str) -> ParseResult { let pairs = DhallParser::parse(Rule::final_expression, s)?; // Match the only item in the pairs iterator // println!("{}", debug_pair(pairs.clone().next().unwrap())); -- cgit v1.2.3