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 ++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 39 deletions(-) (limited to 'dhall_core/src/core.rs') 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, { -- cgit v1.2.3