Skip to content

Commit

Permalink
Fix for punionWith argument-flipping, issue #556
Browse files Browse the repository at this point in the history
  • Loading branch information
blamario committed Aug 23, 2022
1 parent ff0ccb3 commit c1ed4b9
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions Plutarch/Api/V1/AssocMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ punionWith = phoistAcyclic $

data MapUnionCarrier k v s = MapUnionCarrier
{ merge :: Term s (PBuiltinListOfPairs k v :--> PBuiltinListOfPairs k v :--> PBuiltinListOfPairs k v)
, mergeInsert :: Term s (PBuiltinPair (PAsData k) (PAsData v) :--> PBuiltinListOfPairs k v :--> PBuiltinListOfPairs k v :--> PBuiltinListOfPairs k v)
, mergeInsert :: Term s (PBuiltinPair (PAsData k) (PAsData v) :--> PBuiltinPair (PAsData k) (PAsData v) :--> PBuiltinListOfPairs k v :--> PBuiltinListOfPairs k v :--> PBuiltinListOfPairs k v)
}
deriving stock (Generic)
deriving anyclass (PlutusType)
Expand All @@ -371,33 +371,37 @@ mapUnionCarrier = phoistAcyclic $ plam \combine self ->
MapUnionCarrier
{ merge = plam $ \xs ys -> pmatch xs $ \case
PNil -> ys
PCons x xs' -> mergeInsert # x # xs' # ys
, mergeInsert = plam $ \x xs ys ->
pmatch ys $ \case
PNil -> pcons # x # xs
PCons y1 ys' ->
plet y1 $ \y ->
plet (pfstBuiltin # x) $ \xk ->
plet (pfstBuiltin # y) $ \yk ->
pif
(xk #== yk)
( pcons
# (ppairDataBuiltin # xk #$ combine # (psndBuiltin # x) # (psndBuiltin # y))
#$ merge
# xs
# ys'
)
( pif
(pfromData xk #< pfromData yk)
( pcons
# x
# (mergeInsert # y # ys' # xs)
PCons x xs' -> pmatch ys $ \case
PNil -> xs
PCons y ys' -> mergeInsert # x # y # xs' # ys'
, mergeInsert = plam $ \x y xs ys ->
plet (pfstBuiltin # x) $ \xk ->
plet (pfstBuiltin # y) $ \yk ->
pif
(xk #== yk)
( pcons
# (ppairDataBuiltin # xk #$ combine # (psndBuiltin # x) # (psndBuiltin # y))
#$ merge
# xs
# ys
)
( pif
(pfromData xk #< pfromData yk)
( pcons
# x
# ( pmatch xs $ \case
PNil -> pcons # y # ys
PCons x' xs' -> mergeInsert # x' # y # xs' # ys
)
( pcons
# y
# (mergeInsert # x # xs # ys')
)
( pcons
# y
# ( pmatch ys $ \case
PNil -> pcons # x # xs
PCons y' ys' -> mergeInsert # x # y' # xs # ys'
)
)
)
)
}

mapUnion :: forall k v s. (POrd k, PIsData k) => Term s ((PAsData v :--> PAsData v :--> PAsData v) :--> MapUnionCarrier k v)
Expand Down

0 comments on commit c1ed4b9

Please sign in to comment.