Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-1539: Use foldr for Foldable #5179

Merged
merged 3 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plutus-benchmark/nofib/test/formulaBudget.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 26593391908
| mem: 114092648})
({cpu: 26160853908
| mem: 112212048})
4 changes: 2 additions & 2 deletions plutus-benchmark/nofib/test/knightsBudget.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 7596820298
| mem: 28604240})
({cpu: 7353687298
| mem: 27547140})
4 changes: 2 additions & 2 deletions plutus-benchmark/nofib/test/queens4budget.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 14229364305
| mem: 54977742})
({cpu: 14180259305
| mem: 54764242})
4 changes: 2 additions & 2 deletions plutus-benchmark/nofib/test/queens5budget.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 191619634648
| mem: 725849980})
({cpu: 191451412648
| mem: 725118580})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 490859997
| mem: 1606369})
({cpu: 460200997
| mem: 1473069})
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 141343245
| mem: 464193})
({cpu: 133868245
| mem: 431693})
4 changes: 2 additions & 2 deletions plutus-tx-plugin/test/Budget/allCheap.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 28136630
| mem: 110020})
({cpu: 16383630
| mem: 58920})
86 changes: 27 additions & 59 deletions plutus-tx-plugin/test/Budget/allCheap.plc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,33 @@ letrec data (List :: * -> *) a | Nil_match where
Nil : List a
Cons : a -> List a -> List a
in
let data (Monoid :: * -> *) a | Monoid_match where
CConsMonoid : (\a -> a -> a -> a) a -> a -> Monoid a
in
letrec !fFoldableNil_cfoldMap
: all m. all a. Monoid m -> (a -> m) -> List a -> m
= /\m a ->
\(dMonoid : Monoid m) (eta : a -> m) (eta : List a) ->
Nil_match
{a} eta {all dead. m} (/\dead ->
Monoid_match
{m} dMonoid {m}
(\(v : (\a -> a -> a -> a) m) (v
: m) ->
v))
(\(x : a) (xs : List a) ->
/\dead ->
Monoid_match
{m} dMonoid {(\a -> a -> a -> a) m}
(\(v : (\a -> a -> a -> a) m) (v : m) -> v) (eta x)
(fFoldableNil_cfoldMap {m} {a} dMonoid eta xs))
{all dead. dead}
in
let data Bool | Bool_match where
True : Bool
False : Bool
data (MultiplicativeMonoid :: * -> *) a | MultiplicativeMonoid_match where
CConsMultiplicativeMonoid
: (\a -> a -> a -> a) a -> a -> MultiplicativeMonoid a
in fFoldableNil_cfoldMap
{(\a -> a) Bool} {integer} (let !v : MultiplicativeMonoid Bool
= CConsMultiplicativeMonoid
{Bool} (\(l : Bool) (r : Bool) ->
Bool_match
l {all dead. Bool}
(/\dead -> r) (/\dead ->
False)
{all dead. dead}) True
in CConsMonoid
{(\a -> a) Bool}
(\(eta : (\a -> a) Bool) (eta
: (\a -> a) Bool) ->
MultiplicativeMonoid_match
{Bool} v {(\a -> a -> a -> a) Bool}
(\(v : (\a -> a -> a -> a) Bool) (v
: Bool) ->
v) eta eta)
(MultiplicativeMonoid_match
{Bool} v {Bool}
(\(v : (\a -> a -> a -> a) Bool) (v
: Bool) ->
v))) (\(v : integer) ->
ifThenElse
{Bool}
(lessThanEqualsInteger
1 v) False True)
((let a = List integer in \(c : integer -> a -> a) (n : a) ->
c
1 (c
2 (c
3 (c
in
letrec !go : List integer -> Bool
= \(ds : List integer) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prettyprinting improvement: always put the binding on the next line so we can indent it just 2 instead of 7 for letrec

Nil_match
{integer} ds {all dead. Bool} (/\dead -> True)
(\(x : integer) (xs : List integer) ->
/\dead ->
let !acc : Bool = go xs in Bool_match
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why didn't we break at the in here?

(ifThenElse
{Bool}
(lessThanEqualsInteger 1 x)
False True) {all dead. Bool}
(/\dead -> acc) (/\dead ->
False)
{all dead. dead})
{all dead. dead}
Comment on lines +15 to +23
Copy link
Contributor

@effectfully effectfully Feb 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So everything works just fine except GHC makes a thunk for go xs and pulls it out of ifThenElse. And we interpret it as a strict binding.

in
let !eta : List integer
= (let a = List integer in \(c : integer -> a -> a) (n : a) ->
c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is hilarious but I'm not sure what we could do better

1 (c
2 (c
3
(c
4
(c
5
Expand All @@ -76,5 +43,6 @@ in fFoldableNil_cfoldMap
(c
10
n))))))))))
(\(ds : integer) (ds : List integer) -> Cons {integer} ds ds)
(Nil {integer}))
(\(ds : integer) (ds : List integer) -> Cons {integer} ds ds)
(Nil {integer})
in go eta
4 changes: 2 additions & 2 deletions plutus-tx-plugin/test/Budget/allEmptyList.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 1748100
| mem: 7700})
({cpu: 736100
| mem: 3300})
71 changes: 18 additions & 53 deletions plutus-tx-plugin/test/Budget/allEmptyList.plc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,24 @@ letrec data (List :: * -> *) a | Nil_match where
Nil : List a
Cons : a -> List a -> List a
in
let data (Monoid :: * -> *) a | Monoid_match where
CConsMonoid : (\a -> a -> a -> a) a -> a -> Monoid a
in
letrec !fFoldableNil_cfoldMap
: all m. all a. Monoid m -> (a -> m) -> List a -> m
= /\m a ->
\(dMonoid : Monoid m) (eta : a -> m) (eta : List a) ->
Nil_match
{a} eta {all dead. m} (/\dead ->
Monoid_match
{m} dMonoid {m}
(\(v : (\a -> a -> a -> a) m) (v
: m) ->
v))
(\(x : a) (xs : List a) ->
/\dead ->
Monoid_match
{m} dMonoid {(\a -> a -> a -> a) m}
(\(v : (\a -> a -> a -> a) m) (v : m) -> v) (eta x)
(fFoldableNil_cfoldMap {m} {a} dMonoid eta xs))
{all dead. dead}
in
let data Bool | Bool_match where
True : Bool
False : Bool
data (MultiplicativeMonoid :: * -> *) a | MultiplicativeMonoid_match where
CConsMultiplicativeMonoid
: (\a -> a -> a -> a) a -> a -> MultiplicativeMonoid a
in fFoldableNil_cfoldMap
{(\a -> a) Bool} {integer} (let !v : MultiplicativeMonoid Bool
= CConsMultiplicativeMonoid
{Bool} (\(l : Bool) (r : Bool) ->
Bool_match
l {all dead. Bool}
(/\dead -> r) (/\dead ->
False)
{all dead. dead}) True
in CConsMonoid
{(\a -> a) Bool}
(\(eta : (\a -> a) Bool) (eta
: (\a -> a) Bool) ->
MultiplicativeMonoid_match
{Bool} v {(\a -> a -> a -> a) Bool}
(\(v : (\a -> a -> a -> a) Bool) (v
: Bool) ->
v) eta eta)
(MultiplicativeMonoid_match
{Bool} v {Bool}
(\(v : (\a -> a -> a -> a) Bool) (v
: Bool) ->
v))) (\(v : integer) ->
ifThenElse
{Bool}
(lessThanEqualsInteger
1 v) False True)
(Nil {integer})
in
letrec !go : List integer -> Bool
= \(ds : List integer) ->
Nil_match
{integer} ds {all dead. Bool} (/\dead -> True)
(\(x : integer) (xs : List integer) ->
/\dead ->
let !acc : Bool = go xs in Bool_match
(ifThenElse
{Bool}
(lessThanEqualsInteger 1 x)
False True) {all dead. Bool}
(/\dead -> acc) (/\dead ->
False)
{all dead. dead})
{all dead. dead}
in
let !eta : List integer = Nil {integer} in go eta
4 changes: 2 additions & 2 deletions plutus-tx-plugin/test/Budget/allExpensive.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 28136630
| mem: 110020})
({cpu: 16383630
| mem: 58920})
86 changes: 27 additions & 59 deletions plutus-tx-plugin/test/Budget/allExpensive.plc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,33 @@ letrec data (List :: * -> *) a | Nil_match where
Nil : List a
Cons : a -> List a -> List a
in
let data (Monoid :: * -> *) a | Monoid_match where
CConsMonoid : (\a -> a -> a -> a) a -> a -> Monoid a
in
letrec !fFoldableNil_cfoldMap
: all m. all a. Monoid m -> (a -> m) -> List a -> m
= /\m a ->
\(dMonoid : Monoid m) (eta : a -> m) (eta : List a) ->
Nil_match
{a} eta {all dead. m} (/\dead ->
Monoid_match
{m} dMonoid {m}
(\(v : (\a -> a -> a -> a) m) (v
: m) ->
v))
(\(x : a) (xs : List a) ->
/\dead ->
Monoid_match
{m} dMonoid {(\a -> a -> a -> a) m}
(\(v : (\a -> a -> a -> a) m) (v : m) -> v) (eta x)
(fFoldableNil_cfoldMap {m} {a} dMonoid eta xs))
{all dead. dead}
in
let data Bool | Bool_match where
True : Bool
False : Bool
data (MultiplicativeMonoid :: * -> *) a | MultiplicativeMonoid_match where
CConsMultiplicativeMonoid
: (\a -> a -> a -> a) a -> a -> MultiplicativeMonoid a
in fFoldableNil_cfoldMap
{(\a -> a) Bool} {integer} (let !v : MultiplicativeMonoid Bool
= CConsMultiplicativeMonoid
{Bool} (\(l : Bool) (r : Bool) ->
Bool_match
l {all dead. Bool}
(/\dead -> r) (/\dead ->
False)
{all dead. dead}) True
in CConsMonoid
{(\a -> a) Bool}
(\(eta : (\a -> a) Bool) (eta
: (\a -> a) Bool) ->
MultiplicativeMonoid_match
{Bool} v {(\a -> a -> a -> a) Bool}
(\(v : (\a -> a -> a -> a) Bool) (v
: Bool) ->
v) eta eta)
(MultiplicativeMonoid_match
{Bool} v {Bool}
(\(v : (\a -> a -> a -> a) Bool) (v
: Bool) ->
v))) (\(v : integer) ->
ifThenElse
{Bool}
(lessThanEqualsInteger
11 v) False True)
((let a = List integer in \(c : integer -> a -> a) (n : a) ->
c
1 (c
2 (c
3 (c
in
letrec !go : List integer -> Bool
= \(ds : List integer) ->
Nil_match
{integer} ds {all dead. Bool} (/\dead -> True)
(\(x : integer) (xs : List integer) ->
/\dead ->
let !acc : Bool = go xs in Bool_match
(ifThenElse
{Bool}
(lessThanEqualsInteger 11 x)
False True) {all dead. Bool}
(/\dead -> acc) (/\dead ->
False)
{all dead. dead})
{all dead. dead}
in
let !eta : List integer
= (let a = List integer in \(c : integer -> a -> a) (n : a) ->
c
1 (c
2 (c
3
(c
4
(c
5
Expand All @@ -76,5 +43,6 @@ in fFoldableNil_cfoldMap
(c
10
n))))))))))
(\(ds : integer) (ds : List integer) -> Cons {integer} ds ds)
(Nil {integer}))
(\(ds : integer) (ds : List integer) -> Cons {integer} ds ds)
(Nil {integer})
in go eta
4 changes: 2 additions & 2 deletions plutus-tx-plugin/test/Budget/anyCheap.budget.golden
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
({cpu: 28136630
| mem: 110020})
({cpu: 16383630
| mem: 58920})
Loading