opensheaf-lean

A learning record, checked by computation

A second story, from the use case "A learning record": a student writes a thesis in an archive of their own, and the department keeps the record of the work. Like the first story, it is told in valid steps, from nothing, signatures and key logs included, and each check is proved by decide. It exercises what the first story does not: a mirror kept as a receipt of each submission, a selection whose revision set grows by the department's acts, comments that keep pointing to the version they were written about, and the deletion of the record at the end of its retention period.

import OpenSheaf.Story

namespace OpenSheaf.Record

open OpenSheaf OpenSheaf.Patterns OpenSheaf.Story

The cast

Two owners, the student and the department, each with a key log (section 6): the student's current key is s1, the department's d1, each with a rotation key of four characters committed in its genesis record. The department's archive hosts the department, the student's the student; the department keeps a copy of the student's key log, beside the receipt (section 6.5). The digest is the toy digest of the first story.

def keysS : Keys Nat := { keys := ["s1"], threshold := 1, next := [4], nextThreshold := 1 }
def keysD : Keys Nat := { keys := ["d1"], threshold := 1, next := [4], nextThreshold := 1 }

def genS : Genesis Nat := { kind := .owner, nonce := "student", keys := some keysS }
def genD : Genesis Nat := { kind := .owner, nonce := "dept", keys := some keysD }

def logS : Obj Nat := keyLog "student" genS
def logD : Obj Nat := keyLog "dept" genD
continues the fragment before; continued further on.

The first submission

The student seals the thesis, version 1, and hands it in. The department receives it by mirroring it: the mirror is the receipt.

def genT : Genesis Nat := { kind := .document, nonce := "thesis", originator := "student" }
def thesisLine : LineId := ⟨"thesis", "student"⟩

def t1 : Version Nat :=
  { documentVersion := 1, files := [("thesis.tm", 11), ("thesis.pdf", 12)],
    description := "A thesis", depositor := "student", message := "submitted for comments" }

def thesis0 : Obj Nat :=
  { id := .line thesisLine, kind := .document, genesis := some genT, versions := [t1] }

def sigT1 : Sig Nat := ⟨thesisLine, 1, toy (thesis0.inventory 1), "s1", logPin "student" logS 1⟩

def thesis1 : Obj Nat := { thesis0 with sigs := [sigT1] }
continues the fragment before; continued further on.

The supervisor's comments are a document of the department. They point to the version commented on by a Reviews reference (chapter 15), which pins version 1 of the thesis, with the student's archive as its hint. In the specification, Reviews is a DataCite relation that tools do not follow (section 4.4); here it is the pinned reference proposed in issues #21 and #28.

def pinT1 : Pin Nat := ⟨thesisLine, 1, toy (thesis1.inventory 1), ["student"]⟩

def genC1 : Genesis Nat := { kind := .document, nonce := "comments", originator := "dept" }
def commentsLine : LineId := ⟨"comments", "dept"⟩

def c1 : Version Nat :=
  { documentVersion := 1, files := [("comments.pdf", 41)], refs := [⟨"Reviews", pinT1⟩],
    description := "Comments on version 1", depositor := "supervisor", message := "comments" }

def comments0 : Obj Nat :=
  { id := .line commentsLine, kind := .document, genesis := some genC1, versions := [c1] }

def sigC1 : Sig Nat := ⟨commentsLine, 1, toy (comments0.inventory 1), "d1", logPin "dept" logD 1⟩

def comments : Obj Nat := { comments0 with sigs := [sigC1] }
continues the fragment before; continued further on.

The record

The record is a selection of the department, which pins each document of the process in the version that played its part: here, version 1 of the thesis and the comments on it.

def pinC1 : Pin Nat := ⟨commentsLine, 1, toy (comments.inventory 1), ["dept"]⟩

def rec1 : Members Nat :=
  { type := .selection,
    members := [{ line := thesisLine, hints := ["student"], pins := [pinT1] },
                { line := commentsLine, hints := ["dept"], pins := [pinC1] }] }

