Documentation

PfsProgs25.Unit11.Fib

Fibonacci numbers: memoization using the state monad #

The naive way of computing Fibonacci numbers is very inefficient because it recomputes the same values many times. We can use the state monad to store the values of the Fibonacci numbers we have already computed. This is a simple example of memoization.

We will also take a more complicated example: Catalan numbers. The Catalan numbers are a sequence of natural numbers that occur in various counting problems, often involving recursively defined objects. They satisfy the recurrence relation:

@[reducible, inline]
abbrev FibState :
Equations
Instances For
    @[reducible, inline]
    abbrev FibM (α : Type) :
    Equations
    Instances For
      def fib (n : Nat) :
      Equations
      • One or more equations did not get rendered due to their size.
      • fib 0 = pure 1
      • fib 1 = pure 1
      Instances For

        Catalan numbers #

        @[reducible, inline]
        Equations
        Instances For
          @[reducible, inline]
          abbrev Catalan.CatM (α : Type) :
          Equations
          Instances For
            @[irreducible]
            Equations
            • One or more equations did not get rendered due to their size.
            • Catalan.cat 0 = pure 1
            Instances For
              @[reducible, inline]
              Equations
              Instances For
                @[reducible, inline]
                Equations
                Instances For
                  @[irreducible]
                  Equations
                  Instances For
                    Instances For
                      Equations
                      Equations
                      • One or more equations did not get rendered due to their size.
                      @[reducible, inline]
                      abbrev CatalanOps.CatM (α : Type) :
                      Equations
                      Instances For
                        Equations
                        Instances For
                          Equations
                          Instances For
                            @[irreducible]
                            Equations
                            Instances For
                              structure MyStateM (σ α : Type) :
                              • run : σα × σ
                              Instances For
                                def MyStateM.pure {σ α : Type} (a : α) :
                                MyStateM σ α
                                Equations
                                Instances For
                                  def MyStateM.bind {σ α β : Type} (x : MyStateM σ α) (f : αMyStateM σ β) :
                                  MyStateM σ β
                                  Equations
                                  • x.bind f = { run := fun (s : σ) => match x.run s with | (a, s') => (f a).run s' }
                                  Instances For
                                    instance MyStateM.instMonad {σ : Type} :
                                    Equations
                                    • MyStateM.instMonad = Monad.mk
                                    def MyStateM.get {σ : Type} :
                                    MyStateM σ σ
                                    Equations
                                    • MyStateM.get = { run := fun (s : σ) => (s, s) }
                                    Instances For
                                      def MyStateM.set {σ : Type} (s : σ) :
                                      Equations
                                      Instances For
                                        def MyStateM.modify {σ : Type} (f : σσ) :
                                        Equations
                                        Instances For
                                          def MyStateM.run' {σ α : Type} (x : MyStateM σ α) (s : σ) :
                                          α
                                          Equations
                                          • x.run' s = match x.run s with | (a, snd) => a
                                          Instances For