Documentation

Grip.Grade

Grip.Grade: the parser grade algebra #

structure Grade (tracking error and consumption Modality), the named grades, Grade.mul/Grade.choice, and consumptionWitness with its composition lemmas.

No mathlib. Monoid/Lattice instances for Grade belong in grip-props.

structure Grade :

A parser's static grade: whether it may/must produce errors and whether it may/must consume input.

  • errors : Modality

    Whether the parser may/must error: never, possibly, or always.

  • consumes : Modality

    Whether the parser may/must consume input: never, possibly, or always.

Instances For
    def instDecidableEqGrade.decEq (x✝ x✝¹ : Grade) :
    Decidable (x✝ = x✝¹)
    Equations
    Instances For
      @[instance_reducible]
      Equations
      Equations
      • One or more equations did not get rendered due to their size.
      Instances For
        @[reducible, inline]

        A parser that always consumes and possibly errors.

        Equations
        Instances For
          @[reducible, inline]

          A parser that possibly consumes and never errors.

          Equations
          Instances For
            @[reducible, inline]

            A parser that possibly consumes and possibly errors.

            Equations
            Instances For
              @[reducible, inline]

              A parser that never consumes and never errors.

              Equations
              Instances For
                @[reducible, inline]

                A parser that never consumes and possibly errors.

                Equations
                Instances For
                  @[reducible, inline]

                  A parser that never consumes and always errors.

                  Equations
                  Instances For
                    @[reducible, inline]

                    The impossible grade: always consumes, never errors. No parser can inhabit this grade on arbitrary input (it must accept empty input).

                    Equations
                    Instances For
                      @[reducible]
                      def Grade.mul (a b : Grade) :

                      Sequential composition: the grade of p >>= q is mul g1 g2, with errors and consumption each being the join of the two grades.

                      Reducible for the same reason as Modality.sup: goals mentioning a grade product have to stay type-correct at instances transparency.

                      Equations
                      Instances For

                        The identity grade for sequential composition: ⟨never, never⟩ = pure.

                        Equations
                        Instances For
                          @[instance_reducible]
                          Equations
                          @[instance_reducible]
                          Equations
                          def Grade.choice (a b : Grade) :

                          Ordered-choice grade: errors is the meet (both must agree to fail), consumption is a.errors.ite b.consumes a.consumes (if a always errors, use b's consumption; if a never errors, use a's).

                          Equations
                          Instances For
                            theorem Grade.grade_mul_errors (a b : Grade) :

                            The error grade of a product is the join of the error grades.

                            The consumption grade of a product is the join of the consumption grades.

                            @[reducible, inline]
                            abbrev consumptionWitness (n m : Nat) :

                            Relates the start offset n and end offset m of a successful parse, according to a Modality grade:

                            • always requires n < m (at least one byte consumed)
                            • possibly allows n ≤ m (consumed some or none)
                            • never requires n = m (no input consumed)
                            Equations
                            Instances For
                              theorem consumptionWitness.le {n m : Nat} {a : Modality} (w : consumptionWitness n m a) :
                              n m

                              Every consumption witness is monotone in the absolute input offset.

                              @[simp]

                              A reflexive witness holds for any grade a ≤ possibly (i.e. a ≠ always), since no input has been consumed.

                              theorem consumptionWitness.trans {gc gc' : Modality} {n1 n2 n3 : Nat} (w1 : consumptionWitness n2 n1 gc) (w2 : consumptionWitness n3 n2 gc') :
                              consumptionWitness n3 n1 (max gc gc')

                              Transitivity: chain two witnesses through a common midpoint. If gc witnesses (n2, n1) and gc' witnesses (n3, n2), then max gc gc' witnesses (n3, n1).

                              theorem consumptionWitness.ite_left {ge' gc gc' : Modality} {n m : Nat} (c : ge' possibly) (w : consumptionWitness n m gc') :
                              consumptionWitness n m (ge'.ite gc gc')

                              If the error grade ge' is at most possibly (i.e. ge' ≠ always), a consumption witness for the second branch gc' lifts to a witness for the ite-computed consumption ge'.ite gc gc'.

                              theorem consumptionWitness.ite_right {ge' gc gc' : Modality} {n m : Nat} (c : possibly ge') (w : consumptionWitness n m gc) :
                              consumptionWitness n m (ge'.ite gc gc')

                              If the error grade ge' is at least possibly (i.e. ge' ≠ never), a consumption witness for the first branch gc lifts to a witness for the ite-computed consumption ge'.ite gc gc'.