Documentation

CatCryptCore.Core.Heap

Heap Model #

This file defines the heap model for stateful computations.

Design #

Following the Rocq SSProve design, values are stored using their Fintype encoding as natural numbers in a partial finite map (Finmap) from location ids to encoded values. Unaccessed locations map to none; get returns default for such locations.

This is the Lean 4 analogue of Rocq SSProve's {fmap nat → nat}.

Key Properties #

Heap: partial finite map from location ids to encoded values. Values are encoded using their Fintype encoding as natural numbers. Unaccessed locations are absent from the map; get returns default for absent locations.

  • data : Finmap fun (x : ) =>

    Partial map from location id to encoded value (natural number)

Instances For
    theorem CatCrypt.Core.Heap.ext {h₁ h₂ : Heap} (hdata : h₁.data = h₂.data) :
    h₁ = h₂

    Extensionality for heaps

    theorem CatCrypt.Core.Heap.ext_iff {h₁ h₂ : Heap} :
    h₁ = h₂ h₁.data = h₂.data
    theorem CatCrypt.Core.Heap.ext_lookup {h₁ h₂ : Heap} (hlookup : ∀ (id : ), Finmap.lookup id h₁.data = Finmap.lookup id h₂.data) :
    h₁ = h₂

    Extensionality for heaps via lookup

    Empty heap: no locations are initialized

    Equations
    Instances For
      noncomputable def CatCrypt.Core.Heap.encode {α : Type} [Fintype α] (v : α) :

      Encode a value using its Fintype equivalence

      Equations
      Instances For
        noncomputable def CatCrypt.Core.Heap.decode (α : Type) [Fintype α] [Inhabited α] (n : ) :
        α

        Decode a natural number to a value. If the number is out of range, returns default.

        Equations
        Instances For
          theorem CatCrypt.Core.Heap.decode_encode {α : Type} [Fintype α] [Inhabited α] (v : α) :
          decode α (encode v) = v

          Encoding then decoding returns the original value

          noncomputable def CatCrypt.Core.Heap.get (h : Heap) (l : Location) :
          l.ty

          Get a value from the heap at a location. Returns default if the location has never been written.

          Equations
          Instances For
            noncomputable def CatCrypt.Core.Heap.set (h : Heap) (l : Location) (v : l.ty) :

            Set a value in the heap at a location

            Equations
            Instances For
              def CatCrypt.Core.Heap.agreeOn (h1 h2 : Heap) (locs : LocSet) :

              Two heaps agree on a set of location ids

              Equations
              Instances For
                @[implicit_reducible]

                Heap equality is not decidable in general, but we can use classical logic

                Equations
                @[simp]
                theorem CatCrypt.Core.Heap.get_set_same (h : Heap) (l : Location) (v : l.ty) :
                (h.set l v).get l = v

                Getting after setting the same location returns the stored value

                @[simp]
                theorem CatCrypt.Core.Heap.get_set_other (h : Heap) (l l' : Location) (v : l.ty) (hne : l.id l'.id) :
                (h.set l v).get l' = h.get l'

                Setting at one location doesn't affect getting from another location

                @[simp]
                theorem CatCrypt.Core.Heap.set_set_same (h : Heap) (l : Location) (v₁ v₂ : l.ty) :
                (h.set l v₁).set l v₂ = h.set l v₂

                Setting the same location twice: the second write wins

                theorem CatCrypt.Core.Heap.set_set_comm (h : Heap) (l₁ l₂ : Location) (v₁ : l₁.ty) (v₂ : l₂.ty) (hne : l₁.id l₂.id) :
                (h.set l₁ v₁).set l₂ v₂ = (h.set l₂ v₂).set l₁ v₁

                Setting different locations commutes

                @[simp]

                Looking up a location in the empty heap returns none