Zulip Chat Archive

Stream: lean4

Topic: JSX


Patrick Massot (Jan 27 2023 at 13:50):

Are there short terms plan to move the JSX-like syntax out of doc-gen 4 into some specific library or core? I'm talking about that work that started in https://github.com/leanprover/lean4/pull/723 and ended in in doc-gen4 through https://github.com/leanprover/doc-gen4/commit/11de4f7f5593a1ceaba284601b5ed3334f79e026. @Henrik Böving @Wojciech Nawrocki

Henrik Böving (Jan 27 2023 at 13:51):

In principle nothing speaks against that but I don't have concrete plans right now.

Patrick Massot (Jan 27 2023 at 13:52):

It could be useful outside of doc-gen

Henrik Böving (Jan 27 2023 at 13:54):

I'm very much aware of that, if someone wants to do this sure.

Patrick Massot (Jan 27 2023 at 13:55):

Do you think there will be no complication?

Wojciech Nawrocki (Jan 27 2023 at 13:57):

@Patrick Massot what would you like to use it for? We have it here https://github.com/EdAyers/WidgetKit/blob/main/WidgetKit/Html.lean , but there it will be specialized for use in widgets (i.e. we may change the data type to be widget-html rather than general-html)

Patrick Massot (Jan 27 2023 at 13:58):

I'd like to use it to write Html files that have nothing to do with doc-gen or widgets.

Henrik Böving (Jan 27 2023 at 14:00):

in principle the HTML itself is just a single file in doc-gen4: https://github.com/leanprover/doc-gen4/blob/main/DocGen4/Output/ToHtmlFormat.lean

The toStringAux can definitley need some work if you want to output pretty HTML but in principle you only need this file which as you can see has no dependencies to doc-gen itself.

Wojciech Nawrocki (Jan 27 2023 at 14:01):

The file is quite small, you could probably just copy it into another project. I am not sure it is worth maintaining a single dsl-library for different use cases

Patrick Massot (Jan 27 2023 at 14:05):

Thanks, that is very useful to know

Gabriel Ebner (Jan 27 2023 at 18:04):

I think we've added quite a few new features to the doc-gen4 version after it was forked from the widget code, so you probably want to copy the doc-gen4 version.

Patrick Massot (Feb 02 2023 at 21:05):

As a nice exercise before bedtime I wanted to try to use this JSX code. But it doesn't support boolean attributed or the <!DOCTYPE html> tag. So I decided it would make a nice exercise. And of course I failed. I'm trying to edit https://github.com/leanprover/doc-gen4/blob/main/DocGen4/Output/ToHtmlFormat.lean. The dumbest addition I tried was to add

| `("<!DOCTYPE html>") => `(Html.element "!DOCTYPE" true #[("html", "")] #[])

to the macro rules at the end. I don't understand why this fails. I also fails if add syntax "<!DOCTYPE html>" : jsxElement somewhere up.

Mario Carneiro (Feb 02 2023 at 21:11):

Strictly speaking <!DOCTYPE html> isn't an element, since it does not end in /> or </!DOCTYPE>

Patrick Massot (Feb 02 2023 at 21:12):

I understand this is a super weird piece of html. But my question is rather about Lean macros.

Mario Carneiro (Feb 02 2023 at 21:12):

I'm not sure syntax with internal spaces works

Mario Carneiro (Feb 02 2023 at 21:12):

did you try syntax "<!DOCTYPE" "html>"?

Patrick Massot (Feb 02 2023 at 21:13):

Removing the space doesn't help

Patrick Massot (Feb 02 2023 at 21:14):

Here is the full thing (depending on nothing outside core Lean):

Mario Carneiro (Feb 02 2023 at 21:15):

breaking up all the tokens seems to work

Mario Carneiro (Feb 02 2023 at 21:15):

syntax "<" "!" "DOCTYPE" "html" ">" : jsxElement
...
macro_rules
  | `(<!DOCTYPE html>) => `(Html.element "!DOCTYPE" true #[("html", "")] #[])

Mario Carneiro (Feb 02 2023 at 21:16):

actually every variation seems to work for me, including syntax "<!DOCTYPE html>" : jsxElement

Mario Carneiro (Feb 02 2023 at 21:17):

ah, I guess the issue in your version is that you put string quotes inside the quotation

Patrick Massot (Feb 02 2023 at 21:18):

Oh! Indeed that fixes everything

Patrick Massot (Feb 02 2023 at 21:19):

I really don't know why I did that.

Eric Wieser (Feb 02 2023 at 21:54):

Mario Carneiro said:

Strictly speaking <!DOCTYPE html> isn't an element, since it does not end in /> or </!DOCTYPE>

In fact there's an entire separate grammar for this type of thing, such as

<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

Eric Wieser (Feb 02 2023 at 21:57):

Based on https://github.com/facebook/react/issues/1035 it seems that <!doctype html> is not supported by JSX either. Another reasonable design would be a HtmlDocument type that just injects that string as a prefix when written out to a file

Patrick Massot (Feb 02 2023 at 21:57):

I give up boolean arguments for now. My best effort is:

where the last #eval fails.

Mario Carneiro (Feb 02 2023 at 22:00):

    | `(jsxAttr| $n:jsxAttrName) =>
      let n  match n with
        | `(jsxAttrName| $n:str) => pure n
        | `(jsxAttrName| $n:ident) => pure <| quote (toString n.getId)
        | _ => Macro.throwUnsupported
      `(($as).push <| HtmlAttribute.bool $n)

Patrick Massot (Feb 02 2023 at 22:07):

Oh, this is silly again. I should go to bed. Thank you very much.


Last updated: Dec 20 2023 at 11:08 UTC