From caf36246a517b884d7cfcf7c31e1b5d8fce60dfa Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 20 Aug 2019 13:49:13 +0200 Subject: Cleanup --- dhall/src/core/value.rs | 16 ++------ dhall/src/core/valuef.rs | 6 +-- dhall/src/phase/mod.rs | 2 +- dhall/src/phase/normalize.rs | 87 ++++++++++++++++++++++++++++---------------- dhall/src/phase/typecheck.rs | 6 +-- 5 files changed, 65 insertions(+), 52 deletions(-) (limited to 'dhall') diff --git a/dhall/src/core/value.rs b/dhall/src/core/value.rs index 87816c4..0af7c8c 100644 --- a/dhall/src/core/value.rs +++ b/dhall/src/core/value.rs @@ -40,8 +40,7 @@ struct ValueInternal { /// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, /// sharing computation automatically. Uses a RefCell to share computation. -/// Can optionally store a type from typechecking to preserve type information through -/// normalization. +/// Can optionally store a type from typechecking to preserve type information. #[derive(Clone)] pub struct Value(Rc>); @@ -160,10 +159,6 @@ impl Value { pub(crate) fn to_expr_alpha(&self) -> NormalizedSubExpr { self.as_whnf().normalize_to_expr_maybe_alpha(true) } - // TODO: deprecated - pub(crate) fn to_value(&self) -> Value { - self.clone() - } /// TODO: cloning a valuef can often be avoided pub(crate) fn to_whnf(&self) -> ValueF { self.as_whnf().clone() @@ -172,8 +167,7 @@ impl Value { Typed::from_value(self) } - /// Mutates the contents. If no one else shares this thunk, - /// mutates directly, thus avoiding a RefCell lock. + /// Mutates the contents. If no one else shares this, this avoids a RefCell lock. fn mutate_internal(&mut self, f: impl FnOnce(&mut ValueInternal)) { match Rc::get_mut(&mut self.0) { // Mutate directly if sole owner @@ -183,7 +177,7 @@ impl Value { } } /// Normalizes contents to normal form; faster than `normalize_nf` if - /// no one else shares this thunk. + /// no one else shares this. pub(crate) fn normalize_mut(&mut self) { self.mutate_internal(|vint| vint.normalize_nf()) } @@ -218,10 +212,6 @@ impl Value { self.as_nf().normalize_to_expr_maybe_alpha(alpha) } - pub(crate) fn app_valuef(&self, val: ValueF) -> ValueF { - self.app_value(val.into_value_untyped()) - } - pub(crate) fn app_value(&self, th: Value) -> ValueF { apply_any(self.clone(), th) } diff --git a/dhall/src/core/valuef.rs b/dhall/src/core/valuef.rs index 0d2655e..0c3058d 100644 --- a/dhall/src/core/valuef.rs +++ b/dhall/src/core/valuef.rs @@ -257,17 +257,17 @@ impl ValueF { } } - /// Apply to a value + /// Apply to a valuef pub(crate) fn app(self, val: ValueF) -> ValueF { self.app_valuef(val) } - /// Apply to a value + /// Apply to a valuef pub(crate) fn app_valuef(self, val: ValueF) -> ValueF { self.app_value(val.into_value_untyped()) } - /// Apply to a thunk + /// Apply to a value pub fn app_value(self, th: Value) -> ValueF { Value::from_valuef_untyped(self).app_value(th) } diff --git a/dhall/src/phase/mod.rs b/dhall/src/phase/mod.rs index 91d64c3..1f7e5f0 100644 --- a/dhall/src/phase/mod.rs +++ b/dhall/src/phase/mod.rs @@ -110,7 +110,7 @@ impl Typed { self.0.to_expr_alpha() } pub fn to_value(&self) -> Value { - self.0.to_value() + self.0.clone() } pub(crate) fn into_value(self) -> Value { self.0 diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index 76349e4..a379a4b 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -231,20 +231,28 @@ pub(crate) fn apply_builtin(b: Builtin, args: Vec) -> ValueF { unimplemented!() } } - _ => Ok(( - r, - f.app_valuef(ValueF::from_builtin(List).app_value(t.clone())) - .app_value({ - // Move `t` under new `x` variable - let t1 = t.under_binder(Label::from("x")); - make_closure!( - λ(x : #t) -> - λ(xs : List #t1) -> - [ var(x, 1) ] # var(xs, 0) - ) - }) - .app_valuef(EmptyListLit(t.clone())), - )), + _ => { + let list_t = ValueF::from_builtin(List) + .app_value(t.clone()) + .into_value_simple_type(); + Ok(( + r, + f.app_value(list_t.clone()) + .app_value({ + // Move `t` under new `x` variable + let t1 = t.under_binder(Label::from("x")); + make_closure!( + λ(x : #t) -> + λ(xs : List #t1) -> + [ var(x, 1) ] # var(xs, 0) + ) + }) + .app_value( + EmptyListLit(t.clone()) + .into_value_with_type(list_t), + ), + )) + } }, (ListFold, [_, l, _, cons, nil, r..]) => match &*l.as_whnf() { EmptyListLit(_) => Ok((r, nil.to_whnf())), @@ -271,14 +279,20 @@ pub(crate) fn apply_builtin(b: Builtin, args: Vec) -> ValueF { unimplemented!() } } - _ => Ok(( - r, - f.app_valuef( - ValueF::from_builtin(Optional).app_value(t.clone()), - ) - .app_value(make_closure!(λ(x: #t) -> Some(var(x, 0)))) - .app_valuef(EmptyOptionalLit(t.clone())), - )), + _ => { + let optional_t = ValueF::from_builtin(Optional) + .app_value(t.clone()) + .into_value_simple_type(); + Ok(( + r, + f.app_value(optional_t.clone()) + .app_value(make_closure!(λ(x: #t) -> Some(var(x, 0)))) + .app_value( + EmptyOptionalLit(t.clone()) + .into_value_with_type(optional_t), + ), + )) + } }, (OptionalFold, [_, v, _, just, nothing, r..]) => match &*v.as_whnf() { EmptyOptionalLit(_) => Ok((r, nothing.to_whnf())), @@ -295,12 +309,20 @@ pub(crate) fn apply_builtin(b: Builtin, args: Vec) -> ValueF { unimplemented!() } } - _ => Ok(( - r, - f.app_valuef(ValueF::from_builtin(Natural)) - .app_value(make_closure!(λ(x : Natural) -> 1 + var(x, 0))) - .app_valuef(NaturalLit(0)), - )), + _ => { + let nat_type = + ValueF::from_builtin(Natural).into_value_simple_type(); + Ok(( + r, + f.app_value(nat_type.clone()) + .app_value( + make_closure!(λ(x : Natural) -> 1 + var(x, 0)), + ) + .app_value( + NaturalLit(0).into_value_with_type(nat_type), + ), + )) + } }, (NaturalFold, [n, t, succ, zero, r..]) => match &*n.as_whnf() { NaturalLit(0) => Ok((r, zero.to_whnf())), @@ -309,8 +331,9 @@ pub(crate) fn apply_builtin(b: Builtin, args: Vec) -> ValueF { .app(NaturalLit(n - 1)) .app_value(t.clone()) .app_value(succ.clone()) - .app_value(zero.clone()); - Ok((r, succ.app_valuef(fold))) + .app_value(zero.clone()) + .into_value_with_type(t.clone()); + Ok((r, succ.app_value(fold))) } _ => Err(()), }, @@ -571,8 +594,8 @@ fn apply_binop<'a>(o: BinOp, x: &'a Value, y: &'a Value) -> Option> { let kvs = merge_maps(kvs1, kvs2, |v1, v2| { Value::from_valuef_untyped(ValueF::PartialExpr(ExprF::BinOp( RecursiveRecordTypeMerge, - v1.to_value(), - v2.to_value(), + v1.clone(), + v2.clone(), ))) }); Ret::ValueF(RecordType(kvs)) diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs index 8a3b43a..c47eb78 100644 --- a/dhall/src/phase/typecheck.rs +++ b/dhall/src/phase/typecheck.rs @@ -300,7 +300,7 @@ fn type_with( let tx = type_with(ctx, t.clone())?; let ctx2 = ctx.insert_type(x, tx.clone()); let b = type_with(&ctx2, b.clone())?; - let v = ValueF::Lam(x.clone().into(), tx.clone(), b.to_value()); + let v = ValueF::Lam(x.clone().into(), tx.clone(), b.clone()); let tb = b.get_type()?.into_owned(); let t = tck_pi_type(ctx, x.clone(), tx, tb)?; Value::from_valuef_and_type(v, t) @@ -446,7 +446,7 @@ fn type_last_layer( RetTypeOnly(Value::from_valuef_and_type( ValueF::from_builtin(dhall_syntax::Builtin::List) - .app_value(t.to_value()), + .app_value(t.into_owned()), Value::from_const(Type), )) } @@ -458,7 +458,7 @@ fn type_last_layer( RetTypeOnly(Value::from_valuef_and_type( ValueF::from_builtin(dhall_syntax::Builtin::Optional) - .app_value(t.to_value()), + .app_value(t), Value::from_const(Type), )) } -- cgit v1.2.3