summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/tck
diff options
context:
space:
mode:
authorNadrieril2020-01-29 22:06:01 +0000
committerNadrieril2020-01-29 22:06:01 +0000
commita928c3c4f51d87fd942e8a81727962c00abf6808 (patch)
tree4351f1326814d7ba5ecd89d47a56c9f55be40fb5 /dhall/src/semantics/tck
parent489174a426e6057a68b6edd2e9b4387d09912a25 (diff)
Cleanup variable handling
Diffstat (limited to 'dhall/src/semantics/tck')
-rw-r--r--dhall/src/semantics/tck/env.rs10
-rw-r--r--dhall/src/semantics/tck/tyexpr.rs26
2 files changed, 25 insertions, 11 deletions
diff --git a/dhall/src/semantics/tck/env.rs b/dhall/src/semantics/tck/env.rs
index 32c2f21..1276a48 100644
--- a/dhall/src/semantics/tck/env.rs
+++ b/dhall/src/semantics/tck/env.rs
@@ -37,7 +37,7 @@ impl VarEnv {
}
pub fn lookup_fallible(&self, var: &NzVar) -> Option<AlphaVar> {
let idx = self.size.checked_sub(var.idx() + 1)?;
- Some(AlphaVar::new(V((), idx)))
+ Some(AlphaVar::new(idx))
}
}
@@ -68,7 +68,7 @@ impl NameEnv {
self.names.pop();
}
- pub fn unlabel_var(&self, var: &V<Label>) -> Option<AlphaVar> {
+ pub fn unlabel_var(&self, var: &V) -> Option<AlphaVar> {
let V(name, idx) = var;
let (idx, _) = self
.names
@@ -77,9 +77,9 @@ impl NameEnv {
.enumerate()
.filter(|(_, n)| *n == name)
.nth(*idx)?;
- Some(AlphaVar::new(V((), idx)))
+ Some(AlphaVar::new(idx))
}
- pub fn label_var(&self, var: &AlphaVar) -> V<Label> {
+ pub fn label_var(&self, var: &AlphaVar) -> V {
let name = &self.names[self.names.len() - 1 - var.idx()];
let idx = self
.names
@@ -129,7 +129,7 @@ impl TyEnv {
items: self.items.insert_value(e),
}
}
- pub fn lookup(&self, var: &V<Label>) -> Option<(TyExprKind, Type)> {
+ pub fn lookup(&self, var: &V) -> Option<(TyExprKind, Type)> {
let var = self.names.unlabel_var(var)?;
let ty = self.items.lookup_val(&var).get_type().unwrap();
Some((TyExprKind::Var(var), ty))
diff --git a/dhall/src/semantics/tck/tyexpr.rs b/dhall/src/semantics/tck/tyexpr.rs
index 262cda6..114bb14 100644
--- a/dhall/src/semantics/tck/tyexpr.rs
+++ b/dhall/src/semantics/tck/tyexpr.rs
@@ -1,5 +1,4 @@
use crate::error::{TypeError, TypeMessage};
-use crate::semantics::core::var::AlphaVar;
use crate::semantics::phase::normalize::normalize_tyexpr_whnf;
use crate::semantics::phase::typecheck::rc;
use crate::semantics::phase::Normalized;
@@ -9,6 +8,19 @@ use crate::syntax::{ExprKind, Span, V};
pub(crate) type Type = Value;
+/// Stores an alpha-normalized variable.
+#[derive(Debug, Clone, Copy)]
+pub struct AlphaVar {
+ idx: usize,
+}
+
+#[derive(Debug, Clone)]
+pub(crate) enum TyExprKind {
+ Var(AlphaVar),
+ // Forbidden ExprKind variants: Var, Import, Embed
+ Expr(ExprKind<TyExpr, Normalized>),
+}
+
// An expression with inferred types at every node and resolved variables.
#[derive(Clone)]
pub(crate) struct TyExpr {
@@ -17,11 +29,13 @@ pub(crate) struct TyExpr {
span: Span,
}
-#[derive(Debug, Clone)]
-pub(crate) enum TyExprKind {
- Var(AlphaVar),
- // Forbidden ExprKind variants: Var, Import, Embed
- Expr(ExprKind<TyExpr, Normalized>),
+impl AlphaVar {
+ pub(crate) fn new(idx: usize) -> Self {
+ AlphaVar { idx }
+ }
+ pub(crate) fn idx(&self) -> usize {
+ self.idx
+ }
}
impl TyExpr {