summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/nze
diff options
context:
space:
mode:
authorNadrieril2020-01-30 19:56:31 +0000
committerNadrieril2020-01-30 19:56:31 +0000
commitd8de45763037937b5c2dedbe5f7bb95a4e7bc7cd (patch)
tree0c73b8ec2df6e8f592136133f07957e3ae8e8931 /dhall/src/semantics/nze
parent673a580e11d31356bec25d73213b283685fd6ea3 (diff)
Avoid unnecessary allocations of `Value`s
Diffstat (limited to 'dhall/src/semantics/nze')
-rw-r--r--dhall/src/semantics/nze/env.rs16
-rw-r--r--dhall/src/semantics/nze/normalize.rs21
-rw-r--r--dhall/src/semantics/nze/value.rs6
3 files changed, 21 insertions, 22 deletions
diff --git a/dhall/src/semantics/nze/env.rs b/dhall/src/semantics/nze/env.rs
index 073886e..0b22a8b 100644
--- a/dhall/src/semantics/nze/env.rs
+++ b/dhall/src/semantics/nze/env.rs
@@ -59,14 +59,18 @@ impl NzEnv {
env.items.push(NzEnvItem::Replaced(e));
env
}
- pub fn lookup_val(&self, var: &AlphaVar) -> Value {
+ pub fn lookup_val(&self, var: &AlphaVar) -> ValueKind {
let idx = self.items.len() - 1 - var.idx();
match &self.items[idx] {
- NzEnvItem::Kept(ty) => Value::from_kind_and_type(
- ValueKind::Var(NzVar::new(idx)),
- ty.clone(),
- ),
- NzEnvItem::Replaced(x) => x.clone(),
+ NzEnvItem::Kept(_) => ValueKind::Var(NzVar::new(idx)),
+ NzEnvItem::Replaced(x) => x.kind().clone(),
+ }
+ }
+ pub fn lookup_ty(&self, var: &AlphaVar) -> Value {
+ let idx = self.items.len() - 1 - var.idx();
+ match &self.items[idx] {
+ NzEnvItem::Kept(ty) => ty.clone(),
+ NzEnvItem::Replaced(x) => x.get_type().unwrap(),
}
}
}
diff --git a/dhall/src/semantics/nze/normalize.rs b/dhall/src/semantics/nze/normalize.rs
index 7632d12..e677f78 100644
--- a/dhall/src/semantics/nze/normalize.rs
+++ b/dhall/src/semantics/nze/normalize.rs
@@ -445,14 +445,9 @@ pub(crate) fn normalize_one_layer(
}
/// Normalize a TyExpr into WHNF
-pub(crate) fn normalize_tyexpr_whnf(tye: &TyExpr, env: &NzEnv) -> Value {
- let ty = match tye.get_type() {
- Ok(ty) => ty,
- Err(_) => return Value::from_const(Const::Sort),
- };
-
- let kind = match tye.kind() {
- TyExprKind::Var(var) => return env.lookup_val(var),
+pub(crate) fn normalize_tyexpr_whnf(tye: &TyExpr, env: &NzEnv) -> ValueKind {
+ match tye.kind() {
+ TyExprKind::Var(var) => env.lookup_val(var),
TyExprKind::Expr(ExprKind::Lam(binder, annot, body)) => {
let annot = annot.eval(env);
ValueKind::LamClosure {
@@ -472,13 +467,15 @@ pub(crate) fn normalize_tyexpr_whnf(tye: &TyExpr, env: &NzEnv) -> Value {
}
TyExprKind::Expr(ExprKind::Let(_, None, val, body)) => {
let val = val.eval(env);
- return body.eval(&env.insert_value(val));
+ body.eval(&env.insert_value(val)).kind().clone()
}
TyExprKind::Expr(e) => {
+ let ty = match tye.get_type() {
+ Ok(ty) => ty,
+ Err(_) => return ValueKind::Const(Const::Sort),
+ };
let e = e.map_ref(|tye| tye.eval(env));
normalize_one_layer(e, &ty, env)
}
- };
-
- Value::from_kind_and_type(kind, ty)
+ }
}
diff --git a/dhall/src/semantics/nze/value.rs b/dhall/src/semantics/nze/value.rs
index 108dc9c..ff0ca42 100644
--- a/dhall/src/semantics/nze/value.rs
+++ b/dhall/src/semantics/nze/value.rs
@@ -405,7 +405,7 @@ impl ValueInternal {
use std::mem::replace;
let dummy = Form::PartialExpr(ExprKind::Const(Const::Type));
self.form = match replace(&mut self.form, dummy) {
- Form::Thunk(th) => Form::WHNF(th.eval().kind().clone()),
+ Form::Thunk(th) => Form::WHNF(th.eval()),
Form::PartialExpr(e) => {
Form::WHNF(match &self.ty {
// TODO: env
@@ -516,9 +516,7 @@ impl Thunk {
body,
}
}
-
- // TODO: maybe return a ValueKind ?
- pub fn eval(&self) -> Value {
+ pub fn eval(&self) -> ValueKind {
normalize_tyexpr_whnf(&self.body, &self.env)
}
}