Documentation

STLCLean.inference

inductive CurryType :
Instances For
    Equations
    • One or more equations did not get rendered due to their size.
    Instances For
      def instDecidableEqCurryType.decEq (x✝ x✝¹ : CurryType) :
      Decidable (x✝ = x✝¹)
      Equations
      Instances For
        inductive Term :
        Instances For
          Equations
          Instances For
            structure TypeCtx :
            Instances For
              Equations
              • One or more equations did not get rendered due to their size.
              Instances For
                Equations
                Instances For
                  def union (ctx1 ctx2 : TypeCtx) :
                  Equations
                  Instances For
                    def add (c : Char) (ty : CurryType) (ctx : TypeCtx) :
                    Equations
                    Instances For
                      Equations
                      Instances For
                        structure PrincipalPair :
                        Instances For
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            partial def termToString :
                            Equations
                            • One or more equations did not get rendered due to their size.
                            Equations
                            def occursIn (p : Char) :

                            Check whether type variable p occurs (free) in type τ. Used in the occurs-check of unify to prevent circular substitutions.

                            Equations
                            Instances For

                              Substitute type variable x with type ty throughout the given type. The caller must ensure the occurs-check holds (i.e., ¬occursIn x ty) to prevent introducing a cyclic type.

                              Equations
                              Instances For

                                Compute the set of free type variables in a CurryType. Used as the primary component of the lexicographic termination measure for unify: cardinality of vars left ∪ vars right must decrease (or stay equal with size decreasing) across recursive calls.

                                Equations
                                Instances For
                                  def size :

                                  Compute the structural size (number of syntax-tree nodes) of a CurryType. Used as the secondary component of the termination measure for unify: when the variable-cardinality component stays equal, size must strictly decrease.

                                  Equations
                                  Instances For
                                    theorem SizeGtZ (ty : CurryType) :
                                    size ty > 0

                                    Every CurryType has strictly positive size. Used as a termination witness for unify.

                                    theorem VarsArrow (ty ty' : CurryType) :
                                    vars ty vars (ty.arrow ty')

                                    The free type variables of any type are a subset of the free variables of any arrow type built from it. Used as a termination witness for unify.

                                    structure Subst (left right : CurryType) :

                                    A type substitution witnessing that left and right can be unified. Carries apply : CurryTypeCurryType together with three invariants used to prove termination of unify:

                                    • vars_decreased: the free variables of any image are bounded by those of left, right, and the input type's own variables.
                                    • vars_eq_then_id: if the variable-cardinality does not decrease, apply is the identity.
                                    • subst_rec: apply distributes over arrow types.
                                    Instances For
                                      def Subst.id (left right : CurryType) :
                                      Subst left right

                                      The identity substitution. apply is id, so all invariants hold trivially.

                                      Equations
                                      Instances For
                                        theorem ReplaceVarScope (a : Char) (ty ty1 : CurryType) :

                                        Substituting ty for a in any type ty1 can only introduce variables already present in ty (where a was replaced). All free variables of the result lie within vars (.phi a) ∪ vars tyvars ty1.

                                        theorem OccursInIffInVars {c : Char} {τ : CurryType} :
                                        c vars τoccursIn c τ = true

                                        Membership in vars implies occursIn: if c ∈ vars τ then occursIn c τ = true. Connects the Finset-based and Bool-based representations of free-variable occurrence.

                                        theorem ReplaceElim (a : Char) (ty : CurryType) (ho : ¬occursIn a ty = true) (t : CurryType) :
                                        avars (replaceVar a ty t)

                                        After substituting ty for a, the variable a no longer appears in the result. The occurs-check condition ¬occursIn a ty prevents a from being reintroduced via ty.

                                        theorem ReplaceAddsNewVars (a : Char) (ty : CurryType) (ho : ¬occursIn a ty = true) (ty' : CurryType) :

                                        The variable count can never stay equal after a replaceVar substitution. Formally, (vars (replaceVar a ty ty')).card ≠ (vars (.phi a) ∪ vars tyvars ty').card. This is used to show that Subst.replace satisfies vars_eq_then_id by contradiction: if cardinalities were equal, subset equality would follow (by ReplaceVarScope), but a is in the RHS (vars (.phi a)) and absent from the LHS (ReplaceElim).

                                        def Subst.replace (a : Char) (ty : CurryType) (ho : ¬occursIn a ty = true) :

                                        Substitution that replaces type variable a with ty everywhere, given the occurs-check ¬occursIn a ty.

                                        Equations
                                        Instances For
                                          def Subst.replacer (a : Char) (ty : CurryType) (ho : ¬occursIn a ty = true) :

                                          Like Subst.replace but with the Subst type indices swapped (Subst ty (.phi a) instead of Subst (.phi a) ty). The underlying function is the same (replaceVar a ty); invariants delegate to ReplaceVarScope and ReplaceAddsNewVars.

                                          Equations
                                          Instances For
                                            def Subst.comp {a c b d : CurryType} (s1 : Subst a c) (s2 : Subst (s1.apply b) (s1.apply d)) :
                                            Subst (a.arrow b) (c.arrow d)

                                            Composition of two substitutions. Given s1 : Subst a c and s2 : Subst (s1.apply b) (s1.apply d), their composition s2 ∘ s1 is a Subst (.arrow a b) (.arrow c d).

                                            • vars_decreased: chain the two subset inclusions via calc.
                                            • vars_eq_then_id: if the composition is variable-count-preserving, derive that s2 = id (by cardinality squeeze on s2's image), then that s1 = id similarly.
                                            • subst_rec: each factor distributes over arrows; compose the two equations.
                                            Equations
                                            • s1.comp s2 = { apply := s2.apply s1.apply, vars_decreased := , vars_eq_then_id := , subst_rec := , uleft := a.arrow b, uright := c.arrow d }
                                            Instances For
                                              @[irreducible]
                                              def unify (left right : CurryType) :
                                              Option (Subst left right)

                                              Unification of two CurryTypes. Returns a Subst left right whose apply unifies the two types, or none if no unifier exists (fails occurs check). Termination measure: ((vars left ∪ vars right).card, size left + size right) lexicographically

                                              • φp = φq: identity if equal, otherwise replace p (φq).
                                              • φp, non-variable: occurs check; if clear, replace p ty.
                                              • arrow cases: unify heads with s1, then unify s1-images of tails with s2; compose.
                                              • non-variable, φp: symmetric to the φp case.
                                              Equations
                                              Instances For
                                                structure CtxSubst (ctx1 ctx2 : TypeCtx) :

                                                A substitution that simultaneously unifies all common variable bindings of two typing contexts. Unlike Subst, it does not carry termination invariants; it stores the apply function, the list of unified type pairs, and a subst_rec distribution proof.

                                                Instances For
                                                  def CtxSubst.from (cvars : List (Char × CurryType × CurryType)) (f : CurryTypeCurryType) (subst_rec : ∀ (a b : CurryType), f (a.arrow b) = (f a).arrow (f b)) (ctx1 ctx2 : TypeCtx) :
                                                  CtxSubst ctx1 ctx2

                                                  Construct a CtxSubst ctx1 ctx2 from a list of (variable, type1, type2) triples, a substitution function f, a proof that f distributes over arrows, and the two contexts.

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

                                                    Wrapper that erases the dependent type params of a Subst, allowing substitutions for different type pairs to be stored in a homogeneous list.

                                                    Instances For

                                                      Extract the underlying substitution function from an ExSubst.

                                                      Equations
                                                      Instances For
                                                        @[irreducible]

                                                        Helper function to recursively unify a list of common variables. Crucially, it applies the substitution from unifying the first pair to the rest of the list before continuing the recursion.

                                                        Equations
                                                        Instances For
                                                          def unifyCtx (ctx1 ctx2 : TypeCtx) :
                                                          Option (CtxSubst ctx1 ctx2)

                                                          Unify two typing contexts by unifying the types assigned to their common variables. Collects all variables present in both environments, runs unifyCtx' to recursively unify and accumulate substitutions, and composes the resulting substitutions left-to-right via foldl. Returns none if any individual unification fails.

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

                                                            Look up the type assigned to variable c in the typing context.

                                                            Equations
                                                            Instances For

                                                              Principal-type algorithm helper. Infers a PrincipalPair for term t given initial context ctx. Fresh type variables are allocated with next; type and context unification use unify / unifyCtx. Returns none only if unification fails (term is untypeable).

                                                              • .var c: return the existing type if c is in ctx, else allocate a fresh variable.
                                                              • .abs x m: recursively infer for m; if x was assigned a type, wrap as x.ty → m.ty; otherwise allocate a fresh type for x.
                                                              • .app m n: infer for m and n sequentially; unify p1 with p2 → a (fresh a); then unify the two resulting contexts and apply the composed substitution.
                                                              Equations
                                                              Instances For

                                                                Principal-type algorithm. Infers the principal type of t starting from an empty context. Returns some pair where pair.ctx is the minimal typing context and pair.ty the principal type, or none if t is not typeable in the simply-typed lambda calculus.

                                                                Equations
                                                                Instances For
                                                                  def runTest (name : String) (t : Term) :
                                                                  Equations
                                                                  Instances For