Skip to content

Commit 3bd8c91

Browse files
committed
simple widget
1 parent 16185db commit 3bd8c91

File tree

6 files changed

+217
-2
lines changed

6 files changed

+217
-2
lines changed

LeanTeX.lean

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import LeanTeX.LatexChar
55
import LeanTeX.LatexCmd
66
import LeanTeX.MkAppN
77
import LeanTeX.RuleSyntax
8+
import LeanTeX.Widget

LeanTeX/Widget.lean

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import LeanTeX.Basic
2+
import ProofWidgets.Component.HtmlDisplay
3+
4+
namespace LeanTeX
5+
6+
open Lean Elab Tactic ProofWidgets ProofWidgets.Jsx
7+
8+
/-- Displays the markdown source in `md` in a widget when the cursor is placed at `stx`. -/
9+
def displayMarkdown (md : String) (stx : Syntax) : CoreM Unit := do
10+
let html : Html := <MarkdownDisplay contents={md}/>
11+
Widget.savePanelWidgetInfo
12+
(hash HtmlDisplayPanel.javascript)
13+
(return json% { html: $(← Server.RpcEncodable.rpcEncode html) })
14+
stx
15+
16+
/--
17+
Command for displaying an expression in LaTeX form.
18+
-/
19+
elab tk:"#texify " e:term : command => do
20+
Elab.Command.runTermElabM fun _ => do
21+
let e ← Elab.Term.elabTerm e none
22+
Elab.Term.synthesizeSyntheticMVarsNoPostponing
23+
let e ← instantiateMVars e
24+
let res ← run_latexPP e {}
25+
displayMarkdown s!"$\\displaystyle {res}$" tk
26+
27+
/--
28+
Tactic for displaying the goal in LaTeX form.
29+
-/
30+
elab tk:"texify" : tactic => withMainContext do
31+
let mut lines : Array String := #[]
32+
for decl in (← getLCtx) do
33+
unless decl.isImplementationDetail do
34+
let var ← run_latexPP decl.toExpr {}
35+
let type ← run_latexPP (← instantiateMVars decl.type) {}
36+
if let some value := decl.value? then
37+
let val ← run_latexPP (← instantiateMVars value) {}
38+
lines := lines.push s!"{var} : {type} := {val}"
39+
else
40+
lines := lines.push s!"{var} : {type}"
41+
let goal ← run_latexPP (← instantiateMVars (← getMainTarget)).consumeMData {}
42+
lines := lines.push s!"\\vdash {goal}"
43+
let md := String.intercalate "\n\n" (lines
44+
|>.map (fun line => s!"$\\displaystyle {line}$")
45+
|>.toList)
46+
displayMarkdown md tk

