Skip to content

Commit e2e0d73

Browse files
author
David Ledvinka
committed
Rename analogue attribute and simplify core change tracking
1 parent 9005c79 commit e2e0d73

16 files changed

Lines changed: 363 additions & 378 deletions

‎.github/workflows/build.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ jobs:
5858
- name: Test upstream tracking
5959
run: |
6060
python3 -m unittest discover -s scripts/tests -p 'test_upstream.py'
61-
lake env lean scripts/tests/Upstream.lean
61+
node --test scripts/tests/test_upstream_issue.cjs
62+
lake env lean scripts/tests/Analogue.lean
6263
6364
- name: Check adapted definitions against the pinned Lean toolchain
6465
run: |

‎.github/workflows/upstream-core.yml‎

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,4 @@ jobs:
3838
script: |
3939
const fs = require('fs');
4040
const report = JSON.parse(fs.readFileSync('upstream-report.json', 'utf8'));
41-
const marker = '<!-- randomdo-upstream-core-watch -->';
42-
const digest = `<!-- upstream-fingerprint:${report.fingerprint} -->`;
43-
const issues = await github.paginate(github.rest.issues.listForRepo, {
44-
...context.repo, state: 'all', creator: 'github-actions[bot]', per_page: 100,
45-
});
46-
const issue = issues.find(i => !i.pull_request && i.body?.includes(marker));
47-
if (report.changes.length === 0) {
48-
if (issue?.state === 'open') {
49-
await github.rest.issues.update({
50-
...context.repo, issue_number: issue.number, state: 'closed',
51-
});
52-
}
53-
return;
54-
}
55-
if (issue?.state === 'open' && issue.body?.includes(digest)) return;
56-
const summary = fs.readFileSync('upstream-report.md', 'utf8');
57-
const run = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
58-
// Leave room below GitHub's issue-body limit, also for non-ASCII source text.
59-
let details = summary;
60-
if (Buffer.byteLength(details, 'utf8') > 55000) {
61-
details = Array.from(details).slice(0, 13000).join('')
62-
+ '\n\nReport truncated; see the workflow summary for the full diff.';
63-
}
64-
const body = `${marker}\n${digest}\n${details}\n\n[Workflow report](${run})`;
65-
if (issue) {
66-
await github.rest.issues.update({
67-
...context.repo, issue_number: issue.number, body, state: 'open',
68-
});
69-
} else {
70-
await github.rest.issues.create({
71-
...context.repo, title: 'Lean core changes affecting adapted definitions', body,
72-
});
73-
}
41+
await require('./scripts/upstream-issue.cjs')({ github, context }, report);

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Random-do notation
22

3-
See [Tracking adaptations of Lean core](docs/upstream-tracking.md) for upstream annotations,
3+
See [Tracking Lean core analogues](docs/upstream-tracking.md) for `analogue` annotations,
44
change reports, and the review workflow.

‎RandomDo.lean‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ public import RandomDo.Tactic.IsMarkov.Deriving
2323
public import RandomDo.Tactic.IsMarkov.Elab
2424
public import RandomDo.Tactic.IsMarkov.ForInStep
2525
public import RandomDo.Tactic.IsMarkov.Lemmas
26-
public import RandomDo.Util.Upstream
26+
public import RandomDo.Util.Analogue

‎RandomDo/Monad/MeasurableSpace.lean‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module
77

88
public import Mathlib.Control.Random
99
public import Mathlib.MeasureTheory.Measure.GiryMonad
10-
public meta import RandomDo.Util.Upstream
10+
public meta import RandomDo.Util.Analogue
1111

