AutoMathText / data /code /github-lean-train /0.85-0.90.jsonl
March07's picture
add batch 4/5 (200 files)
5697766 verified
Raw
History Blame Contribute Delete
493 kB
{"text": "open list\n\n\n/-\n#1. You are to implement a function\nthat takes a \"predicate\" function and a\nlist and that returns a new list: namely\na list that contains all and only those\nelements of the first list for which the\ngiven predicate function returns true.\n\nWe start by giving you two predicate\nfunctions. Each takes an argument, n,\nof type ℕ. The first returns true if\nand only if n < 5. The second returns\ntrue if and only if n is even. \n-/\n\n\n/-\nHere's a \"predicate\" function that\ntakes a natural number as an argument\nand returns true if the number is less\nthan 5 otherwise it returns false. \n-/\ndef lt_5 (n : ℕ) : bool :=\n n < 5\n\n-- example cases\n#eval lt_5 2\n#eval lt_5 6\n\n/-\nHere's another predicate function, one\nthat returns true if a given nat is even,\nand false otherwise. Note that we have\ndefined this function recursively. Zero\nis even, one is not, and (2 + n') is if\nand only if n' is.\n\nYou can think of a predicate function\nas a function that answers the question,\ndoes some value (here a nat) have some\nproperty? The properties in these two\ncases are (1) the property of being less\nthan 5, and (2) the property of being\neven.\n-/\ndef evenb : ℕ → bool\n| 0 := tt\n| 1 := ff\n| (n' + 2) := evenb n'\n\n#eval evenb 0\n#eval evenb 3\n#eval evenb 4\n#eval evenb 5\n\n/-\nWe call the function you are going to \nimplement a \"filter\" function, because\nit takes a list and returns a \"filtered\"\nversion of the list. Call you function\n\"myfilter\".\n\nIf you filter an empty list you always\nget an empty list as a result. If you\nfilter a non-empty list, l = (cons h t), \nthe returned list has h at its head if\nand only if the predicate function applied\nto h returns true, otherwise the returned\nvalue is just the filtered version of t.\n\nA. [15 points] \n\nYour task is to complete an incomplete\nversion of the definition of the myfilter\nfunction. We use a Lean construct new to \nyou: the if ... then ... else. It works\nas you would expect from your work with\nother programming languages. Replace the \nunderscores to complete the definition.\n-/\n\ndef myfilter : (ℕ → bool) → list ℕ → list ℕ \n| p list.nil := list.nil\n| p (cons h t) :=\n if p h \n then (list.cons h (myfilter p t))\n else myfilter p t\n\n/-\nHere's the definition of a simple list.\n-/\n\ndef a_list := [0,1,2,3,4,5,6,7,8,9]\n\n/-\nB. [10 points]\n\nReplace the underscores in the first two \neval commands below as follows. Replace \nthe first one with an expression in which \nmyfilter is used to compute a new list that \ncontains the numbers in a_list that are \nless than 5. Replace the second one with \nan expression in which mfilter is used to \ncompute a list containing even elements of \na_list. You may use the predicate functions\nwe defined above.\n\nReplace the third underscore with a similar\nexpression but where you use a λ expression\nto specify a predicate function that takes\na nat, n, and returns tt if n is equal to\nthree and false otherwise. Hint: n=3 is\nan expression that will return the desired \nbool.\n-/\n\n#eval myfilter lt_5 a_list\n#eval myfilter evenb a_list\n#eval myfilter (λ n:ℕ , n=3) a_list\n\n\n\n/-\n#2.\n\nHere's a function that takes a function, f,\nfrom ℕ to ℕ, and a value, n, of type ℕ, and\nthat returns the value that is obtained by \nsimply applying f to n. \n-/\n\ndef f_apply : (ℕ → ℕ) → ℕ → ℕ\n| f n := (f n)\n-- examples of its use\n#eval f_apply nat.succ 3\n#eval f_apply (λ n : ℕ, n * n) 3\n\n/-\nA. [10 points]\n\nWrite a function, f_apply_2, that takes a \nfunction, f, from ℕ to ℕ, and a value, n, \nof type ℕ, and that returns (read this\ncarefully) the value obtained by applying \nf twice to n: the result of applying f to \nthe result of applying f to n. For example,\nif f is the function that squares its nat\nargument, then (f_apply_2 f 3) returns 81,\nas f applied to 3 is 9 and f applied to 9\nis 81. \n-/\n\n-- Your answer here\n\ndef f_apply_2 : (ℕ → ℕ) → ℕ → ℕ \n| f n := f (f n)\n\n\n/-\nB. [10 points]\n\nWrite a function f_apply_k that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nn, of type ℕ, and (3) a value, k of type ℕ,\nand that returns the result of applying f \nto n k times. \n\nNote that f_apply applies f to n once and\nff_apply applies f to n twice. Your job is\nto write a function that is general in the\nsense that you specify by a parameter, k,\nhow many times to apply f.\n\nHint 1: Use recursion. Note: The result of\napplying any function, f, to n, zero times\nis just n.\n-/\n\n\n-- Answer here\ndef f_apply_k : (ℕ → ℕ ) → ℕ → ℕ → ℕ \n| f n 0 := n\n| f n (k+1) := f (f_apply_k f n k)\n\n/-\nUse #eval to evaluate an expression in\nwhich the squaring function, expressed\nas a λ expression, is applied to 3 two \ntimes. You should be able to confirm that\nyou get the same answer given by using\nthe f_apply_2 function in the example above.\n-/\n\n-- Answer here\n#eval f_apply_k (λ n:ℕ, n*n) 3 2\n#eval f_apply_2 (λ n:ℕ, n*n) 3\n\n/-\nC. [Extra Credit]\n\nWrite a function f_apply_k_fun that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nk of type ℕ, and that returns a function that,\nwhen applied to a natural number, n, returns\nthe result of applying f to n k times.\n-/\ndef f_apply_k_fun : (ℕ → ℕ ) → ℕ → (ℕ → ℕ) \n| f k := (λ n:ℕ, (f_apply_k f n k))\n\n\n/-\n#3: [15 points]\n\nWrite a function, mcompose, that\ntakes two functions, g and f (in that\norder), each of type ℕ → ℕ, and that \nreturns *a function* that, when applied\nto an argument, n, of type ℕ, returns\nthe result of applying g to the result \nof applying f to n.\n-/\n\n-- Answer here\ndef mcompose : (ℕ → ℕ ) → (ℕ → ℕ) → (ℕ → ℕ)\n| f g := (λ n:ℕ, f_apply f (f_apply g n))\n\n/-\n#4. Higher-Order Functions\n\n4A. [10 points] Provide an implementatation of\na function, map_pred that takes as its arguments \n(1) a predicate function of type ℕ → bool, (2) a\nlist of natural numbers (of type \"list nat\"), \nand that returns a list in which each ℕ value,\nn,in the argument list is replaced by true (tt) \nif the predicate returns true for a, otherwise\nfalse (ff).\n\nFor example, if the predicate function returns\ntrue if and only if its argument is zero, then\napplying map to this function and to the list\n[0,1,2,0,1,0] must return [tt,ff,ff,tt,ff,tt].\n\n\nTest your code by using #eval or #reduce to evaluate\nan expression in which map_pred is applied to \nsuch an \"is_zero\" predicate function and to the\nlist 0,1,2,0,1,0]. Express the predicate function\nas a lambda abstraction within the #eval command.\n\nNOTE: You will have to use list.nil and list.cons\nto refer to the nil and cons constructors of the\nlibrary-provided list data type, as you already\nhave definitions for list and cons in the current\nnamespace.\n-/\n\n-- Answer here\ndef map_pred : (ℕ → bool) → list ℕ → list bool\n| f list.nil := list.nil\n|f (list.cons h t) := list.cons (f h) (map_pred f t)\n\n#eval map_pred (λ n:ℕ, n=0) [0,1,3,0,1,0]\n\n/-\n4B. [10 points] Implement a function, reduce_or, \nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if there is at least one true value in the list,\notherwise ff. Note: the Lean libraries provide the\nfunction \"bor\" to compute \"b1 or b2\", where b1 and\nb2 are Booleans. We recommend that you include\ntests of your solution.\n-/\n\n-- example\n#reduce bor tt tt\n\n-- Answer here\ndef reduce_or : list bool → bool\n| list.nil := ff\n| (list.cons h t) := bor h (reduce_or t) \n\n#reduce reduce_or []\n#reduce reduce_or [tt,tt, tt]\n#reduce reduce_or [ff, tt, tt]\n#reduce reduce_or [tt, ff, tt]\n#reduce reduce_or [tt, ff, ff]\n#reduce reduce_or [ff, ff, ff]\n/-\n4C. [10points] Implement a function, reduce_and,\nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if every value in the list is true, otherwise ff.\n-/\n\n-- Note: band implements the Boolean \"and\" function\n#reduce band tt tt\n\n-- Answer here\ndef reduce_and : list bool → bool\n| list.nil := ff\n| [tt] := tt\n| (list.cons h t) := band h (reduce_and t) \n\n#reduce reduce_and []\n#reduce reduce_and [tt, tt, tt]\n#reduce reduce_and [ff, tt, tt]\n#reduce reduce_and [tt, ff, tt]\n#reduce reduce_and [tt, ff, ff]\n#reduce reduce_and [ff, ff, ff]\n\n/-\n4D. [10 points] Define a function, all_zero, that \ntakes a list of nat values and returns true if and \nonly if they are all zero. Express your answer using \nmap and reduce functions that you have previously\ndefined above. Again we recommend that you test your \nsolution.\n-/\n\n-- Answer here (replace the _'s as needed)\n\ndef all_zero : list nat → bool\n| list.nil := ff\n| (list.cons h t) := reduce_and (map_pred (λ n:ℕ, n=0) (list.cons h t))\n\n/-\nSome tests\n-/\n#reduce all_zero []\n#reduce all_zero [0,0,0,0]\n#reduce all_zero [1,0,0,0]\n#reduce all_zero [0,1,0,0]\n#reduce all_zero [1,0,0,1]\n", "meta": {"author": "yl4df", "repo": "Discrete-Mathematics", "sha": "c93ce9f6a6e36d194e350d9fa0a0360191e97fa0", "save_path": "github-repos/lean/yl4df-Discrete-Mathematics", "path": "github-repos/lean/yl4df-Discrete-Mathematics/Discrete-Mathematics-c93ce9f6a6e36d194e350d9fa0a0360191e97fa0/assignments/hw5_higher_order_functions.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9653811601648193, "lm_q2_score": 0.9263037292767218, "lm_q1q2_score": 0.8942361688341603}}
{"text": "-- Define a predicate `sorted` that takes a list of natural numbers and returns a proposition\n-- that is true if the list is sorted in non-decreasing order\ndef sorted : list ℕ → Prop\n| [] := true -- An empty list is always sorted\n| [x] := true -- A list with a single element is always sorted\n| (x :: y :: rest) := x ≤ y ∧ sorted (y :: rest) -- A list with two or more elements is sorted if the first two elements are in non-decreasing order and the remaining elements are also sorted\n\n-- The following code defines and verifies the correctness of an insertion sort algorithm\n-- that uses the `sorted` predicate to ensure that the output is always sorted\n\n-- Define a function `insert` that takes a natural number `x` and a sorted list of natural numbers\n-- `ys` and returns a new sorted list with `x` inserted in the correct position\ndef insert (x : ℕ) : list ℕ → list ℕ\n| [] := [x] -- If `ys` is empty, `x` is the only element and is inserted at the front of the list\n| (y :: ys) := if x ≤ y then x :: y :: ys else y :: insert ys -- If `ys` is non-empty, compare `x` with the first element `y` and recursively insert `x` into the remaining list `ys`\n\n-- Define a function `insertion_sort` that takes a list of natural numbers and returns a sorted list\n-- using the `insert` function to repeatedly insert each element into the correct position\ndef insertion_sort : list ℕ → list ℕ\n| [] := [] -- An empty list is already sorted\n| (x :: xs) := insert x (insertion_sort xs) -- For a non-empty list, insert the first element `x` into the sorted list `insertion_sort xs`\n\n-- Define a theorem `insertion_sort_correct` that states that `insertion_sort` always returns a sorted list\ntheorem insertion_sort_correct (xs : list ℕ) : sorted (insertion_sort xs) :=\nlist.rec_on xs -- Perform structural induction on `xs`\n (show sorted (insertion_sort []), by simp [insertion_sort]) -- The base case is an empty list, which is already sorted\n (assume x xs ih,\n have h₁ : sorted (insertion_sort xs), from ih, -- Assume that `insertion_sort xs` is sorted\n have h₂ : sorted (insert x (insertion_sort xs)), from\n match insertion_sort xs with\n | [] := by simp [insertion_sort, sorted] -- If `xs` is empty, `insertion_sort xs` is empty and `x` is the only element, which is trivially sorted\n | (y :: ys) := by simp [insertion_sort, sorted] at h₁ ⊢; -- If `xs` is non-empty, use the induction hypothesis to show that `insertion_sort xs` is sorted\n exact ⟨le_total x y, h₁⟩ -- Then use the `sorted` predicate to show that `x` is inserted into the correct position and the resulting list is sorted\n end,\n show sorted (insertion_sort (x :: xs)), by simp [insertion_sort, sorted] at h₂ ⊢; exact h₂) -- Finally, use the `sorted` predicate to show that `insertion_sort (x :: xs)` is sorted by applying the `insert` function to the first element `x` and the sorted list `\n", "meta": {"author": "voizlav", "repo": "formal-verification", "sha": "a980eff3f99645b4e854396d3a922dcd5e7d069d", "save_path": "github-repos/lean/voizlav-formal-verification", "path": "github-repos/lean/voizlav-formal-verification/formal-verification-a980eff3f99645b4e854396d3a922dcd5e7d069d/sorted.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9678992951349231, "lm_q2_score": 0.923039161657607, "lm_q1q2_score": 0.8934089539503282}}
{"text": "import .love01_definitions_and_statements_demo\n\n\n/-! # LoVe Exercise 1: Definitions and Statements\n\nReplace the placeholders (e.g., `:= sorry`) with your solutions. -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1: Truncated Subtraction\n\n1.1. Define the function `sub` that implements truncated subtraction on natural\nnumbers by recursion. \"Truncated\" means that results that mathematically would\nbe negative are represented by 0. For example:\n\n `sub 7 2 = 5`\n `sub 2 7 = 0` -/\n\ndef sub : ℕ → ℕ → ℕ :=\n | 0, m => 0\n | n, 0 => n\n | n+1, m+1 => sub n m \n\n/-! 1.2. Check that your function works as expected. -/\n\n#eval sub 0 0 -- expected: 0\n#eval sub 0 1 -- expected: 0\n#eval sub 0 7 -- expected: 0\n#eval sub 1 0 -- expected: 1\n#eval sub 1 1 -- expected: 0\n#eval sub 3 0 -- expected: 3\n#eval sub 2 7 -- expected: 0\n#eval sub 3 1 -- expected: 2\n#eval sub 3 3 -- expected: 0\n#eval sub 3 7 -- expected: 0\n#eval sub 7 2 -- expected: 5\n\n\n/-! ## Question 2: Arithmetic Expressions\n\nConsider the type `aexp` from the lecture and the function `eval` that\ncomputes the value of an expression. You will find the definitions in the file\n`love01_definitions_and_statements_demo.lean`. One way to find them quickly is\nto\n\n1. hold the Control (on Linux and Windows) or Command (on macOS) key pressed;\n2. move the cursor to the identifier `aexp` or `eval`;\n3. click the identifier. -/\n\n#check aexp\n#check eval\n\n/-! 2.1. Test that `eval` behaves as expected. Make sure to exercise each\nconstructor at least once. You can use the following environment in your tests.\nWhat happens if you divide by zero?\n\nMake sure to use `#eval`. For technical reasons, `#reduce` does not work well\nhere. Note that `#eval` (Lean's evaluation command) and `eval` (our evaluation\nfunction on `aexp`) are unrelated. -/\n\ndef some_env : string → ℤ\n| \"x\" := 3\n| \"y\" := 17\n| _ := 201\n\n#eval eval some_env (aexp.var \"x\") -- expected: 3\n-- invoke `#eval` here\n\n/-! 2.2. The following function simplifies arithmetic expressions involving\naddition. It simplifies `0 + e` and `e + 0` to `e`. Complete the definition so\nthat it also simplifies expressions involving the other three binary\noperators. -/\n\ndef simplify : aexp → aexp\n| (aexp.add (aexp.num 0) e₂) := simplify e₂\n| (aexp.add e₁ (aexp.num 0)) := simplify e₁\n-- insert the missing cases here\n-- catch-all cases below\n| (aexp.num i) := aexp.num i\n| (aexp.var x) := aexp.var x\n| (aexp.add e₁ e₂) := aexp.add (simplify e₁) (simplify e₂)\n| (aexp.sub e₁ e₂) := aexp.sub (simplify e₁) (simplify e₂)\n| (aexp.mul e₁ e₂) := aexp.mul (simplify e₁) (simplify e₂)\n| (aexp.div e₁ e₂) := aexp.div (simplify e₁) (simplify e₂)\n\n/-! 2.3. Is the `simplify` function correct? In fact, what would it mean for it\nto be correct or not? Intuitively, for `simplify` to be correct, it must\nreturn an arithmetic expression that yields the same numeric value when\nevaluated as the original expression.\n\nGiven an environment `env` and an expression `e`, state (without proving it)\nthe property that the value of `e` after simplification is the same as the\nvalue of `e` before. -/\n\nlemma simplify_correct (env : string → ℤ) (e : aexp) :\n true := -- replace `true` by your lemma statement\nsorry\n\n\n/-! ## Question 3: λ-Terms\n\n3.1. Complete the following definitions, by replacing the `sorry` markers by\nterms of the expected type.\n\nHint: A procedure for doing so systematically is described in Section 1.1.4 of\nthe Hitchhiker's Guide. As explained there, you can use `_` as a placeholder\nwhile constructing a term. By hovering over `_`, you will see the current\nlogical context. -/\n\ndef I : α → α :=\nλa, a\n\ndef K : α → β → α :=\nλa b, a\n\ndef C : (α → β → γ) → β → α → γ :=\nsorry\n\ndef proj_1st : α → α → α :=\nsorry\n\n/-! Please give a different answer than for `proj_1st`. -/\n\ndef proj_2nd : α → α → α :=\nsorry\n\ndef some_nonsense : (α → β → γ) → α → (α → γ) → β → γ :=\nsorry\n\n/-! 3.2. Show the typing derivation for your definition of `C` above, on paper\nor using ASCII or Unicode art. You might find the characters `–` (to draw\nhorizontal bars) and `⊢` useful. -/\n\n-- write your solution in a comment here or on paper\n\nend LoVe\n", "meta": {"author": "jappaaa", "repo": "Bachelor-project", "sha": "56d13d7ad5136ac2142d0d7cccb859c1a96a81e5", "save_path": "github-repos/lean/jappaaa-Bachelor-project", "path": "github-repos/lean/jappaaa-Bachelor-project/Bachelor-project-56d13d7ad5136ac2142d0d7cccb859c1a96a81e5/master course files/logical_verification_2021-main/lean/love01_definitions_and_statements_exercise_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9579122672782974, "lm_q2_score": 0.9304582516374121, "lm_q1q2_score": 0.891297373433794}}
{"text": "open list\n\n\n/-\n#1. You are to implement a function\nthat takes a \"predicate\" function and a\nlist and that returns a new list: namely\na list that contains all and only those\nelements of the first list for which the\ngiven predicate function returns true.\n\nWe start by giving you two predicate\nfunctions. Each takes an argument, n,\nof type ℕ. The first returns true if\nand only if n < 5. The second returns\ntrue if and only if n is even. \n-/\n\n\n/-\nHere's a \"predicate\" function that\ntakes a natural number as an argument\nand returns true if the number is less\nthan 5 otherwise it returns false. \n-/\ndef lt_5 (n : ℕ) : bool :=\n n < 5\n\n-- example cases\n#eval lt_5 2\n#eval lt_5 6\n\n/-\nHere's another predicate function, one\nthat returns true if a given nat is even,\nand false otherwise. Note that we have\ndefined this function recursively. Zero\nis even, one is not, and (2 + n') is if\nand only if n' is.\n\nYou can think of a predicate function\nas a function that answers the question,\ndoes some value (here a nat) have some\nproperty? The properties in these two\ncases are (1) the property of being less\nthan 5, and (2) the property of being\neven.\n-/\ndef evenb : ℕ → bool\n| 0 := tt\n| 1 := ff\n| (n' + 2) := evenb n'\n\n#eval evenb 0\n#eval evenb 3\n#eval evenb 4\n#eval evenb 5\n\n/-\nWe call the function you are going to \nimplement a \"filter\" function, because\nit takes a list and returns a \"filtered\"\nversion of the list. Call you function\n\"myfilter\".\n\nIf you filter an empty list you always\nget an empty list as a result. If you\nfilter a non-empty list, l = (cons h t), \nthe returned list has h at its head if\nand only if the predicate function applied\nto h returns true, otherwise the returned\nvalue is just the filtered version of t.\n\nA. [15 points] \n\nYour task is to complete an incomplete\nversion of the definition of the myfilter\nfunction. We use a Lean construct new to \nyou: the if ... then ... else. It works\nas you would expect from your work with\nother programming languages. Replace the \nunderscores to complete the definition.\n-/\n\ndef myfylter : (ℕ → bool) → (list ℕ) → ( list ℕ) \n| p [] := []\n| p (cons h t) :=\n if p h = tt\n then (cons (h) (myfylter p t))\n else myfylter p t\n\n/-\nHere's the definition of a simple list.\n-/\n\ndef a_list := [0,1,2,3,4,5,6,7,8,9]\n\n/-\nB. [10 points]\n\nReplace the underscores in the first two \neval commands below as follows. \n(1) Replace the first one with an expression in which \nmyfilter is used to compute a new list that \ncontains the numbers in a_list that are \nless than 5. \n\n(2) Replace the second one with an expression in which myfilter is used to \ncompute a list containing even elements of \na_list. You may use the predicate functions\nwe defined above.\n\n(3) Replace the third underscore with a similar\nexpression but where you use a λ expression\nto specify a predicate function that takes\na nat, n, and returns tt if n is equal to\nthree and false otherwise. Hint: n=3 is\nan expression that will return the desired \nbool.\n-/\n\n#eval (myfylter (lt_5)(a_list))\n#eval (myfylter (evenb)(a_list))\n#eval (myfylter (λ n:nat, if n = 3 then tt else ff) (a_list))\n\n\n\n/-\n#2.\n\nHere's a function that takes a function, f,\nfrom ℕ to ℕ, and a value, n, of type ℕ, and\nthat returns the value that is obtained by \nsimply applying f to n. \n-/\n\ndef f_apply : (ℕ → ℕ) → ℕ → ℕ \n| f n := (f n)\n\n-- examples of its use\n#eval f_apply nat.succ 3\n#eval f_apply (λ n : ℕ, n * n) 3\n\n/-\nA. [10 points]\n\nWrite a function, f_apply_2, that takes a \nfunction, f, from ℕ to ℕ, and a value, n, \nof type ℕ, and that returns (read this\ncarefully) the value obtained by applying \nf twice to n: the result of applying f to \nthe result of applying f to n. For example,\nif f is the function that squares its nat\nargument, then (f_apply_2 f 3) returns 81,\nas f applied to 3 is 9 and f applied to 9\nis 81. \n-/\n\n-- Your answer here\n\ndef f_apply_2 : (ℕ → ℕ) → ℕ → ℕ \n| f n := f(f n)\n\n/-\nB. [10 points]\n\nWrite a function f_apply_k that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nn, of type ℕ, and (3) a value, k of type ℕ,\nand that returns the result of applying f \nto n k times. \n\nNote that f_apply applies f to n once and\nf_apply_2 applies f to n twice. Your job is\nto write a function that is general in the\nsense that you specify by a parameter, k,\nhow many times to apply f.\n\nHint 1: Use recursion. Note: The result of\napplying any function, f, to n, zero times\nis just n.\n-/\n\n\n-- Answer here\n\n/-\nk = 0\nk = r + 1\nkeep on doing until r is 0 \n\nf n = f ( f_apply_k(f,l,r))\n-/\n\ndef f_apply_k : (ℕ → ℕ ) → ℕ → ℕ → ℕ\n|f n 0 := n\n|f n (nat.succ k) := f (f_apply_k f n k) -- stop when k = 0\n\n#eval f_apply_k (λ n : ℕ, n * n) 3 2\n\n#eval nat.pred 5\n/-\nUse #eval to evaluate an expression in\nwhich the squaring function, expressed\nas a λ expression, is applied to 3 two \ntimes. You should be able to confirm that\nyou get the same answer given by using\nthe ff_apply function in the example above.\n-/\n\n-- Answer here\n\n\n/-\nC. [Extra Credit]\n\nWrite a function f_apply_k_fun that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nk of type ℕ, and that returns a function that,\nwhen applied to a natural number, n, returns\nthe result of applying f to n k times.\n-/\n\n\n/-\n#3: [15 points]\n\nWrite a function, mcompose, that\ntakes two functions, g and f (in that\norder), each of type ℕ → ℕ, and that \nreturns *a function* that, when applied\nto an argument, n, of type ℕ,\n\nreturns the result of applying g to the result \nof applying f to n.\n-/\n\n-- Answer here\n\ndef mcompose : (ℕ → ℕ ) → ( ℕ → ℕ ) → (ℕ → ℕ ) := \nλ g f,\nλ n:nat ,g (f n) \n\n\n/-\n#4. Higher-Order Functions\n\n4A. [10 points] Provide an implementatation of\na function, map_pred that takes as its arguments \n(1) a predicate function of type ℕ → bool, (2) a\nlist of natural numbers (of type \"list nat\"), \nand that returns a list in which each ℕ value,\nn,in the argument list is replaced by true (tt) \nif the predicate returns true for a, otherwise\nfalse (ff).\n\nFor example, if the predicate function returns\ntrue if and only if its argument is zero, then\napplying map to this function and to the list\n[0,1,2,0,1,0] must return [tt,ff,ff,tt,ff,tt].\n\n\nTest your code by using #eval or #reduce to evaluate\nan expression in which map_pred is applied to \nsuch an \"is_zero\" predicate function and to the\nlist 0,1,2,0,1,0]. Express the predicate function\nas a lambda abstraction within the #eval command.\n\nNOTE: You will have to use list.nil and list.cons\nto refer to the nil and cons constructors of the\nlibrary-provided list data type, as you already\nhave definitions for list and cons in the current\nnamespace.\n-/\n\n-- Answer here\n\n\n\ndef map_pred : (ℕ → bool) → list nat → list bool\n|f list.nil := list.nil\n|f(list.cons h t) := list.cons (f h) ( map_pred f t)\n\ndef is_zero : ℕ → bool\n| 0 := tt\n| nat := ff\n\n#eval map_pred is_zero [0,1,2,1,0]\n\n/-\n4B. [10 points] Implement a function, reduce_or, \nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if there is at least one true value in the list,\notherwise ff. Note: the Lean libraries provide the\nfunction \"bor\" to compute \"b1 or b2\", where b1 and\nb2 are Booleans. We recommend that you include\ntests of your solution.\n-/\n\n-- example\n#reduce bor tt tt\n\n-- Answer here\ndef reduce_or: list bool → bool \n| [] := ff\n| (cons h t) := bor h (reduce_or t) \n \n\n/-\n4C. [10points] Implement a function, reduce_and,\nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if every value in the list is true, otherwise ff.\n-/\n\n-- Note: band implements the Boolean \"and\" function\n#reduce band tt tt\n\n-- Answer here\ndef reduce_and: list bool → bool\n| [] := ff\n|(cons h t) := band (h) (reduce_and(t))\n \n\n/-\n4D. [10 points] Define a function, all_zero, that \ntakes a list of nat values and returns true if and \nonly if they are all zero. Express your answer using \nmap and reduce functions that you have previously\ndefined above. Again we recommend that you test your \nsolution.\n-/\n\n-- Answer here (replace the _'s as needed)\n\ndef all_zero : list nat → bool\n| [] := tt\n| (cons h t):= \n if is_zero h\n then band tt (all_zero t)\n else ff\n/-\nSome tests\n-/\n#reduce all_zero []\n#reduce all_zero [0,0,0,0]\n#reduce all_zero [1,0,0,0]\n#reduce all_zero [0,1,0,0]\n#reduce all_zero [1,0,0,1]\n", "meta": {"author": "avonstar21", "repo": "CS-2102", "sha": "2b99b40c9fe8c532afee5d334d87f6265a36bfec", "save_path": "github-repos/lean/avonstar21-CS-2102", "path": "github-repos/lean/avonstar21-CS-2102/CS-2102-2b99b40c9fe8c532afee5d334d87f6265a36bfec/My Work/Homeworks/hw5_higher_order_functions.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9609517061554855, "lm_q2_score": 0.9252299632771662, "lm_q1q2_score": 0.8891013117973701}}
{"text": "-- we can say let f be the function which maps any natural number x to x + 5.\n-- The expression λ x : nat, x + 5 is just a symbolic representation of the right-hand side of this assignment.\n#check fun x : nat, x + 5\n#check λ x : nat, x + 5\n\n-- Here are some more abstract examples \nconstants α β : Type\nconstants a1 a2 : α \nconstants b1 b2 : β\n\nconstant f : α → α \nconstant g : α → β \nconstant h : α → β → α \nconstant p : α → α → bool\n\n#check fun x : α, f x\n#check λ x : α , f x\n#check λ x : α , f (f x)\n#check λ x : α , h x b1\n#check λ y : β , h a1 y \n#check λ x : α , p (f (f x)) (h (f a1) b2)\n#check λ x : α , λ y : β , h (f x) y\n#check λ (x : α) (y : β), h (f x ) y\n#check λ x y, h (f x) y\n\n\n-- Some mathematically common examples of operations of functions can be described in terms of lambda abstraction:\nconstants α' β' γ : Type\nconstant f' : α' → β'\nconstant g' : β' → γ \nconstant b : β'\n\n#check λ x : α', x -- denotes the identity function on α' \n#check λ x : α', b -- denotes the constant fn that always returns b \n#check λ x : α', g' (f' x) -- denotes the composition of f and g\n#check λ x , g' (f' x) -- We can omit the type declaration as Lean can infer \n\n\n-- We can abstract over any of the constants in the previous definitions:\n#check λ b : β', λ x : α', x\n#check λ (b: β') (x: α'), x\n#check λ (g' : β' → γ) (f' : α' → β') (x : α'), g' (f' x)\n\n-- Lean let's us combine lambdas, so the second example is equivalent to the first.\n-- We can even abstract over the type\n#check λ (α β : Type*) (b : β) (x : α), x\n#check λ (α β γ : Type*) (g: β → γ) (f : α → β) (x : α), g (f x)\n-- The last expression, e.g denotes the fn that takes three types α β γ and two \n-- fns g : β → γ and f : α → β and returns the composition of g and f \n\n-- notice the types of the following expressions(P.S: bound variables)\nconstants α'' β'' γ'' : Type\nconstant f'' : α'' → β''\nconstant g'' : β'' → γ''\nconstant h'' : α'' → α''\nconstants (a' : α'') (b' : β'') \n\n#check (λ x : α'', x) a'\n#check (λ x : α'' , b') a'\n#check (λ x : α'', b') (h'' a')\n#check (λ x : α'', g'' (f'' x)) (h'' (h'' a'))\n\n#check (λ (v: β'' → γ'') (u: α'' → β'') x, v (u x)) g'' f'' a'\n\n#check (λ (Q R S: Type*) (v: R → S) (u : Q → R) (x: Q),\n v (u x)) α'' β'' γ'' g'' f'' a'\n\n-- As expected, the expression (λ x : α, x) a has type α\n-- In fact, more should be true: applying the expression (λ x : α, x) to a should “return” the value a\n\nconstants α''' β''' γ''' : Type \nconstant f''' : α''' → β'''\nconstant g''' : β''' → γ'''\nconstant h''' : α''' → α'''\nconstants (a'' : α''') (b'' : β''') \n\n#reduce (λ x : α''', x) a''\n#reduce (λ x : α''', b'') a''\n#reduce (λ x : α''', b'') (h''' a'')\n#reduce (λ x : α''', g''' (f''' x)) a''\n\n#reduce (λ (v: β''' → γ''') (u: α''' → β''') x, v (u x)) g''' f''' a''\n\n#reduce (λ (Q R S: Type*) (v: R → S) (u : Q → R) (x: Q),\n v (u x)) α''' β''' γ''' g''' f''' a''\n\n-- The command #reduce tells Lean to evaluate an expression by reducing it to normal\n-- form, which is to say, carrying out all computational reductions that are \n-- sanctioned by the underlying logic\n-- The process of simplifying an expression (λ x, t)s to t[s/x] \n-- that is, t with s substituted for the variable x – is known as beta reduction\n-- and two terms that beta reduce to a common term are called beta equivalent\n\n-- other forms of reduction\nconstants m n : nat\nconstant b''' : bool\n\n#print \"reduce pairs\"\n#reduce (m,n).1\n#reduce (m,n).2\n\n#print \"reducing boolean expressions\"\n#reduce tt && ff\n#reduce ff && b'''\n#reduce b''' && ff\n\n#print \"reducing arithmetic expressions\"\n#reduce n + 0\n#reduce n + 2\n#reduce 2 + 3\n\n-- Important!!!: every term has a computational behavior, and supports a \n-- notion of reduction, or normalization\n-- In principle, two terms that reduce to the same value are called definitionally equal", "meta": {"author": "0x-Inf", "repo": "theorem_proving_in_lean", "sha": "ffba957d08381aa846eb33cd1c855dd30eaca52f", "save_path": "github-repos/lean/0x-Inf-theorem_proving_in_lean", "path": "github-repos/lean/0x-Inf-theorem_proving_in_lean/theorem_proving_in_lean-ffba957d08381aa846eb33cd1c855dd30eaca52f/code_examples/part2/fn_abstraction_n_evaluation.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9553191348157373, "lm_q2_score": 0.930458263207691, "lm_q1q2_score": 0.888884582989725}}
{"text": "/-\nRead, and if you have already read, then re-read, the \nchapters in the notes on proofs of disjunctions and negations. \nWe have added some new material, especially under negation\nelimination.\n\nIn proofs of bi-implications, use comments to mark the start of\nthe proofs of the implications in each direction. Label one as\n\"forward\" the other other as \"backward.\"\n\nThe collaboration policy for this homework is \"no collaboration\nallowed.\" You may study and discuss the underlying concepts with\nanyone.\n\nYou may provide proofs in the style of your choice: term-style,\ntactic style, or mixed. Yes, you can using tactic scripts within\nterms and terms within tactic scripts. You may use any tactics \nyou know of. As a courtesy, we provide begin/end pairs, in case\nyou should want to use them. Otherwise you may delete them.\n-/\n\n/- \n1. 15 points\n-/\nexample : ∀ (P Q : Prop), P ∧ Q → P ∨ Q :=\nbegin\nend\n\n\n/-\n2. 15 points\n-/\nexample : \n ∀ (P Q R : Prop), (P ∨ Q) → (Q ∨ R) → ¬ Q → (P ∧ R) :=\nbegin\nend \n\n/-\n3. 15 points\n-/\nexample : \n ∀ (P Q R : Prop), P ∧ (Q ∨ R) ↔ (P ∧ Q) ∨ (P ∧ R) :=\nbegin\nend\n\n\n/-\n4. 10 points\n-/\n\nexample : ∀ (P Q R : Prop), P → Q → R → ¬ Q → (Q ∨ ¬ Q) :=\nbegin\nend\n\nopen classical -- hint: you can now use em easily\n\n/-\n4a. 5 points. Write *your own* proof of this conjecture.\n-/\n\nexample : ∀ (P Q : Prop), ¬ (P ∨ Q) ↔ ¬ P ∧ ¬ Q :=\nbegin\nend\n\n/-\n4b. 5 points. Is this theorem classically true in neither, one, or\nboth directions. Explain your answer in relation to your proof. \n\nAnswer: \n-/\n\n/-\n5. 5 points. Write *your own proof* of this conjecture.\n-/\nexample : ∀ (P Q : Prop), ¬ (P ∧ Q) ↔ (¬ P ∨ ¬ Q) :=\nbegin\nend\n\n\n/-\n6. 10 points\n-/\n\nexample : ∀ (P : Prop), (¬ ¬ P → P) ↔ (P ∨ ¬ P) :=\nbegin\nend\n\n\n/-\n7. 5 points\n\nTranlate the preceding proposition into English,\nreferring explicitly to the principles of negation \nelimination and excluded middle. Write your sentence\nhere:\n\n-/\n\n\n/-\n8. [10 points]\n-/\n\nexample : \n (∀ ( P Q : Prop ), (P → Q) ↔ (¬ Q → ¬ P)) → \n ∀ (Raining Wet : Prop), (¬ Wet → ¬ Raining) → \n (Raining → Wet) :=\nbegin\nend\n\n\n/-\n9. [5 points]\n\nWhat is the name of the principle expressed by the\npremise, (P → Q) ↔ (¬ Q → ¬ P)), in the preceding\nproblem? Answer here:\n\n\n-/", "meta": {"author": "kevinsullivan", "repo": "uva-cs-dm-s19", "sha": "3e7177682acdb56a2d16914e0344c10335583dcf", "save_path": "github-repos/lean/kevinsullivan-uva-cs-dm-s19", "path": "github-repos/lean/kevinsullivan-uva-cs-dm-s19/uva-cs-dm-s19-3e7177682acdb56a2d16914e0344c10335583dcf/hw/hw7.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9525741281688026, "lm_q2_score": 0.9294404047899392, "lm_q1q2_score": 0.8853608832776353}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio 1. Realizar las siguientes acciones:\n-- 1. Importar la teoría de anillos.\n-- 2. Crear el espacio de nombres my_ring\n-- 3. Declarar R como una variable sobre anillos.\n-- 4. Declarar a y b como variables sobre R.\n-- ----------------------------------------------------------------------\n\nimport algebra.ring -- 1\nnamespace my_ring -- 2\nvariables {R : Type*} [ring R] -- 3\nvariables {a b : R} -- 4\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 2. Demostrar que si\n-- a + b = 0\n-- entonces\n-- -a = b\n-- ----------------------------------------------------------------------\n\ntheorem neg_eq_of_add_eq_zero\n (h : a + b = 0)\n : -a = b :=\ncalc\n -a = -a + 0 : by rw add_zero\n ... = -a + (a + b) : by rw h\n ... = b : by rw neg_add_cancel_left\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 3. Demostrar que\n-- (a + b) + -b = a\n-- ----------------------------------------------------------------------\n\n-- 1ª demostración\nexample : (a + b) + -b = a :=\ncalc (a + b) + -b = a + (b + -b) : by rw add_assoc\n ... = a + 0 : by rw add_right_neg\n ... = a : by rw add_zero\n\n-- 2ª demostración\nlemma neg_add_cancel_right : (a + b) + -b = a :=\nby rw [add_assoc, add_right_neg, add_zero]\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 4. Demostrar que si\n-- a + b = 0\n-- entonces\n-- a = -b\n-- ----------------------------------------------------------------------\n\ntheorem eq_neg_of_add_eq_zero\n (h : a + b = 0)\n : a = -b :=\ncalc\n a = (a + b) + -b : by rw neg_add_cancel_right\n ... = 0 + -b : by rw h\n ... = -b : by rw zero_add\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 5. Demostrar que\n-- -0 = 0\n-- ----------------------------------------------------------------------\n\ntheorem neg_zero : (-0 : R) = 0 :=\nbegin\n apply neg_eq_of_add_eq_zero,\n rw add_zero,\nend\n\n-- El desarrollo de la prueba es\n--\n-- R : Type u_1,\n-- _inst_1 : ring R\n-- ⊢ -0 = 0\n-- apply neg_eq_of_add_eq_zero,\n-- ⊢ 0 + 0 = 0\n-- rw add_zero,\n-- no goals\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 6. Demostrar que\n-- -(-a) = a\n-- ----------------------------------------------------------------------\n\ntheorem neg_neg : -(-a) = a :=\nbegin\n apply neg_eq_of_add_eq_zero,\n rw add_left_neg,\nend\n\n-- El desarrollo de la prueba es\n--\n-- R : Type u_1,\n-- _inst_1 : ring R,\n-- a : R\n-- ⊢ - -a = a\n-- apply neg_eq_of_add_eq_zero,\n-- ⊢ -a + a = 0\n-- rw add_left_neg,\n-- no goals\n\nend my_ring\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Basicos/Ejercicios_sobre_anillos.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.950410972802222, "lm_q2_score": 0.9304582550120769, "lm_q1q2_score": 0.8843177352978859}}
{"text": "/- LoVe Exercise 5: Inductive Predicates -/\n\nimport .love05_inductive_predicates_demo\n\nnamespace LoVe\n\n\n/- Question 1: Even and Odd -/\n\n/- The `even` predicate is true for even numbers and false for odd numbers. -/\n\n#check even\n\n/- 1.1. Prove that 0, 2, 4, and 6 are even. -/\n\nlemma even_0 : even 0 := even.zero\nlemma even_2 : even 2 := even.add_two _ even_0\nlemma even_4 : even 4 := even.add_two _ even_2\nlemma even_6 : even 6 := even.add_two _ even_4\n\n/- We define `odd` as the negation of `even`: -/\n\ndef odd (n : ℕ) : Prop :=\n ¬ even n\n\n/- 1.2. Prove that 1 is odd and register this fact as a `simp` rule.\n\nHint: `cases` is useful to reason about hypotheses of the form `even …`. -/\n\n@[simp] lemma odd_1 :\n odd 1 :=\nby intro h; cases h\n\n/- 1.3. Prove that 3, 5, and 7 are odd. -/\n\nexample : odd 3 := by intro h; cases h; cases h_a\nexample : odd 5 := by intro h; cases h; cases h_a; cases h_a_a\nexample : odd 7 := by intro h; cases h; cases h_a; cases h_a_a; cases h_a_a_a\n\n/- 1.4. Complete the following proof by structural induction.\n\nHint: You can rely implicitly on computation for the induction step. -/\n\nlemma even_two_times :\n ∀m : ℕ, even (2 * m)\n| 0 := even.zero\n| (m + 1) :=\n begin\n apply even.add_two,\n apply even_two_times\n end\n\n/- 1.5. Complete the following proof by rule induction.\n\nHint: You can use the `cases` tactic (or `match … with`) to destruct an\nexistential quantifier and extract the witness. -/\n\nlemma even_imp_exists_two_times :\n ∀n : ℕ, even n → ∃m, n = 2 * m\n| _ even.zero := exists.intro 0 (by simp)\n| _ (even.add_two n hen) :=\n begin\n cases even_imp_exists_two_times n hen,\n use w + 1,\n rw h,\n linarith\n end\n\n/- 1.6. Using `even_two_times` and `even_imp_exists_two_times`, prove the\nfollowing equivalence. -/\n\nlemma even_iff_exists_two_times (n : ℕ) :\n even n ↔ ∃m, n = 2 * m :=\nbegin\n apply iff.intro,\n { apply even_imp_exists_two_times },\n { intro h,\n cases h,\n simp *,\n apply even_two_times }\nend\n\n\n/- Question 2: Binary Trees -/\n\n/- 2.1. Prove the converse of `is_full_mirror`. You may exploit already proved\nlemmas (e.g., `is_full_mirror`, `mirror_mirror`). -/\n\nlemma mirror_is_full {α : Type} :\n ∀t : btree α, is_full (mirror t) → is_full t :=\nbegin\n intros t fmt,\n have fmmt : is_full (mirror (mirror t)) := is_full_mirror _ fmt,\n rw mirror_mirror at fmmt,\n assumption\nend\n\n/- 2.2. Define a function that counts the number of constructors (`empty` or\n`node`) in a tree. -/\n\ndef count {α : Type} : btree α → ℕ\n| empty := 1\n| (node _ l r) := count l + count r + 1\n\nend LoVe\n", "meta": {"author": "blanchette", "repo": "logical_verification_2019", "sha": "7c5f1c90f6e5a0b221a02c8fa4e36c0c66393036", "save_path": "github-repos/lean/blanchette-logical_verification_2019", "path": "github-repos/lean/blanchette-logical_verification_2019/logical_verification_2019-7c5f1c90f6e5a0b221a02c8fa4e36c0c66393036/lean/love05_inductive_predicates_exercise_solution.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9609517039189089, "lm_q2_score": 0.9184802462567085, "lm_q1q2_score": 0.8826151576562432}}
{"text": "import algebra.big_operators.basic\nimport tactic.interactive tactic.ring tactic.fin_cases\n\n/- Exercise 1\n Show that for every natural number n, there exists m with m > n\n\n Useful ingredients: \n nat.lt_succ_self; the tactics intro, use, apply\n-/\n\nlemma exists_greater_a : ∀ (n : ℕ), ∃ (m : ℕ), m > n := \nbegin\n intro n,\n use n + 1,\n apply nat.lt_succ_self,\nend\n\nlemma exists_greater_b (n : ℕ) : ∃ m, m > n := \n exists.intro (n + 1) n.lt_succ_self\n\n/- Exercise 2\n Show that if we append a list to itself, then the length doubles.\n\n Useful ingredients:\n list.length_append, two_mul\n-/ \n\nlemma double_length_a {α : Type} (l : list α) : \n (l ++ l).length = 2 * l.length := \nbegin\n rw[list.length_append l l],ring\nend\n\nlemma double_length_b {α : Type} (l : list α) : \n (l ++ l).length = 2 * l.length := \n (list.length_append l l).trans (two_mul l.length).symm\n\n/- Exercise 3\n Show that 2 * (0 + 1 + ... + n) = n * (n + 1)\n\n Useful ingredients:\n finset.sum_range, finset.sum_range_succ; the tactics\n induction, rw and ring\n-/\n\nlemma range_sum_a (n : ℕ) :\n 2 * ((finset.range (n + 1)).sum id) = n * (n + 1) := \nbegin\n induction n with n ih,\n {refl},\n {have e : nat.succ n = n + 1 := rfl,\n rw[finset.sum_range_succ id (n + 1),mul_add,ih,id,e],\n ring,\n }\nend\n\nlemma range_sum_b : ∀ n : ℕ,\n 2 * ((finset.range (n + 1)).sum id) = n * (n + 1)\n| 0 := rfl\n| (n + 1) := begin\n rw[finset.sum_range_succ id (n + 1),mul_add,range_sum_a n,id],\n ring,\nend\n\n/- Exercise 4\n Show that every natural number can be written in the form\n 2 * m or 2 * m + 1.\n\n This one seems harder than it should be. \n\n Useful ingredients: the operations n / 2 and n % 2, the theorems\n nat.mod_add_div and nat.mod_lt, the fin_cases tactic.\n\n For a different approach: the functions nat.bodd and nat.div2 \n and the theorem nat.bodd_add_div2\n-/\n\nlemma even_odd_a : ∀ (n : ℕ), (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) \n| 0 := or.inl ⟨0,rfl⟩ \n| 1 := or.inr ⟨0,rfl⟩ \n| (n + 2) := begin\n rcases even_odd_a n with ⟨m,e⟩ | ⟨m,e⟩; rw[e],\n {left,use m + 1,ring}, \n {right,use m + 1,ring}, \nend\n\nlemma even_odd_b (n : ℕ) : (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) :=\nbegin\n let r := n % 2,\n let m := n / 2,\n have h : n = 2 * m + r := (nat.mod_add_div n 2).symm.trans (add_comm _ _),\n have r_lt_2 : r < 2 := nat.mod_lt n dec_trivial,\n let r0 : fin 2 := ⟨r,r_lt_2⟩,\n fin_cases r0,\n {left,\n use m,\n have hr : r = 0 := fin.veq_of_eq this,\n rw[hr, add_zero] at h,\n exact h,\n },{\n right,\n use m,\n have hr : r = 1 := fin.veq_of_eq this,\n rw[hr] at h,\n exact h,\n },\nend\n\nlemma even_odd_c_aux (n m : ℕ) (b : bool) (e : (cond b 1 0) + 2 * m = n) : \n (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) :=\nbegin\n cases b, \n {left ,exact ⟨m,(e.symm.trans (zero_add _))⟩,},\n {right,exact ⟨m,e.symm.trans (add_comm _ _)⟩,},\nend\n\nlemma even_odd_c (n : ℕ) : (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) :=\n even_odd_c_aux n n.div2 (nat.bodd n) (nat.bodd_add_div2 n)\n\n/- Exercise 5\n Given propositions p and q, show that p → q is equivalent to\n p → (p → q)\n\n Useful ingredients: \n The tactics split, intros and exact. \n\n Alternatively, you can use the tauto tactic, which will do \n all the work for you.\n-/\n\nlemma ppq_a (p q : Prop) : (p → q) ↔ (p → p → q) := \nbegin\n split,\n {intros hpq hp _,exact hpq hp,},\n {intros hppq hp,exact hppq hp hp,}\nend\n\nlemma ppq_b (p q : Prop) : (p → q) ↔ (p → p → q) := \n ⟨λ hpq hp _,hpq hp,λ hppq hp,hppq hp hp⟩\n\nlemma ppq_c (p q : Prop) : (p → q) ↔ (p → p → q) := \n by tauto\n\n/- Exercise 6\n If G is a group where a^2 = 1 for all a, then G is commutative.\n\n Proof: for any a and b we have a^2 = 1 and b^2 = 1 and (ab)^2 = 1.\n Write the last of these as abab = 1, multiply on the left by\n a and on the right by b to get aababb = ab. Cancel aa = 1 and\n bb = 1 to get ba = ab.\n\n Useful ingredients: \n pow_two, calc, rw, mul_assoc, one_mul, mul_one\n-/\n\nlemma elem_ab_a {G : Type*} [group G] (h : ∀ a : G, a ^ 2 = 1) : \n ∀ a b : G, a * b = b * a := \nbegin\n intros a b,\n let ea : a * a = 1 := (pow_two a).symm.trans (h a),\n let eb : b * b = 1 := (pow_two b).symm.trans (h b),\n let eab : (a * b) * (a * b) = 1 :=\n (pow_two (a * b)).symm.trans (h (a * b)),\n exact calc\n a * b = (a * 1) * b : by rw[mul_one]\n ... = (a * ((a * b) * (a * b))) * b : by rw[← eab]\n ... = ((a * (a * b)) * (a * b)) * b : by rw[← (mul_assoc a (a * b) (a * b))]\n ... = (b * (a * b)) * b : by rw[← (mul_assoc a a b),ea,one_mul b]\n ... = b * (a * (b * b)) : by rw[mul_assoc b (a * b) b,mul_assoc a b b]\n ... = b * a : by rw[eb,mul_one a]\nend\n\n/- Exercise 7\n Encapsulate Exercise 6 as a typeclass instance\n-/\n\nclass elementary_two_group (G : Type*) extends (group G) := \n (square_one : ∀ a : G, a ^ 2 = 1)\n\ninstance elementary_two_group_commutes\n (G : Type*) [e : elementary_two_group G] : comm_group G := \n { mul_comm := elem_ab_a e.square_one,\n ..e\n }\n\n/- Exercise 8\n Define an inductive type of nonempty lists \n (which we will call pos_list). \n \n The basic structure will be similar to the definition of \n arbitrary lists, which can be done like this:\n\n inductive list (T : Type)\n| nil : list\n| cons : T → T → list\n\n However, instead of the constructor `nil`, which constructs the\n empty list, we should have a constructor called `const`, which\n constructs a list of length one. \n\n We can then give inductive definitions of functions that give \n the first and last element of a nonempty list, the length \n (normalised so that lists with one entry count as having length\n zero), the reverse and so on.\n-/\n\ninductive pos_list (α : Type) : Type \n| const : α → pos_list\n| cons : α → pos_list → pos_list\n\nnamespace pos_list\n\nvariable {α : Type}\n\ndef length : pos_list α → ℕ \n| (const a) := 0\n| (cons a p) := p.length.succ\n\ndef to_list : pos_list α → list α \n| (const a) := [a]\n| (cons a p) := list.cons a (to_list p)\n\ndef head : pos_list α → α \n| (const a) := a\n| (cons a p) := a\n\ndef foot : pos_list α → α \n| (const a) := a\n| (cons a p) := p.foot\n\ndef append : pos_list α → pos_list α → pos_list α\n| (const a) q := cons a q\n| (cons a p) q := cons a (append p q)\n\nlemma head_append : ∀ (p q : pos_list α),\n head (append p q) = head p\n| (const a) q := rfl\n| (cons a p) q := rfl\n\nlemma foot_append : ∀ (p q : pos_list α),\n foot (append p q) = foot q\n| (const a) q := rfl\n| (cons a p) q := foot_append p q\n\nlemma append_assoc : ∀ (p q r : pos_list α), \n append (append p q) r = append p (append q r)\n| (const a) q r := rfl\n| (cons a p) q r := by {dsimp[append],rw[append_assoc p q r],}\n\ndef reverse : pos_list α → pos_list α \n| (const a) := const a\n| (cons a p) := p.reverse.append (const a)\n\nlemma head_reverse : ∀ (p : pos_list α), p.reverse.head = p.foot\n| (const a) := rfl\n| (cons a p) := by {dsimp[reverse,foot],rw[head_append,head_reverse p],}\n\nlemma foot_reverse : ∀ (p : pos_list α), p.reverse.foot = p.head\n| (const a) := rfl\n| (cons a p) := by {dsimp[reverse,head],rw[foot_append],refl,}\n\nlemma reverse_append : ∀ (p q : pos_list α),\n reverse (append p q) = append (reverse q) (reverse p) \n| (const a) q := rfl\n| (cons a p) q := by {dsimp[append,reverse],rw[reverse_append p q,append_assoc],}\n\nlemma reverse_reverse : ∀ (p : pos_list α), p.reverse.reverse = p\n| (const a) := rfl\n| (cons a p) := begin \n dsimp[reverse],rw[reverse_append,reverse_reverse p],refl,\nend\n\nend pos_list\n\n/- Exercise 9\n Set up some basic theory of graphs. In more detail, suppose\n we have a type V (to be considered as the vertex set). To \n make this a graph, we should have a predicate E so that E a b\n is true if there is an edge from a to b. We are considering\n simple undirected graphs with no loops, to this relation should\n be symmetric (ie E a b implies E b a) and irreflexive (so \n E a a is always false).\n \n Then define a type of paths in a graph. We can start by \n defining a predicate `is_path` on nonempty lists of vertices, \n which checks whether adjacent vertices in the list are linked\n by an edge. Using this, we can define paths as a subtype of\n nonempty lists.\n\n Now define a relation `are_connected` on V, which is satisfied\n by a pair of vertices if there is a path between them. Prove \n that this is an equivalence relation. \n-/\n\nclass graph (V : Type) : Type := \n(edge : V → V → Prop)\n(symm : ∀ {u v : V}, edge u v → edge v u)\n(asymm : ∀ v : V, ¬ edge v v)\n\nnamespace graph\n\nvariables {V : Type} [graph V]\n\ndef is_path : pos_list V → Prop\n| (pos_list.const _) := true\n| (pos_list.cons v p) := (edge v p.head) ∧ is_path p\n\nlemma is_path_append (p q : pos_list V) : \n is_path (pos_list.append p q) ↔ \n is_path p ∧ edge p.foot q.head ∧ is_path q := \nbegin\n induction p with a a p ih,\n {dsimp[is_path,pos_list.foot,pos_list.append,is_path],\n rw[true_and]\n },\n {dsimp[pos_list.append,pos_list.foot,is_path],\n rw[pos_list.head_append,ih,and_assoc],\n }\nend\n\nlemma is_path_reverse : ∀ {p : pos_list V},\n is_path p → is_path p.reverse \n| (pos_list.const a) _ := by {dsimp[pos_list.reverse,is_path],trivial}\n| (pos_list.cons a p) h := begin\n dsimp[pos_list.reverse,is_path],\n dsimp[is_path] at h,\n rw[is_path_append,pos_list.head,pos_list.foot_reverse,is_path,and_true],\n exact ⟨is_path_reverse h.right,symm h.left⟩, \nend\nvariable (V)\n\ndef path : Type := { p : pos_list V // is_path p }\n\nvariable {V}\n\ndef path_between (a b : V) : Type := \n { p : pos_list V // is_path p ∧ p.head = a ∧ p.foot = b }\n\nnamespace path \n\ndef head (p : path V) : V := p.val.head\n\ndef foot (p : path V) : V := p.val.foot\n\ndef length (p : path V) : ℕ := p.val.length\n\ndef cons (v : V) (p : path V) (h : edge v p.head) : path V := \n ⟨pos_list.cons v p.val,⟨h,p.property⟩⟩ \n\ndef const (v : V) : path_between v v := \n ⟨pos_list.const v,⟨trivial,rfl,rfl⟩⟩ \n\ndef reverse {u v : V} : ∀ (p : path_between u v), path_between v u\n| ⟨p,⟨p_path,p_head,p_foot⟩⟩ := \n ⟨p.reverse,⟨is_path_reverse p_path,\n (pos_list.head_reverse p).trans p_foot,\n (pos_list.foot_reverse p).trans p_head⟩⟩ \n\ndef splice {u v w : V} :\n ∀ (p : path_between u v) (q : path_between v w), \n path_between u w \n| ⟨p,⟨p_path,p_head,p_foot⟩⟩ ⟨pos_list.const x,⟨q_path,q_head,q_foot⟩⟩ :=\n ⟨p,begin \n change x = v at q_head,\n change x = w at q_foot,\n replace p_foot := p_foot.trans (q_head.symm.trans q_foot),\n exact ⟨p_path,p_head,p_foot⟩\n end⟩\n| ⟨p,⟨p_path,p_head,p_foot⟩⟩ ⟨pos_list.cons x q,⟨q_path,q_head,q_foot⟩⟩ := \n ⟨pos_list.append p q,\n begin \n change x = v at q_head,\n change q.foot = w at q_foot,\n rw[pos_list.head_append,p_head,pos_list.foot_append,q_foot],\n split, \n {dsimp[is_path] at q_path,rw[is_path_append,p_foot,← q_head],\n exact ⟨p_path,q_path⟩},\n {split;refl,}\n end\n\n\ninstance are_connected : setoid V := {\n r := λ a b, nonempty (path_between a b),\n iseqv := ⟨\n λ a, nonempty.intro (const a),\n λ a b ⟨p⟩, nonempty.intro (reverse p),\n λ a b c ⟨p⟩ ⟨q⟩, nonempty.intro (splice p q)\n\n}\n\nend path\n\nend graph\n\n/-\n Exercise 10\n\n Construct an equivalence `ℕ ≃ E ⊕ O`, where `E` and `O` are the\n subtypes of even and odd natural numbers. Here `E ⊕ O` is\n alternative notation for `sum E O`, which is defined in the core\n lean library to be the disjoint union of E and O. \n\n Equivalences are defined in data.equiv: an equivalence from X to \n Y is a structure with four members: functions \n `to_fun : X → Y` and `inv_fun : Y → X`, and proofs that `inv_fun`\n is both left inverse and right inverse to `to_fun`.\n-/\n\ndef E := {n : ℕ // n.bodd = ff}\n\ndef O := {n : ℕ // n.bodd = tt}\n\ndef bool_elim {C : Sort*} {b : bool} : b ≠ ff → b ≠ tt → C := \n by {intros,cases b;contradiction}\n\ndef f (n : ℕ) : E ⊕ O := \nbegin\n by_cases n_even : n.bodd = ff,\n {exact sum.inl ⟨n,n_even⟩},\n by_cases n_odd : n.bodd = tt,\n {exact sum.inr ⟨n,n_odd⟩},\n exact bool_elim n_even n_odd\nend\n\ndef g : E ⊕ O → ℕ \n| (sum.inl n) := n.val\n| (sum.inr n) := n.val\n\nlemma fg : ∀ x : E ⊕ O, f (g x) = x \n| (sum.inl ⟨n,n_even⟩) := by {dsimp[g,f],rw[dif_pos n_even]}\n| (sum.inr ⟨n,n_odd⟩) := \n begin\n have n_not_even : n.bodd ≠ ff := \n by {rw[n_odd],intro e,injection e,},\n dsimp[g,f],\n rw[dif_neg n_not_even,dif_pos n_odd],\n end \n\nlemma gf (n : ℕ) : g (f n) = n := \nbegin\n dsimp[f],\n by_cases n_even : n.bodd = ff,\n {rw[dif_pos n_even],refl},\n by_cases n_odd : n.bodd = tt,\n {have n_not_even : n.bodd ≠ ff := \n by {rw[n_odd],intro e,injection e,},\n rw[dif_neg n_not_even,dif_pos n_odd],\n refl,\n },\n exact bool_elim n_even n_odd\nend\n\ndef F : ℕ ≃ (E ⊕ O) := ⟨f,g,gf,fg⟩\n", "meta": {"author": "NeilStrickland", "repo": "lean_lib", "sha": "6a9563de93748ace509d9db4302db6cd77d8f92c", "save_path": "github-repos/lean/NeilStrickland-lean_lib", "path": "github-repos/lean/NeilStrickland-lean_lib/lean_lib-6a9563de93748ace509d9db4302db6cd77d8f92c/src/exercises/misc_exercices_1.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9458012762876287, "lm_q2_score": 0.9314625131514056, "lm_q1q2_score": 0.8809784337526815}}
{"text": "import tactic.linarith\n\nnamespace mth1001\n\n/-\nThe following is a Lean proof that `10 < 20`.\nThe line `example : 10 < 20` can be read as, 'Here is a proof of `10 < 20`.\n\nThe line `by norm_num` says, 'the result is proved using `norm_num`, a 'tactic'\nthat can prove many numerical equations and inequalities.\n\nYou know the proof is valid because there are no error messages in the\nLean Infoview window and no red wavy text.\n-/\n\nexample : 10 < 20 :=\nby norm_num\n\n/-\nIn standard mathematical language (henceforth 'standard maths'), the following can be read as\n\n `6 ≥ 2` follows using known numerical equations or inequalities.\n\nNote: `≥` is written `\\ge`. Can you guess how to write `≤`?\n-/\nexample : 6 ≥ 2 := by norm_num\n\n\n/-\nWrite the following in standard maths. Note: `≠` is written `\\ne`.\n-/\nexample : 15 ≠ 10 :=\nby norm_num\n\n-- Play around with this. Come up with your own examples similar to those above.\n-- See what happens if you try to use `norm_num` to prove something false, like `1 = 0`.\n\n/-\nLet's make things interesting by introducing variables. Here, `ℤ` is the 'type' of integers and is \nwritten `\\Z`. The following command introduces variables `x` and `y` of type `ℤ`.\n-/\n\nvariables (x y : ℤ)\n\n/-\nThe `linarith` tactic can prove many equations and inequalities involving\nvariables. The standard maths tranlation of the following proof is:\n\n For every integer `x`, `x ≥ x` follows using known linear equations and inequalities.\n\n-/\nexample : x ≥ x :=\nby linarith\n\n/-\nWe can suppress error messages by writing `sorry` where Lean expects a proof.\nLean warns us that the proof is not complete with a yellow wavy line and /or a warning message\nin the Infoview window.\n-/\n\n-- Exercise 001:\n-- Replace `sorry` with `by linarith` in the following example. Write the example in standard maths.\nexample : y + 5 ≥ y + 3 :=\nsorry \n\n/-\n`linarith` can only handle _linear_ equations and inequalities --- those involving no quadratic or\nhigher-order terms. The `ring` tactic can simplify many other algebraic expressions.\n\nThe following states (in standard maths):\n\n For every integer `y`, `(y + 2)^2 = y^2 + 4*y + 4` follows by algebra.\n\nNote: `^` represent exponentiation. Do not confuse it with `∧`, which you'll see later.\n-/\nexample : (y + 2)^2 = y^2 + 4*y + 4 :=\nby ring\n\n\n/-\nSome of the claims in the next few examples are true while others are false!\nDetermine, by hand, which are which. Test your guesses by using either the\n`linarith` or the `ring` tactic to replace the sorry.\n\nFor each true statement, write out the example in standard maths.\n-/\n\n-- Exercise 002:\n-- Is the following true or false?\nexample : (x + y)^2 = x^2 + y^2 :=\nsorry\n\n-- Exercise 003:\n-- Is the following true or false?\nexample : (x - y) * (x + y) = x^2 - y^2 :=\nsorry \n\n-- Exercise 004:\n-- Is the following true or false?\nexample : 3 * x + 8 ≥ 5 * x :=\nsorry \n\n/-\nThe previous example may cause some confusion. This isn't a question of *finding* the values of `x`\nfor which the equality `3 * x + 8 ≥ 5 * x` is true. Rather the problem is, 'Can you\nprove, *for every* value of `x`, that `3 * x + 8 ≥ 5 * x`? If so, give a proof!'.\n-/\n\n/-\nOf course, there *are* values of `x` for which the inequality holds. You can\neasily that the inequality holds if `x ≤ 4`. In Lean, we write `h : x ≤ 4`\nto mean, 'let `h` denote the premise (or assumption or hypothesis) that `x ≤ 4`'.\n\nThere's nothing special about `h`, we could instead write `k : x ≤ 4`,\n`jane : x ≤ 4`, or `hal9000 : x ≤ 4`.\n-/\n\n/-\nIn English, we write the result below as, 'Given the premise `k : x ≤ 4`, the\ninequality `3 * x + 8 ≥ 5 * x` follows'.\n\nIn normal mathematical writing, we do not label premises. Thus, we could write,\n'Given `x ≤ 4`, the inequality `3 * x + 8 ≥ 5 * x` follows'.\n-/\n\nexample (k : x ≤ 4) : 3 * x + 8 ≥ 5 * x :=\nby linarith\n\n/-\nIf `x ≤ 0`, then also `x ≤ 4`, so we should expect to get the conclusion of the above example from\nthis new premise.\n-/\n\nexample (k : x ≤ 0) : 3 * x + 8 ≥ 5 * x :=\nby linarith\n\n/-\nMore generally, the result is true with any stronger premise. That is, suppose we assert\n`k₁ : x ≤ y` together with `k₂ : y ≤ 4`. It then follows that `x ≤ 4` and we recover the result.\n\nHere, we type `k\\1` for `k₁`.\n-/ \n\nexample (k₁ : x ≤ y) (k₂ : y ≤ 4) : 3 * x + 8 ≥ 5 * x :=\nby linarith\n\n/-\nWhat's special about `4` is that it's the *largest* integer with this property. We'll consider\nhow to formalise this notion in a later section.\n-/\n\n-- Exercise 005:\n/- Complete the following statement and proof. Ensure you fill in the first `sorry` with\nthe *smallest* integer value that works.\n-/\nexample (h : x ≥ sorry) : 9 * x + 6 ≥ 2 * x + 20 := \nsorry \n\n-- Exercise 006:\n/- Complete the following statement and proof. Fill in the first `sorry` with the *largest*\ninteger value that works. Can you prove (by hand) *why* this is the largest value?\n-/\nexample (h₁ : -3 * x + 2 * y ≤ 2 ) (h₂ : 5 * x + y ≥ sorry): x ≥ 6 := \nsorry \n\n/-\nSUMMARY:\n\nIn this file, you have learned about:\n\n* Statements of results in Lean, in the form `example premise₁ … premiseₙ : conclusion`.\n* Translating between Lean and standard maths.\n* The idea of a premise, also known as a hypothesis or assumption. Lean notation for premises.\n* One-line tactic-style proofs, using `by`.\n* Variables in Lean, using `variables`.\n* The `norm_num` tactic for proving numerical equations and inequalities.\n* The `linarith` tactic for proving linear equations and inequalities (involving variables).\n* The `ring` tactic for simplifying more general equations.\n* Writing subscripts in Lean.\n-/\n\nend mth1001\n", "meta": {"author": "gihanmarasingha", "repo": "mth1001_tutorial", "sha": "bb277eebd5013766e1418365b91416b406275130", "save_path": "github-repos/lean/gihanmarasingha-mth1001_tutorial", "path": "github-repos/lean/gihanmarasingha-mth1001_tutorial/mth1001_tutorial-bb277eebd5013766e1418365b91416b406275130/src/exercises/src_00_introduction_to_formal_maths.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9489172601537142, "lm_q2_score": 0.9273633001635968, "lm_q1q2_score": 0.8799910419583468}}
{"text": "-- In propositions as types paradigm, theorems involving only → can be proved using \n-- lambda abstraction and application.\n-- In Lean the theorem command introduces a new theorem \n\nconstants p q : Prop \n \ntheorem t1 : p → q → p := λ hp : p, λ hq : q, hp\n\n-- This looks like the definition of a constant the only difference is that instead of Type\n-- we have Prop. Intuitively our proof of p → q → p assumes p and q are true, and uses \n-- the first hypothesis (trivially) to establish that the consclusion p is true\n\n-- Lean tags proofs as irreducible, which serves as a hint to the parser (more precisely, the elaborator)\n-- that there is generally no need to unfold it when processing a file\n\n-- Lean is generally able to process and check proofs in parallel, \n--since assessing the correctness of one proof does not require knowing the details of another.\n\n-- As with definitions, the #print command will show you the proof of a theorem.\n#print t1\n\n\n-- Notice that lambda abstractions hp : p and hq : q can be viewed as temporary assumptions \n-- in the proof of t1. Lean provides an alternative syntax assume for such lambda abstraction \n\ntheorem t1' : p → q → p :=\nassume hp : p,\nassume hq : q,\nhp\n\n-- Lean also allows us to specify the type of the final term hp, explicitly, with show statement\ntheorem t1'' : p → q → p :=\nassume hp : p,\nassume hq : q, \nshow p, from hp\n\n-- The show command does nothing more than annotate the type, and,\n-- internally, all the presentations of t1 that we have seen produce the same term\n\n-- Lean also allows you to use the alternative syntax lemma instead of theorem \nlemma t1''' : p → q → p := \nassume hp : p,\nassume hq : q,\nshow p, from hp\n\n\n-- As with ordinary definitions, we can move the lambda-abstracted variable to the left of the colon\ntheorem t1'''' (hp : p) (hq : q) : p := hp\n\n#check t1''''\n\n-- Now we can apply the theorem t1 just as a function application\naxiom hp : p\ntheorem t2 : q → p := t1 hp\n\n-- Here, the axiom command is alternative syntax for constant. \n-- Declaring a “constant” hp : p is tantamount to declaring that p is true, as witnessed by hp.\n\n-- Applying the theorem t1 : p → q → p to the fact hp : p that p is true yields the theorem t2 : q → p\n\n\n-- t1 is true for any propositions p and q, \ntheorem t1''''' (p q : Prop) (hp : p) (hq : q) : p := hp\n#check t1'''''\n\n-- he symbol ∀ is alternate syntax for Π, and\n-- later we will see how Pi types let us model universal quantifiers more generally.\n\n-- we can move all parameters to the right of the colon:\ntheorem t1'''''' : ∀ (p q : Prop), p → q → p := \nλ (p q: Prop) (hp: p) (hq : q), hp\n\n-- If p and q are declared variables, Lean will generalize them automatically \n\nvariables p q : Prop \ntheorem t1''''''': p → q → p := λ (hp : p) (hq : q), hp\n\n-- By the proposition-as-types correspondence we can declare the assumption hp that p\n-- holds as another variable \nvariable hp : p\n\ntheorem t1'''''''' : q → p := λ (hq : q), hp\n\n#check t1''''''''\n\n-- We can also write the above as ∀ (p q : Prop) (hp : p) (hq :q), p, \n-- since the arrow denotes nothing more than a Pi type in which the target does not depend on \n-- the bound variable \n\n-- When we generalize t1 in such a way, we can apply it to different pairs of propositions,\n-- to obtain different instances of the general theorem \n\ntheorem t1''''''''' (p q : Prop) (hp : p) (hq : q) :p := hp\n\nvariables r s : Prop \n\n#check t1''''''''' p q\n#check t1''''''''' r s\n#check t1''''''''' (r → s) (r → s)\n\nvariable h : r → s\n#check t1''''''''' (r → s) (s → r) h\n\n\n-- Let's now consider the composition function now with propositions instead of types\n\nvariables p' q' r' s' : Prop\n\ntheorem t2' (h₁ : q' → r') (h₂ : p' → q'): p' → r' :=\nassume h₃ : p',\nshow r', from h₁ (h₂ h₃)\n\n#check t2'", "meta": {"author": "0x-Inf", "repo": "theorem_proving_in_lean", "sha": "ffba957d08381aa846eb33cd1c855dd30eaca52f", "save_path": "github-repos/lean/0x-Inf-theorem_proving_in_lean", "path": "github-repos/lean/0x-Inf-theorem_proving_in_lean/theorem_proving_in_lean-ffba957d08381aa846eb33cd1c855dd30eaca52f/code_examples/part3/working_w_propositions_as_types.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9553191335436405, "lm_q2_score": 0.9207896737173119, "lm_q1q2_score": 0.8796479932715539}}
{"text": "/-\nThis in-class exercise requires solutions to two problems\n-/\n\n/-\nPROBLEM #1. \n\nIn Lean, define pf1 to be a proof of the proposition\nthat, \"for any proposition, Q, ¬ (Q ∧ ¬ Q). We write\nthis as ∀ Q : Prop, ¬ (Q ∧ ¬ Q). In English it says,\nit's not possible for any proposition to be both true\nand false. \n\nThere are at least three forms a solution can take.\n\n#1. You can prove the proposition by declaring\npf1 to be a proof (value) of the proposition/type\nas written, then use a lambda expression to give\nthe actual proof term. Here we give you the first\nline of the lambda, which you can read as either\n(A) \"a function that takes a proposition Q as an\nargument and that returns ...\", or as (B) \"assume\nQ is some proposition and then return ...\" You \nhave to fill in the _. You know of course that \nit could be another lambda expression.\n\ntheorem pf1 : ∀ Q : Prop, ¬ (Q ∧ ¬ Q) :=\n λ (Q : Prop),\n _\n\n#2: You can use an equivalent tactic script. It \nwill start like this:\n\ntheorem pf1' : ∀ Q : Prop, ¬ (Q ∧ ¬ Q) :=\nassume Q : Prop,\n...\n\nYou have to fill in the rest in place of the\nelipsis.\n\n#3: You can prove the theorem by writing an\nordinary function. It'd start like this:\n\ntheorem pf1'' (Q : Prop) (pfQnQ : Q ∧ ¬ Q) : false :=\n_\n\nAgain you have to replace the _ with \"code\"\nthat constructs the required proof.\n\nFor extra credit, give the answer in all three\nforms.\n\nHere are some extra hints.\n\nHint #1: Remember that, assuming that A is any\nproposition (such as Q ∧ ¬ Q), ¬ A simply means \n(A → false). So what does ¬ (Q ∧ ¬ Q) mean? That\nis the proposition you need to prove, and its \nform will tell you a lot about what for a proof\nhas to have.\n\nHint #2: Hover your mouse cursor over the underscores\nin the preceding code. That will tell you exactly \nwhat proof you need to fill in in place of the _.\n\nHint #3: (Q ∧ ¬ Q) → false is an implication. \nRemember what a proof of an implication looks like. \nUse a corresponding expression in place of the _.\nYes, we're repeating the same hint as before but\nusing other words.\n\nHint #4: Remember the elimination rules for ∧. If\nyou assume you have a proof of (Q ∧ ¬ Q) what two\nsmaller proofs can you derive from it?\n\nHint #5: Remember again: ¬ Q means (Q → false).\nIf you have a proof of Q → false, then you have\na function that, if it is given an assumed proof\nof Q, reduces it to a proof of false. Look for a\nway to get this function and then to apply it to\na proof of Q!\n-/\n\n/- SOLUTION #1 -/\n\ntheorem pf1 : ∀ Q : Prop, ¬ (Q ∧ ¬ Q) :=\n λ (Q : Prop) (q : (Q ∧ ¬ Q)),\n q.2 q.1\n\n/- SOLUTION #2 -/\n\ntheorem pf1' : ∀ Q : Prop, ¬ (Q ∧ ¬ Q) :=\n begin\n assume Q : Prop,\n assume q : (Q ∧ ¬ Q),\n show false,\n from q.2 q.1\n end\n\n/- SOLUTION #3 -/\n\ntheorem pf1'' (Q : Prop) (pfQnQ : Q ∧ ¬ Q) : false :=\n begin\n have pfQ := and.elim_left pfQnQ,\n have pfnQ := and.elim_right pfQnQ,\n exact pfnQ pfQ\n end\n/-\nPROBLEM #2.\n\nProduce a proof, pf2, of the proposition, that \nfor any propositions, P and Q, (P ∧ Q) ∧ (P ∧ ¬ Q) \n→ false. (You could of course also write this as\n¬ ((P ∧ Q) ∧ (P ∧ ¬ Q)). You can use the partial \nsolutions we gave for the last problem as models. \nStart by assuming (P Q : Prop) rather than just\nQ : Prop. Hint: Remember the and elimination rules.\nLook for ways to extract from the assumed proof\nthe other proofs that you'll need to combine in\nsome way to construct a proof of false. You may\nprovide your answer in any of the three forms we\ndiscussed. For extra credit, provide it in all\nthree forms.\n-/\n\n/- #1 -/\n\ntheorem pf2 : \n ∀ P Q : Prop, (P ∧ Q) ∧ (P ∧ ¬ Q) → false :=\n λ (P Q : Prop),\n λ (p : (P ∧ Q) ∧ (P ∧ ¬ Q)),\n (p.2).2 (p.1).2\n/- #2 -/\n\ntheorem pf2' : \n ∀ P Q : Prop, (P ∧ Q) ∧ (P ∧ ¬ Q) → false :=\n begin\n assume P Q : Prop,\n assume p : (P ∧ Q) ∧ (P ∧ ¬ Q),\n show false,\n from (p.2).2 (p.1).2\n end \n\n/- #3 -/\n\ntheorem pf1''' (P Q : Prop) (pf : (P ∧ Q) ∧ (P ∧ ¬ Q)) : false :=\n begin\n have pfL := and.elim_left pf,\n have pfR := and.elim_right pf,\n have pfQ := and.elim_right pfL,\n have pfnQ := and.elim_right pfR,\n exact pfnQ pfQ\n end\n", "meta": {"author": "Mnormansell", "repo": "Discrete-Notes", "sha": "61f13b98be590269fc4822be7b47924a6ddc1261", "save_path": "github-repos/lean/Mnormansell-Discrete-Notes", "path": "github-repos/lean/Mnormansell-Discrete-Notes/Discrete-Notes-61f13b98be590269fc4822be7b47924a6ddc1261/HW/in-class-quiz-pm1.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9579122768904644, "lm_q2_score": 0.9149009509324104, "lm_q1q2_score": 0.8763948530369163}}
{"text": "import data.real.basic\n\n/-\nIn the previous file, we saw how to rewrite using equalities.\nThe analogue operation with mathematical statements is rewriting using\nequivalences. This is also done using the `rw` tactic.\nLean uses ↔ to denote equivalence instead of ⇔.\n\nIn the following exercises we will use the lemma:\n\n sub_nonneg {x y : ℝ} : 0 ≤ y - x ↔ x ≤ y\n\nThe curly braces around x and y instead of parentheses mean Lean will always try to figure out what\nx and y are from context, unless we really insist on telling it (we'll see how to insist much later).\nLet's not worry about that for now.\n\nIn order to announce an intermediate statement we use:\n\n have my_name : my statement,\n\nThis triggers the apparition of a new goal: proving the statement. After this is done,\nthe statement becomes available under the name `my_name`.\nWe can focus on the current goal by typing tactics between curly braces.\n-/\n\nexample {a b c : ℝ} (hab : a ≤ b) : c + a ≤ c + b :=\nbegin\n rw ← sub_nonneg,\n have key : (c + b) - (c + a) = b - a, -- Here we introduce an intermediate statement named key\n { ring, }, -- and prove it between curly braces\n rw key, -- we can now use the key statement\n rw sub_nonneg,\n exact hab,\nend\n\n/-\nOf course the previous lemma is already in the core library, named `add_le_add_left`, so we can use it below.\n\nLet's prove a variation (without invoking commutativity of addition since this would spoil our fun).\n-/\n\n-- 0009\nexample {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c :=\nbegin\n rw ←sub_nonneg,\n have key : (b + c) - (a + c) = b - a, ring,\n rw key,\n rwa sub_nonneg,\nend\n\n\n/-\nLet's see how we could use this lemma. It is already in the core library, under the name `add_le_add_right`:\n\n add_le_add_right {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c\n\nThis can be read as: \"add_le_add_right is a function that will take as input real numbers a and b, an\nassumption `hab` claiming a ≤ b and a real number c, and will output a proof of a + c ≤ b + c\".\n\nIn addition, recall that curly braces around a b mean Lean will figure out those arguments unless we\ninsist to help. This is because they can be deduced from the next argument `hab`.\nSo it will be sufficient to feed `hab` and c to this function.\n-/\n\nexample {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : by exact add_le_add_right ha b,\nend\n\n/-\nIn the second line of the above proof, we need to prove 0 + b ≤ a + b.\nThe proof after the colon says: this is exactly lemma `add_le_add_right` applied to ha and b.\nActually the `calc` block expects proof terms, and the `by` keyword is used to tell Lean we will use tactics\nto build such a proof term. But since the only tactic used in this block is `exact`, we can skip\ntactics entirely, and write:\n-/\n\nexample (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : add_le_add_right ha b,\nend\n\n/- Let's do a variant. -/\n\n-- 0010\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n calc\n a = a + 0 : by ring\n ... ≤ a + b : add_le_add_left hb a,\nend\n\n/-\nThe two preceding examples are in the core library :\n\n le_add_of_nonneg_left {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b\n le_add_of_nonneg_right {a b : ℝ} (hb : 0 ≤ b) : a ≤ a + b\n\nAgain, there won't be any need to memorize those names, we will\nsoon see how to get rid of such goals automatically.\nBut we can already try to understand how their names are built:\n\"le_add\" describe the conclusion \"less or equal than some addition\"\nIt comes first because we are focussed on proving stuff, and\nauto-completion works by looking at the beginning of words.\n\"of\" introduces assumptions. \"nonneg\" is Lean's abbreviation for non-negative.\n\"left\" or \"right\" disambiguates between the two variations.\n\nLet's use those lemmas by hand for now.\n-/\n\n-- 0011\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n calc 0 ≤ a : ha\n ... ≤ a + b : le_add_of_nonneg_right hb,\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0012\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d :=\nbegin\n calc a + c ≤ b + c : add_le_add_right hab c\n ... ≤ b + d : add_le_add_left hcd b,\nend\n\n/-\nIn the above examples, we prepared proofs of assumptions of our lemmas beforehand, so\nthat we could feed them to the lemmas. This is called forward reasonning.\nThe `calc` proofs also belong to this category.\n\nWe can also announce the use of a lemma, and provide proofs after the fact, using\nthe `apply` tactic. This is called backward reasonning because we get the conclusion\nfirst, and provide proofs later. Using `rw` on the goal (rather than on an assumption\nfrom the local context) is also backward reasonning.\n\nLet's do that using the lemma\n\n mul_nonneg' {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : 0 ≤ x*y\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n have key : b*c - a*c = (b - a)*c,\n { ring },\n rw key,\n apply mul_nonneg, -- Here we don't provide proofs for the lemma's assumptions\n -- Now we need to provide the proofs.\n { rw sub_nonneg,\n exact hab },\n { exact hc },\nend\n\n/-\nLet's prove the same statement using only forward reasonning: announcing stuff,\nproving it by working with known facts, moving forward.\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rw ← sub_nonneg at hab,\n exact hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rw h₂ at h₁,\n exact h₁, },\n rw sub_nonneg at h₃,\n exact h₃,\nend\n\n/-\nOne reason why the backward reasoning proof is shorter is because Lean can\ninfer of lot of things by comparing the goal and the lemma statement. Indeed\nin the `apply mul_nonneg'` line, we didn't need to tell Lean that x = b - a\nand y = c in the lemma. It was infered by \"unification\" between the lemma\nstatement and the goal.\n\nTo be fair to the forward reasoning version, we should introduce a convenient\nvariation on `rw`. The `rwa` tactic performs rewrite and then looks for an\nassumption matching the goal. We can use it to rewrite our latest proof as:\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rwa ← sub_nonneg at hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rwa h₂ at h₁, },\n rwa sub_nonneg at h₃,\nend\n\n/-\nLet's now combine forward and backward reasonning, to get our most\nefficient proof of this statement. Note in particular how unification is used\nto know what to prove inside the parentheses in the `mul_nonneg'` arguments.\n-/\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n calc 0 ≤ (b - a)*c : mul_nonneg (by rwa sub_nonneg) hc\n ... = b*c - a*c : by ring,\nend\n\n/-\nLet's now practice all three styles using:\n\n mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b\n\n sub_nonpos {a b : α} : a - b ≤ 0 ↔ a ≤ b\n-/\n\n/- First using mostly backward reasonning -/\n-- 0013\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n rw ←sub_nonneg,\n have key : a * c - b * c = (a - b) * c, ring,\n rw key,\n apply mul_nonneg_of_nonpos_of_nonpos,\n rwa sub_nonpos,\n exact hc,\nend\n\n/- Using forward reasonning -/\n-- 0014\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n replace hab, exact sub_nonpos.mpr hab,\n replace hab, exact mul_nonneg_of_nonpos_of_nonpos hab hc,\n have : (a - b) * c = a * c - b * c, ring,\n rw this at hab, clear this,\n exact sub_nonneg.mp hab,\nend\n\n/-- Using a combination of both, with a `calc` block -/\n-- 0015\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n rw ←sub_nonneg,\n calc 0 ≤ (a - b) * c : mul_nonneg_of_nonpos_of_nonpos (sub_nonpos.mpr hab) hc\n ... = a * c - b * c : by ring,\nend\n\n/-\nLet's now move to proving implications. Lean denotes implications using\na simple arrow →, the same it uses for functions (say denoting the type of functions\nfrom ℕ to ℕ by ℕ → ℕ). This is because it sees a proof of P ⇒ Q as a function turning\na proof of P into a proof Q.\n\nMany of the examples that we already met are implications under the hood. For instance we proved\n\n le_add_of_nonneg_left (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b\n\nBut this can be rephrased as\n\n le_add_of_nonneg_left (a b : ℝ) : 0 ≤ a → b ≤ a + b\n\nIn order to prove P → Q, we use the tactic `intros`, followed by an assumption name.\nThis creates an assumption with that name asserting that P holds, and turns the goal into Q.\n\nLet's check we can go from our old version of `le_add_of_nonneg_left` to the new one.\n\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nbegin\n intros ha,\n exact le_add_of_nonneg_left ha,\nend\n\n/-\nActually Lean doesn't make any difference between those two versions. It is also happy with\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nle_add_of_nonneg_left\n\n/- No tactic state is shown in the above line because we don't even need to enter\ntactic mode using `begin` or `by`.\n\nLet's practise using `intros`. -/\n\n-- 0016\nexample (a b : ℝ): 0 ≤ b → a ≤ a + b :=\nbegin\n intro hb,\n exact le_add_of_nonneg_right hb,\nend\n\n\n\n/-\nWhat about lemmas having more than one assumption? For instance:\n\n add_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b\n\nA natural idea is to use the conjunction operator (logical AND), which Lean denotes\nby ∧. Assumptions built using this operator can be decomposed using the `cases` tactic,\nwhich is a very general assumption-decomposing tactic.\n-/\nexample {a b : ℝ} : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b :=\nbegin\n intros hyp,\n cases hyp with ha hb,\n exact add_nonneg ha hb,\nend\n\n/-\nNeeding that intermediate line invoking `cases` shows this formulation is not what is used by\nLean. It rather sees `add_nonneg` as two nested implications:\nif a is non-negative then if b is non-negative then a+b is non-negative.\nIt reads funny, but it is much more convenient to use in practice.\n-/\nexample {a b : ℝ} : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nadd_nonneg\n\n/-\nThe above pattern is so common that implications are defined as right-associative operators,\nhence parentheses are not needed above.\n\nLet's prove that the naive conjunction version implies the funny Lean version. For this we need\nto know how to prove a conjunction. The `split` tactic creates two goals from a conjunction goal.\nIt can also be used to create two implication goals from an equivalence goal.\n-/\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha,\n intros hb,\n apply H,\n split,\n exact ha,\n exact hb,\nend\n\n/-\nLet's practice `cases` and `split`. In the next exercise, P, Q and R denote\nunspecified mathematical statements.\n-/\n\n-- 0017\nexample (P Q R : Prop) : P ∧ Q → Q ∧ P :=\nbegin\n intro h,\n cases h with hp hq,\n split,\n exact hq,\n exact hp,\nend\n\n/-\nOf course using `split` only to be able to use `exact` twice in a row feels silly. One can\nalso use the anonymous constructor syntax: ⟨ ⟩\nBeware those are not parentheses but angle brackets. This is a generic way of providing\ncompound objects to Lean when Lean already has a very clear idea of what it is waiting for.\n\nSo we could have replaced the last three lines by:\n exact ⟨hQ, hP⟩\n\nWe can also combine the `intros` steps. We can now compress our earlier proof to:\n-/\n\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha hb,\n exact H ⟨ha, hb⟩,\nend\n\n/-\nThe anonymous contructor trick actually also works in `intros` provided we use\nits recursive version `rintros`. So we can replace\n intro h,\n cases h with h₁ h₂\nby\n rintros ⟨h₁, h₂⟩,\nNow redo the previous exercise using all those compressing techniques, in exactly two lines. -/\n\n-- 0018\nexample (P Q R : Prop): P ∧ Q → Q ∧ P :=\nbegin\n rintro ⟨hp, hq⟩,\n exact ⟨hq, hp⟩,\nend\n\n/-\nWe are ready to come back to the equivalence between the different formulations of\nlemmas having two assumptions. Remember the `split` tactic can be used to split\nan equivalence into two implications.\n-/\n\n-- 0019\nexample (P Q R : Prop) : (P ∧ Q → R) ↔ (P → (Q → R)) :=\nbegin\n split,\n intros hpq hp hq,\n exact hpq ⟨hp, hq⟩,\n rintro hpq ⟨hp, hq⟩,\n exact hpq hp hq,\nend\n\n/-\nIf you used more than five lines in the above exercise then try to compress things\n(without simply removing line ends).\n\nOne last compression technique: given a proof h of a conjunction P ∧ Q, one can get\na proof of P using h.left and a proof of Q using h.right, without using cases.\nOne can also use the more generic (but less legible) names h.1 and h.2.\n\nSimilarly, given a proof h of P ↔ Q, one can get a proof of P → Q using h.mp\nand a proof of Q → P using h.mpr (or the generic h.1 and h.2 that are even less legible\nin this case).\n\nBefore the final exercise in this file, let's make sure we'll be able to leave\nwithout learning 10 lemma names. The `linarith` tactic will prove any equality or\ninequality or contradiction that follows by linear combinations of assumptions from the\ncontext (with constant coefficients).\n-/\n\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n linarith,\nend\n\n/-\nNow let's enjoy this for a while.\n-/\n\n-- 0020\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n linarith,\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0021\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d :=\nbegin\n linarith,\nend\n\n\n/-\nFinal exercise\n\nIn the last exercise of this file, we will use the divisibility relation on ℕ,\ndenoted by ∣ (beware this is a unicode divisibility bar, not the ASCII pipe character),\nand the gcd function.\n\nThe definitions are the usual ones, but our goal is to avoid using these definitions and\nonly use the following three lemmas:\n\n dvd_refl (a : ℕ) : a ∣ a\n\n dvd_antisymm {a b : ℕ} : a ∣ b → b ∣ a → a = b :=\n\n dvd_gcd_iff {a b c : ℕ} : c ∣ gcd a b ↔ c ∣ a ∧ c ∣ b\n-/\n\n-- All functions and lemmas below are about natural numbers.\nopen nat\n\n-- 0022\nexample (a b : ℕ) : a ∣ b ↔ gcd a b = a :=\nbegin\n /- not sure why dvd_gcd_iff would be preferable -/\n cases gcd_dvd a b with ha hb,\n split,\n intro h,\n apply dvd_antisymm,\n exact ha,\n rw dvd_gcd_iff,\n exact ⟨dvd_refl a, h⟩,\n intro h,\n rwa ← h,\nend\n\n", "meta": {"author": "michens", "repo": "learn-lean", "sha": "f38fc342780ddff5a164a18e5482163dea506ccd", "save_path": "github-repos/lean/michens-learn-lean", "path": "github-repos/lean/michens-learn-lean/learn-lean-f38fc342780ddff5a164a18e5482163dea506ccd/tutorials/src/exercises/02_iff_if_and.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9504109770159682, "lm_q2_score": 0.9219218284193597, "lm_q1q2_score": 0.8762046256803915}}
{"text": "import .lovelib\n\n\n/-! # LoVe Homework 5: Inductive Predicates\n\nHomework must be done individually. -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1 (3 points): A Type of λ-Terms\n\nRecall the following type of λ-terms from question 3 of exercise 4: -/\n\ninductive term : Type\n| var : string → term\n| lam : string → term → term\n| app : term → term → term\n\n/-! 1.1 (1 point). Define an inductive predicate `is_app` that returns `true` if\nits argument is of the form `term.app …` and that returns false otherwise. -/\n\n-- enter your definition here\n\n/-! 1.2 (2 points). Define an inductive predicate `is_abs_free` that is true if\nand only if its argument is a λ-term that contains no λ-expressions. -/\n\n-- enter your definition here\n\n\n/-! ## Question 2 (6 points): Even and Odd\n\nConsider the following inductive definition of even numbers: -/\n\ninductive even : ℕ → Prop\n| zero : even 0\n| add_two {n : ℕ} : even n → even (n + 2)\n\n/-! 2.1 (1 point). Define a similar predicate for odd numbers, by completing the\nLean definition below. The definition should distinguish two cases, like `even`,\nand should not rely on `even`. -/\n\ninductive odd : ℕ → Prop\n-- supply the missing cases here\n\n/-! 2.2 (1 point). Give proof terms for the following propositions, based on\nyour answer to question 2.1. -/\n\nlemma odd_3 :\n odd 3 :=\nsorry\n\nlemma odd_5 :\n odd 5 :=\nsorry\n\n/-! 2.3 (2 points). Prove the following lemma by rule induction, as a \"paper\"\nproof. This is a good exercise to develop a deeper understanding of how rule\ninduction works (and is good practice for the final exam).\n\n lemma even_odd {n : ℕ} (heven : even n) :\n odd (n + 1) :=\n\nGuidelines for paper proofs:\n\nWe expect detailed, rigorous, mathematical proofs. You are welcome to use\nstandard mathematical notation or Lean structured commands (e.g., `assume`,\n`have`, `show`, `calc`). You can also use tactical proofs (e.g., `intro`,\n`apply`), but then please indicate some of the intermediate goals, so that we\ncan follow the chain of reasoning.\n\nMajor proof steps, including applications of induction and invocation of the\ninduction hypothesis, must be stated explicitly. For each case of a proof by\ninduction, you must list the inductive hypotheses assumed (if any) and the goal\nto be proved. Minor proof steps corresponding to `refl`, `simp`, or `linarith`\nneed not be justified if you think they are obvious (to humans), but you should\nsay which key lemmas they depend on. You should be explicit whenever you use a\nfunction definition or an introduction rule for an inductive predicate. -/\n\n-- enter your paper proof here\n\n/-! 2.4 (1 point). Prove the same lemma again, but this time in Lean: -/\n\nlemma even_odd {n : ℕ} (heven : even n) :\n odd (n + 1) :=\nsorry\n\n/-! 2.5 (1 point). Prove the following lemma in Lean.\n\nHint: Recall that `¬ a` is defined as `a → false`. -/\n\nlemma even_not_odd {n : ℕ} (heven : even n) :\n ¬ odd n :=\nsorry\n\nend LoVe\n", "meta": {"author": "blanchette", "repo": "logical_verification_2022", "sha": "5aee593fbef9b63d4338288b4789d85851d258aa", "save_path": "github-repos/lean/blanchette-logical_verification_2022", "path": "github-repos/lean/blanchette-logical_verification_2022/logical_verification_2022-5aee593fbef9b63d4338288b4789d85851d258aa/lean/love05_inductive_predicates_homework_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9334308128813471, "lm_q2_score": 0.9381240086540332, "lm_q1q2_score": 0.8756738559814421}}
{"text": "import tactic\nimport data.real.basic\nimport data.set\n\n/--\nDefinition for convergence of a sequence a n to a limit t.\n-/\ndef tendsto (a : ℕ → ℝ) (t : ℝ) : Prop :=\n∀ ε > 0, ∃ B : ℕ, ∀ n, B ≤ n → |a n - t| < ε\n\n/--\nLemma for rewriting tendsto without using the change tactic.\n-/\nlemma tendsto_def {a : ℕ → ℝ} {t : ℝ} :\n tendsto a t ↔ ∀ ε, 0 < ε → ∃ B : ℕ, ∀ n, B ≤ n → |a n - t| < ε :=\nbegin\n refl\nend\n\n\n/--\nA sequence a n tends to t iff -a n tends to -t.\nThis is a problem from the problem sheets converted to an iff.\n-/\ntheorem tendsto_neg_iff {a : ℕ → ℝ} {t : ℝ} :tendsto a t ↔ tendsto (λ n, - a n) (-t) :=\nbegin\n split,\n intro ha,\n rw tendsto_def at ⊢ ha,\n intro eps,\n intro he,\n specialize ha eps,\n have hb : (∃ (B : ℕ), ∀ (n : ℕ), B ≤ n → |a n - t| < eps),\n apply ha, exact he,\n cases hb,\n use hb_w,\n intro n,\n intro i,\n rw ←abs_neg (- a n - -t),\n ring_nf,\n apply hb_h,exact i,\n\n intro ha,\n rw tendsto_def at ⊢ ha,\n intro eps,\n intro he,\n specialize ha eps,\n have hb: (∃ (B : ℕ), ∀ (n : ℕ), B ≤ n → | -a n - -t| < eps),\n apply ha, exact he,\n cases hb,\n use hb_w,\n intros n i,\n specialize hb_h n,\n rw ←abs_neg (a n -t),\n ring_nf,\n ring_nf at hb_h,\n apply hb_h, exact i,\nend\n#lint", "meta": {"author": "sterguel", "repo": "formalising-mathematics", "sha": "ecbb2b6d22ee08606c54b3968bf96950488aed42", "save_path": "github-repos/lean/sterguel-formalising-mathematics", "path": "github-repos/lean/sterguel-formalising-mathematics/formalising-mathematics-ecbb2b6d22ee08606c54b3968bf96950488aed42/src/cw1/reals_ps.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9433475730993027, "lm_q2_score": 0.9273632856092016, "lm_q1q2_score": 0.8748259048608358}}
{"text": "-- BOTH:\nimport data.real.basic\n\n/- TEXT:\n.. _the_existential_quantifier:\n\nThe Existential Quantifier\n--------------------------\n\nThe existential quantifier, which can be entered as ``\\ex`` in VS Code,\nis used to represent the phrase \"there exists.\"\nThe formal expression ``∃ x : ℝ, 2 < x ∧ x < 3`` in Lean says\nthat there is a real number between 2 and 3.\n(We will discuss the conjunction symbol, ``∧``, in :numref:`conjunction_and_biimplication`.)\nThe canonical way to prove such a statement is to exhibit a real number\nand show that it has the stated property.\nThe number 2.5, which we can enter as ``5 / 2``\nor ``(5 : ℝ) / 2`` when Lean cannot infer from context that we have\nthe real numbers in mind, has the required property,\nand the ``norm_num`` tactic can prove that it meets the description.\n\n.. index:: use, tactics ; use\n\nThere are a few ways we can put the information together.\nGiven a goal that begins with an existential quantifier,\nthe ``use`` tactic is used to provide the object,\nleaving the goal of proving the property.\nTEXT. -/\n-- QUOTE:\nexample : ∃ x : ℝ, 2 < x ∧ x < 3 :=\nbegin\n use 5 / 2,\n norm_num\nend\n-- QUOTE.\n\n/- TEXT:\n.. index:: anonyomous constructor\n\nAlternatively, we can use Lean's *anonyomous constructor* notation\nto construct the proof.\nTEXT. -/\n-- QUOTE:\nexample : ∃ x : ℝ, 2 < x ∧ x < 3 :=\nbegin\n have h : 2 < (5 : ℝ) / 2 ∧ (5 : ℝ) / 2 < 3,\n by norm_num,\n exact ⟨5 / 2, h⟩\nend\n-- QUOTE.\n\n/- TEXT:\nThe left and right angle brackets,\nwhich can be entered as ``\\<`` and ``\\>`` respectively,\ntell Lean to put together the given data using\nwhatever construction is appropriate\nfor the current goal.\nWe can use the notation without going first into tactic mode:\nTEXT. -/\n-- QUOTE:\nexample : ∃ x : ℝ, 2 < x ∧ x < 3 :=\n⟨5 / 2, by norm_num⟩\n-- QUOTE.\n\n/- TEXT:\nSo now we know how to *prove* an exists statement.\nBut how do we *use* one?\nIf we know that there exists an object with a certain property,\nwe should be able to give a name to an arbitrary one\nand reason about it.\nFor example, remember the predicates ``fn_ub f a`` and ``fn_lb f a``\nfrom the last section,\nwhich say that ``a`` is an upper bound or lower bound on ``f``,\nrespectively.\nWe can use the existential quantifier to say that \"``f`` is bounded\"\nwithout specifying the bound:\nTEXT. -/\n-- BOTH:\n-- QUOTE:\ndef fn_ub (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a\ndef fn_lb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x\n\ndef fn_has_ub (f : ℝ → ℝ) := ∃ a, fn_ub f a\ndef fn_has_lb (f : ℝ → ℝ) := ∃ a, fn_lb f a\n-- QUOTE.\n\n/- TEXT:\nWe can use the theorem ``fn_ub_add`` from the last section\nto prove that if ``f`` and ``g`` have upper bounds,\nthen so does ``λ x, f x + g x``.\nTEXT. -/\n\n-- BOTH:\ntheorem fn_ub_add {f g : ℝ → ℝ} {a b : ℝ}\n (hfa : fn_ub f a) (hgb : fn_ub g b) :\n fn_ub (λ x, f x + g x) (a + b) :=\nλ x, add_le_add (hfa x) (hgb x)\n\nsection\n-- QUOTE:\nvariables {f g : ℝ → ℝ}\n\n-- EXAMPLES:\nexample (ubf : fn_has_ub f) (ubg : fn_has_ub g) :\n fn_has_ub (λ x, f x + g x) :=\nbegin\n cases ubf with a ubfa,\n cases ubg with b ubfb,\n use a + b,\n apply fn_ub_add ubfa ubfb\nend\n-- QUOTE.\n\n/- TEXT:\n.. index:: cases, tactics ; cases\n\nThe ``cases`` tactic unpacks the information\nin the existential quantifier.\nGiven the hypothesis ``ubf`` that there is an upper bound\nfor ``f``,\n``cases`` adds a new variable for an upper\nbound to the context,\ntogether with the hypothesis that it has the given property.\nThe ``with`` clause allows us to specify the names\nwe want Lean to use.\nThe goal is left unchanged;\nwhat *has* changed is that we can now use\nthe new object and the new hypothesis\nto prove the goal.\nThis is a common pattern in mathematics:\nwe unpack objects whose existence is asserted or implied\nby some hypothesis, and then use it to establish the existence\nof something else.\n\nTry using this pattern to establish the following.\nYou might find it useful to turn some of the examples\nfrom the last section into named theorems,\nas we did with ``fn_ub_add``,\nor you can insert the arguments directly\ninto the proofs.\nTEXT. -/\n-- QUOTE:\nexample (lbf : fn_has_lb f) (lbg : fn_has_lb g) :\n fn_has_lb (λ x, f x + g x) :=\nsorry\n\nexample {c : ℝ} (ubf : fn_has_ub f) (h : c ≥ 0):\n fn_has_ub (λ x, c * f x) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (lbf : fn_has_lb f) (lbg : fn_has_lb g) :\n fn_has_lb (λ x, f x + g x) :=\nbegin\n cases lbf with a lbfa,\n cases lbg with b lbgb,\n use a + b,\n intro x,\n exact add_le_add (lbfa x) (lbgb x)\nend\n\nexample {c : ℝ} (ubf : fn_has_ub f) (h : c ≥ 0):\n fn_has_ub (λ x, c * f x) :=\nbegin\n cases ubf with a lbfa,\n use c * a,\n intro x,\n exact mul_le_mul_of_nonneg_left (lbfa x) h\nend\n\n/- TEXT:\n.. index:: rintros, tactics ; rintros, rcases, tactics ; rcases\n\nThe task of unpacking information in a hypothesis is\nso important that Lean and mathlib provide a number of\nways to do it.\nA cousin of the ``cases`` tactic, ``rcases``, is more\nflexible in that it allows us to unpack nested data.\n(The \"r\" stands for \"recursive.\")\nIn the ``with`` clause for unpacking an existential quantifier,\nwe name the object and the hypothesis by presenting\nthem as a pattern ``⟨a, h⟩`` that ``rcases`` then tries to match.\nThe ``rintro`` tactic (which can also be written ``rintros``)\nis a combination of ``intros`` and ``rcases``.\nThese examples illustrate their use:\nTEXT. -/\n-- QUOTE:\nexample (ubf : fn_has_ub f) (ubg : fn_has_ub g) :\n fn_has_ub (λ x, f x + g x) :=\nbegin\n rcases ubf with ⟨a, ubfa⟩,\n rcases ubg with ⟨b, ubfb⟩,\n exact ⟨a + b, fn_ub_add ubfa ubfb⟩\nend\n\nexample : fn_has_ub f → fn_has_ub g →\n fn_has_ub (λ x, f x + g x) :=\nbegin\n rintros ⟨a, ubfa⟩ ⟨b, ubfb⟩,\n exact ⟨a + b, fn_ub_add ubfa ubfb⟩\nend\n-- QUOTE.\n\n/- TEXT:\nIn fact, Lean also supports a pattern-matching lambda\nin expressions and proof terms:\nTEXT. -/\n-- QUOTE:\nexample : fn_has_ub f → fn_has_ub g →\n fn_has_ub (λ x, f x + g x) :=\nλ ⟨a, ubfa⟩ ⟨b, ubfb⟩, ⟨a + b, fn_ub_add ubfa ubfb⟩\n-- QUOTE.\n\n-- BOTH:\nend\n\n/- TEXT:\nThese are power-user moves, and there is no harm\nin favoring the use of ``cases`` until you are more comfortable\nwith the existential quantifier.\nBut we will come to learn that all of these tools,\nincluding ``cases``, ``use``, and the anonymous constructors,\nare like Swiss army knives when it comes to theorem proving.\nThey can be used for a wide range of purposes,\nnot just for unpacking exists statements.\n\nTo illustrate one way that ``rcases`` can be used,\nwe prove an old mathematical chestnut:\nif two integers ``x`` and ``y`` can each be written as\na sum of two squares,\nthen so can their product, ``x * y``.\nIn fact, the statement is true for any commutative\nring, not just the integers.\nIn the next example, ``rcases`` unpacks two existential\nquantifiers at once.\nWe then provide the magic values needed to express ``x * y``\nas a sum of squares as a list to the ``use`` statement,\nand we use ``ring`` to verify that they work.\nTEXT. -/\nsection\n-- QUOTE:\nvariables {α : Type*} [comm_ring α]\n\ndef sum_of_squares (x : α) := ∃ a b, x = a^2 + b^2\n\ntheorem sum_of_squares_mul {x y : α}\n (sosx : sum_of_squares x) (sosy : sum_of_squares y) :\n sum_of_squares (x * y) :=\nbegin\n rcases sosx with ⟨a, b, xeq⟩,\n rcases sosy with ⟨c, d, yeq⟩,\n rw [xeq, yeq],\n use [a*c - b*d, a*d + b*c],\n ring\nend\n-- QUOTE.\n/- TEXT:\nThis proof doesn't provide much insight,\nbut here is one way to motivate it.\nA *Gaussian integer* is a number of the form :math:`a + bi`\nwhere :math:`a` and :math:`b` are integers and :math:`i = \\sqrt{-1}`.\nThe *norm* of the Gaussian integer :math:`a + bi` is, by definition,\n:math:`a^2 + b^2`.\nSo the norm of a Gaussian integer is a sum of squares,\nand any sum of squares can be expressed in this way.\nThe theorem above reflects the fact that norm of a product of\nGaussian integers is the product of their norms:\nif :math:`x` is the norm of :math:`a + bi` and\n:math:`y` in the norm of :math:`c + di`,\nthen :math:`xy` is the norm of :math:`(a + bi) (c + di)`.\nOur cryptic proof illustrates the fact that\nthe proof that is easiest to formalize isn't always\nthe most perspicuous one.\nIn the chapters to come,\nwe will provide you with the means to define the Gaussian\nintegers and use them to provide an alternative proof.\n\nThe pattern of unpacking an equation inside an existential quantifier\nand then using it to rewrite an expression in the goal\ncomes up often,\nso much so that the ``rcases`` tactic provides\nan abbreviation:\nif you use the keyword ``rfl`` in place of a new identifier,\n``rcases`` does the rewriting automatically (this trick doesn't work\nwith pattern-matching lambdas).\nTEXT. -/\n-- QUOTE:\ntheorem sum_of_squares_mul' {x y : α}\n (sosx : sum_of_squares x) (sosy : sum_of_squares y) :\n sum_of_squares (x * y) :=\nbegin\n rcases sosx with ⟨a, b, rfl⟩,\n rcases sosy with ⟨c, d, rfl⟩,\n use [a*c - b*d, a*d + b*c],\n ring\nend\n-- QUOTE.\n\nend\n/- TEXT:\nAs with the universal quantifier,\nyou can find existential quantifiers hidden all over\nif you know how to spot them.\nFor example, divisibility is implicitly an \"exists\" statement.\nTEXT. -/\n-- BOTH:\nsection\nvariables {a b c : ℕ}\n\n-- EXAMPLES:\n-- QUOTE:\nexample (divab : a ∣ b) (divbc : b ∣ c) : a ∣ c :=\nbegin\n cases divab with d beq,\n cases divbc with e ceq,\n rw [ceq, beq],\n use (d * e), ring\nend\n-- QUOTE.\n\n/- TEXT:\nAnd once again, this provides a nice setting for using\n``rcases`` with ``rfl``.\nTry it out in the proof above.\nIt feels pretty good!\n\nThen try proving the following:\nTEXT. -/\n-- QUOTE:\nexample (divab : a ∣ b) (divac : a ∣ c) : a ∣ (b + c) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (divab : a ∣ b) (divbc : b ∣ c) : a ∣ c :=\nbegin\n rcases divab with ⟨d, rfl⟩,\n rcases divbc with ⟨e, rfl⟩,\n use (d * e), ring\nend\n\nexample (divab : a ∣ b) (divac : a ∣ c) : a ∣ (b + c) :=\nbegin\n rcases divab with ⟨d, rfl⟩,\n rcases divac with ⟨e, rfl⟩,\n use (d + e), ring\nend\n\n-- BOTH:\nend\n\n/- TEXT:\n.. index:: surjective function\n\nFor another important example, a function :math:`f : \\alpha \\to \\beta`\nis said to be *surjective* if for every :math:`y` in the\ncodomain, :math:`\\beta`,\nthere is an :math:`x` in the domain, :math:`\\alpha`,\nsuch that :math:`f(x) = y`.\nNotice that this statement includes both a universal\nand an existential quantifier, which explains\nwhy the next example makes use of both ``intro`` and ``use``.\nTEXT. -/\n-- BOTH:\nsection\nopen function\n\n-- EXAMPLES:\n-- QUOTE:\nexample {c : ℝ} : surjective (λ x, x + c) :=\nbegin\n intro x,\n use x - c,\n dsimp, ring\nend\n-- QUOTE.\n\n/- TEXT:\nTry this example yourself:\nTEXT. -/\n-- QUOTE:\nexample {c : ℝ} (h : c ≠ 0) : surjective (λ x, c * x) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample {c : ℝ} (h : c ≠ 0) : surjective (λ x, c * x) :=\nbegin\n intro x,\n use x / c,\n dsimp, rw [mul_div_cancel' _ h]\nend\n\nexample {c : ℝ} (h : c ≠ 0) : surjective (λ x, c * x) :=\nbegin\n intro x,\n use x / c,\n field_simp [h], ring\nend\n\n/- TEXT:\nindex:: field_simp, tactic ; field_simp\n\nAt this point, it is worth mentioning that there is a tactic, `field_simp`,\nthat will often clear denominators in a useful way.\nIt can be used in conjunction with the ``ring`` tactic.\nTEXT. -/\n-- QUOTE:\nexample (x y : ℝ) (h : x - y ≠ 0) : (x^2 - y^2) / (x - y) = x + y :=\nby { field_simp [h], ring }\n-- QUOTE.\n\n/- TEXT:\nYou can use the theorem ``div_mul_cancel``.\nThe next example uses a surjectivity hypothesis\nby applying it to a suitable value.\nNote that you can use ``cases`` with any expression,\nnot just a hypothesis.\nTEXT. -/\n-- QUOTE:\nexample {f : ℝ → ℝ} (h : surjective f) : ∃ x, (f x)^2 = 4 :=\nbegin\n cases h 2 with x hx,\n use x,\n rw hx,\n norm_num\nend\n-- QUOTE.\n\n-- BOTH:\nend\n\n/- TEXT:\nSee if you can use these methods to show that\nthe composition of surjective functions is surjective.\nTEXT. -/\n-- BOTH:\nsection\nopen function\n-- QUOTE:\nvariables {α : Type*} {β : Type*} {γ : Type*}\nvariables {g : β → γ} {f : α → β}\n\n-- EXAMPLES:\nexample (surjg : surjective g) (surjf : surjective f) :\n surjective (λ x, g (f x)) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (surjg : surjective g) (surjf : surjective f) :\n surjective (λ x, g (f x)) :=\nbegin\n intro z,\n rcases surjg z with ⟨y, rfl⟩,\n rcases surjf y with ⟨x, rfl⟩,\n use [x, rfl]\nend\n\n-- BOTH:\nend\n", "meta": {"author": "avigad", "repo": "mathematics_in_lean_source", "sha": "4eeb8c43762522c1acaa7f799599609d6a48f59e", "save_path": "github-repos/lean/avigad-mathematics_in_lean_source", "path": "github-repos/lean/avigad-mathematics_in_lean_source/mathematics_in_lean_source-4eeb8c43762522c1acaa7f799599609d6a48f59e/lean_source/03_Logic/source_02_The_Existential_Quantifier.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.951142217223021, "lm_q2_score": 0.9196425251212038, "lm_q1q2_score": 0.8747108303963596}}
{"text": "/-\nCopyright (c) 2019 Neil Strickland. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Neil Strickland\n\nThis is about the function falling n k = n (n - 1) ... (n - k + 1)\nNote that the above formula involves subtraction but we give a\ndifferent recursive definition that only involves addition and\nso works more smoothly as a map ℕ → ℕ → ℕ.\n\n-/\n\nimport data.nat.choose\n\nopen nat\n\ndef falling : ℕ → ℕ → ℕ \n| 0 0 := 1\n| 0 (k + 1) := 0\n| (n + 1) 0 := 1\n| (n + 1) (k + 1) := (n + 1) * falling n k\n\n@[simp]\nlemma falling_zero (n : ℕ) : falling n 0 = 1 := begin\n cases n; refl,\nend\n\n@[simp]\nlemma falling_succ (n k : ℕ) : falling (n + 1) (k + 1) = \n (n + 1) * falling n k := rfl\n\n@[simp]\nlemma falling_zero_succ (k : ℕ) : falling 0 (k + 1) = 0 := rfl\n\n/- falling n n = n! -/\nlemma falling_factorial : ∀ n : ℕ, falling n n = factorial n \n| 0 := rfl\n| (n + 1) := by {rw[falling,factorial,falling_factorial n]}\n\n/- falling p n = p ! / (p - n)! for p ≥ n. \n We formalise this with p = n + m rather than with an inequality.\n-/\nlemma falling_factorial_quot : ∀ n m : ℕ, (falling (n + m) n) * factorial m = factorial (n + m)\n| n 0 := by {rw[add_zero,factorial,mul_one,falling_factorial n]}\n| 0 (m + 1) := by {rw[zero_add,falling,one_mul]}\n| (n + 1) (m + 1) := calc \n falling ((n + 1) + (m + 1)) (n + 1) * factorial (m + 1) = \n ((n + 1) + (m + 1)) * (falling ((n + 1) + m) n) * factorial (m + 1) : rfl\n ... = ((n + 1) + (m + 1)) * ((falling (n + (m + 1)) n) * factorial (m + 1)) :\n by rw[mul_assoc,add_assoc n 1 m,add_comm 1 m]\n ... = ((n + 1) + (m + 1)) * factorial (n + (m + 1)) :\n by {rw[falling_factorial_quot n (m + 1)]}\n ... = (n + (m + 1) + 1) * factorial (n + (m + 1)) : \n by {rw[add_assoc n 1 (m + 1),add_comm 1 (m + 1),← add_assoc n (m + 1) 1]}\n ... = factorial (n + (m + 1) + 1) : rfl\n ... = factorial ((n + 1) + (m + 1)) : \n by {rw[add_assoc n (m + 1) 1,add_assoc n 1 (m + 1),add_comm (m + 1) 1],}\n\n", "meta": {"author": "NeilStrickland", "repo": "lean_lib", "sha": "6a9563de93748ace509d9db4302db6cd77d8f92c", "save_path": "github-repos/lean/NeilStrickland-lean_lib", "path": "github-repos/lean/NeilStrickland-lean_lib/lean_lib-6a9563de93748ace509d9db4302db6cd77d8f92c/src/combinatorics/falling.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9615338090839606, "lm_q2_score": 0.908617900644622, "lm_q1q2_score": 0.873666831008695}}
{"text": "/-\nProduce a proof, pf1, of the proposition\nthat 0 = 0 ∧ (1 = 1 ∧ 2 = 2).\n-/\n\nlemma pf1 : 0 = 0 ∧ (1 = 1 ∧ 2 = 2) :=\n and.intro rfl (and.intro rfl rfl)\n\n/-\nProduce a proof, pf2, of the proposition\nthat (0 = 0 ∧ 1 = 1) ∧ 2 = 2\n-/\n\nlemma pf2 : (0 = 0 ∧ 1 = 1) ∧ 2 = 2 :=\n and.intro (and.intro rfl rfl) rfl\n\n/-\nProduce a proof, pf3, of the proposition\nthat 0 = 0 ∧ 1 = 1 ∧ 2 = 2. Hint, one of\nthe two preceding proofs can be used to\nprove this proposition; there's no need\nto type out a whole new proof.\n-/\nlemma pf3 : 0 = 0 ∧ 1 = 1 ∧ 2 = 2 :=\n pf1\n\n/-\nAn operator, *, is \"right associative\"\nif X * Y * Z means X * (Y * Z), and is\n\"left associative\" if X * Y * Z means\n(X * Y) * Y. Is the logical connective,\n∧, left or right associative? Explain.\n-/\n\n-- It's right associative as we just proved\n-- pf3 with pf1, and pf1 is in the form\n-- X * (Y * Z). \n\n/-\nUse Lean to produce a proof, pf4, that \n0 = 0 ∧ 1 = 1 ∧ 2 = 2 is exactly the\nsame proposition as one of the two\nparenthesized forms. It will be a \nproof that two propositions are equal.\nPut parentheses around each of the\npropositions.\n-/\n\nlemma pf4 :\n (0 = 0 ∧ 1 = 1 ∧ 2 = 2) =\n (0 = 0 ∧ (1 = 1 ∧ 2 = 2)) := \n rfl\n\n/-\nGiven arbitrary propositions, P, Q and\nR it should be possible to produce a\nproof, pf5, showing that if P ∧ (Q ∧ R)\nis true then so is (P ∧ Q) ∧ R. Written\nin inference rule form, this would say \nthe following:\n\n{ P Q R: Prop }, p_qr : P ∧ (Q ∧ R)\n---------------------------------- ∧.assoc_1\n pq_r : (P ∧ Q) ∧ R\n\nProving that this is a valid rule \ncan be done by defining a function, \nlet's call it and_assoc_l, that when \ngiven any propositions, P, Q, and R \n(implicitly), and when given a proof \nof P ∧ (Q ∧ R), constructs and returns\na proof of (P ∧ Q) ∧ R. \n\nHere we give you this function, and\nwe explain each part in comments. \nYou will then apply what you learn \nby studying this example to solve \nthe same problem but going in the\nother direction. Here's the solution.\n-/\n\n-- define the function name\ndef and_assoc_l \n-- specify the arguments and their types\n {P Q R: Prop} \n (pf: P ∧ (Q ∧ R)) : -- note colon\n\n-- the return type \n (P ∧ Q) ∧ R\n/-\nWhat we've given so far is what we call\nthe function signature: its name, the\nnames and types of the arguments that it\ntakes, and the type of the return value.\nIn this case, the return value is of \ntype (P ∧ Q) ∧ R, and will thus serve\nas a proof of this proposition. This is\na function that takes a proof and returns\na (different) proof. It thus provides a\ngeneral recipe for turning any proof of\nP ∧ (Q ∧ R) into a proof of (P ∧ Q) ∧ R.\n-/ \n := -- now give function body\n\n/-\nUsually we'd expect to see an expression\nhere, involving multiple, nested and.elim \nand and.intro expressions. We could write\nthe function body that way, but it's a bit\ntricky to get all the nested expressions\nright. Here's a revelation: We can use a\ntactic script to produce the same result.\n\nOpen your Messages window, put your cursor\non begin, study carefully the tactic state,\nnotice that the arguments are given in the\ncontext to the left of the turnstile and\nthe goal remaining to be proved is to the\nright. You can use the context values as\narguments to tactics.\n\nNow click through each line of the script\nand study very carefully how it changes the\ncontext. By the end of the script, you \nshould see how we've been able to use \nelimination rules take apart the proof \nthat was given as an argument, giving names \nto the parts, and how we can then further \ntake apart those parts, giving names to the \nsubparts, and finally how we can intro \nrules to put all these pieces together\nagain into the proof we need. \n-/\n begin\n have pfP := and.elim_left pf,\n have pfQR := and.elim_right pf,\n have pfQ := and.elim_left pfQR,\n have pfR := and.elim_right pfQR,\n have pfPQ := and.intro pfP pfQ,\n have pfPQ_R := and.intro pfPQ pfR,\n exact pfPQ_R\n end\n\n/-\nDefine another function, and_assoc_r,\nthat goes the other direction: given\na proof of (P ∧ Q) ∧ R it derives and\nreturns a proof of P ∧ (Q ∧ R). Write \nthe entire solution yourself.\n-/\n\ndef and_assoc_r \n {P Q R: Prop} \n (pf: (P ∧ Q) ∧ R) : \n P ∧ (Q ∧ R) :=\n begin\n have pfPQ := and.elim_left pf,\n have pfR := and.elim_right pf,\n have pfP := and.elim_left pfPQ,\n have pfQ := and.elim_right pfPQ, \n have pfQR := and.intro pfQ pfR,\n have pfP_QR := and.intro pfP pfQR,\n exact pfP_QR\n end\n\n\n/-\nIt's important to learn how you would\ngive such proofs in natural langage.\nLet's take our first example. Here is\na natural language version.\n\n\"Given arbitrary propositions, P, Q, and\nR, and the assumption that P ∧ (Q ∧ R) is\ntrue, we are to show that (P ∧ Q) ∧ R is\ntrue.\n\nGiven that P ∧ (Q ∧ R) is true, it must \nbe that P is true and that Q ∧ R is also\ntrue. Given that Q ∧ R is true, it must\nbe that Q is true, and R is also true.\nSo we have that P, Q, and R are all true.\n\nFrom these conclusions we can in turn\ndeduce that P ∧ Q must be true. And so\nwe now have that P ∧ Q is true and so is\nR, from which, finally we can deduce \nthat (P ∧ Q) ∧ R must be true as well.\n\nQED.\"\n\nNow it's your turn: write an English\nlanguage proof for the theorem in the \nother direction.\n-/\n\n/-\n\"Given arbitrary propositions, P, Q, and\nR, and the assumption that (P ∧ Q) ∧ R is\ntrue, we are to show that P ∧ (Q ∧ R) is\ntrue.\n\nGiven that (P ∧ Q) ∧ R, it must be that \n(P ∧ Q) is true and also that R is true.\nGiven that (P ∧ Q) is true, then P must \nbe true, and Q must be true. Therefore\nP, Q, and R are all true.\n\nFrom this conclusion we can deduce that \nQ ∧ R must be true. Since P is true and \n(Q ∧ R) is true, it follows that P ∧ (Q ∧ R)\nmust be true as well.\n\nQED.\"\n\n-/\n\n/-\nUse Lean to produce a proof, tnott, of\nthe proposition that truth isn't truth. \nI.e., true is not true. We'll write this\nis Lean like this:\n\ntheorem tnott: true ≠ true := _. \n\nTo make it a little easier to solve \nthis otherwise difficult problem, we\nallow you to stipulate one \"axiom\" of\nyour choice, which you can then use\nto produce the required proof.\n-/\n\n-- You can introduce an axiom here\naxiom f: false\n\n-- Now prove the theorem\ntheorem tnott : true ≠ true := false.elim f\n\n/-\nWhat did you have to accept to be able \nto prove that truth isn't truth?\n-/\n\n/- We had to accept that there was an axiom\nfor false, which is an absurdity, which\nthen allows us to create a proof for a \nproposition for which there is not proof\n-/", "meta": {"author": "Mnormansell", "repo": "Discrete-Notes", "sha": "61f13b98be590269fc4822be7b47924a6ddc1261", "save_path": "github-repos/lean/Mnormansell-Discrete-Notes", "path": "github-repos/lean/Mnormansell-Discrete-Notes/Discrete-Notes-61f13b98be590269fc4822be7b47924a6ddc1261/HW/HW3.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9184802350995702, "lm_q2_score": 0.9511422168767872, "lm_q1q2_score": 0.873605326970118}}
{"text": "import tuto_lib\nimport data.int.parity\n/-\nNegations, proof by contradiction and contraposition.\n\nThis file introduces the logical rules and tactics related to negation:\nexfalso, by_contradiction, contrapose, by_cases and push_neg.\n\nThere is a special statement denoted by `false` which, by definition,\nhas no proof.\n\nSo `false` implies everything. Indeed `false → P` means any proof of \n`false` could be turned into a proof of P.\nThis fact is known by its latin name\n\"ex falso quod libet\" (from false follows whatever you want).\nHence Lean's tactic to invoke this is called `exfalso`.\n-/\n\nexample : false → 0 = 1 :=\nbegin\n intro h,\n exfalso,\n exact h,\nend\n\n/-\nThe preceding example suggests that this definition of `false` isn't very useful.\nBut actually it allows us to define the negation of a statement P as\n\"P implies false\" that we can read as \"if P were true, we would get \na contradiction\". Lean denotes this by `¬ P`.\n\nOne can prove that (¬ P) ↔ (P ↔ false). But in practice we directly\nuse the definition of `¬ P`.\n-/\n\nexample {x : ℝ} : ¬ x < x :=\nbegin\n intro hyp,\n rw lt_iff_le_and_ne at hyp,\n cases hyp with hyp_inf hyp_non,\n clear hyp_inf, -- we won't use that one, so let's discard it\n change x = x → false at hyp_non, -- Lean doesn't need this psychological line\n apply hyp_non,\n refl,\nend\n\nopen int\n\n-- 0045\nexample (n : ℤ) (h_even : even n) (h_not_even : ¬ even n) : 0 = 1 :=\nbegin\n sorry\nend\n\n-- 0046\nexample (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q :=\nbegin\n sorry\nend\n\n/-\nThe definition of negation easily implies that, for every statement P,\nP → ¬ ¬ P\n\nThe excluded middle axiom, which asserts P ∨ ¬ P allows us to\nprove the converse implication.\n\nTogether those two implications form the principle of double negation elimination.\n not_not {P : Prop} : (¬ ¬ P) ↔ P\n\nThe implication `¬ ¬ P → P` is the basis for proofs by contradiction:\nin order to prove P, it suffices to prove ¬¬ P, ie `¬ P → false`.\n\nOf course there is no need to keep explaining all this. The tactic\n`by_contradiction Hyp` will transform any goal P into `false` and \nadd Hyp : ¬ P to the local context.\n\nLet's return to a proof from the 5th file: uniqueness of limits for a sequence.\nThis cannot be proved without using some version of the excluded middle\naxiom. We used it secretely in\n\neq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y\n\n(we'll prove a variation on this lemma below).\n\nIn the proof below, we also take the opportunity to introduce the `let` tactic\nwhich creates a local definition. If needed, it can be unfolded using `dsimp` which \ntakes a list of definitions to unfold. For instance after using `let N₀ := max N N'`, \nyou could write `dsimp [N₀] at h` to replace `N₀` by its definition in some\nlocal assumption `h`.\n-/\nexample (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' :=\nbegin\n intros hl hl',\n by_contradiction H,\n change l ≠ l' at H, -- Lean does not need this line\n have ineg : |l-l'| > 0,\n exact abs_pos.mpr (sub_ne_zero_of_ne H), -- details about that line are not important\n cases hl ( |l-l'|/4 ) (by linarith) with N hN,\n cases hl' ( |l-l'|/4 ) (by linarith) with N' hN',\n let N₀ := max N N', \n specialize hN N₀ (le_max_left _ _),\n specialize hN' N₀ (le_max_right _ _),\n have clef : |l-l'| < |l-l'|,\n calc\n |l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring_nf\n ... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add\n ... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub_comm\n ... < |l-l'| : by linarith,\n linarith, -- linarith can also find simple numerical contradictions\nend\n\n/-\nAnother incarnation of the excluded middle axiom is the principle of\ncontraposition: in order to prove P ⇒ Q, it suffices to prove\nnon Q ⇒ non P.\n-/\n\n-- Using a proof by contradiction, let's prove the contraposition principle\n-- 0047\nexample (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=\nbegin\n sorry\nend\n\n/-\nAgain Lean doesn't need this principle explained to it. We can use the\n`contrapose` tactic.\n-/\n\nexample (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=\nbegin\n contrapose,\n exact h,\nend\n\n/-\nIn the next exercise, we'll use\n odd n : ∃ k, n = 2*k + 1\n int.odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n\n-/\n-- 0048\nexample (n : ℤ) : even (n^2) ↔ even n :=\nbegin\n sorry\nend\n/-\nAs a last step on our law of the excluded middle tour, let's notice that, especially\nin pure logic exercises, it can sometimes be useful to use the\nexcluded middle axiom in its original form:\n classical.em : ∀ P, P ∨ ¬ P\n\nInstead of applying this lemma and then using the `cases` tactic, we\nhave the shortcut\n by_cases h : P,\n\ncombining both steps to create two proof branches: one assuming\nh : P, and the other assuming h : ¬ P\n\nFor instance, let's prove a reformulation of this implication relation,\nwhich is sometimes used as a definition in other logical foundations,\nespecially those based on truth tables (hence very strongly using\nexcluded middle from the very beginning).\n-/\n\nvariables (P Q : Prop)\n\nexample : (P → Q) ↔ (¬ P ∨ Q) :=\nbegin\n split,\n { intro h,\n by_cases hP : P,\n { right,\n exact h hP },\n { left,\n exact hP } },\n { intros h hP,\n cases h with hnP hQ,\n { exfalso,\n exact hnP hP },\n { exact hQ } },\nend\n\n-- 0049\nexample : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q :=\nbegin\n sorry\nend\n\n/-\nIt is crucial to understand negation of quantifiers. \nLet's do it by hand for a little while.\nIn the first exercise, only the definition of negation is needed.\n-/\n\n-- 0050\nexample (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k :=\nbegin\n sorry\nend\n\n/-\nContrary to negation of the existential quantifier, negation of the\nuniversal quantifier requires excluded middle for the first implication.\nIn order to prove this, we can use either\n* a double proof by contradiction\n* a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction.\n-/\n\ndef even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x\n\n-- 0051\nexample (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=\nbegin\n sorry\nend\n\n/-\nOf course we can't keep repeating the above proofs, especially the second one.\nSo we use the `push_neg` tactic.\n-/\n\nexample : ¬ even_fun (λ x, 2*x) :=\nbegin\n unfold even_fun, -- Here unfolding is important because push_neg won't do it.\n push_neg,\n use 42,\n linarith,\nend\n\n-- 0052\nexample (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=\nbegin\n sorry\nend\n\ndef bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M\n\nexample : ¬ bounded_above (λ x, x) :=\nbegin\n unfold bounded_above,\n push_neg,\n intro M,\n use M + 1,\n linarith,\nend\n\n-- Let's contrapose\n-- 0053\nexample (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 :=\nbegin\n sorry\nend\n\n/-\nThe \"contrapose, push_neg\" combo is so common that we can abreviate it to\n`contrapose!`\n\nLet's use this trick, together with:\n eq_or_lt_of_le : a ≤ b → a = b ∨ a < b\n-/\n\n-- 0054\nexample (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) :=\nbegin\n sorry\nend\n\n", "meta": {"author": "leanprover-community", "repo": "tutorials", "sha": "79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1", "save_path": "github-repos/lean/leanprover-community-tutorials", "path": "github-repos/lean/leanprover-community-tutorials/tutorials-79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1/src/exercises/07_first_negations.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9473810466522862, "lm_q2_score": 0.9219218273467447, "lm_q1q2_score": 0.8734112657233473}}
{"text": "import data.set\n\nuniverses u v -- Universe levels, don't worry\n\n/-\nHomework #6: Functions, Sets, Relations\n-/\n\n\n/- #1. Basic Functions [35 points]-/\n\n\n/- A [5 points].\n\nWrite a function, isZero, using \"by cases\" syntax, \nthat takes any natural number, n, as an argument and \nreturns a Boolean result, true if n = 0 and false if\nn > 0 (i.e., n = n' + 1 = (nat.succ n') for some n').\n(Don't get distracted by unnecessary details.)\n-/\n\ndef isZero : ℕ → bool\n| 0 := tt\n| _ := ff\n\n\n-- These test cases should pass except the last \nexample : isZero 0 = tt := rfl\nexample : isZero 1 = ff := rfl\nexample : isZero 2 = ff := rfl\nexample : isZero 3 = tt := rfl -- tester tester\n\n\n\n/- B [10 points].\n\nHere's a function, fib, using \"by cases\" syntax, that\ntakes any natural number, n, as an argument and that\nreturns the n'th Fibonacci number. The n'th Fibonacci\nnumber is defined by cases: when n is 0, it's 0; when\nn is 1 (nat.succ nat.zero), it's 1, otherwise, in the\ncase where n = n' + 2, it's fib n' + fib n'+1\n-/\n\ndef fib : ℕ → ℕ \n| 0 := 0\n| 1 := 1\n| (n' + 2) := fib n' + fib (n' + 1)\n\n-- These test cases should pass except the last \nexample : fib 0 = 0 := rfl\nexample : fib 1 = 1 := rfl \nexample : fib 2 = 1 := rfl\nexample : fib 3 = 2 := rfl\nexample : fib 4 = 3 := rfl\nexample : fib 5 = 5 := rfl\nexample : fib 10 = 55 := rfl\nexample : fib 2 = 2 := rfl -- tester tester\n\n\n\n/- C [10 points]\nWrite a function, add, that takes two natural \nnumbers and computes their sum. You may *not*\nuse the addition function that Lean defines in\nits libraries. Your addition function should \nbe defined by cases. Given arguments n and m,\nif m is zero, just return n; otherwise, if m\nis (m' + 1), return the sum of n and m' plus\none. (That's still the right answer, right!)\n-/\n\ndef add : ℕ → ℕ → ℕ \n| n 0 := n\n| n (m' + 1) := (add n m') + 1\n\n-- These test cases should pass except the last \nexample : add 0 0 = 0 := rfl\nexample : add 5 0 = 5 := rfl\nexample : add 0 5 = 5 := rfl\nexample : add 1 1 = 2 := rfl\nexample : add 1 2 = 3 := rfl\nexample : add 2 2 = 4 := rfl\nexample : add 3 2 = 4 := rfl -- tester tester\n\n\n/- D [10 points]\n\nA function is said to be involutive, or an\ninvolution, if applying it twice to *any* value\n(of the right type) returns that original value. \nFor example, if f is a function, and a is any\nargument, then f is involutive if f (f a) = a.\nFormally define involutive as a property of\nany function, f : α → α. \n-/\n\ndef involutive {α : Sort u} (f : α → α) := \n ∀ (a : α), f (f a) = a \n\n/-\nNow prove the proposition that the Boolean negation \nfunction, called bnot in Lean, is an involution. The\ntrick here is simply to pick the right proof strategy.\nAsk, how can I prove it? Hint: It's just a bool!\n-/\n\nexample : involutive bnot :=\nbegin\n-- By the definition of involutive ...\nunfold involutive,\n\n-- ... we are to\nshow ∀ (b : bool), !!b = b, \n\n-- To start we assume b to be an arbitrary bool value.\nassume b,\n\n-- We then show that in all cases (b=tt, b=ff) that !!b = b\ncases b,\n\n-- In all cases the conclusion is true.\nexact rfl,\nexact rfl,\n\n-- QED\nend \n\n\n/- 2 Sets [35 points] -/\n\n\n/- A [5 points].\n\nFormally define a predicate, perfectSquare,\nsatisfied by any natural number that is the \nsquare of some other natural number. 25 is a\nperfect square, for example, because it's the\nsquare of the natural number 5. Fill in both\nunderscores in the following skeleton to give\nyour answer. Remember: Predicates take values\nto propositions. \n-/\n\ndef perfectSquare (n : ℕ) : Prop := ∃ m, m*m = n\n\n\n/- B [5 points].\n\nDefine the set of all perfect squares using\nset comprehension (set builder) notation and\nthe perfectSquare predicate.\n-/\n\ndef perfectSquares : set ℕ := { n | perfectSquare n }\n\n\n/- C [5 point].\n\nState and prove the proposition that 25 is in\nthe set of perfect squares. Use set membership\nnotation in writing your proposition.\n-/\n\n\nexample : 25 ∈ perfectSquares :=\nbegin\n-- Expanding the definition of perfectSquares, ...\nunfold perfectSquares,\n\n-- We are to ...\nshow perfectSquare 25,\n\n-- Expanding the definition of perfectSquare, ...\nunfold perfectSquare,\n\n-- We are to show that \nshow ∃ (m : ℕ), m * m = 25,\n\n-- The challenge in proving a ∃ is to find/compute a witness\n-- Here it's easy to see that the witness has to be 5\napply exists.intro 5,\n\n-- The rest is by simple algebra and reflexivity of equality\ntrivial,\nend\n\n\n/- D [20 points].\nConsider the following sets of natural numbers:\n\nX = { 2, 3, 4}\nY = { 4, 5, 6}\n-/\n\ndef X : set ℕ := { 2, 3, 4 }\ndef Y : set ℕ := { 4, 5, 6 }\n\n/-\nFormally state and prove the following propositions\nusing set and set operator notations.\n\n1. 4 is \"in\" the intersection of X and Y\n2. 4 is in the union of X and Y\n3. 4 is not in the set difference, X \\ Y\n4. 10 is in the complement of X\n-/\n\n\n-- 1\nexample : 4 ∈ X :=\nbegin\n-- It's easier if we expand the definition of X\nunfold X,\n\n-- What we now need to show is\nshow 4 = 2 ∨ 4 = 3 ∨ 4 = 4,\n\n-- Remembering ∨ is right associative, we can prove the right side\nright,\n\n-- And again\nright,\n\n-- And the rest is trivial\nexact rfl,\n\n-- QED\nend\n\n-- 2\nexample : 4 ∈ (X ∪ Y) :=\nbegin\n-- Unfolding the definitions of X and Y ...\nunfold X Y,\n\n-- we are to show \nshow (4 = 2 ∨ 4 = 3 ∨ 4 = 4) ∨ (4 = 4 ∨ 4 = 5 ∨ 4 = 6),\n\n-- We can prove either side, but the right is easier\nright,\n\n-- We can prove the left disjunct\nleft,\nexact rfl,\n\n-- And we're done. QED.\nend\n\n/-\nHint: IN the following problem, you \nmight want to fact that 4 ∈ {4, 5, 6} \nwithout stopping to prove it. Here's a \nnice trick/tactic: \n\nlet n : 4 ∈ {4, 5, 6} := _\n\nThis will put the assumption, n, in your\ncontext, but with a note attached saying\nthat you still need a proof of it. Don't\nworry about the crazy goal set after you\nuse this hint (if you do). Just do what \nyou've learned to do looking at the top\ngoal carefully and choosing the right \nnext oroof strategy.\n-/\n\n-- 3\nexample : 4 ∉ (X \\ Y) :=\nbegin\n-- Unfolding X and Y we see that ...\nunfold X Y,\n\n-- ... we must \nshow 4 ∈ {2, 3, 4} \\ {4, 5, 6} → false,\n\n-- The proof of this is by negation. We assume the premise\nassume h,\n\n-- and show that that leads to a contradiction\n\n/-\nA case analysis on this assumption indicates that\nwe have to prove 4 is in the left and not in the\nright set\n-/\ncases h with x y,\n\n/-\nBut 4 is in the right-hand set. So that's going\nto produce a contradiction. Let's just assert that\n4 is in the right hand set (and we'll prove it later).\n-/\nlet n : 4 ∈ {4, 5, 6} := _, -- _ is the missing proof\n\n-- We now have our contradiction\ncontradiction,\n\n-- All we need now is that delayed proof ...\nleft,\nexact rfl,\n\n-- That's easy to prove. QED.\nend\n\n\n-- 4. 10 ∈ Xᶜ \n\nexample : 10 ∈ Xᶜ :=\nbegin\n-- We'll start by expanding the definition of X\nunfold X,\n\n-- What we must now show is that ¬ (10 ∈ {2, 3, 4}).\n-- The prove is by negation.\nassume h,\n\n-- But 10 is not in {2, 3, 4}, \n-- as revealed by repeated analysis of h\nrepeat { cases h }, \nend \n\n\n\n/- 3. Relations [30 points] -/\n\n/- A [15 points].\n\nState and prove the proposition that any \nfunction, f, in Lean, from any type α to any\ntype β, is single valued. \n-/\n\n/- 1 [5 point].\nFormally define single-valuedness for any \nbinary relation r : α → β → Prop. If r is \nsingle valued then r x can have two values, \nsay y and z, only if y = z. Think about this\nhard enough that it really makes sense to you. \n-/\n\ndef single_valued -- predicate on relations\n{α β : Type} \n(r : α → β → Prop) \n: Prop := \n ∀ a x y, r a x → r a y → x = y\n\n/- 2 [5 points]\n\nDefine a relation sqrs : ℕ → ℕ → Prop where sq x y\nmeans that y is the square of x. \n-/\n\ndef sqrs : ℕ → ℕ → Prop \n| x y := y = x * x\n\n/- 3 [5 points] \n\nNow formally state and prove the proposition\nthat the sqrs relation is single-valued.\n-/\n\nexample : single_valued sqrs :=\nbegin\n-- By expanding the definitions of single_valued and sqrs ... \nunfold single_valued sqrs,\n\n-- ... we see that we are to ,,,\nshow ∀ (a x y : ℕ), x = a * a → y = a * a → x = y,\n\n-- We assume the premises \nassume a x y hx hy,\n\n-- and now need to show that x = y, which is done \n-- by rewriting both x and y to a*a (using eq elimination)\nrw hx,\nrw hy,\n\n-- A final (Lean-automated) application of reflexivity of\n-- equality finishes the proof. QED.\nend\n\n\n/- B [5 points]\n\nA mathematical function, f : α → β, is said \nto be injective if it never relates different \nα (first) values to the same β (second) value. \nThe only way an injective function f can relate \na to x and also b to x, is if a = b. A function\nthat satisfies this constraint is injective.\n-/\n\ndef injective \n(α : Sort u) -- any type from any universe\n(β : Sort v) -- any type from any universe\n(r : α → β → Prop) -- any relation on α and β \n(a b : α) -- any two arbitrary α values\n(x : β) : -- any β value\nProp := \n -- Answer\n r a x → -- if r relates a to x, and\n r b x → -- if r relates b to x, then \n a = b -- injectivity demands a = b\n\n\n/- C [10 points].\n\nA mathematical function, f : α → β, is said to be \na total function if it's defined (there is some \nβ value) for all α values. ∀ (a : α), β. Indeed,\nthis is the meaning of the notation α → β in Lean\nand similar logics. The upshot is that functions \nin Lean can only be total. Complete the following\nproof that every function in Lean is total.\n-/\n\nexample : \n ∀ (α β : Type) \n (f : α → β), \n ∀ (a : α), \n ∃ (b : β), \n f a = b :=\nbegin\n-- Assume the given conditions\nassume α β f a, \n\n/- \nGiven a, and given the fact that \"functions\" in Lean \nmust be total, we can of course compute a b such that \nf a = b, and that done by just computing f a. Totality\nof f means that this application must and will return\nresult of type β. So we can just use that value as a\nwitness to prove that ∃ (b : β), f a = b. \n-/\napply exists.intro (f a),\n\n-- The rest is ... \ntrivial,\n\n-- QED\nend\n\n", "meta": {"author": "kevinsullivan", "repo": "cs2120f22", "sha": "8710cf4262e905ffe2b1dee165473ee1f940440b", "save_path": "github-repos/lean/kevinsullivan-cs2120f22", "path": "github-repos/lean/kevinsullivan-cs2120f22/cs2120f22-8710cf4262e905ffe2b1dee165473ee1f940440b/src/homeworks/hw6/hw6_key.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.959762057376384, "lm_q2_score": 0.9099070005411123, "lm_q1q2_score": 0.8732942148605125}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio. Sean a y b números reales. Demostrar que\n-- 2*a*b ≤ a^2 + b^2\n-- ----------------------------------------------------------------------\n\nimport data.real.basic\nimport tactic\n\nvariables a b : ℝ\n\n-- 1ª demostración\nexample : 2*a*b ≤ a^2 + b^2 :=\nbegin\n have : 0 ≤ (a - b)^2 := sq_nonneg (a - b),\n have : 0 ≤ a^2 - 2*a*b + b^2, by linarith,\n show 2*a*b ≤ a^2 + b^2, by linarith,\nend\n\n-- 2ª demostración\nexample : 2*a*b ≤ a^2 + b^2 :=\nbegin\n have h : 0 ≤ a^2 - 2*a*b + b^2,\n { calc a^2 - 2*a*b + b^2\n = (a - b)^2 : by ring\n ... ≥ 0 : by apply pow_two_nonneg },\n calc 2*a*b\n = 2*a*b + 0 : by ring\n ... ≤ 2*a*b + (a^2 - 2*a*b + b^2) : add_le_add (le_refl _) h\n ... = a^2 + b^2 : by ring\nend\n\n-- 3ª demostración\nexample : 2*a*b ≤ a^2 + b^2 :=\nbegin\n have : 0 ≤ a^2 - 2*a*b + b^2,\n { calc a^2 - 2*a*b + b^2\n = (a - b)^2 : by ring\n ... ≥ 0 : by apply pow_two_nonneg },\n linarith,\nend\n\n-- 4ª demostración\nexample : 2*a*b ≤ a^2 + b^2 :=\n-- by library_search\ntwo_mul_le_add_sq a b\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Basicos/Desigualdades_con_calc.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9559813463747181, "lm_q2_score": 0.9124361688107864, "lm_q1q2_score": 0.8722719571407251}}
{"text": "-- BOTH:\nimport data.real.basic\n\n/- TEXT:\n.. _sequences_and_convergence:\n\nSequences and Convergence\n-------------------------\n\nWe now have enough skills at our disposal to do some real mathematics.\nIn Lean, we can represent a sequence :math:`s_0, s_1, s_2, \\ldots` of\nreal numbers as a function ``s : ℕ → ℝ``.\nSuch a sequence is said to *converge* to a number :math:`a` if for every\n:math:`\\varepsilon > 0` there is a point beyond which the sequence\nremains within :math:`\\varepsilon` of :math:`a`,\nthat is, there is a number :math:`N` such that for every\n:math:`n \\ge N`, :math:`| s_n - a | < \\varepsilon`.\nIn Lean, we can render this as follows:\nBOTH: -/\n-- QUOTE:\ndef converges_to (s : ℕ → ℝ) (a : ℝ) :=\n∀ ε > 0, ∃ N, ∀ n ≥ N, abs (s n - a) < ε\n-- QUOTE.\n\n/- TEXT:\nThe notation ``∀ ε > 0, ...`` is a convenient abbreviation\nfor ``∀ ε, ε > 0 → ...``, and, similarly,\n``∀ n ≥ N, ...`` abbreviates ``∀ n, n ≥ N → ...``.\nAnd remember that ``ε > 0``, in turn, is defined as ``0 < ε``,\nand ``n ≥ N`` is defined as ``N ≤ n``.\n\n.. index:: extentionality, ext, tactics ; ext\n\nIn this section, we'll establish some properties of convergence.\nBut first, we will discuss three tactics for working with equality\nthat will prove useful.\nThe first, the ``ext`` tactic,\ngives us a way of proving that two functions are equal.\nLet :math:`f(x) = x + 1` and :math:`g(x) = 1 + x`\nbe functions from reals to reals.\nThen, of course, :math:`f = g`, because they return the same\nvalue for every :math:`x`.\nThe ``ext`` tactic enables us to prove an equation between functions\nby proving that their values are the same\nat all the values of their arguments.\nTEXT. -/\n-- QUOTE:\nexample : (λ x y : ℝ, (x + y)^2) = (λ x y : ℝ, x^2 + 2*x*y + y^2) :=\nby { ext, ring }\n-- QUOTE.\n\n/- TEXT:\n.. index:: congr, tactics ; congr\n\nWe'll see later that ``ext`` is actually more general, and also one can\nspecify the name of the variables that appear.\nFor instance you can try to replace ``ext`` with ``ext u v`` in the\nabove proof.\nThe second tactic, the ``congr`` tactic,\nallows us to prove an equation between two expressions\nby reconciling the parts that are different:\nTEXT. -/\n-- QUOTE:\nexample (a b : ℝ) : abs a = abs (a - b + b) :=\nby { congr, ring }\n-- QUOTE.\n\n/- TEXT:\nHere the ``congr`` tactic peels off the ``abs`` on each side,\nleaving us to prove ``a = a - b + b``.\n\n.. index:: convert, tactics ; convert\n\nFinally, the ``convert`` tactic is used to apply a theorem\nto a goal when the conclusion of the theorem doesn't quite match.\nFor example, suppose we want to prove ``a < a * a`` from ``1 < a``.\nA theorem in the library, ``mul_lt_mul_right``,\nwill let us prove ``1 * a < a * a``.\nOne possibility is to work backwards and rewrite the goal\nso that it has that form.\nInstead, the ``convert`` tactic lets us apply the theorem\nas it is,\nand leaves us with the task of proving the equations that\nare needed to make the goal match.\nTEXT. -/\n-- QUOTE:\nexample {a : ℝ} (h : 1 < a) : a < a * a :=\nbegin\n convert (mul_lt_mul_right _).2 h,\n { rw [one_mul] },\n exact lt_trans zero_lt_one h\nend\n-- QUOTE.\n\n/- TEXT:\nThis example illustrates another useful trick: when we apply an\nexpression with an underscore\nand Lean can't fill it in for us automatically,\nit simply leaves it for us as another goal.\n\nThe following shows that any constant sequence :math:`a, a, a, \\ldots`\nconverges.\nBOTH: -/\n-- QUOTE:\ntheorem converges_to_const (a : ℝ) : converges_to (λ x : ℕ, a) a :=\nbegin\n intros ε εpos,\n use 0,\n intros n nge, dsimp,\n rw [sub_self, abs_zero],\n apply εpos\nend\n-- QUOTE.\n\n/- TEXT:\n.. TODO: reference to the simplifier\n\nLean has a tactic, ``simp``, which can often save you the\ntrouble of carrying out steps like ``rw [sub_self, abs_zero]``\nby hand.\nWe will tell you more about it soon.\n\nFor a more interesting theorem, let's show that if ``s``\nconverges to ``a`` and ``t`` converges to ``b``, then\n``λ n, s n + t n`` converges to ``a + b``.\nIt is helpful to have a clear pen-and-paper\nproof in mind before you start writing a formal one.\nGiven ``ε`` greater than ``0``,\nthe idea is to use the hypotheses to obtain an ``Ns``\nsuch that beyond that point, ``s`` is within ``ε / 2``\nof ``a``,\nand an ``Nt`` such that beyond that point, ``t`` is within\n``ε / 2`` of ``b``.\nThen, whenever ``n`` is greater than or equal to the\nmaximum of ``Ns`` and ``Nt``,\nthe sequence ``λ n, s n + t n`` should be within ``ε``\nof ``a + b``.\nThe following example begins to implement this strategy.\nSee if you can finish it off.\nTEXT. -/\n-- QUOTE:\ntheorem converges_to_add {s t : ℕ → ℝ} {a b : ℝ}\n (cs : converges_to s a) (ct : converges_to t b):\nconverges_to (λ n, s n + t n) (a + b) :=\nbegin\n intros ε εpos, dsimp,\n have ε2pos : 0 < ε / 2,\n { linarith },\n cases cs (ε / 2) ε2pos with Ns hs,\n cases ct (ε / 2) ε2pos with Nt ht,\n use max Ns Nt,\n sorry\nend\n-- QUOTE.\n\n-- SOLUTIONS:\ntheorem converges_to_addαα {s t : ℕ → ℝ} {a b : ℝ}\n (cs : converges_to s a) (ct : converges_to t b):\nconverges_to (λ n, s n + t n) (a + b) :=\nbegin\n intros ε εpos, dsimp,\n have ε2pos : 0 < ε / 2,\n { linarith },\n cases cs (ε / 2) ε2pos with Ns hs,\n cases ct (ε / 2) ε2pos with Nt ht,\n use max Ns Nt,\n intros n hn,\n have ngeNs : n ≥ Ns := le_of_max_le_left hn,\n have ngeNt : n ≥ Nt := le_of_max_le_right hn,\n calc\n |s n + t n - (a + b)| = | s n - a + (t n - b) | :\n by { congr, ring }\n ... ≤ | s n - a | + | (t n - b) | :\n abs_add _ _\n ... < ε / 2 + ε / 2 : add_lt_add (hs n ngeNs) (ht n ngeNt)\n ... = ε : by norm_num\nend\n\n/- TEXT:\nAs hints, you can use ``le_of_max_le_left`` and ``le_of_max_le_right``,\nand ``norm_num`` can prove ``ε / 2 + ε / 2 = ε``.\nAlso, it is helpful to use the ``congr`` tactic to\nshow that ``abs (s n + t n - (a + b))`` is equal to\n``abs ((s n - a) + (t n - b)),``\nsince then you can use the triangle inequality.\nNotice that we marked all the variables ``s``, ``t``, ``a``, and ``b``\nimplicit because they can be inferred from the hypotheses.\n\nProving the same theorem with multiplication in place\nof addition is tricky.\nWe will get there by proving some auxiliary statements first.\nSee if you can also finish off the next proof,\nwhich shows that if ``s`` converges to ``a``,\nthen ``λ n, c * s n`` converges to ``c * a``.\nIt is helpful to split into cases depending on whether ``c``\nis equal to zero or not.\nWe have taken care of the zero case,\nand we have left you to prove the result with\nthe extra assumption that ``c`` is nonzero.\nTEXT. -/\n-- QUOTE:\ntheorem converges_to_mul_const {s : ℕ → ℝ} {a : ℝ}\n (c : ℝ) (cs : converges_to s a) :\n converges_to (λ n, c * s n) (c * a) :=\nbegin\n by_cases h : c = 0,\n { convert converges_to_const 0,\n { ext, rw [h, zero_mul] },\n rw [h, zero_mul] },\n have acpos : 0 < abs c,\n from abs_pos.mpr h,\n sorry\nend\n-- QUOTE.\n\n-- SOLUTIONS:\ntheorem converges_to_mul_constαα {s : ℕ → ℝ} {a : ℝ}\n (c : ℝ) (cs : converges_to s a) :\n converges_to (λ n, c * s n) (c * a) :=\nbegin\n by_cases h : c = 0,\n { convert converges_to_const 0,\n { ext, rw [h, zero_mul] },\n rw [h, zero_mul] },\n have acpos : 0 < abs c,\n from abs_pos.mpr h,\n intros ε εpos, dsimp,\n have εcpos : 0 < ε / abs c,\n { apply div_pos εpos acpos },\n cases cs (ε / abs c) εcpos with Ns hs,\n use Ns,\n intros n ngt,\n calc\n |c * s n - c * a| = |c| * |s n - a| :\n by { rw [←abs_mul, mul_sub] }\n ... < |c| * (ε / |c|) :\n mul_lt_mul_of_pos_left (hs n ngt) acpos\n ... = ε : mul_div_cancel' _ (ne_of_lt acpos).symm\nend\n\n/- TEXT:\nThe next theorem is also independently interesting:\nit shows that a convergent sequence is eventually bounded\nin absolute value.\nWe have started you off; see if you can finish it.\nTEXT. -/\n-- QUOTE:\ntheorem exists_abs_le_of_converges_to {s : ℕ → ℝ} {a : ℝ}\n (cs : converges_to s a) :\n ∃ N b, ∀ n, N ≤ n → abs (s n) < b :=\nbegin\n cases cs 1 zero_lt_one with N h,\n use [N, abs a + 1],\n sorry\nend\n-- QUOTE.\n\n-- SOLUTIONS:\ntheorem exists_abs_le_of_converges_toαα {s : ℕ → ℝ} {a : ℝ}\n (cs : converges_to s a) :\n ∃ N b, ∀ n, N ≤ n → abs (s n) < b :=\nbegin\n cases cs 1 zero_lt_one with N h,\n use [N, abs a + 1],\n intros n ngt,\n calc\n |s n| = |s n - a + a| : by { congr, abel }\n ... ≤ |s n - a| + |a| : abs_add _ _\n ... < |a| + 1 : by linarith [h n ngt]\nend\n\n/- TEXT:\nIn fact, the theorem could be strengthened to assert\nthat there is a bound ``b`` that holds for all values of ``n``.\nBut this version is strong enough for our purposes,\nand we will see at the end of this section that it\nholds more generally.\n\nThe next lemma is auxiliary: we prove that if\n``s`` converges to ``a`` and ``t`` converges to ``0``,\nthen ``λ n, s n * t n`` converges to ``0``.\nTo do so, we use the previous theorem to find a ``B``\nthat bounds ``s`` beyond some point ``N₀``.\nSee if you can understand the strategy we have outlined\nand finish the proof.\nTEXT. -/\n-- QUOTE:\nlemma aux {s t : ℕ → ℝ} {a : ℝ}\n (cs : converges_to s a) (ct : converges_to t 0) :\n converges_to (λ n, s n * t n) 0 :=\nbegin\n intros ε εpos, dsimp,\n rcases exists_abs_le_of_converges_to cs with ⟨N₀, B, h₀⟩,\n have Bpos : 0 < B,\n from lt_of_le_of_lt (abs_nonneg _) (h₀ N₀ (le_refl _)),\n have pos₀ : ε / B > 0,\n from div_pos εpos Bpos,\n cases ct _ pos₀ with N₁ h₁,\n sorry\nend\n-- QUOTE.\n\n-- SOLUTIONS:\nlemma auxαα {s t : ℕ → ℝ} {a : ℝ}\n (cs : converges_to s a) (ct : converges_to t 0) :\n converges_to (λ n, s n * t n) 0 :=\nbegin\n intros ε εpos, dsimp,\n rcases exists_abs_le_of_converges_to cs with ⟨N₀, B, h₀⟩,\n have Bpos : 0 < B,\n from lt_of_le_of_lt (abs_nonneg _) (h₀ N₀ (le_refl _)),\n have pos₀ : ε / B > 0,\n from div_pos εpos Bpos,\n cases ct _ pos₀ with N₁ h₁,\n use max N₀ N₁,\n intros n ngt,\n have ngeN₀ : n ≥ N₀ := le_of_max_le_left ngt,\n have ngeN₁ : n ≥ N₁ := le_of_max_le_right ngt,\n calc\n |s n * t n - 0| = |s n| * |t n - 0| :\n by rw [sub_zero, abs_mul, sub_zero]\n ... < B * (ε / B) :\n mul_lt_mul'' (h₀ n ngeN₀) (h₁ n ngeN₁) (abs_nonneg _) (abs_nonneg _)\n ... = ε : mul_div_cancel' _ (ne_of_lt Bpos).symm\nend\n\n/- TEXT:\nIf you have made it this far, congratulations!\nWe are now within striking distance of our theorem.\nThe following proof finishes it off.\nTEXT. -/\n-- QUOTE:\ntheorem converges_to_mul {s t : ℕ → ℝ} {a b : ℝ}\n (cs : converges_to s a) (ct : converges_to t b):\n converges_to (λ n, s n * t n) (a * b) :=\nbegin\n have h₁ : converges_to (λ n, s n * (t n - b)) 0,\n { apply aux cs,\n convert converges_to_add ct (converges_to_const (-b)),\n ring },\n convert (converges_to_add h₁ (converges_to_mul_const b cs)),\n { ext, ring },\n ring\nend\n-- QUOTE.\n\n-- SOLUTIONS:\ntheorem converges_to_muLαα {s t : ℕ → ℝ} {a b : ℝ}\n (cs : converges_to s a) (ct : converges_to t b):\n converges_to (λ n, s n * t n) (a * b) :=\nbegin\n have h₁ : converges_to (λ n, s n * (t n - b)) 0,\n { apply aux cs,\n convert converges_to_add ct (converges_to_const (-b)),\n ring },\n convert (converges_to_add h₁ (converges_to_mul_const b cs)),\n { ext, ring },\n ring\nend\n\n/- TEXT:\nFor another challenging exercise,\ntry filling out the following sketch of a proof that limits\nare unique.\n(If you are feeling bold,\nyou can delete the proof sketch and try proving it from scratch.)\nTEXT. -/\n-- QUOTE:\ntheorem converges_to_unique {s : ℕ → ℝ} {a b : ℝ}\n (sa : converges_to s a) (sb : converges_to s b) :\n a = b :=\nbegin\n by_contradiction abne,\n have : abs (a - b) > 0,\n { sorry },\n let ε := abs (a - b) / 2,\n have εpos : ε > 0,\n { change abs (a - b) / 2 > 0, linarith },\n cases sa ε εpos with Na hNa,\n cases sb ε εpos with Nb hNb,\n let N := max Na Nb,\n have absa : abs (s N - a) < ε,\n { sorry },\n have absb : abs (s N - b) < ε,\n { sorry },\n have : abs (a - b) < abs (a - b),\n { sorry },\n exact lt_irrefl _ this\nend\n-- QUOTE.\n\n-- SOLUTIONS:\ntheorem converges_to_uniqueαα {s : ℕ → ℝ} {a b : ℝ}\n (sa : converges_to s a) (sb : converges_to s b) :\n a = b :=\nbegin\n by_contradiction abne,\n have : abs (a - b) > 0,\n { apply lt_of_le_of_ne,\n { apply abs_nonneg },\n intro h'',\n apply abne,\n apply eq_of_abs_sub_eq_zero h''.symm, },\n let ε := abs (a - b) / 2,\n have εpos : ε > 0,\n { change abs (a - b) / 2 > 0, linarith },\n cases sa ε εpos with Na hNa,\n cases sb ε εpos with Nb hNb,\n let N := max Na Nb,\n have absa : abs (s N - a) < ε,\n { apply hNa, apply le_max_left },\n have absb : abs (s N - b) < ε,\n { apply hNb, apply le_max_right },\n have : abs (a - b) < abs (a - b),\n calc\n abs (a - b) = abs (- (s N - a) + (s N - b)) :\n by { congr, ring }\n ... ≤ abs (- (s N - a)) + abs (s N - b) :\n abs_add _ _\n ... = abs (s N - a) + abs (s N - b) :\n by rw [abs_neg]\n ... < ε + ε : add_lt_add absa absb\n ... = abs (a - b) : by norm_num,\n exact lt_irrefl _ this\nend\n\n/- TEXT:\nWe close the section with the observation that our proofs can be generalized.\nFor example, the only properties that we have used of the\nnatural numbers is that their structure carries a partial order\nwith ``min`` and ``max``.\nYou can check that everything still works if you replace ``ℕ``\neverywhere by any linear order ``α``:\nTEXT. -/\nsection\n-- QUOTE:\nvariables {α : Type*} [linear_order α]\n\ndef converges_to' (s : α → ℝ) (a : ℝ) :=\n∀ ε > 0, ∃ N, ∀ n ≥ N, abs (s n - a) < ε\n-- QUOTE.\n\nend\n\n/- TEXT:\n.. TODO: reference to later chapter\n\nIn a later chapter, we will see that mathlib has mechanisms\nfor dealing with convergence in vastly more general terms,\nnot only abstracting away particular features of the domain\nand codomain,\nbut also abstracting over different types of convergence.\nTEXT. -/", "meta": {"author": "avigad", "repo": "mathematics_in_lean_source", "sha": "4eeb8c43762522c1acaa7f799599609d6a48f59e", "save_path": "github-repos/lean/avigad-mathematics_in_lean_source", "path": "github-repos/lean/avigad-mathematics_in_lean_source/mathematics_in_lean_source-4eeb8c43762522c1acaa7f799599609d6a48f59e/lean_source/03_Logic/source_06_Sequences_and_Convergence.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9532750460199726, "lm_q2_score": 0.9136765292901317, "lm_q1q2_score": 0.8709850355064191}}
{"text": "import PnP2023.Labs.Lab02.SelectionSort\n\n/-!\n# Selection Sort (continued)\n\nIn this lab you will show that `selectionSort` defined in the previous lab is correct, i.e. the result is sorted and preserves membership.\n\n__Note:__ If the list has duplicates the sorted list will erase these. For example, `[1, 1, 2, 3]` will be sorted to `[1, 2, 3]`. For the purposes of this exercise, this is fine.\n\n\nWe first define recursively when a list is sorted. -/\n\n/-- A direct definition of an element being below all members of a list. -/\ndef List.le_all (a : ℕ) (l : List ℕ) : Prop := ∀ b: ℕ, b ∈ l → a ≤ b\n\n/-- Whether a given list is sorted. -/\ndef List.sorted: List ℕ → Prop \n| [] => True \n| h :: t => (t.le_all h) ∧ (t.sorted)\n\n-- For your convenience, here are some definitions/theorems that may be useful in the two labs. Clicking on them takes you to the source, where you may find other useful results.\n#check List.remove -- {α : Type u_1} → [inst : DecidableEq α] → α → List α → List α\n#check List.length_cons -- ∀ {α : Type u_1} (a : α) (as : List α), List.length (a :: as) = Nat.succ (List.length as)\n#check List.mem_cons -- ∀ {α : Type u_1} {a b : α} {l : List α}, a ∈ b :: l ↔ a = b ∨ a ∈ l\n#check List.mem_of_mem_remove -- ∀ {α : Type u_1} [inst : DecidableEq α] {a b : α} {as : List α}, b ∈ List.remove a as → b ∈ as\n#check List.remove_eq_of_not_mem -- ∀ {α : Type u_1} [inst : DecidableEq α] {a : α} {as : List α}, ¬a ∈ as → List.remove a as = as\n#check List.mem_remove_iff -- ∀ {α : Type u_1} [inst : DecidableEq α] {a b : α} {as : List α}, b ∈ List.remove a as ↔ b ∈ as ∧ b ≠ a\n\n/-!\n- Problem 1: show that members of the given list are members of the sorted list(remove sorry). \n-/\n\n/-- Members of a list are members of the given sorted list -/\ntheorem selectionSort_mem_of_mem {a : ℕ} {l : List ℕ} (hyp : a ∈ l) : a ∈ selectionSort l := by \n sorry\n\n/-!\n- Problem 2: show that members of the sorted list are members of the given list(remove sorry). \n-/\n\n/-- Members of the sorted list are members of the given list -/\ntheorem selectionSort_mem_mem {a : ℕ} {l : List ℕ} (hyp : a ∈ selectionSort l) : a ∈ l := by \n sorry\n\ntheorem selectionSort_mem (l : List ℕ) (a : ℕ) : a ∈ l ↔ a ∈ selectionSort l := by \n apply Iff.intro\n · apply selectionSort_mem_of_mem\n · apply selectionSort_mem_mem\n\n/-!\n- Problems 3: Prove that the results of `selectionSort` is sorted (remove the sorry).\n-/\n\n\n/-- The result of `selectionSort` is sorted -/\ntheorem selectionSort_sorted (l : List ℕ) : (selectionSort l).sorted := \n by sorry\n ", "meta": {"author": "siddhartha-gadgil", "repo": "proofs-and-programs-2023", "sha": "9d95a5396c018b9a26ed0d27c52cd446794cd1aa", "save_path": "github-repos/lean/siddhartha-gadgil-proofs-and-programs-2023", "path": "github-repos/lean/siddhartha-gadgil-proofs-and-programs-2023/proofs-and-programs-2023-9d95a5396c018b9a26ed0d27c52cd446794cd1aa/PnP2023/Labs/Lab03/SelectionSort.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9381240160063031, "lm_q2_score": 0.9284088069931713, "lm_q1q2_score": 0.8709625985120546}}
{"text": "/- \n\n# Advanced proposition world. \n\n## Level 2: the `cases` tactic.\n\nIf `P ∧ Q` is in the goal, then we can make progress with `split`.\nBut what if `P ∧ Q` is a hypothesis? In this case, the `cases` tactic will enable\nus to extract proofs of `P` and `Q` from this hypothesis.\n\nThe lemma below asks us to prove `P ∧ Q → Q ∧ P`, that is,\nsymmetry of the \"and\" relation. The obvious first move is\n\n`intro h,`\n\nbecause the goal is an implication and this tactic is guaranteed\nto make progress. Now `h : P ∧ Q` is a hypothesis, and\n\n`cases h with p q,`\n\nwill change `h`, the proof of `P ∧ Q`, into two proofs `p : P`\nand `q : Q`. From there, `split` and `exact` will get you home.\n-/\n\n/- Lemma\nIf $P$ and $Q$ are true/false statements, then $P\\land Q\\implies Q\\land P$. \n-/\nlemma and_symm (P Q : Prop) : P ∧ Q → Q ∧ P :=\nbegin\n intro h,\n cases h with p q,\n split,\n exact q,\n exact p,\n\n\nend \n\n/- Tactic : cases\n\n## Summary:\n\n`cases` is a tactic which works on hypotheses.\nIf `h : P ∧ Q` or `h : P ↔ Q` is a hypothesis then `cases h with h1 h2` will remove `h`\nfrom the list of hypotheses and replace it with the \"ingredients\" of `h`,\ni.e. `h1 : P` and `h2 : Q`, or `h1 : P → Q` and `h2 : Q → P`. Also\nworks with `h : P ∨ Q` and `n : mynat`. \n\n## Details\n\nHow does one prove `P ∧ Q`? The way to do it is to prove `P` and to\nprove `Q`. There are hence two ingredients which go into a proof of\n`P ∧ Q`, and the `cases` tactic extracts them. \n\nMore precisely, if the local context contains\n```\nh : P ∧ Q`\n```\n\nthen after the tactic `cases h with p q,` the local context will\nchange to\n```\np : P,\nq : Q\n```\nand `h` will disappear. \n\nSimilarly `h : P ↔ Q` is proved by proving `P → Q` and `Q → P`,\nand `cases h with hpq hqp` will delete our assumption `h` and\nreplace it with\n```\nhpq : P → Q,\nhqp : Q → P\n```\n\nBe warned though -- `rw h` works with `h : P ↔ Q` (`rw` works with\n`=` and `↔`), whereas you cannot rewrite with an implication.\n\n`cases` also works with hypotheses of the form `P ∨ Q` and even\nwith `n : mynat`. Here the situation is different however. \nTo prove `P ∨ Q` you need to give either a proof of `P` *or* a proof\nof `Q`, so if `h : P ∨ Q` then `cases h with p q` will change one goal\ninto two, one with `p : P` and the other with `q : Q`. Similarly, each\nnatural is either `0` or `succ(d)` for `d` another natural, so if\n`n : mynat` then `cases n with d` also turns one goal into two,\none with `n = 0` and the other with `d : mynat` and `n = succ(d)`.\n-/", "meta": {"author": "ImperialCollegeLondon", "repo": "natural_number_game", "sha": "f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd", "save_path": "github-repos/lean/ImperialCollegeLondon-natural_number_game", "path": "github-repos/lean/ImperialCollegeLondon-natural_number_game/natural_number_game-f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd/src/game/world7/level2.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.950410982634296, "lm_q2_score": 0.9161096181702032, "lm_q1q2_score": 0.8706806424058725}}
{"text": "import Arithmetic.definition\n\nopen Nat_\n\ntheorem add_zero (m : Nat_) : m + zero = m := rfl\n\ntheorem add_succ (m n : Nat_) : m + succ n = succ (m + n) := rfl\n\ntheorem zero_add (m : Nat_) : zero + m = m := by\n induction m with\n | zero => rfl\n | succ m hyp => \n rw [add_succ]\n rw [hyp]\n\ntheorem succ_add (m n : Nat_) : succ m + n = succ (m + n) := by\n induction n with\n | zero => \n rfl\n | succ n hyp =>\n rw [add_succ]\n rw [hyp]\n rw [←add_succ]\n\n@[simp]\ntheorem add_comm (m n : Nat_) : m + n = n + m := by\n induction n with\n | zero => \n rw [zero_add]\n rfl\n | succ n hyp =>\n rw [succ_add]\n rw [add_succ] \n rw [hyp]\n \ntheorem add_assoc (l m n : Nat_) : (l + m) + n = l + (m + n) := by\n induction n with\n | zero => \n repeat (rw [add_zero])\n | succ n hyp => \n repeat (rw [add_succ])\n rw [hyp]\n\ntheorem mul_succ (m n : Nat_) : m * (succ n) = m + m * n := rfl\n\ntheorem mul_zero (n : Nat_) : n * zero = zero := rfl\n\ntheorem zero_mul (n : Nat_) : zero * n = zero := by\n induction n with\n | zero => rfl\n | succ n hyp => \n rw [mul_succ]\n rw [hyp]\n rfl\n\ntheorem succ_mul (m n : Nat_) : (succ m) * n = n + m * n := by\n induction n with\n | zero => rfl\n | succ n hyp =>\n rw [mul_succ]\n rw [hyp]\n rw [mul_succ]\n rw [succ_add]\n rw [succ_add]\n rw [←add_assoc]\n rw [←add_assoc]\n simp\n \n\ntheorem mul_comm (m n : Nat_) : m * n = n * m := by\n induction m with\n | zero => \n rw [zero_mul]\n rfl\n | succ m hyp => \n rw [mul_succ]\n rw [succ_mul]\n rw [hyp]\n \ntheorem distr_r (l m n : Nat_) : (l + m) * n = l * n + m * n := by\n induction n with\n | zero => \n repeat (rw [mul_zero])\n rfl\n | succ n hyp =>\n repeat (rw [mul_succ])\n rw [hyp]\n rw [add_assoc l (l*n) _]\n rw [add_comm (l*n) (m + m * n)]\n rw [add_assoc m (m*n) _]\n rw [add_comm (m * n) (l * n)]\n rw [←add_assoc l m _]\n\ntheorem distr_l (l m n : Nat_) : l * (m + n) = l * m + l * n := by\n rw [mul_comm]\n rw [distr_r]\n rw [mul_comm]\n rw [mul_comm n]\n \n\ntheorem mul_assoc (l m n : Nat_) : (l * m) * n = l * (m * n) := by\n induction n with\n | zero =>\n repeat (rw [mul_zero])\n | succ n hyp => \n rw [mul_succ m]\n rw [mul_succ]\n rw [distr_l]\n rw [hyp]", "meta": {"author": "WinstonHartnett", "repo": "lean-club", "sha": "2e6ef9345fa57ba48851db1e5b4145337312f33c", "save_path": "github-repos/lean/WinstonHartnett-lean-club", "path": "github-repos/lean/WinstonHartnett-lean-club/lean-club-2e6ef9345fa57ba48851db1e5b4145337312f33c/Arithmetic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9719924785827003, "lm_q2_score": 0.8947894569842487, "lm_q1q2_score": 0.8697286221037884}}
{"text": "import game.order.level05\nimport data.real.basic\nopen real\n\nnamespace xena -- hide\n\n/-\n# Chapter 2 : Order\n\n## Level 6\n\nAn interesting result to prove.\n-/\n\n\n-- You will need to use lemma \n--mul_le_mul (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : \n--a * b ≤ c * d\n\n\n\ntheorem pow_nonneg {a : ℝ } (H : 0 ≤ a) : ∀ (n : ℕ), 0 ≤ a ^ n\n| 0 := zero_le_one\n| (n+1) := mul_nonneg H (pow_nonneg _)\n\n\n/- Lemma\nFor any two non-negative real numbers $a$ and $b$, we have that\n$$a \\le b \\iff a^2 \\le b^2 $$.\n-/\ntheorem le_iff_sq_le (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b): a ≤ b ↔ a^2 ≤ b^2:=\nbegin\n split,\n intro h,\n have h1 : a^2 ≤ a * b,\n have h11 : a ≤ a,\n linarith,\n --hint next line\n have h2 := mul_le_mul h11 h ha ha,\n have h3 : a * a = a ^ 2,\n ring,\n rw h3 at h2,\n exact h2,\n have h5 : a * b ≤ b ^ 2,\n have h2 : b ≤ b,\n linarith,\n have h4 := mul_le_mul h2 h ha hb,\n rw mul_comm at h4,\n have h6 : b * b = b ^ 2,\n ring,\n rw h6 at h4,\n exact h4,\n\n exact le_trans h1 h5,\n intro j,\n \n have h1 : 0 ≤ a ^ 2, exact pow_nonneg ha 2,\n have h2 : 0 ≤ b ^ 2, exact pow_nonneg hb 2,\n have h3 := (sqrt_le h1 h2).2 j,\n have h4 := sqrt_sqr ha, \n have h5 := sqrt_sqr hb,\n rw h4 at h3,\n rw h5 at h3,\n exact h3,\nend\n\nend xena -- hide\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n/-\nsplit,\n intro h,\n have h1 : a^2 ≤ a * b, \n have h11 : a ≤ a, linarith,\n have h12 := mul_le_mul h11 h ha ha,\n have h13 : a * a = a^2, ring,\n rw h13 at h12, exact h12,\n have h2 : a * b ≤ b^2, \n have h21 : b ≤ b, linarith,\n have h22 := mul_le_mul h21 h ha hb,\n rw mul_comm at h22,\n have h23 : b * b = b^2, ring,\n rw h23 at h22, exact h22,\n exact le_trans h1 h2,\n intro h,\n have ha2 : 0 ≤ a^2, exact pow_nonneg ha 2,\n have hb2 : 0 ≤ b^2, exact pow_nonneg hb 2,\n have h1 := (sqrt_le ha2 hb2).mpr h,\n have h2a := sqrt_sqr ha, \n have h2b := sqrt_sqr hb,\n rw h2a at h1, rw h2b at h1, exact h1, done\n-/", "meta": {"author": "DavidTalone", "repo": "Real-Number-Game", "sha": "b159ae1219209c3da97566b64b8986b146ec9053", "save_path": "github-repos/lean/DavidTalone-Real-Number-Game", "path": "github-repos/lean/DavidTalone-Real-Number-Game/Real-Number-Game-b159ae1219209c3da97566b64b8986b146ec9053/src/game/order/level06.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9626731158685838, "lm_q2_score": 0.9032941988938414, "lm_q1q2_score": 0.8695770409951507}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio. Demostrar que los productos de los números naturales por\n-- números pares son pares.\n-- ---------------------------------------------------------------------\n\nimport data.nat.parity\nimport tactic\n\nopen nat\n\n-- 1ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nbegin\n intros m n hn,\n unfold even at *,\n cases hn with k hk,\n use m * k,\n calc m * n\n = m * (k + k) : congr_arg (has_mul.mul m) hk\n ... = m * k + m * k : mul_add m k k\nend\n\n-- 2ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nbegin\n intros m n hn,\n cases hn with k hk,\n use m * k,\n calc m * n\n = m * (k + k) : congr_arg (has_mul.mul m) hk\n ... = m * k + m * k : mul_add m k k\nend\n\n-- 3ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nbegin\n rintros m n ⟨k, hk⟩,\n use m * k,\n calc m * n\n = m * (k + k) : congr_arg (has_mul.mul m) hk\n ... = m * k + m * k : mul_add m k k\nend\n\n-- 4ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nbegin\n rintros m n ⟨k, hk⟩,\n use m * k,\n rw hk,\n exact mul_add m k k,\nend\n\n-- 5ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nbegin\n rintros m n ⟨k, hk⟩,\n use m * k,\n rw [hk, mul_add]\nend\n\n-- 6ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nbegin\n rintros m n ⟨k, hk⟩,\n exact ⟨m * k, by rw [hk, mul_add]⟩\nend\n\n-- 7ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\nλ m n ⟨k, hk⟩, ⟨m * k, by rw [hk, mul_add]⟩\n\n-- 8ª demostración\nexample : ∀ m n : ℕ, even n → even (m * n) :=\n assume m n ⟨k, (hk : n = k + k)⟩,\n have hmn : m * n = m * k + m * k,\n by rw [hk, mul_add],\n show ∃ l, m * n = l + l,\n from ⟨_, hmn⟩\n\n-- Comentarios:\n-- 1. Al poner el curso en la línea 1 sobre data.nat.parity y pulsar M-.\n-- se abre la teoría correspondiente.\n-- 2. Se activa la ventana de objetivos (*Lean Goal*) pulsando C-c C-g\n-- 3. Al mover el cursor sobre las pruebas se actualiza la ventana de\n-- objetivos.\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Introduccion/Ejemplo_de_demostracion.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9511422227627597, "lm_q2_score": 0.9111797003640646, "lm_q1q2_score": 0.8666614855405818}}
{"text": "/-\nCopyright (c) 2018 Chris Hughes. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Chris Hughes\nMostly based on Jeremy Avigad's choose file in lean 2\n-/\n\nimport data.nat.basic\n\nopen nat\n\ndef choose : ℕ → ℕ → ℕ\n| _ 0 := 1\n| 0 (k + 1) := 0\n| (n + 1) (k + 1) := choose n k + choose n (succ k)\n\n@[simp] lemma choose_zero_right (n : ℕ) : choose n 0 = 1 := by cases n; refl\n\n@[simp] lemma choose_zero_succ (k : ℕ) : choose 0 (succ k) = 0 := rfl\n\nlemma choose_succ_succ (n k : ℕ) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl\n\nlemma choose_eq_zero_of_lt : ∀ {n k}, n < k → choose n k = 0\n| _ 0 hk := absurd hk dec_trivial\n| 0 (k + 1) hk := choose_zero_succ _\n| (n + 1) (k + 1) hk :=\n have hnk : n < k, from lt_of_succ_lt_succ hk,\n have hnk1 : n < k + 1, from lt_of_succ_lt hk,\n by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1]\n\n@[simp] lemma choose_self (n : ℕ) : choose n n = 1 :=\nby induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)]\n\n@[simp] lemma choose_succ_self (n : ℕ) : choose n (succ n) = 0 :=\nchoose_eq_zero_of_lt (lt_succ_self _)\n\n@[simp] lemma choose_one_right (n : ℕ) : choose n 1 = n :=\nby induction n; simp [*, choose]\n\nlemma choose_pos : ∀ {n k}, k ≤ n → 0 < choose n k\n| 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial\n| (n + 1) 0 hk := by simp; exact dec_trivial\n| (n + 1) (k + 1) hk := by rw choose_succ_succ;\n exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (zero_le _)\n\n\nlemma succ_mul_choose_eq : ∀ n k, succ n * choose n k = choose (succ n) (succ k) * succ k\n| 0 0 := dec_trivial\n| 0 (k + 1) := by simp [choose]\n| (n + 1) 0 := by simp\n| (n + 1) (k + 1) :=\n by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ,\n ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul]\n\nlemma choose_mul_fact_mul_fact : ∀ {n k}, k ≤ n → choose n k * fact k * fact (n - k) = fact n\n| 0 _ hk := by simp [eq_zero_of_le_zero hk]\n| (n + 1) 0 hk := by simp\n| (n + 1) (succ k) hk :=\nbegin\n cases lt_or_eq_of_le hk with hk₁ hk₁,\n { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n :=\n by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk);\n simp [fact_succ, mul_comm, mul_left_comm],\n have h₁ : fact (n - k) = (n - k) * fact (n - succ k) :=\n by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ],\n have h₂ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n :=\n by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁);\n simp [fact_succ, mul_comm, mul_left_comm, mul_assoc],\n have h₃ : k * fact n ≤ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk),\n rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, h₂, ← add_one, add_mul, nat.mul_sub_right_distrib,\n fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] },\n { simp [hk₁, mul_comm, choose, nat.sub_self] }\nend\n\ntheorem choose_eq_fact_div_fact {n k : ℕ} (hk : k ≤ n) : choose n k = fact n / (fact k * fact (n - k)) :=\nbegin\n have : fact n = choose n k * (fact k * fact (n - k)) :=\n by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm,\n exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm\nend\n\ntheorem fact_mul_fact_dvd_fact {n k : ℕ} (hk : k ≤ n) : fact k * fact (n - k) ∣ fact n :=\nby rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _\n", "meta": {"author": "khoek", "repo": "mathlib-tidy", "sha": "866afa6ab597c47f1b72e8fe2b82b97fff5b980f", "save_path": "github-repos/lean/khoek-mathlib-tidy", "path": "github-repos/lean/khoek-mathlib-tidy/mathlib-tidy-866afa6ab597c47f1b72e8fe2b82b97fff5b980f/data/nat/choose.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9362850075259039, "lm_q2_score": 0.9252299612154571, "lm_q1q2_score": 0.8662789411998061}}
{"text": "open list\n\n/-\n#1. You are to implement a function\nthat takes a \"predicate\" function and a\nlist and that returns a new list: namely\na list that contains all and only those\nelements of the first list for which the\ngiven predicate function returns true.\n\nWe start by giving you two predicate\nfunctions. Each takes an argument, n,\nof type ℕ. The first returns true if\nand only if n < 5. The second returns\ntrue if and only if n is even. \n-/\n\n/-\nHere's a \"predicate\" function that\ntakes a natural number as an argument\nand returns true if the number is less\nthan 5 otherwise it returns false. \n-/\ndef lt_5 (n : ℕ) : bool :=\n n < 5\n\n-- example cases\n#eval lt_5 2\n#eval lt_5 6\n\n/-\nHere's another predicate function, one\nthat returns true if a given nat is even,\nand false otherwise. Note that we have\ndefined this function recursively. Zero\nis even, one is not, and (2 + n') is if\nand only if n' is.\n\nYou can think of a predicate function\nas a function that answers the question,\ndoes some value (here a nat) have some\nproperty? The properties in these two\ncases are (1) the property of being less\nthan 5, and (2) the property of being\neven.\n-/\ndef evenb : ℕ → bool\n| 0 := tt\n| 1 := ff\n| (n' + 2) := evenb n'\n\n#eval evenb 0\n#eval evenb 3\n#eval evenb 4\n#eval evenb 5\n\n/-\nWe call the function you are going to \nimplement a \"filter\" function, because\nit takes a list and returns a \"filtered\"\nversion of the list. Call you function\n\"mfilter\".\n\nIf you filter an empty list you always\nget an empty list as a result. If you\nfilter a non-empty list, l = (cons h t), \nthe returned list has h at its head if\nand only if the predicate function applied\nto h returns true, otherwise the returned\nvalue is just the filtered version of t.\n\nA. [15 points] \n\nYour task is to complete an incomplete\nversion of the definition of the mfilter\nfunction. We use a Lean construct new to \nyou: the if ... then ... else. It works\nas you would expect from your work with\nother programming languages. Replace the \nunderscores to complete the definition.\n-/\n\ndef mfilter : (ℕ → bool) → list ℕ → list ℕ \n| p [] := []\n| p (cons h t) :=\n if _ \n then (cons _ _)\n else _\n\n/-\nHere's the definition of a simple list.\n-/\n\ndef a_list := [0,1,2,3,4,5,6,7,8,9]\n\n/-\nB. [10 points]\n\nReplace the underscores in the first two \neval commands below as follows. Replace \nthe first one with an expression in which \nmfilter is used to compute a new list that \ncontains the numbers in a_list that are \nless than 5. Replace the second one with \nan expression in which mfilter is used to \ncompute a list containing even elements of \na_list. You may use the predicate functions\nwe defined above.\n\nReplace the third underscore with a similar\nexpression but where you use a λ expression\nto specify a predicate function that takes\na nat, n, and returns tt if n is equal to\nthree and false otherwise. Hint: n=3 is\nan expression that will return the desired \nbool.\n-/\n\n#eval _\n#eval _\n#eval _\n\n\n\n/-\n#2.\n\nHere's a function that takes a function, f,\nfrom ℕ to ℕ, and a value, n, of type ℕ, and\nthat returns the value that is obtained by \nsimply applying f to n. \n-/\n\ndef f_apply : (ℕ → ℕ) → ℕ → ℕ \n| f n := (f n)\n\n-- examples of its use\n#eval f_apply nat.succ 3\n#eval f_apply (λ n : ℕ, n * n) 3\n\n/-\nA. [10 points]\n\nWrite a function, f_apply_2, that takes a \nfunction, f, from ℕ to ℕ, and a value, n, \nof type ℕ, and that returns (read this\ncarefully) the value obtained by applying \nf twice to n: the result of applying f to \nthe result of applying f to n. For example,\nif f is the function that squares its nat\nargument, then (f_apply_2 f 3) returns 81,\nas f applied to 3 is 9 and f applied to 9\nis 81. \n-/\n\n-- Your answer here\n\ndef ff_apply : (ℕ → ℕ) → ℕ → ℕ \n| f n := _\n\n/-\nB. [10 points]\n\nWrite a function f_apply_k that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nn, of type ℕ, and (3) a value, k of type ℕ,\nand that returns the result of applying f \nto n k times. \n\nNote that f_apply applies f to n once and\nff_apply applies f to n twice. Your job is\nto write a function that is general in the\nsense that you specify by a parameter, k,\nhow many times to apply f.\n\nHint 1: Use recursion. Note: The result of\napplying any function, f, to n, zero times\nis just n.\n-/\n\n\n-- Answer here\n\n/-\nUse #eval to evaluate an expression in\nwhich the squaring function, expressed\nas a λ expression, is applied to 3 two \ntimes. You should be able to confirm that\nyou get the same answer given by using\nthe ff_apply function in the example above.\n-/\n\n-- Answer here\n\n\n/-\nC. [Extra Credit]\n\nWrite a function f_apply_k_fun that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nk of type ℕ, and that returns a function that,\nwhen applied to a natural number, n, returns\nthe result of applying f to n k times.\n-/\n\n\n/-\n#3: [15 points]\n\nWrite a function, mcompose, that\ntakes two functions, g and f (in that\norder), each of type ℕ → ℕ, and that \nreturns *a function* that, when applied\nto an argument, n, of type ℕ, returns\nthe result of applying g to the result \nof applying f to n.\n-/\n\n-- Answer here\n\n/-\n#4. Higher-Order Functions\n\n4A. [10 points] Provide an implementatation of\na function, map_pred that takes as its arguments \n(1) a predicate function of type ℕ → bool, (2) a\nlist of natural numbers (of type \"list nat\"), \nand that returns a list in which each ℕ value,\nn,in the argument list is replaced by true (tt) \nif the predicate returns true for a, otherwise\nfalse (ff).\n\nFor example, if the predicate function returns\ntrue if and only if its argument is zero, then\napplying map to this function and to the list\n[0,1,2,0,1,0] must return [tt,ff,ff,tt,ff,tt].\n\n\nTest your code by using #eval or #reduce to evaluate\nan expression in which map_pred is applied to \nsuch an \"is_zero\" predicate function and to the\nlist 0,1,2,0,1,0]. Express the predicate function\nas a lambda abstraction within the #eval command.\n\nNOTE: You will have to use list.nil and list.cons\nto refer to the nil and cons constructors of the\nlibrary-provided list data type, as you already\nhave definitions for list and cons in the current\nnamespace.\n-/\n\n-- Answer here\n\n\n/-\n4B. [10 points] Implement a function, reduce_or, \nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if there is at least one true value in the list,\notherwise ff. Note: the Lean libraries provide the\nfunction \"bor\" to compute \"b1 or b2\", where b1 and\nb2 are Booleans. We recommend that you include\ntests of your solution.\n-/\n\n-- example\n#reduce bor tt tt\n\n-- Answer here\n\n\n/-\n4C. [10points] Implement a function, reduce_and,\nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if every value in the list is true, otherwise ff.\n-/\n\n-- Note: band implements the Boolean \"and\" function\n#reduce band tt tt\n\n-- Answer here\n\n\n/-\n4D. [10 points] Define a function, all_zero, that \ntakes a list of nat values and returns true if and \nonly if they are all zero. Express your answer using \nmap and reduce functions that you have previously\ndefined above. Again we recommend that you test your \nsolution.\n-/\n\n-- Answer here (replace the _'s as needed)\n\ndef all_zero : list nat → bool\n| [] := _\n| _ := _\n\n/-\nSome tests\n-/\n#reduce all_zero []\n#reduce all_zero [0,0,0,0]\n#reduce all_zero [1,0,0,0]\n#reduce all_zero [0,1,0,0]\n#reduce all_zero [1,0,0,1]\n", "meta": {"author": "kevinsullivan", "repo": "uva-cs-dm-f19", "sha": "09a950752884bd7ade4be33e9e89a2c4b1927167", "save_path": "github-repos/lean/kevinsullivan-uva-cs-dm-f19", "path": "github-repos/lean/kevinsullivan-uva-cs-dm-f19/uva-cs-dm-f19-09a950752884bd7ade4be33e9e89a2c4b1927167/assignments/hw5_higher_order_functions.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9648551495568569, "lm_q2_score": 0.8976952914230972, "lm_q1q2_score": 0.8661459246625187}}
{"text": "-- Exercise 6\nvariables (real : Type) [ordered_ring real]\nvariables (log exp : real → real)\nvariable log_exp_eq : ∀ x, log (exp x) = x\nvariable exp_log_eq : ∀ {x}, x > 0 → exp (log x) = x\nvariable exp_pos : ∀ x, exp x > 0\nvariable exp_add : ∀ x y, exp (x + y) = exp x * exp y\n\n-- this ensures the assumptions are available in tactic proofs\ninclude log_exp_eq exp_log_eq exp_pos exp_add\n\nexample (x y z : real) :\n exp (x + y + z) = exp x * exp y * exp z :=\nby rw [exp_add, exp_add]\n\nexample (y : real) (h : y > 0) : exp (log y) = y :=\nexp_log_eq h\n\ntheorem log_mul {x y : real} (hx : x > 0) (hy : y > 0) :\n log (x * y) = log x + log y :=\nbegin\n have z : exp (log (x * y)) = exp (log x + log y) :=\n calc\n exp (log (x * y)) = x * y : by rw exp_log_eq (mul_pos hx hy) \n ... = exp (log x) * exp (log y) : by rw [exp_log_eq hx, exp_log_eq hy]\n ... = exp (log x + log y) : by rw exp_add,\n\n have pf : log (exp (log (x * y))) = log (exp (log x + log y)) :=\n congr_arg log z,\n\n have lhs := calc\n log (exp (log (x * y))) = log (x * y) : by rw log_exp_eq,\n\n have rhs := calc\n log (exp (log x + log y)) = log x + log y : by rw log_exp_eq,\n\n show log (x * y) = log x + log y, from calc\n log (x * y) = log x + log y : by rw [eq.symm(lhs), pf, rhs],\nend\n\n\n-- Exercise 7\n-- Prove the theorem below, using only the \n-- ring properties of ℤ enumerated in Section 4.2 and the theorem sub_self.\n\n#check sub_self\n\nexample (x : ℤ) : x * 0 = 0 :=\ncalc\n x * 0 = x * (x - x) : by rw sub_self\n ... = x * x - x * x : by rw mul_sub\n ... = 0 : by rw sub_self\n\n-- Important remark:\n-- Why this task was not automated?\n-- Example:\n-- `search solution in graph of states` using set_of_lemmas\n-- [sub_self as rewrite_rule, mul_sub as rewrite_rule, -default_addition_set, ...]\n-- `-`lemma_name means not using this lemma\n-- More general:\n-- Metarules₁ = [action ∈ {sub_self, mul_sub} ∪ default_boolean]\n-- search_in_lib using Metarules₁\n--\n-- * Also we can write proof about Metarules like for ordinary theorems\n\nset_option trace.simplify.rewrite true\n\nexample (x : ℤ) : x * 0 = 0 :=\ncalc\n x * 0 = 0 : by simp\n -- [mul_zero]: x * 0 ==> 0\n -- [eq_self_iff_true]: 0 = 0 ==> true", "meta": {"author": "mathprocessing", "repo": "lean_mathlib_examples", "sha": "743c6456c0a3219dd1722efdd31ee6f3a113818a", "save_path": "github-repos/lean/mathprocessing-lean_mathlib_examples", "path": "github-repos/lean/mathprocessing-lean_mathlib_examples/lean_mathlib_examples-743c6456c0a3219dd1722efdd31ee6f3a113818a/src/exercises/real_rings.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9173026618464796, "lm_q2_score": 0.9433475782756537, "lm_q1q2_score": 0.8653352445986874}}
{"text": "/-\nKey differences: Predicate vs propositional logic.\n\n(1) In propositional logic, variables range over\nBoolean truth values. A variable is either Boolean\ntrue or Boolean false. In predicate logic, variables\ncan range over values of many types. A variable can\nrepresent a Boolean truth value, but also a natural\nnumber, a string, a person, a chemical, or whatever.\nPredicate logic thus has far greater expressiveness\nthan propositional logic.\n\n(2) Predicate logic has quantifiers: forall (∀) and\nexists (∃). These allow one to express the ideas \nthat some proposition is true of, every, or of at\nleast one, object of some kind.\n\n(3) Predicate logic has predicates, which you can\nthink of as propositions with parameters, such that\nthe application of a predicate to argument values \nof its parameter types gives rise to a specific\nproposition about those specific argument values.\nIn general, some of these propositions will be true\nand some won't be.\n\nHere's an example of a proposition, in predicate\nlogic, that illustrates all three points. It \nasserts, in precise mathematical terms, that\nevery natural number is equal to itself. Even\nin this simple example, we see that predicate\nlogic includes (1) quantifiers, (2) variables\nthat can range over domains other than bool,\nand (3) predicates. Here eq is a 2-argument\npredicate that, when applied to two values,\nyields the proposition that the first one\nequals the second. THe more common notation\nfor eq a b is a = b. \n-/\ndef every_nat_eq_itself := ∀ (n : ℕ), eq n n\n\n-- (_ = _) is an infix notation for (eq _ _)\ndef every_nat_eq_itself' := ∀ (n : ℕ), n = n\n\n/-\nThis example illustrates all three points. First,\nthe logical variable, n, ranges over all values of\ntype nat. Second, the proposition has a universal\nquantifier. Third, in the context of the \"binding\"\nof n to some arbitrary but specific natural number\nby the ∀, the overall proposition then asserts that\nthe proposition n = n is true, using a two-argument\npredicate, eq, applied to n and n as arguments, to\nstate this proposition.\n-/\n\n/-\nIt will be important for you to learn both how\nto translate predicate logical expressions into\nnatural language (here, English), and to express\nconcepts stated in English in predicate logic.\n\nLet's start with some expressions in predicate\nlogic. We imagine a world in which there are\npeople and a relation between people that we\nwill called \"likes _ _\", where the proposition\n\"likes p q\" asserts that person p likes person\nq.\n\nWhat, then, do the following proposition mean,\ntranslating from predicate logic to English?\n\n∀ (p : Person), ∃ (q : Person), likes p q\n\n∀ (p : Person), ∃ (q : Person), ¬ likes p q\n\n∃ (p : Person), ∀ (q : Person), likes p q\n\n∃ (p : Person), ∀ (q : Person), likes q p\n\n∃ (p : Person), ∀ (q : Person), ¬ likes q p\n\n(∀ (p : Person), ∃ (q : Person), ¬ likes p q) → \n(∃ (p : Person), ∀ (q : Person), ¬ likes q p)\n\n(∃ (p : Person), ∀ (q : Person), ¬ likes q p) → \n(∀ (p : Person), ∃ (q : Person), ¬ likes p q)\n-/\n\n/-\nOne of the last two propositions is valid. \nWhich one? \n\nIn predicate logic, we can no longer evaluate\nthe validity of a proposition using truth\ntables. What we need will instead is a proof. \n\nA proof is an object that we will accept as\ncompelling evidence for the truth of a given\nproposition.\n\nIt will also be important for you to learn how\nto express proofs in both natural language and\nformally, in terms of the \"inference rules\" of\nwhat we call \"natural deduction.\" \n\nLet's go for a natural language proof of this:\n\n(∃ (p : Person), ∀ (q : Person), ¬ likes q p) → \n(∀ (p : Person), ∃ (q : Person), ¬ likes p q)\n\nWhat is says in plain English is that if there \nis a person no one likes, then everyone dislikes\nsomebody.\n\nTo prove such an implication we will assume\nthat there is a proof of the premise (and\nthat the premise is therefore true), and we\nwill then show that given this assumption we\ncan construct a proof of the conclusion.\n\nStep 1: To prove the proposition, we assume\n(∃ (p : Person), ∀ (q : Person), ¬ likes q p).\nGiven (in the context of) this assumption, it\nremains to be proved that (∀ (p : Person), \n∃ (q : Person), ¬ likes p q).\n\nStep 2: We've assumed that there is someone\nno one likes. We give that someone (whoever it\nis) a name: let's say, w. What we know about \nw is that ∀ (q : Person), ¬ likes q w. That\nis, we know everyone dislikes w in particular.\n\nStep 3: To prove our remaining goal, that\n(∀ (p : Person), ∃ (q : Person), ¬ likes p q), \nfor each person p, we select w as a \"witness\"\nfor q: a person that we expect will make the\nremaining proposition true. All that remains\nto be proved now is that ¬ likes p w.\n\nStep 4: To prove this, we just apply the fact\nfrom step 2 that ∀ (q : Person), ¬ likes q w,\ni.e., that *everyone* dislikes w, to conclude\nthat p, in particular, dislikes w. \n\nWe've thus shown that if there is someone\neveryone dislikes then everyone dislikes \nsomeone. For every (∀) person, p, it is\nenough to point to w to show there is (∃) \nsomeone that p dislikes. While p might\ndislike other people, we can be sure that\nhe or she dislikes w because w is someone\nwhom everyone dislikes. \n\nQED. [Latin for Quod Est Demonstratum. Thus\nit is shown/proved/demonstrated.]\n-/\n\n", "meta": {"author": "kevinsullivan", "repo": "uva-cs-dm-f19", "sha": "09a950752884bd7ade4be33e9e89a2c4b1927167", "save_path": "github-repos/lean/kevinsullivan-uva-cs-dm-f19", "path": "github-repos/lean/kevinsullivan-uva-cs-dm-f19/uva-cs-dm-f19-09a950752884bd7ade4be33e9e89a2c4b1927167/instructor-notes/2019.10.29.props_and_proofs/2019.10.29.props_and_proofs.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9441768635777511, "lm_q2_score": 0.9161096216057903, "lm_q1q2_score": 0.8649695092211555}}
{"text": "/-\nAs discussed in the last unit, a \npredicate is a proposition with\none more more arguments. \n\nIn the last unit, we studied \npredicates with one argument. We \nsaw that such a predicate can be \nseen as specifying a *property* \nof values of its argument type.\nExamples included the properties\nof being even, being equal to 0,\nbeing a perfect square, or of (a\nperson) being from Cville. \n\nWe now look at predicates with\nmultiple arguments. We emphasize\nthe idea that such predicates can\nbe used to specify properties of \n*tuples* (e.g., pairs) of values. \nWe refer to properties involving \ntuples as relations.\n\nAs an example we can consider \nequality as a relation between\ntwo values. A relation involving\ntwo values is said to be a binary\nrelation. We could represent the \n\"equals\" relation as a predicate \nwith two arguments, n and m, and\nrules that provide proofs of the\nproposition \"equals m n\" if and\nonly if m = n. We can represent \nthis predicate as a function \nthat now takes two arguments and \nreturns Prop.\n-/\n\ndef areEqual : ℕ → ℕ → Prop :=\n λ n m, n = m\n\n#check areEqual 0 0\n#reduce areEqual 0 0\n\nlemma zeqz : areEqual 0 0 := eq.refl 0\n\n#check areEqual 2 3\n#reduce areEqual 2 3\n\n/-\nBe sure you're comfortable with\nthese ideas. We define areEqual\nas a function that takes n and m as\narguments (of type ℕ) and that then\nreturns the *proposition*, n = m.\nBe clear that this function does \nNOT return a proof. It returns a\nproposition that might or might \nnot be true! \n\nThe single predicate, which most\nmathematicians would write as \nareEqual(n,m), with two arguments,\ndefines an infinity of propositions,\neach one about one specific pair of\nnatural numbers. \n\nSome of these propositions have \nproofs and thus are true. Others \ndo not, and are not true. Such a\npredicate thus \"picks out\" a subset\nof all possible pairs, namely those\nfor which its proposition is true.\nThese pairs include (0, 0) but not\n(2, 3). \n\nSuch a set of pairs is what we think \nof as the relation that the predicate\nspecifies. In particular, areEqual\nspecifies a \"binary\" relation, the\nset of all pairs of natural numbers\nthat are equal to themselves. \n\nLet's prove that a few elements are\nin, and that another one is not in,\nthe areEqual(n,m) relation.\n-/\n\n-- (1, 1) is in the relation\nexample : areEqual 1 1 :=\n eq.refl 1\n\n-- (2, 2) is in the relation\nexample : areEqual 2 2 :=\n eq.refl 2\n\n-- but (0, 1) is not in it\nexample : ¬(areEqual 0 1) :=\n λ zeqo, nat.no_confusion zeqo\n\n\n/-\nEXERCISE [Worked]: Define a predicate\nisSquare that expresses the relation\nbetween two natural numbers that the\nsecond natural number is the square\nof the first and prove that the pair,\n(3, 9), is in the isSquare, relation.\n-/\n\n-- Answer\n\ndef isSquare: ℕ → ℕ → Prop :=\n λ n m, n * n = m\n\nexample : isSquare 3 9 :=\nbegin\nunfold isSquare,\nexact rfl,\nend\n\n\n\n/-\nEXERCISE: In lean, the function,\nstring.length returns the length\nof a given string. Specify a new\npredicate sHasLenN taking a string\nand a natural number as arguments\nthat asserts that a given string\nhas the given length. Write the\nfunction using lambda notation to\nmake the type of the predicate as\nclear as possible.\n-/\n\n#eval string.length \"Hello\"\n\n\n\n\n-- answer here\n\ndef sHasLenN (s :string) (n : ℕ) : Prop :=\n s.length = n\n\nexample : sHasLenN \"Hello\" 5 := \nbegin\nunfold sHasLenN,\nexact rfl,\nend\n\n\n\ndef pythagoreanTriple : ℕ → ℕ → ℕ → Prop :=\n λ a b h, a^2 + b^2 = h^2\n\nexample : pythagoreanTriple 3 4 5 :=\nbegin\nunfold pythagoreanTriple,\nexact rfl,\nend ", "meta": {"author": "kevinsullivan", "repo": "cs-dm", "sha": "bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c", "save_path": "github-repos/lean/kevinsullivan-cs-dm", "path": "github-repos/lean/kevinsullivan-cs-dm/cs-dm-bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c/10b_Relations/00_intro.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.962673111584966, "lm_q2_score": 0.897695283896349, "lm_q1q2_score": 0.8641871122036477}}
{"text": "import data.real.basic\n\n/-\nIn the previous file, we saw how to rewrite using equalities.\nThe analogue operation with mathematical statements is rewriting using\nequivalences. This is also done using the `rw` tactic.\nLean uses ↔ to denote equivalence instead of ⇔.\n\nIn the following exercises we will use the lemma:\n\n sub_nonneg {x y : ℝ} : 0 ≤ y - x ↔ x ≤ y\n\nThe curly braces around x and y instead of parentheses mean Lean will always try to figure out what\nx and y are from context, unless we really insist on telling it (we'll see how to insist much later).\nLet's not worry about that for now.\n\nIn order to announce an intermediate statement we use:\n\n have my_name : my statement,\n\nThis triggers the apparition of a new goal: proving the statement. After this is done,\nthe statement becomes available under the name `my_name`.\nWe can focus on the current goal by typing tactics between curly braces.\n-/\n\nexample {a b c : ℝ} (hab : a ≤ b) : c + a ≤ c + b :=\nbegin\n rw ← sub_nonneg,\n have key : (c + b) - (c + a) = b - a, -- Here we introduce an intermediate statement named key\n { ring, }, -- and prove it between curly braces\n rw key, -- we can now use the key statement\n rw sub_nonneg,\n exact hab,\nend\n\n/-\nOf course the previous lemma is already in the core library, named `add_le_add_left`, so we can use it below.\n\nLet's prove a variation (without invoking commutativity of addition since this would spoil our fun).\n-/\n\n-- 0009\nexample {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c :=\nbegin\n have key : c + a ≤ c + b, {apply add_le_add_left hab},\n rw add_comm c a at key,\n rw add_comm c b at key,\n exact key\nend\n\n\n/-\nLet's see how we could use this lemma. It is already in the core library, under the name `add_le_add_right`:\n\n add_le_add_right {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c\n\nThis can be read as: \"add_le_add_right is a function that will take as input real numbers a and b, an\nassumption `hab` claiming a ≤ b and a real number c, and will output a proof of a + c ≤ b + c\".\n\nIn addition, recall that curly braces around a b mean Lean will figure out those arguments unless we\ninsist to help. This is because they can be deduced from the next argument `hab`.\nSo it will be sufficient to feed `hab` and c to this function.\n-/\n\nexample {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : by exact add_le_add_right ha b,\nend\n\n/-\nIn the second line of the above proof, we need to prove 0 + b ≤ a + b.\nThe proof after the colon says: this is exactly lemma `add_le_add_right` applied to ha and b.\nActually the `calc` block expects proof terms, and the `by` keyword is used to tell Lean we will use tactics\nto build such a proof term. But since the only tactic used in this block is `exact`, we can skip\ntactics entirely, and write:\n-/\n\nexample (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by rw zero_add b\n ... ≤ a + b : add_le_add_right ha b,\nend\n\n/- Let's do a variant. -/\n\n-- 0010\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n have key : a+0 ≤ a+b, {by apply add_le_add_left hb a},\n rw (add_zero a) at key,\n exact key\nend\n\n\n/-\nThe two preceding examples are in the core library :\n\n le_add_of_nonneg_left {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b\n le_add_of_nonneg_right {a b : ℝ} (hb : 0 ≤ b) : a ≤ a + b\n\nAgain, there won't be any need to memorize those names, we will\nsoon see how to get rid of such goals automatically.\nBut we can already try to understand how their names are built:\n\"le_add\" describe the conclusion \"less or equal than some addition\"\nIt comes first because we are focussed on proving stuff, and\nauto-completion works by looking at the beginning of words.\n\"of\" introduces assumptions. \"nonneg\" is Lean's abbreviation for non-negative.\n\"left\" or \"right\" disambiguates between the two variations.\n\nLet's use those lemmas by hand for now.\n-/\n\n-- 0011\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n calc\n 0 ≤ b : by exact hb\n ... ≤ a + b : by apply le_add_of_nonneg_left ha\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0012\nexample (a b x y : ℝ) (hab : a ≤ b) (hcd : x ≤ y) : a + x ≤ b + y :=\nbegin\n calc\n a + x ≤ b + x : by exact add_le_add_right hab x\n ... ≤ b + y : by exact add_le_add_left hcd b\nend\n\n/-\nIn the above examples, we prepared proofs of assumptions of our lemmas beforehand, so\nthat we could feed them to the lemmas. This is called forward reasonning.\nThe `calc` proofs also belong to this category.\n\nWe can also announce the use of a lemma, and provide proofs after the fact, using\nthe `apply` tactic. This is called backward reasonning because we get the conclusion\nfirst, and provide proofs later. Using `rw` on the goal (rather than on an assumption\nfrom the local context) is also backward reasonning.\n\nLet's do that using the lemma\n\n mul_nonneg' {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : 0 ≤ x*y\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n have key : b*c - a*c = (b - a)*c,\n { ring },\n rw key,\n apply mul_nonneg', -- Here we don't provide proofs for the lemma's assumptions\n -- Now we need to provide the proofs.\n { rw sub_nonneg,\n exact hab },\n { exact hc },\nend\n\n/-\nLet's prove the same statement using only forward reasonning: announcing stuff,\nproving it by working with known facts, moving forward.\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a, by\n { rw ← sub_nonneg at hab,\n exact hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg' hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rw h₂ at h₁,\n exact h₁, },\n rw sub_nonneg at h₃,\n exact h₃,\nend\n\n/-\nOne reason why the backward reasoning proof is shorter is because Lean can\ninfer of lot of things by comparing the goal and the lemma statement. Indeed\nin the `apply mul_nonneg'` line, we didn't need to tell Lean that x = b - a\nand y = c in the lemma. It was infered by \"unification\" between the lemma\nstatement and the goal.\n\nTo be fair to the forward reasoning version, we should introduce a convenient\nvariation on `rw`. The `rwa` tactic performs rewrite and then looks for an\nassumption matching the goal. We can use it to rewrite our latest proof as:\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rwa ← sub_nonneg at hab },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg' hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rwa h₂ at h₁, },\n rwa sub_nonneg at h₃,\nend\n\n/-\nLet's now combine forward and backward reasonning, to get our most\nefficient proof of this statement. Note in particular how unification is used\nto know what to prove inside the parentheses in the `mul_nonneg'` arguments.\n-/\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n calc 0 ≤ (b - a)*c : mul_nonneg' (by rwa sub_nonneg) hc\n ... = b*c - a*c : by ring,\nend\n\n/-\nLet's now practice all three styles using:\n\n mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b\n\n sub_nonpos {a b : α} : a - b ≤ 0 ↔ a ≤ b\n-/\n\n/- First using mostly backward reasonning -/\n-- 0013\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n rw ← sub_nonneg,\n have key : a*c - b*c = (a - b)*c,\n { ring },\n rw key,\n apply mul_nonneg_of_nonpos_of_nonpos,\n {rwa [<-sub_nonpos] at hab,},\n {exact hc}\nend\n\n/- Using forward reasonning -/\n-- 0014\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n rw ← sub_nonneg,\n have key : a*c - b*c = (a - b)*c, { ring },\n rw key,\n have h₁ : a - b ≤ 0, by rwa [<- sub_nonpos] at hab,\n exact mul_nonneg_of_nonpos_of_nonpos h₁ hc,\nend\n\n/-- Using a combination of both, with a `calc` block -/\n-- 0015\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\nsorry\nend\n\n/-\nLet's now move to proving implications. Lean denotes implications using\na simple arrow →, the same it uses for functions (say denoting the type of functions\nfrom ℕ to ℕ by ℕ → ℕ). This is because it sees a proof of P ⇒ Q as a function turning\na proof of P into a proof Q.\n\nMany of the examples that we already met are implications under the hood. For instance we proved\n\n le_add_of_nonneg_left (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b\n\nBut this can be rephrased as\n\n le_add_of_nonneg_left (a b : ℝ) : 0 ≤ a → b ≤ a + b\n\nIn order to prove P → Q, we use the tactic `intros`, followed by an assumption name.\nThis creates an assumption with that name asserting that P holds, and turns the goal into Q.\n\nLet's check we can go from our old version of `le_add_of_nonneg_left` to the new one.\n\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nbegin\n intros ha,\n exact le_add_of_nonneg_left ha,\nend\n\n/-\nActually Lean doesn't make any difference between those two versions. It is also happy with\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nle_add_of_nonneg_left\n\n/- No tactic state is shown in the above line because we don't even need to enter\ntactic mode using `begin` or `by`.\n\nLet's practise using `intros`. -/\n\n-- 0016\nexample (a b : ℝ): 0 ≤ b → a ≤ a + b :=\nbegin\n intros ha,\n exact le_add_of_nonneg_right ha\nend\n\n\n\n/-\nWhat about lemmas having more than one assumption? For instance:\n\n add_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b\n\nA natural idea is to use the conjunction operator (logical AND), which Lean denotes\nby ∧. Assumptions built using this operator can be decomposed using the `cases` tactic,\nwhich is a very general assumption-decomposing tactic.\n-/\nexample {a b : ℝ} : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b :=\nbegin\n intros hyp,\n cases hyp with ha hb,\n exact add_nonneg ha hb,\nend\n\n/-\nNeeding that intermediate line invoking `cases` shows this formulation is not what is used by\nLean. It rather sees `add_nonneg` as two nested implications:\nif a is non-negative then if b is non-negative then a+b is non-negative.\nIt reads funny, but it is much more convenient to use in practice.\n-/\nexample {a b : ℝ} : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nadd_nonneg\n\n/-\nThe above pattern is so common that implications are defined as right-associative operators,\nhence parentheses are not needed above.\n\nLet's prove that the naive conjunction version implies the funny Lean version. For this we need\nto know how to prove a conjunction. The `split` tactic creates two goals from a conjunction goal.\nIt can also be used to create two implication goals from an equivalence goal.\n-/\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha hb,\n apply H,\n split,\n exact ha,\n exact hb,\nend\n\n/-\nLet's practice `cases` and `split`. In the next exercise, P, Q and R denote\nunspecified mathematical statements.\n-/\n\n-- 0017\nexample (P Q R : Prop) : P ∧ Q → Q ∧ P :=\nbegin\n intro hpq,\n cases hpq with hp hq,\n split,\n exact hq,\n exact hp,\nend\n\n/-\nOf course using `split` only to be able to use `exact` twice in a row feels silly. One can\nalso use the anonymous constructor syntax: ⟨ ⟩\nBeware those are not parentheses but angle brackets. This is a generic way of providing\ncompound objects to Lean when Lean already has a very clear idea of what it is waiting for.\n\nSo we could have replaced the last three lines by:\n exact ⟨hQ, hP⟩\n\nWe can also combine the `intros` steps. We can now compress our earlier proof to:\n-/\n\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha hb,\n exact H ⟨ha, hb⟩,\nend\n\n/-\nThe anonymous contructor trick actually also works in `intros` provided we use\nits recursive version `rintros`. So we can replace\n intro h,\n cases h with h₁ h₂\nby\n rintros ⟨h₁, h₂⟩,\nNow redo the previous exercise using all those compressing techniques, in exactly two lines. -/\n\n-- 0018\nexample (P Q R : Prop): P ∧ Q → Q ∧ P :=\nbegin\n rintros ⟨hp, hq⟩,\n exact ⟨hq, hp⟩\nend\n\n/-\nWe are ready to come back to the equivalence between the different formulations of\nlemmas having two assumptions. Remember the `split` tactic can be used to split\nan equivalence into two implications.\n-/\n\n-- 0019\nexample (P Q R : Prop) : (P ∧ Q → R) ↔ (P → (Q → R)) :=\nbegin\n split,\n assume (hpqr : P ∧ Q → R) (hp : P) (hq : Q),\n show R, by {apply hpqr, exact ⟨hp,hq⟩},\n assume (hpqr : P → (Q → R)) (hpq : P ∧ Q),\n show R, by {exact hpqr hpq.1 hpq.2}\nend\n\n/-\nIf you used more than five lines in the above exercise then try to compress things\n(without simply removing line ends).\n\nOne last compression technique: given a proof h of a conjunction P ∧ Q, one can get\na proof of P using h.left and a proof of Q using h.right, without using cases.\nOne can also use the more generic (but less legible) names h.1 and h.2.\n\nSimilarly, given a proof h of P ↔ Q, one can get a proof of P → Q using h.mp\nand a proof of Q → P using h.mpr (or the generic h.1 and h.2 that are even less legible\nin this case).\n\nBefore the final exercise in this file, let's make sure we'll be able to leave\nwithout learning 10 lemma names. The `linarith` tactic will prove any equality or\ninequality or contradiction that follows by linear combinations of assumptions from the\ncontext (with constant coefficients).\n-/\n\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n linarith,\nend\n\n/-\nNow let's enjoy this for a while.\n-/\n\n-- 0020\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n linarith\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0021\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d :=\nbegin\n linarith\nend\n\n\n/-\nFinal exercise\n\nIn the last exercise of this file, we will use the divisibility relation on ℕ,\ndenoted by ∣ (beware this is a unicode divisibility bar, not the ASCII pipe character),\nand the gcd function.\n\nThe definitions are the usual ones, but our goal is to avoid using these definitions and\nonly use the following three lemmas:\n\n dvd_refl (a : ℕ) : a ∣ a\n\n dvd_antisymm {a b : ℕ} : a ∣ b → b ∣ a → a = b :=\n\n dvd_gcd_iff {a b c : ℕ} : c ∣ gcd a b ↔ c ∣ a ∧ c ∣ b\n-/\n\n-- All functions and lemmas below are about natural numbers.\nopen nat\n\n-- 0022\nexample (a b : ℕ) : a ∣ b ↔ gcd a b = a :=\nbegin\n have hg : gcd a b ∣ gcd a b, from dvd_refl (gcd a b),\n rw dvd_gcd_iff at hg,\n cases hg with hg1 hg2,\n split,\n {assume hab : a ∣ b,\n have h2 : a ∣ gcd a b, from dvd_gcd_iff.mpr ⟨dvd_refl a, hab⟩,\n exact dvd_antisymm hg1 h2\n },\n {assume hgab : gcd a b = a,\n rw hgab at hg2,\n exact hg2,\n }\nend\n\n", "meta": {"author": "kris-brown", "repo": "theorem_proving_in_lean", "sha": "774460c21bf857daff158210741bd88d1c8323cd", "save_path": "github-repos/lean/kris-brown-theorem_proving_in_lean", "path": "github-repos/lean/kris-brown-theorem_proving_in_lean/theorem_proving_in_lean-774460c21bf857daff158210741bd88d1c8323cd/tutorial_src/exercises/02_iff_if_and.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9372107949104866, "lm_q2_score": 0.9219218343187416, "lm_q1q2_score": 0.8640350951872018}}
{"text": "import .love05_inductive_predicates_demo\n\n\n/-! # LoVe Exercise 5: Inductive Predicates -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1: Even and Odd\n\nThe `even` predicate is true for even numbers and false for odd numbers. -/\n\n#check even\n\n/-! We define `odd` as the negation of `even`: -/\n\ndef odd (n : ℕ) : Prop :=\n ¬ even n\n\n/-! 1.1. Prove that 1 is odd and register this fact as a simp rule.\n\nHint: `cases'` is useful to reason about hypotheses of the form `even …`. -/\n\n@[simp] lemma odd_1 :\n odd 1 :=\nbegin\n intro h,\n cases' h\nend\n\n/-! 1.2. Prove that 3 and 5 are odd. -/\n\nlemma odd_3 :\n odd 3 :=\nbegin\n intro h,\n cases' h,\n cases' h\nend\n\nlemma odd_5 :\n odd 5 :=\nbegin\n intro h,\n cases' h,\n cases' h,\n cases' h\nend\n\n/-! 1.3. Complete the following proof by structural induction. -/\n\nlemma even_two_times :\n ∀m : ℕ, even (2 * m)\n| 0 := even.zero\n| (m + 1) :=\n begin\n apply even.add_two,\n simp,\n apply even_two_times\n end\n\n/-! 1.4. Complete the following proof by rule induction.\n\nHint: You can use the `cases'` tactic (or `match … with`) to destruct an\nexistential quantifier and extract the witness. -/\n\nlemma even_imp_exists_two_times :\n ∀n : ℕ, even n → ∃m, n = 2 * m :=\nbegin\n intros n hen,\n induction' hen,\n case zero {\n apply exists.intro 0,\n refl },\n case add_two : k hek ih {\n cases' ih with w hk,\n apply exists.intro (w + 1),\n rw hk,\n linarith }\nend\n\n/-! 1.5. Using `even_two_times` and `even_imp_exists_two_times`, prove the\nfollowing equivalence. -/\n\nlemma even_iff_exists_two_times (n : ℕ) :\n even n ↔ ∃m, n = 2 * m :=\nbegin\n apply iff.intro,\n { apply even_imp_exists_two_times },\n { intro h,\n cases' h,\n simp *,\n apply even_two_times }\nend\n\n/-! 1.6 (**optional**). Give a structurally recursive definition of `even` and\ntest it with `#eval`.\n\nHint: The negation operator on `bool` is called `not`. -/\n\ndef even_rec : nat → bool\n| 0 := tt\n| (n + 1) := not (even_rec n)\n\n#eval even_rec 0\n#eval even_rec 1\n#eval even_rec 2\n#eval even_rec 3\n#eval even_rec 4\n\n\n/-! ## Question 2: Tennis Games\n\nRecall the inductive type of tennis scores from the demo: -/\n\n#check score\n\n/-! 2.1. Define an inductive predicate that returns true if the server is ahead\nof the receiver and that returns false otherwise. -/\n\ninductive srv_ahead : score → Prop\n| vs {m n : ℕ} : m > n → srv_ahead (score.vs m n)\n| adv_srv : srv_ahead score.adv_srv\n| game_srv : srv_ahead score.game_srv\n\n/-! 2.2. Validate your predicate definition by proving the following lemmas. -/\n\nlemma srv_ahead_vs {m n : ℕ} (hgt : m > n) :\n srv_ahead (score.vs m n) :=\nsrv_ahead.vs hgt\n\nlemma srv_ahead_adv_srv :\n srv_ahead score.adv_srv :=\nsrv_ahead.adv_srv\n\nlemma not_srv_ahead_adv_rcv :\n ¬ srv_ahead score.adv_rcv :=\nbegin\n intro h,\n cases' h\nend\n\nlemma srv_ahead_game_srv :\n srv_ahead score.game_srv :=\nsrv_ahead.game_srv\n\nlemma not_srv_ahead_game_rcv :\n ¬ srv_ahead score.game_rcv :=\nbegin\n intro h,\n cases' h\nend\n\n/-! 2.3. Compare the above lemma statements with your definition. What do you\nobserve? -/\n\n/-! The positive lemmas correspond exactly to the introduction rules of the\ndefinition. By contrast, the negative lemmas have no counterparts in the\ndefinition. -/\n\n\n/-! ## Question 3: Binary Trees\n\n3.1. Prove the converse of `is_full_mirror`. You may exploit already proved\nlemmas (e.g., `is_full_mirror`, `mirror_mirror`). -/\n\n#check is_full_mirror\n#check mirror_mirror\n\nlemma mirror_is_full {α : Type} :\n ∀t : btree α, is_full (mirror t) → is_full t :=\nbegin\n intros t fmt,\n have fmmt : is_full (mirror (mirror t)) :=\n is_full_mirror _ fmt,\n rw mirror_mirror at fmmt,\n assumption\nend\n\n/-! 3.2. Define a `map` function on binary trees, similar to `list.map`. -/\n\ndef map_btree {α β : Type} (f : α → β) : btree α → btree β\n| btree.empty := btree.empty\n| (btree.node a l r) := btree.node (f a) (map_btree l) (map_btree r)\n\n/-! 3.3. Prove the following lemma by case distinction. -/\n\nlemma map_btree_eq_empty_iff {α β : Type} (f : α → β) :\n ∀t : btree α, map_btree f t = btree.empty ↔ t = btree.empty\n| btree.empty := by simp [map_btree]\n| (btree.node _ _ _) := by simp [map_btree]\n\n/-! 3.4 (**optional**). Prove the following lemma by rule induction. -/\n\nlemma map_btree_mirror {α β : Type} (f : α → β) :\n ∀t : btree α, is_full t → is_full (map_btree f t) :=\nbegin\n intros t hfull,\n induction' hfull,\n case empty {\n simp [map_btree],\n exact is_full.empty },\n case node {\n simp [map_btree],\n apply is_full.node,\n { exact ih_hfull f },\n { exact ih_hfull_1 f },\n { simp [map_btree_eq_empty_iff],\n assumption } }\nend\n\nend LoVe\n", "meta": {"author": "blanchette", "repo": "logical_verification_2021", "sha": "23b469c79afd482fa66da82e4726a317e3a7b5d5", "save_path": "github-repos/lean/blanchette-logical_verification_2021", "path": "github-repos/lean/blanchette-logical_verification_2021/logical_verification_2021-23b469c79afd482fa66da82e4726a317e3a7b5d5/lean/love05_inductive_predicates_exercise_solution.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9591542840900507, "lm_q2_score": 0.9005297754396142, "lm_q1q2_score": 0.8637469920635572}}
{"text": "import tactic\nset_option pp.structure_projections false\n\ntheorem binom : ∀ x y : ℕ, (x + y)^2 = x^2 + 2*x*y + y^2 :=\nbegin\n assume a b,\n ring,\nend\n\nset_option pp.structure_projections false\nopen nat\n\n--multiplication\ndef mul : ℕ → ℕ → ℕ\n| m 0 := 0\n| m (succ n) := (mul m n) + m\n\n-- m * 0 = n\n-- m * (n + 1) = m * n + m\n\nlocal notation m * n := mul m n\n\n#reduce mul 5 3\n\n\n\n----ring\n--semi ring\n/--\nA semiring is a set together with two binary operators S(+,*) satisfying the following conditions:\n\n1. Additive associativity: For all a,b,c in S, (a+b)+c=a+(b+c),\n\n2. Additive commutativity: For all a,b in S, a+b=b+a, --left and right neutral\n\n3. Multiplicative associativity: For all a,b,c in S, (a*b)*c=a*(b*c),\n\n4. Left and right distributivity: For all a,b,c in S, a*(b+c)=(a*b)+(a*c) and (b+c)*a=(b*a)+(c*a).\n\n--/\n\n--ring with 4. Additive inverse: For every a in S there exists -a in S such that a+(-a)=(-a)+a=0,\n\n/--\nThe field axioms are generally written in additive and multiplicative pairs.\n\nname\taddition\tmultiplication\nassociativity\t(a+b)+c=a+(b+c)\t(ab)c=a(bc)\ncommutativity\ta+b=b+a\tab=ba\ndistributivity\ta(b+c)=ab+ac\t(a+b)c=ac+bc\nidentity\ta+0=a=0+a\ta·1=a=1·a\ninverses\ta+(-a)=0=(-a)+a\taa^(-1)=1=a^(-1)a if a!=0\n--/\n\n/--\nNatural numbers (ℕ)\tSemiring\nIntegers (ℤ)\tRing\nRational numbers (ℚ)\tField\nReal numbers (ℝ)\tComplete Field\nComplex numbers (ℂ)\tAlgebraically complete Field\n--/\n\n\n\ndef exp : ℕ → ℕ → ℕ\n| n zero := 1\n| n (succ m) := exp n m * n\n\n\n\n--order ≤ , m ≤ n\ndef le (m n : ℕ) : Prop := ∃ k : ℕ , k + m = n\n\nlocal notation x ≤ y := le x y\nlocal notation x ≥ y := le x y\n\ntheorem le_refll : ∀ x : ℕ , x ≤ x :=\nbegin\n assume x,\n dsimp[(≤),le], \n existsi 0,\n ring,\nend\n\ntheorem le_transs : ∀ x y z : ℕ , x ≤ y → y ≤ z → x ≤ z :=\nbegin\n assume x y z ,\n assume xy yz,\n dsimp[(≤ ),le],\n dsimp[(≤ ),le] at xy,\n dsimp[(≤ ),le] at yz,\n\n cases xy with k p, -- ≤ is an exist statement\n cases yz with l q,\n existsi (k+l),\n rewrite← q,\n rewrite← p,\n ring,\nend\n\n--partial order: a partial order is a relation which is reflexive, transitive and antisymmetric. \n--reflexive (∀ x : A, x≤x),\n--transitive (∀ x y z : A, x≤y → y≤z → x≤z)\n--antisymmetry (∀ x y : ℕ , x ≤ y → y ≤ x → x = y)\n\n\n\n\n\n\n\n\n\n-- decidability\n-- x,y : ℕ \n-- x = y : Prop\nset_option pp.structure_projections false\n\nexample: 3=3 :=\nbegin\n reflexivity,\nend \n\nexample : ¬ (3 = 5) :=\nbegin\n assume n,\n have h : 2=4,\n injection n,\n have k : 1=3,\n injection h,\n have l : 0 = 2,\n injection k,\n contradiction,\nend\n\nexample : ¬ (3 = 5) :=\nbegin\n assume n,\n cases n,\nend\n\nopen nat\n\ndef eq_nat : ℕ → ℕ → bool \n| zero zero := tt\n| zero (succ n) := ff\n| (succ m) zero := ff\n| (succ m) (succ n) := eq_nat m n\n\n#reduce eq_nat 3 3\n\n#reduce eq_nat 3 5 \n\nlemma eq_nat_1 : ∀ m : ℕ, eq_nat m m = tt :=\nbegin\n assume m,\n induction m with n' ih,\n dsimp [eq_nat],\n reflexivity,\n dsimp [eq_nat],\n exact ih,\nend\n\nlemma eq_nat_2 : ∀ m n : ℕ, eq_nat m n = tt → m = n :=\nbegin\n assume m,\n induction m with m' ih_m,\n assume n,\n cases n, -- n be 0 and succ n\n assume h,\n reflexivity,\n\n assume h,\n dsimp [eq_nat] at h,\n contradiction,\n\n assume n h,\n cases n,\n dsimp [eq_nat] at h,\n contradiction,\n have g : m'=n,\n apply ih_m,\n dsimp [eq_nat] at h,\n exact h,\n rewrite g,\nend \n \ntheorem eq_nat_dec : ∀ m n : ℕ, m=n ↔ eq_nat m n = tt :=\nbegin\n assume m n,\n constructor,\n assume e,\n rewrite e,\n apply eq_nat_1,\n apply eq_nat_2,\nend \n\n-- ok equality for natural numbers is decidable\n-- are all relations decidable, are all equalities decidable?\n\n/--\nCertainly not all relations or predicates are decidable. An example for an undecidable relation is equality of functions that is given f g : ℕ → ℕ is f = g? Two functions are equal iff they agree on all arguments f = g ↔ ∀ n :ℕ,f n = g n. It is clear that we cannot decide this, i.e. define a function into bool, because we would have to check infinitely many inputs.\n--/\n\n-- there are undecidable predicates\n-- TM : Set\n-- Halts : TM → Prop\n-- halts : TM → bool -- there is no such function, see Halting problem\n\n-- f g : ℕ → ℕ\n-- f = g ?\n-- ∀ n : ℕ, f n = g n\n-- eq_fun : (ℕ → ℕ) → (ℕ → ℕ) → bool -- is undecidable\n-- halts_after_n_steps : TM → ℕ → bool -- is definable\n-- Halts tm := ¬ (halts_after_n_step = λ x, ff)\n\n-- h : succ m = succ n\n-- ⊢ m = n\n-- injection h,\n\n-- a = b\n-- calc \n-- a = c : by ..\n-- ... = d : by ..\n-- ... = b : by ..\n\n\n", "meta": {"author": "kyrran", "repo": "Lean", "sha": "915f45d695eb01a80e58916f03e8f7c1e878be8b", "save_path": "github-repos/lean/kyrran-Lean", "path": "github-repos/lean/kyrran-Lean/Lean-915f45d695eb01a80e58916f03e8f7c1e878be8b/lean12-13.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9481545362802363, "lm_q2_score": 0.9099070054272775, "lm_q1q2_score": 0.8627324547890388}}
{"text": "/-\nCopyright (c) 2022 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor : Kevin Buzzard\n-/\n\nimport tactic -- imports all the Lean tactics\nimport data.real.basic -- imports the real numbers\n\n/-\n\n# Limits of sequences in Lean\n\nWe give the standard `ε`, `N` definition of the limit of a sequence\nand prove some theorems about them.\n\n## lambda (λ) notation for functions\n\nHere's how we define the functions from the naturals to the reals\nsending n to n^2 + 3:\n\n-/\n\ndef f : ℕ → ℝ := λ n, n^2+3\n\n/-\n\nMathematicians might write `n ↦ n^2+3` for this functions; indeed `λ` is\njust prefix notation for the infix notation `↦` (i.e. you write `λ` at\nthe front and `↦` in the middle but they mean the same thing). You can\nread more about function types in the \"three kinds of types\" section\nof Part B of the course book.\n\nSometimes you might find yourself with a lambda-defined function\nevaluated at a number. For example, you might see something like\n`(λ n, n^2 + 3) 37`, which means \"take the function sending\n`n` to `n^2+3` and then evaluate it at 37\". You can use the `dsimp`\n(or `dsimp only`) tactic to simplify this to `37^2+3`.\n\nThe reason we need to know about function notation for this sheet\nis that a sequence `x₀, x₁, x₂, …` of reals on this sheet will\nbe encoded as a function from `ℕ` to `ℝ` sending `0` to `x₀`, `1` to `x₁`\nand so on.\n \n## Limit of a sequence.\n\nHere's the definition of the limit of a sequence.\n-/\n\n/-- If `a(n)` is a sequence of reals and `t` is a real, `tendsto a t`\nis the assertion that the limit of `a(n)` as `n → ∞` is `t`. -/\ndef tendsto (a : ℕ → ℝ) (t : ℝ) : Prop :=\n∀ ε > 0, ∃ B : ℕ, ∀ n, B ≤ n → |a n - t| < ε\n\n/-\n\nWe've made a definition, so it's our job to now make the API\nfor the definition, i.e. prove some basic theorems about it. \n-/\n\n-- If your goal is `tendsto a t` and you want to replace it with\n-- `∀ ε > 0, ∃ B, …` then you can do this with `rw tendsto_def`.\ntheorem tendsto_def {a : ℕ → ℝ} {t : ℝ} :\n tendsto a t ↔ ∀ ε, 0 < ε → ∃ B : ℕ, ∀ n, B ≤ n → |a n - t| < ε :=\nbegin\n -- true by definition\n refl\nend\n\n-- the eagle-eyed viewers amongst you might have spotted\n-- that `∀ ε > 0, ...` and `∀ ε, ε > 0 → ...` and `∀ ε, 0 < ε → ...`\n-- are all definitionally equal, so `refl` sees through them. \n\n/-\n\n## The questions\n\nHere are some basic results about limits of sequences.\nSee if you can fill in the `sorry`s with Lean proofs.\nNote that `norm_num` can work with `|x|` if `x` is a numeral like 37,\nbut it can't do anything with it if it's a variable.\n-/\n\n/-- The limit of the constant sequence with value 37 is 37. -/\ntheorem tendsto_thirtyseven : tendsto (λ n, 37) 37 :=\nbegin\n sorry,\nend\n\n/-- The limit of the constant sequence with value `c` is `c`. -/\ntheorem tendsto_const (c : ℝ) : tendsto (λ n, c) c :=\nbegin\n sorry,\nend\n\n/-- If `a(n)` tends to `t` then `a(n) + c` tends to `t + c` -/\ntheorem tendsto_add_const {a : ℕ → ℝ} {t : ℝ} (c : ℝ)\n (h : tendsto a t) :\n tendsto (λ n, a n + c) (t + c) :=\nbegin\n sorry,\n -- hints: make sure you know the maths proof!\n -- use `cases` to deconstruct an `exists`\n -- hypothesis, and `specialize` to specialize\n -- a `forall` hypothesis to specific values.\n -- Look up the explanations of these tactics in Part C\n -- of the course notes. \nend\n\n\n/-- If `a(n)` tends to `t` then `-a(n)` tends to `-t`. -/\nexample {a : ℕ → ℝ} {t : ℝ} (ha : tendsto a t) :\n tendsto (λ n, - a n) (-t) :=\nbegin\n sorry,\n -- Try this one. Where do you get stuck?\n -- The problem is that you probably don't\n -- know any API for the absolute value function |.|.\n -- We need to figure out how to prove |(-x)| = |x|,\n -- or |a - b| = |b - a| or something like that.\n -- Find out how in sheet 4.\nend\n", "meta": {"author": "ImperialCollegeLondon", "repo": "formalising-mathematics-2022", "sha": "af5f176b3b881b7bc0ae89b55befe48c9d4ab790", "save_path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2022", "path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2022/formalising-mathematics-2022-af5f176b3b881b7bc0ae89b55befe48c9d4ab790/src/section02reals/sheet3.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.948154530420204, "lm_q2_score": 0.9099070011518829, "lm_q1q2_score": 0.8627324454032196}}
{"text": "/-\nTo show that something (such as n = n)\nis true for *all* values of some type\n(such as nat), assume that you have an\n*arbitrary* value of that type and in\nthat context show that that something\nis true for that arbitrary n. Because\nit's true for an n selected arbitrarily\nit must be true for every n. (Think \nabout that.) This is the introduction\nrule for ∀. In Lean, to prove a ∀ we\ndefine a function: one that *assumes*\nit's given an arbitrary argument of a\ngiven type and that, in that context,\nproduces a value of a desired type. \nSo you already know how to prove a ∀\nproposition in Lean: write a function. \n-/\n\n#check ∀ (n : nat), n = n\n\nlemma all1 : ∀ (n : nat), n = n := λ n, (eq.refl n) \n\nlemma all2 (n : nat) : n = n := rfl\n\n#check ∀ (n : nat), n = n \n\n-- Introduction rule for forall \n\n/-\nNow *given* a proof of a forall, you\n*use* it by *applying* it to get a \nproof for any particular instance of\na universally quantified proposition.\nSuppse you know that ∀ n, n = n and\nyou need a proof of 5 = 5. You can\njust *apply* the \"proof\" to 5 to get\nwhat you need. This is the elimination\nrule for ∀. \n-/\n\nexample : 5 = 5 := all1 5\n\n#check all1 5\n\naxioms (Person : Type) \n (Friendly : Person → Prop) \n (allFriendly : ∀ (p : Person), Friendly p)\n (John : Person)\n\nexample : Friendly John := allFriendly John\n\n/-\nProofs are just \"programs\" -- functions, data structures\n-/\n\n\n\n\n", "meta": {"author": "kevinsullivan", "repo": "complogic-s21", "sha": "99039501b770248c8ceb39890be5dfe129dc1082", "save_path": "github-repos/lean/kevinsullivan-complogic-s21", "path": "github-repos/lean/kevinsullivan-complogic-s21/complogic-s21-99039501b770248c8ceb39890be5dfe129dc1082/src/inClassNotes/predicate_logic/all.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9591542852576265, "lm_q2_score": 0.8991213840277783, "lm_q1q2_score": 0.8623961284570116}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio 1. Realizar las siguientes acciones:\n-- 1. Importar la teoría de grupo.\n-- 2. Crear el espacio de nombres my_group\n-- 3. Declarar G como una variable sobre anillos.\n-- 4. Declarar a y b como variable sobre G.\n-- ----------------------------------------------------------------------\n\nimport algebra.group -- 1\nvariables {G : Type*} [group G] -- 2\nnamespace my_group -- 3\nvariables (a b : G) -- 4\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 2. Demostrar que\n-- a * a⁻¹ = 1\n-- ----------------------------------------------------------------------\n\ntheorem mul_right_inv : a * a⁻¹ = 1 :=\ncalc\n a * a⁻¹ = 1 * (a * a⁻¹) : by rw one_mul\n ... = (1 * a) * a⁻¹ : by rw mul_assoc\n ... = (((a⁻¹)⁻¹ * a⁻¹) * a) * a⁻¹ : by rw mul_left_inv\n ... = ((a⁻¹)⁻¹ * (a⁻¹ * a)) * a⁻¹ : by rw ← mul_assoc\n ... = ((a⁻¹)⁻¹ * 1) * a⁻¹ : by rw mul_left_inv\n ... = (a⁻¹)⁻¹ * (1 * a⁻¹) : by rw mul_assoc\n ... = (a⁻¹)⁻¹ * a⁻¹ : by rw one_mul\n ... = 1 : by rw mul_left_inv\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 3. Demostrar que\n-- a * 1 = a\n-- ----------------------------------------------------------------------\n\ntheorem mul_one : a * 1 = a :=\ncalc\n a * 1 = a * (a⁻¹ * a) : by rw mul_left_inv\n ... = (a * a⁻¹) * a : by rw mul_assoc\n ... = 1 * a : by rw mul_right_inv\n ... = a : by rw one_mul\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 4. Demostrar que si\n-- b * a = 1\n-- entonces\n-- a⁻¹ = b\n-- ----------------------------------------------------------------------\n\n\nlemma inv_eq_of_mul_eq_one\n (h : b * a = 1)\n : a⁻¹ = b :=\ncalc\n a⁻¹ = 1 * a⁻¹ : by rw one_mul\n ... = (b * a) * a⁻¹ : by rw h\n ... = b * (a * a⁻¹) : by rw mul_assoc\n ... = b * 1 : by rw mul_right_inv\n ... = b : by rw mul_one\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 5. Demostrar que\n-- (a * b)⁻¹ = b⁻¹ * a⁻¹\n-- ----------------------------------------------------------------------\n\n-- En la demostración se usará el siguiente lema:\nlemma mul_inv_rev_aux : (b⁻¹ * a⁻¹) * (a * b) = 1 :=\ncalc\n (b⁻¹ * a⁻¹) * (a * b)\n = b⁻¹ * (a⁻¹ * (a * b)) : by rw mul_assoc\n ... = b⁻¹ * ((a⁻¹ * a) * b) : by rw mul_assoc\n ... = b⁻¹ * (1 * b) : by rw mul_left_inv\n ... = b⁻¹ * b : by rw one_mul\n ... = 1 : by rw mul_left_inv\n\ntheorem mul_inv_rev : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=\nbegin\n apply inv_eq_of_mul_eq_one,\n rw mul_inv_rev_aux,\nend\n\n-- El desarrollo de la prueba es\n--\n-- G : Type u_1,\n-- _inst_1 : group G,\n-- a b : G\n-- ⊢ (a * b)⁻¹ = b⁻¹ * a⁻¹\n-- apply inv_eq_of_mul_eq_one,\n-- ⊢ b⁻¹ * a⁻¹ * (a * b) = 1\n-- rw mul_inv_rev_aux,\n-- no goals\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 6. Cerrar el espacio de nombre my_group.\n-- ----------------------------------------------------------------------\n\nend my_group\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Basicos/Ejercicios_sobre_grupos.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9399133481428691, "lm_q2_score": 0.9173026550642018, "lm_q1q2_score": 0.8621850097817373}}
{"text": "-- Pruebas de longitud (repite n x) = n\n-- ====================================\n\nopen nat\nopen list\n\nvariable {α : Type}\nvariable (x : α)\nvariable (xs : list α)\nvariable (n : ℕ)\n\n-- ----------------------------------------------------\n-- Nota. Se usará la función longitud y sus propiedades\n-- estudiadas anteriormente.\n-- ----------------------------------------------------\n\ndef longitud : list α → nat\n| [] := 0\n| (x :: xs) := longitud xs + 1\n\n@[simp]\nlemma longitud_nil :\n longitud ([] : list α) = 0 :=\nrfl\n\n@[simp]\nlemma longitud_cons :\n longitud (x :: xs) = longitud xs + 1 :=\nrfl\n\n-- ----------------------------------------------------\n-- Ejercicio 1. Definir la función\n-- repite :: ℕ → α → list α\n-- tal que (repite n x) es la lista formada por n\n-- copias del elemento x. Por ejemplo,\n-- repite 3 7 = [7,7,7]\n-- ----------------------------------------------------\n\ndef repite : ℕ → α → list α\n| 0 _ := []\n| (succ n) x := x :: repite n x\n\n-- #eval repite 3 7\n\n-- ----------------------------------------------------\n-- Ejercicio 2. Demostrar los siguientes lemas\n-- + repite_cero :\n-- repite 0 x = []\n-- + repite_suc :\n-- repite (succ n) x = x :: repite n x\n-- ----------------------------------------------------\n\n@[simp]\nlemma repite_cero :\n repite 0 x = [] :=\nrfl\n\n@[simp]\nlemma repite_suc :\n repite (succ n) x = x :: repite n x :=\nrfl\n\n-- ----------------------------------------------------\n-- Ejercicio 3. (p. 18) Demostrar que\n-- longitud (repite n x) = n\n-- ----------------------------------------------------\n\n-- 1ª demostración\nexample :\n longitud (repite n x) = n :=\nbegin\n induction n with n HI,\n { calc\n longitud (repite 0 x)\n = longitud [] : by rw repite_cero\n ... = 0 : by rw longitud_nil },\n { calc\n longitud (repite (succ n) x)\n = longitud (x :: repite n x) : by rw repite_suc\n ... = longitud (repite n x) + 1 : by rw longitud_cons\n ... = n + 1 : by rw HI\n ... = succ n : rfl, },\nend\n\n-- 2ª demostración\nexample : longitud (repite n x) = n :=\nbegin\n induction n with n HI,\n { rw repite_cero,\n rw longitud_nil, },\n { rw repite_suc,\n rw longitud_cons,\n rw HI, },\nend\n\n-- 3ª demostración\nexample : longitud (repite n x) = n :=\nbegin\n induction n with n HI,\n { simp only [repite_cero, longitud_nil], },\n { simp only [repite_suc, longitud_cons, HI], },\nend\n\n-- 4ª demostración\nexample : longitud (repite n x) = n :=\nbegin\n induction n with n HI,\n { simp, },\n { simp [HI], },\nend\n\n-- 5ª demostración\nexample : longitud (repite n x) = n :=\nby induction n ; simp [*]\n\n-- 6ª demostración\nexample : longitud (repite n x) = n :=\nnat.rec_on n\n ( show longitud (repite 0 x) = 0, from\n calc\n longitud (repite 0 x)\n = longitud [] : by rw repite_cero\n ... = 0 : by rw longitud_nil )\n ( assume n,\n assume HI : longitud (repite n x) = n,\n show longitud (repite (succ n) x) = succ n, from\n calc\n longitud (repite (succ n) x)\n = longitud (x :: repite n x) : by rw repite_suc\n ... = longitud (repite n x) + 1 : by rw longitud_cons\n ... = n + 1 : by rw HI\n ... = succ n : rfl )\n\n-- 7ª demostración\nexample : longitud (repite n x) = n :=\nnat.rec_on n\n ( by simp )\n ( λ n HI, by simp [HI])\n\n-- 7ª demostración\nlemma longitud_repite_1 :\n ∀ n, longitud (repite n x) = n\n| 0 := by calc\n longitud (repite 0 x)\n = longitud ([] : list α) : by rw repite_cero\n ... = 0 : by rw longitud_nil\n| (n+1) := by calc\n longitud (repite (n + 1) x)\n = longitud (x :: repite n x) : by rw repite_suc\n ... = longitud (repite n x) + 1 : by rw longitud_cons\n ... = n + 1 : by rw longitud_repite_1\n\n-- 8ª demostración\nlemma longitud_repite_2 :\n ∀ n, longitud (repite n x) = n\n| 0 := by simp\n| (n+1) := by simp [*]\n\n-- Comentarios sobre la función (repeat x n)\n-- + Es equivalente a la función (repite n x).\n-- + Para usarla hay que importar la librería\n-- data.list.basic y abrir el espacio de nombre\n-- list escribiendo al principio del fichero\n-- import data.list.basic\n-- open list\n-- + Se puede evaluar. Por ejemplo,\n-- #eval list.repeat 7 3\n-- + Se puede demostrar. Por ejemplo,\n-- example : length (repeat x n) = n :=\n-- by induction n ; simp [*]\n--\n-- example : length (repeat x n) = n :=\n-- -- by library_search\n-- length_repeat x n\n", "meta": {"author": "jaalonso", "repo": "Logica_con_Lean", "sha": "beb6765c6ff3c05590a03f45722eda0c815a25cd", "save_path": "github-repos/lean/jaalonso-Logica_con_Lean", "path": "github-repos/lean/jaalonso-Logica_con_Lean/Logica_con_Lean-beb6765c6ff3c05590a03f45722eda0c815a25cd/src/7_Programas/Pruebas_de_longitud_(repite_n_x)_Ig_n.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9111797075998823, "lm_q2_score": 0.9458012648298515, "lm_q1q2_score": 0.8617949199352629}}
{"text": "/-\nCopyright (c) 2022 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor : Kevin Buzzard\n-/\n\nimport tactic -- imports all the Lean tactics\nimport linear_algebra.basis -- vector spaces and bases\n\n/-!\n\n# Steinitz exchange lemma\n\n-/\n\n-- This says \"let `k` be a field and let `V` be a vector space over `k`\"\nvariables (k : Type) [field k] (V : Type) [add_comm_group V] [module k V]\n\n/-\nSteinitz Exchange Lemma:\n\nLet `V` be a vector space over the field `k`, and `X` be a subset of `V`.\nSuppose `u ∈ Span(X)` but `u ∉ Span(X∖{v})` for some `v ∈ X`, and\nlet `Y = (X∖{v}) ∪ {u}`.\nThen, `Span(X) = Span(Y)`\n-/\n\nvariables {X Y : set V}\nvariables {u : V} {v : X}\n\n-- let w be v (w : V)\n-- let Z be X - {v}, so X = Z ∪ {v}\n-- i.e. X = insert w Z\n-- Y is insert u Z\nvariables (w : V) (Z : set V)\n\nopen submodule\n\nlemma steinitz_exchange \n (hu : u ∈ (submodule.span k (insert w Z)))\n (hnu : u ∉ submodule.span k Z) :\n submodule.span k (insert w Z) = submodule.span k (insert u Z) :=\nbegin\n apply le_antisymm,\n -- key lemma!\n { have hw := mem_span_insert_exchange hu hnu,\n rw span_le,\n rw set.insert_subset,\n refine ⟨hw, _⟩,\n rw ← span_le,\n apply span_mono,\n simp },\n -- Z ⊆ span (w ∪ Z)\n -- and by `hu`, u ∈ span (w ∪ Z)\n -- so span (u ∪ Z) ⊆ span (w ∪ Z)\n { \n rw span_le,\n rw set.insert_subset,\n split,\n { exact hu,\n },\n { rw ← span_le,\n apply span_mono,\n simp }\n }\nend\n\nlemma steinitz_exchange'\n (hu : u ∈ (submodule.span k X))\n (hnu : u ∉ submodule.span k (X.diff {v})) \n (hY : Y = (X.diff {v}).union {u}):\n submodule.span k X = submodule.span k Y :=\nbegin\n cases v with w hw,\n have hX : X = insert w (X \\ {w}),\n { simp [hw] },\n convert steinitz_exchange k V w (X \\ {w}) _ hnu,\n { change Y = (X \\ {w}) ∪ {u} at hY,\n simpa using hY, \n },\n { rwa ← hX }\nend\n\nlemma steinitz_exchange''\n (hu : u ∈ (submodule.span k X))\n (hnu : u ∉ submodule.span k (X \\ {v})) \n (hY : Y = (X \\ {v}) ∪ {u}):\n submodule.span k X = submodule.span k Y :=\nbegin\n cases v with w hw,\n rw hY,\n simp,\n convert steinitz_exchange k V w (X \\ {w}) _ hnu,\n { simp [hw] },\n convert hu,\n simp [hw],\nend", "meta": {"author": "ImperialCollegeLondon", "repo": "formalising-mathematics-2022", "sha": "af5f176b3b881b7bc0ae89b55befe48c9d4ab790", "save_path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2022", "path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2022/formalising-mathematics-2022-af5f176b3b881b7bc0ae89b55befe48c9d4ab790/src/section10vectorspaces/examples/example1steinitz.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9334308147331957, "lm_q2_score": 0.9230391674796138, "lm_q1q2_score": 0.8615932021311465}}
{"text": "-- Distancia_no_negativa.lean\n-- Si X es un espacio métrico y x, y ∈ X, entonces dist(x,y) ≥ 0\n-- José A. Alonso Jiménez <https://jaalonso.github.io>\n-- Sevilla, 31-octubre-2022\n-- ---------------------------------------------------------------------\n\n-- ---------------------------------------------------------------------\n-- Demostrar que si X es un espacio métrico y x, y ∈ X, entonces\n-- 0 ≤ dist x y\n-- ----------------------------------------------------------------------\n\nimport topology.metric_space.basic\n\nvariables {X : Type*} [metric_space X]\nvariables x y : X\n\n-- 1ª demostración\nexample : 0 ≤ dist x y :=\n have h1 : 2 * dist x y ≥ 0, by calc\n 2 * dist x y = dist x y + dist x y : two_mul (dist x y)\n ... = dist x y + dist y x : by rw [dist_comm x y]\n ... ≥ dist x x : dist_triangle x y x\n ... = 0 : dist_self x,\n show 0 ≤ dist x y,\n by exact nonneg_of_mul_nonneg_left h1 zero_lt_two\n\n-- 2ª demostración\nexample : 0 ≤ dist x y :=\n-- by library_search\ndist_nonneg\n", "meta": {"author": "jaalonso", "repo": "Calculemus", "sha": "0fb664ab298c0e90b4b8034729a2cdad20503e18", "save_path": "github-repos/lean/jaalonso-Calculemus", "path": "github-repos/lean/jaalonso-Calculemus/Calculemus-0fb664ab298c0e90b4b8034729a2cdad20503e18/src/Distancia_no_negativa.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9441768588653856, "lm_q2_score": 0.9124361586911173, "lm_q1q2_score": 0.8615011062281777}}
{"text": "open list\n\n\n/-\n#1. You are to implement a function\nthat takes a \"predicate\" function and a\nlist and that returns a new list: namely\na list that contains all and only those\nelements of the first list for which the\ngiven predicate function returns true.\n\nWe start by giving you two predicate\nfunctions. Each takes an argument, n,\nof type ℕ. The first returns true if\nand only if n < 5. The second returns\ntrue if and only if n is even. \n-/\n\n\n/-\nHere's a \"predicate\" function that\ntakes a natural number as an argument\nand returns true if the number is less\nthan 5 otherwise it returns false. \n-/\ndef lt_5 (n : ℕ) : bool :=\n n < 5\n\n-- example cases\n#eval lt_5 2\n#eval lt_5 6\n\n/-\nHere's another predicate function, one\nthat returns true if a given nat is even,\nand false otherwise. Note that we have\ndefined this function recursively. Zero\nis even, one is not, and (2 + n') is if\nand only if n' is.\n\nYou can think of a predicate function\nas a function that answers the question,\ndoes some value (here a nat) have some\nproperty? The properties in these two\ncases are (1) the property of being less\nthan 5, and (2) the property of being\neven.\n-/\ndef evenb : ℕ → bool\n| 0 := tt\n| 1 := ff\n| (n' + 2) := evenb n'\n\n#eval evenb 0\n#eval evenb 3\n#eval evenb 4\n#eval evenb 5\n\n/-\nWe call the function you are going to \nimplement a \"filter\" function, because\nit takes a list and returns a \"filtered\"\nversion of the list. Call you function\n\"mfilter\".\n\nIf you filter an empty list you always\nget an empty list as a result. If you\nfilter a non-empty list, l = (cons h t), \nthe returned list has h at its head if\nand only if the predicate function applied\nto h returns true, otherwise the returned\nvalue is just the filtered version of t.\n\nA. [15 points] \n\nTA: Original had typo (actually due an\nan unforeseen name conflict). Have the\nstudents change all appearances of the\nname, mfilter, to myfilter.\n\nYour task is to complete an incomplete\nversion of the definition of the myfilter\nfunction. We use a Lean construct new to \nyou: the if ... then ... else. It works\nas you would expect from your work with\nother programming languages. Replace the \nunderscores to complete the definition.\n-/\n\ndef myfilter : (ℕ → bool) → list ℕ → list ℕ \n| p [] := []\n| p (cons h t) :=\n if p h \n then (cons h (myfilter p t))\n else myfilter p t\n\n/-\nB. [10 points]\n\nHere's the definition of a simple list.\n-/\n\ndef a_list := [0,1,2,3,4,5,6,7,8,9]\n\n\n/-\nReplace the underscores in the first two \neval commands below as follows. Replace \nthe first one with an expression in which \nmyfilter is used to compute a new list that \ncontains the numbers in a_list that are \nless than 5. Replace the second one with \nan expression in which mfilter is used to \ncompute a list containing even elements of \na_list. You may use the predicate functions\nwe defined above.\n\nReplace the third underscore with a similar\nexpression but where you use a λ expression\nto specify a predicate function that takes\na nat, n, and returns tt if n is equal to\nthree and false otherwise. Hint: n=3 is\nan expression that will return the desired \nbool.\n-/\n\n#eval myfilter lt_5 a_list\n#eval myfilter evenb a_list\n#eval myfilter (λ (n : ℕ), n = 3) a_list\n\n#eval a_list\n\n\n/-\n#2.\n\nHere's a function that takes a function, f,\nfrom ℕ to ℕ, and a value, n, of type ℕ, and\nthat returns the value that is obtained by \nsimply applying f to n. \n-/\n\ndef f_apply : (ℕ → ℕ) → ℕ → ℕ \n| f n := (f n)\n\n-- examples of its use\n#eval f_apply nat.succ 3\n#eval f_apply (λ n : ℕ, n * n) 3\n\n/-\nA. [10 points]\n\nWrite a function, f_apply_2, that takes a \nfunction, f, from ℕ to ℕ, and a value, n, \nof type ℕ, and that returns (read this\ncarefully) the value obtained by applying \nf twice to n: the result of applying f to \nthe result of applying f to n. For example,\nif f is the function that squares its nat\nargument, then (f_apply_2 f 3) returns 81,\nas f applied to 3 is 9 and f applied to 9\nis 81. \n-/\n\n-- Your answer here\n\n/-\nTA: Original had typo: ff_apply instead of\nf_apply_2. Have students change ff_apply to\nf_apply_2.\n-/\n\ndef f_apply_2 : (ℕ → ℕ) → ℕ → ℕ \n| f n := f (f n)\n\n/-\nB. [10 points]\n\nWrite a function f_apply_k that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nn, of type ℕ, and (3) a value, k of type ℕ,\nand that returns the result of applying f \nto n k times. \n\nNote that f_apply applies f to n once and\nff_apply applies f to n twice. Your job is\nto write a function that is general in the\nsense that you specify by a parameter, k,\nhow many times to apply f.\n\nHint 1: Use recursion. Note: The result of\napplying any function, f, to n, zero times\nis just n.\n-/\n\ndef f_apply_k : (ℕ → ℕ) → ℕ → ℕ → ℕ \n| f n 0 := n\n| f n (nat.succ k') := f (f_apply_k f n k')\n\n-- Answer here\n\n/-\nUse #eval to evaluate an expression in\nwhich the squaring function, expressed\nas a λ expression, is applied to 3 two \ntimes. You should be able to confirm that\nyou get the same answer given by using\nthe ff_apply function in the example above.\n-/\n\n-- Answer here\n\n#eval f_apply_k (λ n, n^2) 3 2\n\n\n/-\nC. [Extra Credit]\n\nWrite a function f_apply_k_fun that takes (1)\na function, f, of type ℕ to ℕ, (2) a value,\nk of type ℕ, and that returns a function that,\nwhen applied to a natural number, n, returns\nthe result of applying f to n k times.\n-/\n\ndef f_apply_k_fun : (ℕ → ℕ) → ℕ → (ℕ → ℕ) \n| f 0 := λ n, n\n| f (nat.succ k') := λ n, f (f_apply_k f n k')\n\ndef sq_twice := f_apply_k_fun (λ n, n^2) 2\n#eval sq_twice 3 -- yay!\n\n/-\n#3: [15 points]\n\nWrite a function, mcompose, that\ntakes two functions, g and f (in that\norder), each of type ℕ → ℕ, and that \nreturns *a function* that, when applied\nto an argument, n, of type ℕ, returns\nthe result of applying g to the result \nof applying f to n.\n-/\n\n-- Answer here\n\ndef mcompose : (ℕ → ℕ) → (ℕ → ℕ) → (ℕ → ℕ)\n| g f := λ n, g (f n)\n\n\n-- test\n#eval (mcompose nat.succ (λ n, n^2)) 4 -- 4^2+1 = 17\n\n/-\n#4. Higher-Order Functions\n\n4A. [10 points] Provide an implementatation of\na function, map_pred that takes as its arguments \n(1) a predicate function of type ℕ → bool, (2) a\nlist of natural numbers (of type \"list nat\"), \nand that returns a list in which each ℕ value,\nn,in the argument list is replaced by true (tt) \nif the predicate returns true for a, otherwise\nfalse (ff).\n\nFor example, if the predicate function returns\ntrue if and only if its argument is zero, then\napplying map to this function and to the list\n[0,1,2,0,1,0] must return [tt,ff,ff,tt,ff,tt].\n\n\nTest your code by using #eval or #reduce to evaluate\nan expression in which map_pred is applied to \nsuch an \"is_zero\" predicate function and to the\nlist 0,1,2,0,1,0]. Express the predicate function\nas a lambda abstraction within the #eval command.\n\nNOTE: You will have to use list.nil and list.cons\nto refer to the nil and cons constructors of the\nlibrary-provided list data type, as you already\nhave definitions for list and cons in the current\nnamespace.\n-/\n\n-- Answer here\n\ndef map_pred : (ℕ → bool) → list nat → list bool\n| p [] := []\n| p (cons h t) := cons (p h) (map_pred p t)\n\n\n/-\n4B. [10 points] Implement a function, reduce_or, \nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if there is at least one true value in the list,\notherwise ff. Note: the Lean libraries provide the\nfunction \"bor\" to compute \"b1 or b2\", where b1 and\nb2 are Booleans. We recommend that you include\ntests of your solution.\n-/\n\n-- example\n#reduce bor tt tt\n\n-- Answer here\n\ndef reduce_or : list bool → bool\n| [] := ff\n| (cons h t) := bor h (reduce_or t) \n\n\n/-\n4C. [10points] Implement a function, reduce_and,\nthat takes as its argument a list of Boolean values \nand that reduces the list to a single Boolean value: \ntt if every value in the list is true, otherwise ff.\n-/\n\n-- Note: band implements the Boolean \"and\" function\n#reduce band tt tt\n\n-- Answer here\n\ndef reduce_and : list bool → bool\n| [] := tt\n| (cons h t) := band h (reduce_and t) \n\n/-\n4D. [10 points] Define a function, all_zero, that \ntakes a list of nat values and returns true if and \nonly if they are all zero. Express your answer using \nmap and reduce functions that you have previously\ndefined above. Again we recommend that you test your \nsolution.\n-/\n\n-- Answer here (replace the _'s as needed)\n\ndef all_zero : list nat → bool\n| [] := tt\n| (cons h t) := band (h=0) (all_zero t) \n\n/-\nSome tests\n-/\n#reduce all_zero []\n#reduce all_zero [0,0,0,0]\n#reduce all_zero [1,0,0,0]\n#reduce all_zero [0,1,0,0]\n#reduce all_zero [1,0,0,1]\n", "meta": {"author": "kevinsullivan", "repo": "uva-cs-dm-f19", "sha": "09a950752884bd7ade4be33e9e89a2c4b1927167", "save_path": "github-repos/lean/kevinsullivan-uva-cs-dm-f19", "path": "github-repos/lean/kevinsullivan-uva-cs-dm-f19/uva-cs-dm-f19-09a950752884bd7ade4be33e9e89a2c4b1927167/answers/hw5_higher_order_functions.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9643214491222695, "lm_q2_score": 0.8933094060543487, "lm_q1q2_score": 0.8614374209608834}}
{"text": "-- Prueba por inducción 6: (∀ m n k ∈ ℕ) m^(n + k) = m^n * m^k\n-- ===========================================================\n\nimport data.nat.basic\nimport tactic\nopen nat\n\nvariables (m n k : ℕ)\n\n-- 1ª demostración\nexample : m^(n + k) = m^n * m^k :=\nbegin\n induction k with k HI,\n { rw add_zero,\n rw pow_zero,\n rw mul_one, },\n { rw add_succ,\n rw pow_succ',\n rw HI,\n rw pow_succ',\n rw mul_assoc, },\nend\n\n-- 2ª demostración\nexample : m^(n + k) = m^n * m^k :=\nbegin\n induction k with k HI,\n { calc\n m^(n + 0)\n = m^n : by rw add_zero\n ... = m^n * 1 : by rw mul_one\n ... = m^n * m^0 : by rw pow_zero, },\n { calc\n m^(n + succ k)\n = m^(succ (n + k)) : by rw nat.add_succ\n ... = m^(n + k) * m : by rw pow_succ'\n ... = m^n * m^k * m : by rw HI\n ... = m^n * (m^k * m) : by rw mul_assoc\n ... = m^n * m^(succ k) : by rw pow_succ', },\nend\n\n-- 3ª demostración\nexample : m^(n + k) = m^n * m^k :=\nbegin\n induction k with k HI,\n { rw [add_zero,\n pow_zero,\n mul_one], },\n { rw [add_succ,\n pow_succ',\n HI,\n pow_succ',\n mul_assoc], },\nend\n\n-- 4ª demostración\nexample : m^(n + k) = m^n * m^k :=\nbegin\n induction k with k HI,\n { simp only [add_zero,\n pow_zero,\n mul_one], },\n { simp only [add_succ,\n pow_succ',\n HI,\n pow_succ',\n mul_assoc], },\nend\n\n-- 5ª demostración\nexample : m^(n + k) = m^n * m^k :=\nbegin\n induction k with k HI,\n { simp, },\n { simp [add_succ,\n HI,\n pow_succ',\n mul_assoc], },\nend\n\n-- 6ª demostración\nexample : m^(n + k) = m^n * m^k :=\nby induction k; simp [*, add_succ, pow_succ', mul_assoc]\n\n-- 7ª demostración\nexample : m^(n + k) = m^n * m^k :=\nnat.rec_on k\n (show m^(n + 0) = m^n * m^0, from\n calc\n m^(n + 0)\n = m^n : by rw add_zero\n ... = m^n * 1 : by rw mul_one\n ... = m^n * m^0 : by rw pow_zero)\n (assume k,\n assume HI : m^(n + k) = m^n * m^k,\n show m^(n + succ k) = m^n * m^(succ k), from\n calc\n m^(n + succ k)\n = m^(succ (n + k)) : by rw nat.add_succ\n ... = m^(n + k) * m : by rw pow_succ'\n ... = m^n * m^k * m : by rw HI\n ... = m^n * (m^k * m) : by rw mul_assoc\n ... = m^n * m^(succ k) : by rw pow_succ')\n\n-- 8ª demostración\nexample : m^(n + k) = m^n * m^k :=\nnat.rec_on k\n (by simp)\n (λ n HI, by simp [HI, add_succ, pow_succ', mul_assoc])\n\n-- 9ª demostración\nexample : m^(n + k) = m^n * m^k :=\n-- by library_search\npow_add m n k\n", "meta": {"author": "jaalonso", "repo": "Logica_con_Lean", "sha": "beb6765c6ff3c05590a03f45722eda0c815a25cd", "save_path": "github-repos/lean/jaalonso-Logica_con_Lean", "path": "github-repos/lean/jaalonso-Logica_con_Lean/Logica_con_Lean-beb6765c6ff3c05590a03f45722eda0c815a25cd/src/6_Naturales/Prueba_por_induccion_6.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9465966686936262, "lm_q2_score": 0.9099070005411123, "lm_q1q2_score": 0.8613149355332265}}
{"text": "import data.real.basic\nimport data.int.parity\n\n/-\nIn this file, we learn how to handle the ∃ quantifier.\n\nIn order to prove `∃ x, P x`, we give some x₀ using tactic `use x₀` and\nthen prove `P x₀`. This x₀ can be an object from the local context\nor a more complicated expression.\n-/\nexample : ∃ n : ℕ, 8 = 2*n :=\nbegin\n use 4,\n refl, -- this is the tactic analogue of the rfl proof term\nend\n\n/-\nIn order to use `h : ∃ x, P x`, we use the `cases` tactic to fix\none x₀ that works.\n\nAgain h can come straight from the local context or can be a more\ncomplicated expression.\n-/\nexample (n : ℕ) (h : ∃ k : ℕ, n = k + 1) : n > 0 :=\nbegin\n -- Let's fix k₀ such that n = k₀ + 1.\n cases h with k₀ hk₀,\n -- It now suffices to prove k₀ + 1 > 0.\n rw hk₀,\n -- and we have a lemma about this\n exact nat.succ_pos k₀,\nend\n\n/-\nThe next exercises use divisibility in ℤ (beware the ∣ symbol which is\nnot ASCII).\n\nBy definition, a ∣ b ↔ ∃ k, b = a*k, so you can prove a ∣ b using the\n`use` tactic.\n-/\n\n-- Until the end of this file, a, b and c will denote integers, unless\n-- explicitly stated otherwise\nvariables (a b c : ℤ)\n\n-- 0029\nexample (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c :=\nbegin\n cases h₁ with k₁ h₁,\n cases h₂ with k₂ h₂,\n use k₁ * k₂,\n rw h₁ at h₂,\n rwa ←mul_assoc,\nend\n\n/-\nA very common pattern is to have an assumption or lemma asserting\n h : ∃ x, y = ...\nand this is used through the combo:\n cases h with x hx,\n rw hx at ...\nThe tactic `rcases` allows us to do recursive `cases`, as indicated by its name,\nand also simplifies the above combo when the name hx is replaced by the special\nname `rfl`, as in the following example.\nIt uses the anonymous constructor angle brackets syntax.\n-/\n\n\nexample (h1 : a ∣ b) (h2 : a ∣ c) : a ∣ b+c :=\nbegin\n rcases h1 with ⟨k, rfl⟩,\n rcases h2 with ⟨l, rfl⟩,\n use k+l,\n ring,\nend\n\n/-\nYou can use the same `rfl` trick with the `rintros` tactic.\n-/\n\nexample : a ∣ b → a ∣ c → a ∣ b+c :=\nbegin\n rintros ⟨k, rfl⟩ ⟨l, rfl⟩,\n use k+l,\n ring,\nend\n\n-- 0030\nexample : 0 ∣ a ↔ a = 0 :=\nbegin\n split,\n rintro ⟨a, rfl⟩,\n ring,\n rintro rfl,\n use 0,\n refl,\nend\n\n/-\nWe can now start combining quantifiers, using the definition\n\n surjective (f : X → Y) := ∀ y, ∃ x, f x = y\n\n-/\nopen function\n\n-- In the remaining of this file, f and g will denote functions from\n-- ℝ to ℝ.\nvariables (f g : ℝ → ℝ)\n\n\n-- 0031\nexample (h : surjective (g ∘ f)) : surjective g :=\nbegin\n intro y,\n rcases h y with ⟨w, rfl⟩,\n use f w,\nend\n\n/-\nThe above exercise can be done in three lines. Try to do the\nnext exercise in four lines.\n-/\n\n-- 0032\nexample (hf : surjective f) (hg : surjective g) : surjective (g ∘ f) :=\nbegin\n intro y,\n rcases hg y with ⟨g', rfl⟩,\n rcases hf g' with ⟨f', rfl⟩,\n use f',\nend\n\n", "meta": {"author": "michens", "repo": "learn-lean", "sha": "f38fc342780ddff5a164a18e5482163dea506ccd", "save_path": "github-repos/lean/michens-learn-lean", "path": "github-repos/lean/michens-learn-lean/learn-lean-f38fc342780ddff5a164a18e5482163dea506ccd/tutorials/src/exercises/04_exists.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9621075701109193, "lm_q2_score": 0.8947894555814342, "lm_q1q2_score": 0.860883708870326}}
{"text": "-- The short explanation that what makes dependent type theory dependent is that types can depend on parameters. \n-- For example the type of list α depends on the argument α \n-- this dependence is what distinguishes list ℕ and list bool\n-- Another example, consider the type vec α n, the type of vectors of elements of α of length n. \n\n\n-- There is something called Pi type or dependent function type where Given α : Type and\n-- β : α → Type, think of β as a family of types over α,that is, a type β for each a : α \n-- In that case, the type Π x : α, β x denotes the type of functions f with the property that,\n-- for each a : α, f a is an element of β a.\n-- In other words, the type of the value returned by f depends on its input.\n\n-- in dependent type theory (and in Lean), the Pi construction is fundamental, \n--and α → β is just notation for Π x : α, β when β does not depend on x.\n\n-- Using the examples on lists, we can model some basic ops as follows\n-- we use namespace hidden to avoid conflicts \n\nnamespace hidden\n\nuniverse u \n\nconstant list : Type u → Type u\n\nconstant cons : Π α : Type u, α → list α → list α \nconstant nil : Π α : Type u, list α \nconstant head : Π α : Type u, list α → α \nconstant tail : Π α : Type u, list α → list α \nconstant append : Π α : Type u, list α → list α → list α \n\nend hidden\n\nopen list \n\n#check list \n\n#check @cons\n#check @nil\n#check @head\n#check @tail\n#check @append\n\n-- There is a subtlety in the definition of head: the type α is required to have at least one element, \n-- when passed the empty list, the function must determine a default element of the relevant type\n\n-- Vector operations are handled similarly\nuniverse u\nconstant vec : Type u → ℕ → Type u\n\nnamespace vec\nconstant empty: Π α : Type u, vec α 0\nconstant cons :\n Π (α : Type u) (n : ℕ), α → vec α n → vec α (n+1)\nconstant append :\n Π (α : Type u) (n m : ℕ), vec α m → vec α n → vec α (n+m)\nend vec\n\n-- We'll mention one important and illustrative example\n-- the Sigma types, Σ x : α, β x, sometimes also known as dependent products. \n-- The type Σ x : α, β x denotes the type of pairs sigma.mk a b where a : α and b : β a.\n-- in the expression sigma.mk a b, the type of the second element of the pair, b : β a,\n-- depends on the first element of the pair, a : α.\n\nvariable α : Type\nvariable β : α → Type\nvariable a : α \nvariable b : β a\n\n#check sigma.mk a b\n#check (sigma.mk a b).1\n#check (sigma.mk a b).2\n\n#reduce (sigma.mk a b).1\n#reduce (sigma.mk a b).2\n", "meta": {"author": "0x-Inf", "repo": "theorem_proving_in_lean", "sha": "ffba957d08381aa846eb33cd1c855dd30eaca52f", "save_path": "github-repos/lean/0x-Inf-theorem_proving_in_lean", "path": "github-repos/lean/0x-Inf-theorem_proving_in_lean/theorem_proving_in_lean-ffba957d08381aa846eb33cd1c855dd30eaca52f/code_examples/part2/dependent_types.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9518632247867715, "lm_q2_score": 0.9032942158155877, "lm_q1q2_score": 0.8598125451974632}}
{"text": "/-\nCopyright (c) 2022 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor : Kevin Buzzard\n-/\n\nimport tactic -- imports all the Lean tactics\nimport data.real.basic -- the reals\n\n/-!\n\n# Sets in Lean, sheet 6 : pushforward and pullback\n\n## Pushforward of a set along a map\n\nIf `f : X → Y` then given a subset `S : set X` of `X` we can push it\nforward to make a subset `f(S) : set Y` of `Y`. The definition\nof `f(S)` is `{y : Y | ∃ x : X, x ∈ S ∧ f x = y}`. \n\nHowever `f(S)` doesn't make sense in Lean, because `f` eats\nterms of type `X` and not `S`, which has type `set X`. \nIn Lean we use the notation `f '' S` for this. This is notation\nfor `set.image` and if you need any API for this, it's likely\nto use the word `image`.\n\n## Pullback of a set along a map\n\nIf `f : X → Y` then given a subset `T : set Y` of `Y` we can\npull it back to make a subset `f⁻¹(T) : set X` of `X`. The definition\nof `f⁻¹(T)` is `{x : X | f x ∈ T}`.\n\nHowever `f⁻¹(T)` doesn't make sense in Lean either, because\n`⁻¹` is notation for `has_inv.inv`, whose type in Lean\nis `α → α`. In other words, if `x` has a certain type, then\n`x⁻¹` *must* have the same type. The notation was basically designed\nfor group theory. In Lean we use the notation `f ⁻¹' T` for this pullback.\n\n-/\n\nvariables (X Y : Type) (f : X → Y) (S : set X) (T : set Y)\n\nexample : S ⊆ f ⁻¹' (f '' S) :=\nbegin\n intros x hx,\n exact ⟨x, hx, rfl⟩, -- use x, etc etc also works\nend\n\nexample : f '' (f ⁻¹' T) ⊆ T :=\nbegin\n rintros _ ⟨x, hx, rfl⟩,\n exact hx,\nend\n\nexample : f '' S ⊆ T ↔ S ⊆ f ⁻¹' T :=\nbegin\n split,\n { intros h x hxS,\n refine h ⟨x, hxS, rfl⟩ },\n { rintro h _ ⟨x, hx, rfl⟩,\n refine h hx }\nend\n\n-- Pushforward and pullback along the identity map don't change anything\n\n-- pullback is not so hard\nexample : id ⁻¹' S = S :=\nbegin\n refl\nend\n\n-- pushforward is a little trickier. You might have to `ext x, split`.\nexample : id '' S = S :=\nbegin\n ext x,\n split,\n { rintro ⟨y, hyS, rfl⟩,\n exact hyS, },\n { intro hxS,\n use x,\n exact ⟨hxS, rfl⟩, },\nend\n\n-- Now let's try composition.\nvariables (Z : Type) (g : Y → Z) (U : set Z)\n\n-- preimage of preimage is preimage of comp\nexample : (g ∘ f) ⁻¹' U = f ⁻¹' (g ⁻¹' U) :=\nbegin\n refl,\nend\n\n-- preimage of preimage is preimage of comp\nexample : (g ∘ f) '' S = g '' (f '' S) :=\nbegin\n ext x,\n split,\n { rintro ⟨x, hxS, rfl⟩,\n refine ⟨f x, _, rfl⟩,\n exact ⟨x, hxS, rfl⟩, },\n { rintro ⟨y, ⟨x, hxS, rfl⟩, rfl⟩,\n exact ⟨x, hxS, rfl⟩, },\nend\n", "meta": {"author": "ImperialCollegeLondon", "repo": "formalising-mathematics-2023", "sha": "982f6d28a900f2e04e93e7cf298db4f5998be0d5", "save_path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2023", "path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2023/formalising-mathematics-2023-982f6d28a900f2e04e93e7cf298db4f5998be0d5/src/solutions/section05sets/sheet6.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9518632302488963, "lm_q2_score": 0.9032942001955142, "lm_q1q2_score": 0.8598125352631953}}
{"text": "import Basics.FunctionDefinitions\n/-!\n## Theorem Statements\n\nWhat makes Lean a proof assistant and not only a programming language is that we can state theorems\nabout the types and constants we define and prove that they hold. We will use the terms theorem,\nlemma, corollary, fact, property, and true statement more or less interchangeably. Similarly,\npropositions, logical formulas, and statements will all mean the same.\n\nIn Lean, propositions are simply terms of type `Prop`. This stands in contrast with most\ndescriptions of first-order logic, where terms and formulas are separate syntactic entities. A\nproposition that can be proved is called a theorem (or lemma, corollary, etc.); otherwise it is a\nnontheorem or false statement. Mathematicians sometimes use the term “proposition” as a synonym for\ntheorem (e.g., “Proposition 3.14”), but in formal logic propositions can also be false. Here are\nexamples of true statements that can be made about the addition, multiplication, and list reversal\noperations defined in [Function Definitions](./FunctionDefinitions.lean.md):\n\n-/\ntheorem add_comm₁ (m n : Nat) :\n add m n = add n m :=\n sorry\n\ntheorem add_assoc₁ (l m n : Nat) :\n add (add l m) n = add l (add m n) :=\n sorry\n\ntheorem mul_comm₁ (m n : Nat) :\n mul m n = mul n m :=\n sorry\n\ntheorem mul_assoc₁ (l m n : Nat) :\n mul (mul l m) n = mul l (mul m n) :=\n sorry\n\ntheorem mul_add₁ (l m n : Nat) :\n mul l (add m n) = add (mul l m) (mul l n) :=\n sorry\n\ntheorem reverse_reverse₁ {α : Type} (xs : List α) :\n reverse (reverse xs) = xs :=\n sorry\n/-!\nThe general format is\n```lean\ntheorem name (params1 : type1) . . . (paramsm : typem) :\n statement :=\n proof\n```\nThe `:=` symbol separates the theorem’s statement and its proof. The syntax of theorem is very\nsimilar to that of a `def` command without pattern matching, with `statement` instead of `type` and\n`proof` instead of `result`. In the examples above, we put the marker `sorry` as a placeholder for\nthe actual proof. The marker is quite literally an apology to future readers and to Lean for the\nabsence of a proof. It is also a reason to _worry_, until we manage to eliminate it. In Chapters 2\nand 3, we will see how to achieve this.\n\nThe intuitive semantic of a theorem command with a `sorry` proof is, “This proposition should be\nprovable, but I have not carried out the proof yet—sorry.” Sometimes, we want to express a related\nidea, namely, “Let us assume this proposition holds.” Lean provides the axiom command for this,\nwhich is often used in conjunction with constant(s). For example:\n-/\naxiom a_less_b (a b : ℤ) : a < b\n\n/-!\nHere we have no information about a and b beyond their type. The axiom specifies the desired\nproperty about them. The general format of the command is\n```lean\naxiom name (params₁ : type₁) . . . (paramsₘ : typeₘ) :\n statement\n```\nAxioms are dangerous, because they rapidly can lead to an inconsistent logic, in which we can derive\nfalse. For example, if we added a second axiom stating that `a = b`, we could easily derive `b < b`,\nfrom which we could derive `false` is true. The history of interactive theorem proving is paved with\ninconsistent axioms. An anecdote among many: At the 2020 edition of the Certified Programs and\nProofs (CPP) conference, a submitted paper was rejected due to a flawed axiom, from which one of the\npeer reviewers derived `false`. Therefore, we will generally avoid axioms, preferring the benign\ncombination of `def` and `theorem` to the more dangerous `axiom`.\n-/", "meta": {"author": "lovettchris", "repo": "hglv", "sha": "339f0b10f4b4a2b1e53e2b29532003a5ea4d9c9b", "save_path": "github-repos/lean/lovettchris-hglv", "path": "github-repos/lean/lovettchris-hglv/hglv-339f0b10f4b4a2b1e53e2b29532003a5ea4d9c9b/Basics/TheoremStatements.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9489172615983309, "lm_q2_score": 0.9059898102301019, "lm_q1q2_score": 0.8597093697595398}}
{"text": "import tuto_lib\nimport data.int.parity\n/-\nNegations, proof by contradiction and contraposition.\n\nThis file introduces the logical rules and tactics related to negation:\nexfalso, by_contradiction, contrapose, by_cases and push_neg.\n\nThere is a special statement denoted by `false` which, by definition,\nhas no proof.\n\nSo `false` implies everything. Indeed `false → P` means any proof of \n`false` could be turned into a proof of P.\nThis fact is known by its latin name\n\"ex falso quod libet\" (from false follows whatever you want).\nHence Lean's tactic to invoke this is called `exfalso`.\n-/\n\nexample : false → 0 = 1 :=\nbegin\n intro h,\n exfalso,\n exact h,\nend\n\n/-\nThe preceding example suggests that this definition of `false` isn't very useful.\nBut actually it allows us to define the negation of a statement P as\n\"P implies false\" that we can read as \"if P were true, we would get \na contradiction\". Lean denotes this by `¬ P`.\n\nOne can prove that (¬ P) ↔ (P ↔ false). But in practice we directly\nuse the definition of `¬ P`.\n-/\n\nexample {x : ℝ} : ¬ x < x :=\nbegin\n intro hyp,\n rw lt_iff_le_and_ne at hyp,\n cases hyp with hyp_inf hyp_non,\n clear hyp_inf, -- we won't use that one, so let's discard it\n change x = x → false at hyp_non, -- Lean doesn't need this psychological line\n apply hyp_non,\n refl,\nend\n\nopen int\n\n-- 0045\nexample (n : ℤ) (h_even : even n) (h_not_even : ¬ even n) : 0 = 1 :=\nbegin\n exfalso,\n apply h_not_even,\n exact h_even,\nend\n\n-- 0046\nexample (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q :=\nbegin\n split,\n intro h,\n cases h₁ with a b,\n exfalso,\n apply h,\n exact a,\n exact b,\n intros h ha,\n apply h₂,\n exact ⟨ha, h⟩, \nend\n\n/-\nThe definition of negation easily implies that, for every statement P,\nP → ¬ ¬ P\n\nThe excluded middle axiom, which asserts P ∨ ¬ P allows us to\nprove the converse implication.\n\nTogether those two implications form the principle of double negation elimination.\n not_not {P : Prop} : (¬ ¬ P) ↔ P\n\nThe implication `¬ ¬ P → P` is the basis for proofs by contradiction:\nin order to prove P, it suffices to prove ¬¬ P, ie `¬ P → false`.\n\nOf course there is no need to keep explaining all this. The tactic\n`by_contradiction Hyp` will transform any goal P into `false` and \nadd Hyp : ¬ P to the local context.\n\nLet's return to a proof from the 5th file: uniqueness of limits for a sequence.\nThis cannot be proved without using some version of the excluded middle\naxiom. We used it secretely in\n\neq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y\n\n(we'll prove a variation on this lemma below).\n\nIn the proof below, we also take the opportunity to introduce the `let` tactic\nwhich creates a local definition. If needed, it can be unfolded using `dsimp` which \ntakes a list of definitions to unfold. For instance after using `let N₀ := max N N'`, \nyou could write `dsimp [N₀] at h` to replace `N₀` by its definition in some\nlocal assumption `h`.\n-/\nexample (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' :=\nbegin\n intros hl hl',\n by_contradiction H,\n change l ≠ l' at H, -- Lean does not need this line\n have ineg : |l-l'| > 0,\n exact abs_pos.mpr (sub_ne_zero_of_ne H), -- details about that line are not important\n cases hl ( |l-l'|/4 ) (by linarith) with N hN,\n cases hl' ( |l-l'|/4 ) (by linarith) with N' hN',\n let N₀ := max N N', \n specialize hN N₀ (le_max_left _ _),\n specialize hN' N₀ (le_max_right _ _),\n have clef : |l-l'| < |l-l'|,\n calc\n |l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring_nf\n ... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add\n ... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub_comm\n ... < |l-l'| : by linarith,\n linarith, -- linarith can also find simple numerical contradictions\nend\n\n/-\nAnother incarnation of the excluded middle axiom is the principle of\ncontraposition: in order to prove P ⇒ Q, it suffices to prove\nnon Q ⇒ non P.\n-/\n\n-- Using a proof by contradiction, let's prove the contraposition principle\n-- 0047\nexample (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=\nbegin\n intro hP,\n by_contradiction H,\n apply h,\n exact H,\n exact hP,\nend\n\n/-\nAgain Lean doesn't need this principle explained to it. We can use the\n`contrapose` tactic.\n-/\n\nexample (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=\nbegin\n contrapose,\n exact h,\nend\n\n/-\nIn the next exercise, we'll use\n odd n : ∃ k, n = 2*k + 1\n int.odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n\n-/\n-- 0048\nexample (n : ℤ) : even (n^2) ↔ even n :=\nbegin\n split,\n contrapose,\n intro h,\n rw ← int.odd_iff_not_even,\n rw ← int.odd_iff_not_even at h,\n cases h with k hk,\n use ((2)*(k^2) + 2*k),\n rw hk,\n ring,\n intro h,\n cases h with a ha,\n use (2*(a^2)),\n rw ha,\n ring,\nend\n/-\nAs a last step on our law of the excluded middle tour, let's notice that, especially\nin pure logic exercises, it can sometimes be useful to use the\nexcluded middle axiom in its original form:\n classical.em : ∀ P, P ∨ ¬ P\n\nInstead of applying this lemma and then using the `cases` tactic, we\nhave the shortcut\n by_cases h : P,\n\ncombining both steps to create two proof branches: one assuming\nh : P, and the other assuming h : ¬ P\n\nFor instance, let's prove a reformulation of this implication relation,\nwhich is sometimes used as a definition in other logical foundations,\nespecially those based on truth tables (hence very strongly using\nexcluded middle from the very beginning).\n-/\n\nvariables (P Q : Prop)\n\nexample : (P → Q) ↔ (¬ P ∨ Q) :=\nbegin\n split,\n { intro h,\n by_cases hP : P,\n { right,\n exact h hP },\n { left,\n exact hP } },\n { intros h hP,\n cases h with hnP hQ,\n { exfalso,\n exact hnP hP },\n { exact hQ } },\nend\n\n-- 0049\nexample : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q :=\nbegin\n split,\n intro h,\n by_cases hP : P,\n right,\n intro hQ,\n apply h,\n exact ⟨hP, hQ⟩,\n left,\n exact hP,\n intro h,\n by_cases hP : P,\n intro hQ,\n cases h with a b,\n apply a,\n exact hP,\n apply b,\n exact hQ.right,\n intro hQ,\n apply hP,\n exact hQ.left,\nend\n\n/-\nIt is crucial to understand negation of quantifiers. \nLet's do it by hand for a little while.\nIn the first exercise, only the definition of negation is needed.\n-/\n\n-- 0050\nexample (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k :=\nbegin\n split,\n intros h k hk,\n apply h,\n use k,\n exact hk,\n intros h hk,\n cases hk with k ahk,\n specialize h k,\n apply h,\n exact ahk,\nend\n\n/-\nContrary to negation of the existential quantifier, negation of the\nuniversal quantifier requires excluded middle for the first implication.\nIn order to prove this, we can use either\n* a double proof by contradiction\n* a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction.\n-/\n\ndef even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x\n\n-- 0051\nexample (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=\nbegin\n split,\n contrapose,\n rw not_not,\n intros h x,\n by_contradiction H,\n apply h,\n use x,\n contrapose,\n rw not_not,\n intros h hx,\n cases hx with x Hx,\n apply Hx,\n specialize h x,\n exact h,\nend\n\n/-\nOf course we can't keep repeating the above proofs, especially the second one.\nSo we use the `push_neg` tactic.\n-/\n\nexample : ¬ even_fun (λ x, 2*x) :=\nbegin\n unfold even_fun, -- Here unfolding is important because push_neg won't do it.\n push_neg,\n use 42,\n linarith,\nend\n\n-- 0052\nexample (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=\nbegin\n split,\n intro h,\n unfold even_fun at h,\n push_neg at h,\n exact h,\n intro h,\n unfold even_fun,\n push_neg,\n exact h,\nend\n\ndef bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M\n\nexample : ¬ bounded_above (λ x, x) :=\nbegin\n unfold bounded_above,\n push_neg,\n intro M,\n use M + 1,\n linarith,\nend\n\n-- Let's contrapose\n-- 0053\nexample (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 :=\nbegin\n contrapose,\n intro ha,\n push_neg,\n push_neg at ha,\n use (x/2),\n split,\n linarith,\n linarith,\nend\n\n/-\nThe \"contrapose, push_neg\" combo is so common that we can abreviate it to\n`contrapose!`\n\nLet's use this trick, together with:\n eq_or_lt_of_le : a ≤ b → a = b ∨ a < b\n-/\n\n-- 0054\nexample (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) :=\nbegin\n split,\n intros h x y,\n split,\n intro hx,\n have key : x = y ∨ x < y,\n { exact eq_or_lt_of_le hx,},\n cases key with key₁ key₂,\n rw key₁,\n have k : f x < f y,\n { exact h x y key₂,},\n linarith,\n contrapose!,\n exact h y x,\n intros h x y,\n contrapose!,\n specialize h y x,\n rw h,\n intro ob,\n exact ob,\nend\n\n", "meta": {"author": "al-ramsey", "repo": "lean-exercises", "sha": "94d0cfb3c6055faf6254af0cd970b33278ba528c", "save_path": "github-repos/lean/al-ramsey-lean-exercises", "path": "github-repos/lean/al-ramsey-lean-exercises/lean-exercises-94d0cfb3c6055faf6254af0cd970b33278ba528c/src/exercises _live_23.01.2023/07_first_negations.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9372107984180245, "lm_q2_score": 0.9173026584553408, "lm_q1q2_score": 0.8597059569219063}}
{"text": "/-\nComplete exam below. Attest to agreement to honor\ncode. Submit your exam when done. \n-/\n\nnamespace hidden -- you can ignore this\n\n/-\n1. [10 points]\n\nWe define a polymorphic tree type. A tree of \nvalues of type α is either empty or it is a node \nformed from value of type α along with two smaller\ntrees, left and right, of the same kind. The left\nand right trees are often called the children of \na given node. \n\nWe formalize this definition as a polymorphic \ninductive type with two constructors, one, empty, \nwith no arguments, and one, with three arguments, \nas follows.\n-/\n\ninductive tree (α : Type) : Type\n| empty : tree \n| node (value : α) (left : tree) (right : tree) : tree \n\n\n/-\n1A. Define three values of type \"tree nat\": the first,\nt1, as the empty tree (of nat); the second, as a tree of nat with\none node, containing the value 5 and with two empty children; and\nt3, a tree with 1 value in its \"top\" node, where each child tree is \na node with the value 5 and two empty children.\n\nNote: You need to give a type argment explicitly when you use the\nempty constructor, but now when you use the node constructor, as\nits value can be inferred in the second case.\n-/\nopen tree\n--ANSWER\ndef t1 : tree nat := tree.empty ℕ\ndef t2 : tree nat := tree.node 5 t1 t1\ndef t3 : tree nat := tree.node 1 t2 t2\n\n/-\n1b. \nDefine a function, tree_sum, that takes a value of type \ntree nat, and that returns the *sum* of all of the values\nin the given tree. Hint: You may want to test your function\nusing some of the trees you just defined as arguments.\n-/\n\n-- ANSWER \ndef tree_sum : (tree nat) → ℕ\n| (tree.empty α) := 0\n| (tree.node a l r) := a + (tree_sum l) + (tree_sum r)\n\n#eval tree_sum t3\n/- 2. [10 points]\n\nThe memory of a computer is an array of memory cells\ncalled words. Each word has a numerical \"address\", from \nzero to the number of words in the memory minus one. If\nan address is a represented as a 16-bit value, how big\ncan the memory of the computer be? \n-/\n\n-- ANSWER\n-- 2^16\n/-\n3. [10 points] \n\nDefine a polymorphic function, tree_to_list,\nthat takes a value of type tree α as an argument and returns \na value of type list α, where the returned list contains\nall and only the values that appear in the tree: none if\nthe tree is empty, otherwise the value in the top (given)\nnode, then all the values in the left child, then all of\nthe values in the right child. \n\nHints: Remember to specify α as the type argument to\ntree.empty explicitly, and put parentheses around the\n\"patterns\" to the left of := when pattern matching.\nAlso, the list namespace is not open by default, so\nuse list.cons and list.nil for the list constructors.\nRemember that list.append can be used to append two\nlists. Finally, you might want to test your definition\nusing the tree values you defined above.\n-/\nopen list\n-- ANSWER\ndef tree_to_list {α : Type} : (tree α) → list α\n| (tree.empty α) := list.nil \n| (tree.node a l r) := list.cons a (tree_to_list l) \n/- 4. [15 points]\n\nThe next two problems ask you to specify the set of\nnatural numbers that are multiples of three in two\ndifferent ways: first as a simple predicate, and then\nas an inductive definition.\n\n4a. Specify a simple predicate (hint, use the mod\nfunction, denoted %) that is true for each multiple\nof three and false otherwise.\n-/\n\ndef mult_three (n: ℕ) : Prop := \n ∀(n), n%3=0\n\n/-\n4b. Specify the set of multiples of three as an\ninductively defined family of propositions called \nis_mult_three taking a natural number argument.\nThe proof constructors should provide proofs for\nargument values that are multiples of three and\nnot for values that are not multiples of three.\nHint: zero is a multiple of three, and, for any\nn, if n is a multiple of three, then so is n+3.\n-/\n\ninductive is_mult_three : ℕ → Prop\n| pf_zero : (is_mult_three 0)\n| pf_mult_three : ∀(n : ℕ), is_mult_three (n) → is_mult_three(nat.succ(nat.succ(nat.succ n)))\n\n/- 4c. State and prove the proposition that\n9 is a multiple of three: is_mult_three 9.\n(Don't use the \"mod\" version here.)\n-/\n\nexample : is_mult_three 9 :=\n begin\n apply is_mult_three.pf_mult_three,\n apply is_mult_three.pf_mult_three,\n apply is_mult_three.pf_mult_three,\n apply is_mult_three.pf_zero, \n end\n/- 5. [5 points].\n\nConsider a binary relation, R, on a set \nof values of some arbitrary type, α. In plain\nbut precise mathematical English, explain what \nit means for R to be transitive. \n-/\n\n-- Answer\n\n\n/-\n6. [10 points] Here we set up a set of assumptions \nand you are to prove a conclusion.\n-/\n\naxioms (Raining Sprinkler Wet : Prop)\naxiom rw : Raining → Wet\naxiom sw : Sprinkler → Wet\naxiom h : Raining ∨ Sprinkler\n\n/-\nGiven these assumptions, prove \"Wet\". Hint:\nit's a one-line proof, very short.\n-/\nexample : Wet :=\n begin\n cases h,\n apply rw h,\n apply sw h,\n end\n\n/- [10 points]\n7. Use an inductive definition to formally specify\na ternary (three-place) relation, times_rel, on ℕ\nnumbers, such that a triple, (m, n, k) is in the \nrelation if and only if k = m * n, then state and\nprove the proposition that the triple, (5, 6, 30),\nis in the relation. Hint: You need one constructor.\nCall it intro.\n-/\n\n-- Answer here\ninductive times_rel : nat → nat → nat → Prop\n| intro : ∀(m n k : ℕ), (k = m*n) → times_rel m n k\n\nexample : times_rel 5 6 30 :=\n begin\n apply times_rel.intro,\n apply eq.refl (30),\n end\n\n/- 8. [10 points] \n\nSuppose there are balls; that a ball can be made \nof uranium; that a ball can be heavy; and that if\na ball is made from uranium then it is heavy. Prove \nthat there if there is a ball made of uranium then\nthere is a ball that is heavy.\n-/\n\n-- Here are the assumptions\naxiom Ball : Type\naxiom Uranium : Ball → Prop\naxiom Heavy : Ball → Prop\naxiom uh: ∀ (b : Ball), Uranium b → Heavy b\n\n/-\n8a. *Informally* (in English) prove the conjecture\nthat if there is a Ball that is made of uranium then \nthere is a heavy ball. Identify exactly the rules of\ninference (introduction and elimination) that you are\nusing to reach your conclusion. I.e., give a precise\nand complete mathematical proof, without constructing\na formal proof.\n-/\n\n-- Answer\n/-\n Assume that a ball, a, is made of Uranium. Assume the proposition, uh, that\n if a ball is made of Uranium it is also heavy. These two assume statements \n were reached using introduction. Since it is true that ball a\n is made of urianium, by proposition uh we know that ball a is heavy. This can\n be proven using the definition of implication.\n-/\n\n\n/-\n8b. Formally prove the conjecture that if\nthere is a ball made of uranium, then there is a\nheavy ball. Hint: Put parentheses around the major\nelements of the proposition to be proved. Second\nhint: Lean won't show the preceding axioms as\nbeing in your proof contet, but you may (and\nwill have to) use at least one of them.\n-/\n\nexample : (∃ (b : Ball), Uranium b) → ∃ (b : Ball), Heavy b :=\n begin\n intros,\n cases a with b ub,\n have hb := uh b,\n have haevy := hb ub,\n apply exists.intro b,\n assumption,\n end\n\n/- 9. [10 points]\n\nRecall that a binary relation, R, on a set of \nvalues of some type, α, is said to be asymmetric\nif whenever a pair (x, y) is in R, the pair (y, x)\nis not; and that R is said to be irreflexive if\nfor all x, (x, x) is not in R. Give a mathematically\nprecise but informal (English language) proof that \nif a given binary relation, R, is asymmetric then\nit is irreflexive. As part of your proof, define,\nusing precise but informal mathematical English, \nwhat each of these properties means. And define\nusing mathematical notation the conjecture you\nare to prove. Identify each inference rule you\nuse in writing your informal proof.\n-/\n\n-- Answer\n/-\nA binary relation is Asymmetric when the ordering of the \nsupplied pair affects the outcome of the relation. For example, \nif R is asymmetric then whenever a pair (x, y) is in R, the pair (y, x)\nis not. In mathematical notation, ∀ (x y : α), R x y → ¬ R y x\nA binary relation is Irreflexive if applying the same value as both\narguments to the relation result in a statment that is not true. For \nexample, a binary relation, R, is irreflexive if the pair (x,x) is not \nin R. In mathematical notation, ∀ (x : α), ¬ R x x\n\nIf a binary relation is asymmetric it implies that it is also \nirreflexive. This is shown with the mathematical expression below\n ∀ (α : Type) (F : α → α → Prop), asymmetric F → irreflexive F\nUsing the mathematical notation for asymmetric and irreflexive from above \nwe can write the second half of the Proposition as follows,\n ∀ (x y : α),(R : Bin_op), (R x y → ¬ R y x) → (∀ (x : α),(¬ R x x))\nTo prove this relation we do the followig\n1) Assume there is some value x of type α\n2) Assume there is a value rxx that is bin_op R applied to the pair (x,x).\n3) Apply value x to the asymmetric definition and call this val rxxnrxx.\n rxxnrxx evaluates to R x x → ¬R x x\n4) apply value rxx to the Proposition rxxnrxx. This will yield a value of ¬ R x x\n which we will can nrxx\n5) At this point we have a contradiction in our assumptions. A contradiction\n is when two oposite claims are held at the same time. The opposite\n claims that we are holding are rxx and nrxx. Since these can not both \n be true we have proved irreflexive by contradiction.\n \n\n-/\n\n/-\n10. [5 points].\n\nFormally prove the proposition, ¬ false.\n-/\n\nexample : ¬ false :=\n begin\n apply false.elim, \n end\n/-\n11. [5 points] Formally prove the proposition, \n¬ true → false.\n-/\n\n-- Answer\nexample : ¬ true → false :=\n begin\n assume nt,\n apply not.intro nt,\n apply true.intro,\n end\n\n/- Extra credit.\nSuppose P and Q are arbitrary propositions. Formally \nprove that, ¬ ( P ∧ Q) ↔ (¬ P ∨ ¬ Q). Hint: Do you\nhave to reason classically? Or does it help?\n-/\n\n-- Answer\n\nexample (P Q : Prop): ¬ ( P ∧ Q) ↔ (¬ P ∨ ¬ Q) :=\n begin\n intros,\n apply iff.intro,\n intros npandq,\n apply or.inl,\n assume p,\n \n end\n\nend hidden -- You can ignore this", "meta": {"author": "derekjohnsonva", "repo": "CS2102", "sha": "b3f507d4be824a2511838a1054d04fc9aef3304c", "save_path": "github-repos/lean/derekjohnsonva-CS2102", "path": "github-repos/lean/derekjohnsonva-CS2102/CS2102-b3f507d4be824a2511838a1054d04fc9aef3304c/Exams/FinalExam2102F19.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9496693716759489, "lm_q2_score": 0.9046505447409666, "lm_q1q2_score": 0.8591189144104586}}
{"text": "/- Homework 1.1: Basics — Specifications -/\n\n/- Question 1: Snoc -/\n\n/- 1.1. Define the function `snoc` that appends a single element to the end of\n a list. -/\n\ndef snoc {α : Type} : list α → α → list α\n| [] l := [] ++ [l]\n| (a::b) l := a :: b ++ [l]\n\n-- ZZZ: Jasmins solution using append.\ndef snoc' {α : Type} : list α → α → list α\n| l x := l ++ [x]\n\n#reduce snoc [] 5\n#reduce snoc [2,4,6] 8\n\n/- 1.2. Convince yourself that your definition of `snoc` works by testing it on\n a few examples. -/\n\n-- invoke `#reduce` here\n\n\n/- Question 2: Map -/\n\n/- 2.1. Define a generic `map` function that applies a function to every\n element in a list. -/\n\ndef map {α : Type} {β : Type} (f : α → β) : list α → list β\n| [] := []\n| (a::b) := (f a) :: (map b)\n\n#reduce map(+10)[2,4,5]\n#reduce map(/2)[10,20,30]\n\n/- 2.2. State the so-called functiorial properties of `map` as lemmas.\n Schematically:\n\n map (λx, x) xs = xs\n map (λx, g (f x)) xs = map g (map f xs)\n\n Make sure to state the second law as generally as possible, for\n arbitrary types.\n-/\n\nlemma m1 {α: Type} (xs: list α): map(λx, x) xs = xs\n:=sorry\n\nlemma m2 {α: Type} {β: Type} {γ: Type} (xs: list α) (f: α → β) (g: β → γ): map(λx, g(f x)) xs = map g (map f xs)\n:=sorry\n", "meta": {"author": "KjellZijlemaker", "repo": "Logical_Verification_VU", "sha": "4578b93bf1615466996157bb333c84122b201d99", "save_path": "github-repos/lean/KjellZijlemaker-Logical_Verification_VU", "path": "github-repos/lean/KjellZijlemaker-Logical_Verification_VU/Logical_Verification_VU-4578b93bf1615466996157bb333c84122b201d99/1.1/Homework week1.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9294403979493139, "lm_q2_score": 0.924141819456443, "lm_q1q2_score": 0.8589347404371994}}
{"text": "-- Razonamiento ecuacional sobre longitudes de listas\n-- ==================================================\n\nimport data.list.basic\nopen list\n\nvariable {α : Type}\nvariable x : α\nvariable (xs : list α)\n\n-- ----------------------------------------------------\n-- Ejercicio 1. Definir, por recursión, la función\n-- longitud : list α → ℕ\n-- tal que (longitud xs) es la longitud de la lista\n-- xs. Por ejemplo,\n-- longitud [a,c,d] = 3\n-- ----------------------------------------------------\n\ndef longitud : list α → ℕ\n| [] := 0\n| (_ :: xs) := longitud xs + 1\n\n-- ----------------------------------------------------\n-- Ejercicio 2. Calcular\n-- longitud [4,2,5]\n-- ----------------------------------------------------\n\n-- #eval longitud [4,2,5]\n-- da 3\n\n-- ----------------------------------------------------\n-- Ejercicio 3. Demostrar los siguientes lemas\n-- + longitud_nil :\n-- longitud ([] : list α) = 0\n-- + longitud_cons :\n-- longitud (x :: xs) = longitud xs + 1\n-- ----------------------------------------------------\n\n@[simp]\nlemma longitud_nil :\n longitud ([] : list α) = 0 :=\nrfl\n\n@[simp]\nlemma longitud_cons :\n longitud (x :: xs) = longitud xs + 1 :=\nrfl\n\n-- ----------------------------------------------------\n-- Ejercicio 4. Demostrar que\n-- longitud [a,b,c] = 3\n-- ----------------------------------------------------\n\n-- 1ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\ncalc\n longitud [a,b,c]\n = longitud [b,c] + 1 : by rw longitud_cons\n ... = (longitud [c] + 1) + 1 : by rw longitud_cons\n ... = ((longitud [] + 1) + 1) + 1 : by rw longitud_cons\n ... = ((0 + 1) + 1) + 1 : by rw longitud_nil\n ... = 3 : rfl\n\n-- 2ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\ncalc\n longitud [a,b,c]\n = longitud [b,c] + 1 : rfl\n ... = (longitud [c] + 1) + 1 : rfl\n ... = ((longitud [] + 1) + 1) + 1 : rfl\n ... = ((0 + 1) + 1) + 1 : rfl\n ... = 3 : rfl\n\n-- 3ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\nbegin\n rw longitud_cons,\n rw longitud_cons,\n rw longitud_cons,\n rw longitud_nil,\nend\n\n-- 4ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\nby rw [longitud_cons,\n longitud_cons,\n longitud_cons,\n longitud_nil]\n\n-- 5ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\nby simp only [longitud_cons,\n longitud_nil]\n\n-- 4ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\nby simp\n\n-- 6ª demostración\nexample\n (a b c : α)\n : longitud [a,b,c] = 3 :=\nrfl\n\n-- Comentarios sobre la función length:\n-- + Es equivalente a la función longitud.\n-- + Para usarla hay que importar la librería\n-- data.list.basic y abrir el espacio de nombre\n-- list escribiendo al principio del fichero\n-- import data.list.basic\n-- open list\n-- + Se puede evaluar. Por ejemplo,\n-- #eval length [4,2,5,7,2]\n-- + Se puede demostrar. Por ejemplo,\n-- example\n-- (a b c : α)\n-- : length [a,b,c] = 3 :=\n-- rfl\n", "meta": {"author": "jaalonso", "repo": "Logica_con_Lean", "sha": "beb6765c6ff3c05590a03f45722eda0c815a25cd", "save_path": "github-repos/lean/jaalonso-Logica_con_Lean", "path": "github-repos/lean/jaalonso-Logica_con_Lean/Logica_con_Lean-beb6765c6ff3c05590a03f45722eda0c815a25cd/src/7_Programas/Razonamiento_ecuacional_sobre_longitudes_de_listas.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9263037262250327, "lm_q2_score": 0.9263037333456406, "lm_q1q2_score": 0.8580385998142259}}
{"text": "/-\nProduce a proof, pf1, of the proposition\nthat 0 = 0 ∧ (1 = 1 ∧ 2 = 2).\n-/\n\nlemma pf1 : 0 = 0 ∧ (1 = 1 ∧ 2 = 2) :=\n and.intro rfl (and.intro rfl rfl)\n\n/-\nProduce a proof, pf2, of the proposition\nthat (0 = 0 ∧ 1 = 1) ∧ 2 = 2\n-/\n\nlemma pf2 : (0 = 0 ∧ 1 = 1) ∧ 2 = 2 :=\n and.intro (and.intro rfl rfl) rfl\n\n/-\nProduce a proof, pf3, of the proposition\nthat 0 = 0 ∧ 1 = 1 ∧ 2 = 2. Hint, one of\nthe two preceding proofs can be used to\nprove this proposition; there's no need\nto type out a whole new proof.\n-/\nlemma pf3 : 0 = 0 ∧ 1 = 1 ∧ 2 = 2 :=\n pf1\n\n/-\nAn operator, *, is \"right associative\"\nif X * Y * Z means X * (Y * Z), and is\n\"left associative\" if X * Y * Z means\n(X * Y) * Y. Is the logical connective,\n∧, left or right associative? Explain.\n-/\n\n-- It's right associative as we just proved\n-- pf3 with pf1, and pf1 is in the form\n-- X * (Y * Z). \n\n/-\nUse Lean to produce a proof, pf4, that \n0 = 0 ∧ 1 = 1 ∧ 2 = 2 is exactly the\nsame proposition as one of the two\nparenthesized forms. It will be a \nproof that two propositions are equal.\nPut parentheses around each of the\npropositions.\n-/\n\nlemma pf4 :\n (0 = 0 ∧ 1 = 1 ∧ 2 = 2) =\n (0 = 0 ∧ (1 = 1 ∧ 2 = 2)) := \n rfl\n\n/-\nGiven arbitrary propositions, P, Q and\nR it should be possible to produce a\nproof, pf5, showing that if P ∧ (Q ∧ R)\nis true then so is (P ∧ Q) ∧ R. Written\nin inference rule form, this would say \nthe following:\n\n{ P Q R: Prop }, pqr_left : P ∧ (Q ∧ R)\n---------------------------------- ∧.assoc'\n pqr_right : (P ∧ Q) ∧ R\n\nProving that this is a valid rule \ncan be done by defining a function, \nlet's call it and_assoc_l, that when \ngiven any propositions, P, Q, and R \n(implicitly), and when given a proof \nof P ∧ (Q ∧ R), constructs and returns\na proof of (P ∧ Q) ∧ R. \n\nHere we give you this function, and\nwe explain each part in comments. \nYou will then apply what you learn \nby studying this example to solve \nthe same problem but going in the\nother direction. Here's the solution.\n-/\n\n-- define the function name\ndef and_assoc_l \n-- specify the arguments and their types\n {P Q R: Prop} \n (pf: P ∧ (Q ∧ R)) : -- note colon\n\n-- the return type \n (P ∧ Q) ∧ R\n/-\nWhat we've given so far is what we call\nthe function signature: its name, the\nnames and types of the arguments that it\ntakes, and the type of the return value.\nIn this case, the return value is of \ntype (P ∧ Q) ∧ R, and will thus serve\nas a proof of this proposition. This is\na function that takes a proof and returns\na (different) proof. It thus provides a\ngeneral recipe for turning any proof of\nP ∧ (Q ∧ R) into a proof of (P ∧ Q) ∧ R.\n-/ \n := -- now give function body\n\n/-\nUsually we'd expect to see an expression\nhere, involving multiple, nested and.elim \nand and.intro expressions. We could write\nthe function body that way, but it's a bit\ntricky to get all the nested expressions\nright. Here's a revelation: We can use a\ntactic script to produce the same result.\n\nOpen your Messages window, put your cursor\non begin, study carefully the tactic state,\nnotice that the arguments are given in the\ncontext to the left of the turnstile and\nthe goal remaining to be proved is to the\nright. You can use the context values as\narguments to tactics.\n\nNow click through each line of the script\nand study very carefully how it changes the\ncontext. By the end of the script, you \nshould see how we've been able to use \nelimination rules take apart the proof \nthat was given as an argument, giving names \nto the parts, and how we can then further \ntake apart those parts, giving names to the \nsubparts, and finally how we can intro \nrules to put all these pieces together\nagain into the proof we need. \n-/\n begin\n have pfP := and.elim_left pf,\n have pfQR := and.elim_right pf,\n have pfQ := and.elim_left pfQR,\n have pfR := and.elim_right pfQR,\n have pfPQ := and.intro pfP pfQ,\n have pfPQ_R := and.intro pfPQ pfR,\n exact pfPQ_R\n end\n\n/-\nDefine another function, and_assoc_r,\nthat goes the other direction: given\na proof of (P ∧ Q) ∧ R it derives and\nreturns a proof of P ∧ (Q ∧ R). Write \nthe entire solution yourself.\n-/\n\ndef and_assoc_r \n {P Q R: Prop} \n (pf: (P ∧ Q) ∧ R) : \n P ∧ (Q ∧ R) :=\n begin\n have pfPQ := and.elim_left pf,\n have pfR := and.elim_right pf,\n have pfP := and.elim_left pfPQ,\n have pfQ := and.elim_right pfPQ, \n have pfQR := and.intro pfQ pfR,\n have pfP_QR := and.intro pfP pfQR,\n exact pfP_QR\n end\n\n\n/-\nIt's important to learn how you would\ngive such proofs in natural langage.\nLet's take our first example. Here is\na natural language version.\n\n\"Given arbitrary propositions, P, Q, and\nR, and the assumption that P ∧ (Q ∧ R) is\ntrue, we are to show that (P ∧ Q) ∧ R is\ntrue.\n\nGiven that P ∧ (Q ∧ R) is true, it must \nbe that P is true and that Q ∧ R is also\ntrue. Given that Q ∧ R is true, it must\nbe that Q is true, and R is also true.\nSo we have that P, Q, and R are all true.\n\nFrom these conclusions we can in turn\ndeduce that P ∧ Q must be true. And so\nwe now have that P ∧ Q is true and so is\nR, from which, finally we can deduce \nthat (P ∧ Q) ∧ R must be true as well.\n\nQED.\"\n\nNow it's your turn: write an English\nlanguage proof for the theorem in the \nother direction.\n-/\n\n/-\n\"Given arbitrary propositions, P, Q, and\nR, and the assumption that (P ∧ Q) ∧ R is\ntrue, we are to show that P ∧ (Q ∧ R) is\ntrue.\n\nGiven that (P ∧ Q) ∧ R, it must be that \n(P ∧ Q) is true and also that R is true.\nGiven that (P ∧ Q) is true, then P must \nbe true, and Q must be true. Therefore\nP, Q, and R are all true.\n\nFrom this conclusion we can deduce that \nQ ∧ R must be true. Since P is true and \n(Q ∧ R) is true, it follows that P ∧ (Q ∧ R)\nmust be true as well.\n\nQED.\"\n\n-/\n\n/-\nUse Lean to produce a proof, tnott, of\nthe proposition that truth isn't truth. \nI.e., true is not true. We'll write this\nis Lean like this:\n\ntheorem tnott: true ≠ true := _. \n\nTo make it a little easier to solve \nthis otherwise difficult problem, we\nallow you to stipulate one \"axiom\" of\nyour choice, which you can then use\nto produce the required proof.\n-/\n\n-- You can introduce an axiom here\naxiom f: false\n\n-- Now prove the theorem\ntheorem tnott : true ≠ true := false.elim f\n\n/-\nWhat did you have to accept to be able \nto prove that truth isn't truth?\n-/\n\n/- We had to accept that there was an axiom\nfor false, which is an absurdity, which\nthen allows us to create a proof for a \nproposition for which there is not proof\n-/", "meta": {"author": "Mnormansell", "repo": "Discrete-Notes", "sha": "61f13b98be590269fc4822be7b47924a6ddc1261", "save_path": "github-repos/lean/Mnormansell-Discrete-Notes", "path": "github-repos/lean/Mnormansell-Discrete-Notes/Discrete-Notes-61f13b98be590269fc4822be7b47924a6ddc1261/HW/HW3-key.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.901920681802153, "lm_q2_score": 0.951142221550942, "lm_q1q2_score": 0.85785484095204}}
{"text": "import ..lectures.love05_inductive_predicates_demo\n\n\n/-! # LoVe Exercise 5: Inductive Predicates -/\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1: Even and Odd\n\nThe `even` predicate is true for even numbers and false for odd numbers. -/\n\n#check even\n\n/-! We define `odd` as the negation of `even`: -/\n\ndef odd (n : ℕ) : Prop :=\n ¬ even n\n\n/-! 1.1. Prove that 1 is odd and register this fact as a simp rule.\n\nHint: `cases'` is useful to reason about hypotheses of the form `even …`. -/\n\n@[simp] lemma odd_1 :\n odd 1 :=\nbegin\n intro h,\n cases' h\nend\n\n/-! 1.2. Prove that 3 and 5 are odd. -/\n\nlemma odd_3 :\n odd 3 :=\nbegin\n intro h,\n cases' h,\n cases' h\nend\n\nlemma odd_5 :\n odd 5 :=\nbegin\n intro h,\n cases' h,\n cases' h,\n cases' h\nend\n\n/-! 1.3. Complete the following proof by structural induction. -/\n\nlemma even_two_times :\n ∀m : ℕ, even (2 * m)\n| 0 := even.zero\n| (m + 1) :=\n begin\n apply even.add_two,\n simp,\n apply even_two_times\n end\n\n/-! 1.4. Complete the following proof by rule induction.\n\nHint: You can use the `cases'` tactic (or `match … with`) to destruct an\nexistential quantifier and extract the witness. -/\n\nlemma even_imp_exists_two_times :\n ∀n : ℕ, even n → ∃m, n = 2 * m :=\nbegin\n intros n hen,\n induction' hen,\n case zero {\n apply exists.intro 0,\n refl },\n case add_two : k hek ih {\n cases' ih with w hk,\n apply exists.intro (w + 1),\n rw hk,\n linarith }\nend\n\n/-! 1.5. Using `even_two_times` and `even_imp_exists_two_times`, prove the\nfollowing equivalence. -/\n\nlemma even_iff_exists_two_times (n : ℕ) :\n even n ↔ ∃m, n = 2 * m :=\nbegin\n apply iff.intro,\n { apply even_imp_exists_two_times },\n { intro h,\n cases' h,\n simp *,\n apply even_two_times }\nend\n\n/-! 1.6 (**optional**). Give a structurally recursive definition of `even` and\ntest it with `#eval`.\n\nHint: The negation operator on `bool` is called `not`. -/\n\ndef even_rec : nat → bool\n| 0 := tt\n| (n + 1) := not (even_rec n)\n\n#eval even_rec 0\n#eval even_rec 1\n#eval even_rec 2\n#eval even_rec 3\n#eval even_rec 4\n\n\n/-! ## Question 2: Tennis Games\n\nRecall the inductive type of tennis scores from the demo: -/\n\n#check score\n\n/-! 2.1. Define an inductive predicate that returns true if the server is ahead\nof the receiver and that returns false otherwise. -/\n\ninductive srv_ahead : score → Prop\n| vs {m n : ℕ} : m > n → srv_ahead (score.vs m n)\n| adv_srv : srv_ahead score.adv_srv\n| game_srv : srv_ahead score.game_srv\n\n/-! 2.2. Validate your predicate definition by proving the following lemmas. -/\n\nlemma srv_ahead_vs {m n : ℕ} (hgt : m > n) :\n srv_ahead (score.vs m n) :=\nsrv_ahead.vs hgt\n\nlemma srv_ahead_adv_srv :\n srv_ahead score.adv_srv :=\nsrv_ahead.adv_srv\n\nlemma not_srv_ahead_adv_rcv :\n ¬ srv_ahead score.adv_rcv :=\nbegin\n intro h,\n cases' h\nend\n\nlemma srv_ahead_game_srv :\n srv_ahead score.game_srv :=\nsrv_ahead.game_srv\n\nlemma not_srv_ahead_game_rcv :\n ¬ srv_ahead score.game_rcv :=\nbegin\n intro h,\n cases' h\nend\n\n/-! 2.3. Compare the above lemma statements with your definition. What do you\nobserve? -/\n\n/-! The positive lemmas correspond exactly to the introduction rules of the\ndefinition. By contrast, the negative lemmas have no counterparts in the\ndefinition. -/\n\n\n/-! ## Question 3: Binary Trees\n\n3.1. Prove the converse of `is_full_mirror`. You may exploit already proved\nlemmas (e.g., `is_full_mirror`, `mirror_mirror`). -/\n\n#check is_full_mirror\n#check mirror_mirror\n\nlemma mirror_is_full {α : Type} :\n ∀t : btree α, is_full (mirror t) → is_full t :=\nbegin\n intros t fmt,\n have fmmt : is_full (mirror (mirror t)) :=\n is_full_mirror _ fmt,\n rw mirror_mirror at fmmt,\n assumption\nend\n\n/-! 3.2. Define a `map` function on binary trees, similar to `list.map`. -/\n\ndef map_btree {α β : Type} (f : α → β) : btree α → btree β\n| btree.empty := btree.empty\n| (btree.node a l r) := btree.node (f a) (map_btree l) (map_btree r)\n\n/-! 3.3. Prove the following lemma by case distinction. -/\n\nlemma map_btree_eq_empty_iff {α β : Type} (f : α → β) :\n ∀t : btree α, map_btree f t = btree.empty ↔ t = btree.empty\n| btree.empty := by simp [map_btree]\n| (btree.node _ _ _) := by simp [map_btree]\n\n/-! 3.4 (**optional**). Prove the following lemma by rule induction. -/\n\nlemma map_btree_mirror {α β : Type} (f : α → β) :\n ∀t : btree α, is_full t → is_full (map_btree f t) :=\nbegin\n intros t hfull,\n induction' hfull,\n case empty {\n simp [map_btree],\n exact is_full.empty },\n case node {\n simp [map_btree],\n apply is_full.node,\n { exact ih_hfull f },\n { exact ih_hfull_1 f },\n { simp [map_btree_eq_empty_iff],\n assumption } }\nend\n\nend LoVe\n", "meta": {"author": "BrownCS1951x", "repo": "fpv2021", "sha": "10bdbd92e64fb34115b68794b8ff480468f4dcaa", "save_path": "github-repos/lean/BrownCS1951x-fpv2021", "path": "github-repos/lean/BrownCS1951x-fpv2021/fpv2021-10bdbd92e64fb34115b68794b8ff480468f4dcaa/src/exercises/love05_inductive_predicates_exercise_solution.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9525741295151718, "lm_q2_score": 0.9005297947939938, "lm_q1q2_score": 0.8578213853783649}}
{"text": "/-\nCopyright (c) 2022 Thomas Browning. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Thomas Browning\n\n! This file was ported from Lean 3 source module algebra.group.commutator\n! leanprover-community/mathlib commit 448144f7ae193a8990cb7473c9e9a01990f64ac7\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Algebra.Group.Defs\nimport Mathbin.Data.Bracket\n\n/-!\n# The bracket on a group given by commutator.\n\n> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.\n> Any changes to this file require a corresponding PR to mathlib4.\n-/\n\n\n#print commutatorElement /-\n/-- The commutator of two elements `g₁` and `g₂`. -/\ninstance commutatorElement {G : Type _} [Group G] : Bracket G G :=\n ⟨fun g₁ g₂ => g₁ * g₂ * g₁⁻¹ * g₂⁻¹⟩\n#align commutator_element commutatorElement\n-/\n\n/- warning: commutator_element_def -> commutatorElement_def is a dubious translation:\nlean 3 declaration is\n forall {G : Type.{u1}} [_inst_1 : Group.{u1} G] (g₁ : G) (g₂ : G), Eq.{succ u1} G (Bracket.bracket.{u1, u1} G G (commutatorElement.{u1} G _inst_1) g₁ g₂) (HMul.hMul.{u1, u1, u1} G G G (instHMul.{u1} G (MulOneClass.toHasMul.{u1} G (Monoid.toMulOneClass.{u1} G (DivInvMonoid.toMonoid.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1))))) (HMul.hMul.{u1, u1, u1} G G G (instHMul.{u1} G (MulOneClass.toHasMul.{u1} G (Monoid.toMulOneClass.{u1} G (DivInvMonoid.toMonoid.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1))))) (HMul.hMul.{u1, u1, u1} G G G (instHMul.{u1} G (MulOneClass.toHasMul.{u1} G (Monoid.toMulOneClass.{u1} G (DivInvMonoid.toMonoid.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1))))) g₁ g₂) (Inv.inv.{u1} G (DivInvMonoid.toHasInv.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1)) g₁)) (Inv.inv.{u1} G (DivInvMonoid.toHasInv.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1)) g₂))\nbut is expected to have type\n forall {G : Type.{u1}} [_inst_1 : Group.{u1} G] (g₁ : G) (g₂ : G), Eq.{succ u1} G (Bracket.bracket.{u1, u1} G G (commutatorElement.{u1} G _inst_1) g₁ g₂) (HMul.hMul.{u1, u1, u1} G G G (instHMul.{u1} G (MulOneClass.toMul.{u1} G (Monoid.toMulOneClass.{u1} G (DivInvMonoid.toMonoid.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1))))) (HMul.hMul.{u1, u1, u1} G G G (instHMul.{u1} G (MulOneClass.toMul.{u1} G (Monoid.toMulOneClass.{u1} G (DivInvMonoid.toMonoid.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1))))) (HMul.hMul.{u1, u1, u1} G G G (instHMul.{u1} G (MulOneClass.toMul.{u1} G (Monoid.toMulOneClass.{u1} G (DivInvMonoid.toMonoid.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1))))) g₁ g₂) (Inv.inv.{u1} G (DivInvMonoid.toInv.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1)) g₁)) (Inv.inv.{u1} G (DivInvMonoid.toInv.{u1} G (Group.toDivInvMonoid.{u1} G _inst_1)) g₂))\nCase conversion may be inaccurate. Consider using '#align commutator_element_def commutatorElement_defₓ'. -/\ntheorem commutatorElement_def {G : Type _} [Group G] (g₁ g₂ : G) :\n ⁅g₁, g₂⁆ = g₁ * g₂ * g₁⁻¹ * g₂⁻¹ :=\n rfl\n#align commutator_element_def commutatorElement_def\n\n", "meta": {"author": "leanprover-community", "repo": "mathlib3port", "sha": "62505aa236c58c8559783b16d33e30df3daa54f4", "save_path": "github-repos/lean/leanprover-community-mathlib3port", "path": "github-repos/lean/leanprover-community-mathlib3port/mathlib3port-62505aa236c58c8559783b16d33e30df3daa54f4/Mathbin/Algebra/Group/Commutator.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9372107896491796, "lm_q2_score": 0.9149009578933863, "lm_q1q2_score": 0.8574550491980514}}
{"text": "import algebra.group_power tactic.norm_num algebra.big_operators\n\ndef Fib : ℕ → ℕ\n| 0 := 0\n| 1 := 1\n| (n+2) := Fib n + Fib (n+1)\n\ndef is_even (n : ℕ) : Prop := ∃ k, n=2*k\ndef is_odd (n : ℕ) : Prop := ∃ k, n=2*k+1\n\nlemma even_of_even_add_even (a b : ℕ) : is_even a → is_even b → is_even (a+b) :=\nbegin\nintros Ha Hb,\ncases Ha with k Hk,\ncases Hb with l Hl,\nexistsi k+l,\nsimp [Hk,Hl,add_mul],\nend\n\nlemma odd_of_odd_add_even {a b : ℕ} : is_odd a → is_even b → is_odd (a+b) :=\nbegin\nintros Ha Hb,\ncases Ha with k Hk,\ncases Hb with l Hl,\nexistsi k+l,\nsimp [Hk,Hl,add_mul],\nend\n\nlemma odd_of_even_add_odd {a b : ℕ} : is_even a → is_odd b → is_odd (a+b) :=\nλ h1 h2, (add_comm b a) ▸ (odd_of_odd_add_even h2 h1)\n\n\nlemma even_of_odd_add_odd {a b : ℕ} : is_odd a → is_odd b → is_even (a+b) :=\nbegin\nintros Ha Hb,\ncases Ha with k Hk,\ncases Hb with l Hl,\nexistsi k+l+1,\n-- simp [mul_add,Hk,Hl,one_add_one_eq_two] -- fails!\nrw [Hk,Hl,mul_add,mul_add],\nchange 2 with 1+1,\nsimp [mul_add,add_mul],\nend\n\ntheorem Q1a : ∀ n : ℕ, n ≥ 1 →\n is_odd (Fib (3*n-2)) ∧ is_odd (Fib (3*n-1)) ∧ is_even (Fib (3*n)) := sorry\n\ntheorem Q1b : is_odd (Fib (2017)) := sorry\n\n", "meta": {"author": "ImperialCollegeLondon", "repo": "xena-UROP-2018", "sha": "b111fb87f343cf79eca3b886f99ee15c1dd9884b", "save_path": "github-repos/lean/ImperialCollegeLondon-xena-UROP-2018", "path": "github-repos/lean/ImperialCollegeLondon-xena-UROP-2018/xena-UROP-2018-b111fb87f343cf79eca3b886f99ee15c1dd9884b/src/M1F/problem_bank/0501/Q0501.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9653811631528337, "lm_q2_score": 0.8872045966995027, "lm_q1q2_score": 0.8564906055163066}}
{"text": "import data.nat.prime\nimport data.nat.parity\nimport tactic\nopen nat\n\nlemma eq_two_of_prime_and_even\n {n : ℕ}\n (hn : even n)\n (hn' : nat.prime n)\n : n = 2 :=\nbegin\n symmetry,\n rw ← prime_dvd_prime_iff_eq prime_two hn',\n exact even_iff_two_dvd.mp hn\nend\n\n-- 2ª demostración\n-- ===============\n\nlemma eq_two_of_prime_and_even2\n (n : ℕ)\n (hn : even n)\n (hn' : nat.prime n)\n : n = 2 :=\nbegin\n contrapose! hn,\n rw [← odd_iff_not_even, odd_iff],\n exact or.resolve_left (prime.eq_two_or_odd hn') hn,\nend\n", "meta": {"author": "jaalonso", "repo": "Demostraciones-con-Lean", "sha": "034ee559250cf55e28d10d8e65f1a4642e284251", "save_path": "github-repos/lean/jaalonso-Demostraciones-con-Lean", "path": "github-repos/lean/jaalonso-Demostraciones-con-Lean/Demostraciones-con-Lean-034ee559250cf55e28d10d8e65f1a4642e284251/src/Si n es un primo par entonces es igual a 2.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.952574122783325, "lm_q2_score": 0.8991213745668094, "lm_q1q2_score": 0.8564797546537158}}
{"text": "/-\nWe've met but haven't yet been\nproperly introduced to ∀. \n\nAssuming P is a Type and Q is a\nproposition, What does (∀ p : P, \nQ) mean?\n\nFor example, what does this mean?\n\n ∀ n : nat, n + 1 ≠ 0\n\nFirst, you can read ∀ n : nat \n\"for all values, n, of type nat.\"\nThis proposition thus asserts that, \nfor all natural numbers, n, of type\nnat, (i.e., \"for any natural number, \nn\") the successor of n is not 0.\n\nMore generally, with P as any type,\nand Q as a proposition, (∀ p : P, Q)\nstates that for every value, p, in \nthe set of values defined by the type, \nP, one can derive a proof of Q. \n\nHere's our concrete example stated \nand proved in Lean.\n-/\n\nexample : ∀ n : nat, n + 1 ≠ 0 :=\n -- assume an arbitrary nat, n\n λ n : nat, \n -- assume proof of n + 1 = 0\n λ h : (n + 1 = 0), \n -- derive a contradiction\n (nat.no_confusion h : false)\n -- therefore n + 1 ≠ 0\n\n/-\nNote that the parentheses around (n: nat)\nare not required by Lean, but they might\nhelp the human reader to understand the\nintention better.\n-/\n\n/-\nHere's another example, which you\nhave seen before (without a proof).\nIt says that for any proposition, \nP (in the set of all propositions\ndefined by the type, Prop), it is \nnot possible to have both P and \n¬ P be true at the same time.\n-/\n\nexample : ∀ P : Prop, ¬ (P ∧ ¬ P) :=\n -- assume an arbitrary proposition\n λ P : Prop,\n -- assume it's both true and false\n λ h : (P ∧ ¬ P),\n -- derive a contradiction\n (h.right h.left)\n -- thereby proving ¬ h\n\n#check (3)\n/-\nThe ∀ parts of these propositions, \nbefore the commas, give names to\narbitrary elements of a type. The\nparts after the commas then state\na claim (that is usually) about the\nnamed element.\n\nLook at the preceding examples. In\nthe case of ∀ n : nat, n + 1 ≠ 0, the\npart before the comma asserts that\nthe proposition following the comma\nis true of any arbitrary (and thus \nof every) element, n, of type nat.\n\nWe say that the ∀ \"binds\" the name\nto an arbitrary value of the given\ntype. The binding is operative for \nthe remainder of the proposition. \n\nWe note that successive bindings\naccumulate, defining a context in\nwhich the remainder of a proposition\nis stated and proved. \n\nHere's an example that states that\nfor any two natural numbers, n and\nm, either n = m or n ≠ m. We use the\nconnective, ∨, to denote disjunction\n(logical or). More about that later.\nWe \"stub out\" the simple proof for\nnow.\n-/\n\nexample : ∀ (n : nat), ∀ (m : nat), \n m = n ∨ m ≠ n := \nbegin\n assume n : nat,\n -- the context now includes n\n assume m : nat,\n -- the context now also has m\n sorry\nend\n-- the bindings no longer hold here\n\n\n/-\nSo that should give an understanding\nof the use and meaning of the universal \nquantifier, ∀, in predicate logic.\n\nWhat does (∀ (p : P), Q) mean in the\nconstructive logic of Lean?\n\nLet's check! First we'll assume two\narbitrary propositions, P and Q, and\na proof of ∀ p : P, Q. Then we can ask\nwhat is the type of ∀ p : P, Q. It is\na proposition, after all, and in Lean,\npropositions are types. What type is \nit?\n-/\n\n-- Assume P and Q are propositions\nvariables P Q : Prop\n\n-- What is the type of (∀ p : P, Q)\n#check (∀ p : P, Q)\n\n#check ∀ n: nat, nat \n\n/-\nWhat? the proposition/type, \n(∀ p : P, Q) is really just\nthe proposition/type, P → Q.\n-/\n\n/-\nIf that's true, we should be able \nto assume proof of (∀ p : P, Q)\nand then use it as a function. \nLet's see.\n-/\n\n-- Assume a proof of ∀ (p : P), Q.\nvariable ap2q : (∀ p : P, Q)\n\n-- Assume a proof of P.\nvariable p : P\n\n-- What is the type of (ap2q p). Q\n#check ap2q p\n\n-- They're the same types! Here's a proof.\ntheorem same : (∀ p : P, Q) = (P → Q) := rfl\n\n/-\nSo why not just use → instead of ∀? The\nanswer is that ∀ let's us bind a name to\nan assumed value, a name that we can then\nuse in the remainder of the expression.\nThe → connective doesn't let us bind a\nname to a value. \n\nThe following example thus defines a type \nof function that, given a natural number, \nn, reduces to (returns) the proposition\nthat that particular n is either 0 or not\n0. This is a function type. \n-/\n\n#check ∀ n : nat, n = 0 ∨ n ≠ 0.\n\n\n/-\nA proof of this proposition/type is then\na function that, if given any nat value, n,\nreturns a value of type (a proof that) that\nparticular n is either 0 or not 0.\n\nThe binding of a name to an assumed value\neffected by a ∀ provides us a context in \nwhich we can state the remainder of the\nuniversally quantified proposition. \n-/\n\n/-\nSome work done in class\n-/\n\ndef inc : ℕ → ℕ := \n λ n : nat, \n n + 1\n\ndef inc' : ∀ n: nat, nat := \n λ n : nat, n + 1\n\ndef add : ∀ n : nat, ∀ m :nat, nat :=\n λ n m, n + m\n\ndef add' : ∀ n : nat, (∀ m :nat, nat) :=\n λ n : nat, \n λ m : nat, \n n + m\n\n#eval add 3 4\n#eval add' 3 4\n\n#check add' 3\n\n#eval (add' 3) \n\ndef add3 := (add' 3) \n\n#check add3\n\n#eval add3 4\n\n\n/- ** Further explanation ** -/\n\n/-\nSo now we can see at least three ways to\nread (∀ p : P, Q).\n\n(1) As a universally quantified logical \nproposition. As such, it asserts that if\np is any value of type P, then from it\none can derive a proof of the proposition,\nQ, which will typically involve p. \n\n\n(2) As a logical implication, P → Q. This\nis read simply as P implies Q. \n\n(3) As the type of total functions from \nP to Q. \n\nBy a total function, we mean a function \nthat is defined \"for all\" values of its \nargument types. It cannot be a \"partial\"\nfunction that is undefined on some values\nof its argument type. \n\nThe concepts of \"for all\" and of total\nfunctions are intimately related here.\nThe ∀ explicitly requires that such a \nfunction be defined \"for all\" values of\nthe designated type.\n\nThat functions are total is fundamental \nto constructive logic. We will explain\nwhy later.\n-/\n\n\n\n/- ** Further examples ** -/\n\n/- \n\n#1. Nested ∀ bindings.\n\nLet' assume we have another proposition, R.\n-/\n\nvariable R : Prop\n\n/-\nSo, what does this mean: ∀ p : P, ∀ q : Q, R?\nThe ∀ is right-associative so we read this as \n∀ p : P, (∀ q : Q, R). Using what we learned \nabove, we can see that it means P → Q → R!\n-/\n\n#check ∀ (p : P), (∀ q : Q, R)\n\n/-\nThere are at least three ways to think about\nthis construct.\n\n(1) It is the universally quantified proposition \nthat holds that if one assumes any proof P and \nthen further assumes any proof of Q, then in \nthat context one can derive a proof of R. \n\n(2) It is the logical proposition, P implies\nQ implies R, written as P → Q → R.\n\n(3) Is it the function type, P → Q → R. As\nwe've discussed, → is right associative, \nso this is the function type is P → (Q → R). \n-/\n\n#check ∀(n: ℕ), (∀(m: ℕ), m + n >= 0)\n\n/- ** Chained implications ** -/\n\n/-\nWhat is the function type, P → Q → R? It\nis the type of function that takes a value \nof type P as an argument and reduces to a\nfunction of type (Q → R), a function that\ntakes a value of type Q, and that finally \nreduces to a value of type R.\n\nOf course, you can just think of this as a\nfunction that takes two arguments, the first\nof type P and the second of type Q, and that\nthen reduces to a value of type R.\n\nIn fact in Lean and in most functional \nlanguages, you can treat any function that \ntakes multiple arguments as one that takes \none argument (the first) and then reduces \nto a function that takes the next argument,\nand so on until the last argument has been\nconsumed, at which point it reduces to a\nresult of the type at the end of the chain. \n\nLet's look at an example involving the \n+ operator applied to two natural numbers.\nThe + operator is a shorthand for nat.add.\n-/\n\n#check nat.add 2 3\n#check (nat.add 2) 3 -- same thing\n#check nat.add 2\n#check nat.add\n\n/-\nFunction application is left-associative.\nHere, for example, nat.add consumes a 2 in\nthe first line and reduces to a function\n(one that \"bakes in the 2\") that consumes \nthe 3 and finally reduces to 5.\n-/\n\n/-\n#2\n\nLet's return to an example we saw on the\nrecent in-class no-contradiction exercise:\nno_contradiction: ∀ P : Prop, ¬ (P ∧ ¬ P). \n\nWe can now this type as a function type for\nfunctions that take a proposition, P, and that \nreduce to the proposition, ¬ (P ∧ ¬ P), which\nis to say, to (P ∧ ¬ P) → false. We thus have\na function type like this:\n\n(P : Prop) → (P ∧ ¬ P) → false.\n\nBut we can't write it like that because Lean\ndoesn't allow us to bind names to value in this\nway. We have to use ∀ instead.\n\n∀ P : Prop, (P ∧ ¬ P) → false.\n\nNevertheless, if we have a value of this type,\nit will be a function whose first argument is\nany proposition. Let's see this in action. \n-/\n\n-- Assume a proof/function of the given type\nvariable no_contra : ∀ (P : Prop), ¬ (P ∧ ¬ P)\n\n/-\nNow look at what we get when we apply this \nfunction to any proposition, P. We get back \na value of type (the proposition), P ∧ ¬ P.\n-/\n\n#check no_contra (0 = 0)\n#check no_contra ¬ P\n#check no_contra (P → Q)\n\n/-\nOf course each of the results is a value\nof type ¬ (P ∧ ¬ P), which is to say it's\nanother function: from proofs of (P ∧ ¬ P)\nto false. \n\nFor a little (logic-destroying) fun,let's \nassume we have a proof of 0 = 0 ∧ ¬ 0 = 0 \nand produce a proof of false. \n-/\n\n-- Assume a proof of 0 = 0 ∧ 0 ≠ 0,\nvariable inconsistency : 0 = 0 ∧ 0 ≠ 0\n\n--Apply the function, contra (0 = 0), to it\n#check (no_contra (0 = 0)) inconsistency\n\n/-\nVoila! A proof of false. Of course we\nnever could have constructed it without\nhaving made an absurd assumption.\n-/\n\n\n/-\n ** Conclusion **\n-/\n\n/-\nNow we know that (∀ p : P, Q) means P → Q, \nbut it also binds a name to an assumed value\nof type P that we can use in expressing Q.\nAn easy way to think about this is that a\nbinding of a name to a value of type P is\nexactly like the declaration of an argument\nto a function, and Q is like the body of \nthe function in which the name P can be\nused.\n-/\n", "meta": {"author": "kevinsullivan", "repo": "cs-dm", "sha": "bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c", "save_path": "github-repos/lean/kevinsullivan-cs-dm", "path": "github-repos/lean/kevinsullivan-cs-dm/cs-dm-bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c/06_Forall/00_intro.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9585377284730286, "lm_q2_score": 0.8933093996634686, "lm_q1q2_score": 0.8562707627770261}}
{"text": "import .lovelib\n\n\n/- # LoVe Homework 5: Inductive Predicates\n\nHomework must be done individually. -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/- ## Question 1 (3 points): A Type of λ-Terms\n\nRecall the following type of λ-terms from question 3 of exercise 4. -/\n\ninductive term : Type\n| var : string → term\n| lam : string → term → term\n| app : term → term → term\n\n/- 1.1 (1 point). Define an inductive predicate `is_app` that returns true if\nand only if its argument has an `app` constructor at the top level. -/\n\n-- enter your definition here\n\n/- 1.2 (2 points). Define an inductive predicate `is_abs_free` that is true if\nand only if its argument is a λ-term that contains no λ-expressions. -/\n\n-- enter your definition here\n\n\n/- ## Question 2 (6 points): Even and Odd\n\nConsider the following inductive definition of even numbers: -/\n\ninductive even : ℕ → Prop\n| zero : even 0\n| add_two {n : ℕ} : even n → even (n + 2)\n\n/- 2.1 (1 point). Define a similar predicate for odd numbers, by completing the\nLean definition below. The definition should distinguish two cases, like `even`,\nand should not rely on `even`. -/\n\ninductive odd : ℕ → Prop\n-- supply the missing cases here\n\n/- 2.2 (1 point). Give proof terms for the following propositions, based on\nyour answer to question 2.1. -/\n\nlemma odd_3 :\n odd 3 :=\nsorry\n\nlemma odd_5 :\n odd 5 :=\nsorry\n\n/- 2.3 (2 points). Prove the following lemma by rule induction, as a \"paper\"\nproof. This is a good exercise to develop a deeper understanding of how rule\ninduction works (and is good practice for the final exam).\n\n lemma even_odd {n : ℕ} (heven : even n) :\n odd (n + 1) :=\n\nGuidelines for paper proofs:\n\nWe expect detailed, rigorous, mathematical proofs. You are welcome to use\nstandard mathematical notation or Lean structured commands (e.g., `assume`,\n`have`, `show`, `calc`). You can also use tactical proofs (e.g., `intro`,\n`apply`), but then please indicate some of the intermediate goals, so that we\ncan follow the chain of reasoning.\n\nMajor proof steps, including applications of induction and invocation of the\ninduction hypothesis, must be stated explicitly. For each case of a proof by\ninduction, you must list the inductive hypotheses assumed (if any) and the goal\nto be proved. Minor proof steps corresponding to `refl`, `simp`, or `linarith`\nneed not be justified if you think they are obvious (to humans), but you should\nsay which key lemmas they depend on. You should be explicit whenever you use a\nfunction definition or an introduction rule for an inductive predicate. -/\n\n-- enter your paper proof here\n\n/- 2.4 (1 point). Prove the same lemma again, but this time in Lean: -/\n\nlemma even_odd {n : ℕ} (heven : even n) :\n odd (n + 1) :=\nsorry\n\n/- 2.5 (1 point). Prove the following lemma in Lean.\n\nHint: Recall that `¬ a` is defined as `a → false`. -/\n\nlemma even_not_odd {n : ℕ} (heven : even n) :\n ¬ odd n :=\nsorry\n\nend LoVe\n", "meta": {"author": "blanchette", "repo": "logical_verification_2020", "sha": "7a9f4bd73498189d9beb5d4591e0f2b3ca316111", "save_path": "github-repos/lean/blanchette-logical_verification_2020", "path": "github-repos/lean/blanchette-logical_verification_2020/logical_verification_2020-7a9f4bd73498189d9beb5d4591e0f2b3ca316111/lean/love05_inductive_predicates_homework_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9263037302939515, "lm_q2_score": 0.9241418283357702, "lm_q1q2_score": 0.8560360229080965}}
{"text": "import data.real.basic\n\n/-\nIn the previous file, we saw how to rewrite using equalities. \nThe analogue operation with mathematical statements is rewriting using\nequivalences. This is also done using the `rw` tactic.\nLean uses ↔ to denote equivalence instead of ⇔.\n\nIn the following exercises we will use the lemma:\n\n sub_nonneg {x y : ℝ} : 0 ≤ y - x ↔ x ≤ y\n\nThe curly braces around x and y instead of parentheses mean Lean will always try to figure out what \nx and y are from context, unless we really insist on telling it (we'll see how to insist much later). \nLet's not worry about that for now.\n\nIn order to announce an intermediate statement we use:\n\n have my_name : my statement,\n\nThis triggers the apparition of a new goal: proving the statement. After this is done,\nthe statement becomes available under the name `my_name`.\nWe can focus on the current goal by typing tactics between curly braces.\n-/\n\nexample {a b c : ℝ} (hab : a ≤ b) : c + a ≤ c + b :=\nbegin\n rw ←sub_nonneg,\n have key : (c + b) - (c + a) = b - a, -- Here we introduce an intermediate statement named key\n { ring, }, -- and prove it between curly braces\n rw key, -- we can now use the key statement\n rw sub_nonneg,\n exact hab,\nend\n\n/-\nOf course the previous lemma is already in the core library, named `add_le_add_left`, so we can use it below.\n\nLet's prove a variation (without invoking commutativity of addition since this would spoil our fun).\n-/\n\nexample {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c :=\nby linarith\n\n-- 0009\nexample {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c :=\nbegin\n rwa [←sub_nonneg, show (b + c) - (a + c) = b - a, by ring, sub_nonneg],\nend\n\n\n/-\nLet's see how we could use this lemma. It is already in the core library, under the name `add_le_add_right`:\n\n add_le_add_right {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c\n\nThis can be read as: \"add_le_add_right is a function that will take as input real numbers a and b, an\nassumption `hab` claiming a ≤ b and a real number c, and will output a proof of a + c ≤ b + c\". \n\nIn addition, recall that curly braces around a b mean Lean will figure out those arguments unless we\ninsist to help. This is because they can be deduced from the next argument `hab`.\nSo it will be sufficient to feed `hab` and c to this function.\n-/\n\nexample {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : by exact add_le_add_right ha b,\nend\n\n/-\nIn the second line of the above proof, we need to prove 0 + b ≤ a + b. \nThe proof after the colon says: this is exactly lemma `add_le_add_right` applied to ha and b.\nActually the `calc` block expects proof terms, and the `by` keyword is used to tell Lean we will use tactics\nto build such a proof term. But since the only tactic used in this block is `exact`, we can skip\ntactics entirely, and write:\n-/\n\nexample (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : add_le_add_right ha b,\nend\n\n/- Let's do a variant. -/\n\n-- 0010\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n calc\n a = a + 0 : by ring\n ... ≤ a + b : add_le_add_left hb a,\nend\n\n/-\nThe two preceding examples are in the core library :\n\n le_add_of_nonneg_left {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b\n le_add_of_nonneg_right {a b : ℝ} (hb : 0 ≤ b) : a ≤ a + b\n\nAgain, there won't be any need to memorize those names, we will\nsoon see how to get rid of such goals automatically. \nBut we can already try to understand how their names are built:\n\"le_add\" describe the conclusion \"less or equal than some addition\"\nIt comes first because we are focussed on proving stuff, and \nauto-completion works by looking at the beginning of words. \n\"of\" introduces assumptions. \"nonneg\" is Lean's abbreviation for non-negative.\n\"left\" or \"right\" disambiguates between the two variations.\n\nLet's use those lemmas by hand for now.\n-/\n\n-- 0011\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n calc\n 0 ≤ a : ha\n ... ≤ a + b : le_add_of_nonneg_right hb,\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0012\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d :=\nbegin\n calc\n a + c ≤ b + c : add_le_add_right hab c\n ... ≤ b + d : add_le_add_left hcd b,\nend\n\n/-\nIn the above examples, we prepared proofs of assumptions of our lemmas beforehand, so\nthat we could feed them to the lemmas. This is called forward reasonning. \nThe `calc` proofs also belong to this category.\n\nWe can also announce the use of a lemma, and provide proofs after the fact, using \nthe `apply` tactic. This is called backward reasonning because we get the conclusion \nfirst, and provide proofs later. Using `rw` on the goal (rather than on an assumption \nfrom the local context) is also backward reasonning.\n\nLet's do that using the lemma\n \n mul_nonneg' {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : 0 ≤ x*y\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n have key : b*c - a*c = (b - a)*c,\n { ring },\n rw key,\n apply mul_nonneg', -- Here we don't provide proofs for the lemma's assumptions\n -- Now we need to provide the proofs.\n { rw sub_nonneg,\n exact hab },\n { exact hc },\nend\n\n/-\nLet's prove the same statement using only forward reasonning: announcing stuff,\nproving it by working with known facts, moving forward.\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rw ← sub_nonneg at hab,\n exact hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg' hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rw h₂ at h₁,\n exact h₁, },\n rw sub_nonneg at h₃,\n exact h₃,\nend\n\n/-\nOne reason why the backward reasoning proof is shorter is because Lean can\ninfer of lot of things by comparing the goal and the lemma statement. Indeed\nin the `apply mul_nonneg'` line, we didn't need to tell Lean that x = b - a \nand y = c in the lemma. It was infered by \"unification\" between the lemma\nstatement and the goal.\n\nTo be fair to the forward reasoning version, we should introduce a convenient\nvariation on `rw`. The `rwa` tactic performs rewrite and then looks for an\nassumption matching the goal. We can use it to rewrite our latest proof as:\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rwa ← sub_nonneg at hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg' hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rwa h₂ at h₁, },\n rwa sub_nonneg at h₃,\nend\n\n/-\nLet's now combine forward and backward reasonning, to get our most \nefficient proof of this statement. Note in particular how unification is used\nto know what to prove inside the parentheses in the `mul_nonneg'` arguments.\n-/\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n calc\n 0 ≤ (b - a)*c : mul_nonneg' (by rwa sub_nonneg) hc\n ... = b*c - a*c : by ring,\nend\n\n/-\nLet's now practice all three styles using:\n\n mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b\n\n sub_nonpos {a b : α} : a - b ≤ 0 ↔ a ≤ b\n-/\n\n/- First using mostly backward reasonning -/\n-- 0013\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n rw ←sub_nonneg,\n rw (show a * c - b * c = (a - b) * c, by ring), \n apply mul_nonneg_of_nonpos_of_nonpos _ hc,\n rwa sub_nonpos,\nend\n\n/- Using forward reasonning -/\n-- 0014\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n have : a - b ≤ 0, by rwa sub_nonpos,\n have : 0 ≤ (a - b) * c, from mul_nonneg_of_nonpos_of_nonpos this hc,\n have : a * c - b * c = (a - b) * c, by ring,\n have : 0 ≤ a * c - b * c, by rwa this,\n show b * c ≤ a * c, by rwa ←sub_nonneg,\nend\n\n/-- Using a combination of both, with a `calc` block -/\n-- 0015\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n rw ←sub_nonneg,\n calc\n 0 ≤ (a - b) * c : mul_nonneg_of_nonpos_of_nonpos (by rwa sub_nonpos) hc\n ... = a * c - b * c : by ring,\nend\n\n/-\nLet's now move to proving implications. Lean denotes implications using\na simple arrow →, the same it uses for functions (say denoting the type of functions\nfrom ℕ to ℕ by ℕ → ℕ). This is because it sees a proof of P ⇒ Q as a function turning \na proof of P into a proof Q.\n\nMany of the examples that we already met are implications under the hood. For instance we proved\n\n le_add_of_nonneg_left (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b\n\nBut this can be rephrased as \n \n le_add_of_nonneg_left (a b : ℝ) : 0 ≤ a → b ≤ a + b\n\nIn order to prove P → Q, we use the tactic `intros`, followed by an assumption name.\nThis creates an assumption with that name asserting that P holds, and turns the goal into Q.\n\nLet's check we can go from our old version of l`e_add_of_nonneg_left` to the new one.\n\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nbegin\n intros ha,\n exact le_add_of_nonneg_left ha,\nend\n\n/-\nActually Lean doesn't make any difference between those two versions. It is also happy with\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nle_add_of_nonneg_left\n\n/- No tactic state is shown in the above line because we don't even need to enter\ntactic mode using `begin` or `by`. \n\nLet's practise using `intros`. -/\n\n-- 0016\nexample (a b : ℝ): 0 ≤ b → a ≤ a + b :=\nbegin\n intros hb,\n exact le_add_of_nonneg_right hb,\nend\n\n\n\n/-\nWhat about lemmas having more than one assumption? For instance:\n \n add_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b\n\nA natural idea is to use the conjunction operator (logical AND), which Lean denotes\nby ∧. Assumptions built using this operator can be decomposed using the `cases` tactic,\nwhich is a very general assumption-decomposing tactic.\n-/\nexample {a b : ℝ} : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b :=\nbegin\n intros hyp,\n cases hyp with ha hb,\n exact add_nonneg ha hb,\nend\n\n/-\nNeeding that intermediate line invoking `cases` shows this formulation is not what is used by\nLean. It rather sees `add_nonneg` as two nested implications: \nif a is non-negative then if b is non-negative then a+b is non-negative. \nIt reads funny, but it is much more convenient to use in practice.\n-/\nexample {a b : ℝ} : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) := \nadd_nonneg\n\n/-\nThe above pattern is so common that implications are defined as right-associative operators,\nhence parentheses are not needed above.\n\nLet's prove that the naive conjunction version implies the funny Lean version. For this we need\nto know how to prove a conjunction. The `split` tactic creates two goals from a conjunction goal.\nIt can also be used to create two implication goals from an equivalence goal.\n-/\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha,\n intros hb,\n apply H,\n split,\n exact ha,\n exact hb,\nend\n\n/-\nLet's practice `cases` and `split`. In the next exercise, P, Q and R denote \nunspecified mathematical statements.\n-/\n\n-- 0017\nexample (P Q R : Prop) : P ∧ Q → Q ∧ P :=\nbegin\n rintros ⟨p, q⟩,\n split; assumption,\nend\n\n/-\nOf course using `split` only to be able to use `exact` twice in a row feels silly. One can\nalso use the anonymous constructor syntax: ⟨ ⟩ \nBeware those are not parentheses but angle brackets. This is a generic way of providing \ncompound objects to Lean when Lean already has a very clear idea of what it is waiting for.\n\nSo we could have replaced the last three lines by: \n exact ⟨hQ, hP⟩\n\nWe can also combine the `intros` steps. We can now compress our earlier proof to:\n-/\n\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha hb,\n exact H ⟨ha, hb⟩,\nend\n\n/- \nThe anonymous contructor trick actually also works in `intros` provided we use\nits recursive version `rintros`. So we can replace\n intro h,\n cases h with h₁ h₂ \nby\n rintros ⟨h₁, h₂⟩,\nNow redo the previous exercise using all those compressing techniques, in exactly two lines. -/\n\n-- 0018\nexample (P Q R : Prop): P ∧ Q → Q ∧ P :=\nby { rintros ⟨p, q⟩, exact ⟨q, p⟩ }\n\n/-\nWe are ready to come back to the equivalence between the different formulations of\nlemmas having two assumptions. Remember the `split` tactic can be used to split\nan equivalence into two implications.\n-/\n\n-- 0019\nexample (P Q R : Prop) : (P ∧ Q → R) ↔ (P → (Q → R)) :=\nbegin\n split,\n { intros h p q, exact h ⟨p, q⟩ },\n { rintros h ⟨p, q⟩, exact h p q },\nend\n\n/-\nIf you used more than five lines in the above exercise then try to compress things\n(without simply removing line ends).\n\nOne last compression technique: given a proof h of a conjunction P ∧ Q, one can get\na proof of P using h.left and a proof of Q using h.right, without using cases.\nOne can also use the more generic (but less legible) names h.1 and h.2. \n\nSimilarly, given a proof h of P ↔ Q, one can get a proof of P → Q using h.mp \nand a proof of Q → P using h.mpr (or the generic h.1 and h.2 that are even less legible \nin this case).\n\nBefore the final exercise in this file, let's make sure we'll be able to leave \nwithout learning 10 lemma names. The `linarith` tactic will prove any equality or\ninequality or contradiction that follows by linear combinations of assumptions from the \ncontext (with constant coefficients).\n-/\n\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n linarith,\nend\n\n/-\nNow let's enjoy this for a while.\n-/\n\n-- 0020\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n linarith\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0021\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d := add_le_add hab hcd\n\n\n/-\nFinal exercise\n\nIn the last exercise of this file, we will use the divisibility relation on ℕ,\ndenoted by ∣ (beware this is a unicode divisibility bar, not the ASCII pipe character),\nand the gcd function.\n\nThe definitions are the usual ones, but our goal is to avoid using these definitions and\nonly use the following three lemmas:\n\n dvd_refl (a : ℕ) : a ∣ a \n\n dvd_antisym {a b : ℕ} : a ∣ b → b ∣ a → a = b :=\n \n dvd_gcd_iff {a b c : ℕ} : c ∣ gcd a b ↔ c ∣ a ∧ c ∣ b\n-/\n\n-- All functions and lemmas below are about natural numbers.\nopen nat\n\n-- 0022\nexample (a b : ℕ) : a ∣ b ↔ gcd a b = a :=\nbegin\n have : gcd a b ∣ a ∧ gcd a b ∣ b, by rw ←dvd_gcd_iff,\n split; intro h,\n { apply dvd_antisymm,\n show gcd a b ∣ a, from this.left,\n show a ∣ gcd a b, from dvd_gcd_iff.mpr ⟨by refl, h⟩, },\n { rw ←h, exact this.right },\nend", "meta": {"author": "pedrominicz", "repo": "learn", "sha": "b79b802a9846c86c21d4b6f3e17af36e7382f0ef", "save_path": "github-repos/lean/pedrominicz-learn", "path": "github-repos/lean/pedrominicz-learn/learn-b79b802a9846c86c21d4b6f3e17af36e7382f0ef/src/tutorials/02_iff_if_and.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9273632916317102, "lm_q2_score": 0.9230391590112403, "lm_q1q2_score": 0.8559926328056293}}
{"text": "/- Exercise 2.2: Functional Programming — Trees -/\n\n/- Question 1: Even and odd -/\n\n/- 1.1. Define an inductive predicate `even` on `ℕ` that is true for even numbers and false for odd\nnumbers.\n\nHint: Your predicate should have two introduction rules, one for the 0 case and one for the `n + 2`\ncase. -/\n\ninductive even : ℕ → Prop\n| zero : even 0\n| even_plus_two (n: ℕ) (h: even n): even(n + 2) \n\n\n/- 1.2. Prove that 0, 2, 4, and 6 are even. -/\n \nlemma iseven0: even 0 := by {apply even.zero}\nlemma iseven2: even 2 := by {apply even.even_plus_two; apply even.zero}\nlemma iseven4: even 4 := by {repeat{apply even.even_plus_two}; apply even.zero}\nlemma iseven6: even 6 := by {repeat{apply even.even_plus_two}; apply even.zero}\n\n\ndef odd (n : ℕ) : Prop :=\n ¬ even n\n\n/- 1.3. Prove that 1 is odd and register this fact as a `simp` rule.\n\nHint: `cases` is useful to reason about hypotheses of the form `even ...`. -/\n\nlemma isodd: odd 1 := by {intro h; cases h} \n\n/- 1.4. Prove that 3, 5, and 7 are odd. -/\n\nlemma isodd3 : odd 3 := by {intro s; cases s, cases s_h}\nlemma isodd5 : odd 5 := by {intro s; cases s, cases s_h, cases s_h_h}\nlemma isodd7 : odd 7 := by {intro s; cases s, cases s_h, cases s_h_h, cases s_h_h_h}\n\n/- 1.5. Complete the following proof by structural induction. -/\n\nlemma even_two_times :\n ∀m : ℕ, even (2 * m)\n| 0 := even.zero\n| (n + 1) := begin apply even.even_plus_two, apply even_two_times n end\n\n/- 1.6. Complete the following proof by rule induction.\n\nHint: You can use the `cases` tactic or `match ... with` to destruct an existential quantifier and\nextract the witness. -/\n\nlemma even_imp_exists_two_times :\n ∀n : ℕ, even n → ∃m, n = 2 * m\n| _ even.zero := ⟨0, by simp⟩\n| _ (even.add_two n h) :=\nsorry\n\n/- 1.7. Using `even_two_times` and `even_imp_exists_two_times`, prove the following equivalence. -/\n\nlemma even_iff_exists_two_times (n : ℕ) :\n even n ↔ ∃m, n = 2 * m :=\nsorry\n\n\n/- Question 2: Binary trees -/\n\nnamespace my_bin_tree\n\n/- Recall the binary trees from the lecture. As usual, we omit the proofs using `sorry`. -/\n\ninductive tree (α : Type) : Type\n| empty {} : tree\n| node (a : α) (l : tree) (r : tree) : tree\n\nexport tree (empty node)\n\ndef mirror {α : Type} : tree α → tree α\n| empty := empty\n| (node a l r) := node a (mirror r) (mirror l)\n\nlemma empty_mirror_iff {α : Type} : ∀t : tree α, mirror t = empty ↔ t = empty := sorry\nlemma mirror_mirror {α : Type} : ∀t : tree α, mirror (mirror t) = t := sorry\n\ninductive is_full {α : Type} : tree α → Prop\n| empty : is_full empty\n| node (a : α) (l r : tree α) (hl : is_full l) (hr : is_full r)\n (empty_iff : l = empty ↔ r = empty) :\n is_full (node a l r)\n\nlemma is_full_singleton {α : Type} (a : α) : is_full (node a empty empty) := sorry\nlemma is_full_mirror {α : Type} : ∀t : tree α, is_full t → is_full (mirror t) := sorry\n\n/- 2.1. State and prove the converse of `is_full_mirror`. -/\n\n-- enter your answer here\n\n/- 2.2. Define a function that counts the number of constructors (`empty` or `node`) in a tree. -/\n\ndef count {α : Type} : tree α → ℕ\n:= sorry\n\nend my_bin_tree\n\n\n/- Question 3: λ-terms -/\n\nnamespace my_lambda_term\n\n/- 3.1. Define an inductive type corresponding to the untyled λ-terms, as given by the following\ncontext-free grammar:\n\n<lam> ::= 'var' <string>\n | 'abs' <string> <lam>\n | 'app' <lam> <lam>\n-/\n\n-- enter your definition here\n\n/- 3.2. Define an inductive predicate `is_βnf` that determines whether a lambda term is in β-normal\nform (https://en.wikipedia.org/wiki/Beta_normal_form). -/\n\n-- enter your answer here\n\n/- 3.3. Register a textual representation of the type `lam`, as we did for Huffman trees in the\nlecture. -/\n\n-- enter your answer here\n\nend my_lambda_term\n", "meta": {"author": "KjellZijlemaker", "repo": "Logical_Verification_VU", "sha": "4578b93bf1615466996157bb333c84122b201d99", "save_path": "github-repos/lean/KjellZijlemaker-Logical_Verification_VU", "path": "github-repos/lean/KjellZijlemaker-Logical_Verification_VU/Logical_Verification_VU-4578b93bf1615466996157bb333c84122b201d99/Exercises week 5/22_exercise_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9597620591027811, "lm_q2_score": 0.891811041124754, "lm_q1q2_score": 0.8559264011604889}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio 1. Importar la librería de los números reales.\n-- ----------------------------------------------------------------------\n\nimport data.real.basic\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 2. Enunciar el lema ej: \"para todos los números reales x,\n-- y, ε si\n-- 0 < ε\n-- ε ≤ 1\n-- abs x < ε\n-- abs y < ε\n-- entonces\n-- abs (x * y) < ε\n-- ----------------------------------------------------------------------\n\n-- lemma ej :\n-- ∀ x y ε : ℝ,\n-- 0 < ε →\n-- ε ≤ 1 →\n-- abs x < ε →\n-- abs y < ε →\n-- abs (x * y) < ε :=\n-- sorry\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 3. Crear una sección con las siguientes declaraciones\n-- a b δ : ℝ\n-- h₀ : 0 < δ\n-- h₁ : δ ≤ 1\n-- ha : abs a < δ\n-- hb : abs b < δ\n-- y calcular el tipo de las siguientes expresiones\n-- ej a b δ\n-- ej a b δ h₀ h₁\n-- ej a b δ h₀ h₁ ha hb\n-- ----------------------------------------------------------------------\n\nsection\n\nvariables a b δ : ℝ\nvariables (h₀ : 0 < δ) (h₁ : δ ≤ 1)\nvariables (ha : abs a < δ) (hb : abs b < δ)\n\n-- #check ej a b δ\n-- #check ej a b δ h₀ h₁\n-- #check ej a b δ h₀ h₁ ha hb\n\n-- Comentario: Al colocar el cursor sobre check se obtiene\n-- ej a b δ : 0 < δ → δ ≤ 1 → abs a < δ → abs b < δ → abs (a * b) < δ\n-- ej a b δ h₀ h₁ : abs a < δ → abs b < δ → abs (a * b) < δ\n-- ej a b δ h₀ h₁ ha hb : abs (a * b) < δ\n\nend\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Logica/Lema_con_implicaciones_y_cuantificador_universal.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9504109798251322, "lm_q2_score": 0.9005297907896396, "lm_q1q2_score": 0.8558734008261026}}
{"text": "import data.real.basic\n\n/-\nIn the previous file, we saw how to rewrite using equalities. \nThe analogue operation with mathematical statements is rewriting using\nequivalences. This is also done using the `rw` tactic.\nLean uses ↔ to denote equivalence instead of ⇔.\n\nIn the following exercises we will use the lemma:\n\n sub_nonneg {x y : ℝ} : 0 ≤ y - x ↔ x ≤ y\n\nThe curly braces around x and y instead of parentheses mean Lean will always try to figure out what \nx and y are from context, unless we really insist on telling it (we'll see how to insist much later). \nLet's not worry about that for now.\n\nIn order to announce an intermediate statement we use:\n\n have my_name : my statement,\n\nThis triggers the apparition of a new goal: proving the statement. After this is done,\nthe statement becomes available under the name `my_name`.\nWe can focus on the current goal by typing tactics between curly braces.\n-/\n\nexample {a b c : ℝ} (hab : a ≤ b) : c + a ≤ c + b :=\nbegin\n rw ← sub_nonneg,\n have key : (c + b) - (c + a) = b - a, -- Here we introduce an intermediate statement named key\n { ring, }, -- and prove it between curly braces\n rw key, -- we can now use the key statement\n rw sub_nonneg,\n exact hab,\nend\n\n/-\nOf course the previous lemma is already in the core library, named `add_le_add_left`, so we can use it below.\n\nLet's prove a variation (without invoking commutativity of addition since this would spoil our fun).\n-/\n\n-- 0009\nexample {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c :=\nbegin\n -- sorry\n have key : (b + c) - (a + c) = b - a,\n { ring },\n rw ← sub_nonneg,\n rw key,\n rw sub_nonneg,\n exact hab,\n -- sorry\nend\n\n\n/-\nLet's see how we could use this lemma. It is already in the core library, under the name `add_le_add_right`:\n\n add_le_add_right {a b : ℝ} (hab : a ≤ b) (c : ℝ) : a + c ≤ b + c\n\nThis can be read as: \"add_le_add_right is a function that will take as input real numbers a and b, an\nassumption `hab` claiming a ≤ b and a real number c, and will output a proof of a + c ≤ b + c\". \n\nIn addition, recall that curly braces around a b mean Lean will figure out those arguments unless we\ninsist to help. This is because they can be deduced from the next argument `hab`.\nSo it will be sufficient to feed `hab` and c to this function.\n-/\n\nexample {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : by exact add_le_add_right ha b,\nend\n\n/-\nIn the second line of the above proof, we need to prove 0 + b ≤ a + b. \nThe proof after the colon says: this is exactly lemma `add_le_add_right` applied to ha and b.\nActually the `calc` block expects proof terms, and the `by` keyword is used to tell Lean we will use tactics\nto build such a proof term. But since the only tactic used in this block is `exact`, we can skip\ntactics entirely, and write:\n-/\n\nexample (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b :=\nbegin\n calc b = 0 + b : by ring\n ... ≤ a + b : add_le_add_right ha b,\nend\n\n/- Let's do a variant. -/\n\n-- 0010\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n -- sorry\n calc a = a + 0 : by ring\n ... ≤ a + b : add_le_add_left hb a,\n -- sorry\nend\n\n/-\nThe two preceding examples are in the core library :\n\n le_add_of_nonneg_left {a b : ℝ} (ha : 0 ≤ a) : b ≤ a + b\n le_add_of_nonneg_right {a b : ℝ} (hb : 0 ≤ b) : a ≤ a + b\n\nAgain, there won't be any need to memorize those names, we will\nsoon see how to get rid of such goals automatically. \nBut we can already try to understand how their names are built:\n\"le_add\" describe the conclusion \"less or equal than some addition\"\nIt comes first because we are focussed on proving stuff, and \nauto-completion works by looking at the beginning of words. \n\"of\" introduces assumptions. \"nonneg\" is Lean's abbreviation for non-negative.\n\"left\" or \"right\" disambiguates between the two variations.\n\nLet's use those lemmas by hand for now.\n\nNote that you can have several inequalities steps in a `calc` block,\ntransitivity of inequalities will be used automatically to assemble\nthe pieces.\n-/\n\n-- 0011\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n -- sorry\n calc 0 ≤ a : ha\n ... ≤ a + b : le_add_of_nonneg_right hb,\n -- sorry\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0012\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d :=\nbegin\n -- sorry\n calc\n a + c ≤ b + c : add_le_add_right hab c\n ... ≤ b + d : add_le_add_left hcd b,\n -- sorry\nend\n\n/-\nIn the above examples, we prepared proofs of assumptions of our lemmas beforehand, so\nthat we could feed them to the lemmas. This is called forward reasoning. \nThe `calc` proofs also belong to this category.\n\nWe can also announce the use of a lemma, and provide proofs after the fact, using \nthe `apply` tactic. This is called backward reasoning because we get the conclusion \nfirst, and provide proofs later. Using `rw` on the goal (rather than on an assumption \nfrom the local context) is also backward reasoning.\n\nLet's do that using the lemma\n \n mul_nonneg {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : 0 ≤ x*y\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n have key : b*c - a*c = (b - a)*c,\n { ring },\n rw key,\n apply mul_nonneg, -- Here we don't provide proofs for the lemma's assumptions\n -- Now we need to provide the proofs.\n { rw sub_nonneg,\n exact hab },\n { exact hc },\nend\n\n/-\nLet's prove the same statement using only forward reasoning: announcing stuff,\nproving it by working with known facts, moving forward.\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rw ← sub_nonneg at hab,\n exact hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rw h₂ at h₁,\n exact h₁, },\n rw sub_nonneg at h₃,\n exact h₃,\nend\n\n/-\nOne reason why the backward reasoning proof is shorter is because Lean can\ninfer of lot of things by comparing the goal and the lemma statement. Indeed\nin the `apply mul_nonneg` line, we didn't need to tell Lean that x = b - a \nand y = c in the lemma. It was infered by \"unification\" between the lemma\nstatement and the goal.\n\nTo be fair to the forward reasoning version, we should introduce a convenient\nvariation on `rw`. The `rwa` tactic performs rewrite and then looks for an\nassumption matching the goal. We can use it to rewrite our latest proof as:\n-/\n\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n have hab' : 0 ≤ b - a,\n { rwa ← sub_nonneg at hab, },\n have h₁ : 0 ≤ (b - a)*c,\n { exact mul_nonneg hab' hc },\n have h₂ : (b - a)*c = b*c - a*c,\n { ring, },\n have h₃ : 0 ≤ b*c - a*c,\n { rwa h₂ at h₁, },\n rwa sub_nonneg at h₃,\nend\n\n/-\nLet's now combine forward and backward reasoning, to get our most \nefficient proof of this statement. Note in particular how unification is used\nto know what to prove inside the parentheses in the `mul_nonneg` arguments.\n-/\nexample (a b c : ℝ) (hc : 0 ≤ c) (hab : a ≤ b) : a*c ≤ b*c :=\nbegin\n rw ← sub_nonneg,\n calc 0 ≤ (b - a)*c : mul_nonneg (by rwa sub_nonneg) hc\n ... = b*c - a*c : by ring,\nend\n\n/-\nLet's now practice all three styles using:\n\n mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b\n\n sub_nonpos {a b : α} : a - b ≤ 0 ↔ a ≤ b\n-/\n\n/- First using mostly backward reasoning -/\n-- 0013\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n -- sorry\n rw ← sub_nonneg,\n have fact : a*c - b*c = (a - b)*c,\n ring,\n rw fact,\n apply mul_nonneg_of_nonpos_of_nonpos,\n { rwa sub_nonpos },\n { exact hc },\n -- sorry\nend\n\n/- Using forward reasoning -/\n-- 0014\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n -- sorry\n have hab' : a - b ≤ 0,\n { rwa ← sub_nonpos at hab, },\n have h₁ : 0 ≤ (a - b)*c,\n { exact mul_nonneg_of_nonpos_of_nonpos hab' hc },\n have h₂ : (a - b)*c = a*c - b*c,\n { ring, },\n have h₃ : 0 ≤ a*c - b*c,\n { rwa h₂ at h₁, },\n rwa sub_nonneg at h₃,\n -- sorry\nend\n\n/-- Using a combination of both, with a `calc` block -/\n-- 0015\nexample (a b c : ℝ) (hc : c ≤ 0) (hab : a ≤ b) : b*c ≤ a*c :=\nbegin\n -- sorry\n have hab' : a - b ≤ 0,\n { rwa sub_nonpos },\n rw ← sub_nonneg,\n calc 0 ≤ (a - b)*c : mul_nonneg_of_nonpos_of_nonpos hab' hc\n ... = a*c - b*c : by ring,\n -- sorry\nend\n\n/-\nLet's now move to proving implications. Lean denotes implications using\na simple arrow →, the same it uses for functions (say denoting the type of functions\nfrom ℕ to ℕ by ℕ → ℕ). This is because it sees a proof of P ⇒ Q as a function turning \na proof of P into a proof Q.\n\nMany of the examples that we already met are implications under the hood. For instance we proved\n\n le_add_of_nonneg_left (a b : ℝ) (ha : 0 ≤ a) : b ≤ a + b\n\nBut this can be rephrased as \n \n le_add_of_nonneg_left (a b : ℝ) : 0 ≤ a → b ≤ a + b\n\nIn order to prove P → Q, we use the tactic `intros`, followed by an assumption name.\nThis creates an assumption with that name asserting that P holds, and turns the goal into Q.\n\nLet's check we can go from our old version of `le_add_of_nonneg_left` to the new one.\n\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nbegin\n intros ha,\n exact le_add_of_nonneg_left ha,\nend\n\n/-\nActually Lean doesn't make any difference between those two versions. It is also happy with\n-/\nexample (a b : ℝ): 0 ≤ a → b ≤ a + b :=\nle_add_of_nonneg_left\n\n/- No tactic state is shown in the above line because we don't even need to enter\ntactic mode using `begin` or `by`. \n\nLet's practise using `intros`. -/\n\n-- 0016\nexample (a b : ℝ): 0 ≤ b → a ≤ a + b :=\nbegin\n -- sorry\n intros hb,\n calc a = a + 0 : by ring\n ... ≤ a + b : add_le_add_left hb a,\n -- sorry\nend\n\n\n\n/-\nWhat about lemmas having more than one assumption? For instance:\n \n add_nonneg {a b : ℝ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b\n\nA natural idea is to use the conjunction operator (logical AND), which Lean denotes\nby ∧. Assumptions built using this operator can be decomposed using the `cases` tactic,\nwhich is a very general assumption-decomposing tactic.\n-/\nexample {a b : ℝ} : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b :=\nbegin\n intros hyp,\n cases hyp with ha hb,\n exact add_nonneg ha hb,\nend\n\n/-\nNeeding that intermediate line invoking `cases` shows this formulation is not what is used by\nLean. It rather sees `add_nonneg` as two nested implications: \nif a is non-negative then if b is non-negative then a+b is non-negative. \nIt reads funny, but it is much more convenient to use in practice.\n-/\nexample {a b : ℝ} : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) := \nadd_nonneg\n\n/-\nThe above pattern is so common that implications are defined as right-associative operators,\nhence parentheses are not needed above.\n\nLet's prove that the naive conjunction version implies the funny Lean version. For this we need\nto know how to prove a conjunction. The `split` tactic creates two goals from a conjunction goal.\nIt can also be used to create two implication goals from an equivalence goal.\n-/\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha,\n intros hb,\n apply H,\n split,\n exact ha,\n exact hb,\nend\n\n/-\nLet's practice `cases` and `split`. In the next exercise, P, Q and R denote \nunspecified mathematical statements.\n-/\n\n-- 0017\nexample (P Q R : Prop) : P ∧ Q → Q ∧ P :=\nbegin\n -- sorry\n intro hyp,\n cases hyp with hP hQ,\n split,\n exact hQ,\n exact hP,\n -- sorry\nend\n\n/-\nOf course using `split` only to be able to use `exact` twice in a row feels silly. One can\nalso use the anonymous constructor syntax: ⟨ ⟩ \nBeware those are not parentheses but angle brackets. This is a generic way of providing \ncompound objects to Lean when Lean already has a very clear idea of what it is waiting for.\n\nSo we could have replaced the last three lines by: \n exact ⟨hQ, hP⟩\n\nWe can also combine the `intros` steps. We can now compress our earlier proof to:\n-/\n\nexample {a b : ℝ} (H : (0 ≤ a ∧ 0 ≤ b) → 0 ≤ a + b) : 0 ≤ a → (0 ≤ b → 0 ≤ a + b) :=\nbegin\n intros ha hb,\n exact H ⟨ha, hb⟩,\nend\n\n/- \nThe anonymous contructor trick actually also works in `intros` provided we use\nits recursive version `rintros`. So we can replace\n intro h,\n cases h with h₁ h₂ \nby\n rintros ⟨h₁, h₂⟩,\nNow redo the previous exercise using all those compressing techniques, in exactly two lines. -/\n\n-- 0018\nexample (P Q R : Prop): P ∧ Q → Q ∧ P :=\nbegin\n -- sorry\n rintros ⟨hP, hQ⟩,\n exact ⟨hQ, hP⟩,\n -- sorry\nend\n\n/-\nWe are ready to come back to the equivalence between the different formulations of\nlemmas having two assumptions. Remember the `split` tactic can be used to split\nan equivalence into two implications.\n-/\n\n-- 0019\nexample (P Q R : Prop) : (P ∧ Q → R) ↔ (P → (Q → R)) :=\nbegin\n -- sorry\n split,\n { intros hyp hP hQ,\n exact hyp ⟨hP, hQ⟩ },\n { rintro hyp ⟨hP, hQ⟩,\n exact hyp hP hQ },\n -- sorry\nend\n\n/-\nIf you used more than five lines in the above exercise then try to compress things\n(without simply removing line ends).\n\nOne last compression technique: given a proof h of a conjunction P ∧ Q, one can get\na proof of P using h.left and a proof of Q using h.right, without using cases.\nOne can also use the more generic (but less legible) names h.1 and h.2. \n\nSimilarly, given a proof h of P ↔ Q, one can get a proof of P → Q using h.mp \nand a proof of Q → P using h.mpr (or the generic h.1 and h.2 that are even less legible \nin this case).\n\nBefore the final exercise in this file, let's make sure we'll be able to leave \nwithout learning 10 lemma names. The `linarith` tactic will prove any equality or\ninequality or contradiction that follows by linear combinations of assumptions from the \ncontext (with constant coefficients).\n-/\n\nexample (a b : ℝ) (hb : 0 ≤ b) : a ≤ a + b :=\nbegin\n linarith,\nend\n\n/-\nNow let's enjoy this for a while.\n-/\n\n-- 0020\nexample (a b : ℝ) (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a + b :=\nbegin\n -- sorry\n linarith,\n -- sorry\nend\n\n/- And let's combine with our earlier lemmas. -/\n\n-- 0021\nexample (a b c d : ℝ) (hab : a ≤ b) (hcd : c ≤ d) : a + c ≤ b + d :=\nbegin\n -- sorry\n linarith,\n -- sorry\nend\n\n\n/-\nFinal exercise\n\nIn the last exercise of this file, we will use the divisibility relation on ℕ,\ndenoted by ∣ (beware this is a unicode divisibility bar, not the ASCII pipe character),\nand the gcd function.\n\nThe definitions are the usual ones, but our goal is to avoid using these definitions and\nonly use the following three lemmas:\n\n dvd_refl (a : ℕ) : a ∣ a \n\n dvd_antisymm {a b : ℕ} : a ∣ b → b ∣ a → a = b :=\n \n dvd_gcd_iff {a b c : ℕ} : c ∣ gcd a b ↔ c ∣ a ∧ c ∣ b\n-/\n\n-- All functions and lemmas below are about natural numbers.\nopen nat\n\n-- 0022\nexample (a b : ℕ) : a ∣ b ↔ gcd a b = a :=\nbegin\n -- sorry\n have fact : gcd a b ∣ a ∧ gcd a b ∣ b,\n { rw ← dvd_gcd_iff },\n split,\n { intro h,\n apply dvd_antisymm fact.left,\n rw dvd_gcd_iff,\n exact ⟨dvd_refl a, h⟩ },\n { intro h,\n rw ← h,\n exact fact.right },\n -- sorry\nend\n", "meta": {"author": "leanprover-community", "repo": "tutorials", "sha": "79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1", "save_path": "github-repos/lean/leanprover-community-tutorials", "path": "github-repos/lean/leanprover-community-tutorials/tutorials-79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1/src/solutions/02_iff_if_and.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9496693674025231, "lm_q2_score": 0.9005297887874625, "lm_q1q2_score": 0.8552055548449172}}
{"text": "/-\nGiven any two propositions, P and Q, we can form\nthe proposition, P → Q. That is the syntax of an\nimplications. \n\nIf P and Q are propositions, we read P → Q as \nP implies Q. A proof of a proposition, P → Q, \nis a program that that converts any proof of P \ninto a proof of Q. The type of such a program \nis P → Q.\n-/\n\n/- Elimination Rule for Implication -/\n\n/-\nFrom two premises, (1) that \"if it's raining then\nthe streets are wet,\" and (2) \"it's raining,\"\" we \ncan surely derive as a conclusion that the streets\nare wet. Combining \"raining → streets wet\" with \n\"raining\" reduces to \"streets wet.\" This is modus\nponens.\n\nLet's abbreviate the proposition \"it's raining\", \nas R, and let's also abbreviate the proposition, \n\"the streets are wet as W\". We will then abbreviate\nthe proposition \"if it's raining then the streets \nare wet\" as R → W. \n\nSuch a proposition is in the form of what we call \nan implication. It can be pronounced as \"R implies \nW\", or simply \"if R is true then W must be true.\" \nNote that by this latter reading, in a case where \nR is not true, the implication says nothing about \nwhether W must be true or not. We will thus judge\nan implication to be true if either R is false (in\nwhich case the implications does not constrain W\nto be any value), or if whenever R is true, W is,\ntoo. The one situation under which we will not be\nable to judge R → W to be true is if it can be\nthe case that R is true and yet W is not, as that\nwould contradict the meaning of an implication. \n\nWith these abbreviations in hand, we can write \nan informal inference rule to capture the idea\nwe started with. If we know that a proposition,\nR → W, is true, and we know that the proposition,\nR, is true, then we can deduce that W therefore \nmust also be true. We can write this inference\nrule informally like this: \n\n R → W, R\n -------- (→ elim)\n W\n\nThis is the arrow (→) elimination rule?\n\nIn the rest of this chapter, we will formalize\nthis notion of inference by first presenting the\nelimination rule for implication. We will see \nthat this rule not only formalizes Aristotle's \nmodus ponens rule of reasoning (it is one of\nhis fundamental \"syllogisms\"), but it also\ncorresponds to function application! \n\nEXERCISE: When you apply a function that takes \nan argument of type R and returns a value of \ntype W to a value of type R, what do you get? \n-/\n\n/-\nNow let's specify that R and W are arbitrary\npropositions in the type theory of Lean. And\nrecall that to judge R → W to be true or to\njudge either R or W to be true means that we\nhave proofs of these propositions. We can now\ngive a precise rule in type theory capturing\nAristotle's modus ponens: what it means to be\na function, and how function application works.\n\n{ R W : Prop }, pfRtoW : R → W, pfR : R\n--------------------------------------- (→-elim)\n pfW: W \n\n\nHere it is formalized as a function.\n-/\n\ndef \narrow_elim \n { R W: Prop } (pfRtopfW : R → W) (pfR : R) : W := \n pfRtopfW pfR\n\n/-\nThis program expresses the inference rule. The\nname of the rule (a program) is arrow_elim. The\nfunction takes (1) two propositions, R and W;\n(2) a proof of R → W (itself a program that \nconverts and proof of R into a proof of W;\n(3) a proof of R. If promises that if it is\ngiven any values of these types, it will \nreturn a proof of(a value of type) W. Given\nvalues for its arguments it derives a proof \nof W by applying that given function to that\ngiven value. The result will be a proof of (a \nvalue of type) W. \n\nWe thus now have another way to pronounce this\ninference rule: \"if you have a function that can\nturn any proof of R into a proof of W, and if you \nhave a proof of R, then you obtain a proof of W,\nand you do it in particular by applying the \nfunction to that value.\"\n-/\n\n/- In yet other words, if you've got a function\nand you've got an argument value, then you can \neliminate the function (the \"arrow\") by applying \nthe function to that value, yielding a value of\nthe return type. \n-/\n\n/-\nA concrete example of a program that serves as\na proof of W → R is found in our program, from \nthe 03_Conjunction chapter that turns any proof\nof P ∧ Q (W) into a proof Q ∧ P (R). \n\nWe wrote that code so that for any propositions, \nP and Q, for any proof of P ∧ Q, it returns a \nproof of Q ∧ P. It can always do this because \nfrom any proof of P ∧ Q, it can obtain separate \nproofs of P and Q that it can then re-assembled\ninto a proof of Q ∧ P. That function is a proof\nof this type: ∀ P Q: Prop, P ∧ Q → Q ∧ P. That \nsays, for any propositions, P and Q, a function\nof this type turns a proof of P ∧ Q into a proof\nof Q ∧ P. It thus proves P ∧ Q → Q ∧ P.\n-/\n\n\n/-\nWe want to give an example of the use of the\narrow-elim rule. In this example we use a new\n(for us) capability of Lean: you can define\nvariables to be give given types without proofs,\ni.e., as axioms. \n\nHere (read the code) we assume that P and Q\nare arbitrary propositions. We do not say\nwhat specific propositions they are. Next\nwe assume that we have a proof that P → Q,\nwhich will be represented as a program\nthat takes proofs of Ps and returns proofs\nof Qs. Third we assume that we have some\nproof of P. And finally we check to see\nthat the result of applying impl to pfP is\nof type Q.\n-/\n\nvariables P Q : Prop\nvariable impl : P → Q\nvariable pfP : P\n#check (impl pfP)\n#check (arrow_elim impl pfP)\n\n\n/-\nIn Lean, a proof of R → W is given as a \nprogram: a \"recipe\" for making a proof of W \nout of a proof of R. With such a program in \nhand, if we apply it to a proof of R it will\nderive a proof of W. \n-/\n\n\n\n\n/-\nEXAMPLE: implications involving true and false\n-/\n\n/-\nAnother way to read P → Q is \"if P (is true)\nthen Q (is true).\"\n \nWe now ask which of the following implications\ncan be proved?\n\ntrue → true -- if true (is true) then true (is true)\ntrue → false -- if true (is true) then false (is true)\nfalse → true -- if false (is true) then true (is true)\nfalse → false -- if false (is true) then false (is true)\n\nEXERCISE: What is your intuition?\n\nHint: Remember the unit on true and false. Think about \nthe false elimination rule. Recall how many proofs there\nare of the proposition, false.\n-/\n\n\n/- true → true -/\n\n /-\n Let's see one of the simplest of all possible\n examples to make these abstract ideas concrete. \n Consider the proposition, true → true. We can\n read this as \"true implies true\". But for our\n purposes, a better way to say it is, \"if you \n assume you are given a proof of true, then you \n can construct and return a proof of true.\" \n \nWe can also see this proposition as the type of\nany program that turns a proof of true into a\nproof of true. That's going to be easy! Here it\nis: a simple function definition in Lean. We call\nthe program timpt (for \"true implies true\"). It\ntakes an argument, pf_true, of type true, i.e., \na proof of true, as an argument, and it builds\nand returns a proof of true by just returning \nthe proof it was given! This function is thus\njust the identity function of type true → true.\n-/\n\ndef timpt ( pf_true: true ) : true := pf_true\n\ntheorem timpt_theorem : true → true := timpt\n\n#check timpt\n\n/-\nIf this program is given a proof, pf, of true,\nit can and does return a proof of true. Let's\nquickly verify that by looking at the value we\nget when we apply the function (implication) to\ntrue.intro, which is the always-available proof\nof true. \n-/\n\n#reduce (timpt true.intro)\n\n/-\nIndeed, this program is in effect a proof\nof true → true because if it's given any\nproof of true (there's only one), it then\nreturns a proof of true.\n\nNow we can see explicitly that this program \nis a proof of the proposition, true → true,\nby formalizing the proposition, true → true,\nand giving the program as a proof!\n-/\n\ntheorem true_imp_true : true → true := timpt\n\n/-\nAnd the type of the program, which we are\nnow interpreting as a proof of true → true,\nis thus true → true. The program is a value\nof the proposition, and type, true → true. \n-/\n\n#check timpt\n\n\n\n\n\n/- true → false -/\n\n/-\nEXERCISE: Can you prove true → false? If\nso, state and prove the theorem. If not,\nexplain exactly why you think you can't\ndo it. \n-/\n\n-- def timpf (pf_true : true) : false := _\n\n-- theorem timpf_theorem: true → false := _\n\n\n/- false → true -/\n\n/-\nEXERCISE: Prove false → true. The key to\ndoing this is to remember that applying \nfalse.elim (think of it as a function) to\na proof of false proves anything at all.\n-/\n\ndef fimpt (f: false) : true := true.intro\n\ntheorem fimpt_theorem : false → true := fimpt\n\n\n/- false → false -/\n\n/-\nEXERCISE: Is it true that false → false? \nProve it. Hint: We wrote a program that\nturned out to be a proof of true → true.\nIf you can write such a program, call it \nfimpf, then use it to prove the theorem,\nfalse_imp_false: false → false.\n-/\n\ndef fimpf (f: false) : false := f\n\ntheorem fimpf_theorem : false → false := fimpf\n\ndef fimpzeqo (f: false) : 0 = 1 := false.elim f\n\ntheorem fizeo : false → 0 = 1 := fimpzeqo\n\n\n\n\n/-\nWe summarize our findings in the following\ntable for implication.\n\ntrue → true : proof, true\ntrue → false : <no proof>\nfalse → true : proof, true\nfalse → false : proof, true\n\nWhat's deeply interesting here is that \nwe're not just given these judgments as \nunexplained pronouncements. We've *proved*\nthree of these judgments. The fourth we\ncould not prove, and we made an argument \nthat it can't be proved, but we haven't\nyet formally proved, nor do even have a\nway to say yet, that the proposition is\nfalse. The best we can say at this time\nis that we don't have a proof.\n-/\n\n\n#check true_imp_true -- (proof of) implication\n#check true.intro -- (proof of) premise\n#check (true_imp_true true.intro) -- conclusion!\n\n\n\n/- *** → INTRODUCTION RULE -/\n\n\n/-\nThe → introduction rules say that if \nassuming that there is proof of P allows\nyou to derive a proof of Q, then one can\nderive a proof of P → Q, discharging the \nassumption. \n\nTo represent this rule as an inference\nrule, we need a notation to represent \nthe idea that from an assumption that\nthere is a proof of P one can derive a\nproof of Q. The notation used in most\nlogic books represents this notion as a\nvertical dotted line from a P above to\na Q below. If one has such a derivation\nthen one can conclude P → Q. The idea\nis that the derivation is in essence a\nprogram; the program is the proof; and\nit is a proof of the proposition, which\nis to say, of the type, P → Q. \n\n\n P\n |\n |\n Q\n -----\n P → Q\n\n\nThe proof of a proposition, P → Q, in\nLean, is thus a program that takes an \nargument of type P and returns a result \nof type Q.\n-/\n\n/- ** Direct Proofs: Proof Strategy -/\n\n/-\nMany of the propositions you need to\nprove in practice are implications, of\nthe form P → Q. It's a way of saying,\n\"under conditions defined by P it must\nbe the case that Q is also true. A \ndirect proof is just what we have\nseen: a way to derive a proof of Q \nfrom an assumed proof of P. \n\nTo prove P → Q, you thus start with an\nassumption that there's a proof of P, \nand from that you deduce a proof of Q. \n-/\n\n/-\nEXAMPLE: Give a direct proof of eqsym:\na = b → b = a. Give it as both a lambda \nexpression and as an assume-show-from \ntactic script.\n-/\n\nlemma eqsym : \n ∀ a b : nat, a = b → b = a \n := sorry\n\nlemma eqsym' : \n ∀ a b : nat, a = b → b = a \n := begin\n sorry\n end\n\n/-\nhttp://zimmer.csufresno.edu/~larryc/proofs/proofs.direct.html\n-/", "meta": {"author": "kevinsullivan", "repo": "cs-dm", "sha": "bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c", "save_path": "github-repos/lean/kevinsullivan-cs-dm", "path": "github-repos/lean/kevinsullivan-cs-dm/cs-dm-bfd2f5fd2612472e15bd970c7870b5d0dd73bd1c/04_Implication/00_intro.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9372107949104866, "lm_q2_score": 0.9124361664296878, "lm_q1q2_score": 0.8551450248446448}}
{"text": "import data.real.basic\n\n/-\nOne of the earliest kind of proofs one encounters while learning mathematics is proving by\na calculation. It may not sound like a proof, but this is actually using lemmas expressing \nproperties of operations on numbers. It also uses the fundamental property of equality: if two\nmathematical objects A and B are equal then, in any statement involving A, one can replace A \nby B. This operation is called rewriting, and the Lean \"tactic\" for this is `rw`.\n\nIn the following exercises, we will use the following two lemmas:\n mul_assoc a b c : a * b * c = a * (b * c)\n mul_comm a b : a*b = b*a\n\nHence the command \n rw mul_assoc a b c,\nwill replace a*b*c by a*(b*c) in the current goal.\n\nIn order to replace backward, we use\n rw ← mul_assoc a b c,\nreplacing a*(b*c) by a*b*c in the current goal.\n\nOf course we don't want to constantly invoke those lemmas, and we will eventually introduce\nmore powerful solutions.\n-/\n\n-- Uncomment the following line if you want to see parentheses around subexpressions.\n-- set_option pp.parens true\n\nexample (a b c : ℝ) : (a * b) * c = b * (a * c) :=\nbegin\n rw mul_comm a b,\n rw mul_assoc b a c,\nend\n\n-- 0001\nexample (a b c : ℝ) : (c * b) * a = b * (a * c) :=\nbegin\n rw mul_comm c b,\n rw mul_assoc b c a,\n rw mul_comm c a,\nend\n\n-- 0002\nexample (a b c : ℝ) : a * (b * c) = b * (a * c) :=\nbegin\n rw ← mul_assoc a b c,\n rw mul_comm a b,\n rw mul_assoc,\nend\n\n/-\nNow let's return to the preceding example to experiment with what happens\nif we don't give arguments to mul_assoc or mul_comm.\nFor instance, you can start the next proof with\n rw ← mul_assoc,\nTry to figure out what happens.\n-/\n\n-- 0003\nexample (a b c : ℝ) : a * (b * c) = b * (a * c) :=\nbegin\n rw ← mul_assoc,\n rw mul_comm a b,\n rw mul_assoc,\nend\n\n/-\nWe can also perform rewriting in an assumption of the local context, using for instance\n rw mul_comm a b at hyp,\nin order to replace a*b by b*a in assumption hyp.\n\nThe next example will use a third lemma:\n two_mul a : 2*a = a + a\n\nAlso we use the `exact` tactic, which allows to provide a direct proof term.\n-/\n\nexample (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d :=\nbegin\n rw hyp' at hyp,\n rw mul_comm d a at hyp,\n rw ← two_mul (a*d) at hyp,\n rw ← mul_assoc 2 a d at hyp,\n exact hyp, -- Our assumption hyp is now exactly what we have to prove\nend\n\n/-\nAnd the next one can use:\n sub_self x : x - x = 0\n-/\n\n-- 0004\nexample (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 :=\nbegin\n calc c = b*a -d : by {rw hyp}\n ... = b*a - a*b : by {rw hyp'}\n ... = b*a - b*a : by {rw mul_comm}\n ... = 0 : by {rw sub_self},\nend\n\n/-\nWhat is written in the two preceding example is very far away from what we would write on\npaper. Let's now see how to get a more natural layout.\nInside each pair of curly braces below, the goal is to prove equality with the preceding line.\n-/\n\nexample (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d :=\nbegin\n calc c = d*a + b : by { rw hyp }\n ... = d*a + a*d : by { rw hyp' }\n ... = a*d + a*d : by { rw mul_comm d a }\n ... = 2*(a*d) : by { rw two_mul }\n ... = 2*a*d : by { rw mul_assoc },\nend\n\n/-\nLet's note there is no comma at the end of each line of calculation. `calc` is really one\ncommand, and the comma comes only after it's fully done.\n\nFrom a practical point of view, when writing such a proof, it is convenient to:\n* pause the tactic state view update in VScode by clicking the Pause icon button\n in the top right corner of the Lean Goal buffer\n* write the full calculation, ending each line with \": by {}\"\n* resume tactic state update by clicking the Play icon button and fill in proofs between \n curly braces.\n\nLet's return to the other example using this method. \n-/\n\n-- 0005\nexample (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 :=\nbegin\n calc c = b*a - d : by {rw hyp}\n ... = b*a - a*b : by {rw hyp'}\n ...= b*a - b*a : by {rw mul_comm b a}\n ...= 0 : by {rw sub_self}\nend\n\n/-\nThe preceding proofs have exhausted our supply of \"mul_comm\" patience. Now it's time\nto get the computer to work harder. The `ring` tactic will prove any goal that follows by\napplying only the axioms of commutative (semi-)rings, in particular commutativity and \nassociativity of addition and multiplication, as well as distributivity.\n\nWe also note that curly braces are not necessary when we write a single tactic proof, so \nlet's get rid of them.\n-/\n\nexample (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d :=\nbegin\n calc c = d*a + b : by rw hyp\n ... = d*a + a*d : by rw hyp'\n ... = 2*a*d : by ring,\nend\n\n/-\nOf course we can use `ring` outside of `calc`. Let's do the next one in one line.\n-/\n\n-- 0006\nexample (a b c : ℝ) : a * (b * c) = b * (a * c) :=\nbegin\n calc a * (b * c) = b * (a * c) : by ring,\nend\n\n/-\nThis is too much fun. Let's do it again.\n-/\n\n-- 0007\nexample (a b : ℝ) : (a + b) + a = 2*a + b :=\nbegin\n calc (a + b) + a = 2*a + b : by ring,\nend\n\n/-\nMaybe this is cheating. Let's try to do the next computation without ring.\nWe could use:\npow_two x : x^2 = x*x\nmul_sub a b c : a*(b-c) = a*b - a*c\nadd_mul a b c : (a+b)*c = a*c + b*c\nadd_sub a b c : a + (b - c) = (a + b) - c\nsub_sub a b c : a - b - c = a - (b + c)\nadd_zero a : a + 0 = a\n-/\n\n-- 0008\nexample (a b : ℝ) : (a + b)*(a - b) = a^2 - b^2 :=\nbegin\n rw mul_sub (a+b) a b,\n rw add_mul a b a,\n rw mul_comm b a,\n rw add_mul a b b,\n rw ← sub_sub,\n rw ← add_sub,\n rw sub_self,\n rw add_zero (a*a),\n rw pow_two a,\n rw pow_two b,\n\n\n\n \n\n\n\n\n\nend\n\n/- Let's stick to ring in the end. -/\n\n", "meta": {"author": "marco-campos", "repo": "lean-exercises", "sha": "5093594c7d499e59f94a2eb62ef96979f4298b05", "save_path": "github-repos/lean/marco-campos-lean-exercises", "path": "github-repos/lean/marco-campos-lean-exercises/lean-exercises-5093594c7d499e59f94a2eb62ef96979f4298b05/01_equality_rewriting.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9585377320263431, "lm_q2_score": 0.891811054783143, "lm_q1q2_score": 0.8548345458478547}}
{"text": "/-\nCopyright (c) 2022 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor : Kevin Buzzard\n-/\n\nimport tactic -- imports all the Lean tactics\nimport data.real.basic -- the reals\n\n/-!\n\n# Sets in Lean, sheet 6 : pushforward and pullback\n\n## Pushforward of a set along a map\n\nIf `f : X → Y` then given a subset `S : set X` of `X` we can push it\nforward to make a subset `f(S) : set Y` of `Y`. The definition\nof `f(S)` is `{y : Y | ∃ x : X, x ∈ S ∧ f x = y}`. \n\nHowever `f(S)` doesn't make sense in Lean, because `f` eats\nterms of type `X` and not `S`, which has type `set X`. \nIn Lean we use the notation `f '' S` for this. This is notation\nfor `set.image` and if you need any API for this, it's likely\nto use the word `image`.\n\n## Pullback of a set along a map\n\nIf `f : X → Y` then given a subset `T : set Y` of `Y` we can\npull it back to make a subset `f⁻¹(T) : set X` of `X`. The definition\nof `f⁻¹(T)` is `{x : X | f x ∈ T}`.\n\nHowever `f⁻¹(T)` doesn't make sense in Lean either, because\n`⁻¹` is notation for `has_inv.inv`, whose type in Lean\nis `α → α`. In other words, if `x` has a certain type, then\n`x⁻¹` *must* have the same type. The notation was basically designed\nfor group theory. In Lean we use the notation `f ⁻¹' T` for this pullback.\nThis is notation for `set.preimage` and if you need any API for this,\nit's likely to use the word `preimage`.\n\n-/\n\nvariables (X Y : Type) (f : X → Y) (S : set X) (T : set Y)\n\nexample : S ⊆ f ⁻¹' (f '' S) :=\nbegin\n sorry\nend\n\nexample : f '' (f ⁻¹' T) ⊆ T :=\nbegin\n sorry\nend\n\n-- `library_search` will do this but see if you can do it yourself.\nexample : f '' S ⊆ T ↔ S ⊆ f ⁻¹' T :=\nbegin\n sorry\nend\n\n-- Pushforward and pullback along the identity map don't change anything\n\n-- pullback is not so hard\nexample : id ⁻¹' S = S :=\nbegin\n sorry\nend\n\n-- pushforward is a little trickier. You might have to `ext x, split`.\nexample : id '' S = S :=\nbegin\n sorry\nend\n\n-- Now let's try composition.\nvariables (Z : Type) (g : Y → Z) (U : set Z)\n\n-- preimage of preimage is preimage of comp\nexample : (g ∘ f) ⁻¹' U = f ⁻¹' (g ⁻¹' U) :=\nbegin\n sorry\nend\n\n-- preimage of preimage is preimage of comp\nexample : (g ∘ f) '' S = g '' (f '' S) :=\nbegin\n sorry\nend\n", "meta": {"author": "ImperialCollegeLondon", "repo": "formalising-mathematics-2023", "sha": "982f6d28a900f2e04e93e7cf298db4f5998be0d5", "save_path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2023", "path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2023/formalising-mathematics-2023-982f6d28a900f2e04e93e7cf298db4f5998be0d5/src/section05sets/sheet6.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9433475683211323, "lm_q2_score": 0.9059898178450964, "lm_q1q2_score": 0.8546632915878772}}
{"text": "import data.real.basic\n\n/-\nOne of the earliest kind of proofs one encounters while learning mathematics is proving by\na calculation. It may not sound like a proof, but this is actually using lemmas expressing \nproperties of operations on numbers. It also uses the fundamental property of equality: if two\nmathematical objects A and B are equal then, in any statement involving A, one can replace A \nby B. This operation is called rewriting, and the Lean \"tactic\" for this is `rw`.\n\nIn the following exercises, we will use the following two lemmas:\n mul_assoc a b c : a * b * c = a * (b * c)\n mul_comm a b : a*b = b*a\n\nHence the command \n rw mul_assoc a b c,\nwill replace a*b*c by a*(b*c) in the current goal.\n\nIn order to replace backward, we use\n rw ← mul_assoc a b c,\nreplacing a*(b*c) by a*b*c in the current goal.\n\nOf course we don't want to constantly invoke those lemmas, and we will eventually introduce\nmore powerful solutions.\n-/\n\n-- Uncomment the following line if you want to see parentheses around subexpressions.\n-- set_option pp.parens true\n\nexample (a b c : ℝ) : (a * b) * c = b * (a * c) :=\nbegin\n rw mul_comm a b,\n rw mul_assoc b a c,\nend\n\n-- 0001\nexample (a b c : ℝ) : (c * b) * a = b * (a * c) :=\nbegin\n rw mul_comm c b,\n rw mul_assoc b c a,\n rw mul_comm c a,\nend\n\n-- 0002\nexample (a b c : ℝ) : a * (b * c) = b * (a * c) :=\nbegin\n rw ← mul_assoc a b c,\n rw mul_comm a b,\n rw mul_assoc b a c,\nend\n\n/-\nNow let's return to the preceding example to experiment with what happens\nif we don't give arguments to mul_assoc or mul_comm.\nFor instance, you can start the next proof with\n rw ← mul_assoc,\nTry to figure out what happens.\n-/\n\n-- 0003\nexample (a b c : ℝ) : a * (b * c) = b * (a * c) :=\nbegin\n rw ← mul_assoc,\n rw mul_comm a b,\n rw mul_assoc,\nend\n\n/-\nWe can also perform rewriting in an assumption of the local context, using for instance\n rw mul_comm a b at hyp,\nin order to replace a*b by b*a in assumption hyp.\n\nThe next example will use a third lemma:\n two_mul a : 2*a = a + a\n\nAlso we use the `exact` tactic, which allows to provide a direct proof term.\n-/\n\nexample (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d :=\nbegin\n rw hyp' at hyp,\n rw mul_comm d a at hyp,\n rw ← two_mul (a*d) at hyp,\n rw ← mul_assoc 2 a d at hyp,\n exact hyp, -- Our assumption hyp is now exactly what we have to prove\nend\n\n/-\nAnd the next one can use:\n sub_self x : x - x = 0\n-/\n\n-- 0004\nexample (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 :=\nbegin\n rw hyp' at hyp,\n rw mul_comm b a at hyp,\n rw sub_self at hyp,\n exact hyp,\nend\n\n/-\nWhat is written in the two preceding example is very far away from what we would write on\npaper. Let's now see how to get a more natural layout.\nInside each pair of curly braces below, the goal is to prove equality with the preceding line.\n-/\n\nexample (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d :=\nbegin\n calc c = d*a + b : by { rw hyp }\n ... = d*a + a*d : by { rw hyp' }\n ... = a*d + a*d : by { rw mul_comm d a }\n ... = 2*(a*d) : by { rw two_mul }\n ... = 2*a*d : by { rw mul_assoc },\nend\n\n/-\nLet's note there is no comma at the end of each line of calculation. `calc` is really one\ncommand, and the comma comes only after it's fully done.\n\nFrom a practical point of view, when writing such a proof, it is convenient to:\n* pause the tactic state view update in VScode by clicking the Pause icon button\n in the top right corner of the Lean Goal buffer\n* write the full calculation, ending each line with \": by {}\"\n* resume tactic state update by clicking the Play icon button and fill in proofs between \n curly braces.\n\nLet's return to the other example using this method. \n-/\n\n-- 0005\nexample (a b c d : ℝ) (hyp : c = b*a - d) (hyp' : d = a*b) : c = 0 :=\nbegin\n calc c = b*a-d : by { rw hyp }\n ... = b*a - a*b : by { rw hyp' }\n ... = b*a - b*a : by { rw mul_comm }\n ... = 0 : by { rw sub_self },\nend\n\n/-\nThe preceding proofs have exhausted our supply of \"mul_comm\" patience. Now it's time\nto get the computer to work harder. The `ring` tactic will prove any goal that follows by\napplying only the axioms of commutative (semi-)rings, in particular commutativity and \nassociativity of addition and multiplication, as well as distributivity.\n\nWe also note that curly braces are not necessary when we write a single tactic proof, so \nlet's get rid of them.\n-/\n\nexample (a b c d : ℝ) (hyp : c = d*a + b) (hyp' : b = a*d) : c = 2*a*d :=\nbegin\n calc c = d*a + b : by rw hyp\n ... = d*a + a*d : by rw hyp'\n ... = 2*a*d : by ring,\nend\n\n/-\nOf course we can use `ring` outside of `calc`. Let's do the next one in one line.\n-/\n\n-- 0006\nexample (a b c : ℝ) : a * (b * c) = b * (a * c) :=\nbegin\n ring,\nend\n\n/-\nThis is too much fun. Let's do it again.\n-/\n\n-- 0007\nexample (a b : ℝ) : (a + b) + a = 2*a + b :=\nbegin\n ring,\nend\n\n/-\nMaybe this is cheating. Let's try to do the next computation without ring.\nWe could use:\npow_two x : x^2 = x*x\nmul_sub a b c : a*(b-c) = a*b - a*c\nadd_mul a b c : (a+b)*c = a*c + b*c\nadd_sub a b c : a + (b - c) = (a + b) - c\nsub_sub a b c : a - b - c = a - (b + c)\nadd_zero a : a + 0 = a\n-/\n\n-- 0008\nexample (a b : ℝ) : (a + b)*(a - b) = a^2 - b^2 :=\nbegin\n -- ring,\n\n -- calc (a+b)*(a-b) = a*(a-b) + b*(a-b) : by rw add_mul\n -- ... = a*a - a*b + b*(a-b) : by rw mul_sub\n -- ... = a*a - a*b + (b*a - b*b) : by rw mul_sub\n -- ... = a*a - a*b + b*a - b*b : by rw add_sub\n -- ... = a*a - a*b + a*b - b*b : by rw mul_comm b a\n -- ... = a*a + (a*b - a*b) - b*b : by sorry\n -- ... = a*a + 0 - b*b : by sub_self\n -- ... = a*a - b*b : by rw add_zero\n -- ... = a^2 - b*b : by rw pow_two a\n -- ... = a^2 - b^2 : by rw pow_two b,\n\n rw pow_two,\n rw pow_two,\n rw mul_sub,\n rw add_mul,\n rw add_mul,\n rw ← add_sub,\n rw ← sub_sub,\n rw mul_comm b a,\n rw sub_self,\n rw add_sub,\n rw add_zero,\nend\n\n/- Let's stick to ring in the end. -/\n\n", "meta": {"author": "fzyzcjy", "repo": "learn_lean", "sha": "3d47e1641bb7d7afb590d18a73fa0c562e51e733", "save_path": "github-repos/lean/fzyzcjy-learn_lean", "path": "github-repos/lean/fzyzcjy-learn_lean/learn_lean-3d47e1641bb7d7afb590d18a73fa0c562e51e733/src/tutorial/exercises/01_equality_rewriting.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9489172601537141, "lm_q2_score": 0.9005297801113612, "lm_q1q2_score": 0.8545282516300995}}
{"text": "/- Math40001 : Introduction to university mathematics.\n\nProblem Sheet 1, October 2020.\n\n-/\n\n/- Question 1. \n\nLet P and Q be Propositions (that is, true/false statements).\nProve that P ∨ Q → Q ∨ P. \n\n-/\nimport tactic\n\nlemma question_one (P Q : Prop) : P ∨ Q → Q ∨ P :=\nbegin\n intro hPorQ,\n cases hPorQ with hP hQ,\n right,\n exact hP,\n left,\n exact hQ,\nend\n/-\n\nFor question 2, comment out one option (or just delete it)\nand prove the other one.\n-/\n\n-- Part (a): is → symmetric? \n\nlemma question_2a_false : ¬ (∀ P Q : Prop, (P → Q) → (Q → P)) :=\nbegin\n intro hPQ,\n \nend\n\n-- Part (b) : is ↔ symmetric?\n\nlemma question_2b_true (P Q : Prop) : (P ↔ Q) → (Q ↔ P) :=\nbegin\n sorry\nend\n\nlemma question_2b_false : ¬ (∀ P Q : Prop, (P ↔ Q) → (Q ↔ P)) :=\nbegin\n sorry\nend\n\n/- Question 3.\n\nSay P, Q and R are propositions, and we know:\n1) if Q is true then P is true\n2) If Q is false then R is false.\n\nCan we deduce that R implies P? Comment out one\noption and prove the other. Hint: if you're stuck,\n\"apply classical.by_contradiction\" sometimes helps.\nclassical.by_contradiction is the theorem that ¬ ¬ P → P.\n-/\n\nlemma question_3_true (P Q R : Prop) \n (h1 : Q → P)\n (h2 : ¬ Q → ¬ R) : \nR → P :=\nbegin\n sorry\nend\n\nlemma question_3_false : ¬ (∀ P Q R : Prop, \n (Q → P) →\n (¬ Q → ¬ R) → \n R → P) :=\nbegin\n sorry\nend\n\n/- Question 4.\n\nIs it possible to find three true-false statements P , Q and R, such that\n(P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R)\nis true?\n\n-/\n\nlemma question_4_true : ∃ (P Q R : Prop),\n (P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R) :=\nbegin\n sorry\nend\n\n\nlemma question_4_false : ∀ (P Q R : Prop),\n ¬ ((P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (¬P ∨ ¬Q ∨ R) ∧ (¬P ∨ ¬Q ∨ ¬R)) :=\nbegin\n sorry\nend\n\n/- Question 5.\n\n Say that for every integer n we have a proposition P n.\n Say we know P n → P (n + 8) for all n, and\n P n → P (n -3) for all n. Prove that the P n are either\n all true, or all false. \n\nThis question is harder than the others.\n-/\nlemma question_5 (P : ℤ → Prop) (h8 : ∀ n, P n → P (n + 8)) (h3 : ∀ n, P n → P (n - 3)) :\n(∀ n, P n) ∨ (∀ n, ¬ (P n)) :=\nbegin\n sorry\nend\n\n/-\nThe first four of these questions can be solved using only the following\ntactics:\n\nintro\napply (or, better, refine)\nleft, right, cases, split\nassumption (or, better, exact)\nhave,\nsimp,\nuse,\ncontradiction (or, better, false.elim)\n\nThe fifth question is harder. \n-/\n", "meta": {"author": "SzymonKubica", "repo": "Lean", "sha": "627bff2f001ba3f009c112c9332093e8de84863c", "save_path": "github-repos/lean/SzymonKubica-Lean", "path": "github-repos/lean/SzymonKubica-Lean/Lean-627bff2f001ba3f009c112c9332093e8de84863c/ProblemSheet1.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9518632247867717, "lm_q2_score": 0.8976952866333483, "lm_q1q2_score": 0.8544831304107042}}
{"text": "-- Cantor's theorem in Lean\n\nimport tactic.interactive\n\nopen function\n\n/- Theorem: If X is any type, then there is no bijective function f\n from X to the power set of X.\n-/\n\ntheorem no_bijection_to_power_set (X : Type) :\n∀ f : X → set X, ¬ (bijective f) :=\nbegin\n -- Proof by Kevin Buzzard\n\n -- let f be an arbitrary function from X to the power set of X\n intro f,\n -- Assume, for a contradiction, that f is bijective\n intro Hf,\n -- f is bijective, so it's surjective.\n cases Hf with Hi Hs,\n -- it's also injective, but I don't even care\n clear Hi,\n -- Let S be the usual cunning set\n let S : set X := {x : X | x ∉ f x},\n -- what is the definition of surjective?\n unfold surjective at Hs,\n -- What does surjectivity of f say when applied to S?\n have HCantor := Hs S,\n -- It tells us that there's x in X with f x = S!\n cases HCantor with x Hx,\n -- That means x is in f x if and only if x has is in S.\n have Hconclusion_so_far : x ∈ f x ↔ x ∈ S := by rw [Hx],\n -- but this means (x ∈ f x) ↔ ¬ (x ∈ f x)\n have Hlogical_nonsense : (x ∈ f x) ↔ ¬ (x ∈ f x) := Hconclusion_so_far,\n -- automation can now take over.\n revert Hlogical_nonsense,\n simp,\n -- Other proofs welcome. Get in touch. Write a function. \n -- xenaproject.wordpress.com\nend\n\n-- folding up the proof gives this\ntheorem no_bijection_to_power_set'\n (X : Type)\n (f : X → set X) :\nbijective f → false :=\nλ ⟨Hi, Hs⟩,\nlet ⟨x,Hx⟩ := Hs {x : X | x ∉ f x} in\nhave Hconclusion_so_far : x ∈ f x ↔ x ∈ _ := by rw [Hx],\nbegin clear _fun_match,clear _let_match,tauto! end\n\n-- NB this function is in mathlib already, it's called\n-- cantor_surjective. The proof is quite compressed: two lines!\ntheorem cantor_surjective\n (α : Type)\n (f : α → α → Prop)\n (h : function.surjective f) :\nfalse :=\nlet ⟨D, e⟩ := h (λ a, ¬ f a a) in\n(iff_not_self (f D D)).1 $ iff_of_eq (congr_fun e D)\n\n\n-- When unravelled, the mathlib proof looks like this:\ntheorem cantor_surjective'\n (X : Type)\n (f : X → set X)\n (Hs : surjective f) :\nfalse :=\nbegin\nhave Hcantor := Hs {x : X | x ∉ f x},\ncases Hcantor with x Hx,\n apply (iff_not_self (x ∈ f x)).1,\n apply iff_of_eq,\n apply congr_fun Hx x,\nend \n\n/-\n NB I renamed the variables in the mathlib proof\n α → X\n h → Hf\n D → x\n e → Hx,\n-/", "meta": {"author": "ImperialCollegeLondon", "repo": "M1F_room_342_questions", "sha": "63de9a6ab9c27a433039dd5530bc9b10b1d227f7", "save_path": "github-repos/lean/ImperialCollegeLondon-M1F_room_342_questions", "path": "github-repos/lean/ImperialCollegeLondon-M1F_room_342_questions/M1F_room_342_questions-63de9a6ab9c27a433039dd5530bc9b10b1d227f7/src/Cantor/cantor.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9481545296876999, "lm_q2_score": 0.9005297901222472, "lm_q1q2_score": 0.8538413996231223}}
{"text": "/- Homework 2.1: Functional Programming — Lists -/\n\n/- Question 1: replicateting an element -/\n\n/- 1.1. Define a function `replicate` that takes an element `a` of type `α` and a natural number `n`\nand that returns a list of length `n` consisting of `n` occurrences of `a`: `[a, ..., a]` -/\n\ndef replicate {α : Type} (a: α): ℕ → list α\n| 0 := []\n| (x+1) := a :: replicate x \n\n#reduce replicate 2 9\n#reduce replicate 1 9\n#reduce replicate 2 2\n\n#check list.length\n\n/- 1.2. State and prove that `replicate a n` has length `n`. -/\nlemma same_length {α : Type} : ∀(n : ℕ), ∀(a: α), list.length(replicate a n) = n \n| 0 a := begin simp[replicate] end\n| (n + 1) a := by simp[replicate, list.length, same_length] \n\n#check list.map\n\n/- 1.3. State and prove that `list.map f (replicate a n)` is the same as `replicate (f a) n`. Make\nsure to state the result as generally as possible. -/\n\nlemma replicate_map {α β : Type} (f : α → β): ∀ (n : ℕ), ∀ (a: α), list.map f (replicate a n) = replicate (f a) n\n| 0 a := by simp[replicate]\n| (n + 1) a := begin rw[replicate], rw[replicate], simp[replicate_map] end\n\n/- 1.4. State and prove that `replicate a m ++ replicate a n` equals `replicate a (m + n)`.\n\nThere are many ways to prove this. A calculational proof might be useful here to manipulate the\nexpression step by step (but is not mandatory). -/\nlemma replicate_append {α : Type}: ∀ (n m : ℕ), ∀ (a: α), replicate a m ++ replicate a n = replicate a (m + n)\n| n 0 a:= by simp[replicate] \n| n (m+1) a := begin simp[replicate], rw[<-add_assoc], rw[replicate_append], rw[<-replicate_append], rw[<-replicate_append], simp[replicate], rw[replicate_append], simp[replicate], sorry end\n\n\nlemma replicate_append' {α : Type}: ∀ (n m : ℕ), ∀ (a: α), replicate a m ++ replicate a n = replicate a (m + n):=\nbegin\nintros m n as,\ninduction n,\nrepeat{simp[replicate]},\nrw[n_ih],\nsimp[replicate],\nrefl\nend\n\ndef map {α β: Type} (f: α → β): list α → list β\n| [] := []\n| (x :: xs) := f x :: map xs\n\ndef concat {α: Type}: list(list α) → list α \n| [] := []\n| (xs :: xss) := xs ++ concat xss\n\nlemma map_concat {α β: Type} (f :α →β) :∀xss : list (list α), map f (concat xss) = concat (map (map f) xss)\n| [] := by refl\n| (xs :: xss) := begin simp[map], simp[concat], rw[<-map_concat], end\n/- 1.5. State and prove that `reverse` has no effect on `replicate a n`.\n\nHint: If you get stuck in the induction step, this may indicate that you first need to prove\nanother lemma, also by induction. -/\n\ndef reverse {α : Type} : list α → list α\n| [] := []\n| (x :: xs) := reverse xs ++ [x]\n\nlemma replicate_reverse_append {α : Type}: ∀ (n : ℕ), ∀ (a: α), replicate a n ++ [a]= a :: replicate a n\n| 0 a := by simp[replicate]\n| (n + 1) a := begin simp[replicate], rw[replicate_reverse_append] end\n\n\nlemma replicate_reverse {α : Type}: ∀ (n : ℕ), ∀ (a: α), reverse (replicate a n) = replicate a n\n| 0 a := begin simp[replicate], simp[reverse] end\n| (n + 1) a := begin simp[replicate], simp[reverse], rw[replicate_reverse], rw[replicate_reverse_append] end\n\n\n/- Question 2: Binary relations -/\n\n/- The following questions concern modeling properties of binary relations (actually, curried binary\npredicates). To avoid repeating `α` and `R` in each definition, we use a section and fix them\nlocally in the section. -/\n\nsection\n\nparameter {α : Type}\nparameter R : α → α → Prop\n\ndef irreflexive_rel := ∀x : α, ¬ R x x\ndef antisymmetric_rel := ∀x y : α, R x y → ¬ R y x\ndef transitive_rel := ∀x y z : α, R x y → R y z → R x z\n\n/- 2.1. Prove that antisymmetry implies irreflexivity.\n\nHint: It may help to first think abstractly why this is the case before trying to prove it in Lean.\nAlternatively, try to see this as a video game and use `intros` and `apply` until you have defeated\nthe boss. There exists a fairly short `apply`-style proof. -/\n\nlemma antisymmetric_rel_implies_irreflexive_rel : antisymmetric_rel → irreflexive_rel :=\nbegin\nintros a x rx,\napply a,\napply rx,\nassumption\nend\n\n\n/- 2.2. Define `symmetric_rel` as the proposition stating that `R` is symmetric—that is, if `x`\nand `y` are related via `R`, then `y` and `x` are related via `R`. -/\n\ndef symmetric_rel := ∀x y : α, R x y → R y x\n\n/- 2.3. State and prove the property that any irreflexive and transitive relation is\nantisymmetric. -/\n\nlemma irreflexive_transitive_antisymmetric : irreflexive_rel → transitive_rel → antisymmetric_rel:=\nbegin\nintros x y hx hy,\nintro relx,\nintro rely,\napply x,\napply y,\napply relx,\nassumption\nend\n\n/- Notice that each of the definition takes `α` as implicit argument and `R` as explicit argument.\nEverything we have defined and proved above is now available in a very general form. -/\n\n#check irreflexive_rel\n#check antisymmetric_rel\n#check transitive_rel\n\n#check antisymmetric_rel_implies_irreflexive_rel\n\nend", "meta": {"author": "KjellZijlemaker", "repo": "Logical_Verification_VU", "sha": "4578b93bf1615466996157bb333c84122b201d99", "save_path": "github-repos/lean/KjellZijlemaker-Logical_Verification_VU", "path": "github-repos/lean/KjellZijlemaker-Logical_Verification_VU/Logical_Verification_VU-4578b93bf1615466996157bb333c84122b201d99/Exercises week 4/21_homework_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9284087926320944, "lm_q2_score": 0.9196425262224018, "lm_q1q2_score": 0.8538042074232693}}
{"text": "\n/-\nLet's look at three functions, each transforming\none list into another by converting each element\nin the argument list, through the application of\na function, into a corresponding element in the\nresulting list. Try to see the commonalities and\nthe dimesions of variation. \n-/\n\n\n-- map each n in argument list to n+1 in result\n-- e.g., add_1 [1, 2, 3] = [2, 3, 4]\ndef add_1 : list nat → list nat\n| [] := []\n| (h::t) := (h+1)::add_1 t\n\nexample : add_1 [] = [] := rfl\nexample : add_1 [1, 2, 3] = [2, 3, 4] := rfl \n\n\n-- map each n in argument list to n+2 in result\n-- e.g., add_2 [1, 2, 3] = [3, 4, 5] \ndef add_2 : list nat → list nat\n| [] := []\n| (h::t) := (h+2)::add_2 t\n\nexample : add_2 [] = [] := rfl\nexample : add_2 [1, 2, 3] = [3, 4, 5] := rfl \n\n\n-- map each n in argument list to n^2 in result\n-- e.g., add_2 [1, 2, 3] = [1, 4, 9] \ndef sqr_list : list nat → list nat \n| [] := []\n| (h::t) := (h*h)::sqr_list t \n\nexample : sqr_list [1, 2, 3] = [1, 4, 9] := rfl \n\n\n/-\nOne *dimension of variation* is in the function applied\nto each element of the given argument list to compute \nthe corresponding element of the result list. We abstract\nfrom these variations, replacing the applications of these\nwith applications of the function passed as an argument.\nThe result is a map function that can convert any list of\nnatural numbers to another list of natural numbers by\napplying a given function (of type nat → nat) to each\nelement in the given list to compute its corresponding \nvalue in the list to be returned. \n-/\ndef map_nat : (nat → nat) → list nat → list nat\n| fn [] := []\n| fn (h::t) := (fn h)::(map_nat fn t) \n\nexample : map_nat (nat.succ) [1, 2, 3] = [2, 3, 4] := rfl\nexample : map_nat (λ n, n * n) [1, 2, 3] = [1, 4, 9] := rfl\n\n\n/-\nA second, orthogonal, dimension of variation is in the\ntypes of elements in the argument and results lists. In \nthe general case, a map function can take in a list of \nvalues of any type, α, and convert it to a corresponding\nlist of elements of type β by applying a function of type\nα → β to each element of the first list to calculate the\ncorresponding elements of the second list. \n-/\nuniverse u \ndef map {α β : Type u} : (α → β) → list α → list β \n| fn [] := []\n| fn (h::t) := (fn h)::(map fn t) \n\nexample: map nat.succ [1, 2, 3] = [2, 3, 4] := rfl\n\n\n/-\nPractice: write an expression to convert a list of nat to \na list of bool, where each bool is the parity (oddness on\na scale of {0, 1}) of its corresponding value.\n-/\n\n-- Answer\n\nexample : \n map (λ n, n%2) [1, 2, 3, 4, 5] = [1, 0, 1, 0, 1] \n:= rfl \n \n /-\n Practice: write an expression that uses map to convert\n a list of strings to a list of their lengths.\n -/\n \n example :\n map (λ s, string.length s) [\"Hello\", \"Lean\"] = [5, 4] \n:= rfl\n\n\n\n/-\n*******************************************\nThe foldr, or reduce, higher-order function\n*******************************************\n-/\n\n/-\nAs before, we'll start with a few concrete examples\nand from them will work to a general definition of \nthe folr (\"right fold\") operation on lists of values.\n-/\n\n/-\nExample #1 Reduce a list of natural numbers to the sum \nof all its elements.\n-/\n\ndef sum_of_elts : list nat → nat\n| [] := 0\n| (h::t) := h + sum_of_elts t\n\nexample : sum_of_elts [0, 1, 2, 3, 4, 5] = 15 := rfl\n\ndef prod_of_elts : list nat → nat\n| [] := 1\n| (h::t) := h * sum_of_elts t\n\n-- we can write tests like this\n-- but then we have to use our eyes \n#reduce prod_of_elts [] -- expect 1\n#reduce prod_of_elts [2] -- expect 2\n\n-- it's better to let the type checker do the work\nexample : prod_of_elts [] = 1 := rfl\n-- The following tests catch an error I made in class!\nexample : prod_of_elts [2] = 2 := rfl\nexample : prod_of_elts [1, 2, 3, 4, 5] = 120 := rfl\n-- Practice: find and fix the bug\n\n/-\nClearly the two reduction functions we've looked at, \nsum_of_elts and prod_of_elts vary *dependently* in two\ndimensions: (1) the value returned for the base case of\nan empty list, (2) the function used to produce a final\nanswer by combining the head of a non-empty list with\nthe result of reducing the tail of the list (which we\ndo recursively).\n-/\ndef reduce_elts : (nat → nat → nat) → nat → list nat → nat\n| f id [] := id\n| f id (h::t) := f h (reduce_elts f id t)\n\nexample : reduce_elts nat.add 0 [1,2,3,4,5] = 15 := rfl\nexample : reduce_elts nat.mul 1 [1,2,3,4,5] = 120 := rfl\n\n\n/-\nWe can also generalize over the type of list elements\nand the type of the reduced return result. As an example\nof a case where list elements are of one type and the\nresult of the reduction is of another type, think of a\nfunction, all_even, that takes a list of natural numbers\nand returns true if and only if all are even (otherwise\nfalse). The key to understanding our algorithm is to see\nit as applying one operation one time, with two cases.\nIf the list is empty, return a \"base case\" value for \nthis case, otherwise apply a function to two aguments,\nthe head of the list and the reduction of the tail to\ncompute the final result. That's it. (The computation \nof the result for the tail is by recursion.) The operation\nthat combines the head of the list with the answer for\nthe rest of the list thus takes two arguments, the first\nof the input list element type, the second, the answer\nfor the rest of the list, which will be of the second\ntype: bool, in our example. \n-/\ndef reduce {α β : Type u} : (α → β → β) → β → list α → β \n| f id [] := id\n| f id (h::t) := f h (reduce f id t)\n\n/-\nAs an example, use reduce to implement a function\nthat reduces any list of strings to a single number\nthat is the sum of their lengths.\n-/\n\n/-\nFirst let's write the reduce operator, which will\ntake the string at the head of a list of strings\nalong with the reduction of the rest of the list\n(computed by recursion) and that will then return\nthe answer for the whole list: both head and tail.\n-/ \n\ndef red_strs : string → nat → nat \n| s n := s.length + n\n\n\n-- example: sum of lengths of list of strings\nexample : \n reduce red_strs 0 [\"hello\", \"lean\", \"!\"] = 10 \n:= rfl\n\n-- example: number of odd numbers in a list of nats\nexample : \n reduce nat.add 0 (map (λ n, n%2) [1,2,3,4,5]) = 3 \n:= rfl\n\n/-\nFinally a generalized map/reduce function, taking\na reduction operator, its value for when a list is\nempty, an element conversion function, and a list \nof elements, and that (1) maps it by applying the\nelement conversion function elementwise to produce\na new list, which (2) is then reduced to a final\nresult.\n-/\ndef map_reduce { α β γ : Type u} : (β → γ → γ) → γ → (α → β) → list α → γ\n| red_op id map_fn l := reduce red_op id (map map_fn l)\n\n-- Use reduce to compute number of odd values in a list\n#reduce map_reduce nat.add 0 (λ n, n%2) [1,2,3,4,5]\n\n/- \nUse partial evaluation to define a funciton that \ntakes any list of natural numbers to the number of\nodd values in the list.\n-/\n\ndef count_odds := map_reduce nat.add 0 (λ n, n%2)\nexample : count_odds [1,2,3,4,5] = 3 := rfl\n\n/-\nThe map and reduce functions are function-building\nmachines. Here we've applied map_reduce to (1) a\nreduction operator, (2) it's identity, and (3) an\nelement mapping function to produce a function that\ncounts the number of odd values in a list of nats.\nmap_reduce nat.add 0 (λ n, n%2)\n-/\n\n\n/-\nPractice: use map_reduce to convert a list of\ninterpretations for an expression, e, \n-/\n\n/-\nPractice: Identify the places in the SAT solver\ncode that I/Kevin will provide where special cases\nof map or reduce functions are used and replace \nthem with uses of the higher-order list map and\nreduce functions while maintaining correctness \nof the code.\n\nThe versions in the Lean library are called\nlist.map and list.foldr (for \"fold right\"). You \nmay, and should, use Lean's versions for this\nassignment.\n-/\n\n#check @list.map\n#check @list.foldr\n\n-- Yay!", "meta": {"author": "kevinsullivan", "repo": "cs6501s22", "sha": "c55d342145b127e7b7bf396c660966034e80a944", "save_path": "github-repos/lean/kevinsullivan-cs6501s22", "path": "github-repos/lean/kevinsullivan-cs6501s22/cs6501s22-c55d342145b127e7b7bf396c660966034e80a944/src/content/lectures/S_04_HOF/hof.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9449947101574299, "lm_q2_score": 0.9032942021480236, "lm_q1q2_score": 0.8536082427457584}}
{"text": "import .love01_definitions_and_statements_demo\n\n\n/-! # LoVe Exercise 1: Definitions and Statements\n\nReplace the placeholders (e.g., `:= sorry`) with your solutions. -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1: Truncated Subtraction\n\n1.1. Define the function `sub` that implements truncated subtraction on natural\nnumbers by recursion. \"Truncated\" means that results that mathematically would\nbe negative are represented by 0. For example:\n\n `sub 7 2 = 5`\n `sub 2 7 = 0` -/\n\ndef sub : ℕ → ℕ → ℕ\n| m nat.zero := m\n| 0 n := 0\n| (nat.succ m) (nat.succ n) := (sub m n)\n\n/-! 1.2. Check that your function works as expected. -/\n\n#eval sub 0 0 -- expected: 0\n#eval sub 0 1 -- expected: 0\n#eval sub 0 7 -- expected: 0\n#eval sub 1 0 -- expected: 1\n#eval sub 1 1 -- expected: 0\n#eval sub 3 0 -- expected: 3\n#eval sub 2 7 -- expected: 0\n#eval sub 3 1 -- expected: 2\n#eval sub 3 3 -- expected: 0\n#eval sub 3 7 -- expected: 0\n#eval sub 7 2 -- expected: 5\n\n\n/-! ## Question 2: Arithmetic Expressions\n\nConsider the type `aexp` from the lecture and the function `eval` that\ncomputes the value of an expression. You will find the definitions in the file\n`love01_definitions_and_statements_demo.lean`. One way to find them quickly is\nto\n\n1. hold the Control (on Linux and Windows) or Command (on macOS) key pressed;\n2. move the cursor to the identifier `aexp` or `eval`;\n3. click the identifier. -/\n\n#check aexp\n#check eval\n\n/-! 2.1. Test that `eval` behaves as expected. Make sure to exercise each\nconstructor at least once. You can use the following environment in your tests.\nWhat happens if you divide by zero?\n\nMake sure to use `#eval`. For technical reasons, `#reduce` does not work well\nhere. Note that `#eval` (Lean's evaluation command) and `eval` (our evaluation\nfunction on `aexp`) are unrelated. -/\n\ndef some_env : string → ℤ\n| \"x\" := 3\n| \"y\" := 17\n| _ := 201\n\n#eval eval some_env (aexp.var \"x\") -- expected: 3\n-- invoke `#eval` here\n\n/-! 2.2. The following function simplifies arithmetic expressions involving\naddition. It simplifies `0 + e` and `e + 0` to `e`. Complete the definition so\nthat it also simplifies expressions involving the other three binary\noperators. -/\n\ndef simplify : aexp → aexp\n| (aexp.add (aexp.num 0) e₂) := simplify e₂\n| (aexp.add e₁ (aexp.num 0)) := simplify e₁\n-- insert the missing cases here\n-- catch-all cases below\n| (aexp.num i) := aexp.num i\n| (aexp.var x) := aexp.var x\n| (aexp.add e₁ e₂) := aexp.add (simplify e₁) (simplify e₂)\n| (aexp.sub e₁ e₂) := aexp.sub (simplify e₁) (simplify e₂)\n| (aexp.mul e₁ e₂) := aexp.mul (simplify e₁) (simplify e₂)\n| (aexp.div e₁ e₂) := aexp.div (simplify e₁) (simplify e₂)\n\n/-! 2.3. Is the `simplify` function correct? In fact, what would it mean for it\nto be correct or not? Intuitively, for `simplify` to be correct, it must\nreturn an arithmetic expression that yields the same numeric value when\nevaluated as the original expression.\n\nGiven an environment `env` and an expression `e`, state (without proving it)\nthe property that the value of `e` after simplification is the same as the\nvalue of `e` before. -/\n\nlemma simplify_correct (env : string → ℤ) (e : aexp) :\n eval env e = eval env (simplify e) :=\nsorry\n\n\n/-! ## Question 3: λ-Terms\n\n3.1. Complete the following definitions, by replacing the `sorry` markers by\nterms of the expected type.\n\nHint: A procedure for doing so systematically is described in Section 1.1.4 of\nthe Hitchhiker's Guide. As explained there, you can use `_` as a placeholder\nwhile constructing a term. By hovering over `_`, you will see the current\nlogical context. -/\n\ndef I : α → α :=\nλa, a\n\ndef K : α → β → α :=\nλa b, a\n\ndef C : (α → β → γ) → β → α → γ :=\nλf b a, (f a b)\n\ndef proj_1st : α → α → α :=\nλx y, x\n\n/-! Please give a different answer than for `proj_1st`. -/\n\ndef proj_2nd : α → α → α :=\nλx y, y\n\ndef some_nonsense : (α → β → γ) → α → (α → γ) → β → γ :=\nλ_ a g _, g a\n\n/-! 3.2. Show the typing derivation for your definition of `C` above, on paper\nor using ASCII or Unicode art. You might find the characters `–` (to draw\nhorizontal bars) and `⊢` useful. -/\n\n-- write your solution in a comment here or on paper\n\nend LoVe\n", "meta": {"author": "superestos", "repo": "-logical_verification", "sha": "dab8b8704680679a78b83c2f82e1113ff098b34f", "save_path": "github-repos/lean/superestos--logical_verification", "path": "github-repos/lean/superestos--logical_verification/-logical_verification-dab8b8704680679a78b83c2f82e1113ff098b34f/lean/love01_definitions_and_statements_exercise_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9407897542390751, "lm_q2_score": 0.9073122251200417, "lm_q1q2_score": 0.8535900452887925}}
{"text": "/-\nCopyright (c) 2020 Kevin Lacker. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Kevin Lacker\n-/\nimport algebra.group_power.identities\nimport data.int.nat_prime\nimport tactic.linarith\nimport tactic.norm_cast\nimport data.set.finite\n/-!\n# IMO 1969 Q1\n\nProve that there are infinitely many natural numbers $a$ with the following property:\nthe number $z = n^4 + a$ is not prime for any natural number $n$.\n-/\n\nopen int nat\n\n/-- `good_nats` is the set of natural numbers satisfying the condition in the problem\nstatement, namely the `a : ℕ` such that `n^4 + a` is not prime for any `n : ℕ`. -/\ndef good_nats : set ℕ := {a : ℕ | ∀ n : ℕ, ¬ nat.prime (n^4 + a)}\n\n/-!\nThe key to the solution is that you can factor $z$ into the product of two polynomials,\nif $a = 4*m^4$. This is Sophie Germain's identity, called `pow_four_add_four_mul_pow_four`\nin mathlib.\n-/\nlemma factorization {m n : ℤ} : ((n - m)^2 + m^2) * ((n + m)^2 + m^2) = n^4 + 4*m^4 :=\npow_four_add_four_mul_pow_four.symm\n\n/-!\nTo show that the product is not prime, we need to show each of the factors is at least 2,\nwhich `nlinarith` can solve since they are each expressed as a sum of squares.\n-/\nlemma left_factor_large {m : ℤ} (n : ℤ) (h : 1 < m) : 1 < ((n - m)^2 + m^2) := by nlinarith\nlemma right_factor_large {m : ℤ} (n : ℤ) (h : 1 < m) : 1 < ((n + m)^2 + m^2) := by nlinarith\n\n/-!\nThe factorization is over the integers, but we need the nonprimality over the natural numbers.\n-/\nlemma int_large {m : ℤ} (h : 1 < m) : 1 < m.nat_abs :=\nby exact_mod_cast lt_of_lt_of_le h le_nat_abs\n\nlemma not_prime_of_int_mul' {m n : ℤ} {c : ℕ}\n (hm : 1 < m) (hn : 1 < n) (hc : m*n = (c : ℤ)) : ¬ nat.prime c :=\nnot_prime_of_int_mul (int_large hm) (int_large hn) hc\n\n/-- Every natural number of the form `n^4 + 4*m^4` is not prime. -/\nlemma polynomial_not_prime {m : ℕ} (h1 : 1 < m) (n : ℕ) : ¬ nat.prime (n^4 + 4*m^4) :=\nhave h2 : 1 < (m : ℤ), from coe_nat_lt.mpr h1,\nbegin\n refine not_prime_of_int_mul' (left_factor_large (n : ℤ) h2) (right_factor_large (n : ℤ) h2) _,\n exact_mod_cast factorization\nend\n\n/--\nWe define $a_{choice}(b) := 4*(2+b)^4$, so that we can take $m = 2+b$ in `polynomial_not_prime`.\n-/\ndef a_choice (b : ℕ) : ℕ := 4*(2+b)^4\n\nlemma a_choice_good (b : ℕ) : a_choice b ∈ good_nats :=\npolynomial_not_prime (show 1 < 2+b, by linarith)\n\n/-- `a_choice` is a strictly monotone function; this is easily proven by chaining together lemmas\nin the `strict_mono` namespace. -/\nlemma a_choice_strict_mono : strict_mono a_choice :=\n((strict_mono_id.const_add 2).nat_pow (dec_trivial : 0 < 4)).const_mul (dec_trivial : 0 < 4)\n\n/-- We conclude by using the fact that `a_choice` is an injective function from the natural numbers\nto the set `good_nats`. -/\ntheorem imo1969_q1 : set.infinite {a : ℕ | ∀ n : ℕ, ¬ nat.prime (n^4 + a)} :=\nset.infinite_of_injective_forall_mem a_choice_strict_mono.injective a_choice_good\n", "meta": {"author": "leanprover-community", "repo": "mathzoo", "sha": "87e9b492daeb929838706942aaa2437621b34a0e", "save_path": "github-repos/lean/leanprover-community-mathzoo", "path": "github-repos/lean/leanprover-community-mathzoo/mathzoo-87e9b492daeb929838706942aaa2437621b34a0e/src/mathzoo/olympiads/imo/1969/p1.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9304582554941719, "lm_q2_score": 0.917302657890151, "lm_q1q2_score": 0.8535118308206371}}
{"text": "/-\nCopyright (c) 2018 Chris Hughes. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Chris Hughes\nMostly based on Jeremy Avigad's choose file in lean 2\n-/\n\nimport data.nat.basic\n\nopen nat\n\ndef choose : ℕ → ℕ → ℕ\n| _ 0 := 1\n| 0 (k + 1) := 0\n| (n + 1) (k + 1) := choose n k + choose n (succ k)\n\n@[simp] lemma choose_zero_right (n : ℕ) : choose n 0 = 1 := by cases n; refl\n\n@[simp] lemma choose_zero_succ (k : ℕ) : choose 0 (succ k) = 0 := rfl\n\nlemma choose_succ_succ (n k : ℕ) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl\n\nlemma choose_eq_zero_of_lt : ∀ {n k}, n < k → choose n k = 0\n| _ 0 hk := absurd hk dec_trivial\n| 0 (k + 1) hk := choose_zero_succ _\n| (n + 1) (k + 1) hk := \n have hnk : n < k, from lt_of_succ_lt_succ hk,\n have hnk1 : n < k + 1, from lt_of_succ_lt hk,\n by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1]\n\n@[simp] lemma choose_self (n : ℕ) : choose n n = 1 :=\nby induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)]\n\n@[simp] lemma choose_succ_self (n : ℕ) : choose n (succ n) = 0 := \nchoose_eq_zero_of_lt (lt_succ_self _)\n\n@[simp] lemma choose_one_right (n : ℕ) : choose n 1 = n :=\nby induction n; simp [*, choose]\n\nlemma choose_pos : ∀ {n k}, k ≤ n → 0 < choose n k\n| 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial\n| (n + 1) 0 hk := by simp; exact dec_trivial\n| (n + 1) (k + 1) hk := by rw choose_succ_succ;\n exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (zero_le _)\n\n\nlemma succ_mul_choose_eq : ∀ n k, succ n * choose n k = choose (succ n) (succ k) * succ k\n| 0 0 := dec_trivial\n| 0 (k + 1) := by simp [choose]\n| (n + 1) 0 := by simp\n| (n + 1) (k + 1) := \n by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ,\n ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul] \n\nlemma choose_mul_fact_mul_fact : ∀ {n k}, k ≤ n → choose n k * fact k * fact (n - k) = fact n\n| 0 _ hk := by simp [eq_zero_of_le_zero hk]\n| (n + 1) 0 hk := by simp\n| (n + 1) (succ k) hk := \nbegin\n cases lt_or_eq_of_le hk with hk₁ hk₁,\n { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n :=\n by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk);\n simp [fact_succ, mul_comm, mul_left_comm],\n have h₁ : fact (n - k) = (n - k) * fact (n - succ k) := \n by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ],\n have h₂ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n :=\n by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁); \n simp [fact_succ, mul_comm, mul_left_comm, mul_assoc],\n have h₃ : k * fact n ≤ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk),\n rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, h₂, ← add_one, add_mul, nat.mul_sub_right_distrib,\n fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] },\n { simp [hk₁, mul_comm, choose, nat.sub_self] } \nend\n\ntheorem choose_eq_fact_div_fact {n k : ℕ} (hk : k ≤ n) : choose n k = fact n / (fact k * fact (n - k)) :=\nbegin\n have : fact n = choose n k * (fact k * fact (n - k)) :=\n by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm,\n exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm\nend\n\ntheorem fact_mul_fact_dvd_fact {n k : ℕ} (hk : k ≤ n) : fact k * fact (n - k) ∣ fact n :=\nby rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _\n", "meta": {"author": "ChrisHughes24", "repo": "leanstuff", "sha": "9efa85f72efaccd1d540385952a6acc18fce8687", "save_path": "github-repos/lean/ChrisHughes24-leanstuff", "path": "github-repos/lean/ChrisHughes24-leanstuff/leanstuff-9efa85f72efaccd1d540385952a6acc18fce8687/choose.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9390248242542284, "lm_q2_score": 0.9086178882719769, "lm_q1q2_score": 0.8532147528488412}}
{"text": "import .love01_definitions_and_statements_demo\n\n\n/-! # LoVe Exercise 1: Definitions and Statements\n\nReplace the placeholders (e.g., `:= sorry`) with your solutions. -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1: Truncated Subtraction\n\n1.1. Define the function `sub` that implements truncated subtraction on natural\nnumbers by recursion. \"Truncated\" means that results that mathematically would\nbe negative are represented by 0. For example:\n\n `sub 7 2 = 5`\n `sub 2 7 = 0` -/\n\ndef sub : ℕ → ℕ → ℕ :=\nsorry\n\n/-! 1.2. Check that your function works as expected. -/\n\n#eval sub 0 0 -- expected: 0\n#eval sub 0 1 -- expected: 0\n#eval sub 0 7 -- expected: 0\n#eval sub 1 0 -- expected: 1\n#eval sub 1 1 -- expected: 0\n#eval sub 3 0 -- expected: 3\n#eval sub 2 7 -- expected: 0\n#eval sub 3 1 -- expected: 2\n#eval sub 3 3 -- expected: 0\n#eval sub 3 7 -- expected: 0\n#eval sub 7 2 -- expected: 5\n\n\n/-! ## Question 2: Arithmetic Expressions\n\nConsider the type `aexp` from the lecture and the function `eval` that\ncomputes the value of an expression. You will find the definitions in the file\n`love01_definitions_and_statements_demo.lean`. One way to find them quickly is\nto\n\n1. hold the Control (on Linux and Windows) or Command (on macOS) key pressed;\n2. move the cursor to the identifier `aexp` or `eval`;\n3. click the identifier. -/\n\n#check aexp\n#check eval\n\n/-! 2.1. Test that `eval` behaves as expected. Make sure to exercise each\nconstructor at least once. You can use the following environment in your tests.\nWhat happens if you divide by zero?\n\nMake sure to use `#eval`. For technical reasons, `#reduce` does not work well\nhere. Note that `#eval` (Lean's evaluation command) and `eval` (our evaluation\nfunction on `aexp`) are unrelated. -/\n\ndef some_env : string → ℤ\n| \"x\" := 3\n| \"y\" := 17\n| _ := 201\n\n#eval eval some_env (aexp.var \"x\") -- expected: 3\n-- invoke `#eval` here\n\n/-! 2.2. The following function simplifies arithmetic expressions involving\naddition. It simplifies `0 + e` and `e + 0` to `e`. Complete the definition so\nthat it also simplifies expressions involving the other three binary\noperators. -/\n\ndef simplify : aexp → aexp\n| (aexp.add (aexp.num 0) e₂) := simplify e₂\n| (aexp.add e₁ (aexp.num 0)) := simplify e₁\n-- insert the missing cases here\n-- catch-all cases below\n| (aexp.num i) := aexp.num i\n| (aexp.var x) := aexp.var x\n| (aexp.add e₁ e₂) := aexp.add (simplify e₁) (simplify e₂)\n| (aexp.sub e₁ e₂) := aexp.sub (simplify e₁) (simplify e₂)\n| (aexp.mul e₁ e₂) := aexp.mul (simplify e₁) (simplify e₂)\n| (aexp.div e₁ e₂) := aexp.div (simplify e₁) (simplify e₂)\n\n/-! 2.3. Is the `simplify` function correct? In fact, what would it mean for it\nto be correct or not? Intuitively, for `simplify` to be correct, it must\nreturn an arithmetic expression that yields the same numeric value when\nevaluated as the original expression.\n\nGiven an environment `env` and an expression `e`, state (without proving it)\nthe property that the value of `e` after simplification is the same as the\nvalue of `e` before. -/\n\nlemma simplify_correct (env : string → ℤ) (e : aexp) :\n true := -- replace `true` by your lemma statement\nsorry\n\n\n/-! ## Question 3: λ-Terms\n\n3.1. Complete the following definitions, by replacing the `sorry` markers by\nterms of the expected type.\n\nHint: A procedure for doing so systematically is described in Section 1.1.4 of\nthe Hitchhiker's Guide. As explained there, you can use `_` as a placeholder\nwhile constructing a term. By hovering over `_`, you will see the current\nlogical context. -/\n\ndef I : α → α :=\nλa, a\n\ndef K : α → β → α :=\nλa b, a\n\ndef C : (α → β → γ) → β → α → γ :=\nsorry\n\ndef proj_1st : α → α → α :=\nsorry\n\n/-! Please give a different answer than for `proj_1st`. -/\n\ndef proj_2nd : α → α → α :=\nsorry\n\ndef some_nonsense : (α → β → γ) → α → (α → γ) → β → γ :=\nsorry\n\n/-! 3.2. Show the typing derivation for your definition of `C` above, on paper\nor using ASCII or Unicode art. You might find the characters `–` (to draw\nhorizontal bars) and `⊢` useful. -/\n\n-- write your solution in a comment here or on paper\n\nend LoVe\n", "meta": {"author": "blanchette", "repo": "logical_verification_2021", "sha": "23b469c79afd482fa66da82e4726a317e3a7b5d5", "save_path": "github-repos/lean/blanchette-logical_verification_2021", "path": "github-repos/lean/blanchette-logical_verification_2021/logical_verification_2021-23b469c79afd482fa66da82e4726a317e3a7b5d5/lean/love01_definitions_and_statements_exercise_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9390248157222396, "lm_q2_score": 0.908617890746506, "lm_q1q2_score": 0.8532147474201679}}
{"text": "import algebra.group group_theory.order_of_element\nimport data.nat.parity\n/-!\n# Group theory basics (drawn from Sheet 1 of the MTH2010 Groups, Rings, and Fields module)\n-/\n\n/-!\n## Commutative groups\n-/\n\ndef is_comm (G : Type) [group G] : Prop := ∀ g h : G, g * h = h * g\n\nvariables {G H : Type} [group G] [group H]\n\n/--\nProve that a product `G × H` of groups is commutatitive if and only if each of `G` and `H` are.\n-/\nlemma commutative_prod_iff : is_comm (G × H) ↔ (is_comm G) ∧ (is_comm H) :=\nsorry\n\n/-!\n## Group orders\n\nLet `G` be a group and let `x : G`. We write `order_of x` for the order of `x` in `G`.\n\nUseful theorems regarding group orders:\n\n1. `pow_order_of_eq_one x` states that `x` raised to the power `order_of x` equals\nthe identity element in the group. That is, `x ^ order_of x = 1`.\n\n2. `order_of_dvd_of_pow_eq_one` states that if `x ^ n = 1`, then `order_of x ∣ n`.\n\n3. `nat.dvd_antisymm` is the result that if `n ∣ m` and `m ∣ n`, then `n = m`. This is useful for\nproving `order_of y = z` as it reduces the problem to proving `order_of y ∣ z` and `z ∣ order_of y`.\n\n4. `pow mul a m n` states `a ^ (m * n) = (a ^ m) ^ n`.\n\n5. `nat.mul_dvd_mul_iff_left` states that if `0 < a`, then `a * b ∣ a * c ↔ b ∣ c`.\n\n6. `inv_pow a n` states that `a⁻¹ ^ n = (a ^ n)⁻¹`.\n\n7. `one_inv` states that `1⁻¹ = 1`.\n\n8. `inv_inv a` states that `(a⁻¹)⁻¹ = a`.\n\n9. `conj_pow` states that `(a * b * a⁻¹) ^ i = a * (b ^ i) * a⁻¹`.\n\n-/\n\nlemma order_of_pow_of_order_of_eq_mul \n (x : G) (s t : ℕ) (h : order_of x = s * t) (hspos : s > 0) (htpos : t > 0) :\n order_of (x ^ s) = t :=\nsorry\n\nlemma order_of_inv (x : G) : order_of x⁻¹ = order_of x :=\nsorry\n\nlemma order_of_conj (x y : G) : order_of x = order_of (y * x * y⁻¹) :=\nsorry\n\nlemma order_comm (a b : G) : order_of (a * b) = order_of (b * a) :=\nsorry\n\nopen_locale classical\n\nlemma exists_even_order_of_of_even_card [fintype G] (h : even (fintype.card G)) :\n ∃ x : G, even (order_of x) := sorry\n\n\n", "meta": {"author": "gihanmarasingha", "repo": "mth1001_summer_2021", "sha": "fd168488a238c2e01800ebeeff2809adb3fa88b5", "save_path": "github-repos/lean/gihanmarasingha-mth1001_summer_2021", "path": "github-repos/lean/gihanmarasingha-mth1001_summer_2021/mth1001_summer_2021-fd168488a238c2e01800ebeeff2809adb3fa88b5/src/groupTheory/basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9416541577509314, "lm_q2_score": 0.905989819748845, "lm_q1q2_score": 0.8531290806465168}}
{"text": "/-\n6. (a) Find an onto function from ℕ to ℤ.\n(b) Find a 1-1 function from ℤ to ℕ.\n-/\n\nimport tactic\n\nopen function\n\ndef f (n : ℕ) : ℤ := if 2 ∣ n then n / 2 else - (n+1) / 2 -- replace 37 with a surjective function\n\nlemma f_surj : surjective f :=\nbegin\n intro z,\n rcases lt_trichotomy z 0 with h1 | rfl | h3,\n { use (-2*z-1).nat_abs,\n unfold f,\n split_ifs,\n { exfalso,\n rw int.coe_nat_dvd_left.symm at h,\n simp only [int.coe_nat_bit0, nat.cast_one, neg_mul] at h,\n rw (show -(2 * z) - 1 = 2 * (- z - 1) + 1, by ring) at h,\n rw dvd_add_right at h,\n norm_num at h,\n apply dvd_mul_right, },\n { apply int.div_eq_of_eq_mul_left,\n norm_num,\n rw int.nat_abs_of_nonneg (show ((-2) * z - 1) ≥ 0, by linarith),\n ring, }, },\n { use 0,\n unfold f,\n split_ifs,\n simp only [nat.cast_zero, euclidean_domain.zero_div],\n exfalso,\n norm_num at h, },\n { use (2 * z).nat_abs,\n unfold f,\n split_ifs,\n { apply int.div_eq_of_eq_mul_left,\n norm_num,\n rw int.nat_abs_of_nonneg (show 2 * z ≥ 0, by linarith), \n ring, },\n exfalso,\n apply h,\n rw ← int.coe_nat_dvd_left,\n norm_num, },\nend\n\ndef g (z : ℤ) : ℕ := if 0 < z then 2 * z.nat_abs else 2 * z.nat_abs + 1 -- replace 37 with an injective function\n\nlemma g_inj : injective g :=\nbegin\n intros a b hab,\n rcases nat.even_or_odd (g a) with h1 | h2,\n { have ha : 0 < a,\n { by_contra,\n push_neg at h,\n unfold g at h1,\n rw [if_neg h.not_lt, even_iff_two_dvd, nat.dvd_add_right] at h1,\n norm_num at h1,\n apply dvd_mul_right, },\n have hgb : even (g b),\n { rw ← hab, exact h1 },\n have hb : 0 < b,\n { by_contra,\n push_neg at h,\n unfold g at hgb,\n rw [if_neg h.not_lt, even_iff_two_dvd, nat.dvd_add_right] at hgb,\n norm_num at hgb,\n apply dvd_mul_right, },\n unfold g at hab,\n simp only [if_pos ha, if_pos hb, mul_eq_mul_left_iff, bit0_eq_zero, nat.one_ne_zero, or_false] at hab,\n zify at hab,\n rwa [int.nat_abs_of_nonneg ha.le, int.nat_abs_of_nonneg hb.le] at hab, },\n { have ha : a ≤ 0,\n { by_contra,\n push_neg at h,\n unfold g at h2,\n rw [if_pos h, nat.odd_iff_not_even] at h2,\n apply h2,\n rw even_iff_two_dvd,\n apply dvd_mul_right, },\n have hgb : odd (g b),\n { rw ← hab, exact h2 },\n have hb : b ≤ 0,\n { by_contra,\n push_neg at h,\n unfold g at hgb,\n rw [if_pos h, nat.odd_iff_not_even] at hgb,\n apply hgb,\n rw even_iff_two_dvd,\n apply dvd_mul_right, },\n unfold g at hab,\n simp only [if_neg ha.not_lt, if_neg hb.not_lt, add_left_inj, mul_eq_mul_left_iff, bit0_eq_zero, nat.one_ne_zero, or_false] at hab,\n zify at hab,\n simpa [int.of_nat_nat_abs_of_nonpos ha, int.of_nat_nat_abs_of_nonpos hb] using hab, },\nend\n", "meta": {"author": "ImperialCollegeLondon", "repo": "m1fexplained_lean3", "sha": "570c9a3ff8cfc3805047f2796864516a49f51510", "save_path": "github-repos/lean/ImperialCollegeLondon-m1fexplained_lean3", "path": "github-repos/lean/ImperialCollegeLondon-m1fexplained_lean3/m1fexplained_lean3-570c9a3ff8cfc3805047f2796864516a49f51510/src/chapter19/exercises/exercise06.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9532750427013548, "lm_q2_score": 0.8947894696095782, "lm_q1q2_score": 0.8529804698507932}}
{"text": "/-\nCopyright (c) 2022 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor : Kevin Buzzard\n-/\n\nimport tactic -- imports all the Lean tactics\nimport data.real.basic -- the reals\n/-!\n\n# Sets in Lean, sheet 4 : making sets from predicates\n\nIf we define\n\n`def is_even (n : ℕ) : Prop := ∃ t, n = 2 * t`\n\nthen for `n` a natural, `is_even n` is a true-false statement,\ni.e., a proposition. This means that `is_even : ℕ → Prop` is\na function taking naturals to true-false statements (also known as\na \"predicate\" on naturals), so we should be able to make the subset\nof naturals where this predicate is true. In Lean the syntax for\nthis is\n\n`{ n : ℕ | is_even n }`\n\nThe big question you would need to know about sets constructed in this\nway is: how do you get from `t ∈ { n : ℕ | is_even n }` to `is_even t`?\nAnd the answer is that these are equal by definition.\n\nThe general case: if you have a type `X` and a predicate `P : X → Prop`\nthen the subset of `X` consisting of the terms where the predicate is\ntrue, is `{ x : X | P x }`, and the proof that `a ∈ { x : X | P x } ↔ P a` is `refl`.\nLet's check:\n-/\n\nlemma mem_def (X : Type) (P : X → Prop) (a : X) : a ∈ { x : X | P x } ↔ P a :=\nbegin\n refl\nend\n\n/-\n\nIf you want, you can `rw mem_def` instead.\n\n-/\n\nopen set\n\ndef is_even (n : ℕ) : Prop := ∃ t, n = 2 * t\n\n-- note that this is *syntactically* equal to `is_even : ℕ → Prop := λ n, ∃ t, n = 2 * t`\n-- but the way I've written it is perhaps easier to follow.\n\nexample : 74 ∈ {n : ℕ | is_even n} :=\nbegin\n change ∃ (t : ℕ), 74 = 2 * t,\n -- exact ⟨37, by norm_num⟩, -- works\n use 37,\n norm_num,\nend\n\n-- Let's develop a theory of even real numbers\ndef real.is_even (r : ℝ) := ∃ t : ℝ, r = 2 * t\n\n-- Turns out it's not interesting\nexample : ∀ x , x ∈ {r : ℝ | real.is_even r} :=\nbegin\n intro x,\n use x/2,\n ring,\nend\n\n-- likewise, the theory of positive negative real numbers is not interesting\nexample : ∀ x, x ∉ {r : ℝ | 0 < r ∧ r < 0} :=\nbegin\n -- quick way to change the type of `hx` to something definitionally equal\n rintro x (hx : 0 < x ∧ x < 0),\n -- `linarith` is happy to use ∧ hypotheses\n linarith,\nend\n\n", "meta": {"author": "ImperialCollegeLondon", "repo": "formalising-mathematics-2022", "sha": "af5f176b3b881b7bc0ae89b55befe48c9d4ab790", "save_path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2022", "path": "github-repos/lean/ImperialCollegeLondon-formalising-mathematics-2022/formalising-mathematics-2022-af5f176b3b881b7bc0ae89b55befe48c9d4ab790/src/solutions/section05sets/sheet4.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9441768580799912, "lm_q2_score": 0.9032941988938413, "lm_q1q2_score": 0.8528694786334697}}
{"text": "/-\nCopyright (c) 2022 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor : Kevin Buzzard\n-/\n\nimport tactic -- imports all the Lean tactics\n\n/-!\n\n# Logic in Lean, example sheet 4 : \"and\" (`∧`)\n\nWe learn about how to manipulate `P ∧ Q` in Lean.\n\n## Tactics\n\nYou'll need to know about the tactics from the previous sheets,\nand also the following tactics:\n\n* `cases`\n* `split`\n\n-/\n\n-- Throughout this sheet, `P`, `Q` and `R` will denote propositions.\n\nvariables (P Q R : Prop)\n\nexample : P ∧ Q → P :=\nbegin\n intro hPQ,\n exact hPQ.left,\nend\n\nexample : P ∧ Q → P :=\nbegin\n rintro ⟨hP, hQ⟩,\n exact hP,\nend\n\n-- Alternative solution\nexample : P ∧ Q → P :=\nbegin\n intro hPQ,\n cases hPQ with hP _,\n exact hP,\nend\n\nexample : P ∧ Q → Q :=\nbegin\n intro hPQ,\n cases hPQ with _ hQ,\n exact hQ,\nend\n\nexample : (P → Q → R) → (P ∧ Q → R) :=\nbegin\n intros hPQR hPQ,\n exact hPQR hPQ.left hPQ.right,\nend\n\nexample : P → Q → P ∧ Q :=\nbegin\n intros hP hQ,\n exact ⟨hP, hQ⟩,\nend\n\nexample : P → Q → P ∧ Q :=\nbegin\n intros hP hQ,\n split,\n exact hP, -- DON'T DO THIS! Will dock marks. After split, put each goal in { } or exacts if it's this simple\n exact hQ,\nend\n\n/-- `∧` is symmetric -/\nexample : P ∧ Q → Q ∧ P :=\nbegin\n intro hPQ,\n exact ⟨hPQ.right, hPQ.left⟩,\nend\n\nexample : P ∧ Q → Q ∧ P :=\nbegin\n rintro ⟨hP, hQ⟩,\n split,\n exacts [hQ, hP],\nend\n\nexample : P → P ∧ true :=\nbegin\n intro hP,\n split, -- gross\n exact hP,\n triv,\nend\n\nexample : P → P ∧ true :=\nbegin\n intro hP,\n exact ⟨hP, true.intro⟩,\nend\n\nexample : P → P ∧ true :=\nλ hP, ⟨hP, true.intro⟩ -- yum\n\nexample : false → P ∧ false :=\nbegin\n intro hF,\n split,\n exfalso,\n exacts [hF, hF],\nend\n\n-- TODO: improve this\nexample : false → P ∧ false :=\nbegin\n intro hF,\n split, -- how do I do the thing where you solve part of a goal\n exfalso, -- and then the rest becomes the rest of the goal?\n { exact hF, }, \n { exact hF, },\nend\n\n/-- `∧` is transitive -/\nexample : (P ∧ Q) → (Q ∧ R) → (P ∧ R) :=\nbegin\n intros hPQ hQR,\n exact ⟨hPQ.left, hQR.right⟩,\nend\n\nexample : ((P ∧ Q) → R) → (P → Q → R) :=\nbegin\n intros hPQR hP hQ,\n exact hPQR ⟨hP, hQ⟩,\nend\n\n\n\n", "meta": {"author": "ineswright", "repo": "formalising-maths-2023", "sha": "d29dee07d9d55e96bcfd3d783d6f56943d61d8b6", "save_path": "github-repos/lean/ineswright-formalising-maths-2023", "path": "github-repos/lean/ineswright-formalising-maths-2023/formalising-maths-2023-d29dee07d9d55e96bcfd3d783d6f56943d61d8b6/src/section01logic/sheet4.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9372107843878721, "lm_q2_score": 0.9099070072595894, "lm_q1q2_score": 0.852774659993781}}
{"text": "import Mathlib.Init.Data.Nat.Basic\n/-!\n## Pattern Matching Expressions\n\nPattern matching is possible not only at the top level of a `def` command but also\ndeeply within terms, via a `match` expression. The construct has the following general syntax:\n\n```lean\nmatch term₁, . . ., termm with\n| pattern₁₁, . . ., pattern₁ₘ => result₁\n.\n.\n.\n| patternn₁, . . ., patternₙₘ => resultₙ\n```\n\nThe patterns may contain variables, constructors, and nameless\nplaceholders (_). The `resultᵢ` expressions may refer to the variables introduced\nin the corresponding patterns.\n\nThe following function definition demonstrates the syntax of pattern matching\nwithin expressions:\n-/\ndef bcount {α : Type} (p : α → Bool) : List α → ℕ\n| [] => 0\n| (x :: xs) =>\n match p x with\n | true => (bcount p xs) + 1\n | false => bcount p xs\n\n#eval bcount (λ x : Nat => x >= 5) (List.range 10) -- 5\n\n/-!\nThe `bcount` function counts the number of elements in a list that satisfy the given\npredicate `p`. The predicate’s codomain is `Bool`. As a general rule, we will use type\n`Bool`, of Booleans, within programs and use the type `Prop`, of propositions, when\nstating properties of programs.\n\nThe connectives are called `or` (infix: `||`), `and` (infix: `&&`), and `not` (`¬`) or (`!`).\n\nBUGBUG: the book also adds `xor` but I couldn't find that in Lean, I only found HXor.hXor?\n\nHere's an example that shows how to match on two variables:\n-/\ndef extract {α : Type} : List α → ℕ → List α\n | _, 0 => []\n | [], _ => []\n | (x :: xs), i + 1 => x :: (extract xs i)\n\n#eval extract (List.range 10) 3 -- [0, 1, 2]\n\n/-!\nNotice that the variables are extracted in order from the return type.\nThis example also shows how to use an induction technique matching `i + 1` so that\nthe count is decremented by one on each recursive call.\n\nYou can write this with full match expression if you prefer to name the arguments for\ndocumentation reasons:\n-/\ndef extract₂ {α : Type} (xs : List α) (count : ℕ) : List α :=\n match xs, count with\n | _, 0 => []\n | [], _ => []\n | (x :: xs), i + 1 => x :: (extract xs i)\n/-!\nWe cannot match on a proposition of type `Prop`, but we can use if–then–else\ninstead. For example, the min operator on natural numbers operator can be defined as follows:\n\n-/\ndef minimum (a b : ℕ) : ℕ :=\n if a ≤ b then a else b\n\n#eval minimum 5 9 -- 5\n\n/-!\nThis works only for propositions that are decidable (i.e., executable). This is the\ncase for `≤`: Given concrete values for the arguments, such as `35` and `49`, Lean can\nreduce `35 ≤ 49` to true. Lean keeps track of decidability using a mechanism called\ntype classes, which will be explained below.\n-/", "meta": {"author": "lovettchris", "repo": "hglv", "sha": "339f0b10f4b4a2b1e53e2b29532003a5ea4d9c9b", "save_path": "github-repos/lean/lovettchris-hglv", "path": "github-repos/lean/lovettchris-hglv/hglv-339f0b10f4b4a2b1e53e2b29532003a5ea4d9c9b/FunctionalProgramming/PatternMatching.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9343951661947455, "lm_q2_score": 0.91243616285804, "lm_q1q2_score": 0.8525759400358341}}
{"text": "/-\nCopyright (c) 2020 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Kevin Buzzard\n-/\n\nimport tactic\nimport data.nat.modeq\n\n/-!\n# IMO 1964 Q1\n\n(a) Find all positive integers $n$ for which $2^n-1$ is divisible by $7$.\n\n(b) Prove that there is no positive integer $n$ for which $2^n+1$ is divisible by $7$.\n\nWe define a predicate for the solutions in (a), and prove that it is the set of positive\nintegers which are a multiple of 3.\n-/\n\n/-!\n## Intermediate lemmas\n-/\n\nopen nat.modeq\n\nlemma two_pow_three_mul_mod_seven (m : ℕ) : 2 ^ (3 * m) ≡ 1 [MOD 7] :=\nbegin\n rw pow_mul,\n have h : 8 ≡ 1 [MOD 7] := modeq_of_dvd (by {use -1, norm_num }),\n convert modeq_pow _ h,\n simp,\nend\n\nlemma two_pow_three_mul_add_one_mod_seven (m : ℕ) : 2 ^ (3 * m + 1) ≡ 2 [MOD 7] :=\nbegin\n rw pow_add,\n exact modeq_mul (two_pow_three_mul_mod_seven m) (show 2 ^ 1 ≡ 2 [MOD 7], by refl),\nend\n\nlemma two_pow_three_mul_add_two_mod_seven (m : ℕ) : 2 ^ (3 * m + 2) ≡ 4 [MOD 7] :=\nbegin\n rw pow_add,\n exact modeq_mul (two_pow_three_mul_mod_seven m) (show 2 ^ 2 ≡ 4 [MOD 7], by refl),\nend\n\n/-!\n## The question\n-/\n\ndef problem_predicate (n : ℕ) : Prop := 7 ∣ 2 ^ n - 1\n\nlemma aux (n : ℕ) : problem_predicate n ↔ 2 ^ n ≡ 1 [MOD 7] :=\nbegin\n rw nat.modeq.comm,\n apply (modeq_iff_dvd' _).symm,\n apply nat.one_le_pow'\nend\n\ntheorem imo1964_q1a (n : ℕ) (hn : 0 < n) : problem_predicate n ↔ 3 ∣ n :=\nbegin\n rw aux,\n split,\n { intro h,\n let t := n % 3,\n rw [(show n = 3 * (n / 3) + t, from (nat.div_add_mod n 3).symm)] at h,\n have ht : t < 3 := nat.mod_lt _ dec_trivial,\n interval_cases t with hr; rw hr at h,\n { exact nat.dvd_of_mod_eq_zero hr },\n { exfalso,\n have nonsense := (two_pow_three_mul_add_one_mod_seven _).symm.trans h,\n rw modeq_iff_dvd at nonsense,\n norm_num at nonsense },\n { exfalso,\n have nonsense := (two_pow_three_mul_add_two_mod_seven _).symm.trans h,\n rw modeq_iff_dvd at nonsense,\n norm_num at nonsense } },\n { rintro ⟨m, rfl⟩,\n apply two_pow_three_mul_mod_seven }\nend\n\ntheorem imo1964_q1b (n : ℕ) : ¬ (7 ∣ 2 ^ n + 1) :=\nbegin\n let t := n % 3,\n rw [← modeq_zero_iff, (show n = 3 * (n / 3) + t, from (nat.div_add_mod n 3).symm)],\n have ht : t < 3 := nat.mod_lt _ dec_trivial,\n interval_cases t with hr; rw hr,\n { rw add_zero,\n intro h,\n have := h.symm.trans (modeq_add (two_pow_three_mul_mod_seven _) (nat.modeq.refl _)),\n rw modeq_iff_dvd at this,\n norm_num at this },\n { intro h,\n have := h.symm.trans (modeq_add (two_pow_three_mul_add_one_mod_seven _) (nat.modeq.refl _)),\n rw modeq_iff_dvd at this,\n norm_num at this },\n { intro h,\n have := h.symm.trans (modeq_add (two_pow_three_mul_add_two_mod_seven _) (nat.modeq.refl _)),\n rw modeq_iff_dvd at this,\n norm_num at this },\nend\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/archive/imo/imo1964_q1.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9603611586300241, "lm_q2_score": 0.8872045922259089, "lm_q1q2_score": 0.852036830131952}}
{"text": "/- ∀ introduction\n\nSuppose P and Q are arbitrary propositions. \nTo prove a proposition given in the form of\na universal generalization, ∀ (p : P), Q,\nassume that P is proved and that you have an\narbitrary but specific proof p' of P, and\nshow that, in this context, Q must be true,\nby showing you can construct a proof of it.\n\nHere's an example. The proposition says that\nfor all propositions, P and Q, if P ∨ Q is\nproven, then Q ∨ P can be proven. To prove it,\n*assume* P and Q are arbitrary but specific,\npropositions, then, in this context, prove\nthe rest (that P ∨ Q → Q ∨ P).\n\nIn short, to prove ∀ (p : P), Q, assume that\nyou have a proof of P and show that in this\ncontext you can then prove Q. This is the rule\nof ∀ introduction.\n-/\n\ntheorem or_commutative : ∀ {P Q : Prop}, P ∨ Q → Q ∨ P :=\nλ (P Q : Prop), -- assume arbitrary values of P, Q \n -- in this context, prove the rest\n -- for completeness, here's the rest\n λ (porq : P ∨ Q), -- by → introduction\n match porq with -- by case analysis\n | or.inl p := or.inr p -- true in this case\n | or.inr q := or.inl q -- true in other case\n end -- QED\n\n\n/- ∀ elimination\n\nSuppose that in the set of proofs you already have \nor that you have assumed, you have a proof, all_p_q,\nof ∀ (p : P), Q. The rule of ∀ elimination allows\nyou to *apply* this generalization to any specific\nvalue, p' : P, to obtain a corresponding proof of Q.\n\nTo put it simply, once you've proved a *general*\ntheorem, you can apply that result to any particular\ninstance. For example, we just proved that the ∨ \nconnective of predicate logic is commutative, *in\ngeneral*. So now suppose we're given a proof of\n1=1 ∨ 2=3 and what we need to prove is 2 = 3 ∨ 1 = 1.\nWe do *not* need to prove this special case from \nscratch; rather we can simply *apply* the theorem\nand be done with it. \n-/\n\n-- First, let's get a proof of (1 = 1 ∨ 2 = 3)\n\nlemma l1 : 1=1 ∨ 2=3 := or.inl rfl\n\n-- Just apply theorem to get proof of 2 = 3 ∨ 1 = 1\n#check or_commutative l1\n\nexample : 2 = 3 ∨ 1 = 1 := or_commutative l1\n\n/-\nIt would be hard to overstate the importance of the\nidea of ∀ elimination. It's just a fancy way to say\nthat if you've proven a general theorem, you can use\nit immediately without having to re-prove anything to\nprove any particular special case that matches the\nconditions that it demands (here that P and Q are\nany propositions and that you have a proof of P ∨ Q).\n\nYou can think of a theorem as a subroutine that you \nhave already implemented that you can apply whenever\nyou need it! This idea is what allows mathematics to\nwork, so not every proof has to start from scratch.\n\nIn the constructive logic of Lean, indeed a proof of\n∀ (p : P), Q, *is* a function: one that, if given any\nproof (or equivalently, a value) of P returns a proof\n(a value) of Q.\n-/\n\n", "meta": {"author": "kevinsullivan", "repo": "dm.s20", "sha": "6f90ecb3881c602cdd1e3f12aad458bcdabd250a", "save_path": "github-repos/lean/kevinsullivan-dm.s20", "path": "github-repos/lean/kevinsullivan-dm.s20/dm.s20-6f90ecb3881c602cdd1e3f12aad458bcdabd250a/instructor/predicate_logic/intro_and_elim_rules/forall.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9390248165754386, "lm_q2_score": 0.9073122132152182, "lm_q1q2_score": 0.8519886845910755}}
{"text": "-- BOTH:\nimport data.real.basic\n\n/- TEXT:\n.. _implication_and_the_universal_quantifier:\n\nImplication and the Universal Quantifier\n----------------------------------------\n\nConsider the statement after the ``#check``:\nTEXT. -/\n-- QUOTE:\n#check ∀ x : ℝ, 0 ≤ x → abs x = x\n-- QUOTE.\n\n/- TEXT:\nIn words, we would say \"for every real number ``x``, if ``0 ≤ x`` then\nthe absolute value of ``x`` equals ``x``\".\nWe can also have more complicated statements like:\nTEXT. -/\n-- QUOTE:\n#check ∀ x y ε : ℝ, 0 < ε → ε ≤ 1 → abs x < ε → abs y < ε → abs (x * y) < ε\n-- QUOTE.\n\n/- TEXT:\nIn words, we would say \"for every ``x``, ``y``, and ``ε``,\nif ``0 < ε ≤ 1``, the absolute value of ``x`` is less than ``ε``,\nand the absolute value of ``y`` is less than ``ε``,\nthen the absolute value of ``x * y`` is less than ``ε``.\"\nIn Lean, in a sequence of implications there are\nimplicit parentheses grouped to the right.\nSo the expression above means\n\"if ``0 < ε`` then if ``ε ≤ 1`` then if ``abs x < ε`` ...\"\nAs a result, the expression says that all the\nassumptions together imply the conclusion.\n\nYou have already seen that even though the universal quantifier\nin this statement\nranges over objects and the implication arrows introduce hypotheses,\nLean treats the two in very similar ways.\nIn particular, if you have proved a theorem of that form,\nyou can apply it to objects and hypotheses in the same way:\nTEXT. -/\n-- QUOTE:\nlemma my_lemma : ∀ x y ε : ℝ,\n 0 < ε → ε ≤ 1 → abs x < ε → abs y < ε → abs (x * y) < ε :=\nsorry\n\nsection\n variables a b δ : ℝ\n variables (h₀ : 0 < δ) (h₁ : δ ≤ 1)\n variables (ha : abs a < δ) (hb : abs b < δ)\n\n #check my_lemma a b δ\n #check my_lemma a b δ h₀ h₁\n #check my_lemma a b δ h₀ h₁ ha hb\nend\n-- QUOTE.\n\n/- TEXT:\nYou have also already seen that it is common in Lean\nto use curly brackets to make quantified variables implicit\nwhen they can be inferred from subsequent hypotheses.\nWhen we do that, we can just apply a lemma to the hypotheses without\nmentioning the objects.\nTEXT. -/\n-- QUOTE:\nlemma my_lemma2 : ∀ {x y ε : ℝ},\n 0 < ε → ε ≤ 1 → abs x < ε → abs y < ε → abs (x * y) < ε :=\nsorry\n\nsection\n variables a b δ : ℝ\n variables (h₀ : 0 < δ) (h₁ : δ ≤ 1)\n variables (ha : abs a < δ) (hb : abs b < δ)\n\n #check my_lemma2 h₀ h₁ ha hb\nend\n-- QUOTE.\n\n/- TEXT:\nAt this stage, you also know that if you use\nthe ``apply`` tactic to apply ``my_lemma``\nto a goal of the form ``abs (a * b) < δ``,\nyou are left with new goals that require you to prove\neach of the hypotheses.\n\n.. index:: intros, tactics ; intros\n\nTo prove a statement like this, use the ``intros`` tactic.\nTake a look at what it does in this example:\nTEXT. -/\n-- QUOTE:\nlemma my_lemma3 : ∀ {x y ε : ℝ},\n 0 < ε → ε ≤ 1 → abs x < ε → abs y < ε → abs (x * y) < ε :=\nbegin\n intros x y ε epos ele1 xlt ylt,\n sorry\nend\n-- QUOTE.\n\n/- TEXT:\nWe can use any names we want for the universally quantified variables;\nthey do not have to be ``x``, ``y``, and ``ε``.\nNotice that we have to introduce the variables\neven though they are marked implicit:\nmaking them implicit means that we leave them out when\nwe write an expression *using* ``my_lemma``,\nbut they are still an essential part of the statement\nthat we are proving.\nAfter the ``intros`` command,\nthe goal is what it would have been at the start if we\nlisted all the variables and hypotheses *before* the colon,\nas we did in the last section.\nIn a moment, we will see why it is sometimes necessary to\nintroduce variables and hypotheses after the proof begins.\n\nTo help you prove the lemma, we will start you off:\nTEXT. -/\n-- QUOTE:\nlemma my_lemma4 : ∀ {x y ε : ℝ},\n 0 < ε → ε ≤ 1 → abs x < ε → abs y < ε → abs (x * y) < ε :=\nbegin\n intros x y ε epos ele1 xlt ylt,\n calc\n abs (x * y) = abs x * abs y : sorry\n ... ≤ abs x * ε : sorry\n ... < 1 * ε : sorry\n ... = ε : sorry\nend\n-- QUOTE.\n\n-- OMIT:\n/- TODO : remember to introduce ``suffices`` eventually\n\n We have introduced another new tactic here:\n ``suffices`` works like ``have`` in reverse,\n asking you to prove the goal using the\n stated fact,\n and then leaving you the new goal of proving that fact. -/\n\n/- TEXT:\nFinish the proof using the theorems\n``abs_mul``, ``mul_le_mul``, ``abs_nonneg``,\n``mul_lt_mul_right``, and ``one_mul``.\nRemember that you can find theorems like these using\ntab completion.\nRemember also that you can use ``.mp`` and ``.mpr``\nor ``.1`` and ``.2`` to extract the two directions\nof an if-and-only-if statement.\n\nUniversal quantifiers are often hidden in definitions,\nand Lean will unfold definitions to expose them when necessary.\nFor example, let's define two predicates,\n``fn_ub f a`` and ``fn_lb f a``,\nwhere ``f`` is a function from the real numbers to the real\nnumbers and ``a`` is a real number.\nThe first says that ``a`` is an upper bound on the\nvalues of ``f``,\nand the second says that ``a`` is a lower bound\non the values of ``f``.\nBOTH: -/\n-- QUOTE:\ndef fn_ub (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a\ndef fn_lb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x\n-- QUOTE.\n\n/- TEXT:\n.. index:: lambda abstraction\n\nIn the next example, ``λ x, f x + g x`` is a name for the\nfunction that maps ``x`` to ``f x + g x``.\nComputer scientists refer to this as \"lambda abstraction,\"\nwhereas a mathematician might describe it as the function\n:math:`x \\mapsto f(x) + g(x)`.\nBOTH: -/\nsection\nvariables (f g : ℝ → ℝ) (a b : ℝ)\n\n-- EXAMPLES:\n-- QUOTE:\nexample (hfa : fn_ub f a) (hgb : fn_ub g b) :\n fn_ub (λ x, f x + g x) (a + b) :=\nbegin\n intro x,\n dsimp,\n apply add_le_add,\n apply hfa,\n apply hgb\nend\n-- QUOTE.\n\n/- TEXT:\n.. index:: dsimp, tactics ; dsimp, change, tactics ; change\n\nApplying ``intro`` to the goal ``fn_ub (λ x, f x + g x) (a + b)``\nforces Lean to unfold the definition of ``fn_ub``\nand introduce ``x`` for the universal quantifier.\nThe goal is then ``(λ (x : ℝ), f x + g x) x ≤ a + b``.\nBut applying ``(λ x, f x + g x)`` to ``x`` should result in ``f x + g x``,\nand the ``dsimp`` command performs that simplification.\n(The \"d\" stands for \"definitional.\")\nYou can delete that command and the proof still works;\nLean would have to perform that contraction anyhow to make\nsense of the next ``apply``.\nThe ``dsimp`` command simply makes the goal more readable\nand helps us figure out what to do next.\nAnother option is to use the ``change`` tactic\nby writing ``change f x + g x ≤ a + b``.\nThis helps make the proof more readable,\nand gives you more control over how the goal is transformed.\n\nThe rest of the proof is routine.\nThe last two ``apply`` commands force Lean to unfold the definitions\nof ``fn_ub`` in the hypotheses.\nTry carrying out similar proofs of these:\nTEXT. -/\n-- QUOTE:\nexample (hfa : fn_lb f a) (hgb : fn_lb g b) :\n fn_lb (λ x, f x + g x) (a + b) :=\nsorry\n\nexample (nnf : fn_lb f 0) (nng : fn_lb g 0) :\n fn_lb (λ x, f x * g x) 0 :=\nsorry\n\nexample (hfa : fn_ub f a) (hfb : fn_ub g b)\n (nng : fn_lb g 0) (nna : 0 ≤ a) :\n fn_ub (λ x, f x * g x) (a * b) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (hfa : fn_lb f a) (hgb : fn_lb g b) :\n fn_lb (λ x, f x + g x) (a + b) :=\nbegin\n intro x,\n apply add_le_add,\n apply hfa,\n apply hgb\nend\n\nexample (nnf : fn_lb f 0) (nng : fn_lb g 0) :\n fn_lb (λ x, f x * g x) 0 :=\nbegin\n intro x,\n apply mul_nonneg,\n apply nnf,\n apply nng\nend\n\nexample (hfa : fn_ub f a) (hfb : fn_ub g b)\n (nng : fn_lb g 0) (nna : 0 ≤ a) :\n fn_ub (λ x, f x * g x) (a * b) :=\nbegin\n intro x,\n apply mul_le_mul,\n apply hfa,\n apply hfb,\n apply nng,\n apply nna\nend\n\n-- BOTH:\nend\n\n/- TEXT:\nEven though we have defined ``fn_ub`` and ``fn_lb`` for functions\nfrom the reals to the reals,\nyou should recognize that the definitions and proofs are much\nmore general.\nThe definitions make sense for functions between any two types\nfor which there is a notion of order on the codomain.\nChecking the type of the theorem ``add_le_add`` shows that it holds\nof any structure that is an \"ordered additive commutative monoid\";\nthe details of what that means don't matter now,\nbut it is worth knowing that the natural numbers, integers, rationals,\nand real numbers are all instances.\nSo if we prove the theorem ``fn_ub_add`` at that level of generality,\nit will apply in all these instances.\nTEXT. -/\nsection\n-- QUOTE:\nvariables {α : Type*} {R : Type*} [ordered_cancel_add_comm_monoid R]\n\n#check @add_le_add\n\ndef fn_ub' (f : α → R) (a : R) : Prop := ∀ x, f x ≤ a\n\ntheorem fn_ub_add {f g : α → R} {a b : R}\n (hfa : fn_ub' f a) (hgb : fn_ub' g b) :\n fn_ub' (λ x, f x + g x) (a + b) :=\nλ x, add_le_add (hfa x) (hgb x)\n-- QUOTE.\nend\n\n/- TEXT:\nYou have already seen square brackets like these in\nSection :numref:`proving_identities_in_algebraic_structures`,\nthough we still haven't explained what they mean.\nFor concreteness, we will stick to the real numbers\nfor most of our examples,\nbut it is worth knowing that mathlib contains definitions and theorems\nthat work at a high level of generality.\n\n.. index:: monotone function\n\nFor another example of a hidden universal quantifier,\nmathlib defines a predicate ``monotone``,\nwhich says that a function is nondecreasing in its arguments:\nTEXT. -/\n-- QUOTE:\nexample (f : ℝ → ℝ) (h : monotone f) :\n ∀ {a b}, a ≤ b → f a ≤ f b := h\n-- QUOTE.\n\n/- TEXT:\nProving statements about monotonicity\ninvolves using ``intros`` to introduce two variables,\nsay, ``a`` and ``b``, and the hypothesis ``a ≤ b``.\nTo *use* a monotonicity hypothesis,\nyou can apply it to suitable arguments and hypotheses,\nand then apply the resulting expression to the goal.\nOr you can apply it to the goal and let Lean help you\nwork backwards by displaying the remaining hypotheses\nas new subgoals.\nBOTH: -/\nsection\nvariables (f g : ℝ → ℝ)\n\n-- EXAMPLES:\n-- QUOTE:\nexample (mf : monotone f) (mg : monotone g) :\n monotone (λ x, f x + g x) :=\nbegin\n intros a b aleb,\n apply add_le_add,\n apply mf aleb,\n apply mg aleb\nend\n-- QUOTE.\n\n/- TEXT:\nWhen a proof is this short, it is often convenient\nto give a proof term instead.\nTo describe a proof that temporarily introduces objects\n``a`` and ``b`` and a hypothesis ``aleb``,\nLean uses the notation ``λ a b aleb, ...``.\nThis is analogous to the way that a lambda abstraction\nlike ``λ x, x^2`` describes a function\nby temporarily naming an object, ``x``,\nand then using it to describe a value.\nSo the ``intros`` command in the previous proof\ncorresponds to the lambda abstraction in the next proof term.\nThe ``apply`` commands then correspond to building\nthe application of the theorem to its arguments.\nTEXT. -/\n-- QUOTE:\nexample (mf : monotone f) (mg : monotone g) :\n monotone (λ x, f x + g x) :=\nλ a b aleb, add_le_add (mf aleb) (mg aleb)\n-- QUOTE.\n\n/- TEXT:\nHere is a useful trick: if you start writing\nthe proof term ``λ a b aleb, _`` using\nan underscore where the rest of the\nexpression should go,\nLean will flag an error,\nindicating that it can't guess the value of that expression.\nIf you check the Lean Goal window in VS Code or\nhover over the squiggly error marker,\nLean will show you the goal that the remaining\nexpression has to solve.\n\nTry proving these, with either tactics or proof terms:\nTEXT. -/\n-- QUOTE:\nexample {c : ℝ} (mf : monotone f) (nnc : 0 ≤ c) :\n monotone (λ x, c * f x) :=\nsorry\n\nexample (mf : monotone f) (mg : monotone g) :\n monotone (λ x, f (g x)) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample {c : ℝ} (mf : monotone f) (nnc : 0 ≤ c) :\n monotone (λ x, c * f x) :=\nbegin\n intros a b aleb,\n apply mul_le_mul_of_nonneg_left _ nnc,\n apply mf aleb\nend\n\nexample {c : ℝ} (mf : monotone f) (nnc : 0 ≤ c) :\n monotone (λ x, c * f x) :=\nλ a b aleb, mul_le_mul_of_nonneg_left (mf aleb) nnc\n\nexample (mf : monotone f) (mg : monotone g) :\n monotone (λ x, f (g x)) :=\nbegin\n intros a b aleb,\n apply mf,\n apply mg,\n apply aleb\nend\n\nexample (mf : monotone f) (mg : monotone g) :\n monotone (λ x, f (g x)) :=\nλ a b aleb, mf (mg aleb)\n\n/- TEXT:\nHere are some more examples.\nA function :math:`f` from :math:`\\Bbb R` to\n:math:`\\Bbb R` is said to be *even* if\n:math:`f(-x) = f(x)` for every :math:`x`,\nand *odd* if :math:`f(-x) = -f(x)` for every :math:`x`.\nThe following example defines these two notions formally\nand establishes one fact about them.\nYou can complete the proofs of the others.\nTEXT. -/\n-- QUOTE:\n-- BOTH:\ndef fn_even (f : ℝ → ℝ) : Prop := ∀ x, f x = f (-x)\ndef fn_odd (f : ℝ → ℝ) : Prop := ∀ x, f x = - f (-x)\n\n-- EXAMPLES:\nexample (ef : fn_even f) (eg : fn_even g) : fn_even (λ x, f x + g x) :=\nbegin\n intro x,\n calc\n (λ x, f x + g x) x = f x + g x : rfl\n ... = f (-x) + g (-x) : by rw [ef, eg]\nend\n\nexample (of : fn_odd f) (og : fn_odd g) : fn_even (λ x, f x * g x) :=\nsorry\n\nexample (ef : fn_even f) (og : fn_odd g) : fn_odd (λ x, f x * g x) :=\nsorry\n\nexample (ef : fn_even f) (og : fn_odd g) : fn_even (λ x, f (g x)) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (of : fn_odd f) (og : fn_odd g) : fn_even (λ x, f x * g x) :=\nbegin\n intro x,\n calc\n (λ x, f x * g x) x = f x * g x : rfl\n ... = f (- x) * g (- x) : by rw [of, og, neg_mul_neg]\nend\n\nexample (ef : fn_even f) (og : fn_odd g) : fn_odd (λ x, f x * g x) :=\nbegin\n intro x,\n dsimp,\n rw [ef, og, neg_mul_eq_mul_neg]\nend\n\nexample (ef : fn_even f) (og : fn_odd g) : fn_even (λ x, f (g x)) :=\nbegin\n intro x,\n dsimp,\n rw [og, ←ef]\nend\n\n-- BOTH:\nend\n\n/- TEXT:\n.. index:: erw, tactics ; erw\n\nThe first proof can be shortened using ``dsimp`` or ``change``\nto get rid of the lambda.\nBut you can check that the subsequent ``rw`` won't work\nunless we get rid of the lambda explicitly,\nbecause otherwise it cannot find the patterns ``f x`` and ``g x``\nin the expression.\nContrary to some other tactics, ``rw`` operates on the syntactic level,\nit won't unfold definitions or apply reductions for you\n(it has a variant called ``erw`` that tries a little harder in this\ndirection, but not much harder).\n\nYou can find implicit universal quantifiers all over the place,\nonce you know how to spot them.\nMathlib includes a good library for rudimentary set theory.\nLean's logical foundation imposes the restriction that when\nwe talk about sets, we are always talking about sets of\nelements of some type. If ``x`` has type ``α`` and ``s`` has\ntype ``set α``, then ``x ∈ s`` is a proposition that\nasserts that ``x`` is an element of ``s``.\nIf ``s`` and ``t`` are of type ``set α``,\nthen the subset relation ``s ⊆ t`` is defined to mean\n``∀ {x : α}, x ∈ s → x ∈ t``.\nThe variable in the quantifier is marked implicit so that\ngiven ``h : s ⊆ t`` and ``h' : x ∈ s``,\nwe can write ``h h'`` as justification for ``x ∈ t``.\nThe following example provides a tactic proof and a proof term\njustifying the reflexivity of the subset relation,\nand asks you to do the same for transitivity.\nTEXT. -/\n-- BOTH:\nsection\n-- QUOTE:\nvariables {α : Type*} (r s t : set α)\n\n-- EXAMPLES:\nexample : s ⊆ s :=\nby { intros x xs, exact xs }\n\ntheorem subset.refl : s ⊆ s := λ x xs, xs\n\ntheorem subset.trans : r ⊆ s → s ⊆ t → r ⊆ t :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample : r ⊆ s → s ⊆ t → r ⊆ t :=\nbegin\n intros rsubs ssubt x xr,\n apply ssubt,\n apply rsubs,\n apply xr\nend\n\ntheorem subset.transαα : r ⊆ s → s ⊆ t → r ⊆ t :=\nλ rsubs ssubt x xr, ssubt (rsubs xr)\n\n-- BOTH:\nend\n\n/- TEXT:\nJust as we defined ``fn_ub`` for functions,\nwe can define ``set_ub s a`` to mean that ``a``\nis an upper bound on the set ``s``,\nassuming ``s`` is a set of elements of some type that\nhas an order associated with it.\nIn the next example, we ask you to prove that\nif ``a`` is a bound on ``s`` and ``a ≤ b``,\nthen ``b`` is a bound on ``s`` as well.\nTEXT. -/\n-- BOTH:\nsection\n\n-- QUOTE:\nvariables {α : Type*} [partial_order α]\nvariables (s : set α) (a b : α)\n\ndef set_ub (s : set α) (a : α) := ∀ x, x ∈ s → x ≤ a\n\n-- EXAMPLES:\nexample (h : set_ub s a) (h' : a ≤ b) : set_ub s b :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (h : set_ub s a) (h' : a ≤ b) : set_ub s b :=\nbegin\n intros x xs,\n apply le_trans (h x xs) h'\nend\n\nexample (h : set_ub s a) (h' : a ≤ b) : set_ub s b :=\nλ x xs, le_trans (h x xs) h'\n\n-- BOTH:\nend\n\n/- TEXT:\n.. index:: injective function\n\nWe close this section with one last important example.\nA function :math:`f` is said to be *injective* if for\nevery :math:`x_1` and :math:`x_2`,\nif :math:`f(x_1) = f(x_2)` then :math:`x_1 = x_2`.\nMathlib defines ``function.injective f`` with\n``x₁`` and ``x₂`` implicit.\nThe next example shows that, on the real numbers,\nany function that adds a constant is injective.\nWe then ask you to show that multiplication by a nonzero\nconstant is also injective.\nTEXT. -/\n-- BOTH:\nsection\n-- QUOTE:\nopen function\n\n-- EXAMPLES:\nexample (c : ℝ) : injective (λ x, x + c) :=\nbegin\n intros x₁ x₂ h',\n exact (add_left_inj c).mp h',\nend\n\nexample {c : ℝ} (h : c ≠ 0) : injective (λ x, c * x) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample {c : ℝ} (h : c ≠ 0) : injective (λ x, c * x) :=\nbegin\n intros x₁ x₂ h',\n apply (mul_right_inj' h).mp h'\nend\n\n/- TEXT:\nFinally, show that the composition of two injective functions is injective:\nBOTH: -/\n-- QUOTE:\nvariables {α : Type*} {β : Type*} {γ : Type*}\nvariables {g : β → γ} {f : α → β}\n\n-- EXAMPLES:\nexample (injg : injective g) (injf : injective f) :\n injective (λ x, g (f x)) :=\nsorry\n-- QUOTE.\n\n-- SOLUTIONS:\nexample (injg : injective g) (injf : injective f) :\n injective (λ x, g (f x)) :=\nbegin\n intros x₁ x₂ h,\n apply injf,\n apply injg,\n apply h\nend\n\n-- BOTH:\nend\n", "meta": {"author": "avigad", "repo": "mathematics_in_lean_source", "sha": "4eeb8c43762522c1acaa7f799599609d6a48f59e", "save_path": "github-repos/lean/avigad-mathematics_in_lean_source", "path": "github-repos/lean/avigad-mathematics_in_lean_source/mathematics_in_lean_source-4eeb8c43762522c1acaa7f799599609d6a48f59e/lean_source/03_Logic/source_01_Implication_and_the_Universal_Quantifier.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9263037262250327, "lm_q2_score": 0.9196425377849806, "lm_q1q2_score": 0.8518683095452729}}
{"text": "import data.real.basic\nimport data.int.parity\n\n/-\nIn this file, we learn how to handle the ∃ quantifier.\n\nIn order to prove `∃ x, P x`, we give some x₀ using tactic `use x₀` and\nthen prove `P x₀`. This x₀ can be an object from the local context\nor a more complicated expression.\n-/\nexample : ∃ n : ℕ, 8 = 2*n :=\nbegin\n use 4,\n refl, -- this is the tactic analogue of the rfl proof term\nend\n\n/-\nIn order to use `h : ∃ x, P x`, we use the `cases` tactic to fix\none x₀ that works.\n\nAgain h can come straight from the local context or can be a more\ncomplicated expression.\n-/\nexample (n : ℕ) (h : ∃ k : ℕ, n = k + 1) : n > 0 :=\nbegin\n -- Let's fix k₀ such that n = k₀ + 1.\n cases h with k₀ hk₀,\n -- It now suffices to prove k₀ + 1 > 0.\n rw hk₀,\n -- and we have a lemma about this\n exact nat.succ_pos k₀,\nend\n\n/-\nThe next exercises use divisibility in ℤ (beware the ∣ symbol which is\nnot ASCII).\n\nBy definition, a ∣ b ↔ ∃ k, b = a*k, so you can prove a ∣ b using the\n`use` tactic.\n-/\n\n-- Until the end of this file, a, b and c will denote integers, unless\n-- explicitly stated otherwise\nvariables (a b c : ℤ)\n\n-- 0029\nexample (h₁ : a ∣ b) (h₂ : b ∣ c) : a ∣ c :=\nbegin\n cases h₁ with k1 hk1,\n cases h₂ with k2 hk2,\n have h: c = a * (k1 * k2), by\n {calc c = b * k2 : by rw hk2\n ... = (a * k1) * k2 : by rw hk1\n ... = a * (k1 * k2) : by rw mul_assoc},\n use k1*k2,\n exact h,\nend\n\n/-\nA very common pattern is to have an assumption or lemma asserting\n h : ∃ x, y = ...\nand this is used through the combo:\n cases h with x hx,\n rw hx at ...\nThe tactic `rcases` allows us to do recursive `cases`, as indicated by its name,\nand also simplifies the above combo when the name hx is replaced by the special\nname `rfl`, as in the following example.\nIt uses the anonymous constructor angle brackets syntax.\n-/\n\n\nexample (h1 : a ∣ b) (h2 : a ∣ c) : a ∣ b+c :=\nbegin\n rcases h1 with ⟨k, rfl⟩,\n rcases h2 with ⟨l, rfl⟩,\n use k+l,\n ring,\nend\n\n/-\nYou can use the same `rfl` trick with the `rintros` tactic.\n-/\n\nexample : a ∣ b → a ∣ c → a ∣ b+c :=\nbegin\n rintros ⟨k, rfl⟩ ⟨l, rfl⟩,\n use k+l,\n ring,\nend\n\n-- 0030\nexample : 0 ∣ a ↔ a = 0 :=\nbegin\n split,\n {rintros ⟨k, refl⟩,\n linarith\n },\n {intros h,\n use 0, -- 0 = 0 * 0, so 0 | 0\n linarith}\nend\n\n/-\nWe can now start combining quantifiers, using the definition\n\n surjective (f : X → Y) := ∀ y, ∃ x, f x = y\n\n-/\nopen function\n\n-- In the remaining of this file, f and g will denote functions from\n-- ℝ to ℝ.\nvariables (f g : ℝ → ℝ)\n\n\n-- 0031\nexample (h : surjective (g ∘ f)) : surjective g :=\nbegin\n intros x,\n rcases h x with ⟨t,rfl⟩,\n exact ⟨f t, rfl⟩\nend\n\n/-\nThe above exercise can be done in three lines. Try to do the\nnext exercise in four lines.\n-/\n\n-- 0032\nexample (hf : surjective f) (hg : surjective g) : surjective (g ∘ f) :=\nbegin\n intros x,\n rcases hg x with ⟨t, rfl⟩,\n rcases hf t with ⟨tt, rfl⟩,\n exact ⟨tt, rfl⟩\nend\n\n", "meta": {"author": "kris-brown", "repo": "theorem_proving_in_lean", "sha": "774460c21bf857daff158210741bd88d1c8323cd", "save_path": "github-repos/lean/kris-brown-theorem_proving_in_lean", "path": "github-repos/lean/kris-brown-theorem_proving_in_lean/theorem_proving_in_lean-774460c21bf857daff158210741bd88d1c8323cd/tutorial_src/exercises/04_exists.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9473810407096791, "lm_q2_score": 0.8991213853793453, "lm_q1q2_score": 0.8518105538050126}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio . Demostrar que la composición de funciones suprayectivas\n-- es suprayectiva.\n-- ----------------------------------------------------------------------\n\n\nimport tactic\n\nopen function\n\nvariables {α : Type*} {β : Type*} {γ : Type*}\nvariables {f : α → β} {g : β → γ} \n\nexample \n (surjg : surjective g) \n (surjf : surjective f) \n : surjective (λ x, g (f x)) :=\nbegin\n intro x,\n cases surjg x with y hy,\n cases surjf y with z hz,\n use z,\n change g (f z) = x,\n rw hz,\n exact hy,\nend\n\n-- La prueba es\n-- \n-- α : Type u_1,\n-- β : Type u_2,\n-- γ : Type u_3,\n-- f : α → β,\n-- g : β → γ,\n-- surjg : surjective g,\n-- surjf : surjective f\n-- ⊢ surjective (λ (x : α), g (f x))\n-- >> intro x,\n-- x : γ\n-- ⊢ ∃ (a : α), (λ (x : α), g (f x)) a = x\n-- >> cases surjg x with y hy,\n-- y : β,\n-- hy : g y = x\n-- ⊢ ∃ (a : α), (λ (x : α), g (f x)) a = x\n-- >> cases surjf y with z hz,\n-- z : α,\n-- hz : f z = y\n-- ⊢ ∃ (a : α), (λ (x : α), g (f x)) a = x\n-- >> use z,\n-- ⊢ (λ (x : α), g (f x)) z = x\n-- >> change g (f z) = x,\n-- ⊢ g (f z) = x\n-- >> rw hz,\n-- ⊢ g y = x\n-- >> exact hy\n-- no goals\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Logica/Composicion_de_suprayectivas.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9504109812297141, "lm_q2_score": 0.8962513765975758, "lm_q1q2_score": 0.8518071502605841}}
{"text": "\n\ninductive Formula\n| eqf : Nat → Nat → Formula\n| andf : Formula → Formula → Formula\n| impf : Formula → Formula → Formula\n| notf : Formula → Formula\n| orf : Formula → Formula → Formula\n| allf : (Nat → Formula) → Formula\n\nnamespace Formula\ndef implies (a b : Prop) : Prop := a → b\n\ndef denote : Formula → Prop\n| eqf n1 n2 => n1 = n2\n| andf f1 f2 => denote f1 ∧ denote f2\n| impf f1 f2 => implies (denote f1) (denote f2)\n| orf f1 f2 => denote f1 ∨ denote f2\n| notf f => ¬ denote f\n| allf f => (n : Nat) → denote (f n)\n\ntheorem denote_eqf (n1 n2 : Nat) : denote (eqf n1 n2) = (n1 = n2) :=\nrfl\n\ntheorem denote_andf (f1 f2 : Formula) : denote (andf f1 f2) = (denote f1 ∧ denote f2) :=\nrfl\n\ntheorem denote_impf (f1 f2 : Formula) : denote (impf f1 f2) = (denote f1 → denote f2) :=\nrfl\n\ntheorem denote_orf (f1 f2 : Formula) : denote (orf f1 f2) = (denote f1 ∨ denote f2) :=\nrfl\n\ntheorem denote_notf (f : Formula) : denote (notf f) = ¬ denote f :=\nrfl\n\ntheorem denote_allf (f : Nat → Formula) : denote (allf f) = (∀ n, denote (f n)) :=\nrfl\n\ntheorem ex : denote (allf (fun n₁ => allf (fun n₂ => impf (eqf n₁ n₂) (eqf n₂ n₁)))) = (∀ (n₁ n₂ : Nat), n₁ = n₂ → n₂ = n₁) :=\nrfl\n\nend Formula\n", "meta": {"author": "leanprover", "repo": "lean4", "sha": "742d053a97bdd109a41a921facd1cd6a55e89bc7", "save_path": "github-repos/lean/leanprover-lean4", "path": "github-repos/lean/leanprover-lean4/lean4-742d053a97bdd109a41a921facd1cd6a55e89bc7/tests/lean/run/def11.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9702399051935107, "lm_q2_score": 0.8774767906859264, "lm_q1q2_score": 0.8513629982046192}}
{"text": "import ..lectures.love05_inductive_predicates_demo\n\n\n/-! # LoVe Exercise 5: Inductive Predicates -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## Question 1: Even and Odd\n\nThe `even` predicate is true for even numbers and false for odd numbers. -/\n\n#check even\n\n/-! We define `odd` as the negation of `even`: -/\n\ndef odd (n : ℕ) : Prop :=\n ¬ even n\n\n/-! 1.1. Prove that 1 is odd and register this fact as a simp rule.\n\nHint: `cases'` is useful to reason about hypotheses of the form `even …`. -/\n\n@[simp] lemma odd_1 :\n odd 1 :=\nbegin\n intro h,\n cases' h\nend\n\n/-! 1.2. Prove that 3 and 5 are odd. -/\n\nlemma odd_3 :\n odd 3 :=\nbegin\n intro h,\n cases' h,\n cases' h\nend\n\nlemma odd_5 :\n odd 5 :=\nbegin\n intro h,\n cases' h,\n cases' h,\n cases' h\nend\n\n/-! 1.3. Complete the following proof by structural induction. -/\n\nlemma even_two_times :\n ∀m : ℕ, even (2 * m)\n| 0 := even.zero\n| (m + 1) :=\n begin\n apply even.add_two,\n simp,\n apply even_two_times\n end\n\n/-! 1.4. Complete the following proof by rule induction.\n\nHint: You can use the `cases'` tactic (or `match … with`) to destruct an\nexistential quantifier and extract the witness. -/\n\nlemma even_imp_exists_two_times :\n ∀n : ℕ, even n → ∃m, n = 2 * m :=\nbegin\n intros n hen,\n induction' hen,\n case zero {\n apply exists.intro 0,\n refl },\n case add_two : k hek ih {\n cases' ih with w hk,\n apply exists.intro (w + 1),\n rw hk,\n linarith }\nend\n\n/-! 1.5. Using `even_two_times` and `even_imp_exists_two_times`, prove the\nfollowing equivalence. -/\n\nlemma even_iff_exists_two_times (n : ℕ) :\n even n ↔ ∃m, n = 2 * m :=\nbegin\n apply iff.intro,\n { apply even_imp_exists_two_times },\n { intro h,\n cases' h,\n simp *,\n apply even_two_times }\nend\n\n/-! 1.6 (**optional**). Give a structurally recursive definition of `even` and\ntest it with `#eval`.\n\nHint: The negation operator on `bool` is called `not`. -/\n\ndef even_rec : nat → bool\n| 0 := tt\n| (n + 1) := not (even_rec n)\n\n#eval even_rec 0\n#eval even_rec 1\n#eval even_rec 2\n#eval even_rec 3\n#eval even_rec 4\n\n\n/-! ## Question 2: Tennis Games\n\nRecall the inductive type of tennis scores from the demo: -/\n\n#check score\n\n/-! 2.1. Define an inductive predicate that returns true if the server is ahead\nof the receiver and that returns false otherwise. -/\n\ninductive srv_ahead : score → Prop\n| vs {m n : ℕ} : m > n → srv_ahead (score.vs m n)\n| adv_srv : srv_ahead score.adv_srv\n| game_srv : srv_ahead score.game_srv\n\n/-! 2.2. Validate your predicate definition by proving the following lemmas. -/\n\nlemma srv_ahead_vs {m n : ℕ} (hgt : m > n) :\n srv_ahead (score.vs m n) :=\nsrv_ahead.vs hgt\n\nlemma srv_ahead_adv_srv :\n srv_ahead score.adv_srv :=\nsrv_ahead.adv_srv\n\nlemma not_srv_ahead_adv_rcv :\n ¬ srv_ahead score.adv_rcv :=\nbegin\n intro h,\n cases' h\nend\n\nlemma srv_ahead_game_srv :\n srv_ahead score.game_srv :=\nsrv_ahead.game_srv\n\nlemma not_srv_ahead_game_rcv :\n ¬ srv_ahead score.game_rcv :=\nbegin\n intro h,\n cases' h\nend\n\n/-! 2.3. Compare the above lemma statements with your definition. What do you\nobserve? -/\n\n/-! The positive lemmas correspond exactly to the introduction rules of the\ndefinition. By contrast, the negative lemmas have no counterparts in the\ndefinition. -/\n\n\n/-! ## Question 3: Binary Trees\n\n3.1. Prove the converse of `is_full_mirror`. You may exploit already proved\nlemmas (e.g., `is_full_mirror`, `mirror_mirror`). -/\n\n#check is_full_mirror\n#check mirror_mirror\n\nlemma mirror_is_full {α : Type} :\n ∀t : btree α, is_full (mirror t) → is_full t :=\nbegin\n intros t fmt,\n have fmmt : is_full (mirror (mirror t)) :=\n is_full_mirror _ fmt,\n rw mirror_mirror at fmmt,\n assumption\nend\n\n/-! 3.2. Define a `map` function on binary trees, similar to `list.map`. -/\n\ndef map_btree {α β : Type} (f : α → β) : btree α → btree β\n| btree.empty := btree.empty\n| (btree.node a l r) := btree.node (f a) (map_btree l) (map_btree r)\n\n/-! 3.3. Prove the following lemma by case distinction. -/\n\nlemma map_btree_eq_empty_iff {α β : Type} (f : α → β) :\n ∀t : btree α, map_btree f t = btree.empty ↔ t = btree.empty\n| btree.empty := by simp [map_btree]\n| (btree.node _ _ _) := by simp [map_btree]\n\n/-! 3.4 (**optional**). Prove the following lemma by rule induction. -/\n\nlemma map_btree_mirror {α β : Type} (f : α → β) :\n ∀t : btree α, is_full t → is_full (map_btree f t) :=\nbegin\n intros t hfull,\n induction' hfull,\n case empty {\n simp [map_btree],\n exact is_full.empty },\n case node {\n simp [map_btree],\n apply is_full.node,\n { exact ih_hfull f },\n { exact ih_hfull_1 f },\n { simp [map_btree_eq_empty_iff],\n assumption } }\nend\n\nend LoVe\n", "meta": {"author": "BrownCS1951x", "repo": "fpv2022", "sha": "aeaf291183721460387f8ae4c3c008836b8460e7", "save_path": "github-repos/lean/BrownCS1951x-fpv2022", "path": "github-repos/lean/BrownCS1951x-fpv2022/fpv2022-aeaf291183721460387f8ae4c3c008836b8460e7/src/exercises/love05_inductive_predicates_exercise_solution.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES\n\n", "lm_q1_score": 0.9496693645535724, "lm_q2_score": 0.8962513620489619, "lm_q1q2_score": 0.8511424614773114}}
{"text": "/-\nCopyright (c) 2021 Yourong Zang. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Yourong Zang\n-/\nimport analysis.calculus.conformal.normed_space\nimport analysis.inner_product_space.conformal_linear_map\n\n/-!\n# Conformal maps between inner product spaces\n\nA function between inner product spaces is which has a derivative at `x`\nis conformal at `x` iff the derivative preserves inner products up to a scalar multiple.\n-/\n\nnoncomputable theory\n\nvariables {E F : Type*} [inner_product_space ℝ E] [inner_product_space ℝ F]\n\nopen_locale real_inner_product_space\n\n/-- A real differentiable map `f` is conformal at point `x` if and only if its\n differential `fderiv ℝ f x` at that point scales every inner product by a positive scalar. -/\nlemma conformal_at_iff' {f : E → F} {x : E} :\n conformal_at f x ↔\n ∃ (c : ℝ), 0 < c ∧ ∀ (u v : E), ⟪fderiv ℝ f x u, fderiv ℝ f x v⟫ = c * ⟪u, v⟫ :=\nby rw [conformal_at_iff_is_conformal_map_fderiv, is_conformal_map_iff]\n\n/-- A real differentiable map `f` is conformal at point `x` if and only if its\n differential `f'` at that point scales every inner product by a positive scalar. -/\nlemma conformal_at_iff {f : E → F} {x : E} {f' : E →L[ℝ] F} (h : has_fderiv_at f f' x) :\n conformal_at f x ↔ ∃ (c : ℝ), 0 < c ∧ ∀ (u v : E), ⟪f' u, f' v⟫ = c * ⟪u, v⟫ :=\nby simp only [conformal_at_iff', h.fderiv]\n\n/-- The conformal factor of a conformal map at some point `x`. Some authors refer to this function\n as the characteristic function of the conformal map. -/\ndef conformal_factor_at {f : E → F} {x : E} (h : conformal_at f x) : ℝ :=\nclassical.some (conformal_at_iff'.mp h)\n\nlemma conformal_factor_at_pos {f : E → F} {x : E} (h : conformal_at f x) :\n 0 < conformal_factor_at h :=\n(classical.some_spec $ conformal_at_iff'.mp h).1\n\nlemma conformal_factor_at_inner_eq_mul_inner' {f : E → F} {x : E}\n (h : conformal_at f x) (u v : E) :\n ⟪(fderiv ℝ f x) u, (fderiv ℝ f x) v⟫ = (conformal_factor_at h : ℝ) * ⟪u, v⟫ :=\n(classical.some_spec $ conformal_at_iff'.mp h).2 u v\n\nlemma conformal_factor_at_inner_eq_mul_inner {f : E → F} {x : E} {f' : E →L[ℝ] F}\n (h : has_fderiv_at f f' x) (H : conformal_at f x) (u v : E) :\n ⟪f' u, f' v⟫ = (conformal_factor_at H : ℝ) * ⟪u, v⟫ :=\n(H.differentiable_at.has_fderiv_at.unique h) ▸ conformal_factor_at_inner_eq_mul_inner' H u v\n", "meta": {"author": "jjaassoonn", "repo": "projective_space", "sha": "11fe19fe9d7991a272e7a40be4b6ad9b0c10c7ce", "save_path": "github-repos/lean/jjaassoonn-projective_space", "path": "github-repos/lean/jjaassoonn-projective_space/projective_space-11fe19fe9d7991a272e7a40be4b6ad9b0c10c7ce/src/analysis/calculus/conformal/inner_product.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9465966717067252, "lm_q2_score": 0.8991213867309121, "lm_q1q2_score": 0.8511053121398168}}
{"text": "-- ---------------------------------------------------------------------\n-- Ejercicio 1. Realizar las siguientes acciones:\n-- 1. Importar la teoría Definicion_de_funciones_acotadas\n-- 2. Declarar f como variable de funciones de ℝ en ℝ.\n-- 3. Declarar a y c como variables sobre ℝ.\n-- ----------------------------------------------------------------------\n\nimport .Definicion_de_funciones_acotadas -- 1\n\nvariables {f : ℝ → ℝ} -- 2\nvariables {a c : ℝ} -- 3\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 2. Demostrar que si a es una cota superior de f y c no es\n-- negativo, entonces c * a es una cota superior de c * f.\n-- ----------------------------------------------------------------------\n\nlemma fn_ub_mul\n (hfa : fn_ub f a)\n (h : c ≥ 0)\n : fn_ub (λ x, c * f x) (c * a) :=\nλ x, mul_le_mul_of_nonneg_left (hfa x) h\n\n-- ---------------------------------------------------------------------\n-- Ejercicio 3. Demostrar que si c ≥ 0 y f está acotada superiormente,\n-- entonces c * f también lo está.\n-- ----------------------------------------------------------------------\n\n-- 1ª demostración\n-- ===============\n\nexample\n (ubf : fn_has_ub f)\n (h : c ≥ 0)\n : fn_has_ub (λ x, c * f x) :=\nbegin\n cases ubf with a ha,\n have h1 : fn_ub (λ x, c * f x) (c * a) := fn_ub_mul ha h,\n have h2 : ∃ z, ∀ x, (λ x, c * f x) x ≤ z,\n by exact Exists.intro (c * a) h1,\n show fn_has_ub (λ x, c * f x),\n by exact h2,\nend\n\n-- 2ª demostración\n-- ===============\n\nexample\n (ubf : fn_has_ub f)\n (h : c ≥ 0)\n : fn_has_ub (λ x, c * f x) :=\nbegin\n cases ubf with a ha,\n use c * a,\n apply fn_ub_mul ha h,\nend\n\n-- Su desarrollo es\n--\n-- f : ℝ → ℝ,\n-- c : ℝ,\n-- ubf : fn_has_ub f,\n-- h : c ≥ 0\n-- ⊢ fn_has_ub (λ (x : ℝ), c * f x)\n-- >> cases ubf with a ha,\n-- a : ℝ,\n-- ha : fn_ub f a\n-- ⊢ fn_has_ub (λ (x : ℝ), c * f x)\n-- >> use c * a,\n-- ⊢ fn_ub (λ (x : ℝ), c * f x) (c * a)\n-- >> apply fn_ub_mul ha h\n-- no goals\n\n-- 3ª demostración\n-- ===============\n\nexample\n (ubf : fn_has_ub f)\n (h : c ≥ 0)\n : fn_has_ub (λ x, c * f x) :=\nbegin\n rcases ubf with ⟨a, ha⟩,\n exact ⟨c * a, fn_ub_mul ha h⟩,\nend\n\n-- 4ª demostración\n-- ===============\n\nexample\n (h : c ≥ 0)\n : fn_has_ub f → fn_has_ub (λ x, c * f x) :=\nbegin\n rintro ⟨a, ha⟩,\n exact ⟨c * a, fn_ub_mul ha h⟩,\nend\n\n-- 5ª demostración\n-- ===============\n\nexample\n (h : c ≥ 0)\n : fn_has_ub f → fn_has_ub (λ x, c * f x) :=\nλ ⟨a, ha⟩, ⟨c * a, fn_ub_mul ha h⟩\n", "meta": {"author": "jaalonso", "repo": "Matematicas_en_Lean", "sha": "c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8", "save_path": "github-repos/lean/jaalonso-Matematicas_en_Lean", "path": "github-repos/lean/jaalonso-Matematicas_en_Lean/Matematicas_en_Lean-c44e23d87665cb4aa00c813c6bfb3c41ebc83aa8/src/Logica/Producto_por_funcion_acotada_superiormente.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. YES\n2. YES", "lm_q1_score": 0.9273632856092014, "lm_q2_score": 0.917302665802808, "lm_q1q2_score": 0.8506728140569713}}