Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
stdpp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Herman Bergwerf
stdpp
Commits
6fb7d791
Commit
6fb7d791
authored
3 years ago
by
Robbert Krebbers
Browse files
Options
Downloads
Patches
Plain Diff
Very preliminary version of a quick start guide for sets.
parent
4bea88d1
Branches
robbert/set_guide
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
guides/sets.v
+99
-0
99 additions, 0 deletions
guides/sets.v
with
99 additions
and
0 deletions
guides/sets.v
0 → 100644
+
99
−
0
View file @
6fb7d791
From
stdpp
Require
Import
gmap
sorting
.
(* [gset K] is the type of sets you probably want to use. It enjoys good
properties w.r.t. computation (most operations are logarithmic or linear) and
equality. It works for any type [K] that is countable (see type class
[Countable]). There are [Countable] instances for all the usual types, [nat],
[list nat], [nat * nat], [gset nat], etc. *)
Check
gset
.
(* All the set operations are overloaded via type classes, but there are
instances for [gset]. The most important operations are:
- [elem_of], notation [∈]
- [subseteq], notation [⊆]
- [subset], notation [⊂]
- [empty], notation [∅]
- [singleton], notation [ {[ x ]} ]
- [union], notation [∪]
- [intersection], notation [∩]
- [difference], notation [∖]
*)
(* Let us try to type check some stuff. *)
Check
{[
10
]}
:
gset
nat
.
Check
{[
[
10
]
]}
:
gset
(
list
nat
)
.
Check
{[
{[
10
]}
]}
:
gset
(
gset
nat
)
.
Check
(
λ
X
:
gset
nat
,
X
∪
{[
10
]})
.
(** And let write some lemmas. The most useful tactic is [set_solver]. *)
Lemma
some_stuff
(
X
Y
Z
:
gset
nat
)
:
X
∪
Y
∩
X
∪
Z
∩
X
=
(
Y
∪
X
∪
Z
∖
∅
∪
X
)
∩
X
.
Proof
.
set_solver
.
Qed
.
Lemma
some_stuff_poly
`{
Countable
A
}
(
X
Y
Z
:
gset
nat
)
:
X
∪
Y
∩
X
∪
Z
∩
X
=
(
Y
∪
X
∪
Z
∖
∅
∪
X
)
∩
X
.
Proof
.
set_solver
.
Qed
.
(** If you want to search for lemmas, search for the operations, not [gset]
since all lemmas are overloaded. *)
Search
difference
intersection
.
(** Some important notes:
- The lemmas look a bit dounting because of the additional arguments due to
type classes, but these arguments can mostly be ignored
- There are both lemmas about Leibniz equality [=] and setoid equality [≡].
The first ones are suffixed [_L]. For [gset] you always want to use [=] (and
thus the [_L] lemmas) because we have [X ≡ Y ↔ X = Y]. This is not the case
for other implementations of sets, like [propset A := A → Prop] or
[listset A := list A], hence [≡] is useful in the general case. *)
(** Some other examples *)
Definition
evens
(
X
:
gset
nat
)
:
gset
nat
:=
filter
(
λ
x
,
(
2
|
x
))
X
.
Definition
intersection_alt
(
X
Y
:
gset
nat
)
:
gset
nat
:=
filter
(.
∈
Y
)
X
.
Definition
add_one
(
X
:
gset
nat
)
:
gset
nat
:=
set_map
S
X
.
Definition
until_n
(
n
:
nat
)
:
gset
nat
:=
set_seq
0
n
.
(** Keep in mind that [filter], [set_map], [set_seq], etc are overloaded via
type classes. So, you need sufficient type information in your definitions and
you won't find much about them when searching for [gset]. *)
(** When computing with sets, always make sure to cast the result that you want
to display is a simple type like [nat], [list nat], [bool], etc. The result
of a [gset] computation is a big sigma type with lots of noise, so it won't be
pretty (or useful) to look at. *)
Eval
vm_compute
in
(
elements
(
add_one
(
evens
{[
10
;
11
;
14
]})))
.
Eval
vm_compute
in
(
elements
(
evens
(
until_n
40
)))
.
(** [elements] converts a set to a list. They are not sorted, but you can do
that yourself. *)
Eval
vm_compute
in
(
merge_sort
(
≤
)
(
elements
(
evens
(
until_n
40
))))
.
Eval
vm_compute
in
(
fresh
({[
10
;
12
]}
:
gset
nat
))
.
Eval
vm_compute
in
(
size
({[
10
;
12
]}
:
gset
nat
))
.
(** You can use [bool_decide] to turn decidable [Prop]s into [bool]s. *)
Eval
vm_compute
in
bool_decide
(
10
∈
evens
{[
10
;
11
]})
.
Eval
vm_compute
in
(
bool_decide
({[
10
;
14
;
17
]}
⊂
until_n
40
))
.
Eval
vm_compute
in
(
bool_decide
(
set_Forall
(
λ
x
,
(
2
|
x
))
(
evens
(
until_n
40
))))
.
(** Want to know more:
- Look up the definitions of the type classes for [union], [intersection], etc.,
interfaces [SimpleSet], [Set_], etc. in [theories/base.v].
- Look up the generic theory of sets in [theories/sets.v].
- Look up the generic theory of finite sets in [theories/fin_sets.v].
- Probably don't look into the implementation of [gset] in [theories/gmap.v],
unless you are very interested in encodings as bit strings and radix-2 search
trees. You should treat [gset] as a black box. *)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment