cubical-transport-hott-lean4/CubicalTransport/Algebra/MacroAlias.lean
Maximus Gorog 7ccebb606d
Some checks are pending
Lean Action CI / build (push) Waiting to run
ALGEBRA Phases A+B+C+D' + cubical_search tactic + doc state-of-play
Lands the metacoding stack from ALGEBRA_PLAN.md per the user's
discipline directive (no shortcuts, end-to-end correct).

CubicalTransport/Algebra/Meta.lean (Phase A — meta-mirror types):
- MetaCType: 11 constructors mirroring the cubical CType arms.
- MetaClassifier: lattice of "where in the codebase" predicates
  with .always / .never / .meet / .join / .atDecl / .inFile /
  .underAttribute / .dependencyOf / .inNamespace.
- MetaArtifact: source / declAt / refTo / empty.
- MetaPosition: (declName, filePath, range?) addressing.
- DecidableEq for MetaCType, MetaClassifier (manual mutual decEq
  for the recursive lattice arms).

CubicalTransport/Algebra/Edit.lean (Phase B — Edit + Context):
- Edit α: result + List EditOp.  Monad / Functor instances.
- Context α: focal artifact + position + siblings.  Functor +
  comonad operations (extract / extend).
- contextualEdit: the comonad-to-monad distributive law.
- MetaClassifier.atPosition: syntactic dispatch on classifier shape;
  meet/join lattice laws stated as theorems.

CubicalTransport/Algebra/Restructure.lean (Phase B — universal macro):
- restructure: the comp-shaped 5-field operation, returns Edit Unit.
- Frozen aliases: transport_artifact, relocate_invariant,
  rename_throughout, define_question_shape, compose_proof_fragments,
  materialize.
- Headless interpreter: SourceBuffer + EditOp.apply + Edit.runHeadless.
- Soundness scaffold: brokenRefs / selfConsistent / Edit.guarded.

CubicalTransport/Algebra/MacroAlias.lean (Phase C):
- @[macroAlias] attribute + AliasEntry registry (EnvExtension).
- Lookup helpers + diagnostic printer.

CubicalTransport/Algebra/Methodology.lean (Phase D'):
- @[methodology Identifier] attribute + MethodologyEntry registry.
- cubical_search tactic: walks the methodology library by classifier
  dispatch, applies via exact/apply.  deriveByTransport stub awaits
  @[metaPath] (REL2.6+).
- Diagnostic printer for the registry.

CubicalTransport/Algebra/Test.lean: compile-time end-to-end tests:
- Construct meta-mirror values; check DecidableEq.
- Build Edit values via restructure; verify selfConsistent on a
  broken-ref batch (correctly flagged).
- Register an alias via @[macroAlias].
- Register two methodologies via @[methodology] and verify
  cubical_search dispatches to them on representative goals.

Runtime smoke tests: 4 new Algebra smokes verifying restructure
emits the right ops, the broken-ref guard fires, and the
classifier lattice computes correctly.  93/93 tests pass.

Documentation:
- docs/QUESTIONS.md §4: Levels 1, 2, 3-light marked LANDED with
  commit refs; full Level 3 graph-walking marked pending.
- docs/ALGEBRA_PLAN.md §6: phase table updated with status column;
  Phases A/B/C/D' marked landed; Phases B.2 (LSP) + D (widget) +
  REL2.6 methodology-transport explicitly marked pending.
- docs/EULERIAN.md §9, §10: "the map" and "autodiscovery" rows
  updated from "planned REL2.5" to "landed 2026-05-01" with
  module-level cross-references.
- docs/KERNEL_BOUNDARY.md §3.7: cubical_simp (light) and
  cubical_search marked landed; full graph-walking cubical_simp
  marked dependent on @[metaPath].

Pending items deliberately out of scope this session:
- LSP widget (D) — needs running Lean LSP server.
- B.2 LSP integration — needs CodeActionContext.
- @[metaPath] declarations + full deriveByTransport — REL2.6+.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 00:59:06 -06:00

96 lines
4 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
CubicalTransport.Algebra.MacroAlias — `@[macroAlias]` attribute
===============================================================
Phase C of `docs/ALGEBRA_PLAN.md`. Provides the user-extensible
registry of frozen `restructure` invocations.
When a developer notices that a `restructure` invocation pattern
recurs ("oh, this is just a relocate-with-namespace-fixup"), they
attach `@[macroAlias]` to a `def` that wraps the pattern. The
attribute records the alias in a global registry (`AliasRegistry`)
keyed by name. The widget can then suggest "name this pattern as
X" when an instantiation matches an existing alias's signature.
Per ALGEBRA_PLAN §2.3:
> The codebase ships with `restructure` itself (~150 lines). Each
> `@[macroAlias] def …` is a 13-line shorthand.
This module provides the attribute and the registry; concrete
alias declarations (`transport_artifact`, `relocate_invariant`,
…) live in `Algebra/Restructure.lean`.
-/
import Lean
import CubicalTransport.Algebra.Restructure
namespace CubicalTransport.Algebra
open Lean
-- ── The alias registry ──────────────────────────────────────────────────────
/-- An entry in the alias registry: a `Name` paired with its
documentary description (extracted from the `def`'s docstring
if present). -/
structure AliasEntry where
name : Name
description : String := ""
deriving Repr, Inhabited
/-- Global registry of `@[macroAlias]`-tagged declarations, indexed
by name. Implemented as a Lean `EnvExtension` so that adds in
one module are visible from any importing module.
Per ALGEBRA_PLAN §10 OQ #2 the registry is on-the-fly; persistence
via `lake exe algebra-cache` is deferred. -/
initialize aliasRegistryExt :
SimplePersistentEnvExtension AliasEntry (Array AliasEntry) ←
registerSimplePersistentEnvExtension {
name := `Algebra.aliasRegistry
addEntryFn := fun arr e => arr.push e
addImportedFn := fun arrs => arrs.foldl (init := #[]) Array.append
}
/-- Look up every registered alias. Order is unspecified (no
guarantees beyond "all registered entries are in the array"). -/
def getAliases : CoreM (Array AliasEntry) := do
let env ← getEnv
return aliasRegistryExt.getState env
/-- Register an alias entry. Inserts into the env extension. -/
def registerAlias (entry : AliasEntry) : CoreM Unit := do
modifyEnv (aliasRegistryExt.addEntry · entry)
-- ── The `@[macroAlias]` attribute ───────────────────────────────────────────
/-- Lean attribute syntax registered as `@[macroAlias]`. Attached to
a `def` to register it as a frozen `restructure` invocation
accessible via `Algebra.getAliases`. -/
initialize macroAliasAttr : Unit ←
registerBuiltinAttribute {
name := `macroAlias
descr := "Register this declaration as a `restructure` alias \
(Phase C of ALGEBRA_PLAN.md). The decl's name and \
docstring become an `AliasEntry` in the global \
`aliasRegistryExt`."
add := fun declName _stx _kind => do
let env ← getEnv
let docstring? := (← findDocString? env declName).getD ""
registerAlias { name := declName, description := docstring? }
}
-- ── Diagnostics ─────────────────────────────────────────────────────────────
/-- Print the current alias registry to `IO.println`. Used by
`lake exe algebra-list-aliases` and the widget's "show registered
aliases" button. -/
def printAliases : CoreM Unit := do
let aliases ← getAliases
IO.println s!"── Algebra macro-alias registry ({aliases.size}) ──"
for entry in aliases do
if entry.description.isEmpty then
IO.println s!" {entry.name}"
else
IO.println s!" {entry.name} — {entry.description}"
end CubicalTransport.Algebra