Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
6ac633e
feat(MultiTapeTM): machines as transformers of tape words
crei Sep 9, 2026
e3c67fb
feat(MultiTapeTM): a machine that clears a work tape
crei Sep 9, 2026
381156c
fix(MultiTapeTM): expose TransformsTapes
crei Sep 9, 2026
745cd0e
refactor(MultiTapeTM): rename listTape to tapeOfList
crei Sep 9, 2026
fa83806
feat(MultiTapeTM): sequential composition of tape transformations
crei Sep 9, 2026
5ccdd95
feat(MultiTapeTM): the tidy normal form, and the raw sequential layer
crei Sep 9, 2026
ba825ac
feat(MultiTapeTM): a machine that rewinds the input head
crei Sep 9, 2026
e7eee98
feat(MultiTapeTM): instrumenting a machine with footprint tapes
crei Sep 9, 2026
6926923
feat(MultiTapeTM): anchors and the space of an instrumented run
crei Sep 9, 2026
96ac00e
feat(MultiTapeTM): the sweep that erases a footprinted tape pair
crei Sep 9, 2026
176e3fd
chore(MultiTapeTM): silence the sweep's unused-binder lints
crei Sep 9, 2026
b5cba22
feat(MultiTapeTM): mark the cells under the footprint heads
crei Sep 9, 2026
7a0f58b
feat(MultiTapeTM): support lemmas for the tidy assembly
crei Sep 9, 2026
a863fda
chore(MultiTapeTM): modern dite lemmas in val_moveInputPos_le
crei Sep 9, 2026
f4d053d
feat(MultiTapeTM): the tidy normal form theorem
crei Sep 9, 2026
2884eba
feat(MultiTapeTM): redirecting the output to a work tape
crei Sep 9, 2026
92efab5
feat(MultiTapeTM): reading the input from a work tape
crei Sep 9, 2026
4c77bfd
feat(MultiTapeTM): step-reduction and SignType/omega tooling
crei Sep 9, 2026
8bd49b9
refactor(MultiTapeTM): drop redundant SignType cast lemmas
crei Sep 9, 2026
4361262
feat(MultiTapeTM): the input-redirection semiconjugation
crei Sep 9, 2026
7a67bdd
feat(MultiTapeTM): a machine that rewinds a work-tape head
crei Sep 9, 2026
62392a8
feat(MultiTapeTM): connection lemmas for the output-redirection adapter
crei Sep 9, 2026
c3e674d
refactor(MultiTapeTM): promote seq_spec to public in Sequential
crei Sep 9, 2026
54e8876
feat(MultiTapeTM): bound the rewindTape head excursion
crei Sep 9, 2026
f92363d
feat(MultiTapeTM): space bound for a run with one moving head
crei Sep 9, 2026
3439c60
fix(MultiTapeTM): actually add the output-adapter connection lemmas
crei Sep 9, 2026
66ed381
chore(MultiTapeTM): trim unused simp args in withOutput step
crei Sep 9, 2026
1b6d298
feat(MultiTapeTM): a computable function read from the input tape, as…
crei Sep 9, 2026
ac24648
feat(MultiTapeTM): reindex a machine's tapes along an embedding
crei Sep 9, 2026
df8f12e
feat(MultiTapeTM): machines to emit a tape to the output and to set a…
crei Sep 9, 2026
6236667
feat(MultiTapeTM): the NeverOutputs building block
crei Sep 9, 2026
7f756f2
feat(MultiTapeTM): space bound for the input-redirected machine
crei Sep 9, 2026
7413491
feat(MultiTapeTM): a computable function read from a work tape, as a …
crei Sep 9, 2026
8111025
feat(MultiTapeTM): from a tape transformer back to a computable function
crei Sep 9, 2026
d31b5d6
feat(MultiTapeTM): reindexing preserves being a tape transformer
crei Sep 9, 2026
9f11d17
wip(MultiTapeTM): computableInTimeAndSpace_comp statement (sorry body)
crei Sep 9, 2026
3cf2b3d
chore(MultiTapeTM): modern dite lemmas in ExtendTapes
crei Sep 9, 2026
e72e09b
feat(MultiTapeTM): complexity of function composition
crei Sep 9, 2026
1df08ee
feat(MultiTapeTM): branching on a tape symbol
crei Sep 9, 2026
a6d970f
feat(MultiTapeTM): repeating a machine until a tape signals stop
crei Sep 9, 2026
c808324
feat(Turing): general-layout adapters for computable tape transformers
crei Sep 10, 2026
dd126a4
feat(Turing): prove complexity of the loop combinator
crei Sep 10, 2026
2451681
refactor(Turing): simplify computableInTimeAndSpace_comp via the adap…
crei Sep 10, 2026
34fdb38
refactor(Turing): move computableInTimeAndSpace_comp to Combinators
crei Sep 10, 2026
3046b6a
refactor(Turing): reuse length_encOut_le in ofComputableInput_fixed
crei Sep 10, 2026
18b5a91
refactor(Turing): remove dead declarations and dedup low-level lemmas
crei Sep 10, 2026
be54f65
refactor(Turing): dedup proofs across adapters, loop, and tidy machines
crei Sep 10, 2026
f16021c
refactor(Turing): Mathlib-readiness polish from review
crei Sep 10, 2026
15e2245
feat(Turing): streaming-dispatch tool exists_branch_run
crei Sep 10, 2026
38409b6
feat(Turing): boolEnc and computableInTimeAndSpace_cond
crei Sep 10, 2026
5c91bd4
feat(Turing): finite case analysis computableInTimeAndSpace_match
crei Sep 10, 2026
a368919
refactor(Turing): unify state-remap embeddings via Cfg.mapState
crei Sep 10, 2026
b196faf
refactor(Turing): share wordsCfg_eq_embed between Adapters and Ite
crei Sep 10, 2026
41de338
feat(Turing): input-symbol dispatch machine exists_inputBranch_run
crei Sep 10, 2026
4e85540
feat(Turing): computableInTimeAndSpace_iteFirstBit atom
crei Sep 10, 2026
adffe15
feat(Turing): computableInTimeAndSpace_concat and _pair atoms
crei Sep 10, 2026
3cb4f4a
feat(Turing): computableInTimeAndSpace_drop and _take atoms
crei Sep 10, 2026
a37463b
refactor(Turing): rebuild cond/ite/dite/match on elementary atoms
crei Sep 10, 2026
c6c4981
refactor(Turing): remove now-unused exists_branch_run; rewrite Ite doc
crei Sep 10, 2026
31396a3
refactor(Turing): factor branch/inputBranch onto a shared `armed` core
crei Sep 10, 2026
4cedcf3
refactor(Turing): single time/space bound for iteFirstBit's two arms
crei Sep 10, 2026
0edd525
refactor(Turing): rebuild cond on the transformer path, drop iteFirstBit
crei Sep 10, 2026
1593bff
refactor(Turing): delete the now-dead input-branch machinery
crei Sep 10, 2026
b76d2f1
refactor(Turing): delete dead concat/pair/take/drop combinators
crei Sep 10, 2026
eea37ab
refactor(Turing): share a single t s across cond/ite/dite
crei Sep 10, 2026
1cf2082
ci: drop manual TEST_ARGS step (#896)
jessealama Sep 11, 2026
1aa5810
chore(Turing): align tape-transformer files with the split-out transf…
crei Sep 11, 2026
70b7176
docs: add campaign PR plan (dependency DAG + proposed PR split)
crei Sep 11, 2026
1cda4bb
fix(PACLearning): restrict consistency to realizable samples (#747)
SamuelSchlesinger Sep 11, 2026
b777e08
feat(governance): add Xueying Qin as reviewer (#898)
fmontesi Sep 11, 2026
bdbd823
feat: add a predicate for monad morphisms (#856)
eric-wieser Sep 12, 2026
ec95751
feat(Foundations/Relation/Confluence): generalize results from conflu…
thomaskwaring Sep 13, 2026
966d27a
feat(CCS): notation for CCS (#771)
fmontesi Sep 14, 2026
37297d2
chore: Bump `mathlib` dependency to 87befc8 (#645)
mathlib-nightly-testing[bot] Sep 14, 2026
105c46e
feat(MultitapeTM): Prove an exponential upper bound in the number of …
crei Sep 14, 2026
a374775
feat(MultiTapeTM): Any function constant with finitely many exception…
crei Sep 14, 2026
91ab23c
fix: correct Confluent.to_churchRosser alias (#906)
lengyijun Sep 15, 2026
990e65a
chore: bump toolchain to v4.34.0 (#907)
Garmelon Sep 15, 2026
2277592
chore: bump toolchain to v4.35.0-rc1 (#909)
Garmelon Sep 15, 2026
34a6570
ci: add Dependabot config for actions and pip (#895)
jessealama Sep 16, 2026
e9ebe0f
ci: pin zulip in a requirements file (#894)
jessealama Sep 16, 2026
b55fc86
ci: lint workflow files with actionlint (#892)
jessealama Sep 16, 2026
012cc2f
ci(nightly-testing): pass ZULIP_API_KEY through env instead of interp…
jessealama Sep 16, 2026
0ef0424
chore: bump toolchain to v4.35.0-rc2 (#916)
Garmelon Sep 17, 2026
41094e5
feat(Relation): Normal.sup_iff (#922)
lengyijun Sep 18, 2026
17ff855
feat(Automata): Two-way automata accept exactly the regular languages…
crei Sep 18, 2026
758c2c5
feat(MultiTapeTM): TransformsTapes interface and sequential compositi…
crei Sep 18, 2026
619c183
feat(Circuit): prove Lupanov upper bound (#890)
SamuelSchlesinger Sep 18, 2026
ba09df8
feat(Language): characterizing regular languages using syntactic mono…
ctchou Sep 20, 2026
e68a7a7
feat(Governance): add Samuel Schlesinger to maintainers (#930)
fmontesi Sep 21, 2026
c906ed2
feat(Circuit): prove Shannon lower bound (#891)
SamuelSchlesinger Sep 21, 2026
4635aeb
feat(CODEOWNERS): add SamuelSchlesinger as a code owner (#932)
fmontesi Sep 21, 2026
133d92d
chore: bump mathlib to 1cae91f, fix breaking changes (#934)
mathlib-nightly-testing[bot] Sep 21, 2026
8100246
Merge origin/main into loop/06-tape-transformers
crei Sep 22, 2026
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
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# For an overview of the governance model of cslib, please refer to /GOVERNANCE.md and /DECISION_MAKING.md

* @fmontesi @chenson2018 @kim-em @arademaker @sorrachai
* @fmontesi @chenson2018 @kim-em @arademaker @SamuelSchlesinger @sorrachai
/.github/CODEOWNERS @fmontesi
/DECISION_MAKING.md @fmontesi
25 changes: 25 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2
updates:
# Keep action pins fresh. Grouped so that all bumps land in a single weekly PR,
# which bounds the number of full Lean CI builds this triggers.
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
groups:
actions:
patterns: ["*"]
commit-message:
prefix: ci

# Python packages installed by workflows (see requirements.txt in this directory).
# Major bumps are left for a human to review.
- package-ecosystem: pip
directory: /.github
schedule:
interval: weekly
ignore:
- dependency-name: "*"
update-types: ["version-update:semver-major"]
commit-message:
prefix: ci
1 change: 1 addition & 0 deletions .github/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zulip==0.9.1
31 changes: 31 additions & 0 deletions .github/workflows/actionlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: actionlint

on:
push:
branches:
- main
paths:
- '.github/workflows/**'
pull_request:
paths:
- '.github/workflows/**'
workflow_dispatch:

permissions:
contents: read

jobs:
actionlint:
name: Lint workflow files
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
sparse-checkout: |
.github/workflows

# Also runs shellcheck over every `run:` block and pyflakes over
# `shell: python` steps.
- uses: raven-actions/actionlint@3d39aea434753780c3b3d4a1a31c854b4dbf49d7 # v2.2.0
with:
version: 1.7.12
2 changes: 1 addition & 1 deletion .github/workflows/bump_toolchain_nightly-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
# Extract date from RELEASE_TAG (format: nightly-YYYY-MM-DD)
DATE_PART=$(echo "$RELEASE_TAG" | sed 's/nightly-//')
DATE_PART="${RELEASE_TAG#nightly-}"
NIGHTLY_TESTING_TAG="nightly-testing-${DATE_PART}"
echo "NIGHTLY_TESTING_TAG=$NIGHTLY_TESTING_TAG" >> "${GITHUB_ENV}"

Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/lean_action_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set TEST_ARGS manually
run: |
echo "TEST_ARGS='--wfail --iofail'" >> $GITHUB_ENV
shell: bash
- uses: leanprover/lean-action@v1
with:
build-args: "--wfail --iofail"
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/report_failures_nightly-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
# Now post a success message to zulip, if the last message there is not a success message.
# https://chat.openai.com/share/87656d2c-c804-4583-91aa-426d4f1537b3
- name: Install Zulip API client
run: pip install zulip
run: pip install -r .github/requirements.txt

- name: Check last message and post if necessary
env:
Expand Down Expand Up @@ -268,17 +268,18 @@ jobs:
BUMP_VERSION: ${{ steps.bump_version.outputs.result }}
BUMP_BRANCH: ${{ steps.latest_bump_branch.outputs.result }}
SHA: ${{ env.SHA }}
ZULIP_API_KEY: ${{ secrets.ZULIP_API_KEY }}
run: |
echo "Installing zulip CLI..."
pip install zulip
pip install -r .github/requirements.txt
echo "Configuring git identity for mathlib4-bot..."
git config --global user.name "mathlib4-bot"
git config --global user.email "github-mathlib4-bot@leanprover.zulipchat.com"
echo "Setting up zulip credentials..."
{
echo "[api]"
echo "email=github-mathlib4-bot@leanprover.zulipchat.com"
echo "key=${{ secrets.ZULIP_API_KEY }}"
echo "key=$ZULIP_API_KEY"
echo "site=https://leanprover.zulipchat.com"
} > ~/.zuliprc
chmod 600 ~/.zuliprc
Expand Down
44 changes: 43 additions & 1 deletion Cslib.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,23 @@ public import Cslib.Computability.Automata.NA.ToDA
public import Cslib.Computability.Automata.NA.Total
public import Cslib.Computability.Automata.Transducers.Transducer
public import Cslib.Computability.Automata.TwoWayNA.Basic
public import Cslib.Computability.Automata.TwoWayNA.ComplToNA
public import Cslib.Computability.Automata.TwoWayNA.OfNA
public import Cslib.Computability.Circuit.Basic
public import Cslib.Computability.Circuit.Boolean.Basic
public import Cslib.Computability.Circuit.Boolean.Counting
public import Cslib.Computability.Circuit.Boolean.Lupanov
public import Cslib.Computability.Circuit.Boolean.LupanovConstruction
public import Cslib.Computability.Circuit.Boolean.Shannon
public import Cslib.Computability.Circuit.Boolean.Synthesis
public import Cslib.Computability.Circuit.Counting
public import Cslib.Computability.Circuit.Finite
public import Cslib.Computability.Circuit.Homomorphism
public import Cslib.Computability.Circuit.Normalization
public import Cslib.Computability.Circuit.Program
public import Cslib.Computability.Circuit.Shannon
public import Cslib.Computability.Circuit.Signature
public import Cslib.Computability.Circuit.Synthesis
public import Cslib.Computability.Circuit.Wire
public import Cslib.Computability.Distributed.FLP.Algorithm
public import Cslib.Computability.Distributed.FLP.CanReachVia
Expand All @@ -44,8 +57,9 @@ public import Cslib.Computability.Distributed.FLP.Impossibility
public import Cslib.Computability.Distributed.FLP.OnePseudoConsensus
public import Cslib.Computability.Distributed.FLP.PseudoConsensus
public import Cslib.Computability.Distributed.FLP.ZeroConsensus
public import Cslib.Computability.Languages.Congruences.Basic
public import Cslib.Computability.Languages.Congruences.BuchiCongruence
public import Cslib.Computability.Languages.Congruences.RightCongruence
public import Cslib.Computability.Languages.Congruences.MyhillCongruence
public import Cslib.Computability.Languages.ExampleEventuallyZero
public import Cslib.Computability.Languages.Language
public import Cslib.Computability.Languages.LanguageHom
Expand All @@ -54,10 +68,34 @@ public import Cslib.Computability.Languages.OmegaLanguage
public import Cslib.Computability.Languages.OmegaRegularLanguage
public import Cslib.Computability.Languages.RegularLanguage
public import Cslib.Computability.Languages.SafetyLiveness
public import Cslib.Computability.Languages.SyntacticMonoid
public import Cslib.Computability.Machines.Turing.MultiTape.Combinators.AlmostConstant
public import Cslib.Computability.Machines.Turing.MultiTape.Combinators.Comp
public import Cslib.Computability.Machines.Turing.MultiTape.Combinators.Id
public import Cslib.Computability.Machines.Turing.MultiTape.Combinators.Ite
public import Cslib.Computability.Machines.Turing.MultiTape.Combinators.Loop
public import Cslib.Computability.Machines.Turing.MultiTape.ConfigBound
public import Cslib.Computability.Machines.Turing.MultiTape.Configuration
public import Cslib.Computability.Machines.Turing.MultiTape.Deterministic
public import Cslib.Computability.Machines.Turing.MultiTape.DeterministicToNondeterministic
public import Cslib.Computability.Machines.Turing.MultiTape.Encodings.Option
public import Cslib.Computability.Machines.Turing.MultiTape.Nondeterministic
public import Cslib.Computability.Machines.Turing.MultiTape.NormalForms.Adapters
public import Cslib.Computability.Machines.Turing.MultiTape.NormalForms.Instrument
public import Cslib.Computability.Machines.Turing.MultiTape.NormalForms.Sweep
public import Cslib.Computability.Machines.Turing.MultiTape.NormalForms.Tidy
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.Branch
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.Clear
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.EmitTape
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.ExtendTapes
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.InputFromTape
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.OutputToTape
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.Repeat
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.RewindInput
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.RewindTape
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.Sequential
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.StepLemmas
public import Cslib.Computability.Machines.Turing.MultiTape.Plumbing.TransformsTapes
public import Cslib.Computability.Machines.Turing.MultiTape.TapeLemmas
public import Cslib.Computability.Machines.Turing.SingleTape.Defs
public import Cslib.Computability.Machines.Turing.SingleTape.Deterministic
Expand All @@ -83,12 +121,16 @@ public import Cslib.Foundations.Combinatorics.InfiniteGraphRamsey
public import Cslib.Foundations.Control.Monad.Free
public import Cslib.Foundations.Control.Monad.Free.Effects
public import Cslib.Foundations.Control.Monad.Free.Fold
public import Cslib.Foundations.Control.Monad.IsMonadHom
public import Cslib.Foundations.Control.Monad.IsMonadHom.List
public import Cslib.Foundations.Data.BiTape
public import Cslib.Foundations.Data.DecidableEqZero
public import Cslib.Foundations.Data.FinFun.Basic
public import Cslib.Foundations.Data.FinFun.Update
public import Cslib.Foundations.Data.HasFresh
public import Cslib.Foundations.Data.List.IsChainFromTo
public import Cslib.Foundations.Data.Nat.Asymptotics
public import Cslib.Foundations.Data.Nat.Factorial
public import Cslib.Foundations.Data.Nat.Segment
public import Cslib.Foundations.Data.OmegaSequence.Defs
public import Cslib.Foundations.Data.OmegaSequence.Flatten
Expand Down
14 changes: 7 additions & 7 deletions Cslib/Algorithms/CCS/VendingMachine.lean
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ inductive Constant
| vm

/-- The vending machine process. -/
def vm : Process String Constant := const .vm
def vm : Process String Constant := `(CCS| const .vm)

/-! ## Deterministic vending machine -/

/-- Constant definitions: vm = coin.(tea.VM + coffee.VM) -/
@[local grind =]
def vendingDefs : Constant → Option (Process String Constant)
| .vm => some <| pre Coin (choice (pre Tea (const .vm)) (pre Coffee (const .vm)))
| .vm => some <| `(CCS| Coin. ((Tea. const .vm) + (Coffee. const .vm)))

/-- The LTS of CCS for the deterministic vending machine. -/
abbrev ltsD := CCS.lts (defs := vendingDefs)

/-- VM can perform a coin action. -/
example : ltsD.Tr vm Coin (choice (pre Tea (const .vm)) (pre Coffee (const .vm))) :=
example : ltsD.Tr vm Coin `(CCS| (Tea. (const .vm)) + (Coffee. (const .vm))) :=
Tr.const rfl Tr.pre

/-! ## Nondeterministic vending machine -/

/-- vm = coin.tea.VM + coin.coffee.VM -/
def vendingDefsND : Constant → Option (Process String Constant)
| .vm => some <| (choice (pre Coin (pre Tea (const .vm))) (pre Coin (pre Coffee (const .vm))))
| .vm => some <| `(CCS| (Coin. Tea. const .vm) + (Coin. Coffee. const .vm))

/-- The LTS of CCS for the nondeterministic vending machine. -/
abbrev ltsND := CCS.lts (defs := vendingDefsND)
Expand All @@ -78,8 +78,8 @@ open LTS LTS.IsBisimulation LTS.Bisimilarity
/-- The deterministic and nondeterministic vending machines are not bisimilar. -/
theorem vm_ltsD_ltsND_not_bisim : ¬(vm ~[ltsD, ltsND] vm) := by
rintro ⟨r, hr, hbisim⟩
let p₁ := (choice (pre Tea (const Constant.vm)) (pre Coffee (const Constant.vm)))
let q₁ := (pre Tea (const Constant.vm))
let p₁ := `(CCS| (Tea. const Constant.vm) + (Coffee. const Constant.vm))
let q₁ := `(CCS| Tea. const Constant.vm)
have ltsD_vm_deterministic : ltsD.DeterministicStateLabel vm Coin := by
intro _ _ htr₁ htr₂
grind [const_tr htr₁, const_tr htr₂]
Expand All @@ -90,7 +90,7 @@ theorem vm_ltsD_ltsND_not_bisim : ¬(vm ~[ltsD, ltsND] vm) := by
(.const rfl .pre)
(.const rfl (.choiceL .pre))
have hp₁q₁ : p₁ ~[ltsD, ltsND] q₁ := by grind
have hp₁coffee : ltsD.Tr p₁ Coffee (const Constant.vm) := .choiceR .pre
have hp₁coffee : ltsD.Tr p₁ Coffee (.const .vm) := .choiceR .pre
grind [hp₁q₁.follow_fst]

end Cslib.Algorithms.CCS.VendingMachine
8 changes: 2 additions & 6 deletions Cslib/Algorithms/Lean/MergeSort/MergeSort.lean
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@ open List in
@[simp, grind =]
theorem ret_mergeM {T} [AddMonoid T] (xs ys : List α) (le : α → α → TimeM T Bool) :
⟪List.mergeM xs ys le⟫ = List.merge xs ys (fun x y => ⟪le x y⟫) := by
fun_induction merge with grind [mergeM, nil_merge, merge_right, cons_merge_cons]
simpa using Id.ext_iff.1 <| isMonadHom_pure_ret.map_listMergeM xs ys le

open List in
/-- `TimeM.ret` passes through `List.mergeSortM` into the comparator. -/
@[simp]
theorem ret_mergeSortM {T} [AddMonoid T] (xs : List α) (le : α → α → TimeM T Bool) :
⟪List.mergeSortM xs le⟫ = List.mergeSort xs (fun x y => ⟪le x y⟫) := by
fun_induction List.mergeSortM with
| case1 | case2 => simp
| case3 a b xs le iha ihb =>
simp only [ret_bind, ret_mergeM, mergeSort]
rw [iha, ihb]
simpa using Id.ext_iff.1 <| isMonadHom_pure_ret.map_listMergeSortM xs le

variable [LinearOrder α]

Expand Down
21 changes: 18 additions & 3 deletions Cslib/Algorithms/Lean/Sort/Insertion.lean
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Authors: Jeremy Avigad, Eric Wieser
module

public import Mathlib.Data.List.Sort
public import Cslib.Foundations.Control.Monad.IsMonadHom

import Cslib.Init

Expand All @@ -18,18 +19,20 @@ algorithmic analysis.

public section

open Cslib (IsMonadHom)

namespace List

variable {m} [Monad m] (r : α → α → m Bool)
variable {m n} [Monad m] [Monad n] (r : α → α → m Bool)

/-- A monadic version of `List.orderedInsert`. -/
def orderedInsertM (a : α) : List α → m (List α)
| [] => return [a]
| b :: l => do if ← r a b then return a :: b :: l else return b :: (← orderedInsertM a l)

@[simp] theorem orderedInsertM_nil (a : α) : orderedInsertM r a [] = pure [a] := by
@[simp, grind =] theorem orderedInsertM_nil (a : α) : orderedInsertM r a [] = pure [a] := by
rfl
@[simp] theorem orderedInsertM_cons (a b : α) (l : List α) :
@[simp, grind =] theorem orderedInsertM_cons (a b : α) (l : List α) :
orderedInsertM r a (b :: l) = do
if ← r a b then return a :: b :: l else return b :: (← orderedInsertM r a l) := by
rfl
Expand All @@ -45,6 +48,12 @@ theorem idRun_orderedInsertM (r : α → α → Id Bool) (a : α) (xs : List α)
Id.run (orderedInsertM r a xs) = orderedInsert (fun x y => Id.run <| r x y) a xs :=
orderedInsertM_pure _ _ _

@[grind .]
theorem _root_.Cslib.IsMonadHom.map_orderedInsertM {f : {β : Type} → m β → n β}
(hf : IsMonadHom m n f) (r : α → α → m Bool) (a : α) (xs : List α) :
f (orderedInsertM r a xs) = orderedInsertM (fun x y => f (r x y)) a xs := by
fun_induction orderedInsertM r a xs with grind

/-- A monadic version of `List.insertionSort`. -/
def insertionSortM : List α → m (List α)
| [] => return []
Expand All @@ -66,4 +75,10 @@ theorem idRun_insertionSortM (xs : List α) (r : α → α → Id Bool) :
Id.run (insertionSortM r xs) = insertionSort (fun x y => Id.run <| r x y) xs :=
insertionSortM_pure _ _

@[grind .]
theorem _root_.Cslib.IsMonadHom.map_listInsertionSortM {f : {β : Type} → m β → n β}
(hf : IsMonadHom m n f) (r : α → α → m Bool) (xs : List α) :
f (insertionSortM r xs) = insertionSortM (fun x y => f (r x y)) xs := by
fun_induction insertionSortM r xs with simp [hf.map_pure, hf.map_bind, hf.map_orderedInsertM, *]

end List
29 changes: 25 additions & 4 deletions Cslib/Algorithms/Lean/Sort/Merge.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Authors: Kim Morrison, Eric Wieser
-/
module

import all Init.Data.List.Sort.Basic
public import Cslib.Foundations.Control.Monad.IsMonadHom

import all Init.Data.List.Sort.Basic
import Cslib.Init

/-!
Expand All @@ -18,9 +19,11 @@ algorithmic analysis.

public section

open Cslib (IsMonadHom)

namespace List

variable {m} [Monad m]
variable {m n} [Monad m] [Monad n]

/-- A monadic version of `List.merge` -/
def mergeM (xs ys : List α) (le : α → α → m Bool) : m (List α) := do
Expand All @@ -33,9 +36,11 @@ def mergeM (xs ys : List α) (le : α → α → m Bool) : m (List α) := do
else
return y :: (← mergeM (x :: xs) ys le)

@[simp] theorem nil_mergeM (ys : List α) (le : α → α → m Bool) : mergeM [] ys le = pure ys := by
@[simp, grind =]
theorem nil_mergeM (ys : List α) (le : α → α → m Bool) : mergeM [] ys le = pure ys := by
simp [mergeM]
@[simp] theorem mergeM_right (xs : List α) (le : α → α → m Bool) : mergeM xs [] le = pure xs := by
@[simp, grind =]
theorem mergeM_right (xs : List α) (le : α → α → m Bool) : mergeM xs [] le = pure xs := by
induction xs with
| nil => simp
| cons x xs ih => simp [mergeM]
Expand All @@ -57,6 +62,14 @@ theorem idRun_mergeM (xs ys : List α) (le : α → α → Id Bool) :
Id.run (mergeM xs ys le) = merge xs ys (fun x y => Id.run <| le x y) :=
mergeM_pure _ _ _

@[grind .]
theorem _root_.Cslib.IsMonadHom.map_listMergeM {f : {β : Type} → m β → n β}
(hf : IsMonadHom m n f) (xs ys : List α) (le : α → α → m Bool) :
f (mergeM xs ys le) = mergeM xs ys (fun x y => f (le x y)) := by
fun_induction mergeM xs ys le with
| case1 | case2 => grind
| case3 x xs y ys ihx ihy => simp [hf.map_bind, hf.map_pure, apply_ite f, ihx, ihy]

/-- A monadic version of `List.mergeSortM` -/
def mergeSortM (xs : List α) (le : α → α → m Bool) : m (List α) :=
match xs with
Expand Down Expand Up @@ -88,4 +101,12 @@ theorem idRun_mergeSortM (xs : List α) (le : α → α → Id Bool) :
Id.run (mergeSortM xs le) = mergeSort xs (fun x y => Id.run <| le x y) :=
mergeSortM_pure _ _

@[grind .]
theorem _root_.Cslib.IsMonadHom.map_listMergeSortM {f : {β : Type} → m β → n β}
(hf : IsMonadHom m n f) (xs : List α) (le : α → α → m Bool) :
f (mergeSortM xs le) = mergeSortM xs (fun x y => f (le x y)) := by
fun_induction mergeSortM xs le with
| case1 | case2 => simp [hf.map_pure]
| case3 a b xs le ih1 ih2 => simp only [mergeSortM, hf.map_bind, ih1, ih2, hf.map_listMergeM, le]

end List
Loading
Loading