Documentation

Tutorial

Lean from zero, for reading Schedule.lean #

The book's chapter "Proofs" (Foundations) walks through this file. It uses only Lean's core library, like Schedule.lean, and every example is a smaller version of something there.

lean verify/Tutorial.lean      # prints nothing when every proof checks

1. Definitions, and proofs by computation #

Equations
Instances For
    theorem Tutorial.double_21 :
    double 21 = 42

    $$\mathrm{double}(21) = 42$$

    rfl: both sides compute to the same value.

    theorem Tutorial.double_eq (n : Nat) :
    double n = 2 * n

    $$\forall n,\ \mathrm{double}(n) = 2n$$

    For every n, which no amount of computing can check: unfold, then arithmetic.

    2. Programs as lists of steps (runAll in Schedule.lean) #

    def Tutorial.applyAll :
    List (Nat → Nat) → Nat → Nat

    Run a list of state transformers in order.

    Equations
    Instances For
      theorem Tutorial.applyAll_append (xs ys : List (Nat → Nat)) (s : Nat) :
      applyAll (xs ++ ys) s = applyAll ys (applyAll xs s)

      $$\mathrm{applyAll}(xs \mathbin{+\!\!+} ys)(s) = \mathrm{applyAll}(ys)\bigl(\mathrm{applyAll}(xs)(s)\bigr)$$

      Running xs ++ ys is running xs, then ys. Proof by induction on xs.

      theorem Tutorial.swap2 (f g : Nat → Nat) (h : ∀ (s : Nat), f (g s) = g (f s)) (s : Nat) :

      $$(\forall s,\ f(g(s)) = g(f(s))) \implies \mathrm{applyAll}[f, g](s) = \mathrm{applyAll}[g, f](s)$$

      Two steps that commute can be swapped (commute in Schedule.lean, for two steps).

      3. State as a function from locations to values (State in Schedule.lean) #

      @[reducible, inline]
      Equations
      Instances For
        def Tutorial.set (x : String) (v : Nat) (s : State) :

        Write v to location x.

        Equations
        Instances For
          theorem Tutorial.set_comm (x y : String) (a b : Nat) (hxy : x ≠ y) (s : State) :
          set x a (set y b s) = set y b (set x a s)

          $$x \neq y \implies s[y \mapsto b][x \mapsto a] = s[x \mapsto a][y \mapsto b]$$

          Writes to different locations commute: the simplest case of commute.

          4. The rest of what Schedule.lean uses #

          A set of locations as a predicate: Small l is the proposition "l is in the set". Schedule.lean's footprints R and W are sets like this, Loc → Prop.

          Equations
          Instances For
            structure Tutorial.Bump :

            A structure bundles data with proofs about it, as Op bundles run with frame.

            Instances For
              Equations
              Instances For
                theorem Tutorial.both_small {a b : Nat} (h : Small a ∧ Small b) :
                a + b < 20

                $$\mathrm{Small}(a) \wedge \mathrm{Small}(b) \implies a + b < 20$$

                Implicit arguments {..} are filled in by Lean; h.1 and h.2 take apart an ∧.

                inductive Tutorial.Even :
                Nat → Prop

                An inductive predicate is defined by rules, like Interleave and Runs: a number is Even by zero, or by step from an even number two below.

                Instances For

                  $$\mathrm{Even}(4)$$

                  theorem Tutorial.even_double {n : Nat} (h : Even n) :
                  ∃ (k : Nat), n = 2 * k

                  $$\mathrm{Even}(n) \implies \exists k,\ n = 2k$$

                  Proofs about an inductive predicate go by induction over its rules.

                  5. Solutions to the chapter's exercises #

                  $$\mathrm{applyAll}[\,](s) = s$$

                  Exercise 1.

                  theorem Tutorial.set_set (x : String) (a b : Nat) (s : State) :
                  set x a (set x b s) = set x a s

                  $$s[x \mapsto b][x \mapsto a] = s[x \mapsto a]$$

                  Exercise 2: the later write wins (why psync keeps two writes to one place in order).

                  theorem Tutorial.identities_harmless (fs : List (Nat → Nat)) (h : ∀ (f : Nat → Nat), f ∈ fs → ∀ (s : Nat), f s = s) (s : Nat) :
                  applyAll fs s = s

                  $$(\forall f \in fs,\ \forall s,\ f(s) = s) \implies \mathrm{applyAll}(fs)(s) = s$$

                  Exercise 3: steps that change nothing can be run any number of times (speculation_harmless in Schedule.lean).