lean2js

Lean2Js.Parse

source

A reader for the text the printers write #

A reader for the text the printers write, and the proof that it gives the printers' input back.

The grammar it accepts is not JavaScript: it is the subset this compiler emits, which is fully parenthesised and has no optional whitespace. Reading JS is not the goal — reading back what was written is.

Every reader takes a List Char and returns what it read together with the characters left over, so the roundtrip lemmas compose: each one is stated about a rendering followed by an arbitrary remainder.

Decimal numerals #

def

Lean2Js.Parse.digitVal

source
(c : Char) :
Equations
Instances For
    Used by
      def

      Lean2Js.Parse.parseDigits

      source
      (acc : Nat) :
      Equations
      Instances For
        Used by
          def

          Lean2Js.Parse.parseNat

          source
          Equations
          Instances For
            Used by
              def

              Lean2Js.Parse.parseInt

              source
              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                Used by
                  def

                  Lean2Js.Parse.notDigitFirst

                  source
                  (cs : List Char) :

                  What a numeral can be followed by. A reader of digits is greedy, so the character after the numeral has to be one it will not take.

                  Equations
                  Instances For
                    Used by
                      theorem

                      Lean2Js.Parse.digitVal_digitChar

                      source
                      {d : Nat} (h : d < 10) :
                      Used by
                        theorem

                        Lean2Js.Parse.isDigit_digitChar

                        source
                        {d : Nat} (h : d < 10) :
                        Used by
                          theorem

                          Lean2Js.Parse.natDigits_all_digit

                          source
                          Used by
                            theorem

                            Lean2Js.Parse.natDigits_ne_nil

                            source
                            (n : Nat) :
                            Used by
                              theorem

                              Lean2Js.Parse.parseDigits_append

                              source
                              (ds : List Char) (hds : ds.all Char.isDigit = true) (rest : List Char) (hrest : notDigitFirst rest) (acc : Nat) :
                              parseDigits acc (ds ++ rest) = (List.foldl (fun (a : Nat) (c : Char) => a * 10 + digitVal c) acc ds, rest)

                              Reading digits off a rendering of n, with anything that is not a digit behind it, gives n back and leaves the remainder untouched.

                              Used by
                                theorem

                                Lean2Js.Parse.foldl_natDigits

                                source
                                (n : Nat) :
                                List.foldl (fun (a : Nat) (c : Char) => a * 10 + digitVal c) 0 (natDigits n) = n
                                Used by
                                  theorem

                                  Lean2Js.Parse.toList_renderNat

                                  source
                                  Used by
                                    theorem

                                    Lean2Js.Parse.parseNat_append

                                    source
                                    (n : Nat) (rest : List Char) (hrest : notDigitFirst rest) :
                                    parseNat ((renderNat n).toList ++ rest) = some (n, rest)
                                    Used by
                                      theorem

                                      Lean2Js.Parse.head_natDigits_ne_neg

                                      source
                                      {n : Nat} {d : Char} {ds : List Char} (hn : natDigits n = d :: ds) :
                                      d ≠ '-'
                                      Used by
                                        theorem

                                        Lean2Js.Parse.parseInt_ofNat

                                        source
                                        (n : Nat) (rest : List Char) (hrest : notDigitFirst rest) :
                                        parseInt ((renderNat n).toList ++ rest) = some (↑n, rest)
                                        Used by
                                          theorem

                                          Lean2Js.Parse.parseInt_negSucc

                                          source
                                          (n : Nat) (rest : List Char) (hrest : notDigitFirst rest) :
                                          parseInt ('-' :: (renderNat (n + 1)).toList ++ rest) = some (Int.negSucc n, rest)
                                          Used by
                                            theorem

                                            Lean2Js.Parse.parseInt_append

                                            source
                                            (i : Int) (rest : List Char) (hrest : notDigitFirst rest) :
                                            parseInt ((renderInt i).toList ++ rest) = some (i, rest)
                                            Used by

                                              String literals #

                                              def

                                              Lean2Js.Parse.hexVal

                                              source
                                              (c : Char) :
                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                Used by
                                                  def

                                                  Lean2Js.Parse.unescape

                                                  source

                                                  Reads the body of a string literal up to its closing quote, returning the characters it stood for and what follows the quote.

                                                  Equations
                                                  Instances For
                                                    Used by
                                                      def

                                                      Lean2Js.Parse.parseStr

                                                      source
                                                      Equations
                                                      Instances For
                                                        Used by
                                                          theorem

                                                          Lean2Js.Parse.hex4_toList

                                                          source
                                                          (n : Nat) :
                                                          (hex4 n).toList = [hexDigit (n / 4096 % 16), hexDigit (n / 256 % 16), hexDigit (n / 16 % 16), hexDigit (n % 16)]
                                                          Used by
                                                            theorem

                                                            Lean2Js.Parse.hexVal_hexDigit

                                                            source
                                                            {d : Nat} (h : d < 16) :
                                                            Used by
                                                              theorem

                                                              Lean2Js.Parse.unescape_hex4

                                                              source
                                                              (n : Nat) (h : n < 65536) (tail : List Char) :
                                                              unescape ('\\' :: 'u' :: ((hex4 n).toList ++ tail)) = Option.map (fun (p : List Char × List Char) => (Char.ofNat n :: p.fst, p.snd)) (unescape tail)
                                                              Used by
                                                                theorem

                                                                Lean2Js.Parse.unescape_plain

                                                                source
                                                                {c : Char} (h1 : c ≠ '\"') (h2 : c ≠ '\\') (tail : List Char) :
                                                                unescape (c :: tail) = Option.map (fun (p : List Char × List Char) => (c :: p.fst, p.snd)) (unescape tail)
                                                                Used by
                                                                  theorem

                                                                  Lean2Js.Parse.unescape_escapeChar

                                                                  source
                                                                  (c : Char) (tail : List Char) :
                                                                  unescape ((escapeChar c).toList ++ tail) = Option.map (fun (p : List Char × List Char) => (c :: p.fst, p.snd)) (unescape tail)
                                                                  Used by
                                                                    theorem

                                                                    Lean2Js.Parse.unescape_escapeChars

                                                                    source
                                                                    (cs tail : List Char) :
                                                                    unescape ((escapeChars cs).toList ++ '\"' :: tail) = some (cs, tail)
                                                                    Used by
                                                                      theorem

                                                                      Lean2Js.Parse.parseStr_append

                                                                      source
                                                                      (s : String) (tail : List Char) :
                                                                      parseStr (("\"" ++ escapeString s ++ "\"").toList ++ tail) = some (s, tail)
                                                                      Used by
                                                                        theorem

                                                                        Lean2Js.Parse.parseStr_cons

                                                                        source
                                                                        (s : String) (tail : List Char) :
                                                                        parseStr ('\"' :: ((escapeString s).toList ++ '\"' :: tail)) = some (s, tail)

                                                                        The shape a string literal takes inside a larger rendering, where the quotes are part of a neighbouring chunk of literal text rather than of the escaped body.

                                                                        Used by
                                                                          theorem

                                                                          Lean2Js.Parse.parseStr_ctors

                                                                          source
                                                                          (tail : List Char) :
                                                                          parseStr ('\"' :: 'c' :: 't' :: 'o' :: 'r' :: 's' :: '\"' :: tail) = some ("ctors", tail)

                                                                          The head of a descriptor for a declared type, read back while parseStr stays folded: the key that follows it is a string the author chose, and reading that one needs parseStr_cons.

                                                                          Used by
                                                                            theorem

                                                                            Lean2Js.Parse.parseStr_mu

                                                                            source
                                                                            (tail : List Char) :
                                                                            parseStr ('\"' :: 'm' :: 'u' :: '\"' :: tail) = some ("mu", tail)
                                                                            Used by
                                                                              theorem

                                                                              Lean2Js.Parse.parseStr_ref

                                                                              source
                                                                              (tail : List Char) :
                                                                              parseStr ('\"' :: 'r' :: 'e' :: 'f' :: '\"' :: tail) = some ("ref", tail)
                                                                              Used by

                                                                                Identifiers #

                                                                                def

                                                                                Lean2Js.Parse.parseIdentChars

                                                                                source
                                                                                Equations
                                                                                Instances For
                                                                                  Used by
                                                                                    def

                                                                                    Lean2Js.Parse.parseIdent

                                                                                    source
                                                                                    Equations
                                                                                    Instances For
                                                                                      Used by
                                                                                        def

                                                                                        Lean2Js.Parse.notIdentFirst

                                                                                        source
                                                                                        (cs : List Char) :

                                                                                        What a name can be followed by. A reader of names is greedy, so the character after the name has to be one it will not take.

                                                                                        Equations
                                                                                        Instances For
                                                                                          Used by
                                                                                            theorem

                                                                                            Lean2Js.Parse.parseIdentChars_append

                                                                                            source
                                                                                            (ds : List Char) (hds : ds.all isIdentPart = true) (rest : List Char) (hrest : notIdentFirst rest) :
                                                                                            parseIdentChars (ds ++ rest) = (ds, rest)
                                                                                            Used by
                                                                                              theorem

                                                                                              Lean2Js.Parse.parseIdent_append

                                                                                              source
                                                                                              (name : String) (hne : name.toList ≠ []) (hall : name.toList.all isIdentPart = true) (rest : List Char) (hrest : notIdentFirst rest) :
                                                                                              parseIdent (name.toList ++ rest) = some (name, rest)
                                                                                              Used by

                                                                                                Literal chunks #

                                                                                                def

                                                                                                Lean2Js.Parse.expect

                                                                                                source
                                                                                                Equations
                                                                                                Instances For
                                                                                                  Used by
                                                                                                    theorem

                                                                                                    Lean2Js.Parse.expect_append

                                                                                                    source
                                                                                                    (lit rest : List Char) :
                                                                                                    expect lit (lit ++ rest) = some rest
                                                                                                    Used by

                                                                                                      Type descriptors #

                                                                                                      The budget is the descriptor's own size, which the caller reads off the text it is about to hand over. It bounds the descent, not the guarantee: the roundtrip below holds for every budget that is large enough, and the top-level reader takes the length of the text it was given.

                                                                                                      def

                                                                                                      Lean2Js.Parse.descSize

                                                                                                      source
                                                                                                      @[irreducible]
                                                                                                      Equations
                                                                                                      Instances For
                                                                                                        Used by
                                                                                                          def

                                                                                                          Lean2Js.Parse.altsSize

                                                                                                          source
                                                                                                          @[irreducible]
                                                                                                          Equations
                                                                                                          Instances For
                                                                                                            Used by
                                                                                                              def

                                                                                                              Lean2Js.Parse.fieldsSize

                                                                                                              source
                                                                                                              @[irreducible]
                                                                                                              Equations
                                                                                                              Instances For
                                                                                                                Used by
                                                                                                                  def

                                                                                                                  Lean2Js.Parse.parseDesc

                                                                                                                  source
                                                                                                                  Equations
                                                                                                                  Instances For
                                                                                                                    Used by
                                                                                                                      def

                                                                                                                      Lean2Js.Parse.parseAlts

                                                                                                                      source
                                                                                                                      Equations
                                                                                                                      Instances For
                                                                                                                        Used by
                                                                                                                          def

                                                                                                                          Lean2Js.Parse.parseFields

                                                                                                                          source
                                                                                                                          Equations
                                                                                                                          Instances For
                                                                                                                            Used by
                                                                                                                              theorem

                                                                                                                              Lean2Js.Parse.descSize_pos

                                                                                                                              source
                                                                                                                              (d : Js.TyDesc) :
                                                                                                                              Used by
                                                                                                                                theorem

                                                                                                                                Lean2Js.Parse.altsSize_pos

                                                                                                                                source
                                                                                                                                (alts : List (String × List (String × Js.TyDesc))) :
                                                                                                                                0 < altsSize alts
                                                                                                                                Used by
                                                                                                                                  theorem

                                                                                                                                  Lean2Js.Parse.fieldsSize_pos

                                                                                                                                  source
                                                                                                                                  Used by
                                                                                                                                    theorem

                                                                                                                                    Lean2Js.Parse.parseDesc_append

                                                                                                                                    source
                                                                                                                                    (d : Js.TyDesc) (f : Nat) (hf : descSize d ≤ f) (rest : List Char) :
                                                                                                                                    parseDesc f (d.render.toList ++ rest) = some (d, rest)
                                                                                                                                    Used by
                                                                                                                                      theorem

                                                                                                                                      Lean2Js.Parse.parseAlts_append

                                                                                                                                      source
                                                                                                                                      (alts : List (String × List (String × Js.TyDesc))) (f : Nat) (hf : altsSize alts ≤ f) (rest : List Char) (hrest : ∃ (r : List Char), rest = ']' :: r) :
                                                                                                                                      parseAlts f ((Js.TyDesc.renderAlts alts).toList ++ rest) = some (alts, rest)
                                                                                                                                      Used by
                                                                                                                                        theorem

                                                                                                                                        Lean2Js.Parse.parseFields_append

                                                                                                                                        source
                                                                                                                                        (fs : List (String × Js.TyDesc)) (f : Nat) (hf : fieldsSize fs ≤ f) (rest : List Char) (hrest : ∃ (r : List Char), rest = ']' :: r) :
                                                                                                                                        Used by

                                                                                                                                          Fold specs #

                                                                                                                                          A spec carries the shape TyAlts does with a kind where a descriptor sits, so the reader is the descriptor's with the descent taken out: a kind is one of three words and nothing below it nests. The budget is still the spec's own size, because the recursion is on the text rather than on the tree.

                                                                                                                                          The spec is printed as an object rather than as nested arrays, because __fold indexes it by the constructor's name, so the end of an empty one is told from the start of an entry the way parseObjFields tells them apart — by the space that opens }.

                                                                                                                                          def

                                                                                                                                          Lean2Js.Parse.foldFieldsSize

                                                                                                                                          source
                                                                                                                                          Equations
                                                                                                                                          Instances For
                                                                                                                                            Used by
                                                                                                                                              def

                                                                                                                                              Lean2Js.Parse.specSize

                                                                                                                                              source
                                                                                                                                              Equations
                                                                                                                                              Instances For
                                                                                                                                                Used by
                                                                                                                                                  theorem

                                                                                                                                                  Lean2Js.Parse.foldFieldsSize_pos

                                                                                                                                                  source
                                                                                                                                                  Used by
                                                                                                                                                    theorem

                                                                                                                                                    Lean2Js.Parse.specSize_pos

                                                                                                                                                    source
                                                                                                                                                    (spec : Js.FoldSpec) :
                                                                                                                                                    0 < specSize spec
                                                                                                                                                    Used by
                                                                                                                                                      def

                                                                                                                                                      Lean2Js.Parse.parseFoldKind

                                                                                                                                                      source

                                                                                                                                                      The three words FoldKind.render writes. A fourth is a text this printer does not write, so the reader has nothing to give back for it.

                                                                                                                                                      Equations
                                                                                                                                                      • One or more equations did not get rendered due to their size.
                                                                                                                                                      Instances For
                                                                                                                                                        Used by
                                                                                                                                                          theorem

                                                                                                                                                          Lean2Js.Parse.parseFoldKind_cons

                                                                                                                                                          source
                                                                                                                                                          (k : Js.FoldKind) (tail : List Char) :
                                                                                                                                                          parseFoldKind ('\"' :: (k.render.toList ++ '\"' :: tail)) = some (k, tail)
                                                                                                                                                          Used by
                                                                                                                                                            def

                                                                                                                                                            Lean2Js.Parse.parseFoldFields

                                                                                                                                                            source
                                                                                                                                                            Equations
                                                                                                                                                            Instances For
                                                                                                                                                              Used by
                                                                                                                                                                def

                                                                                                                                                                Lean2Js.Parse.parseFoldSpec

                                                                                                                                                                source
                                                                                                                                                                Equations
                                                                                                                                                                Instances For
                                                                                                                                                                  Used by
                                                                                                                                                                    theorem

                                                                                                                                                                    Lean2Js.Parse.parseFoldFields_append

                                                                                                                                                                    source
                                                                                                                                                                    (fs : Js.FoldFields) (f : Nat) (hf : foldFieldsSize fs ≤ f) (rest : List Char) (hrest : ∃ (r : List Char), rest = ']' :: r) :
                                                                                                                                                                    Used by
                                                                                                                                                                      theorem

                                                                                                                                                                      Lean2Js.Parse.parseFoldSpec_append

                                                                                                                                                                      source
                                                                                                                                                                      (spec : Js.FoldSpec) (f : Nat) (hf : specSize spec ≤ f) (rest : List Char) (hrest : ∃ (r : List Char), rest = ' ' :: r) :
                                                                                                                                                                      parseFoldSpec f ((Js.renderFoldSpec spec).toList ++ rest) = some (spec, rest)
                                                                                                                                                                      Used by

                                                                                                                                                                        Expressions #

                                                                                                                                                                        The grammar is fully parenthesised and has no optional whitespace, so one character of lookahead picks the form everywhere except after (, where arrowHead reads ahead for (names) => (. Nothing else the printer writes has that shape: member closes with )., and binary and cond put a space where the arrow wants ).

                                                                                                                                                                        def

                                                                                                                                                                        Lean2Js.Parse.isOpChar

                                                                                                                                                                        source
                                                                                                                                                                        (c : Char) :
                                                                                                                                                                        Equations
                                                                                                                                                                        Instances For
                                                                                                                                                                          Used by
                                                                                                                                                                            def

                                                                                                                                                                            Lean2Js.Parse.parseOpChars

                                                                                                                                                                            source
                                                                                                                                                                            Equations
                                                                                                                                                                            Instances For
                                                                                                                                                                              Used by
                                                                                                                                                                                def

                                                                                                                                                                                Lean2Js.Parse.parseOp

                                                                                                                                                                                source
                                                                                                                                                                                Equations
                                                                                                                                                                                Instances For
                                                                                                                                                                                  Used by
                                                                                                                                                                                    def

                                                                                                                                                                                    Lean2Js.Parse.parseNumeral

                                                                                                                                                                                    source
                                                                                                                                                                                    Equations
                                                                                                                                                                                    • One or more equations did not get rendered due to their size.
                                                                                                                                                                                    Instances For
                                                                                                                                                                                      Used by
                                                                                                                                                                                        def

                                                                                                                                                                                        Lean2Js.Parse.negateNumeral

                                                                                                                                                                                        source

                                                                                                                                                                                        Undoes the sign a numeral reader takes with it, for the one place where (- opens a unary minus rather than a negative literal.

                                                                                                                                                                                        Equations
                                                                                                                                                                                        Instances For
                                                                                                                                                                                          Used by
                                                                                                                                                                                            def

                                                                                                                                                                                            Lean2Js.Parse.parseIdentList

                                                                                                                                                                                            source
                                                                                                                                                                                            Equations
                                                                                                                                                                                            Instances For
                                                                                                                                                                                              Used by
                                                                                                                                                                                                def

                                                                                                                                                                                                Lean2Js.Parse.parseArrowHead

                                                                                                                                                                                                source
                                                                                                                                                                                                Equations
                                                                                                                                                                                                • One or more equations did not get rendered due to their size.
                                                                                                                                                                                                Instances For
                                                                                                                                                                                                  Used by
                                                                                                                                                                                                    def

                                                                                                                                                                                                    Lean2Js.Parse.parseExpr

                                                                                                                                                                                                    source
                                                                                                                                                                                                    Equations
                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                      Used by
                                                                                                                                                                                                        def

                                                                                                                                                                                                        Lean2Js.Parse.parseNamed

                                                                                                                                                                                                        source

                                                                                                                                                                                                        What follows a name: a keyword, a helper the printer has a constructor for, a call, or the name itself.

                                                                                                                                                                                                        Equations
                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                          Used by
                                                                                                                                                                                                            def

                                                                                                                                                                                                            Lean2Js.Parse.parseLambdaCall

                                                                                                                                                                                                            source
                                                                                                                                                                                                            Equations
                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                              Used by
                                                                                                                                                                                                                def

                                                                                                                                                                                                                Lean2Js.Parse.parseParen

                                                                                                                                                                                                                source
                                                                                                                                                                                                                Equations
                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                  Used by
                                                                                                                                                                                                                    def

                                                                                                                                                                                                                    Lean2Js.Parse.parseAfterHead

                                                                                                                                                                                                                    source

                                                                                                                                                                                                                    The three forms that open with a subexpression: (e).f, (e op e) and (e ? e : e). A ) with no field behind it is the unary minus a numeral reader has already swallowed the sign of.

                                                                                                                                                                                                                    Equations
                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                      Used by
                                                                                                                                                                                                                        def

                                                                                                                                                                                                                        Lean2Js.Parse.parseExprList

                                                                                                                                                                                                                        source
                                                                                                                                                                                                                        Equations
                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                          Used by
                                                                                                                                                                                                                            def

                                                                                                                                                                                                                            Lean2Js.Parse.parseObjFields

                                                                                                                                                                                                                            source
                                                                                                                                                                                                                            Equations
                                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                                              Used by
                                                                                                                                                                                                                                def

                                                                                                                                                                                                                                Lean2Js.Parse.parseEntries

                                                                                                                                                                                                                                source
                                                                                                                                                                                                                                Equations
                                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                                  Used by

                                                                                                                                                                                                                                    Statements, functions and the module #

                                                                                                                                                                                                                                    The runtime helpers are matched as the one literal block the printer writes them as. The block is what Helper's own printer makes of the definitions the helper proofs are stated over, so it is an AST on the way out; the reader does not take it back apart, and accepting it verbatim is what "reads back exactly what this printer writes" means for that part.

                                                                                                                                                                                                                                    def

                                                                                                                                                                                                                                    Lean2Js.Parse.takeUntil

                                                                                                                                                                                                                                    source
                                                                                                                                                                                                                                    Equations
                                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                                      Used by
                                                                                                                                                                                                                                        def

                                                                                                                                                                                                                                        Lean2Js.Parse.parseStmt

                                                                                                                                                                                                                                        source
                                                                                                                                                                                                                                        (f : Nat) (cs : List Char) :
                                                                                                                                                                                                                                        Equations
                                                                                                                                                                                                                                        • One or more equations did not get rendered due to their size.
                                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                                          Used by
                                                                                                                                                                                                                                            def

                                                                                                                                                                                                                                            Lean2Js.Parse.parseStmts

                                                                                                                                                                                                                                            source
                                                                                                                                                                                                                                            Equations
                                                                                                                                                                                                                                            Instances For
                                                                                                                                                                                                                                              Used by
                                                                                                                                                                                                                                                def

                                                                                                                                                                                                                                                Lean2Js.Parse.parseFunc

                                                                                                                                                                                                                                                source
                                                                                                                                                                                                                                                Equations
                                                                                                                                                                                                                                                Instances For
                                                                                                                                                                                                                                                  Used by
                                                                                                                                                                                                                                                    def

                                                                                                                                                                                                                                                    Lean2Js.Parse.parseFuncs

                                                                                                                                                                                                                                                    source
                                                                                                                                                                                                                                                    Equations
                                                                                                                                                                                                                                                    Instances For
                                                                                                                                                                                                                                                      Used by
                                                                                                                                                                                                                                                        def

                                                                                                                                                                                                                                                        Lean2Js.Parse.parseModule

                                                                                                                                                                                                                                                        source
                                                                                                                                                                                                                                                        Equations
                                                                                                                                                                                                                                                        • One or more equations did not get rendered due to their size.
                                                                                                                                                                                                                                                        Instances For
                                                                                                                                                                                                                                                          Used by

                                                                                                                                                                                                                                                            The grammar the reader accepts #

                                                                                                                                                                                                                                                            One case per printer branch, plus the places where two branches open with the same character. The theorem below covers these too; they are here because a reader that drifts from the printer stops building long before a proof about it does.