def genR : Genesis Nat := { kind := .collection, nonce := "record", originator := "dept" }
def recordLine : LineId := ⟨"record", "dept"⟩

def g1 : Version Nat :=
  { documentVersion := 1, files := [], members := some rec1, description := "Record",
    depositor := "registry", message := "first submission, with comments" }

def record0 : Obj Nat :=
  { id := .line recordLine, kind := .collection, genesis := some genR, versions := [g1] }

def sigG1 : Sig Nat := ⟨recordLine, 1, toy (record0.inventory 1), "d1", logPin "dept" logD 1⟩

def record1 : Obj Nat := { record0 with sigs := [sigG1] }
continues the fragment before; continued further on.

The second submission and the examination

The student revises the thesis in answer to the comments, a new document version, and hands it in; the department copies it into the receipt. The examiner reports on version 2, and the department records the examination: a new version of the record, whose revision set for the thesis grows to versions 1 and 2, and which adds the report.

def t2 : Version Nat :=
  Version.revise [t1] [("thesis.tm", 21), ("thesis.pdf", 22)] none "A thesis" "student"
    "answers the comments"

def sigT2 : Sig Nat :=
  ⟨thesisLine, 2, toy ((thesis1.append t2).inventory 2), "s1", logPin "student" logS 1⟩

def thesis2 : Obj Nat := thesis1.append t2 [sigT2]

def genE : Genesis Nat := { kind := .document, nonce := "report", originator := "dept" }
def reportLine : LineId := ⟨"report", "dept"⟩

def e1 : Version Nat :=
  { documentVersion := 1, files := [("report.pdf", 51)],
    description := "Report on version 2", depositor := "examiner", message := "report" }

def report0 : Obj Nat :=
  { id := .line reportLine, kind := .document, genesis := some genE, versions := [e1] }

def sigE1 : Sig Nat := ⟨reportLine, 1, toy (report0.inventory 1), "d1", logPin "dept" logD 1⟩

def report : Obj Nat := { report0 with sigs := [sigE1] }

def pinT2 : Pin Nat := ⟨thesisLine, 2, toy (thesis2.inventory 2), ["student"]⟩
def pinE1 : Pin Nat := ⟨reportLine, 1, toy (report.inventory 1), ["dept"]⟩

def rec2 : Members Nat :=
  { type := .selection,
    members := [{ line := thesisLine, hints := ["student"], pins := [pinT1, pinT2] },
                { line := commentsLine, hints := ["dept"], pins := [pinC1] },
                { line := reportLine, hints := ["dept"], pins := [pinE1] }] }

def g2 : Version Nat :=
  Version.revise [g1] [] (some rec2) "Record" "registry" "examined: report on version 2"

def sigG2 : Sig Nat :=
  ⟨recordLine, 2, toy ((record1.append g2).inventory 2), "d1", logPin "dept" logD 1⟩

def record2 : Obj Nat := record1.append g2 [sigG2]
continues the fragment before; continued further on.

The environment

The signatures made are the story's, and the environment is the first story's, with them.

def madeR : List (Sig Nat) := [sigT1, sigC1, sigG1, sigT2, sigE1, sigG2]

def ER : Env Nat :=
  { hash := toy, keyDigest := keyDigest, nameOf := nameOf, verifies := fun s => decide (s ∈ madeR) }
continues the fragment before; continued further on.

The steps

