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

Lax194892.ForTransducers

For-transducers

concepts/Lax194892/ForTransducers.lean · lax-194892

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

    In the paper

    Definition

    A for-transducer (Section D.1 of Transducers) is an imperative program with forfor loops ranging over the positions of the input string, in increasing or decreasing order. It has position variables, bound by the loops and read only, and Boolean variables, initially false and assignable; its tests compare positions (x==yx == y, x<=yx <= y), read the letter at a position (w[x]==aw[x] == a) and read Boolean variables, with Boolean connectives; and its statements are output(b)output(b), assignment to a Boolean variable, sequential composition, conditionals and loops. The output of a run is the concatenation of the output letters produced. A for-transducer is in prenex form (Definition D.1.2) if it is a block of nested loops whose body is loop-free and produces at most one output letter per iteration, followed by a loop-free epilogue. For-transducers compute exactly the polyregular functions (Theorem D.1.1).

    Lean source view on GitHub

    1import Mathlib.Logic.Function.Basic
    2import Mathlib.Data.List.Basic
    3
    4/-!
    5---
    6title: For-transducers
    7type: definition
    8---
    9A *for-transducer* (Section D.1 of *Transducers*) is an imperative program
    10with `for` loops ranging over the positions of the input string, in increasing
    11or decreasing order. It has position variables, bound by the loops and read
    12only, and Boolean variables, initially false and assignable; its tests compare
    13positions (`x == y`, `x <= y`), read the letter at a position (`w[x] == a`) and
    14read Boolean variables, with Boolean connectives; and its statements are
    15`output(b)`, assignment to a Boolean variable, sequential composition,
    16conditionals and loops. The output of a run is the concatenation of the output
    17letters produced. A for-transducer is in *prenex form* (Definition D.1.2) if it
    18is a block of nested loops whose body is loop-free and produces at most one
    19output letter per iteration, followed by a loop-free epilogue. For-transducers
    20compute exactly the polyregular functions (Theorem D.1.1).
    21
    22# Formalization notes
    23
    24Variables of both kinds are named by natural numbers; a valuation of the
    25position variables is `ℕ → ℕ` and of the Boolean variables `ℕ → Bool`. `exec`
    26runs a program from a valuation and returns the final Boolean valuation and the
    27output; `eval P w` is the output from the all-zero valuations. `loop true x P`
    28ranges over the positions in increasing order, `loop false x P` in decreasing
    29order.
    30-/
    31
    32namespace Lax194892.ForTransducers
    33
    34/-- Tests of a for-transducer. -/
    35inductive ForTest (A : Type) : Type
    36 /-- The value of a Boolean variable. -/
    37 | boolVar : ℕ → ForTest A
    38 /-- The equality test `x == y` on position variables. -/
    39 | eqPos : ℕ → ℕ → ForTest A
    40 /-- The order test `x <= y` on position variables. -/
    41 | lePos : ℕ → ℕ → ForTest A
    42 /-- The label test `w[x] == a`. -/
    43 | label : ℕ → A → ForTest A
    44 /-- Negation. -/
    45 | not : ForTest A → ForTest A
    46 /-- Conjunction. -/
    47 | and : ForTest A → ForTest A → ForTest A
    48 /-- Disjunction. -/
    49 | or : ForTest A → ForTest A → ForTest A
    50
    51/-- Programs of a for-transducer. -/
    52inductive ForProg (A B : Type) : Type
    53 /-- The empty program. -/
    54 | skip : ForProg A B
    55 /-- `output(b)`. -/
    56 | output : B → ForProg A B
    57 /-- `X = true` / `X = false`. -/
    58 | assign : ℕ → Bool → ForProg A B
    59 /-- Sequential composition `I ; J`. -/
    60 | seq : ForProg A B → ForProg A B → ForProg A B
    61 /-- A conditional. -/
    62 | ite : ForTest A → ForProg A B → ForProg A B → ForProg A B
    63 /-- `for x in positions(w)` (`true`) or `for x in positions_reverse(w)` (`false`). -/
    64 | loop : Bool → ℕ → ForProg A B → ForProg A B
    65
    66namespace ForTest
    67
    68variable {A : Type}
    69
    70/-- The truth value of a test under valuations of the position and Boolean
    71variables. -/
    72def Holds (w : List A) (pos : ℕ → ℕ) (bv : ℕ → Bool) : ForTest A → Prop
    73 | boolVar i => bv i = true
    74 | eqPos i j => pos i = pos j
    75 | lePos i j => pos i ≤ pos j
    76 | label i a => w[pos i]? = some a
    77 | not t => ¬ Holds w pos bv t
    78 | and t s => Holds w pos bv t ∧ Holds w pos bv s
    79 | or t s => Holds w pos bv t ∨ Holds w pos bv s
    80
    81end ForTest
    82
    83/-- Running the body of a loop over a list of positions, threading the Boolean
    84valuation and concatenating the outputs. -/
    85def forLoopRun {B : Type} (body : (ℕ → Bool) → ℕ → (ℕ → Bool) × List B) :
    86 List ℕ → (ℕ → Bool) → (ℕ → Bool) × List B
    87 | [], bv => (bv, [])
    88 | p :: ps, bv =>
    89 let r := body bv p
    90 let r' := forLoopRun body ps r.1
    91 (r'.1, r.2 ++ r'.2)
    92
    93namespace ForProg
    94
    95variable {A B : Type}
    96
    97open scoped Classical in
    98/-- The semantics of a program: the final Boolean valuation and the output. -/
    99noncomputable def exec (w : List A) :
    100 ForProg A B → (ℕ → ℕ) → (ℕ → Bool) → (ℕ → Bool) × List B
    101 | skip, _, bv => (bv, [])
    102 | output b, _, bv => (bv, [b])
    103 | assign i v, _, bv => (Function.update bv i v, [])
    104 | seq P Q, pos, bv =>
    105 let r := exec w P pos bv
    106 let r' := exec w Q pos r.1
    107 (r'.1, r.2 ++ r'.2)
    108 | ite t P Q, pos, bv =>
    109 if ForTest.Holds w pos bv t then exec w P pos bv else exec w Q pos bv
    110 | loop dir x P, pos, bv =>
    111 forLoopRun (fun bv' p => exec w P (Function.update pos x p) bv')
    112 (if dir then List.range w.length else (List.range w.length).reverse) bv
    113
    114/-- The function computed by a for-transducer. -/
    115noncomputable def eval (P : ForProg A B) (w : List A) : List B :=
    116 (exec w P (fun _ => 0) (fun _ => false)).2
    117
    118/-- A program without loops. -/
    119def LoopFree : ForProg A B → Prop
    120 | skip => True
    121 | output _ => True
    122 | assign _ _ => True
    123 | seq P Q => LoopFree P ∧ LoopFree Q
    124 | ite _ P Q => LoopFree P ∧ LoopFree Q
    125 | loop _ _ _ => False
    126
    127/-- Nested loops `for x₁ in τ₁: ⋯ for x_k in τ_k: body`. -/
    128def nestLoops : List (Bool × ℕ) → ForProg A B → ForProg A B
    129 | [], body => body
    130 | (d, x) :: rest, body => ForProg.loop d x (nestLoops rest body)
    131
    132/-- A program produces at most one output letter per execution. -/
    133def OutputsAtMostOne (P : ForProg A B) : Prop :=
    134 ∀ (w : List A) (pos : ℕ → ℕ) (bv : ℕ → Bool), ((exec w P pos bv).2).length ≤ 1
    135
    136/-- Prenex form: nested loops whose body is loop-free and outputs at most one
    137letter per iteration, followed by a loop-free epilogue. -/
    138def PrenexForm (P : ForProg A B) : Prop :=
    139 ∃ (ls : List (Bool × ℕ)) (body epilogue : ForProg A B),
    140 LoopFree body ∧ LoopFree epilogue ∧ OutputsAtMostOne body ∧
    141 P = ForProg.seq (nestLoops ls body) epilogue
    142
    143end ForProg
    144
    145/-- A function computed by a for-transducer. -/
    146def IsForTransducer {A B : Type} (f : List A → List B) : Prop :=
    147 ∃ P : ForProg A B, ∀ w, P.eval w = f w
    148
    149end Lax194892.ForTransducers
    150

    Formalization notes

    Variables of both kinds are named by natural numbers; a valuation of the position variables is NNℕ → ℕ and of the Boolean variables NBoolℕ → Bool. execexec runs a program from a valuation and returns the final Boolean valuation and the output; evalPweval P w is the output from the all-zero valuations. looptruexPloop true x P ranges over the positions in increasing order, loopfalsexPloop false x P in decreasing order.

    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…