diff options
author | FintanH | 2019-08-03 18:50:00 +0100 |
---|---|---|
committer | FintanH | 2019-08-03 18:50:00 +0100 |
commit | a390b54b6a55a348b7fc000f9f81d395996a32c1 (patch) | |
tree | 17c4f09f97961c2f7e23503548dd50fdebbf5f21 /dhall/src | |
parent | fc309bf41dfe53d043766ec9d468389b2b4269ae (diff) | |
parent | 9113e15534f57626ae9be662738e1d3d22434e88 (diff) |
Merge remote-tracking branch 'origin/master' into fintan/typecheck-combine-types
Diffstat (limited to '')
-rw-r--r-- | dhall/src/error/mod.rs | 2 | ||||
-rw-r--r-- | dhall/src/phase/normalize.rs | 2 | ||||
-rw-r--r-- | dhall/src/phase/typecheck.rs | 52 |
3 files changed, 50 insertions, 6 deletions
diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index 2f65443..aef84a9 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -77,9 +77,11 @@ pub(crate) enum TypeMessage { MergeAnnotMismatch, MergeHandlerTypeMismatch, MergeHandlerReturnTypeMustNotBeDependent, + MustCombineRecord(Typed), ProjectionMustBeRecord, ProjectionMissingEntry, Sort, + RecordMismatch(Typed, Typed), RecordTypeDuplicateField, RecordTypeMergeRequiresRecordType(Type), RecordTypeMismatch(Type, Type, Type, Type), diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index 653f415..4a25ed9 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -404,7 +404,7 @@ where kvs } -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 bd8e1bb..3bff7ca 100644 --- a/dhall/src/phase/typecheck.rs +++ b/dhall/src/phase/typecheck.rs @@ -598,6 +598,47 @@ 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()?; + let r_kind = r_type.get_type()?; + + // Check the equality of kinds. + // This is to disallow expression such as: + // "{ x = Text } // { y = 1 }" + ensure_equal!( + l_kind, + r_kind, + mkerr(RecordMismatch(l.clone(), r.clone())), + ); + + // Extract the LHS record type + let kts_x = match l_type.to_value() { + Value::RecordType(kts) => kts, + _ => return Err(mkerr(MustCombineRecord(l.clone()))), + }; + + // Extract the RHS record type + let kts_y = match r_type.to_value() { + Value::RecordType(kts) => kts, + _ => return Err(mkerr(MustCombineRecord(r.clone()))), + }; + + // Union the two records, prefering + // the values found in the RHS. + 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.iter() + .map(|(x, v)| Ok((x.clone(), v.to_type()))), + )? + .into_type())) + } BinOp(RecursiveRecordTypeMerge, l, r) => { // A recursive function to dig down into // records of records when merging. @@ -699,6 +740,7 @@ fn type_last_layer( NaturalTimes => Natural, TextAppend => Text, ListAppend => unreachable!(), + RightBiasedRecordMerge => unreachable!(), RecursiveRecordTypeMerge => unreachable!(), _ => return Err(mkerr(Unimplemented)), })?; @@ -1213,11 +1255,11 @@ mod spec_tests { ti_success!(ti_success_unit_RecursiveRecordTypeMergeTwo, "unit/RecursiveRecordTypeMergeTwo"); ti_success!(ti_success_unit_RecursiveRecordTypeMergeTwoKinds, "unit/RecursiveRecordTypeMergeTwoKinds"); ti_success!(ti_success_unit_RecursiveRecordTypeMergeTwoTypes, "unit/RecursiveRecordTypeMergeTwoTypes"); - // ti_success!(ti_success_unit_RightBiasedRecordMergeRhsEmpty, "unit/RightBiasedRecordMergeRhsEmpty"); - // ti_success!(ti_success_unit_RightBiasedRecordMergeTwo, "unit/RightBiasedRecordMergeTwo"); - // ti_success!(ti_success_unit_RightBiasedRecordMergeTwoDifferent, "unit/RightBiasedRecordMergeTwoDifferent"); - // ti_success!(ti_success_unit_RightBiasedRecordMergeTwoKinds, "unit/RightBiasedRecordMergeTwoKinds"); - // ti_success!(ti_success_unit_RightBiasedRecordMergeTwoTypes, "unit/RightBiasedRecordMergeTwoTypes"); + ti_success!(ti_success_unit_RightBiasedRecordMergeRhsEmpty, "unit/RightBiasedRecordMergeRhsEmpty"); + ti_success!(ti_success_unit_RightBiasedRecordMergeTwo, "unit/RightBiasedRecordMergeTwo"); + ti_success!(ti_success_unit_RightBiasedRecordMergeTwoDifferent, "unit/RightBiasedRecordMergeTwoDifferent"); + ti_success!(ti_success_unit_RightBiasedRecordMergeTwoKinds, "unit/RightBiasedRecordMergeTwoKinds"); + ti_success!(ti_success_unit_RightBiasedRecordMergeTwoTypes, "unit/RightBiasedRecordMergeTwoTypes"); ti_success!(ti_success_unit_SomeTrue, "unit/SomeTrue"); ti_success!(ti_success_unit_Text, "unit/Text"); ti_success!(ti_success_unit_TextLiteral, "unit/TextLiteral"); |