Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions REUSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ named `budget` argument. The object index below supplies owners for those declar
| Move outside query and reply maps through attachment | `relabel_apply_outside_both` in `RandomSystems.DDC.DDCRelabel` |
| Extend distinct list-position labels to evaluated inputs | `List.exists_perm_apply_idxOf_eq` in `Probability.List.Permutation` |
| Evaluate a completed adaptive invocation from its local trace | `Program.combine_eq_of_consistent_replies` in `RandomSystems.Converter.ProgramTrace` |
| Certify a scheduled collection and attach it to retained state | `DSL.collectCalls_exchangesOf` in `RandomSystems.Typed.DSL.Collect`; `Program.combine_eq_of_collectCalls` in `CollectAttachment` |
| Transport complete register converters through address embeddings | `registersDDC_mapCommand` in `Commons.Definitions.AbstractModel.RegisterDDC.AddressMap` |
| Transport oracle bodies through state and outside-query maps | `Program.step_stateInputMap` in `RandomSystems.Converter.Program`; `DSL.callProg_map` in `RandomSystems.Typed.DSL.Programs` |
| Rename register addresses while preserving contents | `AbstractModel.mapInput`, `mapCommand`, `registers_mapCommand` in `Commons.Definitions.AbstractModel` and `Registers.Laws` |
Expand Down
1 change: 1 addition & 0 deletions src/RandomSystems.lean
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public import RandomSystems.Converter.ChronologicalLabeling
public import RandomSystems.Converter.ChronologicalLabeling.Laws

public import RandomSystems.Converter.ProgramTrace
public import RandomSystems.Typed.DSL.CollectAttachment


/-!
Expand Down
33 changes: 32 additions & 1 deletion src/RandomSystems/Typed/DSL/Collect.lean
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Replies retain their requested types; `finish` receives the dependent tuple. The
actual invocation order, while the inside interface keeps its original port labels.

`collectCalls_cost` gives exactly `n` calls and `collectCalls_next` gives their exact order.
`collectCalls_reply` computes the result of matching replies. Against fresh inside laws,
`collectCalls_exchangesOf` certifies the scheduled query/reply trace; `collectCalls_reply`
computes its completed result. Against fresh inside laws,
`Program.memorylessReplyLaw_collectCalls` gives the pushforward of their independent product,
using `piDep_fin_zero` and `piDep_fin_succ` from `Probability.DependentProduct`.

Expand Down Expand Up @@ -62,6 +63,36 @@ lemma collectCalls_cost
-- One matching call precedes the remaining collection.
exact callCost_call _ _ _ fun _ => ih _ _

/-- The scheduled queries with replies at their requested ports form the body's own exchanges. -/
lemma collectCalls_exchangesOf
-- `request` lists the calls; `finish` computes the completed result.
(n : ℕ) (request : Fin n → Σ j, X j)
(finish : ((i : Fin n) → Y (request i).1) → R)
-- `ys i` has the reply type of the port selected by `request i`.
(ys : (i : Fin n) → Y (request i).1) :
Program.ExchangesOf (collectCalls n request finish)
(List.ofFn fun i => (request i, ⟨(request i).1, ys i⟩)) := by
induction n with
| zero =>
intro i hi
simp only [List.ofFn_zero, List.length_nil, Nat.not_lt_zero] at hi
| succ n ih =>
intro i hi
cases i with
| zero =>
simp only [List.ofFn_succ, List.take_zero, collectCalls, callProg,
List.getElem_cons_zero]
exact ⟨True.intro, True.intro⟩
| succ i =>
-- After its first matching reply, collection follows the remaining schedule.
have tailIndex : i < (List.ofFn fun j : Fin n =>
(request j.succ, (⟨(request j.succ).1, ys j.succ⟩ : Σ j, Y j))).length := by
simpa only [List.length_ofFn, Nat.succ_lt_succ_iff] using hi
have tail := ih (fun j => request j.succ)
(fun zs => finish (Fin.cons (ys 0) zs)) (fun j => ys j.succ) i tailIndex
simpa only [List.ofFn_succ, List.take_succ_cons, List.getElem_cons_succ,
collectCalls, callProg, ↓reduceDIte] using tail

/-- A complete sequence of matching replies returns `finish ys`. -/
lemma collectCalls_reply
-- `request` lists the `n` queries; `finish` combines their replies `ys` into one result.
Expand Down
51 changes: 51 additions & 0 deletions src/RandomSystems/Typed/DSL/CollectAttachment.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module

public import RandomSystems.Typed.DSL.Collect
public import RandomSystems.Converter.ProgramTrace

/-!
# Attaching an ordered collection

`Program.combine_eq_of_collectCalls` computes the combined step from a scheduled reply tuple.
Collection supplies its own trace consistency and terminal result; the inside automaton supplies
its actual replies. The generic completed-trace law retains the resulting inside state.
-/

