Documentation

Lean.Compiler.LCNF.Specialize

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

      Dependency collector for the code specialization function. #

      During code specialization, we select which arguments are going to be used during the specialization. Then, we have to collect their dependencies. For example, suppose are trying to specialize the following IO.println and List.forM applications in the following example:

          def f xs a.1 :=
            let _x.2 := @instMonadEIO IO.Error
            let _x.5 := instToStringString
            let _x.9 := instToStringNat
            let _x.6 := "hello"
            let _x.61 := @IO.println String _x.5 _x.6 a.1 -- (*)
            cases _x.61
            | EStateM.Result.ok a.6 a.7 =>
              fun _f.72 _y.69 _y.70 :=
                let _x.71 := @IO.println Nat _x.9 _y.69 _y.70 -- (*)
                _x.71
              let _x.65 := @List.forM (fun α => PUnitEStateM.Result IO.Error PUnit α) _x.2 Nat xs _f.72 a.7 -- (*)
              ...
            ...
      

      For IO.println the SpecArgInfo is [N, I, O, O], i.e., only the first two arguments are considered for code specialization. The first one is computationally neutral, and the second one is an instance. For List.forM, we have [N, I, N, O, H]. In this case, the fifth argument (tagged as H) is a function. Note that the actual List.forM application has 6 arguments, the extra argument comes from the IO monad.

      For the first IO.println application, the collector collects _x.5. For the List.forM, it collects _x.2, _x.9, and _f.72. The collected values are used to construct a key to identify the specialization. Arguments that were not considered are replaced with lcErased. The key is used to make sure we don't keep generating the same specialization over and over again. This is not an optimization, it is essential to prevent the code specializer from looping while specializing recursive functions. The keys for these two applications are the terms.

      @IO.println Nat instToStringNat lcErased lcErased
      

      and

      @List.forM
        (fun α => PUnitEStateM.Result IO.Error PUnit α)
        (@instMonadEIO IO.Error) Nat lcErased
        (fun _y.69 _y.70 =>
         let _x.71 := @IO.println Nat instToStringNat _y.69 _y.70;
        _x.71)
      

      The keys never contain free variables or loose bound variables.