opensheaf-lean · sheaf

Separate worlds

Archives are built separately, by different owners. This chapter takes two worlds, each built from nothing by events, on archives that do not overlap, and puts them side by side. It proves:

In version 0.1 an object named its archive, and two worlds on separate archives shared nothing: no named document version was held in both, no pin held in one named an archive of the other, and they glued with no condition. Version 0.2 names lines, not archives (section 4): the same line may be copied into both worlds, or deposited in both by its owner, and a pin carries hints that may point anywhere. So separate worlds may share names, and those two theorems of version 0.1 are gone. What makes them glue now is what makes one world glue: the owners' signatures, which do not depend on the world (chapter Names are eternal). Two copies of a line, one in each world, are two histories the owner signed, and agree when the owner signs one history.

What the model still cannot express is a step that crosses worlds: a pin written in one world, found only through a copy kept in another, checked when the two are combined. That needs a step relation across worlds, and it is listed among what is left out (see the introduction).

import Sheaf.Local

namespace OpenSheaf.Sheaf

variable {D : Type}

Two worlds side by side

The union of two worlds has the archives of both. When no archive is in both, an entry of the union is an entry of exactly one of them.

/-- Two worlds side by side: the archives of the first, then those of the
second. -/
def World.union (w1 w2 : World D) : World D :=
  fun a => match w1 a with
    | some ar => some ar
    | none => w2 a

/-- No archive is in both worlds. -/
def SeparateWorlds (w1 w2 : World D) : Prop := ∀ a, w1 a = none ∨ w2 a = none

theorem lookup_union_left {w1 w2 : World D} {a : ArchiveId} {i : ObjectId} {e : Entry D}
    (h : lookup w1 a i = some e) : lookup (World.union w1 w2) a i = some e := by
  unfold lookup World.union at *
  cases h1 : w1 a with
  | none => rw [h1] at h; cases h
  | some ar => rw [h1] at h; exact h

theorem lookup_union_right {w1 w2 : World D} (hd : SeparateWorlds w1 w2) {a : ArchiveId}
    {i : ObjectId} {e : Entry D} (h : lookup w2 a i = some e) :
    lookup (World.union w1 w2) a i = some e := by
  have h2 : w2 a ≠ none := by
    intro h2; unfold lookup at h; rw [h2] at h; cases h
  have h1 : w1 a = none := (hd a).resolve_right h2
  unfold lookup World.union at *
  rw [h1]; exact h

theorem lookup_union_cases {w1 w2 : World D} {a : ArchiveId} {i : ObjectId} {e : Entry D}
    (h : lookup (World.union w1 w2) a i = some e) :
    lookup w1 a i = some e ∨ lookup w2 a i = some e := by
  unfold lookup World.union at *
  cases h1 : w1 a with
  | none => rw [h1] at h; exact Or.inr h
  | some ar => rw [h1] at h; exact Or.inl h
continues the fragment before; continued further on.

Separate worlds glue

The objects of the union are those of the two worlds, so they are well formed. Two copies of a line in the union come from one world or from both; either way, each ends in a version its owner signed (the model's Events.headsSigned), so they are prefixes of one another when the owner signs one history (honest_comparable, in the chapter The sections of a world glue). The argument of the main theorem then applies to the union, although the union is not built by events. The proof does not use that the archives are separate: where both worlds have an archive, the union keeps the first world's, and the theorem holds of the union of any two worlds built by events, whence its name. That each world keeps its own section, below, needs them separate.

variable [DecidableEq D] (E : Env D)

/-- **Two worlds glue side by side**, separate or not, when owners are honest. -/
theorem union_glue {w1 w2 : World D}
    (hs1 : Events E (fun _ => (none : Option (Archive D))) w1)
    (hs2 : Events E (fun _ => (none : Option (Archive D))) w2) {H : Histories D}
    (hh1 : Honest E H w1) (hh2 : Honest E H w2) :
    ∃ g, Glues (sections (World.union w1 w2)) g ∧
      ∀ g', Glues (sections (World.union w1 w2)) g' → g' = g := by
  apply sheaf
  apply compatible_of_agree
  · intro b i c hl
    rcases lookup_union_cases hl with hl | hl
    · exact SoundWorld.wfObjects E (Events.sound E hs1) _ _ _ hl
    · exact SoundWorld.wfObjects E (Events.sound E hs2) _ _ _ hl
  · intro l b b' m m' hl hl' hk hk'
    rcases lookup_union_cases hl with hl | hl <;> rcases lookup_union_cases hl' with hl' | hl'
    · exact Events.copiesAgree E hs1 (hh1.one E l) _ _ _ _ hl hl' hk hk'
    · exact honest_comparable E hs1 hs2 hh1 hh2 hl hl' hk hk'
    · exact (honest_comparable E hs1 hs2 hh1 hh2 hl' hl hk' hk).symm
    · exact Events.copiesAgree E hs2 (hh2.one E l) _ _ _ _ hl hl' hk hk'
continues the fragment before; continued further on.

Each world keeps its section

A source of either world is a source of the union, so the glued section of the union restricts to a section that glues each world: its own glued section, by uniqueness. Putting the worlds side by side changes nothing that either says.

/-- A source of the first world is a source of the union. -/
def Source.left {w1 w2 : World D} (i : Source w1) : Source (World.union w1 w2) :=
  ⟨i.archive, i.line, i.obj, lookup_union_left i.live, i.kind, i.version⟩

/-- A source of the second world is a source of the union, when the worlds are
separate. -/
def Source.right {w1 w2 : World D} (hd : SeparateWorlds w1 w2) (i : Source w2) :
    Source (World.union w1 w2) :=
  ⟨i.archive, i.line, i.obj, lookup_union_right hd i.live, i.kind, i.version⟩

omit [DecidableEq D] in
theorem union_restricts_left {w1 w2 : World D}
    {g : Sec (HeldIn (World.union w1 w2)) (Content D)}
    (hg : Glues (sections (World.union w1 w2)) g) :
    Glues (sections w1) (g.restrict fun _ ⟨i, hx⟩ => ⟨Source.left i, hx⟩) := by
  intro i
  funext x hx
  exact congrFun (congrFun (hg (Source.left i)) x) hx

omit [DecidableEq D] in
theorem union_restricts_right {w1 w2 : World D} (hd : SeparateWorlds w1 w2)
    {g : Sec (HeldIn (World.union w1 w2)) (Content D)}
    (hg : Glues (sections (World.union w1 w2)) g) :
    Glues (sections w2) (g.restrict fun _ ⟨i, hx⟩ => ⟨Source.right hd i, hx⟩) := by
  intro i
  funext x hx
  exact congrFun (congrFun (hg (Source.right hd i)) x) hx

end OpenSheaf.Sheaf