@[expose] public section

namespace SystemAlgebra.Program

open SystemAlgebra.DSL

/-- Collecting actual inside replies returns `finish ys` and retains the inside query fold. -/
lemma combine_eq_of_collectCalls
-- `P` carries program state `S`; `stepR` updates the retained inside state `R`.
{S R O J : Type}
-- `O` and `J` label outside and inside ports; `U`/`V` and `X`/`Y` carry their queries/replies.
{U V : O → Type} {X Y : J → Type}
(P : Program S O J U V X Y) (stepR : R → (j : J) → X j → R × Y j)
-- `bounded` certifies the existing total attachment presentation.
{b : ℕ} (bounded : P.Bounded b) (s : S) (o : O) (u : U o) (r : R)
-- `request` specifies this invocation's calls; `finish` combines their typed replies.
(n : ℕ) (request : Fin n → Σ j, X j)
(finish : ((i : Fin n) → Y (request i).1) → S × V o)
(body : ∀ h, P s o u h = collectCalls n request finish h)
-- `ys` are the actual replies from the same inside state, in the scheduled order.
(ys : (i : Fin n) → Y (request i).1)
(replies : Replies (automatonSystem stepR r) (List.ofFn request)
(List.ofFn fun i => (⟨(request i).1, ys i⟩ : Σ j, Y j))) :
P.combine stepR bounded (s, r) o u =
(((finish ys).1, stateAfter stepR r (List.ofFn request)), (finish ys).2) := by
let h := List.ofFn fun i => (request i, (⟨(request i).1, ys i⟩ : Σ j, Y j))
have consistent : P.Consistent s o u h := by
change ExchangesOf (P s o u) h
rw [funext body]
exact collectCalls_exchangesOf n request finish ys
have returned : P s o u h = .inl (finish ys) :=
(body h).trans (collectCalls_reply n request finish ys)
have actual : Replies (automatonSystem stepR r) (h.map Prod.fst) (h.map Prod.snd) := by
simpa only [h, List.map_ofFn, Function.comp_def] using replies
simpa only [h, List.map_ofFn, Function.comp_def] using
P.combine_eq_of_consistent_replies stepR bounded s o u r h consistent actual returned

end SystemAlgebra.Program
2 changes: 2 additions & 0 deletions src/Tests.lean
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ public import Tests.AbstractModelDomainMap
public import Tests.RegisterAddressCompiler

public import Tests.ProgramTrace
public import Tests.CollectCallsTrace
public import Tests.CollectAttachment

public import Tests.ListPermutation

Expand Down
75 changes: 75 additions & 0 deletions src/Tests/CollectAttachment.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module

public import RandomSystems.Typed.DSL.CollectAttachment
public import Tests.CollectCallsTrace

/-!
# Collection against retained state

The Boolean port checks whether the inside counter is zero; the natural-number port returns
its current value plus six. Both calls increment the same retained counter. The second reply
therefore distinguishes shared state from restarting the inside automaton for each call.
An empty collection leaves the counter unchanged.
-/

@[expose] public section

namespace Tests.CollectAttachment

open SystemAlgebra SystemAlgebra.DSL Tests.CollectCallsTrace

/-- Increment the counter; reply with its zero test at `false`, or its value plus six at `true`. -/
def advance (count : Nat) : (port : Bool) → Unit → Nat × Reply port
| false, _ => (count + 1, count == 0)
| true, _ => (count + 1, count + 6)

/-- Ask the existing Boolean/natural schedule and return its ordered pair. -/
noncomputable def collectPair : Program Unit Unit Bool (fun _ => Unit)
(fun _ => Bool × Nat) (fun _ => Unit) Reply :=
fun _ _ _ => collectCalls (Y := Reply) 2 request (fun ys => ((), finish ys))

lemma collectPair_bounded : collectPair.Bounded 2 := by
intro s o u h q consistent issued
exact (collectCalls_cost (Y := Reply) 2 request
(fun ys => ((), finish ys)) h consistent).1 q issued

/-- The shared counter starts at zero, giving Boolean `true` and then natural number `7`. -/
lemma scheduled_replies : Replies (automatonSystem advance 0) (List.ofFn request)
(List.ofFn fun i => (⟨(request i).1, answers i⟩ : Σ port, Reply port)) := by
change Replies (automatonSystem advance 0)
(([] ++ [⟨false, ()⟩]) ++ [⟨true, ()⟩])
(([] ++ [⟨false, true⟩]) ++ [⟨true, (7 : Nat)⟩])
apply replies_snoc.mpr
refine ⟨replies_snoc.mpr ⟨replies_nil _, ?_⟩, ?_⟩
· rw [automatonSystem_snoc]
rfl
· rw [automatonSystem_snoc]
rfl

