{"text": "/-\n# Example: Quotient and Remainder\n\nComputing the quotient and remainder is a basic function provided by all languages, including Lean.\nHowever, it is a simple example that nicely demonstrates some basic features of Lean as a\nspecification language.\n\nA specification of the property a function computing the quotient and remainder\nshould have is captured by the following proposition:\n-/\ndef QRProp := ∀ n d : Nat, d ≠ 0 → ∃ q r : Nat, n = d * q + r ∧ r < d\n\n/-\nAs noted earlier, being a proposition, the specification has no computational\ncontent. A proof of the proposition verifies a function computing the quotient and\nremainder exists, but does not yield such a function. We need a type for the argument, a type\nfor the result, and a proposition to capture the relationship between the argument and result.\nThe dependent function type captures the functional nature, and the subtype captures the\nthe result and the property relating the result to the argument.\n-/\ndef QR₁ := (n d : Nat) → (d ≠ 0) →\n { qr : Nat × Nat // n = d * qr.1 + qr.2 ∧ qr.2 < d }\n\n/-\n`QR₁` is the type of functions that take a natural number `n`, a natural number `d`, evidence that `d` is non-zero,\nand produce a pair of values that are the quotient and remainder when `n` is divided by `d`.\nThe data value in the result type is a pair (`Nat × Nat`) whose first element (`qr.1`) is the quotient\nand second element (`qr.2`) is the remainder. (Note: `qr.fst` and `qr.snd` can be used respectively for `qr.1` and `qr.2`.)\n\nIt is interesting to note there is no fundamental difference between dependent functions and\nthe universal quantifier. We could write `QR₁` as:\n-/\ndef QR₂ := ∀ n d : Nat, d ≠ 0 →\n { qr : Nat × Nat // n = d * qr.1 + qr.2 ∧ qr.2 < d }\n\n/-\nDespite the use of `∀`, `QR₂` is still a type, not a proposition. `∀` is simply notation defined\nin terms of the dependent function type. The presentation we choose is a matter of taste. The\nconvention usually adopted is to employ the dependent function type for a computable function,\nand the universal quantifier when specifying a property.\n\nThe one difference is the type annotation is mandatory in a dependent function type,\nbut optional in a universal quantifier (if Lean is able to infer the type). For example,\n`QR₂` can be shortened to:\n-/\ndef QR₃ := ∀ n d, d ≠ 0 →\n { qr : Nat × Nat // n = d * qr.1 + qr.2 ∧ qr.2 < d }\n\n/-\nA function derived from the specification `QR₂` has three arguments: the numerator,\nthe denominator, and evidence the denominator is non-zero. Alternatively, we could make\nfurther use of the subtype to specify a function of two arguments:\n-/\ndef QR₄ := (n : Nat) → (d : { x : Nat // x ≠ 0 }) →\n { qr : Nat × Nat // n = d * qr.1 + qr.2 ∧ qr.2 < d }\n\n/-\nIn this case the second argument is the subtype of natural numbers that excludes `0`, obviating\nthe need for the third argument. The two specifications are equivalent.\n\nMoving the function arguments to the left of the `:=`:\n-/\ndef QR₅ (n : Nat) (d : { x : Nat // x ≠ 0 }) :=\n { qr : Nat × Nat // n = d * qr.1 + qr.2 ∧ qr.2 < d }\n/-\n`QR₅` is the type of functions that take a natural number `n`, then a non-zero natural number `d`,\nand produce a pair of numbers `(q,r)` such that `n = d * q + r ∧ r < d`.\n\n## Exercises\n\n- Specify a property that determines if a number is a prime number. Use the Lean remainder on division operator (`%`).\n\n- Specify a property that determines if a number is a prime factor of another number.\n\n- Specify a function that computes that maximum prime factor of a natural number greater than 1.\n-/", "meta": {"author": "paulch42", "repo": "lean-spec", "sha": "4755a25caf719f935bcc4d54bd8a86462c9aceb9", "save_path": "github-repos/lean/paulch42-lean-spec", "path": "github-repos/lean/paulch42-lean-spec/lean-spec-4755a25caf719f935bcc4d54bd8a86462c9aceb9/LeanSpec/QuotRem.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9603611654370414, "lm_q2_score": 0.9481545276733135, "lm_q1q2_score": 0.9105707872107509}}