|
| 1 | +(* SPDX-License-Identifier: MPL-2.0 *) |
| 2 | +(* SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell (hyperpolymath) *) |
| 3 | + |
| 4 | +(* |
| 5 | + QttTyping.v |
| 6 | + ═══════════ |
| 7 | + Wave 3 (capstone): a **quantitative type system** that fuses the three Wave-1/2 |
| 8 | + pieces — the term structure + typing discipline of P2_Stlc, the multiplicity |
| 9 | + semiring of QttSemiring, and the occurrence-count usage of AffineUsage — into |
| 10 | + one judgment whose context *tracks quantities*. |
| 11 | +
|
| 12 | + `qtt Γ t A` : Γ : uctx (a usage context, var ↦ quantity) records how much each |
| 13 | + variable is used in t. Q_Var consumes one variable once; Q_App ADDS the two |
| 14 | + subcontexts (`Γ₁ + Γ₂`, the QTT context-splitting read backwards); Q_Lam binds |
| 15 | + a variable at the multiplicity it is used. |
| 16 | +
|
| 17 | + The capstone theorem **usage_soundness**: the typing context records EXACTLY |
| 18 | + the QTT usage of every variable — |
| 19 | +
|
| 20 | + qtt Γ t A → ∀ x, usage x t = Γ x |
| 21 | +
|
| 22 | + — so the type system's quantity bookkeeping is sound w.r.t. actual occurrence |
| 23 | + counts. Corollary **linear_uses_once**: a term typed in a singleton context |
| 24 | + uses exactly that one variable, exactly once. Axiom-free, no Admitted. |
| 25 | +
|
| 26 | + Scope: the *static* quantity discipline is now unified and proven sound |
| 27 | + against usage. Operational progress+preservation that PRESERVES the quantity |
| 28 | + accounting under reduction (the dynamic half) is the remaining step. |
| 29 | +
|
| 30 | + `.v` is Coq, not V-lang — see formal/README.adoc and .hypatia-ignore. |
| 31 | +*) |
| 32 | + |
| 33 | +Require Import PeanoNat. |
| 34 | +Require Import ASFormal.QttSemiring. |
| 35 | +Require Import ASFormal.AffineUsage. |
| 36 | + |
| 37 | +(* Multiplicity-annotated types: a function records how often it uses its arg. *) |
| 38 | +Inductive qty := QBase | QArr (q : quant) (A B : qty). |
| 39 | + |
| 40 | +(* Usage contexts. *) |
| 41 | +Definition uctx := id -> quant. |
| 42 | +Definition uadd (G1 G2 : uctx) : uctx := fun x => qplus (G1 x) (G2 x). |
| 43 | +Definition usingle (x : id) : uctx := fun y => if Nat.eqb x y then One else Zero. |
| 44 | +Definition uext (G : uctx) (x : id) (q : quant) : uctx := |
| 45 | + fun y => if Nat.eqb x y then q else G y. |
| 46 | + |
| 47 | +(* Quantitative typing: the context records each variable's usage. *) |
| 48 | +Inductive qtt : uctx -> tm -> qty -> Prop := |
| 49 | +| Q_Var : forall x A, qtt (usingle x) (var x) A |
| 50 | +| Q_App : forall G1 G2 f a q A B, |
| 51 | + qtt G1 f (QArr q A B) -> qtt G2 a A -> qtt (uadd G1 G2) (app f a) B |
| 52 | +| Q_Lam : forall G x q A B b, |
| 53 | + G x = Zero -> qtt (uext G x q) b B -> qtt G (lam x b) (QArr q A B). |
| 54 | + |
| 55 | +(* ── capstone: the typing context records exactly the usage ─────────────── *) |
| 56 | + |
| 57 | +Theorem usage_soundness : forall G t A, |
| 58 | + qtt G t A -> forall x, usage x t = G x. |
| 59 | +Proof. |
| 60 | + intros G t A H. |
| 61 | + induction H as [ x A |
| 62 | + | G1 G2 f a q A B Hf IHf Ha IHa |
| 63 | + | G x q A B b Hz Hb IHb ]; intro z. |
| 64 | + - (* Q_Var *) |
| 65 | + unfold usage, usingle; simpl. rewrite (Nat.eqb_sym x z). |
| 66 | + destruct (Nat.eqb z x); reflexivity. |
| 67 | + - (* Q_App *) |
| 68 | + rewrite usage_app, (IHf z), (IHa z); unfold uadd; reflexivity. |
| 69 | + - (* Q_Lam *) |
| 70 | + destruct (Nat.eqb z x) eqn:E. |
| 71 | + + apply Nat.eqb_eq in E; subst z. |
| 72 | + rewrite usage_lam_bound; symmetry; exact Hz. |
| 73 | + + assert (usage z (lam x b) = usage z b) as Hlz |
| 74 | + by (unfold usage; simpl; rewrite E; reflexivity). |
| 75 | + rewrite Hlz, (IHb z); unfold uext; rewrite (Nat.eqb_sym x z), E; reflexivity. |
| 76 | +Qed. |
| 77 | + |
| 78 | +(* A term typed in a singleton context uses exactly that one variable, once — |
| 79 | + the essence of linearity, now read off the typing. *) |
| 80 | +Corollary linear_uses_once : forall x t A, |
| 81 | + qtt (usingle x) t A -> |
| 82 | + usage x t = One /\ (forall y, y <> x -> usage y t = Zero). |
| 83 | +Proof. |
| 84 | + intros x t A H; split. |
| 85 | + - rewrite (usage_soundness _ _ _ H x); unfold usingle; rewrite Nat.eqb_refl; reflexivity. |
| 86 | + - intros y Hy. rewrite (usage_soundness _ _ _ H y); unfold usingle. |
| 87 | + assert (x <> y) as Hxy by (intro Hc; apply Hy; symmetry; exact Hc). |
| 88 | + apply Nat.eqb_neq in Hxy; rewrite Hxy; reflexivity. |
| 89 | +Qed. |
| 90 | + |
| 91 | +Print Assumptions usage_soundness. |
| 92 | +Print Assumptions linear_uses_once. |
0 commit comments