From f10dd510adc8ba292bd2fa1810497ff6cf42f81c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 27 Apr 2019 15:30:11 +0200 Subject: Rename Now to Thunk --- dhall/src/normalize.rs | 89 +++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 14b062f..f3b0979 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -78,7 +78,7 @@ fn apply_builtin(b: Builtin, args: Vec) -> WHNF { let ret = match b { OptionalNone => improved_slice_patterns::match_vec!(args; - [t] => EmptyOptionalLit(Now::from_whnf(t)), + [t] => EmptyOptionalLit(Thunk::from_whnf(t)), ), NaturalIsZero => improved_slice_patterns::match_vec!(args; [NaturalLit(n)] => BoolLit(n == 0), @@ -126,12 +126,12 @@ fn apply_builtin(b: Builtin, args: Vec) -> WHNF { let mut kts = BTreeMap::new(); kts.insert( "index".into(), - Now::from_whnf( + Thunk::from_whnf( WHNF::from_builtin(Natural) ), ); kts.insert("value".into(), t); - EmptyListLit(Now::from_whnf(RecordType(kts))) + EmptyListLit(Thunk::from_whnf(RecordType(kts))) }, [_, NEListLit(xs)] => { let xs = xs @@ -140,9 +140,9 @@ fn apply_builtin(b: Builtin, args: Vec) -> WHNF { .map(|(i, e)| { let i = NaturalLit(i); let mut kvs = BTreeMap::new(); - kvs.insert("index".into(), Now::from_whnf(i)); + kvs.insert("index".into(), Thunk::from_whnf(i)); kvs.insert("value".into(), e); - Now::from_whnf(RecordLit(kvs)) + Thunk::from_whnf(RecordLit(kvs)) }) .collect(); NEListLit(xs) @@ -161,8 +161,8 @@ fn apply_builtin(b: Builtin, args: Vec) -> WHNF { }, [t, g] => g .app(WHNF::from_builtin(List).app(t.clone())) - .app(ListConsClosure(Now::from_whnf(t.clone()), None)) - .app(EmptyListLit(Now::from_whnf(t))), + .app(ListConsClosure(Thunk::from_whnf(t.clone()), None)) + .app(EmptyListLit(Thunk::from_whnf(t))), ), ListFold => improved_slice_patterns::match_vec!(args; // fold/build fusion @@ -195,8 +195,8 @@ fn apply_builtin(b: Builtin, args: Vec) -> WHNF { }, [t, g] => g .app(WHNF::from_builtin(Optional).app(t.clone())) - .app(OptionalSomeClosure(Now::from_whnf(t.clone()))) - .app(EmptyOptionalLit(Now::from_whnf(t))), + .app(OptionalSomeClosure(Thunk::from_whnf(t.clone()))) + .app(EmptyOptionalLit(Thunk::from_whnf(t))), ), OptionalFold => improved_slice_patterns::match_vec!(args; // fold/build fusion @@ -344,25 +344,25 @@ impl NormalizationContext { #[derive(Debug, Clone)] pub(crate) enum WHNF { /// Closures - Lam(Label, Now, (NormalizationContext, InputSubExpr)), + Lam(Label, Thunk, (NormalizationContext, InputSubExpr)), AppliedBuiltin(Builtin, Vec), /// `λ(x: a) -> Some x` - OptionalSomeClosure(Now), + OptionalSomeClosure(Thunk), /// `λ(x : a) -> λ(xs : List a) -> [ x ] # xs` /// `λ(xs : List a) -> [ x ] # xs` - ListConsClosure(Now, Option), + ListConsClosure(Thunk, Option), /// `λ(x : Natural) -> x + 1` NaturalSuccClosure, BoolLit(bool), NaturalLit(Natural), IntegerLit(Integer), - EmptyOptionalLit(Now), - NEOptionalLit(Now), - EmptyListLit(Now), - NEListLit(Vec), - RecordLit(BTreeMap), - RecordType(BTreeMap), + EmptyOptionalLit(Thunk), + NEOptionalLit(Thunk), + EmptyListLit(Thunk), + NEListLit(Vec), + RecordLit(BTreeMap), + RecordType(BTreeMap), UnionType(NormalizationContext, BTreeMap>), UnionConstructor( NormalizationContext, @@ -371,10 +371,10 @@ pub(crate) enum WHNF { ), UnionLit( Label, - Now, + Thunk, (NormalizationContext, BTreeMap>), ), - TextLit(Vec>), + TextLit(Vec>), /// This must not contain a value captured by one of the variants above. Expr(OutputSubExpr), } @@ -490,7 +490,7 @@ impl WHNF { )), WHNF::TextLit(elts) => { fn normalize_textlit( - elts: Vec>, + elts: Vec>, ) -> InterpolatedText { elts.into_iter() .flat_map(|contents| { @@ -533,10 +533,10 @@ impl WHNF { apply_builtin(b, args) } (WHNF::OptionalSomeClosure(_), val) => { - WHNF::NEOptionalLit(Now::from_whnf(val)) + WHNF::NEOptionalLit(Thunk::from_whnf(val)) } (WHNF::ListConsClosure(t, None), val) => { - WHNF::ListConsClosure(t, Some(Now::from_whnf(val))) + WHNF::ListConsClosure(t, Some(Thunk::from_whnf(val))) } (WHNF::ListConsClosure(_, Some(x)), WHNF::EmptyListLit(_)) => { WHNF::NEListLit(vec![x]) @@ -549,7 +549,7 @@ impl WHNF { WHNF::NaturalLit(n + 1) } (WHNF::UnionConstructor(ctx, l, kts), val) => { - WHNF::UnionLit(l, Now::from_whnf(val), (ctx, kts)) + WHNF::UnionLit(l, Thunk::from_whnf(val), (ctx, kts)) } // Can't do anything useful, convert to expr (f, a) => WHNF::Expr(rc(ExprF::App( @@ -625,35 +625,34 @@ impl WHNF { } } -/// Normalize-on-write smart container. Contains either a (partially) normalized value or a +/// Contains either a (partially) normalized value or a /// non-normalized value alongside a normalization context. -/// The name is a pun on std::borrow::Cow. #[derive(Debug, Clone)] -pub(crate) enum Now { +pub(crate) enum Thunk { Normalized(Box), Unnormalized(NormalizationContext, InputSubExpr), } -impl Now { - fn new(ctx: NormalizationContext, e: InputSubExpr) -> Now { - Now::Unnormalized(ctx, e) +impl Thunk { + fn new(ctx: NormalizationContext, e: InputSubExpr) -> Thunk { + Thunk::Unnormalized(ctx, e) } - fn from_whnf(v: WHNF) -> Now { - Now::Normalized(Box::new(v)) + fn from_whnf(v: WHNF) -> Thunk { + Thunk::Normalized(Box::new(v)) } fn normalize(self) -> WHNF { match self { - Now::Normalized(v) => *v, - Now::Unnormalized(ctx, e) => normalize_whnf(ctx, e), + Thunk::Normalized(v) => *v, + Thunk::Unnormalized(ctx, e) => normalize_whnf(ctx, e), } } fn shift(&mut self, delta: isize, var: &V