lake-manifest.json

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
{"version": 7,
1+
{"version": "1.1.0",
22
"packagesDir": ".lake/packages",
3-
"packages": [],
3+
"packages":
4+
[{"url": "https://github.com/leanprover-community/ProofWidgets4",
5+
"type": "git",
6+
"subDir": null,
7+
"scope": "leanprover-community",
8+
"rev": "a602d13aca2913724c7d47b2d7df0353620c4ee8",
9+
"name": "proofwidgets",
10+
"manifestFile": "lake-manifest.json",
11+
"inputRev": "v0.0.53",
12+
"inherited": false,
13+
"configFile": "lakefile.lean"},
14+
{"url": "https://github.com/leanprover-community/batteries",
15+
"type": "git",
16+
"subDir": null,
17+
"scope": "leanprover-community",
18+
"rev": "092b30de8e7ee78e96b24c235d99e26f2942d77e",
19+
"name": "batteries",
20+
"manifestFile": "lake-manifest.json",
21+
"inputRev": "v4.18.0-rc1",
22+
"inherited": true,
23+
"configFile": "lakefile.toml"}],
424
"name": "LeanTeX",
525
"lakeDir": ".lake"}

lakefile.lean

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Lake
22
open Lake DSL
33

4+
require "leanprover-community" / "proofwidgets" @ git "v0.0.53"
5+
46
package LeanTeX { }
57

68
@[default_target]

test/widget_basic.lean

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import LeanTeX
2+
3+
set_option linter.unusedVariables false
4+
5+
open Lean
6+
7+
def a : Nat := 22
8+
9+
abbrev Real : Type := Nat
10+
def Real.pi : Real := 3
11+
12+
open LeanTeX in
13+
latex_pp_const_rule Real.pi := LatexData.atomString "\\pi" |>.maybeWithTooltip "Real.pi"
14+
15+
def f : Nat → Nat → Nat := λ x y => x + y
16+
17+
def g : Unit → Nat := λ _ => 22
18+
19+
section
20+
21+
variable (b : Nat) (c : Nat → Real → Real) (x : Real)
22+
23+
#texify 1 + 2 + 3
24+
#texify 1 + (2 + 3)
25+
#texify (1 + 2) + 3
26+
#texify b * (1 + 2 * 3 * 4 + 5) + 1
27+
#texify f (Real.pi + a + f b 22) 2
28+
#texify (1, (2, 3), a, Real.pi)
29+
#texify Nat × Nat × Nat × Fin 37
30+
#texify Nat × (n : Nat) × Fin n × Nat
31+
#texify (⟨1, 1, 1⟩ : (n : Nat) × Fin n × Nat)
32+
#texify ()
33+
#texify g ()
34+
#texify (λ (x : Nat) => x + 1)
35+
#texify (λ (x y : Nat) => x + y + 1)
36+
#texify (1 + 2) / 3
37+
#texify (1 + 2/3)/4
38+
#texify 1+1/(1+1/(1+1/(1+1/(1+1/(1+1)))))
39+
#texify 2^2
40+
#texify 4/2
41+
#texify 2^(4/2)
42+
#texify (1+2)^(3+4)
43+
#texify 2^2^2
44+
#texify (2^2)^2
45+
#texify (1/2)^3
46+
#texify Nat → Nat
47+
#texify Prop
48+
#texify (n : Nat) → Nat → Fin n
49+
#texify (m n : Nat) → Fin (m + n)
50+
#texify ∀ (n m : Nat), n < m → n < m + 1
51+
#texify ∀ (n : Nat), 1 = 1
52+
#texify {α : Type} → {β : Type} → {γ : Type} → [self : HAdd α β γ] → α → β → γ
53+
#texify (α β : Type) → (f : α → β) → ∀ {x y : α}, f x = f y → x = y
54+
#texify (p q r : Prop) → (p → q) → (p → q → r) → p → r
55+
#texify (p₁ p₂ : Prop) → p₁ → (p₂ → p₁)
56+
#texify (p p' : Prop) → p → (p' → p)
57+
#texify c 1 x
58+
59+
#texify ∃ (n : Nat), 1 < n
60+
#texify ∃ (n : Nat), ¬ n < 10
61+
#texify ∃ (n : Nat), ¬ n = 0
62+
63+
def foo (n : Nat) : Prop := true
64+
#texify Exists foo
65+
66+
#texify True ∧ True ∧ True
67+
#texify ¬True ∨ True
68+
69+
variable (f_1 f_2 : Nat → Nat)
70+
71+
#texify (f_1 ∘ f_2) 37
72+
73+
#texify fun (_ : Nat) => 1
74+
75+
#texify 1 = 2
76+
77+
#texify ((1 = 1) = (2 = 2))
78+
79+
def Set (α : Type u) := α → Prop
80+
instance : Membership α (Set α) := ⟨fun s x => s x⟩
81+
82+
variable (X : Type) (U : Set X) (x : X)
83+
84+
#texify x ∈ U
85+
86+
#texify x ∉ U
87+
88+
#texify Prod.mk 1
89+
90+
end
91+
92+
/-!
93+
### Tactic
94+
-/
95+
96+
example (x y : Nat) (h : x < y) : 2 * x < 2 * y := by
97+
texify
98+
sorry

test/widget_bigop.lean

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import LeanTeX
2+
import Lean.Elab.Tactic.Guard
3+
--import Mathlib.Algebra.BigOperators.Basic
4+
5+
set_option linter.unusedVariables false
6+
7+
open Lean
8+
9+
def a : Nat := 22
10+
11+
abbrev Real := Nat
12+
def Real.pi : Nat := 3
13+
14+
open Real LeanTeX in
15+
latex_pp_const_rule pi := LatexData.atomString "\\pi" |>.maybeWithTooltip "real.pi"
16+
17+
def f : Nat → Nat → Nat := λ x y => x + y
18+
19+
def g : Unit → Nat := λ _ => 22
20+
21+
def Finset (α : Type u) : Type u := α
22+
def Finset.sum {α : Type u} {β : Type v} [Inhabited β] (s : Finset α) (f : α → β) : β := default
23+
24+
variable (b : Nat) (s : Finset Nat) (t : Nat → Finset Nat) (c : Nat → Real → Real) (x : Real)
25+
26+
#texify s.sum (λ x => x + 1)
27+
#texify s.sum (λ x => 2 * x)
28+
#texify s.sum (λ x => x + 1) * a
29+
#texify a * s.sum (λ x => x + 1)
30+
#texify s.sum (λ x => x + 1) + a
31+
#texify a + s.sum (λ x => x + 1)
32+
#texify s.sum (λ x => x + 1) * s.sum (λ x => 2 * x)
33+
#texify λ (c : Nat) => c * s.sum (λ x => (t x).sum (λ y => x * y))
34+
#texify s.sum id
35+
36+
open LeanTeX in
37+
/-- An experiment: use a subscript for an argument. Represents `Fin n` as `\mathbb{N}_{<n}` -/
38+
-- TODO make work for multi-argument like Nat → Nat → Real → Real
39+
latex_pp_app_rules (kind := any) (paramKinds := params)
40+
| f, #[n] => do
41+
if params[0]!.bInfo.isExplicit && params[0]!.type.isConstOf `Nat then
42+
let f ← latexPP f
43+
let n ← withExtraSmallness 2 <| latexPP n
44+
return f.sub n
45+
else
46+
failure
47+
48+
#texify λ (f : Nat → Nat) => (s.sum f)^2

0 commit comments

Comments
 (0)