|
| 1 | +/- |
| 2 | +Copyright (c) 2023 Patrick Massot. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Patrick Massot |
| 5 | +-/ |
| 6 | + |
| 7 | +import Std.CodeAction |
| 8 | + |
| 9 | +import Mathlib.Data.String.Defs |
| 10 | +import Mathlib.Tactic.Widget.SelectPanelUtils |
| 11 | + |
| 12 | +/-! # Calc widget |
| 13 | +
|
| 14 | +This file redefines the `calc` tactic so that it displays a widget panel allowing to create |
| 15 | +new calc steps with holes specified by selected sub-expressions in the goal. |
| 16 | +-/ |
| 17 | + |
| 18 | +section code_action |
| 19 | +open Std CodeAction |
| 20 | +open Lean Server RequestM |
| 21 | + |
| 22 | +/-- Code action to create a `calc` tactic from the current goal. -/ |
| 23 | +@[tactic_code_action calcTactic] |
| 24 | +def createCalc : TacticCodeAction := fun params _snap ctx _stack node => do |
| 25 | + let .node (.ofTacticInfo info) _ := node | return #[] |
| 26 | + if info.goalsBefore.isEmpty then return #[] |
| 27 | + let eager := { |
| 28 | + title := s!"Generate a calc block." |
| 29 | + kind? := "quickfix" |
| 30 | + } |
| 31 | + let doc ← readDoc |
| 32 | + return #[{ |
| 33 | + eager |
| 34 | + lazy? := some do |
| 35 | + let tacPos := doc.meta.text.utf8PosToLspPos info.stx.getPos?.get! |
| 36 | + let endPos := doc.meta.text.utf8PosToLspPos info.stx.getTailPos?.get! |
| 37 | + let goal := info.goalsBefore[0]! |
| 38 | + let goalFmt ← ctx.runMetaM {} <| goal.withContext do Meta.ppExpr (← goal.getType) |
| 39 | + return { eager with |
| 40 | + edit? := some <|.ofTextEdit params.textDocument.uri |
| 41 | + { range := ⟨tacPos, endPos⟩, newText := s!"calc {goalFmt} := by sorry" } |
| 42 | + } |
| 43 | + }] |
| 44 | +end code_action |
| 45 | + |
| 46 | +open ProofWidgets |
| 47 | +open Lean Meta |
| 48 | + |
| 49 | +open Lean Server in |
| 50 | + |
| 51 | +/-- Parameters for the calc widget. -/ |
| 52 | +structure CalcParams extends SelectInsertParams where |
| 53 | + /-- Is this the first calc step? -/ |
| 54 | + isFirst : Bool |
| 55 | + /-- indentation level of the calc block. -/ |
| 56 | + indent : Nat |
| 57 | + deriving SelectInsertParamsClass, RpcEncodable |
| 58 | + |
| 59 | +open Lean Meta |
| 60 | + |
| 61 | +/-- Return the link text and inserted text above and below of the calc widget. -/ |
| 62 | +def suggestSteps (pos : Array Lean.SubExpr.GoalsLocation) (goalType : Expr) (params : CalcParams) : |
| 63 | + MetaM (String × String × Option (String.Pos × String.Pos)) := do |
| 64 | + let subexprPos := getGoalLocations pos |
| 65 | + let some (rel, lhs, rhs) ← Lean.Elab.Term.getCalcRelation? goalType | |
| 66 | + throwError "invalid 'calc' step, relation expected{indentExpr goalType}" |
| 67 | + let relApp := mkApp2 rel |
| 68 | + (← mkFreshExprMVar none) |
| 69 | + (← mkFreshExprMVar none) |
| 70 | + let some relStr := (← Meta.ppExpr relApp) |> toString |>.splitOn |>.get? 1 |
| 71 | + | throwError "could not find relation symbol in {relApp}" |
| 72 | + let isSelectedLeft := subexprPos.any (fun L ↦ #[0, 1].isPrefixOf L.toArray) |
| 73 | + let isSelectedRight := subexprPos.any (fun L ↦ #[1].isPrefixOf L.toArray) |
| 74 | + |
| 75 | + let mut goalType := goalType |
| 76 | + for pos in subexprPos do |
| 77 | + goalType ← insertMetaVar goalType pos |
| 78 | + let some (_, newLhs, newRhs) ← Lean.Elab.Term.getCalcRelation? goalType | unreachable! |
| 79 | + |
| 80 | + let lhsStr := (toString <| ← Meta.ppExpr lhs).renameMetaVar |
| 81 | + let newLhsStr := (toString <| ← Meta.ppExpr newLhs).renameMetaVar |
| 82 | + let rhsStr := (toString <| ← Meta.ppExpr rhs).renameMetaVar |
| 83 | + let newRhsStr := (toString <| ← Meta.ppExpr newRhs).renameMetaVar |
| 84 | + |
| 85 | + let spc := String.replicate params.indent ' ' |
| 86 | + let insertedCode := match isSelectedLeft, isSelectedRight with |
| 87 | + | true, true => |
| 88 | + if params.isFirst then |
| 89 | + s!"{lhsStr} {relStr} {newLhsStr} := by sorry\n{spc}_ {relStr} {newRhsStr} := by sorry\n" ++ |
| 90 | + s!"{spc}_ {relStr} {rhsStr} := by sorry" |
| 91 | + else |
| 92 | + s!"_ {relStr} {newLhsStr} := by sorry\n{spc}_ {relStr} {newRhsStr} := by sorry\n" ++ |
| 93 | + s!"{spc}_ {relStr} {rhsStr} := by sorry" |
| 94 | + | false, true => |
| 95 | + if params.isFirst then |
| 96 | + s!"{lhsStr} {relStr} {newRhsStr} := by sorry\n{spc}_ {relStr} {rhsStr} := by sorry" |
| 97 | + else |
| 98 | + s!"_ {relStr} {newRhsStr} := by sorry\n{spc}_ {relStr} {rhsStr} := by sorry" |
| 99 | + | true, false => |
| 100 | + if params.isFirst then |
| 101 | + s!"{lhsStr} {relStr} {newLhsStr} := by sorry\n{spc}_ {relStr} {rhsStr} := by sorry" |
| 102 | + else |
| 103 | + s!"_ {relStr} {newLhsStr} := by sorry\n{spc}_ {relStr} {rhsStr} := by sorry" |
| 104 | + | false, false => "This should not happen" |
| 105 | + |
| 106 | + let stepInfo := match isSelectedLeft, isSelectedRight with |
| 107 | + | true, true => "Create two new steps" |
| 108 | + | true, false | false, true => "Create a new step" |
| 109 | + | false, false => "This should not happen" |
| 110 | + let pos : String.Pos := insertedCode.find (fun c => c == '?') |
| 111 | + return (stepInfo, insertedCode, some (pos, ⟨pos.byteIdx + 2⟩) ) |
| 112 | + |
| 113 | +/-- Rpc function for the calc widget. -/ |
| 114 | +@[server_rpc_method] |
| 115 | +def CalcPanel.rpc := mkSelectionPanelRPC suggestSteps |
| 116 | + "Please select subterms." |
| 117 | + "Calc 🔍" |
| 118 | + |
| 119 | +/-- The calc widget. -/ |
| 120 | +@[widget_module] |
| 121 | +def CalcPanel : Component CalcParams := |
| 122 | + mk_rpc_widget% CalcPanel.rpc |
| 123 | + |
| 124 | +namespace Lean.Elab.Tactic |
| 125 | +open Meta |
| 126 | + |
| 127 | +/-- Elaborator for the `calc` tactic mode variant with widgets. -/ |
| 128 | +elab_rules : tactic |
| 129 | +| `(tactic|calc%$calcstx $stx) => do |
| 130 | + let steps : TSyntax ``calcSteps := ⟨stx⟩ |
| 131 | + let some calcRange := (← getFileMap).rangeOfStx? calcstx | unreachable! |
| 132 | + let indent := calcRange.start.character |
| 133 | + let mut isFirst := true |
| 134 | + for step in ← Lean.Elab.Term.getCalcSteps steps do |
| 135 | + let some replaceRange := (← getFileMap).rangeOfStx? step | unreachable! |
| 136 | + let `(calcStep| $(_) := $proofTerm) := step | unreachable! |
| 137 | + let json := open scoped ProofWidgets.Json in json% {"replaceRange": $(replaceRange), |
| 138 | + "isFirst": $(isFirst), |
| 139 | + "indent": $(indent)} |
| 140 | + ProofWidgets.savePanelWidgetInfo proofTerm `CalcPanel (pure json) |
| 141 | + isFirst := false |
| 142 | + evalCalc (← `(tactic|calc%$calcstx $stx)) |
0 commit comments