Documentation

Strata.DL.Lambda.Factory

Lambda's Factory #

This module formalizes Lambda's factory, which is a mechanism to extend the type checker (see Strata.DL.Lambda.LExprT) and partial evaluator (see Strata.DL.Lambda.LExprEval) by providing a map from operations to their types and optionally, denotations. The factory allows adding type checking and evaluation support for new operations without modifying the implementation of either or any core ASTs.

Also see Strata.DL.Lambda.IntBoolFactory for a concrete example of a factory.

@[reducible, inline]
abbrev Lambda.Signature (IDMeta Ty : Type) :

A signature is a map from variable identifiers to types.

Equations
Instances For
    @[reducible, inline]
    abbrev Lambda.LMonoTySignature {IDMeta : Type} :
    Equations
    Instances For
      @[reducible, inline]
      abbrev Lambda.LTySignature {IDMeta : Type} :
      Equations
      Instances For
        @[reducible, inline]

        The AST-facing Lambda function structure - instantiation of the base Func for Lambda expressions. It is used for functions that appear in the Strata AST (e.g. Core.Function, funcDecl), and carries only plain data — the base Func excludes the function-typed concreteEval, so it has decidable equality.

        Universally quantified type identifiers, if any, appear before this signature and can quantify over the type identifiers in it.

        Equations
        Instances For

          A Lambda factory function - the full, evaluator/factory-facing function structure. It extends the AST-facing Func with the partial-evaluator hook concreteEval. All other fields live on the base Func.

          A optional evaluation function can be provided in the concreteEval field for each factory function to allow the partial evaluator to do constant propagation when all the arguments of a function are concrete. Such a function should take two inputs: a function call expression and also -- somewhat redundantly, but perhaps more conveniently -- the list of arguments in this expression. Here's an example of a concreteEval function for Int.Add:

          (fun e args => match args with
                         | [e1, e2] =>
                           let e1i := LExpr.denoteInt e1
                           let e2i := LExpr.denoteInt e2
                           match e1i, e2i with
                           | some x, some y => (.const (toString (x + y)) mty[int])
                           | _, _ => e
                         | _ => e)
          

          Note that if there is an arity mismatch or if the arguments are not concrete/constants, this fails and it returns .none. If LFunc already has body, it must not have concreteEval, and vice versa.

          Instances For
            def Lambda.LFunc.mk {T : LExprParams} (name : T.Identifier) (typeArgs : List TyIdentifier := []) (isConstr isRecursive : Bool := false) (inputs : ListMap T.Identifier LMonoTy) (output : LMonoTy) (body : Option (LExpr T.mono) := none) (attr : Array Strata.DL.Util.FuncAttr := #[]) (concreteEval : Option (T.MetadataList (LExpr T.mono)Option (LExpr T.mono)) := none) (axioms : List (LExpr T.mono) := []) (preconditions : List (Strata.DL.Util.FuncPrecondition (LExpr T.mono) T.Metadata) := []) (measure : Option (LExpr T.mono) := none) :

            Helper constructor for LFunc to maintain backward compatibility.

            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              def Lambda.LFuncDefined.toLFunc {T : LExprParams} (f : LFuncDefined T) (concreteEval : Option (T.MetadataList (LExpr T.mono)Option (LExpr T.mono)) := none) :

              Lift an AST-facing LFuncDefined (base Func) into the full LFunc, optionally attaching concreteEval at the evaluator boundary. All other data carries over unchanged via toFunc.

              Equations
              • f.toLFunc concreteEval = { toFunc := f, concreteEval := concreteEval }
              Instances For
                @[implicit_reducible]
                Equations
                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  Equations
                  Instances For
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For
                      Equations
                      Instances For

                        The type checker and partial evaluator for Lambda is parameterizable by a user-provided Factory.

                        We don't have any "built-in" functions like +, -, etc. in (LExpr IDMeta) -- lambdas are our only tool. Factory gives us a way to add support for concrete/symbolic evaluation and type checking for FunFactory functions without actually modifying any core logic or the ASTs.

                        Instances For
                          def Lambda.Factory.mem {T : LExprParams} (f : Factory T) (name : String) :
                          Equations
                          Instances For
                            Equations
                            Instances For
                              @[implicit_reducible]
                              Equations
                              def Lambda.Factory.get {T : LExprParams} (f : Factory T) (name : String) (p : name f) :
                              Equations
                              Instances For
                                def Lambda.Factory.get? {T : LExprParams} (f : Factory T) (name : String) :
                                Equations
                                Instances For
                                  Equations
                                  Instances For
                                    def Lambda.Factory.push {T : LExprParams} (F : Factory T) (fn : LFunc T) (is_new : ¬fn.name.name F) :
                                    Equations
                                    Instances For

                                      Insert fn into the factory if no function with the same name already exists.

                                      Equations
                                      Instances For

                                        Add a function func to the factory F. Redefinitions are not allowed.

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

                                          Append a factory newF to an existing factory F, checking for redefinitions along the way.

                                          Equations
                                          Instances For

                                            Append a factory newF to an existing factory F, checking for redefinitions along the way.

                                            Equations
                                            Instances For
                                              def Lambda.getLFuncCall {T : LExprParams} {GenericTy : Type} (e : LExpr { base := T, TypeType := GenericTy }) :
                                              LExpr { base := T, TypeType := GenericTy } × List (LExpr { base := T, TypeType := GenericTy })
                                              Equations
                                              Instances For
                                                def Lambda.getLFuncCall.go {T : LExprParams} {GenericTy : Type} (e : LExpr { base := T, TypeType := GenericTy }) (acc : List (LExpr { base := T, TypeType := GenericTy })) :
                                                LExpr { base := T, TypeType := GenericTy } × List (LExpr { base := T, TypeType := GenericTy })
                                                Equations
                                                Instances For
                                                  def Lambda.getConcreteLFuncCall {T : LExprParams} {GenericTy : Type} (e : LExpr { base := T, TypeType := GenericTy }) :
                                                  LExpr { base := T, TypeType := GenericTy } × List (LExpr { base := T, TypeType := GenericTy })
                                                  Equations
                                                  Instances For
                                                    def Lambda.Factory.callOfLFunc {T : LExprParams} {GenericTy : Type} (F : Factory T) (e : LExpr { base := T, TypeType := GenericTy }) (allowPartialApp : Bool := false) :
                                                    Option (LExpr { base := T, TypeType := GenericTy } × List (LExpr { base := T, TypeType := GenericTy }) × LFunc T)

                                                    If e is a call of a factory function, get the operator (.op), a list of all the actuals, and the (LFunc IDMeta).

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

                                                      Apply type substitution S to all type annotations in an LExpr. This is only for user-defined types, not metadata-stored resolved types. If e is an LExprT whose metadata contains type information, use applySubstT.

                                                      Equations
                                                      Instances For

                                                        Best-effort type extraction from an LExpr without a typing context. Returns none when the type cannot be determined syntactically.

                                                        Equations
                                                        Instances For

                                                          Derive a type substitution from the .op type annotation alone, by unifying it against the function's generic type. On annotated terms (i.e., terms that have undergone type inference), the .op node always carries a type annotation, so this suffices.

                                                          Returns some Subst.empty when fn.typeArgs is empty (monomorphic — no-op). Returns none if the callee is not annotated or unification fails.

                                                          Equations
                                                          Instances For
                                                            def Lambda.LFunc.computeTypeSubst {T : LExprParams} (fn : LFunc T) (callee : LExpr T.mono) (args : List (LExpr T.mono)) :

                                                            Derive a type substitution by unifying the instantiated operator type against the function's generic type. Used when inlining a polymorphic function body to instantiate type variables.

                                                            Prefers the .op annotation (via opTypeSubst). Falls back to a best-effort approach using argument types when the .op is not annotated. On annotated terms (after type inference), the .op always carries a type annotation, so the fallback is never needed.

                                                            Returns some Subst.empty when fn.typeArgs is empty (monomorphic — no-op). Returns none if the type substitution cannot be derived.

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