1212
/-!
1313
# Measurable Space Monad
@@ -38,7 +38,7 @@ section MeasurableSpaceMonad
3838
/-- A functor on types with a `MeasurableSpace` instance. The `mMap` operator `<$>ₘ` is overloaded
3939
via instances of this class. This class does not require proofs of the `MeasurableSpaceFunctor`
4040
axioms. Proofs may be provided or required via the `LawfulMeasurableSpaceFunctor` class. -/
41-
@[upstream Functor]
41+
@[analogue Functor]
4242
class MeasurableSpaceFunctor (f : (α : Type u) → [MeasurableSpace α] → Type v) :
4343
Type (max (u+1) v) where
4444
/--
@@ -66,7 +66,7 @@ The `mPure` function is overloaded via `MeasurableSpacePure` instances.
6666
6767
`MeasurableSpacePure` is typically accessed via `MeasurableSpaceMonad` instances, which extend it.
6868
-/
69-
@[upstream Pure]
69+
@[analogue Pure]
7070
class MeasurableSpacePure (f : (α : Type u) → [MeasurableSpace α] → Type v) where
7171
/-- Given `a : α` where `α` has a `MeasurableSpace` instance, `mPure a : f α` represents an
7272
action that does nothing and returns a -/
@@ -77,7 +77,7 @@ The `>>=ₘ` operator is overloaded via instances of `MeasurableSpaceBind`.
7777
7878
`MeasurableSpaceBind` is typically used via `MeasurableSpaceMonad`, which extends it.
7979
-/
80-
@[upstream Bind]
80+
@[analogue Bind]
8181
class MeasurableSpaceBind (m : (α : Type u) → [MeasurableSpace α] → Type v) where
8282
/--
8383
Sequences two computations, allowing the second to depend on the value computed by the first.
@@ -91,7 +91,7 @@ class MeasurableSpaceBind (m : (α : Type u) → [MeasurableSpace α] → Type v
9191

9292
/-- A monad on types with a `MeasurableSpace` instance. This abstraction allows the user to write
9393
code that depends on "random" side-effects -/
94-
@[upstream Monad]
94+
@[analogue Monad]
9595
class MeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Type v) :
9696
Type (max (u+1) v)
9797
extends MeasurableSpaceFunctor m, MeasurableSpacePure m, MeasurableSpaceBind m where
@@ -100,12 +100,12 @@ class MeasurableSpaceMonad (m : (α : Type u) → [MeasurableSpace α] → Type
100100
variable {m : (α : Type u) → [MeasurableSpace α] → Type v} {α β : Type u}
101101
[MeasurableSpace α] [MeasurableSpace β]
102102

103-
@[upstream _root_.bind_congr]
103+
@[analogue _root_.bind_congr]
104104
theorem MeasurableSpaceBind.bind_congr [MeasurableSpaceBind m] {x : m α} {f g : α → m β}
105105
(h : ∀ a, f a = g a) : x >>=ₘ f = x >>=ₘ g := by
106106
simp [funext h]
107107

108-
@[upstream _root_.map_congr]
108+
@[analogue _root_.map_congr]
109109
theorem MeasurableSpaceFunctor.map_congr [MeasurableSpaceFunctor m] {x : m α} {f g : α → β}
110110
(h : ∀ a, f a = g a) : (f <$>ₘ x : m β) = g <$>ₘ x := by
111111
simp [funext h]
@@ -117,7 +117,7 @@ section Lawful
117117
open MeasurableSpaceFunctor
118118

119119
/-- A `MeasurableSpaceFunctor` satisfies the measurable space functor laws. -/
120-
@[upstream LawfulFunctor]
120+
@[analogue LawfulFunctor]
121121
class LawfulMeasurableSpaceFunctor
122122
(f : (α : Type u) → [MeasurableSpace α] → Type v) [MeasurableSpaceFunctor f]
123123
[∀ α, [MeasurableSpace α] → MeasurableSpace (f α)] : Prop where
@@ -143,10 +143,10 @@ variable {f : (α : Type u) → [MeasurableSpace α] → Type v} [MeasurableSpac
143143
[∀ α, [MeasurableSpace α] → MeasurableSpace (f α)] [LawfulMeasurableSpaceFunctor f]
144144
{α β γ : Type u} [MeasurableSpace α] [MeasurableSpace β] [MeasurableSpace γ]
145145

146-
@[upstream id_map', simp]
146+
@[analogue id_map', simp]
147147
theorem id_mMap' (x : f α) : (fun a => a) <$>ₘ x = x := id_mMap x
148148

149-
@[upstream Functor.map_map, simp]
149+
@[analogue Functor.map_map, simp]
150150
theorem mMap_mMap {g₀ : α → β} {g₁ : β → γ} (x : f α)
151151
(hg₀ : Measurable g₀) (hg₁ : Measurable g₁) :
152152
g₁ <$>ₘ g₀ <$>ₘ x = (fun a => g₁ (g₀ a)) <$>ₘ x :=
@@ -155,7 +155,7 @@ theorem mMap_mMap {g₀ : α → β} {g₁ : β → γ} (x : f α)
155155
open MeasurableSpaceBind MeasurableSpacePure MeasurableSpaceFunctor
156156

157157
/-- A `MeasurableSpaceMonad` satisfies the measurable space monad laws. -/
158-
@[upstream LawfulMonad]
158+
@[analogue LawfulMonad]
159159
class LawfulMeasurableSpaceMonad
160160
(m : (α : Type u) → [MeasurableSpace α] → Type v) [MeasurableSpaceMonad m]
161161
[∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] : Prop
@@ -198,32 +198,32 @@ variable {m : (α : Type u) → [MeasurableSpace α] → Type v} [MeasurableSpac
198198
[∀ α, [MeasurableSpace α] → MeasurableSpace (m α)] [LawfulMeasurableSpaceMonad m]
199199
{α β γ : Type u} [MeasurableSpace α] [MeasurableSpace β] [MeasurableSpace γ]
200200

201-
@[upstream LawfulApplicative.map_pure, simp]
201+
@[analogue LawfulApplicative.map_pure, simp]
202202
theorem mMap_mPure {f : α → β} (hf : Measurable f) (x : α) :
203203
f <$>ₘ (mPure x : m α) = mPure (f x) := by
204204
rw [← mBind_mPure_comp hf, mPure_mBind _ (by fun_prop)]
205205

206-
@[upstream bind_pure, simp]
206+
@[analogue bind_pure, simp]
207207
theorem mBind_mPure (x : m α) : x >>=ₘ mPure = x := by
208208
change x >>=ₘ (fun a => (mPure (id a))) = x
209209
rw [mBind_mPure_comp (by fun_prop), id_mMap]
210210

211-
@[upstream map_eq_pure_bind]
211+
@[analogue map_eq_pure_bind]
212212
theorem mMap_eq_mPure_mBind {f : α → β} (hf : Measurable f) (x : m α) :
213213
f <$>ₘ x = x >>=ₘ fun a => mPure (f a) := by
214214
rw [← mBind_mPure_comp hf x]
215215

216-
@[upstream bind_pure_unit]
216+
@[analogue bind_pure_unit]
217217
theorem mBind_mPure_unit {x : m PUnit} : (x >>=ₘ fun _ => mPure ⟨⟩) = x := by rw [mBind_mPure]
218218

219-
@[upstream map_bind, simp]
219+
@[analogue map_bind, simp]
220220
theorem mMap_mBind {f : β → γ} (hf : Measurable f) (x : m α)
221221
{g : α → m β} (hg : Measurable g) :
222222
f <$>ₘ (x >>=ₘ g) = x >>=ₘ fun a => f <$>ₘ g a := by
223223
rw [← mBind_mPure_comp hf, mBind_assoc _ hg (by fun_prop)]
224224
simp (disch := fun_prop) [mBind_mPure_comp]
225225

226-
@[upstream bind_map_left, simp]
226+
@[analogue bind_map_left, simp]
227227
theorem mBind_mMap_left {f : α → β} (hf : Measurable f) (x : m α)
228228
{g : β → m γ} (hg : Measurable g) :
229229
((f <$>ₘ x) >>=ₘ fun b => g b) = (x >>=ₘ fun a => g (f a)) := by
@@ -247,7 +247,7 @@ instance instMeasurableSpace {β : Type u} [mβ : MeasurableSpace β] :
247247
/--
248248
Monadic iteration in `rdo`-blocks, using the `for x in xs` notation.
249249
-/
250-
@[upstream ForIn]
250+
@[analogue ForIn]
251251
class MeasurableSpaceForIn (ρ : Type uρ) (α : outParam (Type uα)) where
252252
/--
253253
Monadically iterates over the contents of a collection `xs`, with a local state `b` and the
@@ -259,7 +259,7 @@ class MeasurableSpaceForIn (ρ : Type uρ) (α : outParam (Type uα)) where
259259
/--
260260
Monadic iteration in `rdo`-blocks with a membership proof, using the `for h : x in xs` notation.
261261
-/
262-
@[upstream ForIn']
262+
@[analogue ForIn']
263263
class MeasurableSpaceForIn' (ρ : Type uρ) (α : outParam (Type uα))
264264
(d : outParam (Membership α ρ)) where
265265
/--
@@ -270,7 +270,7 @@ class MeasurableSpaceForIn' (ρ : Type uρ) (α : outParam (Type uα))
270270
forIn' {β : Type u} [MeasurableSpace β] (xs : ρ) (b : β)
271271
(f : (a : α) → a ∈ xs → β → m (ForInStep β)) : m β
272272

273-
@[upstream instForInOfForIn', simps]
273+
@[analogue instForInOfForIn', simps]
274274
instance (priority := 500) instMeasurableSpaceForInOfForIn'
275275
{ρ : Type uρ} {α : Type uα} {d : Membership α ρ} [MeasurableSpaceForIn' m ρ α d] :
276276
MeasurableSpaceForIn m ρ α where

‎RandomDo/Monad/Notation.lean‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public meta section
2525
namespace RDo
2626

2727
/-- Define `DoOp`s for `rdo` notation. -/
28-
@[upstream Lean.Elab.Do.DoOps.default]
28+
@[analogue Lean.Elab.Do.DoOps.default]
2929
def randOps : DoOps := { DoOps.default with
3030
mkPureApp α e := do
3131
let ⟨m, u, v, _, _⟩ := (← read).monadInfo
@@ -69,27 +69,27 @@ def randOps : DoOps := { DoOps.default with
6969
syntax (name := randKind) "rdo" doSeq : term
7070

7171
/-- Define `rdo` notation elaborator. -/
72-
@[upstream Lean.Elab.Do.elabDo, term_elab randKind]
72+
@[analogue Lean.Elab.Do.elabDo, term_elab randKind]
7373
def elabRand : Term.TermElab := fun stx et? => do
7474
let `(rdo $doSeq) := stx | throwUnsupportedSyntax
7575
elabDoWith randOps doSeq et?
7676

7777
section LoopElab
7878

7979
/-- parses `_ : _ in` for `rdo` for loops -/
80-
@[upstream Lean.Parser.Term.doForDecl]
80+
@[analogue Lean.Parser.Term.doForDecl]
8181
def rdoForDecl := leading_parser
8282
Lean.Parser.optional (atomic (Term.ident >> " : ")) >> termParser >> " in " >>
8383
withForbidden "rdo" termParser
8484

8585
/-- parser for `rdo` for loops -/
86-
@[upstream Lean.Parser.Term.doFor, doElem_parser]
86+
@[analogue Lean.Parser.Term.doFor, doElem_parser]
8787
def rdoFor := leading_parser
8888
"for " >> sepBy1 rdoForDecl ", " >> "rdo " >> doSeq
8989

9090
/-- Define expander for loops in `rdo` notation. Note this code mirrors core's implementation for
9191
`do` notation as much as possible. -/
92-
@[upstream Lean.Elab.Do.expandDoFor, macro rdoFor]
92+
@[analogue Lean.Elab.Do.expandDoFor, macro rdoFor]
9393
def expandRDoFor : Macro := fun stx => do
9494
match stx with
9595
| `(rdoFor| for $[$_ : ]? $_:ident in $_ rdo $_) =>
@@ -152,7 +152,7 @@ def expandRDoFor : Macro := fun stx => do
152152

153153
/-- Define loop elaborator for `rdo` notation. Note this code mirrors core's implementation for
154154
`do` notation as much as possible. -/
155-
@[upstream Lean.Elab.Do.elabDoFor, doElem_elab rdoFor]
155+
@[analogue Lean.Elab.Do.elabDoFor, doElem_elab rdoFor]
156156
def elabRDoFor : DoElab := fun stx dec => do
157157
let `(rdoFor| for%$tk $[$h? : ]? $x:ident in $xs rdo $body) := stx | throwUnsupportedSyntax
158158
let dec ← dec.ensureUnitAt tk
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@ public meta import Lean.Elab.Command
1010
/-!
1111
# Tracking adaptations of Lean core
1212
13-
`@[upstream decl₁ decl₂]` records the Lean core declarations from which a declaration was adapted.
13+
`@[analogue decl₁ decl₂]` records the Lean core declarations from which a declaration was adapted.
1414
It checks that the names exist and stores them as metadata, without changing the declaration or
1515
its docstring. `scripts/ExportUpstream.lean` exports this metadata for the upstream change checker.
1616
See `docs/upstream-tracking.md` for the review and baseline update workflow.
1717
-/
1818

1919
public meta section
2020

21-
namespace RandomDo.Upstream
21+
namespace RandomDo.Analogue
2222

2323
open Lean
2424

2525
/-- Mark a declaration as an adaptation of one or more declarations in Lean core. -/
26-
syntax (name := upstream) "upstream " ident+ : attr
26+
syntax (name := analogue) "analogue " ident+ : attr
2727

2828
/-- The upstream declarations associated with each adapted declaration. -/
29-
initialize upstreamAttr : ParametricAttribute (Array Name) ← registerParametricAttribute {
30-
name := `upstream
29+
initialize analogueAttr : ParametricAttribute (Array Name) ← registerParametricAttribute {
30+
name := `analogue
3131
descr := "Record the Lean core declarations from which this declaration was adapted."
3232
getParam := fun _ stx => do
3333
let names ← stx[1].getArgs.mapM resolveGlobalConstNoOverload
34+
let env ← getEnv
3435
for name in names do
35-
let env ← getEnv
3636
let some idx := env.getModuleIdxFor? name
37-
| throwErrorAt stx "Upstream declaration `{name}` must be imported from Lean core"
37+
| throwErrorAt stx "Analogue declaration `{name}` must be imported from Lean core"
3838
let mod := env.header.moduleNames[idx.toNat]!
3939
unless [`Init, `Lean, `Std].any (·.isPrefixOf mod) do
40-
throwErrorAt stx "Upstream declaration `{name}` belongs to `{mod}`, not Lean core"
40+
throwErrorAt stx "Analogue declaration `{name}` belongs to `{mod}`, not Lean core"
4141
unless names.toList.eraseDups.length == names.size do
42-
throwErrorAt stx "Duplicate upstream declarations"
42+
throwErrorAt stx "Duplicate analogue declarations"
4343
return names
4444
}
4545

46-
end RandomDo.Upstream
46+
end RandomDo.Analogue

0 commit comments

Comments
 (0)