Lean UTF-8 representation evidence
==================================

Scope
-----

This record pins the configured Lean definitions used to characterize the
UTF-8 conjunct of DecimalGoalVocabularyDefault.sourceString.  The Rust str
invariant and configured isize::MAX bound are pinned separately in
reference/model-evidence/source-valid-byte-slice.txt.  The Aeneas ordered byte
carrier is pinned separately in reference/model-evidence/aeneas-slice-observations.txt.

Configured toolchain and source identities
------------------------------------------

All commands below were run from the repository root.  The exact reproduction
commands are:

  cat lean-toolchain
  sha256sum lean-toolchain
  lake env lean --version
  lake env lean --print-prefix
  P=$(lake env lean --print-prefix)
  sha256sum "$P/bin/lean" \
    "$P/src/lean/Init/Prelude.lean" \
    "$P/src/lean/Init/Data/String/Defs.lean"
  nl -ba "$P/src/lean/Init/Prelude.lean" | sed -n '3480,3526p'
  nl -ba "$P/src/lean/Init/Data/String/Defs.lean" | sed -n '64,79p'

The configured toolchain file contains exactly:

  leanprover/lean4:v4.30.0-rc2

The version and prefix results are:

  Lean (version 4.30.0-rc2, x86_64-unknown-linux-gnu, commit 3dc1a088b6d2d8eafe25a7cd7ec7b58d731bd7cc, Release)
  /home/leanagent/.elan/toolchains/leanprover--lean4---v4.30.0-rc2

The reproduced SHA-256 identities are:

  ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61  lean-toolchain
  3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf  bin/lean
  8f7cb4fa6dd34299944ad2b083b8a4b205e69c50893df3b199d130a2fbf0280b  src/lean/Init/Prelude.lean
  ff44638846752ae8d2b16707e12d4978a792a672a3f486cbff7433bd3d79ad69  src/lean/Init/Data/String/Defs.lean

Exact defining excerpts
-----------------------

Init/Prelude.lean defines the byte encoding and validity witness as:

  noncomputable def List.utf8Encode (l : List Char) : ByteArray :=
    l.flatMap String.utf8EncodeChar |>.toByteArray

  inductive ByteArray.IsValidUTF8 (b : ByteArray) : Prop
    | intro (m : List Char) (hm : Eq b (List.utf8Encode m))

It then defines String as precisely a byte array carrying such a witness:

  structure String where ofByteArray ::
    toByteArray : ByteArray
    isValidUTF8 : ByteArray.IsValidUTF8 toByteArray

Init/Data/String/Defs.lean defines conversion from a valid byte array and the
toUTF8 observation by:

  def String.fromUTF8 (a : @& ByteArray) (h : a.IsValidUTF8) : String :=
    .ofByteArray a h

  def String.toUTF8 (a : @& String) : ByteArray :=
    a.toByteArray

  theorem String.toUTF8_eq_toByteArray {s : String} :
      s.toUTF8 = s.toByteArray := (rfl)

Two-direction consequence used by the model
-------------------------------------------

Let b be any finite byte array.  If b is the toUTF8 byte array of a Lean
String t, then toUTF8 is definitionally t.toByteArray, and the String field
t.isValidUTF8 supplies ByteArray.IsValidUTF8 b.  Unfolding that inductive proof
exhibits characters m with b = List.utf8Encode m.

Conversely, if b is valid UTF-8, unfolding ByteArray.IsValidUTF8 supplies
characters m and an equality b = List.utf8Encode m.  Equivalently, use the
validity proof directly in String.fromUTF8 b h; the resulting String has
toByteArray, hence toUTF8, definitionally equal to b.  Therefore existence of
a Lean String with a given toUTF8 byte array is equivalent in both directions
to ByteArray.IsValidUTF8 of that array; this is a consequence of the displayed
definitions, not an assumed UTF-8 equivalence.
