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

[Merged by Bors] - fix(data/{Finset,Multiset}): better line breaks in Repr #6333

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Mathlib/Data/Finset/Sort.lean
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ theorem orderEmbOfCardLe_mem (s : Finset α) {k : ℕ} (h : k ≤ s.card) (a) :

end SortLinearOrder

unsafe instance [Repr α] : Repr (Finset α) :=
⟨fun s _ => repr s.1⟩
unsafe instance [Repr α] : Repr (Finset α) where
reprPrec s _ :=
-- multiset uses `0` not `∅` for empty sets
if s.card = 0 then "∅" else repr s.1

end Finset
8 changes: 6 additions & 2 deletions Mathlib/Data/Multiset/Sort.lean
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ theorem sort_singleton (a : α) : sort r {a} = [a] :=
end sort

-- TODO: use a sort order if available, gh-18166
unsafe instance [Repr α] : Repr (Multiset α) :=
⟨fun s _ => "{" ++ Std.Format.joinSep (s.unquot.map repr) ", " ++ "}"⟩
unsafe instance [Repr α] : Repr (Multiset α) where
reprPrec s _ :=
if Multiset.card s = 0 then
"0"
else
Std.Format.bracket "{" (Std.Format.joinSep (s.unquot.map repr) ("," ++ Std.Format.line)) "}"

end Multiset
11 changes: 11 additions & 0 deletions test/finset_repr.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Mathlib.Data.Finset.Sort

#eval show Lean.MetaM Unit from guard <|
(repr (0 : Multiset (List ℕ)) |>.pretty 15) = "0"
Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests might be clearer with a simple #guard_msgs, rather than mentioning MetaM.

Copy link
Member Author

Choose a reason for hiding this comment

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

I specifically want to use a narrow width, so would prefer to test equality.

Is there a better way to write simple assertions than what I do here?

Copy link
Member Author

Choose a reason for hiding this comment

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

There are a handful of other tests in this style, so I'm going to leave it as is for now

bors merge

#eval show Lean.MetaM Unit from guard <|
(repr ({[1, 2, 3], [4, 5, 6]} : Multiset (List ℕ)) |>.pretty 15) = "{[1, 2, 3],\n [4, 5, 6]}"

#eval show Lean.MetaM Unit from guard <|
(repr (∅ : Finset (List ℕ)) |>.pretty 15) = "∅"
#eval show Lean.MetaM Unit from guard <|
(repr ({[1, 2, 3], [4, 5, 6]} : Finset (List ℕ)) |>.pretty 15) = "{[1, 2, 3],\n [4, 5, 6]}"