def r0 : World Nat := fun _ => none
def r1 : World Nat := fun b => if b = "student" then some (arch ["student"]) else r0 b
def r2 : World Nat := fun b => if b = "dept" then some (arch ["dept"]) else r1 b
def r3 : World Nat := setEntry r2 "student" (logLine "student") (.live logS)
def r4 : World Nat := setEntry r3 "dept" (logLine "dept") (.live logD)
def r5 : World Nat := setEntry r4 "dept" (logLine "student") (.live logS)
def r6 : World Nat := setEntry r5 "student" (.line thesisLine) (.live thesis1)
def r7 : World Nat := setEntry r6 "dept" (.line thesisLine) (.live thesis1)
def r8 : World Nat := setEntry r7 "dept" (.line commentsLine) (.live comments)
def r9 : World Nat := setEntry r8 "dept" (.line recordLine) (.live record1)
def r10 : World Nat := setEntry r9 "student" (.line thesisLine) (.live thesis2)
def r11 : World Nat := setEntry r10 "dept" (.line thesisLine) (.live thesis2)
def r12 : World Nat := setEntry r11 "dept" (.line reportLine) (.live report)
def r13 : World Nat := setEntry r12 "dept" (.line recordLine) (.live record2)

theorem a1 : ValidStep ER r0 r1 := .newArchive _ _ _ rfl (fun _ => rfl)
theorem a2 : ValidStep ER r1 r2 := .newArchive _ _ _ rfl (fun _ => rfl)
continues the fragment before; continued further on.

The key logs, the student's twice, in its archive and beside the receipt:

theorem a3 : ValidStep ER r2 r3 := create_keyLog rfl rfl (by decide)
theorem a4 : ValidStep ER r3 r4 := create_keyLog rfl rfl (by decide)
theorem a5 : ValidStep ER r4 r5 := create_keyLog rfl rfl (by decide)
continues the fragment before; continued further on.

The thesis is sealed and signed by the student; the department's receipt is a copy, valid only as one, since the department's archive does not host the student.

theorem a6 : ValidStep ER r5 r6 := create_hosted rfl rfl rfl (no_pins (by decide)) (by decide)

theorem copy_r6 : CopyOf r6 "dept" thesis1 := ⟨"student", thesis1, by decide, rfl, rfl, [], by simp⟩

theorem a7 : ValidStep ER r6 r7 := create_copy rfl rfl rfl copy_r6 (no_pins (by decide)) (by decide)
continues the fragment before; continued further on.

The comments are created only because the version they review exists: the step resolves the reference's pin.

theorem a8 : ValidStep ER r7 r8 := create_hosted rfl rfl rfl (pins_resolve (by decide)) (by decide)
theorem a9 : ValidStep ER r8 r9 := create_hosted rfl rfl rfl (pins_resolve (by decide)) (by decide)
continues the fragment before; continued further on.

The second submission, its receipt, the report, and the record's second version, a new document version of the record since its member list changed.

theorem a10 : ValidStep ER r9 r10 :=
  append_hosted rfl rfl rfl (pins_resolve (by decide)) (by decide)

theorem copy_r10 : CopyOf r10 "dept" thesis2 := ⟨"student", thesis2, by decide, rfl, rfl, [], by simp⟩

theorem a11 : ValidStep ER r10 r11 :=
  append_copy rfl rfl rfl copy_r10 (pins_resolve (by decide)) (by decide)

theorem a12 : ValidStep ER r11 r12 := create_hosted rfl rfl rfl (no_pins (by decide)) (by decide)

theorem a13 : ValidStep ER r12 r13 :=
  append_hosted rfl rfl rfl (pins_resolve (by decide)) (by decide)
continues the fragment before; continued further on.

The record is valid

theorem valid : ValidSteps ER r0 r13 :=
  .tail (.tail (.tail (.tail (.tail (.tail (.tail (.tail (.tail (.tail (.tail (.tail (.tail
    (.refl _) a1) a2) a3) a4) a5) a6) a7) a8) a9) a10) a11) a12) a13

theorem events : Events ER r0 r13 := valid.mono .step

example : WFWorld ER r13 := valid.wf ER (WFWorld.empty ER)
continues the fragment before; continued further on.

What the record shows

The verifier checks the record: every pin resolves, to the version that played its part.

example : verify ER r13 rec2 =
    [(pinT1, .ok t1), (pinT2, .ok t2), (pinC1, .ok c1), (pinE1, .ok e1)] := by decide
continues the fragment before; continued further on.

The comments still point to version 1, the version they were written about, although the thesis has moved on: the reference of the comments resolves to version 1, while the head of the thesis is version 2.

