Skip to content

Commit d5af074

Browse files
committed
Updated todo and readme. Cleaned up ticket_lock_advanced. Added contributors.
1 parent 5e8a639 commit d5af074

3 files changed

Lines changed: 45 additions & 103 deletions

File tree

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Iris Tutorial
22
The Iris Tutorial is an introduction to the [Iris separation logic framework](https://iris-project.org/) and how to work with its [Coq formalization](https://gitlab.mpi-sws.org/iris/iris/).
33

4-
The exposition is intended for a broad range of readers from advanced undergraduates to PhD students and researchers. We assume that readers are already motivated to learn Iris and thus present the material in a bottom-up fashion, rather than starting out with cool motivating examples. The tutorial material is intended to be self-contained. No specific background in logic or programming languages is assumed but some familiarity with basic programming languages theory and discrete mathematics will be beneficial, see e.g. [TAPL](https://www.cis.upenn.edu/~bcpierce/tapl/). Additionally, basic knowledge of the Coq proof assistant is assumed. Advanced Coq tactics have been purposefully kept to a minimum, and some proofs are longer than necessary to facilitate learning.
4+
The exposition is intended for a broad range of readers from advanced undergraduates to PhD students and researchers. We assume that readers are already motivated to learn Iris and thus present the material in a bottom-up fashion, rather than starting out with cool motivating examples. The tutorial material is intended to be self-contained. No specific background in logic or programming languages is assumed but some familiarity with basic programming languages theory and discrete mathematics will be beneficial, see e.g. [TAPL](https://www.cis.upenn.edu/~bcpierce/tapl/). Additionally, basic knowledge of the Coq proof assistant is assumed. Advanced Coq tactics have been purposefully kept to a minimum, and some proofs are longer than necessary to facilitate learning. We demonstrate more advanced tactics in chapter [ticket_lock_advanced](/theories/ticket_lock_advanced.v).
55

66
The tutorial comes in two versions:
77

@@ -57,6 +57,7 @@ Iris makes extensive use of Unicode characters. [This guide](https://gitlab.mpi-
5757
- [counter](/exercises/counter.v) - The authoritative camera
5858
- [spin_lock](/exercises/spin_lock.v) - Specification of a spin lock
5959
- [ticket_lock](/exercises/ticket_lock.v) - Specification of a ticket lock
60+
- [ticket_lock_advanced](/theories/ticket_lock_advanced.v) - Advanced Coq tactics
6061
- [adequacy](/exercises/adequacy.v) - Adequacy
6162
- [merge_sort](/exercises/merge_sort.v) - Merge sort
6263
- [custom_ra](/exercises/custom_ra.v) - Defining resource algebras from scratch
@@ -196,4 +197,19 @@ If you have specific requests or questions about the tutorial, please contact:
196197

197198
**Amin Timany**\
198199
Aarhus University\
199-
<timany@cs.au.dk>
200+
<timany@cs.au.dk>
201+
202+
### Contributors
203+
Below is a list of people who have contributed to the tutorial, sorted by last name. If you have contributed to the project, please add yourself to the list.
204+
205+
**Lars Birkedal**\
206+
Aarhus University\
207+
<birkedal@cs.au.dk>
208+
209+
**Mathias Pedersen**\
210+
Aarhus University\
211+
<mp@cs.au.dk>
212+
213+
**Amin Timany**\
214+
Aarhus University\
215+
<timany@cs.au.dk>

TODO.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
# Fixes
2+
- Clean up proofs in some chapters
3+
- Reduce use of SSreflect
4+
- Simplify proofs
5+
16
# Possible future topics
2-
- Have a file showcasing advanced Coq tactics.
7+
- Chapter on prophecies
8+
- Chapter on HOCAP-style specifications
39
- Chapters "Linked List", "Arrays", and "Merge Sort" all use list functionality from std++ (e.g. fmap and lookup). These should be introduced beforehand, for example in an appendix, with the chapters referring to the appendix.
410
- Consider introducing commands "About", "Locate", "Print", etc. in an introductory chapter.
511
- Add example with "Landin's knot" (recursion through the store) to showcase use of dicardable fractions or invariants for sequential programs

theories/ticket_lock_advanced.v

Lines changed: 20 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,17 @@ From iris.base_logic.lib Require Export invariants.
33
From iris.heap_lang Require Import lang proofmode notation.
44

55
(* ################################################################# *)
6-
(** * Case Study: Ticket Lock *)
7-
8-
(* ================================================================= *)
9-
(** ** Implementation *)
6+
(** * Case Study: Ticket Lock Advanced *)
107

118
(**
12-
Let us look at another implementation of a lock, namely a ticket lock.
13-
Instead of having every thread fight to acquire the lock, the ticket
14-
lock makes them wait in line. It functions similarly to a ticketing
15-
system that one often finds in bakeries and pharmacies. Upon entering
16-
the shop, you pick a ticket with some number and wait until the number
17-
on the screen has reached your number. Once this happens, it becomes
18-
your turn to speak to the shop assistant. In our scenario, talking to
19-
the shop assistant corresponds to accessing the protected resources.
20-
21-
To implement this, we will maintain two counters: [o] and [n]. The
22-
first counter, [o], represents the number on the screen – the customer
23-
currently being served. The second counter, [n], represents the next
24-
number to be dispensed by the ticketing machine.
25-
26-
To acquire the lock, a thread must increment the second counter, [n],
27-
and keep its previous value as a ticket for a position in the queue.
28-
Once the ticket has been obtained, the thread must wait until the
29-
first counter, [o], reaches its ticket value. Once this happens, the
30-
thread gets access to the protected resources. The thread can then
31-
release the lock by incrementing the first counter.
9+
The implementation, resource algebra, and representation predicates
10+
are identical to the original Ticket Lock chapter. Only the proofs
11+
differ.
3212
*)
3313

14+
(* ================================================================= *)
15+
(** ** Implementation *)
16+
3417
Definition mk_lock : val :=
3518
λ: <>, (ref #0, ref #0).
3619

@@ -53,98 +36,35 @@ Definition release : val :=
5336
(* ================================================================= *)
5437
(** ** Representation Predicates *)
5538

56-
(**
57-
As a ticket lock is a lock, we expect it to satisfy the same
58-
specification as the spin-lock. This time, you have to come up with
59-
the necessary resource algebra and lock invariant by yourself. It
60-
might be instructive to first look through all required predicates and
61-
specifications to figure out exactly what needs to be proven.
62-
*)
63-
64-
Definition RA : cmra
65-
(* BEGIN SOLUTION *)
66-
(**
67-
We will use a finite set of numbers to represent the tickets that
68-
have been issued – the second counter. This becomes a camera by
69-
using the disjoint union as an operation.
70-
71-
For the first counter, we will use the exclusive camera over the
72-
natural numbers – this means that there can be only one
73-
access-granting ticket owned at a time.
74-
75-
By wrapping them both in an authoritative camera, we can use the
76-
authoritative fragment to bind the values of our counters to the
77-
ghost state.
78-
*)
79-
:= authR (prodUR (optionUR (exclR natO)) (gset_disjR nat)).
80-
(* END SOLUTION BEGIN TEMPLATE
81-
(* := insert your definition here *). Admitted.
82-
END TEMPLATE *)
39+
Definition RA : cmra :=
40+
authR (prodUR (optionUR (exclR natO)) (gset_disjR nat)).
8341

8442
Section proofs.
8543
Context `{!heapGS Σ, !inG Σ RA}.
8644
Let N := nroot .@ "ticket_lock".
8745

88-
(**
89-
This time around, we know that the thread is locked by a thread with a
90-
specific ticket. As such, we first define a predicate [locked_by]
91-
which states that the lock is locked by ticket [o].
92-
*)
93-
Definition locked_by (γ : gname) (o : nat) : iProp Σ
94-
(* BEGIN SOLUTION *)
95-
(**
96-
We know that the lock is locked by ticket [o] when we have ownership
97-
of the exclusive counter being [o].
98-
*)
99-
:= own γ (◯ (Excl' o, GSet ∅)).
100-
(* END SOLUTION BEGIN TEMPLATE
101-
(* := insert your definition here *). Admitted.
102-
END TEMPLATE *)
46+
Definition locked_by (γ : gname) (o : nat) : iProp Σ :=
47+
own γ (◯ (Excl' o, GSet ∅)).
10348

104-
(** The lock is locked when it has been locked by some ticket. *)
10549
Definition locked (γ : gname) : iProp Σ :=
10650
∃ o, locked_by γ o.
10751

10852
Lemma locked_excl γ : locked γ -∗ locked γ -∗ False.
109-
(* SOLUTION *) Proof.
53+
Proof.
11054
iIntros "[%o1 H1] [%o2 H2]".
11155
iDestruct (own_valid_2 with "H1 H2") as %[]%auth_frag_valid_1; done.
11256
Qed.
11357

114-
(**
115-
We will also have a predicate signifying that ticket [x] has been
116-
_issued_. A thread will need to have been issued ticket [x] in order
117-
to wait for the first counter to become [x].
118-
*)
119-
Definition issued (γ : gname) (x : nat) : iProp Σ
120-
(* BEGIN SOLUTION *)
121-
(** A ticket is simply the singleton set over its index. *)
122-
:= own γ (◯ (ε : option (excl nat), GSet {[x]})).
123-
(* END SOLUTION BEGIN TEMPLATE
124-
(* := insert your definition here *). Admitted.
125-
END TEMPLATE *)
126-
127-
Definition lock_inv (γ : gname) (lo ln : loc) (P : iProp Σ) : iProp Σ
128-
(* BEGIN SOLUTION *)
129-
(**
130-
Our invariant will first link the authoritative fragment to the
131-
counters. For the second counter, this means that all tickets prior
132-
to the counter's current value must have been issued.
58+
Definition issued (γ : gname) (x : nat) : iProp Σ :=
59+
own γ (◯ (ε : option (excl nat), GSet {[x]})).
13360

134-
Secondly, the lock contains either ownership of the value of the
135-
first counter as well as the protected resources (the queue is
136-
unlocked), or the current access-granting ticket (the queue is
137-
locked).
138-
*)
139-
:= ∃ o n : nat, lo ↦ #o ∗ ln ↦ #n ∗
61+
Definition lock_inv (γ : gname) (lo ln : loc) (P : iProp Σ) : iProp Σ :=
62+
∃ o n : nat, lo ↦ #o ∗ ln ↦ #n ∗
14063
own γ (● (Excl' o, GSet (set_seq 0 n))) ∗
14164
(
14265
(locked_by γ o ∗ P) ∨
14366
issued γ o
14467
).
145-
(* END SOLUTION BEGIN TEMPLATE
146-
(* := insert your definition here *). Admitted.
147-
END TEMPLATE *)
14868

14969
Definition is_lock (γ : gname) (l : val) (P : iProp Σ) : iProp Σ :=
15070
∃ lo ln : loc, ⌜l = (#lo, #ln)%V⌝ ∗ inv N (lock_inv γ lo ln P).
@@ -154,7 +74,7 @@ Definition is_lock (γ : gname) (l : val) (P : iProp Σ) : iProp Σ :=
15474

15575
Lemma mk_lock_spec P :
15676
{{{ P }}} mk_lock #() {{{ γ l, RET l; is_lock γ l P }}}.
157-
(* SOLUTION *) Proof.
77+
Proof.
15878
iIntros "%Φ HP HΦ".
15979
wp_lam.
16080
wp_alloc lo; wp_alloc ln.
@@ -170,7 +90,7 @@ Lemma wait_spec γ l P x :
17090
{{{ is_lock γ l P ∗ issued γ x }}}
17191
wait #x l
17292
{{{ RET #(); locked γ ∗ P }}}.
173-
(* SOLUTION *) Proof.
93+
Proof.
17494
iIntros "%Φ [(%lo & %ln & -> & #I) Hx] HΦ".
17595
iLöb as "IH".
17696
wp_rec.
@@ -199,7 +119,7 @@ Qed.
199119

200120
Lemma acquire_spec γ l P :
201121
{{{ is_lock γ l P }}} acquire l {{{ RET #(); locked γ ∗ P }}}.
202-
(* SOLUTION *) Proof.
122+
Proof.
203123
iIntros "%Φ (%lo & %ln & -> & #I) HΦ".
204124
iLöb as "IH".
205125
wp_rec.
@@ -236,7 +156,7 @@ Qed.
236156

237157
Lemma release_spec γ l P :
238158
{{{ is_lock γ l P ∗ locked γ ∗ P }}} release l {{{ RET #(); True }}}.
239-
(* SOLUTION *) Proof.
159+
Proof.
240160
iIntros "%Φ ((%lo & %ln & -> & #I) & [%o Hexcl] & HP) HΦ".
241161
wp_lam.
242162
wp_pures.

0 commit comments

Comments
 (0)