1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-20 08:05:15 +03:00

Merge pull request #234 from tweag/refactor/meta-values-cleaning

[Refactor]Remove the previous representation of metavalues
This commit is contained in:
Eelco Dolstra 2020-12-21 14:42:20 +01:00 committed by GitHub
commit 3a72ae41e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 42 additions and 425 deletions

View File

@ -429,51 +429,6 @@ where
return Err(EvalError::Other(String::from("empty metavalue"), pos));
}
}
Term::Contract(_, _) if enriched_strict => {
return Err(EvalError::Other(
String::from(
"Expected a simple term, got a Contract. Contracts cannot be evaluated",
),
pos,
));
}
enriched @ Term::DefaultValue(_) | enriched @ Term::Docstring(_, _)
if enriched_strict =>
{
/* Since we are forcing an enriched value, we are morally breaking subject
* reduction (i.e., the type of the current term changes from `enriched something`
* to just `something`). Updating a thunk after having performed this forcing may
* alter the semantics of the program in an unexpected way (see issue
* https://github.com/tweag/nickel/issues/123): we update potential thunks now so
* that their content remains an enriched value.
*/
let update_closure = Closure {
body: RichTerm {
term: Box::new(enriched),
pos,
},
env,
};
update_thunks(&mut stack, &update_closure);
let Closure {
body:
RichTerm {
term: enriched_box,
pos: _,
},
env,
} = update_closure;
let t = match *enriched_box {
Term::DefaultValue(t) | Term::Docstring(_, t) => t,
_ => panic!("eval::eval(): previous match enforced that a term is a default or a docstring, but matched something else")
};
Closure { body: t, env }
}
Term::ContractWithDefault(ty, label, t) if enriched_strict => Closure {
body: Term::Assume(ty, label, t).into(),
env,
},
Term::ResolvedImport(id) => {
if let Some(t) = resolver.get(id) {
Closure::atomic_closure(t)
@ -685,35 +640,6 @@ fn subst(rt: RichTerm, global_env: &Environment, env: &Environment) -> RichTerm
RichTerm::new(Term::StrChunks(chunks), pos)
}
Term::Contract(ty, label) => {
let ty = match ty {
Types(AbsType::Flat(t)) => {
Types(AbsType::Flat(subst_(t, global_env, env, bound)))
}
ty => ty,
};
RichTerm::new(Term::Contract(ty, label), pos)
}
Term::DefaultValue(t) => {
let t = subst_(t, global_env, env, bound);
RichTerm::new(Term::DefaultValue(t), pos)
}
Term::ContractWithDefault(ty, lbl, t) => {
let ty = match ty {
Types(AbsType::Flat(t)) => Types(AbsType::Flat(subst_(
t,
global_env,
env,
Cow::Borrowed(bound.as_ref()),
))),
ty => ty,
};
let t = subst_(t, global_env, env, bound);
RichTerm::new(Term::ContractWithDefault(ty, lbl, t), pos)
}
Term::MetaValue(meta) => {
let contract = meta.contract.map(|(ty, lbl)| {
let ty = match ty {
@ -740,11 +666,6 @@ fn subst(rt: RichTerm, global_env: &Environment, env: &Environment) -> RichTerm
RichTerm::new(Term::MetaValue(meta), pos)
}
Term::Docstring(s, t) => {
let t = subst_(t, global_env, env, bound);
RichTerm::new(Term::Docstring(s, t), pos)
}
}
}
@ -857,13 +778,27 @@ mod tests {
assert_eq!(Ok(Term::Bool(true)), eval_no_import(lambda));
}
fn mk_default(t: RichTerm) -> Term {
use crate::term::MergePriority;
let mut meta = MetaValue::from(t);
meta.priority = MergePriority::Default;
Term::MetaValue(meta)
}
fn mk_docstring<S>(t: RichTerm, s: S) -> Term
where
S: Into<String>,
{
let mut meta = MetaValue::from(t);
meta.doc.replace(s.into());
Term::MetaValue(meta)
}
#[test]
fn enriched_terms_unwrapping() {
let t = Term::DefaultValue(
Term::DefaultValue(Term::Docstring("a".to_string(), Term::Bool(false).into()).into())
.into(),
)
.into();
let t = mk_default(mk_default(mk_docstring(Term::Bool(false).into(), "a").into()).into())
.into();
assert_eq!(Ok(Term::Bool(false)), eval_no_import(t));
}
@ -872,7 +807,7 @@ mod tests {
let t = mk_term::op2(
BinaryOp::Merge(),
Term::Num(1.0),
Term::DefaultValue(Term::Num(2.0).into()),
mk_default(Term::Num(2.0).into()),
);
assert_eq!(Ok(Term::Num(1.0)), eval_no_import(t));
}
@ -881,8 +816,8 @@ mod tests {
fn merge_incompatible_defaults() {
let t = mk_term::op2(
BinaryOp::Merge(),
Term::DefaultValue(Term::Num(1.0).into()),
Term::DefaultValue(Term::Num(2.0).into()),
mk_default(Term::Num(1.0).into()),
mk_default(Term::Num(2.0).into()),
);
eval_no_import(t).unwrap_err();

View File

@ -246,138 +246,6 @@ pub fn merge(
env,
})
}
// Right-biased: when merging two docstrings (s1,t2) and (s2,t2), the right one will end up
// as the outermost position in the resulting term (s2,(s1,merge t1 t2))
(t1, Term::Docstring(s, t2)) => {
let Closure { body, env } = mk_merge_closure(
RichTerm {
term: Box::new(t1),
pos: pos1,
},
env1,
t2,
env2,
);
let body = Term::Docstring(s, body).into();
Ok(Closure { body, env })
}
(Term::Docstring(s, t1), t2) => {
let Closure { body, env } = mk_merge_closure(
t1,
env1,
RichTerm {
term: Box::new(t2),
pos: pos2,
},
env2,
);
let body = Term::Docstring(s, body).into();
Ok(Closure { body, env })
}
// Default merging
(Term::DefaultValue(t1), Term::DefaultValue(t2)) => {
let Closure { body, env } = mk_merge_closure(t1, env1, t2, env2);
let body = Term::DefaultValue(body).into();
Ok(Closure { body, env })
}
(Term::DefaultValue(t1), Term::ContractWithDefault(ty, lbl, t2))
| (Term::ContractWithDefault(ty, lbl, t2), Term::DefaultValue(t1)) => {
let Closure { body, mut env } = mk_merge_closure(t1, env1, t2, env2.clone());
let ty_closure = ty.closurize(&mut env, env2);
let body = Term::ContractWithDefault(ty_closure, lbl, body).into();
Ok(Closure { body, env })
}
// Composed contracts carry and blame their original label. As any contract, the composite
// still requires a label, but it will be ignored, so we can provide a dummy one.
(Term::ContractWithDefault(ty1, lbl1, t1), Term::ContractWithDefault(ty2, lbl2, t2)) => {
let Closure { body, mut env } = mk_merge_closure(t1, env1.clone(), t2, env2.clone());
let body = Term::ContractWithDefault(
merge_types_closure(&mut env, ty1, lbl1, env1, ty2, lbl2, env2),
Label::dummy(),
body,
)
.into();
Ok(Closure { body, env })
}
// We need to keep the environment of contracts as well: custom contracts may use variables
// from the environment, and even standard contracts need access to builtins contracts (see
// issue https://github.com/tweag/nickel/issues/117)
(Term::DefaultValue(t), Term::Contract(ty, lbl)) => {
let mut env = HashMap::new();
let t_closure = t.closurize(&mut env, env1);
let ty_closure = ty.closurize(&mut env, env2);
let body = Term::ContractWithDefault(ty_closure, lbl, t_closure).into();
Ok(Closure { body, env })
}
(Term::Contract(ty, lbl), Term::DefaultValue(t)) => {
let mut env = HashMap::new();
let ty_closure = ty.closurize(&mut env, env1);
let t_closure = t.closurize(&mut env, env2);
let body = Term::ContractWithDefault(ty_closure, lbl, t_closure).into();
Ok(Closure { body, env })
}
(Term::DefaultValue(_), t) => {
let body = RichTerm {
term: Box::new(t),
pos: pos2,
};
Ok(Closure { body, env: env2 })
}
(t, Term::DefaultValue(_)) => {
let body = RichTerm {
term: Box::new(t),
pos: pos1,
};
Ok(Closure { body, env: env1 })
}
// Contracts merging
(Term::Contract(ty1, lbl1), Term::Contract(ty2, lbl2)) => {
let mut env = HashMap::new();
let body = Term::Contract(
merge_types_closure(&mut env, ty1, lbl1, env1, ty2, lbl2, env2),
Label::dummy(),
)
.into();
Ok(Closure { body, env })
}
(Term::Contract(ty1, lbl1), Term::ContractWithDefault(ty2, lbl2, t)) => {
let mut env = HashMap::new();
let ty_closure =
merge_types_closure(&mut env, ty1, lbl1, env1, ty2, lbl2, env2.clone());
let t_closure = t.closurize(&mut env, env2);
let body = Term::ContractWithDefault(ty_closure, Label::dummy(), t_closure).into();
Ok(Closure { body, env })
}
(Term::ContractWithDefault(ty1, lbl1, t), Term::Contract(ty2, lbl2)) => {
let mut env = HashMap::new();
let ty_closure =
merge_types_closure(&mut env, ty1, lbl1, env1.clone(), ty2, lbl2, env2);
let t_closure = t.closurize(&mut env, env1);
let body = Term::ContractWithDefault(ty_closure, Label::dummy(), t_closure).into();
Ok(Closure { body, env })
}
(Term::Contract(ty, lbl), t) | (Term::ContractWithDefault(ty, lbl, _), t) => {
let mut env = HashMap::new();
let t = RichTerm {
term: Box::new(t),
pos: pos2,
};
let ty_closure = ty.closurize(&mut env, env1);
let t_closure = t.closurize(&mut env, env2);
let body = Term::Assume(ty_closure, lbl, t_closure).into();
Ok(Closure { body, env })
}
(t, Term::Contract(ty, lbl)) | (t, Term::ContractWithDefault(ty, lbl, _)) => {
let mut env = HashMap::new();
let t = RichTerm {
term: Box::new(t),
pos: pos1,
};
let t_closure = t.closurize(&mut env, env1);
let ty_closure = ty.closurize(&mut env, env2);
let body = Term::Assume(ty_closure, lbl, t_closure).into();
Ok(Closure { body, env })
}
// Merge put together the fields of records, and recursively merge
// fields that are present in both terms
(Term::Record(m1), Term::Record(m2)) => {
@ -469,15 +337,6 @@ fn merge_closurize(
body.closurize(env, local_env)
}
/// Take two terms together with their environment, and return a closure representing their merge.
/// Just call [`merge_closurize`](./fn.merge_closurize.html) in a new environment and return the
/// result.
fn mk_merge_closure(t1: RichTerm, env1: Environment, t2: RichTerm, env2: Environment) -> Closure {
let mut env = HashMap::new();
let body = merge_closurize(&mut env, t1, env1, t2, env2);
Closure { body, env }
}
/// Compose two contracts, given as terms.
///
/// To compose contracts `c1` and `c2`, construct the term `fun _l x => c1 l1 (c2 l2 x)`, where

View File

@ -1,34 +1,10 @@
//! Serialization of an evaluated program to various data format.
use crate::error::SerializationError;
use crate::identifier::Ident;
use crate::label::Label;
use crate::term::{MetaValue, RichTerm, Term};
use crate::types::Types;
use serde::ser::{Error, Serialize, SerializeMap, Serializer};
use std::collections::HashMap;
/// Serializer for docstring. Ignore the meta-data and serialize the underlying term.
pub fn serialize_docstring<S>(_doc: &String, t: &RichTerm, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
t.serialize(serializer)
}
/// Serializer for a contract with a default value. Ignore the meta-data and serialize the
/// underlying term.
pub fn serialize_contract_default<S>(
_ty: &Types,
_l: &Label,
t: &RichTerm,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
t.serialize(serializer)
}
/// Serializer for metavalues.
pub fn serialize_meta_value<S>(meta: &MetaValue, serializer: S) -> Result<S::Ok, S::Error>
where
@ -72,6 +48,7 @@ impl Serialize for RichTerm {
/// Check that a term is serializable. Serializable terms are booleans, numbers, strings, enum,
/// lists of serializable terms or records of serializable terms.
pub fn validate(t: &RichTerm) -> Result<(), SerializationError> {
use crate::term;
use Term::*;
match t.term.as_ref() {
@ -84,7 +61,10 @@ pub fn validate(t: &RichTerm) -> Result<(), SerializationError> {
vec.iter().try_for_each(validate)?;
Ok(())
}
DefaultValue(ref t) | ContractWithDefault(_, _, ref t) | Docstring(_, ref t) => validate(t),
//TODO: have a specific error for such missing value.
MetaValue(term::MetaValue {
value: Some(ref t), ..
}) => validate(t),
_ => Err(SerializationError::NonSerializable(t.clone())),
}
}

View File

@ -126,36 +126,9 @@ pub enum Term {
#[serde(skip)]
Wrapped(i32, RichTerm),
/// A contract. Enriched value.
///
/// A contract at the term level. This contract is enforced when merged with a value.
#[serde(skip)]
Contract(Types, Label),
/// A default value. Enriched value.
///
/// An enriched term representing a default value. It is dropped as soon as it is merged with a
/// concrete value. Otherwise, if it lives long enough to be accessed, it evaluates to the
/// underlying term.
#[serde(skip_deserializing)]
DefaultValue(RichTerm),
/// A contract with combined with default value. Enriched value.
///
/// This is a combination generated during evaluation, when merging a contract and a default
/// value, as both need to be remembered.
#[serde(serialize_with = "crate::serialize::serialize_contract_default")]
#[serde(skip_deserializing)]
ContractWithDefault(Types, Label, RichTerm),
#[serde(serialize_with = "crate::serialize::serialize_meta_value")]
MetaValue(MetaValue),
/// A term together with its documentation string. Enriched value.
#[serde(serialize_with = "crate::serialize::serialize_docstring")]
#[serde(skip_deserializing)]
Docstring(String, RichTerm),
/// An unresolved import.
#[serde(skip)]
Import(String),
@ -207,14 +180,8 @@ pub enum StrChunk<E> {
),
}
#[cfg(test)]
impl<E> StrChunk<E> {
pub fn literal<S>(s: S) -> Self
where
S: Into<String>,
{
StrChunk::Literal(s.into())
}
pub fn expr(e: E) -> Self {
StrChunk::Expr(e, 0)
}
@ -250,24 +217,13 @@ impl Term {
func(t1);
func(t2)
}
Bool(_)
| Num(_)
| Str(_)
| Lbl(_)
| Var(_)
| Sym(_)
| Enum(_)
| Contract(_, _)
| Import(_)
Bool(_) | Num(_) | Str(_) | Lbl(_) | Var(_) | Sym(_) | Enum(_) | Import(_)
| ResolvedImport(_) => {}
Fun(_, ref mut t)
| Op1(_, ref mut t)
| Promise(_, _, ref mut t)
| Assume(_, _, ref mut t)
| Wrapped(_, ref mut t)
| DefaultValue(ref mut t)
| Docstring(_, ref mut t)
| ContractWithDefault(_, _, ref mut t) => {
| Wrapped(_, ref mut t) => {
func(t);
}
MetaValue(ref mut meta) => {
@ -313,11 +269,7 @@ impl Term {
Term::List(_) => Some("List"),
Term::Sym(_) => Some("Sym"),
Term::Wrapped(_, _) => Some("Wrapped"),
Term::Contract(_, _)
| Term::ContractWithDefault(_, _, _)
| Term::Docstring(_, _)
| Term::DefaultValue(_)
| Term::MetaValue(_) => Some("Metavalue"),
Term::MetaValue(_) => Some("Metavalue"),
Term::Let(_, _, _)
| Term::App(_, _)
| Term::Var(_)
@ -358,13 +310,6 @@ impl Term {
Term::List(_) => String::from("[ ... ]"),
Term::Sym(_) => String::from("<sym>"),
Term::Wrapped(_, _) => String::from("<wrapped>"),
Term::Contract(_, _) => String::from("<enriched:contract>"),
Term::ContractWithDefault(_, _, ref t) => {
format!("<enriched:contract,default={}>", (*t.term).shallow_repr())
}
Term::Docstring(_, ref t) => {
format!("<enriched:doc,term={}>", (*t.term).shallow_repr())
}
Term::MetaValue(ref meta) => {
let mut content = String::new();
@ -388,7 +333,6 @@ impl Term {
format!("<{}{}={}>", content, value_label, value)
}
Term::DefaultValue(ref t) => format!("<enriched:default={}", (*t.term).shallow_repr()),
Term::Var(Ident(id)) => id.clone(),
Term::Let(_, _, _)
| Term::App(_, _)
@ -421,11 +365,7 @@ impl Term {
| Term::Promise(_, _, _)
| Term::Assume(_, _, _)
| Term::Wrapped(_, _)
| Term::Contract(_, _)
| Term::DefaultValue(_)
| Term::ContractWithDefault(_, _, _)
| Term::MetaValue(_)
| Term::Docstring(_, _)
| Term::Import(_)
| Term::ResolvedImport(_)
| Term::StrChunks(_)
@ -436,11 +376,7 @@ impl Term {
/// Determine if a term is an enriched value.
pub fn is_enriched(&self) -> bool {
match self {
Term::Contract(_, _)
| Term::DefaultValue(_)
| Term::ContractWithDefault(_, _, _)
| Term::Docstring(_, _)
| Term::MetaValue(_) => true,
Term::MetaValue(_) => true,
Term::Bool(_)
| Term::Num(_)
| Term::Str(_)
@ -488,11 +424,7 @@ impl Term {
| Term::Promise(_, _, _)
| Term::Assume(_, _, _)
| Term::Wrapped(_, _)
| Term::Contract(_, _)
| Term::DefaultValue(_)
| Term::ContractWithDefault(_, _, _)
| Term::MetaValue(_)
| Term::Docstring(_, _)
| Term::Import(_)
| Term::ResolvedImport(_)
| Term::StrChunks(_)
@ -1006,52 +938,6 @@ impl RichTerm {
state,
)
}
Term::Contract(ty, label) => {
let ty = match ty {
Types(AbsType::Flat(t)) => Types(AbsType::Flat(t.traverse(f, state)?)),
ty => ty,
};
Ok(RichTerm {
term: Box::new(Term::Contract(ty, label)),
pos,
})
}
Term::DefaultValue(t) => {
let t = t.traverse(f, state)?;
f(
RichTerm {
term: Box::new(Term::DefaultValue(t)),
pos,
},
state,
)
}
Term::ContractWithDefault(ty, lbl, t) => {
let ty = match ty {
Types(AbsType::Flat(t)) => Types(AbsType::Flat(t.traverse(f, state)?)),
ty => ty,
};
let t = t.traverse(f, state)?;
f(
RichTerm {
term: Box::new(Term::ContractWithDefault(ty, lbl, t)),
pos,
},
state,
)
}
Term::Docstring(s, t) => {
let t = t.traverse(f, state)?;
f(
RichTerm {
term: Box::new(Term::Docstring(s, t)),
pos,
},
state,
)
}
Term::MetaValue(meta) => {
let contract = meta
.contract

View File

@ -132,55 +132,6 @@ pub mod share_normal_form {
RichTerm::new(Term::MetaValue(meta), pos)
}
}
Term::DefaultValue(t) => {
if should_share(&t.term) {
let fresh_var = fresh_var();
let inner = RichTerm {
term: Box::new(Term::DefaultValue(Term::Var(fresh_var.clone()).into())),
pos,
};
Term::Let(fresh_var, t, inner).into()
} else {
RichTerm {
term: Box::new(Term::DefaultValue(t)),
pos,
}
}
}
Term::ContractWithDefault(ty, lbl, t) => {
if should_share(&t.term) {
let fresh_var = fresh_var();
let inner = RichTerm {
term: Box::new(Term::ContractWithDefault(
ty,
lbl,
Term::Var(fresh_var.clone()).into(),
)),
pos,
};
Term::Let(fresh_var, t, inner).into()
} else {
RichTerm {
term: Box::new(Term::ContractWithDefault(ty, lbl, t)),
pos,
}
}
}
Term::Docstring(s, t) => {
if should_share(&t.term) {
let fresh_var = fresh_var();
let inner = RichTerm {
term: Box::new(Term::Docstring(s, Term::Var(fresh_var.clone()).into())),
pos,
};
Term::Let(fresh_var, t, inner).into()
} else {
RichTerm {
term: Box::new(Term::Docstring(s, t)),
pos,
}
}
}
t => RichTerm {
term: Box::new(t),
pos,

View File

@ -634,9 +634,6 @@ fn type_check_(
Term::Sym(_) => unify(state, strict, ty, mk_typewrapper::sym())
.map_err(|err| err.to_typecheck_err(state, &rt.pos)),
Term::Wrapped(_, t)
| Term::DefaultValue(t)
| Term::ContractWithDefault(_, _, t)
| Term::Docstring(_, t)
| Term::MetaValue(MetaValue {
contract: None,
value: Some(t),
@ -653,7 +650,7 @@ fn type_check_(
let new_ty = TypeWrapper::Ptr(new_var(state.table));
type_check_(state, envs, false, t, new_ty)
}
Term::Contract(_, _) | Term::MetaValue(_) => Ok(()),
Term::MetaValue(_) => Ok(()),
Term::Import(_) => unify(state, strict, ty, mk_typewrapper::dynamic())
.map_err(|err| err.to_typecheck_err(state, &rt.pos)),
Term::ResolvedImport(file_id) => {
@ -682,6 +679,15 @@ fn type_check_(
fn apparent_type(t: &Term, table: &mut UnifTable, strict: bool) -> TypeWrapper {
match t {
Term::Assume(ty, _, _) | Term::Promise(ty, _, _) => to_typewrapper(ty.clone()),
Term::MetaValue(MetaValue {
contract: Some((ty, _)),
..
}) => to_typewrapper(ty.clone()),
Term::MetaValue(MetaValue {
contract: None,
value: Some(v),
..
}) => apparent_type(v.as_ref(), table, strict),
_ if strict => TypeWrapper::Ptr(new_var(table)),
_ => mk_typewrapper::dynamic(),
}