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

feat(Set): Add set notation for finite sets #9

Merged
merged 7 commits into from
May 26, 2021
Merged
Changes from 4 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
28 changes: 28 additions & 0 deletions Mathlib/Set.lean
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def univ : Set α := {a | True }
protected def insert (a : α) (s : Set α) : Set α :=
{b | b = a ∨ b ∈ s}

protected def singleton (a : α) : Set α :=
{b | b = a}

protected def union (s₁ s₂ : Set α) : Set α :=
{a | a ∈ s₁ ∨ a ∈ s₂}

Expand Down Expand Up @@ -151,4 +154,29 @@ instance : LawfulFunctor Set where
λ ⟨b, ⟨⟨a, ⟨h₁, h₂⟩⟩, h₃⟩⟩ => ⟨a, ⟨h₁, show h (g a) = c from h₂ ▸ h₃⟩⟩⟩
map_const := rfl

syntax (priority := high) "{ " term,* " }" : term
shingtaklam1324 marked this conversation as resolved.
Show resolved Hide resolved

open Lean Macro in
macro_rules
| `({ $elems:term,* }) => do
let n := elems.elemsAndSeps.size
if n = 0 then throwUnsupported
let rec expandSetLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do
match i, skip with
| 0, _ => result
| i + 1, true => expandSetLit i false result
| i + 1, false => expandSetLit i true (← ``(Set.insert $(elems.elemsAndSeps[i]) $result))
let some hd ← pure $ elems.elemsAndSeps.back? | throwUnsupported
expandSetLit (n - 1) true (← ``(Set.singleton $hd))
shingtaklam1324 marked this conversation as resolved.
Show resolved Hide resolved

@[appUnexpander Set.singleton]
def singletonUnexpander : Lean.PrettyPrinter.Unexpander
| `(Set.singleton $a) => `({ $a })
| _ => throw ()

@[appUnexpander Set.insert]
def insertUnexpander : Lean.PrettyPrinter.Unexpander
| `(Set.insert $a { $ts,* }) => `({$a, $ts,*})
| _ => throw ()

end Set