on master help sign in
Change append/combine implemantion to simply append records (#24)

Co-authored-by: kamoii <>

commit94ab29fa932e9141164212df56c5a419ccfd958e authorkamoii <52167401+kamoii@users.noreply.github.com> dateWed 25 Nov 2020, 21:34 (6 years ago) parent93e20f4
3 files changed+19−31
src/JRec.hs+1−7 src/JRec/Internal.hs+8−20 test/JRecSpec.hs+10−4
src/JRec.hs+1−7
@@ -49,13 +49,7 @@ unField _ (_ R.:= value) = value
4949-- FIXME: See spec for a bug when there are duplicates.
5050append ::
5151 forall lhs rhs res.
52− ( KnownNat (R.RecSize lhs),
53− KnownNat (R.RecSize rhs),
54− KnownNat (R.RecSize lhs + R.RecSize rhs),
55− res ~ R.RecAppend lhs rhs,
56− R.RecCopy lhs lhs res,
57− R.RecCopy rhs rhs res
58− ) =>
52+ res ~ R.RecAppend lhs rhs =>
5953 Rec lhs ->
6054 Rec rhs ->
6155 Rec res
src/JRec/Internal.hs+8−20
@@ -403,26 +403,20 @@ type family RecDeepTy (ps :: r) (lts :: [*]) :: * where
403403-- NOTE: changed from original superrecord to not require a 'Sort'
404404combine ::
405405 forall lhs rhs res.
406− ( KnownNat (RecSize lhs),
407− KnownNat (RecSize rhs),
408− KnownNat (RecSize lhs + RecSize rhs),
409− res ~ RecAppend lhs rhs,
410− RecCopy lhs lhs res,
411− RecCopy rhs rhs res
412− ) =>
406+ res ~ RecAppend lhs rhs =>
413407 Rec lhs ->
414408 Rec rhs ->
415409 Rec res
416−combine lts rts =
417− let !(I# size#) =
418− fromIntegral $ natVal' (proxy# :: Proxy# (RecSize lhs + RecSize rhs))
410+combine (MkRec arr0#) (MkRec arr1#) =
411+ let !size0# = sizeofSmallArray# arr0#
412+ !size1# = sizeofSmallArray# arr1#
419413 in runST' $
420414 ST $ \s# ->
421− case newSmallArray# size# (error "No value") s# of
415+ case newSmallArray# (size0# +# size1#) (error "No value") s# of
422416 (# s'#, arr# #) ->
423− case recCopyInto (Proxy :: Proxy lhs) lts (Proxy :: Proxy res) arr# s'# of
417+ case copySmallArray# arr1# 0# arr# 0# size1# s'# of
424418 s''# ->
425− case recCopyInto (Proxy :: Proxy rhs) rts (Proxy :: Proxy res) arr# s''# of
419+ case copySmallArray# arr0# 0# arr# size1# size0# s''# of
426420 s'''# ->
427421 case unsafeFreezeSmallArray# arr# s'''# of
428422 (# s''''#, a# #) -> (# s''''#, MkRec a# #)
@@ -492,13 +486,7 @@ insert (l := v) rts =
492486-- | Alias for 'combine'
493487(++:) ::
494488 forall lhs rhs res.
495− ( KnownNat (RecSize lhs),
496− KnownNat (RecSize rhs),
497− KnownNat (RecSize lhs + RecSize rhs),
498− res ~ RecAppend lhs rhs,
499− RecCopy lhs lhs res,
500− RecCopy rhs rhs res
501− ) =>
489+ res ~ RecAppend lhs rhs =>
502490 Rec lhs ->
503491 Rec rhs ->
504492 Rec res
test/JRecSpec.hs+10−4
@@ -71,11 +71,17 @@ spec = do
7171 it "simple append" $ do
7272 Rec (#a := 1) `append` Rec (#b := 2)
7373 `shouldBe` Rec (#a := 1, #b := 2)
74+ Rec () `append` Rec (#b := 2)
75+ `shouldBe` Rec (#b := 2)
76+ Rec (#a := 1) `append` Rec ()
77+ `shouldBe` Rec (#a := 1)
78+ Rec (#a := 1, #b := 2) `append` Rec (#c := 3)
79+ `shouldBe` Rec (#a := 1, #b := 2, #c := 3)
80+ Rec (#a := 1) `append` Rec (#b := 2, #c := 3)
81+ `shouldBe` Rec (#a := 1, #b := 2, #c := 3)
82+ Rec (#a := 1, #b := 2) `append` Rec (#c := 3, #d := 4)
83+ `shouldBe` Rec (#a := 1, #b := 2, #c := 3, #d := 4)
7484 it "append with duplicates" $ do
75− -- TODO: `append` will not deal with duplicates; based on current
76− -- implementation, the last value (here, 8) will overwrite all duplicate
77− -- fields. See `union` if you want to deal with duplicates sensibly.
78− pendingWith "append should just append?"
7985 let r1 = Rec (#b := 5, #a := 6)
8086 r2 = Rec (#c := 7, #a := 8)
8187 r1 `append` r2