Zulip Chat Archive

Stream: mathlib4

Topic: superscript parser


Mario Carneiro (Jun 10 2023 at 15:57):

import Mathlib.Util.Superscript

macro a:term b:superscript(term) : term => `($a ^ $(⟨b.raw[0]⟩))

example : 2² = 4 := rfl
example : 2¹⁶ = 65536 := rfl
example : (fun n => 2ⁿ⁺¹) 15 = 2¹⁶ := rfl

Mario Carneiro (Jun 10 2023 at 16:05):

not yet PR'd, but it basically works as pictured

Mario Carneiro (Jun 10 2023 at 16:06):

you can even ctrl click the to go to the binding site of the fun n =>

Henrik Böving (Jun 10 2023 at 16:08):

But not all unicode characters can be superscripted can they? Or was that subscripted. I can clearly remember that not even all numbers could be super or subscripted I just don't remember which it was.

Mario Carneiro (Jun 10 2023 at 16:09):

that's right, so you can only spell some words or use some symbols in the subscript

Mario Carneiro (Jun 10 2023 at 16:10):

there is also a subscript parser

Mario Carneiro (Jun 10 2023 at 16:11):

my plan is to make the parser available in mathlib but not actually use it globally, and let people use it in local notations when it makes sense

Henrik Böving (Jun 10 2023 at 16:12):

Hm, I'm not sure if it is good to further splitting up notation style with this because we can but if the majority likes it go for it :thumbs_up:

Mario Carneiro (Jun 10 2023 at 16:12):

what do you mean by that?

Henrik Böving (Jun 10 2023 at 16:15):

Well with this we are once again handing out yet another option to write quite literally the same thing (something that is already possible in base Lean already, for example inductive types can be denote with and without where if you wish) just because it is possible, I don't really see the advantage of having it done like this. I guess it looks nice and more familiar?

I just generally believe that with Lean already being as rich in syntax as it is we should try to stick to one way to write the same thing.

Mario Carneiro (Jun 10 2023 at 16:17):

Local notations which do this kind of thing already exist in mathlib, for example defining ℝⁿ⁺¹ to mean Fin (n + 1) -> ℝ for some local variable named n

Mario Carneiro (Jun 10 2023 at 16:19):

I'm not sure what you mean by "literally the same thing", it's a macro (which you can think of as a notation in this case) which lets you write the same thing you could before but with different syntax

Mario Carneiro (Jun 10 2023 at 16:19):

that's kind of the point

Mario Carneiro (Jun 10 2023 at 16:35):

(Just to be clear, I have no intention of using this for the power function on Nat. That was just a demo.)

Mario Carneiro (Jun 10 2023 at 16:49):

Henrik Böving said:

But not all unicode characters can be superscripted can they? Or was that subscripted. I can clearly remember that not even all numbers could be super or subscripted I just don't remember which it was.

Actually both are incomplete. Here are the defined mappings:

def Mapping.superscript := mkMapping
  "⁰¹²³⁴⁵⁶⁷⁸⁹ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖ𐞥ʳˢᵗᵘᵛʷˣʸᶻᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾꟴᴿᵀᵁⱽᵂᵝᵞᵟᵋᶿᶥᶹᵠᵡ⁺⁻⁼⁽⁾"
  "0123456789abcdefghijklmnopqrstuvwxyzABDEGHIJKLMNOPQRTUVWβγδεθιυφχ+-=()"

def Mapping.subscript := mkMapping
  "₀₁₂₃₄₅₆₇₈₉ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘꞯʀꜱᴛᴜᴠᴡʏᴢᵦᵧᵨᵩᵪ₊₋₌₍₎"
  "0123456789aehijklmnoprstuvxABCDEFGHIJKLMNOPQRSTUVWYZβγρφχ+-=()"

Mario Carneiro (Jun 10 2023 at 16:50):

you don't even have *, which makes it kind of hard to write formulas

James Gallicchio (Jun 10 2023 at 17:00):

those feel so arbitrary... unicode weirdness??

Mario Carneiro (Jun 10 2023 at 17:01):

it's because different parts of the alphabet were introduced at different times for different reasons

Mario Carneiro (Jun 10 2023 at 17:01):

if you look at the codepoint values they are all over the place

Eric Wieser (Jun 10 2023 at 17:10):

This mkMapping API seems nice in principle ("oh, the columns will align"), but the reality is:

image.png
image.png

Eric Wieser (Jun 10 2023 at 17:11):

It looks like (white screenshot) Microsoft rather dropped the ball on Consolas subscript characters (maybe that's fixed in newer versions than the one I have?)

Henrik Böving (Jun 10 2023 at 17:13):

I once spend two days figuring out the font with the most monospaced unicode characters to use with Lean, my decision at the time was julia mono:
image.png
i would say it is holding up pretty well

Eric Wieser (Jun 10 2023 at 17:13):

Though Zulip's "Source Code Pro" default has even fewer characters

Eric Wieser (Jun 10 2023 at 17:14):

@Henrik Böving, very weird that the only failing subscript is a capital F

Damiano Testa (Jun 10 2023 at 17:17):

Also, what's up with the q?

Henrik Böving (Jun 10 2023 at 17:17):

It seems like it doesnt exist in julia mono, if you look at marios thing above for me the q looks very weird as well

Henrik Böving (Jun 10 2023 at 17:17):

Maybe the q is new?

Eric Wieser (Jun 10 2023 at 17:19):

The q is not a true superscript character

Eric Wieser (Jun 10 2023 at 17:19):

It's https://unicode-explorer.com/c/107A5

Henrik Böving (Jun 10 2023 at 17:23):

"modifier letter small q" who comes up with this

Eric Wieser (Jun 10 2023 at 17:39):

I mean it's IPA

Eric Wieser (Jun 10 2023 at 17:40):

Unicode is supposed to be semantic not visual

Mario Carneiro (Jun 10 2023 at 18:12):

I think that character only theoretically exists. I put it in for completeness but I have not seen any font render it as not a box

Mario Carneiro (Jun 10 2023 at 18:13):

at least the julia font renders the capital Q, mine doesn't

Mario Carneiro (Jun 10 2023 at 20:48):

!4#4957

Mario Carneiro (Jun 10 2023 at 21:31):

Hm, now here's an interesting edge case:

#check 2² ⁻⁻ᶠᵒᵒ + 2
-- 2 ^ 2 + 2 : Nat

Mario Carneiro (Jun 10 2023 at 21:36):

oh my...

#check 2² ⁻⁻ /-
- 2 --/ + 2

-- 2 ^ (2 - 2) + 2 : Nat

you can comment out comments :mind_blown:


Last updated: Dec 20 2023 at 11:08 UTC