Documentation

FormaleSystemeInLean.Lecture3

Formalisation of Lecture 3: Covers the definition of DFAs and total DFAs, the corresponding languages and how to obtain a total DFA from a non-total DFA. Grammars are (for now) not part of this formalisation, so in lecture 3 and 4 all things related to grammars and the Chomsky Hierarchy is left out.

Slides for lecture 3 can be found at https://iccl.inf.tu-dresden.de/w/images/e/e2/FS2025-Vorlesung-03-print.pdf

structure DFA (Q : Type u) (Sigma : Type v) [Fintype Q] [Fintype Sigma] :
Type (max u v)

We define a DFA as a generic structure with two type parameters Q and Sigma. Definitions in the lecture and textbooks usually define Q and Sigma as finite sets. We already know from lecture 1 that it is sufficient to define Sigma as an arbitrary type. The states however need to be finite, otherwise it would not be a finite automaton. Therefore we require an instance of Fintype for Q. For further information on Fintype please refer to the corresponding file.

δ is a function mapping a state of type Q and a symbol of type Sigma to something of type Option Q. This is how we "encode" that δ is a partial function: if δ(q,a) = None, it is undefined.

Instances For
    structure TotalDFA (Q : Type u) (Sigma : Type v) [Fintype Q] [Fintype Sigma] :
    Type (max u v)

    A total DFA is defined like a DFA except for the transition function δ.

    • δ : QSigmaQ
    • q0 : Q
    • F : List Q
    Instances For
      def DFA.δ_word {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (dfa : DFA Q Sigma) (q : Q) :
      Word SigmaOption Q

      The transition function δ of a DFA can be extended to words.

      • w = ε: δ_word(q,ε) = q
      • w = av (a ∈ Σ, v ∈ Σ*): δ_word(q,av) = δ_word(δ(q,a),v) In case any of the transitions is undefined, δ_word is undefined as well (which is expressed with bind).
      Equations
      Instances For
        def TotalDFA.δ_word {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (t_dfa : TotalDFA Q Sigma) (q : Q) :
        Word SigmaQ

        The transition function δ of a total DFA can be extended to words.

        • w = ε: δ_word(q,ε) = q
        • w = av (a ∈ Σ, v ∈ Σ*): δ_word(q,av) = δ_word(δ(q,a),v) Contrary to the DFA this function is defined for any pair (q,w) since δ is total.
        Equations
        Instances For
          def TotalDFA.Language {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (t_dfa : TotalDFA Q Sigma) :

          Given a total DFA, a word w is contained in its language iff the DFA ends up in a final state when reading w.

          Equations
          Instances For
            def DFA.Language {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (dfa : DFA Q Sigma) :

            Given a DFA, a word w is contained in its language iff δ_word(q0,w) is defined and the DFA ends up in a final state when reading w.

            Equations
            Instances For
              def DFA.to_totalDFA {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) :
              TotalDFA (Option Q) Sigma

              A function turning a DFA into a total DFA.

              At first glance this might look very different from the lecture where we explicitly introduced a "garbage state". We change the type of Q to (Option Q) and just keep all the transitions. Since Q is an arbitrary type this results in a total DFA with states of type (Option Q). "none" is now a valid state that is reached whenever the original DFA had an undefined transition. It behaves exactly like the the garbage state from which there is no way out.

              Equations
              Instances For
                def TotalDFA.to_DFA {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : TotalDFA Q Sigma) :
                DFA Q Sigma

                A total DFA can (obviously) be converted to a DFA. The only thing that changes is the type of δ. Since δ is defined for every possible input (q,a) we can just wrap the value of δ with some to obtain an Option Q.

                Equations
                Instances For
                  theorem final_iff_final {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) (q : Q) :

                  Given a DFA M and a state q, q is a final state of M iff q is a final state of M.to_totalDFA.

                  theorem δ_eq_δ {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) (q : Q) (a : Sigma) :
                  M.δ q a = M.to_totalDFA.δ (some q) a

                  The transition functions of a DFA M and M.to_totalDFA map to the same values.

                  theorem δ_none_eq_none {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) (a : Sigma) :

                  For all alphabet symbols a, δ(none,a) = none.

                  theorem garbage_state {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) (w : Word Sigma) :

                  For all words w, δ_word(w,none) = none.

                  theorem final_ne_none {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] {q : Option Q} (M : DFA Q Sigma) :

                  the state "none" (corresponding to a total DFA's garbage state) cannot be an accepting final state.

                  theorem to_totalDFA_δ_word_eq {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) (q : Q) (w : Word Sigma) :

                  The transition function for words of a DFA M and M.to_totalDFA have the same values for every input (q,w).

                  theorem totalDFA_eq_DFA {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : DFA Q Sigma) :

                  The language of a DFA M does not change when converting it to a total DFA.

                  theorem to_DFA_δ_eq {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : TotalDFA Q Sigma) (q : Q) (a : Sigma) :
                  some (M.δ q a) = M.to_DFA.δ q a

                  The transition functions of a total DFA M and M.to_DFA map to the same values.

                  theorem to_DFA_δ_word_eq {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : TotalDFA Q Sigma) (q : Q) (w : Word Sigma) :
                  some (M.δ_word q w) = M.to_DFA.δ_word q w

                  The transition function for words of a total DFA M and M.to_DFA have the same values for every input (q,w).

                  theorem DFA_eq_totalDFA {Q : Type u} {Sigma : Type v} [Fintype Q] [Fintype Sigma] (M : TotalDFA Q Sigma) :

                  Every total DFA can be seen as a DFA.