Documentation

LambdaS.DeclareSolver

Executable declaration solving #

This module is the declaration checker of Section 3 of our paper. It decides, in exact rational arithmetic, the conditions that LambdaS.Declare (consistency), LambdaS.Determinacy (which factors are fixed), and LambdaS.DeclarationComplete (whether every legal conversion is fixed) state semantically, and proves each decision sound and complete.

Three entry points, in the order a caller wants them.

DeclarationSolverExamples runs all three: disconnected feet and meters are rejected, adding foot = 0.3048 meter makes the system pass, and the dimensionless self-equation u = 2/u forces the magnitude √2, returned as radicand 2 and degree 2.

The mechanics: the solver works over explicit enumerations of the finite base-unit and base-dimension universes (enum, dims), supplied as input data rather than as a noncomputable choice of order, and reduces each declaration to one linear equation in the logarithms of the magnitudes (satisfies_valuation_iff). RationalSolver performs the elimination, and LogFactor carries the exact arithmetic that keeps it decidable.

def LambdaS.DeclSolver.matrix {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) :
Matrix (Fin n) (Fin m) ℚ

The rational coefficient matrix of declaration equations.

Equations
Instances For
    def LambdaS.DeclSolver.valuation {B : Type} {m : ℕ} (enum : Fin m ≃ B) (x : Fin m → ℝ) :

    The valuation whose log-magnitudes are x, indexed in the base-unit order enum supplies.

    Equations
    Instances For
      theorem LambdaS.DeclSolver.logScale_valuation {B : Type} [Fintype B] {m : ℕ} (enum : Fin m ≃ B) (x : Fin m → ℝ) (u : UExp B 0) :
      (valuation enum x).logScale u = ∑ j : Fin m, ↑(u.base (enum j)) * x j

      Reindexing changes neither the unit nor its magnitude.

      theorem LambdaS.DeclSolver.satisfies_valuation_iff {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (x : Fin m → ℝ) :
      (∀ (i : Fin n), Decl.Satisfies (valuation enum x) (ds i)) ↔ ∀ (i : Fin n), ∑ j : Fin m, matrix enum ds i j • x j = Real.log ↑(ds i).factor

      Declaration satisfaction is exactly the linear system in logarithms.

      theorem LambdaS.DeclSolver.valuation_coordinates {B : Type} {m : ℕ} (enum : Fin m ≃ B) (V : Scaling B 0) :
      (valuation enum fun (j : Fin m) => V.base (enum j)) = V

      Every supplied valuation has coordinates in the explicit enumeration.

      def LambdaS.DeclSolver.factorCoefficients {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (r : UExp B 0) :
      Option (Fin n → ℚ)

      Compute rational witnesses expressing a queried ratio through declarations. Failure means the ratio is not in the ℚ-span of the declared ratios. For a consistent set that is exactly the semantic statement that the declarations leave the factor undetermined (Decl.determined_iff_coefficients); for an inconsistent set no valuation satisfies the declarations at all, so the semantic notion is vacuous and only the span statement applies.

      Equations
      Instances For
        theorem LambdaS.DeclSolver.factorCoefficients_sound {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (r : UExp B 0) {c : Fin n → ℚ} (h : factorCoefficients enum ds r = some c) (b : B) :
        ∑ i : Fin n, c i * (ds i).ratio.base b = r.base b

        Successful factor lookup supplies an exact dependency witness.

        theorem LambdaS.DeclSolver.factorCoefficients_isSome_iff {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (r : UExp B 0) :
        (factorCoefficients enum ds r).isSome = true ↔ ∃ (c : Fin n → ℚ), ∀ (b : B), ∑ i : Fin n, c i * (ds i).ratio.base b = r.base b

        Lookup decides span membership, including failure, using exact rationals.

        def LambdaS.DeclSolver.solve {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) :

        Solve declaration equations in exact logarithmic numbers. A solution fixes free log-magnitudes to zero, but its particular choices do not determine a conversion unless the separate factor-witness check succeeds.

        Equations
        Instances For
          theorem LambdaS.DeclSolver.solve_sound {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) {x : Fin m → LogFactor} (h : solve enum ds = some x) (i : Fin n) :
          Decl.Satisfies (valuation enum fun (j : Fin m) => LogFactor.interpret (x j)) (ds i)

          Every returned exact solution denotes a valuation satisfying every declaration.

          theorem LambdaS.DeclSolver.solve_isSome_iff {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) :
          (solve enum ds).isSome = true ↔ ∃ (V : Scaling B 0), ∀ (i : Fin n), Decl.Satisfies V (ds i)

          Executable solving decides existence of a real satisfying valuation. No assumption that the magnitudes or their logarithms are rational is needed.

          def LambdaS.DeclSolver.factorLog {B : Type} {n : ℕ} (ds : Fin n → Decl B) (c : Fin n → ℚ) :

          Interpret the factor witness without choosing magnitudes for unconstrained units.

          Equations
          Instances For
            theorem LambdaS.DeclSolver.factorLog_correct {B : Type} [Fintype B] [DecidableEq B] {n : ℕ} (ds : Fin n → Decl B) (r : UExp B 0) (c : Fin n → ℚ) (hc : ∀ (b : B), ∑ i : Fin n, c i * (ds i).ratio.base b = r.base b) (V : Scaling B 0) (hV : ∀ (i : Fin n), Decl.Satisfies V (ds i)) :

            A linear combination of declaration ratios fixes the same combination of log-factors.

            def LambdaS.DeclSolver.conversion {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (u v : UExp B 0) :

            Exact conversion lookup. An unresolved ratio returns none, even if an arbitrary satisfying valuation could assign it a numerical value.

            Equations
            Instances For
              theorem LambdaS.DeclSolver.conversion_correct {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (u v : UExp B 0) {q : LogFactor} (h : conversion enum ds u v = some q) (V : Scaling B 0) (hV : ∀ (i : Fin n), Decl.Satisfies V (ds i)) :

              The returned exact logarithm gives the conversion factor in every satisfying valuation.

              theorem LambdaS.DeclSolver.factorCoefficients_iff_determined {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (hc : ∃ (V : Scaling B 0), ∀ (i : Fin n), Decl.Satisfies V (ds i)) (r : UExp B 0) :

              The executable factor test is exactly semantic determinacy on consistent declarations.

              theorem LambdaS.DeclSolver.conversion_isSome_iff {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (hc : ∃ (V : Scaling B 0), ∀ (i : Fin n), Decl.Satisfies V (ds i)) (u v : UExp B 0) :

              For a consistent declaration set, conversion lookup succeeds exactly when the factor of u / v is determined, so success decides determinacy rather than producing a guess.

              An inspectable exact positive factor: the positive degree-th root of radicand.

              • radicand : ℚ

                The positive rational under the root.

              • degree : ℕ

                The root degree, a positive natural number.

              • radicand_pos : 0 < self.radicand
              • degree_pos : 0 < self.degree
              Instances For
                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  The real number the factor denotes, radicand ^ (1 / degree), written as exp (log radicand / degree). Noncomputable; constructing and comparing ExactFactor values is executable.

                  Equations
                  Instances For
                    def LambdaS.DeclSolver.factorExpr {B : Type} {n : ℕ} (ds : Fin n → Decl B) (c : Fin n → ℚ) :

                    Preserve a finite expression so its radical representation can be inspected.

                    Equations
                    Instances For
                      def LambdaS.DeclSolver.exactFactor {B : Type} {n : ℕ} (ds : Fin n → Decl B) (c : Fin n → ℚ) :

                      Denominator clearing constructs a positive rational radicand and positive degree.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        def LambdaS.DeclSolver.conversionExact {B : Type} [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (u v : UExp B 0) :

                        Return an inspectable exact radical for a determined conversion.

                        Equations
                        Instances For
                          theorem LambdaS.DeclSolver.conversionExact_correct {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} (enum : Fin m ≃ B) (ds : Fin n → Decl B) (u v : UExp B 0) {q : ExactFactor} (h : conversionExact enum ds u v = some q) (V : Scaling B 0) (hV : ∀ (i : Fin n), Decl.Satisfies V (ds i)) :
                          q.value = conv V u v

                          The extracted radical is the conversion factor in every satisfying valuation.

                          structure LambdaS.DeclSolver.Checked {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} {D : Type} [UnitSys B D] {d : ℕ} (enum : Fin m ≃ B) (dims : Fin d ≃ D) (ds : Fin n → Decl B) :

                          A checked unit system carries its computed solution and evidence that its declarations are sound, consistent, and determine every legal conversion.

                          Instances For
                            def LambdaS.DeclSolver.check {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} {D : Type} [Fintype D] [DecidableEq D] [UnitSys B D] {d : ℕ} (enum : Fin m ≃ B) (dims : Fin d ≃ D) (ds : Fin n → Decl B) :
                            Option (Checked enum dims ds)

                            The declaration checker. Accepts, returning a Checked certificate, when every declaration is Decl.Sound, solve finds a solution, and DeclarationComplete.check confirms that the declared ratios and dimension rows span the unit space; returns none otherwise. check_isSome_iff characterizes acceptance.

                            Equations
                            • One or more equations did not get rendered due to their size.
                            Instances For
                              theorem LambdaS.DeclSolver.Checked.consistent {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} {D : Type} [UnitSys B D] {d : ℕ} {enum : Fin m ≃ B} {dims : Fin d ≃ D} {ds : Fin n → Decl B} (cert : Checked enum dims ds) :
                              ∃ (V : Scaling B 0), ∀ (i : Fin n), Decl.Satisfies V (ds i)

                              Every checked system supplies a genuine satisfying valuation.

                              theorem LambdaS.DeclSolver.Checked.determined {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} {D : Type} [UnitSys B D] {d : ℕ} {enum : Fin m ≃ B} {dims : Fin d ≃ D} {ds : Fin n → Decl B} (cert : Checked enum dims ds) (u v : UExp B 0) (h : SameDim (DCtx.nil D) u v) :

                              Every same-dimension conversion in a checked system is determined.

                              theorem LambdaS.DeclSolver.check_isSome_iff {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} {D : Type} [Fintype D] [DecidableEq D] [UnitSys B D] {d : ℕ} (enum : Fin m ≃ B) (dims : Fin d ≃ D) (ds : Fin n → Decl B) :
                              (check enum dims ds).isSome = true ↔ (∀ (i : Fin n), (ds i).Sound) ∧ (∃ (V : Scaling B 0), ∀ (i : Fin n), Decl.Satisfies V (ds i)) ∧ ∀ (u v : UExp B 0), SameDim (DCtx.nil D) u v → Decl.Determined ds (Term.div u v)

                              Executable declaration checking (Theorem 3.2 of our paper). check accepts exactly when every declaration is dimensionally sound, some valuation satisfies them all, and every same-dimension pair has the same factor in every satisfying valuation. So an accepted system has these properties, and a system that has them is never rejected.

                              theorem LambdaS.DeclSolver.Checked.conversionExact_isSome {B : Type} [Fintype B] [DecidableEq B] {m n : ℕ} {D : Type} [UnitSys B D] {d : ℕ} {enum : Fin m ≃ B} {dims : Fin d ≃ D} {ds : Fin n → Decl B} (cert : Checked enum dims ds) (u v : UExp B 0) (h : SameDim (DCtx.nil D) u v) :

                              Factor extraction cannot fail for a legal conversion in a checked system.