Countable-Typed Heap Locations #
CatCrypt.Core.Location requires Fintype ty, so a heap cell can only hold a value
of a finite type. GLocation requires only Countable ty and Inhabited ty, which
admits cells holding Nat, List α, association lists, and other unbounded values.
Both flavours read and write the same carrier, CatCrypt.Core.Heap — a partial
finite map from location ids to natural numbers. Only the value codec differs:
Heap.get/Heap.set code through Fintype.equivFin, while Heap.gget/Heap.gset
code through gencode, a chosen injection ty ↪ Nat. Since gencode uses
Fintype.equivFin whenever the value type happens to be finite, the two codecs
agree on finite types, and gget/gset restrict to get/set along
GLocation.ofLocation (gget_ofLocation, gset_ofLocation).
SPComp α is Heap → SDistr (α × Heap), so it needs no adjustment: rHoare,
DependsOn and PreservesOutside apply directly to computations built from
SPComp.gget/SPComp.gset. This file supplies the footprint lemmas for those
operations.
Main definitions #
gencode,gdecode— codec for a countable inhabited typeGLocation— heap location with a countable value typeHeap.gget,Heap.gset— heap access at aGLocationSPComp.gget,SPComp.gset— the corresponding stateful operationsCatCrypt.Relational.gLeftHas,gRightHas— one-sided pRHL assertions
Codec for a countable type #
Injection of a countable type into Nat. On a finite type it is
Fintype.equivFin followed by Fin.val; otherwise it is a chosen injection.
Equations
- CatCrypt.Core.gencode α = if h : Nonempty (Fintype α) then fun (a : α) => ↑((Fintype.equivFin α) a) else ⋯.choose
Instances For
On a finite type, gencode is Heap.encode.
Partial inverse of gencode, returning default off its image.
Equations
- CatCrypt.Core.gdecode α n = if h : ∃ (a : α), CatCrypt.Core.gencode α a = n then h.choose else default
Instances For
On a finite type, gdecode is Heap.decode.
Locations with a countable value type #
Heap location whose value type is countable and inhabited. The value type is
fixed at Type (universe 0), matching Location.
Instances For
Read a countable-typed location. Returns default if never written.
Equations
- h.gget l = match Finmap.lookup l.id h.data with | some n => CatCrypt.Core.gdecode l.ty n | none => default
Instances For
Write a countable-typed location.
Equations
- h.gset l v = { data := Finmap.insert l.id (CatCrypt.Core.gencode l.ty v) h.data }
Instances For
Compatibility of the two flavours #
The countable codec extends the finite one, so a Location read or written through
the generalized interface behaves exactly as it does through Heap.get/Heap.set.
Stateful operations #
Read a countable-typed location.
Equations
Instances For
Write a countable-typed location.
Equations
- CatCrypt.Core.SPComp.gset l v h = CatCrypt.Prob.SDistr.pure ((), h.gset l v)
Instances For
The generalized operations restrict to the finite-typed ones.
Framing #
The left heap stores v at a countable-typed location.
Equations
- CatCrypt.Relational.gLeftHas l v h₁ x✝ = (h₁.gget l = v)
Instances For
The right heap stores v at a countable-typed location.
Equations
- CatCrypt.Relational.gRightHas l v x✝ h₂ = (h₂.gget l = v)
Instances For
An unbounded counter cell.
Equations
- CatCrypt.Core.natCounter id = { id := id, ty := ℕ, countable := instCountableNat, inhabited := instInhabitedNat }
Instances For
Nat carries no Fintype instance, so natCounter's value type is outside
the range of Location.
Incrementing a counter held in the heap.
Equations
- One or more equations did not get rendered due to their size.
Instances For
incr run twice from the empty heap yields 2 and stores 2.