Skip to content

Commit

Permalink
feat: add compatilibity shim system
Browse files Browse the repository at this point in the history
To work with newer Lean versions, SubVerso needs to have some
compatibility shims.
  • Loading branch information
david-christiansen committed Apr 8, 2024
1 parent b1bc281 commit 026f1ce
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lakefile.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ open Lake DSL
package «subverso» where
-- add package configuration options here

lean_lib SubVersoCompat where
srcDir := "src/compat"
roots := #[`SubVerso.Compat]

lean_lib SubVersoHighlighting where
srcDir := "src/highlighting"
roots := #[`SubVerso.Highlighting]
Expand Down
54 changes: 54 additions & 0 deletions src/compat/SubVerso/Compat.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Lean

open Lean Elab Term

namespace SubVerso.Compat

elab "%first_defined" "[" xs:ident,* "]" : term => do
let env ← getEnv
for x in xs.getElems do
if env.contains x.getId then
let expr ← elabTerm x none
return expr
throwError "None of the names exist"


def realizeNameNoOverloads
[Monad m] [MonadEnv m] [MonadLiftT CoreM m] [MonadError m]
[MonadInfoTree m] [MonadResolveName m]
(ident : Syntax) : m Name :=
%first_defined [
Lean.Elab.realizeGlobalConstNoOverloadWithInfo,
Lean.Elab.resolveGlobalConstNoOverloadWithInfo
] ident


elab "%first_succeeding" "[" es:term,* "]" : term <= ty => do
for e in es.getElems do
try
let expr ←
withReader ({· with errToSorry := false}) <|
elabTerm e (some ty)
return expr
catch _ =>
continue
throwError "No alternative succeeded"


def mkRefIdentFVar [Monad m] [MonadEnv m] (id : FVarId) : m Lean.Lsp.RefIdent := do
pure %first_succeeding [
.fvar (← getEnv).mainModule id,
.fvar id
]

def refIdentCase (ri : Lsp.RefIdent)
(onFVar : FVarId → α)
(onConst : Name → α) : α :=
%first_succeeding [
match ri with
| .fvar _ id => onFVar id
| .const _ x => onConst x,
match ri with
| .fvar id => onFVar id
| .const x => onConst x
]
3 changes: 2 additions & 1 deletion src/examples/SubVerso/Examples.lean
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SubVerso.Highlighting
import SubVerso.Compat
import SubVerso.Examples.Env
import Lean.Environment

Expand Down Expand Up @@ -83,7 +84,7 @@ where
asBool (stx : TSyntax `term) : TermElabM Bool := do
match stx with
| `($x:ident) =>
matchresolveGlobalConstNoOverloadWithInfo x with
matchCompat.realizeNameNoOverloads x with
| ``true => pure true
| ``false => pure false
| other => throwErrorAt stx "Expected Boolean literal, got {other}"
Expand Down
30 changes: 16 additions & 14 deletions src/highlighting/SubVerso/Highlighting/Code.lean
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Lean
import Lean.Widget.TaggedText

import SubVerso.Compat
import SubVerso.Highlighting.Highlighted

open Lean Elab
Expand Down Expand Up @@ -116,20 +117,21 @@ def exprKind [Monad m] [MonadLiftT IO m] [MonadMCtx m] [MonadEnv m]
match ← instantiateMVars expr with
| Expr.fvar id =>
let seen ←
if let some y := ids.find? (.fvar id) then
match y with
| .fvar x =>
let ty ← instantiateMVars (← runMeta <| Meta.inferType expr)
let tyStr := toString (← runMeta <| Meta.ppExpr ty)
if let some localDecl := lctx.find? x then
if localDecl.isAuxDecl then
let e ← runMeta <| Meta.ppExpr expr
return some (.const (toString e) tyStr none)
return some (.var x tyStr)
| .const x =>
let sig := toString (← runMeta (PrettyPrinter.ppSignature x)).1
let docs ← findDocString? (← getEnv) x
return some (.const x sig docs)
if let some y := ids.find? (← Compat.mkRefIdentFVar id) then
Compat.refIdentCase y
(onFVar := fun x => do
let ty ← instantiateMVars (← runMeta <| Meta.inferType expr)
let tyStr := toString (← runMeta <| Meta.ppExpr ty)
if let some localDecl := lctx.find? x then
if localDecl.isAuxDecl then
let e ← runMeta <| Meta.ppExpr expr
-- FIXME the mkSimple is a bit of a kludge
return some (.const (.mkSimple (toString e)) tyStr none)
return some (.var x tyStr))
(onConst := fun x => do
let sig := toString (← runMeta (PrettyPrinter.ppSignature x)).1
let docs ← findDocString? (← getEnv) x
return some (.const x sig docs))
else
let ty ← instantiateMVars (← runMeta <| Meta.inferType expr)
let tyStr := toString (← runMeta <| Meta.ppExpr ty)
Expand Down

0 comments on commit 026f1ce

Please sign in to comment.