Documentation

Cvc.Untyped.Grammar

SyGuS grammars, sort-erased #

A grammar restricts the shape of a term the solver is allowed to produce — the candidates a synthFun may return, or the shape of an interpolant or abduct.

Two kinds of variable go into one: the parameters (BVar) the synthesized term may mention, and the non-terminals (NT) the rules are written over. An NT converts to a term, since a non-terminal appears inside its own rules, but has no coercion to BVar, so it cannot be passed as a parameter by mistake.

A grammar freezes once it is used. addRule and friends answer a new Grammar, so the type reads as though grammars were persistent values, but the object underneath is shared: once a grammar has been passed to synthFun, cvc5 rejects every further rule with Grammar cannot be modified after passing it as an argument to synthFun. Build a grammar completely before using it.

Non-terminal symbols #

structure Cvc.Untyped.NT [Ω] :

A grammar's non-terminal symbol: a bound variable, kept apart from parameters by its type.

Instances For
    @[instance_reducible]
    instance Cvc.Untyped.instBEqNT {inst✝ : Ω} :
    Equations
    def Cvc.Untyped.instBEqNT.beq {inst✝ : Ω} :
    NT → NT → Bool
    Equations
    Instances For
      def Cvc.Untyped.instOrdNT.ord {inst✝ : Ω} :
      NT → NT → Ordering
      Equations
      Instances For
        @[instance_reducible]
        instance Cvc.Untyped.instOrdNT {inst✝ : Ω} :
        Equations
        Equations
        Instances For
          @[reducible, inline]

          An array of non-terminal symbols.

          Equations
          Instances For
            def Cvc.Untyped.NT.mk [Ω] (srt : Srt) (name : String) :

            A non-terminal symbol of the given sort.

            Equations
            Instances For

              Turns itself into a bound variable, explicitly only: a non-terminal is not a parameter.

              Equations
              Instances For

                Turns itself into a term, which is how a non-terminal appears inside its own rules.

                Equations
                Instances For

                  Grammars #

                  A grammar is built through a solver, but any solver of the scope accepts it.

                  def Cvc.Untyped.Solver.mkGrammar [Ω] (s : Solver) (boundVars : BVars) (start : NT) (others : NTs := #[]) :

                  A grammar over the given parameters, starting at start.

                  boundVars are the parameters of the synthFun this grammar will constrain, and must be the very same variables that function is declared with. others are the further non-terminals the rules may mention: every one has to be declared here. A non-terminal unreachable from start is left out of the grammar.

                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    def Cvc.Untyped.Grammar.mk [Ω] (boundVars : BVars) (start : NT) (others : NTs := #[]) :

                    Solver.mkGrammar, without a solver at hand.

                    Equations
                    Instances For

                      The sort of the functions the grammar generates.

                      The parameters' sorts arrowed onto the start symbol's, flattened as a curried Lean arrow is, or the start symbol's sort alone when there are no parameters.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        def Cvc.Untyped.Grammar.addRule [Ω] (g : Grammar) (nt : NT) (rule : Term) :

                        Adds a production rule.

                        nt must be one of the non-terminals this grammar was declared with, and rule must have its sort. Neither is visible in a signature, so both are checked when the rule is added.

                        Equations
                        Instances For
                          def Cvc.Untyped.Grammar.addRules [Ω] (g : Grammar) (nt : NT) (rules : Terms) :

                          Adds a production rule.

                          nt must be one of the non-terminals this grammar was declared with, and rule must have its sort. Neither is visible in a signature, so both are checked when the rule is added.

                          Equations
                          Instances For

                            Allows any constant of the non-terminal's sort.

                            Equations
                            Instances For

                              Allows any of the grammar's parameters that has the non-terminal's sort.

                              Equations
                              Instances For

                                smt.grammar! #

                                A grammar written as one block, BNF-style, with smt! terms as its alternatives:

                                smt.grammar! s (x y : Int) where
                                  start : Int  := x | y | if cnd then start else start
                                  cnd   : Bool := start ≤ start
                                

                                A sort is written as a Lean type (Int) or a Srt value (int).

                                Extending a grammar afterwards #

                                smt.grammar! answers the grammar alone, its parameters and non-terminals being made out of sight, so nothing can be added to it later. Two forms with the same body answer them too:

                                In these two forms a non-terminal may be declared without alternatives, nt : sort alone, for its rules to be added afterwards — rules computed from data, say, which a block cannot spell:

                                smt.letGrammar! g ← s (x : Int) where
                                  start : Int := x | start + num
                                  num   : Int
                                let mut g := g
                                for k in [3, 7] do g ← g.addRule num (← Term.mkInt k)
                                

                                smt.grammar! refuses such a line, since the grammar it answers cannot be extended.

                                A grammar can be extended until it is used for synthesis, after which cvc5 rejects new rules. A non-terminal unreachable from the start symbol is left out of the grammar until a rule reaches it.

                                All three forms are scoped in each layer, as smt! is: open Cvc.Untyped selects this one.

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

                                  One alternative of a smt.grammar! rule.

                                  Equations
                                  Instances For

                                    ?const or ?var: any constant, or any parameter, of the non-terminal's sort.

                                    Equations
                                    Instances For

                                      An alternative that is a term.

                                      Equations
                                      Instances For

                                        One line of a smt.grammar!: a non-terminal, its sort, and its alternatives.

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

                                            nt : sort := alt | alt | …, or nt : sort alone, its rules to be added afterwards.

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

                                              What follows smt.grammar!: the solver, the parameters, and the rules.

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

                                                  s (x : Int) … where followed by the rules, one per line.

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

                                                    A sort-erased grammar; smt.grammar! under open Cvc.Untyped.

                                                    Equations
                                                    Instances For

                                                      A typed grammar; smt.grammar! under open Cvc.Typed.

                                                      Equations
                                                      Instances For

                                                        A sort-erased grammar and its parts; smt.mkGrammar! under open Cvc.Untyped.

                                                        Equations
                                                        Instances For

                                                          A typed grammar and its parts; smt.mkGrammar! under open Cvc.Typed.

                                                          Equations
                                                          Instances For

                                                            A sort-erased SyGuS grammar, written as one BNF-style block whose alternatives are smt! terms.

                                                            smt.grammar! s (x y : Int) where
                                                              start : Int := x | y | start + start
                                                            
                                                            Equations
                                                            • One or more equations did not get rendered due to their size.
                                                            Instances For

                                                              smt.grammar!, also answering its parameters and non-terminals, to extend it afterwards.

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

                                                                smt.grammar! as a do element, binding the grammar, its parameters and its non-terminals.

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

                                                                  Whether an identifier of this name occurs anywhere in a piece of syntax.

                                                                  Equations
                                                                  Instances For

                                                                    A right-nested tuple of the given terms, (a, b, c); a single term stands for itself.

                                                                    Equations
                                                                    Instances For

                                                                      The right-nested product of the given types, α × β × γ; a single type stands for itself.

                                                                      Equations
                                                                      Instances For

                                                                        The alternatives of one smt.grammar! rule, none when it only declares its non-terminal.

                                                                        Equations
                                                                        Instances For
                                                                          def Cvc.Ext.expandGrammar (layer : Layer) (body : Lean.Syntax) (withParts : Bool := false) :

                                                                          Expands the body of a smt.grammar! or smt.mkGrammar! for the given layer.

                                                                          With withParts, the result carries what the grammar was built from as well as the grammar, so that it can be extended afterwards: grammar × NT₁ × NT₂ × … without parameters, and (grammar × PARAM₁ × …) × NT₁ × NT₂ × … with some, the grouping keeping the two apart.

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

                                                                            The pattern smt.letGrammar! g ← … binds: g, the parameters and the non-terminals.

                                                                            They are grouped as smt.mkGrammar! answers them, and named as the block declares them.

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