summaryrefslogtreecommitdiff
path: root/dhall/src
diff options
context:
space:
mode:
authorFintanH2019-07-31 12:15:12 +0100
committerFintanH2019-07-31 12:15:12 +0100
commitf806ad6bf0fad5a720c9b87310d86838612f3b7a (patch)
tree660d08295161ce4d73ac1fd8be24a61eee996bb2 /dhall/src
parente51b89d717a1f1aaba7e59c79c9fb4b8e7031bab (diff)
Reuse the merge_maps function to implement the right-biased union
Diffstat (limited to '')
-rw-r--r--dhall/src/phase/normalize.rs2
-rw-r--r--dhall/src/phase/typecheck.rs12
2 files changed, 7 insertions, 7 deletions
diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs
index be2ba51..da19dc1 100644
--- a/dhall/src/phase/normalize.rs
+++ b/dhall/src/phase/normalize.rs
@@ -372,7 +372,7 @@ enum Ret<'a> {
Expr(ExprF<Thunk, X>),
}
-fn merge_maps<K, V>(
+pub(crate) fn merge_maps<K, V>(
map1: &HashMap<K, V>,
map2: &HashMap<K, V>,
mut f: impl FnMut(&V, &V) -> V,
diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs
index 2315edb..9107e36 100644
--- a/dhall/src/phase/typecheck.rs
+++ b/dhall/src/phase/typecheck.rs
@@ -599,6 +599,8 @@ fn type_last_layer(
Ok(RetTypeOnly(text_type))
}
BinOp(RightBiasedRecordMerge, l, r) => {
+ use crate::phase::normalize::merge_maps;
+
let l_type = l.get_type()?;
let l_kind = l_type.get_type()?;
let r_type = r.get_type()?;
@@ -614,7 +616,7 @@ fn type_last_layer(
);
// Extract the LHS record type
- let mut kts_x = match l_type.to_value() {
+ let kts_x = match l_type.to_value() {
Value::RecordType(kts) => kts,
_ => return Err(mkerr(MustCombineRecord(l.clone()))),
};
@@ -627,15 +629,13 @@ fn type_last_layer(
// Union the two records, prefering
// the values found in the RHS.
- for (label, value) in kts_y {
- kts_x.insert(label, value);
- }
+ let kts = merge_maps(&kts_x, &kts_y, |_, r_t| r_t.clone());
// Construct the final record type from the union
Ok(RetTypeOnly(tck_record_type(
ctx,
- kts_x.iter()
- .map(|(x, v)| Ok((x.clone(), v.to_type()))),
+ kts.iter()
+ .map(|(x, v)| Ok((x.clone(), v.to_type()))),
)?
.into_type()))
}