feat: Curated Notes

This commit is contained in:
jstoobysmith 2025-01-23 06:31:11 +00:00
parent 8d7df853a7
commit ba51484b1f
6 changed files with 233 additions and 11 deletions

View file

@ -0,0 +1,68 @@
/-
Copyright (c) 2024 Joseph Tooby-Smith. All rights reserved.
Released under Apache 2.0 license.
Authors: Joseph Tooby-Smith
-/
import Batteries.Lean.HashSet
import Mathlib.Lean.Expr.Basic
import Mathlib.Lean.CoreM
import ImportGraph.RequiredModules
/-!
## Underlying structure for remarks
-/
namespace HepLean
open Lean System Meta
/-- The information from a `remark ...` command. To be used in a note file-/
structure RemarkInfo where
/-- The content of the remark. -/
content : String
/-- The file name where the remark came from. -/
fileName : Name
/-- The line from where the remark came from. -/
line : Nat
/-- The name of the remark. -/
name : Name
/-- The namespace of the remark. -/
nameSpace : Name
/-- Environment extension to store `remark ...`. -/
initialize remarkExtension : SimplePersistentEnvExtension RemarkInfo (Array RemarkInfo) ←
registerSimplePersistentEnvExtension {
name := `remarkExtension
addEntryFn := fun arr remarkInfoT => arr.push remarkInfoT
addImportedFn := fun es => es.foldl (· ++ ·) #[]
}
/-- A remark is a string used for important information. -/
syntax (name := remark_syntax) "remark " ident ":=" str : command
/-- Elaborator for the `note ...` command -/
@[command_elab remark_syntax]
def elabRemark : Lean.Elab.Command.CommandElab := fun stx =>
match stx with
| `(remark $n := $s) => do
let str : String := s.getString
let pos := stx.getPos?
match pos with
| some pos => do
let env ← getEnv
let fileMap ← getFileMap
let filePos := fileMap.toPosition pos
let line := filePos.line
let modName := env.mainModule
let nameSpace := (← getCurrNamespace)
let noteInfo : RemarkInfo := {
content := str,
fileName := modName,
line := line,
name := n.getId,
nameSpace := nameSpace}
modifyEnv fun env => remarkExtension.addEntry env noteInfo
| none => throwError "Invalid syntax for `note` command"
| _ => throwError "Invalid syntax for `note` command"
end HepLean

View file

@ -0,0 +1,40 @@
/-
Copyright (c) 2024 Joseph Tooby-Smith. All rights reserved.
Released under Apache 2.0 license.
Authors: Joseph Tooby-Smith
-/
import HepLean.Meta.Remark.Basic
/-!
## Underlying structure for remarks
-/
namespace HepLean
open Lean System Meta
def Name.allRemarkInfo : MetaM (List RemarkInfo) := do
let env ← getEnv
let allRemarks := (remarkExtension.getState env)
pure allRemarks.toList
def RemarkInfo.toFullName (r : RemarkInfo) : Name :=
if r.nameSpace != .anonymous then
(r.nameSpace.toString ++ "." ++ r.name.toString).toName
else
r.name
def RemarkInfo.IsRemark (n : Name) : MetaM Bool := do
let allRemarks ← Name.allRemarkInfo
let r := allRemarks.find? (fun r => r.toFullName = n)
match r with
| some _ => pure true
| none => pure false
def RemarkInfo.getRemarkInfo (n : Name) : MetaM RemarkInfo := do
let allRemarks ← Name.allRemarkInfo
let r := allRemarks.find? (fun r => r.toFullName = n)
match r with
| some r => pure r
| none => throwError s!"No remark named {n}"
end HepLean