Skip to content

Commit

Permalink
simplify foldMap interface to avoid having the silly fn interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianAlgehed committed Apr 17, 2024
1 parent 208819b commit b7c2839
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Expand Up @@ -152,7 +152,7 @@ utxoTxSpec env st =
outputList
depositSum =
foldMap_
(composeFn fstFn toGenericFn)
fst_
proposalsList
in outputSum + depositSum + ctbTxfee + ctbTreasuryDonation ==. totalValueConsumed
]
Expand Down
14 changes: 11 additions & 3 deletions libs/constrained-generators/src/Constrained/Base.hs
Expand Up @@ -3664,17 +3664,25 @@ sum_ ::
) =>
Term fn [a] ->
Term fn a
sum_ = app (foldMapFn idFn)
sum_ = foldMap_ id

foldMap_ ::
forall fn a b.
( BaseUniverse fn
, Foldy fn b
, HasSpec fn a
) =>
fn '[a] b ->
(Term fn a -> Term fn b) ->
Term fn [a] ->
Term fn b
foldMap_ fn = app (foldMapFn fn)
foldMap_ f = app $ foldMapFn $ toFn $ f (V v)
where
v = Var (-1) :: Var a
toFn :: forall b. HasCallStack => Term fn b -> fn '[a] b
toFn (App fn (V v' :> Nil)) | Just Refl <- eqVar v v' = fn
toFn (App fn (t :> Nil)) = injectFn $ Compose fn (toFn t)
toFn (V v') | Just Refl <- eqVar v v' = idFn
toFn _ = error "foldMap_ has not been given a function on the form \\ x -> f (g ... (h x))"

-- Language constructs ----------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion libs/constrained-generators/src/Constrained/Test.hs
Expand Up @@ -585,7 +585,7 @@ listSumElemRange = constrained $ \xs ->

listSumPair :: Numbery a => Spec BaseFn [(a, Int)]
listSumPair = constrained $ \xs ->
[ assert $ foldMap_ (composeFn fstFn toGenericFn) xs ==. 100
[ assert $ foldMap_ fst_ xs ==. 100
, forAll' xs $ \x y -> [20 <. x, x <. 30, y <. 100]
]

Expand Down

0 comments on commit b7c2839

Please sign in to comment.