example : c1.refs.map (fun r => resolve toy r13 r.pin) = [.ok t1] ∧
    headPin toy r13 ["student"] thesisLine = some pinT2 := by
  decide
continues the fragment before; continued further on.

The record's first version is kept, as the history of the process: its pin resolves to the version deposited at the first submission. The record is a selection: updating it changes nothing.

def pinG1 : Pin Nat := ⟨recordLine, 1, toy (record2.inventory 1), ["dept"]⟩

example : resolve toy r13 pinG1 = .ok g1 := by decide

example : collect toy r13 rec1 = rec1 := by decide
continues the fragment before; continued further on.

Who decides

By the theorems of chapter 8, without looking at the objects: every copy of the record ends in a version the department signed, and the receipt ends in a version the student signed, although the department made the steps that copied it. The student cannot change the record without the department's keys, nor the department the thesis without the student's.

example : HeadSigned ER r13 recordLine record2 :=
  Events.headsSigned ER events (b := "dept") rfl (by decide)

example : HeadSigned ER r13 thesisLine thesis2 :=
  Events.headsSigned ER events (b := "dept") rfl (by decide)
continues the fragment before; continued further on.

The receipt

The versions the department received are the student's, at the same numbers, as decide checks; and each stays there, whatever the student's archive does later, by immutability at the department's archive.

example : resolveAt toy r13 "dept" pinT1 = .ok t1 ∧ resolveAt toy r13 "dept" pinT2 = .ok t2 := by
  decide
continues the fragment before; continued further on.

The student rewrites version 1 in place, outside the protocol, to make the first submission look better: a new source file under the same name. The pin of the first submission no longer resolves on the student's archive; on the receipt it still does, to what was handed in. (The toy digest reads the digests of files, so the rewritten inventory has another digest; a rewrite that it does not read, of the message alone, would collide with the original, the case that NoCollision excludes.)

def t1' : Version Nat := { t1 with files := [("thesis.tm", 99), ("thesis.pdf", 12)] }

def rTamper : World Nat :=
  setEntry r13 "student" (.line thesisLine) (.live { thesis2 with versions := [t1', t2] })

example : resolve toy rTamper pinT1 = .mismatch := by decide

example : resolveAt toy rTamper "dept" pinT1 = .ok t1 := by decide
continues the fragment before; continued further on.

Retention

At the end of the retention period, the department takes the record down (section 13.2), an event of the model.

def rDown : World Nat := takedown toy r13 "dept" (.line recordLine) record2

theorem down : Events ER r0 rDown := .tail events (.takedown _ _ _ _ (by decide) rfl)
continues the fragment before; continued further on.

The record's pins report removed at the department's archive; the student's thesis, and the pins of it, are not affected.

def pinG2 : Pin Nat := ⟨recordLine, 2, toy (record2.inventory 2), ["dept"]⟩

example : resolve toy rDown pinG1 = .removed ∧ resolve toy rDown pinG2 = .removed := by decide

example : resolve toy rDown pinT2 = .ok t2 := by decide
continues the fragment before; continued further on.

By record_retention (chapter 11), this holds from then on: the record's pins report removed there in every later world, and the thesis's pins resolve as before through any later valid steps.

example (w' : World Nat) (h : Events ER rDown w') : resolveAt toy w' "dept" pinG2 = .removed :=
  (record_retention ER (w := r13) (a := "dept") (oid := .line recordLine) (r := record2) rfl).1
    pinG2 rfl w' h

example (w' : World Nat) (h : ValidSteps ER rDown w') : resolveAt toy w' "student" pinT2 = .ok t2 :=
  (record_retention ER (w := r13) (a := "dept") (oid := .line recordLine) (r := record2) rfl).2
    "student" pinT2 (by decide) t2 (by decide) w' h
continues the fragment before; continued further on.

The comments and the report are documents of their own, and stay; deleting them too is a takedown of each.

end OpenSheaf.Record