/-- Collection returns `(true,7)` and leaves the same inside counter at two. -/
theorem collected_result : collectPair.combine advance collectPair_bounded ((), 0) () () =
(((), 2), (true, 7)) :=
Program.combine_eq_of_collectCalls collectPair advance collectPair_bounded () () () 0
2 request (fun ys => ((), finish ys)) (fun _ => rfl) answers scheduled_replies

/-- A zero-call collection returns 17 without querying either port. -/
noncomputable def collectNone : Program Unit Unit Bool (fun _ => Unit)
(fun _ => Nat) (fun _ => Unit) Reply :=
fun _ _ _ => collectCalls 0 (fun i => Fin.elim0 i) (fun _ => ((), 17))

lemma collectNone_bounded : collectNone.Bounded 0 := by
intro s o u
exact callBound_return 0 _

/-- Empty collection retains the initial inside counter nine. -/
theorem empty_result : collectNone.combine advance collectNone_bounded ((), 9) () () =
(((), 9), 17) := by
exact Program.combine_eq_of_collectCalls collectNone advance collectNone_bounded () () () 9
0 (fun i => Fin.elim0 i) (fun _ => ((), 17)) (fun _ => rfl) (fun i => Fin.elim0 i)
(by simpa only [List.ofFn_zero] using replies_nil (automatonSystem advance 9))

#print axioms Program.combine_eq_of_collectCalls
#print axioms collected_result
#print axioms empty_result

end Tests.CollectAttachment
70 changes: 70 additions & 0 deletions src/Tests/CollectCallsTrace.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
module

public import RandomSystems.Typed.DSL.Collect

/-!
# Canonical collection traces

A two-port schedule receives a Boolean at its first port and a natural number at its second.
The forward certificate checks this dependent reply tuple. The empty schedule has no exchanges
and returns its selected result immediately.
-/

@[expose] public section

namespace Tests.CollectCallsTrace

open SystemAlgebra SystemAlgebra.DSL

/-- Port `false` returns a Boolean; port `true` returns a natural number. -/
def Reply : Bool → Type
| false => Bool
| true => Nat

/-- Ask the Boolean port, then the natural-number port; both inputs are unit. -/
def request : Fin 2 → Σ _ : Bool, Unit :=
Fin.cases ⟨false, ()⟩ (fun _ => ⟨true, ()⟩)

/-- The two replies are `true` and `7`, each in its requested port's type. -/
def answers : (i : Fin 2) → Reply (request i).1 :=
Fin.cases true (fun _ => (7 : Nat))

/-- Return the ordered Boolean/natural pair. -/
def finish (ys : (i : Fin 2) → Reply (request i).1) : Bool × Nat :=
(ys 0, ys 1)

/-- The scheduled dependent replies give this concrete ordered trace. -/
lemma recorded_trace :
(List.ofFn fun i => (request i, (⟨(request i).1, answers i⟩ : Σ port, Reply port))) =
[(⟨false, ()⟩, ⟨false, true⟩), (⟨true, ()⟩, ⟨true, (7 : Nat)⟩)] := rfl

/-- The concrete heterogeneous trace follows collection's own query order. -/
theorem dependent_exchanges : Program.ExchangesOf
(collectCalls (Y := Reply) 2 request finish)
[(⟨false, ()⟩, ⟨false, true⟩), (⟨true, ()⟩, ⟨true, (7 : Nat)⟩)] := by
rw [← recorded_trace]
exact collectCalls_exchangesOf (Y := Reply) 2 request finish answers

/-- The same trace returns the selected pair. -/
example : collectCalls (Y := Reply) 2 request finish
[(⟨false, ()⟩, ⟨false, true⟩), (⟨true, ()⟩, ⟨true, (7 : Nat)⟩)] = .inl (true, 7) := by
rw [← recorded_trace]
exact collectCalls_reply (Y := Reply) 2 request finish answers

/-- A zero-call schedule certifies the empty trace. -/
theorem empty_exchanges : Program.ExchangesOf
(collectCalls (X := fun _ : Bool => Unit) (Y := Reply)
0 (fun i => Fin.elim0 i) (fun _ => (17 : Nat))) [] := by
simpa only [List.ofFn_zero] using
collectCalls_exchangesOf (X := fun _ : Bool => Unit) (Y := Reply)
0 (fun i => Fin.elim0 i) (fun _ => (17 : Nat)) (fun i => Fin.elim0 i)

/-- Empty collection returns 17 without an inside exchange. -/
example : collectCalls (X := fun _ : Bool => Unit) (Y := Reply)
0 (fun i => Fin.elim0 i) (fun _ => (17 : Nat)) [] = .inl 17 := rfl

#print axioms collectCalls_exchangesOf
#print axioms dependent_exchanges
#print axioms empty_exchanges

end Tests.CollectCallsTrace