Draft — mutable and not usable as a dependency; its citation marks the draft state.

Lax489179.IntegerEncoding

Encoding signed integers and missing distances

concepts/Lax489179/IntegerEncoding.lean · lax-489179

definition

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Concept map

    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A

    Definition

    Signed integers are stored in natural-number words using 000\mapsto0, 11-1\mapsto1, 121\mapsto2, 23-2\mapsto3, and so on. For an optional integer, zero denotes absence and an integer is encoded by its signed code plus one. This separates a missing edge or infinite distance from every finite weight, including zero and negative weights.

    Lean source view on GitHub

    1import Mathlib.Data.Int.Basic
    2
    3/-!
    4---
    5title: Encoding signed integers and missing distances
    6type: definition
    7---
    8Signed integers are stored in natural-number words using
    9000\mapsto0, 11-1\mapsto1, 121\mapsto2, 23-2\mapsto3, and so on.
    10For an optional integer, zero denotes absence and an integer is encoded
    11by its signed code plus one. This separates a missing edge or infinite
    12distance from every finite weight, including zero and negative weights.
    13-/
    14
    15namespace Lax489179.IntegerEncoding
    16
    17def encodeInt : ℤ → ℕ
    18 | .ofNat n => 2 * n
    19 | .negSucc n => 2 * n + 1
    20
    21def decodeInt (n : ℕ) : ℤ :=
    22 if n % 2 = 0 then .ofNat (n / 2) else .negSucc (n / 2)
    23
    24def encodeOption : Option ℤ → ℕ
    25 | none => 0
    26 | some z => encodeInt z + 1
    27
    28def decodeOption : ℕ → Option ℤ
    29 | 0 => none
    30 | n + 1 => some (decodeInt n)
    31
    32end Lax489179.IntegerEncoding
    33

    Community review

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above; your ORCID profile must share a public name.

    0 comments

    Loading discussion…