{"text": "/-\n# The Maze Game, version 1.3.0.\n\n## By Kevin Buzzard \n\nAn idea coming out of discussions with Kendall Frey.\n\n# What is this game?\n\nFour mazes and a theorem (which I must confess I haven't yet proved).\n\nYou are in a maze of twisty passages, all distinct (at this point).\nUse the tactics `n`, `s`, `e`, `w` to escape.\nUse the `out` tactic to get out when you're at the exit room.\n\n# Credits\n\nThe Maze Game is brought to you by the Xena project, a project based at Imperial College London\nwhose aim is to get mathematics undergraduates using computer theorem provers.\nLean is a computer theorem prover being developed at Microsoft Research.\n\nProve a theorem. Write a function. <a href=\"https://twitter.com/XenaProject\" target=\"blank\">@XenaProject</a>.\n-/\n", "meta": {"author": "kbuzzard", "repo": "lean-game-skeleton", "sha": "098454dd6acc4c06beccf52b6547bf4cd99cc581", "save_path": "github-repos/lean/kbuzzard-lean-game-skeleton", "path": "github-repos/lean/kbuzzard-lean-game-skeleton/lean-game-skeleton-098454dd6acc4c06beccf52b6547bf4cd99cc581/src/game/intro.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.4493926197162522, "lm_q2_score": 0.20946969120038575, "lm_q1q2_score": 0.09413413327969573}}
{"text": "/-\nCopyright (c) 2019 Patrick Massot. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Patrick Massot\n-/\nimport tactic.interactive\n\n/-!\n# Rename bound variable tactic\n\nThis files defines a tactic `rename_var` whose main purpose is to teach\nrenaming of bound variables.\n\n* `rename_var old new` renames all bound variables named `old` to `new` in the goal.\n* `rename_var old new at h` does the same in hypothesis `h`.\n\n```lean\nexample (P : \u2115 \u2192  \u2115 \u2192 Prop) (h : \u2200 n, \u2203 m, P n m) : \u2200 l, \u2203 m, P l m :=\nbegin\n  rename_var n q at h, -- h is now \u2200 (q : \u2115), \u2203 (m : \u2115), P q m,\n  rename_var m n, -- goal is now \u2200 (l : \u2115), \u2203 (n : \u2115), P k n,\n  exact h -- Lean does not care about those bound variable names\nend\n```\n\n## Tags\n\nteaching, tactic\n-/\nopen expr\n\n/-- Rename bound variable `old` to `new` in an `expr`-/\nmeta def expr.rename_var (old new : name) : expr \u2192 expr\n| (pi n bi t b) := (pi (if n = old then new else n) bi (expr.rename_var t) (expr.rename_var b))\n| (lam n bi t b) := (lam (if n = old then new else n) bi (expr.rename_var t) (expr.rename_var b))\n| (app t b) := (app (expr.rename_var t) (expr.rename_var b))\n| e := e\n\nnamespace tactic\n/-- Rename bound variable `old` to `new` in goal -/\nmeta def rename_var_at_goal (old new : name) : tactic unit :=\ndo\n  old_tgt \u2190 target,\n  tactic.change (expr.rename_var old new old_tgt)\n\n/-- Rename bound variable `old` to `new` in assumption `h` -/\nmeta def rename_var_at_hyp (old new : name) (e : expr) : tactic unit :=\ndo\n  old_e \u2190 infer_type e,\n  tactic.change_core (expr.rename_var old new old_e) (some e)\nend tactic\n\nnamespace tactic.interactive\nopen tactic\nsetup_tactic_parser\n\n/--\n`rename_var old new` renames all bound variables named `old` to `new` in the goal.\n`rename_var old new at h` does the same in hypothesis `h`.\n-/\nmeta def rename_var (old : parse ident) (new : parse ident) (l : parse location) : tactic unit :=\nl.apply (rename_var_at_hyp old new) (rename_var_at_goal old new)\nend tactic.interactive\n\n/--\n`rename_var old new` renames all bound variables named `old` to `new` in the goal.\n`rename_var old new at h` does the same in hypothesis `h`.\nThis is meant for teaching bound variables only. Such a renaming should never be relevant to Lean.\n\n```lean\nexample (P : \u2115 \u2192  \u2115 \u2192 Prop) (h : \u2200 n, \u2203 m, P n m) : \u2200 l, \u2203 m, P l m :=\nbegin\n  rename_var n q at h, -- h is now \u2200 (q : \u2115), \u2203 (m : \u2115), P q m,\n  rename_var m n, -- goal is now \u2200 (l : \u2115), \u2203 (n : \u2115), P k n,\n  exact h -- Lean does not care about those bound variable names\nend\n```\n-/\nadd_tactic_doc\n{ name       := \"rename_var\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.rename_var],\n  tags       := [\"renaming\"] }\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/rename_var.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.3738758227716966, "lm_q2_score": 0.23934934732271165, "lm_q1q2_score": 0.0894869341601474}}
{"text": "/- LoVe Exercise 7: Metaprogramming -/\n\nimport .lovelib\n\nnamespace LoVe\n\nopen expr\nopen tactic\n\n\n/- Question 1: A Term Exploder -/\n\n/- In this exercise, we develop a string format for the `expr` metatype. By\ndefault, there is no `has_repr` instance to print a nice string. For\nexample: -/\n\n#eval (expr.app (expr.var 0) (expr.var 1) : expr)   -- result: `[external]`\n#eval (`(\u03bbx : \u2115, x + x) : expr)                     -- result: `[external]`\n\n/- 1.1. Define a metafunction `expr.repr` that converts an `expr` into a\n`string`. It is acceptable to leave out some fields from the `expr`\nconstructors, such as the level `l` of a sort, the binder information `bi` of\na \u03bb or \u03a0 binder, and the arguments of the `macro` constructor.\n\n**Hint**: Use `name.to_string` to convert a name to a string, and `repr` for\nother types that belong to the `has_repr` type class. -/\n\n-- enter your definition here\n\n/- We register `expr.repr` in the `has_repr` type class, so that we can use\n`repr` without qualification in the future, and so that it is available to\n`#eval`. We need the `meta` keyword in front of the command we enter. -/\n\nmeta instance : has_repr expr := \u27e8expr.repr\u27e9\n\n/- 1.2. Test your setup. -/\n\n#eval (expr.app (expr.var 0) (expr.var 1) : expr)\n#eval (`(\u03bbx : \u2115, x + x) : expr)\n\n/- 1.3. Compare your answer with `expr.to_raw_fmt`. -/\n\n#check expr.to_raw_fmt\n\n\n/- Question 2: `destruct_and` on Steroids -/\n\n/- Recall from the lecture that `destruct_and` fails on the following easy\ngoal: -/\n\nexample {a b c d : Prop} (h : a \u2227 (b \u2227 c) \u2227 d) :\n  b \u2227 d :=\nsorry\n\n/- We will now address this by developing a new tactic called `destro_and`,\nwhich applies both **des**truction and in**tro**duction rules for conjunction.\nIt will also go automatically through the hypotheses instead of taking an\nargument. We will develop it in three steps. -/\n\n/- 2.1. Develop a tactic `intro_ands` that replaces all goals of the form\n`a \u2227 b` with two new goals `a` and `b` systematically, until all top-level\nconjunctions are gone.\n\nFor this, we can use tactics such as `repeat` (which repeatedly applies a tactic\non all goals until the tactic fails on each of the goal) and `applyc` (which can\nbe used to apply a rule, in connection with backtick quoting). -/\n\nmeta def intro_ands : tactic unit :=\nsorry\n\nexample {a b c d : Prop} (h : a \u2227 (b \u2227 c) \u2227 d) :\n  b \u2227 d :=\nbegin\n  intro_ands,\n  /- The proof state should be as follows:\n\n  2 goals\n  a b c d : Prop,\n  h : a \u2227 (b \u2227 c) \u2227 d\n  \u22a2 b\n\n  a b c d : Prop,\n  h : a \u2227 (b \u2227 c) \u2227 d\n  \u22a2 d -/\n  repeat { sorry }\nend\n\nexample {a b c d : Prop} (h : a \u2227 (b \u2227 c) \u2227 d) :\n  b \u2227 (a \u2227 (c \u2227 b)) :=\nbegin\n  intro_ands,\n  /- The proof state should be as follows:\n\n  4 goals\n  a b c d : Prop,\n  h : a \u2227 (b \u2227 c) \u2227 d\n  \u22a2 b\n\n  a b c d : Prop,\n  h : a \u2227 (b \u2227 c) \u2227 d\n  \u22a2 a\n\n  a b c d : Prop,\n  h : a \u2227 (b \u2227 c) \u2227 d\n  \u22a2 c\n\n  a b c d : Prop,\n  h : a \u2227 (b \u2227 c) \u2227 d\n  \u22a2 b -/\n  repeat { sorry }\nend\n\n/- 2.2. Develop a tactic `destruct_ands` that replaces hypotheses of the form\n`h : a \u2227 b` by two new hypotheses `h_left : a` and `h_right : b` systematically,\nuntil all top-level conjunctions are gone.\n\nHere is imperative-style pseudocode that you can follow:\n\n1. Retrieve the list of hypotheses from the context. This is provided by the\nmetaconstant `local_context`.\n\n2. Find the first hypothesis (= term) with a type (= proposition) of the form\n`_ \u2227 _`. Here, you can use the `list.mfirst` function, in conjunction with\npattern matching. You can use `infer_type` to query the type of a term.\n\n3. Perform a case split on the first found hypothesis. This can be achieved\nusing the `cases` metafunction.\n\n4. Go to step 1.\n\nThe above procedure might fail if there exists no hypotheses of the required\nform. Make sure to handle this failure gracefully using `<|>`. -/\n\nmeta def destruct_ands : tactic unit :=\nsorry\n\nexample {a b c d : Prop} (h : a \u2227 (b \u2227 c) \u2227 d) :\n  b \u2227 d :=\nbegin\n  destruct_ands,\n  /- The proof state should be as follows:\n\n  a b c d : Prop,\n  h_left : a,\n  h_right_right : d,\n  h_right_left_left : b,\n  h_right_left_right : c\n  \u22a2 b \u2227 d -/\n  sorry\nend\n\n/- 2.3. Finally, combine the two tactics developed above and the `assumption`\ntactic to implement the desired `destro_and` tactic. -/\n\nmeta def destro_and : tactic unit :=\nsorry\n\nexample {a b c d : Prop} (h : a \u2227 (b \u2227 c) \u2227 d) :\n  b \u2227 d :=\nby destro_and\n\nexample {a b c d : Prop} (h : a \u2227 (b \u2227 c) \u2227 d) :\n  b \u2227 (a \u2227 (c \u2227 b)) :=\nby destro_and\n\n\n/- Question 3 **optional**: A Theorem Finder -/\n\n/- We will implement a function that allows us to find theorems by constants\nappearing in their statements. So given a list of constant names, the function\nwill list all theorems in which all these constants appear.\n\nYou can use the following metaconstants:\n\n* `declaration` contains all data (name, type, value) associated with a\n  declaration understood broadly (e.g., axiom, lemma, constant, etc.);\n* `tactic.get_env` gives us access to the `environment`, a metatype thats lists\n  all `declaration`s (including all theorems);\n* `environment.fold` allows us to walk through the environment and collect data;\n* `expr.fold` allows us to walk through an expression and collect data. -/\n\n/- 3.1 **optional**. Write a metafunction that checks whether an expression\ncontains a specific constant.\n\nYou can use `expr.fold` to walk through the expression, `||` and `ff` for\nBooleans, and `expr.is_constant_of` to check whether an expression is a\nconstant. -/\n\nmeta def term_contains (e : expr) (nam : name) : bool :=\nsorry\n\n/- 3.2 **optional**. Write a metafunction that checks whether an expression\ncontains _all_ constants in a list.\n\nYou can use `list.band` (Boolean and). -/\n\nmeta def term_contains_all (nams : list name) (e : expr) : bool :=\nsorry\n\n/- 3.3 **optional**. Produce the list of all theorems that contain all constants\n`nams` in their statement.\n\n`environment.fold` allows you to walk over the list of declarations. With\n`declaration.type`, you get the type of a theorem, and with\n`declaration.to_name` you get the name. -/\n\nmeta def list_constants (nams : list name) (e : environment) : list name :=\nsorry\n\n/- Finally, we develop a tactic that uses the above metafunctions to log all\nfound theorems: -/\n\nmeta def find_constants (nams : list name) : tactic unit :=\ndo\n  env \u2190 get_env,\n  list.mmap' trace (list_constants nams env)\n\n/- We test the solution. -/\n\nrun_cmd find_constants []   -- lists all theorems\nrun_cmd find_constants [`list.map, `function.comp]\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/love07_metaprogramming_exercise_sheet.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.37754068280545827, "lm_q2_score": 0.22000710997428735, "lm_q1q2_score": 0.083061634521748}}
{"text": "import .lovelib\n\n\n/-! # LoVe Preface\n\n## Proof Assistants\n\nProof assistants (also called interactive theorem provers)\n\n* check and help develop formal proofs;\n* can be used to prove big theorems, not only logic puzzles;\n* can be tedious to use;\n* are highly addictive (think video games).\n\nA selection of proof assistants, classified by logical foundations:\n\n* set theory: Isabelle/ZF, Metamath, Mizar;\n* simple type theory: HOL4, HOL Light, Isabelle/HOL;\n* **dependent type theory**: Agda, Coq, **Lean**, Matita, PVS.\n\n\n## Success Stories\n\nMathematics:\n\n* the four-color theorem (in Coq);\n* the Kepler conjecture (in HOL Light and Isabelle/HOL);\n* the definition of perfectoid spaces (in Lean).\n\nComputer science:\n\n* hardware;\n* operating systems;\n* programming language theory;\n* compilers;\n* security.\n\n\n## Lean\n\nLean is a proof assistant developed primarily by Leonardo de Moura (Microsoft\nResearch) since 2012.\n\nIts mathematical library, `mathlib`, is developed under the leadership of\nJeremy Avigad (Carnegie Mellon University).\n\nWe use the community version of Lean 3. We use its basic libraries, `mathlib`,\nand `LoVelib`. Lean is a research project.\n\nStrengths:\n\n* highly expressive logic based on a dependent type theory called the\n  **calculus of inductive constructions**;\n* extended with classical axioms and quotient types;\n* metaprogramming framework;\n* modern user interface;\n* documentation;\n* open source;\n* endless source of puns (Lean Forward, Lean Together, Boolean, \u2026).\n\n\n## This Course\n\n### Web Site\n\n    https://lean-forward.github.io/logical-verification/2021/index.html\n\n\n### Installation Instructions\n\n    https://github.com/blanchette/logical_verification_2021/blob/main/README.md#logical-verification-2021---installation-instructions\n\n\n### Repository (Demos, Exercises, Homework)\n\n    https://github.com/blanchette/logical_verification_2021\n\nThe file you are currently looking at is a demo. There are\n\n* 13 demo files;\n* 13 exercise sheets;\n* 11 homework sheets (10 points each);\n* 1 project (20 points).\n\nYou may submit at most 10 homework, or at most 8 homework and the project.\nHomework, including the project, must be done individually. The homework builds\non the exercises, which build on the demos.\n\n\n### The Hitchhiker's Guide to Logical Verification\n\n    https://github.com/blanchette/logical_verification_2021/blob/main/hitchhikers_guide.pdf\n    https://github.com/blanchette/logical_verification_2021/blob/main/hitchhikers_guide_tablet.pdf\n\nThe lecture notes consist of a preface and 13 chapters. They cover the same\nmaterial as the corresponding lectures but with more details. Sometimes there\nwill not be enough time to cover everything in class, so reading the lecture\nnotes will be necessary.\n\n\n### Final Exam\n\nThe course aims at teaching concepts, not syntax. Therefore, the final exam is\non paper. It is also closed book.\n\n\n## Our Goal\n\nWe want you to\n\n* master fundamental theory and techniques in interactive theorem proving;\n* familiarize yourselves with some application areas;\n* develop some practical skills you can apply on a larger project (as a hobby,\n  for an MSc or PhD, or in industry);\n* feel ready to move to another proof assistant and apply what you have learned;\n* understand the domain well enough to start reading scientific papers.\n\nThis course is neither a pure logical foundations course nor a Lean tutorial.\nLean is our vehicle, not an end in itself.\n\n\n# LoVe Demo 1: Definitions and Statements\n\nWe introduce the basics of Lean and proof assistants, without trying to carry\nout actual proofs yet. We focus on specifying objects and statements of their\nintended properties. -/\n\n\nset_option pp.beta true\nset_option pp.generalized_field_notation false\n\nnamespace LoVe\n\n\n/-! ## A View of Lean\n\nIn a first approximation:\n\n    Lean = functional programming + logic\n\nIn today's lecture, we cover inductive types, recursive functions, and lemma\nstatements.\n\nIf you are not familiar with typed functional programming (e.g., Haskell, ML,\nOCaml, Scala), we recommend that you study a tutorial, such as the first\nchapters of the online tutorial __Learn You a Haskell for Great Good!__:\n\n    http://learnyouahaskell.com/chapters\n\nMake sure to at least reach, and read, the section titled \"Lambdas\".\n\n\n## Types and Terms\n\nSimilar to simply typed \u03bb-calculus or typed functional programming languages\n(ML, OCaml, Haskell).\n\nTypes `\u03c3`, `\u03c4`, `\u03c5`:\n\n* type variables `\u03b1`;\n* basic types `T`;\n* complex types `T \u03c31 \u2026 \u03c3N`.\n\nSome type constructors `T` are written infix, e.g., `\u2192` (function type).\n\nThe function arrow is right-associative:\n`\u03c3\u2081 \u2192 \u03c3\u2082 \u2192 \u03c3\u2083 \u2192 \u03c4` = `\u03c3\u2081 \u2192 (\u03c3\u2082 \u2192 (\u03c3\u2083 \u2192 \u03c4))`.\n\nPolymorphic types are also possible. In Lean, the type variables must be bound\nusing `\u2200`, e.g., `\u2200\u03b1, \u03b1 \u2192 \u03b1`.\n\nTerms `t`, `u`:\n\n* constants `c`;\n* variables `x`;\n* applications `t u`;\n* \u03bb-expressions `\u03bbx, t`.\n\n__Currying__: functions can be\n\n* fully applied (e.g., `f x y z` if `f` is ternary);\n* partially applied (e.g., `f x y`, `f x`);\n* left unapplied (e.g., `f`).\n\nApplication is left-associative: `f x y z` = `((f x) y) z`. -/\n\n#check \u2115\n#check \u2124\n\n#check empty\n#check unit\n#check bool\n\n#check \u2115 \u2192 \u2124\n#check \u2124 \u2192 \u2115\n#check bool \u2192 \u2115 \u2192 \u2124\n#check (bool \u2192 \u2115) \u2192 \u2124\n#check \u2115 \u2192 (bool \u2192 \u2115) \u2192 \u2124\n\n#check \u03bbx : \u2115, x\n#check \u03bbf : \u2115 \u2192 \u2115, \u03bbg : \u2115 \u2192 \u2115, \u03bbh : \u2115 \u2192 \u2115, \u03bbx : \u2115, h (g (f x))\n#check \u03bb(f g h : \u2115 \u2192 \u2115) (x : \u2115), h (g (f x))\n\nconstants a b : \u2124\nconstant f : \u2124 \u2192 \u2124\nconstant g : \u2124 \u2192 \u2124 \u2192 \u2124\n\n#check \u03bbx : \u2124, g (f (g a x)) (g x b)\n#check \u03bbx, g (f (g a x)) (g x b)\n\n#check \u03bbx, x\n\nconstant trool : Type\nconstants trool.true trool.false trool.maybe : trool\n\n\n/-! ### Type Checking and Type Inference\n\nType checking and type inference are decidable problems (although this property is\nquickly lost if features such as overloading or subtyping are added).\n\nType judgment: `C \u22a2 t : \u03c3`, meaning `t` has type `\u03c3` in local context `C`.\n\nTyping rules:\n\n    \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 Cst   if c is declared with type \u03c3\n    C \u22a2 c : \u03c3\n\n    \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 Var   if x : \u03c3 is the last occurrence of x in C\n    C \u22a2 x : \u03c3\n\n    C \u22a2 t : \u03c3 \u2192 \u03c4    C \u22a2 u : \u03c3\n    \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 App\n    C \u22a2 t u : \u03c4\n\n    C, x : \u03c3 \u22a2 t : \u03c4\n    \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 Lam\n    C \u22a2 (\u03bbx : \u03c3, t) : \u03c3 \u2192 \u03c4\n\n\n### Type Inhabitation\n\nGiven a type `\u03c3`, the __type inhabitation__ problem consists of finding a term\nof that type. Type inhabitation is undecidable.\n\nRecursive procedure:\n\n1. If `\u03c3` is of the form `\u03c4 \u2192 \u03c5`, a candidate inhabitant is an anonymous\n   function of the form `\u03bbx, _`.\n\n2. Alternatively, you can use any constant or variable `x : \u03c4\u2081 \u2192 \u22ef \u2192 \u03c4N \u2192 \u03c3` to\n   build the term `x _ \u2026 _`. -/\n\nconstants \u03b1 \u03b2 \u03b3 : Type\n\ndef some_fun_of_type : (\u03b1 \u2192 \u03b2 \u2192 \u03b3) \u2192 ((\u03b2 \u2192 \u03b1) \u2192 \u03b2) \u2192 \u03b1 \u2192 \u03b3 :=\n\u03bbf g a, f a (g (\u03bbb, a))\n\n\n/-! ## Type Definitions\n\nAn __inductive type__ (also called __inductive datatype__,\n__algebraic datatype__, or just __datatype__) is a type that consists all the\nvalues that can be built using a finite number of applications of its\n__constructors__, and only those.\n\n\n### Natural Numbers -/\n\nnamespace my_nat\n\n/-! Definition of type `nat` (= `\u2115`) of natural numbers, using Peano-style unary\nnotation: -/\n\ninductive nat : Type\n| zero : nat\n| succ : nat \u2192 nat\n\n#check nat\n#check nat.zero\n#check nat.succ\n\nend my_nat\n\n#print nat\n#print \u2115\n\n\n/-! ### Arithmetic Expressions -/\n\ninductive aexp : Type\n| num : \u2124 \u2192 aexp\n| var : string \u2192 aexp\n| add : aexp \u2192 aexp \u2192 aexp\n| sub : aexp \u2192 aexp \u2192 aexp\n| mul : aexp \u2192 aexp \u2192 aexp\n| div : aexp \u2192 aexp \u2192 aexp\n\n\n/-! ### Lists -/\n\nnamespace my_list\n\ninductive list (\u03b1 : Type) : Type\n| nil  : list\n| cons : \u03b1 \u2192 list \u2192 list\n\n#check list.nil\n#check list.cons\n\nend my_list\n\n#print list\n\n\n/-! ## Function Definitions\n\nThe syntax for defining a function operating on an inductive type is very\ncompact: We define a single function and use __pattern matching__ to extract the\narguments to the constructors. -/\n\ndef add : \u2115 \u2192 \u2115 \u2192 \u2115\n| m nat.zero     := m\n| m (nat.succ n) := nat.succ (add m n)\n\n#eval add 2 7\n#reduce add 2 7\n\ndef mul : \u2115 \u2192 \u2115 \u2192 \u2115\n| _ nat.zero     := nat.zero\n| m (nat.succ n) := add m (mul m n)\n\n#eval mul 2 7\n\n#print mul\n#print mul._main\n\ndef power : \u2115 \u2192 \u2115 \u2192 \u2115\n| _ nat.zero     := 1\n| m (nat.succ n) := m * power m n\n\n#eval power 2 5\n\ndef power\u2082 (m : \u2115) : \u2115 \u2192 \u2115\n| nat.zero     := 1\n| (nat.succ n) := m * power\u2082 n\n\n#eval power\u2082 2 5\n\ndef iter (\u03b1 : Type) (z : \u03b1) (f : \u03b1 \u2192 \u03b1) : \u2115 \u2192 \u03b1\n| nat.zero     := z\n| (nat.succ n) := f (iter n)\n\n#check iter\n\ndef power\u2083 (m n : \u2115) : \u2115 :=\niter \u2115 1 (\u03bbl, m * l) n\n\n#eval power\u2083 2 5\n\ndef append (\u03b1 : Type) : list \u03b1 \u2192 list \u03b1 \u2192 list \u03b1\n| list.nil         ys := ys\n| (list.cons x xs) ys := list.cons x (append xs ys)\n\n#check append\n#eval append _ [3, 1] [4, 1, 5]\n\ndef append\u2082 {\u03b1 : Type} : list \u03b1 \u2192 list \u03b1 \u2192 list \u03b1\n| list.nil         ys := ys\n| (list.cons x xs) ys := list.cons x (append\u2082 xs ys)\n\n#check append\u2082\n#eval append\u2082 [3, 1] [4, 1, 5]\n\n#check @append\u2082\n#eval @append\u2082 _ [3, 1] [4, 1, 5]\n\n/-! Aliases:\n\n    `[]`          := `nil`\n    `x :: xs`     := `cons x xs`\n    `[x\u2081, \u2026, xN]` := `x\u2081 :: \u2026 :: xN :: []` -/\n\ndef append\u2083 {\u03b1 : Type} : list \u03b1 \u2192 list \u03b1 \u2192 list \u03b1\n| []        ys := ys\n| (x :: xs) ys := x :: append\u2083 xs ys\n\ndef reverse {\u03b1 : Type} : list \u03b1 \u2192 list \u03b1\n| []        := []\n| (x :: xs) := reverse xs ++ [x]\n\ndef eval (env : string \u2192 \u2124) : aexp \u2192 \u2124\n| (aexp.num i)     := i\n| (aexp.var x)     := env x\n| (aexp.add e\u2081 e\u2082) := eval e\u2081 + eval e\u2082\n| (aexp.sub e\u2081 e\u2082) := eval e\u2081 - eval e\u2082\n| (aexp.mul e\u2081 e\u2082) := eval e\u2081 * eval e\u2082\n| (aexp.div e\u2081 e\u2082) := eval e\u2081 / eval e\u2082\n\n#eval eval (\u03bbs, 7) (aexp.div (aexp.var \"x\") (aexp.num 0))\n\n/-! Lean only accepts the function definitions for which it can prove\ntermination. In particular, it accepts __structurally recursive__ functions,\nwhich peel off exactly one constructor at a time.\n\n\n## Lemma Statements\n\nNotice the similarity with `def` commands. -/\n\nnamespace sorry_lemmas\n\nlemma add_comm (m n : \u2115) :\n  add m n = add n m :=\nsorry\n\nlemma add_assoc (l m n : \u2115) :\n  add (add l m) n = add l (add m n) :=\nsorry\n\nlemma mul_comm (m n : \u2115) :\n  mul m n = mul n m :=\nsorry\n\nlemma mul_assoc (l m n : \u2115) :\n  mul (mul l m) n = mul l (mul m n) :=\nsorry\n\nlemma mul_add (l m n : \u2115) :\n  mul l (add m n) = add (mul l m) (mul l n) :=\nsorry\n\nlemma reverse_reverse {\u03b1 : Type} (xs : list \u03b1) :\n  reverse (reverse xs) = xs :=\nsorry\n\n/-! Axioms are like lemmas but without proofs (`:= \u2026`). Constant declarations\nare like definitions but without bodies (`:= \u2026`). -/\n\nconstants a b : \u2124\n\naxiom a_less_b :\n  a < b\n\nend sorry_lemmas\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_demo.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4186969093556867, "lm_q2_score": 0.1943678133521021, "lm_q1q2_score": 0.08138120272874813}}
{"text": "/-\nCopyright (c) 2020 Jannis Limperg. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jannis Limperg\n-/\nimport tactic.core\n\n/-!\n# Tactics About Dependencies\n\nThis module provides tactics to compute dependencies and reverse dependencies of\nhypotheses. An expression `e` depends on a hypothesis `h` if `e` would not be\nvalid if `h` were removed from the context. For example, the expression\n`e := x > 0` depends on `x`. We say that `x` is a dependency of `e` and that `e`\nis a reverse dependency of `x`.\n\nIt is sometimes useful to consider *inclusive* dependency: `e` inclusively\ndepends on `h` iff `e` depends on `h` or `e = h` (so inclusive dependency is the\nreflexive closure of regular dependency).\n\nNote that the standard library does not use quite the same terminology:\n\n* `kdependencies`/`kdeps` from the standard library compute reverse\n  dependencies, not dependencies.\n* `kdepends_on` and functions derived from it ignore local definitions and\n  therefore compute a weaker dependency relation (see next section).\n\n## Local Definitions\n\nDetermining dependencies of hypotheses is usually straightforward: a hypothesis\n`r : R` depends on another hypothesis `d : D` if `d` occurs in `R`. The\nimplementation is more involved, however, in the presence of local definitions.\nConsider this context:\n\n```lean\nn m : \u2115\nk : \u2115 := m\no : \u2115 := k\nh : o > 0\n```\n\n`h` depends on `o`, `k` and `m`, but only the dependency on `o` is syntactically\nobvious. `kdepends_on` ignores this complication and claims that `h` does not\ndepend on `k` or `m`. We do not follow this example but process local\ndefinitions properly. This means that if the context contains a local\ndefinition, we need to compute the syntactic dependencies of `h`, then their\ndependencies, and so on.\n\n## Direct Dependencies\n\nIf you want to ignore local definitions while computing dependencies, this\nmodule also provides tactics to find the *direct* dependencies of a hypothesis.\nThese are the hypotheses that syntactically appear in the hypothesis's type (or\nvalue, if the hypothesis is a local definition).\n-/\n\nopen native\nopen expr_set (local_set_to_name_set)\nopen name_set (local_list_to_name_set)\n\nnamespace tactic\n\n/-! ### Direct Dependencies -/\n\n/-! #### Checking whether hypotheses directly depend on each other -/\n\n/--\n`type_has_local_in_name_set h ns` returns true iff the type of `h` contains a\nlocal constant whose unique name appears in `ns`.\n-/\nmeta def type_has_local_in_name_set (h : expr) (ns : name_set) : tactic bool := do\n  h_type \u2190 infer_type h,\n  pure $ h_type.has_local_in ns\n\n/--\n`type_has_local_in_set h hs` returns true iff the type of `h` contains any of\nthe local constants `hs`.\n-/\nmeta def type_has_local_in_set (h : expr) (hs : expr_set) : tactic bool :=\ntype_has_local_in_name_set h $ local_set_to_name_set hs\n\n/--\n`type_has_local_in h hs` returns true iff the type of `h` contains any of the\nlocal constants `hs`.\n-/\nmeta def type_has_local_in (h : expr) (hs : list expr) : tactic bool :=\ntype_has_local_in_name_set h $ local_list_to_name_set hs\n\n/--\n`local_def_value_has_local_in_name_set h ns` returns true iff `h` is a local\ndefinition whose value contains a local constant whose unique name appears in\n`ns`.\n-/\nmeta def local_def_value_has_local_in_name_set (h : expr) (ns : name_set) :\n  tactic bool := do\n  (some h_val) \u2190 try_core $ local_def_value h | pure ff,\n  pure $ h_val.has_local_in ns\n\n/--\n`local_def_value_has_local_in_set h hs` returns true iff `h` is a local\ndefinition whose value contains any of the local constants `hs`.\n-/\nmeta def local_def_value_has_local_in_set (h : expr) (hs : expr_set) :\n  tactic bool :=\nlocal_def_value_has_local_in_name_set h $ local_set_to_name_set hs\n\n/--\n`local_def_value_has_local_in h hs` returns true iff `h` is a local definition\nwhose value contains any of the local constants `hs`.\n-/\nmeta def local_def_value_has_local_in (h : expr) (hs : list expr) :\n  tactic bool :=\nlocal_def_value_has_local_in_name_set h $ local_list_to_name_set hs\n\n/--\n`hyp_directly_depends_on_local_name_set h ns` is true iff the hypothesis `h`\ndirectly depends on a hypothesis whose unique name appears in `ns`.\n-/\nmeta def hyp_directly_depends_on_local_name_set (h : expr) (ns : name_set) :\n  tactic bool :=\nlist.mbor\n  [ type_has_local_in_name_set h ns,\n    local_def_value_has_local_in_name_set h ns ]\n\n/--\n`hyp_directly_depends_on_local_set h hs` is true iff the hypothesis `h` directly\ndepends on any of the hypotheses `hs`.\n-/\nmeta def hyp_directly_depends_on_local_set (h : expr) (hs : expr_set) :\n  tactic bool :=\nhyp_directly_depends_on_local_name_set h $ local_set_to_name_set hs\n\n/--\n`hyp_directly_depends_on_locals h hs` is true iff the hypothesis `h` directly\ndepends on any of the hypotheses `hs`.\n-/\nmeta def hyp_directly_depends_on_locals (h : expr) (hs : list expr) :\n  tactic bool :=\nhyp_directly_depends_on_local_name_set h $ local_list_to_name_set hs\n\n/--\n`hyp_directly_depends_on_local_name_set_inclusive h ns` is true iff the\nhypothesis `h` directly depends on a hypothesis whose unique name appears in\n`ns` or `h`'s name appears in `ns`.\n-/\nmeta def hyp_directly_depends_on_local_name_set_inclusive (h : expr)\n  (ns : name_set) : tactic bool :=\nlist.mbor\n  [ pure $ ns.contains h.local_uniq_name\n  , hyp_directly_depends_on_local_name_set h ns ]\n\n/--\n`hyp_directly_depends_on_local_set_inclusive h ns` is true iff the hypothesis `h`\ndirectly depends on any of the hypotheses `hs` or `h` appears in `hs`.\n-/\nmeta def hyp_directly_depends_on_local_set_inclusive (h : expr) (hs : expr_set) :\n  tactic bool :=\nhyp_directly_depends_on_local_name_set_inclusive h $ local_set_to_name_set hs\n\n/--\n`hyp_directly_depends_on_locals_inclusive h ns` is true iff the hypothesis `h`\ndirectly depends on any of the hypotheses `hs` or `h` appears in `hs`.\n-/\nmeta def hyp_directly_depends_on_locals_inclusive (h : expr) (hs : list expr) :\n  tactic bool :=\nhyp_directly_depends_on_local_name_set_inclusive h $ local_list_to_name_set hs\n\n\n/-! #### Computing the direct dependencies of a hypothesis -/\n\n/--\n`direct_dependency_set_of_hyp h` is the set of hypotheses that the hypothesis\n`h` directly depends on. These are the hypotheses that appear in `h`'s type or\nvalue (if `h` is a local definition).\n-/\nmeta def direct_dependency_set_of_hyp (h : expr) : tactic expr_set := do\n  t \u2190 infer_type h,\n  let deps := t.list_local_consts',\n  (some val) \u2190 try_core $ local_def_value h | pure deps,\n  let deps := deps.union val.list_local_consts',\n  pure deps\n\n/--\n`direct_dependency_name_set_of_hyp h` is the set of unique names of hypotheses\nthat the hypothesis `h` directly depends on. These are the hypotheses that\nappear in `h`'s type or value (if `h` is a local definition).\n-/\nmeta def direct_dependency_name_set_of_hyp (h : expr) : tactic name_set :=\nlocal_set_to_name_set <$> direct_dependency_set_of_hyp h\n\n/--\n`direct_dependencies_of_hyp h` is the list of hypotheses that the hypothesis `h`\ndirectly depends on. These are the hypotheses that appear in `h`'s type or value\n(if `h` is a local definition). The dependencies are returned in no particular\norder.\n-/\nmeta def direct_dependencies_of_hyp (h : expr) : tactic (list expr) :=\nrb_set.to_list <$> direct_dependency_set_of_hyp h\n\n/--\n`direct_dependency_set_of_hyp_inclusive h` is the set of hypotheses that the\nhypothesis `h` directly depends on, plus `h` itself.\n-/\nmeta def direct_dependency_set_of_hyp_inclusive (h : expr) : tactic expr_set := do\n  deps \u2190 direct_dependency_set_of_hyp h,\n  pure $ deps.insert h\n\n/--\n`direct_dependency_name_set_of_hyp_inclusive h` is the set of unique names of\nhypotheses that the hypothesis `h` directly depends on, plus `h` itself.\n-/\nmeta def direct_dependency_name_set_of_hyp_inclusive (h : expr) :\n  tactic name_set :=\nlocal_set_to_name_set <$> direct_dependency_set_of_hyp_inclusive h\n\n/--\n`direct_dependencies_of_hyp_inclusive h` is the list of hypotheses that the\nhypothesis `h` directly depends on, plus `h` itself. The dependencies are\nreturned in no particular order.\n-/\nmeta def direct_dependencies_of_hyp_inclusive (h : expr) : tactic (list expr) :=\nrb_set.to_list <$> direct_dependency_set_of_hyp_inclusive h\n\n\n/-! ### Indirect/Transitive Dependencies -/\n\n/-! #### Checking whether hypotheses depend on each other -/\n\n/--\n`hyp_depends_on_local_name_set' cache h ns` is true iff `h` depends on any of\nthe hypotheses whose unique names appear in `ns`. `cache` must be a set of\nhypotheses known *not* to depend (even indirectly) on any of the `ns`. This is\na performance optimisation, so you can give an empty cache. The tactic also\nreturns an expanded cache with hypotheses which the tactic has encountered.\n\nYou probably want to use `tactic.hyp_depends_on_local_name_set` or\n`tactic.hyps_depend_on_local_name_set` instead of this tactic.\n-/\nmeta def hyp_depends_on_local_name_set' : expr_set \u2192 expr \u2192 name_set \u2192\n  tactic (bool \u00d7 expr_set) := \u03bb cache h ns, do\n  ff \u2190 pure $ cache.contains h | pure (ff, cache),\n  direct_deps \u2190 direct_dependency_set_of_hyp h,\n  let has_dep := direct_deps.fold ff (\u03bb d b, b || ns.contains d.local_uniq_name),\n  ff \u2190 pure has_dep | pure (tt, cache),\n  (has_dep, cache) \u2190 direct_deps.mfold (ff, cache) $ \u03bb d \u27e8b, cache\u27e9,\n    if b\n      then pure (tt, cache)\n      else hyp_depends_on_local_name_set' cache d ns,\n  if has_dep\n    then pure (tt, cache)\n    else pure (ff, cache.insert h)\n\n/--\n`hyp_depends_on_local_name_set h ns` is true iff the hypothesis `h` depends on\nany of the hypotheses whose unique names appear in `ns`. If you need to check\ndependencies of multiple hypotheses, use `tactic.hyps_depend_on_local_name_set`.\n-/\nmeta def hyp_depends_on_local_name_set (h : expr) (ns : name_set) : tactic bool := do\n  ctx_has_local_def \u2190 context_upto_hyp_has_local_def h,\n  if ctx_has_local_def\n    then prod.fst <$> hyp_depends_on_local_name_set' mk_expr_set h ns\n    else hyp_directly_depends_on_local_name_set h ns\n\n/--\n`hyp_depends_on_local_set h hs` is true iff the hypothesis `h` depends on\nany of the hypotheses `hs`. If you need to check dependencies of multiple\nhypotheses, use `tactic.hyps_depend_on_local_set`.\n-/\nmeta def hyp_depends_on_local_set (h : expr) (hs : expr_set) : tactic bool :=\nhyp_depends_on_local_name_set h $ local_set_to_name_set hs\n\n/--\n`hyp_depends_on_locals h hs` is true iff the hypothesis `h` depends on any of\nthe hypotheses `hs`. If you need to check dependencies of multiple hypotheses,\nuse `tactic.hyps_depend_on_locals`.\n-/\nmeta def hyp_depends_on_locals (h : expr) (hs : list expr) : tactic bool :=\nhyp_depends_on_local_name_set h $ local_list_to_name_set hs\n\n/--\n`hyps_depend_on_local_name_set hs ns` returns, for each `h \u2208 hs`, whether `h`\ndepends on a hypothesis whose unique name appears in `ns`. This is the same as\n(but more efficient than) calling `tactic.hyp_depends_on_local_name_set` for\nevery `h \u2208 hs`.\n-/\nmeta def hyps_depend_on_local_name_set (hs : list expr) (ns : name_set) :\n  tactic (list bool) := do\n  ctx_has_local \u2190 context_has_local_def,\n  if ctx_has_local\n    then\n      let go : expr \u2192 list bool \u00d7 expr_set \u2192 tactic (list bool \u00d7 expr_set) :=\n      \u03bb h \u27e8deps, cache\u27e9, do {\n        (h_dep, cache) \u2190 hyp_depends_on_local_name_set' cache h ns,\n        pure (h_dep :: deps, cache)\n      } in\n      prod.fst <$> hs.mfoldr go ([], mk_expr_map)\n    else hs.mmap $ \u03bb h, hyp_directly_depends_on_local_name_set h ns\n\n/--\n`hyps_depend_on_local_set hs is` returns, for each `h \u2208 hs`, whether `h` depends\non any of the hypotheses `is`. This is the same as (but more efficient than)\ncalling `tactic.hyp_depends_on_local_set` for every `h \u2208 hs`.\n-/\nmeta def hyps_depend_on_local_set (hs : list expr) (is : expr_set) :\n  tactic (list bool) :=\nhyps_depend_on_local_name_set hs $ local_set_to_name_set is\n\n/--\n`hyps_depend_on_locals hs is` returns, for each `h \u2208 hs`, whether `h` depends\non any of the hypotheses `is`. This is the same as (but more efficient than)\ncalling `tactic.hyp_depends_on_locals` for every `h \u2208 hs`.\n-/\nmeta def hyps_depend_on_locals (hs is : list expr) : tactic (list bool) :=\nhyps_depend_on_local_name_set hs $ local_list_to_name_set is\n\n/--\n`hyp_depends_on_local_name_set_inclusive' cache h ns` is true iff the hypothesis\n`h` inclusively depends on a hypothesis whose unique name appears in `ns`.\n`cache` must be a set of hypotheses known *not* to depend (even indirectly) on\nany of the `ns`. This is a performance optimisation, so you can give an empty\ncache. The tactic also returns an expanded cache with hypotheses which the\ntactic has encountered. Note that the cache records exclusive, not inclusive\ndependencies.\n\nYou probably want to use `tactic.hyp_depends_on_local_name_set_inclusive` or\n`tactic.hyps_depend_on_local_name_set_inclusive` instead of this tactic.\n-/\nmeta def hyp_depends_on_local_name_set_inclusive' (cache : expr_set) (h : expr)\n  (ns : name_set) : tactic (bool \u00d7 expr_set) :=\nif ns.contains h.local_uniq_name\n  then pure (tt, cache)\n  else hyp_depends_on_local_name_set' cache h ns\n\n/--\n`hyp_depends_on_local_name_set_inclusive h ns` is true iff the hypothesis `h`\ninclusively depends on any of the hypotheses whose unique names appear in `ns`.\nIf you need to check the dependencies of multiple hypotheses, use\n`tactic.hyps_depend_on_local_name_set_inclusive`.\n-/\nmeta def hyp_depends_on_local_name_set_inclusive (h : expr) (ns : name_set) :\n  tactic bool :=\nlist.mbor\n  [ pure $ ns.contains h.local_uniq_name,\n    hyp_depends_on_local_name_set h ns ]\n\n/--\n`hyp_depends_on_local_set_inclusive h hs` is true iff the hypothesis `h`\ninclusively depends on any of the hypotheses `hs`. If you need to check\ndependencies of multiple hypotheses, use\n`tactic.hyps_depend_on_local_set_inclusive`.\n-/\nmeta def hyp_depends_on_local_set_inclusive (h : expr) (hs : expr_set) :\n  tactic bool :=\nhyp_depends_on_local_name_set_inclusive h $ local_set_to_name_set hs\n\n/--\n`hyp_depends_on_locals_inclusive h hs` is true iff the hypothesis `h`\ninclusively depends on any of the hypotheses `hs`. If you need to check\ndependencies of multiple hypotheses, use\n`tactic.hyps_depend_on_locals_inclusive`.\n-/\nmeta def hyp_depends_on_locals_inclusive (h : expr) (hs : list expr) :\n  tactic bool :=\nhyp_depends_on_local_name_set_inclusive h $ local_list_to_name_set hs\n\n/--\n`hyps_depend_on_local_name_set_inclusive hs ns` returns, for each `h \u2208 hs`,\nwhether `h` inclusively depends on a hypothesis whose unique name appears in\n`ns`. This is the same as (but more efficient than) calling\n`tactic.hyp_depends_on_local_name_set_inclusive` for every `h \u2208 hs`.\n-/\nmeta def hyps_depend_on_local_name_set_inclusive (hs : list expr) (ns : name_set) :\n  tactic (list bool) := do\n  ctx_has_local \u2190 context_has_local_def,\n  if ctx_has_local\n    then\n      let go : expr \u2192 list bool \u00d7 expr_set \u2192 tactic (list bool \u00d7 expr_set) :=\n      \u03bb h \u27e8deps, cache\u27e9, do {\n        (h_dep, cache) \u2190 hyp_depends_on_local_name_set_inclusive' cache h ns,\n        pure (h_dep :: deps, cache)\n      } in\n      prod.fst <$> hs.mfoldr go ([], mk_expr_map)\n    else\n      hs.mmap $ \u03bb h, hyp_directly_depends_on_local_name_set_inclusive h ns\n\n/--\n`hyps_depend_on_local_set_inclusive hs is` returns, for each `h \u2208 hs`, whether\n`h` depends inclusively on any of the hypotheses `is`. This is the same as\n(but more efficient than) calling `tactic.hyp_depends_on_local_set_inclusive`\nfor every `h \u2208 hs`.\n-/\nmeta def hyps_depend_on_local_set_inclusive (hs : list expr) (is : expr_set) :\n  tactic (list bool) :=\nhyps_depend_on_local_name_set_inclusive hs $ local_set_to_name_set is\n\n/--\n`hyps_depend_on_locals_inclusive hs is` returns, for each `h \u2208 hs`, whether `h`\ndepends inclusively on any of the hypotheses `is`. This is the same as (but more\nefficient than) calling `tactic.hyp_depends_on_locals_inclusive` for every\n`h \u2208 hs`.\n-/\nmeta def hyps_depend_on_locals_inclusive (hs is : list expr) : tactic (list bool) :=\nhyps_depend_on_local_name_set_inclusive hs $ local_list_to_name_set is\n\n\n/-! #### Computing the dependencies of a hypothesis -/\n\n/--\n`dependency_set_of_hyp' cache h` is the set of dependencies of the hypothesis\n`h`. `cache` is a map from hypotheses to all their dependencies (including\nindirect dependencies). This is a performance optimisation, so you can give an\nempty cache. The tactic also returns an expanded cache with hypotheses which\nthe tactic has encountered.\n\nYou probably want to use `tactic.dependency_set_of_hyp` or\n`tactic.dependency_sets_of_hyps` instead of this tactic.\n-/\nmeta def dependency_set_of_hyp' : expr_map expr_set \u2192 expr \u2192\n  tactic (expr_set \u00d7 expr_map expr_set) := \u03bb cache h, do\n  match cache.find h with\n  | some deps := pure (deps, cache)\n  | none := do\n    direct_deps \u2190 direct_dependency_set_of_hyp h,\n    (deps, cache) \u2190\n      direct_deps.mfold (direct_deps, cache) $ \u03bb h' \u27e8deps, cache\u27e9, do {\n        (deps', cache) \u2190 dependency_set_of_hyp' cache h',\n        pure (deps.union deps', cache)\n      },\n    pure (deps, cache.insert h deps)\n  end\n\n/--\n`dependency_set_of_hyp h` is the set of dependencies of the hypothesis `h`. If\nyou need the dependencies of multiple hypotheses, use\n`tactic.dependency_sets_of_hyps`.\n-/\nmeta def dependency_set_of_hyp (h : expr) : tactic expr_set := do\n  ctx_has_local \u2190 context_upto_hyp_has_local_def h,\n  if ctx_has_local\n    then prod.fst <$> dependency_set_of_hyp' mk_expr_map h\n    else direct_dependency_set_of_hyp h\n\n/--\n`dependency_name_set_of_hyp h` is the set of unique names of the dependencies of\nthe hypothesis `h`. If you need the dependencies of multiple hypotheses, use\n`tactic.dependency_name_sets_of_hyps`.\n-/\nmeta def dependency_name_set_of_hyp (h : expr) : tactic name_set :=\nlocal_set_to_name_set <$> dependency_set_of_hyp h\n\n/--\n`dependencies_of_hyp h` is the list of dependencies of the hypothesis `h`.\nThe dependencies are returned in no particular order. If you need the\ndependencies of multiple hypotheses, use `tactic.dependencies_of_hyps`.\n-/\nmeta def dependencies_of_hyp (h : expr) : tactic (list expr) :=\nrb_set.to_list <$> dependency_set_of_hyp h\n\n/--\n`dependency_sets_of_hyps hs` returns, for each `h \u2208 hs`, the set of dependencies\nof `h`. This is the same as (but more performant than) using\n`tactic.dependency_set_of_hyp` on every `h \u2208 hs`.\n-/\nmeta def dependency_sets_of_hyps (hs : list expr) : tactic (list expr_set) := do\n  ctx_has_def \u2190 context_has_local_def,\n  if ctx_has_def\n    then\n      let go : expr \u2192 list expr_set \u00d7 expr_map expr_set \u2192\n        tactic (list expr_set \u00d7 expr_map expr_set) := do\n      \u03bb h \u27e8deps, cache\u27e9, do {\n          (h_deps, cache) \u2190 dependency_set_of_hyp' cache h,\n          pure (h_deps :: deps, cache)\n      } in\n      prod.fst <$> hs.mfoldr go ([], mk_expr_map)\n    else\n      hs.mmap direct_dependency_set_of_hyp\n\n/--\n`dependency_name_sets_of_hyps hs` returns, for each `h \u2208 hs`, the set of unique\nnames of the dependencies of `h`. This is the same as (but more performant than)\nusing `tactic.dependency_name_set_of_hyp` on every `h \u2208 hs`.\n-/\nmeta def dependency_name_sets_of_hyps (hs : list expr) : tactic (list name_set) :=\nlist.map local_set_to_name_set <$> dependency_sets_of_hyps hs\n\n/--\n`dependencies_of_hyps hs` returns, for each `h \u2208 hs`, the dependencies of `h`.\nThe dependencies appear in no particular order in the returned lists. This is\nthe same as (but more performant than) using `tactic.dependencies_of_hyp` on\nevery `h \u2208 hs`.\n-/\nmeta def dependencies_of_hyps (hs : list expr) : tactic (list (list expr)) :=\nlist.map rb_set.to_list <$> dependency_sets_of_hyps hs\n\n/--\n`dependency_set_of_hyp_inclusive' cache h` is the set of dependencies of the\nhypothesis `h`, plus `h` itself. `cache` is a map from hypotheses to all their\ndependencies (including indirect dependencies). This is a performance\noptimisation, so you can give an empty cache. The tactic also returns an\nexpanded cache with hypotheses which the tactic has encountered. Note that the\ncache records exclusive, not inclusive dependencies.\n\nYou probably want to use `tactic.dependency_set_of_hyp_inclusive` or\n`tactic.dependency_sets_of_hyps_inclusive` instead of this tactic.\n-/\nmeta def dependency_set_of_hyp_inclusive' (cache : expr_map expr_set) (h : expr) :\n  tactic (expr_set \u00d7 expr_map expr_set) := do\n  (deps, cache) \u2190 dependency_set_of_hyp' cache h,\n  pure (deps.insert h, cache)\n\n/--\n`dependency_set_of_hyp_inclusive h` is the set of dependencies of the hypothesis\n`h`, plus `h` itself. If you need the dependencies of multiple hypotheses, use\n`tactic.dependency_sets_of_hyps_inclusive`.\n-/\nmeta def dependency_set_of_hyp_inclusive (h : expr) : tactic expr_set := do\n  deps \u2190 dependency_set_of_hyp h,\n  pure $ deps.insert h\n\n/--\n`dependency_name_set_of_hyp_inclusive h` is the set of unique names of the\ndependencies of the hypothesis `h`, plus the unique name of `h` itself. If you\nneed the dependencies of multiple hypotheses, use\n`tactic.dependency_name_sets_of_hyps_inclusive`.\n-/\nmeta def dependency_name_set_of_hyp_inclusive (h : expr) : tactic name_set :=\nlocal_set_to_name_set <$> dependency_set_of_hyp_inclusive h\n\n/--\n`dependencies_of_hyp_inclusive h` is the list of dependencies of the hypothesis\n`h`, plus `h` itself. The dependencies are returned in no particular order. If\nyou need the dependencies of multiple hypotheses, use\n`tactic.dependencies_of_hyps_inclusive`.\n-/\nmeta def dependencies_of_hyp_inclusive (h : expr) : tactic (list expr) :=\nrb_set.to_list <$> dependency_set_of_hyp_inclusive h\n\n/--\n`dependency_sets_of_hyps_inclusive hs` returns, for each `h \u2208 hs`, the\ndependencies of `h`, plus `h` itself. This is the same as (but more performant\nthan) using `tactic.dependency_set_of_hyp_inclusive` on every `h \u2208 hs`.\n-/\nmeta def dependency_sets_of_hyps_inclusive (hs : list expr) :\n  tactic (list expr_set) := do\n  ctx_has_def \u2190 context_has_local_def,\n  if ctx_has_def\n    then\n      let go : expr \u2192 list expr_set \u00d7 expr_map expr_set \u2192\n        tactic (list expr_set \u00d7 expr_map expr_set) :=\n      \u03bb h \u27e8deps, cache\u27e9, do {\n        (h_deps, cache) \u2190 dependency_set_of_hyp_inclusive' cache h,\n        pure (h_deps :: deps, cache)\n      } in\n      prod.fst <$> hs.mfoldr go ([], mk_expr_map)\n    else\n      hs.mmap direct_dependency_set_of_hyp_inclusive\n\n/--\n`dependency_name_sets_of_hyps_inclusive hs` returns, for each `h \u2208 hs`, the\nunique names of the dependencies of `h`, plus the unique name of `h` itself.\nThis is the same as (but more performant than) using\n`tactic.dependency_name_set_of_hyp_inclusive` on every `h \u2208 hs`.\n-/\nmeta def dependency_name_sets_of_hyps_inclusive (hs : list expr) :\n  tactic (list name_set) :=\nlist.map local_set_to_name_set <$> dependency_sets_of_hyps_inclusive hs\n\n/--\n`dependencies_of_hyps_inclusive hs` returns, for each `h \u2208 hs`, the dependencies\nof `h`, plus `h` itself. The dependencies appear in no particular order in the\nreturned lists. This is the same as (but more performant than) using\n`tactic.dependencies_of_hyp_inclusive` on every `h \u2208 hs`.\n-/\nmeta def dependencies_of_hyps_inclusive (hs : list expr) :\n  tactic (list (list expr)) :=\nlist.map rb_set.to_list <$> dependency_sets_of_hyps_inclusive hs\n\n\n/-! #### Computing the reverse dependencies of a hypothesis -/\n\nprivate meta def reverse_dependencies_of_hyp_name_set_aux (hs : name_set) :\n  list expr \u2192 list expr \u2192 name_set \u2192 tactic (list expr)\n| [] revdeps _ := pure revdeps.reverse\n| (H :: Hs) revdeps ns := do\n  let H_uname := H.local_uniq_name,\n  H_is_revdep \u2190 list.mband\n    [ pure $ \u00ac hs.contains H_uname,\n      hyp_directly_depends_on_local_name_set H ns ],\n  if H_is_revdep\n    then\n      reverse_dependencies_of_hyp_name_set_aux Hs (H :: revdeps)\n        (ns.insert H_uname)\n    else\n      reverse_dependencies_of_hyp_name_set_aux Hs revdeps ns\n\n/--\n`reverse_dependencies_of_hyp_name_set hs` is the list of reverse dependencies of\nthe hypotheses whose unique names appear in `hs`, excluding the `hs` themselves.\nThe reverse dependencies are returned in the order in which they appear in the\ncontext.\n-/\nmeta def reverse_dependencies_of_hyp_name_set (hs : name_set) :\n  tactic (list expr) := do\n  ctx \u2190 local_context,\n  let ctx := ctx.after (\u03bb h, hs.contains h.local_uniq_name),\n  reverse_dependencies_of_hyp_name_set_aux hs ctx [] hs\n\n/--\n`reverse_dependencies_of_hyp_set hs` is the list of reverse dependencies of the\nhypotheses `hs`, excluding the `hs` themselves. The reverse dependencies are\nreturned in the order in which they appear in the context.\n-/\nmeta def reverse_dependencies_of_hyp_set (hs : expr_set) : tactic (list expr) :=\nreverse_dependencies_of_hyp_name_set $ local_set_to_name_set hs\n\n/--\n`reverse_dependencies_of_hyps hs` is the list of reverse dependencies of the\nhypotheses `hs`, excluding the `hs` themselves. The reverse dependencies are\nreturned in the order in which they appear in the context.\n-/\nmeta def reverse_dependencies_of_hyps (hs : list expr) : tactic (list expr) :=\nreverse_dependencies_of_hyp_name_set $ local_list_to_name_set hs\n\nprivate meta def reverse_dependencies_of_hyp_name_set_inclusive_aux :\n  list expr \u2192 list expr \u2192 name_set \u2192 tactic (list expr)\n| [] revdeps _ := pure revdeps.reverse\n| (H :: Hs) revdeps ns := do\n  let H_uname := H.local_uniq_name,\n  H_is_revdep \u2190 list.mbor\n    [ pure $ ns.contains H.local_uniq_name,\n      hyp_directly_depends_on_local_name_set H ns ],\n  if H_is_revdep\n    then\n      reverse_dependencies_of_hyp_name_set_inclusive_aux Hs (H :: revdeps)\n        (ns.insert H_uname)\n    else\n      reverse_dependencies_of_hyp_name_set_inclusive_aux Hs revdeps ns\n\n/--\n`reverse_dependencies_of_hyp_name_set_inclusive hs` is the list of reverse\ndependencies of the hypotheses whose unique names appear in `hs`, including the\n`hs` themselves. The reverse dependencies are returned in the order in which\nthey appear in the context.\n-/\nmeta def reverse_dependencies_of_hyp_name_set_inclusive (hs : name_set) :\n  tactic (list expr) := do\n  ctx \u2190 local_context,\n  let ctx := ctx.drop_while (\u03bb h, \u00ac hs.contains h.local_uniq_name),\n  reverse_dependencies_of_hyp_name_set_inclusive_aux ctx [] hs\n\n/--\n`reverse_dependencies_of_hyp_set_inclusive hs` is the list of reverse\ndependencies of the hypotheses `hs`, including the `hs` themselves. The\ninclusive reverse dependencies are returned in the order in which they appear in\nthe context.\n-/\nmeta def reverse_dependencies_of_hyp_set_inclusive (hs : expr_set) :\n  tactic (list expr) :=\nreverse_dependencies_of_hyp_name_set_inclusive $ local_set_to_name_set hs\n\n/--\n`reverse_dependencies_of_hyps_inclusive hs` is the list of reverse dependencies\nof the hypotheses `hs`, including the `hs` themselves. The reverse dependencies\nare returned in the order in which they appear in the context.\n-/\nmeta def reverse_dependencies_of_hyps_inclusive (hs : list expr) :\n  tactic (list expr) :=\nreverse_dependencies_of_hyp_name_set_inclusive $ local_list_to_name_set hs\n\n\n/-! ### Reverting a hypothesis and its reverse dependencies -/\n\n/--\n`revert_name_set hs` reverts the hypotheses whose unique names appear in `hs`,\nas well as any hypotheses that depend on them. Returns the number of reverted\nhypotheses and a list containing these hypotheses. The reverted hypotheses are\nreturned in the order in which they used to appear in the context and are\nguaranteed to store the correct type (see `tactic.update_type`).\n-/\nmeta def revert_name_set (hs : name_set) : tactic (\u2115 \u00d7 list expr) := do\n  to_revert \u2190 reverse_dependencies_of_hyp_name_set_inclusive hs,\n  to_revert_with_types \u2190 to_revert.mmap update_type,\n  num_reverted \u2190 revert_lst to_revert,\n  pure (num_reverted, to_revert_with_types)\n\n/--\n`revert_set hs` reverts the hypotheses `hs`, as well as any hypotheses that\ndepend on them. Returns the number of reverted hypotheses and a list containing\nthese hypotheses. The reverted hypotheses are returned in the order in which\nthey used to appear in the context and are guaranteed to store the correct type\n(see `tactic.update_type`).\n-/\nmeta def revert_set (hs : expr_set) : tactic (\u2115 \u00d7 list expr) :=\nrevert_name_set $ local_set_to_name_set hs\n\n/--\n`revert_lst' hs` reverts the hypotheses `hs`, as well as any hypotheses that\ndepend on them. Returns the number of reverted hypotheses and a list containing\nthese hypotheses. The reverted hypotheses are returned in the order in which\nthey used to appear in the context and are guaranteed to store the correct type\n(see `tactic.update_type`).\n\nThis is a more informative version of `tactic.revert_lst`.\n-/\nmeta def revert_lst' (hs : list expr) : tactic (\u2115 \u00d7 list expr) :=\nrevert_name_set $ local_list_to_name_set hs\n\n/--\n`revert_reverse_dependencies_of_hyp h` reverts all the hypotheses that depend on\nthe hypothesis `h`, including the local definitions that have `h` in their\nvalue. This fixes a bug in `tactic.revert_kdependencies` that does not revert\nlocal definitions for which `h` only appears in the value. Returns the number\nof reverted hypotheses.\n-/\n/- We cannot implement it as `revert e >> intro1` because that would change the\nlocal constant in the context. -/\nmeta def revert_reverse_dependencies_of_hyp (h : expr) : tactic \u2115 :=\nreverse_dependencies_of_hyp_name_set (mk_name_set.insert h.local_uniq_name) >>=\n  revert_lst\n\n/--\n`revert_reverse_dependencies_of_hyp_name_set hs` reverts all the hypotheses that\ndepend on a hypothesis whose unique name appears in `hs`. The `hs` themselves\nare not reverted, unless they depend on each other. Returns the number of\nreverted hypotheses.\n-/\nmeta def revert_reverse_dependencies_of_hyp_name_set (hs : name_set) : tactic \u2115 :=\nreverse_dependencies_of_hyp_name_set hs >>= revert_lst\n\n/--\n`revert_reverse_dependencies_of_hyp_set hs` reverts all the hypotheses that\ndepend on a hypothesis in `hs`. The `hs` themselves are not reverted, unless\nthey depend on each other. Returns the number of reverted hypotheses.\n-/\nmeta def revert_reverse_dependencies_of_hyp_set (hs : expr_set) : tactic \u2115 :=\nreverse_dependencies_of_hyp_set hs >>= revert_lst\n\n/--\n`revert_reverse_dependencies_of_hyp hs` reverts all the hypotheses that depend\non a hypothesis in `hs`. The `hs` themselves are not reverted, unless they\ndepend on each other. Returns the number of reverted hypotheses.\n-/\nmeta def revert_reverse_dependencies_of_hyps (hs : list expr) : tactic \u2115 :=\nreverse_dependencies_of_hyps hs >>= revert_lst\n\nend tactic\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/tactic/dependencies.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.49609382947091946, "lm_q2_score": 0.1623800346399628, "lm_q1q2_score": 0.08055573321415971}}
{"text": "\nimport data.dlist\n\n/- We want to write a set of tactics to automate the construction of\n   instances. One approach might be to automate the specific instances\n   that we are interested in. Instead, we will try to build reusable\n   components and aim for wide applicability.\n\n   Our first example of application is the construction of a `functor`\n   instance for `(_ \u2295)`. We would like the instance to look like:\n\n```\ninstance {\u03b1 : Type*} : functor (sum \u03b1) :=\nbegin\n  refineS { map := sum.map_right }, -- `refineS` focuses on structure fields\n  field map_id\n  { ... },\n  field map_comp\n  { ... },\nend\n```\n\n   This structure is similar to the `cases / case id` syntax and we\n   will use its underlying machinery to make our tactic work. We can\n   identify two parts of the Lean API that we need to understand in\n   order to make this work\n     1. structure syntax and accessing field information\n     2. using goal tagging\n\n   Often, writing a new tactic involves using parts of the API that\n   we're not fully familiar with so we're better get used to\n   experimenting, making small prototypes to check if our\n   understanding is correct. In the above example, we'd like to get\n   the list of fields that are specified in the expression given to\n   `refineS` and we'd like to know what are the fields that are\n   missing in some sense. Let's start with the former.\n\n   We set off to explore the core library and \"grep\" our way to useful\n   functions:\n\n```\nlibrary $ grep -R . -e structure\n```\n\n   The above is a miss: most of what we're getting is the list of\n   structure declaration. What if we included underscores?\n\n```\nlibrary $ grep -R . -e _structure\n```\n\n   It's a hit! We're getting\n   https://github.com/leanprover/lean/blob/master/library/init/meta/pexpr.lean#L22-L31\n   which looks promising. We reproduce the context below:\n\n```\n/-- Information about unelaborated structure instance expressions. -/\nmeta structure structure_instance_info :=\n(struct       : option name := none)\n(field_names  : list name)\n(field_values : list pexpr)\n(sources      : list pexpr := [])\n\n/-- Create a structure instance expression. -/\nmeta constant pexpr.mk_structure_instance : structure_instance_info \u2192 pexpr\nmeta constant pexpr.get_structure_instance_info : pexpr \u2192 option structure_instance_info\n```\n\nLet's still try one more regex:\n\n```\nlibrary $ grep -R . -e structure_\n```\n\n   We get more interesting stuff: https://github.com/leanprover/lean/blob/master/library/init/meta/environment.lean#L86-L87\n\n```\n/-- Return the fields of the structure with the given name, or `none` if it is not a structure -/\nmeta constant structure_fields : environment \u2192 name \u2192 option (list name)\n```\n\n   Let us see what `pexpr.get_structure_instance_info` tells us about `{ map := sum.map_right }`.\n   We start by writing our tactic's type:\n-/\nopen tactic\n\nuniverses u v\n\nexample {\u03b1 : Type u} : functor (sum \u03b1) :=\nbegin\n  (do let e : pexpr := ``({ map := sum.map_right }),\n      str \u2190 e.get_structure_instance_info,\n      trace str.field_names,\n      trace str.field_values),\n    -- prints:\n    -- [map]\n    -- [field_notation sum]\n  admit\nend\n\n/- It's a success! It's important to see that `refineS\u2080` does no type\n   checking on the expression we given it. This is why we get no error\n   messages for `sum.map_right` not existing yet. `refineS\u2080`'s `e`\n   argument is of type `parse texpr` which is definitionally equal to\n   `pexpr`, the result of parsing an expression but doing no\n   elaboration or type checking. In `parse texpr`, the type of `texpr`\n   is `texpr : lean.parser pexpr` where `lean.parser` is the parsing\n   monad.  Without going into details into `monads` as a programming\n   construct or a mathematical object, objects of type `lean.parser \u03b1`\n   are programs with access to the Lean parsing infrastructure\n   including the current position of the parser and various parsing\n   routines and the result of the program is of type `\u03b1`. Lean takes\n   `parse texpr` as a hint that the tactic will be used interactively\n   (as opposed to called by other tactics) and `texpr` is the parser\n   to use to construct argument `e`. Other useful parsers can be found\n   at https://github.com/leanprover/lean/blob/master/library/init/meta/interactive_base.lean#L69-L80.\n\n   Our next experiment is to list the expected fields for the type\n   that we are attempting to build. `environment.structure_fields`\n   will help us in this:\n\n```\nmeta constant structure_fields : environment \u2192 name \u2192 option (list name)\n```\n\n   What argument do we give it? Searching in `init/meta/tactic.lean`\n   shows us that `get_env` will provide the first argument. The second\n   argument is the type of the object that we're trying to build. More\n   specifically, the name of that type.  If we call `target`, that\n   type will be provided to us as an expression. In our case `functor\n   (sum \u03b1)`. This expression contains multiple names. Which one do we\n   want? Let us look at it closely by using `to_raw_fmt` to avoid any\n   pretty printing and just get the syntax tree:\n\n-/\n\nexample {\u03b1 : Type u} : functor.{v} (sum \u03b1) :=\n  -- (we use `.{v}` above so that Lean won't have to guess the universes which would\n  -- create visual noise\nbegin\n  (do let e : pexpr := ``({ map := sum.map_right }),\n      tgt \u2190 target,\n      trace tgt.to_raw_fmt),\n    -- prints:\n    -- (app (const functor [v, max u v])\n    --      (app (const sum [u, v])\n    --           (local_const _ \u03b1 (const 1 []))))\n  admit\nend\n\n/- The printout shows us that our current type of interest is a\n   function application whose function is `functor` and whose argument\n   is another application. We don't actually care about any of the\n   structure of the argument. The function is not always exactly what\n   we care about either.  Consider for example `vector_space \u03b1 \u03b2`. The\n   function in the top-most application is `vector_space \u03b1` so we have\n   to look at its function in order to land on `vector_space`\n   itself. Does that mean that we have to look at the function of the\n   function?  Let us consider a type class with the three arguments:\n   `my_class \u03b1 \u03b2 \u03b3`. The function of its function is `my_class \u03b1`. In\n   short, we should keep retrieving functions until we run out of\n   function applications. The result should be a constant and the name\n   of that constant is what we need. If it is anything else than a\n   constant, the user made a mistake and we will say no more of it\n   here but various approaches can be taken for informative error\n   messages.\n\n   In `init/meta/expr.lean`, in the core library a function is\n   provided to retrieve a function in a multi-argument function\n   application: `expr.get_app_fn`.  It gives us another expression\n   which we expect should be a constant. `expr.const_name` will reveal\n   it's name.\n\n-/\nopen nat\nmeta def mk_mvar_list : \u2115 \u2192 tactic (list expr)\n | 0 := pure []\n | (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n\n\nopen interactive interactive.types lean.parser\n     tactic.interactive (itactic)\n\n-- meta def tactic.interactive.refineS (e : parse texpr) : tactic unit :=\n-- do    str \u2190 e.get_structure_instance_info,\n--       trace str.struct,\n--       trace str.sources,\n--       trace str.field_names,\n--       tgt \u2190 target,\n--       let struct_n : name := tgt.get_app_fn.const_name,\n--       env \u2190 get_env,\n--       exp_fields \u2190 env.structure_fields struct_n,\n--       let missing_f := (exp_fields.diff str.field_names).diff [`to_has_map],\n--       vs \u2190 mk_mvar_list missing_f.length,\n--       let e' : pexpr := pexpr.mk_structure_instance\n--           { -- struct := some struct_n\n--           -- ,\n--           field_names := str.field_names ++ missing_f\n--           , field_values := str.field_values ++ vs.map to_pexpr },\n--       refine ``(%%e' : %%tgt)\n\nmeta def var_names : expr \u2192 list name\n | (expr.pi n _ _ b) := n :: var_names b\n | _ := []\n\nmeta def drop_binders : expr \u2192 tactic expr\n | (expr.pi n bi t b) := b.instantiate_var <$> mk_local' n bi t >>= drop_binders\n | e := pure e\n\nmeta def subobject_names (struct_n : name) : tactic (list name \u00d7 list name) :=\ndo env \u2190 get_env,\n   [c] \u2190 pure $ env.constructors_of struct_n | fail \"too many constructors\",\n   vs  \u2190 var_names <$> (mk_const c >>= infer_type),\n   fields \u2190 env.structure_fields struct_n,\n   return $ fields.partition (\u03bb fn, \u2191(\"_\" ++ fn.to_string) \u2208 vs)\n\ndef dlist.join {\u03b1} : list (dlist \u03b1) \u2192 dlist \u03b1\n | [] := dlist.empty\n | (x :: xs) := x ++ dlist.join xs\n\nmeta def expanded_field_list' : name \u2192 tactic (dlist $ name \u00d7 name) | struct_n :=\ndo (so,fs) \u2190 subobject_names struct_n,\n   ts \u2190 so.mmap (\u03bb n, do\n     e \u2190 mk_const (n.update_prefix struct_n) >>= infer_type >>= drop_binders,\n     expanded_field_list' $ e.get_app_fn.const_name),\n   -- trace so, trace fs,\n   return $ dlist.join ts ++ dlist.of_list (fs.map $ prod.mk struct_n)\n\nmeta def expanded_field_list (struct_n : name) : tactic (list $ name \u00d7 name) :=\ndlist.to_list <$> expanded_field_list' struct_n\n\ndef mmap\u2082 {\u03b1 \u03b2 \u03b3} {m : Type u \u2192 Type v} [applicative m] (f : \u03b1 \u2192 \u03b2 \u2192 m \u03b3) : list \u03b1 \u2192 list \u03b2 \u2192 m (list \u03b3)\n | [] _ := pure []\n | _ [] := pure []\n | (x :: xs) (y :: ys) := (::) <$> f x y <*> mmap\u2082 xs ys\n\ndef mmap\u2082' {\u03b1 \u03b2 \u03b3} {m : Type u \u2192 Type v} [applicative m] (f : \u03b1 \u2192 \u03b2 \u2192 m \u03b3) : list \u03b1 \u2192 list \u03b2 \u2192 m punit\n | [] _ := pure punit.star\n | _ [] := pure punit.star\n | (x :: xs) (y :: ys) := f x y *> mmap\u2082' xs ys\n\ndef sum.map_right {\u03b1 \u03b2 \u03b2'} (f : \u03b2 \u2192 \u03b2') : \u03b1 \u2295 \u03b2 \u2192 \u03b1 \u2295 \u03b2'\n | (sum.inr x) := sum.inr $ f x\n | (sum.inl x) := sum.inl x\n\nmeta def tactic.interactive.refineS (e : parse texpr) (ph : parse $ optional $ tk \"with\" *> ident) : tactic unit :=\ndo    str \u2190 e.get_structure_instance_info,\n      tgt \u2190 target,\n      let struct_n : name := tgt.get_app_fn.const_name,\n      exp_fields \u2190 expanded_field_list struct_n,\n      let exp_fields' := exp_fields.map prod.snd,\n      let missing_f := exp_fields.filter (\u03bb f, (f.2 : name) \u2209 str.field_names),\n      let provided := exp_fields.filter (\u03bb f, (f.2 : name) \u2208 str.field_names),\n      vs \u2190 mk_mvar_list missing_f.length,\n      let e' : pexpr := pexpr.mk_structure_instance\n          { struct := some struct_n\n          , field_names := str.field_names ++ missing_f.map prod.snd\n          , field_values := str.field_values ++ vs.map to_pexpr },\n      refine e',\n      gs \u2190 with_enable_tags (\n        mmap\u2082 (\u03bb (n : name \u00d7 name) v, do\n           set_goals [v],\n           try (interactive.unfold (provided.map $ \u03bb \u27e8s,f\u27e9, f.update_prefix s) (loc.ns [none])),\n           apply_auto_param\n             <|> apply_opt_param\n             <|> (do match ph with\n                       | (some ph) := () <$ (mk_const (n.2.update_prefix n.1) >>= pose ph none)\n                       | none := return ()\n                     end,\n                     set_main_tag [`_field,n.2]),\n           get_goals)\n        missing_f vs),\n      set_goals gs.join\n\nmeta def collect_tagged_goals (pre : name) : tactic (list expr) :=\ndo gs \u2190 get_goals,\n   gs.mfoldr (\u03bb g r, do\n      pre' :: t \u2190 get_tag g,\n      if t = [pre] \u2227 pre' = pre'.get_prefix <.> \"_field\"\n         then return (g::r)\n         else return r)\n      []\n\n-- meta def match_field_tag\n\nmeta def tactic.interactive.field (tag : parse ident) (tac : itactic) : tactic unit :=\ndo ts \u2190 collect_tagged_goals tag,\n   match ts with\n    | [] := fail format!\"no field goal with tag {tag}\"\n    | [g] := do\n      gs \u2190 get_goals,\n      set_goals $ g :: gs.filter (\u2260 g),\n      solve1 tac\n    | _ := fail format!\"multiple goals have tag {tag}\"\n   end\nuniverse w\n\ninstance {\u03b1 : Type*} : functor (sum \u03b1) :=\n  -- (we use `.{v}` above so that Lean won't have to guess the universes which would\n  -- create visual noise\nbegin\n  refineS { map := @sum.map_right _, .. } with law,\n  all_goals { intros, casesm _ \u2295 _ ; refl },\nend\n\nstructure except_t (f : Type u \u2192 Type v) (\u03b1 : Type u) :=\n (run : f (string \u2295 \u03b1))\n\nopen has_map functor\ndef except_t.map {F} [functor F] {\u03b1 \u03b2} (f : \u03b1 \u2192 \u03b2) : except_t F \u03b1 \u2192 except_t F \u03b2\n | \u27e8 x \u27e9 := \u27e8 @has_map.map _ sum.functor.to_has_map _ _ f <$> x \u27e9\n\nlemma map_comp' {f : Type u \u2192 Type v} [functor f] {\u03b1 \u03b2 \u03b3 : Type u} (g : \u03b2 \u2192 \u03b3) (h : \u03b1 \u2192 \u03b2) :\n  map g \u2218 map h = (map (g \u2218 h) : f \u03b1 \u2192 f \u03b3) :=\nby funext x ; rw [map_comp]\n\nlemma map_id' {f : Type u \u2192 Type v} [functor f] {\u03b1} :\n  (map id : f \u03b1 \u2192 f \u03b1) = id :=\nby funext x ; rw [id_map,id]\n\nexample {\u03b1 : Type u} {f : Type u \u2192 Type w} [functor f] : functor (except_t f) :=\nbegin\n  refineS { map := @except_t.map _ _ } with law\n  ; intros ; casesm except_t f _ ; simp [except_t.map,map_id'],\n  rw [\u2190 law,map_comp'],\nend\n\nexample {\u03b1 : Type u} : functor.{v} (sum \u03b1) :=\n  -- (we use `.{v}` above so that Lean won't have to guess the universes which would\n  -- create visual noise\nbegin\n  (do let e : pexpr := ``({ map := sum.map_right }),\n      tgt \u2190 target,\n      let struct_n : name := tgt.get_app_fn.const_name,\n      trace struct_n,\n        -- what are the fields of `functor`?\n      env \u2190 get_env,\n      trace $ env.structure_fields struct_n),\n    -- prints:\n    -- functor\n    -- (some [to_has_map, map_const_eq, id_map, map_comp])\n  -- refine { map := sorry, .. },\n  -- refineS { map := sorry },\n  -- (do fs \u2190 expanded_field_list `applicative,\n  --     trace fs ),\n  refineS { map := @sum.map_right _, .. } with field,\n  -- field map_const_eq { dunfold has_map.map, tactic.apply_auto_param },\n  field map_comp { intros, casesm _ \u2295 _ ; refl },\n  field id_map { intros, casesm _ \u2295 _ ; refl },\n  -- trace_result\n  -- (do let e : pexpr := ``({ map := @sum.map_right _ }),\n  --     str \u2190 e.get_structure_instance_info,\n  --     tgt \u2190 target,\n  --     let struct_n : name := tgt.get_app_fn.const_name,\n  --     exp_fields \u2190 expanded_field_list struct_n,\n  --     let missing_f := exp_fields.diff str.field_names,\n  --     vs \u2190 mk_mvar_list missing_f.length,\n  --     trace str.field_values,\n  --     trace str.field_names,\n  --     trace missing_f,\n  --     let e' : pexpr := pexpr.mk_structure_instance\n  --         { struct := some struct_n\n  --         , field_names := str.field_names ++ missing_f\n  --         , field_values := str.field_values ++ vs.map to_pexpr },\n  --     trace e',\n  --     refine e'),\n  -- admit,\nend\n", "meta": {"author": "unitb", "repo": "lean-tutorials", "sha": "95ea03ebbd7b7162285e6971ef9728377afdca9c", "save_path": "github-repos/lean/unitb-lean-tutorials", "path": "github-repos/lean/unitb-lean-tutorials/lean-tutorials-95ea03ebbd7b7162285e6971ef9728377afdca9c/writing a tactic/refine.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4378234991142019, "lm_q2_score": 0.17553806931030444, "lm_q1q2_score": 0.07685469173318878}}
{"text": "/-\nCopyright (c) 2016 Jeremy Avigad. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jeremy Avigad, Leonardo de Moura\n-/\nimport tactic.doc_commands\nimport tactic.reserved_notation\n\n/-!\n# Basic logic properties\n\nThis file is one of the earliest imports in mathlib.\n\n## Implementation notes\n\nTheorems that require decidability hypotheses are in the namespace \"decidable\".\nClassical versions are in the namespace \"classical\".\n\nIn the presence of automation, this whole file may be unnecessary. On the other hand,\nmaybe it is useful for writing automation.\n-/\n\nlocal attribute [instance, priority 10] classical.prop_decidable\n\nsection miscellany\n\n/- We add the `inline` attribute to optimize VM computation using these declarations. For example,\n  `if p \u2227 q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/\nattribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable\n  decidable.true implies.decidable not.decidable ne.decidable\n  bool.decidable_eq decidable.to_bool\n\nattribute [simp] cast_eq cast_heq\n\nvariables {\u03b1 : Type*} {\u03b2 : Type*}\n\n/-- An identity function with its main argument implicit. This will be printed as `hidden` even\nif it is applied to a large term, so it can be used for elision,\nas done in the `elide` and `unelide` tactics. -/\n@[reducible] def hidden {\u03b1 : Sort*} {a : \u03b1} := a\n\n/-- Ex falso, the nondependent eliminator for the `empty` type. -/\ndef empty.elim {C : Sort*} : empty \u2192 C.\n\ninstance : subsingleton empty := \u27e8\u03bba, a.elim\u27e9\n\ninstance subsingleton.prod {\u03b1 \u03b2 : Type*} [subsingleton \u03b1] [subsingleton \u03b2] : subsingleton (\u03b1 \u00d7 \u03b2) :=\n\u27e8by { intros a b, cases a, cases b, congr, }\u27e9\n\ninstance : decidable_eq empty := \u03bba, a.elim\n\ninstance sort.inhabited : inhabited (Sort*) := \u27e8punit\u27e9\ninstance sort.inhabited' : inhabited (default (Sort*)) := \u27e8punit.star\u27e9\n\ninstance psum.inhabited_left {\u03b1 \u03b2} [inhabited \u03b1] : inhabited (psum \u03b1 \u03b2) := \u27e8psum.inl (default _)\u27e9\ninstance psum.inhabited_right {\u03b1 \u03b2} [inhabited \u03b2] : inhabited (psum \u03b1 \u03b2) := \u27e8psum.inr (default _)\u27e9\n\n@[priority 10] instance decidable_eq_of_subsingleton\n  {\u03b1} [subsingleton \u03b1] : decidable_eq \u03b1\n| a b := is_true (subsingleton.elim a b)\n\n@[simp] lemma eq_iff_true_of_subsingleton {\u03b1 : Sort*} [subsingleton \u03b1] (x y : \u03b1) :\n  x = y \u2194 true :=\nby cc\n\n/-- If all points are equal to a given point `x`, then `\u03b1` is a subsingleton. -/\nlemma subsingleton_of_forall_eq {\u03b1 : Sort*} (x : \u03b1) (h : \u2200 y, y = x) : subsingleton \u03b1 :=\n\u27e8\u03bb a b, (h a).symm \u25b8 (h b).symm \u25b8 rfl\u27e9\n\nlemma subsingleton_iff_forall_eq {\u03b1 : Sort*} (x : \u03b1) : subsingleton \u03b1 \u2194 \u2200 y, y = x :=\n\u27e8\u03bb h y, @subsingleton.elim _ h y x, subsingleton_of_forall_eq x\u27e9\n\n/-- Add an instance to \"undo\" coercion transitivity into a chain of coercions, because\n   most simp lemmas are stated with respect to simple coercions and will not match when\n   part of a chain. -/\n@[simp] theorem coe_coe {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_t \u03b2 \u03b3]\n  (a : \u03b1) : (a : \u03b3) = (a : \u03b2) := rfl\n\ntheorem coe_fn_coe_trans\n  {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_fun \u03b3]\n  (x : \u03b1) : @coe_fn \u03b1 _ x = @coe_fn \u03b2 _ x := rfl\n\n@[simp] theorem coe_fn_coe_base\n  {\u03b1 \u03b2} [has_coe \u03b1 \u03b2] [has_coe_to_fun \u03b2]\n  (x : \u03b1) : @coe_fn \u03b1 _ x = @coe_fn \u03b2 _ x := rfl\n\ntheorem coe_sort_coe_trans\n  {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_sort \u03b3]\n  (x : \u03b1) : @coe_sort \u03b1 _ x = @coe_sort \u03b2 _ x := rfl\n\n/--\nMany structures such as bundled morphisms coerce to functions so that you can\ntransparently apply them to arguments. For example, if `e : \u03b1 \u2243 \u03b2` and `a : \u03b1`\nthen you can write `e a` and this is elaborated as `\u21d1e a`. This type of\ncoercion is implemented using the `has_coe_to_fun` type class. There is one\nimportant consideration:\n\nIf a type coerces to another type which in turn coerces to a function,\nthen it **must** implement `has_coe_to_fun` directly:\n```lean\nstructure sparkling_equiv (\u03b1 \u03b2) extends \u03b1 \u2243 \u03b2\n\n-- if we add a `has_coe` instance,\ninstance {\u03b1 \u03b2} : has_coe (sparkling_equiv \u03b1 \u03b2) (\u03b1 \u2243 \u03b2) :=\n\u27e8sparkling_equiv.to_equiv\u27e9\n\n-- then a `has_coe_to_fun` instance **must** be added as well:\ninstance {\u03b1 \u03b2} : has_coe_to_fun (sparkling_equiv \u03b1 \u03b2) :=\n\u27e8\u03bb _, \u03b1 \u2192 \u03b2, \u03bb f, f.to_equiv.to_fun\u27e9\n```\n\n(Rationale: if we do not declare the direct coercion, then `\u21d1e a` is not in\nsimp-normal form. The lemma `coe_fn_coe_base` will unfold it to `\u21d1\u2191e a`. This\noften causes loops in the simplifier.)\n-/\nlibrary_note \"function coercion\"\n\n@[simp] theorem coe_sort_coe_base\n  {\u03b1 \u03b2} [has_coe \u03b1 \u03b2] [has_coe_to_sort \u03b2]\n  (x : \u03b1) : @coe_sort \u03b1 _ x = @coe_sort \u03b2 _ x := rfl\n\n/-- `pempty` is the universe-polymorphic analogue of `empty`. -/\n@[derive decidable_eq]\ninductive {u} pempty : Sort u\n\n/-- Ex falso, the nondependent eliminator for the `pempty` type. -/\ndef pempty.elim {C : Sort*} : pempty \u2192 C.\n\ninstance subsingleton_pempty : subsingleton pempty := \u27e8\u03bba, a.elim\u27e9\n\n@[simp] lemma not_nonempty_pempty : \u00ac nonempty pempty :=\nassume \u27e8h\u27e9, h.elim\n\n@[simp] theorem forall_pempty {P : pempty \u2192 Prop} : (\u2200 x : pempty, P x) \u2194 true :=\n\u27e8\u03bb h, trivial, \u03bb h x, by cases x\u27e9\n\n@[simp] theorem exists_pempty {P : pempty \u2192 Prop} : (\u2203 x : pempty, P x) \u2194 false :=\n\u27e8\u03bb h, by { cases h with w, cases w }, false.elim\u27e9\n\nlemma congr_arg_heq {\u03b1} {\u03b2 : \u03b1 \u2192 Sort*} (f : \u2200 a, \u03b2 a) : \u2200 {a\u2081 a\u2082 : \u03b1}, a\u2081 = a\u2082 \u2192 f a\u2081 == f a\u2082\n| a _ rfl := heq.rfl\n\nlemma plift.down_inj {\u03b1 : Sort*} : \u2200 (a b : plift \u03b1), a.down = b.down \u2192 a = b\n| \u27e8a\u27e9 \u27e8b\u27e9 rfl := rfl\n\n-- missing [symm] attribute for ne in core.\nattribute [symm] ne.symm\n\nlemma ne_comm {\u03b1} {a b : \u03b1} : a \u2260 b \u2194 b \u2260 a := \u27e8ne.symm, ne.symm\u27e9\n\n@[simp] lemma eq_iff_eq_cancel_left {b c : \u03b1} :\n  (\u2200 {a}, a = b \u2194 a = c) \u2194 (b = c) :=\n\u27e8\u03bb h, by rw [\u2190 h], \u03bb h a, by rw h\u27e9\n\n@[simp] lemma eq_iff_eq_cancel_right {a b : \u03b1} :\n  (\u2200 {c}, a = c \u2194 b = c) \u2194 (a = b) :=\n\u27e8\u03bb h, by rw h, \u03bb h a, by rw h\u27e9\n\n/-- Wrapper for adding elementary propositions to the type class systems.\nWarning: this can easily be abused. See the rest of this docstring for details.\n\nCertain propositions should not be treated as a class globally,\nbut sometimes it is very convenient to be able to use the type class system\nin specific circumstances.\n\nFor example, `zmod p` is a field if and only if `p` is a prime number.\nIn order to be able to find this field instance automatically by type class search,\nwe have to turn `p.prime` into an instance implicit assumption.\n\nOn the other hand, making `nat.prime` a class would require a major refactoring of the library,\nand it is questionable whether making `nat.prime` a class is desirable at all.\nThe compromise is to add the assumption `[fact p.prime]` to `zmod.field`.\n\nIn particular, this class is not intended for turning the type class system\ninto an automated theorem prover for first order logic. -/\nclass fact (p : Prop) : Prop := (out [] : p)\n\nlemma fact.elim {p : Prop} (h : fact p) : p := h.1\nlemma fact_iff {p : Prop} : fact p \u2194 p := \u27e8\u03bb h, h.1, \u03bb h, \u27e8h\u27e9\u27e9\n\nend miscellany\n\n/-!\n### Declarations about propositional connectives\n-/\n\ntheorem false_ne_true : false \u2260 true\n| h := h.symm \u25b8 trivial\n\nsection propositional\nvariables {a b c d : Prop}\n\n/-! ### Declarations about `implies` -/\n\ninstance : is_refl Prop iff := \u27e8iff.refl\u27e9\ninstance : is_trans Prop iff := \u27e8\u03bb _ _ _, iff.trans\u27e9\n\ntheorem iff_of_eq (e : a = b) : a \u2194 b := e \u25b8 iff.rfl\n\ntheorem iff_iff_eq : (a \u2194 b) \u2194 a = b := \u27e8propext, iff_of_eq\u27e9\n\n@[simp] lemma eq_iff_iff {p q : Prop} : (p = q) \u2194 (p \u2194 q) := iff_iff_eq.symm\n\n@[simp] theorem imp_self : (a \u2192 a) \u2194 true := iff_true_intro id\n\ntheorem imp_intro {\u03b1 \u03b2 : Prop} (h : \u03b1) : \u03b2 \u2192 \u03b1 := \u03bb _, h\n\ntheorem imp_false : (a \u2192 false) \u2194 \u00ac a := iff.rfl\n\ntheorem imp_and_distrib {\u03b1} : (\u03b1 \u2192 b \u2227 c) \u2194 (\u03b1 \u2192 b) \u2227 (\u03b1 \u2192 c) :=\n\u27e8\u03bb h, \u27e8\u03bb ha, (h ha).left, \u03bb ha, (h ha).right\u27e9,\n \u03bb h ha, \u27e8h.left ha, h.right ha\u27e9\u27e9\n\n@[simp] theorem and_imp : (a \u2227 b \u2192 c) \u2194 (a \u2192 b \u2192 c) :=\niff.intro (\u03bb h ha hb, h \u27e8ha, hb\u27e9) (\u03bb h \u27e8ha, hb\u27e9, h ha hb)\n\ntheorem iff_def : (a \u2194 b) \u2194 (a \u2192 b) \u2227 (b \u2192 a) :=\niff_iff_implies_and_implies _ _\n\ntheorem iff_def' : (a \u2194 b) \u2194 (b \u2192 a) \u2227 (a \u2192 b) :=\niff_def.trans and.comm\n\ntheorem imp_true_iff {\u03b1 : Sort*} : (\u03b1 \u2192 true) \u2194 true :=\niff_true_intro $ \u03bb_, trivial\n\ntheorem imp_iff_right (ha : a) : (a \u2192 b) \u2194 b :=\n\u27e8\u03bbf, f ha, imp_intro\u27e9\n\n/-! ### Declarations about `not` -/\n\n/-- Ex falso for negation. From `\u00ac a` and `a` anything follows. This is the same as `absurd` with\nthe arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/\ndef not.elim {\u03b1 : Sort*} (H1 : \u00aca) (H2 : a) : \u03b1 := absurd H2 H1\n\n@[reducible] theorem not.imp {a b : Prop} (H2 : \u00acb) (H1 : a \u2192 b) : \u00aca := mt H1 H2\n\ntheorem not_not_of_not_imp : \u00ac(a \u2192 b) \u2192 \u00ac\u00aca :=\nmt not.elim\n\ntheorem not_of_not_imp {a : Prop} : \u00ac(a \u2192 b) \u2192 \u00acb :=\nmt imp_intro\n\ntheorem dec_em (p : Prop) [decidable p] : p \u2228 \u00acp := decidable.em p\n\ntheorem em (p : Prop) : p \u2228 \u00ac p := classical.em _\n\ntheorem or_not {p : Prop} : p \u2228 \u00ac p := em _\n\ntheorem by_contradiction {p} : (\u00acp \u2192 false) \u2192 p := decidable.by_contradiction\n\n-- alias by_contradiction \u2190 by_contra\ntheorem by_contra {p} : (\u00acp \u2192 false) \u2192 p := decidable.by_contradiction\n\n/--\nIn most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely.\nThe `decidable` namespace contains versions of lemmas from the root namespace that explicitly\nattempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs.\n\nYou can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if\n`classical.choice` appears in the list.\n-/\nlibrary_note \"decidable namespace\"\n\n/--\nAs mathlib is primarily classical,\nif the type signature of a `def` or `lemma` does not require any `decidable` instances to state,\nit is preferable not to introduce any `decidable` instances that are needed in the proof\nas arguments, but rather to use the `classical` tactic as needed.\n\nIn the other direction, when `decidable` instances do appear in the type signature,\nit is better to use explicitly introduced ones rather than allowing Lean to automatically infer\nclassical ones, as these may cause instance mismatch errors later.\n-/\nlibrary_note \"decidable arguments\"\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_not [decidable a] : \u00ac\u00aca \u2194 a :=\niff.intro decidable.by_contradiction not_not_intro\n\n/-- The Double Negation Theorem: `\u00ac \u00ac P` is equivalent to `P`.\nThe left-to-right direction, double negation elimination (DNE),\nis classically true but not constructively. -/\n@[simp] theorem not_not : \u00ac\u00aca \u2194 a := decidable.not_not\n\ntheorem of_not_not : \u00ac\u00aca \u2192 a := by_contra\n\n-- See Note [decidable namespace]\nprotected theorem decidable.of_not_imp [decidable a] (h : \u00ac (a \u2192 b)) : a :=\ndecidable.by_contradiction (not_not_of_not_imp h)\n\ntheorem of_not_imp : \u00ac (a \u2192 b) \u2192 a := decidable.of_not_imp\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_symm [decidable a] (h : \u00aca \u2192 b) (hb : \u00acb) : a :=\ndecidable.by_contradiction $ hb \u2218 h\n\ntheorem not.decidable_imp_symm [decidable a] : (\u00aca \u2192 b) \u2192 \u00acb \u2192 a := decidable.not_imp_symm\n\ntheorem not.imp_symm : (\u00aca \u2192 b) \u2192 \u00acb \u2192 a := not.decidable_imp_symm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_comm [decidable a] [decidable b] : (\u00aca \u2192 b) \u2194 (\u00acb \u2192 a) :=\n\u27e8not.decidable_imp_symm, not.decidable_imp_symm\u27e9\n\ntheorem not_imp_comm : (\u00aca \u2192 b) \u2194 (\u00acb \u2192 a) := decidable.not_imp_comm\n\n@[simp] theorem imp_not_self : (a \u2192 \u00aca) \u2194 \u00aca := \u27e8\u03bb h ha, h ha ha, \u03bb h _, h\u27e9\n\ntheorem decidable.not_imp_self [decidable a] : (\u00aca \u2192 a) \u2194 a :=\nby { have := @imp_not_self (\u00aca), rwa decidable.not_not at this }\n\n@[simp] theorem not_imp_self : (\u00aca \u2192 a) \u2194 a := decidable.not_imp_self\n\ntheorem imp.swap : (a \u2192 b \u2192 c) \u2194 (b \u2192 a \u2192 c) :=\n\u27e8function.swap, function.swap\u27e9\n\ntheorem imp_not_comm : (a \u2192 \u00acb) \u2194 (b \u2192 \u00aca) :=\nimp.swap\n\n/-! ### Declarations about `and` -/\n\ntheorem and_congr_left (h : c \u2192 (a \u2194 b)) : a \u2227 c \u2194 b \u2227 c :=\nand.comm.trans $ (and_congr_right h).trans and.comm\n\ntheorem and_congr_left' (h : a \u2194 b) : a \u2227 c \u2194 b \u2227 c := and_congr h iff.rfl\n\ntheorem and_congr_right' (h : b \u2194 c) : a \u2227 b \u2194 a \u2227 c := and_congr iff.rfl h\n\ntheorem not_and_of_not_left (b : Prop) : \u00aca \u2192 \u00ac(a \u2227 b) :=\nmt and.left\n\ntheorem not_and_of_not_right (a : Prop) {b : Prop} : \u00acb \u2192 \u00ac(a \u2227 b) :=\nmt and.right\n\ntheorem and.imp_left (h : a \u2192 b) : a \u2227 c \u2192 b \u2227 c :=\nand.imp h id\n\ntheorem and.imp_right (h : a \u2192 b) : c \u2227 a \u2192 c \u2227 b :=\nand.imp id h\n\nlemma and.right_comm : (a \u2227 b) \u2227 c \u2194 (a \u2227 c) \u2227 b :=\nby simp only [and.left_comm, and.comm]\n\nlemma and.rotate : a \u2227 b \u2227 c \u2194 b \u2227 c \u2227 a :=\nby simp only [and.left_comm, and.comm]\n\ntheorem and_not_self_iff (a : Prop) : a \u2227 \u00ac a \u2194 false :=\niff.intro (assume h, (h.right) (h.left)) (assume h, h.elim)\n\ntheorem not_and_self_iff (a : Prop) : \u00ac a \u2227 a \u2194 false :=\niff.intro (assume \u27e8hna, ha\u27e9, hna ha) false.elim\n\ntheorem and_iff_left_of_imp {a b : Prop} (h : a \u2192 b) : (a \u2227 b) \u2194 a :=\niff.intro and.left (\u03bb ha, \u27e8ha, h ha\u27e9)\n\ntheorem and_iff_right_of_imp {a b : Prop} (h : b \u2192 a) : (a \u2227 b) \u2194 b :=\niff.intro and.right (\u03bb hb, \u27e8h hb, hb\u27e9)\n\n@[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a \u2227 b) \u2194 a) \u2194 (a \u2192 b) :=\n\u27e8\u03bb h ha, (h.2 ha).2, and_iff_left_of_imp\u27e9\n\n@[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a \u2227 b) \u2194 b) \u2194 (b \u2192 a) :=\n\u27e8\u03bb h ha, (h.2 ha).1, and_iff_right_of_imp\u27e9\n\n@[simp] lemma and.congr_right_iff : (a \u2227 b \u2194 a \u2227 c) \u2194 (a \u2192 (b \u2194 c)) :=\n\u27e8\u03bb h ha, by simp [ha] at h; exact h, and_congr_right\u27e9\n\n@[simp] lemma and.congr_left_iff : (a \u2227 c \u2194 b \u2227 c) \u2194 c \u2192 (a \u2194 b) :=\nby simp only [and.comm, \u2190 and.congr_right_iff]\n\n@[simp] lemma and_self_left : a \u2227 a \u2227 b \u2194 a \u2227 b :=\n\u27e8\u03bb h, \u27e8h.1, h.2.2\u27e9, \u03bb h, \u27e8h.1, h.1, h.2\u27e9\u27e9\n\n@[simp] lemma and_self_right : (a \u2227 b) \u2227 b \u2194 a \u2227 b :=\n\u27e8\u03bb h, \u27e8h.1.1, h.2\u27e9, \u03bb h, \u27e8\u27e8h.1, h.2\u27e9, h.2\u27e9\u27e9\n\n/-! ### Declarations about `or` -/\n\ntheorem or_congr_left (h : a \u2194 b) : a \u2228 c \u2194 b \u2228 c := or_congr h iff.rfl\n\ntheorem or_congr_right (h : b \u2194 c) : a \u2228 b \u2194 a \u2228 c := or_congr iff.rfl h\n\ntheorem or.right_comm : (a \u2228 b) \u2228 c \u2194 (a \u2228 c) \u2228 b := by rw [or_assoc, or_assoc, or_comm b]\n\ntheorem or_of_or_of_imp_of_imp (h\u2081 : a \u2228 b) (h\u2082 : a \u2192 c) (h\u2083 : b \u2192 d) : c \u2228 d :=\nor.imp h\u2082 h\u2083 h\u2081\n\ntheorem or_of_or_of_imp_left (h\u2081 : a \u2228 c) (h : a \u2192 b) : b \u2228 c :=\nor.imp_left h h\u2081\n\ntheorem or_of_or_of_imp_right (h\u2081 : c \u2228 a) (h : a \u2192 b) : c \u2228 b :=\nor.imp_right h h\u2081\n\ntheorem or.elim3 (h : a \u2228 b \u2228 c) (ha : a \u2192 d) (hb : b \u2192 d) (hc : c \u2192 d) : d :=\nor.elim h ha (assume h\u2082, or.elim h\u2082 hb hc)\n\ntheorem or_imp_distrib : (a \u2228 b \u2192 c) \u2194 (a \u2192 c) \u2227 (b \u2192 c) :=\n\u27e8assume h, \u27e8assume ha, h (or.inl ha), assume hb, h (or.inr hb)\u27e9,\n  assume \u27e8ha, hb\u27e9, or.rec ha hb\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_imp_left [decidable a] : a \u2228 b \u2194 (\u00ac a \u2192 b) :=\n\u27e8or.resolve_left, \u03bb h, dite _ or.inl (or.inr \u2218 h)\u27e9\n\ntheorem or_iff_not_imp_left : a \u2228 b \u2194 (\u00ac a \u2192 b) := decidable.or_iff_not_imp_left\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_imp_right [decidable b] : a \u2228 b \u2194 (\u00ac b \u2192 a) :=\nor.comm.trans decidable.or_iff_not_imp_left\n\ntheorem or_iff_not_imp_right : a \u2228 b \u2194 (\u00ac b \u2192 a) := decidable.or_iff_not_imp_right\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_not [decidable a] : (\u00ac a \u2192 \u00ac b) \u2194 (b \u2192 a) :=\n\u27e8assume h hb, decidable.by_contradiction $ assume na, h na hb, mt\u27e9\n\ntheorem not_imp_not : (\u00ac a \u2192 \u00ac b) \u2194 (b \u2192 a) := decidable.not_imp_not\n\n@[simp] theorem or_iff_left_iff_imp : (a \u2228 b \u2194 a) \u2194 (b \u2192 a) :=\n\u27e8\u03bb h hb, h.1 (or.inr hb), or_iff_left_of_imp\u27e9\n\n@[simp] theorem or_iff_right_iff_imp : (a \u2228 b \u2194 b) \u2194 (a \u2192 b) :=\nby rw [or_comm, or_iff_left_iff_imp]\n\n/-! ### Declarations about distributivity -/\n\n/-- `\u2227` distributes over `\u2228` (on the left). -/\ntheorem and_or_distrib_left : a \u2227 (b \u2228 c) \u2194 (a \u2227 b) \u2228 (a \u2227 c) :=\n\u27e8\u03bb \u27e8ha, hbc\u27e9, hbc.imp (and.intro ha) (and.intro ha),\n or.rec (and.imp_right or.inl) (and.imp_right or.inr)\u27e9\n\n/-- `\u2227` distributes over `\u2228` (on the right). -/\ntheorem or_and_distrib_right : (a \u2228 b) \u2227 c \u2194 (a \u2227 c) \u2228 (b \u2227 c) :=\n(and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm)\n\n/-- `\u2228` distributes over `\u2227` (on the left). -/\ntheorem or_and_distrib_left : a \u2228 (b \u2227 c) \u2194 (a \u2228 b) \u2227 (a \u2228 c) :=\n\u27e8or.rec (\u03bbha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr),\n and.rec $ or.rec (imp_intro \u2218 or.inl) (or.imp_right \u2218 and.intro)\u27e9\n\n/-- `\u2228` distributes over `\u2227` (on the right). -/\ntheorem and_or_distrib_right : (a \u2227 b) \u2228 c \u2194 (a \u2228 c) \u2227 (b \u2228 c) :=\n(or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm)\n\n@[simp] lemma or_self_left : a \u2228 a \u2228 b \u2194 a \u2228 b :=\n\u27e8\u03bb h, h.elim or.inl id, \u03bb h, h.elim or.inl (or.inr \u2218 or.inr)\u27e9\n\n@[simp] lemma or_self_right : (a \u2228 b) \u2228 b \u2194 a \u2228 b :=\n\u27e8\u03bb h, h.elim id or.inr, \u03bb h, h.elim (or.inl \u2218 or.inl) or.inr\u27e9\n\n/-! Declarations about `iff` -/\n\ntheorem iff_of_true (ha : a) (hb : b) : a \u2194 b :=\n\u27e8\u03bb_, hb, \u03bb _, ha\u27e9\n\ntheorem iff_of_false (ha : \u00aca) (hb : \u00acb) : a \u2194 b :=\n\u27e8ha.elim, hb.elim\u27e9\n\ntheorem iff_true_left (ha : a) : (a \u2194 b) \u2194 b :=\n\u27e8\u03bb h, h.1 ha, iff_of_true ha\u27e9\n\ntheorem iff_true_right (ha : a) : (b \u2194 a) \u2194 b :=\niff.comm.trans (iff_true_left ha)\n\ntheorem iff_false_left (ha : \u00aca) : (a \u2194 b) \u2194 \u00acb :=\n\u27e8\u03bb h, mt h.2 ha, iff_of_false ha\u27e9\n\ntheorem iff_false_right (ha : \u00aca) : (b \u2194 a) \u2194 \u00acb :=\niff.comm.trans (iff_false_left ha)\n\n@[simp]\nlemma iff_mpr_iff_true_intro {P : Prop} (h : P) : iff.mpr (iff_true_intro h) true.intro = h := rfl\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_or_of_imp [decidable a] (h : a \u2192 b) : \u00ac a \u2228 b :=\nif ha : a then or.inr (h ha) else or.inl ha\n\ntheorem not_or_of_imp : (a \u2192 b) \u2192 \u00ac a \u2228 b := decidable.not_or_of_imp\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_iff_not_or [decidable a] : (a \u2192 b) \u2194 (\u00ac a \u2228 b) :=\n\u27e8decidable.not_or_of_imp, or.neg_resolve_left\u27e9\n\ntheorem imp_iff_not_or : (a \u2192 b) \u2194 (\u00ac a \u2228 b) := decidable.imp_iff_not_or\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_or_distrib [decidable a] : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) :=\nby simp [decidable.imp_iff_not_or, or.comm, or.left_comm]\n\ntheorem imp_or_distrib : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) := decidable.imp_or_distrib\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_or_distrib' [decidable b] : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) :=\nby by_cases b; simp [h, or_iff_right_of_imp ((\u2218) false.elim)]\n\ntheorem imp_or_distrib' : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) := decidable.imp_or_distrib'\n\ntheorem not_imp_of_and_not : a \u2227 \u00ac b \u2192 \u00ac (a \u2192 b)\n| \u27e8ha, hb\u27e9 h := hb $ h ha\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp [decidable a] : \u00ac(a \u2192 b) \u2194 a \u2227 \u00acb :=\n\u27e8\u03bb h, \u27e8decidable.of_not_imp h, not_of_not_imp h\u27e9, not_imp_of_and_not\u27e9\n\ntheorem not_imp : \u00ac(a \u2192 b) \u2194 a \u2227 \u00acb := decidable.not_imp\n\n-- for monotonicity\nlemma imp_imp_imp (h\u2080 : c \u2192 a) (h\u2081 : b \u2192 d) : (a \u2192 b) \u2192 (c \u2192 d) :=\nassume (h\u2082 : a \u2192 b), h\u2081 \u2218 h\u2082 \u2218 h\u2080\n\n-- See Note [decidable namespace]\nprotected theorem decidable.peirce (a b : Prop) [decidable a] : ((a \u2192 b) \u2192 a) \u2192 a :=\nif ha : a then \u03bb h, ha else \u03bb h, h ha.elim\n\ntheorem peirce (a b : Prop) : ((a \u2192 b) \u2192 a) \u2192 a := decidable.peirce _ _\n\ntheorem peirce' {a : Prop} (H : \u2200 b : Prop, (a \u2192 b) \u2192 a) : a := H _ id\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff_not [decidable a] [decidable b] : (\u00ac a \u2194 \u00ac b) \u2194 (a \u2194 b) :=\nby rw [@iff_def (\u00ac a), @iff_def' a]; exact and_congr decidable.not_imp_not decidable.not_imp_not\n\ntheorem not_iff_not : (\u00ac a \u2194 \u00ac b) \u2194 (a \u2194 b) := decidable.not_iff_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff_comm [decidable a] [decidable b] : (\u00ac a \u2194 b) \u2194 (\u00ac b \u2194 a) :=\nby rw [@iff_def (\u00ac a), @iff_def (\u00ac b)]; exact and_congr decidable.not_imp_comm imp_not_comm\n\ntheorem not_iff_comm : (\u00ac a \u2194 b) \u2194 (\u00ac b \u2194 a) := decidable.not_iff_comm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff : \u2200 [decidable b], \u00ac (a \u2194 b) \u2194 (\u00ac a \u2194 b) :=\nby intro h; cases h; simp only [h, iff_true, iff_false]\n\ntheorem not_iff : \u00ac (a \u2194 b) \u2194 (\u00ac a \u2194 b) := decidable.not_iff\n\n-- See Note [decidable namespace]\nprotected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a \u2194 \u00ac b) \u2194 (b \u2194 \u00ac a) :=\nby rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm decidable.not_imp_comm\n\ntheorem iff_not_comm : (a \u2194 \u00ac b) \u2194 (b \u2194 \u00ac a) := decidable.iff_not_comm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.iff_iff_and_or_not_and_not [decidable b] :\n  (a \u2194 b) \u2194 (a \u2227 b) \u2228 (\u00ac a \u2227 \u00ac b) :=\nby { split; intro h,\n     { rw h; by_cases b; [left,right]; split; assumption },\n     { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } }\n\ntheorem iff_iff_and_or_not_and_not : (a \u2194 b) \u2194 (a \u2227 b) \u2228 (\u00ac a \u2227 \u00ac b) :=\ndecidable.iff_iff_and_or_not_and_not\n\nlemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] :\n  (a \u2194 b) \u2194 ((\u00aca \u2228 b) \u2227 (a \u2228 \u00acb)) :=\nbegin\n  rw [iff_iff_implies_and_implies a b],\n  simp only [decidable.imp_iff_not_or, or.comm]\nend\n\nlemma iff_iff_not_or_and_or_not : (a \u2194 b) \u2194 ((\u00aca \u2228 b) \u2227 (a \u2228 \u00acb)) :=\ndecidable.iff_iff_not_or_and_or_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_not_right [decidable b] : \u00ac(a \u2227 \u00acb) \u2194 (a \u2192 b) :=\n\u27e8\u03bb h ha, h.decidable_imp_symm $ and.intro ha, \u03bb h \u27e8ha, hb\u27e9, hb $ h ha\u27e9\n\ntheorem not_and_not_right : \u00ac(a \u2227 \u00acb) \u2194 (a \u2192 b) := decidable.not_and_not_right\n\n/-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent.\n**Important**: this function should be used instead of `rw` on `decidable b`, because the\nkernel will get stuck reducing the usage of `propext` otherwise,\nand `dec_trivial` will not work. -/\n@[inline] def decidable_of_iff (a : Prop) (h : a \u2194 b) [D : decidable a] : decidable b :=\ndecidable_of_decidable_of_iff D h\n\n/-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent.\nThis is the same as `decidable_of_iff` but the iff is flipped. -/\n@[inline] def decidable_of_iff' (b : Prop) (h : a \u2194 b) [D : decidable b] : decidable a :=\ndecidable_of_decidable_of_iff D h.symm\n\n/-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b \u2194 a`.\n(This is sometimes taken as an alternate definition of decidability.) -/\ndef decidable_of_bool : \u2200 (b : bool) (h : b \u2194 a), decidable a\n| tt h := is_true (h.1 rfl)\n| ff h := is_false (mt h.2 bool.ff_ne_tt)\n\n/-! ### De Morgan's laws -/\n\ntheorem not_and_of_not_or_not (h : \u00ac a \u2228 \u00ac b) : \u00ac (a \u2227 b)\n| \u27e8ha, hb\u27e9 := or.elim h (absurd ha) (absurd hb)\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_distrib [decidable a] : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb :=\n\u27e8\u03bb h, if ha : a then or.inr (\u03bb hb, h \u27e8ha, hb\u27e9) else or.inl ha, not_and_of_not_or_not\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_distrib' [decidable b] : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb :=\n\u27e8\u03bb h, if hb : b then or.inl (\u03bb ha, h \u27e8ha, hb\u27e9) else or.inr hb, not_and_of_not_or_not\u27e9\n\n/-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the\ndisjunction of the negations. -/\ntheorem not_and_distrib : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb := decidable.not_and_distrib\n\n@[simp] theorem not_and : \u00ac (a \u2227 b) \u2194 (a \u2192 \u00ac b) := and_imp\n\ntheorem not_and' : \u00ac (a \u2227 b) \u2194 b \u2192 \u00aca :=\nnot_and.trans imp_not_comm\n\n/-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the\nconjunction of the negations. -/\ntheorem not_or_distrib : \u00ac (a \u2228 b) \u2194 \u00ac a \u2227 \u00ac b :=\n\u27e8\u03bb h, \u27e8\u03bb ha, h (or.inl ha), \u03bb hb, h (or.inr hb)\u27e9,\n \u03bb \u27e8h\u2081, h\u2082\u27e9 h, or.elim h h\u2081 h\u2082\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a \u2228 b \u2194 \u00ac (\u00aca \u2227 \u00acb) :=\nby rw [\u2190 not_or_distrib, decidable.not_not]\n\ntheorem or_iff_not_and_not : a \u2228 b \u2194 \u00ac (\u00aca \u2227 \u00acb) := decidable.or_iff_not_and_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] :\n  a \u2227 b \u2194 \u00ac (\u00ac a \u2228 \u00ac b) :=\nby rw [\u2190 decidable.not_and_distrib, decidable.not_not]\n\ntheorem and_iff_not_or_not : a \u2227 b \u2194 \u00ac (\u00ac a \u2228 \u00ac b) := decidable.and_iff_not_or_not\n\nend propositional\n\n/-! ### Declarations about equality -/\n\nsection equality\nvariables {\u03b1 : Sort*} {a b : \u03b1}\n\n@[simp] theorem heq_iff_eq : a == b \u2194 a = b :=\n\u27e8eq_of_heq, heq_of_eq\u27e9\n\ntheorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq :=\nhave p = q, from propext \u27e8\u03bb _, hq, \u03bb _, hp\u27e9,\nby subst q; refl\n\ntheorem ne_of_mem_of_not_mem {\u03b1 \u03b2} [has_mem \u03b1 \u03b2] {s : \u03b2} {a b : \u03b1}\n  (h : a \u2208 s) : b \u2209 s \u2192 a \u2260 b :=\nmt $ \u03bb e, e \u25b8 h\n\nlemma ne_of_apply_ne {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) {x y : \u03b1} (h : f x \u2260 f y) : x \u2260 y :=\n\u03bb (w : x = y), h (congr_arg f w)\n\ntheorem eq_equivalence : equivalence (@eq \u03b1) :=\n\u27e8eq.refl, @eq.symm _, @eq.trans _\u27e9\n\n/-- Transport through trivial families is the identity. -/\n@[simp]\nlemma eq_rec_constant {\u03b1 : Sort*} {a a' : \u03b1} {\u03b2 : Sort*} (y : \u03b2) (h : a = a') :\n  (@eq.rec \u03b1 a (\u03bb a, \u03b2) y a' h) = y :=\nby { cases h, refl, }\n\n@[simp]\nlemma eq_mp_eq_cast {\u03b1 \u03b2 : Sort*} (h : \u03b1 = \u03b2) : eq.mp h = cast h := rfl\n\n@[simp]\nlemma eq_mpr_eq_cast {\u03b1 \u03b2 : Sort*} (h : \u03b1 = \u03b2) : eq.mpr h = cast h.symm := rfl\n\n@[simp]\nlemma cast_cast : \u2200 {\u03b1 \u03b2 \u03b3 : Sort*} (ha : \u03b1 = \u03b2) (hb : \u03b2 = \u03b3) (a : \u03b1),\n  cast hb (cast ha a) = cast (ha.trans hb) a\n| _ _ _ rfl rfl a := rfl\n\n@[simp] lemma congr_refl_left {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) {a b : \u03b1} (h : a = b) :\n  congr (eq.refl f) h = congr_arg f h :=\nrfl\n\n@[simp] lemma congr_refl_right {\u03b1 \u03b2 : Sort*} {f g : \u03b1 \u2192 \u03b2} (h : f = g) (a : \u03b1) :\n  congr h (eq.refl a) = congr_fun h a :=\nrfl\n\n@[simp] lemma congr_arg_refl {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (a : \u03b1) :\n  congr_arg f (eq.refl a) = eq.refl (f a) :=\nrfl\n\n@[simp] lemma congr_fun_rfl {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (a : \u03b1) :\n  congr_fun (eq.refl f) a = eq.refl (f a) :=\nrfl\n\n@[simp] lemma congr_fun_congr_arg {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) {a a' : \u03b1} (p : a = a') (b : \u03b2) :\n  congr_fun (congr_arg f p) b = congr_arg (\u03bb a, f a b) p :=\nrfl\n\nlemma heq_of_cast_eq :\n  \u2200 {\u03b1 \u03b2 : Sort*} {a : \u03b1} {a' : \u03b2} (e : \u03b1 = \u03b2) (h\u2082 : cast e a = a'), a == a'\n| \u03b1 ._ a a' rfl h := eq.rec_on h (heq.refl _)\n\nlemma cast_eq_iff_heq {\u03b1 \u03b2 : Sort*} {a : \u03b1} {a' : \u03b2} {e : \u03b1 = \u03b2} : cast e a = a' \u2194 a == a' :=\n\u27e8heq_of_cast_eq _, \u03bb h, by cases h; refl\u27e9\n\nlemma rec_heq_of_heq {\u03b2} {C : \u03b1 \u2192 Sort*} {x : C a} {y : \u03b2} (eq : a = b) (h : x == y) :\n  @eq.rec \u03b1 a C x b eq == y :=\nby subst eq; exact h\n\nprotected lemma eq.congr {x\u2081 x\u2082 y\u2081 y\u2082 : \u03b1} (h\u2081 : x\u2081 = y\u2081) (h\u2082 : x\u2082 = y\u2082) :\n  (x\u2081 = x\u2082) \u2194 (y\u2081 = y\u2082) :=\nby { subst h\u2081, subst h\u2082 }\n\nlemma eq.congr_left {x y z : \u03b1} (h : x = y) : x = z \u2194 y = z := by rw [h]\nlemma eq.congr_right {x y z : \u03b1} (h : x = y) : z = x \u2194 z = y := by rw [h]\n\nlemma congr_arg2 {\u03b1 \u03b2 \u03b3 : Type*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) {x x' : \u03b1} {y y' : \u03b2}\n  (hx : x = x') (hy : y = y') : f x y = f x' y' :=\nby { subst hx, subst hy }\n\nend equality\n\n/-! ### Declarations about quantifiers -/\n\nsection quantifiers\nvariables {\u03b1 : Sort*} {\u03b2 : Sort*} {p q : \u03b1 \u2192 Prop} {b : Prop}\n\nlemma forall_imp (h : \u2200 a, p a \u2192 q a) : (\u2200 a, p a) \u2192 \u2200 a, q a :=\n\u03bb h' a, h a (h' a)\n\nlemma forall\u2082_congr {p q : \u03b1 \u2192 \u03b2 \u2192 Prop} (h : \u2200 a b, p a b \u2194 q a b) :\n  (\u2200 a b, p a b) \u2194 (\u2200 a b, q a b) :=\nforall_congr (\u03bb a, forall_congr (h a))\n\nlemma forall\u2083_congr {\u03b3 : Sort*} {p q : \u03b1 \u2192 \u03b2 \u2192 \u03b3 \u2192 Prop}\n  (h : \u2200 a b c, p a b c \u2194 q a b c) :\n  (\u2200 a b c, p a b c) \u2194 (\u2200 a b c, q a b c) :=\nforall_congr (\u03bb a, forall\u2082_congr (h a))\n\nlemma forall\u2084_congr {\u03b3 \u03b4 : Sort*} {p q : \u03b1 \u2192 \u03b2 \u2192 \u03b3 \u2192 \u03b4 \u2192 Prop}\n  (h : \u2200 a b c d, p a b c d \u2194 q a b c d) :\n  (\u2200 a b c d, p a b c d) \u2194 (\u2200 a b c d, q a b c d) :=\nforall_congr (\u03bb a, forall\u2083_congr (h a))\n\nlemma Exists.imp (h : \u2200 a, (p a \u2192 q a)) (p : \u2203 a, p a) : \u2203 a, q a := exists_imp_exists h p\n\nlemma exists_imp_exists' {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} (f : \u03b1 \u2192 \u03b2) (hpq : \u2200 a, p a \u2192 q (f a))\n  (hp : \u2203 a, p a) : \u2203 b, q b :=\nexists.elim hp (\u03bb a hp', \u27e8_, hpq _ hp'\u27e9)\n\nlemma exists\u2082_congr {p q : \u03b1 \u2192 \u03b2 \u2192 Prop} (h : \u2200 a b, p a b \u2194 q a b) :\n  (\u2203 a b, p a b) \u2194 (\u2203 a b, q a b) :=\nexists_congr (\u03bb a, exists_congr (h a))\n\nlemma exists\u2083_congr {\u03b3 : Sort*} {p q : \u03b1 \u2192 \u03b2 \u2192 \u03b3 \u2192 Prop}\n  (h : \u2200 a b c, p a b c \u2194 q a b c) :\n  (\u2203 a b c, p a b c) \u2194 (\u2203 a b c, q a b c) :=\nexists_congr (\u03bb a, exists\u2082_congr (h a))\n\nlemma exists\u2084_congr {\u03b3 \u03b4 : Sort*} {p q : \u03b1 \u2192 \u03b2 \u2192 \u03b3 \u2192 \u03b4 \u2192 Prop}\n  (h : \u2200 a b c d, p a b c d \u2194 q a b c d) :\n  (\u2203 a b c d, p a b c d) \u2194 (\u2203 a b c d, q a b c d) :=\nexists_congr (\u03bb a, exists\u2083_congr (h a))\n\ntheorem forall_swap {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2200 x y, p x y) \u2194 \u2200 y x, p x y :=\n\u27e8function.swap, function.swap\u27e9\n\ntheorem exists_swap {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2203 x y, p x y) \u2194 \u2203 y x, p x y :=\n\u27e8\u03bb \u27e8x, y, h\u27e9, \u27e8y, x, h\u27e9, \u03bb \u27e8y, x, h\u27e9, \u27e8x, y, h\u27e9\u27e9\n\n@[simp] theorem exists_imp_distrib : ((\u2203 x, p x) \u2192 b) \u2194 \u2200 x, p x \u2192 b :=\n\u27e8\u03bb h x hpx, h \u27e8x, hpx\u27e9, \u03bb h \u27e8x, hpx\u27e9, h x hpx\u27e9\n\n/--\nExtract an element from a existential statement, using `classical.some`.\n-/\n-- This enables projection notation.\n@[reducible] noncomputable def Exists.some {p : \u03b1 \u2192 Prop} (P : \u2203 a, p a) : \u03b1 := classical.some P\n\n/--\nShow that an element extracted from `P : \u2203 a, p a` using `P.some` satisfies `p`.\n-/\nlemma Exists.some_spec {p : \u03b1 \u2192 Prop} (P : \u2203 a, p a) : p (P.some) := classical.some_spec P\n\n--theorem forall_not_of_not_exists (h : \u00ac \u2203 x, p x) : \u2200 x, \u00ac p x :=\n--forall_imp_of_exists_imp h\n\ntheorem not_exists_of_forall_not (h : \u2200 x, \u00ac p x) : \u00ac \u2203 x, p x :=\nexists_imp_distrib.2 h\n\n@[simp] theorem not_exists : (\u00ac \u2203 x, p x) \u2194 \u2200 x, \u00ac p x :=\nexists_imp_distrib\n\ntheorem not_forall_of_exists_not : (\u2203 x, \u00ac p x) \u2192 \u00ac \u2200 x, p x\n| \u27e8x, hn\u27e9 h := hn (h x)\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_forall {p : \u03b1 \u2192 Prop}\n  [decidable (\u2203 x, \u00ac p x)] [\u2200 x, decidable (p x)] : (\u00ac \u2200 x, p x) \u2194 \u2203 x, \u00ac p x :=\n\u27e8not.decidable_imp_symm $ \u03bb nx x, nx.decidable_imp_symm $ \u03bb h, \u27e8x, h\u27e9,\n not_forall_of_exists_not\u27e9\n\n@[simp] theorem not_forall {p : \u03b1 \u2192 Prop} : (\u00ac \u2200 x, p x) \u2194 \u2203 x, \u00ac p x := decidable.not_forall\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_forall_not [decidable (\u2203 x, p x)] :\n  (\u00ac \u2200 x, \u00ac p x) \u2194 \u2203 x, p x :=\n(@decidable.not_iff_comm _ _ _ (decidable_of_iff (\u00ac \u2203 x, p x) not_exists)).1 not_exists\n\ntheorem not_forall_not : (\u00ac \u2200 x, \u00ac p x) \u2194 \u2203 x, p x := decidable.not_forall_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_exists_not [\u2200 x, decidable (p x)] : (\u00ac \u2203 x, \u00ac p x) \u2194 \u2200 x, p x :=\nby simp [decidable.not_not]\n\n@[simp] theorem not_exists_not : (\u00ac \u2203 x, \u00ac p x) \u2194 \u2200 x, p x := decidable.not_exists_not\n\n-- TODO: duplicate of a lemma in core\ntheorem forall_true_iff : (\u03b1 \u2192 true) \u2194 true :=\nimplies_true_iff \u03b1\n\n-- Unfortunately this causes simp to loop sometimes, so we\n-- add the 2 and 3 cases as simp lemmas instead\ntheorem forall_true_iff' (h : \u2200 a, p a \u2194 true) : (\u2200 a, p a) \u2194 true :=\niff_true_intro (\u03bb _, of_iff_true (h _))\n\n@[simp] theorem forall_2_true_iff {\u03b2 : \u03b1 \u2192 Sort*} : (\u2200 a, \u03b2 a \u2192 true) \u2194 true :=\nforall_true_iff' $ \u03bb _, forall_true_iff\n\n@[simp] theorem forall_3_true_iff {\u03b2 : \u03b1 \u2192 Sort*} {\u03b3 : \u03a0 a, \u03b2 a \u2192 Sort*} :\n  (\u2200 a (b : \u03b2 a), \u03b3 a b \u2192 true) \u2194 true :=\nforall_true_iff' $ \u03bb _, forall_2_true_iff\n\n@[simp] theorem forall_const (\u03b1 : Sort*) [i : nonempty \u03b1] : (\u03b1 \u2192 b) \u2194 b :=\n\u27e8i.elim, \u03bb hb x, hb\u27e9\n\n@[simp] theorem exists_const (\u03b1 : Sort*) [i : nonempty \u03b1] : (\u2203 x : \u03b1, b) \u2194 b :=\n\u27e8\u03bb \u27e8x, h\u27e9, h, i.elim exists.intro\u27e9\n\ntheorem forall_and_distrib : (\u2200 x, p x \u2227 q x) \u2194 (\u2200 x, p x) \u2227 (\u2200 x, q x) :=\n\u27e8\u03bb h, \u27e8\u03bb x, (h x).left, \u03bb x, (h x).right\u27e9, \u03bb \u27e8h\u2081, h\u2082\u27e9 x, \u27e8h\u2081 x, h\u2082 x\u27e9\u27e9\n\ntheorem exists_or_distrib : (\u2203 x, p x \u2228 q x) \u2194 (\u2203 x, p x) \u2228 (\u2203 x, q x) :=\n\u27e8\u03bb \u27e8x, hpq\u27e9, hpq.elim (\u03bb hpx, or.inl \u27e8x, hpx\u27e9) (\u03bb hqx, or.inr \u27e8x, hqx\u27e9),\n \u03bb hepq, hepq.elim (\u03bb \u27e8x, hpx\u27e9, \u27e8x, or.inl hpx\u27e9) (\u03bb \u27e8x, hqx\u27e9, \u27e8x, or.inr hqx\u27e9)\u27e9\n\n@[simp] theorem exists_and_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2203x, q \u2227 p x) \u2194 q \u2227 (\u2203x, p x) :=\n\u27e8\u03bb \u27e8x, hq, hp\u27e9, \u27e8hq, x, hp\u27e9, \u03bb \u27e8hq, x, hp\u27e9, \u27e8x, hq, hp\u27e9\u27e9\n\n@[simp] theorem exists_and_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2203x, p x \u2227 q) \u2194 (\u2203x, p x) \u2227 q :=\nby simp [and_comm]\n\n@[simp] theorem forall_eq {a' : \u03b1} : (\u2200a, a = a' \u2192 p a) \u2194 p a' :=\n\u27e8\u03bb h, h a' rfl, \u03bb h a e, e.symm \u25b8 h\u27e9\n\n@[simp] theorem forall_eq' {a' : \u03b1} : (\u2200a, a' = a \u2192 p a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\n-- this lemma is needed to simplify the output of `list.mem_cons_iff`\n@[simp] theorem forall_eq_or_imp {a' : \u03b1} : (\u2200 a, a = a' \u2228 q a \u2192 p a) \u2194 p a' \u2227 \u2200 a, q a \u2192 p a :=\nby simp only [or_imp_distrib, forall_and_distrib, forall_eq]\n\n@[simp] theorem exists_eq {a' : \u03b1} : \u2203 a, a = a' := \u27e8_, rfl\u27e9\n\n@[simp] theorem exists_eq' {a' : \u03b1} : \u2203 a, a' = a := \u27e8_, rfl\u27e9\n\n@[simp] theorem exists_eq_left {a' : \u03b1} : (\u2203 a, a = a' \u2227 p a) \u2194 p a' :=\n\u27e8\u03bb \u27e8a, e, h\u27e9, e \u25b8 h, \u03bb h, \u27e8_, rfl, h\u27e9\u27e9\n\n@[simp] theorem exists_eq_right {a' : \u03b1} : (\u2203 a, p a \u2227 a = a') \u2194 p a' :=\n(exists_congr $ by exact \u03bb a, and.comm).trans exists_eq_left\n\n@[simp] theorem exists_eq_right_right {a' : \u03b1} :\n  (\u2203 (a : \u03b1), p a \u2227 b \u2227 a = a') \u2194 p a' \u2227 b :=\n\u27e8\u03bb \u27e8_, hp, hq, rfl\u27e9, \u27e8hp, hq\u27e9, \u03bb \u27e8hp, hq\u27e9, \u27e8a', hp, hq, rfl\u27e9\u27e9\n\n@[simp] theorem exists_eq_right_right' {a' : \u03b1} :\n  (\u2203 (a : \u03b1), p a \u2227 b \u2227 a' = a) \u2194 p a' \u2227 b :=\n\u27e8\u03bb \u27e8_, hp, hq, rfl\u27e9, \u27e8hp, hq\u27e9, \u03bb \u27e8hp, hq\u27e9, \u27e8a', hp, hq, rfl\u27e9\u27e9\n\n@[simp] theorem exists_apply_eq_apply {\u03b1 \u03b2 : Type*} (f : \u03b1 \u2192 \u03b2) (a' : \u03b1) : \u2203 a, f a = f a' :=\n\u27e8a', rfl\u27e9\n\n@[simp] theorem exists_apply_eq_apply' {\u03b1 \u03b2 : Type*} (f : \u03b1 \u2192 \u03b2) (a' : \u03b1) : \u2203 a, f a' = f a :=\n\u27e8a', rfl\u27e9\n\n@[simp] theorem exists_exists_and_eq_and {f : \u03b1 \u2192 \u03b2} {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} :\n  (\u2203 b, (\u2203 a, p a \u2227 f a = b) \u2227 q b) \u2194 \u2203 a, p a \u2227 q (f a) :=\n\u27e8\u03bb \u27e8b, \u27e8a, ha, hab\u27e9, hb\u27e9, \u27e8a, ha, hab.symm \u25b8 hb\u27e9, \u03bb \u27e8a, hp, hq\u27e9, \u27e8f a, \u27e8a, hp, rfl\u27e9, hq\u27e9\u27e9\n\n@[simp] theorem exists_exists_eq_and {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2203 b, (\u2203 a, f a = b) \u2227 p b) \u2194 \u2203 a, p (f a) :=\n\u27e8\u03bb \u27e8b, \u27e8a, ha\u27e9, hb\u27e9, \u27e8a, ha.symm \u25b8 hb\u27e9, \u03bb \u27e8a, ha\u27e9, \u27e8f a, \u27e8a, rfl\u27e9, ha\u27e9\u27e9\n\n@[simp] lemma exists_or_eq_left (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), x = y \u2228 p x :=\n\u27e8y, or.inl rfl\u27e9\n\n@[simp] lemma exists_or_eq_right (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), p x \u2228 x = y :=\n\u27e8y, or.inr rfl\u27e9\n\n@[simp] lemma exists_or_eq_left' (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), y = x \u2228 p x :=\n\u27e8y, or.inl rfl\u27e9\n\n@[simp] lemma exists_or_eq_right' (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), p x \u2228 y = x :=\n\u27e8y, or.inr rfl\u27e9\n\n@[simp] theorem forall_apply_eq_imp_iff {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 a, \u2200 b, f a = b \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\n\u27e8\u03bb h a, h a (f a) rfl, \u03bb h a b hab, hab \u25b8 h a\u27e9\n\n@[simp] theorem forall_apply_eq_imp_iff' {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, f a = b \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby { rw forall_swap, simp }\n\n@[simp] theorem forall_eq_apply_imp_iff {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 a, \u2200 b, b = f a \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby simp [@eq_comm _ _ (f _)]\n\n@[simp] theorem forall_eq_apply_imp_iff' {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, b = f a \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby { rw forall_swap, simp }\n\n@[simp] theorem forall_apply_eq_imp_iff\u2082 {f : \u03b1 \u2192 \u03b2} {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, p a \u2192 f a = b \u2192 q b) \u2194 \u2200 a, p a \u2192 q (f a) :=\n\u27e8\u03bb h a ha, h (f a) a ha rfl, \u03bb h b a ha hb, hb \u25b8 h a ha\u27e9\n\n@[simp] theorem exists_eq_left' {a' : \u03b1} : (\u2203 a, a' = a \u2227 p a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\n@[simp] theorem exists_eq_right' {a' : \u03b1} : (\u2203 a, p a \u2227 a' = a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\ntheorem exists_comm {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2203 a b, p a b) \u2194 \u2203 b a, p a b :=\n\u27e8\u03bb \u27e8a, b, h\u27e9, \u27e8b, a, h\u27e9, \u03bb \u27e8b, a, h\u27e9, \u27e8a, b, h\u27e9\u27e9\n\ntheorem forall_or_of_or_forall (h : b \u2228 \u2200x, p x) (x) : b \u2228 p x :=\nh.imp_right $ \u03bb h\u2082, h\u2082 x\n\n-- See Note [decidable namespace]\nprotected theorem decidable.forall_or_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} [decidable q] :\n  (\u2200x, q \u2228 p x) \u2194 q \u2228 (\u2200x, p x) :=\n\u27e8\u03bb h, if hq : q then or.inl hq else or.inr $ \u03bb x, (h x).resolve_left hq,\n  forall_or_of_or_forall\u27e9\n\ntheorem forall_or_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2200x, q \u2228 p x) \u2194 q \u2228 (\u2200x, p x) := decidable.forall_or_distrib_left\n\n-- See Note [decidable namespace]\nprotected theorem decidable.forall_or_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} [decidable q] :\n  (\u2200x, p x \u2228 q) \u2194 (\u2200x, p x) \u2228 q :=\nby simp [or_comm, decidable.forall_or_distrib_left]\n\ntheorem forall_or_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2200x, p x \u2228 q) \u2194 (\u2200x, p x) \u2228 q := decidable.forall_or_distrib_right\n\n/-- A predicate holds everywhere on the image of a surjective functions iff\n    it holds everywhere. -/\ntheorem forall_iff_forall_surj\n  {\u03b1 \u03b2 : Type*} {f : \u03b1 \u2192 \u03b2} (h : function.surjective f) {P : \u03b2 \u2192 Prop} :\n  (\u2200 a, P (f a)) \u2194 \u2200 b, P b :=\n\u27e8\u03bb ha b, by cases h b with a hab; rw \u2190hab; exact ha a, \u03bb hb a, hb $ f a\u27e9\n\n@[simp] theorem exists_prop {p q : Prop} : (\u2203 h : p, q) \u2194 p \u2227 q :=\n\u27e8\u03bb \u27e8h\u2081, h\u2082\u27e9, \u27e8h\u2081, h\u2082\u27e9, \u03bb \u27e8h\u2081, h\u2082\u27e9, \u27e8h\u2081, h\u2082\u27e9\u27e9\n\n@[simp] theorem exists_false : \u00ac (\u2203a:\u03b1, false) := assume \u27e8a, h\u27e9, h\n\n@[simp] lemma exists_unique_false : \u00ac (\u2203! (a : \u03b1), false) := assume \u27e8a, h, h'\u27e9, h\n\ntheorem Exists.fst {p : b \u2192 Prop} : Exists p \u2192 b\n| \u27e8h, _\u27e9 := h\n\ntheorem Exists.snd {p : b \u2192 Prop} : \u2200 h : Exists p, p h.fst\n| \u27e8_, h\u27e9 := h\n\ntheorem forall_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2200 h' : p, q h') \u2194 q h :=\n@forall_const (q h) p \u27e8h\u27e9\n\ntheorem exists_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2203 h' : p, q h') \u2194 q h :=\n@exists_const (q h) p \u27e8h\u27e9\n\ntheorem forall_prop_of_false {p : Prop} {q : p \u2192 Prop} (hn : \u00ac p) :\n  (\u2200 h' : p, q h') \u2194 true :=\niff_true_intro $ \u03bb h, hn.elim h\n\ntheorem exists_prop_of_false {p : Prop} {q : p \u2192 Prop} : \u00ac p \u2192 \u00ac (\u2203 h' : p, q h') :=\nmt Exists.fst\n\n@[congr] lemma exists_prop_congr {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : Exists q \u2194 \u2203 h : p', q' (hp.2 h) :=\n\u27e8\u03bb \u27e8_, _\u27e9, \u27e8hp.1 \u2039_\u203a, (hq _).1 \u2039_\u203a\u27e9, \u03bb \u27e8_, _\u27e9, \u27e8_, (hq _).2 \u2039_\u203a\u27e9\u27e9\n\n@[congr] lemma exists_prop_congr' {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : Exists q = \u2203 h : p', q' (hp.2 h) :=\npropext (exists_prop_congr hq _)\n\n@[simp] lemma exists_true_left (p : true \u2192 Prop) : (\u2203 x, p x) \u2194 p true.intro :=\nexists_prop_of_true _\n\n@[simp] lemma exists_false_left (p : false \u2192 Prop) : \u00ac \u2203 x, p x :=\nexists_prop_of_false not_false\n\nlemma exists_unique.exists {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} (h : \u2203! x, p x) : \u2203 x, p x :=\nexists.elim h (\u03bb x hx, \u27e8x, and.left hx\u27e9)\n\nlemma exists_unique.unique {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} (h : \u2203! x, p x)\n  {y\u2081 y\u2082 : \u03b1} (py\u2081 : p y\u2081) (py\u2082 : p y\u2082) : y\u2081 = y\u2082 :=\nunique_of_exists_unique h py\u2081 py\u2082\n\n@[congr] lemma forall_prop_congr {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : (\u2200 h, q h) \u2194 \u2200 h : p', q' (hp.2 h) :=\n\u27e8\u03bb h1 h2, (hq _).1 (h1 (hp.2 _)), \u03bb h1 h2, (hq _).2 (h1 (hp.1 h2))\u27e9\n\n@[congr] lemma forall_prop_congr' {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : (\u2200 h, q h) = \u2200 h : p', q' (hp.2 h) :=\npropext (forall_prop_congr hq _)\n\n@[simp] lemma forall_true_left (p : true \u2192 Prop) : (\u2200 x, p x) \u2194 p true.intro :=\nforall_prop_of_true _\n\n@[simp] lemma forall_false_left (p : false \u2192 Prop) : (\u2200 x, p x) \u2194 true :=\nforall_prop_of_false not_false\n\n@[simp] lemma exists_unique_iff_exists {\u03b1 : Sort*} [subsingleton \u03b1] {p : \u03b1 \u2192 Prop} :\n  (\u2203! x, p x) \u2194 \u2203 x, p x :=\n\u27e8\u03bb h, h.exists, Exists.imp $ \u03bb x hx, \u27e8hx, \u03bb y _, subsingleton.elim y x\u27e9\u27e9\n\nlemma exists_unique.elim2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 x (h : p x), Prop} {b : Prop} (h\u2082 : \u2203! x (h : p x), q x h)\n  (h\u2081 : \u2200 x (h : p x), q x h \u2192 (\u2200 y (hy : p y), q y hy \u2192 y = x) \u2192 b) : b :=\nbegin\n  simp only [exists_unique_iff_exists] at h\u2082,\n  apply h\u2082.elim,\n  exact \u03bb x \u27e8hxp, hxq\u27e9 H, h\u2081 x hxp hxq (\u03bb y hyp hyq, H y \u27e8hyp, hyq\u27e9)\nend\n\nlemma exists_unique.intro2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 (x : \u03b1) (h : p x), Prop} (w : \u03b1) (hp : p w) (hq : q w hp)\n  (H : \u2200 y (hy : p y), q y hy \u2192 y = w) :\n  \u2203! x (hx : p x), q x hx :=\nbegin\n  simp only [exists_unique_iff_exists],\n  exact exists_unique.intro w \u27e8hp, hq\u27e9 (\u03bb y \u27e8hyp, hyq\u27e9, H y hyp hyq)\nend\n\nlemma exists_unique.exists2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} {q : \u03a0 (x : \u03b1) (h : p x), Prop}\n  (h : \u2203! x (hx : p x), q x hx) :\n  \u2203 x (hx : p x), q x hx :=\nh.exists.imp (\u03bb x hx, hx.exists)\n\nlemma exists_unique.unique2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 (x : \u03b1) (hx : p x), Prop} (h : \u2203! x (hx : p x), q x hx)\n  {y\u2081 y\u2082 : \u03b1} (hpy\u2081 : p y\u2081) (hqy\u2081 : q y\u2081 hpy\u2081)\n  (hpy\u2082 : p y\u2082) (hqy\u2082 : q y\u2082 hpy\u2082) : y\u2081 = y\u2082 :=\nbegin\n  simp only [exists_unique_iff_exists] at h,\n  exact h.unique \u27e8hpy\u2081, hqy\u2081\u27e9 \u27e8hpy\u2082, hqy\u2082\u27e9\nend\n\nend quantifiers\n\n/-! ### Classical lemmas -/\n\nnamespace classical\nvariables {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop}\n\ntheorem cases {p : Prop \u2192 Prop} (h1 : p true) (h2 : p false) : \u2200a, p a :=\nassume a, cases_on a h1 h2\n\n/- use shortened names to avoid conflict when classical namespace is open. -/\nnoncomputable lemma dec (p : Prop) : decidable p := -- see Note [classical lemma]\nby apply_instance\nnoncomputable lemma dec_pred (p : \u03b1 \u2192 Prop) : decidable_pred p := -- see Note [classical lemma]\nby apply_instance\nnoncomputable lemma dec_rel (p : \u03b1 \u2192 \u03b1 \u2192 Prop) : decidable_rel p := -- see Note [classical lemma]\nby apply_instance\nnoncomputable lemma dec_eq (\u03b1 : Sort*) : decidable_eq \u03b1 := -- see Note [classical lemma]\nby apply_instance\n\n/--\nWe make decidability results that depends on `classical.choice` noncomputable lemmas.\n* We have to mark them as noncomputable, because otherwise Lean will try to generate bytecode\n  for them, and fail because it depends on `classical.choice`.\n* We make them lemmas, and not definitions, because otherwise later definitions will raise\n  \\\"failed to generate bytecode\\\" errors when writing something like\n  `letI := classical.dec_eq _`.\nCf. <https://leanprover-community.github.io/archive/stream/113488-general/topic/noncomputable.20theorem.html>\n-/\nlibrary_note \"classical lemma\"\n\n/-- Construct a function from a default value `H0`, and a function to use if there exists a value\nsatisfying the predicate. -/\n@[elab_as_eliminator]\nnoncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : \u2200 a, p a \u2192 C) : C :=\nif h : \u2203 a, p a then H (classical.some h) (classical.some_spec h) else H0\n\nlemma some_spec2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} {h : \u2203a, p a}\n  (q : \u03b1 \u2192 Prop) (hpq : \u2200a, p a \u2192 q a) : q (some h) :=\nhpq _ $ some_spec _\n\n/-- A version of classical.indefinite_description which is definitionally equal to a pair -/\nnoncomputable def subtype_of_exists {\u03b1 : Type*} {P : \u03b1 \u2192 Prop} (h : \u2203 x, P x) : {x // P x} :=\n\u27e8classical.some h, classical.some_spec h\u27e9\n\nend classical\n\n/-- This function has the same type as `exists.rec_on`, and can be used to case on an equality,\nbut `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe\nusing the axiom of choice. -/\n@[elab_as_eliminator]\nnoncomputable def {u} exists.classical_rec_on\n {\u03b1} {p : \u03b1 \u2192 Prop} (h : \u2203 a, p a) {C : Sort u} (H : \u2200 a, p a \u2192 C) : C :=\nH (classical.some h) (classical.some_spec h)\n\n/-! ### Declarations about bounded quantifiers -/\n\nsection bounded_quantifiers\nvariables {\u03b1 : Sort*} {r p q : \u03b1 \u2192 Prop} {P Q : \u2200 x, p x \u2192 Prop} {b : Prop}\n\ntheorem bex_def : (\u2203 x (h : p x), q x) \u2194 \u2203 x, p x \u2227 q x :=\n\u27e8\u03bb \u27e8x, px, qx\u27e9, \u27e8x, px, qx\u27e9, \u03bb \u27e8x, px, qx\u27e9, \u27e8x, px, qx\u27e9\u27e9\n\ntheorem bex.elim {b : Prop} : (\u2203 x h, P x h) \u2192 (\u2200 a h, P a h \u2192 b) \u2192 b\n| \u27e8a, h\u2081, h\u2082\u27e9 h' := h' a h\u2081 h\u2082\n\ntheorem bex.intro (a : \u03b1) (h\u2081 : p a) (h\u2082 : P a h\u2081) : \u2203 x (h : p x), P x h :=\n\u27e8a, h\u2081, h\u2082\u27e9\n\ntheorem ball_congr (H : \u2200 x h, P x h \u2194 Q x h) :\n  (\u2200 x h, P x h) \u2194 (\u2200 x h, Q x h) :=\nforall_congr $ \u03bb x, forall_congr (H x)\n\ntheorem bex_congr (H : \u2200 x h, P x h \u2194 Q x h) :\n  (\u2203 x h, P x h) \u2194 (\u2203 x h, Q x h) :=\nexists_congr $ \u03bb x, exists_congr (H x)\n\ntheorem bex_eq_left {a : \u03b1} : (\u2203 x (_ : x = a), p x) \u2194 p a :=\nby simp only [exists_prop, exists_eq_left]\n\ntheorem ball.imp_right (H : \u2200 x h, (P x h \u2192 Q x h))\n  (h\u2081 : \u2200 x h, P x h) (x h) : Q x h :=\nH _ _ $ h\u2081 _ _\n\ntheorem bex.imp_right (H : \u2200 x h, (P x h \u2192 Q x h)) :\n  (\u2203 x h, P x h) \u2192 \u2203 x h, Q x h\n| \u27e8x, h, h'\u27e9 := \u27e8_, _, H _ _ h'\u27e9\n\ntheorem ball.imp_left (H : \u2200 x, p x \u2192 q x)\n  (h\u2081 : \u2200 x, q x \u2192 r x) (x) (h : p x) : r x :=\nh\u2081 _ $ H _ h\n\ntheorem bex.imp_left (H : \u2200 x, p x \u2192 q x) :\n  (\u2203 x (_ : p x), r x) \u2192 \u2203 x (_ : q x), r x\n| \u27e8x, hp, hr\u27e9 := \u27e8x, H _ hp, hr\u27e9\n\ntheorem ball_of_forall (h : \u2200 x, p x) (x) : p x :=\nh x\n\ntheorem forall_of_ball (H : \u2200 x, p x) (h : \u2200 x, p x \u2192 q x) (x) : q x :=\nh x $ H x\n\ntheorem bex_of_exists (H : \u2200 x, p x) : (\u2203 x, q x) \u2192 \u2203 x (_ : p x), q x\n| \u27e8x, hq\u27e9 := \u27e8x, H x, hq\u27e9\n\ntheorem exists_of_bex : (\u2203 x (_ : p x), q x) \u2192 \u2203 x, q x\n| \u27e8x, _, hq\u27e9 := \u27e8x, hq\u27e9\n\n@[simp] theorem bex_imp_distrib : ((\u2203 x h, P x h) \u2192 b) \u2194 (\u2200 x h, P x h \u2192 b) :=\nby simp\n\ntheorem not_bex : (\u00ac \u2203 x h, P x h) \u2194 \u2200 x h, \u00ac P x h :=\nbex_imp_distrib\n\ntheorem not_ball_of_bex_not : (\u2203 x h, \u00ac P x h) \u2192 \u00ac \u2200 x h, P x h\n| \u27e8x, h, hp\u27e9 al := hp $ al x h\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_ball [decidable (\u2203 x h, \u00ac P x h)] [\u2200 x h, decidable (P x h)] :\n  (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) :=\n\u27e8not.decidable_imp_symm $ \u03bb nx x h, nx.decidable_imp_symm $ \u03bb h', \u27e8x, h, h'\u27e9,\n not_ball_of_bex_not\u27e9\n\ntheorem not_ball : (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) := decidable.not_ball\n\ntheorem ball_true_iff (p : \u03b1 \u2192 Prop) : (\u2200 x, p x \u2192 true) \u2194 true :=\niff_true_intro (\u03bb h hrx, trivial)\n\ntheorem ball_and_distrib : (\u2200 x h, P x h \u2227 Q x h) \u2194 (\u2200 x h, P x h) \u2227 (\u2200 x h, Q x h) :=\niff.trans (forall_congr $ \u03bb x, forall_and_distrib) forall_and_distrib\n\ntheorem bex_or_distrib : (\u2203 x h, P x h \u2228 Q x h) \u2194 (\u2203 x h, P x h) \u2228 (\u2203 x h, Q x h) :=\niff.trans (exists_congr $ \u03bb x, exists_or_distrib) exists_or_distrib\n\ntheorem ball_or_left_distrib : (\u2200 x, p x \u2228 q x \u2192 r x) \u2194 (\u2200 x, p x \u2192 r x) \u2227 (\u2200 x, q x \u2192 r x) :=\niff.trans (forall_congr $ \u03bb x, or_imp_distrib) forall_and_distrib\n\ntheorem bex_or_left_distrib :\n  (\u2203 x (_ : p x \u2228 q x), r x) \u2194 (\u2203 x (_ : p x), r x) \u2228 (\u2203 x (_ : q x), r x) :=\nby simp only [exists_prop]; exact\niff.trans (exists_congr $ \u03bb x, or_and_distrib_right) exists_or_distrib\n\nend bounded_quantifiers\n\nnamespace classical\nlocal attribute [instance] prop_decidable\n\ntheorem not_ball {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} {P : \u03a0 (x : \u03b1), p x \u2192 Prop} :\n  (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) := _root_.not_ball\n\nend classical\n\nlemma ite_eq_iff {\u03b1} {p : Prop} [decidable p] {a b c : \u03b1} :\n  (if p then a else b) = c \u2194 p \u2227 a = c \u2228 \u00acp \u2227 b = c :=\nby by_cases p; simp *\n\n@[simp] lemma ite_eq_left_iff {\u03b1} {p : Prop} [decidable p] {a b : \u03b1} :\n  (if p then a else b) = a \u2194 (\u00acp \u2192 b = a) :=\nby by_cases p; simp *\n\n@[simp] lemma ite_eq_right_iff {\u03b1} {p : Prop} [decidable p] {a b : \u03b1} :\n  (if p then a else b) = b \u2194 (p \u2192 a = b) :=\nby by_cases p; simp *\n\n/-! ### Declarations about `nonempty` -/\n\nsection nonempty\nuniverse variables u v w\nvariables {\u03b1 : Type u} {\u03b2 : Type v} {\u03b3 : \u03b1 \u2192 Type w}\n\nattribute [simp] nonempty_of_inhabited\n\n@[priority 20]\ninstance has_zero.nonempty [has_zero \u03b1] : nonempty \u03b1 := \u27e80\u27e9\n@[priority 20]\ninstance has_one.nonempty [has_one \u03b1] : nonempty \u03b1 := \u27e81\u27e9\n\nlemma exists_true_iff_nonempty {\u03b1 : Sort*} : (\u2203a:\u03b1, true) \u2194 nonempty \u03b1 :=\niff.intro (\u03bb\u27e8a, _\u27e9, \u27e8a\u27e9) (\u03bb\u27e8a\u27e9, \u27e8a, trivial\u27e9)\n\n@[simp] lemma nonempty_Prop {p : Prop} : nonempty p \u2194 p :=\niff.intro (assume \u27e8h\u27e9, h) (assume h, \u27e8h\u27e9)\n\nlemma not_nonempty_iff_imp_false {\u03b1 : Sort*} : \u00ac nonempty \u03b1 \u2194 \u03b1 \u2192 false :=\n\u27e8\u03bb h a, h \u27e8a\u27e9, \u03bb h \u27e8a\u27e9, h a\u27e9\n\n@[simp] lemma nonempty_sigma : nonempty (\u03a3a:\u03b1, \u03b3 a) \u2194 (\u2203a:\u03b1, nonempty (\u03b3 a)) :=\niff.intro (assume \u27e8\u27e8a, c\u27e9\u27e9, \u27e8a, \u27e8c\u27e9\u27e9) (assume \u27e8a, \u27e8c\u27e9\u27e9, \u27e8\u27e8a, c\u27e9\u27e9)\n\n@[simp] lemma nonempty_subtype {\u03b1 : Sort u} {p : \u03b1 \u2192 Prop} : nonempty (subtype p) \u2194 (\u2203a:\u03b1, p a) :=\niff.intro (assume \u27e8\u27e8a, h\u27e9\u27e9, \u27e8a, h\u27e9) (assume \u27e8a, h\u27e9, \u27e8\u27e8a, h\u27e9\u27e9)\n\n@[simp] lemma nonempty_prod : nonempty (\u03b1 \u00d7 \u03b2) \u2194 (nonempty \u03b1 \u2227 nonempty \u03b2) :=\niff.intro (assume \u27e8\u27e8a, b\u27e9\u27e9, \u27e8\u27e8a\u27e9, \u27e8b\u27e9\u27e9) (assume \u27e8\u27e8a\u27e9, \u27e8b\u27e9\u27e9, \u27e8\u27e8a, b\u27e9\u27e9)\n\n@[simp] lemma nonempty_pprod {\u03b1 : Sort u} {\u03b2 : Sort v} :\n  nonempty (pprod \u03b1 \u03b2) \u2194 (nonempty \u03b1 \u2227 nonempty \u03b2) :=\niff.intro (assume \u27e8\u27e8a, b\u27e9\u27e9, \u27e8\u27e8a\u27e9, \u27e8b\u27e9\u27e9) (assume \u27e8\u27e8a\u27e9, \u27e8b\u27e9\u27e9, \u27e8\u27e8a, b\u27e9\u27e9)\n\n@[simp] lemma nonempty_sum : nonempty (\u03b1 \u2295 \u03b2) \u2194 (nonempty \u03b1 \u2228 nonempty \u03b2) :=\niff.intro\n  (assume \u27e8h\u27e9, match h with sum.inl a := or.inl \u27e8a\u27e9 | sum.inr b := or.inr \u27e8b\u27e9 end)\n  (assume h, match h with or.inl \u27e8a\u27e9 := \u27e8sum.inl a\u27e9 | or.inr \u27e8b\u27e9 := \u27e8sum.inr b\u27e9 end)\n\n@[simp] lemma nonempty_psum {\u03b1 : Sort u} {\u03b2 : Sort v} :\n  nonempty (psum \u03b1 \u03b2) \u2194 (nonempty \u03b1 \u2228 nonempty \u03b2) :=\niff.intro\n  (assume \u27e8h\u27e9, match h with psum.inl a := or.inl \u27e8a\u27e9 | psum.inr b := or.inr \u27e8b\u27e9 end)\n  (assume h, match h with or.inl \u27e8a\u27e9 := \u27e8psum.inl a\u27e9 | or.inr \u27e8b\u27e9 := \u27e8psum.inr b\u27e9 end)\n\n@[simp] lemma nonempty_psigma {\u03b1 : Sort u} {\u03b2 : \u03b1 \u2192 Sort v} :\n  nonempty (psigma \u03b2) \u2194 (\u2203a:\u03b1, nonempty (\u03b2 a)) :=\niff.intro (assume \u27e8\u27e8a, c\u27e9\u27e9, \u27e8a, \u27e8c\u27e9\u27e9) (assume \u27e8a, \u27e8c\u27e9\u27e9, \u27e8\u27e8a, c\u27e9\u27e9)\n\n@[simp] lemma nonempty_empty : \u00ac nonempty empty :=\nassume \u27e8h\u27e9, h.elim\n\n@[simp] lemma nonempty_ulift : nonempty (ulift \u03b1) \u2194 nonempty \u03b1 :=\niff.intro (assume \u27e8\u27e8a\u27e9\u27e9, \u27e8a\u27e9) (assume \u27e8a\u27e9, \u27e8\u27e8a\u27e9\u27e9)\n\n@[simp] lemma nonempty_plift {\u03b1 : Sort u} : nonempty (plift \u03b1) \u2194 nonempty \u03b1 :=\niff.intro (assume \u27e8\u27e8a\u27e9\u27e9, \u27e8a\u27e9) (assume \u27e8a\u27e9, \u27e8\u27e8a\u27e9\u27e9)\n\n@[simp] lemma nonempty.forall {\u03b1 : Sort u} {p : nonempty \u03b1 \u2192 Prop} :\n  (\u2200h:nonempty \u03b1, p h) \u2194 (\u2200a, p \u27e8a\u27e9) :=\niff.intro (assume h a, h _) (assume h \u27e8a\u27e9, h _)\n\n@[simp] lemma nonempty.exists {\u03b1 : Sort u} {p : nonempty \u03b1 \u2192 Prop} :\n  (\u2203h:nonempty \u03b1, p h) \u2194 (\u2203a, p \u27e8a\u27e9) :=\niff.intro (assume \u27e8\u27e8a\u27e9, h\u27e9, \u27e8a, h\u27e9) (assume \u27e8a, h\u27e9, \u27e8\u27e8a\u27e9, h\u27e9)\n\nlemma classical.nonempty_pi {\u03b1 : Sort u} {\u03b2 : \u03b1 \u2192 Sort v} :\n  nonempty (\u03a0a:\u03b1, \u03b2 a) \u2194 (\u2200a:\u03b1, nonempty (\u03b2 a)) :=\niff.intro (assume \u27e8f\u27e9 a, \u27e8f a\u27e9) (assume f, \u27e8assume a, classical.choice $ f a\u27e9)\n\n/-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued)\n  `inhabited` instance. `classical.inhabited_of_nonempty` already exists, in\n  `core/init/classical.lean`, but the assumption is not a type class argument,\n  which makes it unsuitable for some applications. -/\nnoncomputable def classical.inhabited_of_nonempty' {\u03b1 : Sort u} [h : nonempty \u03b1] : inhabited \u03b1 :=\n\u27e8classical.choice h\u27e9\n\n/-- Using `classical.choice`, extracts a term from a `nonempty` type. -/\n@[reducible] protected noncomputable def nonempty.some {\u03b1 : Sort u} (h : nonempty \u03b1) : \u03b1 :=\nclassical.choice h\n\n/-- Using `classical.choice`, extracts a term from a `nonempty` type. -/\n@[reducible] protected noncomputable def classical.arbitrary (\u03b1 : Sort u) [h : nonempty \u03b1] : \u03b1 :=\nclassical.choice h\n\n/-- Given `f : \u03b1 \u2192 \u03b2`, if `\u03b1` is nonempty then `\u03b2` is also nonempty.\n  `nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/\nlemma nonempty.map {\u03b1 : Sort u} {\u03b2 : Sort v} (f : \u03b1 \u2192 \u03b2) : nonempty \u03b1 \u2192 nonempty \u03b2\n| \u27e8h\u27e9 := \u27e8f h\u27e9\n\nprotected lemma nonempty.map2 {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) : nonempty \u03b1 \u2192 nonempty \u03b2 \u2192 nonempty \u03b3\n| \u27e8x\u27e9 \u27e8y\u27e9 := \u27e8f x y\u27e9\n\nprotected lemma nonempty.congr {\u03b1 : Sort u} {\u03b2 : Sort v} (f : \u03b1 \u2192 \u03b2) (g : \u03b2 \u2192 \u03b1) :\n  nonempty \u03b1 \u2194 nonempty \u03b2 :=\n\u27e8nonempty.map f, nonempty.map g\u27e9\n\nlemma nonempty.elim_to_inhabited {\u03b1 : Sort*} [h : nonempty \u03b1] {p : Prop}\n  (f : inhabited \u03b1 \u2192 p) : p :=\nh.elim $ f \u2218 inhabited.mk\n\ninstance {\u03b1 \u03b2} [h : nonempty \u03b1] [h2 : nonempty \u03b2] : nonempty (\u03b1 \u00d7 \u03b2) :=\nh.elim $ \u03bb g, h2.elim $ \u03bb g2, \u27e8\u27e8g, g2\u27e9\u27e9\n\nend nonempty\n\nlemma subsingleton_of_not_nonempty {\u03b1 : Sort*} (h : \u00ac nonempty \u03b1) : subsingleton \u03b1 :=\n\u27e8\u03bb x, false.elim $ not_nonempty_iff_imp_false.mp h x\u27e9\n\nsection ite\n\n/-- A `dite` whose results do not actually depend on the condition may be reduced to an `ite`. -/\n@[simp]\nlemma dite_eq_ite (P : Prop) [decidable P] {\u03b1 : Sort*} (x y : \u03b1) :\n  dite P (\u03bb h, x) (\u03bb h, y) = ite P x y := rfl\n\n/-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/\nlemma apply_dite {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (P : Prop) [decidable P] (x : P \u2192 \u03b1) (y : \u00acP \u2192 \u03b1) :\n  f (dite P x y) = dite P (\u03bb h, f (x h)) (\u03bb h, f (y h)) :=\nby { by_cases h : P; simp [h] }\n\n/-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/\nlemma apply_ite {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (P : Prop) [decidable P] (x y : \u03b1) :\n  f (ite P x y) = ite P (f x) (f y) :=\napply_dite f P (\u03bb _, x) (\u03bb _, y)\n\n/-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function\napplied to each of the branches. -/\nlemma apply_dite2 {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) (P : Prop) [decidable P] (a : P \u2192 \u03b1)\n  (b : \u00acP \u2192 \u03b1) (c : P \u2192 \u03b2) (d : \u00acP \u2192 \u03b2) :\n  f (dite P a b) (dite P c d) = dite P (\u03bb h, f (a h) (c h)) (\u03bb h, f (b h) (d h)) :=\nby { by_cases h : P; simp [h] }\n\n/-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function\napplied to each of the branches. -/\nlemma apply_ite2 {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) (P : Prop) [decidable P] (a b : \u03b1) (c d : \u03b2) :\n  f (ite P a b) (ite P c d) = ite P (f a c) (f b d) :=\napply_dite2 f P (\u03bb _, a) (\u03bb _, b) (\u03bb _, c) (\u03bb _, d)\n\n/-- A 'dite' producing a `Pi` type `\u03a0 a, \u03b2 a`, applied to a value `x : \u03b1`\nis a `dite` that applies either branch to `x`. -/\nlemma dite_apply {\u03b1 : Sort*} {\u03b2 : \u03b1 \u2192 Sort*} (P : Prop) [decidable P]\n  (f : P \u2192 \u03a0 a, \u03b2 a) (g : \u00ac P \u2192 \u03a0 a, \u03b2 a) (x : \u03b1) :\n  (dite P f g) x = dite P (\u03bb h, f h x) (\u03bb h, g h x) :=\nby { by_cases h : P; simp [h] }\n\n/-- A 'ite' producing a `Pi` type `\u03a0 a, \u03b2 a`, applied to a value `x : \u03b1`\nis a `ite` that applies either branch to `x` -/\nlemma ite_apply {\u03b1 : Sort*} {\u03b2 : \u03b1 \u2192 Sort*} (P : Prop) [decidable P]\n  (f g : \u03a0 a, \u03b2 a) (x : \u03b1) :\n  (ite P f g) x = ite P (f x) (g x) :=\ndite_apply P (\u03bb _, f) (\u03bb _, g) x\n\n/-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/\n@[simp] lemma dite_not {\u03b1 : Sort*} (P : Prop) [decidable P] (x : \u00ac P \u2192 \u03b1) (y : \u00ac\u00ac P \u2192 \u03b1) :\n  dite (\u00ac P) x y = dite P (\u03bb h, y (not_not_intro h)) x :=\nby { by_cases h : P; simp [h] }\n\n/-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/\n@[simp] lemma ite_not {\u03b1 : Sort*} (P : Prop) [decidable P] (x y : \u03b1) :\n  ite (\u00ac P) x y = ite P y x :=\ndite_not P (\u03bb _, x) (\u03bb _, y)\n\nlemma ite_and {\u03b1} {p q : Prop} [decidable p] [decidable q] {x y : \u03b1} :\n  ite (p \u2227 q) x y = ite p (ite q x y) y :=\nby { by_cases hp : p; by_cases hq : q; simp [hp, hq] }\n\n\nend ite\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/logic/basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.5, "lm_q2_score": 0.15203223970113983, "lm_q1q2_score": 0.07601611985056991}}
{"text": "/-\nCopyright (c) 2020 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon\n-/\nimport tactic.core\n\n/-!\n# `pretty_cases` tactic\n\nWhen using `induction` and `cases`, `pretty_cases` prints a `\"Try\nthis:\"` advice that shows how to structure the proof with\n`case { ... }` commands.  In the following example, we apply induction on a\npermutation assumption about lists. `pretty_cases` gives us a proof\nskeleton that explicit selects the branches and explicit names the\nnew local constants:\n\n```lean\nexample {\u03b1} (xs ys : list \u03b1) (h : xs ~ ys) : true :=\nbegin\n  induction h,\n  pretty_cases,\n    -- Try this:\n    --   case list.perm.nil :\n    --   { admit },\n    --   case list.perm.cons : h_x h_l\u2081 h_l\u2082 h_a h_ih\n    --   { admit },\n    --   case list.perm.swap : h_x h_y h_l\n    --   { admit },\n    --   case list.perm.trans : h_l\u2081 h_l\u2082 h_l\u2083 h_a h_a_1 h_ih_a h_ih_a_1\n    --   { admit },\nend\n```\n\n## Main definitions\n\n * `pretty_cases_advice` return `pretty_cases` advice without printing it\n * `pretty_cases` main tactic\n-/\n\nnamespace tactic\n\n/-- Query the proof goal and print the skeleton of a proof by cases. -/\nmeta def pretty_cases_advice : tactic string := retrieve $ do\ngs \u2190 get_goals,\ncases \u2190 gs.mmap $ \u03bb g, do\n{ t : list name \u2190 get_tag g,\n  let vs := t.tail,\n  let \u27e8vs,ts\u27e9 := vs.span (\u03bb n, name.last_string n = \"_arg\"),\n  set_goals [g],\n  ls \u2190 local_context,\n  let m := native.rb_map.of_list $ (ls.map expr.local_uniq_name).zip (ls.map expr.local_pp_name),\n  let vs := vs.map $ \u03bb v, (m.find v.get_prefix).get_or_else `_,\n  let var_decls := string.intercalate \" \" $ vs.map to_string,\n  let var_decls := if vs.empty then \"\" else \" : \" ++ var_decls,\n  pure sformat!\"  case {ts.head}{var_decls}\\n  {{ admit }\" },\nlet cases := string.intercalate \",\\n\" cases,\npure sformat!\"Try this:\\n{cases}\"\n\nnamespace interactive\n\n/--\nQuery the proof goal and print the skeleton of a proof by\ncases.\n\nFor example, let us consider the following proof:\n\n```lean\nexample {\u03b1} (xs ys : list \u03b1) (h : xs ~ ys) : true :=\nbegin\n  induction h,\n  pretty_cases,\n    -- Try this:\n    --   case list.perm.nil :\n    --   { admit },\n    --   case list.perm.cons : h_x h_l\u2081 h_l\u2082 h_a h_ih\n    --   { admit },\n    --   case list.perm.swap : h_x h_y h_l\n    --   { admit },\n    --   case list.perm.trans : h_l\u2081 h_l\u2082 h_l\u2083 h_a h_a_1 h_ih_a h_ih_a_1\n    --   { admit },\nend\n```\n\nThe output helps the user layout the cases and rename the\nintroduced variables.\n-/\nmeta def pretty_cases : tactic unit :=\npretty_cases_advice >>= trace\n\nadd_tactic_doc\n{ name       := \"pretty_cases\",\n  category   := doc_category.tactic,\n  decl_names := [``tactic.interactive.pretty_cases],\n  tags       := [\"context management\", \"goal management\"] }\n\nend interactive\n\nend tactic\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/tactic/pretty_cases.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.38491213037224875, "lm_q2_score": 0.19682620128743875, "lm_q1q2_score": 0.0757607924506251}}
{"text": "/-\nCopyright (c) 2018 Mario Carneiro. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro\n\n! This file was ported from Lean 3 source module data.erased\n! leanprover-community/mathlib commit 10b4e499f43088dd3bb7b5796184ad5216648ab1\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathlib.Logic.Equiv.Defs\n\n/-!\n# A type for VM-erased data\n\nThis file defines a type `Erased \u03b1` which is classically isomorphic to `\u03b1`,\nbut erased in the VM. That is, at runtime every value of `Erased \u03b1` is\nrepresented as `0`, just like types and proofs.\n-/\n\n\nuniverse u\n\n/-- `Erased \u03b1` is the same as `\u03b1`, except that the elements\n  of `Erased \u03b1` are erased in the VM in the same way as types\n  and proofs. This can be used to track data without storing it\n  literally. -/\ndef Erased (\u03b1 : Sort u) : Sort max 1 u :=\n  \u03a3's : \u03b1 \u2192 Prop, \u2203 a, (fun b => a = b) = s\n#align erased Erased\n\nnamespace Erased\n\n/-- Erase a value. -/\n@[inline]\ndef mk {\u03b1} (a : \u03b1) : Erased \u03b1 :=\n  \u27e8fun b => a = b, a, rfl\u27e9\n#align erased.mk Erased.mk\n\n/-- Extracts the erased value, noncomputably. -/\nnoncomputable def out {\u03b1} : Erased \u03b1 \u2192 \u03b1\n  | \u27e8_, h\u27e9 => Classical.choose h\n#align erased.out Erased.out\n\n/-- Extracts the erased value, if it is a type.\n\nNote: `(mk a).OutType` is not definitionally equal to `a`.\n-/\n@[reducible]\ndef OutType (a : Erased (Sort u)) : Sort u :=\n  out a\n#align erased.out_type Erased.OutType\n\n/-- Extracts the erased value, if it is a proof. -/\ntheorem out_proof {p : Prop} (a : Erased p) : p :=\n  out a\n#align erased.out_proof Erased.out_proof\n\n@[simp]\ntheorem out_mk {\u03b1} (a : \u03b1) : (mk a).out = a := by\n  let h := (mk a).2; show Classical.choose h = a\n  have := Classical.choose_spec h\n  exact cast (congr_fun this a).symm rfl\n#align erased.out_mk Erased.out_mk\n\n@[simp]\ntheorem mk_out {\u03b1} : \u2200 a : Erased \u03b1, mk (out a) = a\n  | \u27e8s, h\u27e9 => by simp [mk] ; congr ; exact Classical.choose_spec h\n#align erased.mk_out Erased.mk_out\n\n@[ext]\ntheorem out_inj {\u03b1} (a b : Erased \u03b1) (h : a.out = b.out) : a = b := by simpa using congr_arg mk h\n#align erased.out_inj Erased.out_inj\n\n/-- Equivalence between `Erased \u03b1` and `\u03b1`. -/\nnoncomputable def equiv (\u03b1) : Erased \u03b1 \u2243 \u03b1 :=\n  \u27e8out, mk, mk_out, out_mk\u27e9\n#align erased.equiv Erased.equiv\n\ninstance (\u03b1 : Type u) : Repr (Erased \u03b1) :=\n  \u27e8fun _ _ => \"Erased\"\u27e9\n\ninstance (\u03b1 : Type u) : ToString (Erased \u03b1) :=\n  \u27e8fun _ => \"Erased\"\u27e9\n\n-- Porting note: Deleted `has_to_format`\n\n/-- Computably produce an erased value from a proof of nonemptiness. -/\ndef choice {\u03b1} (h : Nonempty \u03b1) : Erased \u03b1 :=\n  mk (Classical.choice h)\n#align erased.choice Erased.choice\n\n@[simp]\n\n\ninstance {\u03b1} [h : Nonempty \u03b1] : Inhabited (Erased \u03b1) :=\n  \u27e8choice h\u27e9\n\n/-- `(>>=)` operation on `Erased`.\n\nThis is a separate definition because `\u03b1` and `\u03b2` can live in different\nuniverses (the universe is fixed in `Monad`).\n-/\ndef bind {\u03b1 \u03b2} (a : Erased \u03b1) (f : \u03b1 \u2192 Erased \u03b2) : Erased \u03b2 :=\n  \u27e8fun b => (f a.out).1 b, (f a.out).2\u27e9\n#align erased.bind Erased.bind\n\n@[simp]\ntheorem bind_eq_out {\u03b1 \u03b2} (a f) : @bind \u03b1 \u03b2 a f = f a.out := rfl\n#align erased.bind_eq_out Erased.bind_eq_out\n\n/-- Collapses two levels of erasure.\n-/\ndef join {\u03b1} (a : Erased (Erased \u03b1)) : Erased \u03b1 :=\n  bind a id\n#align erased.join Erased.join\n\n@[simp]\ntheorem join_eq_out {\u03b1} (a) : @join \u03b1 a = a.out :=\n  bind_eq_out _ _\n#align erased.join_eq_out Erased.join_eq_out\n\n/-- `(<$>)` operation on `Erased`.\n\nThis is a separate definition because `\u03b1` and `\u03b2` can live in different\nuniverses (the universe is fixed in `Functor`).\n-/\ndef map {\u03b1 \u03b2} (f : \u03b1 \u2192 \u03b2) (a : Erased \u03b1) : Erased \u03b2 :=\n  bind a (mk \u2218 f)\n#align erased.map Erased.map\n\n@[simp]\ntheorem map_out {\u03b1 \u03b2} {f : \u03b1 \u2192 \u03b2} (a : Erased \u03b1) : (a.map f).out = f a.out := by simp [map]\n#align erased.map_out Erased.map_out\n\nprotected instance Monad : Monad Erased where\n  pure := @mk\n  bind := @bind\n  map := @map\n#align erased.monad Erased.Monad\n\n@[simp]\ntheorem pure_def {\u03b1} : (pure : \u03b1 \u2192 Erased \u03b1) = @mk _ :=\n  rfl\n#align erased.pure_def Erased.pure_def\n\n@[simp]\ntheorem bind_def {\u03b1 \u03b2} : ((\u00b7 >>= \u00b7) : Erased \u03b1 \u2192 (\u03b1 \u2192 Erased \u03b2) \u2192 Erased \u03b2) = @bind _ _ :=\n  rfl\n#align erased.bind_def Erased.bind_def\n\n@[simp]\ntheorem map_def {\u03b1 \u03b2} : ((\u00b7 <$> \u00b7) : (\u03b1 \u2192 \u03b2) \u2192 Erased \u03b1 \u2192 Erased \u03b2) = @map _ _ :=\n  rfl\n#align erased.map_def Erased.map_def\n\n--Porting note: Old proof `by refine' { .. } <;> intros <;> ext <;> simp`\nprotected instance LawfulMonad : LawfulMonad Erased :=\n  { Erased.Monad with\n    id_map := by intros ; ext ; simp\n    map_const := by intros ; ext ; simp [Functor.mapConst]\n    pure_bind := by intros ; ext ; simp\n    bind_assoc := by intros ; ext ; simp\n    bind_pure_comp := by intros ; ext ; simp\n    bind_map := by intros ; ext ; simp [Seq.seq]\n    seqLeft_eq := by intros ; ext ; simp [Seq.seq, Functor.mapConst, SeqLeft.seqLeft]\n    seqRight_eq := by intros ; ext ; simp [Seq.seq, Functor.mapConst, SeqRight.seqRight]\n    pure_seq := by intros ; ext ; simp [Seq.seq, Functor.mapConst, SeqRight.seqRight] }\n\nend Erased\n", "meta": {"author": "leanprover-community", "repo": "mathlib4", "sha": "b9a0a30342ca06e9817e22dbe46e75fc7f435500", "save_path": "github-repos/lean/leanprover-community-mathlib4", "path": "github-repos/lean/leanprover-community-mathlib4/mathlib4-b9a0a30342ca06e9817e22dbe46e75fc7f435500/Mathlib/Data/Erased.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4960938294709195, "lm_q2_score": 0.1520322435432087, "lm_q1q2_score": 0.07542225790240589}}
{"text": "/-\nCopyright (c) 2020 Johan Commelin. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Johan Commelin\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.interactive\nimport Mathlib.PostPort\n\nnamespace Mathlib\n\n/-!\n# `dec_trivial` tactic\n\nThe `dec_trivial` tactic tries to use decidability to prove a goal.\nIt is basically a glorified wrapper around `exact dec_trivial`.\n\nThere is an extra option to make it a little bit smarter:\n`dec_trivial!` will revert all hypotheses on which the target depends,\nbefore it tries `exact dec_trivial`.\n-/\n\n/-- `dec_trivial` tries to use decidability to prove a goal\n(i.e., using `exact dec_trivial`).\nThe variant `dec_trivial!` will revert all hypotheses on which the target depends,\nbefore it tries `exact dec_trivial`.\n\nExample:\n```lean\nexample (n : \u2115) (h : n < 2) : n = 0 \u2228 n = 1 :=\nby dec_trivial!\n```\n-/\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/tactic/dec_trivial_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48828339529583475, "lm_q2_score": 0.14804719615221754, "lm_q1q2_score": 0.07228898760123321}}
{"text": "/-\nCopyright (c) 2017 Microsoft Corporation. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor: Leonardo de Moura\n\n! This file was ported from Lean 3 source module data.dlist\n! leanprover-community/mathlib commit 95fa4cfb0a8774570d67bb231c1ab088a94e12bb\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\n\nuniverse u\n\n/-- A difference list is a function that, given a list, returns the original\ncontents of the difference list prepended to the given list.\n\nThis structure supports `O(1)` `append` and `concat` operations on lists, making it\nuseful for append-heavy uses such as logging and pretty printing.\n-/\nstructure Dlist (\u03b1 : Type u) where\n  apply : List \u03b1 \u2192 List \u03b1\n  invariant : \u2200 l, apply l = apply [] ++ l\n#align dlist Dlist\n\nnamespace Dlist\n\nopen Function\n\nvariable {\u03b1 : Type u}\n\n-- mathport name: \u00abexpr\u266f\u00bb\nlocal notation:arg \"\u266f\" => by abstract intros ; simp\n\n/-- Convert a list to a dlist -/\ndef ofList (l : List \u03b1) : Dlist \u03b1 :=\n  \u27e8append l, \u266f\u27e9\n#align dlist.of_list Dlist.ofList\n\n/- warning: dlist.lazy_of_list -> Std.DList.lazy_ofList is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}}, (Thunk\u2093.{u1} (List.{u1} \u03b1)) -> (Dlist.{u1} \u03b1)\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}}, (Thunk.{u1} (List.{u1} \u03b1)) -> (Std.DList.{u1} \u03b1)\nCase conversion may be inaccurate. Consider using '#align dlist.lazy_of_list Std.DList.lazy_ofList\u2093'. -/\n/-- Convert a lazily-evaluated list to a dlist -/\ndef Std.DList.lazy_ofList (l : Thunk (List \u03b1)) : Dlist \u03b1 :=\n  \u27e8fun xs => l () ++ xs, \u266f\u27e9\n#align dlist.lazy_of_list Std.DList.lazy_ofList\n\n/-- Convert a dlist to a list -/\ndef toList : Dlist \u03b1 \u2192 List \u03b1\n  | \u27e8xs, _\u27e9 => xs []\n#align dlist.to_list Dlist.toList\n\n/-- Create a dlist containing no elements -/\ndef empty : Dlist \u03b1 :=\n  \u27e8id, \u266f\u27e9\n#align dlist.empty Dlist.empty\n\n-- mathport name: \u00abexpr ::_\u00bb\nlocal notation:arg a \"::_\" => List.cons a\n\n/-- Create dlist with a single element -/\ndef singleton (x : \u03b1) : Dlist \u03b1 :=\n  \u27e8x::_, \u266f\u27e9\n#align dlist.singleton Dlist.singleton\n\nattribute [local simp] Function.comp\n\n/-- `O(1)` Prepend a single element to a dlist -/\ndef cons (x : \u03b1) : Dlist \u03b1 \u2192 Dlist \u03b1\n  | \u27e8xs, h\u27e9 => \u27e8x::_ \u2218 xs, by abstract intros ; simp; rw [\u2190 h]\u27e9\n#align dlist.cons Dlist.cons\n\n/-- `O(1)` Append a single element to a dlist -/\ndef concat (x : \u03b1) : Dlist \u03b1 \u2192 Dlist \u03b1\n  | \u27e8xs, h\u27e9 => \u27e8xs \u2218 x::_, by abstract intros ; simp; rw [h, h [x]]; simp\u27e9\n#align dlist.concat Dlist.concat\n\n/-- `O(1)` Append dlists -/\nprotected def append : Dlist \u03b1 \u2192 Dlist \u03b1 \u2192 Dlist \u03b1\n  | \u27e8xs, h\u2081\u27e9, \u27e8ys, h\u2082\u27e9 =>\n    \u27e8xs \u2218 ys, by\n      intros\n      simp\n      rw [h\u2082, h\u2081, h\u2081 (ys List.nil)]\n      simp\u27e9\n#align dlist.append Dlist.append\n\ninstance : Append (Dlist \u03b1) :=\n  \u27e8Dlist.append\u27e9\n\nattribute [local simp] of_list to_list Empty singleton cons concat Dlist.append\n\n#print Std.DList.toList_ofList /-\ntheorem Std.DList.toList_ofList (l : List \u03b1) : toList (ofList l) = l := by cases l <;> simp\n#align dlist.to_list_of_list Std.DList.toList_ofList\n-/\n\n/- warning: dlist.of_list_to_list -> Std.DList.ofList_toList is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}} (l : Dlist.{u1} \u03b1), Eq.{succ u1} (Dlist.{u1} \u03b1) (Dlist.ofList.{u1} \u03b1 (Dlist.toList.{u1} \u03b1 l)) l\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}} (l : Std.DList.{u1} \u03b1), Eq.{succ u1} (Std.DList.{u1} \u03b1) (Std.DList.ofList.{u1} \u03b1 (Std.DList.toList.{u1} \u03b1 l)) l\nCase conversion may be inaccurate. Consider using '#align dlist.of_list_to_list Std.DList.ofList_toList\u2093'. -/\ntheorem Std.DList.ofList_toList (l : Dlist \u03b1) : ofList (toList l) = l :=\n  by\n  cases' l with xs\n  have h : append (xs []) = xs := by\n    intros\n    funext x\n    simp [l_invariant x]\n  simp [h]\n#align dlist.of_list_to_list Std.DList.ofList_toList\n\ntheorem toList_empty : toList (@empty \u03b1) = [] := by simp\n#align dlist.to_list_empty Dlist.toList_empty\n\ntheorem toList_singleton (x : \u03b1) : toList (singleton x) = [x] := by simp\n#align dlist.to_list_singleton Dlist.toList_singleton\n\ntheorem toList_append (l\u2081 l\u2082 : Dlist \u03b1) : toList (l\u2081 ++ l\u2082) = toList l\u2081 ++ toList l\u2082 :=\n  show toList (Dlist.append l\u2081 l\u2082) = toList l\u2081 ++ toList l\u2082 by\n    cases l\u2081 <;> cases l\u2082 <;> simp <;> rw [l\u2081_invariant]\n#align dlist.to_list_append Dlist.toList_append\n\ntheorem toList_cons (x : \u03b1) (l : Dlist \u03b1) : toList (cons x l) = x :: toList l := by cases l <;> simp\n#align dlist.to_list_cons Dlist.toList_cons\n\ntheorem toList_concat (x : \u03b1) (l : Dlist \u03b1) : toList (concat x l) = toList l ++ [x] := by\n  cases l <;> simp <;> rw [l_invariant]\n#align dlist.to_list_concat Dlist.toList_concat\n\nend Dlist\n\n", "meta": {"author": "leanprover-community", "repo": "lean3port", "sha": "9ed1898f23e4379865ee93d62cb6353e5ed6c270", "save_path": "github-repos/lean/leanprover-community-lean3port", "path": "github-repos/lean/leanprover-community-lean3port/lean3port-9ed1898f23e4379865ee93d62cb6353e5ed6c270/Leanbin/Data/Dlist.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.44167302036300954, "lm_q2_score": 0.16238003869345893, "lm_q1q2_score": 0.07171888213640236}}
{"text": "/-\nCopyright (c) 2017 Johannes H\u00f6lzl. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Johannes H\u00f6lzl, Scott Morrison\n-/\nimport algebra.big_operators.finsupp\nimport algebra.hom.group_action\nimport algebra.regular.smul\nimport data.finset.preimage\nimport data.rat.big_operators\n\n/-!\n# Miscellaneous definitions, lemmas, and constructions using finsupp\n\n> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.\n> Any changes to this file require a corresponding PR to mathlib4.\n\n## Main declarations\n\n* `finsupp.graph`: the finset of input and output pairs with non-zero outputs.\n* `finsupp.map_range.equiv`: `finsupp.map_range` as an equiv.\n* `finsupp.map_domain`: maps the domain of a `finsupp` by a function and by summing.\n* `finsupp.comap_domain`: postcomposition of a `finsupp` with a function injective on the preimage\n  of its support.\n* `finsupp.some`: restrict a finitely supported function on `option \u03b1` to a finitely supported\n  function on `\u03b1`.\n* `finsupp.filter`: `filter p f` is the finitely supported function that is `f a` if `p a` is true\n  and 0 otherwise.\n* `finsupp.frange`: the image of a finitely supported function on its support.\n* `finsupp.subtype_domain`: the restriction of a finitely supported function `f` to a subtype.\n\n## Implementation notes\n\nThis file is a `noncomputable theory` and uses classical logic throughout.\n\n## TODO\n\n* This file is currently ~1600 lines long and is quite a miscellany of definitions and lemmas,\n  so it should be divided into smaller pieces.\n\n* Expand the list of definitions and important lemmas to the module docstring.\n\n-/\n\nnoncomputable theory\n\nopen finset function\nopen_locale big_operators\n\nvariables {\u03b1 \u03b2 \u03b3 \u03b9 M M' N P G H R S : Type*}\n\nnamespace finsupp\n\n/-! ### Declarations about `graph` -/\n\nsection graph\n\nvariable [has_zero M]\n\n/-- The graph of a finitely supported function over its support, i.e. the finset of input and output\npairs with non-zero outputs. -/\ndef graph (f : \u03b1 \u2192\u2080 M) : finset (\u03b1 \u00d7 M) :=\nf.support.map \u27e8\u03bb a, prod.mk a (f a), \u03bb x y h, (prod.mk.inj h).1\u27e9\n\nlemma mk_mem_graph_iff {a : \u03b1} {m : M} {f : \u03b1 \u2192\u2080 M} : (a, m) \u2208 f.graph \u2194 f a = m \u2227 m \u2260 0 :=\nbegin\n  simp_rw [graph, mem_map, mem_support_iff],\n  split,\n  { rintro \u27e8b, ha, rfl, -\u27e9,\n    exact \u27e8rfl, ha\u27e9 },\n  { rintro \u27e8rfl, ha\u27e9,\n    exact \u27e8a, ha, rfl\u27e9 }\nend\n\n@[simp] lemma mem_graph_iff {c : \u03b1 \u00d7 M} {f : \u03b1 \u2192\u2080 M} : c \u2208 f.graph \u2194 f c.1 = c.2 \u2227 c.2 \u2260 0 :=\nby { cases c, exact mk_mem_graph_iff }\n\nlemma mk_mem_graph (f : \u03b1 \u2192\u2080 M) {a : \u03b1} (ha : a \u2208 f.support) : (a, f a) \u2208 f.graph :=\nmk_mem_graph_iff.2 \u27e8rfl, mem_support_iff.1 ha\u27e9\n\nlemma apply_eq_of_mem_graph {a : \u03b1} {m : M} {f : \u03b1 \u2192\u2080 M} (h : (a, m) \u2208 f.graph) : f a = m :=\n(mem_graph_iff.1 h).1\n\n@[simp] lemma not_mem_graph_snd_zero (a : \u03b1) (f : \u03b1 \u2192\u2080 M) : (a, (0 : M)) \u2209 f.graph :=\n\u03bb h, (mem_graph_iff.1 h).2.irrefl\n\n@[simp] lemma image_fst_graph [decidable_eq \u03b1] (f : \u03b1 \u2192\u2080 M) : f.graph.image prod.fst = f.support :=\nbegin\n  classical,\n  simp only [graph, map_eq_image, image_image, embedding.coe_fn_mk, (\u2218), image_id'],\nend\n\nlemma graph_injective (\u03b1 M) [has_zero M] : injective (@graph \u03b1 M _) :=\nbegin\n  intros f g h,\n  classical,\n  have hsup : f.support = g.support, by rw [\u2190 image_fst_graph, h, image_fst_graph],\n  refine ext_iff'.2 \u27e8hsup, \u03bb x hx, apply_eq_of_mem_graph $ h.symm \u25b8 _\u27e9,\n  exact mk_mem_graph _ (hsup \u25b8 hx)\nend\n\n@[simp] lemma graph_inj {f g : \u03b1 \u2192\u2080 M} : f.graph = g.graph \u2194 f = g :=\n(graph_injective \u03b1 M).eq_iff\n\n@[simp] lemma graph_zero : graph (0 : \u03b1 \u2192\u2080 M) = \u2205 := by simp [graph]\n\n@[simp] lemma graph_eq_empty {f : \u03b1 \u2192\u2080 M} : f.graph = \u2205 \u2194 f = 0 :=\n(graph_injective \u03b1 M).eq_iff' graph_zero\n\nend graph\n\nend finsupp\n\n/-! ### Declarations about `map_range` -/\n\nsection map_range\n\nnamespace finsupp\n\nsection equiv\n\nvariables [has_zero M] [has_zero N] [has_zero P]\n\n/-- `finsupp.map_range` as an equiv. -/\n@[simps apply]\ndef map_range.equiv (f : M \u2243 N) (hf : f 0 = 0) (hf' : f.symm 0 = 0) : (\u03b1 \u2192\u2080 M) \u2243 (\u03b1 \u2192\u2080 N) :=\n{ to_fun := (map_range f hf : (\u03b1 \u2192\u2080 M) \u2192 (\u03b1 \u2192\u2080 N)),\n  inv_fun := (map_range f.symm hf' : (\u03b1 \u2192\u2080 N) \u2192 (\u03b1 \u2192\u2080 M)),\n  left_inv := \u03bb x, begin\n    rw \u2190map_range_comp _ _ _ _; simp_rw equiv.symm_comp_self,\n    { exact map_range_id _ },\n    { refl },\n  end,\n  right_inv := \u03bb x, begin\n    rw \u2190map_range_comp _ _ _ _; simp_rw equiv.self_comp_symm,\n    { exact map_range_id _ },\n    { refl },\n  end }\n\n@[simp]\nlemma map_range.equiv_refl :\n  map_range.equiv (equiv.refl M) rfl rfl = equiv.refl (\u03b1 \u2192\u2080 M) :=\nequiv.ext map_range_id\n\nlemma map_range.equiv_trans\n  (f : M \u2243 N) (hf : f 0 = 0) (hf') (f\u2082 : N \u2243 P) (hf\u2082 : f\u2082 0 = 0) (hf\u2082') :\n  (map_range.equiv (f.trans f\u2082) (by rw [equiv.trans_apply, hf, hf\u2082])\n    (by rw [equiv.symm_trans_apply, hf\u2082', hf']) : (\u03b1 \u2192\u2080 _) \u2243 _) =\n    (map_range.equiv f hf hf').trans (map_range.equiv f\u2082 hf\u2082 hf\u2082') :=\nequiv.ext $ map_range_comp _ _ _ _ _\n\n@[simp] lemma map_range.equiv_symm (f : M \u2243 N) (hf hf') :\n  ((map_range.equiv f hf hf').symm : (\u03b1 \u2192\u2080 _) \u2243 _) = map_range.equiv f.symm hf' hf :=\nequiv.ext $ \u03bb x, rfl\n\nend equiv\n\nsection zero_hom\n\nvariables [has_zero M] [has_zero N] [has_zero P]\n\n/-- Composition with a fixed zero-preserving homomorphism is itself an zero-preserving homomorphism\non functions. -/\n@[simps]\ndef map_range.zero_hom (f : zero_hom M N) : zero_hom (\u03b1 \u2192\u2080 M) (\u03b1 \u2192\u2080 N) :=\n{ to_fun := (map_range f f.map_zero : (\u03b1 \u2192\u2080 M) \u2192 (\u03b1 \u2192\u2080 N)),\n  map_zero' := map_range_zero }\n\n@[simp]\nlemma map_range.zero_hom_id :\n  map_range.zero_hom (zero_hom.id M) = zero_hom.id (\u03b1 \u2192\u2080 M) := zero_hom.ext map_range_id\n\nlemma map_range.zero_hom_comp (f : zero_hom N P) (f\u2082 : zero_hom M N) :\n  (map_range.zero_hom (f.comp f\u2082) : zero_hom (\u03b1 \u2192\u2080 _) _) =\n    (map_range.zero_hom f).comp (map_range.zero_hom f\u2082) :=\nzero_hom.ext $ map_range_comp _ _ _ _ _\n\nend zero_hom\n\nsection add_monoid_hom\nvariables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P]\n\n/--\nComposition with a fixed additive homomorphism is itself an additive homomorphism on functions.\n-/\n@[simps]\ndef map_range.add_monoid_hom (f : M \u2192+ N) : (\u03b1 \u2192\u2080 M) \u2192+ (\u03b1 \u2192\u2080 N) :=\n{ to_fun := (map_range f f.map_zero : (\u03b1 \u2192\u2080 M) \u2192 (\u03b1 \u2192\u2080 N)),\n  map_zero' := map_range_zero,\n  map_add' := \u03bb a b, map_range_add f.map_add _ _ }\n\n@[simp]\nlemma map_range.add_monoid_hom_id :\n  map_range.add_monoid_hom (add_monoid_hom.id M) = add_monoid_hom.id (\u03b1 \u2192\u2080 M) :=\nadd_monoid_hom.ext map_range_id\n\nlemma map_range.add_monoid_hom_comp (f : N \u2192+ P) (f\u2082 : M \u2192+ N) :\n  (map_range.add_monoid_hom (f.comp f\u2082) : (\u03b1 \u2192\u2080 _) \u2192+ _) =\n    (map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f\u2082) :=\nadd_monoid_hom.ext $ map_range_comp _ _ _ _ _\n\n@[simp]\nlemma map_range.add_monoid_hom_to_zero_hom (f : M \u2192+ N) :\n  (map_range.add_monoid_hom f).to_zero_hom =\n    (map_range.zero_hom f.to_zero_hom : zero_hom (\u03b1 \u2192\u2080 _) _) :=\nzero_hom.ext $ \u03bb _, rfl\n\nlemma map_range_multiset_sum (f : M \u2192+ N) (m : multiset (\u03b1 \u2192\u2080 M)) :\n  map_range f f.map_zero m.sum = (m.map $ \u03bbx, map_range f f.map_zero x).sum :=\n(map_range.add_monoid_hom f : (\u03b1 \u2192\u2080 _) \u2192+ _).map_multiset_sum _\n\nlemma map_range_finset_sum (f : M \u2192+ N) (s : finset \u03b9) (g : \u03b9 \u2192 (\u03b1 \u2192\u2080 M))  :\n  map_range f f.map_zero (\u2211 x in s, g x) = \u2211 x in s, map_range f f.map_zero (g x) :=\n(map_range.add_monoid_hom f : (\u03b1 \u2192\u2080 _) \u2192+ _).map_sum _ _\n\n/-- `finsupp.map_range.add_monoid_hom` as an equiv. -/\n@[simps apply]\ndef map_range.add_equiv (f : M \u2243+ N) : (\u03b1 \u2192\u2080 M) \u2243+ (\u03b1 \u2192\u2080 N) :=\n{ to_fun := (map_range f f.map_zero : (\u03b1 \u2192\u2080 M) \u2192 (\u03b1 \u2192\u2080 N)),\n  inv_fun := (map_range f.symm f.symm.map_zero : (\u03b1 \u2192\u2080 N) \u2192 (\u03b1 \u2192\u2080 M)),\n  left_inv := \u03bb x, begin\n    rw \u2190map_range_comp _ _ _ _; simp_rw add_equiv.symm_comp_self,\n    { exact map_range_id _ },\n    { refl },\n  end,\n  right_inv := \u03bb x, begin\n    rw \u2190map_range_comp _ _ _ _; simp_rw add_equiv.self_comp_symm,\n    { exact map_range_id _ },\n    { refl },\n  end,\n  ..(map_range.add_monoid_hom f.to_add_monoid_hom) }\n\n@[simp]\nlemma map_range.add_equiv_refl :\n  map_range.add_equiv (add_equiv.refl M) = add_equiv.refl (\u03b1 \u2192\u2080 M) :=\nadd_equiv.ext map_range_id\n\nlemma map_range.add_equiv_trans (f : M \u2243+ N) (f\u2082 : N \u2243+ P) :\n  (map_range.add_equiv (f.trans f\u2082) : (\u03b1 \u2192\u2080 _) \u2243+ _) =\n    (map_range.add_equiv f).trans (map_range.add_equiv f\u2082) :=\nadd_equiv.ext $ map_range_comp _ _ _ _ _\n\n@[simp] lemma map_range.add_equiv_symm (f : M \u2243+ N) :\n  ((map_range.add_equiv f).symm : (\u03b1 \u2192\u2080 _) \u2243+ _) = map_range.add_equiv f.symm :=\nadd_equiv.ext $ \u03bb x, rfl\n\n@[simp]\nlemma map_range.add_equiv_to_add_monoid_hom (f : M \u2243+ N) :\n  (map_range.add_equiv f : (\u03b1 \u2192\u2080 _) \u2243+ _).to_add_monoid_hom =\n    (map_range.add_monoid_hom f.to_add_monoid_hom : (\u03b1 \u2192\u2080 _) \u2192+ _) :=\nadd_monoid_hom.ext $ \u03bb _, rfl\n\n@[simp]\nlemma map_range.add_equiv_to_equiv (f : M \u2243+ N) :\n  (map_range.add_equiv f).to_equiv =\n    (map_range.equiv f.to_equiv f.map_zero f.symm.map_zero : (\u03b1 \u2192\u2080 _) \u2243 _) :=\nequiv.ext $ \u03bb _, rfl\n\nend add_monoid_hom\n\nend finsupp\n\nend map_range\n\n/-! ### Declarations about `equiv_congr_left` -/\n\nsection equiv_congr_left\nvariable [has_zero M]\n\nnamespace finsupp\n\n/-- Given `f : \u03b1 \u2243 \u03b2`, we can map `l : \u03b1 \u2192\u2080 M` to  `equiv_map_domain f l : \u03b2 \u2192\u2080 M` (computably)\nby mapping the support forwards and the function backwards. -/\ndef equiv_map_domain (f : \u03b1 \u2243 \u03b2) (l : \u03b1 \u2192\u2080 M) : \u03b2 \u2192\u2080 M :=\n{ support := l.support.map f.to_embedding,\n  to_fun := \u03bb a, l (f.symm a),\n  mem_support_to_fun := \u03bb a, by simp only [finset.mem_map_equiv, mem_support_to_fun]; refl }\n\n@[simp] lemma equiv_map_domain_apply (f : \u03b1 \u2243 \u03b2) (l : \u03b1 \u2192\u2080 M) (b : \u03b2) :\n  equiv_map_domain f l b = l (f.symm b) := rfl\n\nlemma equiv_map_domain_symm_apply (f : \u03b1 \u2243 \u03b2) (l : \u03b2 \u2192\u2080 M) (a : \u03b1) :\n  equiv_map_domain f.symm l a = l (f a) := rfl\n\n@[simp] lemma equiv_map_domain_refl (l : \u03b1 \u2192\u2080 M) : equiv_map_domain (equiv.refl _) l = l :=\nby ext x; refl\n\nlemma equiv_map_domain_refl' : equiv_map_domain (equiv.refl _) = @id (\u03b1 \u2192\u2080 M) :=\nby ext x; refl\n\nlemma equiv_map_domain_trans (f : \u03b1 \u2243 \u03b2) (g : \u03b2 \u2243 \u03b3) (l : \u03b1 \u2192\u2080 M) :\n  equiv_map_domain (f.trans g) l = equiv_map_domain g (equiv_map_domain f l) := by ext x; refl\n\nlemma equiv_map_domain_trans' (f : \u03b1 \u2243 \u03b2) (g : \u03b2 \u2243 \u03b3) :\n  @equiv_map_domain _ _ M _ (f.trans g) = equiv_map_domain g \u2218 equiv_map_domain f := by ext x; refl\n\n@[simp] lemma equiv_map_domain_single (f : \u03b1 \u2243 \u03b2) (a : \u03b1) (b : M) :\n  equiv_map_domain f (single a b) = single (f a) b :=\nbegin\n  classical,\n  ext x,\n  simp only [single_apply, equiv.apply_eq_iff_eq_symm_apply, equiv_map_domain_apply],\nend\n\n@[simp] lemma equiv_map_domain_zero {f : \u03b1 \u2243 \u03b2} : equiv_map_domain f (0 : \u03b1 \u2192\u2080 M) = (0 : \u03b2 \u2192\u2080 M) :=\nby ext x; simp only [equiv_map_domain_apply, coe_zero, pi.zero_apply]\n\n/-- Given `f : \u03b1 \u2243 \u03b2`, the finitely supported function spaces are also in bijection:\n`(\u03b1 \u2192\u2080 M) \u2243 (\u03b2 \u2192\u2080 M)`.\n\nThis is the finitely-supported version of `equiv.Pi_congr_left`. -/\ndef equiv_congr_left (f : \u03b1 \u2243 \u03b2) : (\u03b1 \u2192\u2080 M) \u2243 (\u03b2 \u2192\u2080 M) :=\nby refine \u27e8equiv_map_domain f, equiv_map_domain f.symm, \u03bb f, _, \u03bb f, _\u27e9;\n  ext x; simp only [equiv_map_domain_apply, equiv.symm_symm,\n    equiv.symm_apply_apply, equiv.apply_symm_apply]\n\n@[simp] lemma equiv_congr_left_apply (f : \u03b1 \u2243 \u03b2) (l : \u03b1 \u2192\u2080 M) :\n  equiv_congr_left f l = equiv_map_domain f l := rfl\n\n@[simp] lemma equiv_congr_left_symm (f : \u03b1 \u2243 \u03b2) :\n  (@equiv_congr_left _ _ M _ f).symm = equiv_congr_left f.symm := rfl\n\nend finsupp\n\nend equiv_congr_left\n\n\nsection cast_finsupp\nvariables [has_zero M] (f : \u03b1 \u2192\u2080 M)\n\nnamespace nat\n\n@[simp, norm_cast] lemma cast_finsupp_prod [comm_semiring R] (g : \u03b1 \u2192 M \u2192 \u2115) :\n  (\u2191(f.prod g) : R) = f.prod (\u03bb a b, \u2191(g a b)) :=\nnat.cast_prod _ _\n\n@[simp, norm_cast] lemma cast_finsupp_sum [comm_semiring R] (g : \u03b1 \u2192 M \u2192 \u2115) :\n  (\u2191(f.sum g) : R) = f.sum (\u03bb a b, \u2191(g a b)) :=\nnat.cast_sum _ _\n\nend nat\n\nnamespace int\n\n@[simp, norm_cast] lemma cast_finsupp_prod [comm_ring R] (g : \u03b1 \u2192 M \u2192 \u2124) :\n  (\u2191(f.prod g) : R) = f.prod (\u03bb a b, \u2191(g a b)) :=\nint.cast_prod _ _\n\n@[simp, norm_cast] lemma cast_finsupp_sum [comm_ring R] (g : \u03b1 \u2192 M \u2192 \u2124) :\n  (\u2191(f.sum g) : R) = f.sum (\u03bb a b, \u2191(g a b)) :=\nint.cast_sum _ _\n\nend int\n\nnamespace rat\n\n@[simp, norm_cast] lemma cast_finsupp_sum [division_ring R] [char_zero R] (g : \u03b1 \u2192 M \u2192 \u211a) :\n  (\u2191(f.sum g) : R) = f.sum (\u03bb a b, g a b) :=\ncast_sum _ _\n\n@[simp, norm_cast] lemma cast_finsupp_prod [field R] [char_zero R] (g : \u03b1 \u2192 M \u2192 \u211a) :\n  (\u2191(f.prod g) : R) = f.prod (\u03bb a b, g a b) :=\ncast_prod _ _\n\nend rat\nend cast_finsupp\n\n\n\n\n/-! ### Declarations about `map_domain` -/\n\nnamespace finsupp\n\nsection map_domain\nvariables [add_comm_monoid M] {v v\u2081 v\u2082 : \u03b1 \u2192\u2080 M}\n\n\n/-- Given `f : \u03b1 \u2192 \u03b2` and `v : \u03b1 \u2192\u2080 M`, `map_domain f v : \u03b2 \u2192\u2080 M`\n  is the finitely supported function whose value at `a : \u03b2` is the sum\n  of `v x` over all `x` such that `f x = a`. -/\ndef map_domain (f : \u03b1 \u2192 \u03b2) (v : \u03b1 \u2192\u2080 M) : \u03b2 \u2192\u2080 M :=\nv.sum $ \u03bba, single (f a)\n\nlemma map_domain_apply {f : \u03b1 \u2192 \u03b2} (hf : function.injective f) (x : \u03b1 \u2192\u2080 M) (a : \u03b1) :\n  map_domain f x (f a) = x a :=\nbegin\n  rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same],\n  { assume b _ hba, exact single_eq_of_ne (hf.ne hba) },\n  { assume h, rw [not_mem_support_iff.1 h, single_zero, zero_apply] }\nend\n\nlemma map_domain_notin_range {f : \u03b1 \u2192 \u03b2} (x : \u03b1 \u2192\u2080 M) (a : \u03b2) (h : a \u2209 set.range f) :\n  map_domain f x a = 0 :=\nbegin\n  rw [map_domain, sum_apply, sum],\n  exact finset.sum_eq_zero\n    (assume a' h', single_eq_of_ne $ assume eq, h $ eq \u25b8 set.mem_range_self _)\nend\n\n@[simp]\nlemma map_domain_id : map_domain id v = v :=\nsum_single _\n\nlemma map_domain_comp {f : \u03b1 \u2192 \u03b2} {g : \u03b2 \u2192 \u03b3} :\n  map_domain (g \u2218 f) v = map_domain g (map_domain f v) :=\nbegin\n  refine ((sum_sum_index _ _).trans _).symm,\n  { intro, exact single_zero _ },\n  { intro, exact single_add _ },\n  refine sum_congr (\u03bb _ _, sum_single_index _),\n  { exact single_zero _ }\nend\n\n@[simp]\nlemma map_domain_single {f : \u03b1 \u2192 \u03b2} {a : \u03b1} {b : M} : map_domain f (single a b) = single (f a) b :=\nsum_single_index $ single_zero _\n\n@[simp] lemma map_domain_zero {f : \u03b1 \u2192 \u03b2} : map_domain f (0 : \u03b1 \u2192\u2080 M) = (0 : \u03b2 \u2192\u2080 M) :=\nsum_zero_index\n\nlemma map_domain_congr {f g : \u03b1 \u2192 \u03b2} (h : \u2200x\u2208v.support, f x = g x) :\n  v.map_domain f = v.map_domain g :=\nfinset.sum_congr rfl $ \u03bb _ H, by simp only [h _ H]\n\nlemma map_domain_add {f : \u03b1 \u2192 \u03b2} : map_domain f (v\u2081 + v\u2082) = map_domain f v\u2081 + map_domain f v\u2082 :=\nsum_add_index' (\u03bb _, single_zero _) (\u03bb _, single_add _)\n\n@[simp] lemma map_domain_equiv_apply {f : \u03b1 \u2243 \u03b2} (x : \u03b1 \u2192\u2080 M) (a : \u03b2) :\n  map_domain f x a = x (f.symm a) :=\nbegin\n  conv_lhs { rw \u2190f.apply_symm_apply a },\n  exact map_domain_apply f.injective _ _,\nend\n\n/-- `finsupp.map_domain` is an `add_monoid_hom`. -/\n@[simps]\ndef map_domain.add_monoid_hom (f : \u03b1 \u2192 \u03b2) : (\u03b1 \u2192\u2080 M) \u2192+ (\u03b2 \u2192\u2080 M) :=\n{ to_fun := map_domain f,\n  map_zero' := map_domain_zero,\n  map_add' := \u03bb _ _, map_domain_add}\n\n@[simp]\nlemma map_domain.add_monoid_hom_id : map_domain.add_monoid_hom id = add_monoid_hom.id (\u03b1 \u2192\u2080 M) :=\nadd_monoid_hom.ext $ \u03bb _, map_domain_id\n\nlemma map_domain.add_monoid_hom_comp (f : \u03b2 \u2192 \u03b3) (g : \u03b1 \u2192 \u03b2) :\n  (map_domain.add_monoid_hom (f \u2218 g) : (\u03b1 \u2192\u2080 M) \u2192+ (\u03b3 \u2192\u2080 M)) =\n    (map_domain.add_monoid_hom f).comp (map_domain.add_monoid_hom g) :=\nadd_monoid_hom.ext $ \u03bb _, map_domain_comp\n\nlemma map_domain_finset_sum {f : \u03b1 \u2192 \u03b2} {s : finset \u03b9} {v : \u03b9 \u2192 \u03b1 \u2192\u2080 M} :\n  map_domain f (\u2211 i in s, v i) = \u2211 i in s, map_domain f (v i) :=\n(map_domain.add_monoid_hom f : (\u03b1 \u2192\u2080 M) \u2192+ \u03b2 \u2192\u2080 M).map_sum _ _\n\nlemma map_domain_sum [has_zero N] {f : \u03b1 \u2192 \u03b2} {s : \u03b1 \u2192\u2080 N} {v : \u03b1 \u2192 N \u2192 \u03b1 \u2192\u2080 M} :\n  map_domain f (s.sum v) = s.sum (\u03bba b, map_domain f (v a b)) :=\n(map_domain.add_monoid_hom f : (\u03b1 \u2192\u2080 M) \u2192+ \u03b2 \u2192\u2080 M).map_finsupp_sum _ _\n\nlemma map_domain_support [decidable_eq \u03b2] {f : \u03b1 \u2192 \u03b2} {s : \u03b1 \u2192\u2080 M} :\n  (s.map_domain f).support \u2286 s.support.image f :=\nfinset.subset.trans support_sum $\n  finset.subset.trans (finset.bUnion_mono $ assume a ha, support_single_subset) $\n  by rw [finset.bUnion_singleton]; exact subset.refl _\n\nlemma map_domain_apply' (S : set \u03b1) {f : \u03b1 \u2192 \u03b2} (x : \u03b1 \u2192\u2080 M)\n  (hS : (x.support : set \u03b1) \u2286 S) (hf : set.inj_on f S) {a : \u03b1} (ha : a \u2208 S) :\n  map_domain f x (f a) = x a :=\nbegin\n  classical,\n  rw [map_domain, sum_apply, sum],\n  simp_rw single_apply,\n  by_cases hax : a \u2208 x.support,\n  { rw [\u2190 finset.add_sum_erase _ _ hax, if_pos rfl],\n    convert add_zero _,\n    refine finset.sum_eq_zero (\u03bb i hi, if_neg _),\n    exact (hf.mono hS).ne (finset.mem_of_mem_erase hi) hax (finset.ne_of_mem_erase hi), },\n  { rw not_mem_support_iff.1 hax,\n    refine finset.sum_eq_zero (\u03bb i hi, if_neg _),\n    exact hf.ne (hS hi) ha (ne_of_mem_of_not_mem hi hax) }\nend\n\nlemma map_domain_support_of_inj_on [decidable_eq \u03b2] {f : \u03b1 \u2192 \u03b2} (s : \u03b1 \u2192\u2080 M)\n  (hf : set.inj_on f s.support) : (map_domain f s).support = finset.image f s.support :=\nfinset.subset.antisymm map_domain_support $ begin\n  intros x hx,\n  simp only [mem_image, exists_prop, mem_support_iff, ne.def] at hx,\n  rcases hx with \u27e8hx_w, hx_h_left, rfl\u27e9,\n  simp only [mem_support_iff, ne.def],\n  rw map_domain_apply' (\u2191s.support : set _) _ _ hf,\n  { exact hx_h_left, },\n  { simp only [mem_coe, mem_support_iff, ne.def],\n    exact hx_h_left, },\n  { exact subset.refl _, },\nend\n\nlemma map_domain_support_of_injective [decidable_eq \u03b2] {f : \u03b1 \u2192 \u03b2} (hf : function.injective f)\n  (s : \u03b1 \u2192\u2080 M) : (map_domain f s).support = finset.image f s.support :=\nmap_domain_support_of_inj_on s (hf.inj_on _)\n\n@[to_additive]\nlemma prod_map_domain_index [comm_monoid N] {f : \u03b1 \u2192 \u03b2} {s : \u03b1 \u2192\u2080 M}\n  {h : \u03b2 \u2192 M \u2192 N} (h_zero : \u2200b, h b 0 = 1) (h_add : \u2200b m\u2081 m\u2082, h b (m\u2081 + m\u2082) = h b m\u2081 * h b m\u2082) :\n  (map_domain f s).prod h = s.prod (\u03bba m, h (f a) m) :=\n(prod_sum_index h_zero h_add).trans $ prod_congr $ \u03bb _ _, prod_single_index (h_zero _)\n\n/--\nA version of `sum_map_domain_index` that takes a bundled `add_monoid_hom`,\nrather than separate linearity hypotheses.\n-/\n-- Note that in `prod_map_domain_index`, `M` is still an additive monoid,\n-- so there is no analogous version in terms of `monoid_hom`.\n@[simp]\nlemma sum_map_domain_index_add_monoid_hom [add_comm_monoid N] {f : \u03b1 \u2192 \u03b2}\n  {s : \u03b1 \u2192\u2080 M} (h : \u03b2 \u2192 M \u2192+ N) :\n  (map_domain f s).sum (\u03bb b m, h b m) = s.sum (\u03bb a m, h (f a) m) :=\n@sum_map_domain_index _ _ _ _ _ _ _ _\n  (\u03bb b m, h b m)\n  (\u03bb b, (h b).map_zero)\n  (\u03bb b m\u2081 m\u2082, (h b).map_add _ _)\n\nlemma emb_domain_eq_map_domain (f : \u03b1 \u21aa \u03b2) (v : \u03b1 \u2192\u2080 M) :\n  emb_domain f v = map_domain f v :=\nbegin\n  ext a,\n  by_cases a \u2208 set.range f,\n  { rcases h with \u27e8a, rfl\u27e9,\n    rw [map_domain_apply f.injective, emb_domain_apply] },\n  { rw [map_domain_notin_range, emb_domain_notin_range]; assumption }\nend\n\n@[to_additive]\nlemma prod_map_domain_index_inj [comm_monoid N] {f : \u03b1 \u2192 \u03b2} {s : \u03b1 \u2192\u2080 M}\n  {h : \u03b2 \u2192 M \u2192 N} (hf : function.injective f) :\n  (s.map_domain f).prod h = s.prod (\u03bba b, h (f a) b) :=\nby rw [\u2190function.embedding.coe_fn_mk f hf, \u2190emb_domain_eq_map_domain, prod_emb_domain]\n\nlemma map_domain_injective {f : \u03b1 \u2192 \u03b2} (hf : function.injective f) :\n  function.injective (map_domain f : (\u03b1 \u2192\u2080 M) \u2192 (\u03b2 \u2192\u2080 M)) :=\nbegin\n  assume v\u2081 v\u2082 eq, ext a,\n  have : map_domain f v\u2081 (f a) = map_domain f v\u2082 (f a), { rw eq },\n  rwa [map_domain_apply hf, map_domain_apply hf] at this,\nend\n\n/-- When `f` is an embedding we have an embedding `(\u03b1 \u2192\u2080 \u2115)  \u21aa (\u03b2 \u2192\u2080 \u2115)` given by `map_domain`. -/\n@[simps] def map_domain_embedding {\u03b1 \u03b2 : Type*} (f : \u03b1 \u21aa \u03b2) : (\u03b1 \u2192\u2080 \u2115) \u21aa \u03b2 \u2192\u2080 \u2115 :=\n\u27e8finsupp.map_domain f, finsupp.map_domain_injective f.injective\u27e9\n\nlemma map_domain.add_monoid_hom_comp_map_range [add_comm_monoid N] (f : \u03b1 \u2192 \u03b2) (g : M \u2192+ N) :\n  (map_domain.add_monoid_hom f).comp (map_range.add_monoid_hom g) =\n    (map_range.add_monoid_hom g).comp (map_domain.add_monoid_hom f) :=\nby { ext, simp }\n\n/-- When `g` preserves addition, `map_range` and `map_domain` commute. -/\nlemma map_domain_map_range [add_comm_monoid N] (f : \u03b1 \u2192 \u03b2) (v : \u03b1 \u2192\u2080 M) (g : M \u2192 N)\n  (h0 : g 0 = 0) (hadd : \u2200 x y, g (x + y) = g x + g y) :\n  map_domain f (map_range g h0 v) = map_range g h0 (map_domain f v) :=\nlet g' : M \u2192+ N := { to_fun := g, map_zero' := h0, map_add' := hadd} in\nadd_monoid_hom.congr_fun (map_domain.add_monoid_hom_comp_map_range f g') v\n\nlemma sum_update_add [add_comm_monoid \u03b1] [add_comm_monoid \u03b2]\n  (f : \u03b9 \u2192\u2080 \u03b1) (i : \u03b9) (a : \u03b1) (g : \u03b9 \u2192 \u03b1 \u2192 \u03b2) (hg : \u2200 i, g i 0 = 0)\n  (hgg : \u2200 (j : \u03b9) (a\u2081 a\u2082 : \u03b1), g j (a\u2081 + a\u2082) = g j a\u2081 + g j a\u2082) :\n  (f.update i a).sum g + g i (f i) = f.sum g + g i a :=\nbegin\n  rw [update_eq_erase_add_single, sum_add_index' hg hgg],\n  conv_rhs { rw \u2190 finsupp.update_self f i },\n  rw [update_eq_erase_add_single, sum_add_index' hg hgg, add_assoc, add_assoc],\n  congr' 1,\n  rw [add_comm, sum_single_index (hg _), sum_single_index (hg _)],\nend\n\nlemma map_domain_inj_on (S : set \u03b1) {f : \u03b1 \u2192 \u03b2}\n  (hf : set.inj_on f S) :\n  set.inj_on (map_domain f : (\u03b1 \u2192\u2080 M) \u2192 (\u03b2 \u2192\u2080 M)) {w | (w.support : set \u03b1) \u2286 S} :=\nbegin\n  intros v\u2081 hv\u2081 v\u2082 hv\u2082 eq,\n  ext a,\n  classical,\n  by_cases h : a \u2208 v\u2081.support \u222a v\u2082.support,\n  { rw [\u2190 map_domain_apply' S _ hv\u2081 hf _, \u2190 map_domain_apply' S _ hv\u2082 hf _, eq];\n    { apply set.union_subset hv\u2081 hv\u2082,\n      exact_mod_cast h, }, },\n  { simp only [decidable.not_or_iff_and_not, mem_union, not_not, mem_support_iff] at h,\n    simp [h], },\nend\n\nlemma equiv_map_domain_eq_map_domain {M} [add_comm_monoid M] (f : \u03b1 \u2243 \u03b2) (l : \u03b1 \u2192\u2080 M) :\n  equiv_map_domain f l = map_domain f l := by ext x; simp [map_domain_equiv_apply]\n\nend map_domain\n\n\n/-! ### Declarations about `comap_domain` -/\n\nsection comap_domain\n\n/-- Given `f : \u03b1 \u2192 \u03b2`, `l : \u03b2 \u2192\u2080 M` and a proof `hf` that `f` is injective on\nthe preimage of `l.support`, `comap_domain f l hf` is the finitely supported function\nfrom `\u03b1` to `M` given by composing `l` with `f`. -/\n@[simps support]\ndef comap_domain [has_zero M] (f : \u03b1 \u2192 \u03b2) (l : \u03b2 \u2192\u2080 M) (hf : set.inj_on f (f \u207b\u00b9' \u2191l.support)) :\n  \u03b1 \u2192\u2080 M :=\n{ support := l.support.preimage f hf,\n  to_fun := (\u03bb a, l (f a)),\n  mem_support_to_fun :=\n    begin\n      intros a,\n      simp only [finset.mem_def.symm, finset.mem_preimage],\n      exact l.mem_support_to_fun (f a),\n    end }\n\n@[simp]\nlemma comap_domain_apply [has_zero M] (f : \u03b1 \u2192 \u03b2) (l : \u03b2 \u2192\u2080 M)\n  (hf : set.inj_on f (f \u207b\u00b9' \u2191l.support)) (a : \u03b1) :\n  comap_domain f l hf a = l (f a) :=\nrfl\n\nlemma sum_comap_domain [has_zero M] [add_comm_monoid N]\n  (f : \u03b1 \u2192 \u03b2) (l : \u03b2 \u2192\u2080 M) (g : \u03b2 \u2192 M \u2192 N)\n  (hf : set.bij_on f (f \u207b\u00b9' \u2191l.support) \u2191l.support) :\n  (comap_domain f l hf.inj_on).sum (g \u2218 f) = l.sum g :=\nbegin\n  simp only [sum, comap_domain_apply, (\u2218)],\n  simp [comap_domain, finset.sum_preimage_of_bij f _ _ (\u03bb x, g x (l x))],\nend\n\nlemma eq_zero_of_comap_domain_eq_zero [add_comm_monoid M]\n  (f : \u03b1 \u2192 \u03b2) (l : \u03b2 \u2192\u2080 M) (hf : set.bij_on f (f \u207b\u00b9' \u2191l.support) \u2191l.support) :\n   comap_domain f l hf.inj_on = 0 \u2192 l = 0 :=\nbegin\n  rw [\u2190 support_eq_empty, \u2190 support_eq_empty, comap_domain],\n  simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage],\n  assume h a ha,\n  cases hf.2.2 ha with b hb,\n  exact h b (hb.2.symm \u25b8 ha)\nend\n\nsection f_injective\n\nsection has_zero\nvariables [has_zero M]\n\n/-- Note the `hif` argument is needed for this to work in `rw`. -/\n@[simp] lemma comap_domain_zero (f : \u03b1 \u2192 \u03b2)\n  (hif : set.inj_on f (f \u207b\u00b9' \u2191((0 : \u03b2 \u2192\u2080 M).support)) := set.inj_on_empty _) :\n  comap_domain f (0 : \u03b2 \u2192\u2080 M) hif = (0 : \u03b1 \u2192\u2080 M) :=\nby { ext, refl }\n\n@[simp] lemma comap_domain_single (f : \u03b1 \u2192 \u03b2) (a : \u03b1) (m : M)\n  (hif : set.inj_on f (f \u207b\u00b9' (single (f a) m).support)) :\n  comap_domain f (finsupp.single (f a) m) hif = finsupp.single a m :=\nbegin\n  rcases eq_or_ne m 0 with rfl | hm,\n  { simp only [single_zero, comap_domain_zero] },\n  { rw [eq_single_iff, comap_domain_apply, comap_domain_support, \u2190 finset.coe_subset, coe_preimage,\n      support_single_ne_zero _ hm, coe_singleton, coe_singleton, single_eq_same],\n    rw [support_single_ne_zero _ hm, coe_singleton] at hif,\n    exact \u27e8\u03bb x hx, hif hx rfl hx, rfl\u27e9 }\nend\n\nend has_zero\n\nsection add_zero_class\nvariables [add_zero_class M] {f : \u03b1 \u2192 \u03b2}\n\nlemma comap_domain_add (v\u2081 v\u2082 : \u03b2 \u2192\u2080 M)\n  (hv\u2081 : set.inj_on f (f \u207b\u00b9' \u2191(v\u2081.support))) (hv\u2082 : set.inj_on f (f \u207b\u00b9' \u2191(v\u2082.support)))\n  (hv\u2081\u2082 : set.inj_on f (f \u207b\u00b9' \u2191((v\u2081 + v\u2082).support))) :\n  comap_domain f (v\u2081 + v\u2082) hv\u2081\u2082 = comap_domain f v\u2081 hv\u2081 + comap_domain f v\u2082 hv\u2082 :=\nby { ext, simp only [comap_domain_apply, coe_add, pi.add_apply] }\n\n/-- A version of `finsupp.comap_domain_add` that's easier to use. -/\nlemma comap_domain_add_of_injective (hf : function.injective f) (v\u2081 v\u2082 : \u03b2 \u2192\u2080 M) :\n  comap_domain f (v\u2081 + v\u2082) (hf.inj_on _)\n    = comap_domain f v\u2081 (hf.inj_on _) + comap_domain f v\u2082 (hf.inj_on _) :=\ncomap_domain_add _ _ _ _ _\n\n/-- `finsupp.comap_domain` is an `add_monoid_hom`. -/\n@[simps]\ndef comap_domain.add_monoid_hom (hf : function.injective f) : (\u03b2 \u2192\u2080 M) \u2192+ (\u03b1 \u2192\u2080 M) :=\n{ to_fun := \u03bb x, comap_domain f x (hf.inj_on _),\n  map_zero' := comap_domain_zero f,\n  map_add' := comap_domain_add_of_injective hf }\n\nend add_zero_class\n\nvariables [add_comm_monoid M] (f : \u03b1 \u2192 \u03b2)\n\nlemma map_domain_comap_domain\n  (hf : function.injective f) (l : \u03b2 \u2192\u2080 M) (hl : \u2191l.support \u2286 set.range f) :\n  map_domain f (comap_domain f l (hf.inj_on _)) = l :=\nbegin\n  ext a,\n  by_cases h_cases: a \u2208 set.range f,\n  { rcases set.mem_range.1 h_cases with \u27e8b, hb\u27e9,\n    rw [hb.symm, map_domain_apply hf, comap_domain_apply] },\n  { rw map_domain_notin_range _ _ h_cases,\n    by_contra h_contr,\n    apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ \u03bb h, h_contr h.symm) }\nend\n\nend f_injective\n\nend comap_domain\n\n\n/-! ### Declarations about finitely supported functions whose support is an `option` type -/\n\nsection option\n\n/-- Restrict a finitely supported function on `option \u03b1` to a finitely supported function on `\u03b1`. -/\ndef some [has_zero M] (f : option \u03b1 \u2192\u2080 M) : \u03b1 \u2192\u2080 M :=\nf.comap_domain option.some (\u03bb _, by simp)\n\n@[simp] lemma some_apply [has_zero M] (f : option \u03b1 \u2192\u2080 M) (a : \u03b1) :\n  f.some a = f (option.some a) := rfl\n\n@[simp] lemma some_zero [has_zero M] : (0 : option \u03b1 \u2192\u2080 M).some = 0 :=\nby { ext, simp, }\n\n@[simp] lemma some_add [add_comm_monoid M] (f g : option \u03b1 \u2192\u2080 M) : (f + g).some = f.some + g.some :=\nby { ext, simp, }\n\n@[simp] lemma some_single_none [has_zero M] (m : M) : (single none m : option \u03b1 \u2192\u2080 M).some = 0 :=\nby { ext, simp, }\n\n@[simp] lemma some_single_some [has_zero M] (a : \u03b1) (m : M) :\n  (single (option.some a) m : option \u03b1 \u2192\u2080 M).some = single a m :=\nby { classical, ext b, simp [single_apply], }\n\n@[to_additive]\nlemma prod_option_index [add_comm_monoid M] [comm_monoid N]\n  (f : option \u03b1 \u2192\u2080 M) (b : option \u03b1 \u2192 M \u2192 N) (h_zero : \u2200 o, b o 0 = 1)\n  (h_add : \u2200 o m\u2081 m\u2082, b o (m\u2081 + m\u2082) = b o m\u2081 * b o m\u2082) :\n  f.prod b = b none (f none) * f.some.prod (\u03bb a, b (option.some a)) :=\nbegin\n  classical,\n  apply induction_linear f,\n  { simp [some_zero, h_zero], },\n  { intros f\u2081 f\u2082 h\u2081 h\u2082,\n    rw [finsupp.prod_add_index, h\u2081, h\u2082, some_add, finsupp.prod_add_index],\n    simp only [h_add, pi.add_apply, finsupp.coe_add],\n    rw mul_mul_mul_comm,\n    all_goals { simp [h_zero, h_add], }, },\n  { rintros (_|a) m; simp [h_zero, h_add], }\nend\n\nlemma sum_option_index_smul [semiring R] [add_comm_monoid M] [module R M]\n  (f : option \u03b1 \u2192\u2080 R) (b : option \u03b1 \u2192 M) :\n  f.sum (\u03bb o r, r \u2022 b o) =\n    f none \u2022 b none + f.some.sum (\u03bb a r, r \u2022 b (option.some a)) :=\nf.sum_option_index _ (\u03bb _, zero_smul _ _) (\u03bb _ _ _, add_smul _ _ _)\n\nend option\n\n/-! ### Declarations about `filter` -/\n\nsection filter\nsection has_zero\nvariables [has_zero M] (p : \u03b1 \u2192 Prop) (f : \u03b1 \u2192\u2080 M)\n\n/--\n`filter p f` is the finitely supported function that is `f a` if `p a` is true and 0 otherwise. -/\ndef filter (p : \u03b1 \u2192 Prop) (f : \u03b1 \u2192\u2080 M) : \u03b1 \u2192\u2080 M :=\n{ to_fun := \u03bb a, by haveI := classical.dec_pred p; exact if p a then f a else 0,\n  support := by haveI := classical.dec_pred p; exact f.support.filter (\u03bb a, p a),\n  mem_support_to_fun := \u03bb a, by split_ifs; { simp only [h, mem_filter, mem_support_iff], tauto } }\n\nlemma filter_apply (a : \u03b1) [D : decidable (p a)] : f.filter p a = if p a then f a else 0 :=\nby rw subsingleton.elim D; refl\n\nlemma filter_eq_indicator : \u21d1(f.filter p) = set.indicator {x | p x} f := rfl\n\nlemma filter_eq_zero_iff : f.filter p = 0 \u2194 \u2200 x, p x \u2192 f x = 0 :=\nby simp only [fun_like.ext_iff, filter_eq_indicator, zero_apply, set.indicator_apply_eq_zero,\n  set.mem_set_of_eq]\n\nlemma filter_eq_self_iff : f.filter p = f \u2194 \u2200 x, f x \u2260 0 \u2192 p x :=\nby simp only [fun_like.ext_iff, filter_eq_indicator, set.indicator_apply_eq_self, set.mem_set_of_eq,\n  not_imp_comm]\n\n@[simp] lemma filter_apply_pos {a : \u03b1} (h : p a) : f.filter p a = f a :=\nby { classical, convert if_pos h }\n\n@[simp] lemma filter_apply_neg {a : \u03b1} (h : \u00ac p a) : f.filter p a = 0 :=\nby { classical, convert if_neg h }\n\n@[simp] lemma support_filter [D : decidable_pred p] : (f.filter p).support = f.support.filter p :=\nby rw subsingleton.elim D; refl\n\nlemma filter_zero : (0 : \u03b1 \u2192\u2080 M).filter p = 0 :=\nby { classical, rw [\u2190 support_eq_empty, support_filter, support_zero, finset.filter_empty] }\n\n@[simp] lemma filter_single_of_pos {a : \u03b1} {b : M} (h : p a) :\n  (single a b).filter p = single a b :=\n(filter_eq_self_iff _ _).2 $ \u03bb x hx, (single_apply_ne_zero.1 hx).1.symm \u25b8 h\n\n@[simp] lemma filter_single_of_neg {a : \u03b1} {b : M} (h : \u00ac p a) : (single a b).filter p = 0 :=\n(filter_eq_zero_iff _ _).2 $ \u03bb x hpx, single_apply_eq_zero.2 $ \u03bb hxa, absurd hpx (hxa.symm \u25b8 h)\n\n@[to_additive] lemma prod_filter_index [comm_monoid N] (g : \u03b1 \u2192 M \u2192 N) :\n  (f.filter p).prod g = \u220f x in (f.filter p).support, g x (f x) :=\nbegin\n  classical,\n  refine finset.prod_congr rfl (\u03bb x hx, _),\n  rw [support_filter, finset.mem_filter] at hx,\n  rw [filter_apply_pos _ _ hx.2]\nend\n\n@[simp, to_additive] lemma prod_filter_mul_prod_filter_not [comm_monoid N] (g : \u03b1 \u2192 M \u2192 N) :\n  (f.filter p).prod g * (f.filter (\u03bb a, \u00ac p a)).prod g = f.prod g :=\nbegin\n  classical,\n  simp_rw [prod_filter_index, support_filter, prod_filter_mul_prod_filter_not, finsupp.prod]\nend\n\n@[simp, to_additive] lemma prod_div_prod_filter [comm_group G] (g : \u03b1 \u2192 M \u2192 G) :\n  f.prod g / (f.filter p).prod g = (f.filter (\u03bb a, \u00acp a)).prod g :=\ndiv_eq_of_eq_mul' (prod_filter_mul_prod_filter_not _ _ _).symm\n\nend has_zero\n\nlemma filter_pos_add_filter_neg [add_zero_class M] (f : \u03b1 \u2192\u2080 M) (p : \u03b1 \u2192 Prop) :\n  f.filter p + f.filter (\u03bba, \u00ac p a) = f :=\ncoe_fn_injective $ set.indicator_self_add_compl {x | p x} f\n\nend filter\n\n/-! ### Declarations about `frange` -/\n\nsection frange\nvariables [has_zero M]\n\n/-- `frange f` is the image of `f` on the support of `f`. -/\ndef frange (f : \u03b1 \u2192\u2080 M) : finset M :=\nby haveI := classical.dec_eq M; exact finset.image f f.support\n\ntheorem mem_frange {f : \u03b1 \u2192\u2080 M} {y : M} :\n  y \u2208 f.frange \u2194 y \u2260 0 \u2227 \u2203 x, f x = y :=\nby classical; exact finset.mem_image.trans\n\u27e8\u03bb \u27e8x, hx1, hx2\u27e9, \u27e8hx2 \u25b8 mem_support_iff.1 hx1, x, hx2\u27e9,\n\u03bb \u27e8hy, x, hx\u27e9, \u27e8x, mem_support_iff.2 (hx.symm \u25b8 hy), hx\u27e9\u27e9\n\ntheorem zero_not_mem_frange {f : \u03b1 \u2192\u2080 M} : (0:M) \u2209 f.frange :=\n\u03bb H, (mem_frange.1 H).1 rfl\n\ntheorem frange_single {x : \u03b1} {y : M} : frange (single x y) \u2286 {y} :=\n\u03bb r hr, let \u27e8t, ht1, ht2\u27e9 := mem_frange.1 hr in ht2 \u25b8 begin\n  classical,\n  rw single_apply at ht2 \u22a2,\n  split_ifs at ht2 \u22a2,\n  { exact finset.mem_singleton_self _ },\n  { exact (t ht2.symm).elim }\nend\n\nend frange\n\n/-! ### Declarations about `subtype_domain` -/\n\nsection subtype_domain\n\nsection zero\n\nvariables [has_zero M] {p : \u03b1 \u2192 Prop}\n\n/--\n`subtype_domain p f` is the restriction of the finitely supported function `f` to subtype `p`. -/\ndef subtype_domain (p : \u03b1 \u2192 Prop) (f : \u03b1 \u2192\u2080 M) : (subtype p \u2192\u2080 M) :=\n{ support := by haveI := classical.dec_pred p; exact f.support.subtype p,\n  to_fun := f \u2218 coe,\n  mem_support_to_fun := \u03bb a, by simp only [mem_subtype, mem_support_iff] }\n\n@[simp] lemma support_subtype_domain [D : decidable_pred p] {f : \u03b1 \u2192\u2080 M} :\n  (subtype_domain p f).support = f.support.subtype p :=\nby rw subsingleton.elim D; refl\n\n@[simp] lemma subtype_domain_apply {a : subtype p} {v : \u03b1 \u2192\u2080 M} :\n  (subtype_domain p v) a = v (a.val) :=\nrfl\n\n@[simp] lemma subtype_domain_zero : subtype_domain p (0 : \u03b1 \u2192\u2080 M) = 0 :=\nrfl\n\nlemma subtype_domain_eq_zero_iff' {f : \u03b1 \u2192\u2080 M} :\n  f.subtype_domain p = 0 \u2194 \u2200 x, p x \u2192 f x = 0 :=\nbegin\n  classical,\n  simp_rw [\u2190 support_eq_empty, support_subtype_domain, subtype_eq_empty, not_mem_support_iff]\nend\n\nlemma subtype_domain_eq_zero_iff {f : \u03b1 \u2192\u2080 M} (hf : \u2200 x \u2208 f.support , p x) :\n  f.subtype_domain p = 0 \u2194 f = 0 :=\nsubtype_domain_eq_zero_iff'.trans \u27e8\u03bb H, ext $ \u03bb x,\n  by classical; exact\n    if hx : p x then H x hx else not_mem_support_iff.1 $ mt (hf x) hx, \u03bb H x _, by simp [H]\u27e9\n\n@[to_additive]\nlemma prod_subtype_domain_index [comm_monoid N] {v : \u03b1 \u2192\u2080 M}\n  {h : \u03b1 \u2192 M \u2192 N} (hp : \u2200x\u2208v.support, p x) :\n  (v.subtype_domain p).prod (\u03bba b, h a b) = v.prod h :=\nprod_bij (\u03bbp _, p.val)\n  (\u03bb _, by classical; exact mem_subtype.1)\n  (\u03bb _ _, rfl)\n  (\u03bb _ _ _ _, subtype.eq)\n  (\u03bb b hb, \u27e8\u27e8b, hp b hb\u27e9, by classical; exact mem_subtype.2 hb, rfl\u27e9)\n\nend zero\n\nsection add_zero_class\nvariables [add_zero_class M] {p : \u03b1 \u2192 Prop} {v v' : \u03b1 \u2192\u2080 M}\n\n@[simp] lemma subtype_domain_add {v v' : \u03b1 \u2192\u2080 M} :\n  (v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p :=\next $ \u03bb _, rfl\n\n/-- `subtype_domain` but as an `add_monoid_hom`. -/\ndef subtype_domain_add_monoid_hom : (\u03b1 \u2192\u2080 M) \u2192+ subtype p \u2192\u2080 M :=\n{ to_fun := subtype_domain p,\n  map_zero' := subtype_domain_zero,\n  map_add' := \u03bb _ _, subtype_domain_add }\n\n/-- `finsupp.filter` as an `add_monoid_hom`. -/\ndef filter_add_hom (p : \u03b1 \u2192 Prop) : (\u03b1 \u2192\u2080 M) \u2192+ (\u03b1 \u2192\u2080 M) :=\n{ to_fun := filter p,\n  map_zero' := filter_zero p,\n  map_add' := \u03bb f g, coe_fn_injective $ set.indicator_add {x | p x} f g }\n\n@[simp] lemma filter_add {v v' : \u03b1 \u2192\u2080 M} : (v + v').filter p = v.filter p + v'.filter p :=\n(filter_add_hom p).map_add v v'\n\nend add_zero_class\n\nsection comm_monoid\nvariables [add_comm_monoid M] {p : \u03b1 \u2192 Prop}\n\nlemma subtype_domain_sum {s : finset \u03b9} {h : \u03b9 \u2192 \u03b1 \u2192\u2080 M} :\n  (\u2211 c in s, h c).subtype_domain p = \u2211 c in s, (h c).subtype_domain p :=\n(subtype_domain_add_monoid_hom : _ \u2192+ subtype p \u2192\u2080 M).map_sum _ s\n\nlemma subtype_domain_finsupp_sum [has_zero N] {s : \u03b2 \u2192\u2080 N} {h : \u03b2 \u2192 N \u2192 \u03b1 \u2192\u2080 M} :\n  (s.sum h).subtype_domain p = s.sum (\u03bbc d, (h c d).subtype_domain p) :=\nsubtype_domain_sum\n\nlemma filter_sum (s : finset \u03b9) (f : \u03b9 \u2192 \u03b1 \u2192\u2080 M) :\n  (\u2211 a in s, f a).filter p = \u2211 a in s, filter p (f a) :=\n(filter_add_hom p : (\u03b1 \u2192\u2080 M) \u2192+ _).map_sum f s\n\nlemma filter_eq_sum (p : \u03b1 \u2192 Prop) [D : decidable_pred p] (f : \u03b1 \u2192\u2080 M) :\n  f.filter p = \u2211 i in f.support.filter p, single i (f i) :=\n(f.filter p).sum_single.symm.trans $ finset.sum_congr (by rw subsingleton.elim D; refl) $\n  \u03bb x hx, by rw [filter_apply_pos _ _ (mem_filter.1 hx).2]\n\nend comm_monoid\n\nsection group\nvariables [add_group G] {p : \u03b1 \u2192 Prop} {v v' : \u03b1 \u2192\u2080 G}\n\n@[simp] lemma subtype_domain_neg : (- v).subtype_domain p = - v.subtype_domain p :=\next $ \u03bb _, rfl\n\n@[simp] lemma subtype_domain_sub :\n  (v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p :=\next $ \u03bb _, rfl\n\n@[simp] lemma single_neg (a : \u03b1) (b : G) : single a (-b) = -single a b :=\n(single_add_hom a : G \u2192+ _).map_neg b\n\n@[simp] lemma single_sub (a : \u03b1) (b\u2081 b\u2082 : G) : single a (b\u2081 - b\u2082) = single a b\u2081 - single a b\u2082 :=\n(single_add_hom a : G \u2192+ _).map_sub b\u2081 b\u2082\n\n@[simp] lemma erase_neg (a : \u03b1) (f : \u03b1 \u2192\u2080 G) : erase a (-f) = -erase a f :=\n(erase_add_hom a : (_ \u2192\u2080 G) \u2192+ _).map_neg f\n\n@[simp] lemma erase_sub (a : \u03b1) (f\u2081 f\u2082 : \u03b1 \u2192\u2080 G) : erase a (f\u2081 - f\u2082) = erase a f\u2081 - erase a f\u2082 :=\n(erase_add_hom a : (_ \u2192\u2080 G) \u2192+ _).map_sub f\u2081 f\u2082\n\n@[simp] lemma filter_neg (p : \u03b1 \u2192 Prop) (f : \u03b1 \u2192\u2080 G) : filter p (-f) = -filter p f :=\n(filter_add_hom p : (_ \u2192\u2080 G) \u2192+ _).map_neg f\n\n@[simp] lemma filter_sub (p : \u03b1 \u2192 Prop) (f\u2081 f\u2082 : \u03b1 \u2192\u2080 G) :\n  filter p (f\u2081 - f\u2082) = filter p f\u2081 - filter p f\u2082 :=\n(filter_add_hom p : (_ \u2192\u2080 G) \u2192+ _).map_sub f\u2081 f\u2082\n\nend group\n\nend subtype_domain\n\nlemma mem_support_multiset_sum [add_comm_monoid M]\n  {s : multiset (\u03b1 \u2192\u2080 M)} (a : \u03b1) :\n  a \u2208 s.sum.support \u2192 \u2203f\u2208s, a \u2208 (f : \u03b1 \u2192\u2080 M).support :=\nmultiset.induction_on s false.elim\n  begin\n    assume f s ih ha,\n    by_cases a \u2208 f.support,\n    { exact \u27e8f, multiset.mem_cons_self _ _, h\u27e9 },\n    { simp only [multiset.sum_cons, mem_support_iff, add_apply,\n        not_mem_support_iff.1 h, zero_add] at ha,\n      rcases ih (mem_support_iff.2 ha) with \u27e8f', h\u2080, h\u2081\u27e9,\n      exact \u27e8f', multiset.mem_cons_of_mem h\u2080, h\u2081\u27e9 }\n  end\n\nlemma mem_support_finset_sum [add_comm_monoid M]\n  {s : finset \u03b9} {h : \u03b9 \u2192 \u03b1 \u2192\u2080 M} (a : \u03b1) (ha : a \u2208 (\u2211 c in s, h c).support) :\n  \u2203 c \u2208 s, a \u2208 (h c).support :=\nlet \u27e8f, hf, hfa\u27e9 := mem_support_multiset_sum a ha in\nlet \u27e8c, hc, eq\u27e9 := multiset.mem_map.1 hf in\n\u27e8c, hc, eq.symm \u25b8 hfa\u27e9\n\n\n/-! ### Declarations about `curry` and `uncurry` -/\n\nsection curry_uncurry\n\nvariables [add_comm_monoid M] [add_comm_monoid N]\n\n/-- Given a finitely supported function `f` from a product type `\u03b1 \u00d7 \u03b2` to `\u03b3`,\n`curry f` is the \"curried\" finitely supported function from `\u03b1` to the type of\nfinitely supported functions from `\u03b2` to `\u03b3`. -/\nprotected def curry (f : (\u03b1 \u00d7 \u03b2) \u2192\u2080 M) : \u03b1 \u2192\u2080 (\u03b2 \u2192\u2080 M) :=\nf.sum $ \u03bbp c, single p.1 (single p.2 c)\n\n@[simp] lemma curry_apply (f : (\u03b1 \u00d7 \u03b2) \u2192\u2080 M) (x : \u03b1) (y : \u03b2) :\n  f.curry x y = f (x, y) :=\nbegin\n  classical,\n  have : \u2200 (b : \u03b1 \u00d7 \u03b2), single b.fst (single b.snd (f b)) x y = if b = (x, y) then f b else 0,\n  { rintros \u27e8b\u2081, b\u2082\u27e9,\n    simp [single_apply, ite_apply, prod.ext_iff, ite_and],\n    split_ifs; simp [single_apply, *] },\n  rw [finsupp.curry, sum_apply, sum_apply, finsupp.sum, finset.sum_eq_single, this, if_pos rfl],\n  { intros b hb b_ne, rw [this b, if_neg b_ne] },\n  { intros hxy, rw [this (x, y), if_pos rfl, not_mem_support_iff.mp hxy] }\nend\n\nlemma sum_curry_index (f : (\u03b1 \u00d7 \u03b2) \u2192\u2080 M) (g : \u03b1 \u2192 \u03b2 \u2192 M \u2192 N)\n  (hg\u2080 : \u2200 a b, g a b 0 = 0) (hg\u2081 : \u2200a b c\u2080 c\u2081, g a b (c\u2080 + c\u2081) = g a b c\u2080 + g a b c\u2081) :\n  f.curry.sum (\u03bba f, f.sum (g a)) = f.sum (\u03bbp c, g p.1 p.2 c) :=\nbegin\n  rw [finsupp.curry],\n  transitivity,\n  { exact sum_sum_index (assume a, sum_zero_index)\n      (assume a b\u2080 b\u2081, sum_add_index' (assume a, hg\u2080 _ _) (assume c d\u2080 d\u2081, hg\u2081 _ _ _ _)) },\n  congr, funext p c,\n  transitivity,\n  { exact sum_single_index sum_zero_index },\n  exact sum_single_index (hg\u2080 _ _)\nend\n\n/-- Given a finitely supported function `f` from `\u03b1` to the type of\nfinitely supported functions from `\u03b2` to `M`,\n`uncurry f` is the \"uncurried\" finitely supported function from `\u03b1 \u00d7 \u03b2` to `M`. -/\nprotected def uncurry (f : \u03b1 \u2192\u2080 (\u03b2 \u2192\u2080 M)) : (\u03b1 \u00d7 \u03b2) \u2192\u2080 M :=\nf.sum $ \u03bba g, g.sum $ \u03bbb c, single (a, b) c\n\n/-- `finsupp_prod_equiv` defines the `equiv` between `((\u03b1 \u00d7 \u03b2) \u2192\u2080 M)` and `(\u03b1 \u2192\u2080 (\u03b2 \u2192\u2080 M))` given by\ncurrying and uncurrying. -/\ndef finsupp_prod_equiv : ((\u03b1 \u00d7 \u03b2) \u2192\u2080 M) \u2243 (\u03b1 \u2192\u2080 (\u03b2 \u2192\u2080 M)) :=\n{ to_fun := finsupp.curry,\n  inv_fun := finsupp.uncurry,\n  left_inv := \u03bb f, begin\n    rw [finsupp.uncurry, sum_curry_index],\n    { simp_rw [prod.mk.eta, sum_single], },\n    { intros, apply single_zero },\n    { intros, apply single_add }\n  end,\n  right_inv := \u03bb f, by simp only [\n    finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index,\n    sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff,\n    forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single] }\n\nlemma filter_curry (f : \u03b1 \u00d7 \u03b2 \u2192\u2080 M) (p : \u03b1 \u2192 Prop) :\n  (f.filter (\u03bba:\u03b1\u00d7\u03b2, p a.1)).curry = f.curry.filter p :=\nbegin\n  classical,\n  rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, filter_sum, support_filter,\n    sum_filter],\n  refine finset.sum_congr rfl _,\n  rintros \u27e8a\u2081, a\u2082\u27e9 ha,\n  dsimp only,\n  split_ifs,\n  { rw [filter_apply_pos, filter_single_of_pos]; exact h },\n  { rwa [filter_single_of_neg] }\nend\n\nlemma support_curry [decidable_eq \u03b1] (f : \u03b1 \u00d7 \u03b2 \u2192\u2080 M) :\n  f.curry.support \u2286 f.support.image prod.fst :=\nbegin\n  rw \u2190 finset.bUnion_singleton,\n  refine finset.subset.trans support_sum _,\n  refine finset.bUnion_mono (assume a _, support_single_subset)\nend\n\nend curry_uncurry\n\n/-! ### Declarations about finitely supported functions whose support is a `sum` type -/\n\nsection sum\n\n/-- `finsupp.sum_elim f g` maps `inl x` to `f x` and `inr y` to `g y`. -/\ndef sum_elim {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : \u03b1 \u2192\u2080 \u03b3) (g : \u03b2 \u2192\u2080 \u03b3) : \u03b1 \u2295 \u03b2 \u2192\u2080 \u03b3 :=\non_finset\n  (by haveI := classical.dec_eq \u03b1; haveI := classical.dec_eq \u03b2;\n    exact (f.support.map \u27e8_, sum.inl_injective\u27e9) \u222a g.support.map \u27e8_, sum.inr_injective\u27e9)\n  (sum.elim f g)\n  (\u03bb ab h, by { cases ab with a b; simp only [sum.elim_inl, sum.elim_inr] at h; simpa })\n\n@[simp] lemma coe_sum_elim {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : \u03b1 \u2192\u2080 \u03b3) (g : \u03b2 \u2192\u2080 \u03b3) : \u21d1(sum_elim f g) = sum.elim f g := rfl\n\nlemma sum_elim_apply {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : \u03b1 \u2192\u2080 \u03b3) (g : \u03b2 \u2192\u2080 \u03b3) (x : \u03b1 \u2295 \u03b2) : sum_elim f g x = sum.elim f g x := rfl\n\nlemma sum_elim_inl {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : \u03b1 \u2192\u2080 \u03b3) (g : \u03b2 \u2192\u2080 \u03b3) (x : \u03b1) : sum_elim f g (sum.inl x) = f x := rfl\n\nlemma sum_elim_inr {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : \u03b1 \u2192\u2080 \u03b3) (g : \u03b2 \u2192\u2080 \u03b3) (x : \u03b2) : sum_elim f g (sum.inr x) = g x := rfl\n\n/-- The equivalence between `(\u03b1 \u2295 \u03b2) \u2192\u2080 \u03b3` and `(\u03b1 \u2192\u2080 \u03b3) \u00d7 (\u03b2 \u2192\u2080 \u03b3)`.\n\nThis is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/\n@[simps apply symm_apply]\ndef sum_finsupp_equiv_prod_finsupp {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3] :\n  ((\u03b1 \u2295 \u03b2) \u2192\u2080 \u03b3) \u2243 (\u03b1 \u2192\u2080 \u03b3) \u00d7 (\u03b2 \u2192\u2080 \u03b3) :=\n{ to_fun := \u03bb f,\n    \u27e8f.comap_domain sum.inl (sum.inl_injective.inj_on _),\n     f.comap_domain sum.inr (sum.inr_injective.inj_on _)\u27e9,\n  inv_fun := \u03bb fg, sum_elim fg.1 fg.2,\n  left_inv := \u03bb f, by { ext ab, cases ab with a b; simp },\n  right_inv := \u03bb fg, by { ext; simp } }\n\nlemma fst_sum_finsupp_equiv_prod_finsupp {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : (\u03b1 \u2295 \u03b2) \u2192\u2080 \u03b3) (x : \u03b1) :\n  (sum_finsupp_equiv_prod_finsupp f).1 x = f (sum.inl x) :=\nrfl\n\nlemma snd_sum_finsupp_equiv_prod_finsupp {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (f : (\u03b1 \u2295 \u03b2) \u2192\u2080 \u03b3) (y : \u03b2) :\n  (sum_finsupp_equiv_prod_finsupp f).2 y = f (sum.inr y) :=\nrfl\n\nlemma sum_finsupp_equiv_prod_finsupp_symm_inl {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (fg : (\u03b1 \u2192\u2080 \u03b3) \u00d7 (\u03b2 \u2192\u2080 \u03b3)) (x : \u03b1) :\n  (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x :=\nrfl\n\nlemma sum_finsupp_equiv_prod_finsupp_symm_inr {\u03b1 \u03b2 \u03b3 : Type*} [has_zero \u03b3]\n  (fg : (\u03b1 \u2192\u2080 \u03b3) \u00d7 (\u03b2 \u2192\u2080 \u03b3)) (y : \u03b2) :\n  (sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y :=\nrfl\n\nvariables [add_monoid M]\n\n/-- The additive equivalence between `(\u03b1 \u2295 \u03b2) \u2192\u2080 M` and `(\u03b1 \u2192\u2080 M) \u00d7 (\u03b2 \u2192\u2080 M)`.\n\nThis is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/\n@[simps apply symm_apply] def sum_finsupp_add_equiv_prod_finsupp {\u03b1 \u03b2 : Type*} :\n  ((\u03b1 \u2295 \u03b2) \u2192\u2080 M) \u2243+ (\u03b1 \u2192\u2080 M) \u00d7 (\u03b2 \u2192\u2080 M) :=\n{ map_add' :=\n    by { intros, ext;\n          simp only [equiv.to_fun_as_coe, prod.fst_add, prod.snd_add, add_apply,\n              snd_sum_finsupp_equiv_prod_finsupp, fst_sum_finsupp_equiv_prod_finsupp] },\n  .. sum_finsupp_equiv_prod_finsupp }\n\nlemma fst_sum_finsupp_add_equiv_prod_finsupp {\u03b1 \u03b2 : Type*}\n  (f : (\u03b1 \u2295 \u03b2) \u2192\u2080 M) (x : \u03b1) :\n  (sum_finsupp_add_equiv_prod_finsupp f).1 x = f (sum.inl x) :=\nrfl\n\nlemma snd_sum_finsupp_add_equiv_prod_finsupp {\u03b1 \u03b2 : Type*}\n  (f : (\u03b1 \u2295 \u03b2) \u2192\u2080 M) (y : \u03b2) :\n  (sum_finsupp_add_equiv_prod_finsupp f).2 y = f (sum.inr y) :=\nrfl\n\nlemma sum_finsupp_add_equiv_prod_finsupp_symm_inl {\u03b1 \u03b2 : Type*}\n  (fg : (\u03b1 \u2192\u2080 M) \u00d7 (\u03b2 \u2192\u2080 M)) (x : \u03b1) :\n  (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x :=\nrfl\n\nlemma sum_finsupp_add_equiv_prod_finsupp_symm_inr {\u03b1 \u03b2 : Type*}\n  (fg : (\u03b1 \u2192\u2080 M) \u00d7 (\u03b2 \u2192\u2080 M)) (y : \u03b2) :\n  (sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y :=\nrfl\n\nend sum\n\n/-! ### Declarations about scalar multiplication -/\n\nsection\nvariables [has_zero M] [monoid_with_zero R] [mul_action_with_zero R M]\n\n@[simp] lemma single_smul (a b : \u03b1) (f : \u03b1 \u2192 M) (r : R) :\n  (single a r b) \u2022 (f a) = single a (r \u2022 f b) b :=\nby by_cases a = b; simp [h]\n\nend\n\nsection\nvariables [monoid G] [mul_action G \u03b1] [add_comm_monoid M]\n\n/-- Scalar multiplication acting on the domain.\n\nThis is not an instance as it would conflict with the action on the range.\nSee the `instance_diamonds` test for examples of such conflicts. -/\ndef comap_has_smul : has_smul G (\u03b1 \u2192\u2080 M) :=\n{ smul := \u03bb g, map_domain ((\u2022) g) }\n\nlocal attribute [instance] comap_has_smul\n\nlemma comap_smul_def (g : G) (f : \u03b1 \u2192\u2080 M) : g \u2022 f = map_domain ((\u2022) g) f := rfl\n\n@[simp] lemma comap_smul_single (g : G) (a : \u03b1) (b : M) :\n  g \u2022 single a b = single (g \u2022 a) b :=\nmap_domain_single\n\n/-- `finsupp.comap_has_smul` is multiplicative -/\ndef comap_mul_action : mul_action G (\u03b1 \u2192\u2080 M) :=\n{ one_smul := \u03bb f, by  rw [comap_smul_def, one_smul_eq_id, map_domain_id],\n  mul_smul := \u03bb g g' f, by rw [comap_smul_def, comap_smul_def, comap_smul_def, \u2190comp_smul_left,\n    map_domain_comp], }\n\nlocal attribute [instance] comap_mul_action\n\n/-- `finsupp.comap_has_smul` is distributive -/\ndef comap_distrib_mul_action :\n  distrib_mul_action G (\u03b1 \u2192\u2080 M) :=\n{ smul_zero := \u03bb g, by { ext, dsimp [(\u2022)], simp, },\n  smul_add := \u03bb g f f', by { ext, dsimp [(\u2022)], simp [map_domain_add], }, }\n\nend\n\nsection\nvariables [group G] [mul_action G \u03b1] [add_comm_monoid M]\n\nlocal attribute [instance] comap_has_smul comap_mul_action comap_distrib_mul_action\n\n/-- When `G` is a group, `finsupp.comap_has_smul` acts by precomposition with the action of `g\u207b\u00b9`.\n-/\n@[simp] lemma comap_smul_apply (g : G) (f : \u03b1 \u2192\u2080 M) (a : \u03b1) :\n  (g \u2022 f) a = f (g\u207b\u00b9 \u2022 a) :=\nbegin\n  conv_lhs { rw \u2190smul_inv_smul g a },\n  exact map_domain_apply (mul_action.injective g) _ (g\u207b\u00b9 \u2022 a),\nend\n\nend\n\nsection\n\ninstance [has_zero M] [smul_zero_class R M] : smul_zero_class R (\u03b1 \u2192\u2080 M) :=\n{ smul := \u03bb a v, v.map_range ((\u2022) a) (smul_zero _),\n  smul_zero := \u03bb a, by { ext, apply smul_zero } }\n\n/-!\nThroughout this section, some `monoid` and `semiring` arguments are specified with `{}` instead of\n`[]`. See note [implicit instance arguments].\n-/\n\n@[simp] lemma coe_smul [add_monoid M] [distrib_smul R M]\n  (b : R) (v : \u03b1 \u2192\u2080 M) : \u21d1(b \u2022 v) = b \u2022 v := rfl\nlemma smul_apply [add_monoid M] [distrib_smul R M]\n  (b : R) (v : \u03b1 \u2192\u2080 M) (a : \u03b1) : (b \u2022 v) a = b \u2022 (v a) := rfl\n\nlemma _root_.is_smul_regular.finsupp [add_monoid M] [distrib_smul R M] {k : R}\n  (hk : is_smul_regular M k) : is_smul_regular (\u03b1 \u2192\u2080 M) k :=\n\u03bb _ _ h, ext $ \u03bb i, hk (congr_fun h i)\n\ninstance [nonempty \u03b1] [add_monoid M] [distrib_smul R M] [has_faithful_smul R M] :\n  has_faithful_smul R (\u03b1 \u2192\u2080 M) :=\n{ eq_of_smul_eq_smul := \u03bb r\u2081 r\u2082 h, let \u27e8a\u27e9 := \u2039nonempty \u03b1\u203a in eq_of_smul_eq_smul $ \u03bb m : M,\n    by simpa using congr_fun (h (single a m)) a }\n\nvariables (\u03b1 M)\n\ninstance [add_zero_class M] [distrib_smul R M] : distrib_smul R (\u03b1 \u2192\u2080 M) :=\n{ smul      := (\u2022),\n  smul_add  := \u03bb a x y, ext $ \u03bb _, smul_add _ _ _,\n  smul_zero := \u03bb x, ext $ \u03bb _, smul_zero _ }\n\ninstance [monoid R] [add_monoid M] [distrib_mul_action R M] : distrib_mul_action R (\u03b1 \u2192\u2080 M) :=\n{ smul      := (\u2022),\n  one_smul  := \u03bb x, ext $ \u03bb _, one_smul _ _,\n  mul_smul  := \u03bb r s x, ext $ \u03bb _, mul_smul _ _ _,\n  ..finsupp.distrib_smul _ _ }\n\ninstance [monoid R] [monoid S] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action S M]\n  [has_smul R S] [is_scalar_tower R S M] :\n  is_scalar_tower R S (\u03b1 \u2192\u2080 M) :=\n{ smul_assoc := \u03bb r s a, ext $ \u03bb _, smul_assoc _ _ _ }\n\ninstance [monoid R] [monoid S] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action S M]\n  [smul_comm_class R S M] :\n  smul_comm_class R S (\u03b1 \u2192\u2080 M) :=\n{ smul_comm := \u03bb r s a, ext $ \u03bb _, smul_comm _ _ _ }\n\ninstance [monoid R] [add_monoid M] [distrib_mul_action R M] [distrib_mul_action R\u1d50\u1d52\u1d56 M]\n  [is_central_scalar R M] : is_central_scalar R (\u03b1 \u2192\u2080 M) :=\n{ op_smul_eq_smul := \u03bb r a, ext $ \u03bb _, op_smul_eq_smul _ _ }\n\ninstance [semiring R] [add_comm_monoid M] [module R M] : module R (\u03b1 \u2192\u2080 M) :=\n{ smul      := (\u2022),\n  zero_smul := \u03bb x, ext $ \u03bb _, zero_smul _ _,\n  add_smul  := \u03bb a x y, ext $ \u03bb _, add_smul _ _ _,\n  .. finsupp.distrib_mul_action \u03b1 M }\n\nvariables {\u03b1 M} {R}\n\nlemma support_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M] {b : R} {g : \u03b1 \u2192\u2080 M} :\n  (b \u2022 g).support \u2286 g.support :=\n\u03bb a, by { simp only [smul_apply, mem_support_iff, ne.def], exact mt (\u03bb h, h.symm \u25b8 smul_zero _) }\n\n@[simp]\nlemma support_smul_eq [semiring R] [add_comm_monoid M] [module R M]\n  [no_zero_smul_divisors R M] {b : R} (hb : b \u2260 0) {g : \u03b1 \u2192\u2080 M} :\n  (b \u2022 g).support = g.support :=\nfinset.ext (\u03bb a, by simp [finsupp.smul_apply, hb])\n\nsection\n\nvariables {p : \u03b1 \u2192 Prop}\n\n@[simp] lemma filter_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M]\n  {b : R} {v : \u03b1 \u2192\u2080 M} : (b \u2022 v).filter p = b \u2022 v.filter p :=\ncoe_fn_injective $ set.indicator_const_smul {x | p x} b v\n\nend\n\nlemma map_domain_smul {_ : monoid R} [add_comm_monoid M] [distrib_mul_action R M]\n   {f : \u03b1 \u2192 \u03b2} (b : R) (v : \u03b1 \u2192\u2080 M) : map_domain f (b \u2022 v) = b \u2022 map_domain f v :=\nmap_domain_map_range _ _ _ _ (smul_add b)\n\n@[simp] lemma smul_single {_ : monoid R} [add_monoid M] [distrib_mul_action R M]\n  (c : R) (a : \u03b1) (b : M) : c \u2022 finsupp.single a b = finsupp.single a (c \u2022 b) :=\nmap_range_single\n\n@[simp] lemma smul_single' {_ : semiring R}\n  (c : R) (a : \u03b1) (b : R) : c \u2022 finsupp.single a b = finsupp.single a (c * b) :=\nsmul_single _ _ _\n\nlemma map_range_smul {_ : monoid R} [add_monoid M] [distrib_mul_action R M]\n  [add_monoid N] [distrib_mul_action R N]\n  {f : M \u2192 N} {hf : f 0 = 0} (c : R) (v : \u03b1 \u2192\u2080 M) (hsmul : \u2200 x, f (c \u2022 x) = c \u2022 f x) :\n  map_range f hf (c \u2022 v) = c \u2022 map_range f hf v :=\nbegin\n  erw \u2190map_range_comp,\n  have : (f \u2218 (\u2022) c) = ((\u2022) c \u2218 f) := funext hsmul,\n  simp_rw this,\n  apply map_range_comp,\n  rw [function.comp_apply, smul_zero, hf],\nend\n\nlemma smul_single_one [semiring R] (a : \u03b1) (b : R) : b \u2022 single a 1 = single a b :=\nby rw [smul_single, smul_eq_mul, mul_one]\n\nlemma comap_domain_smul [add_monoid M] [monoid R] [distrib_mul_action R M]\n  {f : \u03b1 \u2192 \u03b2} (r : R) (v : \u03b2 \u2192\u2080 M)\n  (hfv : set.inj_on f (f \u207b\u00b9' \u2191(v.support)))\n  (hfrv : set.inj_on f (f \u207b\u00b9' \u2191((r \u2022 v).support)) :=\n    hfv.mono $ set.preimage_mono $ finset.coe_subset.mpr support_smul):\n  comap_domain f (r \u2022 v) hfrv = r \u2022 comap_domain f v hfv :=\nby { ext, refl }\n\n/-- A version of `finsupp.comap_domain_smul` that's easier to use. -/\nlemma comap_domain_smul_of_injective [add_monoid M] [monoid R] [distrib_mul_action R M]\n  {f : \u03b1 \u2192 \u03b2} (hf : function.injective f) (r : R) (v : \u03b2 \u2192\u2080 M) :\n  comap_domain f (r \u2022 v) (hf.inj_on _) = r \u2022 comap_domain f v (hf.inj_on _) :=\ncomap_domain_smul _ _ _ _\n\nend\n\nlemma sum_smul_index [semiring R] [add_comm_monoid M] {g : \u03b1 \u2192\u2080 R} {b : R} {h : \u03b1 \u2192 R \u2192 M}\n  (h0 : \u2200i, h i 0 = 0) : (b \u2022 g).sum h = g.sum (\u03bbi a, h i (b * a)) :=\nfinsupp.sum_map_range_index h0\n\nlemma sum_smul_index' [add_monoid M] [distrib_smul R M] [add_comm_monoid N]\n  {g : \u03b1 \u2192\u2080 M} {b : R} {h : \u03b1 \u2192 M \u2192 N} (h0 : \u2200i, h i 0 = 0) :\n  (b \u2022 g).sum h = g.sum (\u03bbi c, h i (b \u2022 c)) :=\nfinsupp.sum_map_range_index h0\n\n/-- A version of `finsupp.sum_smul_index'` for bundled additive maps. -/\nlemma sum_smul_index_add_monoid_hom\n  [add_monoid M] [add_comm_monoid N] [distrib_smul R M]\n  {g : \u03b1 \u2192\u2080 M} {b : R} {h : \u03b1 \u2192 M \u2192+ N} :\n  (b \u2022 g).sum (\u03bb a, h a) = g.sum (\u03bb i c, h i (b \u2022 c)) :=\nsum_map_range_index (\u03bb i, (h i).map_zero)\n\ninstance [semiring R] [add_comm_monoid M] [module R M] {\u03b9 : Type*}\n  [no_zero_smul_divisors R M] : no_zero_smul_divisors R (\u03b9 \u2192\u2080 M) :=\n\u27e8\u03bb c f h, or_iff_not_imp_left.mpr (\u03bb hc, finsupp.ext\n  (\u03bb i, (smul_eq_zero.mp (finsupp.ext_iff.mp h i)).resolve_left hc))\u27e9\n\nsection distrib_mul_action_hom\n\nvariables [semiring R]\nvariables [add_comm_monoid M] [add_comm_monoid N] [distrib_mul_action R M] [distrib_mul_action R N]\n\n/-- `finsupp.single` as a `distrib_mul_action_hom`.\n\nSee also `finsupp.lsingle` for the version as a linear map. -/\ndef distrib_mul_action_hom.single (a : \u03b1) : M \u2192+[R] (\u03b1 \u2192\u2080 M) :=\n{ map_smul' :=\n    \u03bb k m, by simp only [add_monoid_hom.to_fun_eq_coe, single_add_hom_apply, smul_single],\n  .. single_add_hom a }\n\nlemma distrib_mul_action_hom_ext {f g : (\u03b1 \u2192\u2080 M) \u2192+[R] N}\n  (h : \u2200 (a : \u03b1) (m : M), f (single a m) = g (single a m)) :\n  f = g :=\ndistrib_mul_action_hom.to_add_monoid_hom_injective $ add_hom_ext h\n\n/-- See note [partially-applied ext lemmas]. -/\n@[ext] lemma distrib_mul_action_hom_ext' {f g : (\u03b1 \u2192\u2080 M) \u2192+[R] N}\n  (h : \u2200 (a : \u03b1), f.comp (distrib_mul_action_hom.single a) =\n                  g.comp (distrib_mul_action_hom.single a)) :\n  f = g :=\ndistrib_mul_action_hom_ext $ \u03bb a, distrib_mul_action_hom.congr_fun (h a)\n\nend distrib_mul_action_hom\n\nsection\nvariables [has_zero R]\n\n/-- The `finsupp` version of `pi.unique`. -/\ninstance unique_of_right [subsingleton R] : unique (\u03b1 \u2192\u2080 R) := fun_like.coe_injective.unique\n\n/-- The `finsupp` version of `pi.unique_of_is_empty`. -/\ninstance unique_of_left [is_empty \u03b1] : unique (\u03b1 \u2192\u2080 R) := fun_like.coe_injective.unique\n\nend\n\n/-- Given an `add_comm_monoid M` and `s : set \u03b1`, `restrict_support_equiv s M` is the `equiv`\nbetween the subtype of finitely supported functions with support contained in `s` and\nthe type of finitely supported functions from `s`. -/\ndef restrict_support_equiv (s : set \u03b1) (M : Type*) [add_comm_monoid M] :\n  {f : \u03b1 \u2192\u2080 M // \u2191f.support \u2286 s } \u2243 (s \u2192\u2080 M) :=\n{ to_fun := \u03bb f, subtype_domain (\u03bb x, x \u2208 s) f.1,\n  inv_fun := \u03bb f, \u27e8f.map_domain subtype.val, begin\n    classical,\n    refine set.subset.trans (finset.coe_subset.2 map_domain_support) _,\n    rw [finset.coe_image, set.image_subset_iff],\n    exact assume x hx, x.2,\n  end\u27e9,\n  left_inv := begin\n    rintros \u27e8f, hf\u27e9,\n    apply subtype.eq,\n    ext a,\n    dsimp only,\n    refine classical.by_cases (assume h : a \u2208 set.range (subtype.val : s \u2192 \u03b1), _) (assume h, _),\n    { rcases h with \u27e8x, rfl\u27e9,\n      rw [map_domain_apply subtype.val_injective, subtype_domain_apply] },\n    { convert map_domain_notin_range _ _ h,\n      rw [\u2190 not_mem_support_iff],\n      refine mt _ h,\n      exact assume ha, \u27e8\u27e8a, hf ha\u27e9, rfl\u27e9 }\n  end,\n  right_inv := \u03bb f, begin\n    ext \u27e8a, ha\u27e9,\n    dsimp only,\n    rw [subtype_domain_apply, map_domain_apply subtype.val_injective]\n  end }\n\n/-- Given `add_comm_monoid M` and `e : \u03b1 \u2243 \u03b2`, `dom_congr e` is the corresponding `equiv` between\n`\u03b1 \u2192\u2080 M` and `\u03b2 \u2192\u2080 M`.\n\nThis is `finsupp.equiv_congr_left` as an `add_equiv`. -/\n@[simps apply]\nprotected def dom_congr [add_comm_monoid M] (e : \u03b1 \u2243 \u03b2) : (\u03b1 \u2192\u2080 M) \u2243+ (\u03b2 \u2192\u2080 M) :=\n{ to_fun := equiv_map_domain e,\n  inv_fun := equiv_map_domain e.symm,\n  left_inv := \u03bb v, begin\n    simp only [\u2190 equiv_map_domain_trans, equiv.self_trans_symm],\n    exact equiv_map_domain_refl _\n  end,\n  right_inv := begin\n    assume v,\n    simp only [\u2190 equiv_map_domain_trans, equiv.symm_trans_self],\n    exact equiv_map_domain_refl _\n  end,\n  map_add' := \u03bb a b, by simp only [equiv_map_domain_eq_map_domain]; exact map_domain_add }\n\n@[simp] lemma dom_congr_refl [add_comm_monoid M] :\n  finsupp.dom_congr (equiv.refl \u03b1) = add_equiv.refl (\u03b1 \u2192\u2080 M) :=\nadd_equiv.ext $ \u03bb _, equiv_map_domain_refl _\n\n@[simp] \n\n@[simp] lemma dom_congr_trans [add_comm_monoid M] (e : \u03b1 \u2243 \u03b2) (f : \u03b2 \u2243 \u03b3) :\n  (finsupp.dom_congr e).trans (finsupp.dom_congr f) =\n    (finsupp.dom_congr (e.trans f) : (\u03b1 \u2192\u2080 M) \u2243+ _) :=\nadd_equiv.ext $ \u03bb _, (equiv_map_domain_trans _ _ _).symm\n\nend finsupp\n\nnamespace finsupp\n\n/-! ### Declarations about sigma types -/\n\nsection sigma\n\nvariables {\u03b1s : \u03b9 \u2192 Type*} [has_zero M] (l : (\u03a3 i, \u03b1s i) \u2192\u2080 M)\n\n/-- Given `l`, a finitely supported function from the sigma type `\u03a3 (i : \u03b9), \u03b1s i` to `M` and\nan index element `i : \u03b9`, `split l i` is the `i`th component of `l`,\na finitely supported function from `as i` to `M`.\n\nThis is the `finsupp` version of `sigma.curry`.\n-/\ndef split (i : \u03b9) : \u03b1s i \u2192\u2080 M :=\nl.comap_domain (sigma.mk i) (\u03bb x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2)\n\nlemma split_apply (i : \u03b9) (x : \u03b1s i) : split l i x = l \u27e8i, x\u27e9 :=\nbegin\n  dunfold split,\n  rw comap_domain_apply\nend\n\n/-- Given `l`, a finitely supported function from the sigma type `\u03a3 (i : \u03b9), \u03b1s i` to `\u03b2`,\n`split_support l` is the finset of indices in `\u03b9` that appear in the support of `l`. -/\ndef split_support (l : (\u03a3 i, \u03b1s i) \u2192\u2080 M) : finset \u03b9 :=\nby haveI := classical.dec_eq \u03b9; exact l.support.image sigma.fst\n\nlemma mem_split_support_iff_nonzero (i : \u03b9) :\n  i \u2208 split_support l \u2194 split l i \u2260 0 :=\nbegin\n  rw [split_support, mem_image, ne.def, \u2190 support_eq_empty, \u2190 ne.def,\n    \u2190 finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty],\n  simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right,\n    mem_support_iff, sigma.exists, ne.def]\nend\n\n/-- Given `l`, a finitely supported function from the sigma type `\u03a3 i, \u03b1s i` to `\u03b2` and\nan `\u03b9`-indexed family `g` of functions from `(\u03b1s i \u2192\u2080 \u03b2)` to `\u03b3`, `split_comp` defines a\nfinitely supported function from the index type `\u03b9` to `\u03b3` given by composing `g i` with\n`split l i`. -/\ndef split_comp [has_zero N] (g : \u03a0 i, (\u03b1s i \u2192\u2080 M) \u2192 N)\n  (hg : \u2200 i x, x = 0 \u2194 g i x = 0) : \u03b9 \u2192\u2080 N :=\n{ support := split_support l,\n  to_fun := \u03bb i, g i (split l i),\n  mem_support_to_fun :=\n  begin\n    intros i,\n    rw [mem_split_support_iff_nonzero, not_iff_not, hg],\n  end }\n\nlemma sigma_support : l.support = l.split_support.sigma (\u03bb i, (l.split i).support) :=\nby simp only [finset.ext_iff, split_support, split, comap_domain, mem_image,\n  mem_preimage, sigma.forall, mem_sigma]; tauto\n\nlemma sigma_sum [add_comm_monoid N] (f : (\u03a3 (i : \u03b9), \u03b1s i) \u2192 M \u2192 N) :\n  l.sum f = \u2211 i in split_support l, (split l i).sum (\u03bb (a : \u03b1s i) b, f \u27e8i, a\u27e9 b) :=\nby simp only [sum, sigma_support, sum_sigma, split_apply]\n\nvariables {\u03b7 : Type*} [fintype \u03b7] {\u03b9s : \u03b7 \u2192 Type*} [has_zero \u03b1]\n\n/-- On a `fintype \u03b7`, `finsupp.split` is an equivalence between `(\u03a3 (j : \u03b7), \u03b9s j) \u2192\u2080 \u03b1`\nand `\u03a0 j, (\u03b9s j \u2192\u2080 \u03b1)`.\n\nThis is the `finsupp` version of `equiv.Pi_curry`. -/\nnoncomputable def sigma_finsupp_equiv_pi_finsupp :\n  ((\u03a3 j, \u03b9s j) \u2192\u2080 \u03b1) \u2243 \u03a0 j, (\u03b9s j \u2192\u2080 \u03b1) :=\n{ to_fun := split,\n  inv_fun := \u03bb f, on_finset\n    (finset.univ.sigma (\u03bb j, (f j).support))\n    (\u03bb ji, f ji.1 ji.2)\n    (\u03bb g hg, finset.mem_sigma.mpr \u27e8finset.mem_univ _, mem_support_iff.mpr hg\u27e9),\n  left_inv := \u03bb f, by { ext, simp [split] },\n  right_inv := \u03bb f, by { ext, simp [split] } }\n\n@[simp] lemma sigma_finsupp_equiv_pi_finsupp_apply\n  (f : (\u03a3 j, \u03b9s j) \u2192\u2080 \u03b1) (j i) :\nsigma_finsupp_equiv_pi_finsupp f j i = f \u27e8j, i\u27e9 := rfl\n\n/-- On a `fintype \u03b7`, `finsupp.split` is an additive equivalence between\n`(\u03a3 (j : \u03b7), \u03b9s j) \u2192\u2080 \u03b1` and `\u03a0 j, (\u03b9s j \u2192\u2080 \u03b1)`.\n\nThis is the `add_equiv` version of `finsupp.sigma_finsupp_equiv_pi_finsupp`.\n-/\nnoncomputable def sigma_finsupp_add_equiv_pi_finsupp\n  {\u03b1 : Type*} {\u03b9s : \u03b7 \u2192 Type*} [add_monoid \u03b1] :\n  ((\u03a3 j, \u03b9s j) \u2192\u2080 \u03b1) \u2243+ \u03a0 j, (\u03b9s j \u2192\u2080 \u03b1) :=\n{ map_add' := \u03bb f g, by { ext, simp },\n  .. sigma_finsupp_equiv_pi_finsupp }\n\n@[simp] lemma sigma_finsupp_add_equiv_pi_finsupp_apply\n  {\u03b1 : Type*} {\u03b9s : \u03b7 \u2192 Type*} [add_monoid \u03b1] (f : (\u03a3 j, \u03b9s j) \u2192\u2080 \u03b1) (j i) :\nsigma_finsupp_add_equiv_pi_finsupp f j i = f \u27e8j, i\u27e9 := rfl\n\nend sigma\n\n/-! ### Meta declarations -/\n\n/-- Stringify a `finsupp` as a sequence of `finsupp.single` terms.\n\nNote this is `meta` as it has to choose some order for the terms. -/\nmeta instance (\u03b9 \u03b1 : Type*) [has_zero \u03b1] [has_repr \u03b9] [has_repr \u03b1] :\n  has_repr (\u03b9 \u2192\u2080 \u03b1) :=\n{ repr := \u03bb f,\n  if f.support.card = 0 then \"0\"\n  else \" + \".intercalate $\n    f.support.val.unquot.map (\u03bb i, \"finsupp.single \" ++ repr i ++ \" \" ++ repr (f i)) }\n\nend finsupp\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/data/finsupp/basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48438008427698437, "lm_q2_score": 0.14608724518943894, "lm_q1q2_score": 0.07076175213665291}}
{"text": "/-\nCopyright (c) 2020 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon\n-/\nimport tactic.core\n\n/-!\n# `pretty_cases` tactic\n\nWhen using `induction` and `cases`, `pretty_cases` prints a `\"Try\nthis:\"` advice that shows how to structure the proof with\n`case { ... }` commands.  In the following example, we apply induction on a\npermutation assumption about lists. `pretty_cases` gives us a proof\nskeleton that explicit selects the branches and explicit names the\nnew local constants:\n\n```lean\nexample {\u03b1} (xs ys : list \u03b1) (h : xs ~ ys) : true :=\nbegin\n  induction h,\n  pretty_cases,\n    -- Try this:\n    --   case list.perm.nil :\n    --   { admit },\n    --   case list.perm.cons : h_x h_l\u2081 h_l\u2082 h_a h_ih\n    --   { admit },\n    --   case list.perm.swap : h_x h_y h_l\n    --   { admit },\n    --   case list.perm.trans : h_l\u2081 h_l\u2082 h_l\u2083 h_a h_a_1 h_ih_a h_ih_a_1\n    --   { admit },\nend\n```\n\n## Main definitions\n\n * `pretty_cases_advice` return `pretty_cases` advice without printing it\n * `pretty_cases` main tactic\n-/\n\nnamespace tactic\n\n/-- Query the proof goal and print the skeleton of a proof by cases. -/\nmeta def pretty_cases_advice : tactic string := retrieve $ do\ngs \u2190 get_goals,\ncases \u2190 gs.mmap $ \u03bb g, do\n{ t : list name \u2190 get_tag g,\n  let vs := t.tail,\n  let \u27e8vs,ts\u27e9 := vs.span (\u03bb n, name.last_string n = \"_arg\"),\n  set_goals [g],\n  ls \u2190 local_context,\n  let m := native.rb_map.of_list $ (ls.map expr.local_uniq_name).zip (ls.map expr.local_pp_name),\n  let vs := vs.map $ \u03bb v, (m.find v.get_prefix).get_or_else `_,\n  let var_decls := string.intercalate \" \" $ vs.map to_string,\n  let var_decls := if vs.empty then \"\" else \" : \" ++ var_decls,\n  pure sformat!\"  case {ts.head}{var_decls}\\n  {{ admit }}\" },\nlet cases := string.intercalate \",\\n\" cases,\npure sformat!\"Try this:\\n{cases}\"\n\nnamespace interactive\n\n/--\nQuery the proof goal and print the skeleton of a proof by\ncases.\n\nFor example, let us consider the following proof:\n\n```lean\nexample {\u03b1} (xs ys : list \u03b1) (h : xs ~ ys) : true :=\nbegin\n  induction h,\n  pretty_cases,\n    -- Try this:\n    --   case list.perm.nil :\n    --   { admit },\n    --   case list.perm.cons : h_x h_l\u2081 h_l\u2082 h_a h_ih\n    --   { admit },\n    --   case list.perm.swap : h_x h_y h_l\n    --   { admit },\n    --   case list.perm.trans : h_l\u2081 h_l\u2082 h_l\u2083 h_a h_a_1 h_ih_a h_ih_a_1\n    --   { admit },\nend\n```\n\nThe output helps the user layout the cases and rename the\nintroduced variables.\n-/\nmeta def pretty_cases : tactic unit :=\npretty_cases_advice >>= trace\n\nadd_tactic_doc\n{ name       := \"pretty_cases\",\n  category   := doc_category.tactic,\n  decl_names := [``tactic.interactive.pretty_cases],\n  tags       := [\"context management\", \"goal management\"] }\n\nend interactive\n\nend tactic\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/pretty_cases.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.37754065479083276, "lm_q2_score": 0.18713268896245425, "lm_q1q2_score": 0.07065019792365422}}
{"text": "/-\nCopyright (c) 2020 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Kevin Buzzard, Bryan Gin-ge Chen, Robert Y. Lewis, Scott Morrison\n-/\nimport tactic.doc_commands\n\n/-!\n\n# Core tactic documentation\n\nThis file adds the majority of the interactive tactics from core Lean (i.e. pre-mathlib) to\nthe API documentation.\n\n## TODO\n\n* Make a PR to core changing core docstrings to the docstrings below,\nand also changing the docstrings of `cc`, `simp` and `conv` to the ones\nalready in the API docs.\n\n* SMT tactics are currently not documented.\n\n* `rsimp` and `constructor_matching` are currently not documented.\n\n* `dsimp` deserves better documentation.\n-/\n\nadd_tactic_doc\n{ name       := \"abstract\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.abstract],\n  tags       := [\"core\", \"proof extraction\"] }\n\n/-- Proves a goal of the form `s = t` when `s` and `t` are expressions built up out of a binary\noperation, and equality can be proved using associativity and commutativity of that operation. -/\nadd_tactic_doc\n{ name       := \"ac_refl\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.ac_refl, `tactic.interactive.ac_reflexivity],\n  tags       := [\"core\", \"lemma application\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"all_goals\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.all_goals],\n  tags       := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"any_goals\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.any_goals],\n  tags       := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"apply\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.apply],\n  tags       := [\"core\", \"basic\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"apply_auto_param\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.apply_auto_param],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"apply_instance\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.apply_instance],\n  tags       := [\"core\", \"type class\"] }\n\nadd_tactic_doc\n{ name       := \"apply_opt_param\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.apply_opt_param],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"apply_with\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.apply_with],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"assume\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.assume],\n  tags       := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"assumption\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.assumption],\n  tags       := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"assumption'\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.assumption'],\n  tags       := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"async\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.async],\n  tags       := [\"core\", \"goal management\", \"combinator\", \"proof extraction\"] }\n\n/--\n`by_cases p` splits the main goal into two cases, assuming `h : p` in the first branch, and\n`h : \u00ac p` in the second branch. You can specify the name of the new hypothesis using the syntax\n`by_cases h : p`.\n\nIf `p` is not already decidable, `by_cases` will use the instance `classical.prop_decidable p`.\n-/\nadd_tactic_doc\n{ name       := \"by_cases\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.by_cases],\n  tags       := [\"core\", \"basic\", \"logic\", \"case bashing\"] }\n\n/--\nIf the target of the main goal is a proposition `p`, `by_contra h` reduces the goal to proving\n`false` using the additional hypothesis `h : \u00ac p`. If `h` is omitted, a name is generated\nautomatically.\n\nThis tactic requires that `p` is decidable. To ensure that all propositions are decidable via\nclassical reasoning, use `open_locale classical`\n(or `local attribute [instance, priority 10] classical.prop_decidable` if you are not using\nmathlib).\n-/\nadd_tactic_doc\n{ name       := \"by_contra / by_contradiction\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.by_contra, `tactic.interactive.by_contradiction],\n  tags       := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"case\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.case],\n  tags       := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"cases\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.cases],\n  tags       := [\"core\", \"basic\", \"induction\"] }\n\n/--\n`cases_matching p` applies the `cases` tactic to a hypothesis `h : type`\nif `type` matches the pattern `p`.\n\n`cases_matching [p_1, ..., p_n]` applies the `cases` tactic to a hypothesis `h : type`\nif `type` matches one of the given patterns.\n\n`cases_matching* p` is a more efficient and compact version\nof `focus1 { repeat { cases_matching p } }`.\nIt is more efficient because the pattern is compiled once.\n\n`casesm` is shorthand for `cases_matching`.\n\nExample: The following tactic destructs all conjunctions and disjunctions in the current context.\n```\ncases_matching* [_ \u2228 _, _ \u2227 _]\n```\n-/\nadd_tactic_doc\n{ name       := \"cases_matching / casesm\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.cases_matching, `tactic.interactive.casesm],\n  tags       := [\"core\", \"induction\", \"context management\"] }\n\n/--\n* `cases_type I` applies the `cases` tactic to a hypothesis `h : (I ...)`\n* `cases_type I_1 ... I_n` applies the `cases` tactic to a hypothesis\n  `h : (I_1 ...)` or ... or `h : (I_n ...)`\n* `cases_type* I` is shorthand for `focus1 { repeat { cases_type I } }`\n* `cases_type! I` only applies `cases` if the number of resulting subgoals is <= 1.\n\nExample: The following tactic destructs all conjunctions and disjunctions in the current context.\n```\ncases_type* or and\n```\n-/\nadd_tactic_doc\n{ name       := \"cases_type\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.cases_type],\n  tags       := [\"core\", \"induction\", \"context management\"] }\n\nadd_tactic_doc\n{ name       := \"change\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.change],\n  tags       := [\"core\", \"basic\", \"renaming\"] }\n\nadd_tactic_doc\n{ name       := \"clear\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.clear],\n  tags       := [\"core\", \"context management\"] }\n\n/--\nClose goals of the form `n \u2260 m` when `n` and `m` have type `nat`, `char`, `string`, `int`\nor `fin sz`, and they are literals. It also closes goals of the form `n < m`, `n > m`, `n \u2264 m` and\n`n \u2265 m` for `nat`. If the goal is of the form `n = m`, then it tries to close it using reflexivity.\n\nIn mathlib, consider using `norm_num` instead for numeric types.\n-/\nadd_tactic_doc\n{ name       := \"comp_val\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.comp_val],\n  tags       := [\"core\", \"arithmetic\"] }\n\n/--\nThe `congr` tactic attempts to identify both sides of an equality goal `A = B`,\nleaving as new goals the subterms of `A` and `B` which are not definitionally equal.\nExample: suppose the goal is `x * f y = g w * f z`. Then `congr` will produce two goals:\n`x = g w` and `y = z`.\n\nIf `x y : t`, and an instance `subsingleton t` is in scope, then any goals of the form\n`x = y` are solved automatically.\n\nNote that `congr` can be over-aggressive at times; the `congr'` tactic in mathlib\nprovides a more refined approach, by taking a parameter that limits the recursion depth.\n-/\nadd_tactic_doc\n{ name       := \"congr\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.congr],\n  tags       := [\"core\", \"congruence\"] }\n\nadd_tactic_doc\n{ name       := \"constructor\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.constructor],\n  tags       := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"contradiction\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.contradiction],\n  tags       := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"delta\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.delta],\n  tags       := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n{ name       := \"destruct\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.destruct],\n  tags       := [\"core\", \"induction\"] }\n\nadd_tactic_doc\n{ name       := \"done\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.done],\n  tags       := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"dsimp\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.dsimp],\n  tags       := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n{ name       := \"dunfold\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.dunfold],\n  tags       := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n{ name       := \"eapply\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.eapply],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"econstructor\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.econstructor],\n  tags       := [\"core\", \"logic\"] }\n\n/--\nA variant of `rw` that uses the unifier more aggressively, unfolding semireducible definitions.\n-/\nadd_tactic_doc\n{ name       := \"erewrite / erw\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.erewrite, `tactic.interactive.erw],\n  tags       := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"exact\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.exact],\n  tags       := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"exacts\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.exacts],\n  tags       := [\"core\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"exfalso\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.exfalso],\n  tags       := [\"core\", \"basic\", \"logic\"] }\n\n/--\n`existsi e` will instantiate an existential quantifier in the target with `e` and leave the\ninstantiated body as the new target. More generally, it applies to any inductive type with one\nconstructor and at least two arguments, applying the constructor with `e` as the first argument\nand leaving the remaining arguments as goals.\n\n`existsi [e\u2081, ..., e\u2099]` iteratively does the same for each expression in the list.\n\nNote: in mathlib, the `use` tactic is an equivalent tactic which sometimes is smarter with\nunification.\n-/\nadd_tactic_doc\n{ name       := \"existsi\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.existsi],\n  tags       := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"fail_if_success\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.fail_if_success],\n  tags       := [\"core\", \"testing\", \"combinator\"] }\n\nadd_tactic_doc\n{ name       := \"fapply\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.fapply],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"focus\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.focus],\n  tags       := [\"core\", \"goal management\", \"combinator\"] }\n\nadd_tactic_doc\n{ name       := \"from\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.from],\n  tags       := [\"core\", \"finishing\"] }\n\n/--\nApply function extensionality and introduce new hypotheses.\nThe tactic `funext` will keep applying new the `funext` lemma until the goal target is not reducible\nto\n```\n  |-  ((fun x, ...) = (fun x, ...))\n```\nThe variant `funext h\u2081 ... h\u2099` applies `funext` `n` times, and uses the given identifiers to name\nthe new hypotheses.\n\nNote also the mathlib tactic `ext`, which applies as many extensionality lemmas as possible.\n-/\nadd_tactic_doc\n{ name       := \"funext\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.funext],\n  tags       := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"generalize\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.generalize],\n  tags       := [\"core\", \"context management\"] }\n\nadd_tactic_doc\n{ name       := \"guard_hyp\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.guard_hyp],\n  tags       := [\"core\", \"testing\", \"context management\"] }\n\nadd_tactic_doc\n{ name       := \"guard_target\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.guard_target],\n  tags       := [\"core\", \"testing\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"have\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.have],\n  tags       := [\"core\", \"basic\", \"context management\"] }\n\nadd_tactic_doc\n{ name       := \"induction\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.induction],\n  tags       := [\"core\", \"basic\", \"induction\"] }\n\nadd_tactic_doc\n{ name       := \"injection\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.injection],\n  tags       := [\"core\", \"structures\", \"induction\"] }\n\nadd_tactic_doc\n{ name       := \"injections\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.injections],\n  tags       := [\"core\", \"structures\", \"induction\"] }\n\n/--\nIf the current goal is a Pi/forall `\u2200 x : t, u` (resp. `let x := t in u`) then `intro` puts\n`x : t` (resp. `x := t`) in the local context. The new subgoal target is `u`.\n\nIf the goal is an arrow `t \u2192 u`, then it puts `h : t` in the local context and the new goal\ntarget is `u`.\n\nIf the goal is neither a Pi/forall nor begins with a let binder, the tactic `intro` applies the\ntactic `whnf` until an introduction can be applied or the goal is not head reducible. In the latter\ncase, the tactic fails.\n\nThe variant `intro z` uses the identifier `z` to name the new hypothesis.\n\nThe variant `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall\nor let binder.\n\nThe variant `intros h\u2081 ... h\u2099` introduces `n` new hypotheses using the given identifiers to name\nthem.\n-/\n\nadd_tactic_doc\n{ name       := \"intro / intros\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.intro, `tactic.interactive.intros],\n  tags       := [\"core\", \"basic\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"introv\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.introv],\n  tags       := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"iterate\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.iterate],\n  tags       := [\"core\", \"combinator\"] }\n\n/--\n`left` applies the first constructor when the type of the target is an inductive data type with\ntwo constructors.\n\nSimilarly, `right` applies the second constructor.\n-/\nadd_tactic_doc\n{ name       := \"left / right\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.left, `tactic.interactive.right],\n  tags       := [\"core\", \"basic\", \"logic\"] }\n\n/--\n`let h : t := p` adds the hypothesis `h : t := p` to the current goal if `p` a term of type `t`.\nIf `t` is omitted, it will be inferred.\n\n`let h : t` adds the hypothesis `h : t := ?M` to the current goal and opens a new subgoal `?M : t`.\nThe new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh\nmetavariable.\n\nIf `h` is omitted, the name `this` is used.\n\nNote the related mathlib tactic `set a := t with h`, which adds the hypothesis `h : a = t` to\nthe local context and replaces `t` with `a` everywhere it can.\n-/\nadd_tactic_doc\n{ name       := \"let\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.let],\n  tags       := [\"core\", \"basic\", \"logic\", \"context management\"] }\n\nadd_tactic_doc\n{ name       := \"mapply\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.mapply],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"match_target\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.match_target],\n  tags       := [\"core\", \"testing\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"refine\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.refine],\n  tags       := [\"core\", \"basic\", \"lemma application\"] }\n\n/--\nThis tactic applies to a goal whose target has the form `t ~ u` where `~` is a reflexive relation,\nthat is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`.\nThe tactic checks whether `t` and `u` are definitionally equal and then solves the goal.\n-/\nadd_tactic_doc\n{ name       := \"refl / reflexivity\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.refl, `tactic.interactive.reflexivity],\n  tags       := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"rename\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.rename],\n  tags       := [\"core\", \"renaming\"] }\n\nadd_tactic_doc\n{ name       := \"repeat\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.repeat],\n  tags       := [\"core\", \"combinator\"] }\n\nadd_tactic_doc\n{ name       := \"revert\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.revert],\n  tags       := [\"core\", \"context management\", \"goal management\"] }\n\n/--\n`rw e` applies an equation or iff `e` as a rewrite rule to the main goal. If `e` is preceded by\nleft arrow (`\u2190` or `<-`), the rewrite is applied in the reverse direction. If `e` is a defined\nconstant, then the equational lemmas associated with `e` are used. This provides a convenient\nway to unfold `e`.\n\n`rw [e\u2081, ..., e\u2099]` applies the given rules sequentially.\n\n`rw e at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a list of hypotheses\nin the local context. In the latter case, a turnstile `\u22a2` or `|-` can also be used, to signify\nthe target of the goal.\n\n`rewrite` is synonymous with `rw`.\n-/\nadd_tactic_doc\n{ name       := \"rw / rewrite\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.rw, `tactic.interactive.rewrite],\n  tags       := [\"core\", \"basic\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"rwa\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.rwa],\n  tags       := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"show\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.show],\n  tags       := [\"core\", \"goal management\", \"renaming\"] }\n\nadd_tactic_doc\n{ name       := \"simp_intros\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.simp_intros],\n  tags       := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n{ name       := \"skip\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.skip],\n  tags       := [\"core\", \"combinator\"] }\n\nadd_tactic_doc\n{ name       := \"solve1\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.solve1],\n  tags       := [\"core\", \"combinator\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"sorry / admit\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.sorry, `tactic.interactive.admit],\n  inherit_description_from := `tactic.interactive.sorry,\n  tags       := [\"core\", \"testing\", \"debugging\"] }\n\nadd_tactic_doc\n{ name       := \"specialize\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.specialize],\n  tags       := [\"core\", \"context management\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"split\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.split],\n  tags       := [\"core\", \"basic\", \"logic\"] }\n\nadd_tactic_doc\n{ name       := \"subst\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.subst],\n  tags       := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"subst_vars\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.subst_vars],\n  tags       := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"success_if_fail\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.success_if_fail],\n  tags       := [\"core\", \"testing\", \"combinator\"] }\n\nadd_tactic_doc\n{ name       := \"suffices\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.suffices],\n  tags       := [\"core\", \"basic\", \"goal management\"] }\n\nadd_tactic_doc\n{ name       := \"symmetry\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.symmetry],\n  tags       := [\"core\", \"basic\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"trace\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.trace],\n  tags       := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n{ name       := \"trace_simp_set\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.trace_simp_set],\n  tags       := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n{ name       := \"trace_state\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.trace_state],\n  tags       := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n{ name       := \"transitivity\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.transitivity],\n  tags       := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n{ name       := \"trivial\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.trivial],\n  tags       := [\"core\", \"finishing\"] }\n\nadd_tactic_doc\n{ name       := \"try\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.try],\n  tags       := [\"core\", \"combinator\"] }\n\nadd_tactic_doc\n{ name       := \"type_check\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.type_check],\n  tags       := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n{ name       := \"unfold\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.unfold],\n  tags       := [\"core\", \"basic\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"unfold1\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.unfold1],\n  tags       := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"unfold_projs\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.unfold_projs],\n  tags       := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n{ name       := \"with_cases\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.with_cases],\n  tags       := [\"core\", \"combinator\"] }\n\n/- conv mode tactics -/\n\n/--\nNavigate to the left-hand-side of a relation.\nA goal of `| a = b` will turn into the goal `| a`.\n-/\nadd_tactic_doc\n{ name       := \"conv: to_lhs\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.to_lhs],\n  tags       := [\"conv\"] }\n\n/--\nNavigate to the right-hand-side of a relation.\nA goal of `| a = b` will turn into the goal `| b`.\n-/\nadd_tactic_doc\n{ name       := \"conv: to_rhs\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.to_rhs],\n  tags       := [\"conv\"] }\n\n/--\nNavigate into every argument of the current head function.\nA target of `| (a * b) * c` will turn into the two targets `| a * b` and `| c`.\n-/\nadd_tactic_doc\n{ name       := \"conv: congr\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.congr],\n  tags       := [\"conv\"] }\n\n/--\nNavigate into the contents of top-level `\u03bb` binders.\nA target of `| \u03bb a, a + b` will turn into the target `| a + b` and introduce `a` into the local\ncontext.\nIf there are multiple binders, all of them will be entered, and if there are none, this tactic is a\nno-op.\n-/\nadd_tactic_doc\n{ name       := \"conv: funext\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.funext],\n  tags       := [\"conv\"] }\n\n/--\nNavigate into the first scope matching the expression.\n\nFor a target of `| \u2200 c, a + (b + c) = 1`, `find (b + _) { ... }` will run the tactics within the\n`{}` with a target of `| b + c`.\n-/\nadd_tactic_doc\n{ name       := \"conv: find\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.find],\n  tags       := [\"conv\"] }\n\n/--\nNavigate into the numbered scopes matching the expression.\n\nFor a target of `| \u03bb c, 10 * c + 20 * c + 30 * c`, `for (_ * _) [1, 3] { ... }` will run the\ntactics within the `{}` with first a target of `| 10 * c`, then a target of `| 30 * c`.\n-/\nadd_tactic_doc\n{ name       := \"conv: for\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.for],\n  tags       := [\"conv\"] }\n\n/--\nEnd conversion of the current goal. This is often what is needed when muscle memory would type\n`sorry`.\n-/\nadd_tactic_doc\n{ name       := \"conv: skip\",\n  category   := doc_category.tactic,\n  decl_names := [`conv.interactive.skip],\n  tags       := [\"conv\"] }\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/lean_core_docs.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48438008427698437, "lm_q2_score": 0.144148856709452, "lm_q1q2_score": 0.06982283536135529}}
{"text": "/-\nCopyright (c) 2020 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison\n\n! This file was ported from Lean 3 source module tactic.hint\n! leanprover-community/mathlib commit 8f6fd1b69096c6a587f745d354306c0d46396915\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.SolveByElim\nimport Mathbin.Tactic.Interactive\n\nnamespace Tactic\n\nnamespace Hint\n\n/-- An attribute marking a `tactic unit` or `tactic string` which should be used by the `hint`\ntactic. -/\n@[user_attribute]\nunsafe def hint_tactic_attribute : user_attribute\n    where\n  Name := `hint_tactic\n  descr := \"A tactic that should be tried by `hint`.\"\n#align tactic.hint.hint_tactic_attribute tactic.hint.hint_tactic_attribute\n\nadd_tactic_doc\n  { Name := \"hint_tactic\"\n    category := DocCategory.attr\n    declNames := [`tactic.hint.hint_tactic_attribute]\n    tags := [\"rewrite\", \"search\"] }\n\n/- ./././Mathport/Syntax/Translate/Tactic/Mathlib/Core.lean:38:34: unsupported: setup_tactic_parser -/\nprivate unsafe def add_tactic_hint (n : Name) (t : expr) : tactic Unit := do\n  add_decl <| declaration.defn n [] q(tactic String) t ReducibilityHints.opaque ff\n  hint_tactic_attribute n () tt\n#align tactic.hint.add_tactic_hint tactic.hint.add_tactic_hint\n\n/-- `add_hint_tactic t` runs the tactic `t` whenever `hint` is invoked.\nThe typical use case is `add_hint_tactic \"foo\"` for some interactive tactic `foo`.\n-/\n@[user_command]\nunsafe def add_hint_tactic (_ : parse (tk \"add_hint_tactic\")) : parser Unit := do\n  let n \u2190 parser.pexpr\n  let e \u2190 to_expr n\n  let s \u2190 eval_expr String e\n  let t := \"`[\" ++ s ++ \"]\"\n  let (t, _) \u2190 with_input parser.pexpr t\n  of_tactic do\n      let h := .str s \"_hint\"\n      let t \u2190\n        to_expr\n            ``(do\n              $(t)\n              pure $(n))\n      add_tactic_hint h t\n#align tactic.hint.add_hint_tactic tactic.hint.add_hint_tactic\n\nadd_tactic_doc\n  { Name := \"add_hint_tactic\"\n    category := DocCategory.cmd\n    declNames := [`tactic.hint.add_hint_tactic]\n    tags := [\"search\"] }\n\nadd_hint_tactic rfl\n\nadd_hint_tactic exact by decide\n\nadd_hint_tactic assumption\n\n-- tidy does something better here: it suggests the actual \"intros X Y f\" string.\n-- perhaps add a wrapper?\nadd_hint_tactic intro\n\nadd_hint_tactic infer_param\n\nadd_hint_tactic dsimp at *\n\nadd_hint_tactic simp at *\n\n-- TODO hook up to squeeze_simp?\nadd_hint_tactic fconstructor\n\nadd_hint_tactic injections\n\nadd_hint_tactic solve_by_elim\n\nadd_hint_tactic unfold_coes\n\nadd_hint_tactic unfold_aux\n\nend Hint\n\n/-- Report a list of tactics that can make progress against the current goal,\nand for each such tactic, the number of remaining goals afterwards.\n-/\nunsafe def hint : tactic (List (String \u00d7 \u2115)) := do\n  let names \u2190 attribute.get_instances `hint_tactic\n  focus1 <| try_all_sorted (names name_to_tactic)\n#align tactic.hint tactic.hint\n\nnamespace Interactive\n\n/-- Report a list of tactics that can make progress against the current goal.\n-/\nunsafe def hint : tactic Unit := do\n  let hints \u2190 tactic.hint\n  if hints = 0 then fail \"no hints available\"\n    else do\n      let t \u2190 hints 0\n      if t.2 = 0 then do\n          trace \"the following tactics solve the goal:\\n----\"\n          (hints fun p : String \u00d7 \u2115 => p.2 = 0).mapM' fun p => tactic.trace f! \"Try this: {p.1}\"\n        else do\n          trace \"the following tactics make progress:\\n----\"\n          hints fun p => tactic.trace f! \"Try this: {p.1}\"\n#align tactic.interactive.hint tactic.interactive.hint\n\n/--\n`hint` lists possible tactics which will make progress (that is, not fail) against the current goal.\n\n```lean\nexample {P Q : Prop} (p : P) (h : P \u2192 Q) : Q :=\nbegin\n  hint,\n  /- the following tactics make progress:\n     ----\n     Try this: solve_by_elim\n     Try this: finish\n     Try this: tauto\n  -/\n  solve_by_elim,\nend\n```\n\nYou can add a tactic to the list that `hint` tries by either using\n1. `attribute [hint_tactic] my_tactic`, if `my_tactic` is already of type `tactic string`\n(`tactic unit` is allowed too, in which case the printed string will be the name of the\ntactic), or\n2. `add_hint_tactic \"my_tactic\"`, specifying a string which works as an interactive tactic.\n-/\nadd_tactic_doc\n  { Name := \"hint\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.hint]\n    tags := [\"search\", \"Try this\"] }\n\nend Interactive\n\nend Tactic\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/Tactic/Hint.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.37387582277169656, "lm_q2_score": 0.18476750391438243, "lm_q1q2_score": 0.0690801025474624}}
{"text": "/-\nCopyright (c) 2018 Sean Leather. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Sean Leather, Mario Carneiro\n\n! This file was ported from Lean 3 source module data.list.alist\n! leanprover-community/mathlib commit f808feb6c18afddb25e66a71d317643cf7fb5fbb\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathlib.Data.List.Sigma\n\n/-!\n# Association Lists\n\nThis file defines association lists. An association list is a list where every element consists of\na key and a value, and no two entries have the same key. The type of the value is allowed to be\ndependent on the type of the key.\n\nThis type dependence is implemented using `Sigma`: The elements of the list are of type `Sigma \u03b2`,\nfor some type index `\u03b2`.\n\n## Main definitions\n\nAssociation lists are represented by the `AList` structure. This file defines this structure and\nprovides ways to access, modify, and combine `AList`s.\n\n* `AList.keys` returns a list of keys of the alist.\n* `AList.membership` returns membership in the set of keys.\n* `AList.erase` removes a certain key.\n* `AList.insert` adds a key-value mapping to the list.\n* `AList.union` combines two association lists.\n\n## References\n\n* <https://en.wikipedia.org/wiki/Association_list>\n\n-/\n\n\nuniverse u v w\n\nopen List\n\nvariable {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v}\n\n/-- `AList \u03b2` is a key-value map stored as a `List` (i.e. a linked list).\n  It is a wrapper around certain `List` functions with the added constraint\n  that the list have unique keys. -/\nstructure AList (\u03b2 : \u03b1 \u2192 Type v) : Type max u v where\n  /-- The underlying `List` of an `AList` -/\n  entries : List (Sigma \u03b2)\n  /-- There are no duplicate keys in `entries` -/\n  nodupKeys : entries.NodupKeys\n#align alist AList\n\n/-- Given `l : List (sigma \u03b2)`, create a term of type `alist \u03b2` by removing\nentries with duplicate keys. -/\ndef List.toAList [DecidableEq \u03b1] {\u03b2 : \u03b1 \u2192 Type v} (l : List (Sigma \u03b2)) : AList \u03b2 where\n  entries := _\n  nodupKeys := nodupKeys_dedupKeys l\n#align list.to_alist List.toAList\n\nnamespace AList\n\n@[ext]\ntheorem ext : \u2200 {s t : AList \u03b2}, s.entries = t.entries \u2192 s = t\n  | \u27e8l\u2081, h\u2081\u27e9, \u27e8l\u2082, _\u27e9, H => by congr\n#align alist.ext AList.ext\n\ntheorem ext_iff {s t : AList \u03b2} : s = t \u2194 s.entries = t.entries :=\n  \u27e8congr_arg _, ext\u27e9\n#align alist.ext_iff AList.ext_iff\n\ninstance [DecidableEq \u03b1] [\u2200 a, DecidableEq (\u03b2 a)] : DecidableEq (AList \u03b2) := fun xs ys => by\n  rw [ext_iff]; infer_instance\n\n/-! ### keys -/\n\n\n/-- The list of keys of an association list. -/\ndef keys (s : AList \u03b2) : List \u03b1 :=\n  s.entries.keys\n#align alist.keys AList.keys\n\ntheorem keys_nodup (s : AList \u03b2) : s.keys.Nodup :=\n  s.nodupKeys\n#align alist.keys_nodup AList.keys_nodup\n\n/-! ### mem -/\n\n\n/-- The predicate `a \u2208 s` means that `s` has a value associated to the key `a`. -/\ninstance : Membership \u03b1 (AList \u03b2) :=\n  \u27e8fun a s => a \u2208 s.keys\u27e9\n\ntheorem mem_keys {a : \u03b1} {s : AList \u03b2} : a \u2208 s \u2194 a \u2208 s.keys :=\n  Iff.rfl\n#align alist.mem_keys AList.mem_keys\n\ntheorem mem_of_perm {a : \u03b1} {s\u2081 s\u2082 : AList \u03b2} (p : s\u2081.entries ~ s\u2082.entries) : a \u2208 s\u2081 \u2194 a \u2208 s\u2082 :=\n  (p.map Sigma.fst).mem_iff\n#align alist.mem_of_perm AList.mem_of_perm\n\n/-! ### empty -/\n\n\n/-- The empty association list. -/\ninstance : EmptyCollection (AList \u03b2) :=\n  \u27e8\u27e8[], nodupKeys_nil\u27e9\u27e9\n\ninstance : Inhabited (AList \u03b2) :=\n  \u27e8\u2205\u27e9\n\n@[simp]\ntheorem not_mem_empty (a : \u03b1) : a \u2209 (\u2205 : AList \u03b2) :=\n  not_mem_nil a\n#align alist.not_mem_empty AList.not_mem_empty\n\n@[simp]\ntheorem empty_entries : (\u2205 : AList \u03b2).entries = [] :=\n  rfl\n#align alist.empty_entries AList.empty_entries\n\n@[simp]\ntheorem keys_empty : (\u2205 : AList \u03b2).keys = [] :=\n  rfl\n#align alist.keys_empty AList.keys_empty\n\n/-! ### singleton -/\n\n\n/-- The singleton association list. -/\ndef singleton (a : \u03b1) (b : \u03b2 a) : AList \u03b2 :=\n  \u27e8[\u27e8a, b\u27e9], nodupKeys_singleton _\u27e9\n#align alist.singleton AList.singleton\n\n@[simp]\ntheorem singleton_entries (a : \u03b1) (b : \u03b2 a) : (singleton a b).entries = [Sigma.mk a b] :=\n  rfl\n#align alist.singleton_entries AList.singleton_entries\n\n@[simp]\ntheorem keys_singleton (a : \u03b1) (b : \u03b2 a) : (singleton a b).keys = [a] :=\n  rfl\n#align alist.keys_singleton AList.keys_singleton\n\n/-! ### lookup -/\n\n\nsection\n\nvariable [DecidableEq \u03b1]\n\n/-- Look up the value associated to a key in an association list. -/\ndef lookup (a : \u03b1) (s : AList \u03b2) : Option (\u03b2 a) :=\n  s.entries.dlookup a\n#align alist.lookup AList.lookup\n\n@[simp]\ntheorem lookup_empty (a) : lookup a (\u2205 : AList \u03b2) = none :=\n  rfl\n#align alist.lookup_empty AList.lookup_empty\n\ntheorem lookup_isSome {a : \u03b1} {s : AList \u03b2} : (s.lookup a).isSome \u2194 a \u2208 s :=\n  dlookup_isSome\n#align alist.lookup_is_some AList.lookup_isSome\n\ntheorem lookup_eq_none {a : \u03b1} {s : AList \u03b2} : lookup a s = none \u2194 a \u2209 s :=\n  dlookup_eq_none\n#align alist.lookup_eq_none AList.lookup_eq_none\n\ntheorem mem_lookup_iff {a : \u03b1} {b : \u03b2 a} {s : AList \u03b2} :\n    b \u2208 lookup a s \u2194 Sigma.mk a b \u2208 s.entries :=\n  mem_dlookup_iff s.nodupKeys\n#align alist.mem_lookup_iff AList.mem_lookup_iff\n\ntheorem perm_lookup {a : \u03b1} {s\u2081 s\u2082 : AList \u03b2} (p : s\u2081.entries ~ s\u2082.entries) :\n    s\u2081.lookup a = s\u2082.lookup a :=\n  perm_dlookup _ s\u2081.nodupKeys s\u2082.nodupKeys p\n#align alist.perm_lookup AList.perm_lookup\n\ninstance (a : \u03b1) (s : AList \u03b2) : Decidable (a \u2208 s) :=\n  decidable_of_iff _ lookup_isSome\n\n/-! ### replace -/\n\n\n/-- Replace a key with a given value in an association list.\n  If the key is not present it does nothing. -/\ndef replace (a : \u03b1) (b : \u03b2 a) (s : AList \u03b2) : AList \u03b2 :=\n  \u27e8kreplace a b s.entries, (kreplace_nodupKeys a b).2 s.nodupKeys\u27e9\n#align alist.replace AList.replace\n\n@[simp]\ntheorem keys_replace (a : \u03b1) (b : \u03b2 a) (s : AList \u03b2) : (replace a b s).keys = s.keys :=\n  keys_kreplace _ _ _\n#align alist.keys_replace AList.keys_replace\n\n@[simp]\ntheorem mem_replace {a a' : \u03b1} {b : \u03b2 a} {s : AList \u03b2} : a' \u2208 replace a b s \u2194 a' \u2208 s := by\n  rw [mem_keys, keys_replace, \u2190 mem_keys]\n#align alist.mem_replace AList.mem_replace\n\ntheorem perm_replace {a : \u03b1} {b : \u03b2 a} {s\u2081 s\u2082 : AList \u03b2} :\n    s\u2081.entries ~ s\u2082.entries \u2192 (replace a b s\u2081).entries ~ (replace a b s\u2082).entries :=\n  Perm.kreplace s\u2081.nodupKeys\n#align alist.perm_replace AList.perm_replace\n\nend\n\n/-- Fold a function over the key-value pairs in the map. -/\ndef foldl {\u03b4 : Type w} (f : \u03b4 \u2192 \u2200 a, \u03b2 a \u2192 \u03b4) (d : \u03b4) (m : AList \u03b2) : \u03b4 :=\n  m.entries.foldl (fun r a => f r a.1 a.2) d\n#align alist.foldl AList.foldl\n\n/-! ### erase -/\n\n\nsection\n\nvariable [DecidableEq \u03b1]\n\n/-- Erase a key from the map. If the key is not present, do nothing. -/\ndef erase (a : \u03b1) (s : AList \u03b2) : AList \u03b2 :=\n  \u27e8s.entries.kerase a, s.nodupKeys.kerase a\u27e9\n#align alist.erase AList.erase\n\n@[simp]\n\n\n@[simp]\ntheorem mem_erase {a a' : \u03b1} {s : AList \u03b2} : a' \u2208 erase a s \u2194 a' \u2260 a \u2227 a' \u2208 s := by\n  rw [mem_keys, keys_erase, s.keys_nodup.mem_erase_iff, \u2190 mem_keys]\n#align alist.mem_erase AList.mem_erase\n\ntheorem perm_erase {a : \u03b1} {s\u2081 s\u2082 : AList \u03b2} :\n    s\u2081.entries ~ s\u2082.entries \u2192 (erase a s\u2081).entries ~ (erase a s\u2082).entries :=\n  Perm.kerase s\u2081.nodupKeys\n#align alist.perm_erase AList.perm_erase\n\n@[simp]\ntheorem lookup_erase (a) (s : AList \u03b2) : lookup a (erase a s) = none :=\n  dlookup_kerase a s.nodupKeys\n#align alist.lookup_erase AList.lookup_erase\n\n@[simp]\ntheorem lookup_erase_ne {a a'} {s : AList \u03b2} (h : a \u2260 a') : lookup a (erase a' s) = lookup a s :=\n  dlookup_kerase_ne h\n#align alist.lookup_erase_ne AList.lookup_erase_ne\n\ntheorem erase_erase (a a' : \u03b1) (s : AList \u03b2) : (s.erase a).erase a' = (s.erase a').erase a :=\n  ext <| kerase_kerase\n#align alist.erase_erase AList.erase_erase\n\n/-! ### insert -/\n\n\n/-- Insert a key-value pair into an association list and erase any existing pair\n  with the same key. -/\ndef insert (a : \u03b1) (b : \u03b2 a) (s : AList \u03b2) : AList \u03b2 :=\n  \u27e8kinsert a b s.entries, kinsert_nodupKeys a b s.nodupKeys\u27e9\n#align alist.insert AList.insert\n\n@[simp]\ntheorem insert_entries {a} {b : \u03b2 a} {s : AList \u03b2} :\n    (insert a b s).entries = Sigma.mk a b :: kerase a s.entries :=\n  rfl\n#align alist.insert_entries AList.insert_entries\n\ntheorem insert_entries_of_neg {a} {b : \u03b2 a} {s : AList \u03b2} (h : a \u2209 s) :\n    (insert a b s).entries = \u27e8a, b\u27e9 :: s.entries := by rw [insert_entries, kerase_of_not_mem_keys h]\n#align alist.insert_entries_of_neg AList.insert_entries_of_neg\n\n-- Todo: rename to `insert_of_not_mem`.\ntheorem insert_of_neg {a} {b : \u03b2 a} {s : AList \u03b2} (h : a \u2209 s) :\n    insert a b s = \u27e8\u27e8a, b\u27e9 :: s.entries, nodupKeys_cons.2 \u27e8h, s.2\u27e9\u27e9 :=\n  ext <| insert_entries_of_neg h\n#align alist.insert_of_neg AList.insert_of_neg\n\n@[simp]\ntheorem insert_empty (a) (b : \u03b2 a) : insert a b \u2205 = singleton a b :=\n  rfl\n#align alist.insert_empty AList.insert_empty\n\n@[simp]\ntheorem mem_insert {a a'} {b' : \u03b2 a'} (s : AList \u03b2) : a \u2208 insert a' b' s \u2194 a = a' \u2228 a \u2208 s :=\n  mem_keys_kinsert\n#align alist.mem_insert AList.mem_insert\n\n@[simp]\ntheorem keys_insert {a} {b : \u03b2 a} (s : AList \u03b2) : (insert a b s).keys = a :: s.keys.erase a := by\n  simp [insert, keys, keys_kerase]\n#align alist.keys_insert AList.keys_insert\n\ntheorem perm_insert {a} {b : \u03b2 a} {s\u2081 s\u2082 : AList \u03b2} (p : s\u2081.entries ~ s\u2082.entries) :\n    (insert a b s\u2081).entries ~ (insert a b s\u2082).entries := by\n  simp only [insert_entries]; exact p.kinsert s\u2081.nodupKeys\n#align alist.perm_insert AList.perm_insert\n\n@[simp]\ntheorem lookup_insert {a} {b : \u03b2 a} (s : AList \u03b2) : lookup a (insert a b s) = some b := by\n  simp only [lookup, insert, dlookup_kinsert]\n#align alist.lookup_insert AList.lookup_insert\n\n@[simp]\ntheorem lookup_insert_ne {a a'} {b' : \u03b2 a'} {s : AList \u03b2} (h : a \u2260 a') :\n    lookup a (insert a' b' s) = lookup a s :=\n  dlookup_kinsert_ne h\n#align alist.lookup_insert_ne AList.lookup_insert_ne\n\n@[simp]\ntheorem lookup_to_alist {a} (s : List (Sigma \u03b2)) : lookup a s.toAList = s.dlookup a := by\n  rw [List.toAList, lookup, dlookup_dedupKeys]\n#align alist.lookup_to_alist AList.lookup_to_alist\n\n@[simp]\ntheorem insert_insert {a} {b b' : \u03b2 a} (s : AList \u03b2) : (s.insert a b).insert a b' = s.insert a b' :=\n  by\n  ext : 1 ; simp only [AList.insert_entries, List.kerase_cons_eq]\n#align alist.insert_insert AList.insert_insert\n\ntheorem insert_insert_of_ne {a a'} {b : \u03b2 a} {b' : \u03b2 a'} (s : AList \u03b2) (h : a \u2260 a') :\n    ((s.insert a b).insert a' b').entries ~ ((s.insert a' b').insert a b).entries := by\n  simp only [insert_entries]; rw [kerase_cons_ne, kerase_cons_ne, kerase_comm] <;>\n    [apply Perm.swap, exact h, exact h.symm]\n#align alist.insert_insert_of_ne AList.insert_insert_of_ne\n\n@[simp]\ntheorem insert_singleton_eq {a : \u03b1} {b b' : \u03b2 a} : insert a b (singleton a b') = singleton a b :=\n  ext <| by\n    simp only [AList.insert_entries, List.kerase_cons_eq, and_self_iff, AList.singleton_entries,\n      heq_iff_eq, eq_self_iff_true]\n#align alist.insert_singleton_eq AList.insert_singleton_eq\n\n@[simp]\ntheorem entries_toAList (xs : List (Sigma \u03b2)) : (List.toAList xs).entries = dedupKeys xs :=\n  rfl\n#align alist.entries_to_alist AList.entries_toAList\n\ntheorem toAList_cons (a : \u03b1) (b : \u03b2 a) (xs : List (Sigma \u03b2)) :\n    List.toAList (\u27e8a, b\u27e9 :: xs) = insert a b xs.toAList :=\n  rfl\n#align alist.to_alist_cons AList.toAList_cons\n\ntheorem mk_cons_eq_insert (c : Sigma \u03b2) (l : List (Sigma \u03b2)) (h : (c :: l).NodupKeys) :\n    (\u27e8c :: l, h\u27e9 : AList \u03b2) = insert c.1 c.2 \u27e8l, nodupKeys_of_nodupKeys_cons h\u27e9 := by\n  simpa [insert] using (kerase_of_not_mem_keys <| not_mem_keys_of_nodupKeys_cons h).symm\n#align alist.mk_cons_eq_insert AList.mk_cons_eq_insert\n\n/-- Recursion on an `alist`, using `insert`. Use as `induction l using alist.insert_rec`. -/\n@[elab_as_elim]\ndef insertRec {C : AList \u03b2 \u2192 Sort _} (H0 : C \u2205)\n    (IH : \u2200 (a : \u03b1) (b : \u03b2 a) (l : AList \u03b2), a \u2209 l \u2192 C l \u2192 C (l.insert a b)) :\n    \u2200 l : AList \u03b2, C l\n  | \u27e8[], _\u27e9 => H0\n  | \u27e8c :: l, h\u27e9 => by\n    rw [mk_cons_eq_insert]\n    refine' IH _ _ _ _ (insertRec H0 IH _)\n    exact not_mem_keys_of_nodupKeys_cons h\n#align alist.insert_rec AList.insertRec\n\n-- Test that the `induction` tactic works on `insert_rec`.\nexample (l : AList \u03b2) : True := by induction l using AList.insertRec <;> trivial\n\n@[simp]\ntheorem insertRec_empty {C : AList \u03b2 \u2192 Sort _} (H0 : C \u2205)\n    (IH : \u2200 (a : \u03b1) (b : \u03b2 a) (l : AList \u03b2), a \u2209 l \u2192 C l \u2192 C (l.insert a b)) :\n    @insertRec \u03b1 \u03b2 _ C H0 IH \u2205 = H0 := by\n  change @insertRec \u03b1 \u03b2 _ C H0 IH \u27e8[], _\u27e9 = H0\n  rw [insertRec]\n#align alist.insert_rec_empty AList.insertRec_empty\n\ntheorem insertRec_insert {C : AList \u03b2 \u2192 Sort _} (H0 : C \u2205)\n    (IH : \u2200 (a : \u03b1) (b : \u03b2 a) (l : AList \u03b2), a \u2209 l \u2192 C l \u2192 C (l.insert a b)) {c : Sigma \u03b2}\n    {l : AList \u03b2} (h : c.1 \u2209 l) :\n    @insertRec \u03b1 \u03b2 _ C H0 IH (l.insert c.1 c.2) = IH c.1 c.2 l h (@insertRec \u03b1 \u03b2 _ C H0 IH l) := by\n  cases' l with l hl\n  suffices\n    HEq (@insertRec \u03b1 \u03b2 _ C H0 IH \u27e8c :: l, nodupKeys_cons.2 \u27e8h, hl\u27e9\u27e9)\n      (IH c.1 c.2 \u27e8l, hl\u27e9 h (@insertRec \u03b1 \u03b2 _ C H0 IH \u27e8l, hl\u27e9))\n    by\n    cases c\n    apply eq_of_heq\n    convert this <;> rw [insert_of_neg h]\n  rw [insertRec]\n  apply cast_heq\n#align alist.insert_rec_insert AList.insertRec_insert\n\ntheorem insertRec_insert_mk {C : AList \u03b2 \u2192 Sort _} (H0 : C \u2205)\n    (IH : \u2200 (a : \u03b1) (b : \u03b2 a) (l : AList \u03b2), a \u2209 l \u2192 C l \u2192 C (l.insert a b)) {a : \u03b1} (b : \u03b2 a)\n    {l : AList \u03b2} (h : a \u2209 l) :\n    @insertRec \u03b1 \u03b2 _ C H0 IH (l.insert a b) = IH a b l h (@insertRec \u03b1 \u03b2 _ C H0 IH l) :=\n  @insertRec_insert \u03b1 \u03b2 _ C H0 IH \u27e8a, b\u27e9 l h\n#align alist.recursion_insert_mk AList.insertRec_insert_mk\n\n/-! ### extract -/\n\n\n/-- Erase a key from the map, and return the corresponding value, if found. -/\ndef extract (a : \u03b1) (s : AList \u03b2) : Option (\u03b2 a) \u00d7 AList \u03b2 :=\n  have : (kextract a s.entries).2.NodupKeys := by\n    rw [kextract_eq_dlookup_kerase]; exact s.nodupKeys.kerase _\n  match kextract a s.entries, this with\n  | (b, l), h => (b, \u27e8l, h\u27e9)\n#align alist.extract AList.extract\n\n@[simp]\ntheorem extract_eq_lookup_erase (a : \u03b1) (s : AList \u03b2) : extract a s = (lookup a s, erase a s) := by\n  simp [extract]; constructor <;> rfl\n#align alist.extract_eq_lookup_erase AList.extract_eq_lookup_erase\n\n/-! ### union -/\n\n\n/-- `s\u2081 \u222a s\u2082` is the key-based union of two association lists. It is\nleft-biased: if there exists an `a \u2208 s\u2081`, `lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2081`.\n-/\ndef union (s\u2081 s\u2082 : AList \u03b2) : AList \u03b2 :=\n  \u27e8s\u2081.entries.kunion s\u2082.entries, s\u2081.nodupKeys.kunion s\u2082.nodupKeys\u27e9\n#align alist.union AList.union\n\ninstance : Union (AList \u03b2) :=\n  \u27e8union\u27e9\n\n@[simp]\ntheorem union_entries {s\u2081 s\u2082 : AList \u03b2} : (s\u2081 \u222a s\u2082).entries = kunion s\u2081.entries s\u2082.entries :=\n  rfl\n#align alist.union_entries AList.union_entries\n\n@[simp]\ntheorem empty_union {s : AList \u03b2} : (\u2205 : AList \u03b2) \u222a s = s :=\n  ext rfl\n#align alist.empty_union AList.empty_union\n\n@[simp]\ntheorem union_empty {s : AList \u03b2} : s \u222a (\u2205 : AList \u03b2) = s :=\n  ext <| by simp\n#align alist.union_empty AList.union_empty\n\n@[simp]\ntheorem mem_union {a} {s\u2081 s\u2082 : AList \u03b2} : a \u2208 s\u2081 \u222a s\u2082 \u2194 a \u2208 s\u2081 \u2228 a \u2208 s\u2082 :=\n  mem_keys_kunion\n#align alist.mem_union AList.mem_union\n\ntheorem perm_union {s\u2081 s\u2082 s\u2083 s\u2084 : AList \u03b2} (p\u2081\u2082 : s\u2081.entries ~ s\u2082.entries)\n    (p\u2083\u2084 : s\u2083.entries ~ s\u2084.entries) : (s\u2081 \u222a s\u2083).entries ~ (s\u2082 \u222a s\u2084).entries := by\n  simp [p\u2081\u2082.kunion s\u2083.nodupKeys p\u2083\u2084]\n#align alist.perm_union AList.perm_union\n\ntheorem union_erase (a : \u03b1) (s\u2081 s\u2082 : AList \u03b2) : erase a (s\u2081 \u222a s\u2082) = erase a s\u2081 \u222a erase a s\u2082 :=\n  ext kunion_kerase.symm\n#align alist.union_erase AList.union_erase\n\n@[simp]\ntheorem lookup_union_left {a} {s\u2081 s\u2082 : AList \u03b2} : a \u2208 s\u2081 \u2192 lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2081 :=\n  dlookup_kunion_left\n#align alist.lookup_union_left AList.lookup_union_left\n\n@[simp]\ntheorem lookup_union_right {a} {s\u2081 s\u2082 : AList \u03b2} : a \u2209 s\u2081 \u2192 lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2082 :=\n  dlookup_kunion_right\n#align alist.lookup_union_right AList.lookup_union_right\n\n--Porting note: removing simp, LHS not in SNF, new theorem added instead.\ntheorem mem_lookup_union {a} {b : \u03b2 a} {s\u2081 s\u2082 : AList \u03b2} :\n    b \u2208 lookup a (s\u2081 \u222a s\u2082) \u2194 b \u2208 lookup a s\u2081 \u2228 a \u2209 s\u2081 \u2227 b \u2208 lookup a s\u2082 :=\n  mem_dlookup_kunion\n#align alist.mem_lookup_union AList.mem_lookup_union\n\n--Porting note: new theorem, version of `mem_lookup_union` with LHS in simp-normal form\n@[simp]\ntheorem lookup_union_eq_some {a} {b : \u03b2 a} {s\u2081 s\u2082 : AList \u03b2} :\n    lookup a (s\u2081 \u222a s\u2082) = some b \u2194 lookup a s\u2081 = some b \u2228 a \u2209 s\u2081 \u2227 lookup a s\u2082 = some b :=\n  mem_dlookup_kunion\n\ntheorem mem_lookup_union_middle {a} {b : \u03b2 a} {s\u2081 s\u2082 s\u2083 : AList \u03b2} :\n    b \u2208 lookup a (s\u2081 \u222a s\u2083) \u2192 a \u2209 s\u2082 \u2192 b \u2208 lookup a (s\u2081 \u222a s\u2082 \u222a s\u2083) :=\n  mem_dlookup_kunion_middle\n#align alist.mem_lookup_union_middle AList.mem_lookup_union_middle\n\ntheorem insert_union {a} {b : \u03b2 a} {s\u2081 s\u2082 : AList \u03b2} : insert a b (s\u2081 \u222a s\u2082) = insert a b s\u2081 \u222a s\u2082 :=\n  by ext; simp\n#align alist.insert_union AList.insert_union\n\ntheorem union_assoc {s\u2081 s\u2082 s\u2083 : AList \u03b2} : (s\u2081 \u222a s\u2082 \u222a s\u2083).entries ~ (s\u2081 \u222a (s\u2082 \u222a s\u2083)).entries :=\n  lookup_ext (AList.nodupKeys _) (AList.nodupKeys _)\n    (by simp [not_or, or_assoc, and_or_left, and_assoc])\n#align alist.union_assoc AList.union_assoc\n\nend\n\n/-! ### disjoint -/\n\n\n/-- Two associative lists are disjoint if they have no common keys. -/\ndef Disjoint (s\u2081 s\u2082 : AList \u03b2) : Prop :=\n  \u2200 k \u2208 s\u2081.keys, \u00ack \u2208 s\u2082.keys\n#align alist.disjoint AList.Disjoint\n\nvariable [DecidableEq \u03b1]\n\ntheorem union_comm_of_disjoint {s\u2081 s\u2082 : AList \u03b2} (h : Disjoint s\u2081 s\u2082) :\n    (s\u2081 \u222a s\u2082).entries ~ (s\u2082 \u222a s\u2081).entries :=\n  lookup_ext (AList.nodupKeys _) (AList.nodupKeys _)\n    (by\n      intros ; simp\n      constructor <;> intro h'\n      . cases' h' with h' h'\n        \u00b7 right\n          refine' \u27e8_, h'\u27e9\n          apply h\n          rw [keys, \u2190 List.dlookup_isSome, h']\n          exact rfl\n        \u00b7 left\n          rw [h'.2]\n      . cases' h' with h' h'\n        \u00b7 right\n          refine' \u27e8_, h'\u27e9\n          intro h''\n          apply h _ h''\n          rw [keys, \u2190 List.dlookup_isSome, h']\n          exact rfl\n        \u00b7 left\n          rw [h'.2])\n#align alist.union_comm_of_disjoint AList.union_comm_of_disjoint\n\nend AList\n", "meta": {"author": "leanprover-community", "repo": "mathlib4", "sha": "b9a0a30342ca06e9817e22dbe46e75fc7f435500", "save_path": "github-repos/lean/leanprover-community-mathlib4", "path": "github-repos/lean/leanprover-community-mathlib4/mathlib4-b9a0a30342ca06e9817e22dbe46e75fc7f435500/Mathlib/Data/List/AList.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4882833952958347, "lm_q2_score": 0.14033624589467186, "lm_q1q2_score": 0.06852385862852152}}
{"text": "/-\nCopyright (c) 2020 Jannis Limperg. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jannis Limperg\n\n! This file was ported from Lean 3 source module tactic.dependencies\n! leanprover-community/mathlib commit d6814c584384ddf2825ff038e868451a7c956f31\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Meta.RbMap\nimport Mathbin.Tactic.Core\n\n/-!\n# Tactics About Dependencies\n\nThis module provides tactics to compute dependencies and reverse dependencies of\nhypotheses. An expression `e` depends on a hypothesis `h` if `e` would not be\nvalid if `h` were removed from the context. For example, the expression\n`e := x > 0` depends on `x`. We say that `x` is a dependency of `e` and that `e`\nis a reverse dependency of `x`.\n\nIt is sometimes useful to consider *inclusive* dependency: `e` inclusively\ndepends on `h` iff `e` depends on `h` or `e = h` (so inclusive dependency is the\nreflexive closure of regular dependency).\n\nNote that the standard library does not use quite the same terminology:\n\n* `kdependencies`/`kdeps` from the standard library compute reverse\n  dependencies, not dependencies.\n* `kdepends_on` and functions derived from it ignore local definitions and\n  therefore compute a weaker dependency relation (see next section).\n\n## Local Definitions\n\nDetermining dependencies of hypotheses is usually straightforward: a hypothesis\n`r : R` depends on another hypothesis `d : D` if `d` occurs in `R`. The\nimplementation is more involved, however, in the presence of local definitions.\nConsider this context:\n\n```lean\nn m : \u2115\nk : \u2115 := m\no : \u2115 := k\nh : o > 0\n```\n\n`h` depends on `o`, `k` and `m`, but only the dependency on `o` is syntactically\nobvious. `kdepends_on` ignores this complication and claims that `h` does not\ndepend on `k` or `m`. We do not follow this example but process local\ndefinitions properly. This means that if the context contains a local\ndefinition, we need to compute the syntactic dependencies of `h`, then their\ndependencies, and so on.\n\n## Direct Dependencies\n\nIf you want to ignore local definitions while computing dependencies, this\nmodule also provides tactics to find the *direct* dependencies of a hypothesis.\nThese are the hypotheses that syntactically appear in the hypothesis's type (or\nvalue, if the hypothesis is a local definition).\n-/\n\n\nopen Native\n\nopen ExprSet (local_set_to_name_set)\n\nopen NameSet (local_list_to_name_set)\n\nnamespace Tactic\n\n/-! ### Direct Dependencies -/\n\n\n/-! #### Checking whether hypotheses directly depend on each other -/\n\n\n/-- `type_has_local_in_name_set h ns` returns true iff the type of `h` contains a\nlocal constant whose unique name appears in `ns`.\n-/\nunsafe def type_has_local_in_name_set (h : expr) (ns : name_set) : tactic Bool := do\n  let h_type \u2190 infer_type h\n  pure <| h_type ns\n#align tactic.type_has_local_in_name_set tactic.type_has_local_in_name_set\n\n/-- `type_has_local_in_set h hs` returns true iff the type of `h` contains any of\nthe local constants `hs`.\n-/\nunsafe def type_has_local_in_set (h : expr) (hs : expr_set) : tactic Bool :=\n  type_has_local_in_name_set h <| local_set_to_name_set hs\n#align tactic.type_has_local_in_set tactic.type_has_local_in_set\n\n/-- `type_has_local_in h hs` returns true iff the type of `h` contains any of the\nlocal constants `hs`.\n-/\nunsafe def type_has_local_in (h : expr) (hs : List expr) : tactic Bool :=\n  type_has_local_in_name_set h <| local_list_to_name_set hs\n#align tactic.type_has_local_in tactic.type_has_local_in\n\n/-- `local_def_value_has_local_in_name_set h ns` returns true iff `h` is a local\ndefinition whose value contains a local constant whose unique name appears in\n`ns`.\n-/\nunsafe def local_def_value_has_local_in_name_set (h : expr) (ns : name_set) : tactic Bool := do\n  let some h_val \u2190 try_core <| local_def_value h |\n    pure false\n  pure <| h_val ns\n#align tactic.local_def_value_has_local_in_name_set tactic.local_def_value_has_local_in_name_set\n\n/-- `local_def_value_has_local_in_set h hs` returns true iff `h` is a local\ndefinition whose value contains any of the local constants `hs`.\n-/\nunsafe def local_def_value_has_local_in_set (h : expr) (hs : expr_set) : tactic Bool :=\n  local_def_value_has_local_in_name_set h <| local_set_to_name_set hs\n#align tactic.local_def_value_has_local_in_set tactic.local_def_value_has_local_in_set\n\n/-- `local_def_value_has_local_in h hs` returns true iff `h` is a local definition\nwhose value contains any of the local constants `hs`.\n-/\nunsafe def local_def_value_has_local_in (h : expr) (hs : List expr) : tactic Bool :=\n  local_def_value_has_local_in_name_set h <| local_list_to_name_set hs\n#align tactic.local_def_value_has_local_in tactic.local_def_value_has_local_in\n\n/-- `hyp_directly_depends_on_local_name_set h ns` is true iff the hypothesis `h`\ndirectly depends on a hypothesis whose unique name appears in `ns`.\n-/\nunsafe def hyp_directly_depends_on_local_name_set (h : expr) (ns : name_set) : tactic Bool :=\n  List.orM [type_has_local_in_name_set h ns, local_def_value_has_local_in_name_set h ns]\n#align tactic.hyp_directly_depends_on_local_name_set tactic.hyp_directly_depends_on_local_name_set\n\n/-- `hyp_directly_depends_on_local_set h hs` is true iff the hypothesis `h` directly\ndepends on any of the hypotheses `hs`.\n-/\nunsafe def hyp_directly_depends_on_local_set (h : expr) (hs : expr_set) : tactic Bool :=\n  hyp_directly_depends_on_local_name_set h <| local_set_to_name_set hs\n#align tactic.hyp_directly_depends_on_local_set tactic.hyp_directly_depends_on_local_set\n\n/-- `hyp_directly_depends_on_locals h hs` is true iff the hypothesis `h` directly\ndepends on any of the hypotheses `hs`.\n-/\nunsafe def hyp_directly_depends_on_locals (h : expr) (hs : List expr) : tactic Bool :=\n  hyp_directly_depends_on_local_name_set h <| local_list_to_name_set hs\n#align tactic.hyp_directly_depends_on_locals tactic.hyp_directly_depends_on_locals\n\n/-- `hyp_directly_depends_on_local_name_set_inclusive h ns` is true iff the\nhypothesis `h` directly depends on a hypothesis whose unique name appears in\n`ns` or `h`'s name appears in `ns`.\n-/\nunsafe def hyp_directly_depends_on_local_name_set_inclusive (h : expr) (ns : name_set) :\n    tactic Bool :=\n  List.orM [pure <| ns.contains h.local_uniq_name, hyp_directly_depends_on_local_name_set h ns]\n#align tactic.hyp_directly_depends_on_local_name_set_inclusive tactic.hyp_directly_depends_on_local_name_set_inclusive\n\n/-- `hyp_directly_depends_on_local_set_inclusive h ns` is true iff the hypothesis `h`\ndirectly depends on any of the hypotheses `hs` or `h` appears in `hs`.\n-/\nunsafe def hyp_directly_depends_on_local_set_inclusive (h : expr) (hs : expr_set) : tactic Bool :=\n  hyp_directly_depends_on_local_name_set_inclusive h <| local_set_to_name_set hs\n#align tactic.hyp_directly_depends_on_local_set_inclusive tactic.hyp_directly_depends_on_local_set_inclusive\n\n/-- `hyp_directly_depends_on_locals_inclusive h ns` is true iff the hypothesis `h`\ndirectly depends on any of the hypotheses `hs` or `h` appears in `hs`.\n-/\nunsafe def hyp_directly_depends_on_locals_inclusive (h : expr) (hs : List expr) : tactic Bool :=\n  hyp_directly_depends_on_local_name_set_inclusive h <| local_list_to_name_set hs\n#align tactic.hyp_directly_depends_on_locals_inclusive tactic.hyp_directly_depends_on_locals_inclusive\n\n/-! #### Computing the direct dependencies of a hypothesis -/\n\n\n/-- `direct_dependency_set_of_hyp h` is the set of hypotheses that the hypothesis\n`h` directly depends on. These are the hypotheses that appear in `h`'s type or\nvalue (if `h` is a local definition).\n-/\nunsafe def direct_dependency_set_of_hyp (h : expr) : tactic expr_set := do\n  let t \u2190 infer_type h\n  let deps := t.list_local_consts'\n  let some val \u2190 try_core <| local_def_value h |\n    pure deps\n  let deps := deps.union val.list_local_consts'\n  pure deps\n#align tactic.direct_dependency_set_of_hyp tactic.direct_dependency_set_of_hyp\n\n/-- `direct_dependency_name_set_of_hyp h` is the set of unique names of hypotheses\nthat the hypothesis `h` directly depends on. These are the hypotheses that\nappear in `h`'s type or value (if `h` is a local definition).\n-/\nunsafe def direct_dependency_name_set_of_hyp (h : expr) : tactic name_set :=\n  local_set_to_name_set <$> direct_dependency_set_of_hyp h\n#align tactic.direct_dependency_name_set_of_hyp tactic.direct_dependency_name_set_of_hyp\n\n/-- `direct_dependencies_of_hyp h` is the list of hypotheses that the hypothesis `h`\ndirectly depends on. These are the hypotheses that appear in `h`'s type or value\n(if `h` is a local definition). The dependencies are returned in no particular\norder.\n-/\nunsafe def direct_dependencies_of_hyp (h : expr) : tactic (List expr) :=\n  rb_set.to_list <$> direct_dependency_set_of_hyp h\n#align tactic.direct_dependencies_of_hyp tactic.direct_dependencies_of_hyp\n\n/-- `direct_dependency_set_of_hyp_inclusive h` is the set of hypotheses that the\nhypothesis `h` directly depends on, plus `h` itself.\n-/\nunsafe def direct_dependency_set_of_hyp_inclusive (h : expr) : tactic expr_set := do\n  let deps \u2190 direct_dependency_set_of_hyp h\n  pure <| deps h\n#align tactic.direct_dependency_set_of_hyp_inclusive tactic.direct_dependency_set_of_hyp_inclusive\n\n/-- `direct_dependency_name_set_of_hyp_inclusive h` is the set of unique names of\nhypotheses that the hypothesis `h` directly depends on, plus `h` itself.\n-/\nunsafe def direct_dependency_name_set_of_hyp_inclusive (h : expr) : tactic name_set :=\n  local_set_to_name_set <$> direct_dependency_set_of_hyp_inclusive h\n#align tactic.direct_dependency_name_set_of_hyp_inclusive tactic.direct_dependency_name_set_of_hyp_inclusive\n\n/-- `direct_dependencies_of_hyp_inclusive h` is the list of hypotheses that the\nhypothesis `h` directly depends on, plus `h` itself. The dependencies are\nreturned in no particular order.\n-/\nunsafe def direct_dependencies_of_hyp_inclusive (h : expr) : tactic (List expr) :=\n  rb_set.to_list <$> direct_dependency_set_of_hyp_inclusive h\n#align tactic.direct_dependencies_of_hyp_inclusive tactic.direct_dependencies_of_hyp_inclusive\n\n/-! ### Indirect/Transitive Dependencies -/\n\n\n/-! #### Checking whether hypotheses depend on each other -/\n\n\n/-- `hyp_depends_on_local_name_set' cache h ns` is true iff `h` depends on any of\nthe hypotheses whose unique names appear in `ns`. `cache` must be a set of\nhypotheses known *not* to depend (even indirectly) on any of the `ns`. This is\na performance optimisation, so you can give an empty cache. The tactic also\nreturns an expanded cache with hypotheses which the tactic has encountered.\n\nYou probably want to use `tactic.hyp_depends_on_local_name_set` or\n`tactic.hyps_depend_on_local_name_set` instead of this tactic.\n-/\nunsafe def hyp_depends_on_local_name_set' : expr_set \u2192 expr \u2192 name_set \u2192 tactic (Bool \u00d7 expr_set) :=\n  fun cache h ns => do\n  let ff \u2190 pure <| cache.contains h |\n    pure (false, cache)\n  let direct_deps \u2190 direct_dependency_set_of_hyp h\n  let has_dep := direct_deps.fold false fun d b => b || ns.contains d.local_uniq_name\n  let ff \u2190 pure has_dep |\n    pure (true, cache)\n  let (has_dep, cache) \u2190\n    direct_deps.mfold (false, cache) fun d \u27e8b, cache\u27e9 =>\n        if b then pure (true, cache) else hyp_depends_on_local_name_set' cache d ns\n  if has_dep then pure (tt, cache) else pure (ff, cache h)\n#align tactic.hyp_depends_on_local_name_set' tactic.hyp_depends_on_local_name_set'\n\n/-- `hyp_depends_on_local_name_set h ns` is true iff the hypothesis `h` depends on\nany of the hypotheses whose unique names appear in `ns`. If you need to check\ndependencies of multiple hypotheses, use `tactic.hyps_depend_on_local_name_set`.\n-/\nunsafe def hyp_depends_on_local_name_set (h : expr) (ns : name_set) : tactic Bool := do\n  let ctx_has_local_def \u2190 context_upto_hyp_has_local_def h\n  if ctx_has_local_def then Prod.fst <$> hyp_depends_on_local_name_set' mk_expr_set h ns\n    else hyp_directly_depends_on_local_name_set h ns\n#align tactic.hyp_depends_on_local_name_set tactic.hyp_depends_on_local_name_set\n\n/-- `hyp_depends_on_local_set h hs` is true iff the hypothesis `h` depends on\nany of the hypotheses `hs`. If you need to check dependencies of multiple\nhypotheses, use `tactic.hyps_depend_on_local_set`.\n-/\nunsafe def hyp_depends_on_local_set (h : expr) (hs : expr_set) : tactic Bool :=\n  hyp_depends_on_local_name_set h <| local_set_to_name_set hs\n#align tactic.hyp_depends_on_local_set tactic.hyp_depends_on_local_set\n\n/-- `hyp_depends_on_locals h hs` is true iff the hypothesis `h` depends on any of\nthe hypotheses `hs`. If you need to check dependencies of multiple hypotheses,\nuse `tactic.hyps_depend_on_locals`.\n-/\nunsafe def hyp_depends_on_locals (h : expr) (hs : List expr) : tactic Bool :=\n  hyp_depends_on_local_name_set h <| local_list_to_name_set hs\n#align tactic.hyp_depends_on_locals tactic.hyp_depends_on_locals\n\n/-- `hyps_depend_on_local_name_set hs ns` returns, for each `h \u2208 hs`, whether `h`\ndepends on a hypothesis whose unique name appears in `ns`. This is the same as\n(but more efficient than) calling `tactic.hyp_depends_on_local_name_set` for\nevery `h \u2208 hs`.\n-/\nunsafe def hyps_depend_on_local_name_set (hs : List expr) (ns : name_set) : tactic (List Bool) := do\n  let ctx_has_local \u2190 context_has_local_def\n  if ctx_has_local then\n      let go : expr \u2192 List Bool \u00d7 expr_set \u2192 tactic (List Bool \u00d7 expr_set) := fun h \u27e8deps, cache\u27e9 =>\n        do\n        let (h_dep, cache) \u2190 hyp_depends_on_local_name_set' cache h ns\n        pure (h_dep :: deps, cache)\n      Prod.fst <$> hs go ([], mk_expr_map)\n    else hs fun h => hyp_directly_depends_on_local_name_set h ns\n#align tactic.hyps_depend_on_local_name_set tactic.hyps_depend_on_local_name_set\n\n/-- `hyps_depend_on_local_set hs is` returns, for each `h \u2208 hs`, whether `h` depends\non any of the hypotheses `is`. This is the same as (but more efficient than)\ncalling `tactic.hyp_depends_on_local_set` for every `h \u2208 hs`.\n-/\nunsafe def hyps_depend_on_local_set (hs : List expr) (is : expr_set) : tactic (List Bool) :=\n  hyps_depend_on_local_name_set hs <| local_set_to_name_set is\n#align tactic.hyps_depend_on_local_set tactic.hyps_depend_on_local_set\n\n/-- `hyps_depend_on_locals hs is` returns, for each `h \u2208 hs`, whether `h` depends\non any of the hypotheses `is`. This is the same as (but more efficient than)\ncalling `tactic.hyp_depends_on_locals` for every `h \u2208 hs`.\n-/\nunsafe def hyps_depend_on_locals (hs is : List expr) : tactic (List Bool) :=\n  hyps_depend_on_local_name_set hs <| local_list_to_name_set is\n#align tactic.hyps_depend_on_locals tactic.hyps_depend_on_locals\n\n/-- `hyp_depends_on_local_name_set_inclusive' cache h ns` is true iff the hypothesis\n`h` inclusively depends on a hypothesis whose unique name appears in `ns`.\n`cache` must be a set of hypotheses known *not* to depend (even indirectly) on\nany of the `ns`. This is a performance optimisation, so you can give an empty\ncache. The tactic also returns an expanded cache with hypotheses which the\ntactic has encountered. Note that the cache records exclusive, not inclusive\ndependencies.\n\nYou probably want to use `tactic.hyp_depends_on_local_name_set_inclusive` or\n`tactic.hyps_depend_on_local_name_set_inclusive` instead of this tactic.\n-/\nunsafe def hyp_depends_on_local_name_set_inclusive' (cache : expr_set) (h : expr) (ns : name_set) :\n    tactic (Bool \u00d7 expr_set) :=\n  if ns.contains h.local_uniq_name then pure (true, cache)\n  else hyp_depends_on_local_name_set' cache h ns\n#align tactic.hyp_depends_on_local_name_set_inclusive' tactic.hyp_depends_on_local_name_set_inclusive'\n\n/-- `hyp_depends_on_local_name_set_inclusive h ns` is true iff the hypothesis `h`\ninclusively depends on any of the hypotheses whose unique names appear in `ns`.\nIf you need to check the dependencies of multiple hypotheses, use\n`tactic.hyps_depend_on_local_name_set_inclusive`.\n-/\nunsafe def hyp_depends_on_local_name_set_inclusive (h : expr) (ns : name_set) : tactic Bool :=\n  List.orM [pure <| ns.contains h.local_uniq_name, hyp_depends_on_local_name_set h ns]\n#align tactic.hyp_depends_on_local_name_set_inclusive tactic.hyp_depends_on_local_name_set_inclusive\n\n/-- `hyp_depends_on_local_set_inclusive h hs` is true iff the hypothesis `h`\ninclusively depends on any of the hypotheses `hs`. If you need to check\ndependencies of multiple hypotheses, use\n`tactic.hyps_depend_on_local_set_inclusive`.\n-/\nunsafe def hyp_depends_on_local_set_inclusive (h : expr) (hs : expr_set) : tactic Bool :=\n  hyp_depends_on_local_name_set_inclusive h <| local_set_to_name_set hs\n#align tactic.hyp_depends_on_local_set_inclusive tactic.hyp_depends_on_local_set_inclusive\n\n/-- `hyp_depends_on_locals_inclusive h hs` is true iff the hypothesis `h`\ninclusively depends on any of the hypotheses `hs`. If you need to check\ndependencies of multiple hypotheses, use\n`tactic.hyps_depend_on_locals_inclusive`.\n-/\nunsafe def hyp_depends_on_locals_inclusive (h : expr) (hs : List expr) : tactic Bool :=\n  hyp_depends_on_local_name_set_inclusive h <| local_list_to_name_set hs\n#align tactic.hyp_depends_on_locals_inclusive tactic.hyp_depends_on_locals_inclusive\n\n/-- `hyps_depend_on_local_name_set_inclusive hs ns` returns, for each `h \u2208 hs`,\nwhether `h` inclusively depends on a hypothesis whose unique name appears in\n`ns`. This is the same as (but more efficient than) calling\n`tactic.hyp_depends_on_local_name_set_inclusive` for every `h \u2208 hs`.\n-/\nunsafe def hyps_depend_on_local_name_set_inclusive (hs : List expr) (ns : name_set) :\n    tactic (List Bool) := do\n  let ctx_has_local \u2190 context_has_local_def\n  if ctx_has_local then\n      let go : expr \u2192 List Bool \u00d7 expr_set \u2192 tactic (List Bool \u00d7 expr_set) := fun h \u27e8deps, cache\u27e9 =>\n        do\n        let (h_dep, cache) \u2190 hyp_depends_on_local_name_set_inclusive' cache h ns\n        pure (h_dep :: deps, cache)\n      Prod.fst <$> hs go ([], mk_expr_map)\n    else hs fun h => hyp_directly_depends_on_local_name_set_inclusive h ns\n#align tactic.hyps_depend_on_local_name_set_inclusive tactic.hyps_depend_on_local_name_set_inclusive\n\n/-- `hyps_depend_on_local_set_inclusive hs is` returns, for each `h \u2208 hs`, whether\n`h` depends inclusively on any of the hypotheses `is`. This is the same as\n(but more efficient than) calling `tactic.hyp_depends_on_local_set_inclusive`\nfor every `h \u2208 hs`.\n-/\nunsafe def hyps_depend_on_local_set_inclusive (hs : List expr) (is : expr_set) :\n    tactic (List Bool) :=\n  hyps_depend_on_local_name_set_inclusive hs <| local_set_to_name_set is\n#align tactic.hyps_depend_on_local_set_inclusive tactic.hyps_depend_on_local_set_inclusive\n\n/-- `hyps_depend_on_locals_inclusive hs is` returns, for each `h \u2208 hs`, whether `h`\ndepends inclusively on any of the hypotheses `is`. This is the same as (but more\nefficient than) calling `tactic.hyp_depends_on_locals_inclusive` for every\n`h \u2208 hs`.\n-/\nunsafe def hyps_depend_on_locals_inclusive (hs is : List expr) : tactic (List Bool) :=\n  hyps_depend_on_local_name_set_inclusive hs <| local_list_to_name_set is\n#align tactic.hyps_depend_on_locals_inclusive tactic.hyps_depend_on_locals_inclusive\n\n/-! #### Computing the dependencies of a hypothesis -/\n\n\n/-- `dependency_set_of_hyp' cache h` is the set of dependencies of the hypothesis\n`h`. `cache` is a map from hypotheses to all their dependencies (including\nindirect dependencies). This is a performance optimisation, so you can give an\nempty cache. The tactic also returns an expanded cache with hypotheses which\nthe tactic has encountered.\n\nYou probably want to use `tactic.dependency_set_of_hyp` or\n`tactic.dependency_sets_of_hyps` instead of this tactic.\n-/\nunsafe def dependency_set_of_hyp' :\n    expr_map expr_set \u2192 expr \u2192 tactic (expr_set \u00d7 expr_map expr_set) := fun cache h => do\n  match cache h with\n    | some deps => pure (deps, cache)\n    | none => do\n      let direct_deps \u2190 direct_dependency_set_of_hyp h\n      let (deps, cache) \u2190\n        direct_deps (direct_deps, cache) fun h' \u27e8deps, cache\u27e9 => do\n            let (deps', cache) \u2190 dependency_set_of_hyp' cache h'\n            pure (deps deps', cache)\n      pure (deps, cache h deps)\n#align tactic.dependency_set_of_hyp' tactic.dependency_set_of_hyp'\n\n/-- `dependency_set_of_hyp h` is the set of dependencies of the hypothesis `h`. If\nyou need the dependencies of multiple hypotheses, use\n`tactic.dependency_sets_of_hyps`.\n-/\nunsafe def dependency_set_of_hyp (h : expr) : tactic expr_set := do\n  let ctx_has_local \u2190 context_upto_hyp_has_local_def h\n  if ctx_has_local then Prod.fst <$> dependency_set_of_hyp' mk_expr_map h\n    else direct_dependency_set_of_hyp h\n#align tactic.dependency_set_of_hyp tactic.dependency_set_of_hyp\n\n/-- `dependency_name_set_of_hyp h` is the set of unique names of the dependencies of\nthe hypothesis `h`. If you need the dependencies of multiple hypotheses, use\n`tactic.dependency_name_sets_of_hyps`.\n-/\nunsafe def dependency_name_set_of_hyp (h : expr) : tactic name_set :=\n  local_set_to_name_set <$> dependency_set_of_hyp h\n#align tactic.dependency_name_set_of_hyp tactic.dependency_name_set_of_hyp\n\n/-- `dependencies_of_hyp h` is the list of dependencies of the hypothesis `h`.\nThe dependencies are returned in no particular order. If you need the\ndependencies of multiple hypotheses, use `tactic.dependencies_of_hyps`.\n-/\nunsafe def dependencies_of_hyp (h : expr) : tactic (List expr) :=\n  rb_set.to_list <$> dependency_set_of_hyp h\n#align tactic.dependencies_of_hyp tactic.dependencies_of_hyp\n\n/-- `dependency_sets_of_hyps hs` returns, for each `h \u2208 hs`, the set of dependencies\nof `h`. This is the same as (but more performant than) using\n`tactic.dependency_set_of_hyp` on every `h \u2208 hs`.\n-/\nunsafe def dependency_sets_of_hyps (hs : List expr) : tactic (List expr_set) := do\n  let ctx_has_def \u2190 context_has_local_def\n  if ctx_has_def then\n      let go :\n        expr \u2192 List expr_set \u00d7 expr_map expr_set \u2192 tactic (List expr_set \u00d7 expr_map expr_set) := do\n        fun h \u27e8deps, cache\u27e9 => do\n          let (h_deps, cache) \u2190 dependency_set_of_hyp' cache h\n          pure (h_deps :: deps, cache)\n      Prod.fst <$> hs go ([], mk_expr_map)\n    else hs direct_dependency_set_of_hyp\n#align tactic.dependency_sets_of_hyps tactic.dependency_sets_of_hyps\n\n/-- `dependency_name_sets_of_hyps hs` returns, for each `h \u2208 hs`, the set of unique\nnames of the dependencies of `h`. This is the same as (but more performant than)\nusing `tactic.dependency_name_set_of_hyp` on every `h \u2208 hs`.\n-/\nunsafe def dependency_name_sets_of_hyps (hs : List expr) : tactic (List name_set) :=\n  List.map local_set_to_name_set <$> dependency_sets_of_hyps hs\n#align tactic.dependency_name_sets_of_hyps tactic.dependency_name_sets_of_hyps\n\n/-- `dependencies_of_hyps hs` returns, for each `h \u2208 hs`, the dependencies of `h`.\nThe dependencies appear in no particular order in the returned lists. This is\nthe same as (but more performant than) using `tactic.dependencies_of_hyp` on\nevery `h \u2208 hs`.\n-/\nunsafe def dependencies_of_hyps (hs : List expr) : tactic (List (List expr)) :=\n  List.map rb_set.to_list <$> dependency_sets_of_hyps hs\n#align tactic.dependencies_of_hyps tactic.dependencies_of_hyps\n\n/-- `dependency_set_of_hyp_inclusive' cache h` is the set of dependencies of the\nhypothesis `h`, plus `h` itself. `cache` is a map from hypotheses to all their\ndependencies (including indirect dependencies). This is a performance\noptimisation, so you can give an empty cache. The tactic also returns an\nexpanded cache with hypotheses which the tactic has encountered. Note that the\ncache records exclusive, not inclusive dependencies.\n\nYou probably want to use `tactic.dependency_set_of_hyp_inclusive` or\n`tactic.dependency_sets_of_hyps_inclusive` instead of this tactic.\n-/\nunsafe def dependency_set_of_hyp_inclusive' (cache : expr_map expr_set) (h : expr) :\n    tactic (expr_set \u00d7 expr_map expr_set) := do\n  let (deps, cache) \u2190 dependency_set_of_hyp' cache h\n  pure (deps h, cache)\n#align tactic.dependency_set_of_hyp_inclusive' tactic.dependency_set_of_hyp_inclusive'\n\n/-- `dependency_set_of_hyp_inclusive h` is the set of dependencies of the hypothesis\n`h`, plus `h` itself. If you need the dependencies of multiple hypotheses, use\n`tactic.dependency_sets_of_hyps_inclusive`.\n-/\nunsafe def dependency_set_of_hyp_inclusive (h : expr) : tactic expr_set := do\n  let deps \u2190 dependency_set_of_hyp h\n  pure <| deps h\n#align tactic.dependency_set_of_hyp_inclusive tactic.dependency_set_of_hyp_inclusive\n\n/-- `dependency_name_set_of_hyp_inclusive h` is the set of unique names of the\ndependencies of the hypothesis `h`, plus the unique name of `h` itself. If you\nneed the dependencies of multiple hypotheses, use\n`tactic.dependency_name_sets_of_hyps_inclusive`.\n-/\nunsafe def dependency_name_set_of_hyp_inclusive (h : expr) : tactic name_set :=\n  local_set_to_name_set <$> dependency_set_of_hyp_inclusive h\n#align tactic.dependency_name_set_of_hyp_inclusive tactic.dependency_name_set_of_hyp_inclusive\n\n/-- `dependencies_of_hyp_inclusive h` is the list of dependencies of the hypothesis\n`h`, plus `h` itself. The dependencies are returned in no particular order. If\nyou need the dependencies of multiple hypotheses, use\n`tactic.dependencies_of_hyps_inclusive`.\n-/\nunsafe def dependencies_of_hyp_inclusive (h : expr) : tactic (List expr) :=\n  rb_set.to_list <$> dependency_set_of_hyp_inclusive h\n#align tactic.dependencies_of_hyp_inclusive tactic.dependencies_of_hyp_inclusive\n\n/-- `dependency_sets_of_hyps_inclusive hs` returns, for each `h \u2208 hs`, the\ndependencies of `h`, plus `h` itself. This is the same as (but more performant\nthan) using `tactic.dependency_set_of_hyp_inclusive` on every `h \u2208 hs`.\n-/\nunsafe def dependency_sets_of_hyps_inclusive (hs : List expr) : tactic (List expr_set) := do\n  let ctx_has_def \u2190 context_has_local_def\n  if ctx_has_def then\n      let go :\n        expr \u2192 List expr_set \u00d7 expr_map expr_set \u2192 tactic (List expr_set \u00d7 expr_map expr_set) :=\n        fun h \u27e8deps, cache\u27e9 => do\n        let (h_deps, cache) \u2190 dependency_set_of_hyp_inclusive' cache h\n        pure (h_deps :: deps, cache)\n      Prod.fst <$> hs go ([], mk_expr_map)\n    else hs direct_dependency_set_of_hyp_inclusive\n#align tactic.dependency_sets_of_hyps_inclusive tactic.dependency_sets_of_hyps_inclusive\n\n/-- `dependency_name_sets_of_hyps_inclusive hs` returns, for each `h \u2208 hs`, the\nunique names of the dependencies of `h`, plus the unique name of `h` itself.\nThis is the same as (but more performant than) using\n`tactic.dependency_name_set_of_hyp_inclusive` on every `h \u2208 hs`.\n-/\nunsafe def dependency_name_sets_of_hyps_inclusive (hs : List expr) : tactic (List name_set) :=\n  List.map local_set_to_name_set <$> dependency_sets_of_hyps_inclusive hs\n#align tactic.dependency_name_sets_of_hyps_inclusive tactic.dependency_name_sets_of_hyps_inclusive\n\n/-- `dependencies_of_hyps_inclusive hs` returns, for each `h \u2208 hs`, the dependencies\nof `h`, plus `h` itself. The dependencies appear in no particular order in the\nreturned lists. This is the same as (but more performant than) using\n`tactic.dependencies_of_hyp_inclusive` on every `h \u2208 hs`.\n-/\nunsafe def dependencies_of_hyps_inclusive (hs : List expr) : tactic (List (List expr)) :=\n  List.map rb_set.to_list <$> dependency_sets_of_hyps_inclusive hs\n#align tactic.dependencies_of_hyps_inclusive tactic.dependencies_of_hyps_inclusive\n\n/-! #### Computing the reverse dependencies of a hypothesis -/\n\n\nprivate unsafe def reverse_dependencies_of_hyp_name_set_aux (hs : name_set) :\n    List expr \u2192 List expr \u2192 name_set \u2192 tactic (List expr)\n  | [], revdeps, _ => pure revdeps.reverse\n  | H :: Hs, revdeps, ns => do\n    let H_uname := H.local_uniq_name\n    let H_is_revdep \u2190\n      List.andM [pure <| \u00achs.contains H_uname, hyp_directly_depends_on_local_name_set H ns]\n    if H_is_revdep then reverse_dependencies_of_hyp_name_set_aux Hs (H :: revdeps) (ns H_uname)\n      else reverse_dependencies_of_hyp_name_set_aux Hs revdeps ns\n#align tactic.reverse_dependencies_of_hyp_name_set_aux tactic.reverse_dependencies_of_hyp_name_set_aux\n\n/-- `reverse_dependencies_of_hyp_name_set hs` is the list of reverse dependencies of\nthe hypotheses whose unique names appear in `hs`, excluding the `hs` themselves.\nThe reverse dependencies are returned in the order in which they appear in the\ncontext.\n-/\nunsafe def reverse_dependencies_of_hyp_name_set (hs : name_set) : tactic (List expr) := do\n  let ctx \u2190 local_context\n  let ctx := ctx.after\u2093 fun h => hs.contains h.local_uniq_name\n  reverse_dependencies_of_hyp_name_set_aux hs ctx [] hs\n#align tactic.reverse_dependencies_of_hyp_name_set tactic.reverse_dependencies_of_hyp_name_set\n\n/-- `reverse_dependencies_of_hyp_set hs` is the list of reverse dependencies of the\nhypotheses `hs`, excluding the `hs` themselves. The reverse dependencies are\nreturned in the order in which they appear in the context.\n-/\nunsafe def reverse_dependencies_of_hyp_set (hs : expr_set) : tactic (List expr) :=\n  reverse_dependencies_of_hyp_name_set <| local_set_to_name_set hs\n#align tactic.reverse_dependencies_of_hyp_set tactic.reverse_dependencies_of_hyp_set\n\n/-- `reverse_dependencies_of_hyps hs` is the list of reverse dependencies of the\nhypotheses `hs`, excluding the `hs` themselves. The reverse dependencies are\nreturned in the order in which they appear in the context.\n-/\nunsafe def reverse_dependencies_of_hyps (hs : List expr) : tactic (List expr) :=\n  reverse_dependencies_of_hyp_name_set <| local_list_to_name_set hs\n#align tactic.reverse_dependencies_of_hyps tactic.reverse_dependencies_of_hyps\n\nprivate unsafe def reverse_dependencies_of_hyp_name_set_inclusive_aux :\n    List expr \u2192 List expr \u2192 name_set \u2192 tactic (List expr)\n  | [], revdeps, _ => pure revdeps.reverse\n  | H :: Hs, revdeps, ns => do\n    let H_uname := H.local_uniq_name\n    let H_is_revdep \u2190\n      List.orM [pure <| ns.contains H.local_uniq_name, hyp_directly_depends_on_local_name_set H ns]\n    if H_is_revdep then\n        reverse_dependencies_of_hyp_name_set_inclusive_aux Hs (H :: revdeps) (ns H_uname)\n      else reverse_dependencies_of_hyp_name_set_inclusive_aux Hs revdeps ns\n#align tactic.reverse_dependencies_of_hyp_name_set_inclusive_aux tactic.reverse_dependencies_of_hyp_name_set_inclusive_aux\n\n/-- `reverse_dependencies_of_hyp_name_set_inclusive hs` is the list of reverse\ndependencies of the hypotheses whose unique names appear in `hs`, including the\n`hs` themselves. The reverse dependencies are returned in the order in which\nthey appear in the context.\n-/\nunsafe def reverse_dependencies_of_hyp_name_set_inclusive (hs : name_set) : tactic (List expr) := do\n  let ctx \u2190 local_context\n  let ctx := ctx.dropWhile\u2093 fun h => \u00achs.contains h.local_uniq_name\n  reverse_dependencies_of_hyp_name_set_inclusive_aux ctx [] hs\n#align tactic.reverse_dependencies_of_hyp_name_set_inclusive tactic.reverse_dependencies_of_hyp_name_set_inclusive\n\n/-- `reverse_dependencies_of_hyp_set_inclusive hs` is the list of reverse\ndependencies of the hypotheses `hs`, including the `hs` themselves. The\ninclusive reverse dependencies are returned in the order in which they appear in\nthe context.\n-/\nunsafe def reverse_dependencies_of_hyp_set_inclusive (hs : expr_set) : tactic (List expr) :=\n  reverse_dependencies_of_hyp_name_set_inclusive <| local_set_to_name_set hs\n#align tactic.reverse_dependencies_of_hyp_set_inclusive tactic.reverse_dependencies_of_hyp_set_inclusive\n\n/-- `reverse_dependencies_of_hyps_inclusive hs` is the list of reverse dependencies\nof the hypotheses `hs`, including the `hs` themselves. The reverse dependencies\nare returned in the order in which they appear in the context.\n-/\nunsafe def reverse_dependencies_of_hyps_inclusive (hs : List expr) : tactic (List expr) :=\n  reverse_dependencies_of_hyp_name_set_inclusive <| local_list_to_name_set hs\n#align tactic.reverse_dependencies_of_hyps_inclusive tactic.reverse_dependencies_of_hyps_inclusive\n\n/-! ### Reverting a hypothesis and its reverse dependencies -/\n\n\n/-- `revert_name_set hs` reverts the hypotheses whose unique names appear in `hs`,\nas well as any hypotheses that depend on them. Returns the number of reverted\nhypotheses and a list containing these hypotheses. The reverted hypotheses are\nreturned in the order in which they used to appear in the context and are\nguaranteed to store the correct type (see `tactic.update_type`).\n-/\nunsafe def revert_name_set (hs : name_set) : tactic (\u2115 \u00d7 List expr) := do\n  let to_revert \u2190 reverse_dependencies_of_hyp_name_set_inclusive hs\n  let to_revert_with_types \u2190 to_revert.mapM update_type\n  let num_reverted \u2190 revert_lst to_revert\n  pure (num_reverted, to_revert_with_types)\n#align tactic.revert_name_set tactic.revert_name_set\n\n/-- `revert_set hs` reverts the hypotheses `hs`, as well as any hypotheses that\ndepend on them. Returns the number of reverted hypotheses and a list containing\nthese hypotheses. The reverted hypotheses are returned in the order in which\nthey used to appear in the context and are guaranteed to store the correct type\n(see `tactic.update_type`).\n-/\nunsafe def revert_set (hs : expr_set) : tactic (\u2115 \u00d7 List expr) :=\n  revert_name_set <| local_set_to_name_set hs\n#align tactic.revert_set tactic.revert_set\n\n/-- `revert_lst' hs` reverts the hypotheses `hs`, as well as any hypotheses that\ndepend on them. Returns the number of reverted hypotheses and a list containing\nthese hypotheses. The reverted hypotheses are returned in the order in which\nthey used to appear in the context and are guaranteed to store the correct type\n(see `tactic.update_type`).\n\nThis is a more informative version of `tactic.revert_lst`.\n-/\nunsafe def revert_lst' (hs : List expr) : tactic (\u2115 \u00d7 List expr) :=\n  revert_name_set <| local_list_to_name_set hs\n#align tactic.revert_lst' tactic.revert_lst'\n\n/- We cannot implement it as `revert e >> intro1` because that would change the\nlocal constant in the context. -/\n/-- `revert_reverse_dependencies_of_hyp h` reverts all the hypotheses that depend on\nthe hypothesis `h`, including the local definitions that have `h` in their\nvalue. This fixes a bug in `tactic.revert_kdependencies` that does not revert\nlocal definitions for which `h` only appears in the value. Returns the number\nof reverted hypotheses.\n-/\nunsafe def revert_reverse_dependencies_of_hyp (h : expr) : tactic \u2115 :=\n  reverse_dependencies_of_hyp_name_set (mk_name_set.insert h.local_uniq_name) >>= revert_lst\n#align tactic.revert_reverse_dependencies_of_hyp tactic.revert_reverse_dependencies_of_hyp\n\n/-- `revert_reverse_dependencies_of_hyp_name_set hs` reverts all the hypotheses that\ndepend on a hypothesis whose unique name appears in `hs`. The `hs` themselves\nare not reverted, unless they depend on each other. Returns the number of\nreverted hypotheses.\n-/\nunsafe def revert_reverse_dependencies_of_hyp_name_set (hs : name_set) : tactic \u2115 :=\n  reverse_dependencies_of_hyp_name_set hs >>= revert_lst\n#align tactic.revert_reverse_dependencies_of_hyp_name_set tactic.revert_reverse_dependencies_of_hyp_name_set\n\n/-- `revert_reverse_dependencies_of_hyp_set hs` reverts all the hypotheses that\ndepend on a hypothesis in `hs`. The `hs` themselves are not reverted, unless\nthey depend on each other. Returns the number of reverted hypotheses.\n-/\nunsafe def revert_reverse_dependencies_of_hyp_set (hs : expr_set) : tactic \u2115 :=\n  reverse_dependencies_of_hyp_set hs >>= revert_lst\n#align tactic.revert_reverse_dependencies_of_hyp_set tactic.revert_reverse_dependencies_of_hyp_set\n\n/-- `revert_reverse_dependencies_of_hyp hs` reverts all the hypotheses that depend\non a hypothesis in `hs`. The `hs` themselves are not reverted, unless they\ndepend on each other. Returns the number of reverted hypotheses.\n-/\nunsafe def revert_reverse_dependencies_of_hyps (hs : List expr) : tactic \u2115 :=\n  reverse_dependencies_of_hyps hs >>= revert_lst\n#align tactic.revert_reverse_dependencies_of_hyps tactic.revert_reverse_dependencies_of_hyps\n\nend Tactic\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/Tactic/Dependencies.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4921881357207956, "lm_q2_score": 0.13660838651113866, "lm_q1q2_score": 0.06723702708074322}}
{"text": "/-\nCopyright (c) 2021 Sebastian Ullrich. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor: Sebastian Ullrich\n-/\nimport Lean\nimport Mathlib.Tactic.Cache\n\n/-!\n# The `#find` command and tactic.\n\nThe `#find` command finds definitions & lemmas using pattern matching on the type. For instance:\n```lean\n#find _ + _ = _ + _\n#find ?n + _ = _ + ?n\n#find (_ : Nat) + _ = _ + _\n#find Nat \u2192 Nat\n```\nInside tactic proofs, there is a `#find` tactic with the same syntax,\nor the `find` tactic which looks for lemmas which are `apply`able against the current goal.\n\n-/\n\nopen Lean\nopen Lean.Meta\nopen Lean.Elab\nopen Lean.Elab\n\nnamespace Tactic\nnamespace Find\n\nprivate partial def matchHyps : List Expr \u2192 List Expr \u2192 List Expr \u2192 MetaM Bool\n  | p::ps, oldHyps, h::newHyps => do\n    let pt \u2190 inferType p\n    let t \u2190 inferType h\n    if (\u2190 isDefEq pt t) then\n      matchHyps ps [] (oldHyps ++ newHyps)\n    else\n      matchHyps (p::ps) (h::oldHyps) newHyps\n  | [], _, _    => pure true\n  | _::_, _, [] => pure false\n\n-- from Lean.Server.Completion\nprivate def isBlackListed (declName : Name) : MetaM Bool := do\n  let env \u2190 getEnv\n  pure $ declName.isInternal\n   || isAuxRecursor env declName\n   || isNoConfusion env declName\n  <||> isRec declName\n  <||> isMatcher declName\n\ninitialize findDeclsPerHead : DeclCache (Std.HashMap HeadIndex (Array Name)) \u2190\n  DeclCache.mk \"#find: init cache\" {} fun _ c headMap => do\n    if (\u2190 isBlackListed c.name) then\n      return headMap\n    -- TODO: this should perhaps use `forallTelescopeReducing` instead,\n    -- to avoid leaking metavariables.\n    let (_, _, ty) \u2190 forallMetaTelescopeReducing c.type\n    let head := ty.toHeadIndex\n    pure $ headMap.insert head (headMap.findD head #[] |>.push c.name)\n\ndef findType (t : Expr) : TermElabM Unit := withReducible do\n  let t \u2190 instantiateMVars t\n  let head := (\u2190 forallMetaTelescopeReducing t).2.2.toHeadIndex\n  let pat \u2190 abstractMVars t\n\n  let env \u2190 getEnv\n  let mut numFound := 0\n  for n in (\u2190 findDeclsPerHead.get).findD head #[] do\n    let c := env.find? n |>.get!\n    let cTy := c.instantiateTypeLevelParams (\u2190 mkFreshLevelMVars c.numLevelParams)\n    let found \u2190 forallTelescopeReducing cTy fun cParams cTy' => do\n      let pat := pat.expr.instantiateLevelParamsArray pat.paramNames (\u2190 mkFreshLevelMVars pat.numMVars).toArray\n      let (_, _, pat) \u2190 lambdaMetaTelescope pat\n      let (patParams, _, pat) \u2190 forallMetaTelescopeReducing pat\n      isDefEq cTy' pat <&&> matchHyps patParams.toList [] cParams.toList\n    if found then\n      numFound := numFound + 1\n      if numFound > 20 then\n        logInfo m!\"maximum number of search results reached\"\n        break\n      logInfo m!\"{n}: {cTy}\"\n\nopen Lean.Elab.Command in\n/-\nThe `#find` command finds definitions & lemmas using pattern matching on the type. For instance:\n```lean\n#find _ + _ = _ + _\n#find ?n + _ = _ + ?n\n#find (_ : Nat) + _ = _ + _\n#find Nat \u2192 Nat\n```\nInside tactic proofs, the `#find` tactic can be used instead.\nThere is also the `find` tactic which looks for\nlemmas which are `apply`able against the current goal.\n-/\nelab \"#find\" t:term : command =>\n  liftTermElabM none do\n    let t \u2190 Term.elabTerm t none\n    Term.synthesizeSyntheticMVars (mayPostpone := false) (ignoreStuckTC := true)\n    findType t\n\n/- (Note that you'll get an error trying to run these here:\n   ``cannot evaluate `[init]` declaration 'findDeclsPerHead' in the same module``\n   but they will work fine in a new file!) -/\n-- #find _ + _ = _ + _\n-- #find _ + _ = _ + _\n-- #find ?n + _ = _ + ?n\n-- #find (_ : Nat) + _ = _ + _\n-- #find Nat \u2192 Nat\n-- #find ?n \u2264 ?m \u2192 ?n + _ \u2264 ?m + _\n\nopen Lean.Elab.Tactic\n/-\nDisplay theorems (and definitions) whose result type matches the current goal, i.e. which should be `apply`able.\n```lean\nexample : True := by find\n```\n`find` will not affect the goal by itself and should be removed from the finished proof.\nFor a command that takes the type to search for as an argument, see `#find`, which is also available as a tactic.\n-/\nelab \"find\" : tactic => do\n  findType (\u2190 getMainTarget)\n\n/-\nTactic version of the `#find` command. See also the `find` tactic to search for theorems matching the current goal.\n-/\nelab \"#find\" t:term : tactic => do\n  let t \u2190 Term.elabTerm t none\n  Term.synthesizeSyntheticMVars (mayPostpone := false) (ignoreStuckTC := true)\n  findType t\n\nend Find\nend Tactic\n", "meta": {"author": "JOSHCLUNE", "repo": "Keller_reduction", "sha": "dc392b3da352fc1ffcfbecb1d4717d05f5faed4a", "save_path": "github-repos/lean/JOSHCLUNE-Keller_reduction", "path": "github-repos/lean/JOSHCLUNE-Keller_reduction/Keller_reduction-dc392b3da352fc1ffcfbecb1d4717d05f5faed4a/Lean4_Clique/Mathlib/Mathlib/Tactic/Find.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4843800842769843, "lm_q2_score": 0.13846179234652572, "lm_q1q2_score": 0.06706813464595243}}
{"text": "/-\nCopyright (c) 2018 Johan Commelin. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Johan Commelin, Reid Barton, Simon Hudon\n-/\nimport data.list.tfae\nimport tactic.scc\n\n/-!\n# The Following Are Equivalent (TFAE)\n\nThis file provides the tactics `tfae_have` and `tfae_finish` for proving the pairwise equivalence of\npropositions in a set using various implications between them.\n-/\n\nnamespace tactic\n\nexport list (tfae)\n\nnamespace tfae\n\n@[derive has_reflect, derive inhabited]\ninductive arrow : Type\n| right      : arrow\n| left_right : arrow\n| left       : arrow\n\nmeta def mk_implication : \u03a0 (re : arrow) (e\u2081 e\u2082 : expr), pexpr\n| arrow.right      e\u2081 e\u2082 := ``(%%e\u2081 \u2192 %%e\u2082)\n| arrow.left_right e\u2081 e\u2082 := ``(%%e\u2081 \u2194 %%e\u2082)\n| arrow.left       e\u2081 e\u2082 := ``(%%e\u2082 \u2192 %%e\u2081)\n\nmeta def mk_name : \u03a0 (re : arrow) (i\u2081 i\u2082 : nat), name\n| arrow.right      i\u2081 i\u2082 := (\"tfae_\" ++ to_string i\u2081 ++ \"_to_\"  ++ to_string i\u2082 : string)\n| arrow.left_right i\u2081 i\u2082 := (\"tfae_\" ++ to_string i\u2081 ++ \"_iff_\" ++ to_string i\u2082 : string)\n| arrow.left       i\u2081 i\u2082 := (\"tfae_\" ++ to_string i\u2082 ++ \"_to_\"  ++ to_string i\u2081 : string)\n\nend tfae\n\nnamespace interactive\n\nsetup_tactic_parser\nopen tactic.tfae list\n\nmeta def parse_list : expr \u2192 option (list expr)\n| `([]) := pure []\n| `(%%e :: %%es) := (::) e <$> parse_list es\n| _ := none\n\n/-- In a goal of the form `tfae [a\u2080, a\u2081, a\u2082]`,\n`tfae_have : i \u2192 j` creates the assertion `a\u1d62 \u2192 a\u2c7c`. The other possible\nnotations are `tfae_have : i \u2190 j` and `tfae_have : i \u2194 j`. The user can\nalso provide a label for the assertion, as with `have`: `tfae_have h : i \u2194 j`.\n-/\nmeta def tfae_have\n  (h : parse $ optional ident <* tk \":\")\n  (i\u2081 : parse (with_desc \"i\" small_nat))\n  (re : parse (((tk \"\u2192\" <|> tk \"->\")  *> return arrow.right)      <|>\n               ((tk \"\u2194\" <|> tk \"<->\") *> return arrow.left_right) <|>\n               ((tk \"\u2190\" <|> tk \"<-\")  *> return arrow.left)))\n  (i\u2082 : parse (with_desc \"j\" small_nat)) :\n  tactic unit := do\n    `(tfae %%l) <- target,\n    l \u2190 parse_list l,\n    e\u2081 \u2190 list.nth l (i\u2081 - 1) <|> fail format!\"index {i\u2081} is not between 1 and {l.length}\",\n    e\u2082 \u2190 list.nth l (i\u2082 - 1) <|> fail format!\"index {i\u2082} is not between 1 and {l.length}\",\n    type \u2190 to_expr (tfae.mk_implication re e\u2081 e\u2082),\n    let h := h.get_or_else (mk_name re i\u2081 i\u2082),\n    tactic.assert h type,\n    return ()\n\n/-- Finds all implications and equivalences in the context\nto prove a goal of the form `tfae [...]`.\n-/\nmeta def tfae_finish : tactic unit :=\napplyc ``tfae_nil <|>\nclosure.with_new_closure (\u03bb cl,\ndo impl_graph.mk_scc cl,\n   `(tfae %%l) \u2190 target,\n   l \u2190 parse_list l,\n   (_,r,_) \u2190 cl.root l.head,\n   refine ``(tfae_of_forall %%r _ _),\n   thm \u2190 mk_const ``forall_mem_cons,\n   l.mmap' (\u03bb e,\n     do rewrite_target thm, split,\n        (_,r',p) \u2190 cl.root e,\n        tactic.exact p ),\n   applyc ``forall_mem_nil,\n   pure ())\n\nend interactive\nend tactic\n\n/--\nThe `tfae` tactic suite is a set of tactics that help with proving that certain\npropositions are equivalent.\nIn `data/list/basic.lean` there is a section devoted to propositions of the\nform\n```lean\ntfae [p1, p2, ..., pn]\n```\nwhere `p1`, `p2`, through, `pn` are terms of type `Prop`.\nThis proposition asserts that all the `pi` are pairwise equivalent.\nThere are results that allow to extract the equivalence\nof two propositions `pi` and `pj`.\n\nTo prove a goal of the form `tfae [p1, p2, ..., pn]`, there are two\ntactics.  The first tactic is `tfae_have`.  As an argument it takes an\nexpression of the form `i arrow j`, where `i` and `j` are two positive\nnatural numbers, and `arrow` is an arrow such as `\u2192`, `->`, `\u2190`, `<-`,\n`\u2194`, or `<->`.  The tactic `tfae_have : i arrow j` sets up a subgoal in\nwhich the user has to prove the equivalence (or implication) of `pi` and `pj`.\n\nThe remaining tactic, `tfae_finish`, is a finishing tactic. It\ncollects all implications and equivalences from the local context and\ncomputes their transitive closure to close the\nmain goal.\n\n`tfae_have` and `tfae_finish` can be used together in a proof as\nfollows:\n\n```lean\nexample (a b c d : Prop) : tfae [a,b,c,d] :=\nbegin\n  tfae_have : 3 \u2192 1,\n  { /- prove c \u2192 a -/ },\n  tfae_have : 2 \u2192 3,\n  { /- prove b \u2192 c -/ },\n  tfae_have : 2 \u2190 1,\n  { /- prove a \u2192 b -/ },\n  tfae_have : 4 \u2194 2,\n  { /- prove d \u2194 b -/ },\n    -- a b c d : Prop,\n    -- tfae_3_to_1 : c \u2192 a,\n    -- tfae_2_to_3 : b \u2192 c,\n    -- tfae_1_to_2 : a \u2192 b,\n    -- tfae_4_iff_2 : d \u2194 b\n    -- \u22a2 tfae [a, b, c, d]\n  tfae_finish,\nend\n```\n-/\nadd_tactic_doc\n{ name := \"tfae\",\n  category                 := doc_category.tactic,\n  decl_names               := [`tactic.interactive.tfae_have, `tactic.interactive.tfae_finish],\n  tags                     := [\"logic\"],\n  inherit_description_from := `tactic.interactive.tfae_finish }\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/tfae.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4843800842769844, "lm_q2_score": 0.13846178523628042, "lm_q1q2_score": 0.06706813120189123}}
{"text": "/-\nCopyright (c) 2019 Microsoft Corporation. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor: Leonardo de Moura\n-/\nprelude\nimport Init.Data.Nat.Linear\nimport Init.Data.List.Basic\nimport Init.Util\n\nuniverse u\n\nnamespace List\n/-! The following functions can't be defined at `Init.Data.List.Basic`, because they depend on `Init.Util`,\n   and `Init.Util` depends on `Init.Data.List.Basic`. -/\n\ndef get! [Inhabited \u03b1] : List \u03b1 \u2192 Nat \u2192 \u03b1\n  | a::_,  0   => a\n  | _::as, n+1 => get! as n\n  | _,     _   => panic! \"invalid index\"\n\ndef get? : List \u03b1 \u2192 Nat \u2192 Option \u03b1\n  | a::_,  0   => some a\n  | _::as, n+1 => get? as n\n  | _,     _   => none\n\ndef getD (as : List \u03b1) (idx : Nat) (a\u2080 : \u03b1) : \u03b1 :=\n  (as.get? idx).getD a\u2080\n\ndef head! [Inhabited \u03b1] : List \u03b1 \u2192 \u03b1\n  | []   => panic! \"empty list\"\n  | a::_ => a\n\ndef head? : List \u03b1 \u2192 Option \u03b1\n  | []   => none\n  | a::_ => some a\n\ndef headD : List \u03b1 \u2192 \u03b1 \u2192 \u03b1\n  | [],   a\u2080 => a\u2080\n  | a::_, _  => a\n\ndef head : (as : List \u03b1) \u2192 as \u2260 [] \u2192 \u03b1\n  | a::_, _ => a\n\ndef tail! : List \u03b1 \u2192 List \u03b1\n  | []    => panic! \"empty list\"\n  | _::as => as\n\ndef tail? : List \u03b1 \u2192 Option (List \u03b1)\n  | []    => none\n  | _::as => some as\n\ndef tailD : List \u03b1 \u2192 List \u03b1 \u2192 List \u03b1\n  | [],   as\u2080 => as\u2080\n  | _::as, _  => as\n\ndef getLast : \u2200 (as : List \u03b1), as \u2260 [] \u2192 \u03b1\n  | [],       h => absurd rfl h\n  | [a],      _ => a\n  | _::b::as, _ => getLast (b::as) (fun h => List.noConfusion h)\n\ndef getLast! [Inhabited \u03b1] : List \u03b1 \u2192 \u03b1\n  | []    => panic! \"empty list\"\n  | a::as => getLast (a::as) (fun h => List.noConfusion h)\n\ndef getLast? : List \u03b1 \u2192 Option \u03b1\n  | []    => none\n  | a::as => some (getLast (a::as) (fun h => List.noConfusion h))\n\ndef getLastD : List \u03b1 \u2192 \u03b1 \u2192 \u03b1\n  | [],   a\u2080 => a\u2080\n  | a::as, _ => getLast (a::as) (fun h => List.noConfusion h)\n\ndef rotateLeft (xs : List \u03b1) (n : Nat := 1) : List \u03b1 :=\n  let len := xs.length\n  if len \u2264 1 then\n    xs\n  else\n    let n := n % len\n    let b := xs.take n\n    let e := xs.drop n\n    e ++ b\n\ndef rotateRight (xs : List \u03b1) (n : Nat := 1) : List \u03b1 :=\n  let len := xs.length\n  if len \u2264 1 then\n    xs\n  else\n    let n := len - n % len\n    let b := xs.take n\n    let e := xs.drop n\n    e ++ b\n\ntheorem get_append_left (as bs : List \u03b1) (h : i < as.length) {h'} : (as ++ bs).get \u27e8i, h'\u27e9 = as.get \u27e8i, h\u27e9 := by\n  induction as generalizing i with\n  | nil => trivial\n  | cons a as ih =>\n    cases i with\n    | zero => rfl\n    | succ i => apply ih\n\ntheorem get_append_right (as bs : List \u03b1) (h : \u00ac i < as.length) {h' h''} : (as ++ bs).get \u27e8i, h'\u27e9 = bs.get \u27e8i - as.length, h''\u27e9 := by\n  induction as generalizing i with\n  | nil => trivial\n  | cons a as ih =>\n    cases i with simp [get, Nat.succ_sub_succ] <;> simp_arith [Nat.succ_sub_succ] at h\n    | succ i => apply ih; simp_arith [h]\n\ntheorem get_last {as : List \u03b1} {i : Fin (length (as ++ [a]))} (h : \u00ac i.1 < as.length) : (as ++ [a] : List _).get i = a := by\n  cases i; rename_i i h'\n  induction as generalizing i with\n  | nil => cases i with\n    | zero => simp [List.get]\n    | succ => simp_arith at h'\n  | cons a as ih =>\n    cases i with simp_arith at h\n    | succ i => apply ih; simp_arith [h]\n\ntheorem sizeOf_lt_of_mem [SizeOf \u03b1] {as : List \u03b1} (h : a \u2208 as) : sizeOf a < sizeOf as := by\n  induction h with\n  | head => simp_arith\n  | tail _ _ ih => exact Nat.lt_trans ih (by simp_arith)\n\n/-- This tactic, added to the `decreasing_trivial` toolbox, proves that\n`sizeOf a < sizeOf as` when `a \u2208 as`, which is useful for well founded recursions\nover a nested inductive like `inductive T | mk : List T \u2192 T`. -/\nmacro \"sizeOf_list_dec\" : tactic =>\n  `(tactic| first\n    | apply sizeOf_lt_of_mem; assumption; done\n    | apply Nat.lt_trans (sizeOf_lt_of_mem ?h)\n      case' h => assumption\n      simp_arith)\n\nmacro_rules | `(tactic| decreasing_trivial) => `(tactic| sizeOf_list_dec)\n\ntheorem append_cancel_left {as bs cs : List \u03b1} (h : as ++ bs = as ++ cs) : bs = cs := by\n  induction as with\n  | nil => assumption\n  | cons a as ih =>\n    injection h with _ h\n    exact ih h\n\ntheorem append_cancel_right {as bs cs : List \u03b1} (h : as ++ bs = cs ++ bs) : as = cs := by\n  match as, cs with\n  | [], []       => rfl\n  | [], c::cs    => have aux := congrArg length h; simp_arith at aux\n  | a::as, []    => have aux := congrArg length h; simp_arith at aux\n  | a::as, c::cs => injection h with h\u2081 h\u2082; subst h\u2081; rw [append_cancel_right h\u2082]\n\n@[simp] theorem append_cancel_left_eq (as bs cs : List \u03b1) : (as ++ bs = as ++ cs) = (bs = cs) := by\n  apply propext; apply Iff.intro\n  next => apply append_cancel_left\n  next => intro h; simp [h]\n\n@[simp] theorem append_cancel_right_eq (as bs cs : List \u03b1) : (as ++ bs = cs ++ bs) = (as = cs) := by\n  apply propext; apply Iff.intro\n  next => apply append_cancel_right\n  next => intro h; simp [h]\n\n@[simp] theorem sizeOf_get [SizeOf \u03b1] (as : List \u03b1) (i : Fin as.length) : sizeOf (as.get i) < sizeOf as := by\n  match as, i with\n  | a::as, \u27e80, _\u27e9  => simp_arith [get]\n  | a::as, \u27e8i+1, h\u27e9 =>\n    have ih := sizeOf_get as \u27e8i, Nat.le_of_succ_le_succ h\u27e9\n    apply Nat.lt_trans ih\n    simp_arith\n\ntheorem le_antisymm [LT \u03b1] [s : Antisymm (\u00ac \u00b7 < \u00b7 : \u03b1 \u2192 \u03b1 \u2192 Prop)] {as bs : List \u03b1} (h\u2081 : as \u2264 bs) (h\u2082 : bs \u2264 as) : as = bs :=\n  match as, bs with\n  | [],    []    => rfl\n  | [],    b::bs => False.elim <| h\u2082 (List.lt.nil ..)\n  | a::as, []    => False.elim <| h\u2081 (List.lt.nil ..)\n  | a::as, b::bs => by\n    by_cases hab : a < b\n    \u00b7 exact False.elim <| h\u2082 (List.lt.head _ _ hab)\n    \u00b7 by_cases hba : b < a\n      \u00b7 exact False.elim <| h\u2081 (List.lt.head _ _ hba)\n      \u00b7 have h\u2081 : as \u2264 bs := fun h => h\u2081 (List.lt.tail hba hab h)\n        have h\u2082 : bs \u2264 as := fun h => h\u2082 (List.lt.tail hab hba h)\n        have ih : as = bs := le_antisymm h\u2081 h\u2082\n        have : a = b := s.antisymm hab hba\n        simp [this, ih]\n\ninstance [LT \u03b1] [Antisymm (\u00ac \u00b7 < \u00b7 : \u03b1 \u2192 \u03b1 \u2192 Prop)] : Antisymm (\u00b7 \u2264 \u00b7 : List \u03b1 \u2192 List \u03b1 \u2192 Prop) where\n  antisymm h\u2081 h\u2082 := le_antisymm h\u2081 h\u2082\n\n@[specialize] private unsafe def mapMonoMImp [Monad m] (as : List \u03b1) (f : \u03b1 \u2192 m \u03b1) : m (List \u03b1) := do\n  match as with\n  | [] => return as\n  | b :: bs =>\n    let b'  \u2190 f b\n    let bs' \u2190 mapMonoMImp bs f\n    if ptrEq b' b && ptrEq bs' bs then\n      return as\n    else\n      return b' :: bs'\n\n/--\nMonomorphic `List.mapM`. The internal implementation uses pointer equality, and does not allocate a new list\nif the result of each `f a` is a pointer equal value `a`.\n-/\n@[implemented_by mapMonoMImp] def mapMonoM [Monad m] (as : List \u03b1) (f : \u03b1 \u2192 m \u03b1) : m (List \u03b1) :=\n  match as with\n  | [] => return []\n  | a :: as => return (\u2190 f a) :: (\u2190 mapMonoM as f)\n\ndef mapMono (as : List \u03b1) (f : \u03b1 \u2192 \u03b1) : List \u03b1 :=\n  Id.run <| as.mapMonoM f\n\nend List\n", "meta": {"author": "leanprover", "repo": "lean4", "sha": "742d053a97bdd109a41a921facd1cd6a55e89bc7", "save_path": "github-repos/lean/leanprover-lean4", "path": "github-repos/lean/leanprover-lean4/lean4-742d053a97bdd109a41a921facd1cd6a55e89bc7/src/Init/Data/List/BasicAux.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4882834101888134, "lm_q2_score": 0.13477591916060164, "lm_q1q2_score": 0.06580884541907041}}
{"text": "/-\nCopyright (c) 2020 Dany Fabian. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Dany Fabian\n\n! This file was ported from Lean 3 source module tactic.unfold_cases\n! leanprover-community/mathlib commit 0035e2dd30b71232b3bf7d0827eeaa0c002c1825\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.SplitIfs\n\n/-!\n  # Unfold cases tactic\n\n  In Lean, pattern matching expressions are not atomic parts of the syntax, but\n  rather they are compiled down into simpler terms that are later checked by the kernel.\n\n  This allows Lean to have a minimalistic kernel but can occasionally lead an explosion\n  of cases that need to be considered. What looks like one case in the `match` expression\n  can in fact be compiled into many different cases that all need to proved by case analysis.\n\n  This tactic automates the process by allowing us to write down an equation `f x = y`\n  where we know that `f x = y` is provably true, but does not hold definitionally. In that\n  case the `unfold_cases` tactic will continue unfolding `f` and introducing `cases` where\n  necessary until the left hand side becomes definitionally equal to the right hand side.\n\n  Consider a definition as follows:\n\n  ```lean\n  def myand : bool \u2192 bool \u2192 bool\n  | ff _ := ff\n  | _ ff := ff\n  | _ _ := tt\n  ```\n\n  The equation compiler generates 4 equation lemmas for us:\n  ```lean\n  myand ff ff = ff\n  myand ff tt = ff\n  myand tt ff = ff\n  myand tt tt = tt\n  ```\n\n  This is not in line with what one might expect looking at the definition.\n  Whilst it is provably true, that `\u2200 x, myand ff x = ff` and `\u2200 x, myand x ff = ff`,\n  we do not get these stronger lemmas from the compiler for free but must in fact\n  prove them using `cases` or some other local reasoning.\n\n  In other words, the following does not constitute a proof that lean accepts.\n  ```lean\n  example : \u2200 x, myand ff x = ff :=\n  begin\n    intros, refl\n  end\n  ```\n\n  However, you can use `unfold_cases { refl }` to prove `\u2200 x, myand ff x = ff` and\n  `\u2200 x, myand x ff = ff`. For definitions with many cases, the savings can be very\n  significant.\n\n  The term that gets generated for the above definition looks like this:\n  ```lean\n  \u03bb (a a_1 : bool),\n  a.cases_on\n    (a_1.cases_on (id_rhs bool ff) (id_rhs bool ff))\n    (a_1.cases_on (id_rhs bool ff) (id_rhs bool tt))\n  ```\n\n  When the tactic tries to prove the goal `\u2200 x, myand ff x = ff`, it starts by `intros`,\n  followed by unfolding the definition:\n  ```lean\n  \u22a2 ff.cases_on\n    (x.cases_on (id_rhs bool ff) (id_rhs bool ff))\n    (x.cases_on (id_rhs bool ff) (id_rhs bool tt)) = ff\n  ```\n\n  At this point, it can make progress using `dsimp`. But then it gets stuck:\n  ```lean\n  \u22a2 bool.rec (id_rhs bool ff) (id_rhs bool ff) x = ff\n  ```\n\n  Next, it can introduce a case split on `x`. At this point, it has to prove two\n  goals:\n  ```lean\n  \u22a2 bool.rec (id_rhs bool ff) (id_rhs bool ff) ff = ff\n  \u22a2 bool.rec (id_rhs bool ff) (id_rhs bool ff) tt = ff\n  ```\n\n  Now, however, both goals can be discharged using `refl`.\n-/\n\n\nnamespace Tactic\n\nopen Expr\n\nnamespace UnfoldCases\n\n-- failed to format: unknown constant 'term.pseudo.antiquot'\n/--\n      Given an equation `f x = y`, this tactic tries to infer an expression that can be\n        used to do distinction by cases on to make progress.\n      \n        Pre-condition: assumes that the outer-most application cannot be beta-reduced\n        (e.g. `whnf` or `dsimp`).\n      -/\n    unsafe\n  def\n    find_splitting_expr\n    : expr \u2192 tactic expr\n    |\n        q( @ ite _ $ ( cond ) $ ( dec_inst ) _ _ = _ )\n        =>\n        pure q( @ Decidable.em $ ( cond ) $ ( dec_inst ) )\n      | q( $ ( app x y ) = _ ) => pure y\n      |\n        e\n        =>\n        throwError\n          \"expected an expression of the form: f x = y. Got:\n            { \u2190 e }\"\n#align tactic.unfold_cases.find_splitting_expr tactic.unfold_cases.find_splitting_expr\n\n/-- Tries to finish the current goal using the `inner` tactic. If the tactic\n  fails, it tries to find an expression on which to do a distinction by\n  cases and calls itself recursively.\n\n  The order of operations is significant. Because the unfolding can potentially\n  be infinite, it is important to apply the `inner` tactic at every step.\n\n  Notice, that if the `inner` tactic succeeds, the recursive unfolding is stopped.\n-/\nunsafe def unfold_cases_core (inner : interactive.itactic) : tactic Unit :=\n  inner <|>\n    (do\n        split_ifs []\n        all_goals unfold_cases_core\n        skip) <|>\n      do\n      let tgt \u2190 target\n      let e \u2190 find_splitting_expr tgt\n      focus1 do\n          cases e\n          all_goals <| dsimp_target >> unfold_cases_core <|> skip\n          skip\n#align tactic.unfold_cases.unfold_cases_core tactic.unfold_cases.unfold_cases_core\n\n-- failed to format: unknown constant 'term.pseudo.antiquot'\n/--\n      Given a target of the form `\u22a2 f x\u2081 ... x\u2099 = y`, unfolds `f` using a delta reduction.\n      -/\n    unsafe\n  def\n    unfold_tgt\n    : expr \u2192 tactic Unit\n    |\n        q( $ ( l @ app _ _ ) = $ ( r ) )\n        =>\n        match\n          l . get_app_fn\n          with\n          | const n ls => delta_target [ n ]\n            |\n              e\n              =>\n              throwError\n                \"couldn't unfold:\n                  { \u2190 e }\"\n      |\n        e\n        =>\n        throwError\n          \"expected an expression of the form: f x = y. Got:\n            { \u2190 e }\"\n#align tactic.unfold_cases.unfold_tgt tactic.unfold_cases.unfold_tgt\n\nend UnfoldCases\n\nnamespace Interactive\n\nopen UnfoldCases\n\n/-- This tactic unfolds the definition of a function or `match` expression.\n  Then it recursively introduces a distinction by cases. The decision what expression\n  to do the distinction on is driven by the pattern matching expression.\n\n  A typical use case is using `unfold_cases { refl }` to collapse cases that need to be\n  considered in a pattern matching.\n\n  ```lean\n  have h : foo x = y, by unfold_cases { refl },\n  rw h,\n  ```\n\n  The tactic expects a goal in the form of an equation, possibly universally quantified.\n\n  We can prove a theorem, even if the various case do not directly correspond to the\n  function definition. Here is an example application of the tactic:\n\n  ```lean\n  def foo : \u2115 \u2192 \u2115 \u2192 \u2115\n  | 0     0 := 17\n  | (n+2) 17 := 17\n  | 1     0 := 23\n  | 0     (n+18) := 15\n  | 0     17 := 17\n  | 1     17 := 17\n  | _     (n+18) := 27\n  | _     _ := 15\n\n  example : \u2200 x, foo x 17 = 17 :=\n  begin\n    unfold_cases { refl },\n  end\n  ```\n\n  The compiler generates 57 cases for `foo`. However, when we look at the definition, we see\n  that whenever the function is applied to `17` in the second argument, it returns `17`.\n\n  Proving this property consists of merely considering all the cases, eliminating invalid ones\n  and applying `refl` on the ones which remain.\n\n  Further examples can be found in `test/unfold_cases.lean`.\n-/\nunsafe def unfold_cases (inner : itactic) : tactic Unit :=\n  focus1 do\n    tactic.intros\n    let tgt \u2190 target\n    unfold_tgt tgt\n    try dsimp_target\n    unfold_cases_core inner\n#align tactic.interactive.unfold_cases tactic.interactive.unfold_cases\n\nadd_tactic_doc\n  { Name := \"unfold_cases\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.unfold_cases]\n    tags := [\"induction\", \"case bashing\"] }\n\nend Interactive\n\nend Tactic\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/Tactic/UnfoldCases.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.403566839388498, "lm_q2_score": 0.1602660263115288, "lm_q1q2_score": 0.06467805369989754}}
{"text": "/-\nCopyright (c) 2020 Kevin Buzzard. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Kevin Buzzard, Bryan Gin-ge Chen, Robert Y. Lewis, Scott Morrison\n\n! This file was ported from Lean 3 source module tactic.lean_core_docs\n! leanprover-community/mathlib commit 7e3fa4c114f6f12380cf3b181fd4bd03a2f05b79\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.DocCommands\n\n/-!\n\n# Core tactic documentation\n\nThis file adds the majority of the interactive tactics from core Lean (i.e. pre-mathlib) to\nthe API documentation.\n\n## TODO\n\n* Make a PR to core changing core docstrings to the docstrings below,\nand also changing the docstrings of `cc`, `simp` and `conv` to the ones\nalready in the API docs.\n\n* SMT tactics are currently not documented.\n\n* `rsimp` and `constructor_matching` are currently not documented.\n\n* `dsimp` deserves better documentation.\n-/\n\n\nadd_tactic_doc\n  { Name := \"abstract\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.abstract]\n    tags := [\"core\", \"proof extraction\"] }\n\n/-- Proves a goal of the form `s = t` when `s` and `t` are expressions built up out of a binary\noperation, and equality can be proved using associativity and commutativity of that operation. -/\nadd_tactic_doc\n  { Name := \"ac_refl\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.ac_refl, `tactic.interactive.ac_reflexivity]\n    tags := [\"core\", \"lemma application\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"all_goals\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.all_goals]\n    tags := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"any_goals\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.any_goals]\n    tags := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"apply\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.apply]\n    tags := [\"core\", \"basic\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"apply_auto_param\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.apply_auto_param]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"apply_instance\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.apply_instance]\n    tags := [\"core\", \"type class\"] }\n\nadd_tactic_doc\n  { Name := \"apply_opt_param\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.apply_opt_param]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"apply_with\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.apply_with]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"assume\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.assume]\n    tags := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"assumption\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.assumption]\n    tags := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"assumption'\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.assumption']\n    tags := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"async\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.async]\n    tags := [\"core\", \"goal management\", \"combinator\", \"proof extraction\"] }\n\n/-- `by_cases p` splits the main goal into two cases, assuming `h : p` in the first branch, and\n`h : \u00ac p` in the second branch. You can specify the name of the new hypothesis using the syntax\n`by_cases h : p`.\n\nIf `p` is not already decidable, `by_cases` will use the instance `classical.prop_decidable p`.\n-/\nadd_tactic_doc\n  { Name := \"by_cases\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.by_cases]\n    tags := [\"core\", \"basic\", \"logic\", \"case bashing\"] }\n\n/-- If the target of the main goal is a proposition `p`, `by_contra h` reduces the goal to proving\n`false` using the additional hypothesis `h : \u00ac p`. If `h` is omitted, a name is generated\nautomatically.\n\nThis tactic requires that `p` is decidable. To ensure that all propositions are decidable via\nclassical reasoning, use `open_locale classical`\n(or `local attribute [instance, priority 10] classical.prop_decidable` if you are not using\nmathlib).\n-/\nadd_tactic_doc\n  { Name := \"by_contra / by_contradiction\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.by_contra, `tactic.interactive.by_contradiction]\n    tags := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"case\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.case]\n    tags := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"cases\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.cases]\n    tags := [\"core\", \"basic\", \"induction\"] }\n\n/-- `cases_matching p` applies the `cases` tactic to a hypothesis `h : type`\nif `type` matches the pattern `p`.\n\n`cases_matching [p_1, ..., p_n]` applies the `cases` tactic to a hypothesis `h : type`\nif `type` matches one of the given patterns.\n\n`cases_matching* p` is a more efficient and compact version\nof `focus1 { repeat { cases_matching p } }`.\nIt is more efficient because the pattern is compiled once.\n\n`casesm` is shorthand for `cases_matching`.\n\nExample: The following tactic destructs all conjunctions and disjunctions in the current context.\n```\ncases_matching* [_ \u2228 _, _ \u2227 _]\n```\n-/\nadd_tactic_doc\n  { Name := \"cases_matching / casesm\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.cases_matching, `tactic.interactive.casesm]\n    tags := [\"core\", \"induction\", \"context management\"] }\n\n/-- * `cases_type I` applies the `cases` tactic to a hypothesis `h : (I ...)`\n* `cases_type I_1 ... I_n` applies the `cases` tactic to a hypothesis\n  `h : (I_1 ...)` or ... or `h : (I_n ...)`\n* `cases_type* I` is shorthand for `focus1 { repeat { cases_type I } }`\n* `cases_type! I` only applies `cases` if the number of resulting subgoals is <= 1.\n\nExample: The following tactic destructs all conjunctions and disjunctions in the current context.\n```\ncases_type* or and\n```\n-/\nadd_tactic_doc\n  { Name := \"cases_type\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.cases_type]\n    tags := [\"core\", \"induction\", \"context management\"] }\n\nadd_tactic_doc\n  { Name := \"change\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.change]\n    tags := [\"core\", \"basic\", \"renaming\"] }\n\nadd_tactic_doc\n  { Name := \"clear\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.clear]\n    tags := [\"core\", \"context management\"] }\n\n/-- Close goals of the form `n \u2260 m` when `n` and `m` have type `nat`, `char`, `string`, `int`\nor `fin sz`, and they are literals. It also closes goals of the form `n < m`, `n > m`, `n \u2264 m` and\n`n \u2265 m` for `nat`. If the goal is of the form `n = m`, then it tries to close it using reflexivity.\n\nIn mathlib, consider using `norm_num` instead for numeric types.\n-/\nadd_tactic_doc\n  { Name := \"comp_val\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.comp_val]\n    tags := [\"core\", \"arithmetic\"] }\n\n/-- The `congr` tactic attempts to identify both sides of an equality goal `A = B`,\nleaving as new goals the subterms of `A` and `B` which are not definitionally equal.\nExample: suppose the goal is `x * f y = g w * f z`. Then `congr` will produce two goals:\n`x = g w` and `y = z`.\n\nIf `x y : t`, and an instance `subsingleton t` is in scope, then any goals of the form\n`x = y` are solved automatically.\n\nNote that `congr` can be over-aggressive at times; the `congr'` tactic in mathlib\nprovides a more refined approach, by taking a parameter that limits the recursion depth.\n-/\nadd_tactic_doc\n  { Name := \"congr\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.congr]\n    tags := [\"core\", \"congruence\"] }\n\nadd_tactic_doc\n  { Name := \"constructor\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.constructor]\n    tags := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"contradiction\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.contradiction]\n    tags := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"delta\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.delta]\n    tags := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n  { Name := \"destruct\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.destruct]\n    tags := [\"core\", \"induction\"] }\n\nadd_tactic_doc\n  { Name := \"done\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.done]\n    tags := [\"core\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"dsimp\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.dsimp]\n    tags := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n  { Name := \"dunfold\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.dunfold]\n    tags := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n  { Name := \"eapply\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.eapply]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"econstructor\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.econstructor]\n    tags := [\"core\", \"logic\"] }\n\n/-- A variant of `rw` that uses the unifier more aggressively, unfolding semireducible definitions.\n-/\nadd_tactic_doc\n  { Name := \"erewrite / erw\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.erewrite, `tactic.interactive.erw]\n    tags := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"exact\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.exact]\n    tags := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"exacts\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.exacts]\n    tags := [\"core\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"exfalso\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.exfalso]\n    tags := [\"core\", \"basic\", \"logic\"] }\n\n/-- `existsi e` will instantiate an existential quantifier in the target with `e` and leave the\ninstantiated body as the new target. More generally, it applies to any inductive type with one\nconstructor and at least two arguments, applying the constructor with `e` as the first argument\nand leaving the remaining arguments as goals.\n\n`existsi [e\u2081, ..., e\u2099]` iteratively does the same for each expression in the list.\n\nNote: in mathlib, the `use` tactic is an equivalent tactic which sometimes is smarter with\nunification.\n-/\nadd_tactic_doc\n  { Name := \"existsi\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.existsi]\n    tags := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"fail_if_success\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.fail_if_success]\n    tags := [\"core\", \"testing\", \"combinator\"] }\n\nadd_tactic_doc\n  { Name := \"fapply\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.fapply]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"focus\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.focus]\n    tags := [\"core\", \"goal management\", \"combinator\"] }\n\nadd_tactic_doc\n  { Name := \"from\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.from]\n    tags := [\"core\", \"finishing\"] }\n\n/-- Apply function extensionality and introduce new hypotheses.\nThe tactic `funext` will keep applying new the `funext` lemma until the goal target is not reducible\nto\n```\n  |-  ((fun x, ...) = (fun x, ...))\n```\nThe variant `funext h\u2081 ... h\u2099` applies `funext` `n` times, and uses the given identifiers to name\nthe new hypotheses.\n\nNote also the mathlib tactic `ext`, which applies as many extensionality lemmas as possible.\n-/\nadd_tactic_doc\n  { Name := \"funext\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.funext]\n    tags := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"generalize\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.generalize]\n    tags := [\"core\", \"context management\"] }\n\nadd_tactic_doc\n  { Name := \"guard_hyp\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.guard_hyp]\n    tags := [\"core\", \"testing\", \"context management\"] }\n\nadd_tactic_doc\n  { Name := \"guard_target\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.guard_target]\n    tags := [\"core\", \"testing\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"have\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.have]\n    tags := [\"core\", \"basic\", \"context management\"] }\n\nadd_tactic_doc\n  { Name := \"induction\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.induction]\n    tags := [\"core\", \"basic\", \"induction\"] }\n\nadd_tactic_doc\n  { Name := \"injection\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.injection]\n    tags := [\"core\", \"structures\", \"induction\"] }\n\nadd_tactic_doc\n  { Name := \"injections\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.injections]\n    tags := [\"core\", \"structures\", \"induction\"] }\n\n/-- If the current goal is a Pi/forall `\u2200 x : t, u` (resp. `let x := t in u`) then `intro` puts\n`x : t` (resp. `x := t`) in the local context. The new subgoal target is `u`.\n\nIf the goal is an arrow `t \u2192 u`, then it puts `h : t` in the local context and the new goal\ntarget is `u`.\n\nIf the goal is neither a Pi/forall nor begins with a let binder, the tactic `intro` applies the\ntactic `whnf` until an introduction can be applied or the goal is not head reducible. In the latter\ncase, the tactic fails.\n\nThe variant `intro z` uses the identifier `z` to name the new hypothesis.\n\nThe variant `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall\nor let binder.\n\nThe variant `intros h\u2081 ... h\u2099` introduces `n` new hypotheses using the given identifiers to name\nthem.\n-/\nadd_tactic_doc\n  { Name := \"intro / intros\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.intro, `tactic.interactive.intros]\n    tags := [\"core\", \"basic\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"introv\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.introv]\n    tags := [\"core\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"iterate\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.iterate]\n    tags := [\"core\", \"combinator\"] }\n\n/-- `left` applies the first constructor when the type of the target is an inductive data type with\ntwo constructors.\n\nSimilarly, `right` applies the second constructor.\n-/\nadd_tactic_doc\n  { Name := \"left / right\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.left, `tactic.interactive.right]\n    tags := [\"core\", \"basic\", \"logic\"] }\n\n/-- `let h : t := p` adds the hypothesis `h : t := p` to the current goal if `p` a term of type `t`.\nIf `t` is omitted, it will be inferred.\n\n`let h : t` adds the hypothesis `h : t := ?M` to the current goal and opens a new subgoal `?M : t`.\nThe new subgoal becomes the main goal. If `t` is omitted, it will be replaced by a fresh\nmetavariable.\n\nIf `h` is omitted, the name `this` is used.\n\nNote the related mathlib tactic `set a := t with h`, which adds the hypothesis `h : a = t` to\nthe local context and replaces `t` with `a` everywhere it can.\n-/\nadd_tactic_doc\n  { Name := \"let\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.let]\n    tags := [\"core\", \"basic\", \"logic\", \"context management\"] }\n\nadd_tactic_doc\n  { Name := \"mapply\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.mapply]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"match_target\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.match_target]\n    tags := [\"core\", \"testing\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"refine\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.refine]\n    tags := [\"core\", \"basic\", \"lemma application\"] }\n\n/--\nThis tactic applies to a goal whose target has the form `t ~ u` where `~` is a reflexive relation,\nthat is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`.\nThe tactic checks whether `t` and `u` are definitionally equal and then solves the goal.\n-/\nadd_tactic_doc\n  { Name := \"refl / reflexivity\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.refl, `tactic.interactive.reflexivity]\n    tags := [\"core\", \"basic\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"rename\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.rename]\n    tags := [\"core\", \"renaming\"] }\n\nadd_tactic_doc\n  { Name := \"repeat\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.repeat]\n    tags := [\"core\", \"combinator\"] }\n\nadd_tactic_doc\n  { Name := \"revert\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.revert]\n    tags := [\"core\", \"context management\", \"goal management\"] }\n\n/-- `rw e` applies an equation or iff `e` as a rewrite rule to the main goal. If `e` is preceded by\nleft arrow (`\u2190` or `<-`), the rewrite is applied in the reverse direction. If `e` is a defined\nconstant, then the equational lemmas associated with `e` are used. This provides a convenient\nway to unfold `e`.\n\n`rw [e\u2081, ..., e\u2099]` applies the given rules sequentially.\n\n`rw e at l` rewrites `e` at location(s) `l`, where `l` is either `*` or a list of hypotheses\nin the local context. In the latter case, a turnstile `\u22a2` or `|-` can also be used, to signify\nthe target of the goal.\n\n`rewrite` is synonymous with `rw`.\n-/\nadd_tactic_doc\n  { Name := \"rw / rewrite\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.rw, `tactic.interactive.rewrite]\n    tags := [\"core\", \"basic\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"rwa\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.rwa]\n    tags := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"show\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.show]\n    tags := [\"core\", \"goal management\", \"renaming\"] }\n\nadd_tactic_doc\n  { Name := \"simp_intros\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.simp_intros]\n    tags := [\"core\", \"simplification\"] }\n\nadd_tactic_doc\n  { Name := \"skip\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.skip]\n    tags := [\"core\", \"combinator\"] }\n\nadd_tactic_doc\n  { Name := \"solve1\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.solve1]\n    tags := [\"core\", \"combinator\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"sorry / admit\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.sorry, `tactic.interactive.admit]\n    inheritDescriptionFrom := `tactic.interactive.sorry\n    tags := [\"core\", \"testing\", \"debugging\"] }\n\nadd_tactic_doc\n  { Name := \"specialize\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.specialize]\n    tags := [\"core\", \"context management\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"split\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.split]\n    tags := [\"core\", \"basic\", \"logic\"] }\n\nadd_tactic_doc\n  { Name := \"subst\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.subst]\n    tags := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"subst_vars\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.subst_vars]\n    tags := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"success_if_fail\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.success_if_fail]\n    tags := [\"core\", \"testing\", \"combinator\"] }\n\nadd_tactic_doc\n  { Name := \"suffices\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.suffices]\n    tags := [\"core\", \"basic\", \"goal management\"] }\n\nadd_tactic_doc\n  { Name := \"symmetry\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.symmetry]\n    tags := [\"core\", \"basic\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"trace\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.trace]\n    tags := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n  { Name := \"trace_simp_set\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.trace_simp_set]\n    tags := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n  { Name := \"trace_state\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.trace_state]\n    tags := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n  { Name := \"transitivity\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.transitivity]\n    tags := [\"core\", \"lemma application\"] }\n\nadd_tactic_doc\n  { Name := \"trivial\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.trivial]\n    tags := [\"core\", \"finishing\"] }\n\nadd_tactic_doc\n  { Name := \"try\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.try]\n    tags := [\"core\", \"combinator\"] }\n\nadd_tactic_doc\n  { Name := \"type_check\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.type_check]\n    tags := [\"core\", \"debugging\", \"testing\"] }\n\nadd_tactic_doc\n  { Name := \"unfold\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.unfold]\n    tags := [\"core\", \"basic\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"unfold1\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.unfold1]\n    tags := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"unfold_projs\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.unfold_projs]\n    tags := [\"core\", \"rewriting\"] }\n\nadd_tactic_doc\n  { Name := \"with_cases\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.with_cases]\n    tags := [\"core\", \"combinator\"] }\n\n-- conv mode tactics\n/-- Navigate to the left-hand-side of a relation.\nA goal of `| a = b` will turn into the goal `| a`.\n-/\nadd_tactic_doc\n  { Name := \"conv: to_lhs\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.to_lhs]\n    tags := [\"conv\"] }\n\n/-- Navigate to the right-hand-side of a relation.\nA goal of `| a = b` will turn into the goal `| b`.\n-/\nadd_tactic_doc\n  { Name := \"conv: to_rhs\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.to_rhs]\n    tags := [\"conv\"] }\n\n/-- Navigate into every argument of the current head function.\nA target of `| (a * b) * c` will turn into the two targets `| a * b` and `| c`.\n-/\nadd_tactic_doc\n  { Name := \"conv: congr\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.congr]\n    tags := [\"conv\"] }\n\n/-- Navigate into the contents of top-level `\u03bb` binders.\nA target of `| \u03bb a, a + b` will turn into the target `| a + b` and introduce `a` into the local\ncontext.\nIf there are multiple binders, all of them will be entered, and if there are none, this tactic is a\nno-op.\n-/\nadd_tactic_doc\n  { Name := \"conv: funext\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.funext]\n    tags := [\"conv\"] }\n\n/-- Navigate into the first scope matching the expression.\n\nFor a target of `| \u2200 c, a + (b + c) = 1`, `find (b + _) { ... }` will run the tactics within the\n`{}` with a target of `| b + c`.\n-/\nadd_tactic_doc\n  { Name := \"conv: find\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.find]\n    tags := [\"conv\"] }\n\n/-- Navigate into the numbered scopes matching the expression.\n\nFor a target of `| \u03bb c, 10 * c + 20 * c + 30 * c`, `for (_ * _) [1, 3] { ... }` will run the\ntactics within the `{}` with first a target of `| 10 * c`, then a target of `| 30 * c`.\n-/\nadd_tactic_doc\n  { Name := \"conv: for\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.for]\n    tags := [\"conv\"] }\n\n/-- End conversion of the current goal. This is often what is needed when muscle memory would type\n`sorry`.\n-/\nadd_tactic_doc\n  { Name := \"conv: skip\"\n    category := DocCategory.tactic\n    declNames := [`conv.interactive.skip]\n    tags := [\"conv\"] }\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/Tactic/LeanCoreDocs.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4726834766204328, "lm_q2_score": 0.13660840057146195, "lm_q1q2_score": 0.06457253371767535}}
{"text": "-- import the definition of the example maze\nimport maze_with_fire.definition\n\n/-!\n\n# Maze with fire.\n\nYou are in a maze of twisty passages, all distinct. \n\nYou can go north, south east or west.\n\nIf you fall into the fire, you will end up in the room of death,\nroom 5.\n-/\n\nnamespace maze\n\n/- Solver remark : there are 6 rooms. -/\n\n/-- Use n,s,e,w to move around. When you're at the exit, type `out`. -/\n\nexample : goal :=\nbegin\n  -- ready...\n  unfold goal,\n  -- go!\n  s,\n  n,\n  s,\n  e,\n  n,\n  sorry,\n  -- Don't forget the commas.\n  -- Don't bang into the walls -- those are errors.\n  -- When you get there, the tactic to get you out is `out`. \n  -- There is also a magic word, rumoured to be\n  -- an ancient translation of the word `sorry`.\nend\n\nend maze\n", "meta": {"author": "kbuzzard", "repo": "maze-game", "sha": "362ab394ae5218c43b24232aaeef08efc0067eab", "save_path": "github-repos/lean/kbuzzard-maze-game", "path": "github-repos/lean/kbuzzard-maze-game/maze-game-362ab394ae5218c43b24232aaeef08efc0067eab/src/maze_with_fire/level.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.48828339529583464, "lm_q2_score": 0.12940272991290905, "lm_q1q2_score": 0.0631852043224251}}
{"text": "example : 2 + 3 = 5 :=\nbegin\n  generalize h : 3 = x,\n  rw \u2190h\nend\n", "meta": {"author": "Ailrun", "repo": "Theorem_Proving_in_Lean", "sha": "2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68", "save_path": "github-repos/lean/Ailrun-Theorem_Proving_in_Lean", "path": "github-repos/lean/Ailrun-Theorem_Proving_in_Lean/Theorem_Proving_in_Lean-2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68/src/ch5/ex0219.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.48047867804790706, "lm_q2_score": 0.13117323564948064, "lm_q1q2_score": 0.06302594286012905}}
{"text": "/-\nCopyright (c) 2020 Floris van Doorn. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Floris van Doorn, Robert Y. Lewis\n-/\nimport tactic.lint.basic\n\n/-!\n# Various linters\n\nThis file defines several small linters:\n  - `ge_or_gt` checks that `>` and `\u2265` do not occur in the statement of theorems.\n  - `dup_namespace` checks that no declaration has a duplicated namespace such as `list.list.monad`.\n  - `unused_arguments` checks that definitions and theorems do not have unused arguments.\n  - `doc_blame` checks that every definition has a documentation string\n  - `doc_blame_thm` checks that every theorem has a documentation string (not enabled by default)\n  - `def_lemma` checks that a declaration is a lemma iff its type is a proposition.\n-/\n\nopen tactic expr\n\n/-!\n## Linter against use of `>`/`\u2265`\n-/\n/-- The names of `\u2265` and `>`, mostly disallowed in lemma statements -/\nprivate meta def illegal_ge_gt : list name := [`gt, `ge]\n\nset_option eqn_compiler.max_steps 20000\n/--\n  Checks whether `\u2265` and `>` occurs in an illegal way in the expression.\n  The main ways we legally use these orderings are:\n  - `f (\u2265)`\n  - `\u2203 x \u2265 t, b`. This corresponds to the expression\n    `@Exists \u03b1 (fun (x : \u03b1), (@Exists (x > t) (\u03bb (H : x > t), b)))`\n  This function returns `tt` when it finds `ge`/`gt`, except in the following patterns\n  (which are the same for `gt`):\n  - `f (@ge _ _)`\n  - `f (&0 \u2265 y) (\u03bb x : t, b)`\n  - `\u03bb H : &0 \u2265 t, b`\n  Here `&0` is the 0-th de Bruijn variable.\n-/\nprivate meta def contains_illegal_ge_gt : expr \u2192 bool\n| (const nm us) := if nm \u2208 illegal_ge_gt then tt else ff\n| (app f e@(app (app (const nm us) tp) tc)) :=\n  contains_illegal_ge_gt f || if nm \u2208 illegal_ge_gt then ff else contains_illegal_ge_gt e\n| (app (app custom_binder (app (app (app (app (const nm us) tp) tc) (var 0)) t))\n    e@(lam var_name bi var_type body)) :=\n  contains_illegal_ge_gt e || if nm \u2208 illegal_ge_gt then ff else contains_illegal_ge_gt e\n| (app f x) := contains_illegal_ge_gt f || contains_illegal_ge_gt x\n| (lam `H bi type@(app (app (app (app (const nm us) tp) tc) (var 0)) t) body) :=\n  contains_illegal_ge_gt body || if nm \u2208 illegal_ge_gt then ff else contains_illegal_ge_gt type\n| (lam var_name bi var_type body) := contains_illegal_ge_gt var_type || contains_illegal_ge_gt body\n| (pi `H bi type@(app (app (app (app (const nm us) tp) tc) (var 0)) t) body) :=\n  contains_illegal_ge_gt body || if nm \u2208 illegal_ge_gt then ff else contains_illegal_ge_gt type\n| (pi var_name bi var_type body) := contains_illegal_ge_gt var_type || contains_illegal_ge_gt body\n| (elet var_name type assignment body) :=\n  contains_illegal_ge_gt type || contains_illegal_ge_gt assignment || contains_illegal_ge_gt body\n| _ := ff\n\n/-- Checks whether a `>`/`\u2265` is used in the statement of `d`.\n\nIt first does a quick check to see if there is any `\u2265` or `>` in the statement, and then does a\nslower check whether the occurrences of `\u2265` and `>` are allowed.\nCurrently it checks only the conclusion of the declaration, to eliminate false positive from\nbinders such as `\u2200 \u03b5 > 0, ...` -/\nprivate meta def ge_or_gt_in_statement (d : declaration) : tactic (option string) :=\nreturn $ if d.type.contains_constant (\u03bb n, n \u2208 illegal_ge_gt) &&\n  contains_illegal_ge_gt d.type\n  then some \"the type contains \u2265/>. Use \u2264/< instead.\"\n  else none\n\n-- TODO: the commented out code also checks for classicality in statements, but needs fixing\n-- TODO: this probably needs to also check whether the argument is a variable or @eq <var> _ _\n-- meta def illegal_constants_in_statement (d : declaration) : tactic (option string) :=\n-- return $ if d.type.contains_constant (\u03bb n, (n.get_prefix = `classical \u2227\n--   n.last \u2208 [\"prop_decidable\", \"dec\", \"dec_rel\", \"dec_eq\"]) \u2228 n \u2208 [`gt, `ge])\n-- then\n--   let illegal1 := [`classical.prop_decidable, `classical.dec, `classical.dec_rel, `classical.dec_eq],\n--       illegal2 := [`gt, `ge],\n--       occur1 := illegal1.filter (\u03bb n, d.type.contains_constant (eq n)),\n--       occur2 := illegal2.filter (\u03bb n, d.type.contains_constant (eq n)) in\n--   some $ sformat!\"the type contains the following declarations: {occur1 ++ occur2}.\" ++\n--     (if occur1 = [] then \"\" else \" Add decidability type-class arguments instead.\") ++\n--     (if occur2 = [] then \"\" else \" Use \u2264/< instead.\")\n-- else none\n\n/-- A linter for checking whether illegal constants (\u2265, >) appear in a declaration's type. -/\n@[linter] meta def linter.ge_or_gt : linter :=\n{ test := ge_or_gt_in_statement,\n  auto_decls := ff,\n  no_errors_found := \"Not using \u2265/> in declarations\",\n  errors_found := \"The following declarations use \u2265/>, probably in a way where we would prefer\n  to use \u2264/< instead. See note [nolint_ge] for more information.\",\n  is_fast := ff }\n\n/--\nCurrently, the linter forbids the use of `>` and `\u2265` in definitions and\nstatements, as they cause problems in rewrites.\nThey are still allowed in statements such as `bounded (\u2265)` or `\u2200 \u03b5 > 0` or `\u2a06 n \u2265 m`,\nand the linter allows that.\nIf you write a pattern where you bind two or more variables, like `\u2203 n m > 0`, the linter will\nflag this as illegal, but it is also allowed. In this case, add the line\n```\n@[nolint ge_or_gt] -- see Note [nolint_ge]\n```\n-/\nlibrary_note \"nolint_ge\"\n\n/-!\n## Linter for duplicate namespaces\n-/\n\n/-- Checks whether a declaration has a namespace twice consecutively in its name -/\nprivate meta def dup_namespace (d : declaration) : tactic (option string) :=\nis_instance d.to_name >>= \u03bb is_inst,\nreturn $ let nm := d.to_name.components in if nm.chain' (\u2260) \u2228 is_inst then none\n  else let s := (nm.find $ \u03bb n, nm.count n \u2265 2).iget.to_string in\n  some $ \"The namespace `\" ++ s ++ \"` is duplicated in the name\"\n\n/-- A linter for checking whether a declaration has a namespace twice consecutively in its name. -/\n@[linter] meta def linter.dup_namespace : linter :=\n{ test := dup_namespace,\n  auto_decls := ff,\n  no_errors_found := \"No declarations have a duplicate namespace\",\n  errors_found := \"DUPLICATED NAMESPACES IN NAME\" }\n\n\n\n/-!\n## Linter for unused arguments\n-/\n\n/-- Auxilliary definition for `check_unused_arguments` -/\nprivate meta def check_unused_arguments_aux : list \u2115 \u2192 \u2115 \u2192 \u2115 \u2192 expr \u2192 list \u2115 | l n n_max e :=\nif n > n_max then l else\nif \u00ac is_lambda e \u2227 \u00ac is_pi e then l else\n  let b := e.binding_body in\n  let l' := if b.has_var_idx 0 then l else n :: l in check_unused_arguments_aux l' (n+1) n_max b\n\n/-- Check which arguments of a declaration are not used.\nPrints a list of natural numbers corresponding to which arguments are not used (e.g.\n  this outputs [1, 4] if the first and fourth arguments are unused).\nChecks both the type and the value of `d` for whether the argument is used\n(in rare cases an argument is used in the type but not in the value).\nWe return [] if the declaration was automatically generated.\nWe print arguments that are larger than the arity of the type of the declaration\n(without unfolding definitions). -/\nmeta def check_unused_arguments (d : declaration) : option (list \u2115) :=\nlet l := check_unused_arguments_aux [] 1 d.type.pi_arity d.value in\nif l = [] then none else\nlet l2 := check_unused_arguments_aux [] 1 d.type.pi_arity d.type in\n(l.filter $ \u03bb n, n \u2208 l2).reverse\n\n/-- Check for unused arguments, and print them with their position, variable name, type and whether\nthe argument is a duplicate.\nSee also `check_unused_arguments`.\nThis tactic additionally filters out all unused arguments of type `parse _`. -/\nprivate meta def unused_arguments (d : declaration) : tactic (option string) := do\n  let ns := check_unused_arguments d,\n  if \u00ac ns.is_some then return none else do\n  let ns := ns.iget,\n  (ds, _) \u2190 get_pi_binders d.type,\n  let ns := ns.map (\u03bb n, (n, (ds.nth $ n - 1).iget)),\n  let ns := ns.filter (\u03bb x, x.2.type.get_app_fn \u2260 const `interactive.parse []),\n  if ns = [] then return none else do\n  ds' \u2190 ds.mmap pp,\n  ns \u2190 ns.mmap (\u03bb \u27e8n, b\u27e9, (\u03bb s, to_fmt \"argument \" ++ to_fmt n ++ \": \" ++ s ++\n    (if ds.countp (\u03bb b', b.type = b'.type) \u2265 2 then \" (duplicate)\" else \"\")) <$> pp b),\n  return $ some $ ns.to_string_aux tt\n\n/-- A linter object for checking for unused arguments. This is in the default linter set. -/\n@[linter] meta def linter.unused_arguments : linter :=\n{ test := unused_arguments,\n  auto_decls := ff,\n  no_errors_found := \"No unused arguments\",\n  errors_found := \"UNUSED ARGUMENTS\" }\n\nattribute [nolint unused_arguments] imp_intro\n\n\n\n/-!\n## Linter for documentation strings\n-/\n\n/-- Reports definitions and constants that are missing doc strings -/\nprivate meta def doc_blame_report_defn : declaration \u2192 tactic (option string)\n| (declaration.defn n _ _ _ _ _) := doc_string n >> return none <|> return \"def missing doc string\"\n| (declaration.cnst n _ _ _) := doc_string n >> return none <|> return \"constant missing doc string\"\n| _ := return none\n\n/-- Reports definitions and constants that are missing doc strings -/\nprivate meta def doc_blame_report_thm : declaration \u2192 tactic (option string)\n| (declaration.thm n _ _ _) := doc_string n >> return none <|> return \"theorem missing doc string\"\n| _ := return none\n\n/-- A linter for checking definition doc strings -/\n@[linter] meta def linter.doc_blame : linter :=\n{ test := \u03bb d, mcond (bnot <$> has_attribute' `instance d.to_name)\n    (doc_blame_report_defn d) (return none),\n  auto_decls := ff,\n  no_errors_found := \"No definitions are missing documentation.\",\n  errors_found := \"DEFINITIONS ARE MISSING DOCUMENTATION STRINGS\" }\n\n/-- A linter for checking theorem doc strings. This is not in the default linter set. -/\nmeta def linter.doc_blame_thm : linter :=\n{ test := doc_blame_report_thm,\n  auto_decls := ff,\n  no_errors_found := \"No theorems are missing documentation.\",\n  errors_found := \"THEOREMS ARE MISSING DOCUMENTATION STRINGS\",\n  is_fast := ff }\n\n\n\n/-!\n## Linter for correct usage of `lemma`/`def`\n-/\n\n/--\nChecks whether the correct declaration constructor (definition or theorem) by\ncomparing it to its sort. Instances will not be printed.\n\nThis test is not very quick: maybe we can speed-up testing that something is a proposition?\nThis takes almost all of the execution time.\n-/\nprivate meta def incorrect_def_lemma (d : declaration) : tactic (option string) :=\n  if d.is_constant \u2228 d.is_axiom\n  then return none else do\n    is_instance_d \u2190 is_instance d.to_name,\n    if is_instance_d then return none else do\n      -- the following seems to be a little quicker than `is_prop d.type`.\n      expr.sort n \u2190 infer_type d.type, return $\n      if d.is_theorem \u2194 n = level.zero then none\n      else if (d.is_definition : bool) then \"is a def, should be a lemma/theorem\"\n      else \"is a lemma/theorem, should be a def\"\n\n/-- A linter for checking whether the correct declaration constructor (definition or theorem)\nhas been used. -/\n@[linter] meta def linter.def_lemma : linter :=\n{ test := incorrect_def_lemma,\n  auto_decls := ff,\n  no_errors_found := \"All declarations correctly marked as def/lemma\",\n  errors_found := \"INCORRECT DEF/LEMMA\" }\n\nattribute [nolint def_lemma] classical.dec classical.dec_pred classical.dec_rel classical.dec_eq\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/tactic/lint/misc.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4921881357207956, "lm_q2_score": 0.12765262366243563, "lm_q1q2_score": 0.06282910686028251}}
{"text": "/-\nCopyright (c) 2019 Patrick Massot. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Patrick Massot\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.interactive\nimport Mathlib.PostPort\n\nnamespace Mathlib\n\n/-!\n# Rename bound variable tactic\n\nThis files defines a tactic `rename_var` whose main purpose is to teach\nrenaming of bound variables.\n\n* `rename_var old new` renames all bound variables named `old` to `new` in the goal.\n* `rename_var old new at h` does the same in hypothesis `h`.\n\n```lean\nexample (P : \u2115 \u2192  \u2115 \u2192 Prop) (h : \u2200 n, \u2203 m, P n m) : \u2200 l, \u2203 m, P l m :=\nbegin\n  rename_var n q at h, -- h is now \u2200 (q : \u2115), \u2203 (m : \u2115), P q m,\n  rename_var m n, -- goal is now \u2200 (l : \u2115), \u2203 (n : \u2115), P k n,\n  exact h -- Lean does not care about those bound variable names\nend\n```\n\n## Tags\n\nteaching, tactic\n-/\n\n/-- Rename bound variable `old` to `new` in an `expr`-/\nnamespace tactic\n\n\n/-- Rename bound variable `old` to `new` in goal -/\n/-- Rename bound variable `old` to `new` in assumption `h` -/\nend tactic\n\n\nnamespace tactic.interactive\n\n\n/--\n`rename_var old new` renames all bound variables named `old` to `new` in the goal.\n`rename_var old new at h` does the same in hypothesis `h`.\n-/\nend tactic.interactive\n\n\n/--\n`rename_var old new` renames all bound variables named `old` to `new` in the goal.\n", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/tactic/rename_var.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.3812195521959383, "lm_q2_score": 0.16451645675203383, "lm_q1q2_score": 0.06271688997187279}}
{"text": "/-\nCopyright (c) 2014 Microsoft Corporation. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Leonardo de Moura\n\nnotation, basic datatypes and type classes\n-/\nprelude\nimport Init.Prelude\nimport Init.SizeOf\nset_option linter.missingDocs true -- keep it documented\n\nuniverse u v w\n\n/--\n`inline (f x)` is an indication to the compiler to inline the definition of `f`\nat the application site itself (by comparison to the `@[inline]` attribute,\nwhich applies to all applications of the function).\n-/\ndef inline {\u03b1 : Sort u} (a : \u03b1) : \u03b1 := a\n\n/--\n`flip f a b` is `f b a`. It is useful for \"point-free\" programming,\nsince it can sometimes be used to avoid introducing variables.\nFor example, `(\u00b7<\u00b7)` is the less-than relation,\nand `flip (\u00b7<\u00b7)` is the greater-than relation.\n-/\n@[inline] def flip {\u03b1 : Sort u} {\u03b2 : Sort v} {\u03c6 : Sort w} (f : \u03b1 \u2192 \u03b2 \u2192 \u03c6) : \u03b2 \u2192 \u03b1 \u2192 \u03c6 :=\n  fun b a => f a b\n\n@[simp] theorem Function.const_apply {y : \u03b2} {x : \u03b1} : const \u03b1 y x = y := rfl\n\n@[simp] theorem Function.comp_apply {f : \u03b2 \u2192 \u03b4} {g : \u03b1 \u2192 \u03b2} {x : \u03b1} : comp f g x = f (g x) := rfl\n\nattribute [simp] namedPattern\n\n/--\n  Thunks are \"lazy\" values that are evaluated when first accessed using `Thunk.get/map/bind`.\n  The value is then stored and not recomputed for all further accesses. -/\n-- NOTE: the runtime has special support for the `Thunk` type to implement this behavior\nstructure Thunk (\u03b1 : Type u) : Type u where\n  /-- Constructs a new thunk from a function `Unit \u2192 \u03b1`\n  that will be called when the thunk is forced. -/\n  mk ::\n  /-- Extract the getter function out of a thunk. Use `Thunk.get` instead. -/\n  private fn : Unit \u2192 \u03b1\n\nattribute [extern \"lean_mk_thunk\"] Thunk.mk\n\n/-- Store a value in a thunk. Note that the value has already been computed, so there is no laziness. -/\n@[extern \"lean_thunk_pure\"] protected def Thunk.pure (a : \u03b1) : Thunk \u03b1 :=\n  \u27e8fun _ => a\u27e9\n\n/--\nForces a thunk to extract the value. This will cache the result,\nso a second call to the same function will return the value in O(1)\ninstead of calling the stored getter function.\n-/\n-- NOTE: we use `Thunk.get` instead of `Thunk.fn` as the accessor primitive as the latter has an additional `Unit` argument\n@[extern \"lean_thunk_get_own\"] protected def Thunk.get (x : @& Thunk \u03b1) : \u03b1 :=\n  x.fn ()\n\n/-- Map a function over a thunk. -/\n@[inline] protected def Thunk.map (f : \u03b1 \u2192 \u03b2) (x : Thunk \u03b1) : Thunk \u03b2 :=\n  \u27e8fun _ => f x.get\u27e9\n/-- Constructs a thunk that applies `f` to the result of `x` when forced. -/\n@[inline] protected def Thunk.bind (x : Thunk \u03b1) (f : \u03b1 \u2192 Thunk \u03b2) : Thunk \u03b2 :=\n  \u27e8fun _ => (f x.get).get\u27e9\n\n@[simp] theorem Thunk.sizeOf_eq [SizeOf \u03b1] (a : Thunk \u03b1) : sizeOf a = 1 + sizeOf a.get := by\n   cases a; rfl\n\ninstance thunkCoe : CoeTail \u03b1 (Thunk \u03b1) where\n  -- Since coercions are expanded eagerly, `a` is evaluated lazily.\n  coe a := \u27e8fun _ => a\u27e9\n\n/-- A variation on `Eq.ndrec` with the equality argument first. -/\nabbrev Eq.ndrecOn.{u1, u2} {\u03b1 : Sort u2} {a : \u03b1} {motive : \u03b1 \u2192 Sort u1} {b : \u03b1} (h : a = b) (m : motive a) : motive b :=\n  Eq.ndrec m h\n\n/--\nIf and only if, or logical bi-implication. `a \u2194 b` means that `a` implies `b` and vice versa.\nBy `propext`, this implies that `a` and `b` are equal and hence any expression involving `a`\nis equivalent to the corresponding expression with `b` instead.\n-/\nstructure Iff (a b : Prop) : Prop where\n  /-- If `a \u2192 b` and `b \u2192 a` then `a` and `b` are equivalent. -/\n  intro ::\n  /-- Modus ponens for if and only if. If `a \u2194 b` and `a`, then `b`. -/\n  mp : a \u2192 b\n  /-- Modus ponens for if and only if, reversed. If `a \u2194 b` and `b`, then `a`. -/\n  mpr : b \u2192 a\n\n@[inherit_doc] infix:20 \" <-> \" => Iff\n@[inherit_doc] infix:20 \" \u2194 \"   => Iff\n\n/--\n`Sum \u03b1 \u03b2`, or `\u03b1 \u2295 \u03b2`, is the disjoint union of types `\u03b1` and `\u03b2`.\nAn element of `\u03b1 \u2295 \u03b2` is either of the form `.inl a` where `a : \u03b1`,\nor `.inr b` where `b : \u03b2`.\n-/\ninductive Sum (\u03b1 : Type u) (\u03b2 : Type v) where\n  /-- Left injection into the sum type `\u03b1 \u2295 \u03b2`. If `a : \u03b1` then `.inl a : \u03b1 \u2295 \u03b2`. -/\n  | inl (val : \u03b1) : Sum \u03b1 \u03b2\n  /-- Right injection into the sum type `\u03b1 \u2295 \u03b2`. If `b : \u03b2` then `.inr b : \u03b1 \u2295 \u03b2`. -/\n  | inr (val : \u03b2) : Sum \u03b1 \u03b2\n\n@[inherit_doc] infixr:30 \" \u2295 \" => Sum\n\n/--\n`PSum \u03b1 \u03b2`, or `\u03b1 \u2295' \u03b2`, is the disjoint union of types `\u03b1` and `\u03b2`.\nIt differs from `\u03b1 \u2295 \u03b2` in that it allows `\u03b1` and `\u03b2` to have arbitrary sorts\n`Sort u` and `Sort v`, instead of restricting to `Type u` and `Type v`. This means\nthat it can be used in situations where one side is a proposition, like `True \u2295' Nat`.\n\nThe reason this is not the default is that this type lives in the universe `Sort (max 1 u v)`,\nwhich can cause problems for universe level unification,\nbecause the equation `max 1 u v = ?u + 1` has no solution in level arithmetic.\n`PSum` is usually only used in automation that constructs sums of arbitrary types.\n-/\ninductive PSum (\u03b1 : Sort u) (\u03b2 : Sort v) where\n  /-- Left injection into the sum type `\u03b1 \u2295' \u03b2`. If `a : \u03b1` then `.inl a : \u03b1 \u2295' \u03b2`. -/\n  | inl (val : \u03b1) : PSum \u03b1 \u03b2\n  /-- Right injection into the sum type `\u03b1 \u2295' \u03b2`. If `b : \u03b2` then `.inr b : \u03b1 \u2295' \u03b2`. -/\n  | inr (val : \u03b2) : PSum \u03b1 \u03b2\n\n@[inherit_doc] infixr:30 \" \u2295' \" => PSum\n\n/--\n`Sigma \u03b2`, also denoted `\u03a3 a : \u03b1, \u03b2 a` or `(a : \u03b1) \u00d7 \u03b2 a`, is the type of dependent pairs\nwhose first component is `a : \u03b1` and whose second component is `b : \u03b2 a`\n(so the type of the second component can depend on the value of the first component).\nIt is sometimes known as the dependent sum type, since it is the type level version\nof an indexed summation.\n-/\nstructure Sigma {\u03b1 : Type u} (\u03b2 : \u03b1 \u2192 Type v) where\n  /-- Constructor for a dependent pair. If `a : \u03b1` and `b : \u03b2 a` then `\u27e8a, b\u27e9 : Sigma \u03b2`.\n  (This will usually require a type ascription to determine `\u03b2`\n  since it is not determined from `a` and `b` alone.) -/\n  mk ::\n  /-- The first component of a dependent pair. If `p : @Sigma \u03b1 \u03b2` then `p.1 : \u03b1`. -/\n  fst : \u03b1\n  /-- The second component of a dependent pair. If `p : Sigma \u03b2` then `p.2 : \u03b2 p.1`. -/\n  snd : \u03b2 fst\n\nattribute [unbox] Sigma\n\n/--\n`PSigma \u03b2`, also denoted `\u03a3' a : \u03b1, \u03b2 a` or `(a : \u03b1) \u00d7' \u03b2 a`, is the type of dependent pairs\nwhose first component is `a : \u03b1` and whose second component is `b : \u03b2 a`\n(so the type of the second component can depend on the value of the first component).\nIt differs from `\u03a3 a : \u03b1, \u03b2 a` in that it allows `\u03b1` and `\u03b2` to have arbitrary sorts\n`Sort u` and `Sort v`, instead of restricting to `Type u` and `Type v`. This means\nthat it can be used in situations where one side is a proposition, like `(p : Nat) \u00d7' p = p`.\n\nThe reason this is not the default is that this type lives in the universe `Sort (max 1 u v)`,\nwhich can cause problems for universe level unification,\nbecause the equation `max 1 u v = ?u + 1` has no solution in level arithmetic.\n`PSigma` is usually only used in automation that constructs pairs of arbitrary types.\n-/\nstructure PSigma {\u03b1 : Sort u} (\u03b2 : \u03b1 \u2192 Sort v) where\n  /-- Constructor for a dependent pair. If `a : \u03b1` and `b : \u03b2 a` then `\u27e8a, b\u27e9 : PSigma \u03b2`.\n  (This will usually require a type ascription to determine `\u03b2`\n  since it is not determined from `a` and `b` alone.) -/\n  mk ::\n  /-- The first component of a dependent pair. If `p : @Sigma \u03b1 \u03b2` then `p.1 : \u03b1`. -/\n  fst : \u03b1\n  /-- The second component of a dependent pair. If `p : Sigma \u03b2` then `p.2 : \u03b2 p.1`. -/\n  snd : \u03b2 fst\n\n/--\nExistential quantification. If `p : \u03b1 \u2192 Prop` is a predicate, then `\u2203 x : \u03b1, p x`\nasserts that there is some `x` of type `\u03b1` such that `p x` holds.\nTo create an existential proof, use the `exists` tactic,\nor the anonymous constructor notation `\u27e8x, h\u27e9`.\nTo unpack an existential, use `cases h` where `h` is a proof of `\u2203 x : \u03b1, p x`,\nor `let \u27e8x, hx\u27e9 := h` where `.\n\nBecause Lean has proof irrelevance, any two proofs of an existential are\ndefinitionally equal. One consequence of this is that it is impossible to recover the\nwitness of an existential from the mere fact of its existence.\nFor example, the following does not compile:\n```\nexample (h : \u2203 x : Nat, x = x) : Nat :=\n  let \u27e8x, _\u27e9 := h  -- fail, because the goal is `Nat : Type`\n  x\n```\nThe error message `recursor 'Exists.casesOn' can only eliminate into Prop` means\nthat this only works when the current goal is another proposition:\n```\nexample (h : \u2203 x : Nat, x = x) : True :=\n  let \u27e8x, _\u27e9 := h  -- ok, because the goal is `True : Prop`\n  trivial\n```\n-/\ninductive Exists {\u03b1 : Sort u} (p : \u03b1 \u2192 Prop) : Prop where\n  /-- Existential introduction. If `a : \u03b1` and `h : p a`,\n  then `\u27e8a, h\u27e9` is a proof that `\u2203 x : \u03b1, p x`. -/\n  | intro (w : \u03b1) (h : p w) : Exists p\n\n/--\nAuxiliary type used to compile `for x in xs` notation.\n\nThis is the return value of the body of a `ForIn` call,\nrepresenting the body of a for loop. It can be:\n\n* `.yield (a : \u03b1)`, meaning that we should continue the loop and `a` is the new state.\n  `.yield` is produced by `continue` and reaching the bottom of the loop body.\n* `.done (a : \u03b1)`, meaning that we should early-exit the loop with state `a`.\n  `.done` is produced by calls to `break` or `return` in the loop,\n-/\ninductive ForInStep (\u03b1 : Type u) where\n  /-- `.done a` means that we should early-exit the loop.\n  `.done` is produced by calls to `break` or `return` in the loop. -/\n  | done  : \u03b1 \u2192 ForInStep \u03b1\n  /-- `.yield a` means that we should continue the loop.\n  `.yield` is produced by `continue` and reaching the bottom of the loop body. -/\n  | yield : \u03b1 \u2192 ForInStep \u03b1\n  deriving Inhabited\n\n/--\n`ForIn m \u03c1 \u03b1` is the typeclass which supports `for x in xs` notation.\nHere `xs : \u03c1` is the type of the collection to iterate over, `x : \u03b1`\nis the element type which is made available inside the loop, and `m` is the monad\nfor the encompassing `do` block.\n-/\nclass ForIn (m : Type u\u2081 \u2192 Type u\u2082) (\u03c1 : Type u) (\u03b1 : outParam (Type v)) where\n  /-- `forIn x b f : m \u03b2` runs a for-loop in the monad `m` with additional state `\u03b2`.\n  This traverses over the \"contents\" of `x`, and passes the elements `a : \u03b1` to\n  `f : \u03b1 \u2192 \u03b2 \u2192 m (ForInStep \u03b2)`. `b : \u03b2` is the initial state, and the return value\n  of `f` is the new state as well as a directive `.done` or `.yield`\n  which indicates whether to abort early or continue iteration.\n\n  The expression\n  ```\n  let mut b := ...\n  for x in xs do\n    b \u2190 foo x b\n  ```\n  in a `do` block is syntactic sugar for:\n  ```\n  let b := ...\n  let b \u2190 forIn xs b (fun x b => do\n    let b \u2190 foo x b\n    return .yield b)\n  ```\n  (Here `b` corresponds to the variables mutated in the loop.) -/\n  forIn {\u03b2} [Monad m] (x : \u03c1) (b : \u03b2) (f : \u03b1 \u2192 \u03b2 \u2192 m (ForInStep \u03b2)) : m \u03b2\n\nexport ForIn (forIn)\n\n/--\n`ForIn' m \u03c1 \u03b1 d` is a variation on the `ForIn m \u03c1 \u03b1` typeclass which supports the\n`for h : x in xs` notation. It is the same as `for x in xs` except that `h : x \u2208 xs`\nis provided as an additional argument to the body of the for-loop.\n-/\nclass ForIn' (m : Type u\u2081 \u2192 Type u\u2082) (\u03c1 : Type u) (\u03b1 : outParam (Type v)) (d : outParam $ Membership \u03b1 \u03c1) where\n  /-- `forIn' x b f : m \u03b2` runs a for-loop in the monad `m` with additional state `\u03b2`.\n  This traverses over the \"contents\" of `x`, and passes the elements `a : \u03b1` along\n  with a proof that `a \u2208 x` to `f : (a : \u03b1) \u2192 a \u2208 x \u2192 \u03b2 \u2192 m (ForInStep \u03b2)`.\n  `b : \u03b2` is the initial state, and the return value\n  of `f` is the new state as well as a directive `.done` or `.yield`\n  which indicates whether to abort early or continue iteration. -/\n  forIn' {\u03b2} [Monad m] (x : \u03c1) (b : \u03b2) (f : (a : \u03b1) \u2192 a \u2208 x \u2192 \u03b2 \u2192 m (ForInStep \u03b2)) : m \u03b2\n\nexport ForIn' (forIn')\n\n\n/--\nAuxiliary type used to compile `do` notation. It is used when compiling a do block\nnested inside a combinator like `tryCatch`. It encodes the possible ways the\nblock can exit:\n* `pure (a : \u03b1) s` means that the block exited normally with return value `a`.\n* `return (b : \u03b2) s` means that the block exited via a `return b` early-exit command.\n* `break s` means that `break` was called, meaning that we should exit\n  from the containing loop.\n* `continue s` means that `continue` was called, meaning that we should continue\n  to the next iteration of the containing loop.\n\nAll cases return a value `s : \u03c3` which bundles all the mutable variables of the do-block.\n-/\ninductive DoResultPRBC (\u03b1 \u03b2 \u03c3 : Type u) where\n  /-- `pure (a : \u03b1) s` means that the block exited normally with return value `a` -/\n  | pure : \u03b1 \u2192 \u03c3 \u2192 DoResultPRBC \u03b1 \u03b2 \u03c3\n  /-- `return (b : \u03b2) s` means that the block exited via a `return b` early-exit command -/\n  | return : \u03b2 \u2192 \u03c3 \u2192 DoResultPRBC \u03b1 \u03b2 \u03c3\n  /-- `break s` means that `break` was called, meaning that we should exit\n  from the containing loop -/\n  | break : \u03c3 \u2192 DoResultPRBC \u03b1 \u03b2 \u03c3\n  /-- `continue s` means that `continue` was called, meaning that we should continue\n  to the next iteration of the containing loop -/\n  | continue : \u03c3 \u2192 DoResultPRBC \u03b1 \u03b2 \u03c3\n\n/--\nAuxiliary type used to compile `do` notation. It is the same as\n`DoResultPRBC \u03b1 \u03b2 \u03c3` except that `break` and `continue` are not available\nbecause we are not in a loop context.\n-/\ninductive DoResultPR (\u03b1 \u03b2 \u03c3 : Type u) where\n  /-- `pure (a : \u03b1) s` means that the block exited normally with return value `a` -/\n  | pure   : \u03b1 \u2192 \u03c3 \u2192 DoResultPR \u03b1 \u03b2 \u03c3\n  /-- `return (b : \u03b2) s` means that the block exited via a `return b` early-exit command -/\n  | return : \u03b2 \u2192 \u03c3 \u2192 DoResultPR \u03b1 \u03b2 \u03c3\n\n/--\nAuxiliary type used to compile `do` notation. It is an optimization of\n`DoResultPRBC PEmpty PEmpty \u03c3` to remove the impossible cases,\nused when neither `pure` nor `return` are possible exit paths.\n-/\ninductive DoResultBC (\u03c3 : Type u) where\n  /-- `break s` means that `break` was called, meaning that we should exit\n  from the containing loop -/\n  | break    : \u03c3 \u2192 DoResultBC \u03c3\n  /-- `continue s` means that `continue` was called, meaning that we should continue\n  to the next iteration of the containing loop -/\n  | continue : \u03c3 \u2192 DoResultBC \u03c3\n\n/--\nAuxiliary type used to compile `do` notation. It is an optimization of\neither `DoResultPRBC \u03b1 PEmpty \u03c3` or `DoResultPRBC PEmpty \u03b1 \u03c3` to remove the\nimpossible case, used when either `pure` or `return` is never used.\n-/\ninductive DoResultSBC (\u03b1 \u03c3 : Type u) where\n  /-- This encodes either `pure (a : \u03b1)` or `return (a : \u03b1)`:\n  * `pure (a : \u03b1) s` means that the block exited normally with return value `a`\n  * `return (b : \u03b2) s` means that the block exited via a `return b` early-exit command\n\n  The one that is actually encoded depends on the context of use. -/\n  | pureReturn : \u03b1 \u2192 \u03c3 \u2192 DoResultSBC \u03b1 \u03c3\n  /-- `break s` means that `break` was called, meaning that we should exit\n  from the containing loop -/\n  | break    : \u03c3 \u2192 DoResultSBC \u03b1 \u03c3\n  /-- `continue s` means that `continue` was called, meaning that we should continue\n  to the next iteration of the containing loop -/\n  | continue   : \u03c3 \u2192 DoResultSBC \u03b1 \u03c3\n\n/-- `HasEquiv \u03b1` is the typeclass which supports the notation `x \u2248 y` where `x y : \u03b1`.-/\nclass HasEquiv (\u03b1 : Sort u) where\n  /-- `x \u2248 y` says that `x` and `y` are equivalent. Because this is a typeclass,\n  the notion of equivalence is type-dependent. -/\n  Equiv : \u03b1 \u2192 \u03b1 \u2192 Sort v\n\n@[inherit_doc] infix:50 \" \u2248 \"  => HasEquiv.Equiv\n\n/-- `EmptyCollection \u03b1` is the typeclass which supports the notation `\u2205`, also written as `{}`. -/\nclass EmptyCollection (\u03b1 : Type u) where\n  /-- `\u2205` or `{}` is the empty set or empty collection.\n  It is supported by the `EmptyCollection` typeclass. -/\n  emptyCollection : \u03b1\n\n@[inherit_doc] notation \"{\" \"}\" => EmptyCollection.emptyCollection\n@[inherit_doc] notation \"\u2205\"     => EmptyCollection.emptyCollection\n\n/--\n`Task \u03b1` is a primitive for asynchronous computation.\nIt represents a computation that will resolve to a value of type `\u03b1`,\npossibly being computed on another thread. This is similar to `Future` in Scala,\n`Promise` in Javascript, and `JoinHandle` in Rust.\n\nThe tasks have an overridden representation in the runtime.\n-/\nstructure Task (\u03b1 : Type u) : Type u where\n  /-- `Task.pure (a : \u03b1)` constructs a task that is already resolved with value `a`. -/\n  pure ::\n  /-- If `task : Task \u03b1` then `task.get : \u03b1` blocks the current thread until the\n  value is available, and then returns the result of the task. -/\n  get : \u03b1\n  deriving Inhabited, Nonempty\n\nattribute [extern \"lean_task_pure\"] Task.pure\nattribute [extern \"lean_task_get_own\"] Task.get\n\nnamespace Task\n/-- Task priority. Tasks with higher priority will always be scheduled before ones with lower priority. -/\nabbrev Priority := Nat\n\n/-- The default priority for spawned tasks, also the lowest priority: `0`. -/\ndef Priority.default : Priority := 0\n/--\nThe highest regular priority for spawned tasks: `8`.\n\nSpawning a task with a priority higher than `Task.Priority.max` is not an error but\nwill spawn a dedicated worker for the task, see `Task.Priority.dedicated`.\nRegular priority tasks are placed in a thread pool and worked on according to the priority order.\n-/\n-- see `LEAN_MAX_PRIO`\ndef Priority.max : Priority := 8\n/--\nAny priority higher than `Task.Priority.max` will result in the task being scheduled\nimmediately on a dedicated thread. This is particularly useful for long-running and/or\nI/O-bound tasks since Lean will by default allocate no more non-dedicated workers\nthan the number of cores to reduce context switches.\n-/\ndef Priority.dedicated : Priority := 9\n\nset_option linter.unusedVariables.funArgs false in\n/--\n`spawn fn : Task \u03b1` constructs and immediately launches a new task for\nevaluating the function `fn () : \u03b1` asynchronously.\n\n`prio`, if provided, is the priority of the task.\n-/\n@[noinline, extern \"lean_task_spawn\"]\nprotected def spawn {\u03b1 : Type u} (fn : Unit \u2192 \u03b1) (prio := Priority.default) : Task \u03b1 :=\n  \u27e8fn ()\u27e9\n\nset_option linter.unusedVariables.funArgs false in\n/--\n`map f x` maps function `f` over the task `x`: that is, it constructs\n(and immediately launches) a new task which will wait for the value of `x` to\nbe available and then calls `f` on the result.\n\n`prio`, if provided, is the priority of the task.\n-/\n@[noinline, extern \"lean_task_map\"]\nprotected def map {\u03b1 : Type u} {\u03b2 : Type v} (f : \u03b1 \u2192 \u03b2) (x : Task \u03b1) (prio := Priority.default) : Task \u03b2 :=\n  \u27e8f x.get\u27e9\n\nset_option linter.unusedVariables.funArgs false in\n/--\n`bind x f` does a monad \"bind\" operation on the task `x` with function `f`:\nthat is, it constructs (and immediately launches) a new task which will wait\nfor the value of `x` to be available and then calls `f` on the result,\nresulting in a new task which is then run for a result.\n\n`prio`, if provided, is the priority of the task.\n-/\n@[noinline, extern \"lean_task_bind\"]\nprotected def bind {\u03b1 : Type u} {\u03b2 : Type v} (x : Task \u03b1) (f : \u03b1 \u2192 Task \u03b2) (prio := Priority.default) : Task \u03b2 :=\n  \u27e8(f x.get).get\u27e9\n\nend Task\n\n/--\n`NonScalar` is a type that is not a scalar value in our runtime.\nIt is used as a stand-in for an arbitrary boxed value to avoid excessive\nmonomorphization, and it is only created using `unsafeCast`. It is somewhat\nanalogous to C `void*` in usage, but the type itself is not special.\n-/\nstructure NonScalar where\n  /-- You should not use this function -/ mk ::\n  /-- You should not use this function -/ val : Nat\n\n/--\n`PNonScalar` is a type that is not a scalar value in our runtime.\nIt is used as a stand-in for an arbitrary boxed value to avoid excessive\nmonomorphization, and it is only created using `unsafeCast`. It is somewhat\nanalogous to C `void*` in usage, but the type itself is not special.\n\nThis is the universe-polymorphic version of `PNonScalar`; it is preferred to use\n`NonScalar` instead where applicable.\n-/\ninductive PNonScalar : Type u where\n  /-- You should not use this function -/\n  | mk (v : Nat) : PNonScalar\n\n@[simp] protected theorem Nat.add_zero (n : Nat) : n + 0 = n := rfl\n\ntheorem optParam_eq (\u03b1 : Sort u) (default : \u03b1) : optParam \u03b1 default = \u03b1 := rfl\n\n/-! # Boolean operators -/\n\n/--\n`strictOr` is the same as `or`, but it does not use short-circuit evaluation semantics:\nboth sides are evaluated, even if the first value is `true`.\n-/\n@[extern c inline \"#1 || #2\"] def strictOr  (b\u2081 b\u2082 : Bool) := b\u2081 || b\u2082\n\n/--\n`strictAnd` is the same as `and`, but it does not use short-circuit evaluation semantics:\nboth sides are evaluated, even if the first value is `false`.\n-/\n@[extern c inline \"#1 && #2\"] def strictAnd (b\u2081 b\u2082 : Bool) := b\u2081 && b\u2082\n\n/--\n`x != y` is boolean not-equal. It is the negation of `x == y` which is supplied by\nthe `BEq` typeclass.\n\nUnlike `x \u2260 y` (which is notation for `Ne x y`), this is `Bool` valued instead of\n`Prop` valued. It is mainly intended for programming applications.\n-/\n@[inline] def bne {\u03b1 : Type u} [BEq \u03b1] (a b : \u03b1) : Bool :=\n  !(a == b)\n\n@[inherit_doc] infix:50 \" != \" => bne\n\n/--\n`LawfulBEq \u03b1` is a typeclass which asserts that the `BEq \u03b1` implementation\n(which supplies the `a == b` notation) coincides with logical equality `a = b`.\nIn other words, `a == b` implies `a = b`, and `a == a` is true.\n-/\nclass LawfulBEq (\u03b1 : Type u) [BEq \u03b1] : Prop where\n  /-- If `a == b` evaluates to `true`, then `a` and `b` are equal in the logic. -/\n  eq_of_beq : {a b : \u03b1} \u2192 a == b \u2192 a = b\n  /-- `==` is reflexive, that is, `(a == a) = true`. -/\n  protected rfl : {a : \u03b1} \u2192 a == a\n\nexport LawfulBEq (eq_of_beq)\n\ninstance : LawfulBEq Bool where\n  eq_of_beq {a b} h := by cases a <;> cases b <;> first | rfl | contradiction\n  rfl {a} := by cases a <;> decide\n\ninstance [DecidableEq \u03b1] : LawfulBEq \u03b1 where\n  eq_of_beq := of_decide_eq_true\n  rfl := of_decide_eq_self_eq_true _\n\ninstance : LawfulBEq Char := inferInstance\n\ninstance : LawfulBEq String := inferInstance\n\n/-! # Logical connectives and equality -/\n\n@[inherit_doc True.intro] def trivial : True := \u27e8\u27e9\n\ntheorem mt {a b : Prop} (h\u2081 : a \u2192 b) (h\u2082 : \u00acb) : \u00aca :=\n  fun ha => h\u2082 (h\u2081 ha)\n\ntheorem not_false : \u00acFalse := id\n\ntheorem not_not_intro {p : Prop} (h : p) : \u00ac \u00ac p :=\n  fun hn : \u00ac p => hn h\n\n-- proof irrelevance is built in\ntheorem proofIrrel {a : Prop} (h\u2081 h\u2082 : a) : h\u2081 = h\u2082 := rfl\n\ntheorem id.def {\u03b1 : Sort u} (a : \u03b1) : id a = a := rfl\n\n/--\nIf `h : \u03b1 = \u03b2` is a proof of type equality, then `h.mp : \u03b1 \u2192 \u03b2` is the induced\n\"cast\" operation, mapping elements of `\u03b1` to elements of `\u03b2`.\n\nYou can prove theorems about the resulting element by induction on `h`, since\n`rfl.mp` is definitionally the identity function.\n-/\n@[macro_inline] def Eq.mp {\u03b1 \u03b2 : Sort u} (h : \u03b1 = \u03b2) (a : \u03b1) : \u03b2 :=\n  h \u25b8 a\n\n/--\nIf `h : \u03b1 = \u03b2` is a proof of type equality, then `h.mpr : \u03b2 \u2192 \u03b1` is the induced\n\"cast\" operation in the reverse direction, mapping elements of `\u03b2` to elements of `\u03b1`.\n\nYou can prove theorems about the resulting element by induction on `h`, since\n`rfl.mpr` is definitionally the identity function.\n-/\n@[macro_inline] def Eq.mpr {\u03b1 \u03b2 : Sort u} (h : \u03b1 = \u03b2) (b : \u03b2) : \u03b1 :=\n  h \u25b8 b\n\n@[elab_as_elim]\ntheorem Eq.substr {\u03b1 : Sort u} {p : \u03b1 \u2192 Prop} {a b : \u03b1} (h\u2081 : b = a) (h\u2082 : p a) : p b :=\n  h\u2081 \u25b8 h\u2082\n\ntheorem cast_eq {\u03b1 : Sort u} (h : \u03b1 = \u03b1) (a : \u03b1) : cast h a = a :=\n  rfl\n\n/--\n`a \u2260 b`, or `Ne a b` is defined as `\u00ac (a = b)` or `a = b \u2192 False`,\nand asserts that `a` and `b` are not equal.\n-/\n@[reducible] def Ne {\u03b1 : Sort u} (a b : \u03b1) :=\n  \u00ac(a = b)\n\n@[inherit_doc] infix:50 \" \u2260 \"  => Ne\n\nsection Ne\nvariable {\u03b1 : Sort u}\nvariable {a b : \u03b1} {p : Prop}\n\ntheorem Ne.intro (h : a = b \u2192 False) : a \u2260 b := h\n\ntheorem Ne.elim (h : a \u2260 b) : a = b \u2192 False := h\n\ntheorem Ne.irrefl (h : a \u2260 a) : False := h rfl\n\ntheorem Ne.symm (h : a \u2260 b) : b \u2260 a :=\n  fun h\u2081 => h (h\u2081.symm)\n\ntheorem false_of_ne : a \u2260 a \u2192 False := Ne.irrefl\n\ntheorem ne_false_of_self : p \u2192 p \u2260 False :=\n  fun (hp : p) (h : p = False) => h \u25b8 hp\n\ntheorem ne_true_of_not : \u00acp \u2192 p \u2260 True :=\n  fun (hnp : \u00acp) (h : p = True) =>\n    have : \u00acTrue := h \u25b8 hnp\n    this trivial\n\ntheorem true_ne_false : \u00acTrue = False :=\n  ne_false_of_self trivial\n\nend Ne\n\ntheorem Bool.of_not_eq_true : {b : Bool} \u2192 \u00ac (b = true) \u2192 b = false\n  | true,  h => absurd rfl h\n  | false, _ => rfl\n\ntheorem Bool.of_not_eq_false : {b : Bool} \u2192 \u00ac (b = false) \u2192 b = true\n  | true,  _ => rfl\n  | false, h => absurd rfl h\n\ntheorem ne_of_beq_false [BEq \u03b1] [LawfulBEq \u03b1] {a b : \u03b1} (h : (a == b) = false) : a \u2260 b := by\n  intro h'; subst h'; have : true = false := Eq.trans LawfulBEq.rfl.symm h; contradiction\n\ntheorem beq_false_of_ne [BEq \u03b1] [LawfulBEq \u03b1] {a b : \u03b1} (h : a \u2260 b) : (a == b) = false :=\n  have : \u00ac (a == b) = true := by\n    intro h'; rw [eq_of_beq h'] at h; contradiction\n  Bool.of_not_eq_true this\n\nsection\nvariable {\u03b1 \u03b2 \u03c6 : Sort u} {a a' : \u03b1} {b b' : \u03b2} {c : \u03c6}\n\ntheorem HEq.ndrec.{u1, u2} {\u03b1 : Sort u2} {a : \u03b1} {motive : {\u03b2 : Sort u2} \u2192 \u03b2 \u2192 Sort u1} (m : motive a) {\u03b2 : Sort u2} {b : \u03b2} (h : HEq a b) : motive b :=\n  h.rec m\n\ntheorem HEq.ndrecOn.{u1, u2} {\u03b1 : Sort u2} {a : \u03b1} {motive : {\u03b2 : Sort u2} \u2192 \u03b2 \u2192 Sort u1} {\u03b2 : Sort u2} {b : \u03b2} (h : HEq a b) (m : motive a) : motive b :=\n  h.rec m\n\ntheorem HEq.elim {\u03b1 : Sort u} {a : \u03b1} {p : \u03b1 \u2192 Sort v} {b : \u03b1} (h\u2081 : HEq a b) (h\u2082 : p a) : p b :=\n  eq_of_heq h\u2081 \u25b8 h\u2082\n\ntheorem HEq.subst {p : (T : Sort u) \u2192 T \u2192 Prop} (h\u2081 : HEq a b) (h\u2082 : p \u03b1 a) : p \u03b2 b :=\n  HEq.ndrecOn h\u2081 h\u2082\n\ntheorem HEq.symm (h : HEq a b) : HEq b a :=\n  h.rec (HEq.refl a)\n\ntheorem heq_of_eq (h : a = a') : HEq a a' :=\n  Eq.subst h (HEq.refl a)\n\ntheorem HEq.trans (h\u2081 : HEq a b) (h\u2082 : HEq b c) : HEq a c :=\n  HEq.subst h\u2082 h\u2081\n\ntheorem heq_of_heq_of_eq (h\u2081 : HEq a b) (h\u2082 : b = b') : HEq a b' :=\n  HEq.trans h\u2081 (heq_of_eq h\u2082)\n\ntheorem heq_of_eq_of_heq (h\u2081 : a = a') (h\u2082 : HEq a' b) : HEq a b :=\n  HEq.trans (heq_of_eq h\u2081) h\u2082\n\ntheorem type_eq_of_heq (h : HEq a b) : \u03b1 = \u03b2 :=\n  h.rec (Eq.refl \u03b1)\n\nend\n\ntheorem eqRec_heq {\u03b1 : Sort u} {\u03c6 : \u03b1 \u2192 Sort v} {a a' : \u03b1} : (h : a = a') \u2192 (p : \u03c6 a) \u2192 HEq (Eq.recOn (motive := fun x _ => \u03c6 x) h p) p\n  | rfl, p => HEq.refl p\n\ntheorem heq_of_eqRec_eq {\u03b1 \u03b2 : Sort u} {a : \u03b1} {b : \u03b2} (h\u2081 : \u03b1 = \u03b2) (h\u2082 : Eq.rec (motive := fun \u03b1 _ => \u03b1) a h\u2081 = b) : HEq a b := by\n  subst h\u2081\n  apply heq_of_eq\n  exact h\u2082\n\ntheorem cast_heq {\u03b1 \u03b2 : Sort u} : (h : \u03b1 = \u03b2) \u2192 (a : \u03b1) \u2192 HEq (cast h a) a\n  | rfl, a => HEq.refl a\n\nvariable {a b c d : Prop}\n\ntheorem iff_iff_implies_and_implies (a b : Prop) : (a \u2194 b) \u2194 (a \u2192 b) \u2227 (b \u2192 a) :=\n  Iff.intro (fun h => And.intro h.mp h.mpr) (fun h => Iff.intro h.left h.right)\n\ntheorem Iff.refl (a : Prop) : a \u2194 a :=\n  Iff.intro (fun h => h) (fun h => h)\n\nprotected theorem Iff.rfl {a : Prop} : a \u2194 a :=\n  Iff.refl a\n\ntheorem Iff.trans (h\u2081 : a \u2194 b) (h\u2082 : b \u2194 c) : a \u2194 c :=\n  Iff.intro\n    (fun ha => Iff.mp h\u2082 (Iff.mp h\u2081 ha))\n    (fun hc => Iff.mpr h\u2081 (Iff.mpr h\u2082 hc))\n\ntheorem Iff.symm (h : a \u2194 b) : b \u2194 a :=\n  Iff.intro (Iff.mpr h) (Iff.mp h)\n\ntheorem Iff.comm : (a \u2194 b) \u2194 (b \u2194 a) :=\n  Iff.intro Iff.symm Iff.symm\n\ntheorem Iff.of_eq (h : a = b) : a \u2194 b :=\n  h \u25b8 Iff.refl _\n\ntheorem And.comm : a \u2227 b \u2194 b \u2227 a := by\n  constructor <;> intro \u27e8h\u2081, h\u2082\u27e9 <;> exact \u27e8h\u2082, h\u2081\u27e9\n\n/-! # Exists -/\n\ntheorem Exists.elim {\u03b1 : Sort u} {p : \u03b1 \u2192 Prop} {b : Prop}\n   (h\u2081 : Exists (fun x => p x)) (h\u2082 : \u2200 (a : \u03b1), p a \u2192 b) : b :=\n  match h\u2081 with\n  | intro a h => h\u2082 a h\n\n/-! # Decidable -/\n\ntheorem decide_true_eq_true (h : Decidable True) : @decide True h = true :=\n  match h with\n  | isTrue _  => rfl\n  | isFalse h => False.elim <| h \u27e8\u27e9\n\ntheorem decide_false_eq_false (h : Decidable False) : @decide False h = false :=\n  match h with\n  | isFalse _ => rfl\n  | isTrue h  => False.elim h\n\n/-- Similar to `decide`, but uses an explicit instance -/\n@[inline] def toBoolUsing {p : Prop} (d : Decidable p) : Bool :=\n  decide (h := d)\n\ntheorem toBoolUsing_eq_true {p : Prop} (d : Decidable p) (h : p) : toBoolUsing d = true :=\n  decide_eq_true (inst := d) h\n\ntheorem ofBoolUsing_eq_true {p : Prop} {d : Decidable p} (h : toBoolUsing d = true) : p :=\n  of_decide_eq_true (inst := d) h\n\ntheorem ofBoolUsing_eq_false {p : Prop} {d : Decidable p} (h : toBoolUsing d = false) : \u00ac p :=\n  of_decide_eq_false (inst := d) h\n\ninstance : Decidable True :=\n  isTrue trivial\n\ninstance : Decidable False :=\n  isFalse not_false\n\nnamespace Decidable\nvariable {p q : Prop}\n\n/--\nSynonym for `dite` (dependent if-then-else). We can construct an element `q`\n(of any sort, not just a proposition) by cases on whether `p` is true or false,\nprovided `p` is decidable.\n-/\n@[macro_inline] def byCases {q : Sort u} [dec : Decidable p] (h1 : p \u2192 q) (h2 : \u00acp \u2192 q) : q :=\n  match dec with\n  | isTrue h  => h1 h\n  | isFalse h => h2 h\n\ntheorem em (p : Prop) [Decidable p] : p \u2228 \u00acp :=\n  byCases Or.inl Or.inr\n\nset_option linter.unusedVariables.funArgs false in\ntheorem byContradiction [dec : Decidable p] (h : \u00acp \u2192 False) : p :=\n  byCases id (fun np => False.elim (h np))\n\ntheorem of_not_not [Decidable p] : \u00ac \u00ac p \u2192 p :=\n  fun hnn => byContradiction (fun hn => absurd hn hnn)\n\ntheorem not_and_iff_or_not (p q : Prop) [d\u2081 : Decidable p] [d\u2082 : Decidable q] : \u00ac (p \u2227 q) \u2194 \u00ac p \u2228 \u00ac q :=\n  Iff.intro\n    (fun h => match d\u2081, d\u2082 with\n      | isTrue h\u2081,  isTrue h\u2082   => absurd (And.intro h\u2081 h\u2082) h\n      | _,           isFalse h\u2082 => Or.inr h\u2082\n      | isFalse h\u2081, _           => Or.inl h\u2081)\n    (fun (h) \u27e8hp, hq\u27e9 => match h with\n      | Or.inl h => h hp\n      | Or.inr h => h hq)\n\nend Decidable\n\nsection\nvariable {p q : Prop}\n/-- Transfer a decidability proof across an equivalence of propositions. -/\n@[inline] def decidable_of_decidable_of_iff [Decidable p] (h : p \u2194 q) : Decidable q :=\n  if hp : p then\n    isTrue (Iff.mp h hp)\n  else\n    isFalse fun hq => absurd (Iff.mpr h hq) hp\n\n/-- Transfer a decidability proof across an equality of propositions. -/\n@[inline] def decidable_of_decidable_of_eq [Decidable p] (h : p = q) : Decidable q :=\n  decidable_of_decidable_of_iff (p := p) (h \u25b8 Iff.rfl)\nend\n\n@[macro_inline] instance {p q} [Decidable p] [Decidable q] : Decidable (p \u2192 q) :=\n  if hp : p then\n    if hq : q then isTrue (fun _ => hq)\n    else isFalse (fun h => absurd (h hp) hq)\n  else isTrue (fun h => absurd h hp)\n\ninstance {p q} [Decidable p] [Decidable q] : Decidable (p \u2194 q) :=\n  if hp : p then\n    if hq : q then\n      isTrue \u27e8fun _ => hq, fun _ => hp\u27e9\n    else\n      isFalse fun h => hq (h.1 hp)\n  else\n    if hq : q then\n      isFalse fun h => hp (h.2 hq)\n    else\n      isTrue \u27e8fun h => absurd h hp, fun h => absurd h hq\u27e9\n\n/-! # if-then-else expression theorems -/\n\ntheorem if_pos {c : Prop} {h : Decidable c} (hc : c) {\u03b1 : Sort u} {t e : \u03b1} : (ite c t e) = t :=\n  match h with\n  | isTrue  _   => rfl\n  | isFalse hnc => absurd hc hnc\n\ntheorem if_neg {c : Prop} {h : Decidable c} (hnc : \u00acc) {\u03b1 : Sort u} {t e : \u03b1} : (ite c t e) = e :=\n  match h with\n  | isTrue hc   => absurd hc hnc\n  | isFalse _   => rfl\n\ntheorem dif_pos {c : Prop} {h : Decidable c} (hc : c) {\u03b1 : Sort u} {t : c \u2192 \u03b1} {e : \u00ac c \u2192 \u03b1} : (dite c t e) = t hc :=\n  match h with\n  | isTrue  _   => rfl\n  | isFalse hnc => absurd hc hnc\n\ntheorem dif_neg {c : Prop} {h : Decidable c} (hnc : \u00acc) {\u03b1 : Sort u} {t : c \u2192 \u03b1} {e : \u00ac c \u2192 \u03b1} : (dite c t e) = e hnc :=\n  match h with\n  | isTrue hc   => absurd hc hnc\n  | isFalse _   => rfl\n\n-- Remark: dite and ite are \"defally equal\" when we ignore the proofs.\ntheorem dif_eq_if (c : Prop) {h : Decidable c} {\u03b1 : Sort u} (t : \u03b1) (e : \u03b1) : dite c (fun _ => t) (fun _ => e) = ite c t e :=\n  match h with\n  | isTrue _    => rfl\n  | isFalse _   => rfl\n\ninstance {c t e : Prop} [dC : Decidable c] [dT : Decidable t] [dE : Decidable e] : Decidable (if c then t else e)  :=\n  match dC with\n  | isTrue _   => dT\n  | isFalse _  => dE\n\ninstance {c : Prop} {t : c \u2192 Prop} {e : \u00acc \u2192 Prop} [dC : Decidable c] [dT : \u2200 h, Decidable (t h)] [dE : \u2200 h, Decidable (e h)] : Decidable (if h : c then t h else e h)  :=\n  match dC with\n  | isTrue hc  => dT hc\n  | isFalse hc => dE hc\n\n/-- Auxiliary definition for generating compact `noConfusion` for enumeration types -/\nabbrev noConfusionTypeEnum {\u03b1 : Sort u} {\u03b2 : Sort v} [inst : DecidableEq \u03b2] (f : \u03b1 \u2192 \u03b2) (P : Sort w) (x y : \u03b1) : Sort w :=\n  (inst (f x) (f y)).casesOn\n    (fun _ => P)\n    (fun _ => P \u2192 P)\n\n/-- Auxiliary definition for generating compact `noConfusion` for enumeration types -/\nabbrev noConfusionEnum {\u03b1 : Sort u} {\u03b2 : Sort v} [inst : DecidableEq \u03b2] (f : \u03b1 \u2192 \u03b2) {P : Sort w} {x y : \u03b1} (h : x = y) : noConfusionTypeEnum f P x y :=\n  Decidable.casesOn\n    (motive := fun (inst : Decidable (f x = f y)) => Decidable.casesOn (motive := fun _ => Sort w) inst (fun _ => P) (fun _ => P \u2192 P))\n    (inst (f x) (f y))\n    (fun h' => False.elim (h' (congrArg f h)))\n    (fun _ => fun x => x)\n\n/-! # Inhabited -/\n\ninstance : Inhabited Prop where\n  default := True\n\nderiving instance Inhabited for NonScalar, PNonScalar, True, ForInStep\n\ntheorem nonempty_of_exists {\u03b1 : Sort u} {p : \u03b1 \u2192 Prop} : Exists (fun x => p x) \u2192 Nonempty \u03b1\n  | \u27e8w, _\u27e9 => \u27e8w\u27e9\n\n/-! # Subsingleton -/\n\n/--\nA \"subsingleton\" is a type with at most one element.\nIn other words, it is either empty, or has a unique element.\nAll propositions are subsingletons because of proof irrelevance, but some other types\nare subsingletons as well and they inherit many of the same properties as propositions.\n`Subsingleton \u03b1` is a typeclass, so it is usually used as an implicit argument and\ninferred by typeclass inference.\n-/\nclass Subsingleton (\u03b1 : Sort u) : Prop where\n  /-- Construct a proof that `\u03b1` is a subsingleton by showing that any two elements are equal. -/\n  intro ::\n  /-- Any two elements of a subsingleton are equal. -/\n  allEq : (a b : \u03b1) \u2192 a = b\n\nprotected theorem Subsingleton.elim {\u03b1 : Sort u} [h : Subsingleton \u03b1] : (a b : \u03b1) \u2192 a = b :=\n  h.allEq\n\nprotected theorem Subsingleton.helim {\u03b1 \u03b2 : Sort u} [h\u2081 : Subsingleton \u03b1] (h\u2082 : \u03b1 = \u03b2) (a : \u03b1) (b : \u03b2) : HEq a b := by\n  subst h\u2082\n  apply heq_of_eq\n  apply Subsingleton.elim\n\ninstance (p : Prop) : Subsingleton p :=\n  \u27e8fun a b => proofIrrel a b\u27e9\n\ninstance (p : Prop) : Subsingleton (Decidable p) :=\n  Subsingleton.intro fun\n    | isTrue t\u2081 => fun\n      | isTrue _   => rfl\n      | isFalse f\u2082 => absurd t\u2081 f\u2082\n    | isFalse f\u2081 => fun\n      | isTrue t\u2082  => absurd t\u2082 f\u2081\n      | isFalse _  => rfl\n\ntheorem recSubsingleton\n     {p : Prop} [h : Decidable p]\n     {h\u2081 : p \u2192 Sort u}\n     {h\u2082 : \u00acp \u2192 Sort u}\n     [h\u2083 : \u2200 (h : p), Subsingleton (h\u2081 h)]\n     [h\u2084 : \u2200 (h : \u00acp), Subsingleton (h\u2082 h)]\n     : Subsingleton (h.casesOn h\u2082 h\u2081) :=\n  match h with\n  | isTrue h  => h\u2083 h\n  | isFalse h => h\u2084 h\n\n/--\nAn equivalence relation `~ : \u03b1 \u2192 \u03b1 \u2192 Prop` is a relation that is:\n\n* reflexive: `x ~ x`\n* symmetric: `x ~ y` implies `y ~ x`\n* transitive: `x ~ y` and `y ~ z` implies `x ~ z`\n\nEquality is an equivalence relation, and equivalence relations share many of\nthe properties of equality. In particular, `Quot \u03b1 r` is most well behaved\nwhen `r` is an equivalence relation, and in this case we use `Quotient` instead.\n-/\nstructure Equivalence {\u03b1 : Sort u} (r : \u03b1 \u2192 \u03b1 \u2192 Prop) : Prop where\n  /-- An equivalence relation is reflexive: `x ~ x` -/\n  refl  : \u2200 x, r x x\n  /-- An equivalence relation is symmetric: `x ~ y` implies `y ~ x` -/\n  symm  : \u2200 {x y}, r x y \u2192 r y x\n  /-- An equivalence relation is transitive: `x ~ y` and `y ~ z` implies `x ~ z` -/\n  trans : \u2200 {x y z}, r x y \u2192 r y z \u2192 r x z\n\n/-- The empty relation is the relation on `\u03b1` which is always `False`. -/\ndef emptyRelation {\u03b1 : Sort u} (_ _ : \u03b1) : Prop :=\n  False\n\n/--\n`Subrelation q r` means that `q \u2286 r` or `\u2200 x y, q x y \u2192 r x y`.\nIt is the analogue of the subset relation on relations.\n-/\ndef Subrelation {\u03b1 : Sort u} (q r : \u03b1 \u2192 \u03b1 \u2192 Prop) :=\n  \u2200 {x y}, q x y \u2192 r x y\n\n/--\nThe inverse image of `r : \u03b2 \u2192 \u03b2 \u2192 Prop` by a function `\u03b1 \u2192 \u03b2` is the relation\n`s : \u03b1 \u2192 \u03b1 \u2192 Prop` defined by `s a b = r (f a) (f b)`.\n-/\ndef InvImage {\u03b1 : Sort u} {\u03b2 : Sort v} (r : \u03b2 \u2192 \u03b2 \u2192 Prop) (f : \u03b1 \u2192 \u03b2) : \u03b1 \u2192 \u03b1 \u2192 Prop :=\n  fun a\u2081 a\u2082 => r (f a\u2081) (f a\u2082)\n\n/--\nThe transitive closure `r\u207a` of a relation `r` is the smallest relation which is\ntransitive and contains `r`. `r\u207a a z` if and only if there exists a sequence\n`a r b r ... r z` of length at least 1 connecting `a` to `z`.\n-/\ninductive TC {\u03b1 : Sort u} (r : \u03b1 \u2192 \u03b1 \u2192 Prop) : \u03b1 \u2192 \u03b1 \u2192 Prop where\n  /-- If `r a b` then `r\u207a a b`. This is the base case of the transitive closure. -/\n  | base  : \u2200 a b, r a b \u2192 TC r a b\n  /-- The transitive closure is transitive. -/\n  | trans : \u2200 a b c, TC r a b \u2192 TC r b c \u2192 TC r a c\n\n/-! # Subtype -/\n\nnamespace Subtype\ntheorem existsOfSubtype {\u03b1 : Type u} {p : \u03b1 \u2192 Prop} : { x // p x } \u2192 Exists (fun x => p x)\n  | \u27e8a, h\u27e9 => \u27e8a, h\u27e9\n\nvariable {\u03b1 : Type u} {p : \u03b1 \u2192 Prop}\n\nprotected theorem eq : \u2200 {a1 a2 : {x // p x}}, val a1 = val a2 \u2192 a1 = a2\n  | \u27e8_, _\u27e9, \u27e8_, _\u27e9, rfl => rfl\n\ntheorem eta (a : {x // p x}) (h : p (val a)) : mk (val a) h = a := by\n  cases a\n  exact rfl\n\ninstance {\u03b1 : Type u} {p : \u03b1 \u2192 Prop} {a : \u03b1} (h : p a) : Inhabited {x // p x} where\n  default := \u27e8a, h\u27e9\n\ninstance {\u03b1 : Type u} {p : \u03b1 \u2192 Prop} [DecidableEq \u03b1] : DecidableEq {x : \u03b1 // p x} :=\n  fun \u27e8a, h\u2081\u27e9 \u27e8b, h\u2082\u27e9 =>\n    if h : a = b then isTrue (by subst h; exact rfl)\n    else isFalse (fun h' => Subtype.noConfusion h' (fun h' => absurd h' h))\n\nend Subtype\n\n/-! # Sum -/\n\nsection\nvariable {\u03b1 : Type u} {\u03b2 : Type v}\n\ninstance Sum.inhabitedLeft [Inhabited \u03b1] : Inhabited (Sum \u03b1 \u03b2) where\n  default := Sum.inl default\n\ninstance Sum.inhabitedRight [Inhabited \u03b2] : Inhabited (Sum \u03b1 \u03b2) where\n  default := Sum.inr default\n\ninstance {\u03b1 : Type u} {\u03b2 : Type v} [DecidableEq \u03b1] [DecidableEq \u03b2] : DecidableEq (Sum \u03b1 \u03b2) := fun a b =>\n  match a, b with\n  | Sum.inl a, Sum.inl b =>\n    if h : a = b then isTrue (h \u25b8 rfl)\n    else isFalse fun h' => Sum.noConfusion h' fun h' => absurd h' h\n  | Sum.inr a, Sum.inr b =>\n    if h : a = b then isTrue (h \u25b8 rfl)\n    else isFalse fun h' => Sum.noConfusion h' fun h' => absurd h' h\n  | Sum.inr _, Sum.inl _ => isFalse fun h => Sum.noConfusion h\n  | Sum.inl _, Sum.inr _ => isFalse fun h => Sum.noConfusion h\n\nend\n\n/-! # Product -/\n\ninstance [Inhabited \u03b1] [Inhabited \u03b2] : Inhabited (\u03b1 \u00d7 \u03b2) where\n  default := (default, default)\n\ninstance [Inhabited \u03b1] [Inhabited \u03b2] : Inhabited (MProd \u03b1 \u03b2) where\n  default := \u27e8default, default\u27e9\n\ninstance [Inhabited \u03b1] [Inhabited \u03b2] : Inhabited (PProd \u03b1 \u03b2) where\n  default := \u27e8default, default\u27e9\n\ninstance [DecidableEq \u03b1] [DecidableEq \u03b2] : DecidableEq (\u03b1 \u00d7 \u03b2) :=\n  fun (a, b) (a', b') =>\n    match decEq a a' with\n    | isTrue e\u2081 =>\n      match decEq b b' with\n      | isTrue e\u2082  => isTrue (e\u2081 \u25b8 e\u2082 \u25b8 rfl)\n      | isFalse n\u2082 => isFalse fun h => Prod.noConfusion h fun _   e\u2082' => absurd e\u2082' n\u2082\n    | isFalse n\u2081 => isFalse fun h => Prod.noConfusion h fun e\u2081' _   => absurd e\u2081' n\u2081\n\ninstance [BEq \u03b1] [BEq \u03b2] : BEq (\u03b1 \u00d7 \u03b2) where\n  beq := fun (a\u2081, b\u2081) (a\u2082, b\u2082) => a\u2081 == a\u2082 && b\u2081 == b\u2082\n\n/-- Lexicographical order for products -/\ndef Prod.lexLt [LT \u03b1] [LT \u03b2] (s : \u03b1 \u00d7 \u03b2) (t : \u03b1 \u00d7 \u03b2) : Prop :=\n  s.1 < t.1 \u2228 (s.1 = t.1 \u2227 s.2 < t.2)\n\ninstance Prod.lexLtDec\n    [LT \u03b1] [LT \u03b2] [DecidableEq \u03b1] [DecidableEq \u03b2]\n    [(a b : \u03b1) \u2192 Decidable (a < b)] [(a b : \u03b2) \u2192 Decidable (a < b)]\n    : (s t : \u03b1 \u00d7 \u03b2) \u2192 Decidable (Prod.lexLt s t) :=\n  fun _ _ => inferInstanceAs (Decidable (_ \u2228 _))\n\ntheorem Prod.lexLt_def [LT \u03b1] [LT \u03b2] (s t : \u03b1 \u00d7 \u03b2) : (Prod.lexLt s t) = (s.1 < t.1 \u2228 (s.1 = t.1 \u2227 s.2 < t.2)) :=\n  rfl\n\ntheorem Prod.eta (p : \u03b1 \u00d7 \u03b2) : (p.1, p.2) = p := rfl\n\n/--\n`Prod.map f g : \u03b1\u2081 \u00d7 \u03b2\u2081 \u2192 \u03b1\u2082 \u00d7 \u03b2\u2082` maps across a pair\nby applying `f` to the first component and `g` to the second.\n-/\ndef Prod.map {\u03b1\u2081 : Type u\u2081} {\u03b1\u2082 : Type u\u2082} {\u03b2\u2081 : Type v\u2081} {\u03b2\u2082 : Type v\u2082}\n    (f : \u03b1\u2081 \u2192 \u03b1\u2082) (g : \u03b2\u2081 \u2192 \u03b2\u2082) : \u03b1\u2081 \u00d7 \u03b2\u2081 \u2192 \u03b1\u2082 \u00d7 \u03b2\u2082\n  | (a, b) => (f a, g b)\n\n/-! # Dependent products -/\n\ntheorem ex_of_PSigma {\u03b1 : Type u} {p : \u03b1 \u2192 Prop} : (PSigma (fun x => p x)) \u2192 Exists (fun x => p x)\n  | \u27e8x, hx\u27e9 => \u27e8x, hx\u27e9\n\nprotected theorem PSigma.eta {\u03b1 : Sort u} {\u03b2 : \u03b1 \u2192 Sort v} {a\u2081 a\u2082 : \u03b1} {b\u2081 : \u03b2 a\u2081} {b\u2082 : \u03b2 a\u2082}\n    (h\u2081 : a\u2081 = a\u2082) (h\u2082 : Eq.ndrec b\u2081 h\u2081 = b\u2082) : PSigma.mk a\u2081 b\u2081 = PSigma.mk a\u2082 b\u2082 := by\n  subst h\u2081\n  subst h\u2082\n  exact rfl\n\n/-! # Universe polymorphic unit -/\n\ntheorem PUnit.subsingleton (a b : PUnit) : a = b := by\n  cases a; cases b; exact rfl\n\ntheorem PUnit.eq_punit (a : PUnit) : a = \u27e8\u27e9 :=\n  PUnit.subsingleton a \u27e8\u27e9\n\ninstance : Subsingleton PUnit :=\n  Subsingleton.intro PUnit.subsingleton\n\ninstance : Inhabited PUnit where\n  default := \u27e8\u27e9\n\ninstance : DecidableEq PUnit :=\n  fun a b => isTrue (PUnit.subsingleton a b)\n\n/-! # Setoid -/\n\n/--\nA setoid is a type with a distinguished equivalence relation, denoted `\u2248`.\nThis is mainly used as input to the `Quotient` type constructor.\n-/\nclass Setoid (\u03b1 : Sort u) where\n  /-- `x \u2248 y` is the distinguished equivalence relation of a setoid. -/\n  r : \u03b1 \u2192 \u03b1 \u2192 Prop\n  /-- The relation `x \u2248 y` is an equivalence relation. -/\n  iseqv : Equivalence r\n\ninstance {\u03b1 : Sort u} [Setoid \u03b1] : HasEquiv \u03b1 :=\n  \u27e8Setoid.r\u27e9\n\nnamespace Setoid\n\nvariable {\u03b1 : Sort u} [Setoid \u03b1]\n\ntheorem refl (a : \u03b1) : a \u2248 a :=\n  iseqv.refl a\n\ntheorem symm {a b : \u03b1} (hab : a \u2248 b) : b \u2248 a :=\n  iseqv.symm hab\n\ntheorem trans {a b c : \u03b1} (hab : a \u2248 b) (hbc : b \u2248 c) : a \u2248 c :=\n  iseqv.trans hab hbc\n\nend Setoid\n\n\n/-! # Propositional extensionality -/\n\n/--\nThe axiom of **propositional extensionality**. It asserts that if propositions\n`a` and `b` are logically equivalent (i.e. we can prove `a` from `b` and vice versa),\nthen `a` and `b` are *equal*, meaning that we can replace `a` with `b` in all\ncontexts.\n\nFor simple expressions like `a \u2227 c \u2228 d \u2192 e` we can prove that because all the logical\nconnectives respect logical equivalence, we can replace `a` with `b` in this expression\nwithout using `propext`. However, for higher order expressions like `P a` where\n`P : Prop \u2192 Prop` is unknown, or indeed for `a = b` itself, we cannot replace `a` with `b`\nwithout an axiom which says exactly this.\n\nThis is a relatively uncontroversial axiom, which is intuitionistically valid.\nIt does however block computation when using `#reduce` to reduce proofs directly\n(which is not recommended), meaning that canonicity,\nthe property that all closed terms of type `Nat` normalize to numerals,\nfails to hold when this (or any) axiom is used:\n```\nset_option pp.proofs true\n\ndef foo : Nat := by\n  have : (True \u2192 True) \u2194 True := \u27e8\u03bb _ => trivial, \u03bb _ _ => trivial\u27e9\n  have := propext this \u25b8 (2 : Nat)\n  exact this\n\n#reduce foo\n-- propext { mp := fun x x => True.intro, mpr := fun x => True.intro } \u25b8 2\n\n#eval foo -- 2\n```\n`#eval` can evaluate it to a numeral because the compiler erases casts and\ndoes not evaluate proofs, so `propext`, whose return type is a proposition,\ncan never block it.\n-/\naxiom propext {a b : Prop} : (a \u2194 b) \u2192 a = b\n\ntheorem Eq.propIntro {a b : Prop} (h\u2081 : a \u2192 b) (h\u2082 : b \u2192 a) : a = b :=\n  propext <| Iff.intro h\u2081 h\u2082\n\n-- Eq for Prop is now decidable if the equivalent Iff is decidable\ninstance {p q : Prop} [d : Decidable (p \u2194 q)] : Decidable (p = q) :=\n  match d with\n  | isTrue h => isTrue (propext h)\n  | isFalse h => isFalse fun heq => h (heq \u25b8 Iff.rfl)\n\ngen_injective_theorems% Prod\ngen_injective_theorems% PProd\ngen_injective_theorems% MProd\ngen_injective_theorems% Subtype\ngen_injective_theorems% Fin\ngen_injective_theorems% Array\ngen_injective_theorems% Sum\ngen_injective_theorems% PSum\ngen_injective_theorems% Nat\ngen_injective_theorems% Option\ngen_injective_theorems% List\ngen_injective_theorems% Except\ngen_injective_theorems% EStateM.Result\ngen_injective_theorems% Lean.Name\ngen_injective_theorems% Lean.Syntax\n\n@[simp] theorem beq_iff_eq [BEq \u03b1] [LawfulBEq \u03b1] (a b : \u03b1) : a == b \u2194 a = b :=\n  \u27e8eq_of_beq, by intro h; subst h; exact LawfulBEq.rfl\u27e9\n\n/-! # Quotients -/\n\n/-- Iff can now be used to do substitutions in a calculation -/\ntheorem Iff.subst {a b : Prop} {p : Prop \u2192 Prop} (h\u2081 : a \u2194 b) (h\u2082 : p a) : p b :=\n  Eq.subst (propext h\u2081) h\u2082\n\nnamespace Quot\n/--\nThe **quotient axiom**, or at least the nontrivial part of the quotient\naxiomatization. Quotient types are introduced by the `init_quot` command\nin `Init.Prelude` which introduces the axioms:\n\n```\nopaque Quot {\u03b1 : Sort u} (r : \u03b1 \u2192 \u03b1 \u2192 Prop) : Sort u\n\nopaque Quot.mk {\u03b1 : Sort u} (r : \u03b1 \u2192 \u03b1 \u2192 Prop) (a : \u03b1) : Quot r\n\nopaque Quot.lift {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} {\u03b2 : Sort v} (f : \u03b1 \u2192 \u03b2) :\n  (\u2200 a b : \u03b1, r a b \u2192 f a = f b) \u2192 Quot r \u2192 \u03b2\n\nopaque Quot.ind {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} {\u03b2 : Quot r \u2192 Prop} :\n  (\u2200 a : \u03b1, \u03b2 (Quot.mk r a)) \u2192 \u2200 q : Quot r, \u03b2 q\n```\nAll of these axioms are true if we assume `Quot \u03b1 r = \u03b1` and `Quot.mk` and\n`Quot.lift` are identity functions, so they do not add much. However this axiom\ncannot be explained in that way (it is false for that interpretation), so the\nreal power of quotient types come from this axiom.\n\nIt says that the quotient by `r` maps elements which are related by `r` to equal\nvalues in the quotient. Together with `Quot.lift` which says that functions\nwhich respect `r` can be lifted to functions on the quotient, we can deduce that\n`Quot \u03b1 r` exactly consists of the equivalence classes with respect to `r`.\n\nIt is important to note that `r` need not be an equivalence relation in this axiom.\nWhen `r` is not an equivalence relation, we are actually taking a quotient with\nrespect to the equivalence relation generated by `r`.\n-/\naxiom sound : \u2200 {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} {a b : \u03b1}, r a b \u2192 Quot.mk r a = Quot.mk r b\n\nprotected theorem liftBeta {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} {\u03b2 : Sort v}\n    (f : \u03b1 \u2192 \u03b2)\n    (c : (a b : \u03b1) \u2192 r a b \u2192 f a = f b)\n    (a : \u03b1)\n    : lift f c (Quot.mk r a) = f a :=\n  rfl\n\nprotected theorem indBeta {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} {motive : Quot r \u2192 Prop}\n    (p : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    (a : \u03b1)\n    : (ind p (Quot.mk r a) : motive (Quot.mk r a)) = p a :=\n  rfl\n\n/--\n`Quot.liftOn q f h` is the same as `Quot.lift f h q`. It just reorders\nthe argument `q : Quot r` to be first.\n-/\nprotected abbrev liftOn {\u03b1 : Sort u} {\u03b2 : Sort v} {r : \u03b1 \u2192 \u03b1 \u2192 Prop}\n  (q : Quot r) (f : \u03b1 \u2192 \u03b2) (c : (a b : \u03b1) \u2192 r a b \u2192 f a = f b) : \u03b2 :=\n  lift f c q\n\n@[elab_as_elim]\nprotected theorem inductionOn {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} {motive : Quot r \u2192 Prop}\n    (q : Quot r)\n    (h : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    : motive q :=\n  ind h q\n\ntheorem exists_rep {\u03b1 : Sort u} {r : \u03b1 \u2192 \u03b1 \u2192 Prop} (q : Quot r) : Exists (fun a => (Quot.mk r a) = q) :=\n  q.inductionOn (fun a => \u27e8a, rfl\u27e9)\n\nsection\nvariable {\u03b1 : Sort u}\nvariable {r : \u03b1 \u2192 \u03b1 \u2192 Prop}\nvariable {motive : Quot r \u2192 Sort v}\n\n/-- Auxiliary definition for `Quot.rec`. -/\n@[reducible, macro_inline]\nprotected def indep (f : (a : \u03b1) \u2192 motive (Quot.mk r a)) (a : \u03b1) : PSigma motive :=\n  \u27e8Quot.mk r a, f a\u27e9\n\nprotected theorem indepCoherent\n    (f : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    (h : (a b : \u03b1) \u2192 (p : r a b) \u2192 Eq.ndrec (f a) (sound p) = f b)\n    : (a b : \u03b1) \u2192 r a b \u2192 Quot.indep f a = Quot.indep f b  :=\n  fun a b e => PSigma.eta (sound e) (h a b e)\n\nprotected theorem liftIndepPr1\n    (f : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    (h : \u2200 (a b : \u03b1) (p : r a b), Eq.ndrec (f a) (sound p) = f b)\n    (q : Quot r)\n    : (lift (Quot.indep f) (Quot.indepCoherent f h) q).1 = q := by\n induction q using Quot.ind\n exact rfl\n\n/--\nDependent recursion principle for `Quot`. This constructor can be tricky to use,\nso you should consider the simpler versions if they apply:\n* `Quot.lift`, for nondependent functions\n* `Quot.ind`, for theorems / proofs of propositions about quotients\n* `Quot.recOnSubsingleton`, when the target type is a `Subsingleton`\n* `Quot.hrecOn`, which uses `HEq (f a) (f b)` instead of a `sound p \u25b8 f a = f b` assummption\n-/\nprotected abbrev rec\n    (f : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    (h : (a b : \u03b1) \u2192 (p : r a b) \u2192 Eq.ndrec (f a) (sound p) = f b)\n    (q : Quot r) : motive q :=\n  Eq.ndrecOn (Quot.liftIndepPr1 f h q) ((lift (Quot.indep f) (Quot.indepCoherent f h) q).2)\n\n@[inherit_doc Quot.rec] protected abbrev recOn\n    (q : Quot r)\n    (f : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    (h : (a b : \u03b1) \u2192 (p : r a b) \u2192 Eq.ndrec (f a) (sound p) = f b)\n    : motive q :=\n q.rec f h\n\n/--\nDependent induction principle for a quotient, when the target type is a `Subsingleton`.\nIn this case the quotient's side condition is trivial so any function can be lifted.\n-/\nprotected abbrev recOnSubsingleton\n    [h : (a : \u03b1) \u2192 Subsingleton (motive (Quot.mk r a))]\n    (q : Quot r)\n    (f : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    : motive q := by\n  induction q using Quot.rec\n  apply f\n  apply Subsingleton.elim\n\n/--\nHeterogeneous dependent recursion principle for a quotient.\nThis may be easier to work with since it uses `HEq` instead of\nan `Eq.ndrec` in the hypothesis.\n-/\nprotected abbrev hrecOn\n    (q : Quot r)\n    (f : (a : \u03b1) \u2192 motive (Quot.mk r a))\n    (c : (a b : \u03b1) \u2192 (p : r a b) \u2192 HEq (f a) (f b))\n    : motive q :=\n  Quot.recOn q f fun a b p => eq_of_heq <|\n    have p\u2081 : HEq (Eq.ndrec (f a) (sound p)) (f a) := eqRec_heq (sound p) (f a)\n    HEq.trans p\u2081 (c a b p)\n\nend\nend Quot\n\nset_option linter.unusedVariables.funArgs false in\n/--\n`Quotient \u03b1 s` is the same as `Quot \u03b1 r`, but it is specialized to a setoid `s`\n(that is, an equivalence relation) instead of an arbitrary relation.\nPrefer `Quotient` over `Quot` if your relation is actually an equivalence relation.\n-/\ndef Quotient {\u03b1 : Sort u} (s : Setoid \u03b1) :=\n  @Quot \u03b1 Setoid.r\n\nnamespace Quotient\n\n/-- The canonical quotient map into a `Quotient`. -/\n@[inline]\nprotected def mk {\u03b1 : Sort u} (s : Setoid \u03b1) (a : \u03b1) : Quotient s :=\n  Quot.mk Setoid.r a\n\n/--\nThe canonical quotient map into a `Quotient`.\n(This synthesizes the setoid by typeclass inference.)\n-/\nprotected def mk' {\u03b1 : Sort u} [s : Setoid \u03b1] (a : \u03b1) : Quotient s :=\n  Quotient.mk s a\n\n/--\nThe analogue of `Quot.sound`: If `a` and `b` are related by the equivalence relation,\nthen they have equal equivalence classes.\n-/\ndef sound {\u03b1 : Sort u} {s : Setoid \u03b1} {a b : \u03b1} : a \u2248 b \u2192 Quotient.mk s a = Quotient.mk s b :=\n  Quot.sound\n\n/--\nThe analogue of `Quot.lift`: if `f : \u03b1 \u2192 \u03b2` respects the equivalence relation `\u2248`,\nthen it lifts to a function on `Quotient s` such that `lift f h (mk a) = f a`.\n-/\nprotected abbrev lift {\u03b1 : Sort u} {\u03b2 : Sort v} {s : Setoid \u03b1} (f : \u03b1 \u2192 \u03b2) : ((a b : \u03b1) \u2192 a \u2248 b \u2192 f a = f b) \u2192 Quotient s \u2192 \u03b2 :=\n  Quot.lift f\n\nprotected theorem ind {\u03b1 : Sort u} {s : Setoid \u03b1} {motive : Quotient s \u2192 Prop} : ((a : \u03b1) \u2192 motive (Quotient.mk s a)) \u2192 (q : Quot Setoid.r) \u2192 motive q :=\n  Quot.ind\n\n/--\nThe analogue of `Quot.liftOn`: if `f : \u03b1 \u2192 \u03b2` respects the equivalence relation `\u2248`,\nthen it lifts to a function on `Quotient s` such that `lift (mk a) f h = f a`.\n-/\nprotected abbrev liftOn {\u03b1 : Sort u} {\u03b2 : Sort v} {s : Setoid \u03b1} (q : Quotient s) (f : \u03b1 \u2192 \u03b2) (c : (a b : \u03b1) \u2192 a \u2248 b \u2192 f a = f b) : \u03b2 :=\n  Quot.liftOn q f c\n\n@[elab_as_elim]\nprotected theorem inductionOn {\u03b1 : Sort u} {s : Setoid \u03b1} {motive : Quotient s \u2192 Prop}\n    (q : Quotient s)\n    (h : (a : \u03b1) \u2192 motive (Quotient.mk s a))\n    : motive q :=\n  Quot.inductionOn q h\n\ntheorem exists_rep {\u03b1 : Sort u} {s : Setoid \u03b1} (q : Quotient s) : Exists (fun (a : \u03b1) => Quotient.mk s a = q) :=\n  Quot.exists_rep q\n\nsection\nvariable {\u03b1 : Sort u}\nvariable {s : Setoid \u03b1}\nvariable {motive : Quotient s \u2192 Sort v}\n\n/-- The analogue of `Quot.rec` for `Quotient`. See `Quot.rec`. -/\n@[inline, elab_as_elim]\nprotected def rec\n    (f : (a : \u03b1) \u2192 motive (Quotient.mk s a))\n    (h : (a b : \u03b1) \u2192 (p : a \u2248 b) \u2192 Eq.ndrec (f a) (Quotient.sound p) = f b)\n    (q : Quotient s)\n    : motive q :=\n  Quot.rec f h q\n\n/-- The analogue of `Quot.recOn` for `Quotient`. See `Quot.recOn`. -/\n@[elab_as_elim]\nprotected abbrev recOn\n    (q : Quotient s)\n    (f : (a : \u03b1) \u2192 motive (Quotient.mk s a))\n    (h : (a b : \u03b1) \u2192 (p : a \u2248 b) \u2192 Eq.ndrec (f a) (Quotient.sound p) = f b)\n    : motive q :=\n  Quot.recOn q f h\n\n/-- The analogue of `Quot.recOnSubsingleton` for `Quotient`. See `Quot.recOnSubsingleton`. -/\n@[elab_as_elim]\nprotected abbrev recOnSubsingleton\n    [h : (a : \u03b1) \u2192 Subsingleton (motive (Quotient.mk s a))]\n    (q : Quotient s)\n    (f : (a : \u03b1) \u2192 motive (Quotient.mk s a))\n    : motive q :=\n  Quot.recOnSubsingleton (h := h) q f\n\n/-- The analogue of `Quot.hrecOn` for `Quotient`. See `Quot.hrecOn`. -/\n@[elab_as_elim]\nprotected abbrev hrecOn\n    (q : Quotient s)\n    (f : (a : \u03b1) \u2192 motive (Quotient.mk s a))\n    (c : (a b : \u03b1) \u2192 (p : a \u2248 b) \u2192 HEq (f a) (f b))\n    : motive q :=\n  Quot.hrecOn q f c\nend\n\nsection\nuniverse uA uB uC\nvariable {\u03b1 : Sort uA} {\u03b2 : Sort uB} {\u03c6 : Sort uC}\nvariable {s\u2081 : Setoid \u03b1} {s\u2082 : Setoid \u03b2}\n\n/-- Lift a binary function to a quotient on both arguments. -/\nprotected abbrev lift\u2082\n    (f : \u03b1 \u2192 \u03b2 \u2192 \u03c6)\n    (c : (a\u2081 : \u03b1) \u2192 (b\u2081 : \u03b2) \u2192 (a\u2082 : \u03b1) \u2192 (b\u2082 : \u03b2) \u2192 a\u2081 \u2248 a\u2082 \u2192 b\u2081 \u2248 b\u2082 \u2192 f a\u2081 b\u2081 = f a\u2082 b\u2082)\n    (q\u2081 : Quotient s\u2081) (q\u2082 : Quotient s\u2082)\n    : \u03c6 := by\n  apply Quotient.lift (fun (a\u2081 : \u03b1) => Quotient.lift (f a\u2081) (fun (a b : \u03b2) => c a\u2081 a a\u2081 b (Setoid.refl a\u2081)) q\u2082) _ q\u2081\n  intros\n  induction q\u2082 using Quotient.ind\n  apply c; assumption; apply Setoid.refl\n\n/-- Lift a binary function to a quotient on both arguments. -/\nprotected abbrev liftOn\u2082\n    (q\u2081 : Quotient s\u2081)\n    (q\u2082 : Quotient s\u2082)\n    (f : \u03b1 \u2192 \u03b2 \u2192 \u03c6)\n    (c : (a\u2081 : \u03b1) \u2192 (b\u2081 : \u03b2) \u2192 (a\u2082 : \u03b1) \u2192 (b\u2082 : \u03b2) \u2192 a\u2081 \u2248 a\u2082 \u2192 b\u2081 \u2248 b\u2082 \u2192 f a\u2081 b\u2081 = f a\u2082 b\u2082)\n    : \u03c6 :=\n  Quotient.lift\u2082 f c q\u2081 q\u2082\n\n@[elab_as_elim]\nprotected theorem ind\u2082\n    {motive : Quotient s\u2081 \u2192 Quotient s\u2082 \u2192 Prop}\n    (h : (a : \u03b1) \u2192 (b : \u03b2) \u2192 motive (Quotient.mk s\u2081 a) (Quotient.mk s\u2082 b))\n    (q\u2081 : Quotient s\u2081)\n    (q\u2082 : Quotient s\u2082)\n    : motive q\u2081 q\u2082 := by\n  induction q\u2081 using Quotient.ind\n  induction q\u2082 using Quotient.ind\n  apply h\n\n@[elab_as_elim]\nprotected theorem inductionOn\u2082\n    {motive : Quotient s\u2081 \u2192 Quotient s\u2082 \u2192 Prop}\n    (q\u2081 : Quotient s\u2081)\n    (q\u2082 : Quotient s\u2082)\n    (h : (a : \u03b1) \u2192 (b : \u03b2) \u2192 motive (Quotient.mk s\u2081 a) (Quotient.mk s\u2082 b))\n    : motive q\u2081 q\u2082 := by\n  induction q\u2081 using Quotient.ind\n  induction q\u2082 using Quotient.ind\n  apply h\n\n@[elab_as_elim]\nprotected theorem inductionOn\u2083\n    {s\u2083 : Setoid \u03c6}\n    {motive : Quotient s\u2081 \u2192 Quotient s\u2082 \u2192 Quotient s\u2083 \u2192 Prop}\n    (q\u2081 : Quotient s\u2081)\n    (q\u2082 : Quotient s\u2082)\n    (q\u2083 : Quotient s\u2083)\n    (h : (a : \u03b1) \u2192 (b : \u03b2) \u2192 (c : \u03c6) \u2192 motive (Quotient.mk s\u2081 a) (Quotient.mk s\u2082 b) (Quotient.mk s\u2083 c))\n    : motive q\u2081 q\u2082 q\u2083 := by\n  induction q\u2081 using Quotient.ind\n  induction q\u2082 using Quotient.ind\n  induction q\u2083 using Quotient.ind\n  apply h\n\nend\n\nsection Exact\n\nvariable   {\u03b1 : Sort u}\n\nprivate def rel {s : Setoid \u03b1} (q\u2081 q\u2082 : Quotient s) : Prop :=\n  Quotient.liftOn\u2082 q\u2081 q\u2082\n    (fun a\u2081 a\u2082 => a\u2081 \u2248 a\u2082)\n    (fun _ _ _ _ a\u2081b\u2081 a\u2082b\u2082 =>\n      propext (Iff.intro\n        (fun a\u2081a\u2082 => Setoid.trans (Setoid.symm a\u2081b\u2081) (Setoid.trans a\u2081a\u2082 a\u2082b\u2082))\n        (fun b\u2081b\u2082 => Setoid.trans a\u2081b\u2081 (Setoid.trans b\u2081b\u2082 (Setoid.symm a\u2082b\u2082)))))\n\nprivate theorem rel.refl {s : Setoid \u03b1} (q : Quotient s) : rel q q :=\n  q.inductionOn Setoid.refl\n\nprivate theorem rel_of_eq {s : Setoid \u03b1} {q\u2081 q\u2082 : Quotient s} : q\u2081 = q\u2082 \u2192 rel q\u2081 q\u2082 :=\n  fun h => Eq.ndrecOn h (rel.refl q\u2081)\n\ntheorem exact {s : Setoid \u03b1} {a b : \u03b1} : Quotient.mk s a = Quotient.mk s b \u2192 a \u2248 b :=\n  fun h => rel_of_eq h\n\nend Exact\n\nsection\nuniverse uA uB uC\nvariable {\u03b1 : Sort uA} {\u03b2 : Sort uB}\nvariable {s\u2081 : Setoid \u03b1} {s\u2082 : Setoid \u03b2}\n\n/-- Lift a binary function to a quotient on both arguments. -/\n@[elab_as_elim]\nprotected abbrev recOnSubsingleton\u2082\n    {motive : Quotient s\u2081 \u2192 Quotient s\u2082 \u2192 Sort uC}\n    [s : (a : \u03b1) \u2192 (b : \u03b2) \u2192 Subsingleton (motive (Quotient.mk s\u2081 a) (Quotient.mk s\u2082 b))]\n    (q\u2081 : Quotient s\u2081)\n    (q\u2082 : Quotient s\u2082)\n    (g : (a : \u03b1) \u2192 (b : \u03b2) \u2192 motive (Quotient.mk s\u2081 a) (Quotient.mk s\u2082 b))\n    : motive q\u2081 q\u2082 := by\n  induction q\u2081 using Quot.recOnSubsingleton\n  induction q\u2082 using Quot.recOnSubsingleton\n  apply g\n  intro a; apply s\n  induction q\u2082 using Quot.recOnSubsingleton\n  intro a; apply s\n  infer_instance\n\nend\nend Quotient\n\nsection\nvariable {\u03b1 : Type u}\nvariable (r : \u03b1 \u2192 \u03b1 \u2192 Prop)\n\ninstance {\u03b1 : Sort u} {s : Setoid \u03b1} [d : \u2200 (a b : \u03b1), Decidable (a \u2248 b)] : DecidableEq (Quotient s) :=\n  fun (q\u2081 q\u2082 : Quotient s) =>\n    Quotient.recOnSubsingleton\u2082 q\u2081 q\u2082\n      fun a\u2081 a\u2082 =>\n        match d a\u2081 a\u2082 with\n        | isTrue h\u2081  => isTrue (Quotient.sound h\u2081)\n        | isFalse h\u2082 => isFalse fun h => absurd (Quotient.exact h) h\u2082\n\n/-! # Function extensionality -/\n\n/--\n**Function extensionality** is the statement that if two functions take equal values\nevery point, then the functions themselves are equal: `(\u2200 x, f x = g x) \u2192 f = g`.\nIt is called \"extensionality\" because it talks about how to prove two objects are equal\nbased on the properties of the object (compare with set extensionality,\nwhich is `(\u2200 x, x \u2208 s \u2194 x \u2208 t) \u2192 s = t`).\n\nThis is often an axiom in dependent type theory systems, because it cannot be proved\nfrom the core logic alone. However in lean's type theory this follows from the existence\nof quotient types (note the `Quot.sound` in the proof, as well as the `show` line\nwhich makes use of the definitional equality `Quot.lift f h (Quot.mk x) = f x`).\n-/\ntheorem funext {\u03b1 : Sort u} {\u03b2 : \u03b1 \u2192 Sort v} {f g : (x : \u03b1) \u2192 \u03b2 x}\n    (h : \u2200 x, f x = g x) : f = g := by\n  let eqv (f g : (x : \u03b1) \u2192 \u03b2 x) := \u2200 x, f x = g x\n  let extfunApp (f : Quot eqv) (x : \u03b1) : \u03b2 x :=\n    Quot.liftOn f\n      (fun (f : \u2200 (x : \u03b1), \u03b2 x) => f x)\n      (fun _ _ h => h x)\n  show extfunApp (Quot.mk eqv f) = extfunApp (Quot.mk eqv g)\n  exact congrArg extfunApp (Quot.sound h)\n\ninstance {\u03b1 : Sort u} {\u03b2 : \u03b1 \u2192 Sort v} [\u2200 a, Subsingleton (\u03b2 a)] : Subsingleton (\u2200 a, \u03b2 a) where\n  allEq f g := funext fun a => Subsingleton.elim (f a) (g a)\n\n/-! # Squash -/\n\n/--\n`Squash \u03b1` is the quotient of `\u03b1` by the always true relation.\nIt is empty if `\u03b1` is empty, otherwise it is a singleton.\n(Thus it is unconditionally a `Subsingleton`.)\nIt is the \"universal `Subsingleton`\" mapped from `\u03b1`.\n\nIt is similar to `Nonempty \u03b1`, which has the same properties, but unlike\n`Nonempty` this is a `Type u`, that is, it is \"data\", and the compiler\nrepresents an element of `Squash \u03b1` the same as `\u03b1` itself\n(as compared to `Nonempty \u03b1`, whose elements are represented by a dummy value).\n\n`Squash.lift` will extract a value in any subsingleton `\u03b2` from a function on `\u03b1`,\nwhile `Nonempty.rec` can only do the same when `\u03b2` is a proposition.\n-/\ndef Squash (\u03b1 : Type u) := Quot (fun (_ _ : \u03b1) => True)\n\n/-- The canonical quotient map into `Squash \u03b1`. -/\ndef Squash.mk {\u03b1 : Type u} (x : \u03b1) : Squash \u03b1 := Quot.mk _ x\n\ntheorem Squash.ind {\u03b1 : Type u} {motive : Squash \u03b1 \u2192 Prop} (h : \u2200 (a : \u03b1), motive (Squash.mk a)) : \u2200 (q : Squash \u03b1), motive q :=\n  Quot.ind h\n\n/-- If `\u03b2` is a subsingleton, then a function `\u03b1 \u2192 \u03b2` lifts to `Squash \u03b1 \u2192 \u03b2`. -/\n@[inline] def Squash.lift {\u03b1 \u03b2} [Subsingleton \u03b2] (s : Squash \u03b1) (f : \u03b1 \u2192 \u03b2) : \u03b2 :=\n  Quot.lift f (fun _ _ _ => Subsingleton.elim _ _) s\n\ninstance : Subsingleton (Squash \u03b1) where\n  allEq a b := by\n    induction a using Squash.ind\n    induction b using Squash.ind\n    apply Quot.sound\n    trivial\n\n/-! # Relations -/\n\n/--\n`Antisymm (\u00b7\u2264\u00b7)` says that `(\u00b7\u2264\u00b7)` is antisymmetric, that is, `a \u2264 b \u2192 b \u2264 a \u2192 a = b`.\n-/\nclass Antisymm {\u03b1 : Sort u} (r : \u03b1 \u2192 \u03b1 \u2192 Prop) where\n  /-- An antisymmetric relation `(\u00b7\u2264\u00b7)` satisfies `a \u2264 b \u2192 b \u2264 a \u2192 a = b`. -/\n  antisymm {a b : \u03b1} : r a b \u2192 r b a \u2192 a = b\n\nnamespace Lean\n/-! # Kernel reduction hints -/\n\n/--\nWhen the kernel tries to reduce a term `Lean.reduceBool c`, it will invoke the Lean interpreter to evaluate `c`.\nThe kernel will not use the interpreter if `c` is not a constant.\nThis feature is useful for performing proofs by reflection.\n\nRemark: the Lean frontend allows terms of the from `Lean.reduceBool t` where `t` is a term not containing\nfree variables. The frontend automatically declares a fresh auxiliary constant `c` and replaces the term with\n`Lean.reduceBool c`. The main motivation is that the code for `t` will be pre-compiled.\n\nWarning: by using this feature, the Lean compiler and interpreter become part of your trusted code base.\nThis is extra 30k lines of code. More importantly, you will probably not be able to check your development using\nexternal type checkers (e.g., Trepplein) that do not implement this feature.\nKeep in mind that if you are using Lean as programming language, you are already trusting the Lean compiler and interpreter.\nSo, you are mainly losing the capability of type checking your development using external checkers.\n\nRecall that the compiler trusts the correctness of all `[implemented_by ...]` and `[extern ...]` annotations.\nIf an extern function is executed, then the trusted code base will also include the implementation of the associated\nforeign function.\n-/\nopaque reduceBool (b : Bool) : Bool := b\n\n/--\nSimilar to `Lean.reduceBool` for closed `Nat` terms.\n\nRemark: we do not have plans for supporting a generic `reduceValue {\u03b1} (a : \u03b1) : \u03b1 := a`.\nThe main issue is that it is non-trivial to convert an arbitrary runtime object back into a Lean expression.\nWe believe `Lean.reduceBool` enables most interesting applications (e.g., proof by reflection).\n-/\nopaque reduceNat (n : Nat) : Nat := n\n\n/--\nThe axiom `ofReduceBool` is used to perform proofs by reflection. See `reduceBool`.\n\nThis axiom is usually not used directly, because it has some syntactic restrictions.\nInstead, the `native_decide` tactic can be used to prove any proposition whose\ndecidability instance can be evaluated to `true` using the lean compiler / interpreter.\n\nWarning: by using this feature, the Lean compiler and interpreter become part of your trusted code base.\nThis is extra 30k lines of code. More importantly, you will probably not be able to check your development using\nexternal type checkers (e.g., Trepplein) that do not implement this feature.\nKeep in mind that if you are using Lean as programming language, you are already trusting the Lean compiler and interpreter.\nSo, you are mainly losing the capability of type checking your development using external checkers.\n-/\naxiom ofReduceBool (a b : Bool) (h : reduceBool a = b) : a = b\n\n/--\nThe axiom `ofReduceNat` is used to perform proofs by reflection. See `reduceBool`.\n\nWarning: by using this feature, the Lean compiler and interpreter become part of your trusted code base.\nThis is extra 30k lines of code. More importantly, you will probably not be able to check your development using\nexternal type checkers (e.g., Trepplein) that do not implement this feature.\nKeep in mind that if you are using Lean as programming language, you are already trusting the Lean compiler and interpreter.\nSo, you are mainly losing the capability of type checking your development using external checkers.\n-/\naxiom ofReduceNat (a b : Nat) (h : reduceNat a = b) : a = b\n\n/--\n`IsAssociative op` says that `op` is an associative operation,\ni.e. `(a \u2218 b) \u2218 c = a \u2218 (b \u2218 c)`. It is used by the `ac_rfl` tactic.\n-/\nclass IsAssociative {\u03b1 : Sort u} (op : \u03b1 \u2192 \u03b1 \u2192 \u03b1) where\n  /-- An associative operation satisfies `(a \u2218 b) \u2218 c = a \u2218 (b \u2218 c)`. -/\n  assoc : (a b c : \u03b1) \u2192 op (op a b) c = op a (op b c)\n\n/--\n`IsCommutative op` says that `op` is a commutative operation,\ni.e. `a \u2218 b = b \u2218 a`. It is used by the `ac_rfl` tactic.\n-/\nclass IsCommutative {\u03b1 : Sort u} (op : \u03b1 \u2192 \u03b1 \u2192 \u03b1) where\n  /-- A commutative operation satisfies `a \u2218 b = b \u2218 a`. -/\n  comm : (a b : \u03b1) \u2192 op a b = op b a\n\n/--\n`IsIdempotent op` says that `op` is an idempotent operation,\ni.e. `a \u2218 a = a`. It is used by the `ac_rfl` tactic\n(which also simplifies up to idempotence when available).\n-/\nclass IsIdempotent {\u03b1 : Sort u} (op : \u03b1 \u2192 \u03b1 \u2192 \u03b1) where\n  /-- An idempotent operation satisfies `a \u2218 a = a`. -/\n  idempotent : (x : \u03b1) \u2192 op x x = x\n\n/--\n`IsNeutral op e` says that `e` is a neutral operation for `op`,\ni.e. `a \u2218 e = a = e \u2218 a`. It is used by the `ac_rfl` tactic\n(which also simplifies neutral elements when available).\n-/\nclass IsNeutral {\u03b1 : Sort u} (op : \u03b1 \u2192 \u03b1 \u2192 \u03b1) (neutral : \u03b1) where\n  /-- A neutral element can be cancelled on the left: `e \u2218 a = a`. -/\n  left_neutral : (a : \u03b1) \u2192 op neutral a = a\n  /-- A neutral element can be cancelled on the right: `a \u2218 e = a`. -/\n  right_neutral : (a : \u03b1) \u2192 op a neutral = a\n\nend Lean\n", "meta": {"author": "leanprover", "repo": "lean4", "sha": "742d053a97bdd109a41a921facd1cd6a55e89bc7", "save_path": "github-repos/lean/leanprover-lean4", "path": "github-repos/lean/leanprover-lean4/lean4-742d053a97bdd109a41a921facd1cd6a55e89bc7/src/Init/Core.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4843800842769843, "lm_q2_score": 0.1294027332703691, "lm_q1q2_score": 0.06268010684717351}}
{"text": "/-\nCopyright (c) 2020 Johan Commelin. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Johan Commelin\n\n! This file was ported from Lean 3 source module tactic.dec_trivial\n! leanprover-community/mathlib commit 13881d7a4086e038e49e116066b379a043d13d34\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.Interactive\n\n/-!\n# `dec_trivial` tactic\n\nThe `dec_trivial` tactic tries to use decidability to prove a goal.\nIt is basically a glorified wrapper around `exact dec_trivial`.\n\nThere is an extra option to make it a little bit smarter:\n`dec_trivial!` will revert all hypotheses on which the target depends,\nbefore it tries `exact dec_trivial`.\n-/\n\n\nopen Tactic.Interactive\n\n/- ./././Mathport/Syntax/Translate/Tactic/Mathlib/Core.lean:38:34: unsupported: setup_tactic_parser -/\n/- ./././Mathport/Syntax/Translate/Expr.lean:207:4: warning: unsupported notation `parser.optional -/\n/-- `dec_trivial` tries to use decidability to prove a goal\n(i.e., using `exact dec_trivial`).\nThe variant `dec_trivial!` will revert all hypotheses on which the target depends,\nbefore it tries `exact dec_trivial`.\n\nExample:\n```lean\nexample (n : \u2115) (h : n < 2) : n = 0 \u2228 n = 1 :=\nby dec_trivial!\n```\n-/\nunsafe def tactic.interactive.dec_trivial (revert_deps : parse (parser.optional (tk \"!\"))) :\n    tactic Unit :=\n  if revert_deps.isSome then andthen revert_target_deps tactic.exact_dec_trivial\n  else tactic.exact_dec_trivial\n#align tactic.interactive.dec_trivial tactic.interactive.dec_trivial\n\nadd_tactic_doc\n  { Name := \"dec_trivial\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.dec_trivial]\n    tags := [\"basic\", \"finishing\"] }\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/Tactic/DecTrivial.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.4649015713733885, "lm_q2_score": 0.13296425050508953, "lm_q1q2_score": 0.06181528899630099}}
{"text": "/-\nCopyright (c) 2021 Mario Carneiro. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro\n-/\nimport Std.Linter\nimport Std.Tactic.NoMatch\nimport Std.Tactic.GuardExpr\nimport Std.Tactic.ByCases\nimport Std.Tactic.SeqFocus\nimport Std.Tactic.ShowTerm\nimport Std.Tactic.SimpTrace\nimport Lean.Elab.Tactic.ElabTerm\nimport Std.Lean.Meta.Basic\nimport Std.Lean.Tactic\n\nnamespace Std.Tactic\nopen Lean Parser.Tactic Elab Command Elab.Tactic Meta\n\n/-- `exfalso` converts a goal `\u22a2 tgt` into `\u22a2 False` by applying `False.elim`. -/\nmacro \"exfalso\" : tactic => `(tactic| apply False.elim)\n\n/--\n`_` in tactic position acts like the `done` tactic: it fails and gives the list\nof goals if there are any. It is useful as a placeholder after starting a tactic block\nsuch as `by _` to make it syntactically correct and show the current goal.\n-/\nmacro \"_\" : tactic => `(tactic| {})\n\n@[inherit_doc failIfSuccess]\nsyntax (name := failIfSuccessConv) \"fail_if_success \" Conv.convSeq : conv\n\nattribute [tactic failIfSuccessConv] evalFailIfSuccess\n\n/-- We allow the `rfl` tactic to also use `Iff.rfl`. -/\n-- `rfl` was defined earlier in Lean4, at src/Lean/Init/Tactics.lean\n-- Later we want to allow `rfl` to use all relations marked with an attribute.\nmacro_rules | `(tactic| rfl) => `(tactic| exact Iff.rfl)\n\nmacro_rules | `(tactic| rfl) => `(tactic| exact HEq.rfl)\n\n/-- `rwa` calls `rw`, then closes any remaining goals using `assumption`. -/\nmacro \"rwa \" rws:rwRuleSeq loc:(location)? : tactic =>\n  `(tactic| (rw $rws:rwRuleSeq $[$loc:location]?; assumption))\n\n/--\nLike `exact`, but takes a list of terms and checks that all goals are discharged after the tactic.\n-/\nelab (name := exacts) \"exacts\" \"[\" hs:term,* \"]\" : tactic => do\n  for stx in hs.getElems do\n    evalTactic (\u2190 `(tactic| exact $stx))\n  evalTactic (\u2190 `(tactic| done))\n\n/--\n`by_contra h` proves `\u22a2 p` by contradiction,\nintroducing a hypothesis `h : \u00acp` and proving `False`.\n* If `p` is a negation `\u00acq`, `h : q` will be introduced instead of `\u00ac\u00acq`.\n* If `p` is decidable, it uses `Decidable.byContradiction` instead of `Classical.byContradiction`.\n* If `h` is omitted, the introduced variable `_: \u00acp` will be anonymous.\n-/\nmacro (name := byContra) tk:\"by_contra\" e?:(ppSpace colGt binderIdent)? : tactic => do\n  let e := match e? with\n    | some e => match e with\n      | `(binderIdent| $e:ident) => e\n      | e => Unhygienic.run `(_%$e) -- HACK: hover fails without Unhygienic here\n    | none => Unhygienic.run `(_%$tk)\n  `(tactic| first\n    | guard_target = Not _; intro $e:term\n    | refine Decidable.byContradiction fun $e => ?_\n    | refine Classical.byContradiction fun $e => ?_)\n\n/--\n`iterate n tac` runs `tac` exactly `n` times.\n`iterate tac` runs `tac` repeatedly until failure.\n\nTo run multiple tactics, one can do `iterate (tac\u2081; tac\u2082; \u22ef)` or\n```lean\niterate\n  tac\u2081\n  tac\u2082\n  \u22ef\n```\n-/\nsyntax \"iterate\" (ppSpace num)? ppSpace tacticSeq : tactic\nmacro_rules\n  | `(tactic| iterate $seq:tacticSeq) =>\n    `(tactic| try ($seq:tacticSeq); iterate $seq:tacticSeq)\n  | `(tactic| iterate $n $seq:tacticSeq) =>\n    match n.1.toNat with\n    | 0 => `(tactic| skip)\n    | n+1 => `(tactic| ($seq:tacticSeq); iterate $(quote n) $seq:tacticSeq)\n\n/--\n`repeat' tac` runs `tac` on all of the goals to produce a new list of goals,\nthen runs `tac` again on all of those goals, and repeats until `tac` fails on all remaining goals.\n-/\nelab \"repeat' \" tac:tacticSeq : tactic => do\n  setGoals (\u2190 repeat' (evalTacticAtRaw tac) (\u2190 getGoals))\n\n/--\n`repeat1 tac` applies `tac` to main goal at least once. If the application succeeds,\nthe tactic is applied recursively to the generated subgoals until it eventually fails.\n-/\nmacro \"repeat1 \" tac:tacticSeq : tactic => `(tactic| focus (($tac); repeat' $tac))\n\n/-- `subst_eqs` applies `subst` to all equalities in the context as long as it makes progress. -/\nelab \"subst_eqs\" : tactic => Elab.Tactic.liftMetaTactic1 (\u00b7.substEqs)\n\n/-- `split_ands` applies `And.intro` until it does not make progress. -/\nsyntax \"split_ands\" : tactic\nmacro_rules | `(tactic| split_ands) => `(tactic| repeat' refine And.intro ?_ ?_)\n\n/--\n`fapply e` is like `apply e` but it adds goals in the order they appear,\nrather than putting the dependent goals first.\n-/\nelab \"fapply \" e:term : tactic =>\n  evalApplyLikeTactic (\u00b7.apply (cfg := {newGoals := .all})) e\n\n/--\n`eapply e` is like `apply e` but it does not add subgoals for variables that appear\nin the types of other goals. Note that this can lead to a failure where there are\nno goals remaining but there are still metavariables in the term:\n```\nexample (h : \u2200 x : Nat, x = x \u2192 True) : True := by\n  eapply h\n  rfl\n  -- no goals\n-- (kernel) declaration has metavariables '_example'\n```\n-/\nelab \"eapply \" e:term : tactic =>\n  evalApplyLikeTactic (\u00b7.apply (cfg := {newGoals := .nonDependentOnly})) e\n\n/--\nTries to solve the goal using a canonical proof of `True`, or the `rfl` tactic.\nUnlike `trivial` or `trivial'`, does not use the `contradiction` tactic.\n-/\nmacro (name := triv) \"triv\" : tactic =>\n  `(tactic| first | exact trivial | rfl | fail \"triv tactic failed\")\n\n/-- `conv` tactic to close a goal using an equality theorem. -/\nmacro (name := Conv.exact) \"exact\" t:term : conv => `(conv| tactic => exact $t)\n", "meta": {"author": "leanprover", "repo": "std4", "sha": "5507f9d8409f93b984ce04eccf4914d534e6fca2", "save_path": "github-repos/lean/leanprover-std4", "path": "github-repos/lean/leanprover-std4/std4-5507f9d8409f93b984ce04eccf4914d534e6fca2/Std/Tactic/Basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48828339529583464, "lm_q2_score": 0.12592275335433112, "lm_q1q2_score": 0.06148598955285275}}
{"text": "/-\nCopyright (c) 2020 Reid Barton. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Reid Barton\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.auto_cases\nimport Mathlib.tactic.tidy\nimport Mathlib.tactic.with_local_reducibility\nimport Mathlib.tactic.show_term\nimport Mathlib.topology.basic\nimport Mathlib.PostPort\n\nuniverses u_1 \n\nnamespace Mathlib\n\n/-!\n# Tactics for topology\n\nCurrently we have one domain-specific tactic for topology: `continuity`.\n\n-/\n\n/-!\n### `continuity` tactic\n\nAutomatically solve goals of the form `continuous f`.\n\nMark lemmas with `@[continuity]` to add them to the set of lemmas\nused by `continuity`. Note: `to_additive` doesn't know yet how to\ncopy the attribute to the additive version.\n-/\n\n/-- User attribute used to mark tactics used by `continuity`. -/\n-- Mark some continuity lemmas already defined in `topology.basic`\n\n-- As we will be using `apply_rules` with `md := semireducible`,\n\n-- we need another version of `continuous_id`.\n\ntheorem continuous_id' {\u03b1 : Type u_1} [topological_space \u03b1] : continuous fun (a : \u03b1) => a :=\n  continuous_id\n\nnamespace tactic\n\n\n/--\nTactic to apply `continuous.comp` when appropriate.\n\nApplying `continuous.comp` is not always a good idea, so we have some\nextra logic here to try to avoid bad cases.\n\n* If the function we're trying to prove continuous is actually\n  constant, and that constant is a function application `f z`, then\n  continuous.comp would produce new goals `continuous f`, `continuous\n  (\u03bb _, z)`, which is silly. We avoid this by failing if we could\n  apply continuous_const.\n\n* continuous.comp will always succeed on `continuous (\u03bb x, f x)` and\n  produce new goals `continuous (\u03bb x, x)`, `continuous f`. We detect\n  this by failing if a new goal can be closed by applying\n  continuous_id.\n-/\n/-- List of tactics used by `continuity` internally. -/\nnamespace interactive\n\n\n/--\nSolve goals of the form `continuous f`. `continuity?` reports back the proof term it found.\n-/\n/-- Version of `continuity` for use with auto_param. -/\n/--\n`continuity` solves goals of the form `continuous f` by applying lemmas tagged with the\n", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/topology/tactic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48438008427698437, "lm_q2_score": 0.12592275335433115, "lm_q1q2_score": 0.06099447388216084}}
{"text": "/-\nCopyright (c) 2021 Eric Wieser. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Eric Wieser\n-/\nimport algebra.module.equiv\nimport data.dfinsupp.basic\nimport data.finsupp.basic\n\n/-!\n# Conversion between `finsupp` and homogenous `dfinsupp`\n\n> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.\n> Any changes to this file require a corresponding PR to mathlib4.\n\nThis module provides conversions between `finsupp` and `dfinsupp`.\nIt is in its own file since neither `finsupp` or `dfinsupp` depend on each other.\n\n## Main definitions\n\n* \"identity\" maps between `finsupp` and `dfinsupp`:\n  * `finsupp.to_dfinsupp : (\u03b9 \u2192\u2080 M) \u2192 (\u03a0\u2080 i : \u03b9, M)`\n  * `dfinsupp.to_finsupp : (\u03a0\u2080 i : \u03b9, M) \u2192 (\u03b9 \u2192\u2080 M)`\n  * Bundled equiv versions of the above:\n    * `finsupp_equiv_dfinsupp : (\u03b9 \u2192\u2080 M) \u2243 (\u03a0\u2080 i : \u03b9, M)`\n    * `finsupp_add_equiv_dfinsupp : (\u03b9 \u2192\u2080 M) \u2243+ (\u03a0\u2080 i : \u03b9, M)`\n    * `finsupp_lequiv_dfinsupp R : (\u03b9 \u2192\u2080 M) \u2243\u2097[R] (\u03a0\u2080 i : \u03b9, M)`\n* stronger versions of `finsupp.split`:\n  * `sigma_finsupp_equiv_dfinsupp : ((\u03a3 i, \u03b7 i) \u2192\u2080 N) \u2243 (\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N))`\n  * `sigma_finsupp_add_equiv_dfinsupp : ((\u03a3 i, \u03b7 i) \u2192\u2080 N) \u2243+ (\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N))`\n  * `sigma_finsupp_lequiv_dfinsupp : ((\u03a3 i, \u03b7 i) \u2192\u2080 N) \u2243\u2097[R] (\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N))`\n\n## Theorems\n\nThe defining features of these operations is that they preserve the function and support:\n\n* `finsupp.to_dfinsupp_coe`\n* `finsupp.to_dfinsupp_support`\n* `dfinsupp.to_finsupp_coe`\n* `dfinsupp.to_finsupp_support`\n\nand therefore map `finsupp.single` to `dfinsupp.single` and vice versa:\n\n* `finsupp.to_dfinsupp_single`\n* `dfinsupp.to_finsupp_single`\n\nas well as preserving arithmetic operations.\n\nFor the bundled equivalences, we provide lemmas that they reduce to `finsupp.to_dfinsupp`:\n\n* `finsupp_add_equiv_dfinsupp_apply`\n* `finsupp_lequiv_dfinsupp_apply`\n* `finsupp_add_equiv_dfinsupp_symm_apply`\n* `finsupp_lequiv_dfinsupp_symm_apply`\n\n## Implementation notes\n\nWe provide `dfinsupp.to_finsupp` and `finsupp_equiv_dfinsupp` computably by adding\n`[decidable_eq \u03b9]` and `[\u03a0 m : M, decidable (m \u2260 0)]` arguments. To aid with definitional unfolding,\nthese arguments are also present on the `noncomputable` equivs.\n-/\n\nvariables {\u03b9 : Type*} {R : Type*} {M : Type*}\n\n\n/-! ### Basic definitions and lemmas -/\nsection defs\n\n/-- Interpret a `finsupp` as a homogenous `dfinsupp`. -/\ndef finsupp.to_dfinsupp [has_zero M] (f : \u03b9 \u2192\u2080 M) : \u03a0\u2080 i : \u03b9, M :=\n{ to_fun := f,\n  support' := trunc.mk\n    \u27e8f.support.1, \u03bb i, (classical.em (f i = 0)).symm.imp_left (finsupp.mem_support_iff.mpr)\u27e9 }\n\n@[simp] lemma finsupp.to_dfinsupp_coe [has_zero M] (f : \u03b9 \u2192\u2080 M) : \u21d1f.to_dfinsupp = f := rfl\n\nsection\nvariables [decidable_eq \u03b9] [has_zero M]\n\n@[simp] lemma finsupp.to_dfinsupp_single (i : \u03b9) (m : M) :\n  (finsupp.single i m).to_dfinsupp = dfinsupp.single i m :=\nby { ext, simp [finsupp.single_apply, dfinsupp.single_apply] }\n\nvariables [\u03a0 m : M, decidable (m \u2260 0)]\n\n@[simp] lemma to_dfinsupp_support (f : \u03b9 \u2192\u2080 M) : f.to_dfinsupp.support = f.support :=\nby { ext, simp, }\n\n/-- Interpret a homogenous `dfinsupp` as a `finsupp`.\n\nNote that the elaborator has a lot of trouble with this definition - it is often necessary to\nwrite `(dfinsupp.to_finsupp f : \u03b9 \u2192\u2080 M)` instead of `f.to_finsupp`, as for some unknown reason\nusing dot notation or omitting the type ascription prevents the type being resolved correctly. -/\ndef dfinsupp.to_finsupp (f : \u03a0\u2080 i : \u03b9, M) : \u03b9 \u2192\u2080 M :=\n\u27e8f.support, f, \u03bb i, by simp only [dfinsupp.mem_support_iff]\u27e9\n\n@[simp] lemma dfinsupp.to_finsupp_coe (f : \u03a0\u2080 i : \u03b9, M) : \u21d1f.to_finsupp = f := rfl\n@[simp] lemma dfinsupp.to_finsupp_support (f : \u03a0\u2080 i : \u03b9, M) : f.to_finsupp.support = f.support :=\nby { ext, simp, }\n\n@[simp] lemma dfinsupp.to_finsupp_single (i : \u03b9) (m : M) :\n  (dfinsupp.single i m : \u03a0\u2080 i : \u03b9, M).to_finsupp = finsupp.single i m :=\nby { ext, simp [finsupp.single_apply, dfinsupp.single_apply] }\n\n@[simp] lemma finsupp.to_dfinsupp_to_finsupp (f : \u03b9 \u2192\u2080 M) : f.to_dfinsupp.to_finsupp = f :=\nfinsupp.coe_fn_injective rfl\n\n@[simp] lemma dfinsupp.to_finsupp_to_dfinsupp (f : \u03a0\u2080 i : \u03b9, M) : f.to_finsupp.to_dfinsupp = f :=\ndfinsupp.coe_fn_injective rfl\n\nend\n\nend defs\n\n/-! ### Lemmas about arithmetic operations -/\nsection lemmas\n\nnamespace finsupp\n\n@[simp] lemma to_dfinsupp_zero [has_zero M] :\n  (0 : \u03b9 \u2192\u2080 M).to_dfinsupp = 0 := dfinsupp.coe_fn_injective rfl\n\n@[simp] lemma to_dfinsupp_add [add_zero_class M] (f g : \u03b9 \u2192\u2080 M) :\n  (f + g).to_dfinsupp = f.to_dfinsupp + g.to_dfinsupp := dfinsupp.coe_fn_injective rfl\n\n@[simp] lemma to_dfinsupp_neg [add_group M] (f : \u03b9 \u2192\u2080 M) :\n  (-f).to_dfinsupp = -f.to_dfinsupp := dfinsupp.coe_fn_injective rfl\n\n@[simp] lemma to_dfinsupp_sub [add_group M] (f g : \u03b9 \u2192\u2080 M) :\n  (f - g).to_dfinsupp = f.to_dfinsupp - g.to_dfinsupp :=\ndfinsupp.coe_fn_injective rfl\n\n@[simp] lemma to_dfinsupp_smul [monoid R] [add_monoid M] [distrib_mul_action R M]\n  (r : R) (f : \u03b9 \u2192\u2080 M) : (r \u2022 f).to_dfinsupp = r \u2022 f.to_dfinsupp :=\ndfinsupp.coe_fn_injective rfl\n\nend finsupp\n\nnamespace dfinsupp\nvariables [decidable_eq \u03b9]\n\n@[simp] lemma to_finsupp_zero [has_zero M] [\u03a0 m : M, decidable (m \u2260 0)] :\n  to_finsupp 0 = (0 : \u03b9 \u2192\u2080 M) := finsupp.coe_fn_injective rfl\n\n@[simp] lemma to_finsupp_add [add_zero_class M] [\u03a0 m : M, decidable (m \u2260 0)] (f g : \u03a0\u2080 i : \u03b9, M) :\n  (to_finsupp (f + g) : \u03b9 \u2192\u2080 M) = (to_finsupp f + to_finsupp g) :=\nfinsupp.coe_fn_injective $ dfinsupp.coe_add _ _\n\n@[simp] lemma to_finsupp_neg [add_group M] [\u03a0 m : M, decidable (m \u2260 0)] (f : \u03a0\u2080 i : \u03b9, M) :\n  (to_finsupp (-f) : \u03b9 \u2192\u2080 M) = -to_finsupp f :=\nfinsupp.coe_fn_injective $ dfinsupp.coe_neg _\n\n@[simp] lemma to_finsupp_sub [add_group M] [\u03a0 m : M, decidable (m \u2260 0)] (f g : \u03a0\u2080 i : \u03b9, M) :\n  (to_finsupp (f - g) : \u03b9 \u2192\u2080 M) = to_finsupp f - to_finsupp g :=\nfinsupp.coe_fn_injective $ dfinsupp.coe_sub _ _\n\n@[simp] lemma to_finsupp_smul [monoid R] [add_monoid M] [distrib_mul_action R M]\n  [\u03a0 m : M, decidable (m \u2260 0)]\n  (r : R) (f : \u03a0\u2080 i : \u03b9, M) : (to_finsupp (r \u2022 f) : \u03b9 \u2192\u2080 M) = r \u2022 to_finsupp f :=\nfinsupp.coe_fn_injective $ dfinsupp.coe_smul _ _\n\nend dfinsupp\n\nend lemmas\n\n/-! ### Bundled `equiv`s -/\n\nsection equivs\n\n/-- `finsupp.to_dfinsupp` and `dfinsupp.to_finsupp` together form an equiv. -/\n@[simps {fully_applied := ff}]\ndef finsupp_equiv_dfinsupp [decidable_eq \u03b9] [has_zero M] [\u03a0 m : M, decidable (m \u2260 0)] :\n  (\u03b9 \u2192\u2080 M) \u2243 (\u03a0\u2080 i : \u03b9, M) :=\n{ to_fun := finsupp.to_dfinsupp, inv_fun := dfinsupp.to_finsupp,\n  left_inv := finsupp.to_dfinsupp_to_finsupp, right_inv := dfinsupp.to_finsupp_to_dfinsupp }\n\n/-- The additive version of `finsupp.to_finsupp`. Note that this is `noncomputable` because\n`finsupp.has_add` is noncomputable. -/\n@[simps {fully_applied := ff}]\ndef finsupp_add_equiv_dfinsupp\n  [decidable_eq \u03b9] [add_zero_class M] [\u03a0 m : M, decidable (m \u2260 0)] :\n  (\u03b9 \u2192\u2080 M) \u2243+ (\u03a0\u2080 i : \u03b9, M) :=\n{ to_fun := finsupp.to_dfinsupp, inv_fun := dfinsupp.to_finsupp,\n  map_add' := finsupp.to_dfinsupp_add,\n  .. finsupp_equiv_dfinsupp}\n\nvariables (R)\n\n/-- The additive version of `finsupp.to_finsupp`. Note that this is `noncomputable` because\n`finsupp.has_add` is noncomputable. -/\n@[simps {fully_applied := ff}]\ndef finsupp_lequiv_dfinsupp\n  [decidable_eq \u03b9] [semiring R] [add_comm_monoid M] [\u03a0 m : M, decidable (m \u2260 0)] [module R M] :\n  (\u03b9 \u2192\u2080 M) \u2243\u2097[R] (\u03a0\u2080 i : \u03b9, M) :=\n{ to_fun := finsupp.to_dfinsupp, inv_fun := dfinsupp.to_finsupp,\n  map_smul' := finsupp.to_dfinsupp_smul,\n  map_add' := finsupp.to_dfinsupp_add,\n  .. finsupp_equiv_dfinsupp}\n\nsection sigma\n/-- ### Stronger versions of `finsupp.split` -/\n\nnoncomputable theory\n\nvariables {\u03b7 : \u03b9 \u2192 Type*} {N : Type*} [semiring R]\n\nopen finsupp\n\n/-- `finsupp.split` is an equivalence between `(\u03a3 i, \u03b7 i) \u2192\u2080 N` and `\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)`. -/\ndef sigma_finsupp_equiv_dfinsupp [has_zero N] : ((\u03a3 i, \u03b7 i) \u2192\u2080 N) \u2243 (\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)) :=\n{ to_fun := \u03bb f, \u27e8split f, trunc.mk \u27e8(split_support f : finset \u03b9).val, \u03bb i,\n    begin\n      rw [\u2190 finset.mem_def, mem_split_support_iff_nonzero],\n      exact (em _).symm\n    end\u27e9\u27e9,\n  inv_fun := \u03bb f,\n  begin\n    haveI := classical.dec_eq \u03b9,\n    haveI := \u03bb i, classical.dec_eq (\u03b7 i \u2192\u2080 N),\n    refine on_finset (finset.sigma f.support (\u03bb j, (f j).support)) (\u03bb ji, f ji.1 ji.2)\n      (\u03bb g hg, finset.mem_sigma.mpr \u27e8_, mem_support_iff.mpr hg\u27e9),\n    simp only [ne.def, dfinsupp.mem_support_to_fun],\n    intro h,\n    rw h at hg,\n    simpa using hg\n  end,\n  left_inv := \u03bb f, by { ext, simp [split] },\n  right_inv := \u03bb f, by { ext, simp [split] } }\n\n@[simp]\nlemma sigma_finsupp_equiv_dfinsupp_apply [has_zero N] (f : (\u03a3 i, \u03b7 i) \u2192\u2080 N) :\n  (sigma_finsupp_equiv_dfinsupp f : \u03a0 i, (\u03b7 i \u2192\u2080 N)) = finsupp.split f := rfl\n\n@[simp]\nlemma sigma_finsupp_equiv_dfinsupp_symm_apply [has_zero N] (f : \u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)) (s : \u03a3 i, \u03b7 i) :\n  (sigma_finsupp_equiv_dfinsupp.symm f : (\u03a3 i, \u03b7 i) \u2192\u2080 N) s = f s.1 s.2 := rfl\n\n@[simp]\nlemma sigma_finsupp_equiv_dfinsupp_support\n  [decidable_eq \u03b9] [has_zero N] [\u03a0 (i : \u03b9) (x : \u03b7 i \u2192\u2080 N), decidable (x \u2260 0)]\n  (f : (\u03a3 i, \u03b7 i) \u2192\u2080 N) :\n  (sigma_finsupp_equiv_dfinsupp f).support = finsupp.split_support f :=\nbegin\n  ext,\n  rw dfinsupp.mem_support_to_fun,\n  exact (finsupp.mem_split_support_iff_nonzero _ _).symm,\nend\n\n@[simp] lemma sigma_finsupp_equiv_dfinsupp_single [decidable_eq \u03b9] [has_zero N]\n  (a : \u03a3 i, \u03b7 i) (n : N) :\n  sigma_finsupp_equiv_dfinsupp (finsupp.single a n)\n    = @dfinsupp.single _ (\u03bb i, \u03b7 i \u2192\u2080 N) _ _ a.1 (finsupp.single a.2 n) :=\nbegin\n  obtain \u27e8i, a\u27e9 := a,\n  ext j b,\n  by_cases h : i = j,\n  { subst h,\n    classical,\n    simp [split_apply, finsupp.single_apply] },\n  suffices : finsupp.single (\u27e8i, a\u27e9 : \u03a3 i, \u03b7 i) n \u27e8j, b\u27e9 = 0,\n  { simp [split_apply, dif_neg h, this] },\n  have H : (\u27e8i, a\u27e9 : \u03a3 i, \u03b7 i) \u2260 \u27e8j, b\u27e9 := by simp [h],\n  classical,\n  rw [finsupp.single_apply, if_neg H]\nend\n\n-- Without this Lean fails to find the `add_zero_class` instance on `\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)`.\nlocal attribute [-instance] finsupp.has_zero\n\n@[simp]\nlemma sigma_finsupp_equiv_dfinsupp_add [add_zero_class N] (f g : (\u03a3 i, \u03b7 i) \u2192\u2080 N) :\n  sigma_finsupp_equiv_dfinsupp (f + g) =\n  (sigma_finsupp_equiv_dfinsupp f + (sigma_finsupp_equiv_dfinsupp g) : (\u03a0\u2080 (i : \u03b9), \u03b7 i \u2192\u2080 N)) :=\nby {ext, refl}\n\n/-- `finsupp.split` is an additive equivalence between `(\u03a3 i, \u03b7 i) \u2192\u2080 N` and `\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)`. -/\n@[simps]\ndef sigma_finsupp_add_equiv_dfinsupp [add_zero_class N] : ((\u03a3 i, \u03b7 i) \u2192\u2080 N) \u2243+ (\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)) :=\n{ to_fun := sigma_finsupp_equiv_dfinsupp,\n  inv_fun := sigma_finsupp_equiv_dfinsupp.symm,\n  map_add' := sigma_finsupp_equiv_dfinsupp_add,\n  .. sigma_finsupp_equiv_dfinsupp }\n\nlocal attribute [-instance] finsupp.add_zero_class\n\n--tofix: r \u2022 (sigma_finsupp_equiv_dfinsupp f) doesn't work.\n@[simp]\nlemma sigma_finsupp_equiv_dfinsupp_smul {R} [monoid R] [add_monoid N] [distrib_mul_action R N]\n  (r : R) (f : (\u03a3 i, \u03b7 i) \u2192\u2080 N) : sigma_finsupp_equiv_dfinsupp (r \u2022 f) =\n  @has_smul.smul R (\u03a0\u2080 i, \u03b7 i \u2192\u2080 N) mul_action.to_has_smul r (sigma_finsupp_equiv_dfinsupp f) :=\nby { ext, refl }\n\nlocal attribute [-instance] finsupp.add_monoid\n\n/-- `finsupp.split` is a linear equivalence between `(\u03a3 i, \u03b7 i) \u2192\u2080 N` and `\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)`. -/\n@[simps]\ndef sigma_finsupp_lequiv_dfinsupp [add_comm_monoid N] [module R N] :\n  ((\u03a3 i, \u03b7 i) \u2192\u2080 N) \u2243\u2097[R] (\u03a0\u2080 i, (\u03b7 i \u2192\u2080 N)) :=\n{ map_smul' := sigma_finsupp_equiv_dfinsupp_smul,\n  .. sigma_finsupp_add_equiv_dfinsupp }\n\nend sigma\n\nend equivs\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/data/finsupp/to_dfinsupp.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4960938294709195, "lm_q2_score": 0.12252320771203076, "lm_q1q2_score": 0.06078300731292224}}
{"text": "/-\nCopyright (c) 2020 Reid Barton. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Reid Barton\n-/\nimport tactic.auto_cases\nimport tactic.tidy\nimport tactic.with_local_reducibility\nimport tactic.show_term\nimport topology.basic\n/-!\n# Tactics for topology\n\nCurrently we have one domain-specific tactic for topology: `continuity`.\n\n-/\n\n/-!\n### `continuity` tactic\n\nAutomatically solve goals of the form `continuous f`.\n\nMark lemmas with `@[continuity]` to add them to the set of lemmas\nused by `continuity`.\n-/\n\n/-- User attribute used to mark tactics used by `continuity`. -/\n@[user_attribute]\nmeta def continuity : user_attribute :=\n{ name := `continuity,\n  descr := \"lemmas usable to prove continuity\" }\n\n-- Mark some continuity lemmas already defined in `topology.basic`\nattribute [continuity]\n  continuous_id\n  continuous_const\n\n-- As we will be using `apply_rules` with `md := semireducible`,\n-- we need another version of `continuous_id`.\n@[continuity] lemma continuous_id' {\u03b1 : Type*} [topological_space \u03b1] : continuous (\u03bb a : \u03b1, a) :=\ncontinuous_id\n\nnamespace tactic\n\n/--\nTactic to apply `continuous.comp` when appropriate.\n\nApplying `continuous.comp` is not always a good idea, so we have some\nextra logic here to try to avoid bad cases.\n\n* If the function we're trying to prove continuous is actually\n  constant, and that constant is a function application `f z`, then\n  continuous.comp would produce new goals `continuous f`, `continuous\n  (\u03bb _, z)`, which is silly. We avoid this by failing if we could\n  apply continuous_const.\n\n* continuous.comp will always succeed on `continuous (\u03bb x, f x)` and\n  produce new goals `continuous (\u03bb x, x)`, `continuous f`. We detect\n  this by failing if a new goal can be closed by applying\n  continuous_id.\n-/\nmeta def apply_continuous.comp : tactic unit :=\n`[fail_if_success { exact continuous_const };\n  refine continuous.comp _ _;\n  fail_if_success { exact continuous_id }]\n\n/-- List of tactics used by `continuity` internally. -/\nmeta def continuity_tactics (md : transparency := reducible) : list (tactic string) :=\n[\n  intros1               >>= \u03bb ns, pure (\"intros \" ++ (\" \".intercalate (ns.map (\u03bb e, e.to_string)))),\n  apply_rules [``(continuity)] 50 { md := md }\n                        >> pure \"apply_rules continuity\",\n  apply_continuous.comp >> pure \"refine continuous.comp _ _\"\n]\n\nnamespace interactive\nsetup_tactic_parser\n\n/--\nSolve goals of the form `continuous f`. `continuity?` reports back the proof term it found.\n-/\nmeta def continuity\n  (bang : parse $ optional (tk \"!\")) (trace : parse $ optional (tk \"?\")) (cfg : tidy.cfg := {}) :\n  tactic unit :=\nlet md              := if bang.is_some then semireducible else reducible,\n    continuity_core := tactic.tidy { tactics := continuity_tactics md, ..cfg },\n    trace_fn        := if trace.is_some then show_term else id in\ntrace_fn continuity_core\n\n/-- Version of `continuity` for use with auto_param. -/\nmeta def continuity' : tactic unit := continuity none none {}\n\n/--\n`continuity` solves goals of the form `continuous f` by applying lemmas tagged with the\n`continuity` user attribute.\n\n```\nexample {X Y : Type*} [topological_space X] [topological_space Y]\n  (f\u2081 f\u2082 : X \u2192 Y) (hf\u2081 : continuous f\u2081) (hf\u2082 : continuous f\u2082)\n  (g : Y \u2192 \u211d) (hg : continuous g) : continuous (\u03bb x, (max (g (f\u2081 x)) (g (f\u2082 x))) + 1) :=\nby continuity\n```\nwill discharge the goal, generating a proof term like\n`((continuous.comp hg hf\u2081).max (continuous.comp hg hf\u2082)).add continuous_const`\n\nYou can also use `continuity!`, which applies lemmas with `{ md := semireducible }`.\nThe default behaviour is more conservative, and only unfolds `reducible` definitions\nwhen attempting to match lemmas with the goal.\n\n`continuity?` reports back the proof term it found.\n-/\nadd_tactic_doc\n{ name := \"continuity / continuity'\",\n  category := doc_category.tactic,\n  decl_names := [`tactic.interactive.continuity, `tactic.interactive.continuity'],\n  tags := [\"lemma application\"] }\n\nend interactive\n\nend tactic\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/topology/tactic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48438008427698437, "lm_q2_score": 0.1242130018680137, "lm_q1q2_score": 0.060166304313125696}}
{"text": "/-\nCopyright (c) 2019 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.category_theory.comma\nimport Mathlib.category_theory.groupoid\nimport Mathlib.category_theory.punit\nimport Mathlib.PostPort\n\nuniverses w v u \n\nnamespace Mathlib\n\n/-!\n# The category of elements\n\nThis file defines the category of elements, also known as (a special case of) the Grothendieck construction.\n\nGiven a functor `F : C \u2964 Type`, an object of `F.elements` is a pair `(X : C, x : F.obj X)`.\nA morphism `(X, x) \u27f6 (Y, y)` is a morphism `f : X \u27f6 Y` in `C`, so `F.map f` takes `x` to `y`.\n\n## Implementation notes\nThis construction is equivalent to a special case of a comma construction, so this is mostly just\na more convenient API. We prove the equivalence in `category_theory.category_of_elements.comma_equivalence`.\n\n## References\n* [Emily Riehl, *Category Theory in Context*, Section 2.4][riehl2017]\n* <https://en.wikipedia.org/wiki/Category_of_elements>\n* <https://ncatlab.org/nlab/show/category+of+elements>\n\n## Tags\ncategory of elements, Grothendieck construction, comma category\n-/\n\nnamespace category_theory\n\n\n/--\nThe type of objects for the category of elements of a functor `F : C \u2964 Type`\nis a pair `(X : C, x : F.obj X)`.\n-/\ndef functor.elements {C : Type u} [category C] (F : C \u2964 Type w) :=\n  sigma fun (c : C) => functor.obj F c\n\n/-- The category structure on `F.elements`, for `F : C \u2964 Type`.\n    A morphism `(X, x) \u27f6 (Y, y)` is a morphism `f : X \u27f6 Y` in `C`, so `F.map f` takes `x` to `y`.\n -/\nprotected instance category_of_elements {C : Type u} [category C] (F : C \u2964 Type w) :\n    category (functor.elements F) :=\n  category.mk\n\nnamespace category_of_elements\n\n\ntheorem ext {C : Type u} [category C] (F : C \u2964 Type w) {x : functor.elements F}\n    {y : functor.elements F} (f : x \u27f6 y) (g : x \u27f6 y) (w : subtype.val f = subtype.val g) : f = g :=\n  subtype.ext_val w\n\n@[simp] theorem comp_val {C : Type u} [category C] {F : C \u2964 Type w} {p : functor.elements F}\n    {q : functor.elements F} {r : functor.elements F} {f : p \u27f6 q} {g : q \u27f6 r} :\n    subtype.val (f \u226b g) = subtype.val f \u226b subtype.val g :=\n  rfl\n\n@[simp] theorem id_val {C : Type u} [category C] {F : C \u2964 Type w} {p : functor.elements F} :\n    subtype.val \ud835\udfd9 = \ud835\udfd9 :=\n  rfl\n\nend category_of_elements\n\n\nprotected instance groupoid_of_elements {G : Type u} [groupoid G] (F : G \u2964 Type w) :\n    groupoid (functor.elements F) :=\n  groupoid.mk\n    fun (p q : functor.elements F) (f : p \u27f6 q) => { val := inv (subtype.val f), property := sorry }\n\nnamespace category_of_elements\n\n\n/-- The functor out of the category of elements which forgets the element. -/\n@[simp] theorem \u03c0_map {C : Type u} [category C] (F : C \u2964 Type w) (X : functor.elements F)\n    (Y : functor.elements F) (f : X \u27f6 Y) : functor.map (\u03c0 F) f = subtype.val f :=\n  Eq.refl (functor.map (\u03c0 F) f)\n\n/--\nA natural transformation between functors induces a functor between the categories of elements.\n-/\n@[simp] theorem map_obj_fst {C : Type u} [category C] {F\u2081 : C \u2964 Type w} {F\u2082 : C \u2964 Type w}\n    (\u03b1 : F\u2081 \u27f6 F\u2082) (t : functor.elements F\u2081) : sigma.fst (functor.obj (map \u03b1) t) = sigma.fst t :=\n  Eq.refl (sigma.fst (functor.obj (map \u03b1) t))\n\n@[simp] theorem map_\u03c0 {C : Type u} [category C] {F\u2081 : C \u2964 Type w} {F\u2082 : C \u2964 Type w} (\u03b1 : F\u2081 \u27f6 F\u2082) :\n    map \u03b1 \u22d9 \u03c0 F\u2082 = \u03c0 F\u2081 :=\n  rfl\n\n/-- The forward direction of the equivalence `F.elements \u2245 (*, F)`. -/\ndef to_comma {C : Type u} [category C] (F : C \u2964 Type w) :\n    functor.elements F \u2964 comma (functor.from_punit PUnit) F :=\n  functor.mk\n    (fun (X : functor.elements F) =>\n      comma.mk fun (_x : functor.obj (functor.from_punit PUnit) PUnit.unit) => sigma.snd X)\n    fun (X Y : functor.elements F) (f : X \u27f6 Y) => comma_morphism.mk\n\n@[simp] theorem to_comma_obj {C : Type u} [category C] (F : C \u2964 Type w) (X : functor.elements F) :\n    functor.obj (to_comma F) X =\n        comma.mk fun (_x : functor.obj (functor.from_punit PUnit) PUnit.unit) => sigma.snd X :=\n  rfl\n\n@[simp] theorem to_comma_map {C : Type u} [category C] (F : C \u2964 Type w) {X : functor.elements F}\n    {Y : functor.elements F} (f : X \u27f6 Y) : functor.map (to_comma F) f = comma_morphism.mk :=\n  rfl\n\n/-- The reverse direction of the equivalence `F.elements \u2245 (*, F)`. -/\ndef from_comma {C : Type u} [category C] (F : C \u2964 Type w) :\n    comma (functor.from_punit PUnit) F \u2964 functor.elements F :=\n  functor.mk\n    (fun (X : comma (functor.from_punit PUnit) F) =>\n      sigma.mk (comma.right X) (comma.hom X PUnit.unit))\n    fun (X Y : comma (functor.from_punit PUnit) F) (f : X \u27f6 Y) =>\n      { val := comma_morphism.right f, property := sorry }\n\n@[simp] theorem from_comma_obj {C : Type u} [category C] (F : C \u2964 Type w)\n    (X : comma (functor.from_punit PUnit) F) :\n    functor.obj (from_comma F) X = sigma.mk (comma.right X) (comma.hom X PUnit.unit) :=\n  rfl\n\n@[simp] theorem from_comma_map {C : Type u} [category C] (F : C \u2964 Type w)\n    {X : comma (functor.from_punit PUnit) F} {Y : comma (functor.from_punit PUnit) F} (f : X \u27f6 Y) :\n    functor.map (from_comma F) f =\n        { val := comma_morphism.right f,\n          property := congr_fun (Eq.symm (comma_morphism.w' f)) PUnit.unit } :=\n  rfl\n\n/-- The equivalence between the category of elements `F.elements`\n    and the comma category `(*, F)`. -/\ndef comma_equivalence {C : Type u} [category C] (F : C \u2964 Type w) :\n    functor.elements F \u224c comma (functor.from_punit PUnit) F :=\n  equivalence.mk (to_comma F) (from_comma F)\n    (nat_iso.of_components (fun (X : functor.elements F) => eq_to_iso sorry) sorry)\n    (nat_iso.of_components\n      (fun (X : comma (functor.from_punit PUnit) F) => iso.mk comma_morphism.mk comma_morphism.mk)\n      sorry)\n\n@[simp] theorem comma_equivalence_functor {C : Type u} [category C] (F : C \u2964 Type w) :\n    equivalence.functor (comma_equivalence F) = to_comma F :=\n  rfl\n\n@[simp] theorem comma_equivalence_inverse {C : Type u} [category C] (F : C \u2964 Type w) :\n    equivalence.inverse (comma_equivalence F) = from_comma F :=\n  rfl\n\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/category_theory/elements_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.5, "lm_q2_score": 0.11757213354550883, "lm_q1q2_score": 0.058786066772754414}}
{"text": "/-\nCopyright (c) 2020 Dany Fabian. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Dany Fabian\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.split_ifs\nimport Mathlib.PostPort\n\nnamespace Mathlib\n\n/-!\n  # Unfold cases tactic\n\n  In Lean, pattern matching expressions are not atomic parts of the syntax, but\n  rather they are compiled down into simpler terms that are later checked by the kernel.\n\n  This allows Lean to have a minimalistic kernel but can occasionally lead an explosion\n  of cases that need to be considered. What looks like one case in the `match` expression\n  can in fact be compiled into many different cases that all need to proved by case analysis.\n\n  This tactic automates the process by allowing us to write down an equation `f x = y`\n  where we know that `f x = y` is provably true, but does not hold definitionally. In that\n  case the `unfold_cases` tactic will continue unfolding `f` and introducing `cases` where\n  necessary until the left hand side becomes definitionally equal to the right hand side.\n\n  Consider a definition as follows:\n\n  ```lean\n  def myand : bool \u2192 bool \u2192 bool\n  | ff _ := ff\n  | _ ff := ff\n  | _ _ := tt\n  ```\n\n  The equation compiler generates 4 equation lemmas for us:\n  ```lean\n  myand ff ff = ff\n  myand ff tt = ff\n  myand tt ff = ff\n  myand tt tt = tt\n  ```\n\n  This is not in line with what one might expect looking at the definition.\n  Whilst it is provably true, that `\u2200 x, myand ff x = ff` and `\u2200 x, myand x ff = ff`,\n  we do not get these stronger lemmas from the compiler for free but must in fact\n  prove them using `cases` or some other local reasoning.\n\n  In other words, the following does not constitute a proof that lean accepts.\n  ```lean\n  example : \u2200 x, myand ff x = ff :=\n  begin\n    intros, refl\n  end\n  ```\n\n  However, you can use `unfold_cases { refl }` to prove `\u2200 x, myand ff x = ff` and\n  `\u2200 x, myand x ff = ff`. For definitions with many cases, the savings can be very\n  significant.\n\n  The term that gets generated for the above definition looks like this:\n  ```lean\n  \u03bb (a a_1 : bool),\n  a.cases_on\n    (a_1.cases_on (id_rhs bool ff) (id_rhs bool ff))\n    (a_1.cases_on (id_rhs bool ff) (id_rhs bool tt))\n  ```\n\n  When the tactic tries to prove the goal `\u2200 x, myand ff x = ff`, it starts by `intros`,\n  followed by unfolding the definition:\n  ```lean\n  \u22a2 ff.cases_on\n    (x.cases_on (id_rhs bool ff) (id_rhs bool ff))\n    (x.cases_on (id_rhs bool ff) (id_rhs bool tt)) = ff\n  ```\n\n  At this point, it can make progress using `dsimp`. But then it gets stuck:\n  ```lean\n  \u22a2 bool.rec (id_rhs bool ff) (id_rhs bool ff) x = ff\n  ```\n\n  Next, it can introduce a case split on `x`. At this point, it has to prove two\n  goals:\n  ```lean\n  \u22a2 bool.rec (id_rhs bool ff) (id_rhs bool ff) ff = ff\n  \u22a2 bool.rec (id_rhs bool ff) (id_rhs bool ff) tt = ff\n  ```\n\n  Now, however, both goals can be discharged using `refl`.\n-/\n\nnamespace tactic\n\n\nnamespace unfold_cases\n\n\n/--\n  Given an equation `f x = y`, this tactic tries to infer an expression that can be\n  used to do distinction by cases on to make progress.\n\n  Pre-condition: assumes that the outer-most application cannot be beta-reduced\n  (e.g. `whnf` or `dsimp`).\n-/\n/--\n  Tries to finish the current goal using the `inner` tactic. If the tactic\n  fails, it tries to find an expression on which to do a distinction by\n  cases and calls itself recursively.\n\n  The order of operations is significant. Because the unfolding can potentially\n  be infinite, it is important to apply the `inner` tactic at every step.\n\n  Notice, that if the `inner` tactic succeeds, the recursive unfolding is stopped.\n-/\n/--\n  Given a target of the form `\u22a2 f x\u2081 ... x\u2099 = y`, unfolds `f` using a delta reduction.\n-/\nend unfold_cases\n\n\nnamespace interactive\n\n\n/--\n  This tactic unfolds the definition of a function or `match` expression.\n  Then it recursively introduces a distinction by cases. The decision what expression\n  to do the distinction on is driven by the pattern matching expression.\n\n  A typical use case is using `unfold_cases { refl }` to collapse cases that need to be\n  considered in a pattern matching.\n\n  ```lean\n  have h : foo x = y, by unfold_cases { refl },\n  rw h,\n  ```\n\n  The tactic expects a goal in the form of an equation, possibly universally quantified.\n\n  We can prove a theorem, even if the various case do not directly correspond to the\n  function definition. Here is an example application of the tactic:\n\n  ```lean\n  def foo : \u2115 \u2192 \u2115 \u2192 \u2115\n  | 0     0 := 17\n  | (n+2) 17 := 17\n  | 1     0 := 23\n  | 0     (n+18) := 15\n  | 0     17 := 17\n  | 1     17 := 17\n  | _     (n+18) := 27\n  | _     _ := 15\n\n  example : \u2200 x, foo x 17 = 17 :=\n  begin\n    unfold_cases { refl },\n  end\n  ```\n\n  The compiler generates 57 cases for `foo`. However, when we look at the definition, we see\n  that whenever the function is applied to `17` in the second argument, it returns `17`.\n\n  Proving this property consists of merely considering all the cases, eliminating invalid ones\n  and applying `refl` on the ones which remain.\n\n  Further examples can be found in `test/unfold_cases.lean`.\n-/\n", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/tactic/unfold_cases.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.38121956625614994, "lm_q2_score": 0.15405757045939122, "lm_q1q2_score": 0.05872976018900538}}
{"text": "/-\nCopyright (c) 2018 Mario Carneiro. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro\n\n! This file was ported from Lean 3 source module data.option.defs\n! leanprover-community/mathlib commit c4658a649d216f57e99621708b09dcb3dcccbd23\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathlib.Init.Algebra.Classes\n\n/-!\n# Extra definitions on `Option`\n\nThis file defines more operations involving `Option \u03b1`. Lemmas about them are located in other\nfiles under `Mathlib.Data.Option`.\nOther basic operations on `Option` are defined in the core library.\n-/\n\nnamespace Option\n\n#align option.lift_or_get Option.liftOrGet\n\n/-- Lifts a relation `\u03b1 \u2192 \u03b2 \u2192 Prop` to a relation `Option \u03b1 \u2192 Option \u03b2 \u2192 Prop` by just adding\n`none ~ none`. -/\ninductive rel (r : \u03b1 \u2192 \u03b2 \u2192 Prop) : Option \u03b1 \u2192 Option \u03b2 \u2192 Prop\n| /-- If `a ~ b`, then `some a ~ some b` -/\n  some {a b} : r a b \u2192 rel r (some a) (some b)\n| /-- `none ~ none` -/\n  none : rel r none none\n#align option.rel Option.rel\n\n/-- Traverse an object of `Option \u03b1` with a function `f : \u03b1 \u2192 F \u03b2` for an applicative `F`. -/\nprotected def traverse.{u, v} {F : Type u \u2192 Type v} [Applicative F] {\u03b1 \u03b2 : Type _} (f : \u03b1 \u2192 F \u03b2) :\n  Option \u03b1 \u2192 F (Option \u03b2)\n| none => pure none\n| some x => some <$> f x\n#align option.traverse Option.traverse\n\n/-- If you maybe have a monadic computation in a `[Monad m]` which produces a term of type `\u03b1`,\nthen there is a naturally associated way to always perform a computation in `m` which maybe\nproduces a result. -/\ndef maybe.{u, v} {m : Type u \u2192 Type v} [Monad m] {\u03b1 : Type u} : Option (m \u03b1) \u2192 m (Option \u03b1)\n| none => pure none\n| some fn => some <$> fn\n#align option.maybe Option.maybe\n\n#align option.mmap Option.mapM\n#align option.melim Option.elimM\n\n@[deprecated getDM]\nprotected def getDM' [Monad m] (x : m (Option \u03b1)) (y : m \u03b1) : m \u03b1 := do\n  (\u2190 x).getDM y\n#align option.mget_or_else Option.getDM'\n\nvariable {\u03b1 : Type _} {\u03b2 : Type _}\n\n-- Porting note: Would need to add the attribute directly in `Init.Prelude`.\n-- attribute [inline] Option.isSome Option.isNone\n\n/-- An elimination principle for `Option`. It is a nondependent version of `Option.rec`. -/\n@[simp]\nprotected def elim' (b : \u03b2) (f : \u03b1 \u2192 \u03b2) : Option \u03b1 \u2192 \u03b2\n  | some a => f a\n  | none => b\n\n#align option.elim Option.elim'\n\ntheorem mem_some_iff {\u03b1 : Type _} {a b : \u03b1} : a \u2208 some b \u2194 b = a := by simp\n#align option.mem_some_iff Option.mem_some_iff\n\n/-- `o = none` is decidable even if the wrapped type does not have decidable equality.\nThis is not an instance because it is not definitionally equal to `Option.decidableEq`.\nTry to use `o.isNone` or `o.isSome` instead.\n-/\n@[inline]\ndef decidableEqNone {o : Option \u03b1} : Decidable (o = none) :=\n  decidable_of_decidable_of_iff isNone_iff_eq_none\n#align option.decidable_eq_none Option.decidableEqNone\n\ninstance decidableForallMem {p : \u03b1 \u2192 Prop} [DecidablePred p] :\n    \u2200 o : Option \u03b1, Decidable (\u2200 a \u2208 o, p a)\n  | none => isTrue (by simp [false_imp_iff])\n  | some a =>\n      if h : p a then isTrue fun o e \u21a6 some_inj.1 e \u25b8 h\n      else isFalse <| mt (fun H \u21a6 H _ rfl) h\n\ninstance decidableExistsMem {p : \u03b1 \u2192 Prop} [DecidablePred p] :\n    \u2200 o : Option \u03b1, Decidable (\u2203 a \u2208 o, p a)\n  | none => isFalse fun \u27e8a, \u27e8h, _\u27e9\u27e9 \u21a6 by cases h\n  | some a => if h : p a then isTrue <| \u27e8_, rfl, h\u27e9 else isFalse fun \u27e8_, \u27e8rfl, hn\u27e9\u27e9 \u21a6 h hn\n\n/-- Inhabited `get` function. Returns `a` if the input is `some a`, otherwise returns `default`. -/\n@[reducible]\ndef iget [Inhabited \u03b1] : Option \u03b1 \u2192 \u03b1\n  | some x => x\n  | none => default\n#align option.iget Option.iget\n\ntheorem iget_some [Inhabited \u03b1] {a : \u03b1} : (some a).iget = a :=\n  rfl\n#align option.iget_some Option.iget_some\n\n@[simp]\ntheorem mem_toList {a : \u03b1} {o : Option \u03b1} : a \u2208 toList o \u2194 a \u2208 o := by\n  cases o <;> simp [toList, eq_comm]\n\n#align option.mem_to_list Option.mem_toList\n\ninstance liftOrGet_isCommutative (f : \u03b1 \u2192 \u03b1 \u2192 \u03b1) [IsCommutative \u03b1 f] :\n    IsCommutative (Option \u03b1) (liftOrGet f) :=\n  \u27e8fun a b \u21a6 by cases a <;> cases b <;> simp [liftOrGet, IsCommutative.comm]\u27e9\n\ninstance liftOrGet_isAssociative (f : \u03b1 \u2192 \u03b1 \u2192 \u03b1) [IsAssociative \u03b1 f] :\n    IsAssociative (Option \u03b1) (liftOrGet f) :=\n  \u27e8fun a b c \u21a6 by cases a <;> cases b <;> cases c <;> simp [liftOrGet, IsAssociative.assoc]\u27e9\n\ninstance liftOrGet_isIdempotent (f : \u03b1 \u2192 \u03b1 \u2192 \u03b1) [IsIdempotent \u03b1 f] :\n    IsIdempotent (Option \u03b1) (liftOrGet f) :=\n  \u27e8fun a \u21a6 by cases a <;> simp [liftOrGet, IsIdempotent.idempotent]\u27e9\n\ninstance liftOrGet_isLeftId (f : \u03b1 \u2192 \u03b1 \u2192 \u03b1) : IsLeftId (Option \u03b1) (liftOrGet f) none :=\n  \u27e8fun a \u21a6 by cases a <;> simp [liftOrGet]\u27e9\n\ninstance liftOrGet_isRightId (f : \u03b1 \u2192 \u03b1 \u2192 \u03b1) : IsRightId (Option \u03b1) (liftOrGet f) none :=\n  \u27e8fun a \u21a6 by cases a <;> simp [liftOrGet]\u27e9\n\n#align option.lift_or_get_comm Option.liftOrGet_isCommutative\n#align option.lift_or_get_assoc Option.liftOrGet_isAssociative\n#align option.lift_or_get_idem Option.liftOrGet_isIdempotent\n#align option.lift_or_get_is_left_id Option.liftOrGet_isLeftId\n#align option.lift_or_get_is_right_id Option.liftOrGet_isRightId\n", "meta": {"author": "leanprover-community", "repo": "mathlib4", "sha": "b9a0a30342ca06e9817e22dbe46e75fc7f435500", "save_path": "github-repos/lean/leanprover-community-mathlib4", "path": "github-repos/lean/leanprover-community-mathlib4/mathlib4-b9a0a30342ca06e9817e22dbe46e75fc7f435500/Mathlib/Data/Option/Defs.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.44167300566462564, "lm_q2_score": 0.13296424019782926, "lm_q1q2_score": 0.05872671561408849}}
{"text": "/-\nCopyright (c) 2021 R\u00e9my Degenne. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: R\u00e9my Degenne\n-/\nimport measure_theory.measure.measure_space_def\nimport tactic.auto_cases\nimport tactic.tidy\nimport tactic.with_local_reducibility\n/-!\n# Tactics for measure theory\n\nCurrently we have one domain-specific tactic for measure theory: `measurability`.\n\nThis tactic is to a large extent a copy of the `continuity` tactic by Reid Barton.\n-/\n\n/-!\n### `measurability` tactic\n\nAutomatically solve goals of the form `measurable f`, `ae_measurable f \u03bc` and `measurable_set s`.\n\nMark lemmas with `@[measurability]` to add them to the set of lemmas\nused by `measurability`. Note: `to_additive` doesn't know yet how to\ncopy the attribute to the additive version.\n-/\n\n/-- User attribute used to mark tactics used by `measurability`. -/\n@[user_attribute]\nmeta def measurability : user_attribute :=\n{ name := `measurability,\n  descr := \"lemmas usable to prove (ae)-measurability\" }\n\n/- Mark some measurability lemmas already defined in `measure_theory.measurable_space_def` and\n`measure_theory.measure_space_def` -/\nattribute [measurability]\n  measurable_id\n  measurable_id'\n  ae_measurable_id\n  ae_measurable_id'\n  measurable_const\n  ae_measurable_const\n  ae_measurable.measurable_mk\n  measurable_set.empty\n  measurable_set.univ\n  measurable_set.compl\n  subsingleton.measurable_set\n  measurable_set.Union\n  measurable_set.Inter\n  measurable_set.Union_Prop\n  measurable_set.Inter_Prop\n  measurable_set.union\n  measurable_set.inter\n  measurable_set.diff\n  measurable_set.symm_diff\n  measurable_set.ite\n  measurable_set.cond\n  measurable_set.disjointed\n  measurable_set.const\n  measurable_set.insert\n  measurable_set_eq\n  set.finite.measurable_set\n  finset.measurable_set\n  set.countable.measurable_set\n  measurable_space.measurable_set_top\n\nnamespace tactic\n\n/--\nTactic to apply `measurable.comp` when appropriate.\n\nApplying `measurable.comp` is not always a good idea, so we have some\nextra logic here to try to avoid bad cases.\n\n* If the function we're trying to prove measurable is actually\n  constant, and that constant is a function application `f z`, then\n  measurable.comp would produce new goals `measurable f`, `measurable\n  (\u03bb _, z)`, which is silly. We avoid this by failing if we could\n  apply `measurable_const`.\n\n* measurable.comp will always succeed on `measurable (\u03bb x, f x)` and\n  produce new goals `measurable (\u03bb x, x)`, `measurable f`. We detect\n  this by failing if a new goal can be closed by applying\n  measurable_id.\n-/\nmeta def apply_measurable.comp : tactic unit :=\n`[fail_if_success { exact measurable_const };\n  refine measurable.comp _ _;\n  fail_if_success { exact measurable_id }]\n\n/--\nTactic to apply `measurable.comp_ae_measurable` when appropriate.\n\nApplying `measurable.comp_ae_measurable` is not always a good idea, so we have some\nextra logic here to try to avoid bad cases.\n\n* If the function we're trying to prove measurable is actually\n  constant, and that constant is a function application `f z`, then\n  `measurable.comp_ae_measurable` would produce new goals `measurable f`, `ae_measurable\n  (\u03bb _, z) \u03bc`, which is silly. We avoid this by failing if we could\n  apply `ae_measurable_const`.\n\n* `measurable.comp_ae_measurable` will always succeed on `ae_measurable (\u03bb x, f x) \u03bc` and\n  can produce new goals (`measurable (\u03bb x, x)`, `ae_measurable f \u03bc`) or\n  (`measurable f`, `ae_measurable (\u03bb x, x) \u03bc`). We detect those by failing if a new goal can be\n  closed by applying `measurable_id` or `ae_measurable_id`.\n-/\nmeta def apply_measurable.comp_ae_measurable : tactic unit :=\n`[fail_if_success { exact ae_measurable_const };\n  refine measurable.comp_ae_measurable _ _;\n  fail_if_success { exact measurable_id };\n  fail_if_success { exact ae_measurable_id }]\n\n/--\nWe don't want the intro1 tactic to apply to a goal of the form `measurable f`, `ae_measurable f \u03bc`\nor `measurable_set s`. This tactic tests the target to see if it matches that form.\n -/\nmeta def goal_is_not_measurable : tactic unit :=\ndo t \u2190 tactic.target,\n  match t with\n  | `(measurable %%l) := failed\n  | `(ae_measurable %%l %%r) := failed\n  | `(measurable_set %%l) := failed\n  | _ := skip\n  end\n\n/-- List of tactics used by `measurability` internally. -/\nmeta def measurability_tactics (md : transparency := semireducible) : list (tactic string) :=\n[\n  propositional_goal >> apply_assumption\n                        >> pure \"apply_assumption\",\n  goal_is_not_measurable >> intro1\n                        >>= \u03bb ns, pure (\"intro \" ++ ns.to_string),\n  apply_rules [``(measurability)] 50 { md := md }\n                        >> pure \"apply_rules measurability\",\n  apply_measurable.comp >> pure \"refine measurable.comp _ _\",\n  apply_measurable.comp_ae_measurable\n                        >> pure \"refine measurable.comp_ae_measurable _ _\",\n  `[ refine measurable.ae_measurable _ ]\n                        >> pure \"refine measurable.ae_measurable _\"\n]\n\nnamespace interactive\nsetup_tactic_parser\n\n/--\nSolve goals of the form `measurable f`, `ae_measurable f \u03bc` or `measurable_set s`.\n`measurability?` reports back the proof term it found.\n-/\nmeta def measurability\n  (bang : parse $ optional (tk \"!\")) (trace : parse $ optional (tk \"?\")) (cfg : tidy.cfg := {}) :\n  tactic unit :=\nlet md                 := if bang.is_some then semireducible else reducible,\n    measurability_core := tactic.tidy { tactics := measurability_tactics md, ..cfg },\n    trace_fn           := if trace.is_some then show_term else id in\ntrace_fn measurability_core\n\n/-- Version of `measurability` for use with auto_param. -/\nmeta def measurability' : tactic unit := measurability none none {}\n\n/--\n`measurability` solves goals of the form `measurable f`, `ae_measurable f \u03bc` or `measurable_set s`\nby applying lemmas tagged with the `measurability` user attribute.\n\nYou can also use `measurability!`, which applies lemmas with `{ md := semireducible }`.\nThe default behaviour is more conservative, and only unfolds `reducible` definitions\nwhen attempting to match lemmas with the goal.\n\n`measurability?` reports back the proof term it found.\n-/\nadd_tactic_doc\n{ name := \"measurability / measurability'\",\n  category := doc_category.tactic,\n  decl_names := [`tactic.interactive.measurability, `tactic.interactive.measurability'],\n  tags := [\"lemma application\"] }\n\nend interactive\n\nend tactic\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/measure_theory/tactic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.5, "lm_q2_score": 0.11596071214366648, "lm_q1q2_score": 0.05798035607183324}}
{"text": "/-\nCopyright (c) 2019 Robert Y. Lewis. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro, Simon Hudon, Scott Morrison, Keeley Hoek, Robert Y. Lewis,\nFloris van Doorn, E.W.Ayers, Arthur Paulino\n-/\nimport Lean\nimport Std.Lean.Expr\nimport Std.Data.List.Basic\n\n/-!\n# Additional operations on Expr and related types\n\nThis file defines basic operations on the types expr, name, declaration, level, environment.\n\nThis file is mostly for non-tactics.\n-/\n\nnamespace Lean\n\nnamespace BinderInfo\n\n/-! ### Declarations about `BinderInfo` -/\n\n/-- The brackets corresponding to a given `BinderInfo`. -/\ndef brackets : BinderInfo \u2192 String \u00d7 String\n| BinderInfo.implicit => (\"{\", \"}\")\n| BinderInfo.strictImplicit => (\"{{\", \"}}\")\n| BinderInfo.instImplicit => (\"[\", \"]\")\n| _ => (\"(\", \")\")\n\nend BinderInfo\n\nnamespace Name\n\n/-! ### Declarations about `name` -/\n\n/-- Find the largest prefix `n` of a `Name` such that `f n != none`, then replace this prefix\nwith the value of `f n`. -/\ndef mapPrefix (f : Name \u2192 Option Name) (n : Name) : Name := Id.run do\n  if let some n' := f n then return n'\n  match n with\n  | anonymous => anonymous\n  | str n' s => mkStr (mapPrefix f n') s\n  | num n' i => mkNum (mapPrefix f n') i\n\n/-- Build a name from components. For example ``from_components [`foo, `bar]`` becomes\n  ``` `foo.bar```.\n  It is the inverse of `Name.components` on list of names that have single components. -/\ndef fromComponents : List Name \u2192 Name := go .anonymous where\n  /-- Auxiliary for `Name.fromComponents` -/\n  go : Name \u2192 List Name \u2192 Name\n  | n, []        => n\n  | n, s :: rest => go (s.updatePrefix n) rest\n\n/-- Update the last component of a name. -/\ndef updateLast (f : String \u2192 String) : Name \u2192 Name\n| .str n s => .str n (f s)\n| n        => n\n\n/-- Get the last field of a name as a string.\nDoesn't raise an error when the last component is a numeric field. -/\ndef getString : Name \u2192 String\n| .str _ s => s\n| .num _ n => toString n\n| .anonymous => \"\"\n\n/-- `nm.splitAt n` splits a name `nm` in two parts, such that the *second* part has depth `n`, i.e.\n  `(nm.splitAt n).2.getNumParts = n` (assuming `nm.getNumParts \u2265 n`).\n  Example: ``splitAt `foo.bar.baz.back.bat 1 = (`foo.bar.baz.back, `bat)``. -/\ndef splitAt (nm : Name) (n : Nat) : Name \u00d7 Name :=\n  let (nm2, nm1) := (nm.componentsRev.splitAt n)\n  (.fromComponents <| nm1.reverse, .fromComponents <| nm2.reverse)\n\n/-- `isPrefixOf? pre nm` returns `some post` if `nm = pre ++ post`.\n  Note that this includes the case where `nm` has multiple more namespaces.\n  If `pre` is not a prefix of `nm`, it returns `none`. -/\ndef isPrefixOf? (pre nm : Name) : Option Name :=\n  if pre == nm then\n    some anonymous\n  else match nm with\n  | anonymous => none\n  | num p' a => (isPrefixOf? pre p').map (\u00b7.num a)\n  | str p' s => (isPrefixOf? pre p').map (\u00b7.str s)\n\n/-- Lean 4 makes declarations which are technically not internal\n(that is, head string does not start with `_`) but which sometimes should\nbe treated as such. For example, the `to_additive` attribute needs to\ntransform `proof_1` constants generated by `Lean.Meta.mkAuxDefinitionFor`.\nThis might be better fixed in core, but until then, this method can act\nas a polyfill. This method only looks at the name to decide whether it is probably internal.\nNote: this declaration also occurs as `shouldIgnore` in the Lean 4 file `test/lean/run/printDecls`.\n-/\ndef isInternal' (declName : Name) : Bool :=\n  declName.isInternal ||\n  match declName with\n  | .str _ s => \"match_\".isPrefixOf s || \"proof_\".isPrefixOf s || \"eq_\".isPrefixOf s\n  | _        => true\n\nend Name\n\n\nnamespace ConstantInfo\n\n/-- Checks whether this `ConstantInfo` is a definition, -/\ndef isDef : ConstantInfo \u2192 Bool\n  | defnInfo _ => true\n  | _          => false\n\n/-- Checks whether this `ConstantInfo` is a theorem, -/\ndef isThm : ConstantInfo \u2192 Bool\n  | thmInfo _ => true\n  | _          => false\n\n/-- Update `ConstantVal` (the data common to all constructors of `ConstantInfo`)\nin a `ConstantInfo`. -/\ndef updateConstantVal : ConstantInfo \u2192 ConstantVal \u2192 ConstantInfo\n  | defnInfo   info, v => defnInfo   {info with toConstantVal := v}\n  | axiomInfo  info, v => axiomInfo  {info with toConstantVal := v}\n  | thmInfo    info, v => thmInfo    {info with toConstantVal := v}\n  | opaqueInfo info, v => opaqueInfo {info with toConstantVal := v}\n  | quotInfo   info, v => quotInfo   {info with toConstantVal := v}\n  | inductInfo info, v => inductInfo {info with toConstantVal := v}\n  | ctorInfo   info, v => ctorInfo   {info with toConstantVal := v}\n  | recInfo    info, v => recInfo    {info with toConstantVal := v}\n\n/-- Update the name of a `ConstantInfo`. -/\ndef updateName (c : ConstantInfo) (name : Name) : ConstantInfo :=\n  c.updateConstantVal {c.toConstantVal with name}\n\n/-- Update the type of a `ConstantInfo`. -/\ndef updateType (c : ConstantInfo) (type : Expr) : ConstantInfo :=\n  c.updateConstantVal {c.toConstantVal with type}\n\n/-- Update the level parameters of a `ConstantInfo`. -/\ndef updateLevelParams (c : ConstantInfo) (levelParams : List Name) :\n  ConstantInfo :=\n  c.updateConstantVal {c.toConstantVal with levelParams}\n\n/-- Update the value of a `ConstantInfo`, if it has one. -/\ndef updateValue : ConstantInfo \u2192 Expr \u2192 ConstantInfo\n  | defnInfo   info, v => defnInfo   {info with value := v}\n  | thmInfo    info, v => thmInfo    {info with value := v}\n  | opaqueInfo info, v => opaqueInfo {info with value := v}\n  | d, _ => d\n\n/-- Turn a `ConstantInfo` into a declaration. -/\ndef toDeclaration! : ConstantInfo \u2192 Declaration\n  | defnInfo   info => Declaration.defnDecl info\n  | thmInfo    info => Declaration.thmDecl     info\n  | axiomInfo  info => Declaration.axiomDecl   info\n  | opaqueInfo info => Declaration.opaqueDecl  info\n  | quotInfo   _ => panic! \"toDeclaration for quotInfo not implemented\"\n  | inductInfo _ => panic! \"toDeclaration for inductInfo not implemented\"\n  | ctorInfo   _ => panic! \"toDeclaration for ctorInfo not implemented\"\n  | recInfo    _ => panic! \"toDeclaration for recInfo not implemented\"\n\nend ConstantInfo\n\nopen Meta\n\n/-- Same as `mkConst`, but with fresh level metavariables. -/\ndef mkConst' (constName : Name) : MetaM Expr := do\n  return mkConst constName (\u2190 (\u2190 getConstInfo constName).levelParams.mapM fun _ => mkFreshLevelMVar)\n\nnamespace Expr\n\n/-! ### Declarations about `Expr` -/\n\n/-- If the expression is a constant, return that name. Otherwise return `Name.anonymous`. -/\ndef constName (e : Expr) : Name :=\n  e.constName?.getD Name.anonymous\n\ndef bvarIdx? : Expr \u2192 Option Nat\n  | bvar idx => some idx\n  | _        => none\n\n/-- Return the function (name) and arguments of an application. -/\ndef getAppFnArgs (e : Expr) : Name \u00d7 Array Expr :=\n  withApp e \u03bb e a => (e.constName, a)\n\n/-- Turn an expression that is a natural number literal into a natural number. -/\ndef natLit! : Expr \u2192 Nat\n  | lit (Literal.natVal v) => v\n  | _                      => panic! \"nat literal expected\"\n\n/-- If an `Expr` has form `.fvar n`, then returns `some n`, otherwise `none`. -/\ndef fvarId? : Expr \u2192 Option FVarId\n  | .fvar n => n\n  | _ => none\n\n/-- `isConstantApplication e` checks whether `e` is syntactically an application of the form\n  `(fun x\u2081 \u22ef x\u2099 => H) y\u2081 \u22ef y\u2099` where `H` does not contain the variable `x\u2099`. In other words,\n  it does a syntactic check that the expression does not depend on `y\u2099`. -/\ndef isConstantApplication (e : Expr) :=\ne.isApp && aux e.getAppNumArgs'.pred e.getAppFn' e.getAppNumArgs'\nwhere\n  /-- `aux depth e n` checks whether the body of the `n`-th lambda of `e` has loose bvar\n    `depth - 1`. -/\n  aux (depth : Nat) : Expr \u2192 Nat \u2192 Bool\n  | .lam _ _ b _, n + 1  => aux depth b n\n  | e, 0  => !e.hasLooseBVar (depth - 1)\n  | _, _ => false\n\nopen Meta\n\n/-- Check that an expression contains no metavariables (after instantiation). -/\n-- There is a `TacticM` level version of this, but it's useful to have in `MetaM`.\ndef ensureHasNoMVars (e : Expr) : MetaM Unit := do\n  let e \u2190 instantiateMVars e\n  if e.hasExprMVar then\n    throwError \"tactic failed, resulting expression contains metavariables{indentExpr e}\"\n\n/-- Construct the term of type `\u03b1` for a given natural number\n(doing typeclass search for the `OfNat` instance required). -/\ndef ofNat (\u03b1 : Expr) (n : Nat) : MetaM Expr := do\n  mkAppOptM ``OfNat.ofNat #[\u03b1, mkRawNatLit n, none]\n\n/-- Construct the term of type `\u03b1` for a given integer\n(doing typeclass search for the `OfNat` and `Neg` instances required). -/\ndef ofInt (\u03b1 : Expr) : Int \u2192 MetaM Expr\n| Int.ofNat n => Expr.ofNat \u03b1 n\n| Int.negSucc n => do mkAppM ``Neg.neg #[\u2190 Expr.ofNat \u03b1 (n+1)]\n\n/--\n  Return `some n` if `e` is one of the following\n  - A nat literal (numeral)\n  - `Nat.zero`\n  - `Nat.succ x` where `isNumeral x`\n  - `OfNat.ofNat _ x _` where `isNumeral x` -/\npartial def numeral? (e : Expr) : Option Nat :=\n  if let some n := e.natLit? then n\n  else\n    let f := e.getAppFn\n    if !f.isConst then none\n    else\n      let fName := f.constName!\n      if fName == ``Nat.succ && e.getAppNumArgs == 1 then (numeral? e.appArg!).map Nat.succ\n      else if fName == ``OfNat.ofNat && e.getAppNumArgs == 3 then numeral? (e.getArg! 1)\n      else if fName == ``Nat.zero && e.getAppNumArgs == 0 then some 0\n      else none\n\n/-- Test if an expression is either `Nat.zero`, or `OfNat.ofNat 0`. -/\ndef zero? (e : Expr) : Bool :=\n  match e.numeral? with\n  | some 0 => true\n  | _ => false\n\ndef modifyAppArgM [Functor M] [Pure M] (modifier : Expr \u2192 M Expr) : Expr \u2192 M Expr\n  | app f a => mkApp f <$> modifier a\n  | e => pure e\n\ndef modifyAppArg (modifier : Expr \u2192 Expr) : Expr \u2192 Expr :=\n  modifyAppArgM (M := Id) modifier\n\ndef modifyRevArg (modifier : Expr \u2192 Expr): Nat \u2192 Expr  \u2192 Expr\n  | 0 => modifyAppArg modifier\n  | (i+1) => modifyAppArg (modifyRevArg modifier i)\n\n/-- Given `f a\u2080 a\u2081 ... a\u2099\u208b\u2081`, runs `modifier` on the `i`th argument or\nreturns the original expression if out of bounds. -/\ndef modifyArg (modifier : Expr \u2192 Expr) (e : Expr) (i : Nat) (n := e.getAppNumArgs) : Expr :=\n  modifyRevArg modifier (n - i - 1) e\n\ndef getRevArg? : Expr \u2192 Nat \u2192 Option Expr\n  | app _ a, 0   => a\n  | app f _, i+1 => getRevArg! f i\n  | _,       _   => none\n\n/-- Given `f a\u2080 a\u2081 ... a\u2099\u208b\u2081`, returns the `i`th argument or none if out of bounds. -/\ndef getArg? (e : Expr) (i : Nat) (n := e.getAppNumArgs): Option Expr :=\n  getRevArg? e (n - i - 1)\n\n/-- Given `f a\u2080 a\u2081 ... a\u2099\u208b\u2081`, runs `modifier` on the `i`th argument.\nAn argument `n` may be provided which says how many arguments we are expecting `e` to have. -/\ndef modifyArgM [Monad M] (modifier : Expr \u2192 M Expr) (e : Expr) (i : Nat) (n := e.getAppNumArgs) :\n    M Expr := do\n  let some a := getArg? e i | return e\n  let a \u2190 modifier a\n  return modifyArg (fun _ \u21a6 a) e i n\n\n/-- Traverses an expression `e` and renames bound variables named `old` to `new`. -/\ndef renameBVar (e : Expr) (old new : Name) : Expr :=\n  match e with\n  | app fn arg => app (fn.renameBVar old new) (arg.renameBVar old new)\n  | lam n ty bd bi =>\n    lam (if n == old then new else n) (ty.renameBVar old new) (bd.renameBVar old new) bi\n  | forallE n ty bd bi =>\n    forallE (if n == old then new else n) (ty.renameBVar old new) (bd.renameBVar old new) bi\n  | e => e\n\nopen Lean.Meta in\n/-- `getBinderName e` returns `some n` if `e` is an expression of the form `\u2200 n, ...`\nand `none` otherwise. -/\ndef getBinderName (e : Expr) : MetaM (Option Name) := do\n  match \u2190 withReducible (whnf e) with\n  | .forallE (binderName := n) .. | .lam (binderName := n) .. => pure (some n)\n  | _ => pure none\n\nopen Lean.Elab.Term\n/-- Annotates a `binderIdent` with the binder information from an `fvar`. -/\ndef addLocalVarInfoForBinderIdent (fvar : Expr) : TSyntax ``binderIdent \u2192 TermElabM Unit\n| `(binderIdent| $n:ident) => Elab.Term.addLocalVarInfo n fvar\n| tk => Elab.Term.addLocalVarInfo (Unhygienic.run `(_%$tk)) fvar\n\n/-- If `e` has a structure as type with field `fieldName`, `mkDirectProjection e fieldName` creates\nthe projection expression `e.fieldName` -/\ndef mkDirectProjection (e : Expr) (fieldName : Name) : MetaM Expr := do\n  let type \u2190 whnf (\u2190 inferType e)\n  let .const structName us := type.getAppFn | throwError \"{e} doesn't have a structure as type\"\n  let some projName := getProjFnForField? (\u2190 getEnv) structName fieldName |\n    throwError \"{structName} doesn't have field {fieldName}\"\n  return mkAppN (.const projName us) (type.getAppArgs.push e)\n\n/-- If `e` has a structure as type with field `fieldName` (either directly or in a parent\nstructure), `mkProjection e fieldName` creates the projection expression `e.fieldName` -/\ndef mkProjection (e : Expr) (fieldName : Name) : MetaM Expr := do\n  let .const structName _ := (\u2190 whnf (\u2190inferType e)).getAppFn |\n    throwError \"{e} doesn't have a structure as type\"\n  let some baseStruct := findField? (\u2190 getEnv) structName fieldName |\n    throwError \"No parent of {structName} has field {fieldName}\"\n  let mut e := e\n  for projName in (getPathToBaseStructure? (\u2190 getEnv) baseStruct structName).get! do\n    let type \u2190 whnf (\u2190 inferType e)\n    let .const _structName us := type.getAppFn | throwError \"{e} doesn't have a structure as type\"\n    e := mkAppN (.const projName us) (type.getAppArgs.push e)\n  mkDirectProjection e fieldName\n\nend Expr\n\n/-- Get the projections that are projections to parent structures. Similar to `getParentStructures`,\n  except that this returns the (last component of the) projection names instead of the parent names.\n-/\ndef getFieldsToParents (env : Environment) (structName : Name) : Array Name :=\n  getStructureFields env structName |>.filter fun fieldName =>\n    isSubobjectField? env structName fieldName |>.isSome\n\nend Lean\n", "meta": {"author": "leanprover-community", "repo": "mathlib4", "sha": "b9a0a30342ca06e9817e22dbe46e75fc7f435500", "save_path": "github-repos/lean/leanprover-community-mathlib4", "path": "github-repos/lean/leanprover-community-mathlib4/mathlib4-b9a0a30342ca06e9817e22dbe46e75fc7f435500/Mathlib/Lean/Expr/Basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48438008427698437, "lm_q2_score": 0.1192029235866439, "lm_q1q2_score": 0.0577395221729615}}
{"text": "/-\nCopyright (c) 2018 Sean Leather. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Sean Leather, Mario Carneiro\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.data.list.sigma\nimport Mathlib.PostPort\n\nuniverses u v l w \n\nnamespace Mathlib\n\n/-!\n# Association lists\n-/\n\n/-- `alist \u03b2` is a key-value map stored as a `list` (i.e. a linked list).\n  It is a wrapper around certain `list` functions with the added constraint\n  that the list have unique keys. -/\nstructure alist {\u03b1 : Type u} (\u03b2 : \u03b1 \u2192 Type v) \nwhere\n  entries : List (sigma \u03b2)\n  nodupkeys : list.nodupkeys entries\n\n/-- Given `l : list (sigma \u03b2)`, create a term of type `alist \u03b2` by removing\nentries with duplicate keys. -/\ndef list.to_alist {\u03b1 : Type u} [DecidableEq \u03b1] {\u03b2 : \u03b1 \u2192 Type v} (l : List (sigma \u03b2)) : alist \u03b2 :=\n  alist.mk (list.erase_dupkeys l) sorry\n\nnamespace alist\n\n\ntheorem ext {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {s : alist \u03b2} {t : alist \u03b2} : entries s = entries t \u2192 s = t := sorry\n\ntheorem ext_iff {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {s : alist \u03b2} {t : alist \u03b2} : s = t \u2194 entries s = entries t :=\n  { mp := congr_arg fun {s : alist \u03b2} => entries s, mpr := ext }\n\nprotected instance decidable_eq {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] [(a : \u03b1) \u2192 DecidableEq (\u03b2 a)] : DecidableEq (alist \u03b2) :=\n  fun (xs ys : alist \u03b2) => eq.mpr sorry (list.decidable_eq (entries xs) (entries ys))\n\n/-! ### keys -/\n\n/-- The list of keys of an association list. -/\ndef keys {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (s : alist \u03b2) : List \u03b1 :=\n  list.keys (entries s)\n\ntheorem keys_nodup {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (s : alist \u03b2) : list.nodup (keys s) :=\n  nodupkeys s\n\n/-! ### mem -/\n\n/-- The predicate `a \u2208 s` means that `s` has a value associated to the key `a`. -/\nprotected instance has_mem {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : has_mem \u03b1 (alist \u03b2) :=\n  has_mem.mk fun (a : \u03b1) (s : alist \u03b2) => a \u2208 keys s\n\ntheorem mem_keys {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {a : \u03b1} {s : alist \u03b2} : a \u2208 s \u2194 a \u2208 keys s :=\n  iff.rfl\n\ntheorem mem_of_perm {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} (p : entries s\u2081 ~ entries s\u2082) : a \u2208 s\u2081 \u2194 a \u2208 s\u2082 :=\n  list.perm.mem_iff (list.perm.map sigma.fst p)\n\n/-! ### empty -/\n\n/-- The empty association list. -/\nprotected instance has_emptyc {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : has_emptyc (alist \u03b2) :=\n  has_emptyc.mk (mk [] list.nodupkeys_nil)\n\nprotected instance inhabited {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : Inhabited (alist \u03b2) :=\n  { default := \u2205 }\n\ntheorem not_mem_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) : \u00aca \u2208 \u2205 :=\n  list.not_mem_nil a\n\n@[simp] theorem empty_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : entries \u2205 = [] :=\n  rfl\n\n@[simp] theorem keys_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : keys \u2205 = [] :=\n  rfl\n\n/-! ### singleton -/\n\n/-- The singleton association list. -/\ndef singleton {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) (b : \u03b2 a) : alist \u03b2 :=\n  mk [sigma.mk a b] sorry\n\n@[simp] theorem singleton_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) (b : \u03b2 a) : entries (singleton a b) = [sigma.mk a b] :=\n  rfl\n\n@[simp] theorem keys_singleton {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) (b : \u03b2 a) : keys (singleton a b) = [a] :=\n  rfl\n\n/-! ### lookup -/\n\n/-- Look up the value associated to a key in an association list. -/\ndef lookup {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : Option (\u03b2 a) :=\n  list.lookup a (entries s)\n\n@[simp] theorem lookup_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) : lookup a \u2205 = none :=\n  rfl\n\ntheorem lookup_is_some {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s : alist \u03b2} : \u21a5(option.is_some (lookup a s)) \u2194 a \u2208 s :=\n  list.lookup_is_some\n\ntheorem lookup_eq_none {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s : alist \u03b2} : lookup a s = none \u2194 \u00aca \u2208 s :=\n  list.lookup_eq_none\n\ntheorem perm_lookup {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} (p : entries s\u2081 ~ entries s\u2082) : lookup a s\u2081 = lookup a s\u2082 :=\n  list.perm_lookup a (nodupkeys s\u2081) (nodupkeys s\u2082) p\n\nprotected instance has_mem.mem.decidable {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : Decidable (a \u2208 s) :=\n  decidable_of_iff \u21a5(option.is_some (lookup a s)) sorry\n\n/-! ### replace -/\n\n/-- Replace a key with a given value in an association list.\n  If the key is not present it does nothing. -/\ndef replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a) (s : alist \u03b2) : alist \u03b2 :=\n  mk (list.kreplace a b (entries s)) sorry\n\n@[simp] theorem keys_replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a) (s : alist \u03b2) : keys (replace a b s) = keys s :=\n  list.keys_kreplace a b (entries s)\n\n@[simp] theorem mem_replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {b : \u03b2 a} {s : alist \u03b2} : a' \u2208 replace a b s \u2194 a' \u2208 s :=\n  eq.mpr (id (Eq._oldrec (Eq.refl (a' \u2208 replace a b s \u2194 a' \u2208 s)) (propext mem_keys)))\n    (eq.mpr (id (Eq._oldrec (Eq.refl (a' \u2208 keys (replace a b s) \u2194 a' \u2208 s)) (keys_replace a b s)))\n      (eq.mpr (id (Eq._oldrec (Eq.refl (a' \u2208 keys s \u2194 a' \u2208 s)) (Eq.symm (propext mem_keys)))) (iff.refl (a' \u2208 s))))\n\ntheorem perm_replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : entries s\u2081 ~ entries s\u2082 \u2192 entries (replace a b s\u2081) ~ entries (replace a b s\u2082) :=\n  list.perm.kreplace (nodupkeys s\u2081)\n\n/-- Fold a function over the key-value pairs in the map. -/\ndef foldl {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {\u03b4 : Type w} (f : \u03b4 \u2192 (a : \u03b1) \u2192 \u03b2 a \u2192 \u03b4) (d : \u03b4) (m : alist \u03b2) : \u03b4 :=\n  list.foldl (fun (r : \u03b4) (a : sigma \u03b2) => f r (sigma.fst a) (sigma.snd a)) d (entries m)\n\n/-! ### erase -/\n\n/-- Erase a key from the map. If the key is not present, do nothing. -/\ndef erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : alist \u03b2 :=\n  mk (list.kerase a (entries s)) sorry\n\n@[simp] theorem keys_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : keys (erase a s) = list.erase (keys s) a := sorry\n\n@[simp] theorem mem_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {s : alist \u03b2} : a' \u2208 erase a s \u2194 a' \u2260 a \u2227 a' \u2208 s := sorry\n\ntheorem perm_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : entries s\u2081 ~ entries s\u2082 \u2192 entries (erase a s\u2081) ~ entries (erase a s\u2082) :=\n  list.perm.kerase (nodupkeys s\u2081)\n\n@[simp] theorem lookup_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : lookup a (erase a s) = none :=\n  list.lookup_kerase a (nodupkeys s)\n\n@[simp] theorem lookup_erase_ne {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {s : alist \u03b2} (h : a \u2260 a') : lookup a (erase a' s) = lookup a s :=\n  list.lookup_kerase_ne h\n\ntheorem erase_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (a' : \u03b1) (s : alist \u03b2) : erase a' (erase a s) = erase a (erase a' s) :=\n  ext list.kerase_kerase\n\n/-! ### insert -/\n\n/-- Insert a key-value pair into an association list and erase any existing pair\n  with the same key. -/\ndef insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a) (s : alist \u03b2) : alist \u03b2 :=\n  mk (list.kinsert a b (entries s)) sorry\n\n@[simp] theorem insert_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s : alist \u03b2} : entries (insert a b s) = sigma.mk a b :: list.kerase a (entries s) :=\n  rfl\n\ntheorem insert_entries_of_neg {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s : alist \u03b2} (h : \u00aca \u2208 s) : entries (insert a b s) = sigma.mk a b :: entries s := sorry\n\n@[simp] theorem mem_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {b' : \u03b2 a'} (s : alist \u03b2) : a \u2208 insert a' b' s \u2194 a = a' \u2228 a \u2208 s :=\n  list.mem_keys_kinsert\n\n@[simp] theorem keys_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} (s : alist \u03b2) : keys (insert a b s) = a :: list.erase (keys s) a := sorry\n\ntheorem perm_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} (p : entries s\u2081 ~ entries s\u2082) : entries (insert a b s\u2081) ~ entries (insert a b s\u2082) := sorry\n\n@[simp] theorem lookup_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} (s : alist \u03b2) : lookup a (insert a b s) = some b := sorry\n\n@[simp] theorem lookup_insert_ne {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {b' : \u03b2 a'} {s : alist \u03b2} (h : a \u2260 a') : lookup a (insert a' b' s) = lookup a s :=\n  list.lookup_kinsert_ne h\n\n@[simp] theorem lookup_to_alist {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} (s : List (sigma \u03b2)) : lookup a (list.to_alist s) = list.lookup a s := sorry\n\n@[simp] theorem insert_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {b' : \u03b2 a} (s : alist \u03b2) : insert a b' (insert a b s) = insert a b' s := sorry\n\ntheorem insert_insert_of_ne {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {b : \u03b2 a} {b' : \u03b2 a'} (s : alist \u03b2) (h : a \u2260 a') : entries (insert a' b' (insert a b s)) ~ entries (insert a b (insert a' b' s)) := sorry\n\n@[simp] theorem insert_singleton_eq {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {b' : \u03b2 a} : insert a b (singleton a b') = singleton a b := sorry\n\n@[simp] theorem entries_to_alist {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (xs : List (sigma \u03b2)) : entries (list.to_alist xs) = list.erase_dupkeys xs :=\n  rfl\n\ntheorem to_alist_cons {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a) (xs : List (sigma \u03b2)) : list.to_alist (sigma.mk a b :: xs) = insert a b (list.to_alist xs) :=\n  rfl\n\n/-! ### extract -/\n\n/-- Erase a key from the map, and return the corresponding value, if found. -/\ndef extract {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : Option (\u03b2 a) \u00d7 alist \u03b2 :=\n  (fun (this : list.nodupkeys (prod.snd (list.kextract a (entries s)))) => sorry) sorry\n\n@[simp] theorem extract_eq_lookup_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : extract a s = (lookup a s, erase a s) := sorry\n\n/-! ### union -/\n\n/-- `s\u2081 \u222a s\u2082` is the key-based union of two association lists. It is\nleft-biased: if there exists an `a \u2208 s\u2081`, `lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2081`.\n-/\ndef union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (s\u2081 : alist \u03b2) (s\u2082 : alist \u03b2) : alist \u03b2 :=\n  mk (list.kunion (entries s\u2081) (entries s\u2082)) sorry\n\nprotected instance has_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] : has_union (alist \u03b2) :=\n  has_union.mk union\n\n@[simp] theorem union_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : entries (s\u2081 \u222a s\u2082) = list.kunion (entries s\u2081) (entries s\u2082) :=\n  rfl\n\n@[simp] theorem empty_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s : alist \u03b2} : \u2205 \u222a s = s :=\n  ext rfl\n\n@[simp] theorem union_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s : alist \u03b2} : s \u222a \u2205 = s := sorry\n\n@[simp] theorem mem_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : a \u2208 s\u2081 \u222a s\u2082 \u2194 a \u2208 s\u2081 \u2228 a \u2208 s\u2082 :=\n  list.mem_keys_kunion\n\ntheorem perm_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} {s\u2083 : alist \u03b2} {s\u2084 : alist \u03b2} (p\u2081\u2082 : entries s\u2081 ~ entries s\u2082) (p\u2083\u2084 : entries s\u2083 ~ entries s\u2084) : entries (s\u2081 \u222a s\u2083) ~ entries (s\u2082 \u222a s\u2084) := sorry\n\ntheorem union_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s\u2081 : alist \u03b2) (s\u2082 : alist \u03b2) : erase a (s\u2081 \u222a s\u2082) = erase a s\u2081 \u222a erase a s\u2082 :=\n  ext (Eq.symm list.kunion_kerase)\n\n@[simp] theorem lookup_union_left {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : a \u2208 s\u2081 \u2192 lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2081 :=\n  list.lookup_kunion_left\n\n@[simp] theorem lookup_union_right {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : \u00aca \u2208 s\u2081 \u2192 lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2082 :=\n  list.lookup_kunion_right\n\n@[simp] theorem mem_lookup_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : b \u2208 lookup a (s\u2081 \u222a s\u2082) \u2194 b \u2208 lookup a s\u2081 \u2228 \u00aca \u2208 s\u2081 \u2227 b \u2208 lookup a s\u2082 :=\n  list.mem_lookup_kunion\n\ntheorem mem_lookup_union_middle {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} {s\u2083 : alist \u03b2} : b \u2208 lookup a (s\u2081 \u222a s\u2083) \u2192 \u00aca \u2208 s\u2082 \u2192 b \u2208 lookup a (s\u2081 \u222a s\u2082 \u222a s\u2083) :=\n  list.mem_lookup_kunion_middle\n\ntheorem insert_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : insert a b (s\u2081 \u222a s\u2082) = insert a b s\u2081 \u222a s\u2082 := sorry\n\ntheorem union_assoc {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} {s\u2083 : alist \u03b2} : entries (s\u2081 \u222a s\u2082 \u222a s\u2083) ~ entries (s\u2081 \u222a (s\u2082 \u222a s\u2083)) := sorry\n\n/-! ### disjoint -/\n\n/-- Two associative lists are disjoint if they have no common keys. -/\ndef disjoint {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (s\u2081 : alist \u03b2) (s\u2082 : alist \u03b2) :=\n  \u2200 (k : \u03b1), k \u2208 keys s\u2081 \u2192 \u00ack \u2208 keys s\u2082\n\ntheorem union_comm_of_disjoint {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} (h : disjoint s\u2081 s\u2082) : entries (s\u2081 \u222a s\u2082) ~ entries (s\u2082 \u222a s\u2081) := sorry\n\n", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/data/list/alist.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.47657966593214324, "lm_q2_score": 0.11920292202211756, "lm_q1q2_score": 0.05680968875543611}}
{"text": "/-\nCopyright (c) 2018 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthor: Simon Hudon\n\nLemmas about traversing collections.\n\nInspired by:\n\n    The Essence of the Iterator Pattern\n    Jeremy Gibbons and Bruno C\u00e9sar dos Santos Oliveira\n    In Journal of Functional Programming. Vol. 19. No. 3&4. Pages 377\u2212402. 2009.\n    <http://www.cs.ox.ac.uk/jeremy.gibbons/publications/iterator.pdf>\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.control.traversable.basic\nimport Mathlib.control.applicative\nimport Mathlib.PostPort\n\nuniverses u \n\nnamespace Mathlib\n\nnamespace traversable\n\n\n/-- The natural applicative transformation from the identity functor\nto `F`, defined by `pure : \u03a0 {\u03b1}, \u03b1 \u2192 F \u03b1`. -/\ndef pure_transformation (F : Type u \u2192 Type u) [Applicative F] [is_lawful_applicative F] :\n    applicative_transformation id F :=\n  applicative_transformation.mk pure sorry sorry\n\n@[simp] theorem pure_transformation_apply (F : Type u \u2192 Type u) [Applicative F]\n    [is_lawful_applicative F] {\u03b1 : Type u} (x : id \u03b1) :\n    coe_fn (pure_transformation F) \u03b1 x = pure x :=\n  rfl\n\ntheorem map_eq_traverse_id {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {\u03b2 : Type u} {\u03b3 : Type u} (f : \u03b2 \u2192 \u03b3) : Functor.map f = traverse (id.mk \u2218 f) :=\n  funext fun (y : t \u03b2) => Eq.symm (is_lawful_traversable.traverse_eq_map_id f y)\n\ntheorem map_traverse {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F] {\u03b1 : Type u} {\u03b2 : Type u}\n    {\u03b3 : Type u} (g : \u03b1 \u2192 F \u03b2) (f : \u03b2 \u2192 \u03b3) (x : t \u03b1) :\n    Functor.map f <$> traverse g x = traverse (Functor.map f \u2218 g) x :=\n  sorry\n\ntheorem traverse_map {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F] {\u03b1 : Type u} {\u03b2 : Type u}\n    {\u03b3 : Type u} (f : \u03b2 \u2192 F \u03b3) (g : \u03b1 \u2192 \u03b2) (x : t \u03b1) : traverse f (g <$> x) = traverse (f \u2218 g) x :=\n  sorry\n\ntheorem pure_traverse {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F] {\u03b1 : Type u} (x : t \u03b1) :\n    traverse pure x = pure x :=\n  eq.mp\n    (Eq._oldrec (Eq.refl (traverse pure x = pure (traverse id.mk x)))\n      (is_lawful_traversable.id_traverse x))\n    (Eq.symm (is_lawful_traversable.naturality (pure_transformation F) id.mk x))\n\ntheorem id_sequence {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t] {\u03b1 : Type u}\n    (x : t \u03b1) : sequence (id.mk <$> x) = id.mk x :=\n  sorry\n\ntheorem comp_sequence {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} {G : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F]\n    [Applicative G] [is_lawful_applicative G] {\u03b1 : Type u} (x : t (F (G \u03b1))) :\n    sequence (functor.comp.mk <$> x) = functor.comp.mk (sequence <$> sequence x) :=\n  sorry\n\ntheorem naturality' {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} {G : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F]\n    [Applicative G] [is_lawful_applicative G] {\u03b1 : Type u} (\u03b7 : applicative_transformation F G)\n    (x : t (F \u03b1)) : coe_fn \u03b7 (t \u03b1) (sequence x) = sequence (coe_fn \u03b7 \u03b1 <$> x) :=\n  sorry\n\ntheorem traverse_id {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t] {\u03b1 : Type u} :\n    traverse id.mk = id.mk :=\n  sorry\n\ntheorem traverse_comp {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} {G : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F]\n    [Applicative G] [is_lawful_applicative G] {\u03b1 : Type u} {\u03b2 : Type u} {\u03b3 : Type u} (g : \u03b1 \u2192 F \u03b2)\n    (h : \u03b2 \u2192 G \u03b3) :\n    traverse (functor.comp.mk \u2218 Functor.map h \u2218 g) =\n        functor.comp.mk \u2218 Functor.map (traverse h) \u2218 traverse g :=\n  sorry\n\ntheorem traverse_eq_map_id' {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {\u03b2 : Type u} {\u03b3 : Type u} (f : \u03b2 \u2192 \u03b3) : traverse (id.mk \u2218 f) = id.mk \u2218 Functor.map f :=\n  sorry\n\n-- @[functor_norm]\n\ntheorem traverse_map' {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {G : Type u \u2192 Type u} [Applicative G] [is_lawful_applicative G] {\u03b1 : Type u} {\u03b2 : Type u}\n    {\u03b3 : Type u} (g : \u03b1 \u2192 \u03b2) (h : \u03b2 \u2192 G \u03b3) : traverse (h \u2218 g) = traverse h \u2218 Functor.map g :=\n  sorry\n\ntheorem map_traverse' {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {G : Type u \u2192 Type u} [Applicative G] [is_lawful_applicative G] {\u03b1 : Type u} {\u03b2 : Type u}\n    {\u03b3 : Type u} (g : \u03b1 \u2192 G \u03b2) (h : \u03b2 \u2192 \u03b3) :\n    traverse (Functor.map h \u2218 g) = Functor.map (Functor.map h) \u2218 traverse g :=\n  sorry\n\ntheorem naturality_pf {t : Type u \u2192 Type u} [traversable t] [is_lawful_traversable t]\n    {F : Type u \u2192 Type u} {G : Type u \u2192 Type u} [Applicative F] [is_lawful_applicative F]\n    [Applicative G] [is_lawful_applicative G] {\u03b1 : Type u} {\u03b2 : Type u}\n    (\u03b7 : applicative_transformation F G) (f : \u03b1 \u2192 F \u03b2) :\n    traverse (coe_fn \u03b7 \u03b2 \u2218 f) = coe_fn \u03b7 (t \u03b2) \u2218 traverse f :=\n  sorry\n\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/control/traversable/lemmas_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4843800842769844, "lm_q2_score": 0.11596071519881658, "lm_q1q2_score": 0.05616906100082216}}
{"text": "/-\nCopyright (c) 2018 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon, Scott Morrison\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.core\nimport Mathlib.PostPort\n\nnamespace Mathlib\n\n/-!\n# solve_by_elim\n\nA depth-first search backwards reasoner.\n\n`solve_by_elim` takes a list of lemmas, and repeating tries to `apply` these against\nthe goals, recursively acting on any generated subgoals.\n\nIt accepts a variety of configuration options described below, enabling\n* backtracking across multiple goals,\n* pruning the search tree, and\n* invoking other tactics before or after trying to apply lemmas.\n\nAt present it has no \"premise selection\", and simply tries the supplied lemmas in order\nat each step of the search.\n-/\n\nnamespace tactic\n\n\nnamespace solve_by_elim\n\n\n/--\n`mk_assumption_set` builds a collection of lemmas for use in\nthe backtracking search in `solve_by_elim`.\n\n* By default, it includes all local hypotheses, along with `rfl`, `trivial`, `congr_fun` and\n  `congr_arg`.\n* The flag `no_dflt` removes these.\n* The argument `hs` is a list of `simp_arg_type`s,\n  and can be used to add, or remove, lemmas or expressions from the set.\n* The argument `attr : list name` adds all lemmas tagged with one of a specified list of attributes.\n\n`mk_assumption_set` returns not a `list expr`, but a `list (tactic expr) \u00d7 tactic (list expr)`.\nThere are two separate problems that need to be solved.\n\n### Relevant local hypotheses\n\n`solve_by_elim*` works with multiple goals,\nand we need to use separate sets of local hypotheses for each goal.\nThe second component of the returned value provides these local hypotheses.\n(Essentially using `local_context`, along with some filtering to remove hypotheses\nthat have been explicitly removed via `only` or `[-h]`.)\n\n### Stuck metavariables\n\nLemmas with implicit arguments would be filled in with metavariables if we created the\n`expr` objects immediately, so instead we return thunks that generate the expressions\non demand. This is the first component, with type `list (tactic expr)`.\n\nAs an example, we have `def rfl : \u2200 {\u03b1 : Sort u} {a : \u03b1}, a = a`, which on elaboration will become\n`@rfl ?m_1 ?m_2`.\n\nBecause `solve_by_elim` works by repeated application of lemmas against subgoals,\nthe first time such a lemma is successfully applied,\nthose metavariables will be unified, and thereafter have fixed values.\nThis would make it impossible to apply the lemma\na second time with different values of the metavariables.\n\nSee https://github.com/leanprover-community/mathlib/issues/2269\n\nAs an optimisation, after we build the list of `tactic expr`s, we actually run them, and replace any\nthat do not in fact produce metavariables with a simple `return` tactic.\n-/\n-- We lock the tactic state so that any spurious goals generated during\n\n-- elaboration of pre-expressions are discarded\n\n/--\nConfiguration options for `solve_by_elim`.\n\n* `accept : list expr \u2192 tactic unit` determines whether the current branch should be explored.\n   At each step, before the lemmas are applied,\n   `accept` is passed the proof terms for the original goals,\n   as reported by `get_goals` when `solve_by_elim` started.\n   These proof terms may be metavariables (if no progress has been made on that goal)\n   or may contain metavariables at some leaf nodes\n   (if the goal has been partially solved by previous `apply` steps).\n   If the `accept` tactic fails `solve_by_elim` aborts searching this branch and backtracks.\n   By default `accept := \u03bb _, skip` always succeeds.\n   (There is an example usage in `tests/solve_by_elim.lean`.)\n* `pre_apply : tactic unit` specifies an additional tactic to run before each round of `apply`.\n* `discharger : tactic unit` specifies an additional tactic to apply on subgoals\n  for which no lemma applies.\n  If that tactic succeeds, `solve_by_elim` will continue applying lemmas on resulting goals.\n-/\n/--\nA helper function for trace messages, prepending '....' depending on the current search depth.\n-/\n/-- A helper function to generate trace messages on successful applications. -/\n/-- A helper function to generate trace messages on unsuccessful applications. -/\n/--\nA helper function to generate the tactic that print trace messages.\nThis function exists to ensure the target is pretty printed only as necessary.\n-/\n/--\nThe internal implementation of `solve_by_elim`, with a limiting counter.\n-/\n/--\nArguments for `solve_by_elim`:\n* By default `solve_by_elim` operates only on the first goal,\n  but with `backtrack_all_goals := true`, it operates on all goals at once,\n  backtracking across goals as needed,\n  and only succeeds if it discharges all goals.\n* `lemmas` specifies the list of lemmas to use in the backtracking search.\n  If `none`, `solve_by_elim` uses the local hypotheses,\n  along with `rfl`, `trivial`, `congr_arg`, and `congr_fun`.\n* `lemma_thunks` provides the lemmas as a list of `tactic expr`,\n  which are used to regenerate the `expr` objects to avoid binding metavariables.\n  It should not usually be specified by the user.\n  (If both `lemmas` and `lemma_thunks` are specified, only `lemma_thunks` is used.)\n* `ctx_thunk` is for internal use only: it returns the local hypotheses which will be used.\n* `max_depth` bounds the depth of the search.\n-/\n/--\nIf no lemmas have been specified, generate the default set\n(local hypotheses, along with `rfl`, `trivial`, `congr_arg`, and `congr_fun`).\n-/\nend solve_by_elim\n\n\n/--\n`solve_by_elim` repeatedly tries `apply`ing a lemma\nfrom the list of assumptions (passed via the `opt` argument),\nrecursively operating on any generated subgoals, backtracking as necessary.\n\n`solve_by_elim` succeeds only if it discharges the goal.\n(By default, `solve_by_elim` focuses on the first goal, and only attempts to solve that.\nWith the option `backtrack_all_goals := tt`,\nit attempts to solve all goals, and only succeeds if it does so.\nWith `backtrack_all_goals := tt`, `solve_by_elim` will backtrack a solution it has found for\none goal if it then can't discharge other goals.)\n\nIf passed an empty list of assumptions, `solve_by_elim` builds a default set\nas per the interactive tactic, using the `local_context` along with\n`rfl`, `trivial`, `congr_arg`, and `congr_fun`.\n\nTo pass a particular list of assumptions, use the `lemmas` field\nin the configuration argument. This expects an\n`option (list expr)`. In certain situations it may be necessary to instead use the\n`lemma_thunks` field, which expects a `option (list (tactic expr))`.\nThis allows for regenerating metavariables\nfor each application, which might otherwise get stuck.\n\nSee also the simpler tactic `apply_rules`, which does not perform backtracking.\n-/\nnamespace interactive\n\n\n/--\n`apply_assumption` looks for an assumption of the form `... \u2192 \u2200 _, ... \u2192 head`\nwhere `head` matches the current goal.\n\nIf this fails, `apply_assumption` will call `symmetry` and try again.\n\nIf this also fails, `apply_assumption` will call `exfalso` and try again,\nso that if there is an assumption of the form `P \u2192 \u00ac Q`, the new tactic state\nwill have two goals, `P` and `Q`.\n\nOptional arguments:\n- `lemmas`: a list of expressions to apply, instead of the local constants\n- `tac`: a tactic to run on each subgoal after applying an assumption; if\n  this tactic fails, the corresponding assumption will be rejected and\n  the next one will be attempted.\n-/\n/--\n`solve_by_elim` calls `apply` on the main goal to find an assumption whose head matches\nand then repeatedly calls `apply` on the generated subgoals until no subgoals remain,\nperforming at most `max_depth` recursive steps.\n\n`solve_by_elim` discharges the current goal or fails.\n\n`solve_by_elim` performs back-tracking if subgoals can not be solved.\n\nBy default, the assumptions passed to `apply` are the local context, `rfl`, `trivial`,\n`congr_fun` and `congr_arg`.\n\nThe assumptions can be modified with similar syntax as for `simp`:\n* `solve_by_elim [h\u2081, h\u2082, ..., h\u1d63]` also applies the named lemmas.\n* `solve_by_elim with attr\u2081 ... attr\u1d63` also applies all lemmas tagged with the specified attributes.\n* `solve_by_elim only [h\u2081, h\u2082, ..., h\u1d63]` does not include the local context,\n  `rfl`, `trivial`, `congr_fun`, or `congr_arg` unless they are explicitly included.\n* `solve_by_elim [-id_1, ... -id_n]` uses the default assumptions, removing the specified ones.\n\n`solve_by_elim*` tries to solve all goals together, using backtracking if a solution for one goal\nmakes other goals impossible.\n\noptional arguments passed via a configuration argument as `solve_by_elim { ... }`\n- max_depth: number of attempts at discharging generated sub-goals\n- discharger: a subsidiary tactic to try at each step when no lemmas apply\n  (e.g. `cc` may be helpful).\n- pre_apply: a subsidiary tactic to run at each step before applying lemmas (e.g. `intros`).\n- accept: a subsidiary tactic `list expr \u2192 tactic unit` that at each step,\n    before any lemmas are applied, is passed the original proof terms\n    as reported by `get_goals` when `solve_by_elim` started\n    (but which may by now have been partially solved by previous `apply` steps).\n    If the `accept` tactic fails,\n    `solve_by_elim` will abort searching the current branch and backtrack.\n    This may be used to filter results, either at every step of the search,\n    or filtering complete results\n    (by testing for the absence of metavariables, and then the filtering condition).\n-/\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/tactic/solve_by_elim_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.44167300566462553, "lm_q2_score": 0.1259227582746744, "lm_q1q2_score": 0.055616683128755534}}
{"text": "/-\nCopyright (c) 2023 Jannis Limperg. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jannis Limperg\n-/\n\nimport Aesop\n\nexample : 1 = 2 \u2192 False := by\n  fail_if_success\n    aesop (simp_options := { enabled := false })\n      (options := { terminal := true })\n  aesop\n", "meta": {"author": "JLimperg", "repo": "aesop", "sha": "c68fb1d5a9172498230d81d95c61f6461bea6722", "save_path": "github-repos/lean/JLimperg-aesop", "path": "github-repos/lean/JLimperg-aesop/aesop-c68fb1d5a9172498230d81d95c61f6461bea6722/tests/run/NoNormSimp.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.4726834766204328, "lm_q2_score": 0.11757214436736566, "lm_q1q2_score": 0.05557440995328584}}
{"text": "/-\nCopyright (c) 2018 Sean Leather. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Sean Leather, Mario Carneiro\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.data.list.sigma\nimport Mathlib.PostPort\n\nuniverses u v l w \n\nnamespace Mathlib\n\n/-!\n# Association lists\n-/\n\n/-- `alist \u03b2` is a key-value map stored as a `list` (i.e. a linked list).\n  It is a wrapper around certain `list` functions with the added constraint\n  that the list have unique keys. -/\nstructure alist {\u03b1 : Type u} (\u03b2 : \u03b1 \u2192 Type v) where\n  entries : List (sigma \u03b2)\n  nodupkeys : list.nodupkeys entries\n\n/-- Given `l : list (sigma \u03b2)`, create a term of type `alist \u03b2` by removing\nentries with duplicate keys. -/\ndef list.to_alist {\u03b1 : Type u} [DecidableEq \u03b1] {\u03b2 : \u03b1 \u2192 Type v} (l : List (sigma \u03b2)) : alist \u03b2 :=\n  alist.mk (list.erase_dupkeys l) sorry\n\nnamespace alist\n\n\ntheorem ext {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {s : alist \u03b2} {t : alist \u03b2} :\n    entries s = entries t \u2192 s = t :=\n  sorry\n\ntheorem ext_iff {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {s : alist \u03b2} {t : alist \u03b2} :\n    s = t \u2194 entries s = entries t :=\n  { mp := congr_arg fun {s : alist \u03b2} => entries s, mpr := ext }\n\nprotected instance decidable_eq {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1]\n    [(a : \u03b1) \u2192 DecidableEq (\u03b2 a)] : DecidableEq (alist \u03b2) :=\n  fun (xs ys : alist \u03b2) => eq.mpr sorry (list.decidable_eq (entries xs) (entries ys))\n\n/-! ### keys -/\n\n/-- The list of keys of an association list. -/\ndef keys {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (s : alist \u03b2) : List \u03b1 := list.keys (entries s)\n\ntheorem keys_nodup {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (s : alist \u03b2) : list.nodup (keys s) := nodupkeys s\n\n/-! ### mem -/\n\n/-- The predicate `a \u2208 s` means that `s` has a value associated to the key `a`. -/\nprotected instance has_mem {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : has_mem \u03b1 (alist \u03b2) :=\n  has_mem.mk fun (a : \u03b1) (s : alist \u03b2) => a \u2208 keys s\n\ntheorem mem_keys {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {a : \u03b1} {s : alist \u03b2} : a \u2208 s \u2194 a \u2208 keys s := iff.rfl\n\ntheorem mem_of_perm {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {a : \u03b1} {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2}\n    (p : entries s\u2081 ~ entries s\u2082) : a \u2208 s\u2081 \u2194 a \u2208 s\u2082 :=\n  list.perm.mem_iff (list.perm.map sigma.fst p)\n\n/-! ### empty -/\n\n/-- The empty association list. -/\nprotected instance has_emptyc {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : has_emptyc (alist \u03b2) :=\n  has_emptyc.mk (mk [] list.nodupkeys_nil)\n\nprotected instance inhabited {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : Inhabited (alist \u03b2) := { default := \u2205 }\n\ntheorem not_mem_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) : \u00aca \u2208 \u2205 := list.not_mem_nil a\n\n@[simp] theorem empty_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : entries \u2205 = [] := rfl\n\n@[simp] theorem keys_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} : keys \u2205 = [] := rfl\n\n/-! ### singleton -/\n\n/-- The singleton association list. -/\ndef singleton {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) (b : \u03b2 a) : alist \u03b2 := mk [sigma.mk a b] sorry\n\n@[simp] theorem singleton_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) (b : \u03b2 a) :\n    entries (singleton a b) = [sigma.mk a b] :=\n  rfl\n\n@[simp] theorem keys_singleton {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (a : \u03b1) (b : \u03b2 a) :\n    keys (singleton a b) = [a] :=\n  rfl\n\n/-! ### lookup -/\n\n/-- Look up the value associated to a key in an association list. -/\ndef lookup {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : Option (\u03b2 a) :=\n  list.lookup a (entries s)\n\n@[simp] theorem lookup_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) :\n    lookup a \u2205 = none :=\n  rfl\n\ntheorem lookup_is_some {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s : alist \u03b2} :\n    \u21a5(option.is_some (lookup a s)) \u2194 a \u2208 s :=\n  list.lookup_is_some\n\ntheorem lookup_eq_none {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s : alist \u03b2} :\n    lookup a s = none \u2194 \u00aca \u2208 s :=\n  list.lookup_eq_none\n\ntheorem perm_lookup {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} (p : entries s\u2081 ~ entries s\u2082) : lookup a s\u2081 = lookup a s\u2082 :=\n  list.perm_lookup a (nodupkeys s\u2081) (nodupkeys s\u2082) p\n\nprotected instance has_mem.mem.decidable {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1)\n    (s : alist \u03b2) : Decidable (a \u2208 s) :=\n  decidable_of_iff \u21a5(option.is_some (lookup a s)) sorry\n\n/-! ### replace -/\n\n/-- Replace a key with a given value in an association list.\n  If the key is not present it does nothing. -/\ndef replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a) (s : alist \u03b2) :\n    alist \u03b2 :=\n  mk (list.kreplace a b (entries s)) sorry\n\n@[simp] theorem keys_replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a)\n    (s : alist \u03b2) : keys (replace a b s) = keys s :=\n  list.keys_kreplace a b (entries s)\n\n@[simp] theorem mem_replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {b : \u03b2 a}\n    {s : alist \u03b2} : a' \u2208 replace a b s \u2194 a' \u2208 s :=\n  eq.mpr (id (Eq._oldrec (Eq.refl (a' \u2208 replace a b s \u2194 a' \u2208 s)) (propext mem_keys)))\n    (eq.mpr (id (Eq._oldrec (Eq.refl (a' \u2208 keys (replace a b s) \u2194 a' \u2208 s)) (keys_replace a b s)))\n      (eq.mpr (id (Eq._oldrec (Eq.refl (a' \u2208 keys s \u2194 a' \u2208 s)) (Eq.symm (propext mem_keys))))\n        (iff.refl (a' \u2208 s))))\n\ntheorem perm_replace {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} :\n    entries s\u2081 ~ entries s\u2082 \u2192 entries (replace a b s\u2081) ~ entries (replace a b s\u2082) :=\n  list.perm.kreplace (nodupkeys s\u2081)\n\n/-- Fold a function over the key-value pairs in the map. -/\ndef foldl {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} {\u03b4 : Type w} (f : \u03b4 \u2192 (a : \u03b1) \u2192 \u03b2 a \u2192 \u03b4) (d : \u03b4)\n    (m : alist \u03b2) : \u03b4 :=\n  list.foldl (fun (r : \u03b4) (a : sigma \u03b2) => f r (sigma.fst a) (sigma.snd a)) d (entries m)\n\n/-! ### erase -/\n\n/-- Erase a key from the map. If the key is not present, do nothing. -/\ndef erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) : alist \u03b2 :=\n  mk (list.kerase a (entries s)) sorry\n\n@[simp] theorem keys_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) :\n    keys (erase a s) = list.erase (keys s) a :=\n  sorry\n\n@[simp] theorem mem_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1}\n    {s : alist \u03b2} : a' \u2208 erase a s \u2194 a' \u2260 a \u2227 a' \u2208 s :=\n  sorry\n\ntheorem perm_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} : entries s\u2081 ~ entries s\u2082 \u2192 entries (erase a s\u2081) ~ entries (erase a s\u2082) :=\n  list.perm.kerase (nodupkeys s\u2081)\n\n@[simp] theorem lookup_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) :\n    lookup a (erase a s) = none :=\n  list.lookup_kerase a (nodupkeys s)\n\n@[simp] theorem lookup_erase_ne {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1}\n    {s : alist \u03b2} (h : a \u2260 a') : lookup a (erase a' s) = lookup a s :=\n  list.lookup_kerase_ne h\n\ntheorem erase_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (a' : \u03b1) (s : alist \u03b2) :\n    erase a' (erase a s) = erase a (erase a' s) :=\n  ext list.kerase_kerase\n\n/-! ### insert -/\n\n/-- Insert a key-value pair into an association list and erase any existing pair\n  with the same key. -/\ndef insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a) (s : alist \u03b2) :\n    alist \u03b2 :=\n  mk (list.kinsert a b (entries s)) sorry\n\n@[simp] theorem insert_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    {s : alist \u03b2} : entries (insert a b s) = sigma.mk a b :: list.kerase a (entries s) :=\n  rfl\n\ntheorem insert_entries_of_neg {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    {s : alist \u03b2} (h : \u00aca \u2208 s) : entries (insert a b s) = sigma.mk a b :: entries s :=\n  sorry\n\n@[simp] theorem mem_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1}\n    {b' : \u03b2 a'} (s : alist \u03b2) : a \u2208 insert a' b' s \u2194 a = a' \u2228 a \u2208 s :=\n  list.mem_keys_kinsert\n\n@[simp] theorem keys_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    (s : alist \u03b2) : keys (insert a b s) = a :: list.erase (keys s) a :=\n  sorry\n\ntheorem perm_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} (p : entries s\u2081 ~ entries s\u2082) :\n    entries (insert a b s\u2081) ~ entries (insert a b s\u2082) :=\n  sorry\n\n@[simp] theorem lookup_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    (s : alist \u03b2) : lookup a (insert a b s) = some b :=\n  sorry\n\n@[simp] theorem lookup_insert_ne {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1}\n    {b' : \u03b2 a'} {s : alist \u03b2} (h : a \u2260 a') : lookup a (insert a' b' s) = lookup a s :=\n  list.lookup_kinsert_ne h\n\n@[simp] theorem lookup_to_alist {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1}\n    (s : List (sigma \u03b2)) : lookup a (list.to_alist s) = list.lookup a s :=\n  sorry\n\n@[simp] theorem insert_insert {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    {b' : \u03b2 a} (s : alist \u03b2) : insert a b' (insert a b s) = insert a b' s :=\n  sorry\n\ntheorem insert_insert_of_ne {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {a' : \u03b1} {b : \u03b2 a}\n    {b' : \u03b2 a'} (s : alist \u03b2) (h : a \u2260 a') :\n    entries (insert a' b' (insert a b s)) ~ entries (insert a b (insert a' b' s)) :=\n  sorry\n\n@[simp] theorem insert_singleton_eq {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    {b' : \u03b2 a} : insert a b (singleton a b') = singleton a b :=\n  sorry\n\n@[simp] theorem entries_to_alist {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1]\n    (xs : List (sigma \u03b2)) : entries (list.to_alist xs) = list.erase_dupkeys xs :=\n  rfl\n\ntheorem to_alist_cons {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (b : \u03b2 a)\n    (xs : List (sigma \u03b2)) : list.to_alist (sigma.mk a b :: xs) = insert a b (list.to_alist xs) :=\n  rfl\n\n/-! ### extract -/\n\n/-- Erase a key from the map, and return the corresponding value, if found. -/\ndef extract {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s : alist \u03b2) :\n    Option (\u03b2 a) \u00d7 alist \u03b2 :=\n  (fun (this : list.nodupkeys (prod.snd (list.kextract a (entries s)))) => sorry) sorry\n\n@[simp] theorem extract_eq_lookup_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1)\n    (s : alist \u03b2) : extract a s = (lookup a s, erase a s) :=\n  sorry\n\n/-! ### union -/\n\n/-- `s\u2081 \u222a s\u2082` is the key-based union of two association lists. It is\nleft-biased: if there exists an `a \u2208 s\u2081`, `lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2081`.\n-/\ndef union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (s\u2081 : alist \u03b2) (s\u2082 : alist \u03b2) : alist \u03b2 :=\n  mk (list.kunion (entries s\u2081) (entries s\u2082)) sorry\n\nprotected instance has_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] : has_union (alist \u03b2) :=\n  has_union.mk union\n\n@[simp] theorem union_entries {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} : entries (s\u2081 \u222a s\u2082) = list.kunion (entries s\u2081) (entries s\u2082) :=\n  rfl\n\n@[simp] theorem empty_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s : alist \u03b2} :\n    \u2205 \u222a s = s :=\n  ext rfl\n\n@[simp] theorem union_empty {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s : alist \u03b2} :\n    s \u222a \u2205 = s :=\n  sorry\n\n@[simp] theorem mem_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} : a \u2208 s\u2081 \u222a s\u2082 \u2194 a \u2208 s\u2081 \u2228 a \u2208 s\u2082 :=\n  list.mem_keys_kunion\n\ntheorem perm_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2}\n    {s\u2083 : alist \u03b2} {s\u2084 : alist \u03b2} (p\u2081\u2082 : entries s\u2081 ~ entries s\u2082) (p\u2083\u2084 : entries s\u2083 ~ entries s\u2084) :\n    entries (s\u2081 \u222a s\u2083) ~ entries (s\u2082 \u222a s\u2084) :=\n  sorry\n\ntheorem union_erase {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] (a : \u03b1) (s\u2081 : alist \u03b2)\n    (s\u2082 : alist \u03b2) : erase a (s\u2081 \u222a s\u2082) = erase a s\u2081 \u222a erase a s\u2082 :=\n  ext (Eq.symm list.kunion_kerase)\n\n@[simp] theorem lookup_union_left {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1}\n    {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : a \u2208 s\u2081 \u2192 lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2081 :=\n  list.lookup_kunion_left\n\n@[simp] theorem lookup_union_right {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1}\n    {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} : \u00aca \u2208 s\u2081 \u2192 lookup a (s\u2081 \u222a s\u2082) = lookup a s\u2082 :=\n  list.lookup_kunion_right\n\n@[simp] theorem mem_lookup_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} :\n    b \u2208 lookup a (s\u2081 \u222a s\u2082) \u2194 b \u2208 lookup a s\u2081 \u2228 \u00aca \u2208 s\u2081 \u2227 b \u2208 lookup a s\u2082 :=\n  list.mem_lookup_kunion\n\ntheorem mem_lookup_union_middle {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a}\n    {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2} {s\u2083 : alist \u03b2} :\n    b \u2208 lookup a (s\u2081 \u222a s\u2083) \u2192 \u00aca \u2208 s\u2082 \u2192 b \u2208 lookup a (s\u2081 \u222a s\u2082 \u222a s\u2083) :=\n  list.mem_lookup_kunion_middle\n\ntheorem insert_union {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {a : \u03b1} {b : \u03b2 a} {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} : insert a b (s\u2081 \u222a s\u2082) = insert a b s\u2081 \u222a s\u2082 :=\n  sorry\n\ntheorem union_assoc {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2} {s\u2082 : alist \u03b2}\n    {s\u2083 : alist \u03b2} : entries (s\u2081 \u222a s\u2082 \u222a s\u2083) ~ entries (s\u2081 \u222a (s\u2082 \u222a s\u2083)) :=\n  sorry\n\n/-! ### disjoint -/\n\n/-- Two associative lists are disjoint if they have no common keys. -/\ndef disjoint {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} (s\u2081 : alist \u03b2) (s\u2082 : alist \u03b2) :=\n  \u2200 (k : \u03b1), k \u2208 keys s\u2081 \u2192 \u00ack \u2208 keys s\u2082\n\ntheorem union_comm_of_disjoint {\u03b1 : Type u} {\u03b2 : \u03b1 \u2192 Type v} [DecidableEq \u03b1] {s\u2081 : alist \u03b2}\n    {s\u2082 : alist \u03b2} (h : disjoint s\u2081 s\u2082) : entries (s\u2081 \u222a s\u2082) ~ entries (s\u2082 \u222a s\u2081) :=\n  sorry\n\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/data/list/alist_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.47268347662043286, "lm_q2_score": 0.11757214282138605, "lm_q1q2_score": 0.05557440922252682}}
{"text": "/-\nCopyright (c) 2018 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon\n\n! This file was ported from Lean 3 source module control.traversable.lemmas\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.Control.Applicative\nimport Mathbin.Control.Traversable.Basic\n\n/-!\n# Traversing collections\n\n> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.\n> Any changes to this file require a corresponding PR to mathlib4.\n\nThis file proves basic properties of traversable and applicative functors and defines\n`pure_transformation F`, the natural applicative transformation from the identity functor to `F`.\n\n## References\n\nInspired by [The Essence of the Iterator Pattern][gibbons2009].\n-/\n\n\nuniverse u\n\nopen IsLawfulTraversable\n\nopen Function hiding comp\n\nopen Functor\n\nattribute [functor_norm] IsLawfulTraversable.naturality\n\nattribute [simp] IsLawfulTraversable.id_traverse\n\nnamespace Traversable\n\nvariable {t : Type u \u2192 Type u}\n\nvariable [Traversable t] [IsLawfulTraversable t]\n\nvariable (F G : Type u \u2192 Type u)\n\nvariable [Applicative F] [LawfulApplicative F]\n\nvariable [Applicative G] [LawfulApplicative G]\n\nvariable {\u03b1 \u03b2 \u03b3 : Type u}\n\nvariable (g : \u03b1 \u2192 F \u03b2)\n\nvariable (h : \u03b2 \u2192 G \u03b3)\n\nvariable (f : \u03b2 \u2192 \u03b3)\n\n#print Traversable.PureTransformation /-\n/-- The natural applicative transformation from the identity functor\nto `F`, defined by `pure : \u03a0 {\u03b1}, \u03b1 \u2192 F \u03b1`. -/\ndef PureTransformation : ApplicativeTransformation id F\n    where\n  app := @pure F _\n  preserves_pure' \u03b1 x := rfl\n  preserves_seq' \u03b1 \u03b2 f x := by\n    simp only [map_pure, seq_pure]\n    rfl\n#align traversable.pure_transformation Traversable.PureTransformation\n-/\n\n#print Traversable.pureTransformation_apply /-\n@[simp]\ntheorem pureTransformation_apply {\u03b1} (x : id \u03b1) : PureTransformation F x = pure x :=\n  rfl\n#align traversable.pure_transformation_apply Traversable.pureTransformation_apply\n-/\n\nvariable {F G} (x : t \u03b2)\n\n/- warning: traversable.map_eq_traverse_id -> Traversable.map_eq_traverse_id is a dubious translation:\nlean 3 declaration is\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b2 : Type.{u1}} {\u03b3 : Type.{u1}} (f : \u03b2 -> \u03b3), Eq.{succ u1} ((t \u03b2) -> (t \u03b3)) (Functor.map.{u1, u1} (fun {\u03b2 : Type.{u1}} => t \u03b2) (Traversable.toFunctor.{u1} (fun {\u03b2 : Type.{u1}} => t \u03b2) _inst_1) \u03b2 \u03b3 f) (Traversable.traverse.{u1} t _inst_1 (id.{succ (succ u1)} Type.{u1}) (Monad.toApplicative.{u1, u1} (id.{succ (succ u1)} Type.{u1}) id.monad.{u1}) \u03b2 \u03b3 (Function.comp.{succ u1, succ u1, succ u1} \u03b2 \u03b3 (id.{succ (succ u1)} Type.{u1} \u03b3) (id.mk.{succ u1} \u03b3) f))\nbut is expected to have type\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b2 : Type.{u1}} {\u03b3 : Type.{u1}} (f : \u03b2 -> \u03b3), Eq.{succ u1} ((t \u03b2) -> (t \u03b3)) (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) \u03b2 \u03b3 f) (Traversable.traverse.{u1} t _inst_1 Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1}) \u03b2 \u03b3 (Function.comp.{succ u1, succ u1, succ u1} \u03b2 \u03b3 (Id.{u1} \u03b3) (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) \u03b3) f))\nCase conversion may be inaccurate. Consider using '#align traversable.map_eq_traverse_id Traversable.map_eq_traverse_id\u2093'. -/\ntheorem map_eq_traverse_id : map f = @traverse t _ _ _ _ _ (id.mk \u2218 f) :=\n  funext fun y => (traverse_eq_map_id f y).symm\n#align traversable.map_eq_traverse_id Traversable.map_eq_traverse_id\n\n#print Traversable.map_traverse /-\ntheorem map_traverse (x : t \u03b1) : map f <$> traverse g x = traverse (map f \u2218 g) x :=\n  by\n  rw [@map_eq_traverse_id t _ _ _ _ f]\n  refine' (comp_traverse (id.mk \u2218 f) g x).symm.trans _\n  congr ; apply comp.applicative_comp_id\n#align traversable.map_traverse Traversable.map_traverse\n-/\n\n#print Traversable.traverse_map /-\ntheorem traverse_map (f : \u03b2 \u2192 F \u03b3) (g : \u03b1 \u2192 \u03b2) (x : t \u03b1) :\n    traverse f (g <$> x) = traverse (f \u2218 g) x :=\n  by\n  rw [@map_eq_traverse_id t _ _ _ _ g]\n  refine' (comp_traverse f (id.mk \u2218 g) x).symm.trans _\n  congr ; apply comp.applicative_id_comp\n#align traversable.traverse_map Traversable.traverse_map\n-/\n\n#print Traversable.pure_traverse /-\ntheorem pure_traverse (x : t \u03b1) : traverse pure x = (pure x : F (t \u03b1)) := by\n  have : traverse pure x = pure (traverse id.mk x) :=\n      (naturality (pure_transformation F) id.mk x).symm <;>\n    rwa [id_traverse] at this\n#align traversable.pure_traverse Traversable.pure_traverse\n-/\n\n/- warning: traversable.id_sequence -> Traversable.id_sequence is a dubious translation:\nlean 3 declaration is\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b1 : Type.{u1}} (x : t \u03b1), Eq.{succ u1} (id.{succ (succ u1)} Type.{u1} (t \u03b1)) (sequence.{u1} t \u03b1 (id.{succ (succ u1)} Type.{u1}) (Monad.toApplicative.{u1, u1} (id.{succ (succ u1)} Type.{u1}) id.monad.{u1}) _inst_1 (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) \u03b1 (id.{succ (succ u1)} Type.{u1} \u03b1) (id.mk.{succ u1} \u03b1) x)) (id.mk.{succ u1} (t \u03b1) x)\nbut is expected to have type\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b1 : Type.{u1}} (x : t \u03b1), Eq.{succ u1} (Id.{u1} (t \u03b1)) (sequence.{u1} t \u03b1 Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1}) _inst_1 (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) \u03b1 (Id.{u1} \u03b1) (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) \u03b1) x)) (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) (t \u03b1) x)\nCase conversion may be inaccurate. Consider using '#align traversable.id_sequence Traversable.id_sequence\u2093'. -/\ntheorem id_sequence (x : t \u03b1) : sequence (id.mk <$> x) = id.mk x := by\n  simp [sequence, traverse_map, id_traverse] <;> rfl\n#align traversable.id_sequence Traversable.id_sequence\n\n/- warning: traversable.comp_sequence -> Traversable.comp_sequence is a dubious translation:\nlean 3 declaration is\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {F : Type.{u1} -> Type.{u1}} {G : Type.{u1} -> Type.{u1}} [_inst_3 : Applicative.{u1, u1} F] [_inst_4 : LawfulApplicative.{u1, u1} F _inst_3] [_inst_5 : Applicative.{u1, u1} G] [_inst_6 : LawfulApplicative.{u1, u1} G _inst_5] {\u03b1 : Type.{u1}} (x : t (F (G \u03b1))), Eq.{succ u1} (Functor.Comp.{u1, u1, u1} F G (t \u03b1)) (sequence.{u1} t \u03b1 (Functor.Comp.{u1, u1, u1} F G) (Functor.Comp.applicative.{u1, u1, u1} F G _inst_3 _inst_5) _inst_1 (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) (F (G \u03b1)) (Functor.Comp.{u1, u1, u1} F G \u03b1) (Functor.Comp.mk.{u1, u1, u1} F G \u03b1) x)) (Functor.Comp.mk.{u1, u1, u1} F G (t \u03b1) (Functor.map.{u1, u1} F (Applicative.toFunctor.{u1, u1} F _inst_3) (t (G \u03b1)) (G (t \u03b1)) (sequence.{u1} t \u03b1 G _inst_5 _inst_1) (sequence.{u1} t (G \u03b1) F _inst_3 _inst_1 x)))\nbut is expected to have type\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {F : Type.{u1} -> Type.{u1}} {G : Type.{u1} -> Type.{u1}} [_inst_3 : Applicative.{u1, u1} F] [_inst_4 : LawfulApplicative.{u1, u1} F _inst_3] [_inst_5 : Applicative.{u1, u1} G] [_inst_6 : LawfulApplicative.{u1, u1} G _inst_5] {\u03b1 : Type.{u1}} (x : t (F (G \u03b1))), Eq.{succ u1} (Functor.Comp.{u1, u1, u1} F G (t \u03b1)) (sequence.{u1} t \u03b1 (Functor.Comp.{u1, u1, u1} F G) (Functor.Comp.instApplicativeComp.{u1, u1, u1} F G _inst_3 _inst_5) _inst_1 (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) (F (G \u03b1)) (Functor.Comp.{u1, u1, u1} F G \u03b1) (Functor.Comp.mk.{u1, u1, u1} F G \u03b1) x)) (Functor.Comp.mk.{u1, u1, u1} F G (t \u03b1) (Functor.map.{u1, u1} F (Applicative.toFunctor.{u1, u1} F _inst_3) (t (G \u03b1)) (G (t \u03b1)) (sequence.{u1} t \u03b1 G _inst_5 _inst_1) (sequence.{u1} t (G \u03b1) F _inst_3 _inst_1 x)))\nCase conversion may be inaccurate. Consider using '#align traversable.comp_sequence Traversable.comp_sequence\u2093'. -/\ntheorem comp_sequence (x : t (F (G \u03b1))) :\n    sequence (Comp.mk <$> x) = Comp.mk (sequence <$> sequence x) := by\n  simp [sequence, traverse_map] <;> rw [\u2190 comp_traverse] <;> simp [map_id]\n#align traversable.comp_sequence Traversable.comp_sequence\n\n#print Traversable.naturality' /-\ntheorem naturality' (\u03b7 : ApplicativeTransformation F G) (x : t (F \u03b1)) :\n    \u03b7 (sequence x) = sequence (@\u03b7 _ <$> x) := by simp [sequence, naturality, traverse_map]\n#align traversable.naturality' Traversable.naturality'\n-/\n\n/- warning: traversable.traverse_id -> Traversable.traverse_id is a dubious translation:\nlean 3 declaration is\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b1 : Type.{u1}}, Eq.{succ u1} ((t \u03b1) -> (id.{succ (succ u1)} Type.{u1} (t \u03b1))) (Traversable.traverse.{u1} t _inst_1 (id.{succ (succ u1)} Type.{u1}) (Monad.toApplicative.{u1, u1} (id.{succ (succ u1)} Type.{u1}) id.monad.{u1}) \u03b1 \u03b1 (id.mk.{succ u1} \u03b1)) (id.mk.{succ u1} (t \u03b1))\nbut is expected to have type\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b1 : Type.{u1}}, Eq.{succ u1} ((t \u03b1) -> (Id.{u1} (t \u03b1))) (Traversable.traverse.{u1} t _inst_1 Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1}) \u03b1 \u03b1 (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) \u03b1)) (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) (t \u03b1))\nCase conversion may be inaccurate. Consider using '#align traversable.traverse_id Traversable.traverse_id\u2093'. -/\n@[functor_norm]\ntheorem traverse_id : traverse id.mk = (id.mk : t \u03b1 \u2192 id (t \u03b1)) :=\n  by\n  ext\n  exact id_traverse _\n#align traversable.traverse_id Traversable.traverse_id\n\n/- warning: traversable.traverse_comp -> Traversable.traverse_comp is a dubious translation:\nlean 3 declaration is\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {F : Type.{u1} -> Type.{u1}} {G : Type.{u1} -> Type.{u1}} [_inst_3 : Applicative.{u1, u1} F] [_inst_4 : LawfulApplicative.{u1, u1} F _inst_3] [_inst_5 : Applicative.{u1, u1} G] [_inst_6 : LawfulApplicative.{u1, u1} G _inst_5] {\u03b1 : Type.{u1}} {\u03b2 : Type.{u1}} {\u03b3 : Type.{u1}} (g : \u03b1 -> (F \u03b2)) (h : \u03b2 -> (G \u03b3)), Eq.{succ u1} ((t \u03b1) -> (Functor.Comp.{u1, u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) G (t \u03b3))) (Traversable.traverse.{u1} (fun {\u03b1 : Type.{u1}} => t \u03b1) _inst_1 (Functor.Comp.{u1, u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) G) (Functor.Comp.applicative.{u1, u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) G _inst_3 _inst_5) \u03b1 \u03b3 (Function.comp.{succ u1, succ u1, succ u1} \u03b1 (F (G \u03b3)) (Functor.Comp.{u1, u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) G \u03b3) (Functor.Comp.mk.{u1, u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) G \u03b3) (Function.comp.{succ u1, succ u1, succ u1} \u03b1 (F \u03b2) (F (G \u03b3)) (Functor.map.{u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) (Applicative.toFunctor.{u1, u1} (fun {\u03b2 : Type.{u1}} => F \u03b2) _inst_3) \u03b2 (G \u03b3) h) g))) (Function.comp.{succ u1, succ u1, succ u1} (t \u03b1) (F (G (t \u03b3))) (Functor.Comp.{u1, u1, u1} F G (t \u03b3)) (Functor.Comp.mk.{u1, u1, u1} F G (t \u03b3)) (Function.comp.{succ u1, succ u1, succ u1} (t \u03b1) (F (t \u03b2)) (F (G (t \u03b3))) (Functor.map.{u1, u1} F (Applicative.toFunctor.{u1, u1} F _inst_3) (t \u03b2) (G (t \u03b3)) (Traversable.traverse.{u1} t _inst_1 G _inst_5 \u03b2 \u03b3 h)) (Traversable.traverse.{u1} t _inst_1 F _inst_3 \u03b1 \u03b2 g)))\nbut is expected to have type\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {F : Type.{u1} -> Type.{u1}} {G : Type.{u1} -> Type.{u1}} [_inst_3 : Applicative.{u1, u1} F] [_inst_4 : LawfulApplicative.{u1, u1} F _inst_3] [_inst_5 : Applicative.{u1, u1} G] [_inst_6 : LawfulApplicative.{u1, u1} G _inst_5] {\u03b1 : Type.{u1}} {\u03b2 : Type.{u1}} {\u03b3 : Type.{u1}} (g : \u03b1 -> (F \u03b2)) (h : \u03b2 -> (G \u03b3)), Eq.{succ u1} ((t \u03b1) -> (Functor.Comp.{u1, u1, u1} F G (t \u03b3))) (Traversable.traverse.{u1} t _inst_1 (Functor.Comp.{u1, u1, u1} F G) (Functor.Comp.instApplicativeComp.{u1, u1, u1} F G _inst_3 _inst_5) \u03b1 \u03b3 (Function.comp.{succ u1, succ u1, succ u1} \u03b1 (F (G \u03b3)) (Functor.Comp.{u1, u1, u1} F G \u03b3) (Functor.Comp.mk.{u1, u1, u1} F G \u03b3) (Function.comp.{succ u1, succ u1, succ u1} \u03b1 (F \u03b2) (F (G \u03b3)) (Functor.map.{u1, u1} F (Applicative.toFunctor.{u1, u1} F _inst_3) \u03b2 (G \u03b3) h) g))) (Function.comp.{succ u1, succ u1, succ u1} (t \u03b1) (F (G (t \u03b3))) (Functor.Comp.{u1, u1, u1} F G (t \u03b3)) (Functor.Comp.mk.{u1, u1, u1} F G (t \u03b3)) (Function.comp.{succ u1, succ u1, succ u1} (t \u03b1) (F (t \u03b2)) (F (G (t \u03b3))) (Functor.map.{u1, u1} F (Applicative.toFunctor.{u1, u1} F _inst_3) (t \u03b2) (G (t \u03b3)) (Traversable.traverse.{u1} t _inst_1 G _inst_5 \u03b2 \u03b3 h)) (Traversable.traverse.{u1} t _inst_1 F _inst_3 \u03b1 \u03b2 g)))\nCase conversion may be inaccurate. Consider using '#align traversable.traverse_comp Traversable.traverse_comp\u2093'. -/\n@[functor_norm]\ntheorem traverse_comp (g : \u03b1 \u2192 F \u03b2) (h : \u03b2 \u2192 G \u03b3) :\n    traverse (Comp.mk \u2218 map h \u2218 g) =\n      (Comp.mk \u2218 map (traverse h) \u2218 traverse g : t \u03b1 \u2192 Comp F G (t \u03b3)) :=\n  by\n  ext\n  exact comp_traverse _ _ _\n#align traversable.traverse_comp Traversable.traverse_comp\n\n/- warning: traversable.traverse_eq_map_id' -> Traversable.traverse_eq_map_id' is a dubious translation:\nlean 3 declaration is\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b2 : Type.{u1}} {\u03b3 : Type.{u1}} (f : \u03b2 -> \u03b3), Eq.{succ u1} ((t \u03b2) -> (id.{succ (succ u1)} Type.{u1} (t \u03b3))) (Traversable.traverse.{u1} (fun {\u03b2 : Type.{u1}} => t \u03b2) _inst_1 (id.{succ (succ u1)} Type.{u1}) (Monad.toApplicative.{u1, u1} (id.{succ (succ u1)} Type.{u1}) id.monad.{u1}) \u03b2 \u03b3 (Function.comp.{succ u1, succ u1, succ u1} \u03b2 \u03b3 (id.{succ (succ u1)} Type.{u1} \u03b3) (id.mk.{succ u1} \u03b3) f)) (Function.comp.{succ u1, succ u1, succ u1} (t \u03b2) (t \u03b3) (id.{succ (succ u1)} Type.{u1} (t \u03b3)) (id.mk.{succ u1} (t \u03b3)) (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) \u03b2 \u03b3 f))\nbut is expected to have type\n  forall {t : Type.{u1} -> Type.{u1}} [_inst_1 : Traversable.{u1} t] [_inst_2 : IsLawfulTraversable.{u1} t _inst_1] {\u03b2 : Type.{u1}} {\u03b3 : Type.{u1}} (f : \u03b2 -> \u03b3), Eq.{succ u1} ((t \u03b2) -> (Id.{u1} (t \u03b3))) (Traversable.traverse.{u1} t _inst_1 Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1}) \u03b2 \u03b3 (Function.comp.{succ u1, succ u1, succ u1} \u03b2 \u03b3 (Id.{u1} \u03b3) (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) \u03b3) f)) (Function.comp.{succ u1, succ u1, succ u1} (t \u03b2) (t \u03b3) (Id.{u1} (t \u03b3)) (Pure.pure.{u1, u1} Id.{u1} (Applicative.toPure.{u1, u1} Id.{u1} (Monad.toApplicative.{u1, u1} Id.{u1} Id.instMonadId.{u1})) (t \u03b3)) (Functor.map.{u1, u1} t (Traversable.toFunctor.{u1} t _inst_1) \u03b2 \u03b3 f))\nCase conversion may be inaccurate. Consider using '#align traversable.traverse_eq_map_id' Traversable.traverse_eq_map_id'\u2093'. -/\ntheorem traverse_eq_map_id' (f : \u03b2 \u2192 \u03b3) : traverse (id.mk \u2218 f) = id.mk \u2218 (map f : t \u03b2 \u2192 t \u03b3) :=\n  by\n  ext\n  exact traverse_eq_map_id _ _\n#align traversable.traverse_eq_map_id' Traversable.traverse_eq_map_id'\n\n#print Traversable.traverse_map' /-\n-- @[functor_norm]\ntheorem traverse_map' (g : \u03b1 \u2192 \u03b2) (h : \u03b2 \u2192 G \u03b3) :\n    traverse (h \u2218 g) = (traverse h \u2218 map g : t \u03b1 \u2192 G (t \u03b3)) :=\n  by\n  ext\n  rw [comp_app, traverse_map]\n#align traversable.traverse_map' Traversable.traverse_map'\n-/\n\n#print Traversable.map_traverse' /-\ntheorem map_traverse' (g : \u03b1 \u2192 G \u03b2) (h : \u03b2 \u2192 \u03b3) :\n    traverse (map h \u2218 g) = (map (map h) \u2218 traverse g : t \u03b1 \u2192 G (t \u03b3)) :=\n  by\n  ext\n  rw [comp_app, map_traverse]\n#align traversable.map_traverse' Traversable.map_traverse'\n-/\n\n#print Traversable.naturality_pf /-\ntheorem naturality_pf (\u03b7 : ApplicativeTransformation F G) (f : \u03b1 \u2192 F \u03b2) :\n    traverse (@\u03b7 _ \u2218 f) = @\u03b7 _ \u2218 (traverse f : t \u03b1 \u2192 F (t \u03b2)) :=\n  by\n  ext\n  rw [comp_app, naturality]\n#align traversable.naturality_pf Traversable.naturality_pf\n-/\n\nend Traversable\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/Control/Traversable/Lemmas.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.49218813572079556, "lm_q2_score": 0.11279540031810142, "lm_q1q2_score": 0.05551655780044717}}
{"text": "/-\nCopyright (c) 2019 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon\n-/\nimport category_theory.category.basic\n\n/-!\n# Tools to reformulate category-theoretic axioms in a more associativity-friendly way\n\n## The `reassoc` attribute\n\nThe `reassoc` attribute can be applied to a lemma\n\n```lean\n@[reassoc]\nlemma some_lemma : foo \u226b bar = baz := ...\n```\n\nand produce\n\n```lean\nlemma some_lemma_assoc {Y : C} (f : X \u27f6 Y) : foo \u226b bar \u226b f = baz \u226b f := ...\n```\n\nThe name of the produced lemma can be specified with `@[reassoc other_lemma_name]`. If\n`simp` is added first, the generated lemma will also have the `simp` attribute.\n\n## The `reassoc_axiom` command\n\nWhen declaring a class of categories, the axioms can be reformulated to be more amenable\nto manipulation in right associated expressions:\n\n```lean\nclass some_class (C : Type) [category C] :=\n(foo : \u03a0 X : C, X \u27f6 X)\n(bar : \u2200 {X Y : C} (f : X \u27f6 Y), foo X \u226b f = f \u226b foo Y)\n\nreassoc_axiom some_class.bar\n```\n\nHere too, the `reassoc` attribute can be used instead. It works well when combined with\n`simp`:\n\n```lean\nattribute [simp, reassoc] some_class.bar\n```\n-/\n\nnamespace tactic\n\nopen category_theory\n\n/-- From an expression `f \u226b g`, extract the expression representing the category instance. -/\nmeta def get_cat_inst : expr \u2192 tactic expr\n| `(@category_struct.comp _ %%struct_inst _ _ _ _ _) := pure struct_inst\n| _ := failed\n\n/-- (internals for `@[reassoc]`)\nGiven a lemma of the form `\u2200 ..., f \u226b g = h`, proves a new lemma of the form\n`h : \u2200 ... {W} (k), f \u226b (g \u226b k) = h \u226b k`, and returns the type and proof of this lemma.\n-/\nmeta def prove_reassoc (h : expr) : tactic (expr \u00d7 expr) :=\ndo\n   (vs,t) \u2190 infer_type h >>= open_pis,\n   (lhs,rhs) \u2190 match_eq t,\n   struct_inst \u2190 get_cat_inst lhs <|> get_cat_inst rhs <|> fail \"no composition found in statement\",\n   `(@quiver.hom _ %%hom_inst %%X %%Y) \u2190 infer_type lhs,\n   C \u2190 infer_type X,\n   X' \u2190 mk_local' `X' binder_info.implicit C,\n   ft \u2190 to_expr ``(@quiver.hom _ %%hom_inst %%Y %%X'),\n   f' \u2190 mk_local_def `f' ft,\n   t' \u2190 to_expr ``(@category_struct.comp _ %%struct_inst _ _ _%%lhs %%f' =\n                     @category_struct.comp _ %%struct_inst _ _ _ %%rhs %%f'),\n   let c' := h.mk_app vs,\n   (_,pr) \u2190 solve_aux t' (rewrite_target c'; reflexivity),\n   pr \u2190 instantiate_mvars pr,\n   let s := simp_lemmas.mk,\n   s \u2190 s.add_simp ``category.assoc,\n   s \u2190 s.add_simp ``category.id_comp,\n   s \u2190 s.add_simp ``category.comp_id,\n   (t'', pr', _) \u2190 simplify s [] t',\n   pr' \u2190 mk_eq_mp pr' pr,\n   t'' \u2190 pis (vs ++ [X',f']) t'',\n   pr' \u2190 lambdas (vs ++ [X',f']) pr',\n   pure (t'',pr')\n\n/-- (implementation for `@[reassoc]`)\nGiven a declaration named `n` of the form `\u2200 ..., f \u226b g = h`, proves a new lemma named `n'`\nof the form `\u2200 ... {W} (k), f \u226b (g \u226b k) = h \u226b k`.\n-/\nmeta def reassoc_axiom (n : name) (n' : name := n.append_suffix \"_assoc\") : tactic unit :=\ndo d \u2190 get_decl n,\n   let ls := d.univ_params.map level.param,\n   let c := @expr.const tt n ls,\n   (t'',pr') \u2190 prove_reassoc c,\n   add_decl $ declaration.thm n' d.univ_params t'' (pure pr'),\n   copy_attribute `simp n n'\n\nsetup_tactic_parser\n\n/--\nThe `reassoc` attribute can be applied to a lemma\n\n```lean\n@[reassoc]\nlemma some_lemma : foo \u226b bar = baz := ...\n```\n\nto produce\n\n```lean\nlemma some_lemma_assoc {Y : C} (f : X \u27f6 Y) : foo \u226b bar \u226b f = baz \u226b f := ...\n```\n\nThe name of the produced lemma can be specified with `@[reassoc other_lemma_name]`. If\n`simp` is added first, the generated lemma will also have the `simp` attribute.\n-/\n@[user_attribute]\nmeta def reassoc_attr : user_attribute unit (option name) :=\n{ name := `reassoc,\n  descr := \"create a companion lemma for associativity-aware rewriting\",\n  parser := optional ident,\n  after_set := some (\u03bb n _ _,\n    do some n' \u2190 reassoc_attr.get_param n | reassoc_axiom n (n.append_suffix \"_assoc\"),\n       reassoc_axiom n $ n.get_prefix ++ n' ) }\n\nadd_tactic_doc\n{ name                     := \"reassoc\",\n  category                 := doc_category.attr,\n  decl_names               := [`tactic.reassoc_attr],\n  tags                     := [\"category theory\"] }\n\n/--\nWhen declaring a class of categories, the axioms can be reformulated to be more amenable\nto manipulation in right associated expressions:\n\n```lean\nclass some_class (C : Type) [category C] :=\n(foo : \u03a0 X : C, X \u27f6 X)\n(bar : \u2200 {X Y : C} (f : X \u27f6 Y), foo X \u226b f = f \u226b foo Y)\n\nreassoc_axiom some_class.bar\n```\n\nThe above will produce:\n\n```lean\nlemma some_class.bar_assoc {Z : C} (g : Y \u27f6 Z) :\n  foo X \u226b f \u226b g = f \u226b foo Y \u226b g := ...\n```\n\nHere too, the `reassoc` attribute can be used instead. It works well when combined with\n`simp`:\n\n```lean\nattribute [simp, reassoc] some_class.bar\n```\n-/\n@[user_command]\nmeta def reassoc_cmd (_ : parse $ tk \"reassoc_axiom\") : lean.parser unit :=\ndo n \u2190 ident,\n   of_tactic $\n   do n \u2190 resolve_constant n,\n      reassoc_axiom n\n\nadd_tactic_doc\n{ name                     := \"reassoc_axiom\",\n  category                 := doc_category.cmd,\n  decl_names               := [`tactic.reassoc_cmd],\n  tags                     := [\"category theory\"] }\n\nnamespace interactive\n\n/-- `reassoc h`, for assumption `h : x \u226b y = z`, creates a new assumption\n`h : \u2200 {W} (f : Z \u27f6 W), x \u226b y \u226b f = z \u226b f`.\n`reassoc! h`, does the same but deletes the initial `h` assumption.\n(You can also add the attribute `@[reassoc]` to lemmas to generate new declarations generalized\nin this way.)\n-/\nmeta def reassoc (del : parse (tk \"!\")?) (ns : parse ident*) : tactic unit :=\ndo ns.mmap' (\u03bb n,\n   do h \u2190 get_local n,\n      (t,pr) \u2190 prove_reassoc h,\n      assertv n t pr,\n      when del.is_some (tactic.clear h) )\n\nend interactive\n\ndef calculated_Prop {\u03b1} (\u03b2 : Prop) (hh : \u03b1) := \u03b2\n\nmeta def derive_reassoc_proof : tactic unit :=\ndo `(calculated_Prop %%v %%h) \u2190 target,\n   (t,pr) \u2190 prove_reassoc h,\n   unify v t,\n   exact pr\n\nend tactic\n\n/-- With `h : x \u226b y \u226b z = x` (with universal quantifiers tolerated),\n`reassoc_of h : \u2200 {X'} (f : W \u27f6 X'), x \u226b y \u226b z \u226b f = x \u226b f`.\n\nThe type and proof of `reassoc_of h` is generated by `tactic.derive_reassoc_proof`\nwhich make `reassoc_of` meta-programming adjacent. It is not called as a tactic but as\nan expression. The goal is to avoid creating assumptions that are dismissed after one use:\n\n```lean\nexample (X Y Z W : C) (x : X \u27f6 Y) (y : Y \u27f6 Z) (z z' : Z \u27f6 W) (w : X \u27f6 Z)\n  (h : x \u226b y = w)\n  (h' : y \u226b z = y \u226b z') :\n  x \u226b y \u226b z = w \u226b z' :=\nbegin\n  rw [h',reassoc_of h],\nend\n```\n-/\ntheorem category_theory.reassoc_of {\u03b1} (hh : \u03b1) {\u03b2}\n  (x : tactic.calculated_Prop \u03b2 hh . tactic.derive_reassoc_proof) : \u03b2 := x\n\n/--\n`reassoc_of h` takes local assumption `h` and add a ` \u226b f` term on the right of\nboth sides of the equality. Instead of creating a new assumption from the result, `reassoc_of h`\nstands for the proof of that reassociated statement. This keeps complicated assumptions that are\nused only once or twice from polluting the local context.\n\nIn the following, assumption `h` is needed in a reassociated form. Instead of proving it as a new\ngoal and adding it as an assumption, we use `reassoc_of h` as a rewrite rule which works just as\nwell.\n\n```lean\nexample (X Y Z W : C) (x : X \u27f6 Y) (y : Y \u27f6 Z) (z z' : Z \u27f6 W) (w : X \u27f6 Z)\n  (h : x \u226b y = w)\n  (h' : y \u226b z = y \u226b z') :\n  x \u226b y \u226b z = w \u226b z' :=\nbegin\n  -- reassoc_of h : \u2200 {X' : C} (f : W \u27f6 X'), x \u226b y \u226b f = w \u226b f\n  rw [h',reassoc_of h],\nend\n```\n\nAlthough `reassoc_of` is not a tactic or a meta program, its type is generated\nthrough meta-programming to make it usable inside normal expressions.\n-/\nadd_tactic_doc\n{ name                     := \"category_theory.reassoc_of\",\n  category                 := doc_category.tactic,\n  decl_names               := [`category_theory.reassoc_of],\n  tags                     := [\"category theory\"] }\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/reassoc_axiom.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.42632159254749036, "lm_q2_score": 0.1294027332703691, "lm_q1q2_score": 0.05516717932782187}}
{"text": "/-\nCopyright (c) 2018 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison, Jannis Limperg\n-/\n\n/-!\n# Monadic instances for `ulift` and `plift`\n\nIn this file we define `monad` and `is_lawful_monad` instances on `plift` and `ulift`. -/\n\nuniverses u v\n\nnamespace plift\n\nvariables {\u03b1 : Sort u} {\u03b2 : Sort v}\n\n/-- Functorial action. -/\nprotected def map (f : \u03b1 \u2192 \u03b2) (a : plift \u03b1) : plift \u03b2 :=\nplift.up (f a.down)\n\n@[simp] lemma map_up (f : \u03b1 \u2192 \u03b2) (a : \u03b1) : (plift.up a).map f = plift.up (f a) := rfl\n\n/-- Embedding of pure values. -/\n@[simp] protected def pure : \u03b1 \u2192 plift \u03b1 := up\n\n/-- Applicative sequencing. -/\nprotected def seq (f : plift (\u03b1 \u2192 \u03b2)) (x : plift \u03b1) : plift \u03b2 :=\nplift.up (f.down x.down)\n\n@[simp] lemma seq_up (f : \u03b1 \u2192 \u03b2) (x : \u03b1) : (plift.up f).seq (plift.up x) = plift.up (f x) := rfl\n\n/-- Monadic bind. -/\nprotected def bind (a : plift \u03b1) (f : \u03b1 \u2192 plift \u03b2) : plift \u03b2 := f a.down\n\n@[simp] lemma bind_up (a : \u03b1) (f : \u03b1 \u2192 plift \u03b2) : (plift.up a).bind f = f a := rfl\n\ninstance : monad plift :=\n{ map := @plift.map,\n  pure := @plift.pure,\n  seq := @plift.seq,\n  bind := @plift.bind }\n\ninstance : is_lawful_functor plift :=\n{ id_map := \u03bb \u03b1 \u27e8x\u27e9, rfl,\n  comp_map := \u03bb \u03b1 \u03b2 \u03b3 g h \u27e8x\u27e9, rfl }\n\ninstance : is_lawful_applicative plift :=\n{ pure_seq_eq_map := \u03bb \u03b1 \u03b2 g \u27e8x\u27e9, rfl,\n  map_pure := \u03bb \u03b1 \u03b2 g x, rfl,\n  seq_pure := \u03bb \u03b1 \u03b2 \u27e8g\u27e9 x, rfl,\n  seq_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 \u27e8g\u27e9 \u27e8h\u27e9, rfl }\n\ninstance : is_lawful_monad plift :=\n{ bind_pure_comp_eq_map := \u03bb \u03b1 \u03b2 f \u27e8x\u27e9, rfl,\n  bind_map_eq_seq := \u03bb \u03b1 \u03b2 \u27e8a\u27e9 \u27e8b\u27e9, rfl,\n  pure_bind := \u03bb \u03b1 \u03b2 x f, rfl,\n  bind_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 f g, rfl }\n\n@[simp] lemma rec.constant {\u03b1 : Sort u} {\u03b2 : Type v} (b : \u03b2) :\n  @plift.rec \u03b1 (\u03bb _, \u03b2) (\u03bb _, b) = \u03bb _, b :=\nfunext (\u03bb x, plift.cases_on x (\u03bb a, eq.refl (plift.rec (\u03bb a', b) {down := a})))\n\nend plift\n\n\nnamespace ulift\n\nvariables {\u03b1 : Type u} {\u03b2 : Type v}\n\n/-- Functorial action. -/\nprotected def map (f : \u03b1 \u2192 \u03b2) (a : ulift \u03b1) : ulift \u03b2 :=\nulift.up (f a.down)\n\n@[simp] lemma map_up (f : \u03b1 \u2192 \u03b2) (a : \u03b1) : (ulift.up a).map f = ulift.up (f a) := rfl\n\n/-- Embedding of pure values. -/\n@[simp] protected def pure : \u03b1 \u2192 ulift \u03b1 := up\n\n/-- Applicative sequencing. -/\nprotected def seq (f : ulift (\u03b1 \u2192 \u03b2)) (x : ulift \u03b1) : ulift \u03b2 :=\nulift.up (f.down x.down)\n\n@[simp] lemma seq_up (f : \u03b1 \u2192 \u03b2) (x : \u03b1) : (ulift.up f).seq (ulift.up x) = ulift.up (f x) := rfl\n\n/-- Monadic bind. -/\nprotected def bind (a : ulift \u03b1) (f : \u03b1 \u2192 ulift \u03b2) : ulift \u03b2 := f a.down\n\n@[simp] lemma bind_up (a : \u03b1) (f : \u03b1 \u2192 ulift \u03b2) : (ulift.up a).bind f = f a := rfl\n\ninstance : monad ulift :=\n{ map := @ulift.map,\n  pure := @ulift.pure,\n  seq := @ulift.seq,\n  bind := @ulift.bind }\n\ninstance : is_lawful_functor ulift :=\n{ id_map := \u03bb \u03b1 \u27e8x\u27e9, rfl,\n  comp_map := \u03bb \u03b1 \u03b2 \u03b3 g h \u27e8x\u27e9, rfl }\n\ninstance : is_lawful_applicative ulift :=\n{ to_is_lawful_functor := ulift.is_lawful_functor,\n  pure_seq_eq_map := \u03bb \u03b1 \u03b2 g \u27e8x\u27e9, rfl,\n  map_pure := \u03bb \u03b1 \u03b2 g x, rfl,\n  seq_pure := \u03bb \u03b1 \u03b2 \u27e8g\u27e9 x, rfl,\n  seq_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 \u27e8g\u27e9 \u27e8h\u27e9, rfl }\n\ninstance : is_lawful_monad ulift :=\n{ bind_pure_comp_eq_map := \u03bb \u03b1 \u03b2 f \u27e8x\u27e9, rfl,\n  bind_map_eq_seq := \u03bb \u03b1 \u03b2 \u27e8a\u27e9 \u27e8b\u27e9, rfl,\n  pure_bind := \u03bb \u03b1 \u03b2 x f,\n    by { dsimp only [bind, pure, ulift.pure, ulift.bind], cases (f x), refl },\n  bind_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 f g,\n    by { dsimp only [bind, pure, ulift.pure, ulift.bind], cases (f x), refl } }\n\n@[simp] lemma rec.constant {\u03b1 : Type u} {\u03b2 : Sort v} (b : \u03b2) :\n  @ulift.rec \u03b1 (\u03bb _, \u03b2) (\u03bb _, b) = \u03bb _, b :=\nfunext (\u03bb x, ulift.cases_on x (\u03bb a, eq.refl (ulift.rec (\u03bb a', b) {down := a})))\n\nend ulift\n", "meta": {"author": "Parinya-Siri", "repo": "lean-machine-learning", "sha": "ec610bac246ae7108fc6f0c140b3440f0fbacc52", "save_path": "github-repos/lean/Parinya-Siri-lean-machine-learning", "path": "github-repos/lean/Parinya-Siri-lean-machine-learning/lean-machine-learning-ec610bac246ae7108fc6f0c140b3440f0fbacc52/matlib/control/ulift.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.47268349147711747, "lm_q2_score": 0.11596071367124151, "lm_q1q2_score": 0.05481271501230075}}
{"text": "/-\nCopyright (c) 2016 Jeremy Avigad. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jeremy Avigad, Leonardo de Moura\n-/\nimport tactic.doc_commands\nimport tactic.reserved_notation\n\n/-!\n# Basic logic properties\n\nThis file is one of the earliest imports in mathlib.\n\n## Implementation notes\n\nTheorems that require decidability hypotheses are in the namespace \"decidable\".\nClassical versions are in the namespace \"classical\".\n\nIn the presence of automation, this whole file may be unnecessary. On the other hand,\nmaybe it is useful for writing automation.\n-/\n\nopen function\nlocal attribute [instance, priority 10] classical.prop_decidable\n\nsection miscellany\n\n/- We add the `inline` attribute to optimize VM computation using these declarations. For example,\n  `if p \u2227 q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/\nattribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable\n  decidable.true implies.decidable not.decidable ne.decidable\n  bool.decidable_eq decidable.to_bool\n\nattribute [simp] cast_eq cast_heq\n\nvariables {\u03b1 : Type*} {\u03b2 : Type*}\n\n/-- An identity function with its main argument implicit. This will be printed as `hidden` even\nif it is applied to a large term, so it can be used for elision,\nas done in the `elide` and `unelide` tactics. -/\n@[reducible] def hidden {\u03b1 : Sort*} {a : \u03b1} := a\n\n/-- Ex falso, the nondependent eliminator for the `empty` type. -/\ndef empty.elim {C : Sort*} : empty \u2192 C.\n\ninstance : subsingleton empty := \u27e8\u03bba, a.elim\u27e9\n\ninstance subsingleton.prod {\u03b1 \u03b2 : Type*} [subsingleton \u03b1] [subsingleton \u03b2] : subsingleton (\u03b1 \u00d7 \u03b2) :=\n\u27e8by { intros a b, cases a, cases b, congr, }\u27e9\n\ninstance : decidable_eq empty := \u03bba, a.elim\n\ninstance sort.inhabited : inhabited Sort* := \u27e8punit\u27e9\ninstance sort.inhabited' : inhabited default := \u27e8punit.star\u27e9\n\ninstance psum.inhabited_left {\u03b1 \u03b2} [inhabited \u03b1] : inhabited (psum \u03b1 \u03b2) := \u27e8psum.inl default\u27e9\ninstance psum.inhabited_right {\u03b1 \u03b2} [inhabited \u03b2] : inhabited (psum \u03b1 \u03b2) := \u27e8psum.inr default\u27e9\n\n@[priority 10] instance decidable_eq_of_subsingleton\n  {\u03b1} [subsingleton \u03b1] : decidable_eq \u03b1\n| a b := is_true (subsingleton.elim a b)\n\n@[simp] lemma eq_iff_true_of_subsingleton {\u03b1 : Sort*} [subsingleton \u03b1] (x y : \u03b1) :\n  x = y \u2194 true :=\nby cc\n\n/-- If all points are equal to a given point `x`, then `\u03b1` is a subsingleton. -/\nlemma subsingleton_of_forall_eq {\u03b1 : Sort*} (x : \u03b1) (h : \u2200 y, y = x) : subsingleton \u03b1 :=\n\u27e8\u03bb a b, (h a).symm \u25b8 (h b).symm \u25b8 rfl\u27e9\n\nlemma subsingleton_iff_forall_eq {\u03b1 : Sort*} (x : \u03b1) : subsingleton \u03b1 \u2194 \u2200 y, y = x :=\n\u27e8\u03bb h y, @subsingleton.elim _ h y x, subsingleton_of_forall_eq x\u27e9\n\n-- TODO[gh-6025]: make this an instance once safe to do so\nlemma subtype.subsingleton (\u03b1 : Sort*) [subsingleton \u03b1] (p : \u03b1 \u2192 Prop) : subsingleton (subtype p) :=\n\u27e8\u03bb \u27e8x,_\u27e9 \u27e8y,_\u27e9, have x = y, from subsingleton.elim _ _, by { cases this, refl }\u27e9\n\n/-- Add an instance to \"undo\" coercion transitivity into a chain of coercions, because\n   most simp lemmas are stated with respect to simple coercions and will not match when\n   part of a chain. -/\n@[simp] theorem coe_coe {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_t \u03b2 \u03b3]\n  (a : \u03b1) : (a : \u03b3) = (a : \u03b2) := rfl\n\ntheorem coe_fn_coe_trans\n  {\u03b1 \u03b2 \u03b3 \u03b4} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_fun \u03b3 \u03b4]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\n/-- Non-dependent version of `coe_fn_coe_trans`, helps `rw` figure out the argument. -/\ntheorem coe_fn_coe_trans'\n  {\u03b1 \u03b2 \u03b3} {\u03b4 : out_param $ _} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_fun \u03b3 (\u03bb _, \u03b4)]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\n@[simp] theorem coe_fn_coe_base\n  {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_to_fun \u03b2 \u03b3]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\n/-- Non-dependent version of `coe_fn_coe_base`, helps `rw` figure out the argument. -/\ntheorem coe_fn_coe_base'\n  {\u03b1 \u03b2} {\u03b3 : out_param $ _} [has_coe \u03b1 \u03b2] [has_coe_to_fun \u03b2 (\u03bb _, \u03b3)]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\ntheorem coe_sort_coe_trans\n  {\u03b1 \u03b2 \u03b3 \u03b4} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_sort \u03b3 \u03b4]\n  (x : \u03b1) : @coe_sort \u03b1 _ _ x = @coe_sort \u03b2 _ _ x := rfl\n\n/--\nMany structures such as bundled morphisms coerce to functions so that you can\ntransparently apply them to arguments. For example, if `e : \u03b1 \u2243 \u03b2` and `a : \u03b1`\nthen you can write `e a` and this is elaborated as `\u21d1e a`. This type of\ncoercion is implemented using the `has_coe_to_fun` type class. There is one\nimportant consideration:\n\nIf a type coerces to another type which in turn coerces to a function,\nthen it **must** implement `has_coe_to_fun` directly:\n```lean\nstructure sparkling_equiv (\u03b1 \u03b2) extends \u03b1 \u2243 \u03b2\n\n-- if we add a `has_coe` instance,\ninstance {\u03b1 \u03b2} : has_coe (sparkling_equiv \u03b1 \u03b2) (\u03b1 \u2243 \u03b2) :=\n\u27e8sparkling_equiv.to_equiv\u27e9\n\n-- then a `has_coe_to_fun` instance **must** be added as well:\ninstance {\u03b1 \u03b2} : has_coe_to_fun (sparkling_equiv \u03b1 \u03b2) :=\n\u27e8\u03bb _, \u03b1 \u2192 \u03b2, \u03bb f, f.to_equiv.to_fun\u27e9\n```\n\n(Rationale: if we do not declare the direct coercion, then `\u21d1e a` is not in\nsimp-normal form. The lemma `coe_fn_coe_base` will unfold it to `\u21d1\u2191e a`. This\noften causes loops in the simplifier.)\n-/\nlibrary_note \"function coercion\"\n\n@[simp] theorem coe_sort_coe_base\n  {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_to_sort \u03b2 \u03b3]\n  (x : \u03b1) : @coe_sort \u03b1 _ _ x = @coe_sort \u03b2 _ _ x := rfl\n\n/-- `pempty` is the universe-polymorphic analogue of `empty`. -/\n@[derive decidable_eq]\ninductive {u} pempty : Sort u\n\n/-- Ex falso, the nondependent eliminator for the `pempty` type. -/\ndef pempty.elim {C : Sort*} : pempty \u2192 C.\n\ninstance subsingleton_pempty : subsingleton pempty := \u27e8\u03bba, a.elim\u27e9\n\n@[simp] lemma not_nonempty_pempty : \u00ac nonempty pempty :=\nassume \u27e8h\u27e9, h.elim\n\n@[simp] theorem forall_pempty {P : pempty \u2192 Prop} : (\u2200 x : pempty, P x) \u2194 true :=\n\u27e8\u03bb h, trivial, \u03bb h x, by cases x\u27e9\n\n@[simp] theorem exists_pempty {P : pempty \u2192 Prop} : (\u2203 x : pempty, P x) \u2194 false :=\n\u27e8\u03bb h, by { cases h with w, cases w }, false.elim\u27e9\n\nlemma congr_heq {\u03b1 \u03b2 \u03b3 : Sort*} {f : \u03b1 \u2192 \u03b3} {g : \u03b2 \u2192 \u03b3} {x : \u03b1} {y : \u03b2} (h\u2081 : f == g)\n  (h\u2082 : x == y) : f x = g y :=\nby { cases h\u2082, cases h\u2081, refl }\n\nlemma congr_arg_heq {\u03b1} {\u03b2 : \u03b1 \u2192 Sort*} (f : \u2200 a, \u03b2 a) : \u2200 {a\u2081 a\u2082 : \u03b1}, a\u2081 = a\u2082 \u2192 f a\u2081 == f a\u2082\n| a _ rfl := heq.rfl\n\nlemma ulift.down_injective {\u03b1 : Sort*} : function.injective (@ulift.down \u03b1)\n| \u27e8a\u27e9 \u27e8b\u27e9 rfl := rfl\n\n@[simp] lemma ulift.down_inj {\u03b1 : Sort*} {a b : ulift \u03b1} : a.down = b.down \u2194 a = b :=\n\u27e8\u03bb h, ulift.down_injective h, \u03bb h, by rw h\u27e9\n\nlemma plift.down_injective {\u03b1 : Sort*} : function.injective (@plift.down \u03b1)\n| \u27e8a\u27e9 \u27e8b\u27e9 rfl := rfl\n\n@[simp] lemma plift.down_inj {\u03b1 : Sort*} {a b : plift \u03b1} : a.down = b.down \u2194 a = b :=\n\u27e8\u03bb h, plift.down_injective h, \u03bb h, by rw h\u27e9\n\n-- missing [symm] attribute for ne in core.\nattribute [symm] ne.symm\n\nlemma ne_comm {\u03b1} {a b : \u03b1} : a \u2260 b \u2194 b \u2260 a := \u27e8ne.symm, ne.symm\u27e9\n\n@[simp] lemma eq_iff_eq_cancel_left {b c : \u03b1} :\n  (\u2200 {a}, a = b \u2194 a = c) \u2194 (b = c) :=\n\u27e8\u03bb h, by rw [\u2190 h], \u03bb h a, by rw h\u27e9\n\n@[simp] lemma eq_iff_eq_cancel_right {a b : \u03b1} :\n  (\u2200 {c}, a = c \u2194 b = c) \u2194 (a = b) :=\n\u27e8\u03bb h, by rw h, \u03bb h a, by rw h\u27e9\n\n/-- Wrapper for adding elementary propositions to the type class systems.\nWarning: this can easily be abused. See the rest of this docstring for details.\n\nCertain propositions should not be treated as a class globally,\nbut sometimes it is very convenient to be able to use the type class system\nin specific circumstances.\n\nFor example, `zmod p` is a field if and only if `p` is a prime number.\nIn order to be able to find this field instance automatically by type class search,\nwe have to turn `p.prime` into an instance implicit assumption.\n\nOn the other hand, making `nat.prime` a class would require a major refactoring of the library,\nand it is questionable whether making `nat.prime` a class is desirable at all.\nThe compromise is to add the assumption `[fact p.prime]` to `zmod.field`.\n\nIn particular, this class is not intended for turning the type class system\ninto an automated theorem prover for first order logic. -/\nclass fact (p : Prop) : Prop := (out [] : p)\n\n/--\nIn most cases, we should not have global instances of `fact`; typeclass search only reads the head\nsymbol and then tries any instances, which means that adding any such instance will cause slowdowns\neverywhere. We instead make them as lemmata and make them local instances as required.\n-/\nlibrary_note \"fact non-instances\"\n\nlemma fact.elim {p : Prop} (h : fact p) : p := h.1\nlemma fact_iff {p : Prop} : fact p \u2194 p := \u27e8\u03bb h, h.1, \u03bb h, \u27e8h\u27e9\u27e9\n\n/-- Swaps two pairs of arguments to a function. -/\n@[reducible] def function.swap\u2082 {\u03b9\u2081 \u03b9\u2082 : Sort*} {\u03ba\u2081 : \u03b9\u2081 \u2192 Sort*} {\u03ba\u2082 : \u03b9\u2082 \u2192 Sort*}\n  {\u03c6 : \u03a0 i\u2081, \u03ba\u2081 i\u2081 \u2192 \u03a0 i\u2082, \u03ba\u2082 i\u2082 \u2192 Sort*} (f : \u03a0 i\u2081 j\u2081 i\u2082 j\u2082, \u03c6 i\u2081 j\u2081 i\u2082 j\u2082) :\n  \u03a0 i\u2082 j\u2082 i\u2081 j\u2081, \u03c6 i\u2081 j\u2081 i\u2082 j\u2082 :=\n\u03bb i\u2082 j\u2082 i\u2081 j\u2081, f i\u2081 j\u2081 i\u2082 j\u2082\n\n/-- If `x : \u03b1 . tac_name` then `x.out : \u03b1`. These are definitionally equal, but this can\nnevertheless be useful for various reasons, e.g. to apply further projection notation or in an\nargument to `simp`. -/\ndef auto_param.out {\u03b1 : Sort*} {n : name} (x : auto_param \u03b1 n) : \u03b1 := x\n\n/-- If `x : \u03b1 := d` then `x.out : \u03b1`. These are definitionally equal, but this can\nnevertheless be useful for various reasons, e.g. to apply further projection notation or in an\nargument to `simp`. -/\ndef opt_param.out {\u03b1 : Sort*} {d : \u03b1} (x : \u03b1 := d) : \u03b1 := x\n\nend miscellany\n\nopen function\n\n/-!\n### Declarations about propositional connectives\n-/\n\ntheorem false_ne_true : false \u2260 true\n| h := h.symm \u25b8 trivial\n\nsection propositional\nvariables {a b c d e f : Prop}\n\n/-! ### Declarations about `implies` -/\n\ninstance : is_refl Prop iff := \u27e8iff.refl\u27e9\ninstance : is_trans Prop iff := \u27e8\u03bb _ _ _, iff.trans\u27e9\n\ntheorem iff_of_eq (e : a = b) : a \u2194 b := e \u25b8 iff.rfl\n\ntheorem iff_iff_eq : (a \u2194 b) \u2194 a = b := \u27e8propext, iff_of_eq\u27e9\n\n@[simp] lemma eq_iff_iff {p q : Prop} : (p = q) \u2194 (p \u2194 q) := iff_iff_eq.symm\n\n@[simp] theorem imp_self : (a \u2192 a) \u2194 true := iff_true_intro id\n\nlemma iff.imp (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : (a \u2192 c) \u2194 (b \u2192 d) := imp_congr h\u2081 h\u2082\n\n@[simp] lemma eq_true_eq_id : eq true = id :=\nby { funext, simp only [true_iff, id.def, iff_self, eq_iff_iff], }\n\ntheorem imp_intro {\u03b1 \u03b2 : Prop} (h : \u03b1) : \u03b2 \u2192 \u03b1 := \u03bb _, h\n\ntheorem imp_false : (a \u2192 false) \u2194 \u00ac a := iff.rfl\n\ntheorem imp_and_distrib {\u03b1} : (\u03b1 \u2192 b \u2227 c) \u2194 (\u03b1 \u2192 b) \u2227 (\u03b1 \u2192 c) :=\n\u27e8\u03bb h, \u27e8\u03bb ha, (h ha).left, \u03bb ha, (h ha).right\u27e9,\n \u03bb h ha, \u27e8h.left ha, h.right ha\u27e9\u27e9\n\n@[simp] theorem and_imp : (a \u2227 b \u2192 c) \u2194 (a \u2192 b \u2192 c) :=\niff.intro (\u03bb h ha hb, h \u27e8ha, hb\u27e9) (\u03bb h \u27e8ha, hb\u27e9, h ha hb)\n\ntheorem iff_def : (a \u2194 b) \u2194 (a \u2192 b) \u2227 (b \u2192 a) :=\niff_iff_implies_and_implies _ _\n\ntheorem iff_def' : (a \u2194 b) \u2194 (b \u2192 a) \u2227 (a \u2192 b) :=\niff_def.trans and.comm\n\ntheorem imp_true_iff {\u03b1 : Sort*} : (\u03b1 \u2192 true) \u2194 true :=\niff_true_intro $ \u03bb_, trivial\n\ntheorem imp_iff_right (ha : a) : (a \u2192 b) \u2194 b :=\n\u27e8\u03bbf, f ha, imp_intro\u27e9\n\nlemma imp_iff_not (hb : \u00ac b) : a \u2192 b \u2194 \u00ac a := imp_congr_right $ \u03bb _, iff_false_intro hb\n\ntheorem decidable.imp_iff_right_iff [decidable a] : ((a \u2192 b) \u2194 b) \u2194 (a \u2228 b) :=\n\u27e8\u03bb H, (decidable.em a).imp_right $ \u03bb ha', H.1 $ \u03bb ha, (ha' ha).elim,\n  \u03bb H, H.elim imp_iff_right $ \u03bb hb, \u27e8\u03bb hab, hb, \u03bb _ _, hb\u27e9\u27e9\n\n@[simp] theorem imp_iff_right_iff : ((a \u2192 b) \u2194 b) \u2194 (a \u2228 b) :=\ndecidable.imp_iff_right_iff\n\nlemma decidable.and_or_imp [decidable a] : (a \u2227 b) \u2228 (a \u2192 c) \u2194 a \u2192 (b \u2228 c) :=\nif ha : a then by simp only [ha, true_and, true_implies_iff]\n          else by simp only [ha, false_or, false_and, false_implies_iff]\n\n@[simp] theorem and_or_imp : (a \u2227 b) \u2228 (a \u2192 c) \u2194 a \u2192 (b \u2228 c) :=\ndecidable.and_or_imp\n\n/-! ### Declarations about `not` -/\n\n/-- Ex falso for negation. From `\u00ac a` and `a` anything follows. This is the same as `absurd` with\nthe arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/\ndef not.elim {\u03b1 : Sort*} (H1 : \u00aca) (H2 : a) : \u03b1 := absurd H2 H1\n\n@[reducible] theorem not.imp {a b : Prop} (H2 : \u00acb) (H1 : a \u2192 b) : \u00aca := mt H1 H2\n\ntheorem not_not_of_not_imp : \u00ac(a \u2192 b) \u2192 \u00ac\u00aca :=\nmt not.elim\n\ntheorem not_of_not_imp {a : Prop} : \u00ac(a \u2192 b) \u2192 \u00acb :=\nmt imp_intro\n\ntheorem dec_em (p : Prop) [decidable p] : p \u2228 \u00acp := decidable.em p\n\ntheorem dec_em' (p : Prop) [decidable p] : \u00acp \u2228 p := (dec_em p).swap\n\ntheorem em (p : Prop) : p \u2228 \u00acp := classical.em _\n\ntheorem em' (p : Prop) : \u00acp \u2228 p := (em p).swap\n\ntheorem or_not {p : Prop} : p \u2228 \u00acp := em _\n\nsection eq_or_ne\n\nvariables {\u03b1 : Sort*} (x y : \u03b1)\n\ntheorem decidable.eq_or_ne [decidable (x = y)] : x = y \u2228 x \u2260 y := dec_em $ x = y\n\ntheorem decidable.ne_or_eq [decidable (x = y)] : x \u2260 y \u2228 x = y := dec_em' $ x = y\n\ntheorem eq_or_ne : x = y \u2228 x \u2260 y := em $ x = y\n\ntheorem ne_or_eq : x \u2260 y \u2228 x = y := em' $ x = y\n\nend eq_or_ne\n\ntheorem by_contradiction {p} : (\u00acp \u2192 false) \u2192 p := decidable.by_contradiction\n\n-- alias by_contradiction \u2190 by_contra\ntheorem by_contra {p} : (\u00acp \u2192 false) \u2192 p := decidable.by_contradiction\n\n/--\nIn most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely.\nThe `decidable` namespace contains versions of lemmas from the root namespace that explicitly\nattempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs.\n\nYou can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if\n`classical.choice` appears in the list.\n-/\nlibrary_note \"decidable namespace\"\n\n/--\nAs mathlib is primarily classical,\nif the type signature of a `def` or `lemma` does not require any `decidable` instances to state,\nit is preferable not to introduce any `decidable` instances that are needed in the proof\nas arguments, but rather to use the `classical` tactic as needed.\n\nIn the other direction, when `decidable` instances do appear in the type signature,\nit is better to use explicitly introduced ones rather than allowing Lean to automatically infer\nclassical ones, as these may cause instance mismatch errors later.\n-/\nlibrary_note \"decidable arguments\"\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_not [decidable a] : \u00ac\u00aca \u2194 a :=\niff.intro decidable.by_contradiction not_not_intro\n\n/-- The Double Negation Theorem: `\u00ac \u00ac P` is equivalent to `P`.\nThe left-to-right direction, double negation elimination (DNE),\nis classically true but not constructively. -/\n@[simp] theorem not_not : \u00ac\u00aca \u2194 a := decidable.not_not\n\ntheorem of_not_not : \u00ac\u00aca \u2192 a := by_contra\n\nlemma not_ne_iff {\u03b1 : Sort*} {a b : \u03b1} : \u00ac a \u2260 b \u2194 a = b := not_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.of_not_imp [decidable a] (h : \u00ac (a \u2192 b)) : a :=\ndecidable.by_contradiction (not_not_of_not_imp h)\n\ntheorem of_not_imp : \u00ac (a \u2192 b) \u2192 a := decidable.of_not_imp\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_symm [decidable a] (h : \u00aca \u2192 b) (hb : \u00acb) : a :=\ndecidable.by_contradiction $ hb \u2218 h\n\ntheorem not.decidable_imp_symm [decidable a] : (\u00aca \u2192 b) \u2192 \u00acb \u2192 a := decidable.not_imp_symm\n\ntheorem not.imp_symm : (\u00aca \u2192 b) \u2192 \u00acb \u2192 a := not.decidable_imp_symm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_comm [decidable a] [decidable b] : (\u00aca \u2192 b) \u2194 (\u00acb \u2192 a) :=\n\u27e8not.decidable_imp_symm, not.decidable_imp_symm\u27e9\n\ntheorem not_imp_comm : (\u00aca \u2192 b) \u2194 (\u00acb \u2192 a) := decidable.not_imp_comm\n\n@[simp] theorem imp_not_self : (a \u2192 \u00aca) \u2194 \u00aca := \u27e8\u03bb h ha, h ha ha, \u03bb h _, h\u27e9\n\ntheorem decidable.not_imp_self [decidable a] : (\u00aca \u2192 a) \u2194 a :=\nby { have := @imp_not_self (\u00aca), rwa decidable.not_not at this }\n\n@[simp] theorem not_imp_self : (\u00aca \u2192 a) \u2194 a := decidable.not_imp_self\n\ntheorem imp.swap : (a \u2192 b \u2192 c) \u2194 (b \u2192 a \u2192 c) :=\n\u27e8swap, swap\u27e9\n\ntheorem imp_not_comm : (a \u2192 \u00acb) \u2194 (b \u2192 \u00aca) :=\nimp.swap\n\nlemma iff.not (h : a \u2194 b) : \u00ac a \u2194 \u00ac b := not_congr h\nlemma iff.not_left (h : a \u2194 \u00ac b) : \u00ac a \u2194 b := h.not.trans not_not\nlemma iff.not_right (h : \u00ac a \u2194 b) : a \u2194 \u00ac b := not_not.symm.trans h.not\n\n/-! ### Declarations about `xor` -/\n\n@[simp] theorem xor_true : xor true = not := funext $ \u03bb a, by simp [xor]\n\n@[simp] theorem xor_false : xor false = id := funext $ \u03bb a, by simp [xor]\n\ntheorem xor_comm (a b) : xor a b = xor b a := by simp [xor, and_comm, or_comm]\n\ninstance : is_commutative Prop xor := \u27e8xor_comm\u27e9\n\n@[simp] theorem xor_self (a : Prop) : xor a a = false := by simp [xor]\n\n/-! ### Declarations about `and` -/\n\nlemma iff.and (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : a \u2227 c \u2194 b \u2227 d := and_congr h\u2081 h\u2082\n\ntheorem and_congr_left (h : c \u2192 (a \u2194 b)) : a \u2227 c \u2194 b \u2227 c :=\nand.comm.trans $ (and_congr_right h).trans and.comm\n\ntheorem and_congr_left' (h : a \u2194 b) : a \u2227 c \u2194 b \u2227 c := h.and iff.rfl\n\ntheorem and_congr_right' (h : b \u2194 c) : a \u2227 b \u2194 a \u2227 c := iff.rfl.and h\n\ntheorem not_and_of_not_left (b : Prop) : \u00aca \u2192 \u00ac(a \u2227 b) :=\nmt and.left\n\ntheorem not_and_of_not_right (a : Prop) {b : Prop} : \u00acb \u2192 \u00ac(a \u2227 b) :=\nmt and.right\n\ntheorem and.imp_left (h : a \u2192 b) : a \u2227 c \u2192 b \u2227 c :=\nand.imp h id\n\ntheorem and.imp_right (h : a \u2192 b) : c \u2227 a \u2192 c \u2227 b :=\nand.imp id h\n\nlemma and.right_comm : (a \u2227 b) \u2227 c \u2194 (a \u2227 c) \u2227 b :=\nby simp only [and.left_comm, and.comm]\n\nlemma and_and_and_comm (a b c d : Prop) : (a \u2227 b) \u2227 c \u2227 d \u2194 (a \u2227 c) \u2227 b \u2227 d :=\nby rw [\u2190and_assoc, @and.right_comm a, and_assoc]\n\nlemma and_and_distrib_left (a b c : Prop) : a \u2227 (b \u2227 c) \u2194 (a \u2227 b) \u2227 (a \u2227 c) :=\nby rw [and_and_and_comm, and_self]\n\nlemma and_and_distrib_right (a b c : Prop) : (a \u2227 b) \u2227 c \u2194 (a \u2227 c) \u2227 (b \u2227 c) :=\nby rw [and_and_and_comm, and_self]\n\nlemma and_rotate : a \u2227 b \u2227 c \u2194 b \u2227 c \u2227 a := by simp only [and.left_comm, and.comm]\nlemma and.rotate : a \u2227 b \u2227 c \u2192 b \u2227 c \u2227 a := and_rotate.1\n\ntheorem and_not_self_iff (a : Prop) : a \u2227 \u00ac a \u2194 false :=\niff.intro (assume h, (h.right) (h.left)) (assume h, h.elim)\n\ntheorem not_and_self_iff (a : Prop) : \u00ac a \u2227 a \u2194 false :=\niff.intro (assume \u27e8hna, ha\u27e9, hna ha) false.elim\n\ntheorem and_iff_left_of_imp {a b : Prop} (h : a \u2192 b) : (a \u2227 b) \u2194 a :=\niff.intro and.left (\u03bb ha, \u27e8ha, h ha\u27e9)\n\ntheorem and_iff_right_of_imp {a b : Prop} (h : b \u2192 a) : (a \u2227 b) \u2194 b :=\niff.intro and.right (\u03bb hb, \u27e8h hb, hb\u27e9)\n\n@[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a \u2227 b) \u2194 a) \u2194 (a \u2192 b) :=\n\u27e8\u03bb h ha, (h.2 ha).2, and_iff_left_of_imp\u27e9\n\n@[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a \u2227 b) \u2194 b) \u2194 (b \u2192 a) :=\n\u27e8\u03bb h ha, (h.2 ha).1, and_iff_right_of_imp\u27e9\n\n@[simp] lemma iff_self_and {p q : Prop} : (p \u2194 p \u2227 q) \u2194 (p \u2192 q) :=\nby rw [@iff.comm p, and_iff_left_iff_imp]\n\n@[simp] lemma iff_and_self {p q : Prop} : (p \u2194 q \u2227 p) \u2194 (p \u2192 q) :=\nby rw [and_comm, iff_self_and]\n\n@[simp] lemma and.congr_right_iff : (a \u2227 b \u2194 a \u2227 c) \u2194 (a \u2192 (b \u2194 c)) :=\n\u27e8\u03bb h ha, by simp [ha] at h; exact h, and_congr_right\u27e9\n\n@[simp] lemma and.congr_left_iff : (a \u2227 c \u2194 b \u2227 c) \u2194 c \u2192 (a \u2194 b) :=\nby simp only [and.comm, \u2190 and.congr_right_iff]\n\n@[simp] lemma and_self_left : a \u2227 a \u2227 b \u2194 a \u2227 b :=\n\u27e8\u03bb h, \u27e8h.1, h.2.2\u27e9, \u03bb h, \u27e8h.1, h.1, h.2\u27e9\u27e9\n\n@[simp] lemma and_self_right : (a \u2227 b) \u2227 b \u2194 a \u2227 b :=\n\u27e8\u03bb h, \u27e8h.1.1, h.2\u27e9, \u03bb h, \u27e8\u27e8h.1, h.2\u27e9, h.2\u27e9\u27e9\n\n/-! ### Declarations about `or` -/\n\nlemma iff.or (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : a \u2228 c \u2194 b \u2228 d := or_congr h\u2081 h\u2082\n\nlemma or_congr_left' (h : a \u2194 b) : a \u2228 c \u2194 b \u2228 c := h.or iff.rfl\nlemma or_congr_right' (h : b \u2194 c) : a \u2228 b \u2194 a \u2228 c := iff.rfl.or h\n\ntheorem or.right_comm : (a \u2228 b) \u2228 c \u2194 (a \u2228 c) \u2228 b := by rw [or_assoc, or_assoc, or_comm b]\n\nlemma or_or_or_comm (a b c d : Prop) : (a \u2228 b) \u2228 c \u2228 d \u2194 (a \u2228 c) \u2228 b \u2228 d :=\nby rw [\u2190or_assoc, @or.right_comm a, or_assoc]\n\nlemma or_or_distrib_left (a b c : Prop) : a \u2228 (b \u2228 c) \u2194 (a \u2228 b) \u2228 (a \u2228 c) :=\nby rw [or_or_or_comm, or_self]\n\nlemma or_or_distrib_right (a b c : Prop) : (a \u2228 b) \u2228 c \u2194 (a \u2228 c) \u2228 (b \u2228 c) :=\nby rw [or_or_or_comm, or_self]\n\nlemma or_rotate : a \u2228 b \u2228 c \u2194 b \u2228 c \u2228 a := by simp only [or.left_comm, or.comm]\nlemma or.rotate : a \u2228 b \u2228 c \u2192 b \u2228 c \u2228 a := or_rotate.1\n\ntheorem or_of_or_of_imp_of_imp (h\u2081 : a \u2228 b) (h\u2082 : a \u2192 c) (h\u2083 : b \u2192 d) : c \u2228 d :=\nor.imp h\u2082 h\u2083 h\u2081\n\ntheorem or_of_or_of_imp_left (h\u2081 : a \u2228 c) (h : a \u2192 b) : b \u2228 c :=\nor.imp_left h h\u2081\n\ntheorem or_of_or_of_imp_right (h\u2081 : c \u2228 a) (h : a \u2192 b) : c \u2228 b :=\nor.imp_right h h\u2081\n\ntheorem or.elim3 (h : a \u2228 b \u2228 c) (ha : a \u2192 d) (hb : b \u2192 d) (hc : c \u2192 d) : d :=\nor.elim h ha (assume h\u2082, or.elim h\u2082 hb hc)\n\nlemma or.imp3 (had : a \u2192 d) (hbe : b \u2192 e) (hcf : c \u2192 f) : a \u2228 b \u2228 c \u2192 d \u2228 e \u2228 f :=\nor.imp had $ or.imp hbe hcf\n\ntheorem or_imp_distrib : (a \u2228 b \u2192 c) \u2194 (a \u2192 c) \u2227 (b \u2192 c) :=\n\u27e8assume h, \u27e8assume ha, h (or.inl ha), assume hb, h (or.inr hb)\u27e9,\n  assume \u27e8ha, hb\u27e9, or.rec ha hb\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_imp_left [decidable a] : a \u2228 b \u2194 (\u00ac a \u2192 b) :=\n\u27e8or.resolve_left, \u03bb h, dite _ or.inl (or.inr \u2218 h)\u27e9\n\ntheorem or_iff_not_imp_left : a \u2228 b \u2194 (\u00ac a \u2192 b) := decidable.or_iff_not_imp_left\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_imp_right [decidable b] : a \u2228 b \u2194 (\u00ac b \u2192 a) :=\nor.comm.trans decidable.or_iff_not_imp_left\n\ntheorem or_iff_not_imp_right : a \u2228 b \u2194 (\u00ac b \u2192 a) := decidable.or_iff_not_imp_right\n\n-- See Note [decidable namespace]\nprotected lemma decidable.not_or_of_imp [decidable a] (h : a \u2192 b) : \u00ac a \u2228 b :=\ndite _ (or.inr \u2218 h) or.inl\n\nlemma not_or_of_imp : (a \u2192 b) \u2192 \u00ac a \u2228 b := decidable.not_or_of_imp\n\n-- See Note [decidable namespace]\nprotected lemma decidable.or_not_of_imp [decidable a] (h : a \u2192 b) : b \u2228 \u00ac a :=\ndite _ (or.inl \u2218 h) or.inr\n\nlemma or_not_of_imp : (a \u2192 b) \u2192 b \u2228 \u00ac a := decidable.or_not_of_imp\n\n-- See Note [decidable namespace]\nprotected lemma decidable.imp_iff_not_or [decidable a] : a \u2192 b \u2194 \u00ac a \u2228 b :=\n\u27e8decidable.not_or_of_imp, or.neg_resolve_left\u27e9\n\nlemma imp_iff_not_or : a \u2192 b \u2194 \u00ac a \u2228 b := decidable.imp_iff_not_or\n\n-- See Note [decidable namespace]\nprotected lemma decidable.imp_iff_or_not [decidable b] : b \u2192 a \u2194 a \u2228 \u00ac b :=\ndecidable.imp_iff_not_or.trans or.comm\n\nlemma imp_iff_or_not : b \u2192 a \u2194 a \u2228 \u00ac b  := decidable.imp_iff_or_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_not [decidable a] : (\u00ac a \u2192 \u00ac b) \u2194 (b \u2192 a) :=\n\u27e8assume h hb, decidable.by_contradiction $ assume na, h na hb, mt\u27e9\n\ntheorem not_imp_not : (\u00ac a \u2192 \u00ac b) \u2194 (b \u2192 a) := decidable.not_imp_not\n\n-- See Note [decidable namespace]\nprotected lemma decidable.or_congr_left [decidable c] (h : \u00ac c \u2192 (a \u2194 b)) : a \u2228 c \u2194 b \u2228 c :=\nby { rw [decidable.or_iff_not_imp_right, decidable.or_iff_not_imp_right], exact imp_congr_right h }\n\nlemma or_congr_left (h : \u00ac c \u2192 (a \u2194 b)) : a \u2228 c \u2194 b \u2228 c :=\ndecidable.or_congr_left h\n\n-- See Note [decidable namespace]\nprotected lemma decidable.or_congr_right [decidable a] (h : \u00ac a \u2192 (b \u2194 c)) : a \u2228 b \u2194 a \u2228 c :=\nby { rw [decidable.or_iff_not_imp_left, decidable.or_iff_not_imp_left], exact imp_congr_right h }\n\nlemma or_congr_right (h : \u00ac a \u2192 (b \u2194 c)) : a \u2228 b \u2194 a \u2228 c :=\ndecidable.or_congr_right h\n\n@[simp] theorem or_iff_left_iff_imp : (a \u2228 b \u2194 a) \u2194 (b \u2192 a) :=\n\u27e8\u03bb h hb, h.1 (or.inr hb), or_iff_left_of_imp\u27e9\n\n@[simp] theorem or_iff_right_iff_imp : (a \u2228 b \u2194 b) \u2194 (a \u2192 b) :=\nby rw [or_comm, or_iff_left_iff_imp]\n\nlemma or_iff_left (hb : \u00ac b) : a \u2228 b \u2194 a := \u27e8\u03bb h, h.resolve_right hb, or.inl\u27e9\nlemma or_iff_right (ha : \u00ac a) : a \u2228 b \u2194 b := \u27e8\u03bb h, h.resolve_left ha, or.inr\u27e9\n\n/-! ### Declarations about distributivity -/\n\n/-- `\u2227` distributes over `\u2228` (on the left). -/\ntheorem and_or_distrib_left : a \u2227 (b \u2228 c) \u2194 (a \u2227 b) \u2228 (a \u2227 c) :=\n\u27e8\u03bb \u27e8ha, hbc\u27e9, hbc.imp (and.intro ha) (and.intro ha),\n or.rec (and.imp_right or.inl) (and.imp_right or.inr)\u27e9\n\n/-- `\u2227` distributes over `\u2228` (on the right). -/\ntheorem or_and_distrib_right : (a \u2228 b) \u2227 c \u2194 (a \u2227 c) \u2228 (b \u2227 c) :=\n(and.comm.trans and_or_distrib_left).trans (and.comm.or and.comm)\n\n/-- `\u2228` distributes over `\u2227` (on the left). -/\ntheorem or_and_distrib_left : a \u2228 (b \u2227 c) \u2194 (a \u2228 b) \u2227 (a \u2228 c) :=\n\u27e8or.rec (\u03bbha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr),\n and.rec $ or.rec (imp_intro \u2218 or.inl) (or.imp_right \u2218 and.intro)\u27e9\n\n/-- `\u2228` distributes over `\u2227` (on the right). -/\ntheorem and_or_distrib_right : (a \u2227 b) \u2228 c \u2194 (a \u2228 c) \u2227 (b \u2228 c) :=\n(or.comm.trans or_and_distrib_left).trans (or.comm.and or.comm)\n\n@[simp] lemma or_self_left : a \u2228 a \u2228 b \u2194 a \u2228 b :=\n\u27e8\u03bb h, h.elim or.inl id, \u03bb h, h.elim or.inl (or.inr \u2218 or.inr)\u27e9\n\n@[simp] lemma or_self_right : (a \u2228 b) \u2228 b \u2194 a \u2228 b :=\n\u27e8\u03bb h, h.elim id or.inr, \u03bb h, h.elim (or.inl \u2218 or.inl) or.inr\u27e9\n\n/-! Declarations about `iff` -/\n\nlemma iff.iff (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : (a \u2194 c) \u2194 (b \u2194 d) := iff_congr h\u2081 h\u2082\n\ntheorem iff_of_true (ha : a) (hb : b) : a \u2194 b :=\n\u27e8\u03bb_, hb, \u03bb _, ha\u27e9\n\ntheorem iff_of_false (ha : \u00aca) (hb : \u00acb) : a \u2194 b :=\n\u27e8ha.elim, hb.elim\u27e9\n\ntheorem iff_true_left (ha : a) : (a \u2194 b) \u2194 b :=\n\u27e8\u03bb h, h.1 ha, iff_of_true ha\u27e9\n\ntheorem iff_true_right (ha : a) : (b \u2194 a) \u2194 b :=\niff.comm.trans (iff_true_left ha)\n\ntheorem iff_false_left (ha : \u00aca) : (a \u2194 b) \u2194 \u00acb :=\n\u27e8\u03bb h, mt h.2 ha, iff_of_false ha\u27e9\n\ntheorem iff_false_right (ha : \u00aca) : (b \u2194 a) \u2194 \u00acb :=\niff.comm.trans (iff_false_left ha)\n\n@[simp]\nlemma iff_mpr_iff_true_intro {P : Prop} (h : P) : iff.mpr (iff_true_intro h) true.intro = h := rfl\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_or_distrib [decidable a] : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) :=\nby simp [decidable.imp_iff_not_or, or.comm, or.left_comm]\n\ntheorem imp_or_distrib : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) := decidable.imp_or_distrib\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_or_distrib' [decidable b] : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) :=\nby by_cases b; simp [h, or_iff_right_of_imp ((\u2218) false.elim)]\n\ntheorem imp_or_distrib' : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) := decidable.imp_or_distrib'\n\ntheorem not_imp_of_and_not : a \u2227 \u00ac b \u2192 \u00ac (a \u2192 b)\n| \u27e8ha, hb\u27e9 h := hb $ h ha\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp [decidable a] : \u00ac(a \u2192 b) \u2194 a \u2227 \u00acb :=\n\u27e8\u03bb h, \u27e8decidable.of_not_imp h, not_of_not_imp h\u27e9, not_imp_of_and_not\u27e9\n\ntheorem not_imp : \u00ac(a \u2192 b) \u2194 a \u2227 \u00acb := decidable.not_imp\n\n-- for monotonicity\nlemma imp_imp_imp (h\u2080 : c \u2192 a) (h\u2081 : b \u2192 d) : (a \u2192 b) \u2192 (c \u2192 d) :=\nassume (h\u2082 : a \u2192 b), h\u2081 \u2218 h\u2082 \u2218 h\u2080\n\n-- See Note [decidable namespace]\nprotected theorem decidable.peirce (a b : Prop) [decidable a] : ((a \u2192 b) \u2192 a) \u2192 a :=\nif ha : a then \u03bb h, ha else \u03bb h, h ha.elim\n\ntheorem peirce (a b : Prop) : ((a \u2192 b) \u2192 a) \u2192 a := decidable.peirce _ _\n\ntheorem peirce' {a : Prop} (H : \u2200 b : Prop, (a \u2192 b) \u2192 a) : a := H _ id\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff_not [decidable a] [decidable b] : (\u00ac a \u2194 \u00ac b) \u2194 (a \u2194 b) :=\nby rw [@iff_def (\u00ac a), @iff_def' a]; exact decidable.not_imp_not.and decidable.not_imp_not\n\ntheorem not_iff_not : (\u00ac a \u2194 \u00ac b) \u2194 (a \u2194 b) := decidable.not_iff_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff_comm [decidable a] [decidable b] : (\u00ac a \u2194 b) \u2194 (\u00ac b \u2194 a) :=\nby rw [@iff_def (\u00ac a), @iff_def (\u00ac b)]; exact decidable.not_imp_comm.and imp_not_comm\n\ntheorem not_iff_comm : (\u00ac a \u2194 b) \u2194 (\u00ac b \u2194 a) := decidable.not_iff_comm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff : \u2200 [decidable b], \u00ac (a \u2194 b) \u2194 (\u00ac a \u2194 b) :=\nby intro h; cases h; simp only [h, iff_true, iff_false]\n\ntheorem not_iff : \u00ac (a \u2194 b) \u2194 (\u00ac a \u2194 b) := decidable.not_iff\n\n-- See Note [decidable namespace]\nprotected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a \u2194 \u00ac b) \u2194 (b \u2194 \u00ac a) :=\nby rw [@iff_def a, @iff_def b]; exact imp_not_comm.and decidable.not_imp_comm\n\ntheorem iff_not_comm : (a \u2194 \u00ac b) \u2194 (b \u2194 \u00ac a) := decidable.iff_not_comm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.iff_iff_and_or_not_and_not [decidable b] :\n  (a \u2194 b) \u2194 (a \u2227 b) \u2228 (\u00ac a \u2227 \u00ac b) :=\nby { split; intro h,\n     { rw h; by_cases b; [left,right]; split; assumption },\n     { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } }\n\ntheorem iff_iff_and_or_not_and_not : (a \u2194 b) \u2194 (a \u2227 b) \u2228 (\u00ac a \u2227 \u00ac b) :=\ndecidable.iff_iff_and_or_not_and_not\n\nlemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] :\n  (a \u2194 b) \u2194 ((\u00aca \u2228 b) \u2227 (a \u2228 \u00acb)) :=\nbegin\n  rw [iff_iff_implies_and_implies a b],\n  simp only [decidable.imp_iff_not_or, or.comm]\nend\n\nlemma iff_iff_not_or_and_or_not : (a \u2194 b) \u2194 ((\u00aca \u2228 b) \u2227 (a \u2228 \u00acb)) :=\ndecidable.iff_iff_not_or_and_or_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_not_right [decidable b] : \u00ac(a \u2227 \u00acb) \u2194 (a \u2192 b) :=\n\u27e8\u03bb h ha, h.decidable_imp_symm $ and.intro ha, \u03bb h \u27e8ha, hb\u27e9, hb $ h ha\u27e9\n\ntheorem not_and_not_right : \u00ac(a \u2227 \u00acb) \u2194 (a \u2192 b) := decidable.not_and_not_right\n\n/-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent.\n**Important**: this function should be used instead of `rw` on `decidable b`, because the\nkernel will get stuck reducing the usage of `propext` otherwise,\nand `dec_trivial` will not work. -/\n@[inline] def decidable_of_iff (a : Prop) (h : a \u2194 b) [D : decidable a] : decidable b :=\ndecidable_of_decidable_of_iff D h\n\n/-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent.\nThis is the same as `decidable_of_iff` but the iff is flipped. -/\n@[inline] def decidable_of_iff' (b : Prop) (h : a \u2194 b) [D : decidable b] : decidable a :=\ndecidable_of_decidable_of_iff D h.symm\n\n/-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b \u2194 a`.\n(This is sometimes taken as an alternate definition of decidability.) -/\ndef decidable_of_bool : \u2200 (b : bool) (h : b \u2194 a), decidable a\n| tt h := is_true (h.1 rfl)\n| ff h := is_false (mt h.2 bool.ff_ne_tt)\n\n/-! ### De Morgan's laws -/\n\ntheorem not_and_of_not_or_not (h : \u00ac a \u2228 \u00ac b) : \u00ac (a \u2227 b)\n| \u27e8ha, hb\u27e9 := or.elim h (absurd ha) (absurd hb)\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_distrib [decidable a] : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb :=\n\u27e8\u03bb h, if ha : a then or.inr (\u03bb hb, h \u27e8ha, hb\u27e9) else or.inl ha, not_and_of_not_or_not\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_distrib' [decidable b] : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb :=\n\u27e8\u03bb h, if hb : b then or.inl (\u03bb ha, h \u27e8ha, hb\u27e9) else or.inr hb, not_and_of_not_or_not\u27e9\n\n/-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the\ndisjunction of the negations. -/\ntheorem not_and_distrib : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb := decidable.not_and_distrib\n\n@[simp] theorem not_and : \u00ac (a \u2227 b) \u2194 (a \u2192 \u00ac b) := and_imp\n\ntheorem not_and' : \u00ac (a \u2227 b) \u2194 b \u2192 \u00aca :=\nnot_and.trans imp_not_comm\n\n/-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the\nconjunction of the negations. -/\ntheorem not_or_distrib : \u00ac (a \u2228 b) \u2194 \u00ac a \u2227 \u00ac b :=\n\u27e8\u03bb h, \u27e8\u03bb ha, h (or.inl ha), \u03bb hb, h (or.inr hb)\u27e9,\n \u03bb \u27e8h\u2081, h\u2082\u27e9 h, or.elim h h\u2081 h\u2082\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a \u2228 b \u2194 \u00ac (\u00aca \u2227 \u00acb) :=\nby rw [\u2190 not_or_distrib, decidable.not_not]\n\ntheorem or_iff_not_and_not : a \u2228 b \u2194 \u00ac (\u00aca \u2227 \u00acb) := decidable.or_iff_not_and_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] :\n  a \u2227 b \u2194 \u00ac (\u00ac a \u2228 \u00ac b) :=\nby rw [\u2190 decidable.not_and_distrib, decidable.not_not]\n\ntheorem and_iff_not_or_not : a \u2227 b \u2194 \u00ac (\u00ac a \u2228 \u00ac b) := decidable.and_iff_not_or_not\n\n@[simp] theorem not_xor (P Q : Prop) : \u00ac xor P Q \u2194 (P \u2194 Q) :=\nby simp only [not_and, xor, not_or_distrib, not_not, \u2190 iff_iff_implies_and_implies]\n\ntheorem xor_iff_not_iff (P Q : Prop) : xor P Q \u2194 \u00ac (P \u2194 Q) :=\nby rw [iff_not_comm, not_xor]\n\n\nend propositional\n\n/-! ### Declarations about equality -/\n\nsection mem\nvariables {\u03b1 \u03b2 : Type*} [has_mem \u03b1 \u03b2] {s t : \u03b2} {a b : \u03b1}\n\nlemma ne_of_mem_of_not_mem (h : a \u2208 s) : b \u2209 s \u2192 a \u2260 b := mt $ \u03bb e, e \u25b8 h\nlemma ne_of_mem_of_not_mem' (h : a \u2208 s) : a \u2209 t \u2192 s \u2260 t := mt $ \u03bb e, e \u25b8 h\n\n/-- **Alias** of `ne_of_mem_of_not_mem`. -/\nlemma has_mem.mem.ne_of_not_mem : a \u2208 s \u2192 b \u2209 s \u2192 a \u2260 b := ne_of_mem_of_not_mem\n/-- **Alias** of `ne_of_mem_of_not_mem'`. -/\nlemma has_mem.mem.ne_of_not_mem' : a \u2208 s \u2192 a \u2209 t \u2192 s \u2260 t := ne_of_mem_of_not_mem'\n\nend mem\n\nsection equality\nvariables {\u03b1 : Sort*} {a b : \u03b1}\n\n@[simp] theorem heq_iff_eq : a == b \u2194 a = b :=\n\u27e8eq_of_heq, heq_of_eq\u27e9\n\ntheorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq :=\nhave p = q, from propext \u27e8\u03bb _, hq, \u03bb _, hp\u27e9,\nby subst q; refl\n\n-- todo: change name\nlemma ball_cond_comm {\u03b1} {s : \u03b1 \u2192 Prop} {p : \u03b1 \u2192 \u03b1 \u2192 Prop} :\n  (\u2200 a, s a \u2192 \u2200 b, s b \u2192 p a b) \u2194 (\u2200 a b, s a \u2192 s b \u2192 p a b) :=\n\u27e8\u03bb h a b ha hb, h a ha b hb, \u03bb h a ha b hb, h a b ha hb\u27e9\n\nlemma ball_mem_comm {\u03b1 \u03b2} [has_mem \u03b1 \u03b2] {s : \u03b2} {p : \u03b1 \u2192 \u03b1 \u2192 Prop} :\n  (\u2200 a b \u2208 s, p a b) \u2194 (\u2200 a b, a \u2208 s \u2192 b \u2208 s \u2192 p a b) :=\nball_cond_comm\n\nlemma ne_of_apply_ne {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) {x y : \u03b1} (h : f x \u2260 f y) : x \u2260 y :=\n\u03bb (w : x = y), h (congr_arg f w)\n\ntheorem eq_equivalence : equivalence (@eq \u03b1) :=\n\u27e8eq.refl, @eq.symm _, @eq.trans _\u27e9\n\n/-- Transport through trivial families is the identity. -/\n@[simp]\nlemma eq_rec_constant {\u03b1 : Sort*} {a a' : \u03b1} {\u03b2 : Sort*} (y : \u03b2) (h : a = a') :\n  (@eq.rec \u03b1 a (\u03bb a, \u03b2) y a' h) = y :=\nby { cases h, refl, }\n\n@[simp]\nlemma eq_mp_eq_cast {\u03b1 \u03b2 : Sort*} (h : \u03b1 = \u03b2) : eq.mp h = cast h := rfl\n\n@[simp]\nlemma eq_mpr_eq_cast {\u03b1 \u03b2 : Sort*} (h : \u03b1 = \u03b2) : eq.mpr h = cast h.symm := rfl\n\n@[simp]\nlemma cast_cast : \u2200 {\u03b1 \u03b2 \u03b3 : Sort*} (ha : \u03b1 = \u03b2) (hb : \u03b2 = \u03b3) (a : \u03b1),\n  cast hb (cast ha a) = cast (ha.trans hb) a\n| _ _ _ rfl rfl a := rfl\n\n@[simp] lemma congr_refl_left {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) {a b : \u03b1} (h : a = b) :\n  congr (eq.refl f) h = congr_arg f h :=\nrfl\n\n@[simp] lemma congr_refl_right {\u03b1 \u03b2 : Sort*} {f g : \u03b1 \u2192 \u03b2} (h : f = g) (a : \u03b1) :\n  congr h (eq.refl a) = congr_fun h a :=\nrfl\n\n@[simp] lemma congr_arg_refl {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (a : \u03b1) :\n  congr_arg f (eq.refl a) = eq.refl (f a) :=\nrfl\n\n@[simp] lemma congr_fun_rfl {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (a : \u03b1) :\n  congr_fun (eq.refl f) a = eq.refl (f a) :=\nrfl\n\n@[simp] lemma congr_fun_congr_arg {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) {a a' : \u03b1} (p : a = a') (b : \u03b2) :\n  congr_fun (congr_arg f p) b = congr_arg (\u03bb a, f a b) p :=\nrfl\n\nlemma heq_of_cast_eq :\n  \u2200 {\u03b1 \u03b2 : Sort*} {a : \u03b1} {a' : \u03b2} (e : \u03b1 = \u03b2) (h\u2082 : cast e a = a'), a == a'\n| \u03b1 ._ a a' rfl h := eq.rec_on h (heq.refl _)\n\nlemma cast_eq_iff_heq {\u03b1 \u03b2 : Sort*} {a : \u03b1} {a' : \u03b2} {e : \u03b1 = \u03b2} : cast e a = a' \u2194 a == a' :=\n\u27e8heq_of_cast_eq _, \u03bb h, by cases h; refl\u27e9\n\nlemma rec_heq_of_heq {\u03b2} {C : \u03b1 \u2192 Sort*} {x : C a} {y : \u03b2} (eq : a = b) (h : x == y) :\n  @eq.rec \u03b1 a C x b eq == y :=\nby subst eq; exact h\n\nprotected lemma eq.congr {x\u2081 x\u2082 y\u2081 y\u2082 : \u03b1} (h\u2081 : x\u2081 = y\u2081) (h\u2082 : x\u2082 = y\u2082) :\n  (x\u2081 = x\u2082) \u2194 (y\u2081 = y\u2082) :=\nby { subst h\u2081, subst h\u2082 }\n\nlemma eq.congr_left {x y z : \u03b1} (h : x = y) : x = z \u2194 y = z := by rw [h]\nlemma eq.congr_right {x y z : \u03b1} (h : x = y) : z = x \u2194 z = y := by rw [h]\n\nlemma congr_arg2 {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) {x x' : \u03b1} {y y' : \u03b2}\n  (hx : x = x') (hy : y = y') : f x y = f x' y' :=\nby { subst hx, subst hy }\n\nvariables {\u03b2 : \u03b1 \u2192 Sort*} {\u03b3 : \u03a0 a, \u03b2 a \u2192 Sort*} {\u03b4 : \u03a0 a b, \u03b3 a b \u2192 Sort*}\n\nlemma congr_fun\u2082 {f g : \u03a0 a b, \u03b3 a b} (h : f = g) (a : \u03b1) (b : \u03b2 a) : f a b = g a b :=\ncongr_fun (congr_fun h _) _\n\nlemma congr_fun\u2083 {f g : \u03a0 a b c, \u03b4 a b c} (h : f = g) (a : \u03b1) (b : \u03b2 a) (c : \u03b3 a b) :\n  f a b c = g a b c :=\ncongr_fun\u2082 (congr_fun h _) _ _\n\nlemma funext\u2082 {f g : \u03a0 a, \u03b2 a \u2192 Prop} (h : \u2200 a b, f a b = g a b) : f = g :=\nfunext $ \u03bb _, funext $ h _\n\nlemma funext\u2083 {f g : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, f a b c = g a b c) : f = g :=\nfunext $ \u03bb _, funext\u2082 $ h _\n\nend equality\n\n/-! ### Declarations about quantifiers -/\n\nsection quantifiers\nvariables {\u03b1 : Sort*}\n\nsection dependent\nvariables {\u03b2 : \u03b1 \u2192 Sort*} {\u03b3 : \u03a0 a, \u03b2 a \u2192 Sort*} {\u03b4 : \u03a0 a b, \u03b3 a b \u2192 Sort*}\n  {\u03b5 : \u03a0 a b c, \u03b4 a b c \u2192 Sort*}\n\nlemma pi_congr {\u03b2' : \u03b1 \u2192 Sort*} (h : \u2200 a, \u03b2 a = \u03b2' a) : (\u03a0 a, \u03b2 a) = \u03a0 a, \u03b2' a :=\n(funext h : \u03b2 = \u03b2') \u25b8 rfl\n\nlemma forall\u2082_congr {p q : \u03a0 a, \u03b2 a \u2192 Prop} (h : \u2200 a b, p a b \u2194 q a b) :\n  (\u2200 a b, p a b) \u2194 \u2200 a b, q a b :=\nforall_congr $ \u03bb a, forall_congr $ h a\n\nlemma forall\u2083_congr {p q : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, p a b c \u2194 q a b c) :\n  (\u2200 a b c, p a b c) \u2194 \u2200 a b c, q a b c :=\nforall_congr $ \u03bb a, forall\u2082_congr $ h a\n\nlemma forall\u2084_congr {p q : \u03a0 a b c, \u03b4 a b c \u2192 Prop} (h : \u2200 a b c d, p a b c d \u2194 q a b c d) :\n  (\u2200 a b c d, p a b c d) \u2194 \u2200 a b c d, q a b c d :=\nforall_congr $ \u03bb a, forall\u2083_congr $ h a\n\nlemma forall\u2085_congr {p q : \u03a0 a b c d, \u03b5 a b c d \u2192 Prop}\n  (h : \u2200 a b c d e, p a b c d e \u2194 q a b c d e) :\n  (\u2200 a b c d e, p a b c d e) \u2194 \u2200 a b c d e, q a b c d e :=\nforall_congr $ \u03bb a, forall\u2084_congr $ h a\n\nlemma exists\u2082_congr {p q : \u03a0 a, \u03b2 a \u2192 Prop} (h : \u2200 a b, p a b \u2194 q a b) :\n  (\u2203 a b, p a b) \u2194 \u2203 a b, q a b :=\nexists_congr $ \u03bb a, exists_congr $ h a\n\nlemma exists\u2083_congr {p q : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, p a b c \u2194 q a b c) :\n  (\u2203 a b c, p a b c) \u2194 \u2203 a b c, q a b c :=\nexists_congr $ \u03bb a, exists\u2082_congr $ h a\n\nlemma exists\u2084_congr {p q : \u03a0 a b c, \u03b4 a b c \u2192 Prop} (h : \u2200 a b c d, p a b c d \u2194 q a b c d) :\n  (\u2203 a b c d, p a b c d) \u2194 \u2203 a b c d, q a b c d :=\nexists_congr $ \u03bb a, exists\u2083_congr $ h a\n\nlemma exists\u2085_congr {p q : \u03a0 a b c d, \u03b5 a b c d \u2192 Prop}\n  (h : \u2200 a b c d e, p a b c d e \u2194 q a b c d e) :\n  (\u2203 a b c d e, p a b c d e) \u2194 \u2203 a b c d e, q a b c d e :=\nexists_congr $ \u03bb a, exists\u2084_congr $ h a\n\nlemma forall_imp {p q : \u03b1 \u2192 Prop} (h : \u2200 a, p a \u2192 q a) : (\u2200 a, p a) \u2192 \u2200 a, q a := \u03bb h' a, h a (h' a)\n\nlemma forall\u2082_imp {p q : \u03a0 a, \u03b2 a \u2192 Prop} (h : \u2200 a b, p a b \u2192 q a b) :\n  (\u2200 a b, p a b) \u2192 \u2200 a b, q a b :=\nforall_imp $ \u03bb i, forall_imp $ h i\n\nlemma forall\u2083_imp {p q : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, p a b c \u2192 q a b c) :\n  (\u2200 a b c, p a b c) \u2192 \u2200 a b c, q a b c :=\nforall_imp $ \u03bb a, forall\u2082_imp $ h a\n\nlemma Exists.imp {p q : \u03b1 \u2192 Prop}  (h : \u2200 a, (p a \u2192 q a)) : (\u2203 a, p a) \u2192 \u2203 a, q a :=\nexists_imp_exists h\n\nlemma Exists\u2082.imp {p q : \u03a0 a, \u03b2 a \u2192 Prop} (h : \u2200 a b, p a b \u2192 q a b) :\n  (\u2203 a b, p a b) \u2192 \u2203 a b, q a b :=\nExists.imp $ \u03bb a, Exists.imp $ h a\n\nlemma Exists\u2083.imp {p q : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, p a b c \u2192 q a b c) :\n  (\u2203 a b c, p a b c) \u2192 \u2203 a b c, q a b c :=\nExists.imp $ \u03bb a, Exists\u2082.imp $ h a\n\nend dependent\n\nvariables {\u03b9 \u03b2 : Sort*} {\u03ba : \u03b9 \u2192 Sort*} {p q : \u03b1 \u2192 Prop} {b : Prop}\n\nlemma exists_imp_exists' {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} (f : \u03b1 \u2192 \u03b2) (hpq : \u2200 a, p a \u2192 q (f a))\n  (hp : \u2203 a, p a) : \u2203 b, q b :=\nexists.elim hp (\u03bb a hp', \u27e8_, hpq _ hp'\u27e9)\n\ntheorem forall_swap {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2200 x y, p x y) \u2194 \u2200 y x, p x y :=\n\u27e8swap, swap\u27e9\n\nlemma forall\u2082_swap {\u03b9\u2081 \u03b9\u2082 : Sort*} {\u03ba\u2081 : \u03b9\u2081 \u2192 Sort*} {\u03ba\u2082 : \u03b9\u2082 \u2192 Sort*}\n  {p : \u03a0 i\u2081, \u03ba\u2081 i\u2081 \u2192 \u03a0 i\u2082, \u03ba\u2082 i\u2082 \u2192 Prop} :\n  (\u2200 i\u2081 j\u2081 i\u2082 j\u2082, p i\u2081 j\u2081 i\u2082 j\u2082) \u2194 \u2200 i\u2082 j\u2082 i\u2081 j\u2081, p i\u2081 j\u2081 i\u2082 j\u2082 :=\n\u27e8swap\u2082, swap\u2082\u27e9\n\n/-- We intentionally restrict the type of `\u03b1` in this lemma so that this is a safer to use in simp\nthan `forall_swap`. -/\nlemma imp_forall_iff {\u03b1 : Type*} {p : Prop} {q : \u03b1 \u2192 Prop} : (p \u2192 \u2200 x, q x) \u2194 (\u2200 x, p \u2192 q x) :=\nforall_swap\n\ntheorem exists_swap {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2203 x y, p x y) \u2194 \u2203 y x, p x y :=\n\u27e8\u03bb \u27e8x, y, h\u27e9, \u27e8y, x, h\u27e9, \u03bb \u27e8y, x, h\u27e9, \u27e8x, y, h\u27e9\u27e9\n\n@[simp] theorem forall_exists_index {q : (\u2203 x, p x) \u2192 Prop} :\n  (\u2200 h, q h) \u2194 \u2200 x (h : p x), q \u27e8x, h\u27e9 :=\n\u27e8\u03bb h x hpx, h \u27e8x, hpx\u27e9, \u03bb h \u27e8x, hpx\u27e9, h x hpx\u27e9\n\ntheorem exists_imp_distrib : ((\u2203 x, p x) \u2192 b) \u2194 \u2200 x, p x \u2192 b :=\nforall_exists_index\n\n/--\nExtract an element from a existential statement, using `classical.some`.\n-/\n-- This enables projection notation.\n@[reducible] noncomputable def Exists.some {p : \u03b1 \u2192 Prop} (P : \u2203 a, p a) : \u03b1 := classical.some P\n\n/--\nShow that an element extracted from `P : \u2203 a, p a` using `P.some` satisfies `p`.\n-/\nlemma Exists.some_spec {p : \u03b1 \u2192 Prop} (P : \u2203 a, p a) : p (P.some) := classical.some_spec P\n\n--theorem forall_not_of_not_exists (h : \u00ac \u2203 x, p x) : \u2200 x, \u00ac p x :=\n--forall_imp_of_exists_imp h\n\ntheorem not_exists_of_forall_not (h : \u2200 x, \u00ac p x) : \u00ac \u2203 x, p x :=\nexists_imp_distrib.2 h\n\n@[simp] theorem not_exists : (\u00ac \u2203 x, p x) \u2194 \u2200 x, \u00ac p x :=\nexists_imp_distrib\n\ntheorem not_forall_of_exists_not : (\u2203 x, \u00ac p x) \u2192 \u00ac \u2200 x, p x\n| \u27e8x, hn\u27e9 h := hn (h x)\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_forall {p : \u03b1 \u2192 Prop}\n  [decidable (\u2203 x, \u00ac p x)] [\u2200 x, decidable (p x)] : (\u00ac \u2200 x, p x) \u2194 \u2203 x, \u00ac p x :=\n\u27e8not.decidable_imp_symm $ \u03bb nx x, nx.decidable_imp_symm $ \u03bb h, \u27e8x, h\u27e9,\n not_forall_of_exists_not\u27e9\n\n@[simp] theorem not_forall {p : \u03b1 \u2192 Prop} : (\u00ac \u2200 x, p x) \u2194 \u2203 x, \u00ac p x := decidable.not_forall\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_forall_not [decidable (\u2203 x, p x)] :\n  (\u00ac \u2200 x, \u00ac p x) \u2194 \u2203 x, p x :=\n(@decidable.not_iff_comm _ _ _ (decidable_of_iff (\u00ac \u2203 x, p x) not_exists)).1 not_exists\n\ntheorem not_forall_not : (\u00ac \u2200 x, \u00ac p x) \u2194 \u2203 x, p x := decidable.not_forall_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_exists_not [\u2200 x, decidable (p x)] : (\u00ac \u2203 x, \u00ac p x) \u2194 \u2200 x, p x :=\nby simp [decidable.not_not]\n\n@[simp] theorem not_exists_not : (\u00ac \u2203 x, \u00ac p x) \u2194 \u2200 x, p x := decidable.not_exists_not\n\ntheorem forall_imp_iff_exists_imp [ha : nonempty \u03b1] : ((\u2200 x, p x) \u2192 b) \u2194 \u2203 x, p x \u2192 b :=\nlet \u27e8a\u27e9 := ha in\n\u27e8\u03bb h, not_forall_not.1 $ \u03bb h', classical.by_cases (\u03bb hb : b, h' a $ \u03bb _, hb)\n  (\u03bb hb, hb $ h $ \u03bb x, (not_imp.1 (h' x)).1), \u03bb \u27e8x, hx\u27e9 h, hx (h x)\u27e9\n\n-- TODO: duplicate of a lemma in core\ntheorem forall_true_iff : (\u03b1 \u2192 true) \u2194 true :=\nimplies_true_iff \u03b1\n\n-- Unfortunately this causes simp to loop sometimes, so we\n-- add the 2 and 3 cases as simp lemmas instead\ntheorem forall_true_iff' (h : \u2200 a, p a \u2194 true) : (\u2200 a, p a) \u2194 true :=\niff_true_intro (\u03bb _, of_iff_true (h _))\n\n@[simp] theorem forall_2_true_iff {\u03b2 : \u03b1 \u2192 Sort*} : (\u2200 a, \u03b2 a \u2192 true) \u2194 true :=\nforall_true_iff' $ \u03bb _, forall_true_iff\n\n@[simp] theorem forall_3_true_iff {\u03b2 : \u03b1 \u2192 Sort*} {\u03b3 : \u03a0 a, \u03b2 a \u2192 Sort*} :\n  (\u2200 a (b : \u03b2 a), \u03b3 a b \u2192 true) \u2194 true :=\nforall_true_iff' $ \u03bb _, forall_2_true_iff\n\nlemma exists_unique.exists {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} (h : \u2203! x, p x) : \u2203 x, p x :=\nexists.elim h (\u03bb x hx, \u27e8x, and.left hx\u27e9)\n\n@[simp] lemma exists_unique_iff_exists {\u03b1 : Sort*} [subsingleton \u03b1] {p : \u03b1 \u2192 Prop} :\n  (\u2203! x, p x) \u2194 \u2203 x, p x :=\n\u27e8\u03bb h, h.exists, Exists.imp $ \u03bb x hx, \u27e8hx, \u03bb y _, subsingleton.elim y x\u27e9\u27e9\n\n@[simp] theorem forall_const (\u03b1 : Sort*) [i : nonempty \u03b1] : (\u03b1 \u2192 b) \u2194 b :=\n\u27e8i.elim, \u03bb hb x, hb\u27e9\n\n@[simp] theorem exists_const (\u03b1 : Sort*) [i : nonempty \u03b1] : (\u2203 x : \u03b1, b) \u2194 b :=\n\u27e8\u03bb \u27e8x, h\u27e9, h, i.elim exists.intro\u27e9\n\ntheorem exists_unique_const (\u03b1 : Sort*) [i : nonempty \u03b1] [subsingleton \u03b1] :\n  (\u2203! x : \u03b1, b) \u2194 b :=\nby simp\n\ntheorem forall_and_distrib : (\u2200 x, p x \u2227 q x) \u2194 (\u2200 x, p x) \u2227 (\u2200 x, q x) :=\n\u27e8\u03bb h, \u27e8\u03bb x, (h x).left, \u03bb x, (h x).right\u27e9, \u03bb \u27e8h\u2081, h\u2082\u27e9 x, \u27e8h\u2081 x, h\u2082 x\u27e9\u27e9\n\ntheorem exists_or_distrib : (\u2203 x, p x \u2228 q x) \u2194 (\u2203 x, p x) \u2228 (\u2203 x, q x) :=\n\u27e8\u03bb \u27e8x, hpq\u27e9, hpq.elim (\u03bb hpx, or.inl \u27e8x, hpx\u27e9) (\u03bb hqx, or.inr \u27e8x, hqx\u27e9),\n \u03bb hepq, hepq.elim (\u03bb \u27e8x, hpx\u27e9, \u27e8x, or.inl hpx\u27e9) (\u03bb \u27e8x, hqx\u27e9, \u27e8x, or.inr hqx\u27e9)\u27e9\n\n@[simp] theorem exists_and_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2203x, q \u2227 p x) \u2194 q \u2227 (\u2203x, p x) :=\n\u27e8\u03bb \u27e8x, hq, hp\u27e9, \u27e8hq, x, hp\u27e9, \u03bb \u27e8hq, x, hp\u27e9, \u27e8x, hq, hp\u27e9\u27e9\n\n@[simp] theorem exists_and_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2203x, p x \u2227 q) \u2194 (\u2203x, p x) \u2227 q :=\nby simp [and_comm]\n\n@[simp] theorem forall_eq {a' : \u03b1} : (\u2200a, a = a' \u2192 p a) \u2194 p a' :=\n\u27e8\u03bb h, h a' rfl, \u03bb h a e, e.symm \u25b8 h\u27e9\n\n@[simp] theorem forall_eq' {a' : \u03b1} : (\u2200a, a' = a \u2192 p a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\ntheorem and_forall_ne (a : \u03b1) : (p a \u2227 \u2200 b \u2260 a, p b) \u2194 \u2200 b, p b :=\nby simp only [\u2190 @forall_eq _ p a, \u2190 forall_and_distrib, \u2190 or_imp_distrib, classical.em,\n  forall_const]\n\n-- this lemma is needed to simplify the output of `list.mem_cons_iff`\n@[simp] theorem forall_eq_or_imp {a' : \u03b1} : (\u2200 a, a = a' \u2228 q a \u2192 p a) \u2194 p a' \u2227 \u2200 a, q a \u2192 p a :=\nby simp only [or_imp_distrib, forall_and_distrib, forall_eq]\n\ntheorem exists_eq {a' : \u03b1} : \u2203 a, a = a' := \u27e8_, rfl\u27e9\n\n@[simp] theorem exists_eq' {a' : \u03b1} : \u2203 a, a' = a := \u27e8_, rfl\u27e9\n\n@[simp] theorem exists_unique_eq {a' : \u03b1} : \u2203! a, a = a' :=\nby simp only [eq_comm, exists_unique, and_self, forall_eq', exists_eq']\n\n@[simp] theorem exists_unique_eq' {a' : \u03b1} : \u2203! a, a' = a :=\nby simp only [exists_unique, and_self, forall_eq', exists_eq']\n\n@[simp] theorem exists_eq_left {a' : \u03b1} : (\u2203 a, a = a' \u2227 p a) \u2194 p a' :=\n\u27e8\u03bb \u27e8a, e, h\u27e9, e \u25b8 h, \u03bb h, \u27e8_, rfl, h\u27e9\u27e9\n\n@[simp] theorem exists_eq_right {a' : \u03b1} : (\u2203 a, p a \u2227 a = a') \u2194 p a' :=\n(exists_congr $ by exact \u03bb a, and.comm).trans exists_eq_left\n\n@[simp] theorem exists_eq_right_right {a' : \u03b1} :\n  (\u2203 (a : \u03b1), p a \u2227 q a \u2227 a = a') \u2194 p a' \u2227 q a' :=\n\u27e8\u03bb \u27e8_, hp, hq, rfl\u27e9, \u27e8hp, hq\u27e9, \u03bb \u27e8hp, hq\u27e9, \u27e8a', hp, hq, rfl\u27e9\u27e9\n\n@[simp] theorem exists_eq_right_right' {a' : \u03b1} :\n  (\u2203 (a : \u03b1), p a \u2227 q a \u2227 a' = a) \u2194 p a' \u2227 q a' :=\n\u27e8\u03bb \u27e8_, hp, hq, rfl\u27e9, \u27e8hp, hq\u27e9, \u03bb \u27e8hp, hq\u27e9, \u27e8a', hp, hq, rfl\u27e9\u27e9\n\n@[simp] theorem exists_apply_eq_apply (f : \u03b1 \u2192 \u03b2) (a' : \u03b1) : \u2203 a, f a = f a' := \u27e8a', rfl\u27e9\n\n@[simp] theorem exists_apply_eq_apply' (f : \u03b1 \u2192 \u03b2) (a' : \u03b1) : \u2203 a, f a' = f a := \u27e8a', rfl\u27e9\n\n@[simp] theorem exists_exists_and_eq_and {f : \u03b1 \u2192 \u03b2} {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} :\n  (\u2203 b, (\u2203 a, p a \u2227 f a = b) \u2227 q b) \u2194 \u2203 a, p a \u2227 q (f a) :=\n\u27e8\u03bb \u27e8b, \u27e8a, ha, hab\u27e9, hb\u27e9, \u27e8a, ha, hab.symm \u25b8 hb\u27e9, \u03bb \u27e8a, hp, hq\u27e9, \u27e8f a, \u27e8a, hp, rfl\u27e9, hq\u27e9\u27e9\n\n@[simp] theorem exists_exists_eq_and {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2203 b, (\u2203 a, f a = b) \u2227 p b) \u2194 \u2203 a, p (f a) :=\n\u27e8\u03bb \u27e8b, \u27e8a, ha\u27e9, hb\u27e9, \u27e8a, ha.symm \u25b8 hb\u27e9, \u03bb \u27e8a, ha\u27e9, \u27e8f a, \u27e8a, rfl\u27e9, ha\u27e9\u27e9\n\n@[simp] lemma exists_or_eq_left (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), x = y \u2228 p x :=\n\u27e8y, or.inl rfl\u27e9\n\n@[simp] lemma exists_or_eq_right (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), p x \u2228 x = y :=\n\u27e8y, or.inr rfl\u27e9\n\n@[simp] lemma exists_or_eq_left' (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), y = x \u2228 p x :=\n\u27e8y, or.inl rfl\u27e9\n\n@[simp] lemma exists_or_eq_right' (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), p x \u2228 y = x :=\n\u27e8y, or.inr rfl\u27e9\n\n@[simp] theorem forall_apply_eq_imp_iff {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 a, \u2200 b, f a = b \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\n\u27e8\u03bb h a, h a (f a) rfl, \u03bb h a b hab, hab \u25b8 h a\u27e9\n\n@[simp] theorem forall_apply_eq_imp_iff' {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, f a = b \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby { rw forall_swap, simp }\n\n@[simp] theorem forall_eq_apply_imp_iff {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 a, \u2200 b, b = f a \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby simp [@eq_comm _ _ (f _)]\n\n@[simp] theorem forall_eq_apply_imp_iff' {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, b = f a \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby { rw forall_swap, simp }\n\n@[simp] theorem forall_apply_eq_imp_iff\u2082 {f : \u03b1 \u2192 \u03b2} {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, p a \u2192 f a = b \u2192 q b) \u2194 \u2200 a, p a \u2192 q (f a) :=\n\u27e8\u03bb h a ha, h (f a) a ha rfl, \u03bb h b a ha hb, hb \u25b8 h a ha\u27e9\n\n@[simp] theorem exists_eq_left' {a' : \u03b1} : (\u2203 a, a' = a \u2227 p a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\n@[simp] theorem exists_eq_right' {a' : \u03b1} : (\u2203 a, p a \u2227 a' = a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\ntheorem exists_comm {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2203 a b, p a b) \u2194 \u2203 b a, p a b :=\n\u27e8\u03bb \u27e8a, b, h\u27e9, \u27e8b, a, h\u27e9, \u03bb \u27e8b, a, h\u27e9, \u27e8a, b, h\u27e9\u27e9\n\nlemma exists\u2082_comm {\u03b9\u2081 \u03b9\u2082 : Sort*} {\u03ba\u2081 : \u03b9\u2081 \u2192 Sort*} {\u03ba\u2082 : \u03b9\u2082 \u2192 Sort*}\n  {p : \u03a0 i\u2081, \u03ba\u2081 i\u2081 \u2192 \u03a0 i\u2082, \u03ba\u2082 i\u2082 \u2192 Prop} :\n  (\u2203 i\u2081 j\u2081 i\u2082 j\u2082, p i\u2081 j\u2081 i\u2082 j\u2082) \u2194 \u2203 i\u2082 j\u2082 i\u2081 j\u2081, p i\u2081 j\u2081 i\u2082 j\u2082 :=\nby simp only [@exists_comm (\u03ba\u2081 _), @exists_comm \u03b9\u2081]\n\ntheorem and.exists {p q : Prop} {f : p \u2227 q \u2192 Prop} : (\u2203 h, f h) \u2194 \u2203 hp hq, f \u27e8hp, hq\u27e9 :=\n\u27e8\u03bb \u27e8h, H\u27e9, \u27e8h.1, h.2, H\u27e9, \u03bb \u27e8hp, hq, H\u27e9, \u27e8\u27e8hp, hq\u27e9, H\u27e9\u27e9\n\ntheorem forall_or_of_or_forall (h : b \u2228 \u2200x, p x) (x) : b \u2228 p x :=\nh.imp_right $ \u03bb h\u2082, h\u2082 x\n\n-- See Note [decidable namespace]\nprotected theorem decidable.forall_or_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} [decidable q] :\n  (\u2200x, q \u2228 p x) \u2194 q \u2228 (\u2200x, p x) :=\n\u27e8\u03bb h, if hq : q then or.inl hq else or.inr $ \u03bb x, (h x).resolve_left hq,\n  forall_or_of_or_forall\u27e9\n\ntheorem forall_or_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2200x, q \u2228 p x) \u2194 q \u2228 (\u2200x, p x) := decidable.forall_or_distrib_left\n\n-- See Note [decidable namespace]\nprotected theorem decidable.forall_or_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} [decidable q] :\n  (\u2200x, p x \u2228 q) \u2194 (\u2200x, p x) \u2228 q :=\nby simp [or_comm, decidable.forall_or_distrib_left]\n\ntheorem forall_or_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2200x, p x \u2228 q) \u2194 (\u2200x, p x) \u2228 q := decidable.forall_or_distrib_right\n\n@[simp] theorem exists_prop {p q : Prop} : (\u2203 h : p, q) \u2194 p \u2227 q :=\n\u27e8\u03bb \u27e8h\u2081, h\u2082\u27e9, \u27e8h\u2081, h\u2082\u27e9, \u03bb \u27e8h\u2081, h\u2082\u27e9, \u27e8h\u2081, h\u2082\u27e9\u27e9\n\ntheorem exists_unique_prop {p q : Prop} : (\u2203! h : p, q) \u2194 p \u2227 q :=\nby simp\n\n@[simp] theorem exists_false : \u00ac (\u2203a:\u03b1, false) := assume \u27e8a, h\u27e9, h\n\n@[simp] lemma exists_unique_false : \u00ac (\u2203! (a : \u03b1), false) := assume \u27e8a, h, h'\u27e9, h\n\ntheorem Exists.fst {p : b \u2192 Prop} : Exists p \u2192 b\n| \u27e8h, _\u27e9 := h\n\ntheorem Exists.snd {p : b \u2192 Prop} : \u2200 h : Exists p, p h.fst\n| \u27e8_, h\u27e9 := h\n\ntheorem forall_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2200 h' : p, q h') \u2194 q h :=\n@forall_const (q h) p \u27e8h\u27e9\n\ntheorem exists_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2203 h' : p, q h') \u2194 q h :=\n@exists_const (q h) p \u27e8h\u27e9\n\nlemma exists_iff_of_forall {p : Prop} {q : p \u2192 Prop} (h : \u2200 h, q h) : (\u2203 h, q h) \u2194 p :=\n\u27e8Exists.fst, \u03bb H, \u27e8H, h H\u27e9\u27e9\n\ntheorem exists_unique_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2203! h' : p, q h') \u2194 q h :=\n@exists_unique_const (q h) p \u27e8h\u27e9 _\n\ntheorem forall_prop_of_false {p : Prop} {q : p \u2192 Prop} (hn : \u00ac p) :\n  (\u2200 h' : p, q h') \u2194 true :=\niff_true_intro $ \u03bb h, hn.elim h\n\ntheorem exists_prop_of_false {p : Prop} {q : p \u2192 Prop} : \u00ac p \u2192 \u00ac (\u2203 h' : p, q h') :=\nmt Exists.fst\n\n@[congr] lemma exists_prop_congr {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : Exists q \u2194 \u2203 h : p', q' (hp.2 h) :=\n\u27e8\u03bb \u27e8_, _\u27e9, \u27e8hp.1 \u2039_\u203a, (hq _).1 \u2039_\u203a\u27e9, \u03bb \u27e8_, _\u27e9, \u27e8_, (hq _).2 \u2039_\u203a\u27e9\u27e9\n\n@[congr] lemma exists_prop_congr' {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : Exists q = \u2203 h : p', q' (hp.2 h) :=\npropext (exists_prop_congr hq _)\n\n@[simp] lemma exists_true_left (p : true \u2192 Prop) : (\u2203 x, p x) \u2194 p true.intro :=\nexists_prop_of_true _\n\n@[simp] lemma exists_false_left (p : false \u2192 Prop) : \u00ac \u2203 x, p x :=\nexists_prop_of_false not_false\n\nlemma exists_unique.unique {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} (h : \u2203! x, p x)\n  {y\u2081 y\u2082 : \u03b1} (py\u2081 : p y\u2081) (py\u2082 : p y\u2082) : y\u2081 = y\u2082 :=\nunique_of_exists_unique h py\u2081 py\u2082\n\n@[congr] lemma forall_prop_congr {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : (\u2200 h, q h) \u2194 \u2200 h : p', q' (hp.2 h) :=\n\u27e8\u03bb h1 h2, (hq _).1 (h1 (hp.2 _)), \u03bb h1 h2, (hq _).2 (h1 (hp.1 h2))\u27e9\n\n@[congr] lemma forall_prop_congr' {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : (\u2200 h, q h) = \u2200 h : p', q' (hp.2 h) :=\npropext (forall_prop_congr hq _)\n\n@[simp] lemma forall_true_left (p : true \u2192 Prop) : (\u2200 x, p x) \u2194 p true.intro :=\nforall_prop_of_true _\n\n@[simp] lemma forall_false_left (p : false \u2192 Prop) : (\u2200 x, p x) \u2194 true :=\nforall_prop_of_false not_false\n\nlemma exists_unique.elim2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 x (h : p x), Prop} {b : Prop} (h\u2082 : \u2203! x (h : p x), q x h)\n  (h\u2081 : \u2200 x (h : p x), q x h \u2192 (\u2200 y (hy : p y), q y hy \u2192 y = x) \u2192 b) : b :=\nbegin\n  simp only [exists_unique_iff_exists] at h\u2082,\n  apply h\u2082.elim,\n  exact \u03bb x \u27e8hxp, hxq\u27e9 H, h\u2081 x hxp hxq (\u03bb y hyp hyq, H y \u27e8hyp, hyq\u27e9)\nend\n\nlemma exists_unique.intro2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 (x : \u03b1) (h : p x), Prop} (w : \u03b1) (hp : p w) (hq : q w hp)\n  (H : \u2200 y (hy : p y), q y hy \u2192 y = w) :\n  \u2203! x (hx : p x), q x hx :=\nbegin\n  simp only [exists_unique_iff_exists],\n  exact exists_unique.intro w \u27e8hp, hq\u27e9 (\u03bb y \u27e8hyp, hyq\u27e9, H y hyp hyq)\nend\n\nlemma exists_unique.exists2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} {q : \u03a0 (x : \u03b1) (h : p x), Prop}\n  (h : \u2203! x (hx : p x), q x hx) :\n  \u2203 x (hx : p x), q x hx :=\nh.exists.imp (\u03bb x hx, hx.exists)\n\nlemma exists_unique.unique2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 (x : \u03b1) (hx : p x), Prop} (h : \u2203! x (hx : p x), q x hx)\n  {y\u2081 y\u2082 : \u03b1} (hpy\u2081 : p y\u2081) (hqy\u2081 : q y\u2081 hpy\u2081)\n  (hpy\u2082 : p y\u2082) (hqy\u2082 : q y\u2082 hpy\u2082) : y\u2081 = y\u2082 :=\nbegin\n  simp only [exists_unique_iff_exists] at h,\n  exact h.unique \u27e8hpy\u2081, hqy\u2081\u27e9 \u27e8hpy\u2082, hqy\u2082\u27e9\nend\n\nend quantifiers\n\n/-! ### Classical lemmas -/\n\nnamespace classical\nvariables {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop}\n\ntheorem cases {p : Prop \u2192 Prop} (h1 : p true) (h2 : p false) : \u2200a, p a :=\nassume a, cases_on a h1 h2\n\n/- use shortened names to avoid conflict when classical namespace is open. -/\n/-- Any prop `p` is decidable classically. A shorthand for `classical.prop_decidable`. -/\nnoncomputable def dec (p : Prop) : decidable p :=\nby apply_instance\n/-- Any predicate `p` is decidable classically. -/\nnoncomputable def dec_pred (p : \u03b1 \u2192 Prop) : decidable_pred p :=\nby apply_instance\n/-- Any relation `p` is decidable classically. -/\nnoncomputable def dec_rel (p : \u03b1 \u2192 \u03b1 \u2192 Prop) : decidable_rel p :=\nby apply_instance\n/-- Any type `\u03b1` has decidable equality classically. -/\nnoncomputable def dec_eq (\u03b1 : Sort*) : decidable_eq \u03b1 :=\nby apply_instance\n\n/-- Construct a function from a default value `H0`, and a function to use if there exists a value\nsatisfying the predicate. -/\n@[elab_as_eliminator]\nnoncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : \u2200 a, p a \u2192 C) : C :=\nif h : \u2203 a, p a then H (classical.some h) (classical.some_spec h) else H0\n\nlemma some_spec2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} {h : \u2203a, p a}\n  (q : \u03b1 \u2192 Prop) (hpq : \u2200a, p a \u2192 q a) : q (some h) :=\nhpq _ $ some_spec _\n\n/-- A version of classical.indefinite_description which is definitionally equal to a pair -/\nnoncomputable def subtype_of_exists {\u03b1 : Type*} {P : \u03b1 \u2192 Prop} (h : \u2203 x, P x) : {x // P x} :=\n\u27e8classical.some h, classical.some_spec h\u27e9\n\n/-- A version of `by_contradiction` that uses types instead of propositions. -/\nprotected noncomputable def by_contradiction' {\u03b1 : Sort*} (H : \u00ac (\u03b1 \u2192 false)) : \u03b1 :=\nclassical.choice $ peirce _ false $ \u03bb h, (H $ \u03bb a, h \u27e8a\u27e9).elim\n\n/-- `classical.by_contradiction'` is equivalent to lean's axiom `classical.choice`. -/\ndef choice_of_by_contradiction' {\u03b1 : Sort*} (contra : \u00ac (\u03b1 \u2192 false) \u2192 \u03b1) : nonempty \u03b1 \u2192 \u03b1 :=\n\u03bb H, contra H.elim\n\nend classical\n\n/-- This function has the same type as `exists.rec_on`, and can be used to case on an equality,\nbut `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe\nusing the axiom of choice. -/\n@[elab_as_eliminator]\nnoncomputable def {u} exists.classical_rec_on\n {\u03b1} {p : \u03b1 \u2192 Prop} (h : \u2203 a, p a) {C : Sort u} (H : \u2200 a, p a \u2192 C) : C :=\nH (classical.some h) (classical.some_spec h)\n\n/-! ### Declarations about bounded quantifiers -/\n\nsection bounded_quantifiers\nvariables {\u03b1 : Sort*} {r p q : \u03b1 \u2192 Prop} {P Q : \u2200 x, p x \u2192 Prop} {b : Prop}\n\ntheorem bex_def : (\u2203 x (h : p x), q x) \u2194 \u2203 x, p x \u2227 q x :=\n\u27e8\u03bb \u27e8x, px, qx\u27e9, \u27e8x, px, qx\u27e9, \u03bb \u27e8x, px, qx\u27e9, \u27e8x, px, qx\u27e9\u27e9\n\ntheorem bex.elim {b : Prop} : (\u2203 x h, P x h) \u2192 (\u2200 a h, P a h \u2192 b) \u2192 b\n| \u27e8a, h\u2081, h\u2082\u27e9 h' := h' a h\u2081 h\u2082\n\ntheorem bex.intro (a : \u03b1) (h\u2081 : p a) (h\u2082 : P a h\u2081) : \u2203 x (h : p x), P x h :=\n\u27e8a, h\u2081, h\u2082\u27e9\n\ntheorem ball_congr (H : \u2200 x h, P x h \u2194 Q x h) :\n  (\u2200 x h, P x h) \u2194 (\u2200 x h, Q x h) :=\nforall_congr $ \u03bb x, forall_congr (H x)\n\ntheorem bex_congr (H : \u2200 x h, P x h \u2194 Q x h) :\n  (\u2203 x h, P x h) \u2194 (\u2203 x h, Q x h) :=\nexists_congr $ \u03bb x, exists_congr (H x)\n\ntheorem bex_eq_left {a : \u03b1} : (\u2203 x (_ : x = a), p x) \u2194 p a :=\nby simp only [exists_prop, exists_eq_left]\n\ntheorem ball.imp_right (H : \u2200 x h, (P x h \u2192 Q x h))\n  (h\u2081 : \u2200 x h, P x h) (x h) : Q x h :=\nH _ _ $ h\u2081 _ _\n\ntheorem bex.imp_right (H : \u2200 x h, (P x h \u2192 Q x h)) :\n  (\u2203 x h, P x h) \u2192 \u2203 x h, Q x h\n| \u27e8x, h, h'\u27e9 := \u27e8_, _, H _ _ h'\u27e9\n\ntheorem ball.imp_left (H : \u2200 x, p x \u2192 q x)\n  (h\u2081 : \u2200 x, q x \u2192 r x) (x) (h : p x) : r x :=\nh\u2081 _ $ H _ h\n\ntheorem bex.imp_left (H : \u2200 x, p x \u2192 q x) :\n  (\u2203 x (_ : p x), r x) \u2192 \u2203 x (_ : q x), r x\n| \u27e8x, hp, hr\u27e9 := \u27e8x, H _ hp, hr\u27e9\n\ntheorem ball_of_forall (h : \u2200 x, p x) (x) : p x :=\nh x\n\ntheorem forall_of_ball (H : \u2200 x, p x) (h : \u2200 x, p x \u2192 q x) (x) : q x :=\nh x $ H x\n\ntheorem bex_of_exists (H : \u2200 x, p x) : (\u2203 x, q x) \u2192 \u2203 x (_ : p x), q x\n| \u27e8x, hq\u27e9 := \u27e8x, H x, hq\u27e9\n\ntheorem exists_of_bex : (\u2203 x (_ : p x), q x) \u2192 \u2203 x, q x\n| \u27e8x, _, hq\u27e9 := \u27e8x, hq\u27e9\n\n@[simp] theorem bex_imp_distrib : ((\u2203 x h, P x h) \u2192 b) \u2194 (\u2200 x h, P x h \u2192 b) :=\nby simp\n\ntheorem not_bex : (\u00ac \u2203 x h, P x h) \u2194 \u2200 x h, \u00ac P x h :=\nbex_imp_distrib\n\ntheorem not_ball_of_bex_not : (\u2203 x h, \u00ac P x h) \u2192 \u00ac \u2200 x h, P x h\n| \u27e8x, h, hp\u27e9 al := hp $ al x h\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_ball [decidable (\u2203 x h, \u00ac P x h)] [\u2200 x h, decidable (P x h)] :\n  (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) :=\n\u27e8not.decidable_imp_symm $ \u03bb nx x h, nx.decidable_imp_symm $ \u03bb h', \u27e8x, h, h'\u27e9,\n not_ball_of_bex_not\u27e9\n\ntheorem not_ball : (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) := decidable.not_ball\n\ntheorem ball_true_iff (p : \u03b1 \u2192 Prop) : (\u2200 x, p x \u2192 true) \u2194 true :=\niff_true_intro (\u03bb h hrx, trivial)\n\ntheorem ball_and_distrib : (\u2200 x h, P x h \u2227 Q x h) \u2194 (\u2200 x h, P x h) \u2227 (\u2200 x h, Q x h) :=\niff.trans (forall_congr $ \u03bb x, forall_and_distrib) forall_and_distrib\n\ntheorem bex_or_distrib : (\u2203 x h, P x h \u2228 Q x h) \u2194 (\u2203 x h, P x h) \u2228 (\u2203 x h, Q x h) :=\niff.trans (exists_congr $ \u03bb x, exists_or_distrib) exists_or_distrib\n\ntheorem ball_or_left_distrib : (\u2200 x, p x \u2228 q x \u2192 r x) \u2194 (\u2200 x, p x \u2192 r x) \u2227 (\u2200 x, q x \u2192 r x) :=\niff.trans (forall_congr $ \u03bb x, or_imp_distrib) forall_and_distrib\n\ntheorem bex_or_left_distrib :\n  (\u2203 x (_ : p x \u2228 q x), r x) \u2194 (\u2203 x (_ : p x), r x) \u2228 (\u2203 x (_ : q x), r x) :=\nby simp only [exists_prop]; exact\niff.trans (exists_congr $ \u03bb x, or_and_distrib_right) exists_or_distrib\n\nend bounded_quantifiers\n\nnamespace classical\nlocal attribute [instance] prop_decidable\n\ntheorem not_ball {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} {P : \u03a0 (x : \u03b1), p x \u2192 Prop} :\n  (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) := _root_.not_ball\n\nend classical\n\nsection ite\nvariables {\u03b1 \u03b2 \u03b3 : Sort*} {\u03c3 : \u03b1 \u2192 Sort*} (f : \u03b1 \u2192 \u03b2) {P Q : Prop} [decidable P] [decidable Q]\n  {a b c : \u03b1} {A : P \u2192 \u03b1} {B : \u00ac P \u2192 \u03b1}\n\nlemma dite_eq_iff : dite P A B = c \u2194 (\u2203 h, A h = c) \u2228 \u2203 h, B h = c := by by_cases P; simp *\nlemma ite_eq_iff : ite P a b = c \u2194 P \u2227 a = c \u2228 \u00ac P \u2227 b = c :=\ndite_eq_iff.trans $ by rw [exists_prop, exists_prop]\n\n@[simp] lemma dite_eq_left_iff : dite P (\u03bb _, a) B = a \u2194 \u2200 h, B h = a := by by_cases P; simp *\n@[simp] lemma dite_eq_right_iff : dite P A (\u03bb _, b) = b \u2194 \u2200 h, A h = b := by by_cases P; simp *\n@[simp] lemma ite_eq_left_iff : ite P a b = a \u2194 (\u00ac P \u2192 b = a) := dite_eq_left_iff\n@[simp] lemma ite_eq_right_iff : ite P a b = b \u2194 (P \u2192 a = b) := dite_eq_right_iff\n\nlemma dite_ne_left_iff : dite P (\u03bb _, a) B \u2260 a \u2194 \u2203 h, a \u2260 B h :=\nby { rw [ne.def, dite_eq_left_iff, not_forall], exact exists_congr (\u03bb h, by rw ne_comm) }\n\nlemma dite_ne_right_iff : dite P A (\u03bb _, b) \u2260 b \u2194 \u2203 h, A h \u2260 b :=\nby simp only [ne.def, dite_eq_right_iff, not_forall]\n\nlemma ite_ne_left_iff : ite P a b \u2260 a \u2194 \u00ac P \u2227 a \u2260 b := dite_ne_left_iff.trans $ by rw exists_prop\nlemma ite_ne_right_iff : ite P a b \u2260 b \u2194 P \u2227 a \u2260 b := dite_ne_right_iff.trans $ by rw exists_prop\n\nprotected lemma ne.dite_eq_left_iff (h : \u2200 h, a \u2260 B h) : dite P (\u03bb _, a) B = a \u2194 P :=\ndite_eq_left_iff.trans $ \u27e8\u03bb H, of_not_not $ \u03bb h', h h' (H h').symm, \u03bb h H, (H h).elim\u27e9\n\nprotected lemma ne.dite_eq_right_iff (h : \u2200 h, A h \u2260 b) : dite P A (\u03bb _, b) = b \u2194 \u00ac P :=\ndite_eq_right_iff.trans $ \u27e8\u03bb H h', h h' (H h'), \u03bb h' H, (h' H).elim\u27e9\n\nprotected lemma ne.ite_eq_left_iff (h : a \u2260 b) : ite P a b = a \u2194 P := ne.dite_eq_left_iff $ \u03bb _, h\nprotected lemma ne.ite_eq_right_iff (h : a \u2260 b) : ite P a b = b \u2194 \u00ac P :=\nne.dite_eq_right_iff $ \u03bb _, h\n\nprotected lemma ne.dite_ne_left_iff (h : \u2200 h, a \u2260 B h) : dite P (\u03bb _, a) B \u2260 a \u2194 \u00ac P :=\ndite_ne_left_iff.trans $ exists_iff_of_forall h\n\nprotected lemma ne.dite_ne_right_iff (h : \u2200 h, A h \u2260 b) : dite P A (\u03bb _, b) \u2260 b \u2194 P :=\ndite_ne_right_iff.trans $ exists_iff_of_forall h\n\nprotected lemma ne.ite_ne_left_iff (h : a \u2260 b) : ite P a b \u2260 a \u2194 \u00ac P := ne.dite_ne_left_iff $ \u03bb _, h\n\nprotected lemma ne.ite_ne_right_iff (h : a \u2260 b) : ite P a b \u2260 b \u2194 P := ne.dite_ne_right_iff $ \u03bb _, h\n\nvariables (P Q) (a b)\n\n/-- A `dite` whose results do not actually depend on the condition may be reduced to an `ite`. -/\n@[simp] lemma dite_eq_ite : dite P (\u03bb h, a) (\u03bb h, b) = ite P a b := rfl\n\nlemma dite_eq_or_eq : (\u2203 h, dite P A B = A h) \u2228 \u2203 h, dite P A B = B h :=\ndecidable.by_cases (\u03bb h, or.inl \u27e8h, dif_pos h\u27e9) (\u03bb h, or.inr \u27e8h, dif_neg h\u27e9)\n\nlemma ite_eq_or_eq : ite P a b = a \u2228 ite P a b = b :=\ndecidable.by_cases (\u03bb h, or.inl (if_pos h)) (\u03bb h, or.inr (if_neg h))\n\n/-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/\nlemma apply_dite (x : P \u2192 \u03b1) (y : \u00acP \u2192 \u03b1) : f (dite P x y) = dite P (\u03bb h, f (x h)) (\u03bb h, f (y h)) :=\nby by_cases h : P; simp [h]\n\n/-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/\nlemma apply_ite : f (ite P a b) = ite P (f a) (f b) := apply_dite f P (\u03bb _, a) (\u03bb _, b)\n\n/-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function\napplied to each of the branches. -/\nlemma apply_dite2 (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) (P : Prop) [decidable P] (a : P \u2192 \u03b1) (b : \u00acP \u2192 \u03b1) (c : P \u2192 \u03b2)\n  (d : \u00acP \u2192 \u03b2) :\n  f (dite P a b) (dite P c d) = dite P (\u03bb h, f (a h) (c h)) (\u03bb h, f (b h) (d h)) :=\nby by_cases h : P; simp [h]\n\n/-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function\napplied to each of the branches. -/\nlemma apply_ite2 (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) (P : Prop) [decidable P] (a b : \u03b1) (c d : \u03b2) :\n  f (ite P a b) (ite P c d) = ite P (f a c) (f b d) :=\napply_dite2 f P (\u03bb _, a) (\u03bb _, b) (\u03bb _, c) (\u03bb _, d)\n\n/-- A 'dite' producing a `Pi` type `\u03a0 a, \u03c3 a`, applied to a value `a : \u03b1` is a `dite` that applies\neither branch to `a`. -/\nlemma dite_apply (f : P \u2192 \u03a0 a, \u03c3 a) (g : \u00ac P \u2192 \u03a0 a, \u03c3 a) (a : \u03b1) :\n  (dite P f g) a = dite P (\u03bb h, f h a) (\u03bb h, g h a) :=\nby by_cases h : P; simp [h]\n\n/-- A 'ite' producing a `Pi` type `\u03a0 a, \u03c3 a`, applied to a value `a : \u03b1` is a `ite` that applies\neither branch to `a`. -/\nlemma ite_apply (f g : \u03a0 a, \u03c3 a) (a : \u03b1) : (ite P f g) a = ite P (f a) (g a) :=\ndite_apply P (\u03bb _, f) (\u03bb _, g) a\n\n/-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/\n@[simp] lemma dite_not (x : \u00ac P \u2192 \u03b1) (y : \u00ac\u00ac P \u2192 \u03b1) :\n  dite (\u00ac P) x y = dite P (\u03bb h, y (not_not_intro h)) x :=\nby by_cases h : P; simp [h]\n\n/-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/\n@[simp] lemma ite_not : ite (\u00ac P) a b = ite P b a := dite_not P (\u03bb _, a) (\u03bb _, b)\n\nlemma ite_and : ite (P \u2227 Q) a b = ite P (ite Q a b) b :=\nby by_cases hp : P; by_cases hq : Q; simp [hp, hq]\n\nend ite\n", "meta": {"author": "nick-kuhn", "repo": "leantools", "sha": "567a98c031fffe3f270b7b8dea48389bc70d7abb", "save_path": "github-repos/lean/nick-kuhn-leantools", "path": "github-repos/lean/nick-kuhn-leantools/leantools-567a98c031fffe3f270b7b8dea48389bc70d7abb/src/logic/basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.399811640739795, "lm_q2_score": 0.13660840232900243, "lm_q1q2_score": 0.0546176294740005}}
{"text": "/-\nCopyright (c) 2020 Jannis Limperg. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jannis Limperg\n-/\nimport data.bool\nimport tactic.core\n\n/-!\n# Better `clear` tactics\n\nWe define two variants of the standard `clear` tactic:\n\n* `clear'` works like `clear` but the hypotheses that should be cleared can be\n  given in any order. In contrast, `clear` can fail if hypotheses that depend on\n  each other are given in the wrong order, even if all of them could be cleared.\n\n* `clear_dependent` works like `clear'` but also clears any hypotheses that\n  depend on the given hypotheses.\n\n## Implementation notes\n\nThe implementation (ab)uses the native `revert_lst`, which can figure out\ndependencies between hypotheses. This implementation strategy was suggested by\nSimon Hudon.\n-/\n\nopen native tactic interactive lean.parser\n\n/-- Clears all the hypotheses in `hyps`. The tactic fails if any of the `hyps`\nis not a local or if the target depends on any of the `hyps`. It also fails if\n`hyps` contains duplicates.\n\nIf there are local hypotheses or definitions, say `H`, which are not in `hyps`\nbut depend on one of the `hyps`, what we do depends on `clear_dependent`. If it\nis true, `H` is implicitly also cleared. If it is false, `clear'` fails. -/\nmeta def tactic.clear' (clear_dependent : bool) (hyps : list expr) : tactic unit := do\ntgt \u2190 target,\n-- Check if the target depends on any of the hyps. Doing this (instead of\n-- letting one of the later tactics fail) lets us give a much more informative\n-- error message.\nhyps.mmap' (\u03bb h, do\n  dep \u2190 kdepends_on tgt h,\n  when dep $ fail $\n    format!\"Cannot clear hypothesis {h} since the target depends on it.\"),\nn \u2190 revert_lst hyps,\n-- If revert_lst reverted more hypotheses than we wanted to clear, there must\n-- have been other hypotheses dependent on some of the hyps.\nwhen (! clear_dependent && (n \u2260 hyps.length)) $ fail $ format.join\n  [ \"Some of the following hypotheses cannot be cleared because other \"\n  , \"hypotheses depend on (some of) them:\\n\"\n  , format.intercalate \", \" (hyps.map to_fmt)\n  ],\nv \u2190 mk_meta_var tgt,\nintron n,\nexact v,\ngs \u2190 get_goals,\nset_goals $ v :: gs\n\nnamespace tactic.interactive\n\n/--\nAn improved version of the standard `clear` tactic. `clear` is sensitive to the\norder of its arguments: `clear x y` may fail even though both `x` and `y` could\nbe cleared (if the type of `y` depends on `x`). `clear'` lifts this limitation.\n\n```lean\nexample {\u03b1} {\u03b2 : \u03b1 \u2192 Type} (a : \u03b1) (b : \u03b2 a) : unit :=\nbegin\n  try { clear a b }, -- fails since `b` depends on `a`\n  clear' a b,        -- succeeds\n  exact ()\nend\n```\n-/\nmeta def clear' (p : parse (many ident)) : tactic unit := do\nhyps \u2190 p.mmap get_local,\ntactic.clear' false hyps\n\n/--\nA variant of `clear'` which clears not only the given hypotheses, but also any\nother hypotheses depending on them.\n\n```lean\nexample {\u03b1} {\u03b2 : \u03b1 \u2192 Type} (a : \u03b1) (b : \u03b2 a) : unit :=\nbegin\n  try { clear' a },  -- fails since `b` depends on `a`\n  clear_dependent a, -- succeeds, clearing `a` and `b`\n  exact ()\nend\n```\n -/\nmeta def clear_dependent (p : parse (many ident)) : tactic unit := do\nhyps \u2190 p.mmap get_local,\ntactic.clear' true hyps\n\nadd_tactic_doc\n{ name       := \"clear'\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.clear', `tactic.interactive.clear_dependent],\n  tags       := [\"context management\"],\n  inherit_description_from := `tactic.interactive.clear' }\n\nend tactic.interactive\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/tactic/clear.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.42632159254749036, "lm_q2_score": 0.12765261536566067, "lm_q1q2_score": 0.05442106627554069}}
{"text": "import tactic --hide\n\n/-Lemma\nFalse implies true.\n-/\nlemma false_imp_true : false \u2192 true :=\nbegin\n  intro t,\n  triv,\nend", "meta": {"author": "CBirkbeck", "repo": "logic_projic", "sha": "0b029af0fbfc0ac6eafae47401d5bbf8e641d7d2", "save_path": "github-repos/lean/CBirkbeck-logic_projic", "path": "github-repos/lean/CBirkbeck-logic_projic/logic_projic-0b029af0fbfc0ac6eafae47401d5bbf8e641d7d2/src/true_false/tf3.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.46879062662624377, "lm_q2_score": 0.11596070756094143, "lm_q1q2_score": 0.054361292761516335}}
{"text": "/-\nCopyright (c) 2019 Patrick Massot. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Patrick Massot\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.interactive\nimport Mathlib.PostPort\n\nnamespace Mathlib\n\n/-!\n# Rename bound variable tactic\n\nThis files defines a tactic `rename_var` whose main purpose is to teach\nrenaming of bound variables.\n\n* `rename_var old new` renames all bound variables named `old` to `new` in the goal.\n* `rename_var old new at h` does the same in hypothesis `h`.\n\n```lean\nexample (P : \u2115 \u2192  \u2115 \u2192 Prop) (h : \u2200 n, \u2203 m, P n m) : \u2200 l, \u2203 m, P l m :=\nbegin\n  rename_var n q at h, -- h is now \u2200 (q : \u2115), \u2203 (m : \u2115), P q m,\n  rename_var m n, -- goal is now \u2200 (l : \u2115), \u2203 (n : \u2115), P k n,\n  exact h -- Lean does not care about those bound variable names\nend\n```\n\n## Tags\n\nteaching, tactic\n-/\n\n/-- Rename bound variable `old` to `new` in an `expr`-/\nnamespace tactic\n\n\n/-- Rename bound variable `old` to `new` in goal -/\n/-- Rename bound variable `old` to `new` in assumption `h` -/\nend tactic\n\n\nnamespace tactic.interactive\n\n\n/--\n`rename_var old new` renames all bound variables named `old` to `new` in the goal.\n`rename_var old new at h` does the same in hypothesis `h`.\n-/\nend tactic.interactive\n\n\n/--\n`rename_var old new` renames all bound variables named `old` to `new` in the goal.\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/tactic/rename_var_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.35577489351363034, "lm_q2_score": 0.15203224930631218, "lm_q1q2_score": 0.05408925730759091}}
{"text": "/-\nCopyright 2022 Google LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\nAuthors: Moritz Firsching\n-/\nimport tactic\n/-!\n# The chromatic number of Kneser graphs\n\n## TODO\n  - Kneser's conjecture\n  - The Borsuk-Ulam theorem\n  - Theorem\n  - Proof of the Lysternik-Shnirel'man theorem using Borsuk-Ulam\n  - Gale's Theorem\n  - Proof of the Kneser conjecture.\n  - Appendix: A proof sketch for the Borsuk-Ulam theorem\n-/\n", "meta": {"author": "mo271", "repo": "formal_book", "sha": "34cbc0b9e9d361b74adbe0fd06192a72e684b992", "save_path": "github-repos/lean/mo271-formal_book", "path": "github-repos/lean/mo271-formal_book/formal_book-34cbc0b9e9d361b74adbe0fd06192a72e684b992/src/chapters/43_The_chromatic_number_of_Kneser_graphs.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.44939263446475963, "lm_q2_score": 0.11920291889306492, "lm_q1q2_score": 0.053568913757243514}}
{"text": "/-\nCopyright (c) 2020 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon\n\n! This file was ported from Lean 3 source module tactic.pretty_cases\n! leanprover-community/mathlib commit 60e279ba2bfe69f937b5f69b110a440538b8f6a5\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.Core\n\n/-!\n# `pretty_cases` tactic\n\nWhen using `induction` and `cases`, `pretty_cases` prints a `\"Try\nthis:\"` advice that shows how to structure the proof with\n`case { ... }` commands.  In the following example, we apply induction on a\npermutation assumption about lists. `pretty_cases` gives us a proof\nskeleton that explicit selects the branches and explicit names the\nnew local constants:\n\n```lean\nexample {\u03b1} (xs ys : list \u03b1) (h : xs ~ ys) : true :=\nbegin\n  induction h,\n  pretty_cases,\n    -- Try this:\n    --   case list.perm.nil :\n    --   { admit },\n    --   case list.perm.cons : h_x h_l\u2081 h_l\u2082 h_a h_ih\n    --   { admit },\n    --   case list.perm.swap : h_x h_y h_l\n    --   { admit },\n    --   case list.perm.trans : h_l\u2081 h_l\u2082 h_l\u2083 h_a h_a_1 h_ih_a h_ih_a_1\n    --   { admit },\nend\n```\n\n## Main definitions\n\n * `pretty_cases_advice` return `pretty_cases` advice without printing it\n * `pretty_cases` main tactic\n-/\n\n\nnamespace Tactic\n\n/-- Query the proof goal and print the skeleton of a proof by cases. -/\nunsafe def pretty_cases_advice : tactic String :=\n  retrieve do\n    let gs \u2190 get_goals\n    let cases \u2190\n      gs.mapM fun g => do\n          let t : List Name \u2190 get_tag g\n          let vs := t.tail\n          let \u27e8vs, ts\u27e9 := vs.span\u2093 fun n => Name.lastString n = \"_arg\"\n          set_goals [g]\n          let ls \u2190 local_context\n          let m :=\n            native.rb_map.of_list <| (ls.map expr.local_uniq_name).zip (ls.map expr.local_pp_name)\n          let vs := vs.map fun v => (m.find v.getPrefix).getD `_\n          let var_decls := String.intercalate \" \" <| vs.map toString\n          let var_decls := if vs.Empty then \"\" else \" : \" ++ var_decls\n          pure\n              s! \"  case {ts }{var_decls}\n                  \\{ admit }}\"\n    let cases := String.intercalate \",\\n\" cases\n    pure\n        s! \"Try this:\n          {cases}\"\n#align tactic.pretty_cases_advice tactic.pretty_cases_advice\n\nnamespace Interactive\n\n/-- Query the proof goal and print the skeleton of a proof by\ncases.\n\nFor example, let us consider the following proof:\n\n```lean\nexample {\u03b1} (xs ys : list \u03b1) (h : xs ~ ys) : true :=\nbegin\n  induction h,\n  pretty_cases,\n    -- Try this:\n    --   case list.perm.nil :\n    --   { admit },\n    --   case list.perm.cons : h_x h_l\u2081 h_l\u2082 h_a h_ih\n    --   { admit },\n    --   case list.perm.swap : h_x h_y h_l\n    --   { admit },\n    --   case list.perm.trans : h_l\u2081 h_l\u2082 h_l\u2083 h_a h_a_1 h_ih_a h_ih_a_1\n    --   { admit },\nend\n```\n\nThe output helps the user layout the cases and rename the\nintroduced variables.\n-/\nunsafe def pretty_cases : tactic Unit :=\n  pretty_cases_advice >>= trace\n#align tactic.interactive.pretty_cases tactic.interactive.pretty_cases\n\nadd_tactic_doc\n  { Name := \"pretty_cases\"\n    category := DocCategory.tactic\n    declNames := [`` tactic.interactive.pretty_cases]\n    tags := [\"context management\", \"goal management\"] }\n\nend Interactive\n\nend Tactic\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/Tactic/PrettyCases.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.36658972248186006, "lm_q2_score": 0.14608724890715238, "lm_q1q2_score": 0.053554084035011405}}
{"text": "/-\nCopyright (c) 2020 Jannis Limperg. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jannis Limperg\n-/\nimport data.bool.basic\nimport tactic.core\n\n/-!\n# Better `clear` tactics\n\nWe define two variants of the standard `clear` tactic:\n\n* `clear'` works like `clear` but the hypotheses that should be cleared can be\n  given in any order. In contrast, `clear` can fail if hypotheses that depend on\n  each other are given in the wrong order, even if all of them could be cleared.\n\n* `clear_dependent` works like `clear'` but also clears any hypotheses that\n  depend on the given hypotheses.\n\n## Implementation notes\n\nThe implementation (ab)uses the native `revert_lst`, which can figure out\ndependencies between hypotheses. This implementation strategy was suggested by\nSimon Hudon.\n-/\n\nopen native tactic interactive lean.parser\n\n/-- Clears all the hypotheses in `hyps`. The tactic fails if any of the `hyps`\nis not a local or if the target depends on any of the `hyps`. It also fails if\n`hyps` contains duplicates.\n\nIf there are local hypotheses or definitions, say `H`, which are not in `hyps`\nbut depend on one of the `hyps`, what we do depends on `clear_dependent`. If it\nis true, `H` is implicitly also cleared. If it is false, `clear'` fails. -/\nmeta def tactic.clear' (clear_dependent : bool) (hyps : list expr) : tactic unit := do\ntgt \u2190 target,\n-- Check if the target depends on any of the hyps. Doing this (instead of\n-- letting one of the later tactics fail) lets us give a much more informative\n-- error message.\nhyps.mmap' (\u03bb h, do\n  dep \u2190 kdepends_on tgt h,\n  when dep $ fail $\n    format!\"Cannot clear hypothesis {h} since the target depends on it.\"),\nn \u2190 revert_lst hyps,\n-- If revert_lst reverted more hypotheses than we wanted to clear, there must\n-- have been other hypotheses dependent on some of the hyps.\nwhen (! clear_dependent && (n \u2260 hyps.length)) $ fail $ format.join\n  [ \"Some of the following hypotheses cannot be cleared because other \"\n  , \"hypotheses depend on (some of) them:\\n\"\n  , format.intercalate \", \" (hyps.map to_fmt)\n  ],\nv \u2190 mk_meta_var tgt,\nintron n,\nexact v,\ngs \u2190 get_goals,\nset_goals $ v :: gs\n\nnamespace tactic.interactive\n\n/--\nAn improved version of the standard `clear` tactic. `clear` is sensitive to the\norder of its arguments: `clear x y` may fail even though both `x` and `y` could\nbe cleared (if the type of `y` depends on `x`). `clear'` lifts this limitation.\n\n```lean\nexample {\u03b1} {\u03b2 : \u03b1 \u2192 Type} (a : \u03b1) (b : \u03b2 a) : unit :=\nbegin\n  try { clear a b }, -- fails since `b` depends on `a`\n  clear' a b,        -- succeeds\n  exact ()\nend\n```\n-/\nmeta def clear' (p : parse (many ident)) : tactic unit := do\nhyps \u2190 p.mmap get_local,\ntactic.clear' false hyps\n\n/--\nA variant of `clear'` which clears not only the given hypotheses, but also any\nother hypotheses depending on them.\n\n```lean\nexample {\u03b1} {\u03b2 : \u03b1 \u2192 Type} (a : \u03b1) (b : \u03b2 a) : unit :=\nbegin\n  try { clear' a },  -- fails since `b` depends on `a`\n  clear_dependent a, -- succeeds, clearing `a` and `b`\n  exact ()\nend\n```\n -/\nmeta def clear_dependent (p : parse (many ident)) : tactic unit := do\nhyps \u2190 p.mmap get_local,\ntactic.clear' true hyps\n\nadd_tactic_doc\n{ name       := \"clear'\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.clear', `tactic.interactive.clear_dependent],\n  tags       := [\"context management\"],\n  inherit_description_from := `tactic.interactive.clear' }\n\nend tactic.interactive\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/clear.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.41869690935568665, "lm_q2_score": 0.12765262532179067, "lm_q1q2_score": 0.05344775969337322}}
{"text": "/-\nCopyright (c) 2017 Mario Carneiro. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro\n-/\nimport data.buffer.parser\nimport tactic.core\n\n/-!\n# The `alias` command\n\nThis file defines an `alias` command, which can be used to create copies\nof a theorem or definition with different names.\n\nSyntax:\n\n```lean\n/-- doc string -/\nalias my_theorem \u2190 alias1 alias2 ...\n```\n\nThis produces defs or theorems of the form:\n\n```lean\n/-- doc string -/\n@[alias] theorem alias1 : <type of my_theorem> := my_theorem\n\n/-- doc string -/\n@[alias] theorem alias2 : <type of my_theorem> := my_theorem\n```\n\nIff alias syntax:\n\n```lean\nalias A_iff_B \u2194 B_of_A A_of_B\nalias A_iff_B \u2194 ..\n```\n\nThis gets an existing biconditional theorem `A_iff_B` and produces\nthe one-way implications `B_of_A` and `A_of_B` (with no change in\nimplicit arguments). A blank `_` can be used to avoid generating one direction.\nThe `..` notation attempts to generate the 'of'-names automatically when the\ninput theorem has the form `A_iff_B` or `A_iff_B_left` etc.\n-/\n\nopen lean.parser tactic interactive parser\n\nnamespace tactic.alias\n\n@[user_attribute] meta def alias_attr : user_attribute :=\n{ name := `alias, descr := \"This definition is an alias of another.\", parser := failed }\n\nmeta def alias_direct (d : declaration) (doc : string) (al : name) : tactic unit :=\ndo updateex_env $ \u03bb env,\n  env.add (match d.to_definition with\n  | declaration.defn n ls t _ _ _ :=\n    declaration.defn al ls t (expr.const n (level.param <$> ls))\n      reducibility_hints.abbrev tt\n  | declaration.thm n ls t _ :=\n    declaration.thm al ls t $ task.pure $ expr.const n (level.param <$> ls)\n  | _ := undefined\n  end),\n  alias_attr.set al () tt,\n  add_doc_string al doc\n\nmeta def mk_iff_mp_app (iffmp : name) : expr \u2192 (\u2115 \u2192 expr) \u2192 tactic expr\n| (expr.pi n bi e t) f := expr.lam n bi e <$> mk_iff_mp_app t (\u03bb n, f (n+1) (expr.var n))\n| `(%%a \u2194 %%b) f := pure $ @expr.const tt iffmp [] a b (f 0)\n| _ f := fail \"Target theorem must have the form `\u03a0 x y z, a \u2194 b`\"\n\nmeta def alias_iff (d : declaration) (doc : string) (al : name) (iffmp : name) : tactic unit :=\n(if al = `_ then skip else get_decl al >> skip) <|> do\n  let ls := d.univ_params,\n  let t := d.type,\n  v \u2190 mk_iff_mp_app iffmp t (\u03bb_, expr.const d.to_name (level.param <$> ls)),\n  t' \u2190 infer_type v,\n  updateex_env $ \u03bb env, env.add (declaration.thm al ls t' $ task.pure v),\n  alias_attr.set al () tt,\n  add_doc_string al doc\n\nmeta def make_left_right : name \u2192 tactic (name \u00d7 name)\n| (name.mk_string s p) := do\n  let buf : char_buffer := s.to_char_buffer,\n  sum.inr parts \u2190 pure $ run (sep_by1 (ch '_') (many_char (sat (\u2260 '_')))) s.to_char_buffer,\n  (left, _::right) \u2190 pure $ parts.span (\u2260 \"iff\"),\n  let pfx (a b : string) := a.to_list.is_prefix_of b.to_list,\n  (suffix', right') \u2190 pure $ right.reverse.span (\u03bb s, pfx \"left\" s \u2228 pfx \"right\" s),\n  let right := right'.reverse,\n  let suffix := suffix'.reverse,\n  pure (p <.> \"_\".intercalate (right ++ \"of\" :: left ++ suffix),\n        p <.> \"_\".intercalate (left ++ \"of\" :: right ++ suffix))\n| _ := failed\n\n/--\nThe `alias` command can be used to create copies\nof a theorem or definition with different names.\n\nSyntax:\n\n```lean\n/-- doc string -/\nalias my_theorem \u2190 alias1 alias2 ...\n```\n\nThis produces defs or theorems of the form:\n\n```lean\n/-- doc string -/\n@[alias] theorem alias1 : <type of my_theorem> := my_theorem\n\n/-- doc string -/\n@[alias] theorem alias2 : <type of my_theorem> := my_theorem\n```\n\nIff alias syntax:\n\n```lean\nalias A_iff_B \u2194 B_of_A A_of_B\nalias A_iff_B \u2194 ..\n```\n\nThis gets an existing biconditional theorem `A_iff_B` and produces\nthe one-way implications `B_of_A` and `A_of_B` (with no change in\nimplicit arguments). A blank `_` can be used to avoid generating one direction.\nThe `..` notation attempts to generate the 'of'-names automatically when the\ninput theorem has the form `A_iff_B` or `A_iff_B_left` etc.\n-/\n@[user_command] meta def alias_cmd (meta_info : decl_meta_info)\n  (_ : parse $ tk \"alias\") : lean.parser unit :=\ndo old \u2190 ident,\n  d \u2190 (do old \u2190 resolve_constant old, get_decl old) <|>\n    fail (\"declaration \" ++ to_string old ++ \" not found\"),\n  let doc := \u03bb al : name, meta_info.doc_string.get_or_else $\n    \"**Alias** of `\" ++ to_string old ++ \"`.\",\n  do\n  { tk \"\u2190\" <|> tk \"<-\",\n    aliases \u2190 many ident,\n    \u2191(aliases.mmap' $ \u03bb al, alias_direct d (doc al) al) } <|>\n  do\n  { tk \"\u2194\" <|> tk \"<->\",\n    (left, right) \u2190\n      mcond ((tk \".\" *> tk \".\" >> pure tt) <|> pure ff)\n        (make_left_right old <|> fail \"invalid name for automatic name generation\")\n        (prod.mk <$> types.ident_ <*> types.ident_),\n    alias_iff d (doc left) left `iff.mp,\n    alias_iff d (doc right) right `iff.mpr }\n\nadd_tactic_doc\n{ name                     := \"alias\",\n  category                 := doc_category.cmd,\n  decl_names               := [`tactic.alias.alias_cmd],\n  tags                     := [\"renaming\"] }\n\nmeta def get_lambda_body : expr \u2192 expr\n| (expr.lam _ _ _ b) := get_lambda_body b\n| a                  := a\n\nmeta def get_alias_target (n : name) : tactic (option name) :=\ndo tt \u2190 has_attribute' `alias n | pure none,\n  d \u2190 get_decl n,\n  let (head, args) := (get_lambda_body d.value).get_app_fn_args,\n  let head := if head.is_constant_of `iff.mp \u2228 head.is_constant_of `iff.mpr then\n    expr.get_app_fn (head.ith_arg 2)\n  else head,\n  guardb $ head.is_constant,\n  pure $ head.const_name\n\nend tactic.alias\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/tactic/alias.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.43014734858584286, "lm_q2_score": 0.12421301969912354, "lm_q1q2_score": 0.05342990108341906}}
{"text": "import tactic\nimport .tokens .commun\n\nnamespace tactic\nsetup_tactic_parser\n\n@[derive has_reflect]\nmeta inductive Par_args \n-- Par fait (appliqu\u00e9 \u00e0 args) on obtient news (tel que news')\n| obtenir (fait : pexpr) (args : list pexpr) (news : list maybe_typed_ident) : Par_args\n-- Par fait (appliqu\u00e9 \u00e0 args) on obtient news (tel que news')\n| choisir (fait : pexpr) (args : list pexpr) (news : list maybe_typed_ident) : Par_args\n-- Par fait (appliqu\u00e9 \u00e0 args) il suffit de montrer que buts\n| appliquer (fait : pexpr) (args : list pexpr) (buts : list pexpr) : Par_args \n\nopen Par_args\n\n/-- Parse une liste de cons\u00e9quences, peut-\u00eatre vide. -/\nmeta def on_obtient_parser : lean.parser (list maybe_typed_ident) :=\ndo { news \u2190 tk \"obtient\" *> maybe_typed_ident_parser*,\n     news' \u2190 (tk \"tel\" *> tk \"que\" *> maybe_typed_ident_parser*) <|> pure [], \n     pure (news ++ news') } \n\n/-- Parse une liste de cons\u00e9quences pour `choose`. -/\nmeta def on_choisit_parser : lean.parser (list maybe_typed_ident) :=\ndo { news \u2190 tk \"choisit\" *> maybe_typed_ident_parser*,\n     news' \u2190 (tk \"tel\" *> tk \"que\" *> maybe_typed_ident_parser*) <|> pure [], \n     pure (news ++ news') } \n\n/-- Parse un ou plusieurs nouveaux buts. -/\nmeta def buts_parser : lean.parser (list pexpr) :=\ntk \"il\" *> tk \"suffit\" *> tk \"de\" *> tk \"montrer\" *> tk \"que\" *>  pexpr_list_or_texpr\n\n/-- Parser principal pour la tactique Par. -/\nmeta def Par_parser : lean.parser Par_args :=\nwith_desc \"... (appliqu\u00e9 \u00e0 ...) on obtient ... (tel que ...) / Par ... (appliqu\u00e9 \u00e0 ...) il suffit de montrer que ...\" $\ndo e \u2190 texpr,\n   args \u2190 applique_a_parser,\n   do { _ \u2190 tk \"on\",\n       (Par_args.obtenir e args <$> on_obtient_parser) <|>\n       (Par_args.choisir e args <$> on_choisit_parser) } <|>\n   (Par_args.appliquer e args <$> buts_parser)\n\nmeta def verifie_type : maybe_typed_ident \u2192 tactic unit\n| (n, some t) := do n_type \u2190 get_local n >>= infer_type,\n                    to_expr t >>= unify n_type  \n| (n, none) := skip\n\n/-- R\u00e9cup\u00e8re de l'information d'une hypoth\u00e8se ou d'un lemme ou bien \nr\u00e9duit le but \u00e0 un ou plusieurs nouveaux buts en appliquant une \nhypoth\u00e8se ou un lemme. -/\n@[interactive]\nmeta def Par : parse Par_parser \u2192 tactic unit\n| (Par_args.obtenir fait args news) := focus1 (do\n    news.mmap' (\u03bb p : maybe_typed_ident, verifie_nom p.1),\n    efait \u2190 to_expr fait,\n    applied \u2190 mk_mapp_pexpr efait args,\n    if news.length = 1 then do { -- Cas o\u00f9 il n'y a rien \u00e0 d\u00e9structurer\n         nom \u2190 match news with\n         | ((nom, some new) :: t) := do enew \u2190 to_expr new, \n                                        infer_type applied >>= unify enew,\n                                        pure nom\n         | ((nom, none) :: t) := pure nom\n         | _ := fail \"Il faut indiquer un nom pour l'information obtenue.\" -- ne devrait pas arriver\n         end,\n         hyp \u2190 note nom none applied,\n         nettoyage,\n         news.mmap' verifie_type }\n    else do tactic.rcases none (to_pexpr $ applied)\n                  $ rcases_patt.tuple $ news.map rcases_patt_of_maybe_typed_ident, \n            nettoyage )\n| (Par_args.choisir fait args news) := focus1 (do\n    efait \u2190 to_expr fait,\n    applied \u2190 mk_mapp_pexpr efait args,\n    choose tt applied (news.map prod.fst),\n    nettoyage,\n    news.mmap' verifie_type)\n| (Par_args.appliquer fait args buts) := focus1 (do\n    efait \u2190 to_expr fait,\n    ebuts \u2190 buts.mmap to_expr,\n    mk_mapp_pexpr efait args >>= apply,\n    vrai_buts \u2190 get_goals, \n    let paires := list.zip vrai_buts buts,\n    focus' (paires.map (\u03bb p : expr \u00d7 pexpr, do \n       `(force_type %%p _) \u2190  i_to_expr_no_subgoals ``(force_type %%p.2 %%p.1), skip))\n    <|> fail \"Ce n'est pas ce qu'il faut d\u00e9montrer\")\n\nend tactic\n\nexample (P Q : (\u2115 \u2192 \u2115) \u2192 Prop) (h : true \u2227 \u2203 u : \u2115 \u2192 \u2115, P u \u2227 Q u) : true :=\nbegin\n  Par h on obtient (a : true) (u : \u2115 \u2192 \u2115) (b : P u) (c : Q u),\n  trivial\nend\n\nexample (n : \u2115) (h : \u2203 k, n = 2*k) : \u2203 l, n+1 = 2*l + 1 :=\nbegin\n  Par h on obtient k hk,\n  use k,\n  rw hk\nend\n\nexample (n : \u2115) (h : \u2203 k, n = 2*k) : \u2203 l, n+1 = 2*l + 1 :=\nbegin\n  Par h on obtient k tel que hk : n = 2*k,\n  use k,\n  rw hk\nend\n\nexample (n : \u2115) (h : \u2203 k, n = 2*k) : \u2203 l, n+1 = 2*l + 1 :=\nbegin\n  success_if_fail { \n    Par h on obtient k tel que (hk : 0 = 1), \n  },\n  Par h on obtient k tel que (hk : n = 2*k),\n  use k,\n  rw hk\nend\n\nexample (f g : \u2115 \u2192 \u2115) (hf : \u2200 y, \u2203 x, f x = y) (hg : \u2200 y, \u2203 x, g x = y) : \u2200 y, \u2203 x, (g \u2218 f) x = y :=\nbegin\n  intro y,\n  success_if_fail { Par hg appliqu\u00e9 \u00e0 y on obtient x tel que (hx : g x = x) },\n  Par hg appliqu\u00e9 \u00e0 y on obtient x tel que (hx : g x = y),\n  Par hf appliqu\u00e9 \u00e0 x on obtient z hz,\n  use z,\n  change g (f z) = y,\n  rw [hz, hx],\nend\n\nexample (P Q : Prop) (h : P \u2227 Q)  : Q :=\nbegin\n  Par h on obtient (hP : P) (hQ : Q),\n  exact hQ,\nend\n\n\nnoncomputable example (f : \u2115 \u2192 \u2115) (h : \u2200 y, \u2203 x, f x = y) : \u2115 \u2192 \u2115 :=\nbegin\n  Par h on choisit g tel que (H : \u2200 (y : \u2115), f (g y) = y),\n  exact g,\nend\n\nexample (P Q : Prop) (h : P \u2192 Q) (h' : P) : Q :=\nbegin\n  Par h il suffit de montrer que P,\n  exact h',\nend\n\nexample (P Q R : Prop) (h : P \u2192 R \u2192 Q) (hP : P) (hR : R) : Q :=\nbegin\n  Par h il suffit de montrer que [P, R],\n  exact hP,\n  exact hR\nend\n\nexample (P Q : Prop) (h : \u2200 n : \u2115, P \u2192 Q) (h' : P) : Q :=\nbegin\n  success_if_fail { Par h appliqu\u00e9 \u00e0 [0, 1] il suffit de montrer que P },\n  Par h appliqu\u00e9 \u00e0 0 il suffit de montrer que P,\n  exact h',\nend\n\nexample (Q : Prop) (h : \u2200 n : \u2124, n > 0 \u2192 Q)  : Q :=\nbegin\n  Par h il suffit de montrer que (1 > 0),\n  norm_num,\nend\n", "meta": {"author": "PatrickMassot", "repo": "MDD154", "sha": "00defe82a4b6b7992ed522a92f62abd685e8c943", "save_path": "github-repos/lean/PatrickMassot-MDD154", "path": "github-repos/lean/PatrickMassot-MDD154/MDD154-00defe82a4b6b7992ed522a92f62abd685e8c943/src/lib/Par.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.5, "lm_q2_score": 0.10669058400428263, "lm_q1q2_score": 0.05334529200214132}}
{"text": "/-\nCopyright (c) 2018 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison, Jannis Limperg\n-/\n\n/-!\n# Monadic instances for `ulift` and `plift`\n\nIn this file we define `monad` and `is_lawful_monad` instances on `plift` and `ulift`. -/\n\nuniverses u v\n\nnamespace plift\n\nvariables {\u03b1 : Sort u} {\u03b2 : Sort v}\n\n/-- Functorial action. -/\nprotected def map (f : \u03b1 \u2192 \u03b2) (a : plift \u03b1) : plift \u03b2 :=\nplift.up (f a.down)\n\n@[simp] \n\n/-- Embedding of pure values. -/\n@[simp] protected def pure : \u03b1 \u2192 plift \u03b1 := up\n\n/-- Applicative sequencing. -/\nprotected def seq (f : plift (\u03b1 \u2192 \u03b2)) (x : plift \u03b1) : plift \u03b2 :=\nplift.up (f.down x.down)\n\n@[simp] lemma seq_up (f : \u03b1 \u2192 \u03b2) (x : \u03b1) : (plift.up f).seq (plift.up x) = plift.up (f x) := rfl\n\n/-- Monadic bind. -/\nprotected def bind (a : plift \u03b1) (f : \u03b1 \u2192 plift \u03b2) : plift \u03b2 := f a.down\n\n@[simp] lemma bind_up (a : \u03b1) (f : \u03b1 \u2192 plift \u03b2) : (plift.up a).bind f = f a := rfl\n\ninstance : monad plift :=\n{ map := @plift.map,\n  pure := @plift.pure,\n  seq := @plift.seq,\n  bind := @plift.bind }\n\ninstance : is_lawful_functor plift :=\n{ id_map := \u03bb \u03b1 \u27e8x\u27e9, rfl,\n  comp_map := \u03bb \u03b1 \u03b2 \u03b3 g h \u27e8x\u27e9, rfl }\n\ninstance : is_lawful_applicative plift :=\n{ pure_seq_eq_map := \u03bb \u03b1 \u03b2 g \u27e8x\u27e9, rfl,\n  map_pure := \u03bb \u03b1 \u03b2 g x, rfl,\n  seq_pure := \u03bb \u03b1 \u03b2 \u27e8g\u27e9 x, rfl,\n  seq_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 \u27e8g\u27e9 \u27e8h\u27e9, rfl }\n\ninstance : is_lawful_monad plift :=\n{ bind_pure_comp_eq_map := \u03bb \u03b1 \u03b2 f \u27e8x\u27e9, rfl,\n  bind_map_eq_seq := \u03bb \u03b1 \u03b2 \u27e8a\u27e9 \u27e8b\u27e9, rfl,\n  pure_bind := \u03bb \u03b1 \u03b2 x f, rfl,\n  bind_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 f g, rfl }\n\n@[simp] lemma rec.constant {\u03b1 : Sort u} {\u03b2 : Type v} (b : \u03b2) :\n  @plift.rec \u03b1 (\u03bb _, \u03b2) (\u03bb _, b) = \u03bb _, b :=\nfunext (\u03bb x, plift.cases_on x (\u03bb a, eq.refl (plift.rec (\u03bb a', b) {down := a})))\n\nend plift\n\n\nnamespace ulift\n\nvariables {\u03b1 : Type u} {\u03b2 : Type v}\n\n/-- Functorial action. -/\nprotected def map (f : \u03b1 \u2192 \u03b2) (a : ulift \u03b1) : ulift \u03b2 :=\nulift.up (f a.down)\n\n@[simp] lemma map_up (f : \u03b1 \u2192 \u03b2) (a : \u03b1) : (ulift.up a).map f = ulift.up (f a) := rfl\n\n/-- Embedding of pure values. -/\n@[simp] protected def pure : \u03b1 \u2192 ulift \u03b1 := up\n\n/-- Applicative sequencing. -/\nprotected def seq (f : ulift (\u03b1 \u2192 \u03b2)) (x : ulift \u03b1) : ulift \u03b2 :=\nulift.up (f.down x.down)\n\n@[simp] lemma seq_up (f : \u03b1 \u2192 \u03b2) (x : \u03b1) : (ulift.up f).seq (ulift.up x) = ulift.up (f x) := rfl\n\n/-- Monadic bind. -/\nprotected def bind (a : ulift \u03b1) (f : \u03b1 \u2192 ulift \u03b2) : ulift \u03b2 := f a.down\n\n@[simp] lemma bind_up (a : \u03b1) (f : \u03b1 \u2192 ulift \u03b2) : (ulift.up a).bind f = f a := rfl\n\ninstance : monad ulift :=\n{ map := @ulift.map,\n  pure := @ulift.pure,\n  seq := @ulift.seq,\n  bind := @ulift.bind }\n\ninstance : is_lawful_functor ulift :=\n{ id_map := \u03bb \u03b1 \u27e8x\u27e9, rfl,\n  comp_map := \u03bb \u03b1 \u03b2 \u03b3 g h \u27e8x\u27e9, rfl }\n\ninstance : is_lawful_applicative ulift :=\n{ to_is_lawful_functor := ulift.is_lawful_functor,\n  pure_seq_eq_map := \u03bb \u03b1 \u03b2 g \u27e8x\u27e9, rfl,\n  map_pure := \u03bb \u03b1 \u03b2 g x, rfl,\n  seq_pure := \u03bb \u03b1 \u03b2 \u27e8g\u27e9 x, rfl,\n  seq_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 \u27e8g\u27e9 \u27e8h\u27e9, rfl }\n\ninstance : is_lawful_monad ulift :=\n{ bind_pure_comp_eq_map := \u03bb \u03b1 \u03b2 f \u27e8x\u27e9, rfl,\n  bind_map_eq_seq := \u03bb \u03b1 \u03b2 \u27e8a\u27e9 \u27e8b\u27e9, rfl,\n  pure_bind := \u03bb \u03b1 \u03b2 x f,\n    by { dsimp only [bind, pure, ulift.pure, ulift.bind], cases (f x), refl },\n  bind_assoc := \u03bb \u03b1 \u03b2 \u03b3 \u27e8x\u27e9 f g,\n    by { dsimp only [bind, pure, ulift.pure, ulift.bind], cases (f x), refl } }\n\n@[simp] lemma rec.constant {\u03b1 : Type u} {\u03b2 : Sort v} (b : \u03b2) :\n  @ulift.rec \u03b1 (\u03bb _, \u03b2) (\u03bb _, b) = \u03bb _, b :=\nfunext (\u03bb x, ulift.cases_on x (\u03bb a, eq.refl (ulift.rec (\u03bb a', b) {down := a})))\n\nend ulift\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/control/ulift.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.48438006939036565, "lm_q2_score": 0.1097057753333734, "lm_q1q2_score": 0.05313929106850327}}
{"text": "/-\nCopyright (c) 2020 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison\n-/\nimport tactic.core\n\nopen tactic\n\nnamespace tactic.interactive\n\n/--\n`show_term { tac }` runs the tactic `tac`,\nand then prints the term that was constructed.\n\nThis is useful for\n* constructing term mode proofs from tactic mode proofs, and\n* understanding what tactics are doing, and how metavariables are handled.\n\nAs an example, in\n```\nexample {P Q R : Prop} (h\u2081 : Q \u2192 P) (h\u2082 : R) (h\u2083 : R \u2192 Q) : P \u2227 R :=\nby show_term { tauto }\n```\nthe term mode proof `\u27e8h\u2081 (h\u2083 h\u2082), eq.mpr rfl h\u2082\u27e9` produced by `tauto` will be printed.\n\nAs another example, if the goal is `\u2115 \u00d7 \u2115`, `show_term { split, exact 0 }` will\nprint `refine (0, _)`, and afterwards there will be one remaining goal (of type `\u2115`).\nThis indicates that `split, exact 0` partially filled in the original metavariable,\nbut created a new metavariable for the resulting sub-goal.\n-/\nmeta def show_term (t : itactic) : itactic :=\ndo\n  g :: _ \u2190 get_goals,\n  t,\n  g \u2190 tactic_statement g,\n  trace g\n\nadd_tactic_doc\n{ name := \"show_term\",\n  category := doc_category.tactic,\n  decl_names := [``show_term],\n  tags := [\"debugging\"] }\n\nend tactic.interactive\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/show_term.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.38861802670584894, "lm_q2_score": 0.13660840408654298, "lm_q1q2_score": 0.05308848842754756}}
{"text": "/-\nCopyright (c) 2017 Mario Carneiro. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro\n-/\nimport data.buffer.parser\nimport tactic.core\n\n/-!\n# The `alias` command\n\nThis file defines an `alias` command, which can be used to create copies\nof a theorem or definition with different names.\n\nSyntax:\n\n```lean\n/-- doc string -/\nalias my_theorem \u2190 alias1 alias2 ...\n```\n\nThis produces defs or theorems of the form:\n\n```lean\n/-- doc string -/\n@[alias] theorem alias1 : <type of my_theorem> := my_theorem\n\n/-- doc string -/\n@[alias] theorem alias2 : <type of my_theorem> := my_theorem\n```\n\nIff alias syntax:\n\n```lean\nalias A_iff_B \u2194 B_of_A A_of_B\nalias A_iff_B \u2194 ..\n```\n\nThis gets an existing biconditional theorem `A_iff_B` and produces\nthe one-way implications `B_of_A` and `A_of_B` (with no change in\nimplicit arguments). A blank `_` can be used to avoid generating one direction.\nThe `..` notation attempts to generate the 'of'-names automatically when the\ninput theorem has the form `A_iff_B` or `A_iff_B_left` etc.\n-/\n\nopen lean.parser tactic interactive parser\n\nnamespace tactic.alias\n\n@[user_attribute] meta def alias_attr : user_attribute :=\n{ name := `alias, descr := \"This definition is an alias of another.\" }\n\nmeta def alias_direct (d : declaration) (doc : string) (al : name) : tactic unit :=\ndo updateex_env $ \u03bb env,\n  env.add (match d.to_definition with\n  | declaration.defn n ls t _ _ _ :=\n    declaration.defn al ls t (expr.const n (level.param <$> ls))\n      reducibility_hints.abbrev tt\n  | declaration.thm n ls t _ :=\n    declaration.thm al ls t $ task.pure $ expr.const n (level.param <$> ls)\n  | _ := undefined\n  end),\n  alias_attr.set al () tt,\n  add_doc_string al doc\n\nmeta def mk_iff_mp_app (iffmp : name) : expr \u2192 (\u2115 \u2192 expr) \u2192 tactic expr\n| (expr.pi n bi e t) f := expr.lam n bi e <$> mk_iff_mp_app t (\u03bb n, f (n+1) (expr.var n))\n| `(%%a \u2194 %%b) f := pure $ @expr.const tt iffmp [] a b (f 0)\n| _ f := fail \"Target theorem must have the form `\u03a0 x y z, a \u2194 b`\"\n\nmeta def alias_iff (d : declaration) (doc : string) (al : name) (iffmp : name) : tactic unit :=\n(if al = `_ then skip else get_decl al >> skip) <|> do\n  let ls := d.univ_params,\n  let t := d.type,\n  v \u2190 mk_iff_mp_app iffmp t (\u03bb_, expr.const d.to_name (level.param <$> ls)),\n  t' \u2190 infer_type v,\n  updateex_env $ \u03bb env, env.add (declaration.thm al ls t' $ task.pure v),\n  alias_attr.set al () tt,\n  add_doc_string al doc\n\nmeta def make_left_right : name \u2192 tactic (name \u00d7 name)\n| (name.mk_string s p) := do\n  let buf : char_buffer := s.to_char_buffer,\n  sum.inr parts \u2190 pure $ run (sep_by1 (ch '_') (many_char (sat (\u2260 '_')))) s.to_char_buffer,\n  (left, _::right) \u2190 pure $ parts.span (\u2260 \"iff\"),\n  let pfx (a b : string) := a.to_list.is_prefix_of b.to_list,\n  (suffix', right') \u2190 pure $ right.reverse.span (\u03bb s, pfx \"left\" s \u2228 pfx \"right\" s),\n  let right := right'.reverse,\n  let suffix := suffix'.reverse,\n  pure (p <.> \"_\".intercalate (right ++ \"of\" :: left ++ suffix),\n        p <.> \"_\".intercalate (left ++ \"of\" :: right ++ suffix))\n| _ := failed\n\n/--\nThe `alias` command can be used to create copies\nof a theorem or definition with different names.\n\nSyntax:\n\n```lean\n/-- doc string -/\nalias my_theorem \u2190 alias1 alias2 ...\n```\n\nThis produces defs or theorems of the form:\n\n```lean\n/-- doc string -/\n@[alias] theorem alias1 : <type of my_theorem> := my_theorem\n\n/-- doc string -/\n@[alias] theorem alias2 : <type of my_theorem> := my_theorem\n```\n\nIff alias syntax:\n\n```lean\nalias A_iff_B \u2194 B_of_A A_of_B\nalias A_iff_B \u2194 ..\n```\n\nThis gets an existing biconditional theorem `A_iff_B` and produces\nthe one-way implications `B_of_A` and `A_of_B` (with no change in\nimplicit arguments). A blank `_` can be used to avoid generating one direction.\nThe `..` notation attempts to generate the 'of'-names automatically when the\ninput theorem has the form `A_iff_B` or `A_iff_B_left` etc.\n-/\n@[user_command] meta def alias_cmd (meta_info : decl_meta_info)\n  (_ : parse $ tk \"alias\") : lean.parser unit :=\ndo old \u2190 ident,\n  d \u2190 (do old \u2190 resolve_constant old, get_decl old) <|>\n    fail (\"declaration \" ++ to_string old ++ \" not found\"),\n  let doc := \u03bb al : name, meta_info.doc_string.get_or_else $\n    \"**Alias** of `\" ++ to_string old ++ \"`.\",\n  do {\n    tk \"\u2190\" <|> tk \"<-\",\n    aliases \u2190 many ident,\n    \u2191(aliases.mmap' $ \u03bb al, alias_direct d (doc al) al) } <|>\n  do {\n    tk \"\u2194\" <|> tk \"<->\",\n    (left, right) \u2190\n      mcond ((tk \".\" *> tk \".\" >> pure tt) <|> pure ff)\n        (make_left_right old <|> fail \"invalid name for automatic name generation\")\n        (prod.mk <$> types.ident_ <*> types.ident_),\n    alias_iff d (doc left) left `iff.mp,\n    alias_iff d (doc right) right `iff.mpr }\n\nadd_tactic_doc\n{ name                     := \"alias\",\n  category                 := doc_category.cmd,\n  decl_names               := [`tactic.alias.alias_cmd],\n  tags                     := [\"renaming\"] }\n\nmeta def get_lambda_body : expr \u2192 expr\n| (expr.lam _ _ _ b) := get_lambda_body b\n| a                  := a\n\nmeta def get_alias_target (n : name) : tactic (option name) :=\ndo tt \u2190 has_attribute' `alias n | pure none,\n  d \u2190 get_decl n,\n  let (head, args) := (get_lambda_body d.value).get_app_fn_args,\n  let head := if head.is_constant_of `iff.mp \u2228 head.is_constant_of `iff.mpr then\n    expr.get_app_fn (head.ith_arg 2)\n  else head,\n  guardb $ head.is_constant,\n  pure $ head.const_name\n\nend tactic.alias\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/tactic/alias.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.45713671682749485, "lm_q2_score": 0.11596073047456816, "lm_q1q2_score": 0.05300990761006212}}
{"text": "/-\nCopyright (c) 2022 Floris van Doorn. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Floris van Doorn\n-/\n\nimport Mathlib.Init.Data.Nat.Notation\nimport Mathlib.Lean.Message\nimport Mathlib.Lean.Expr.Basic\nimport Mathlib.Data.String.Defs\nimport Mathlib.Data.KVMap\nimport Mathlib.Tactic.Simps.NotationClass\nimport Std.Classes.Dvd\nimport Std.Util.LibraryNote\nimport Mathlib.Tactic.RunCmd -- not necessary, but useful for debugging\nimport Mathlib.Lean.Linter\n\n/-!\n# Simps attribute\n\nThis file defines the `@[simps]` attribute, to automatically generate `simp` lemmas\nreducing a definition when projections are applied to it.\n\n## Implementation Notes\n\nThere are three attributes being defined here\n* `@[simps]` is the attribute for objects of a structure or instances of a class. It will\n  automatically generate simplification lemmas for each projection of the object/instance that\n  contains data. See the doc strings for `Lean.Parser.Attr.simps` and `Simps.Config`\n  for more details and configuration options.\n* `structureExt` (just an environment extension, not actually an attribute)\n  is automatically added to structures that have been used in `@[simps]`\n  at least once. This attribute contains the data of the projections used for this structure\n  by all following invocations of `@[simps]`.\n* `@[notation_class]` should be added to all classes that define notation, like `Mul` and\n  `Zero`. This specifies that the projections that `@[simps]` used are the projections from\n  these notation classes instead of the projections of the superclasses.\n  Example: if `Mul` is tagged with `@[notation_class]` then the projection used for `Semigroup`\n  will be `fun \u03b1 h\u03b1 \u21a6 @Mul.mul \u03b1 (@Semigroup.toMul \u03b1 h\u03b1)` instead of `@Semigroup.mul`.\n  [this is not correctly implemented in Lean 4 yet]\n\n### Possible Future Improvements\n* If multiple declarations are generated from a `simps` without explicit projection names, then\n  only the first one is shown when mousing over `simps`.\n\n## Changes w.r.t. Lean 3\n\nThere are some small changes in the attribute. None of them should have great effects\n* The attribute will now raise an error if it tries to generate a lemma when there already exists\n  a lemma with that name (in Lean 3 it would generate a different unique name)\n* `transparency.none` has been replaced by `TransparencyMode.reducible`\n* The `attr` configuration option has been split into `isSimp` and `attrs` (for extra attributes)\n* Because Lean 4 uses bundled structures, this means that `simps` applied to anything that\n  implements a notation class will almost certainly require a user-provided custom simps projection.\n\n## Tags\n\nstructures, projections, simp, simplifier, generates declarations\n-/\nopen Lean Elab Parser Command\nopen Meta hiding Config\nopen Elab.Term hiding mkConst\n\n/-- `updateName nm s isPrefix` adds `s` to the last component of `nm`,\n  either as prefix or as suffix (specified by `isPrefix`), separated by `_`.\n  Used by `simps_add_projections`. -/\ndef updateName (nm : Name) (s : String) (isPrefix : Bool) : Name :=\n  nm.updateLast fun s' \u21a6 if isPrefix then s ++ \"_\" ++ s' else s' ++ \"_\" ++ s\n\n-- move\nnamespace Lean.Meta\nopen Tactic Simp\n/-- Make `MkSimpContextResult` giving data instead of Syntax. Doesn't support arguments.\nIntended to be very similar to `Lean.Elab.Tactic.mkSimpContext`\nTodo: support arguments. -/\ndef mkSimpContextResult (cfg : Meta.Simp.Config := {}) (simpOnly := false) (kind := SimpKind.simp)\n    (dischargeWrapper := DischargeWrapper.default) (hasStar := false) :\n    MetaM MkSimpContextResult := do\n  match dischargeWrapper with\n  | .default => pure ()\n  | _ =>\n    if kind == SimpKind.simpAll then\n      throwError \"'simp_all' tactic does not support 'discharger' option\"\n    if kind == SimpKind.dsimp then\n      throwError \"'dsimp' tactic does not support 'discharger' option\"\n  let simpTheorems \u2190 if simpOnly then\n    simpOnlyBuiltins.foldlM (\u00b7.addConst \u00b7) ({} : SimpTheorems)\n  else\n    getSimpTheorems\n  let congrTheorems \u2190 getSimpCongrTheorems\n  let ctx : Simp.Context := {\n    config      := cfg\n    simpTheorems := #[simpTheorems], congrTheorems\n  }\n  if !hasStar then\n    return { ctx, dischargeWrapper }\n  else\n    let mut simpTheorems := ctx.simpTheorems\n    let hs \u2190 getPropHyps\n    for h in hs do\n      unless simpTheorems.isErased (.fvar h) do\n        simpTheorems \u2190 simpTheorems.addTheorem (.fvar h) (\u2190 h.getDecl).toExpr\n    let ctx := { ctx with simpTheorems }\n    return { ctx, dischargeWrapper }\n\n/-- Make `Simp.Context` giving data instead of Syntax. Doesn't support arguments.\nIntended to be very similar to `Lean.Elab.Tactic.mkSimpContext`\nTodo: support arguments. -/\ndef mkSimpContext (cfg : Meta.Simp.Config := {}) (simpOnly := false) (kind := SimpKind.simp)\n    (dischargeWrapper := DischargeWrapper.default) (hasStar := false) :\n    MetaM Simp.Context := do\n  let data \u2190 mkSimpContextResult cfg simpOnly kind dischargeWrapper hasStar\n  return data.ctx\n\nend Lean.Meta\n\n/-- Tests whether `declName` has the `@[simp]` attribute in `env`. -/\ndef hasSimpAttribute (env : Environment) (declName : Name) : Bool :=\n  simpExtension.getState env |>.lemmaNames.contains <| .decl declName\n\nnamespace Lean.Parser\nnamespace Attr\n\n\n/-! Declare notation classes. -/\nattribute [notation_class add] HAdd\nattribute [notation_class mul] HMul\nattribute [notation_class sub] HSub\nattribute [notation_class div] HDiv\nattribute [notation_class mod] HMod\nattribute [notation_class append] HAppend\nattribute [notation_class pow Simps.copyFirst] HPow\nattribute [notation_class andThen] HAndThen\nattribute [notation_class] Neg Dvd LE LT HasEquiv HasSubset HasSSubset Union Inter SDiff Insert\n  Singleton Sep Membership\nattribute [notation_class one Simps.findOneArgs] OfNat\nattribute [notation_class zero Simps.findZeroArgs] OfNat\n\n/-- arguments to `@[simps]` attribute. -/\nsyntax simpsArgsRest := (Tactic.config)? (ppSpace ident)*\n\n/-- The `@[simps]` attribute automatically derives lemmas specifying the projections of this\ndeclaration.\n\nExample:\n```lean\n@[simps] def foo : \u2115 \u00d7 \u2124 := (1, 2)\n```\nderives two `simp` lemmas:\n```lean\n@[simp] lemma foo_fst : foo.fst = 1\n@[simp] lemma foo_snd : foo.snd = 2\n```\n\n* It does not derive `simp` lemmas for the prop-valued projections.\n* It will automatically reduce newly created beta-redexes, but will not unfold any definitions.\n* If the structure has a coercion to either sorts or functions, and this is defined to be one\n  of the projections, then this coercion will be used instead of the projection.\n* If the structure is a class that has an instance to a notation class, like `Neg` or `Mul`,\n  then this notation is used instead of the corresponding projection.\n* You can specify custom projections, by giving a declaration with name\n  `{StructureName}.Simps.{projectionName}`. See Note [custom simps projection].\n\n  Example:\n  ```lean\n  def Equiv.Simps.invFun (e : \u03b1 \u2243 \u03b2) : \u03b2 \u2192 \u03b1 := e.symm\n  @[simps] def Equiv.trans (e\u2081 : \u03b1 \u2243 \u03b2) (e\u2082 : \u03b2 \u2243 \u03b3) : \u03b1 \u2243 \u03b3 :=\n  \u27e8e\u2082 \u2218 e\u2081, e\u2081.symm \u2218 e\u2082.symm\u27e9\n  ```\n  generates\n  ```\n  @[simp] lemma Equiv.trans_toFun : \u2200 {\u03b1 \u03b2 \u03b3} (e\u2081 e\u2082) (a : \u03b1), \u21d1(e\u2081.trans e\u2082) a = (\u21d1e\u2082 \u2218 \u21d1e\u2081) a\n  @[simp] lemma Equiv.trans_invFun : \u2200 {\u03b1 \u03b2 \u03b3} (e\u2081 e\u2082) (a : \u03b3),\n    \u21d1((e\u2081.trans e\u2082).symm) a = (\u21d1(e\u2081.symm) \u2218 \u21d1(e\u2082.symm)) a\n  ```\n\n* You can specify custom projection names, by specifying the new projection names using\n  `initialize_simps_projections`.\n  Example: `initialize_simps_projections Equiv (toFun \u2192 apply, invFun \u2192 symm_apply)`.\n  See `initialize_simps_projections` for more information.\n\n* If one of the fields itself is a structure, this command will recursively create\n  `simp` lemmas for all fields in that structure.\n  * Exception: by default it will not recursively create `simp` lemmas for fields in the structures\n    `Prod` and `PProd`. You can give explicit projection names or change the value of\n    `Simps.Config.notRecursive` to override this behavior.\n\n  Example:\n  ```lean\n  structure MyProd (\u03b1 \u03b2 : Type _) := (fst : \u03b1) (snd : \u03b2)\n  @[simps] def foo : Prod \u2115 \u2115 \u00d7 MyProd \u2115 \u2115 := \u27e8\u27e81, 2\u27e9, 3, 4\u27e9\n  ```\n  generates\n  ```lean\n  @[simp] lemma foo_fst : foo.fst = (1, 2)\n  @[simp] lemma foo_snd_fst : foo.snd.fst = 3\n  @[simp] lemma foo_snd_snd : foo.snd.snd = 4\n  ```\n\n* You can use `@[simps proj1 proj2 ...]` to only generate the projection lemmas for the specified\n  projections.\n* Recursive projection names can be specified using `proj1_proj2_proj3`.\n  This will create a lemma of the form `foo.proj1.proj2.proj3 = ...`.\n\n  Example:\n  ```lean\n  structure MyProd (\u03b1 \u03b2 : Type _) := (fst : \u03b1) (snd : \u03b2)\n  @[simps fst fst_fst snd] def foo : Prod \u2115 \u2115 \u00d7 MyProd \u2115 \u2115 := \u27e8\u27e81, 2\u27e9, 3, 4\u27e9\n  ```\n  generates\n  ```lean\n  @[simp] lemma foo_fst : foo.fst = (1, 2)\n  @[simp] lemma foo_fst_fst : foo.fst.fst = 1\n  @[simp] lemma foo_snd : foo.snd = {fst := 3, snd := 4}\n  ```\n* If one of the values is an eta-expanded structure, we will eta-reduce this structure.\n\n  Example:\n  ```lean\n  structure EquivPlusData (\u03b1 \u03b2) extends \u03b1 \u2243 \u03b2 where\n    data : Bool\n  @[simps] def EquivPlusData.rfl {\u03b1} : EquivPlusData \u03b1 \u03b1 := { Equiv.refl \u03b1 with data := true }\n  ```\n  generates the following:\n  ```lean\n  @[simp] lemma bar_toEquiv : \u2200 {\u03b1 : Sort*}, bar.toEquiv = Equiv.refl \u03b1\n  @[simp] lemma bar_data : \u2200 {\u03b1 : Sort*}, bar.data = true\n  ```\n  This is true, even though Lean inserts an eta-expanded version of `Equiv.refl \u03b1` in the\n  definition of `bar`.\n* For configuration options, see the doc string of `Simps.Config`.\n* The precise syntax is `simps (config := e)? ident*`, where `e : Expr` is an expression of type\n  `Simps.Config` and `ident*` is a list of desired projection names.\n* `@[simps]` reduces let-expressions where necessary.\n* When option `trace.simps.verbose` is true, `simps` will print the projections it finds and the\n  lemmas it generates. The same can be achieved by using `@[simps?]`.\n* Use `@[to_additive (attr := simps)]` to apply both `to_additive` and `simps` to a definition\n  This will also generate the additive versions of all `simp` lemmas.\n-/\n/- If one of the fields is a partially applied constructor, we will eta-expand it\n  (this likely never happens, so is not included in the official doc). -/\nsyntax (name := simps) \"simps\" \"!\"? \"?\"? simpsArgsRest : attr\n\n@[inherit_doc simps] macro \"simps?\"  rest:simpsArgsRest : attr => `(attr| simps   ? $rest)\n@[inherit_doc simps] macro \"simps!\"  rest:simpsArgsRest : attr => `(attr| simps !   $rest)\n@[inherit_doc simps] macro \"simps!?\" rest:simpsArgsRest : attr => `(attr| simps ! ? $rest)\n@[inherit_doc simps] macro \"simps?!\" rest:simpsArgsRest : attr => `(attr| simps ! ? $rest)\n\nend Attr\n\n/-- Linter to check that `simps!` is used when needed -/\nregister_option linter.simpsNoConstructor : Bool := {\n  defValue := true\n  descr := \"Linter to check that `simps!` is used\" }\n\n/-- Linter to check that no unused custom declarations are declared for simps. -/\nregister_option linter.simpsUnusedCustomDeclarations : Bool := {\n  defValue := true\n  descr := \"Linter to check that no unused custom declarations are declared for simps\" }\n\nnamespace Command\n\n/-- Syntax for renaming a projection in `initialize_simps_projections`. -/\nsyntax simpsRule.rename := ident \" \u2192 \" ident\n/-- Syntax for making a  projection non-default in `initialize_simps_projections`. -/\nsyntax simpsRule.erase := \"-\" ident\n/-- Syntax for making a projection default in `initialize_simps_projections`. -/\nsyntax simpsRule.add := \"+\" ident\n/-- Syntax for making a projection prefix. -/\nsyntax simpsRule.prefix := &\"as_prefix\" ident\n/-- Syntax for a single rule in `initialize_simps_projections`. -/\nsyntax simpsRule := simpsRule.prefix <|> simpsRule.rename <|> simpsRule.erase <|> simpsRule.add\n/-- Syntax for `initialize_simps_projections`. -/\nsyntax simpsProj := (ppSpace ident (\" (\" simpsRule,+ \")\")?)\n\n/--\nThis command specifies custom names and custom projections for the simp attribute `simpsAttr`.\n* You can specify custom names by writing e.g.\n  `initialize_simps_projections Equiv (toFun \u2192 apply, invFun \u2192 symm_apply)`.\n* See Note [custom simps projection] and the examples below for information how to declare custom\n  projections.\n* For algebraic structures, we will automatically use the notation (like `Mul`)\n  for the projections if such an instance is available.\n* By default, the projections to parent structures are not default projections,\n  but all the data-carrying fields are (including those in parent structures).\n* You can disable a projection by default by running\n  `initialize_simps_projections Equiv (-invFun)`\n  This will ensure that no simp lemmas are generated for this projection,\n  unless this projection is explicitly specified by the user.\n* Conversely, you can enable a projection by default by running\n  `initialize_simps_projections Equiv (+toEquiv)`.\n* If you want the projection name added as a prefix in the generated lemma name, you can use\n  `as_prefix fieldName`:\n  `initialize_simps_projections Equiv (toFun \u2192 coe, as_prefix coe)`\n  Note that this does not influence the parsing of projection names: if you have a declaration\n  `foo` and you want to apply the projections `snd`, `coe` (which is a prefix) and `fst`, in that\n  order you can run `@[simps snd_coe_fst] def foo ...` and this will generate a lemma with the\n  name `coe_foo_snd_fst`.\n  * Run `initialize_simps_projections?` (or `set_option trace.simps.verbose true`)\n  to see the generated projections.\n* Running `initialize_simps_projections MyStruc` without arguments is not necessary, it has the\n  same effect if you just add `@[simps]` to a declaration.\n* It is recommended to call `@[simps]` or `initialize_simps_projections` in the same file as the\n  structure declaration. Otherwise, the projections could be generated multiple times in different\n  files.\n\nSome common uses:\n* If you define a new homomorphism-like structure (like `MulHom`) you can just run\n  `initialize_simps_projections` after defining the `FunLike` instance (or instance that implies\n  a `FunLike` instance).\n  ```\n    instance {mM : Mul M} {mN : Mul N} : FunLike (MulHom M N) M N := ...\n    initialize_simps_projections MulHom (toFun \u2192 apply)\n  ```\n  This will generate `foo_apply` lemmas for each declaration `foo`.\n* If you prefer `coe_foo` lemmas that state equalities between functions, use\n  `initialize_simps_projections MulHom (toFun \u2192 coe, as_prefix coe)`\n  In this case you have to use `@[simps (config := {fullyApplied := false})]` or equivalently\n  `@[simps (config := .asFn)]` whenever you call `@[simps]`.\n* You can also initialize to use both, in which case you have to choose which one to use by default,\n  by using either of the following\n  ```\n    initialize_simps_projections MulHom (toFun \u2192 apply, toFun \u2192 coe, as_prefix coe, -coe)\n    initialize_simps_projections MulHom (toFun \u2192 apply, toFun \u2192 coe, as_prefix coe, -apply)\n  ```\n  In the first case, you can get both lemmas using `@[simps, simps (config := .asFn) coe]` and in\n  the second case you can get both lemmas using `@[simps (config := .asFn), simps apply]`.\n* If you declare a new homomorphism-like structure (like `RelEmbedding`),\n  then `initialize_simps_projections` will automatically find any `FunLike` coercions\n  that will be used as the default projection for the `toFun` field.\n  ```\n    initialize_simps_projections relEmbedding (toFun \u2192 apply)\n  ```\n* If you have an isomorphism-like structure (like `Equiv`) you often want to define a custom\n  projection for the inverse:\n  ```\n    def Equiv.Simps.symm_apply (e : \u03b1 \u2243 \u03b2) : \u03b2 \u2192 \u03b1 := e.symm\n    initialize_simps_projections Equiv (toFun \u2192 apply, invFun \u2192 symm_apply)\n  ```\n-/\nsyntax (name := initialize_simps_projections)\n  \"initialize_simps_projections\" \"?\"? simpsProj : command\n\n@[inherit_doc \u00abinitialize_simps_projections\u00bb]\nmacro \"initialize_simps_projections?\" rest:simpsProj : command =>\n  `(initialize_simps_projections ? $rest)\n\nend Command\nend Lean.Parser\n\ninitialize registerTraceClass `simps.verbose\ninitialize registerTraceClass `simps.debug\n\nnamespace Simps\n\n/-- Projection data for a single projection of a structure -/\nstructure ProjectionData where\n  /-- The name used in the generated `simp` lemmas -/\n  name : Name\n  /-- An Expression used by simps for the projection. It must be definitionally equal to an original\n  projection (or a composition of multiple projections).\n  These Expressions can contain the universe parameters specified in the first argument of\n  `structureExt`. -/\n  expr : Expr\n  /-- A list of natural numbers, which is the projection number(s) that have to be applied to the\n  Expression. For example the list `[0, 1]` corresponds to applying the first projection of the\n  structure, and then the second projection of the resulting structure (this assumes that the\n  target of the first projection is a structure with at least two projections).\n  The composition of these projections is required to be definitionally equal to the provided\n  Expression. -/\n  projNrs : List \u2115\n  /-- A boolean specifying whether `simp` lemmas are generated for this projection by default. -/\n  isDefault : Bool\n  /-- A boolean specifying whether this projection is written as prefix. -/\n  isPrefix : Bool\n  deriving Inhabited\n\ninstance : ToMessageData ProjectionData where toMessageData\n  | \u27e8a, b, c, d, e\u27e9 => .group <| .nest 1 <|\n    \"\u27e8\" ++ .joinSep [toMessageData a, toMessageData b, toMessageData c, toMessageData d,\n      toMessageData e] (\",\" ++ Format.line) ++ \"\u27e9\"\n\n/--\nThe `Simps.structureExt` environment extension specifies the preferred projections of the given\nstructure, used by the `@[simps]` attribute.\n- You can generate this with the command `initialize_simps_projections`.\n- If not generated, the `@[simps]` attribute will generate this automatically.\n- To change the default value, see Note [custom simps projection].\n- The first argument is the list of names of the universe variables used in the structure\n- The second argument is an array that consists of the projection data for each projection.\n-/\ninitialize structureExt : NameMapExtension (List Name \u00d7 Array ProjectionData) \u2190\n  registerNameMapExtension (List Name \u00d7 Array ProjectionData)\n\n/-- Projection data used internally in `getRawProjections`. -/\nstructure ParsedProjectionData where\n  /-- name for this projection used in the structure definition -/\n  strName : Name\n  /-- syntax that might have provided `strName` -/\n  strStx : Syntax := .missing\n  /-- name for this projection used in the generated `simp` lemmas -/\n  newName : Name\n  /-- syntax that provided `newName` -/\n  newStx : Syntax := .missing\n  /-- will simp lemmas be generated for with (without specifically naming this?) -/\n  isDefault : Bool := true\n  /-- is the projection name a prefix? -/\n  isPrefix : Bool := false\n  /-- projection expression -/\n  expr? : Option Expr := none\n  /-- the list of projection numbers this expression corresponds to -/\n  projNrs : Array Nat := #[]\n  /-- is this a projection that is changed by the user? -/\n  isCustom : Bool := false\n\n/-- Turn `ParsedProjectionData` into `ProjectionData`. -/\ndef ParsedProjectionData.toProjectionData (p : ParsedProjectionData) : ProjectionData :=\n  { p with name := p.newName, expr := p.expr?.getD default, projNrs := p.projNrs.toList }\n\ninstance : ToMessageData ParsedProjectionData where toMessageData\n  | \u27e8x\u2081, x\u2082, x\u2083, x\u2084, x\u2085, x\u2086, x\u2087, x\u2088, x\u2089\u27e9 => .group <| .nest 1 <|\n    \"\u27e8\" ++ .joinSep [toMessageData x\u2081, toMessageData x\u2082, toMessageData x\u2083, toMessageData x\u2084,\n      toMessageData x\u2085, toMessageData x\u2086, toMessageData x\u2087, toMessageData x\u2088, toMessageData x\u2089]\n    (\",\" ++ Format.line) ++ \"\u27e9\"\n\n/-- The type of rules that specify how metadata for projections in changes.\n  See `initialize_simps_projections`. -/\ninductive ProjectionRule where\n  /-- A renaming rule `before\u2192after` or\n    Each name comes with the syntax used to write the rule,\n    which is used to declare hover information. -/\n  | rename (oldName : Name) (oldStx : Syntax) (newName : Name) (newStx : Syntax) :\n      ProjectionRule\n  /-- A adding rule `+fieldName` -/\n  | add : Name \u2192 Syntax \u2192 ProjectionRule\n  /-- A hiding rule `-fieldName` -/\n  | erase : Name \u2192 Syntax \u2192 ProjectionRule\n  /-- A prefix rule `prefix fieldName` -/\n  | prefix : Name \u2192 Syntax \u2192 ProjectionRule\n\ninstance : ToMessageData ProjectionRule where toMessageData\n  | .rename x\u2081 x\u2082 x\u2083 x\u2084 => .group <| .nest 1 <|\n    \"rename \u27e8\" ++ .joinSep [toMessageData x\u2081, toMessageData x\u2082, toMessageData x\u2083, toMessageData x\u2084]\n      (\",\" ++ Format.line) ++ \"\u27e9\"\n  | .add x\u2081 x\u2082 => .group <| .nest 1 <|\n    \"+\u27e8\" ++ .joinSep [toMessageData x\u2081, toMessageData x\u2082] (\",\" ++ Format.line) ++ \"\u27e9\"\n  | .erase x\u2081 x\u2082 => .group <| .nest 1 <|\n    \"-\u27e8\" ++ .joinSep [toMessageData x\u2081, toMessageData x\u2082] (\",\" ++ Format.line) ++ \"\u27e9\"\n  | .prefix x\u2081 x\u2082 => .group <| .nest 1 <|\n    \"prefix \u27e8\" ++ .joinSep [toMessageData x\u2081, toMessageData x\u2082] (\",\" ++ Format.line) ++ \"\u27e9\"\n\n/-- Returns the projection information of a structure. -/\ndef projectionsInfo (l : List ProjectionData) (pref : String) (str : Name) : MessageData :=\n  let \u27e8defaults, nondefaults\u27e9 := l.partition (\u00b7.isDefault)\n  let toPrint : List MessageData :=\n    defaults.map fun s \u21a6\n      let prefixStr := if s.isPrefix then \"(prefix) \" else \"\"\n      m!\"Projection {prefixStr}{s.name}: {s.expr}\"\n  let print2 : MessageData :=\n    String.join <| (nondefaults.map fun nm : ProjectionData \u21a6 toString nm.1).intersperse \", \"\n  let toPrint :=\n    toPrint ++\n      if nondefaults.isEmpty then [] else\n      [(\"No lemmas are generated for the projections: \" : MessageData) ++ print2 ++ \".\"]\n  let toPrint := MessageData.joinSep toPrint (\"\\n\" : MessageData)\n  m!\"{pref} {str}:\\n{toPrint}\"\n\n/-- Find the indices of the projections that need to be applied to elaborate `$e.$projName`.\nExample: If `e : \u03b1 \u2243+ \u03b2` and ``projName = `invFun`` then this returns `[0, 1]`, because the first\nprojection of `MulEquiv` is `toEquiv` and the second projection of `Equiv` is `invFun`. -/\ndef findProjectionIndices (strName projName : Name) : MetaM (List \u2115) := do\n  let env \u2190 getEnv\n  let .some baseStr := findField? env strName projName |\n    throwError \"{strName} has no field {projName} in parent structure\"\n  let .some fullProjName := getProjFnForField? env baseStr projName |\n    throwError \"no such field {projName}\"\n  let .some pathToField := getPathToBaseStructure? env baseStr strName |\n    throwError \"no such field {projName}\"\n  let allProjs := pathToField ++ [fullProjName]\n  return allProjs.map (env.getProjectionFnInfo? \u00b7 |>.get!.i)\n\n/-- Auxiliary function of `getCompositeOfProjections`. -/\npartial def getCompositeOfProjectionsAux\n    (proj : String) (e : Expr) (pos : Array \u2115) (args : Array Expr) : MetaM (Expr \u00d7 Array \u2115) := do\n  let env \u2190 getEnv\n  let .const structName _ := (\u2190 whnf (\u2190inferType e)).getAppFn |\n    throwError \"{e} doesn't have a structure as type\"\n  let projs := getStructureFieldsFlattened env structName\n  let projInfo := projs.toList.map fun p \u21a6 do\n    (\u2190 (p.getString ++ \"_\").isPrefixOf? proj, p)\n  let some (projRest, projName) := projInfo.reduceOption.getLast? |\n    throwError \"Failed to find constructor {proj.dropRight 1} in structure {structName}.\"\n  let newE \u2190 mkProjection e projName\n  let newPos := pos ++ (\u2190 findProjectionIndices structName projName)\n  -- we do this here instead of in a recursive call in order to not get an unnecessary eta-redex\n  if projRest.isEmpty then\n    let newE \u2190 mkLambdaFVars args newE\n    return (newE, newPos)\n  let type \u2190 inferType newE\n  forallTelescopeReducing type fun typeArgs _tgt \u21a6 do\n    getCompositeOfProjectionsAux projRest (mkAppN newE typeArgs) newPos (args ++ typeArgs)\n\n/-- Suppose we are given a structure `str` and a projection `proj`, that could be multiple nested\n  projections (separated by `_`), where each projection could be a projection of a parent structure.\n  This function returns an expression that is the composition of these projections and a\n  list of natural numbers, that are the projection numbers of the applied projections.\n  Note that this function is similar to elaborating dot notation, but it can do a little more.\n  Example: if we do\n  ```\n  structure gradedFun (A : \u2115 \u2192 Type _) where\n    toFun := \u2200 i j, A i \u2192+ A j \u2192+ A (i + j)\n  initialize_simps_projections (toFun_toFun_toFun \u2192 myMul)\n  ```\n  we will be able to generate the \"projection\"\n    `\u03bb {A} (f : gradedFun A) (x : A i) (y : A j) \u21a6 \u2191(\u2191(f.toFun i j) x) y`,\n  which projection notation cannot do. -/\ndef getCompositeOfProjections (structName : Name) (proj : String) : MetaM (Expr \u00d7 Array \u2115) := do\n  let strExpr \u2190 mkConstWithLevelParams structName\n  let type \u2190 inferType strExpr\n  forallTelescopeReducing type fun typeArgs _ \u21a6\n  withLocalDeclD `x (mkAppN strExpr typeArgs) fun e \u21a6\n  getCompositeOfProjectionsAux (proj ++ \"_\") e #[] <| typeArgs.push e\n\n/-- Get the default `ParsedProjectionData` for structure `str`.\n  It first returns the direct fields of the structure in the right order, and then\n  all (non-subobject fields) of all parent structures. The subobject fields are precisely the\n  non-default fields.-/\ndef mkParsedProjectionData (structName : Name) : CoreM (Array ParsedProjectionData) := do\n  let env \u2190 getEnv\n  let projs := getStructureFields env structName\n  if projs.size == 0 then\n    throwError \"Declaration {structName} is not a structure.\"\n  let projData := projs.map fun fieldName \u21a6 {\n    strName := fieldName, newName := fieldName,\n    isDefault := isSubobjectField? env structName fieldName |>.isNone }\n  let parentProjs := getStructureFieldsFlattened env structName false\n  let parentProjs := parentProjs.filter (!projs.contains \u00b7)\n  let parentProjData := parentProjs.map fun nm \u21a6\n    {strName := nm, newName := nm}\n  return projData ++ parentProjData\n\n/-- Execute the projection renamings (and turning off projections) as specified by `rules`. -/\ndef applyProjectionRules (projs : Array ParsedProjectionData) (rules : Array ProjectionRule) :\n  CoreM (Array ParsedProjectionData) := do\n  let projs : Array ParsedProjectionData := rules.foldl (init := projs) fun projs rule \u21a6\n    match rule with\n    | .rename strName strStx newName newStx =>\n      if (projs.map (\u00b7.newName)).contains strName then\n        projs.map fun proj \u21a6 if proj.newName == strName then\n          { proj with\n            newName,\n            newStx,\n            strStx := if proj.strStx.isMissing then strStx else proj.strStx } else\n          proj else\n        projs.push {strName, strStx, newName, newStx}\n    | .erase nm stx =>\n      if (projs.map (\u00b7.newName)).contains nm then\n        projs.map fun proj \u21a6 if proj.newName = nm then\n          { proj with\n            isDefault := false,\n            strStx := if proj.strStx.isMissing then stx else proj.strStx } else\n          proj else\n        projs.push {strName := nm, newName := nm, strStx := stx, newStx := stx, isDefault := false}\n    | .add nm stx =>\n      if (projs.map (\u00b7.newName)).contains nm then\n        projs.map fun proj \u21a6 if proj.newName = nm then\n          { proj with\n            isDefault := true,\n            strStx := if proj.strStx.isMissing then stx else proj.strStx } else\n          proj else\n        projs.push {strName := nm, newName := nm, strStx := stx, newStx := stx}\n    | .prefix nm stx =>\n      if (projs.map (\u00b7.newName)).contains nm then\n        projs.map fun proj \u21a6 if proj.newName = nm then\n          { proj with\n            isPrefix := true,\n            strStx := if proj.strStx.isMissing then stx else proj.strStx } else\n          proj else\n        projs.push {strName := nm, newName := nm, strStx := stx, newStx := stx, isPrefix := true}\n  trace[simps.debug] \"Projection info after applying the rules: {projs}.\"\n  unless (projs.map (\u00b7.newName)).toList.Nodup do throwError\n    \"Invalid projection names. Two projections have the same name.\\n{\"\"\n    }This is likely because a custom composition of projections was given the same name as an {\"\"\n    }existing projection. Solution: rename the existing projection (before naming the {\"\"\n    }custom projection).\"\n  pure projs\n\n/-- Auxilliary function for `getRawProjections`.\n  Generates the default projection, and looks for a custom projection declared by the user,\n  and replaces the default projection with the custom one, if it can find it. -/\ndef findProjection (str : Name) (proj : ParsedProjectionData)\n  (rawUnivs : List Level) : CoreM ParsedProjectionData := do\n  let env \u2190 getEnv\n  let (rawExpr, nrs) \u2190 MetaM.run' <|\n    getCompositeOfProjections str proj.strName.getString\n  if !proj.strStx.isMissing then\n    _ \u2190 MetaM.run' <| TermElabM.run' <| addTermInfo proj.strStx rawExpr\n  trace[simps.debug] \"Projection {proj.newName} has default projection {rawExpr} and\n    uses projection indices {nrs}\"\n  let customName := str ++ `Simps ++ proj.newName\n  match env.find? customName with\n  | some d@(.defnInfo _) =>\n    let customProj := d.instantiateValueLevelParams! rawUnivs\n    trace[simps.verbose] \"found custom projection for {proj.newName}:{indentExpr customProj}\"\n    match (\u2190 MetaM.run' <| isDefEq customProj rawExpr) with\n    | true =>\n      _ \u2190 MetaM.run' <| TermElabM.run' <| addTermInfo proj.newStx <|\n        \u2190 mkConstWithLevelParams customName\n      pure { proj with expr? := some customProj, projNrs := nrs, isCustom := true }\n    | false =>\n      -- if the type of the Expression is different, we show a different error message, because\n      -- (in Lean 3) just stating that the expressions are different is quite unhelpful\n      let customProjType \u2190 MetaM.run' (inferType customProj)\n      let rawExprType \u2190 MetaM.run' (inferType rawExpr)\n      if (\u2190 MetaM.run' (isDefEq customProjType rawExprType)) then\n        throwError \"Invalid custom projection:{indentExpr customProj}\\n{\"\"\n          }Expression is not definitionally equal to {indentExpr rawExpr}\" else\n        throwError \"Invalid custom projection:\\n  {customProj}\\n{\"\"\n          }Expression has different type than {str ++ proj.strName}. Given type:{\n          indentExpr customProjType}\\nExpected type:{indentExpr rawExprType\n          }\\nNote: make sure order of implicit arguments is exactly the same.\"\n  | _ =>\n    _ \u2190 MetaM.run' <| TermElabM.run' <| addTermInfo proj.newStx rawExpr\n    pure {proj with expr? := some rawExpr, projNrs := nrs}\n\n/-- Checks if there are declarations in the current file in the namespace `{str}.Simps` that are\n  not used. -/\ndef checkForUnusedCustomProjs (stx : Syntax) (str : Name) (projs : Array ParsedProjectionData) :\n  CoreM Unit := do\n  let nrCustomProjections := projs.toList.countp (\u00b7.isCustom)\n  let env \u2190 getEnv\n  let customDeclarations := env.constants.map\u2082.foldl (init := #[]) fun xs nm _ =>\n    if (str ++ `Simps).isPrefixOf nm && !nm.isInternal' then xs.push nm else xs\n  if nrCustomProjections < customDeclarations.size then\n    Linter.logLintIf linter.simpsUnusedCustomDeclarations stx\n      m!\"Not all of the custom declarations {customDeclarations} are used. Double check the {\n        \"\"}spelling, and use `?` to get more information.\"\n\n/-- If a structure has a field that corresponds to a coercion to functions or sets, or corresponds\n  to notation, find the custom projection that uses this coercion or notation.\n  Returns the custom projection and the name of the projection used.\n\nWe catch most errors this function causes, so that we don't fail if an unrelated projection has\nan applicable name. (e.g. `Iso.inv`)\n\nImplementation note: getting rid of TermElabM is tricky, since `Expr.mkAppOptM` doesn't allow to\nkeep metavariables around, which are necessary for `OutParam`. -/\ndef findAutomaticProjectionsAux (str : Name) (proj : ParsedProjectionData) (args : Array Expr) :\n  TermElabM <| Option (Expr \u00d7 Name) := do\n  if let some \u27e8className, isNotation, findArgs\u27e9 :=\n    notationClassAttr.find? (\u2190 getEnv) proj.strName then\n    let findArgs \u2190 unsafe evalConst findArgType findArgs\n    let classArgs \u2190 try findArgs str className args\n    catch ex =>\n      trace[simps.debug] \"Projection {proj.strName} is likely unrelated to the projection of {\n        className}:\\n{ex.toMessageData}\"\n      return none\n    let classArgs \u2190 classArgs.mapM fun e => match e with\n      | none => mkFreshExprMVar none\n      | some e => pure e\n    let classArgs := classArgs.map Arg.expr\n    let projName := (getStructureFields (\u2190 getEnv) className)[0]!\n    let projName := className ++ projName\n    let eStr := mkAppN (\u2190 mkConstWithLevelParams str) args\n    let eInstType \u2190\n      try withoutErrToSorry (elabAppArgs (\u2190 Term.mkConst className) #[] classArgs none true false)\n      catch ex =>\n        trace[simps.debug] \"Projection doesn't have the right type for the automatic projection:\\n{\n          ex.toMessageData}\"\n        return none\n    return \u2190 withLocalDeclD `self eStr fun instStr \u21a6 do\n      trace[simps.debug] \"found projection {proj.strName}. Trying to synthesize {eInstType}.\"\n      let eInst \u2190 try synthInstance eInstType\n      catch ex =>\n        trace[simps.debug] \"Didn't find instance:\\n{ex.toMessageData}\"\n        return none\n      let projExpr \u2190 elabAppArgs (\u2190 Term.mkConst projName) #[] (classArgs.push <| .expr eInst)\n        none true false\n      let projExpr \u2190 mkLambdaFVars (if isNotation then args.push instStr else args) projExpr\n      let projExpr \u2190 instantiateMVars projExpr\n      return (projExpr, projName)\n  return none\n\n/-- Auxilliary function for `getRawProjections`.\nFind custom projections, automatically found by simps.\nThese come from `FunLike` and `SetLike` instances. -/\ndef findAutomaticProjections (str : Name) (projs : Array ParsedProjectionData) :\n  CoreM (Array ParsedProjectionData) := do\n  let strDecl \u2190 getConstInfo str\n  trace[simps.debug] \"debug: {projs}\"\n  MetaM.run' <| TermElabM.run' (s := {levelNames := strDecl.levelParams}) <|\n  forallTelescope strDecl.type fun args _ \u21a6 do\n  let projs \u2190 projs.mapM fun proj => do\n    if let some (projExpr, projName) := \u2190 findAutomaticProjectionsAux str proj args then\n      unless \u2190 isDefEq projExpr proj.expr?.get! do\n        throwError \"The projection {proj.newName} is not definitionally equal to an application {\n          \"\"}of {projName}:{indentExpr proj.expr?.get!}\\nvs{indentExpr projExpr}\"\n      if proj.isCustom then\n        trace[simps.verbose] \"Warning: Projection {proj.newName} is given manually by the user, {\n          \"\"}but it can be generated automatically.\"\n        return proj\n      trace[simps.verbose] \"Using {indentExpr projExpr}\\n for projection {proj.newName}.\"\n      return { proj with expr? := some projExpr }\n    return proj\n  return projs\n\n/--\nGet the projections used by `simps` associated to a given structure `str`.\n\nThe returned information is also stored in the environment extension `Simps.structureExt`, which\nis given to `str`. If `str` already has this attribute, the information is read from this\nextension instead. See the documentation for this extension for the data this tactic returns.\n\nThe returned universe levels are the universe levels of the structure. For the projections there\nare three cases\n* If the declaration `{StructureName}.Simps.{projectionName}` has been declared, then the value\n  of this declaration is used (after checking that it is definitionally equal to the actual\n  projection. If you rename the projection name, the declaration should have the *new* projection\n  name.\n* You can also declare a custom projection that is a composite of multiple projections.\n* Otherwise, for every class with the `notation_class` attribute, and the structure has an\n  instance of that notation class, then the projection of that notation class is used for the\n  projection that is definitionally equal to it (if there is such a projection).\n  This means in practice that coercions to function types and sorts will be used instead of\n  a projection, if this coercion is definitionally equal to a projection. Furthermore, for\n  notation classes like `Mul` and `Zero` those projections are used instead of the\n  corresponding projection.\n  Projections for coercions and notation classes are not automatically generated if they are\n  composites of multiple projections (for example when you use `extend` without the\n  `oldStructureCmd` (does this exist?)).\n* Otherwise, the projection of the structure is chosen.\n  For example: ``getRawProjections env `Prod`` gives the default projections.\n```\n  ([u, v], [(`fst, `(Prod.fst.{u v}), [0], true, false),\n     (`snd, `(@Prod.snd.{u v}), [1], true, false)])\n```\n\nOptionally, this command accepts three optional arguments:\n* If `traceIfExists` the command will always generate a trace message when the structure already\n  has an entry in `structureExt`.\n* The `rules` argument specifies whether projections should be added, renamed, used as prefix, and\n  not used by default.\n* if `trc` is true, this tactic will trace information just as if\n  `set_option trace.simps.verbose true` was set.\n-/\ndef getRawProjections (stx : Syntax) (str : Name) (traceIfExists : Bool := false)\n  (rules : Array ProjectionRule := #[]) (trc := false) :\n  CoreM (List Name \u00d7 Array ProjectionData) := do\n  withOptions (\u00b7 |>.updateBool `trace.simps.verbose (trc || \u00b7)) <| do\n  let env \u2190 getEnv\n  if let some data := (structureExt.getState env).find? str then\n    -- We always print the projections when they already exists and are called by\n    -- `initialize_simps_projections`.\n    withOptions (\u00b7 |>.updateBool `trace.simps.verbose (traceIfExists || \u00b7)) <| do\n      trace[simps.debug]\n        projectionsInfo data.2.toList \"Already found projection information for structure\" str\n    return data\n  trace[simps.verbose] \"generating projection information for structure {str}.\"\n  trace[simps.debug] \"Applying the rules {rules}.\"\n  let strDecl \u2190 getConstInfo str\n  let rawLevels := strDecl.levelParams\n  let rawUnivs := rawLevels.map Level.param\n  let projs \u2190 mkParsedProjectionData str\n  let projs \u2190 applyProjectionRules projs rules\n  let projs \u2190 projs.mapM fun proj \u21a6 findProjection str proj rawUnivs\n  checkForUnusedCustomProjs stx str projs\n  let projs \u2190 findAutomaticProjections str projs\n  let projs := projs.map (\u00b7.toProjectionData)\n  -- make all proofs non-default.\n  let projs \u2190 projs.mapM fun proj \u21a6 do\n    match (\u2190 MetaM.run' <| isProof proj.expr) with\n    | true => pure { proj with isDefault := false }\n    | false => pure proj\n  trace[simps.verbose] projectionsInfo projs.toList \"generated projections for\" str\n  structureExt.add str (rawLevels, projs)\n  trace[simps.debug] \"Generated raw projection data:{indentD <| toMessageData (rawLevels, projs)}\"\n  pure (rawLevels, projs)\n\nlibrary_note \"custom simps projection\"/--\nYou can specify custom projections for the `@[simps]` attribute.\nTo do this for the projection `MyStructure.originalProjection` by adding a declaration\n`MyStructure.Simps.myProjection` that is definitionally equal to\n`MyStructure.originalProjection` but has the projection in the desired (simp-normal) form.\nThen you can call\n```\ninitialize_simps_projections (originalProjection \u2192 myProjection, ...)\n```\nto register this projection. See `elabInitializeSimpsProjections` for more information.\n\nYou can also specify custom projections that are definitionally equal to a composite of multiple\nprojections. This is often desirable when extending structures (without `oldStructureCmd`).\n\n`CoeFun` and notation class (like `Mul`) instances will be automatically used, if they\nare definitionally equal to a projection of the structure (but not when they are equal to the\ncomposite of multiple projections).\n-/\n\n/-- Parse a rule for `initialize_simps_projections`. It is `<name>\u2192<name>`, `-<name>`, `+<name>`\n  or `as_prefix <name>`.-/\ndef elabSimpsRule : Syntax \u2192 CommandElabM ProjectionRule\n| `(simpsRule| $id1 \u2192 $id2)   => return .rename id1.getId id1.raw id2.getId id2.raw\n| `(simpsRule| - $id)         => return .erase id.getId id.raw\n| `(simpsRule| + $id)         => return .add id.getId id.raw\n| `(simpsRule| as_prefix $id) => return .prefix id.getId id.raw\n| _                           => Elab.throwUnsupportedSyntax\n\n/-- Function elaborating `initialize_simps_projections`. -/\n@[command_elab \u00abinitialize_simps_projections\u00bb] def elabInitializeSimpsProjections : CommandElab\n| stx@`(initialize_simps_projections $[?%$trc]? $id $[($stxs,*)]?) => do\n  let stxs := stxs.getD <| .mk #[]\n  let rules \u2190 stxs.getElems.raw.mapM elabSimpsRule\n  let nm \u2190 resolveGlobalConstNoOverload id\n  _ \u2190 liftTermElabM <| addTermInfo id.raw <| \u2190 mkConstWithLevelParams nm\n  _ \u2190 liftCoreM <| getRawProjections stx nm true rules trc.isSome\n| _ => throwUnsupportedSyntax\n\n/-- Configuration options for `@[simps]` -/\nstructure Config where\n  /-- Make generated lemmas simp lemmas -/\n  isSimp := true\n  /-- Other simp-attributes to apply to generated lemmas.\n  Attributes that are currently not simp-attributes are not supported. -/\n  attrs : List Name := []\n  /-- simplify the right-hand side of generated simp-lemmas using `dsimp, simp`. -/\n  simpRhs := false\n  /-- TransparencyMode used to reduce the type in order to detect whether it is a structure. -/\n  typeMd := TransparencyMode.instances\n  /-- TransparencyMode used to reduce the right-hand side in order to detect whether it is a\n  constructor. Note: was `none` in Lean 3 -/\n  rhsMd := TransparencyMode.reducible\n  /-- Generated lemmas that are fully applied, i.e. generates equalities between applied functions.\n  Set this to `false` to generate equalities between functions. -/\n  fullyApplied := true\n  /-- List of types in which we are not recursing to generate simplification lemmas.\n  E.g. if we write `@[simps] def e : \u03b1 \u00d7 \u03b2 \u2243 \u03b2 \u00d7 \u03b1 := ...` we will generate `e_apply` and not\n  `e_apply_fst`. -/\n  notRecursive := [`Prod, `PProd]\n  /-- Output debug messages. Not used much, use `set_option simps.debug true` instead. -/\n  debug := false\n  deriving Inhabited\n\n/-- Function elaborating `Config` -/\ndeclare_config_elab elabSimpsConfig Config\n\n/-- A common configuration for `@[simps]`: generate equalities between functions instead equalities\n  between fully applied Expressions. Use this using `@[simps (config := .asFn)]`. -/\ndef Config.asFn : Simps.Config where\n  fullyApplied := false\n\n/-- A common configuration for `@[simps]`: don't tag the generated lemmas with `@[simp]`.\n  Use this using `@[simps (config := .lemmasOnly)]`. -/\ndef Config.lemmasOnly : Config where\n  isSimp := false\n\n/-- `instantiateLambdasOrApps es e` instantiates lambdas in `e` by expressions from `es`.\nIf the length of `es` is larger than the number of lambdas in `e`,\nthen the term is applied to the remaining terms.\nAlso reduces head let-expressions in `e`, including those after instantiating all lambdas.\n\nThis is very similar to `expr.substs`, but this also reduces head let-expressions. -/\npartial def _root_.Lean.Expr.instantiateLambdasOrApps (es : Array Expr) (e : Expr) : Expr :=\n  e.betaRev es.reverse true -- check if this is what I want\n\n/-- Get the projections of a structure used by `@[simps]` applied to the appropriate arguments.\n  Returns a list of tuples\n  ```\n  (corresponding right-hand-side, given projection name, projection Expression,\n    future projection numbers, used by default, is prefix)\n  ```\n  (where all fields except the first are packed in a `ProjectionData` structure)\n  one for each projection. The given projection name is the name for the projection used by the user\n  used to generate (and parse) projection names. For example, in the structure\n\n  Example 1: ``getProjectionExprs env `(\u03b1 \u00d7 \u03b2) `(\u27e8x, y\u27e9)`` will give the output\n  ```\n    [(`(x), `fst, `(@Prod.fst.{u v} \u03b1 \u03b2), [], true, false),\n     (`(y), `snd, `(@Prod.snd.{u v} \u03b1 \u03b2), [], true, false)]\n  ```\n\n  Example 2: ``getProjectionExprs env `(\u03b1 \u2243 \u03b1) `(\u27e8id, id, fun _ \u21a6 rfl, fun _ \u21a6 rfl\u27e9)``\n  will give the output\n  ```\n    [(`(id), `apply, (Equiv.toFun), [], true, false),\n     (`(id), `symm_apply, (fun e \u21a6 e.symm.toFun), [], true, false),\n     ...,\n     ...]\n  ```\n-/\ndef getProjectionExprs (stx : Syntax) (tgt : Expr) (rhs : Expr) (cfg : Config) :\n    MetaM <| Array <| Expr \u00d7 ProjectionData := do\n  -- the parameters of the structure\n  let params := tgt.getAppArgs\n  if cfg.debug && !(\u2190 (params.zip rhs.getAppArgs).allM fun \u27e8a, b\u27e9 \u21a6 isDefEq a b) then\n    throwError \"unreachable code: parameters are not definitionally equal\"\n  let str := tgt.getAppFn.constName?.getD default\n  -- the fields of the object\n  let rhsArgs := rhs.getAppArgs.toList.drop params.size\n  let (rawUnivs, projDeclata) \u2190 getRawProjections stx str\n  return projDeclata.map fun proj \u21a6\n    (rhsArgs.getD (a\u2080 := default) proj.projNrs.head!,\n      { proj with\n        expr := (proj.expr.instantiateLevelParams rawUnivs\n          tgt.getAppFn.constLevels!).instantiateLambdasOrApps params\n        projNrs := proj.projNrs.tail })\n\nvariable (ref : Syntax) (univs : List Name)\n\n/-- Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`,\n  `args` is the list of local constants occurring, and `univs` is the list of universe variables. -/\ndef addProjection (declName : Name) (type lhs rhs : Expr) (args : Array Expr)\n    (cfg : Config) : MetaM Unit := do\n  trace[simps.debug] \"Planning to add the equality{indentD m!\"{lhs} = ({rhs} : {type})\"}\"\n  let env \u2190 getEnv\n  if (env.find? declName).isSome then -- diverging behavior from Lean 3\n    throwError \"simps tried to add lemma {declName} to the environment, but it already exists.\"\n  -- simplify `rhs` if `cfg.simpRhs` is true\n  let lvl \u2190 getLevel type\n  let mut (rhs, prf) := (rhs, mkAppN (mkConst `Eq.refl [lvl]) #[type, lhs])\n  if cfg.simpRhs then\n    let ctx \u2190 mkSimpContext\n    let (rhs2, _) \u2190 dsimp rhs ctx\n    if rhs != rhs2 then\n      trace[simps.debug] \"`dsimp` simplified rhs to{indentExpr rhs2}\"\n    else\n      trace[simps.debug] \"`dsimp` failed to simplify rhs\"\n    let (result, _) \u2190 simp rhs2 ctx\n    if rhs2 != result.expr then\n      trace[simps.debug] \"`simp` simplified rhs to{indentExpr result.expr}\"\n    else\n      trace[simps.debug] \"`simp` failed to simplify rhs\"\n    rhs := result.expr\n    prf := result.proof?.getD prf\n  let eqAp := mkApp3 (mkConst `Eq [lvl]) type lhs rhs\n  let declType \u2190 mkForallFVars args eqAp\n  let declValue \u2190 mkLambdaFVars args prf\n  trace[simps.verbose] \"adding projection {declName}:{indentExpr declType}\"\n  try\n    addDecl <| .thmDecl {\n      name := declName\n      levelParams := univs\n      type := declType\n      value := declValue }\n  catch ex =>\n    throwError \"Failed to add projection lemma {declName}. Nested error:\\n{ex.toMessageData}\"\n  addDeclarationRanges declName {\n    range := \u2190 getDeclarationRange (\u2190 getRef)\n    selectionRange := \u2190 getDeclarationRange ref }\n  _ \u2190 MetaM.run' <| TermElabM.run' <| addTermInfo (isBinder := true) ref <|\n    \u2190 mkConstWithLevelParams declName\n  if cfg.isSimp then\n    addSimpTheorem simpExtension declName true false .global <| eval_prio default\n  _ \u2190 cfg.attrs.mapM fun simpAttr \u21a6 do\n    let .some simpDecl \u2190 getSimpExtension? simpAttr |\n      throwError \"{simpAttr} is not a simp-attribute.\"\n    addSimpTheorem simpDecl declName true false .global <| eval_prio default\n\n/--\nPerform head-structure-eta-reduction on expression `e`. That is, if `e` is of the form\n`\u27e8f.1, f.2, ..., f.n\u27e9` with `f` definitionally equal to `e`, then\n`headStructureEtaReduce e = headStructureEtaReduce f` and `headStructureEtaReduce e = e` otherwise.\n-/\npartial def headStructureEtaReduce (e : Expr) : MetaM Expr := do\n  let env \u2190 getEnv\n  let (ctor, args) := e.getAppFnArgs\n  let some (.ctorInfo { induct := struct, numParams, ..}) := env.find? ctor | pure e\n  let some { fieldNames, .. } := getStructureInfo? env struct | pure e\n  let (params, fields) := args.toList.splitAt numParams -- fix if `Array.take` / `Array.drop` exist\n  trace[simps.debug]\n    \"rhs is constructor application with params{indentD params}\\nand fields {indentD fields}\"\n  let field0 :: fieldsTail := fields | return e\n  let fieldName0 :: fieldNamesTail := fieldNames.toList | return e\n  let (fn0, fieldArgs0) := field0.getAppFnArgs\n  unless fn0 == struct ++ fieldName0 do\n    trace[simps.debug] \"{fn0} \u2260 {struct ++ fieldName0}\"\n    return e\n  let (params', reduct :: _) := fieldArgs0.toList.splitAt numParams | unreachable!\n  unless params' == params do\n    trace[simps.debug] \"{params'} \u2260 {params}\"\n    return e\n  trace[simps.debug] \"Potential structure-eta-reduct:{indentExpr e}\\nto{indentExpr reduct}\"\n  let allArgs := params.toArray.push reduct\n  let isEta \u2190 (fieldsTail.zip fieldNamesTail).allM fun (field, fieldName) \u21a6\n    if field.getAppFnArgs == (struct ++ fieldName, allArgs) then pure true else isProof field\n  unless isEta do return e\n  trace[simps.debug] \"Structure-eta-reduce:{indentExpr e}\\nto{indentExpr reduct}\"\n  headStructureEtaReduce reduct\n\n/-- Derive lemmas specifying the projections of the declaration.\n  `nm`: name of the lemma\n  If `todo` is non-empty, it will generate exactly the names in `todo`.\n  `toApply` is non-empty after a custom projection that is a composition of multiple projections\n  was just used. In that case we need to apply these projections before we continue changing `lhs`.\n  `simpLemmas`: names of the simp lemmas added so far.(simpLemmas : Array Name)\n  -/\npartial def addProjections (nm : Name) (type lhs rhs : Expr)\n  (args : Array Expr) (mustBeStr : Bool) (cfg : Config)\n  (todo : List (String \u00d7 Syntax)) (toApply : List \u2115) : MetaM (Array Name) := do\n  -- we don't want to unfold non-reducible definitions (like `set`) to apply more arguments\n  trace[simps.debug] \"Type of the Expression before normalizing: {type}\"\n  withTransparency cfg.typeMd <| forallTelescopeReducing type fun typeArgs tgt \u21a6 withDefault do\n  trace[simps.debug] \"Type after removing pi's: {tgt}\"\n  let tgt \u2190 whnfD tgt\n  trace[simps.debug] \"Type after reduction: {tgt}\"\n  let newArgs := args ++ typeArgs\n  let lhsAp := lhs.instantiateLambdasOrApps typeArgs\n  let rhsAp := rhs.instantiateLambdasOrApps typeArgs\n  let str := tgt.getAppFn.constName\n  trace[simps.debug] \"todo: {todo}, toApply: {toApply}\"\n  -- We want to generate the current projection if it is in `todo`\n  let todoNext := todo.filter (\u00b7.1 \u2260 \"\")\n  let env \u2190 getEnv\n  let stx? := todo.find? (\u00b7.1 == \"\") |>.map (\u00b7.2)\n  /- The syntax object associated to the projection we're making now (if any).\n  Note that we use `ref[0]` so that with `simps (config := ...)` we associate it to the word `simps`\n  instead of the application of the attribute to arguments. -/\n  let stxProj := stx?.getD ref[0]\n  let strInfo? := getStructureInfo? env str\n  /- Don't recursively continue if `str` is not a structure or if the structure is in\n  `notRecursive`. -/\n  if strInfo?.isNone ||\n    (todo.isEmpty && str \u2208 cfg.notRecursive && !mustBeStr && toApply.isEmpty) then\n    if mustBeStr then\n      throwError \"Invalid `simps` attribute. Target {str} is not a structure\"\n    if !todoNext.isEmpty && str \u2209 cfg.notRecursive then\n      let firstTodo := todoNext.head!.1\n      throwError \"Invalid simp lemma {nm.appendAfter firstTodo}.\\nProjection {\n        (firstTodo.splitOn \"_\")[1]!} doesn't exist, because target {str} is not a structure.\"\n    if cfg.fullyApplied then\n      addProjection stxProj univs nm tgt lhsAp rhsAp newArgs cfg\n    else\n      addProjection stxProj univs nm type lhs rhs args cfg\n    return #[nm]\n  -- if the type is a structure\n  let some (.inductInfo { isRec := false, ctors := [ctor], .. }) := env.find? str | unreachable!\n  trace[simps.debug] \"{str} is a structure with constructor {ctor}.\"\n  let rhsEta \u2190 headStructureEtaReduce rhsAp\n  -- did the user ask to add this projection?\n  let addThisProjection := stx?.isSome && toApply.isEmpty\n  if addThisProjection then\n    -- we pass the precise argument of simps as syntax argument to `addProjection`\n    if cfg.fullyApplied then\n      addProjection stxProj univs nm tgt lhsAp rhsEta newArgs cfg\n    else\n      addProjection stxProj univs nm type lhs rhs args cfg\n  let rhsWhnf \u2190 withTransparency cfg.rhsMd <| whnf rhsEta\n  trace[simps.debug] \"The right-hand-side {indentExpr rhsAp}\\n reduces to {indentExpr rhsWhnf}\"\n  if !rhsWhnf.getAppFn.isConstOf ctor then\n    -- if I'm about to run into an error, try to set the transparency for `rhsMd` higher.\n    if cfg.rhsMd == .reducible && (mustBeStr || !todoNext.isEmpty || !toApply.isEmpty) then\n      trace[simps.debug] \"Using relaxed reducibility.\"\n      Linter.logLintIf linter.simpsNoConstructor ref\n        m!\"The definition {nm} is not a constructor application. Please use `@[simps!]` instead.{\n        \"\"}\\n\\nExplanation: `@[simps]` uses the definition to find what the simp lemmas should {\n        \"\"}be. If the definition is a constructor, then this is easy, since the values of the {\n        \"\"}projections are just the arguments to the constructor. If the definition is not a {\n        \"\"}constructor, then `@[simps]` will unfold the right-hand side until it has found a {\n        \"\"}constructor application, and uses those values.\\n\\n{\n        \"\"}This might not always result in the simp-lemmas you want, so you are advised to use {\n        \"\"}`@[simps?]` to double-check whether `@[simps]` generated satisfactory lemmas.\\n{\n        \"\"}Note 1: `@[simps!]` also calls the `simp` tactic, and this can be expensive in certain {\n        \"\"}cases.\\n{\n        \"\"}Note 2: `@[simps!]` is equivalent to `@[simps (config := \\{rhsMd := .default, {\n        \"\"}simpRhs := true})]`. You can also try `@[simps (config := \\{rhsMd := .default})]` {\n        \"\"}to still unfold the definitions, but avoid calling `simp` on the resulting statement.\\n{\n        \"\"}Note 3: You need `simps!` if not all fields are given explicitly in this definition, {\n        \"\"}even if the definition is a constructor application. For example, if you give a {\n        \"\"}`MulEquiv` by giving the corresponding `Equiv` and the proof that it respects {\n        \"\"}multiplication, then you need to mark it as `@[simps!]`, since the attribute needs to {\n        \"\"}unfold the corresponding `Equiv` to get to the `toFun` field.\"\n      let nms \u2190 addProjections nm type lhs rhs args mustBeStr\n        { cfg with rhsMd := .default, simpRhs := true } todo toApply\n      return if addThisProjection then nms.push nm else nms\n    if !toApply.isEmpty then\n      throwError \"Invalid simp lemma {nm}.\\nThe given definition is not a constructor {\"\"\n        }application:{indentExpr rhsWhnf}\"\n    if mustBeStr then\n      throwError \"Invalid `simps` attribute. The body is not a constructor application:{\n        indentExpr rhsWhnf}\"\n    if !todoNext.isEmpty then\n      throwError \"Invalid simp lemma {nm.appendAfter todoNext.head!.1}.\\n{\"\"\n        }The given definition is not a constructor application:{indentExpr rhsWhnf}\"\n    if !addThisProjection then\n      if cfg.fullyApplied then\n        addProjection stxProj univs nm tgt lhsAp rhsEta newArgs cfg\n      else\n        addProjection stxProj univs nm type lhs rhs args cfg\n    return #[nm]\n  -- if the value is a constructor application\n  trace[simps.debug] \"Generating raw projection information...\"\n  let projInfo \u2190 getProjectionExprs ref tgt rhsWhnf cfg\n  trace[simps.debug] \"Raw projection information:{indentD m!\"{projInfo}\"}\"\n  -- If we are in the middle of a composite projection.\n  if let idx :: rest := toApply then\n    let some \u27e8newRhs, _\u27e9 := projInfo[idx]?\n      | throwError \"unreachable: index of composite projection is out of bounds.\"\n    let newType \u2190 inferType newRhs\n    trace[simps.debug] \"Applying a custom composite projection. Todo: {toApply}. Current lhs:{\n      indentExpr lhsAp}\"\n    return \u2190 addProjections nm newType lhsAp newRhs newArgs false cfg todo rest\n  trace[simps.debug] \"Not in the middle of applying a custom composite projection\"\n  /- We stop if no further projection is specified or if we just reduced an eta-expansion and we\n  automatically choose projections -/\n  if todo.length == 1 && todo.head!.1 == \"\" then return #[nm]\n  let projs : Array Name := projInfo.map fun x \u21a6 x.2.name\n  let todo := todoNext\n  trace[simps.debug] \"Next todo: {todoNext}\"\n  -- check whether all elements in `todo` have a projection as prefix\n  if let some (x, _) := todo.find? fun (x, _) \u21a6 projs.all\n    fun proj \u21a6 !(proj.getString ++ \"_\").isPrefixOf x then\n    let simpLemma := nm.appendAfter x\n    let neededProj := (x.splitOn \"_\")[0]!\n    throwError \"Invalid simp lemma {simpLemma}. Structure {str} does not have projection {\"\"\n      }{neededProj}.\\nThe known projections are:\\n  {projs}\\nYou can also see this information {\"\"\n      }by running\\n  `initialize_simps_projections? {str}`.\\nNote: these projection names might {\"\"\n      }be customly defined for `simps`, and could differ from the projection names of the {\"\"\n      }structure.\"\n  let nms \u2190 projInfo.concatMapM fun \u27e8newRhs, proj, projExpr, projNrs, isDefault, isPrefix\u27e9 \u21a6 do\n    let newType \u2190 inferType newRhs\n    let newTodo := todo.filterMap\n      fun (x, stx) \u21a6 ((proj.getString ++ \"_\").isPrefixOf? x).map (\u00b7, stx)\n    -- we only continue with this field if it is default or mentioned in todo\n    if !(isDefault && todo.isEmpty) && newTodo.isEmpty then return #[]\n    let newLhs := projExpr.instantiateLambdasOrApps #[lhsAp]\n    let newName := updateName nm proj.getString isPrefix\n    trace[simps.debug] \"Recursively add projections for:{indentExpr newLhs}\"\n    addProjections newName newType newLhs newRhs newArgs false cfg newTodo projNrs\n  return if addThisProjection then nms.push nm else nms\n\nend Simps\nopen Simps\n\n/-- `simpsTac` derives `simp` lemmas for all (nested) non-Prop projections of the declaration.\n  If `todo` is non-empty, it will generate exactly the names in `todo`.\n  If `shortNm` is true, the generated names will only use the last projection name.\n  If `trc` is true, trace as if `trace.simps.verbose` is true. -/\ndef simpsTac (ref : Syntax) (nm : Name) (cfg : Config := {})\n    (todo : List (String \u00d7 Syntax) := []) (trc := false) : AttrM (Array Name) :=\n  withOptions (\u00b7 |>.updateBool `trace.simps.verbose (trc || \u00b7)) <| do\n  let env \u2190 getEnv\n  let some d := env.find? nm | throwError \"Declaration {nm} doesn't exist.\"\n  let lhs : Expr := mkConst d.name <| d.levelParams.map Level.param\n  let todo := todo.pwFilter (\u00b7.1 \u2260 \u00b7.1) |>.map fun (proj, stx) \u21a6 (proj ++ \"_\", stx)\n  let mut cfg := cfg\n  MetaM.run' <| addProjections ref d.levelParams\n    nm d.type lhs (d.value?.getD default) #[] (mustBeStr := true) cfg todo []\n\n/-- elaborate the syntax and run `simpsTac`. -/\ndef simpsTacFromSyntax (nm : Name) (stx : Syntax) : AttrM (Array Name) :=\n  match stx with\n  | `(attr| simps $[!%$bang]? $[?%$trc]? $[(config := $c)]? $[$ids]*) => do\n    let cfg \u2190 MetaM.run' <| TermElabM.run' <| withSaveInfoContext <| elabSimpsConfig stx[3][0]\n    let cfg := if bang.isNone then cfg else { cfg with rhsMd := .default, simpRhs := true }\n    let ids := ids.map fun x => (x.getId.eraseMacroScopes.getString, x.raw)\n    simpsTac stx nm cfg ids.toList trc.isSome\n  | _ => throwUnsupportedSyntax\n\n/-- The `simps` attribute. -/\ninitialize simpsAttr : ParametricAttribute (Array Name) \u2190\n  registerParametricAttribute {\n    name := `simps\n    descr := \"Automatically derive lemmas specifying the projections of this declaration.\",\n    getParam := simpsTacFromSyntax }\n", "meta": {"author": "leanprover-community", "repo": "mathlib4", "sha": "b9a0a30342ca06e9817e22dbe46e75fc7f435500", "save_path": "github-repos/lean/leanprover-community-mathlib4", "path": "github-repos/lean/leanprover-community-mathlib4/mathlib4-b9a0a30342ca06e9817e22dbe46e75fc7f435500/Mathlib/Tactic/Simps/Basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4493926492132671, "lm_q2_score": 0.11757213818344735, "lm_q1q2_score": 0.05283605465192772}}
{"text": "/-\nCopyright (c) 2017 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison, Adam Topaz\n-/\nimport category_theory.limits.preserves.basic\nimport category_theory.limits.types\nimport category_theory.limits.shapes.wide_pullbacks\nimport category_theory.limits.shapes.multiequalizer\nimport category_theory.concrete_category.basic\nimport category_theory.limits.shapes.kernels\nimport tactic.apply_fun\n\n/-!\n# Facts about (co)limits of functors into concrete categories\n\n> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.\n> Any changes to this file require a corresponding PR to mathlib4.\n-/\n\nuniverses w v u\n\nopen category_theory\n\nnamespace category_theory.limits\n\nlocal attribute [instance] concrete_category.has_coe_to_fun concrete_category.has_coe_to_sort\n\nsection limits\n\nvariables {C : Type u} [category.{v} C] [concrete_category.{(max w v)} C]\n  {J : Type w} [small_category J] (F : J \u2964 C) [preserves_limit F (forget C)]\n\nlemma concrete.to_product_injective_of_is_limit {D : cone F} (hD : is_limit D) :\n  function.injective (\u03bb (x : D.X) (j : J), D.\u03c0.app j x) :=\nbegin\n  let E := (forget C).map_cone D,\n  let hE : is_limit E := is_limit_of_preserves _ hD,\n  let G := types.limit_cone.{w v} (F \u22d9 forget C),\n  let hG := types.limit_cone_is_limit.{w v} (F \u22d9 forget C),\n  let T : E.X \u2245 G.X := hE.cone_point_unique_up_to_iso hG,\n  change function.injective (T.hom \u226b (\u03bb x j, G.\u03c0.app j x)),\n  have h : function.injective T.hom,\n  { intros a b h,\n    suffices : T.inv (T.hom a) = T.inv (T.hom b), by simpa,\n    rw h },\n  suffices : function.injective (\u03bb (x : G.X) j, G.\u03c0.app j x),\n    by exact this.comp h,\n  apply subtype.ext,\nend\n\nlemma concrete.is_limit_ext {D : cone F} (hD : is_limit D) (x y : D.X) :\n  (\u2200 j, D.\u03c0.app j x = D.\u03c0.app j y) \u2192 x = y :=\n\u03bb h, concrete.to_product_injective_of_is_limit _ hD (funext h)\n\nlemma concrete.limit_ext [has_limit F] (x y : limit F) :\n  (\u2200 j, limit.\u03c0 F j x = limit.\u03c0 F j y) \u2192 x = y :=\nconcrete.is_limit_ext F (limit.is_limit _) _ _\n\nsection wide_pullback\n\nopen wide_pullback\nopen wide_pullback_shape\n\nlemma concrete.wide_pullback_ext {B : C} {\u03b9 : Type w} {X : \u03b9 \u2192 C} (f : \u03a0 j : \u03b9, X j \u27f6 B)\n  [has_wide_pullback B X f] [preserves_limit (wide_cospan B X f) (forget C)]\n  (x y : wide_pullback B X f) (h\u2080 : base f x = base f y)\n  (h : \u2200 j, \u03c0 f j x = \u03c0 f j y) : x = y :=\nbegin\n  apply concrete.limit_ext,\n  rintro (_|j),\n  { exact h\u2080 },\n  { apply h }\nend\n\nlemma concrete.wide_pullback_ext' {B : C} {\u03b9 : Type w} [nonempty \u03b9]\n  {X : \u03b9 \u2192 C} (f : \u03a0 j : \u03b9, X j \u27f6 B) [has_wide_pullback.{w} B X f]\n  [preserves_limit (wide_cospan B X f) (forget C)]\n  (x y : wide_pullback B X f) (h : \u2200 j, \u03c0 f j x = \u03c0 f j y) : x = y :=\nbegin\n  apply concrete.wide_pullback_ext _ _ _ _ h,\n  inhabit \u03b9,\n  simp only [\u2190 \u03c0_arrow f (arbitrary _), comp_apply, h],\nend\n\nend wide_pullback\n\nsection multiequalizer\n\nlemma concrete.multiequalizer_ext {I : multicospan_index.{w} C} [has_multiequalizer I]\n  [preserves_limit I.multicospan (forget C)] (x y : multiequalizer I)\n  (h : \u2200 (t : I.L), multiequalizer.\u03b9 I t x = multiequalizer.\u03b9 I t y) : x = y :=\nbegin\n  apply concrete.limit_ext,\n  rintros (a|b),\n  { apply h },\n  { rw [\u2190 limit.w I.multicospan (walking_multicospan.hom.fst b),\n      comp_apply, comp_apply, h] }\nend\n\n/-- An auxiliary equivalence to be used in `multiequalizer_equiv` below.-/\ndef concrete.multiequalizer_equiv_aux (I : multicospan_index C) :\n  (I.multicospan \u22d9 (forget C)).sections \u2243\n  { x : \u03a0 (i : I.L), I.left i // \u2200 (i : I.R), I.fst i (x _) = I.snd i (x _) } :=\n{ to_fun := \u03bb x, \u27e8\u03bb i, x.1 (walking_multicospan.left _), \u03bb i, begin\n    have a := x.2 (walking_multicospan.hom.fst i),\n    have b := x.2 (walking_multicospan.hom.snd i),\n    rw \u2190 b at a,\n    exact a,\n  end\u27e9,\n  inv_fun := \u03bb x,\n  { val := \u03bb j,\n    match j with\n    | walking_multicospan.left a := x.1 _\n    | walking_multicospan.right b := I.fst b (x.1 _)\n    end,\n    property := begin\n      rintros (a|b) (a'|b') (f|f|f),\n      { change (I.multicospan.map (\ud835\udfd9 _)) _ = _, simp },\n      { refl },\n      { dsimp, erw \u2190 x.2 b', refl },\n      { change (I.multicospan.map (\ud835\udfd9 _)) _ = _, simp },\n    end },\n  left_inv := begin\n    intros x, ext (a|b),\n    { refl },\n    { change _ = x.val _,\n      rw \u2190 x.2 (walking_multicospan.hom.fst b),\n      refl }\n  end,\n  right_inv := by { intros x, ext i, refl } }\n\n/-- The equivalence between the noncomputable multiequalizer and\nand the concrete multiequalizer. -/\nnoncomputable\ndef concrete.multiequalizer_equiv (I : multicospan_index.{w} C) [has_multiequalizer I]\n  [preserves_limit I.multicospan (forget C)] : (multiequalizer I : C) \u2243\n    { x : \u03a0 (i : I.L), I.left i // \u2200 (i : I.R), I.fst i (x _) = I.snd i (x _) } :=\nlet h1 := (limit.is_limit I.multicospan),\n    h2 := (is_limit_of_preserves (forget C) h1),\n    E := h2.cone_point_unique_up_to_iso (types.limit_cone_is_limit _) in\nequiv.trans E.to_equiv (concrete.multiequalizer_equiv_aux I)\n\n@[simp]\nlemma concrete.multiequalizer_equiv_apply (I : multicospan_index.{w} C) [has_multiequalizer I]\n  [preserves_limit I.multicospan (forget C)] (x : multiequalizer I) (i : I.L) :\n  ((concrete.multiequalizer_equiv I) x : \u03a0 (i : I.L), I.left i) i = multiequalizer.\u03b9 I i x := rfl\n\nend multiequalizer\n\n-- TODO: Add analogous lemmas about products and equalizers.\n\nend limits\n\nsection colimits\n\n-- We don't mark this as an `@[ext]` lemma as we don't always want to work elementwise.\nlemma cokernel_funext {C : Type*} [category C] [has_zero_morphisms C] [concrete_category C]\n  {M N K : C} {f : M \u27f6 N} [has_cokernel f] {g h : cokernel f \u27f6 K}\n  (w : \u2200 (n : N), g (cokernel.\u03c0 f n) = h (cokernel.\u03c0 f n)) : g = h :=\nbegin\n  apply coequalizer.hom_ext,\n  apply concrete_category.hom_ext _ _,\n  simpa using w,\nend\n\nvariables {C : Type u} [category.{v} C] [concrete_category.{v} C]\n  {J : Type v} [small_category J] (F : J \u2964 C) [preserves_colimit F (forget C)]\n\nlemma concrete.from_union_surjective_of_is_colimit {D : cocone F} (hD : is_colimit D) :\n  let ff : (\u03a3 (j : J), F.obj j) \u2192 D.X := \u03bb a, D.\u03b9.app a.1 a.2 in function.surjective ff :=\nbegin\n  intro ff,\n  let E := (forget C).map_cocone D,\n  let hE : is_colimit E := is_colimit_of_preserves _ hD,\n  let G := types.colimit_cocone.{v v} (F \u22d9 forget C),\n  let hG := types.colimit_cocone_is_colimit.{v v} (F \u22d9 forget C),\n  let T : E \u2245 G := hE.unique_up_to_iso hG,\n  let TX : E.X \u2245 G.X := (cocones.forget _).map_iso T,\n  suffices : function.surjective (TX.hom \u2218 ff),\n  { intro a,\n    obtain \u27e8b, hb\u27e9 := this (TX.hom a),\n    refine \u27e8b, _\u27e9,\n    apply_fun TX.inv at hb,\n    change (TX.hom \u226b TX.inv) (ff b) = (TX.hom \u226b TX.inv) _ at hb,\n    simpa only [TX.hom_inv_id] using hb },\n  have : TX.hom \u2218 ff = \u03bb a, G.\u03b9.app a.1 a.2,\n  { ext a,\n    change (E.\u03b9.app a.1 \u226b hE.desc G) a.2 = _,\n    rw hE.fac },\n  rw this,\n  rintro \u27e8\u27e8j,a\u27e9\u27e9,\n  exact \u27e8\u27e8j,a\u27e9,rfl\u27e9,\nend\n\nlemma concrete.is_colimit_exists_rep {D : cocone F} (hD : is_colimit D) (x : D.X) :\n  \u2203 (j : J) (y : F.obj j), D.\u03b9.app j y = x :=\nbegin\n  obtain \u27e8a, rfl\u27e9 := concrete.from_union_surjective_of_is_colimit F hD x,\n  exact \u27e8a.1, a.2, rfl\u27e9,\nend\n\nlemma concrete.colimit_exists_rep [has_colimit F] (x : colimit F) :\n  \u2203 (j : J) (y : F.obj j), colimit.\u03b9 F j y = x :=\nconcrete.is_colimit_exists_rep F (colimit.is_colimit _) x\n\nlemma concrete.is_colimit_rep_eq_of_exists {D : cocone F} {i j : J} (hD : is_colimit D)\n  (x : F.obj i) (y : F.obj j) (h : \u2203 k (f : i \u27f6 k) (g : j \u27f6 k), F.map f x = F.map g y) :\n  D.\u03b9.app i x = D.\u03b9.app j y :=\nbegin\n  let E := (forget C).map_cocone D,\n  let hE : is_colimit E := is_colimit_of_preserves _ hD,\n  let G := types.colimit_cocone.{v v} (F \u22d9 forget C),\n  let hG := types.colimit_cocone_is_colimit.{v v} (F \u22d9 forget C),\n  let T : E \u2245 G := hE.unique_up_to_iso hG,\n  let TX : E.X \u2245 G.X := (cocones.forget _).map_iso T,\n  apply_fun TX.hom,\n  swap, { suffices : function.bijective TX.hom, by exact this.1,\n    rw \u2190 is_iso_iff_bijective, apply is_iso.of_iso },\n  change (E.\u03b9.app i \u226b TX.hom) x = (E.\u03b9.app j \u226b TX.hom) y,\n  erw [T.hom.w, T.hom.w],\n  obtain \u27e8k, f, g, h\u27e9 := h,\n  have : G.\u03b9.app i x = (G.\u03b9.app k (F.map f x) : G.X) := quot.sound \u27e8f,rfl\u27e9,\n  rw [this, h],\n  symmetry,\n  exact quot.sound \u27e8g,rfl\u27e9,\nend\n\nlemma concrete.colimit_rep_eq_of_exists [has_colimit F] {i j : J}\n  (x : F.obj i) (y : F.obj j) (h : \u2203 k (f : i \u27f6 k) (g : j \u27f6 k), F.map f x = F.map g y) :\n  colimit.\u03b9 F i x = colimit.\u03b9 F j y :=\nconcrete.is_colimit_rep_eq_of_exists F (colimit.is_colimit _) x y h\n\nsection filtered_colimits\n\nvariable [is_filtered J]\n\nlemma concrete.is_colimit_exists_of_rep_eq {D : cocone F} {i j : J} (hD : is_colimit D)\n  (x : F.obj i) (y : F.obj j) (h : D.\u03b9.app _ x = D.\u03b9.app _ y) :\n  \u2203 k (f : i \u27f6 k) (g : j \u27f6 k), F.map f x = F.map g y :=\nbegin\n  let E := (forget C).map_cocone D,\n  let hE : is_colimit E := is_colimit_of_preserves _ hD,\n  let G := types.colimit_cocone.{v v} (F \u22d9 forget C),\n  let hG := types.colimit_cocone_is_colimit.{v v} (F \u22d9 forget C),\n  let T : E \u2245 G := hE.unique_up_to_iso hG,\n  let TX : E.X \u2245 G.X := (cocones.forget _).map_iso T,\n  apply_fun TX.hom at h,\n  change (E.\u03b9.app i \u226b TX.hom) x = (E.\u03b9.app j \u226b TX.hom) y at h,\n  erw [T.hom.w, T.hom.w] at h,\n  replace h := quot.exact _ h,\n  suffices : \u2200 (a b : \u03a3 j, F.obj j)\n    (h : eqv_gen (limits.types.quot.rel.{v v} (F \u22d9 forget C)) a b),\n    \u2203 k (f : a.1 \u27f6 k) (g : b.1 \u27f6 k), F.map f a.2 = F.map g b.2,\n  { exact this \u27e8i,x\u27e9 \u27e8j,y\u27e9 h },\n  intros a b h,\n  induction h,\n  case eqv_gen.rel : x y hh\n  { obtain \u27e8e,he\u27e9 := hh,\n    use [y.1, e, \ud835\udfd9 _],\n    simpa using he.symm },\n  case eqv_gen.refl : x { use [x.1, \ud835\udfd9 _, \ud835\udfd9 _, rfl] },\n  case eqv_gen.symm : x y _ hh\n  { obtain \u27e8k, f, g, hh\u27e9 := hh,\n    use [k, g, f, hh.symm] },\n  case eqv_gen.trans : x y z _ _ hh1 hh2\n  { obtain \u27e8k1, f1, g1, h1\u27e9 := hh1,\n    obtain \u27e8k2, f2, g2, h2\u27e9 := hh2,\n    let k0 : J := is_filtered.max k1 k2,\n    let e1 : k1 \u27f6 k0 := is_filtered.left_to_max _ _,\n    let e2 : k2 \u27f6 k0 := is_filtered.right_to_max _ _,\n    let k : J := is_filtered.coeq (g1 \u226b e1) (f2 \u226b e2),\n    let e : k0 \u27f6 k := is_filtered.coeq_hom _ _,\n    use [k, f1 \u226b e1 \u226b e, g2 \u226b e2 \u226b e],\n    simp only [F.map_comp, comp_apply, h1, \u2190 h2],\n    simp only [\u2190 comp_apply, \u2190 F.map_comp],\n    rw is_filtered.coeq_condition },\nend\n\ntheorem concrete.is_colimit_rep_eq_iff_exists {D : cocone F} {i j : J}\n  (hD : is_colimit D) (x : F.obj i) (y : F.obj j) :\n  D.\u03b9.app i x = D.\u03b9.app j y \u2194 \u2203 k (f : i \u27f6 k) (g : j \u27f6 k), F.map f x = F.map g y :=\n\u27e8concrete.is_colimit_exists_of_rep_eq _ hD _ _, concrete.is_colimit_rep_eq_of_exists _ hD _ _\u27e9\n\nlemma concrete.colimit_exists_of_rep_eq [has_colimit F] {i j : J}\n  (x : F.obj i) (y : F.obj j) (h : colimit.\u03b9 F _ x = colimit.\u03b9 F _ y) :\n  \u2203 k (f : i \u27f6 k) (g : j \u27f6 k), F.map f x = F.map g y :=\nconcrete.is_colimit_exists_of_rep_eq F (colimit.is_colimit _) x y h\n\ntheorem concrete.colimit_rep_eq_iff_exists [has_colimit F] {i j : J}\n  (x : F.obj i) (y : F.obj j) :\n  colimit.\u03b9 F i x = colimit.\u03b9 F j y \u2194 \u2203 k (f : i \u27f6 k) (g : j \u27f6 k), F.map f x = F.map g y :=\n\u27e8concrete.colimit_exists_of_rep_eq _ _ _, concrete.colimit_rep_eq_of_exists _ _ _\u27e9\n\nend filtered_colimits\n\nsection wide_pushout\n\nopen wide_pushout\nopen wide_pushout_shape\n\nlemma concrete.wide_pushout_exists_rep {B : C} {\u03b1 : Type*} {X : \u03b1 \u2192 C} (f : \u03a0 j : \u03b1, B \u27f6 X j)\n  [has_wide_pushout.{v} B X f] [preserves_colimit (wide_span B X f) (forget C)]\n  (x : wide_pushout B X f) : (\u2203 y : B, head f y = x) \u2228 (\u2203 (i : \u03b1) (y : X i), \u03b9 f i y = x) :=\nbegin\n  obtain \u27e8_ | j, y, rfl\u27e9 := concrete.colimit_exists_rep _ x,\n  { use y },\n  { right,\n    use [j,y] }\nend\n\nlemma concrete.wide_pushout_exists_rep' {B : C} {\u03b1 : Type*} [nonempty \u03b1] {X : \u03b1 \u2192 C}\n  (f : \u03a0 j : \u03b1, B \u27f6 X j) [has_wide_pushout.{v} B X f]\n  [preserves_colimit (wide_span B X f) (forget C)] (x : wide_pushout B X f) :\n  \u2203 (i : \u03b1) (y : X i), \u03b9 f i y = x :=\nbegin\n  rcases concrete.wide_pushout_exists_rep f x with \u27e8y, rfl\u27e9 | \u27e8i, y, rfl\u27e9,\n  { inhabit \u03b1,\n    use [arbitrary _, f _ y],\n    simp only [\u2190 arrow_\u03b9 _ (arbitrary \u03b1), comp_apply] },\n  { use [i,y] }\nend\n\nend wide_pushout\n\n-- TODO: Add analogous lemmas about coproducts and coequalizers.\n\nend colimits\n\nend category_theory.limits\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/category_theory/limits/concrete_category.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.46101677931231594, "lm_q2_score": 0.11436852769112675, "lm_q1q2_score": 0.05272581029085468}}
{"text": "import ProofWidgets.Component.HtmlDisplay\n\nopen scoped ProofWidgets.Jsx -- \u27f5 remember this!\n\ndef x := <b>You can use HTML in lean! {.text <| toString <| 4 + 5} <hr/> </b>\n\n#html x\n\ntheorem ghjk : True := by\n  html! <b>What, HTML in Lean?! </b>\n  html! <i>And another!</i>\n  html! <img src=\"https://upload.wikimedia.org/wikipedia/commons/a/a5/Parrot_montage.jpg\"/>\n  trivial\n", "meta": {"author": "EdAyers", "repo": "ProofWidgets4", "sha": "c57cc40fcc58ff1ac2a2b52cf34c39d90ba0b11e", "save_path": "github-repos/lean/EdAyers-ProofWidgets4", "path": "github-repos/lean/EdAyers-ProofWidgets4/ProofWidgets4-c57cc40fcc58ff1ac2a2b52cf34c39d90ba0b11e/ProofWidgets/Demos/Basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.5, "lm_q2_score": 0.10521052758479323, "lm_q1q2_score": 0.05260526379239661}}
{"text": "/-\nCopyright (c) 2022 Damiano Testa. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Gabriel Ebner, Damiano Testa\n\n! This file was ported from Lean 3 source module tactic.congrm\n! leanprover-community/mathlib commit e985d48324225202b17a7f9eb50b29ba09b77b44\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.Interactive\n\n/-! `congrm`: `congr` with pattern-matching\n\n`congrm e` gives to the use the functionality of using `congr` with an expression `e` \"guiding\"\n`congr` through the matching.  This allows more flexibility than `congr' n`, which enters uniformly\nthrough `n` iterations.  Instead, we can guide the matching deeper on some parts of the expression\nand stop earlier on other parts.\n\n##  Implementation notes\n\n###  Function underscores\n\nSee the doc-string to `tactic.interactive.congrm` for more details.  Here we describe how to add\nmore \"function underscores\".\n\nThe pattern for generating a function underscore is to define a \"generic\" `n`-ary function, for some\nnumber `n`.  You can take a look at `tactic.congrm_fun_1, ..., tactic.congrm_fun_4`.\nThese implement the \"function underscores\" `_\u2081, ..., _\u2084`.  If you want a different arity for your\nfunction, simply\nintroduce\n```lean\n@[nolint unused_arguments]\ndef congrm_fun_n {\u03b1\u2081 \u2026 \u03b1\u2099 \u03c1} {r : \u03c1} : \u03b1\u2081 \u2192 \u22ef \u2192 a\u2099 \u2192 \u03c1 := \u03bb _ \u2026 _, r\nnotation `_\u2099` := congrm_fun_n\n```\n_Warning:_ `convert_to_explicit` checks that the first 18 characters in the name of `_\u2099` are\nidentical to `tactic.congrm_fun_` to perform its job.  Thus, if you want to implement\n\"function underscores\" with different arity, either make sure that their names begin with\n`tactic.congrm_fun_` or you should change `convert_to_explicit` accordingly.\n-/\n\n\nnamespace Tactic\n\n/-- A generic function with one argument.  It is the \"function underscore\" input to `congrm`. -/\n@[nolint unused_arguments]\ndef congrmFun1 {\u03b1 \u03c1} {r : \u03c1} : \u03b1 \u2192 \u03c1 := fun _ => r\n#align tactic.congrm_fun_1 Tactic.congrmFun1\n\n-- mathport name: expr_\u2081\nnotation \"_\u2081\" => congrmFun1\n\n/-- A generic function with two arguments.  It is the \"function underscore\" input to `congrm`. -/\n@[nolint unused_arguments]\ndef congrmFun2 {\u03b1 \u03b2 \u03c1} {r : \u03c1} : \u03b1 \u2192 \u03b2 \u2192 \u03c1 := fun _ _ => r\n#align tactic.congrm_fun_2 Tactic.congrmFun2\n\n-- mathport name: expr_\u2082\nnotation \"_\u2082\" => congrmFun2\n\n/-- A generic function with three arguments.  It is the \"function underscore\" input to `congrm`. -/\n@[nolint unused_arguments]\ndef congrmFun3 {\u03b1 \u03b2 \u03b3 \u03c1} {r : \u03c1} : \u03b1 \u2192 \u03b2 \u2192 \u03b3 \u2192 \u03c1 := fun _ _ _ => r\n#align tactic.congrm_fun_3 Tactic.congrmFun3\n\n-- mathport name: expr_\u2083\nnotation \"_\u2083\" => congrmFun3\n\n/-- A generic function with four arguments.  It is the \"function underscore\" input to `congrm`. -/\n@[nolint unused_arguments]\ndef congrmFun4 {\u03b1 \u03b2 \u03b3 \u03b4 \u03c1} {r : \u03c1} : \u03b1 \u2192 \u03b2 \u2192 \u03b3 \u2192 \u03b4 \u2192 \u03c1 := fun _ _ _ _ => r\n#align tactic.congrm_fun_4 Tactic.congrmFun4\n\n-- mathport name: expr_\u2084\nnotation \"_\u2084\" => congrmFun4\n\n/-- Replaces a \"function underscore\" input to `congrm` into the correct expression,\nread off from the left-hand-side of the target expression. -/\nunsafe def convert_to_explicit (pat lhs : expr) : tactic expr :=\n  if pat.get_app_fn.const_name.toString.startsWith \"tactic.congrm_fun_\" then\n    pat.list_explicit_args >>= lhs.replace_explicit_args\n  else return pat\n#align tactic.convert_to_explicit tactic.convert_to_explicit\n\n/-- For each element of `list congr_arg_kind` that is `eq`, add a pair `(g, pat)` to the\nfinal list.  Otherwise, discard an appropriate number of initial terms from each list\n(possibly none from the first) and repeat.\n\n`pat` is the given pattern-piece at the appropriate location, extracted from the last `list expr`.\nIt appears to be the list of arguments of a function application.\n\n`g` is possibly the proof of an equality?  It is extracted from the first `list expr`.\n-/\nprivate unsafe def extract_subgoals :\n    List expr \u2192 List CongrArgKind \u2192 List expr \u2192 tactic (List (expr \u00d7 expr))\n  | _ :: _ :: g :: prf_args, CongrArgKind.eq :: kinds, pat :: pat_args =>\n    (fun rest => (g, pat) :: rest) <$> extract_subgoals prf_args kinds pat_args\n  | _ :: prf_args, CongrArgKind.fixed :: kinds, _ :: pat_args =>\n    extract_subgoals prf_args kinds pat_args\n  | prf_args, CongrArgKind.fixed_no_param :: kinds, _ :: pat_args =>\n    extract_subgoals prf_args kinds pat_args\n  | _ :: _ :: prf_args, CongrArgKind.cast :: kinds, _ :: pat_args =>\n    extract_subgoals prf_args kinds pat_args\n  | _, _, [] => pure []\n  | _, _, _ => fail \"unsupported congr lemma\"\n#align tactic.extract_subgoals tactic.extract_subgoals\n\n-- failed to format: unknown constant 'term.pseudo.antiquot'\n/--\n      `equate_with_pattern_core pat` solves a single goal of the form `lhs = rhs`\n      (assuming that `lhs` and `rhs` are unifiable with `pat`)\n      by applying congruence lemmas until `pat` is a metavariable.\n      Returns the list of metavariables for the new subgoals at the leafs.\n      Calls `set_goals []` at the end.\n      -/\n    unsafe\n  def\n    equate_with_pattern_core\n    : expr \u2192 tactic ( List expr )\n    |\n      pat\n      =>\n      applyc ` ` Subsingleton.elim >> pure [ ]\n        <|>\n        applyc ` ` rfl >> pure [ ]\n          <|>\n          if\n            pat . is_mvar || pat . get_delayed_abstraction_locals . isSome\n            then\n            do try <| applyc ` ` _root_.propext get_goals <* set_goals [ ]\n            else\n            match\n              pat\n              with\n              |\n                  expr.app _ _\n                  =>\n                  do\n                    let q( $ ( lhs ) = $ ( _ ) ) \u2190 target\n                      let pat \u2190 convert_to_explicit pat lhs\n                      let cl \u2190 mk_specialized_congr_lemma pat\n                      let H_congr_lemma \u2190 assertv `H_congr_lemma cl . type cl . proof\n                      let [ prf ] \u2190 get_goals\n                      apply H_congr_lemma <|> fail \"could not apply congr_lemma\"\n                      all_goals' <| try <| clear H_congr_lemma\n                      set_goals [ ]\n                      let prf \u2190 instantiate_mvars prf\n                      let\n                        subgoals\n                          \u2190\n                          extract_subgoals prf . get_app_args cl . arg_kinds pat . get_app_args\n                      let\n                        subgoals\n                          \u2190\n                          subgoals . mapM\n                            fun\n                              \u27e8 subgoal , subpat \u27e9\n                                =>\n                                do set_goals [ subgoal ] equate_with_pattern_core subpat\n                      pure subgoals\n                |\n                  expr.lam _ _ _ body\n                  =>\n                  do\n                    applyc ` ` _root_.funext\n                      let x \u2190 intro pat . binding_name\n                      equate_with_pattern_core <| body x\n                |\n                  expr.pi _ _ _ codomain\n                  =>\n                  do\n                    applyc ` ` _root_.pi_congr\n                      let x \u2190 intro pat . binding_name\n                      equate_with_pattern_core <| codomain x\n                | _ => do let pat \u2190 pp pat fail <| to_fmt \"unsupported pattern:\\n\" ++ pat\n#align tactic.equate_with_pattern_core tactic.equate_with_pattern_core\n\n/-- `equate_with_pattern pat` solves a single goal of the form `lhs = rhs`\n(assuming that `lhs` and `rhs` are unifiable with `pat`)\nby applying congruence lemmas until `pat` is a metavariable.\nThe subgoals for the leafs are prepended to the goals.\n-/\nunsafe def equate_with_pattern (pat : expr) : tactic Unit := do\n  let congr_subgoals \u2190 solve1 (equate_with_pattern_core pat)\n  let gs \u2190 get_goals\n  set_goals <| congr_subgoals ++ gs\n#align tactic.equate_with_pattern tactic.equate_with_pattern\n\nend Tactic\n\nnamespace Tactic.Interactive\n\nopen Tactic Interactive\n\n/- ./././Mathport/Syntax/Translate/Tactic/Mathlib/Core.lean:38:34: unsupported: setup_tactic_parser -/\n/-- Assume that the goal is of the form `lhs = rhs` or `lhs \u2194 rhs`.\n`congrm e` takes an expression `e` containing placeholders `_` and scans `e, lhs, rhs` in parallel.\n\nIt matches both `lhs` and `rhs` to the pattern `e`, and produces one goal for each placeholder,\nstating that the corresponding subexpressions in `lhs` and `rhs` are equal.\n\nExamples:\n```lean\nexample {a b c d : \u2115} :\n  nat.pred a.succ * (d + (c + a.pred)) = nat.pred b.succ * (b + (c + d.pred)) :=\nbegin\n  congrm nat.pred (nat.succ _) * (_ + _),\n/-  Goals left:\n\u22a2 a = b\n\u22a2 d = b\n\u22a2 c + a.pred = c + d.pred\n-/\n  sorry,\n  sorry,\n  sorry,\nend\n\nexample {a b : \u2115} (h : a = b) : (\u03bb y : \u2115, \u2200 z, a + a = z) = (\u03bb x, \u2200 z, b + a = z) :=\nbegin\n  congrm \u03bb x, \u2200 w, _ + a = w,\n  -- produces one goal for the underscore: \u22a2 a = b\n  exact h,\nend\n```\n\nThe tactic also allows for \"function underscores\", denoted by `_\u2081, ..., _\u2084`.  The index denotes\nthe number of explicit arguments of the function to be matched.\nIf `e` has a \"function underscore\" in a location, then the tactic reads off the function `f` that\nappears in `lhs` at the current location, replacing the *explicit* arguments of `f` by the user\ninputs to the \"function underscore\".  After that, `congrm` continues with its matching.\n-/\nunsafe def congrm (arg : parse texpr) : tactic Unit := do\n  try <| applyc `` _root_.eq.to_iff\n  let q(@Eq $(ty) _ _) \u2190 target |\n    fail \"congrm: goal must be an equality or iff\"\n  let ta \u2190 to_expr ``(($(arg) : $(ty))) true false\n  equate_with_pattern ta\n#align tactic.interactive.congrm tactic.interactive.congrm\n\nadd_tactic_doc\n  { Name := \"congrm\"\n    category := DocCategory.tactic\n    declNames := [`tactic.interactive.congrm]\n    tags := [\"congruence\"] }\n\nend Tactic.Interactive\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/Tactic/Congrm.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.46490157137338844, "lm_q2_score": 0.11279541523008142, "lm_q1q2_score": 0.052438765784178686}}
{"text": "/-\nCopyright (c) 2019 Floris van Doorn. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Floris van Doorn\n-/\nimport tactic.protected\nimport tactic.to_additive\n\n/-!\n# simps attribute\n\nThis file defines the `@[simps]` attribute, to automatically generate `simp` lemmas\nreducing a definition when projections are applied to it.\n\n## Implementation Notes\n\nThere are three attributes being defined here\n* `@[simps]` is the attribute for objects of a structure or instances of a class. It will\n  automatically generate simplification lemmas for each projection of the object/instance that\n  contains data. See the doc strings for `simps_attr` and `simps_cfg` for more details and\n  configuration options.\n* `@[_simps_str]` is automatically added to structures that have been used in `@[simps]` at least\n  once. This attribute contains the data of the projections used for this structure by all following\n  invocations of `@[simps]`.\n* `@[notation_class]` should be added to all classes that define notation, like `has_mul` and\n  `has_zero`. This specifies that the projections that `@[simps]` used are the projections from\n  these notation classes instead of the projections of the superclasses.\n  Example: if `has_mul` is tagged with `@[notation_class]` then the projection used for `semigroup`\n  will be `\u03bb \u03b1 h\u03b1, @has_mul.mul \u03b1 (@semigroup.to_has_mul \u03b1 h\u03b1)` instead of `@semigroup.mul`.\n\n## Tags\n\nstructures, projections, simp, simplifier, generates declarations\n-/\n\nopen tactic expr option sum\n\nsetup_tactic_parser\ndeclare_trace simps.verbose\ndeclare_trace simps.debug\n\n/--\nProjection data for a single projection of a structure, consisting of the following fields:\n- the name used in the generated `simp` lemmas\n- an expression used by simps for the projection. It must be definitionally equal to an original\n  projection (or a composition of multiple projections).\n  These expressions can contain the universe parameters specified in the first argument of\n  `simps_str_attr`.\n- a list of natural numbers, which is the projection number(s) that have to be applied to the\n  expression. For example the list `[0, 1]` corresponds to applying the first projection of the\n  structure, and then the second projection of the resulting structure (this assumes that the\n  target of the first projection is a structure with at least two projections).\n  The composition of these projections is required to be definitionally equal to the provided\n  expression.\n- A boolean specifying whether `simp` lemmas are generated for this projection by default.\n- A boolean specifying whether this projection is written as prefix.\n-/\n@[protect_proj, derive [has_reflect, inhabited]]\nmeta structure projection_data :=\n(name : name)\n(expr : expr)\n(proj_nrs : list \u2115)\n(is_default : bool)\n(is_prefix : bool)\n\n/-- Temporary projection data parsed from `initialize_simps_projections` before the expression\n  matching this projection has been found. Only used internally in `simps_get_raw_projections`. -/\nmeta structure parsed_projection_data :=\n(orig_name : name) -- name for this projection used in the structure definition\n(new_name : name) -- name for this projection used in the generated `simp` lemmas\n(is_default : bool)\n(is_prefix : bool)\n\nsection\nopen format\nmeta instance : has_to_tactic_format projection_data :=\n\u27e8\u03bb \u27e8a, b, c, d, e\u27e9, (\u03bb x, group $ nest 1 $ to_fmt \"\u27e8\"  ++ to_fmt a ++ to_fmt \",\" ++ line ++ x ++\n  to_fmt \",\" ++ line ++ to_fmt c ++ to_fmt \",\" ++ line ++ to_fmt d ++ to_fmt \",\" ++ line ++\n  to_fmt e ++ to_fmt \"\u27e9\") <$> pp b\u27e9\n\nmeta instance : has_to_format parsed_projection_data :=\n\u27e8\u03bb \u27e8a, b, c, d\u27e9, group $ nest 1 $ to_fmt \"\u27e8\"  ++ to_fmt a ++ to_fmt \",\" ++ line ++ to_fmt b ++\n  to_fmt \",\" ++ line ++ to_fmt c ++ to_fmt \",\" ++ line ++ to_fmt d ++ to_fmt \"\u27e9\"\u27e9\nend\n\n/-- The type of rules that specify how metadata for projections in changes.\n  See `initialize_simps_projection`. -/\nabbreviation projection_rule := (name \u00d7 name \u2295 name) \u00d7 bool\n\n/--\nThe `@[_simps_str]` attribute specifies the preferred projections of the given structure,\nused by the `@[simps]` attribute.\n- This will usually be tagged by the `@[simps]` tactic.\n- You can also generate this with the command `initialize_simps_projections`.\n- To change the default value, see Note [custom simps projection].\n- You are strongly discouraged to add this attribute manually.\n- The first argument is the list of names of the universe variables used in the structure\n- The second argument is a list that consists of the projection data for each projection.\n-/\n@[user_attribute] meta def simps_str_attr :\n  user_attribute unit (list name \u00d7 list projection_data) :=\n{ name := `_simps_str,\n  descr := \"An attribute specifying the projection of the given structure.\",\n  parser := failed }\n\n/--\n  The `@[notation_class]` attribute specifies that this is a notation class,\n  and this notation should be used instead of projections by @[simps].\n  * The first argument `tt` for notation classes and `ff` for classes applied to the structure,\n    like `has_coe_to_sort` and `has_coe_to_fun`\n  * The second argument is the name of the projection (by default it is the first projection\n    of the structure)\n-/\n@[user_attribute] meta def notation_class_attr : user_attribute unit (bool \u00d7 option name) :=\n{ name := `notation_class,\n  descr := \"An attribute specifying that this is a notation class. Used by @[simps].\",\n  parser := prod.mk <$> (option.is_none <$> (tk \"*\")?) <*> ident? }\n\nattribute [notation_class] has_zero has_one has_add has_mul has_inv has_neg has_sub has_div has_dvd\n  has_mod has_le has_lt has_append has_andthen has_union has_inter has_sdiff has_equiv has_subset\n  has_ssubset has_emptyc has_insert has_singleton has_sep has_mem has_pow\n\nattribute [notation_class* coe_sort] has_coe_to_sort\nattribute [notation_class* coe_fn] has_coe_to_fun\n\n/-- Returns the projection information of a structure. -/\nmeta def projections_info (l : list projection_data) (pref : string) (str : name) : tactic format :=\ndo\n  \u27e8defaults, nondefaults\u27e9 \u2190 return $ l.partition_map $\n    \u03bb s, if s.is_default then inl s else inr s,\n  to_print \u2190 defaults.mmap $ \u03bb s, to_string <$>\n    let prefix_str := if s.is_prefix then \"(prefix) \" else \"\" in\n    pformat!\"Projection {prefix_str}{s.name}: {s.expr}\",\n  let print2 :=\n    string.join $ (nondefaults.map (\u03bb nm : projection_data, to_string nm.1)).intersperse \", \",\n  let to_print := to_print ++ if nondefaults.length = 0 then [] else\n    [\"No lemmas are generated for the projections: \" ++ print2 ++ \".\"],\n  let to_print := string.join $ to_print.intersperse \"\\n        > \",\n  return format!\"[simps] > {pref} {str}:\\n        > {to_print}\"\n\n/-- Auxiliary function of `get_composite_of_projections`. -/\nmeta def get_composite_of_projections_aux : \u03a0 (str : name) (proj : string) (x : expr)\n  (pos : list \u2115) (args : list expr), tactic (expr \u00d7 list \u2115) | str proj x pos args := do\n  e \u2190 get_env,\n  projs \u2190 e.structure_fields str,\n  let proj_info := projs.map_with_index $ \u03bb n p, (\u03bb x, (x, n, p)) <$> proj.get_rest (\"_\" ++ p.last),\n  when (proj_info.filter_map id = []) $\n    fail!\"Failed to find constructor {proj.popn 1} in structure {str}.\",\n  (proj_rest, index, proj_nm) \u2190 return (proj_info.filter_map id).ilast,\n  str_d \u2190 e.get str,\n  let proj_e : expr := const (str ++ proj_nm) str_d.univ_levels,\n  proj_d \u2190 e.get (str ++ proj_nm),\n  type \u2190 infer_type x,\n  let params := get_app_args type,\n  let univs := proj_d.univ_params.zip type.get_app_fn.univ_levels,\n  let new_x := (proj_e.instantiate_univ_params univs).mk_app $ params ++ [x],\n  let new_pos := pos ++ [index],\n  if proj_rest.is_empty then return (new_x.lambdas args, new_pos) else do\n    type \u2190 infer_type new_x,\n    (type_args, tgt) \u2190 open_pis_whnf type,\n    let new_str := tgt.get_app_fn.const_name,\n    get_composite_of_projections_aux new_str proj_rest (new_x.mk_app type_args) new_pos\n      (args ++ type_args)\n\n/-- Given a structure `str` and a projection `proj`, that could be multiple nested projections\n  (separated by `_`), returns an expression that is the composition of these projections and a\n  list of natural numbers, that are the projection numbers of the applied projections. -/\nmeta def get_composite_of_projections (str : name) (proj : string) : tactic (expr \u00d7 list \u2115) := do\n  e \u2190 get_env,\n  str_d \u2190 e.get str,\n  let str_e : expr := const str str_d.univ_levels,\n  type \u2190 infer_type str_e,\n  (type_args, tgt) \u2190 open_pis_whnf type,\n  let str_ap := str_e.mk_app type_args,\n  x \u2190 mk_local' `x binder_info.default str_ap,\n  get_composite_of_projections_aux str (\"_\" ++ proj) x [] $ type_args ++ [x]\n\n/--\n  Get the projections used by `simps` associated to a given structure `str`.\n\n  The returned information is also stored in a parameter of the attribute `@[_simps_str]`, which\n  is given to `str`. If `str` already has this attribute, the information is read from this\n  attribute instead. See the documentation for this attribute for the data this tactic returns.\n\n  The returned universe levels are the universe levels of the structure. For the projections there\n  are three cases\n  * If the declaration `{structure_name}.simps.{projection_name}` has been declared, then the value\n    of this declaration is used (after checking that it is definitionally equal to the actual\n    projection. If you rename the projection name, the declaration should have the *new* projection\n    name.\n  * You can also declare a custom projection that is a composite of multiple projections.\n  * Otherwise, for every class with the `notation_class` attribute, and the structure has an\n    instance of that notation class, then the projection of that notation class is used for the\n    projection that is definitionally equal to it (if there is such a projection).\n    This means in practice that coercions to function types and sorts will be used instead of\n    a projection, if this coercion is definitionally equal to a projection. Furthermore, for\n    notation classes like `has_mul` and `has_zero` those projections are used instead of the\n    corresponding projection.\n    Projections for coercions and notation classes are not automatically generated if they are\n    composites of multiple projections (for example when you use `extend` without the\n    `old_structure_cmd`).\n  * Otherwise, the projection of the structure is chosen.\n    For example: ``simps_get_raw_projections env `prod`` gives the default projections\n```\n  ([u, v], [prod.fst.{u v}, prod.snd.{u v}])\n```\n    while ``simps_get_raw_projections env `equiv`` gives\n```\n  ([u_1, u_2], [\u03bb \u03b1 \u03b2, coe_fn, \u03bb {\u03b1 \u03b2} (e : \u03b1 \u2243 \u03b2), \u21d1(e.symm), left_inv, right_inv])\n```\n    after declaring the coercion from `equiv` to function and adding the declaration\n```\n  def equiv.simps.inv_fun {\u03b1 \u03b2} (e : \u03b1 \u2243 \u03b2) : \u03b2 \u2192 \u03b1 := e.symm\n```\n\n  Optionally, this command accepts three optional arguments:\n  * If `trace_if_exists` the command will always generate a trace message when the structure already\n    has the attribute `@[_simps_str]`.\n  * The `rules` argument accepts a list of pairs `sum.inl (old_name, new_name)`. This is used to\n    change the projection name `old_name` to the custom projection name `new_name`. Example:\n    for the structure `equiv` the projection `to_fun` could be renamed `apply`. This name will be\n    used for parsing and generating projection names. This argument is ignored if the structure\n    already has an existing attribute. If an element of `rules` is of the form `sum.inr name`, this\n    means that the projection `name` will not be applied by default.\n  * if `trc` is true, this tactic will trace information.\n-/\n-- if performance becomes a problem, possible heuristic: use the names of the projections to\n-- skip all classes that don't have the corresponding field.\nmeta def simps_get_raw_projections (e : environment) (str : name) (trace_if_exists : bool := ff)\n  (rules : list projection_rule := []) (trc := ff) :\n  tactic (list name \u00d7 list projection_data) := do\n  let trc := trc || is_trace_enabled_for `simps.verbose,\n  has_attr \u2190 has_attribute' `_simps_str str,\n  if has_attr then do\n    data \u2190 simps_str_attr.get_param str,\n    -- We always print the projections when they already exists and are called by\n    -- `initialize_simps_projections`.\n    when (trace_if_exists || is_trace_enabled_for `simps.verbose) $ projections_info data.2\n      \"Already found projection information for structure\" str >>= trace,\n    return data\n  else do\n    when trc trace!\"[simps] > generating projection information for structure {str}.\",\n    when_tracing `simps.debug trace!\"[simps] > Applying the rules {rules}.\",\n    d_str \u2190 e.get str,\n    let raw_univs := d_str.univ_params,\n    let raw_levels := level.param <$> raw_univs,\n    /- Figure out projections, including renamings. The information for a projection is (before we\n    figure out the `expr` of the projection:\n    `(original name, given name, is default, is prefix)`.\n    The first projections are always the actual projections of the structure, but `rules` could\n    specify custom projections that are compositions of multiple projections. -/\n    projs \u2190 e.structure_fields str,\n    let projs : list parsed_projection_data := projs.map $ \u03bb nm, \u27e8nm, nm, tt, ff\u27e9,\n    let projs : list parsed_projection_data := rules.foldl (\u03bb projs rule,\n      match rule with\n      | (inl (old_nm, new_nm), is_prefix) := if old_nm \u2208 projs.map (\u03bb x, x.new_name) then\n        projs.map $ \u03bb proj,\n          if proj.new_name = old_nm then\n            { new_name := new_nm, is_prefix := is_prefix, ..proj } else\n            proj else\n        projs ++ [\u27e8old_nm, new_nm, tt, is_prefix\u27e9]\n      | (inr nm, is_prefix) := if nm \u2208 projs.map (\u03bb x, x.new_name) then\n        projs.map $ \u03bb proj, if proj.new_name = nm then\n          { is_default := ff, is_prefix := is_prefix, ..proj } else\n          proj else\n        projs ++ [\u27e8nm, nm, ff, is_prefix\u27e9]\n      end) projs,\n    when_tracing `simps.debug trace!\"[simps] > Projection info after applying the rules: {projs}.\",\n    when \u00ac (projs.map $ \u03bb x, x.new_name : list name).nodup $\n      fail $ \"Invalid projection names. Two projections have the same name.\nThis is likely because a custom composition of projections was given the same name as an \" ++\n\"existing projection. Solution: rename the existing projection (before renaming the custom \" ++\n\"projection).\",\n    /- Define the raw expressions for the projections, by default as the projections\n    (as an expression), but this can be overriden by the user. -/\n    raw_exprs_and_nrs \u2190 projs.mmap $ \u03bb \u27e8orig_nm, new_nm, _, _\u27e9, do\n    { (raw_expr, nrs) \u2190 get_composite_of_projections str orig_nm.last,\n      custom_proj \u2190 do\n      { decl \u2190 e.get (str ++ `simps ++ new_nm.last),\n        let custom_proj := decl.value.instantiate_univ_params $ decl.univ_params.zip raw_levels,\n        when trc trace!\n          \"[simps] > found custom projection for {new_nm}:\\n        > {custom_proj}\",\n        return custom_proj } <|> return raw_expr,\n      is_def_eq custom_proj raw_expr <|>\n        -- if the type of the expression is different, we show a different error message, because\n        -- that is more likely going to be helpful.\n        do\n        { custom_proj_type \u2190 infer_type custom_proj,\n          raw_expr_type \u2190 infer_type raw_expr,\n          b \u2190 succeeds (is_def_eq custom_proj_type raw_expr_type),\n          if b then fail!\"Invalid custom projection:\\n  {custom_proj}\nExpression is not definitionally equal to\\n  {raw_expr}\"\n          else fail!\"Invalid custom projection:\\n  {custom_proj}\nExpression has different type than {str ++ orig_nm}. Given type:\\n  {custom_proj_type}\nExpected type:\\n  {raw_expr_type}\" },\n      return (custom_proj, nrs) },\n    let raw_exprs := raw_exprs_and_nrs.map prod.fst,\n    /- Check for other coercions and type-class arguments to use as projections instead. -/\n    (args, _) \u2190 open_pis d_str.type,\n    let e_str := (expr.const str raw_levels).mk_app args,\n    automatic_projs \u2190 attribute.get_instances `notation_class,\n    raw_exprs \u2190 automatic_projs.mfoldl (\u03bb (raw_exprs : list expr) class_nm, do\n    { (is_class, proj_nm) \u2190 notation_class_attr.get_param class_nm,\n      proj_nm \u2190 proj_nm <|> (e.structure_fields_full class_nm).map list.head,\n      /- For this class, find the projection. `raw_expr` is the projection found applied to `args`,\n        and `lambda_raw_expr` has the arguments `args` abstracted. -/\n      (raw_expr, lambda_raw_expr) \u2190 if is_class then (do\n        guard $ args.length = 1,\n        let e_inst_type := (const class_nm raw_levels).mk_app args,\n        (hyp, e_inst) \u2190 try_for 1000 (mk_conditional_instance e_str e_inst_type),\n        raw_expr \u2190 mk_mapp proj_nm [args.head, e_inst],\n        clear hyp,\n        -- Note: `expr.bind_lambda` doesn't give the correct type\n        raw_expr_lambda \u2190 lambdas [hyp] raw_expr,\n        return (raw_expr, raw_expr_lambda.lambdas args))\n      else (do\n        e_inst_type \u2190 to_expr (((const class_nm []).app (pexpr.of_expr e_str)).app ``(_)),\n        e_inst \u2190 try_for 1000 (mk_instance e_inst_type),\n        raw_expr \u2190 mk_mapp proj_nm [e_str, none, e_inst],\n        return (raw_expr, raw_expr.lambdas args)),\n      raw_expr_whnf \u2190 whnf raw_expr,\n      let relevant_proj := raw_expr_whnf.binding_body.get_app_fn.const_name,\n      /- Use this as projection, if the function reduces to a projection, and this projection has\n        not been overrriden by the user. -/\n      guard $ projs.any $\n        \u03bb x, x.1 = relevant_proj.last \u2227 \u00ac e.contains (str ++ `simps ++ x.new_name.last),\n      let pos := projs.find_index (\u03bb x, x.1 = relevant_proj.last),\n      when trc trace!\n        \"        > using {proj_nm} instead of the default projection {relevant_proj.last}.\",\n      when_tracing `simps.debug trace!\"[simps] > The raw projection is:\\n  {lambda_raw_expr}\",\n      return $ raw_exprs.update_nth pos lambda_raw_expr } <|> return raw_exprs) raw_exprs,\n    let positions := raw_exprs_and_nrs.map prod.snd,\n    let proj_names := projs.map (\u03bb x, x.new_name),\n    let defaults := projs.map (\u03bb x, x.is_default),\n    let prefixes := projs.map (\u03bb x, x.is_prefix),\n    let projs := proj_names.zip_with5 projection_data.mk raw_exprs positions defaults prefixes,\n    /- make all proof non-default. -/\n    projs \u2190 projs.mmap $ \u03bb proj,\n      is_proof proj.expr >>= \u03bb b, return $ if b then { is_default := ff, .. proj } else proj,\n    when trc $ projections_info projs \"generated projections for\" str >>= trace,\n    simps_str_attr.set str (raw_univs, projs) tt,\n    when_tracing `simps.debug trace!\n       \"[simps] > Generated raw projection data: \\n{(raw_univs, projs)}\",\n    return (raw_univs, projs)\n\n/-- Parse a rule for `initialize_simps_projections`. It is either `<name>\u2192<name>` or `-<name>`,\n  possibly following by `as_prefix`.-/\nmeta def simps_parse_rule : parser projection_rule :=\nprod.mk <$>\n  ((\u03bb x y, inl (x, y)) <$> ident <*> (tk \"->\" >> ident) <|> inr <$> (tk \"-\" >> ident)) <*>\n  is_some <$> (tk \"as_prefix\")?\n\n/--\nYou can specify custom projections for the `@[simps]` attribute.\nTo do this for the projection `my_structure.original_projection` by adding a declaration\n`my_structure.simps.my_projection` that is definitionally equal to\n`my_structure.original_projection` but has the projection in the desired (simp-normal) form.\nThen you can call\n```\ninitialize_simps_projections (original_projection \u2192 my_projection, ...)\n```\nto register this projection. See `initialize_simps_projections_cmd` for more information.\n\nYou can also specify custom projections that are definitionally equal to a composite of multiple\nprojections. This is often desirable when extending structures (without `old_structure_cmd`).\n\n`has_coe_to_fun` and notation class (like `has_mul`) instances will be automatically used, if they\nare definitionally equal to a projection of the structure (but not when they are equal to the\ncomposite of multiple projections).\n-/\nlibrary_note \"custom simps projection\"\n\n/--\nThis command specifies custom names and custom projections for the simp attribute `simps_attr`.\n* You can specify custom names by writing e.g.\n  `initialize_simps_projections equiv (to_fun \u2192 apply, inv_fun \u2192 symm_apply)`.\n* See Note [custom simps projection] and the examples below for information how to declare custom\n  projections.\n* If no custom projection is specified, the projection will be `coe_fn`/`\u21d1` if a `has_coe_to_fun`\n  instance has been declared, or the notation of a notation class (like `has_mul`) if such an\n  instance is available. If none of these cases apply, the projection itself will be used.\n* You can disable a projection by default by running\n  `initialize_simps_projections equiv (-inv_fun)`\n  This will ensure that no simp lemmas are generated for this projection,\n  unless this projection is explicitly specified by the user.\n* If you want the projection name added as a prefix in the generated lemma name, you can add the\n  `as_prefix` modifier:\n  `initialize_simps_projections equiv (to_fun \u2192 coe as_prefix)`\n  Note that this does not influence the parsing of projection names: if you have a declaration\n  `foo` and you want to apply the projections `snd`, `coe` (which is a prefix) and `fst`, in that\n  order you can run `@[simps snd_coe_fst] def foo ...` and this will generate a lemma with the\n  name `coe_foo_snd_fst`.\n  * Run `initialize_simps_projections?` (or `set_option trace.simps.verbose true`)\n  to see the generated projections.\n* You can declare a new name for a projection that is the composite of multiple projections, e.g.\n  ```\n    structure A := (proj : \u2115)\n    structure B extends A\n    initialize_simps_projections? B (to_A_proj \u2192 proj, -to_A)\n  ```\n  You can also make your custom projection that is definitionally equal to a composite of\n  projections. In this case, coercions and notation classes are not automatically recognized, and\n  should be manually given by giving a custom projection.\n  This is especially useful when extending a structure (without `old_structure_cmd`).\n  In the above example, it is desirable to add `-to_A`, so that `@[simps]` doesn't automatically\n  apply the `B.to_A` projection and then recursively the `A.proj` projection in the lemmas it\n  generates. If you want to get both the `foo_proj` and `foo_to_A` simp lemmas, you can use\n  `@[simps, simps to_A]`.\n* Running `initialize_simps_projections my_struc` without arguments is not necessary, it has the\n  same effect if you just add `@[simps]` to a declaration.\n* If you do anything to change the default projections, make sure to call either `@[simps]` or\n  `initialize_simps_projections` in the same file as the structure declaration. Otherwise, you might\n  have a file that imports the structure, but not your custom projections.\n\nSome common uses:\n* If you define a new homomorphism-like structure (like `mul_hom`) you can just run\n  `initialize_simps_projections` after defining the `has_coe_to_fun` instance\n  ```\n    instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (M \u2192\u2099* N) := ...\n    initialize_simps_projections mul_hom (to_fun \u2192 apply)\n  ```\n  This will generate `foo_apply` lemmas for each declaration `foo`.\n* If you prefer `coe_foo` lemmas that state equalities between functions, use\n  `initialize_simps_projections mul_hom (to_fun \u2192 coe as_prefix)`\n  In this case you have to use `@[simps {fully_applied := ff}]` or equivalently `@[simps as_fn]`\n  whenever you call `@[simps]`.\n* You can also initialize to use both, in which case you have to choose which one to use by default,\n  by using either of the following\n  ```\n    initialize_simps_projections mul_hom (to_fun \u2192 apply, to_fun \u2192 coe, -coe as_prefix)\n    initialize_simps_projections mul_hom (to_fun \u2192 apply, to_fun \u2192 coe as_prefix, -apply)\n  ```\n  In the first case, you can get both lemmas using `@[simps, simps coe as_fn]` and in the second\n  case you can get both lemmas using `@[simps as_fn, simps apply]`.\n* If your new homomorphism-like structure extends another structure (without `old_structure_cmd`)\n  (like `rel_embedding`), then you have to specify explicitly that you want to use a coercion\n  as a custom projection. For example\n  ```\n    def rel_embedding.simps.apply (h : r \u21aar s) : \u03b1 \u2192 \u03b2 := h\n    initialize_simps_projections rel_embedding (to_embedding_to_fun \u2192 apply, -to_embedding)\n  ```\n* If you have an isomorphism-like structure (like `equiv`) you often want to define a custom\n  projection for the inverse:\n  ```\n    def equiv.simps.symm_apply (e : \u03b1 \u2243 \u03b2) : \u03b2 \u2192 \u03b1 := e.symm\n    initialize_simps_projections equiv (to_fun \u2192 apply, inv_fun \u2192 symm_apply)\n  ```\n-/\n@[user_command] meta def initialize_simps_projections_cmd\n  (_ : parse $ tk \"initialize_simps_projections\") : parser unit := do\n  env \u2190 get_env,\n  trc \u2190 is_some <$> (tk \"?\")?,\n  ns \u2190 (prod.mk <$> ident <*> (tk \"(\" >> sep_by (tk \",\") simps_parse_rule <* tk \")\")?)*,\n  ns.mmap' $ \u03bb data, do\n    nm \u2190 resolve_constant data.1,\n    simps_get_raw_projections env nm tt (data.2.get_or_else []) trc\n\nadd_tactic_doc\n{ name                     := \"initialize_simps_projections\",\n  category                 := doc_category.cmd,\n  decl_names               := [`initialize_simps_projections_cmd],\n  tags                     := [\"simplification\"] }\n\n/--\n  Configuration options for the `@[simps]` attribute.\n  * `attrs` specifies the list of attributes given to the generated lemmas. Default: ``[`simp]``.\n    The attributes can be either basic attributes, or user attributes without parameters.\n    There are two attributes which `simps` might add itself:\n    * If ``[`simp]`` is in the list, then ``[`_refl_lemma]`` is added automatically if appropriate.\n    * If the definition is marked with `@[to_additive ...]` then all generated lemmas are marked\n      with `@[to_additive]`. This is governed by the `add_additive` configuration option.\n  * if `simp_rhs` is `tt` then the right-hand-side of the generated lemmas will be put in\n    simp-normal form. More precisely: `dsimp, simp` will be called on all these expressions.\n    See note [dsimp, simp].\n  * `type_md` specifies how aggressively definitions are unfolded in the type of expressions\n    for the purposes of finding out whether the type is a function type.\n    Default: `instances`. This will unfold coercion instances (so that a coercion to a function type\n    is recognized as a function type), but not declarations like `set`.\n  * `rhs_md` specifies how aggressively definition in the declaration are unfolded for the purposes\n    of finding out whether it is a constructor.\n    Default: `none`\n    Exception: `@[simps]` will automatically add the options\n    `{rhs_md := semireducible, simp_rhs := tt}` if the given definition is not a constructor with\n    the given reducibility setting for `rhs_md`.\n  * If `fully_applied` is `ff` then the generated `simp` lemmas will be between non-fully applied\n    terms, i.e. equalities between functions. This does not restrict the recursive behavior of\n    `@[simps]`, so only the \"final\" projection will be non-fully applied.\n    However, it can be used in combination with explicit field names, to get a partially applied\n    intermediate projection.\n  * The option `not_recursive` contains the list of names of types for which `@[simps]` doesn't\n    recursively apply projections. For example, given an equivalence `\u03b1 \u00d7 \u03b2 \u2243 \u03b2 \u00d7 \u03b1` one usually\n    wants to only apply the projections for `equiv`, and not also those for `\u00d7`. This option is\n    only relevant if no explicit projection names are given as argument to `@[simps]`.\n  * The option `trace` is set to `tt` when you write `@[simps?]`. In this case, the attribute will\n    print all generated lemmas. It is almost the same as setting the option `trace.simps.verbose`,\n    except that it doesn't print information about the found projections.\n  * if `add_additive` is `some nm` then `@[to_additive]` is added to the generated lemma. This\n    option is automatically set to `tt` when the original declaration was tagged with\n    `@[to_additive, simps]` (in that order), where `nm` is the additive name of the original\n    declaration.\n-/\n@[derive [has_reflect, inhabited]] structure simps_cfg :=\n(attrs         := [`simp])\n(simp_rhs      := ff)\n(type_md       := transparency.instances)\n(rhs_md        := transparency.none)\n(fully_applied := tt)\n(not_recursive := [`prod, `pprod])\n(trace         := ff)\n(add_additive  := @none name)\n\n/-- A common configuration for `@[simps]`: generate equalities between functions instead equalities\n  between fully applied expressions. -/\ndef as_fn : simps_cfg := {fully_applied := ff}\n/-- A common configuration for `@[simps]`: don't tag the generated lemmas with `@[simp]`. -/\ndef lemmas_only : simps_cfg := {attrs := []}\n\n/--\n  Get the projections of a structure used by `@[simps]` applied to the appropriate arguments.\n  Returns a list of tuples\n  ```\n  (corresponding right-hand-side, given projection name, projection expression, projection numbers,\n    used by default, is prefix)\n  ```\n  (where all fields except the first are packed in a `projection_data` structure)\n  one for each projection. The given projection name is the name for the projection used by the user\n  used to generate (and parse) projection names. For example, in the structure\n\n  Example 1: ``simps_get_projection_exprs env `(\u03b1 \u00d7 \u03b2) `(\u27e8x, y\u27e9)`` will give the output\n  ```\n    [(`(x), `fst, `(@prod.fst.{u v} \u03b1 \u03b2), [0], tt, ff),\n     (`(y), `snd, `(@prod.snd.{u v} \u03b1 \u03b2), [1], tt, ff)]\n  ```\n\n  Example 2: ``simps_get_projection_exprs env `(\u03b1 \u2243 \u03b1) `(\u27e8id, id, \u03bb _, rfl, \u03bb _, rfl\u27e9)``\n  will give the output\n  ```\n    [(`(id), `apply, `(coe), [0], tt, ff),\n     (`(id), `symm_apply, `(\u03bb f, \u21d1f.symm), [1], tt, ff),\n     ...,\n     ...]\n  ```\n-/\nmeta def simps_get_projection_exprs (e : environment) (tgt : expr)\n  (rhs : expr) (cfg : simps_cfg) : tactic $ list $ expr \u00d7 projection_data := do\n  let params := get_app_args tgt, -- the parameters of the structure\n  (params.zip $ (get_app_args rhs).take params.length).mmap' (\u03bb \u27e8a, b\u27e9, is_def_eq a b)\n    <|> fail \"unreachable code (1)\",\n  let str := tgt.get_app_fn.const_name,\n  let rhs_args := (get_app_args rhs).drop params.length, -- the fields of the object\n  (raw_univs, proj_data) \u2190 simps_get_raw_projections e str ff [] cfg.trace,\n  let univs := raw_univs.zip tgt.get_app_fn.univ_levels,\n  let new_proj_data : list $ expr \u00d7 projection_data := proj_data.map $\n    \u03bb proj, (rhs_args.inth proj.proj_nrs.head,\n      { expr := (proj.expr.instantiate_univ_params univs).instantiate_lambdas_or_apps params,\n        proj_nrs := proj.proj_nrs.tail,\n        .. proj }),\n  return new_proj_data\n\n/-- Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`,\n  `args` is the list of local constants occurring, and `univs` is the list of universe variables. -/\nmeta def simps_add_projection (nm : name) (type lhs rhs : expr) (args : list expr)\n  (univs : list name) (cfg : simps_cfg) : tactic (list name) := do\n  when_tracing `simps.debug trace!\n    \"[simps] > Planning to add the equality\\n        > {lhs} = ({rhs} : {type})\",\n  lvl \u2190 get_univ_level type,\n  -- simplify `rhs` if `cfg.simp_rhs` is true\n  (rhs, prf) \u2190 do { guard cfg.simp_rhs,\n    rhs' \u2190 rhs.dsimp {fail_if_unchanged := ff},\n    when_tracing `simps.debug $ when (rhs \u2260 rhs') trace!\n      \"[simps] > `dsimp` simplified rhs to\\n        > {rhs'}\",\n    (rhsprf1, rhsprf2, ns) \u2190 rhs'.simp {fail_if_unchanged := ff},\n    when_tracing `simps.debug $ when (rhs' \u2260 rhsprf1) trace!\n      \"[simps] > `simp` simplified rhs to\\n        > {rhsprf1}\",\n    return (prod.mk rhsprf1 rhsprf2) }\n    <|> return (rhs, const `eq.refl [lvl] type lhs),\n  let eq_ap := const `eq [lvl] type lhs rhs,\n  decl_name \u2190 get_unused_decl_name nm,\n  let decl_type := eq_ap.pis args,\n  let decl_value := prf.lambdas args,\n  let decl := declaration.thm decl_name univs decl_type (pure decl_value),\n  when cfg.trace trace!\n    \"[simps] > adding projection {decl_name}:\\n        > {decl_type}\",\n  decorate_error (\"Failed to add projection lemma \" ++ decl_name.to_string ++ \". Nested error:\") $\n    add_decl decl,\n  b \u2190 succeeds $ is_def_eq lhs rhs,\n  when (b \u2227 `simp \u2208 cfg.attrs) (set_basic_attribute `_refl_lemma decl_name tt),\n  cfg.attrs.mmap' $ \u03bb nm, set_attribute nm decl_name tt,\n  when cfg.add_additive.is_some $\n    to_additive.attr.set decl_name \u27e8ff, cfg.trace, cfg.add_additive.iget, none, tt\u27e9 tt,\n  pure [decl_name]\n\n/-- Derive lemmas specifying the projections of the declaration.\n  If `todo` is non-empty, it will generate exactly the names in `todo`.\n  `to_apply` is non-empty after a custom projection that is a composition of multiple projections\n  was just used. In that case we need to apply these projections before we continue changing lhs. -/\nmeta def simps_add_projections : \u03a0 (e : environment) (nm : name)\n  (type lhs rhs : expr) (args : list expr) (univs : list name) (must_be_str : bool)\n  (cfg : simps_cfg) (todo : list string) (to_apply : list \u2115), tactic (list name)\n| e nm type lhs rhs args univs must_be_str cfg todo to_apply := do\n  -- we don't want to unfold non-reducible definitions (like `set`) to apply more arguments\n  when_tracing `simps.debug trace!\n    \"[simps] > Type of the expression before normalizing: {type}\",\n  (type_args, tgt) \u2190 open_pis_whnf type cfg.type_md,\n  when_tracing `simps.debug trace!\"[simps] > Type after removing pi's: {tgt}\",\n  tgt \u2190 whnf tgt,\n  when_tracing `simps.debug trace!\"[simps] > Type after reduction: {tgt}\",\n  let new_args := args ++ type_args,\n  let lhs_ap := lhs.instantiate_lambdas_or_apps type_args,\n  let rhs_ap := rhs.instantiate_lambdas_or_apps type_args,\n  let str := tgt.get_app_fn.const_name,\n  /- We want to generate the current projection if it is in `todo` -/\n  let todo_next := todo.filter (\u2260 \"\"),\n  /- Don't recursively continue if `str` is not a structure or if the structure is in\n    `not_recursive`. -/\n  if e.is_structure str \u2227 \u00ac(todo = [] \u2227 str \u2208 cfg.not_recursive \u2227 \u00acmust_be_str) then do\n    [intro] \u2190 return $ e.constructors_of str | fail \"unreachable code (3)\",\n    rhs_whnf \u2190 whnf rhs_ap cfg.rhs_md,\n    -- `todo_now` means that we still have to generate the current simp lemma\n    (rhs_ap, todo_now, added_lems_requested) \u2190\n      if \u00ac is_constant_of rhs_ap.get_app_fn intro \u2227\n        is_constant_of rhs_whnf.get_app_fn intro then do\n      /- If this was a desired projection, we want to apply it before taking the whnf.\n        However, if the current field is an eta-expansion (see below), we first want\n        to eta-reduce it and only then construct the projection.\n        This makes the flow of this function messy. -/\n      added_lems_requested \u2190 cond (\"\" \u2208 todo \u2227 to_apply = []) (if cfg.fully_applied then\n        simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else\n        simps_add_projection nm type lhs rhs args univs cfg) (pure []),\n      return (rhs_whnf, ff, added_lems_requested) else\n      return (rhs_ap, \"\" \u2208 todo \u2227 to_apply = [], []),\n    if is_constant_of (get_app_fn rhs_ap) intro then do -- if the value is a constructor application\n      proj_info \u2190 simps_get_projection_exprs e tgt rhs_ap cfg,\n      when_tracing `simps.debug trace!\"[simps] > Raw projection information:\\n  {proj_info}\",\n      eta \u2190 rhs_ap.is_eta_expansion, -- check whether `rhs_ap` is an eta-expansion\n      let rhs_ap := eta.lhoare rhs_ap, -- eta-reduce `rhs_ap`\n      /- As a special case, we want to automatically generate the current projection if `rhs_ap`\n        was an eta-expansion. Also, when this was a desired projection, we need to generate the\n        current projection if we haven't done it above. -/\n      added_lems_eta \u2190 cond (todo_now \u2228 (todo = [] \u2227 eta.is_some \u2227 to_apply = []))\n        (if cfg.fully_applied then\n          simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else\n          simps_add_projection nm type lhs rhs args univs cfg) (return []),\n      /- If we are in the middle of a composite projection. -/\n      added_lems_custom_proj \u2190 cond (to_apply \u2260 []) (do\n      { \u27e8new_rhs, proj, proj_expr, proj_nrs, is_default, is_prefix\u27e9 \u2190\n          return $ proj_info.inth to_apply.head,\n        new_type \u2190 infer_type new_rhs,\n        when_tracing `simps.debug\n          trace!\"[simps] > Applying a custom composite projection. Current lhs:\n        >  {lhs_ap}\",\n        simps_add_projections e nm new_type lhs_ap new_rhs new_args univs ff cfg todo\n          to_apply.tail }) (pure []),\n      let all_added_lems := added_lems_requested ++ added_lems_eta ++ added_lems_custom_proj,\n      /- We stop if no further projection is specified or if we just reduced an eta-expansion and we\n      automatically choose projections -/\n      cond (\u00ac(to_apply \u2260 [] \u2228 todo = [\"\"] \u2228 (eta.is_some \u2227 todo = []))) (do\n        let projs : list name := proj_info.map $ \u03bb x, x.snd.name,\n        let todo := if to_apply = [] then todo_next else todo,\n        -- check whether all elements in `todo` have a projection as prefix\n        guard (todo.all $ \u03bb x, projs.any $ \u03bb proj, (\"_\" ++ proj.last).is_prefix_of x) <|>\n          let x := (todo.find $ \u03bb x, projs.all $ \u03bb proj, \u00ac (\"_\" ++ proj.last).is_prefix_of x).iget,\n            simp_lemma := nm.append_suffix x,\n            needed_proj := (x.split_on '_').tail.head in\n          fail!\n\"Invalid simp lemma {simp_lemma}. Structure {str} does not have projection {needed_proj}.\nThe known projections are:\n  {projs}\nYou can also see this information by running\n  `initialize_simps_projections? {str}`.\nNote: these projection names might not correspond to the projection names of the structure.\",\n        added_lems_list \u2190 proj_info.mmap_with_index\n          (\u03bb proj_nr \u27e8new_rhs, proj, proj_expr, proj_nrs, is_default, is_prefix\u27e9, do\n          new_type \u2190 infer_type new_rhs,\n          let new_todo :=\n            todo.filter_map $ \u03bb x, x.get_rest (\"_\" ++ proj.last),\n          -- we only continue with this field if it is non-propositional or mentioned in todo\n          cond ((is_default \u2227 todo = []) \u2228 new_todo \u2260 []) (do\n            let new_lhs := proj_expr.instantiate_lambdas_or_apps [lhs_ap],\n            let new_nm := nm.append_to_last proj.last is_prefix,\n            let new_cfg := { add_additive := cfg.add_additive.map $\n              \u03bb nm, nm.append_to_last (to_additive.guess_name proj.last) is_prefix, ..cfg },\n            when_tracing `simps.debug trace!\"[simps] > Recursively add projections for:\n        >  {new_lhs}\",\n            simps_add_projections e new_nm new_type new_lhs new_rhs new_args univs\n              ff new_cfg new_todo proj_nrs) (pure [])),\n          pure $ all_added_lems ++ added_lems_list.join) (pure all_added_lems)\n    -- if I'm about to run into an error, try to set the transparency for `rhs_md` higher.\n    else if cfg.rhs_md = transparency.none \u2227 (must_be_str \u2228 todo_next \u2260 [] \u2228 to_apply \u2260 []) then do\n      when cfg.trace trace!\n        \"[simps] > The given definition is not a constructor application:\n        >   {rhs_ap}\n        > Retrying with the options {{ rhs_md := semireducible, simp_rhs := tt}.\",\n      added_lems_recursive \u2190 simps_add_projections e nm type lhs rhs args univs must_be_str\n        { rhs_md := semireducible, simp_rhs := tt, ..cfg} todo to_apply,\n      pure $ added_lems_requested ++ added_lems_recursive\n    else do\n      when (to_apply \u2260 []) $\n        fail!\"Invalid simp lemma {nm}.\nThe given definition is not a constructor application:\\n  {rhs_ap}\",\n      when must_be_str $\n        fail!\"Invalid `simps` attribute. The body is not a constructor application:\\n  {rhs_ap}\",\n      when (todo_next \u2260 []) $\n        fail!\"Invalid simp lemma {nm.append_suffix todo_next.head}.\nThe given definition is not a constructor application:\\n  {rhs_ap}\",\n      added_lems_no_constructor \u2190 if cfg.fully_applied then\n        simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else\n        simps_add_projection nm type lhs rhs args univs cfg,\n      pure $ added_lems_requested ++ added_lems_no_constructor\n  else do\n    when must_be_str $\n      fail!\"Invalid `simps` attribute. Target {str} is not a structure\",\n    when (todo_next \u2260 [] \u2227 str \u2209 cfg.not_recursive) $\n        let first_todo := todo_next.head in\n        fail!\"Invalid simp lemma {nm.append_suffix first_todo}.\nProjection {(first_todo.split_on '_').tail.head} doesn't exist, because target is not a structure.\",\n    if cfg.fully_applied then\n      simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else\n      simps_add_projection nm type lhs rhs args univs cfg\n\n/--\nThe `@[_simps_aux]` attribute specifies which lemmas are added by `simps`.\nThis should not be used manually and it only exists for mathport\n-/\n@[user_attribute] meta def simps_aux : user_attribute unit (list name) :=\n{ name := `_simps_aux,\n  descr := \"An attribute specifying the added simps lemmas.\",\n  parser := failed }\n\n/-- `simps_tac` derives `simp` lemmas for all (nested) non-Prop projections of the declaration.\n  If `todo` is non-empty, it will generate exactly the names in `todo`.\n  If `short_nm` is true, the generated names will only use the last projection name.\n  If `trc` is true, trace as if `trace.simps.verbose` is true. -/\nmeta def simps_tac (nm : name) (cfg : simps_cfg := {}) (todo : list string := []) (trc := ff) :\n  tactic unit := do\n  e \u2190 get_env,\n  d \u2190 e.get nm,\n  let lhs : expr := const d.to_name d.univ_levels,\n  let todo := todo.dedup.map $ \u03bb proj, \"_\" ++ proj,\n  let cfg := { trace := cfg.trace || is_trace_enabled_for `simps.verbose || trc, ..cfg },\n  b \u2190 has_attribute' `to_additive nm,\n  cfg \u2190 if b then do\n  { dict \u2190 to_additive.aux_attr.get_cache,\n    when cfg.trace\n      trace!\"[simps] > @[to_additive] will be added to all generated lemmas.\",\n    return { add_additive := dict.find nm, ..cfg } } else\n    return cfg,\n  added_names \u2190 simps_add_projections e nm d.type lhs d.value [] d.univ_params tt cfg todo [],\n  simps_aux.set nm added_names true\n\n\n/-- The parser for the `@[simps]` attribute. -/\nmeta def simps_parser : parser (bool \u00d7 list string \u00d7 simps_cfg) := do\n/- note: we don't check whether the user has written a nonsense namespace in an argument. -/\nprod.mk <$> is_some <$> (tk \"?\")? <*>\n  (prod.mk <$> many (name.last <$> ident) <*>\n  (do some e \u2190 parser.pexpr? | return {}, eval_pexpr simps_cfg e))\n\n/--\nThe `@[simps]` attribute automatically derives lemmas specifying the projections of this\ndeclaration.\n\nExample:\n```lean\n@[simps] def foo : \u2115 \u00d7 \u2124 := (1, 2)\n```\nderives two `simp` lemmas:\n```lean\n@[simp] lemma foo_fst : foo.fst = 1\n@[simp] lemma foo_snd : foo.snd = 2\n```\n\n* It does not derive `simp` lemmas for the prop-valued projections.\n* It will automatically reduce newly created beta-redexes, but will not unfold any definitions.\n* If the structure has a coercion to either sorts or functions, and this is defined to be one\n  of the projections, then this coercion will be used instead of the projection.\n* If the structure is a class that has an instance to a notation class, like `has_mul`, then this\n  notation is used instead of the corresponding projection.\n* You can specify custom projections, by giving a declaration with name\n  `{structure_name}.simps.{projection_name}`. See Note [custom simps projection].\n\n  Example:\n  ```lean\n  def equiv.simps.inv_fun (e : \u03b1 \u2243 \u03b2) : \u03b2 \u2192 \u03b1 := e.symm\n  @[simps] def equiv.trans (e\u2081 : \u03b1 \u2243 \u03b2) (e\u2082 : \u03b2 \u2243 \u03b3) : \u03b1 \u2243 \u03b3 :=\n  \u27e8e\u2082 \u2218 e\u2081, e\u2081.symm \u2218 e\u2082.symm\u27e9\n  ```\n  generates\n  ```\n  @[simp] lemma equiv.trans_to_fun : \u2200 {\u03b1 \u03b2 \u03b3} (e\u2081 e\u2082) (a : \u03b1), \u21d1(e\u2081.trans e\u2082) a = (\u21d1e\u2082 \u2218 \u21d1e\u2081) a\n  @[simp] lemma equiv.trans_inv_fun : \u2200 {\u03b1 \u03b2 \u03b3} (e\u2081 e\u2082) (a : \u03b3),\n    \u21d1((e\u2081.trans e\u2082).symm) a = (\u21d1(e\u2081.symm) \u2218 \u21d1(e\u2082.symm)) a\n  ```\n\n* You can specify custom projection names, by specifying the new projection names using\n  `initialize_simps_projections`.\n  Example: `initialize_simps_projections equiv (to_fun \u2192 apply, inv_fun \u2192 symm_apply)`.\n  See `initialize_simps_projections_cmd` for more information.\n\n* If one of the fields itself is a structure, this command will recursively create\n  `simp` lemmas for all fields in that structure.\n  * Exception: by default it will not recursively create `simp` lemmas for fields in the structures\n    `prod` and `pprod`. You can give explicit projection names or change the value of\n    `simps_cfg.not_recursive` to override this behavior.\n\n  Example:\n  ```lean\n  structure my_prod (\u03b1 \u03b2 : Type*) := (fst : \u03b1) (snd : \u03b2)\n  @[simps] def foo : prod \u2115 \u2115 \u00d7 my_prod \u2115 \u2115 := \u27e8\u27e81, 2\u27e9, 3, 4\u27e9\n  ```\n  generates\n  ```lean\n  @[simp] lemma foo_fst : foo.fst = (1, 2)\n  @[simp] lemma foo_snd_fst : foo.snd.fst = 3\n  @[simp] lemma foo_snd_snd : foo.snd.snd = 4\n  ```\n\n* You can use `@[simps proj1 proj2 ...]` to only generate the projection lemmas for the specified\n  projections.\n* Recursive projection names can be specified using `proj1_proj2_proj3`.\n  This will create a lemma of the form `foo.proj1.proj2.proj3 = ...`.\n\n  Example:\n  ```lean\n  structure my_prod (\u03b1 \u03b2 : Type*) := (fst : \u03b1) (snd : \u03b2)\n  @[simps fst fst_fst snd] def foo : prod \u2115 \u2115 \u00d7 my_prod \u2115 \u2115 := \u27e8\u27e81, 2\u27e9, 3, 4\u27e9\n  ```\n  generates\n  ```lean\n  @[simp] lemma foo_fst : foo.fst = (1, 2)\n  @[simp] lemma foo_fst_fst : foo.fst.fst = 1\n  @[simp] lemma foo_snd : foo.snd = {fst := 3, snd := 4}\n  ```\n* If one of the values is an eta-expanded structure, we will eta-reduce this structure.\n\n  Example:\n  ```lean\n  structure equiv_plus_data (\u03b1 \u03b2) extends \u03b1 \u2243 \u03b2 := (data : bool)\n  @[simps] def bar {\u03b1} : equiv_plus_data \u03b1 \u03b1 := { data := tt, ..equiv.refl \u03b1 }\n  ```\n  generates the following:\n  ```lean\n  @[simp] lemma bar_to_equiv : \u2200 {\u03b1 : Sort*}, bar.to_equiv = equiv.refl \u03b1\n  @[simp] lemma bar_data : \u2200 {\u03b1 : Sort*}, bar.data = tt\n  ```\n  This is true, even though Lean inserts an eta-expanded version of `equiv.refl \u03b1` in the\n  definition of `bar`.\n* For configuration options, see the doc string of `simps_cfg`.\n* The precise syntax is `('simps' ident* e)`, where `e` is an expression of type `simps_cfg`.\n* `@[simps]` reduces let-expressions where necessary.\n* When option `trace.simps.verbose` is true, `simps` will print the projections it finds and the\n  lemmas it generates. The same can be achieved by using `@[simps?]`, except that in this case it\n  will not print projection information.\n* Use `@[to_additive, simps]` to apply both `to_additive` and `simps` to a definition, making sure\n  that `simps` comes after `to_additive`. This will also generate the additive versions of all\n  `simp` lemmas.\n-/\n/- If one of the fields is a partially applied constructor, we will eta-expand it\n  (this likely never happens, so is not included in the official doc). -/\n@[user_attribute] meta def simps_attr : user_attribute unit (bool \u00d7 list string \u00d7 simps_cfg) :=\n{ name := `simps,\n  descr := \"Automatically derive lemmas specifying the projections of this declaration.\",\n  parser := simps_parser,\n  after_set := some $\n    \u03bb n _ persistent, do\n      guard persistent <|> fail \"`simps` currently cannot be used as a local attribute\",\n      (trc, todo, cfg) \u2190 simps_attr.get_param n,\n      simps_tac n cfg todo trc }\n\nadd_tactic_doc\n{ name                     := \"simps\",\n  category                 := doc_category.attr,\n  decl_names               := [`simps_attr],\n  tags                     := [\"simplification\"] }\n", "meta": {"author": "leanprover-community", "repo": "mathlib", "sha": "5e526d18cea33550268dcbbddcb822d5cde40654", "save_path": "github-repos/lean/leanprover-community-mathlib", "path": "github-repos/lean/leanprover-community-mathlib/mathlib-5e526d18cea33550268dcbbddcb822d5cde40654/src/tactic/simps.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4960938294709195, "lm_q2_score": 0.10521053950871516, "lm_q1q2_score": 0.052194299445579975}}
{"text": "/-\nCopyright (c) 2017 Mario Carneiro. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Mario Carneiro\n-/\nimport tactic.doc_commands\n\n/-!\n# `generalize_proofs`\n\nA simple tactic to find and replace all occurrences of proof terms in the\ncontext and goal with new variables.\n-/\n\nnamespace tactic\n\nprivate meta def collect_proofs_in :\n  expr \u2192 list expr \u2192 list name \u00d7 list expr \u2192 tactic (list name \u00d7 list expr)\n| e ctx (ns, hs) :=\nlet go (tac : list name \u00d7 list expr \u2192 tactic (list name \u00d7 list expr)) :\n  tactic (list name \u00d7 list expr) :=\ndo t \u2190 infer_type e,\n   mcond (is_prop t) (do\n     first (hs.map $ \u03bb h, do\n       t' \u2190 infer_type h,\n       is_def_eq t t',\n       g \u2190 target,\n       change $ g.replace (\u03bb a n, if a = e then some h else none),\n       return (ns, hs)) <|>\n     (let (n, ns) := (match ns with\n        | [] := (`_x, [])\n        | (n :: ns) := (n, ns)\n        end : name \u00d7 list name) in\n      do generalize e n,\n         h \u2190 intro n,\n         return (ns, h::hs)) <|> return (ns, hs)) (tac (ns, hs)) in\nmatch e with\n| (expr.const _ _)   := go return\n| (expr.local_const _ _ _ _) := do t \u2190 infer_type e, collect_proofs_in t ctx (ns, hs)\n| (expr.mvar _ _ _)  := do t \u2190 infer_type e, collect_proofs_in t ctx (ns, hs)\n| (expr.app f x)     :=\n  go (\u03bb nh, collect_proofs_in f ctx nh >>= collect_proofs_in x ctx)\n| (expr.lam n b d e) :=\n  go (\u03bb nh, do\n    nh \u2190 collect_proofs_in d ctx nh,\n    var \u2190 mk_local' n b d,\n    collect_proofs_in (expr.instantiate_var e var) (var::ctx) nh)\n| (expr.pi n b d e) := do\n  nh \u2190 collect_proofs_in d ctx (ns, hs),\n  var \u2190 mk_local' n b d,\n  collect_proofs_in (expr.instantiate_var e var) (var::ctx) nh\n| (expr.elet n t d e) :=\n  go (\u03bb nh, do\n    nh \u2190 collect_proofs_in t ctx nh,\n    nh \u2190 collect_proofs_in d ctx nh,\n    collect_proofs_in (expr.instantiate_var e d) ctx nh)\n| (expr.macro m l) :=\n  go (\u03bb nh, mfoldl (\u03bb x e, collect_proofs_in e ctx x) nh l)\n| _                  := return (ns, hs)\nend\n\n/-- Generalize proofs in the goal, naming them with the provided list. -/\nmeta def generalize_proofs (ns : list name) (loc : interactive.loc) : tactic unit :=\ndo intros_dep,\n  hs \u2190 local_context >>= mfilter is_proof,\n  n \u2190 loc.get_locals >>= revert_lst,\n  t \u2190 target,\n  collect_proofs_in t [] (ns, hs),\n  intron n <|> (intros $> ())\n\nopen interactive interactive.types lean.parser\nlocal postfix *:9001 := many\n\nnamespace interactive\n/-- Generalize proofs in the goal, naming them with the provided list.\n\nFor example:\n```lean\nexample : list.nth_le [1, 2] 1 dec_trivial = 2 :=\nbegin\n  -- \u22a2 [1, 2].nth_le 1 _ = 2\n  generalize_proofs h,\n  -- h : 1 < [1, 2].length\n  -- \u22a2 [1, 2].nth_le 1 h = 2\nend\n```\n-/\nmeta def generalize_proofs : parse ident_* \u2192 parse location \u2192 tactic unit :=\ntactic.generalize_proofs\nend interactive\n\nadd_tactic_doc\n{ name       := \"generalize_proofs\",\n  category   := doc_category.tactic,\n  decl_names := [`tactic.interactive.generalize_proofs],\n  tags       := [\"context management\"] }\n\nend tactic\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/tactic/generalize_proofs.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4416730056646256, "lm_q2_score": 0.11757214127540648, "lm_q1q2_score": 0.05192844101953477}}
{"text": "/-\nCopyright (c) 2020 Floris van Doorn. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Floris van Doorn\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.tactic.lint.default\nimport Mathlib.tactic.ext\nimport Mathlib.PostPort\n\nnamespace Mathlib\n\n/-!\n# Congruence and related tactics\n\nThis file contains the tactic `congr'`, which is an extension of `congr`, and various tactics\nusing `congr'` internally.\n\n`congr'` has some advantages over `congr`:\n* It turns `\u2194` to equalities, before trying another congr lemma\n* You can write `congr' n` to give the maximal depth of recursive applications. This is useful if\n  `congr` breaks down the goal to aggressively, and the resulting goals are false.\n* You can write `congr' with ...` to do `congr', ext ...` in a single tactic.\n\nOther tactics in this file:\n* `rcongr`: repeatedly apply `congr'` and `ext.`\n* `convert`: like `exact`, but produces an equality goal if the type doesn't match.\n* `convert_to`: changes the goal, if you prove an equality between the old goal and the new goal.\n* `ac_change`: like `convert_to`, but uses `ac_refl` to discharge the goals.\n-/\n\nnamespace tactic\n\n\n/-- Apply the constant `iff_of_eq` to the goal. -/\n/-- The main part of the body for the loop in `congr'`. This will try to replace a goal `f x = f y`\n with `x = y`. Also has support for `==` and `\u2194`. -/\n/-- The main function in `convert_to`. Changes the goal to `r` and a proof obligation that the goal\n  is equal to `r`. -/\n/--\nSame as the `congr` tactic, but takes an optional argument which gives\nthe depth of recursive applications.\n* This is useful when `congr` is too aggressive in breaking down the goal.\n* For example, given `\u22a2 f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `\u22a2 x = y`\n  and `\u22a2 y = x`, while `congr' 2` produces the intended `\u22a2 x + y = y + x`.\n* If, at any point, a subgoal matches a hypothesis then the subgoal will be closed.\n-/\nnamespace interactive\n\n\n/--\nSame as the `congr` tactic, but takes an optional argument which gives\nthe depth of recursive applications.\n* This is useful when `congr` is too aggressive in breaking down the goal.\n* For example, given `\u22a2 f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `\u22a2 x = y`\n  and `\u22a2 y = x`, while `congr' 2` produces the intended `\u22a2 x + y = y + x`.\n* If, at any point, a subgoal matches a hypothesis then the subgoal will be closed.\n* You can use `congr' with p (: n)?` to call `ext p (: n)?` to all subgoals generated by `congr'`.\n  For example, if the goal is `\u22a2 f '' s = g '' s` then `congr' with x` generates the goal\n  `x : \u03b1 \u22a2 f x = g x`.\n-/\n/--\nRepeatedly and apply `congr'` and `ext`, using the the given patterns as arguments for `ext`.\n\nThere are two ways this tactic stops:\n* `congr'` fails (makes no progress), after having already applied `ext`.\n* `congr'` canceled out the last usage of `ext`. In this case, the state is reverted to before\n  the `congr'` was applied.\n\nFor example, when the goal is\n```lean\n\u22a2 (\u03bb x, f x + 3) '' s = (\u03bb x, g x + 3) '' s\n```\nthen `rcongr x` produces the goal\n```lean\nx : \u03b1 \u22a2 f x = g x\n```\nThis gives the same result as `congr', ext x, congr'`.\n\nIn contrast, `congr'` would produce\n```lean\n\u22a2 (\u03bb x, f x + 3) = (\u03bb x, g x + 3)\n```\nand `congr' with x` (or `congr', ext x`) would produce\n```lean\nx : \u03b1 \u22a2 f x + 3 = g x + 3\n```\n-/\n/--\nThe `exact e` and `refine e` tactics require a term `e` whose type is\ndefinitionally equal to the goal. `convert e` is similar to `refine\ne`, but the type of `e` is not required to exactly match the\ngoal. Instead, new goals are created for differences between the type\nof `e` and the goal. For example, in the proof state\n\n```lean\nn : \u2115,\ne : prime (2 * n + 1)\n\u22a2 prime (n + n + 1)\n```\n\nthe tactic `convert e` will change the goal to\n\n```lean\n\u22a2 n + n = 2 * n\n```\n\nIn this example, the new goal can be solved using `ring`.\n\nIf `x y : t`, and an instance `subsingleton t` is in scope, then any goals of the form\n`x = y` are solved automatically.\n\nThe syntax `convert \u2190 e` will reverse the direction of the new goals\n(producing `\u22a2 2 * n = n + n` in this example).\n\nInternally, `convert e` works by creating a new goal asserting that\nthe goal equals the type of `e`, then simplifying it using\n`congr'`. The syntax `convert e using n` can be used to control the\ndepth of matching (like `congr' n`). In the example, `convert e using\n1` would produce a new goal `\u22a2 n + n + 1 = 2 * n + 1`.\n-/\n/--\n`convert_to g using n` attempts to change the current goal to `g`, but unlike `change`,\nit will generate equality proof obligations using `congr' n` to resolve discrepancies.\n`convert_to g` defaults to using `congr' 1`.\n\n`ac_change` is `convert_to` followed by `ac_refl`. It is useful for rearranging/reassociating\ne.g. sums:\n```lean\nexample (a b c d e f g N : \u2115) : (a + b) + (c + d) + (e + f) + g \u2264 N :=\nbegin\n  ac_change a + d + e + f + c + g + b \u2264 _,\n-- \u22a2 a + d + e + f + c + g + b \u2264 N\n\n-- \u22a2 a + d + e + f + c + g + b \u2264 N\nend\n```\n-/\n/-- `ac_change g using n` is `convert_to g using n; try {ac_refl}`. -/\nend Mathlib", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/tactic/congr_auto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.5, "lm_q2_score": 0.10374861786009558, "lm_q1q2_score": 0.05187430893004779}}
{"text": "/-\nCopyright (c) 2022 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Patrick Massot, Scott Morrison\n\n! This file was ported from Lean 3 source module tactic.assert_exists\n! leanprover-community/mathlib commit 90367774bb3afc7bdc1e9acbc770970042378306\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Tactic.Core\nimport Mathbin.Tactic.Lint.Basic\n\n/-!\n# User commands for assert the (non-)existence of declaration or instances.\n\nThese commands are used to enforce the independence of different parts of mathlib.\n\n## Implementation notes\n\nThis file provides two linters that verify that things we assert do not _yet_ exist do _eventually_\nexist. This works by creating declarations of the form:\n\n* ``assert_not_exists._checked.<uniq> : name := `foo`` for `assert_not_exists foo`\n* `assert_no_instance._checked.<uniq> := t` for `assert_instance t`\n\nThese declarations are then picked up by the linter and analyzed accordingly.\nThe `_` in the `_checked` prefix should hide them from doc-gen.\n-/\n\n\nsection\n\n/- ./././Mathport/Syntax/Translate/Tactic/Mathlib/Core.lean:38:34: unsupported: setup_tactic_parser -/\nopen Tactic\n\n/-- `assert_exists n` is a user command that asserts that a declaration named `n` exists\nin the current import scope.\n\nBe careful to use names (e.g. `rat`) rather than notations (e.g. `\u211a`).\n-/\n@[user_command]\nunsafe def assert_exists (_ : parse <| tk \"assert_exists\") : lean.parser Unit := do\n  let decl \u2190 ident\n  let d \u2190 get_decl decl\n  return ()\n#align assert_exists assert_exists\n\n/--\n`assert_not_exists n` is a user command that asserts that a declaration named `n` *does not exist*\nin the current import scope.\n\nBe careful to use names (e.g. `rat`) rather than notations (e.g. `\u211a`).\n\nIt may be used (sparingly!) in mathlib to enforce plans that certain files\nare independent of each other.\n\nIf you encounter an error on an `assert_not_exists` command while developing mathlib,\nit is probably because you have introduced new import dependencies to a file.\n\nIn this case, you should refactor your work\n(for example by creating new files rather than adding imports to existing files).\nYou should *not* delete the `assert_not_exists` statement without careful discussion ahead of time.\n-/\n@[user_command]\nunsafe def assert_not_exists (_ : parse <| tk \"assert_not_exists\") : lean.parser Unit := do\n  let decl \u2190 ident\n  let ff \u2190 succeeds (get_decl decl) |\n    fail f! \"Declaration {decl} is not allowed to exist in this file.\"\n  let n \u2190 tactic.mk_fresh_name\n  let marker := `assert_not_exists._checked.append (decl.append n)\n  add_decl (declaration.defn marker [] q(Name) q(decl) default tt)\n  pure ()\n#align assert_not_exists assert_not_exists\n\n/-- A linter for checking that the declarations marked `assert_not_exists` eventually exist. -/\nunsafe def assert_not_exists.linter : linter\n    where\n  test d := do\n    let n := d.to_name\n    let tt \u2190 pure (`assert_not_exists._checked.isPrefixOf\u2093 n) |\n      pure none\n    let declaration.defn _ _ q(Name) val _ _ \u2190 pure d\n    let n \u2190 tactic.eval_expr Name val\n    let tt \u2190 succeeds (get_decl n) |\n      pure (some (f! \"`{n}` does not ever exist\").toString)\n    pure none\n  auto_decls := true\n  no_errors_found := \"All `assert_not_exists` declarations eventually exist.\"\n  errors_found :=\n    \"The following declarations used in `assert_not_exists` never exist; perhaps there is a typo.\"\n  is_fast := true\n#align assert_not_exists.linter assert_not_exists.linter\n\n/-- `assert_instance e` is a user command that asserts that an instance `e` is available\nin the current import scope.\n\nExample usage:\n```\nassert_instance semiring \u2115\n```\n-/\n@[user_command]\nunsafe def assert_instance (_ : parse <| tk \"assert_instance\") : lean.parser Unit := do\n  let q \u2190 texpr\n  let e \u2190 i_to_expr q\n  mk_instance e\n  return ()\n#align assert_instance assert_instance\n\n/-- `assert_no_instance e` is a user command that asserts that an instance `e` *is not available*\nin the current import scope.\n\nIt may be used (sparingly!) in mathlib to enforce plans that certain files\nare independent of each other.\n\nIf you encounter an error on an `assert_no_instance` command while developing mathlib,\nit is probably because you have introduced new import dependencies to a file.\n\nIn this case, you should refactor your work\n(for example by creating new files rather than adding imports to existing files).\nYou should *not* delete the `assert_no_instance` statement without careful discussion ahead of time.\n\nExample usage:\n```\nassert_no_instance linear_ordered_field \u211a\n```\n-/\n@[user_command]\nunsafe def assert_no_instance (_ : parse <| tk \"assert_no_instance\") : lean.parser Unit := do\n  let q \u2190 texpr\n  let e \u2190 i_to_expr q\n  let i \u2190 try_core (mk_instance e)\n  match i with\n    | none => do\n      let n \u2190 tactic.mk_fresh_name\n      let e_str \u2190 toString <$> pp e\n      let marker := (`assert_no_instance._checked.mk_string e_str).append n\n      let et \u2190 infer_type e\n      let tt \u2190 succeeds (get_decl marker) |\n        add_decl (declaration.defn marker [] et e default tt)\n      pure ()\n    | some i =>\n      (throwError \"Instance `{(\u2190 i)} : {\u2190 e}` is not allowed to be found in this file.\" :\n        tactic Unit)\n#align assert_no_instance assert_no_instance\n\n/-- A linter for checking that the declarations marked `assert_no_instance` eventually exist. -/\nunsafe def assert_no_instance.linter : linter\n    where\n  test d := do\n    let n := d.to_name\n    let tt \u2190 pure (`assert_no_instance._checked.isPrefixOf\u2093 n) |\n      pure none\n    let declaration.defn _ _ _ val _ _ \u2190 pure d\n    let tt \u2190 succeeds (tactic.mk_instance val) |\n      (some \u2218 format.to_string) <$> f!\"No instance of `{\u2190 val}`\"\n    pure none\n  auto_decls := true\n  no_errors_found := \"All `assert_no_instance` instances eventually exist.\"\n  errors_found :=\n    \"The following typeclass instances used in `assert_no_instance` never exist; perhaps they \" ++\n      \"are missing?\"\n  is_fast := false\n#align assert_no_instance.linter assert_no_instance.linter\n\nend\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/Tactic/AssertExists.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.37754066879814546, "lm_q2_score": 0.13660840232900245, "lm_q1q2_score": 0.05157522757873772}}
{"text": "/-\nCopyright (c) 2019 Robert A. Spencer. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Robert A. Spencer, Markus Himmel\n-/\nimport Mathlib.PrePort\nimport Mathlib.Lean3Lib.init.default\nimport Mathlib.algebra.category.Group.basic\nimport Mathlib.category_theory.concrete_category.default\nimport Mathlib.category_theory.limits.shapes.kernels\nimport Mathlib.category_theory.preadditive.default\nimport Mathlib.linear_algebra.basic\nimport Mathlib.PostPort\n\nuniverses v u l u_1 \n\nnamespace Mathlib\n\n/-- The category of R-modules and their morphisms. -/\nstructure Module (R : Type u) [ring R] \nwhere\n  carrier : Type v\n  is_add_comm_group : add_comm_group carrier\n  is_module : module R carrier\n\nnamespace Module\n\n\n-- TODO revisit this after #1438 merges, to check coercions and instances are handled consistently\n\nprotected instance has_coe_to_sort (R : Type u) [ring R] : has_coe_to_sort (Module R) :=\n  has_coe_to_sort.mk (Type v) carrier\n\nprotected instance category_theory.category (R : Type u) [ring R] : category_theory.category (Module R) :=\n  category_theory.category.mk\n\nprotected instance category_theory.concrete_category (R : Type u) [ring R] : category_theory.concrete_category (Module R) :=\n  category_theory.concrete_category.mk\n    (category_theory.functor.mk (fun (R_1 : Module R) => \u21a5R_1) fun (R_1 S : Module R) (f : R_1 \u27f6 S) => \u21d1f)\n\nprotected instance has_forget_to_AddCommGroup (R : Type u) [ring R] : category_theory.has_forget\u2082 (Module R) AddCommGroup :=\n  category_theory.has_forget\u2082.mk\n    (category_theory.functor.mk (fun (M : Module R) => AddCommGroup.of \u21a5M)\n      fun (M\u2081 M\u2082 : Module R) (f : M\u2081 \u27f6 M\u2082) => linear_map.to_add_monoid_hom f)\n\n/-- The object in the category of R-modules associated to an R-module -/\ndef of (R : Type u) [ring R] (X : Type v) [add_comm_group X] [module R X] : Module R :=\n  mk X\n\nprotected instance inhabited (R : Type u) [ring R] : Inhabited (Module R) :=\n  { default := of R PUnit }\n\n@[simp] theorem coe_of (R : Type u) [ring R] (X : Type u) [add_comm_group X] [module R X] : \u21a5(of R X) = X :=\n  rfl\n\n/-- Forgetting to the underlying type and then building the bundled object returns the original\nmodule. -/\n@[simp] theorem of_self_iso_inv {R : Type u} [ring R] (M : Module R) : category_theory.iso.inv (of_self_iso M) = \ud835\udfd9 :=\n  Eq.refl (category_theory.iso.inv (of_self_iso M))\n\nprotected instance of.subsingleton {R : Type u} [ring R] : subsingleton \u21a5(of R PUnit) :=\n  eq.mpr (id (Eq._oldrec (Eq.refl (subsingleton \u21a5(of R PUnit))) (coe_of R PUnit))) punit.subsingleton\n\nprotected instance category_theory.limits.has_zero_object {R : Type u} [ring R] : category_theory.limits.has_zero_object (Module R) :=\n  category_theory.limits.has_zero_object.mk (of R PUnit) (fun (X : Module R) => unique.mk { default := 0 } sorry)\n    fun (X : Module R) => unique.mk { default := 0 } sorry\n\n@[simp] theorem id_apply {R : Type u} [ring R] {M : Module R} (m : \u21a5M) : coe_fn \ud835\udfd9 m = m :=\n  rfl\n\n@[simp] theorem coe_comp {R : Type u} [ring R] {M : Module R} {N : Module R} {U : Module R} (f : M \u27f6 N) (g : N \u27f6 U) : \u21d1(f \u226b g) = \u21d1g \u2218 \u21d1f :=\n  rfl\n\nend Module\n\n\n/-- Reinterpreting a linear map in the category of `R`-modules. -/\ndef Module.as_hom {R : Type u} [ring R] {X\u2081 : Type v} {X\u2082 : Type v} [add_comm_group X\u2081] [module R X\u2081] [add_comm_group X\u2082] [module R X\u2082] : linear_map R X\u2081 X\u2082 \u2192 (Module.of R X\u2081 \u27f6 Module.of R X\u2082) :=\n  id\n\n/-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/\n@[simp] theorem linear_equiv.to_Module_iso_inv {R : Type u} [ring R] {X\u2081 : Type v} {X\u2082 : Type v} {g\u2081 : add_comm_group X\u2081} {g\u2082 : add_comm_group X\u2082} {m\u2081 : module R X\u2081} {m\u2082 : module R X\u2082} (e : linear_equiv R X\u2081 X\u2082) : category_theory.iso.inv (linear_equiv.to_Module_iso e) = \u2191(linear_equiv.symm e) :=\n  Eq.refl (category_theory.iso.inv (linear_equiv.to_Module_iso e))\n\n/--\nBuild an isomorphism in the category `Module R` from a `linear_equiv` between `module`s.\n\nThis version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see\n`Module.of R M` is defeq to `M` when `M : Module R`. -/\ndef linear_equiv.to_Module_iso' {R : Type u} [ring R] {M : Module R} {N : Module R} (i : linear_equiv R \u21a5M \u21a5N) : M \u2245 N :=\n  category_theory.iso.mk \u2191i \u2191(linear_equiv.symm i)\n\nnamespace category_theory.iso\n\n\n/-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/\n@[simp] theorem to_linear_equiv_apply {R : Type u} [ring R] {X : Module R} {Y : Module R} (i : X \u2245 Y) : \u2200 (\u1fb0 : \u21a5X), coe_fn (to_linear_equiv i) \u1fb0 = coe_fn (hom i) \u1fb0 :=\n  fun (\u1fb0 : \u21a5X) => Eq.refl (coe_fn (to_linear_equiv i) \u1fb0)\n\nend category_theory.iso\n\n\n/-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms\nin `Module` -/\n@[simp] theorem linear_equiv_iso_Module_iso_hom {R : Type u} [ring R] {X : Type u} {Y : Type u} [add_comm_group X] [add_comm_group Y] [module R X] [module R Y] (e : linear_equiv R X Y) : category_theory.iso.hom linear_equiv_iso_Module_iso e = linear_equiv.to_Module_iso e :=\n  Eq.refl (category_theory.iso.hom linear_equiv_iso_Module_iso e)\n\nnamespace Module\n\n\nprotected instance category_theory.preadditive {R : Type u} [ring R] : category_theory.preadditive (Module R) :=\n  category_theory.preadditive.mk\n\ntheorem ker_eq_bot_of_mono {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M \u27f6 N) [category_theory.mono f] : linear_map.ker f = \u22a5 :=\n  linear_map.ker_eq_bot_of_cancel\n    fun (u v : linear_map R \u21a5(linear_map.ker f) \u21a5M) => iff.mp (category_theory.cancel_mono f)\n\ntheorem range_eq_top_of_epi {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M \u27f6 N) [category_theory.epi f] : linear_map.range f = \u22a4 :=\n  linear_map.range_eq_top_of_cancel\n    fun (u v : linear_map R (\u21a5N) (submodule.quotient (linear_map.range f))) => iff.mp (category_theory.cancel_epi f)\n\ntheorem mono_of_ker_eq_bot {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M \u27f6 N) (hf : linear_map.ker f = \u22a5) : category_theory.mono f :=\n  category_theory.concrete_category.mono_of_injective f (iff.mp linear_map.ker_eq_bot hf)\n\ntheorem epi_of_range_eq_top {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M \u27f6 N) (hf : linear_map.range f = \u22a4) : category_theory.epi f :=\n  category_theory.concrete_category.epi_of_surjective f (iff.mp linear_map.range_eq_top hf)\n\nend Module\n\n\nprotected instance Module.has_coe {R : Type u} [ring R] (M : Type u) [add_comm_group M] [module R M] : has_coe (submodule R M) (Module R) :=\n  has_coe.mk fun (N : submodule R M) => Module.of R \u21a5N\n\n", "meta": {"author": "AurelienSaue", "repo": "Mathlib4_auto", "sha": "590df64109b08190abe22358fabc3eae000943f2", "save_path": "github-repos/lean/AurelienSaue-Mathlib4_auto", "path": "github-repos/lean/AurelienSaue-Mathlib4_auto/Mathlib4_auto-590df64109b08190abe22358fabc3eae000943f2/Mathlib/algebra/category/Module/basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4960938294709195, "lm_q2_score": 0.10374863656545572, "lm_q1q2_score": 0.05146905841614359}}
{"text": "/-\nCopyright (c) 2022 Devon Tuma. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Devon Tuma\n-/\nimport computational_monads.simulation_semantics.constructions.stateless_oracle\n\n/-!\n# Identity Oracle\n\nDefines a `sim_oracle` that just acts as an identity,\n  e.g. simulation with this oracle has no effect (besides an empty state)\n\nMain use cases is in simulating a pair of oracles, where only one of the oracles is reduced.\n  For example preserving a `uniform_selecting` oracle while reducing a `signing_oracle`\n-/\n\nopen oracle_comp oracle_spec\n\nvariables {\u03b1 \u03b2 : Type} {spec spec' spec'' : oracle_spec}\n  (oa : oracle_comp spec \u03b1) (ob : \u03b1 \u2192 oracle_comp spec \u03b2) (s : unit)\n\n@[inline, reducible]\ndef identity_oracle (spec : oracle_spec) : sim_oracle spec spec unit := \u27eaquery\u27eb\n\nnotation `id\u209b` := identity_oracle _\n\nnamespace identity_oracle\n\n@[simp] lemma apply (i : spec.\u03b9) (t : spec.domain i) (s : unit) :\n  (id\u209b) i (t, s) = query i t >>= \u03bb u, return (u, ()) := rfl\n\nsection support\n\n@[simp] lemma support_apply (i : spec.\u03b9) (t : spec.domain i) :\n  ((id\u209b) i (t, s)).support = {u | u.1 \u2208 (query i t).support} :=\nbegin\n  simp only [apply, support_bind, support_return, set.Union_true,\n    set.Union_singleton_eq_range, support_query, set.top_eq_univ, set.mem_univ, set.set_of_true],\n  exact set.eq_univ_of_forall (\u03bb x, set.mem_range.2\n    \u27e8x.1, prod.eq_iff_fst_eq_snd_eq.2 \u27e8rfl, punit_eq () x.snd\u27e9\u27e9),\nend\n\n@[simp] lemma support_simulate' : (simulate' id\u209b oa s).support = oa.support :=\nstateless_oracle.support_simulate'_eq_support _ query s (\u03bb _ _, rfl)\n\n@[simp] theorem support_simulate : (simulate id\u209b oa s).support = prod.fst \u207b\u00b9' oa.support :=\n(stateless_oracle.support_simulate_eq_preimage_support_simulate' _ _ _).trans\n  (congr_arg _ $ support_simulate' oa ())\n\n@[simp] lemma mem_support_simulate_iff (x : \u03b1 \u00d7 unit) :\n  x \u2208 (simulate id\u209b oa s).support \u2194 x.1 \u2208 oa.support :=\nby rw [support_simulate, set.mem_preimage]\n\nend support\n\nsection distribution_semantics\n\nsection eval_dist\n\n@[simp]\nlemma eval_dist_apply (i : spec.\u03b9) (t : spec.domain i) :\n  \u2045id\u209b i (t, s)\u2046 = \u2045(\u03bb u, (u, ())) <$> query i t\u2046 :=\nrfl\n\n-- @[simp]\n-- theorem eval_dist_simulate : \u2045simulate id\u209b oa s\u2046 = \u2045(\u03bb a, (a, ())) <$> oa\u2046 :=\n-- begin\n--   induction oa with \u03b1 a \u03b1 \u03b2 oa ob hoa hob i t generalizing s,\n--   { simp [punit_eq s (), pmf.pure_map] },\n--   { exact trans (simulate_bind_equiv id\u209b oa ob s) (trans (eval_dist_bind_eq_of_eval_dist_eq\n--       (hoa s) (\u03bb x, hob x.fst x.snd)) (by simp)) },\n--   { exact (simulate_query_equiv id\u209b i t ()).trans (apply_equiv () i t) }\n-- end\n\n-- lemma eval_dist_default_simulate : default_simulate id\u209b oa \u2243\u209a (\u03bb a, (a, ())) <$> oa :=\n-- simulate_equiv oa ()\n\n-- @[simp]\n-- lemma simulate'_equiv : (simulate' id\u209b oa s) \u2243\u209a oa :=\n-- calc simulate' id\u209b oa s \u2243\u209a prod.fst <$> simulate id\u209b oa s : rfl\n--   ... \u2243\u209a prod.fst <$> (\u03bb a, (a, ())) <$> oa : (map_equiv_of_equiv _ (simulate_equiv oa s))\n--   ... \u2243\u209a (prod.fst \u2218 \u03bb a, (a, ())) <$> oa : map_map_equiv oa _ _\n--   ... \u2243\u209a oa : map_id_equiv oa\n\n-- lemma default_simulate'_equiv : default_simulate' id\u209b oa \u2243\u209a oa :=\n-- simulate'_equiv oa ()\n\nend eval_dist\n\nsection prob_event\n\n\n\nend prob_event\n\nend distribution_semantics\n\nend identity_oracle", "meta": {"author": "dtumad", "repo": "lean-crypto-formalization", "sha": "f975a9a9882120b509553a7ced9aa05b745ff154", "save_path": "github-repos/lean/dtumad-lean-crypto-formalization", "path": "github-repos/lean/dtumad-lean-crypto-formalization/lean-crypto-formalization-f975a9a9882120b509553a7ced9aa05b745ff154/src/computational_monads/simulation_semantics/constructions/identity_oracle.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.4921881357207955, "lm_q2_score": 0.10374863517987337, "lm_q1q2_score": 0.051063847332758816}}
{"text": "import tactic\n\n------------------------------------------------\n-- Tag allowable graph theory theorems --\n------------------------------------------------\n@[user_attribute]\nmeta def dummy : user_attribute :=\n{ name := `dummy,\n  descr := \"A tag for all silly theorems for debugging purposes.\" }\n\n------------------------------------------------\n-- The allowable theorems --\n------------------------------------------------\n-- A sample there-exists statement for debugging\n@[dummy] \ntheorem exists_one: \n  \u2203 n : \u2115, n=1 :=\nbegin\n  use 1\nend\n\n-- A sample for-all, there-exists statement for debugging\n@[dummy] \ntheorem forall_exists_greater: \n  \u2200 n : \u2115, \u2203 k : \u2115, k = n+1 :=\nbegin\n  intros n, use n+1,\nend", "meta": {"author": "Human-Oriented-ATP", "repo": "lean-tactics", "sha": "8fa4c8b8efc0c6a1d408b48e999f3a36f228bd0f", "save_path": "github-repos/lean/Human-Oriented-ATP-lean-tactics", "path": "github-repos/lean/Human-Oriented-ATP-lean-tactics/lean-tactics-8fa4c8b8efc0c6a1d408b48e999f3a36f228bd0f/lean3/src/testbed/dummy.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.46490157137338844, "lm_q2_score": 0.10970576369015278, "lm_q1q2_score": 0.05100238192826965}}
{"text": "/-\nCopyright (c) 2016 Jeremy Avigad. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Jeremy Avigad, Leonardo de Moura\n-/\nimport tactic.doc_commands\nimport tactic.reserved_notation\n\n/-!\n# Basic logic properties\n\nThis file is one of the earliest imports in mathlib.\n\n## Implementation notes\n\nTheorems that require decidability hypotheses are in the namespace \"decidable\".\nClassical versions are in the namespace \"classical\".\n\nIn the presence of automation, this whole file may be unnecessary. On the other hand,\nmaybe it is useful for writing automation.\n-/\n\nopen function\nlocal attribute [instance, priority 10] classical.prop_decidable\n\nsection miscellany\n\n/- We add the `inline` attribute to optimize VM computation using these declarations. For example,\n  `if p \u2227 q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/\nattribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable\n  decidable.true implies.decidable not.decidable ne.decidable\n  bool.decidable_eq decidable.to_bool\n\nattribute [simp] cast_eq cast_heq\n\nvariables {\u03b1 : Type*} {\u03b2 : Type*}\n\n/-- An identity function with its main argument implicit. This will be printed as `hidden` even\nif it is applied to a large term, so it can be used for elision,\nas done in the `elide` and `unelide` tactics. -/\n@[reducible] def hidden {\u03b1 : Sort*} {a : \u03b1} := a\n\n/-- Ex falso, the nondependent eliminator for the `empty` type. -/\ndef empty.elim {C : Sort*} : empty \u2192 C.\n\ninstance : subsingleton empty := \u27e8\u03bba, a.elim\u27e9\n\ninstance subsingleton.prod {\u03b1 \u03b2 : Type*} [subsingleton \u03b1] [subsingleton \u03b2] : subsingleton (\u03b1 \u00d7 \u03b2) :=\n\u27e8by { intros a b, cases a, cases b, congr, }\u27e9\n\ninstance : decidable_eq empty := \u03bba, a.elim\n\ninstance sort.inhabited : inhabited (Sort*) := \u27e8punit\u27e9\ninstance sort.inhabited' : inhabited (default) := \u27e8punit.star\u27e9\n\ninstance psum.inhabited_left {\u03b1 \u03b2} [inhabited \u03b1] : inhabited (psum \u03b1 \u03b2) := \u27e8psum.inl default\u27e9\ninstance psum.inhabited_right {\u03b1 \u03b2} [inhabited \u03b2] : inhabited (psum \u03b1 \u03b2) := \u27e8psum.inr default\u27e9\n\n@[priority 10] instance decidable_eq_of_subsingleton\n  {\u03b1} [subsingleton \u03b1] : decidable_eq \u03b1\n| a b := is_true (subsingleton.elim a b)\n\n@[simp] lemma eq_iff_true_of_subsingleton {\u03b1 : Sort*} [subsingleton \u03b1] (x y : \u03b1) :\n  x = y \u2194 true :=\nby cc\n\n/-- If all points are equal to a given point `x`, then `\u03b1` is a subsingleton. -/\nlemma subsingleton_of_forall_eq {\u03b1 : Sort*} (x : \u03b1) (h : \u2200 y, y = x) : subsingleton \u03b1 :=\n\u27e8\u03bb a b, (h a).symm \u25b8 (h b).symm \u25b8 rfl\u27e9\n\nlemma subsingleton_iff_forall_eq {\u03b1 : Sort*} (x : \u03b1) : subsingleton \u03b1 \u2194 \u2200 y, y = x :=\n\u27e8\u03bb h y, @subsingleton.elim _ h y x, subsingleton_of_forall_eq x\u27e9\n\n-- TODO[gh-6025]: make this an instance once safe to do so\nlemma subtype.subsingleton (\u03b1 : Sort*) [subsingleton \u03b1] (p : \u03b1 \u2192 Prop) : subsingleton (subtype p) :=\n\u27e8\u03bb \u27e8x,_\u27e9 \u27e8y,_\u27e9, have x = y, from subsingleton.elim _ _, by { cases this, refl }\u27e9\n\n/-- Add an instance to \"undo\" coercion transitivity into a chain of coercions, because\n   most simp lemmas are stated with respect to simple coercions and will not match when\n   part of a chain. -/\n@[simp] theorem coe_coe {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_t \u03b2 \u03b3]\n  (a : \u03b1) : (a : \u03b3) = (a : \u03b2) := rfl\n\ntheorem coe_fn_coe_trans\n  {\u03b1 \u03b2 \u03b3 \u03b4} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_fun \u03b3 \u03b4]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\n/-- Non-dependent version of `coe_fn_coe_trans`, helps `rw` figure out the argument. -/\ntheorem coe_fn_coe_trans'\n  {\u03b1 \u03b2 \u03b3} {\u03b4 : out_param $ _} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_fun \u03b3 (\u03bb _, \u03b4)]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\n@[simp] theorem coe_fn_coe_base\n  {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_to_fun \u03b2 \u03b3]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\n/-- Non-dependent version of `coe_fn_coe_base`, helps `rw` figure out the argument. -/\ntheorem coe_fn_coe_base'\n  {\u03b1 \u03b2} {\u03b3 : out_param $ _} [has_coe \u03b1 \u03b2] [has_coe_to_fun \u03b2 (\u03bb _, \u03b3)]\n  (x : \u03b1) : @coe_fn \u03b1 _ _ x = @coe_fn \u03b2 _ _ x := rfl\n\ntheorem coe_sort_coe_trans\n  {\u03b1 \u03b2 \u03b3 \u03b4} [has_coe \u03b1 \u03b2] [has_coe_t_aux \u03b2 \u03b3] [has_coe_to_sort \u03b3 \u03b4]\n  (x : \u03b1) : @coe_sort \u03b1 _ _ x = @coe_sort \u03b2 _ _ x := rfl\n\n/--\nMany structures such as bundled morphisms coerce to functions so that you can\ntransparently apply them to arguments. For example, if `e : \u03b1 \u2243 \u03b2` and `a : \u03b1`\nthen you can write `e a` and this is elaborated as `\u21d1e a`. This type of\ncoercion is implemented using the `has_coe_to_fun` type class. There is one\nimportant consideration:\n\nIf a type coerces to another type which in turn coerces to a function,\nthen it **must** implement `has_coe_to_fun` directly:\n```lean\nstructure sparkling_equiv (\u03b1 \u03b2) extends \u03b1 \u2243 \u03b2\n\n-- if we add a `has_coe` instance,\ninstance {\u03b1 \u03b2} : has_coe (sparkling_equiv \u03b1 \u03b2) (\u03b1 \u2243 \u03b2) :=\n\u27e8sparkling_equiv.to_equiv\u27e9\n\n-- then a `has_coe_to_fun` instance **must** be added as well:\ninstance {\u03b1 \u03b2} : has_coe_to_fun (sparkling_equiv \u03b1 \u03b2) :=\n\u27e8\u03bb _, \u03b1 \u2192 \u03b2, \u03bb f, f.to_equiv.to_fun\u27e9\n```\n\n(Rationale: if we do not declare the direct coercion, then `\u21d1e a` is not in\nsimp-normal form. The lemma `coe_fn_coe_base` will unfold it to `\u21d1\u2191e a`. This\noften causes loops in the simplifier.)\n-/\nlibrary_note \"function coercion\"\n\n@[simp] theorem coe_sort_coe_base\n  {\u03b1 \u03b2 \u03b3} [has_coe \u03b1 \u03b2] [has_coe_to_sort \u03b2 \u03b3]\n  (x : \u03b1) : @coe_sort \u03b1 _ _ x = @coe_sort \u03b2 _ _ x := rfl\n\n/-- `pempty` is the universe-polymorphic analogue of `empty`. -/\n@[derive decidable_eq]\ninductive {u} pempty : Sort u\n\n/-- Ex falso, the nondependent eliminator for the `pempty` type. -/\ndef pempty.elim {C : Sort*} : pempty \u2192 C.\n\ninstance subsingleton_pempty : subsingleton pempty := \u27e8\u03bba, a.elim\u27e9\n\n@[simp] lemma not_nonempty_pempty : \u00ac nonempty pempty :=\nassume \u27e8h\u27e9, h.elim\n\n@[simp] theorem forall_pempty {P : pempty \u2192 Prop} : (\u2200 x : pempty, P x) \u2194 true :=\n\u27e8\u03bb h, trivial, \u03bb h x, by cases x\u27e9\n\n@[simp] theorem exists_pempty {P : pempty \u2192 Prop} : (\u2203 x : pempty, P x) \u2194 false :=\n\u27e8\u03bb h, by { cases h with w, cases w }, false.elim\u27e9\n\nlemma congr_arg_heq {\u03b1} {\u03b2 : \u03b1 \u2192 Sort*} (f : \u2200 a, \u03b2 a) : \u2200 {a\u2081 a\u2082 : \u03b1}, a\u2081 = a\u2082 \u2192 f a\u2081 == f a\u2082\n| a _ rfl := heq.rfl\n\nlemma plift.down_inj {\u03b1 : Sort*} : \u2200 (a b : plift \u03b1), a.down = b.down \u2192 a = b\n| \u27e8a\u27e9 \u27e8b\u27e9 rfl := rfl\n\n-- missing [symm] attribute for ne in core.\nattribute [symm] ne.symm\n\nlemma ne_comm {\u03b1} {a b : \u03b1} : a \u2260 b \u2194 b \u2260 a := \u27e8ne.symm, ne.symm\u27e9\n\n@[simp] lemma eq_iff_eq_cancel_left {b c : \u03b1} :\n  (\u2200 {a}, a = b \u2194 a = c) \u2194 (b = c) :=\n\u27e8\u03bb h, by rw [\u2190 h], \u03bb h a, by rw h\u27e9\n\n@[simp] lemma eq_iff_eq_cancel_right {a b : \u03b1} :\n  (\u2200 {c}, a = c \u2194 b = c) \u2194 (a = b) :=\n\u27e8\u03bb h, by rw h, \u03bb h a, by rw h\u27e9\n\n/-- Wrapper for adding elementary propositions to the type class systems.\nWarning: this can easily be abused. See the rest of this docstring for details.\n\nCertain propositions should not be treated as a class globally,\nbut sometimes it is very convenient to be able to use the type class system\nin specific circumstances.\n\nFor example, `zmod p` is a field if and only if `p` is a prime number.\nIn order to be able to find this field instance automatically by type class search,\nwe have to turn `p.prime` into an instance implicit assumption.\n\nOn the other hand, making `nat.prime` a class would require a major refactoring of the library,\nand it is questionable whether making `nat.prime` a class is desirable at all.\nThe compromise is to add the assumption `[fact p.prime]` to `zmod.field`.\n\nIn particular, this class is not intended for turning the type class system\ninto an automated theorem prover for first order logic. -/\nclass fact (p : Prop) : Prop := (out [] : p)\n\n/--\nIn most cases, we should not have global instances of `fact`; typeclass search only reads the head\nsymbol and then tries any instances, which means that adding any such instance will cause slowdowns\neverywhere. We instead make them as lemmata and make them local instances as required.\n-/\nlibrary_note \"fact non-instances\"\n\nlemma fact.elim {p : Prop} (h : fact p) : p := h.1\nlemma fact_iff {p : Prop} : fact p \u2194 p := \u27e8\u03bb h, h.1, \u03bb h, \u27e8h\u27e9\u27e9\n\nend miscellany\n\n/-!\n### Declarations about propositional connectives\n-/\n\ntheorem false_ne_true : false \u2260 true\n| h := h.symm \u25b8 trivial\n\nsection propositional\nvariables {a b c d e f : Prop}\n\n/-! ### Declarations about `implies` -/\n\ninstance : is_refl Prop iff := \u27e8iff.refl\u27e9\ninstance : is_trans Prop iff := \u27e8\u03bb _ _ _, iff.trans\u27e9\n\ntheorem iff_of_eq (e : a = b) : a \u2194 b := e \u25b8 iff.rfl\n\ntheorem iff_iff_eq : (a \u2194 b) \u2194 a = b := \u27e8propext, iff_of_eq\u27e9\n\n@[simp] lemma eq_iff_iff {p q : Prop} : (p = q) \u2194 (p \u2194 q) := iff_iff_eq.symm\n\n@[simp] theorem imp_self : (a \u2192 a) \u2194 true := iff_true_intro id\n\nlemma iff.imp (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : (a \u2192 c) \u2194 (b \u2192 d) := imp_congr h\u2081 h\u2082\n\n@[simp] lemma eq_true_eq_id : eq true = id :=\nby { funext, simp only [true_iff, id.def, iff_self, eq_iff_iff], }\n\ntheorem imp_intro {\u03b1 \u03b2 : Prop} (h : \u03b1) : \u03b2 \u2192 \u03b1 := \u03bb _, h\n\ntheorem imp_false : (a \u2192 false) \u2194 \u00ac a := iff.rfl\n\ntheorem imp_and_distrib {\u03b1} : (\u03b1 \u2192 b \u2227 c) \u2194 (\u03b1 \u2192 b) \u2227 (\u03b1 \u2192 c) :=\n\u27e8\u03bb h, \u27e8\u03bb ha, (h ha).left, \u03bb ha, (h ha).right\u27e9,\n \u03bb h ha, \u27e8h.left ha, h.right ha\u27e9\u27e9\n\n@[simp] theorem and_imp : (a \u2227 b \u2192 c) \u2194 (a \u2192 b \u2192 c) :=\niff.intro (\u03bb h ha hb, h \u27e8ha, hb\u27e9) (\u03bb h \u27e8ha, hb\u27e9, h ha hb)\n\ntheorem iff_def : (a \u2194 b) \u2194 (a \u2192 b) \u2227 (b \u2192 a) :=\niff_iff_implies_and_implies _ _\n\ntheorem iff_def' : (a \u2194 b) \u2194 (b \u2192 a) \u2227 (a \u2192 b) :=\niff_def.trans and.comm\n\ntheorem imp_true_iff {\u03b1 : Sort*} : (\u03b1 \u2192 true) \u2194 true :=\niff_true_intro $ \u03bb_, trivial\n\ntheorem imp_iff_right (ha : a) : (a \u2192 b) \u2194 b :=\n\u27e8\u03bbf, f ha, imp_intro\u27e9\n\nlemma imp_iff_not (hb : \u00ac b) : a \u2192 b \u2194 \u00ac a := imp_congr_right $ \u03bb _, iff_false_intro hb\n\ntheorem decidable.imp_iff_right_iff [decidable a] : ((a \u2192 b) \u2194 b) \u2194 (a \u2228 b) :=\n\u27e8\u03bb H, (decidable.em a).imp_right $ \u03bb ha', H.1 $ \u03bb ha, (ha' ha).elim,\n  \u03bb H, H.elim imp_iff_right $ \u03bb hb, \u27e8\u03bb hab, hb, \u03bb _ _, hb\u27e9\u27e9\n\n@[simp] theorem imp_iff_right_iff : ((a \u2192 b) \u2194 b) \u2194 (a \u2228 b) :=\ndecidable.imp_iff_right_iff\n\nlemma decidable.and_or_imp [decidable a] : (a \u2227 b) \u2228 (a \u2192 c) \u2194 a \u2192 (b \u2228 c) :=\nif ha : a then by simp only [ha, true_and, true_implies_iff]\n          else by simp only [ha, false_or, false_and, false_implies_iff]\n\n@[simp] theorem and_or_imp : (a \u2227 b) \u2228 (a \u2192 c) \u2194 a \u2192 (b \u2228 c) :=\ndecidable.and_or_imp\n\n/-! ### Declarations about `not` -/\n\n/-- Ex falso for negation. From `\u00ac a` and `a` anything follows. This is the same as `absurd` with\nthe arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/\ndef not.elim {\u03b1 : Sort*} (H1 : \u00aca) (H2 : a) : \u03b1 := absurd H2 H1\n\n@[reducible] theorem not.imp {a b : Prop} (H2 : \u00acb) (H1 : a \u2192 b) : \u00aca := mt H1 H2\n\nlemma iff.not (h : a \u2194 b) : \u00ac a \u2194 \u00ac b := not_congr h\n\ntheorem not_not_of_not_imp : \u00ac(a \u2192 b) \u2192 \u00ac\u00aca :=\nmt not.elim\n\ntheorem not_of_not_imp {a : Prop} : \u00ac(a \u2192 b) \u2192 \u00acb :=\nmt imp_intro\n\ntheorem dec_em (p : Prop) [decidable p] : p \u2228 \u00acp := decidable.em p\n\ntheorem dec_em' (p : Prop) [decidable p] : \u00acp \u2228 p := (dec_em p).swap\n\ntheorem em (p : Prop) : p \u2228 \u00acp := classical.em _\n\ntheorem em' (p : Prop) : \u00acp \u2228 p := (em p).swap\n\ntheorem or_not {p : Prop} : p \u2228 \u00acp := em _\n\nsection eq_or_ne\n\nvariables {\u03b1 : Sort*} (x y : \u03b1)\n\ntheorem decidable.eq_or_ne [decidable (x = y)] : x = y \u2228 x \u2260 y := dec_em $ x = y\n\ntheorem decidable.ne_or_eq [decidable (x = y)] : x \u2260 y \u2228 x = y := dec_em' $ x = y\n\ntheorem eq_or_ne : x = y \u2228 x \u2260 y := em $ x = y\n\ntheorem ne_or_eq : x \u2260 y \u2228 x = y := em' $ x = y\n\nend eq_or_ne\n\ntheorem by_contradiction {p} : (\u00acp \u2192 false) \u2192 p := decidable.by_contradiction\n\n-- alias by_contradiction \u2190 by_contra\ntheorem by_contra {p} : (\u00acp \u2192 false) \u2192 p := decidable.by_contradiction\n\n/--\nIn most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely.\nThe `decidable` namespace contains versions of lemmas from the root namespace that explicitly\nattempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs.\n\nYou can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if\n`classical.choice` appears in the list.\n-/\nlibrary_note \"decidable namespace\"\n\n/--\nAs mathlib is primarily classical,\nif the type signature of a `def` or `lemma` does not require any `decidable` instances to state,\nit is preferable not to introduce any `decidable` instances that are needed in the proof\nas arguments, but rather to use the `classical` tactic as needed.\n\nIn the other direction, when `decidable` instances do appear in the type signature,\nit is better to use explicitly introduced ones rather than allowing Lean to automatically infer\nclassical ones, as these may cause instance mismatch errors later.\n-/\nlibrary_note \"decidable arguments\"\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_not [decidable a] : \u00ac\u00aca \u2194 a :=\niff.intro decidable.by_contradiction not_not_intro\n\n/-- The Double Negation Theorem: `\u00ac \u00ac P` is equivalent to `P`.\nThe left-to-right direction, double negation elimination (DNE),\nis classically true but not constructively. -/\n@[simp] theorem not_not : \u00ac\u00aca \u2194 a := decidable.not_not\n\ntheorem of_not_not : \u00ac\u00aca \u2192 a := by_contra\n\nlemma not_ne_iff {\u03b1 : Sort*} {a b : \u03b1} : \u00ac a \u2260 b \u2194 a = b := not_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.of_not_imp [decidable a] (h : \u00ac (a \u2192 b)) : a :=\ndecidable.by_contradiction (not_not_of_not_imp h)\n\ntheorem of_not_imp : \u00ac (a \u2192 b) \u2192 a := decidable.of_not_imp\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_symm [decidable a] (h : \u00aca \u2192 b) (hb : \u00acb) : a :=\ndecidable.by_contradiction $ hb \u2218 h\n\ntheorem not.decidable_imp_symm [decidable a] : (\u00aca \u2192 b) \u2192 \u00acb \u2192 a := decidable.not_imp_symm\n\ntheorem not.imp_symm : (\u00aca \u2192 b) \u2192 \u00acb \u2192 a := not.decidable_imp_symm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_comm [decidable a] [decidable b] : (\u00aca \u2192 b) \u2194 (\u00acb \u2192 a) :=\n\u27e8not.decidable_imp_symm, not.decidable_imp_symm\u27e9\n\ntheorem not_imp_comm : (\u00aca \u2192 b) \u2194 (\u00acb \u2192 a) := decidable.not_imp_comm\n\n@[simp] theorem imp_not_self : (a \u2192 \u00aca) \u2194 \u00aca := \u27e8\u03bb h ha, h ha ha, \u03bb h _, h\u27e9\n\ntheorem decidable.not_imp_self [decidable a] : (\u00aca \u2192 a) \u2194 a :=\nby { have := @imp_not_self (\u00aca), rwa decidable.not_not at this }\n\n@[simp] theorem not_imp_self : (\u00aca \u2192 a) \u2194 a := decidable.not_imp_self\n\ntheorem imp.swap : (a \u2192 b \u2192 c) \u2194 (b \u2192 a \u2192 c) :=\n\u27e8swap, swap\u27e9\n\ntheorem imp_not_comm : (a \u2192 \u00acb) \u2194 (b \u2192 \u00aca) :=\nimp.swap\n\n/-! ### Declarations about `xor` -/\n\n@[simp] theorem xor_true : xor true = not := funext $ \u03bb a, by simp [xor]\n\n@[simp] theorem xor_false : xor false = id := funext $ \u03bb a, by simp [xor]\n\ntheorem xor_comm (a b) : xor a b = xor b a := by simp [xor, and_comm, or_comm]\n\ninstance : is_commutative Prop xor := \u27e8xor_comm\u27e9\n\n@[simp] theorem xor_self (a : Prop) : xor a a = false := by simp [xor]\n\n/-! ### Declarations about `and` -/\n\nlemma iff.and (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : a \u2227 c \u2194 b \u2227 d := and_congr h\u2081 h\u2082\n\ntheorem and_congr_left (h : c \u2192 (a \u2194 b)) : a \u2227 c \u2194 b \u2227 c :=\nand.comm.trans $ (and_congr_right h).trans and.comm\n\ntheorem and_congr_left' (h : a \u2194 b) : a \u2227 c \u2194 b \u2227 c := h.and iff.rfl\n\ntheorem and_congr_right' (h : b \u2194 c) : a \u2227 b \u2194 a \u2227 c := iff.rfl.and h\n\ntheorem not_and_of_not_left (b : Prop) : \u00aca \u2192 \u00ac(a \u2227 b) :=\nmt and.left\n\ntheorem not_and_of_not_right (a : Prop) {b : Prop} : \u00acb \u2192 \u00ac(a \u2227 b) :=\nmt and.right\n\ntheorem and.imp_left (h : a \u2192 b) : a \u2227 c \u2192 b \u2227 c :=\nand.imp h id\n\ntheorem and.imp_right (h : a \u2192 b) : c \u2227 a \u2192 c \u2227 b :=\nand.imp id h\n\nlemma and.right_comm : (a \u2227 b) \u2227 c \u2194 (a \u2227 c) \u2227 b :=\nby simp only [and.left_comm, and.comm]\n\nlemma and_and_and_comm (a b c d : Prop) : (a \u2227 b) \u2227 c \u2227 d \u2194 (a \u2227 c) \u2227 b \u2227 d :=\nby rw [\u2190and_assoc, @and.right_comm a, and_assoc]\n\nlemma and.rotate : a \u2227 b \u2227 c \u2194 b \u2227 c \u2227 a :=\nby simp only [and.left_comm, and.comm]\n\ntheorem and_not_self_iff (a : Prop) : a \u2227 \u00ac a \u2194 false :=\niff.intro (assume h, (h.right) (h.left)) (assume h, h.elim)\n\ntheorem not_and_self_iff (a : Prop) : \u00ac a \u2227 a \u2194 false :=\niff.intro (assume \u27e8hna, ha\u27e9, hna ha) false.elim\n\ntheorem and_iff_left_of_imp {a b : Prop} (h : a \u2192 b) : (a \u2227 b) \u2194 a :=\niff.intro and.left (\u03bb ha, \u27e8ha, h ha\u27e9)\n\ntheorem and_iff_right_of_imp {a b : Prop} (h : b \u2192 a) : (a \u2227 b) \u2194 b :=\niff.intro and.right (\u03bb hb, \u27e8h hb, hb\u27e9)\n\n@[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a \u2227 b) \u2194 a) \u2194 (a \u2192 b) :=\n\u27e8\u03bb h ha, (h.2 ha).2, and_iff_left_of_imp\u27e9\n\n@[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a \u2227 b) \u2194 b) \u2194 (b \u2192 a) :=\n\u27e8\u03bb h ha, (h.2 ha).1, and_iff_right_of_imp\u27e9\n\n@[simp] lemma iff_self_and {p q : Prop} : (p \u2194 p \u2227 q) \u2194 (p \u2192 q) :=\nby rw [@iff.comm p, and_iff_left_iff_imp]\n\n@[simp] lemma iff_and_self {p q : Prop} : (p \u2194 q \u2227 p) \u2194 (p \u2192 q) :=\nby rw [and_comm, iff_self_and]\n\n@[simp] lemma and.congr_right_iff : (a \u2227 b \u2194 a \u2227 c) \u2194 (a \u2192 (b \u2194 c)) :=\n\u27e8\u03bb h ha, by simp [ha] at h; exact h, and_congr_right\u27e9\n\n@[simp] lemma and.congr_left_iff : (a \u2227 c \u2194 b \u2227 c) \u2194 c \u2192 (a \u2194 b) :=\nby simp only [and.comm, \u2190 and.congr_right_iff]\n\n@[simp] lemma and_self_left : a \u2227 a \u2227 b \u2194 a \u2227 b :=\n\u27e8\u03bb h, \u27e8h.1, h.2.2\u27e9, \u03bb h, \u27e8h.1, h.1, h.2\u27e9\u27e9\n\n@[simp] lemma and_self_right : (a \u2227 b) \u2227 b \u2194 a \u2227 b :=\n\u27e8\u03bb h, \u27e8h.1.1, h.2\u27e9, \u03bb h, \u27e8\u27e8h.1, h.2\u27e9, h.2\u27e9\u27e9\n\n/-! ### Declarations about `or` -/\n\nlemma iff.or (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : a \u2228 c \u2194 b \u2228 d := or_congr h\u2081 h\u2082\n\ntheorem or_congr_left (h : a \u2194 b) : a \u2228 c \u2194 b \u2228 c := h.or iff.rfl\n\ntheorem or_congr_right (h : b \u2194 c) : a \u2228 b \u2194 a \u2228 c := iff.rfl.or h\n\ntheorem or.right_comm : (a \u2228 b) \u2228 c \u2194 (a \u2228 c) \u2228 b := by rw [or_assoc, or_assoc, or_comm b]\n\ntheorem or_of_or_of_imp_of_imp (h\u2081 : a \u2228 b) (h\u2082 : a \u2192 c) (h\u2083 : b \u2192 d) : c \u2228 d :=\nor.imp h\u2082 h\u2083 h\u2081\n\ntheorem or_of_or_of_imp_left (h\u2081 : a \u2228 c) (h : a \u2192 b) : b \u2228 c :=\nor.imp_left h h\u2081\n\ntheorem or_of_or_of_imp_right (h\u2081 : c \u2228 a) (h : a \u2192 b) : c \u2228 b :=\nor.imp_right h h\u2081\n\ntheorem or.elim3 (h : a \u2228 b \u2228 c) (ha : a \u2192 d) (hb : b \u2192 d) (hc : c \u2192 d) : d :=\nor.elim h ha (assume h\u2082, or.elim h\u2082 hb hc)\n\nlemma or.imp3 (had : a \u2192 d) (hbe : b \u2192 e) (hcf : c \u2192 f) : a \u2228 b \u2228 c \u2192 d \u2228 e \u2228 f :=\nor.imp had $ or.imp hbe hcf\n\ntheorem or_imp_distrib : (a \u2228 b \u2192 c) \u2194 (a \u2192 c) \u2227 (b \u2192 c) :=\n\u27e8assume h, \u27e8assume ha, h (or.inl ha), assume hb, h (or.inr hb)\u27e9,\n  assume \u27e8ha, hb\u27e9, or.rec ha hb\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_imp_left [decidable a] : a \u2228 b \u2194 (\u00ac a \u2192 b) :=\n\u27e8or.resolve_left, \u03bb h, dite _ or.inl (or.inr \u2218 h)\u27e9\n\ntheorem or_iff_not_imp_left : a \u2228 b \u2194 (\u00ac a \u2192 b) := decidable.or_iff_not_imp_left\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_imp_right [decidable b] : a \u2228 b \u2194 (\u00ac b \u2192 a) :=\nor.comm.trans decidable.or_iff_not_imp_left\n\ntheorem or_iff_not_imp_right : a \u2228 b \u2194 (\u00ac b \u2192 a) := decidable.or_iff_not_imp_right\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp_not [decidable a] : (\u00ac a \u2192 \u00ac b) \u2194 (b \u2192 a) :=\n\u27e8assume h hb, decidable.by_contradiction $ assume na, h na hb, mt\u27e9\n\ntheorem not_imp_not : (\u00ac a \u2192 \u00ac b) \u2194 (b \u2192 a) := decidable.not_imp_not\n\n@[simp] theorem or_iff_left_iff_imp : (a \u2228 b \u2194 a) \u2194 (b \u2192 a) :=\n\u27e8\u03bb h hb, h.1 (or.inr hb), or_iff_left_of_imp\u27e9\n\n@[simp] theorem or_iff_right_iff_imp : (a \u2228 b \u2194 b) \u2194 (a \u2192 b) :=\nby rw [or_comm, or_iff_left_iff_imp]\n\nlemma or_iff_left (hb : \u00ac b) : a \u2228 b \u2194 a := \u27e8\u03bb h, h.resolve_right hb, or.inl\u27e9\nlemma or_iff_right (ha : \u00ac a) : a \u2228 b \u2194 b := \u27e8\u03bb h, h.resolve_left ha, or.inr\u27e9\n\n/-! ### Declarations about distributivity -/\n\n/-- `\u2227` distributes over `\u2228` (on the left). -/\ntheorem and_or_distrib_left : a \u2227 (b \u2228 c) \u2194 (a \u2227 b) \u2228 (a \u2227 c) :=\n\u27e8\u03bb \u27e8ha, hbc\u27e9, hbc.imp (and.intro ha) (and.intro ha),\n or.rec (and.imp_right or.inl) (and.imp_right or.inr)\u27e9\n\n/-- `\u2227` distributes over `\u2228` (on the right). -/\ntheorem or_and_distrib_right : (a \u2228 b) \u2227 c \u2194 (a \u2227 c) \u2228 (b \u2227 c) :=\n(and.comm.trans and_or_distrib_left).trans (and.comm.or and.comm)\n\n/-- `\u2228` distributes over `\u2227` (on the left). -/\ntheorem or_and_distrib_left : a \u2228 (b \u2227 c) \u2194 (a \u2228 b) \u2227 (a \u2228 c) :=\n\u27e8or.rec (\u03bbha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr),\n and.rec $ or.rec (imp_intro \u2218 or.inl) (or.imp_right \u2218 and.intro)\u27e9\n\n/-- `\u2228` distributes over `\u2227` (on the right). -/\ntheorem and_or_distrib_right : (a \u2227 b) \u2228 c \u2194 (a \u2228 c) \u2227 (b \u2228 c) :=\n(or.comm.trans or_and_distrib_left).trans (or.comm.and or.comm)\n\n@[simp] lemma or_self_left : a \u2228 a \u2228 b \u2194 a \u2228 b :=\n\u27e8\u03bb h, h.elim or.inl id, \u03bb h, h.elim or.inl (or.inr \u2218 or.inr)\u27e9\n\n@[simp] lemma or_self_right : (a \u2228 b) \u2228 b \u2194 a \u2228 b :=\n\u27e8\u03bb h, h.elim id or.inr, \u03bb h, h.elim (or.inl \u2218 or.inl) or.inr\u27e9\n\n/-! Declarations about `iff` -/\n\nlemma iff.iff (h\u2081 : a \u2194 b) (h\u2082 : c \u2194 d) : (a \u2194 c) \u2194 (b \u2194 d) := iff_congr h\u2081 h\u2082\n\ntheorem iff_of_true (ha : a) (hb : b) : a \u2194 b :=\n\u27e8\u03bb_, hb, \u03bb _, ha\u27e9\n\ntheorem iff_of_false (ha : \u00aca) (hb : \u00acb) : a \u2194 b :=\n\u27e8ha.elim, hb.elim\u27e9\n\ntheorem iff_true_left (ha : a) : (a \u2194 b) \u2194 b :=\n\u27e8\u03bb h, h.1 ha, iff_of_true ha\u27e9\n\ntheorem iff_true_right (ha : a) : (b \u2194 a) \u2194 b :=\niff.comm.trans (iff_true_left ha)\n\ntheorem iff_false_left (ha : \u00aca) : (a \u2194 b) \u2194 \u00acb :=\n\u27e8\u03bb h, mt h.2 ha, iff_of_false ha\u27e9\n\ntheorem iff_false_right (ha : \u00aca) : (b \u2194 a) \u2194 \u00acb :=\niff.comm.trans (iff_false_left ha)\n\n@[simp]\nlemma iff_mpr_iff_true_intro {P : Prop} (h : P) : iff.mpr (iff_true_intro h) true.intro = h := rfl\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_or_of_imp [decidable a] (h : a \u2192 b) : \u00ac a \u2228 b :=\nif ha : a then or.inr (h ha) else or.inl ha\n\ntheorem not_or_of_imp : (a \u2192 b) \u2192 \u00ac a \u2228 b := decidable.not_or_of_imp\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_iff_not_or [decidable a] : (a \u2192 b) \u2194 (\u00ac a \u2228 b) :=\n\u27e8decidable.not_or_of_imp, or.neg_resolve_left\u27e9\n\ntheorem imp_iff_not_or : (a \u2192 b) \u2194 (\u00ac a \u2228 b) := decidable.imp_iff_not_or\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_or_distrib [decidable a] : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) :=\nby simp [decidable.imp_iff_not_or, or.comm, or.left_comm]\n\ntheorem imp_or_distrib : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) := decidable.imp_or_distrib\n\n-- See Note [decidable namespace]\nprotected theorem decidable.imp_or_distrib' [decidable b] : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) :=\nby by_cases b; simp [h, or_iff_right_of_imp ((\u2218) false.elim)]\n\ntheorem imp_or_distrib' : (a \u2192 b \u2228 c) \u2194 (a \u2192 b) \u2228 (a \u2192 c) := decidable.imp_or_distrib'\n\ntheorem not_imp_of_and_not : a \u2227 \u00ac b \u2192 \u00ac (a \u2192 b)\n| \u27e8ha, hb\u27e9 h := hb $ h ha\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_imp [decidable a] : \u00ac(a \u2192 b) \u2194 a \u2227 \u00acb :=\n\u27e8\u03bb h, \u27e8decidable.of_not_imp h, not_of_not_imp h\u27e9, not_imp_of_and_not\u27e9\n\ntheorem not_imp : \u00ac(a \u2192 b) \u2194 a \u2227 \u00acb := decidable.not_imp\n\n-- for monotonicity\nlemma imp_imp_imp (h\u2080 : c \u2192 a) (h\u2081 : b \u2192 d) : (a \u2192 b) \u2192 (c \u2192 d) :=\nassume (h\u2082 : a \u2192 b), h\u2081 \u2218 h\u2082 \u2218 h\u2080\n\n-- See Note [decidable namespace]\nprotected theorem decidable.peirce (a b : Prop) [decidable a] : ((a \u2192 b) \u2192 a) \u2192 a :=\nif ha : a then \u03bb h, ha else \u03bb h, h ha.elim\n\ntheorem peirce (a b : Prop) : ((a \u2192 b) \u2192 a) \u2192 a := decidable.peirce _ _\n\ntheorem peirce' {a : Prop} (H : \u2200 b : Prop, (a \u2192 b) \u2192 a) : a := H _ id\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff_not [decidable a] [decidable b] : (\u00ac a \u2194 \u00ac b) \u2194 (a \u2194 b) :=\nby rw [@iff_def (\u00ac a), @iff_def' a]; exact decidable.not_imp_not.and decidable.not_imp_not\n\ntheorem not_iff_not : (\u00ac a \u2194 \u00ac b) \u2194 (a \u2194 b) := decidable.not_iff_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff_comm [decidable a] [decidable b] : (\u00ac a \u2194 b) \u2194 (\u00ac b \u2194 a) :=\nby rw [@iff_def (\u00ac a), @iff_def (\u00ac b)]; exact decidable.not_imp_comm.and imp_not_comm\n\ntheorem not_iff_comm : (\u00ac a \u2194 b) \u2194 (\u00ac b \u2194 a) := decidable.not_iff_comm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_iff : \u2200 [decidable b], \u00ac (a \u2194 b) \u2194 (\u00ac a \u2194 b) :=\nby intro h; cases h; simp only [h, iff_true, iff_false]\n\ntheorem not_iff : \u00ac (a \u2194 b) \u2194 (\u00ac a \u2194 b) := decidable.not_iff\n\n-- See Note [decidable namespace]\nprotected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a \u2194 \u00ac b) \u2194 (b \u2194 \u00ac a) :=\nby rw [@iff_def a, @iff_def b]; exact imp_not_comm.and decidable.not_imp_comm\n\ntheorem iff_not_comm : (a \u2194 \u00ac b) \u2194 (b \u2194 \u00ac a) := decidable.iff_not_comm\n\n-- See Note [decidable namespace]\nprotected theorem decidable.iff_iff_and_or_not_and_not [decidable b] :\n  (a \u2194 b) \u2194 (a \u2227 b) \u2228 (\u00ac a \u2227 \u00ac b) :=\nby { split; intro h,\n     { rw h; by_cases b; [left,right]; split; assumption },\n     { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } }\n\ntheorem iff_iff_and_or_not_and_not : (a \u2194 b) \u2194 (a \u2227 b) \u2228 (\u00ac a \u2227 \u00ac b) :=\ndecidable.iff_iff_and_or_not_and_not\n\nlemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] :\n  (a \u2194 b) \u2194 ((\u00aca \u2228 b) \u2227 (a \u2228 \u00acb)) :=\nbegin\n  rw [iff_iff_implies_and_implies a b],\n  simp only [decidable.imp_iff_not_or, or.comm]\nend\n\nlemma iff_iff_not_or_and_or_not : (a \u2194 b) \u2194 ((\u00aca \u2228 b) \u2227 (a \u2228 \u00acb)) :=\ndecidable.iff_iff_not_or_and_or_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_not_right [decidable b] : \u00ac(a \u2227 \u00acb) \u2194 (a \u2192 b) :=\n\u27e8\u03bb h ha, h.decidable_imp_symm $ and.intro ha, \u03bb h \u27e8ha, hb\u27e9, hb $ h ha\u27e9\n\ntheorem not_and_not_right : \u00ac(a \u2227 \u00acb) \u2194 (a \u2192 b) := decidable.not_and_not_right\n\n/-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent.\n**Important**: this function should be used instead of `rw` on `decidable b`, because the\nkernel will get stuck reducing the usage of `propext` otherwise,\nand `dec_trivial` will not work. -/\n@[inline] def decidable_of_iff (a : Prop) (h : a \u2194 b) [D : decidable a] : decidable b :=\ndecidable_of_decidable_of_iff D h\n\n/-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent.\nThis is the same as `decidable_of_iff` but the iff is flipped. -/\n@[inline] def decidable_of_iff' (b : Prop) (h : a \u2194 b) [D : decidable b] : decidable a :=\ndecidable_of_decidable_of_iff D h.symm\n\n/-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b \u2194 a`.\n(This is sometimes taken as an alternate definition of decidability.) -/\ndef decidable_of_bool : \u2200 (b : bool) (h : b \u2194 a), decidable a\n| tt h := is_true (h.1 rfl)\n| ff h := is_false (mt h.2 bool.ff_ne_tt)\n\n/-! ### De Morgan's laws -/\n\ntheorem not_and_of_not_or_not (h : \u00ac a \u2228 \u00ac b) : \u00ac (a \u2227 b)\n| \u27e8ha, hb\u27e9 := or.elim h (absurd ha) (absurd hb)\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_distrib [decidable a] : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb :=\n\u27e8\u03bb h, if ha : a then or.inr (\u03bb hb, h \u27e8ha, hb\u27e9) else or.inl ha, not_and_of_not_or_not\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_and_distrib' [decidable b] : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb :=\n\u27e8\u03bb h, if hb : b then or.inl (\u03bb ha, h \u27e8ha, hb\u27e9) else or.inr hb, not_and_of_not_or_not\u27e9\n\n/-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the\ndisjunction of the negations. -/\ntheorem not_and_distrib : \u00ac (a \u2227 b) \u2194 \u00aca \u2228 \u00acb := decidable.not_and_distrib\n\n@[simp] theorem not_and : \u00ac (a \u2227 b) \u2194 (a \u2192 \u00ac b) := and_imp\n\ntheorem not_and' : \u00ac (a \u2227 b) \u2194 b \u2192 \u00aca :=\nnot_and.trans imp_not_comm\n\n/-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the\nconjunction of the negations. -/\ntheorem not_or_distrib : \u00ac (a \u2228 b) \u2194 \u00ac a \u2227 \u00ac b :=\n\u27e8\u03bb h, \u27e8\u03bb ha, h (or.inl ha), \u03bb hb, h (or.inr hb)\u27e9,\n \u03bb \u27e8h\u2081, h\u2082\u27e9 h, or.elim h h\u2081 h\u2082\u27e9\n\n-- See Note [decidable namespace]\nprotected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a \u2228 b \u2194 \u00ac (\u00aca \u2227 \u00acb) :=\nby rw [\u2190 not_or_distrib, decidable.not_not]\n\ntheorem or_iff_not_and_not : a \u2228 b \u2194 \u00ac (\u00aca \u2227 \u00acb) := decidable.or_iff_not_and_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] :\n  a \u2227 b \u2194 \u00ac (\u00ac a \u2228 \u00ac b) :=\nby rw [\u2190 decidable.not_and_distrib, decidable.not_not]\n\ntheorem and_iff_not_or_not : a \u2227 b \u2194 \u00ac (\u00ac a \u2228 \u00ac b) := decidable.and_iff_not_or_not\n\n@[simp] theorem not_xor (P Q : Prop) : \u00ac xor P Q \u2194 (P \u2194 Q) :=\nby simp only [not_and, xor, not_or_distrib, not_not, \u2190 iff_iff_implies_and_implies]\n\ntheorem xor_iff_not_iff (P Q : Prop) : xor P Q \u2194 \u00ac (P \u2194 Q) :=\nby rw [iff_not_comm, not_xor]\n\n\nend propositional\n\n/-! ### Declarations about equality -/\n\nsection equality\nvariables {\u03b1 : Sort*} {a b : \u03b1}\n\n@[simp] theorem heq_iff_eq : a == b \u2194 a = b :=\n\u27e8eq_of_heq, heq_of_eq\u27e9\n\ntheorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq :=\nhave p = q, from propext \u27e8\u03bb _, hq, \u03bb _, hp\u27e9,\nby subst q; refl\n\ntheorem ne_of_mem_of_not_mem {\u03b1 \u03b2} [has_mem \u03b1 \u03b2] {s : \u03b2} {a b : \u03b1}\n  (h : a \u2208 s) : b \u2209 s \u2192 a \u2260 b :=\nmt $ \u03bb e, e \u25b8 h\n\n-- todo: change name\nlemma ball_cond_comm {\u03b1} {s : \u03b1 \u2192 Prop} {p : \u03b1 \u2192 \u03b1 \u2192 Prop} :\n  (\u2200 a, s a \u2192 \u2200 b, s b \u2192 p a b) \u2194 (\u2200 a b, s a \u2192 s b \u2192 p a b) :=\n\u27e8\u03bb h a b ha hb, h a ha b hb, \u03bb h a ha b hb, h a b ha hb\u27e9\n\nlemma ball_mem_comm {\u03b1 \u03b2} [has_mem \u03b1 \u03b2] {s : \u03b2} {p : \u03b1 \u2192 \u03b1 \u2192 Prop} :\n  (\u2200 a b \u2208 s, p a b) \u2194 (\u2200 a b, a \u2208 s \u2192 b \u2208 s \u2192 p a b) :=\nball_cond_comm\n\nlemma ne_of_apply_ne {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) {x y : \u03b1} (h : f x \u2260 f y) : x \u2260 y :=\n\u03bb (w : x = y), h (congr_arg f w)\n\ntheorem eq_equivalence : equivalence (@eq \u03b1) :=\n\u27e8eq.refl, @eq.symm _, @eq.trans _\u27e9\n\n/-- Transport through trivial families is the identity. -/\n@[simp]\nlemma eq_rec_constant {\u03b1 : Sort*} {a a' : \u03b1} {\u03b2 : Sort*} (y : \u03b2) (h : a = a') :\n  (@eq.rec \u03b1 a (\u03bb a, \u03b2) y a' h) = y :=\nby { cases h, refl, }\n\n@[simp]\nlemma eq_mp_eq_cast {\u03b1 \u03b2 : Sort*} (h : \u03b1 = \u03b2) : eq.mp h = cast h := rfl\n\n@[simp]\nlemma eq_mpr_eq_cast {\u03b1 \u03b2 : Sort*} (h : \u03b1 = \u03b2) : eq.mpr h = cast h.symm := rfl\n\n@[simp]\nlemma cast_cast : \u2200 {\u03b1 \u03b2 \u03b3 : Sort*} (ha : \u03b1 = \u03b2) (hb : \u03b2 = \u03b3) (a : \u03b1),\n  cast hb (cast ha a) = cast (ha.trans hb) a\n| _ _ _ rfl rfl a := rfl\n\n@[simp] lemma congr_refl_left {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) {a b : \u03b1} (h : a = b) :\n  congr (eq.refl f) h = congr_arg f h :=\nrfl\n\n@[simp] lemma congr_refl_right {\u03b1 \u03b2 : Sort*} {f g : \u03b1 \u2192 \u03b2} (h : f = g) (a : \u03b1) :\n  congr h (eq.refl a) = congr_fun h a :=\nrfl\n\n@[simp] lemma congr_arg_refl {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (a : \u03b1) :\n  congr_arg f (eq.refl a) = eq.refl (f a) :=\nrfl\n\n@[simp] lemma congr_fun_rfl {\u03b1 \u03b2 : Sort*} (f : \u03b1 \u2192 \u03b2) (a : \u03b1) :\n  congr_fun (eq.refl f) a = eq.refl (f a) :=\nrfl\n\n@[simp] lemma congr_fun_congr_arg {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) {a a' : \u03b1} (p : a = a') (b : \u03b2) :\n  congr_fun (congr_arg f p) b = congr_arg (\u03bb a, f a b) p :=\nrfl\n\nlemma heq_of_cast_eq :\n  \u2200 {\u03b1 \u03b2 : Sort*} {a : \u03b1} {a' : \u03b2} (e : \u03b1 = \u03b2) (h\u2082 : cast e a = a'), a == a'\n| \u03b1 ._ a a' rfl h := eq.rec_on h (heq.refl _)\n\nlemma cast_eq_iff_heq {\u03b1 \u03b2 : Sort*} {a : \u03b1} {a' : \u03b2} {e : \u03b1 = \u03b2} : cast e a = a' \u2194 a == a' :=\n\u27e8heq_of_cast_eq _, \u03bb h, by cases h; refl\u27e9\n\nlemma rec_heq_of_heq {\u03b2} {C : \u03b1 \u2192 Sort*} {x : C a} {y : \u03b2} (eq : a = b) (h : x == y) :\n  @eq.rec \u03b1 a C x b eq == y :=\nby subst eq; exact h\n\nprotected lemma eq.congr {x\u2081 x\u2082 y\u2081 y\u2082 : \u03b1} (h\u2081 : x\u2081 = y\u2081) (h\u2082 : x\u2082 = y\u2082) :\n  (x\u2081 = x\u2082) \u2194 (y\u2081 = y\u2082) :=\nby { subst h\u2081, subst h\u2082 }\n\nlemma eq.congr_left {x y z : \u03b1} (h : x = y) : x = z \u2194 y = z := by rw [h]\nlemma eq.congr_right {x y z : \u03b1} (h : x = y) : z = x \u2194 z = y := by rw [h]\n\nlemma congr_arg2 {\u03b1 \u03b2 \u03b3 : Sort*} (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) {x x' : \u03b1} {y y' : \u03b2}\n  (hx : x = x') (hy : y = y') : f x y = f x' y' :=\nby { subst hx, subst hy }\n\nend equality\n\n/-! ### Declarations about quantifiers -/\n\nsection quantifiers\nvariables {\u03b1 : Sort*}\n\nsection congr\nvariables {\u03b2 : \u03b1 \u2192 Sort*} {\u03b3 : \u03a0 a, \u03b2 a \u2192 Sort*} {\u03b4 : \u03a0 a b, \u03b3 a b \u2192 Sort*}\n  {\u03b5 : \u03a0 a b c, \u03b4 a b c \u2192 Sort*}\n\nlemma forall\u2082_congr {p q : \u03a0 a, \u03b2 a \u2192 Prop} (h : \u2200 a b, p a b \u2194 q a b) :\n  (\u2200 a b, p a b) \u2194 \u2200 a b, q a b :=\nforall_congr $ \u03bb a, forall_congr $ h a\n\nlemma forall\u2083_congr {p q : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, p a b c \u2194 q a b c) :\n  (\u2200 a b c, p a b c) \u2194 \u2200 a b c, q a b c :=\nforall_congr $ \u03bb a, forall\u2082_congr $ h a\n\nlemma forall\u2084_congr {p q : \u03a0 a b c, \u03b4 a b c \u2192 Prop} (h : \u2200 a b c d, p a b c d \u2194 q a b c d) :\n  (\u2200 a b c d, p a b c d) \u2194 \u2200 a b c d, q a b c d :=\nforall_congr $ \u03bb a, forall\u2083_congr $ h a\n\nlemma forall\u2085_congr {p q : \u03a0 a b c d, \u03b5 a b c d \u2192 Prop}\n  (h : \u2200 a b c d e, p a b c d e \u2194 q a b c d e) :\n  (\u2200 a b c d e, p a b c d e) \u2194 \u2200 a b c d e, q a b c d e :=\nforall_congr $ \u03bb a, forall\u2084_congr $ h a\n\nlemma exists\u2082_congr {p q : \u03a0 a, \u03b2 a \u2192 Prop}  (h : \u2200 a b, p a b \u2194 q a b) :\n  (\u2203 a b, p a b) \u2194 \u2203 a b, q a b :=\nexists_congr $ \u03bb a, exists_congr $ h a\n\nlemma exists\u2083_congr {p q : \u03a0 a b, \u03b3 a b \u2192 Prop} (h : \u2200 a b c, p a b c \u2194 q a b c) :\n  (\u2203 a b c, p a b c) \u2194 \u2203 a b c, q a b c :=\nexists_congr $ \u03bb a, exists\u2082_congr $ h a\n\nlemma exists\u2084_congr {p q : \u03a0 a b c, \u03b4 a b c \u2192 Prop} (h : \u2200 a b c d, p a b c d \u2194 q a b c d) :\n  (\u2203 a b c d, p a b c d) \u2194 \u2203 a b c d, q a b c d :=\nexists_congr $ \u03bb a, exists\u2083_congr $ h a\n\nlemma exists\u2085_congr {p q : \u03a0 a b c d, \u03b5 a b c d \u2192 Prop}\n  (h : \u2200 a b c d e, p a b c d e \u2194 q a b c d e) :\n  (\u2203 a b c d e, p a b c d e) \u2194 \u2203 a b c d e, q a b c d e :=\nexists_congr $ \u03bb a, exists\u2084_congr $ h a\n\nend congr\n\nvariables {\u03b2 : Sort*} {p q : \u03b1 \u2192 Prop} {b : Prop}\n\nlemma forall_imp (h : \u2200 a, p a \u2192 q a) : (\u2200 a, p a) \u2192 \u2200 a, q a :=\n\u03bb h' a, h a (h' a)\n\nlemma Exists.imp (h : \u2200 a, (p a \u2192 q a)) (p : \u2203 a, p a) : \u2203 a, q a := exists_imp_exists h p\n\nlemma exists_imp_exists' {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} (f : \u03b1 \u2192 \u03b2) (hpq : \u2200 a, p a \u2192 q (f a))\n  (hp : \u2203 a, p a) : \u2203 b, q b :=\nexists.elim hp (\u03bb a hp', \u27e8_, hpq _ hp'\u27e9)\n\ntheorem forall_swap {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2200 x y, p x y) \u2194 \u2200 y x, p x y :=\n\u27e8swap, swap\u27e9\n\n/-- We intentionally restrict the type of `\u03b1` in this lemma so that this is a safer to use in simp\nthan `forall_swap`. -/\nlemma imp_forall_iff {\u03b1 : Type*} {p : Prop} {q : \u03b1 \u2192 Prop} : (p \u2192 \u2200 x, q x) \u2194 (\u2200 x, p \u2192 q x) :=\nforall_swap\n\ntheorem exists_swap {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2203 x y, p x y) \u2194 \u2203 y x, p x y :=\n\u27e8\u03bb \u27e8x, y, h\u27e9, \u27e8y, x, h\u27e9, \u03bb \u27e8y, x, h\u27e9, \u27e8x, y, h\u27e9\u27e9\n\n@[simp] theorem forall_exists_index {q : (\u2203 x, p x) \u2192 Prop} :\n  (\u2200 h, q h) \u2194 \u2200 x (h : p x), q \u27e8x, h\u27e9 :=\n\u27e8\u03bb h x hpx, h \u27e8x, hpx\u27e9, \u03bb h \u27e8x, hpx\u27e9, h x hpx\u27e9\n\ntheorem exists_imp_distrib : ((\u2203 x, p x) \u2192 b) \u2194 \u2200 x, p x \u2192 b :=\nforall_exists_index\n\n/--\nExtract an element from a existential statement, using `classical.some`.\n-/\n-- This enables projection notation.\n@[reducible] noncomputable def Exists.some {p : \u03b1 \u2192 Prop} (P : \u2203 a, p a) : \u03b1 := classical.some P\n\n/--\nShow that an element extracted from `P : \u2203 a, p a` using `P.some` satisfies `p`.\n-/\nlemma Exists.some_spec {p : \u03b1 \u2192 Prop} (P : \u2203 a, p a) : p (P.some) := classical.some_spec P\n\n--theorem forall_not_of_not_exists (h : \u00ac \u2203 x, p x) : \u2200 x, \u00ac p x :=\n--forall_imp_of_exists_imp h\n\ntheorem not_exists_of_forall_not (h : \u2200 x, \u00ac p x) : \u00ac \u2203 x, p x :=\nexists_imp_distrib.2 h\n\n@[simp] theorem not_exists : (\u00ac \u2203 x, p x) \u2194 \u2200 x, \u00ac p x :=\nexists_imp_distrib\n\ntheorem not_forall_of_exists_not : (\u2203 x, \u00ac p x) \u2192 \u00ac \u2200 x, p x\n| \u27e8x, hn\u27e9 h := hn (h x)\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_forall {p : \u03b1 \u2192 Prop}\n  [decidable (\u2203 x, \u00ac p x)] [\u2200 x, decidable (p x)] : (\u00ac \u2200 x, p x) \u2194 \u2203 x, \u00ac p x :=\n\u27e8not.decidable_imp_symm $ \u03bb nx x, nx.decidable_imp_symm $ \u03bb h, \u27e8x, h\u27e9,\n not_forall_of_exists_not\u27e9\n\n@[simp] theorem not_forall {p : \u03b1 \u2192 Prop} : (\u00ac \u2200 x, p x) \u2194 \u2203 x, \u00ac p x := decidable.not_forall\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_forall_not [decidable (\u2203 x, p x)] :\n  (\u00ac \u2200 x, \u00ac p x) \u2194 \u2203 x, p x :=\n(@decidable.not_iff_comm _ _ _ (decidable_of_iff (\u00ac \u2203 x, p x) not_exists)).1 not_exists\n\ntheorem not_forall_not : (\u00ac \u2200 x, \u00ac p x) \u2194 \u2203 x, p x := decidable.not_forall_not\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_exists_not [\u2200 x, decidable (p x)] : (\u00ac \u2203 x, \u00ac p x) \u2194 \u2200 x, p x :=\nby simp [decidable.not_not]\n\n@[simp] theorem not_exists_not : (\u00ac \u2203 x, \u00ac p x) \u2194 \u2200 x, p x := decidable.not_exists_not\n\ntheorem forall_imp_iff_exists_imp [ha : nonempty \u03b1] : ((\u2200 x, p x) \u2192 b) \u2194 \u2203 x, p x \u2192 b :=\nlet \u27e8a\u27e9 := ha in\n\u27e8\u03bb h, not_forall_not.1 $ \u03bb h', classical.by_cases (\u03bb hb : b, h' a $ \u03bb _, hb)\n  (\u03bb hb, hb $ h $ \u03bb x, (not_imp.1 (h' x)).1), \u03bb \u27e8x, hx\u27e9 h, hx (h x)\u27e9\n\n-- TODO: duplicate of a lemma in core\ntheorem forall_true_iff : (\u03b1 \u2192 true) \u2194 true :=\nimplies_true_iff \u03b1\n\n-- Unfortunately this causes simp to loop sometimes, so we\n-- add the 2 and 3 cases as simp lemmas instead\ntheorem forall_true_iff' (h : \u2200 a, p a \u2194 true) : (\u2200 a, p a) \u2194 true :=\niff_true_intro (\u03bb _, of_iff_true (h _))\n\n@[simp] theorem forall_2_true_iff {\u03b2 : \u03b1 \u2192 Sort*} : (\u2200 a, \u03b2 a \u2192 true) \u2194 true :=\nforall_true_iff' $ \u03bb _, forall_true_iff\n\n@[simp] theorem forall_3_true_iff {\u03b2 : \u03b1 \u2192 Sort*} {\u03b3 : \u03a0 a, \u03b2 a \u2192 Sort*} :\n  (\u2200 a (b : \u03b2 a), \u03b3 a b \u2192 true) \u2194 true :=\nforall_true_iff' $ \u03bb _, forall_2_true_iff\n\nlemma exists_unique.exists {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} (h : \u2203! x, p x) : \u2203 x, p x :=\nexists.elim h (\u03bb x hx, \u27e8x, and.left hx\u27e9)\n\n@[simp] lemma exists_unique_iff_exists {\u03b1 : Sort*} [subsingleton \u03b1] {p : \u03b1 \u2192 Prop} :\n  (\u2203! x, p x) \u2194 \u2203 x, p x :=\n\u27e8\u03bb h, h.exists, Exists.imp $ \u03bb x hx, \u27e8hx, \u03bb y _, subsingleton.elim y x\u27e9\u27e9\n\n@[simp] theorem forall_const (\u03b1 : Sort*) [i : nonempty \u03b1] : (\u03b1 \u2192 b) \u2194 b :=\n\u27e8i.elim, \u03bb hb x, hb\u27e9\n\n@[simp] theorem exists_const (\u03b1 : Sort*) [i : nonempty \u03b1] : (\u2203 x : \u03b1, b) \u2194 b :=\n\u27e8\u03bb \u27e8x, h\u27e9, h, i.elim exists.intro\u27e9\n\ntheorem exists_unique_const (\u03b1 : Sort*) [i : nonempty \u03b1] [subsingleton \u03b1] :\n  (\u2203! x : \u03b1, b) \u2194 b :=\nby simp\n\ntheorem forall_and_distrib : (\u2200 x, p x \u2227 q x) \u2194 (\u2200 x, p x) \u2227 (\u2200 x, q x) :=\n\u27e8\u03bb h, \u27e8\u03bb x, (h x).left, \u03bb x, (h x).right\u27e9, \u03bb \u27e8h\u2081, h\u2082\u27e9 x, \u27e8h\u2081 x, h\u2082 x\u27e9\u27e9\n\ntheorem exists_or_distrib : (\u2203 x, p x \u2228 q x) \u2194 (\u2203 x, p x) \u2228 (\u2203 x, q x) :=\n\u27e8\u03bb \u27e8x, hpq\u27e9, hpq.elim (\u03bb hpx, or.inl \u27e8x, hpx\u27e9) (\u03bb hqx, or.inr \u27e8x, hqx\u27e9),\n \u03bb hepq, hepq.elim (\u03bb \u27e8x, hpx\u27e9, \u27e8x, or.inl hpx\u27e9) (\u03bb \u27e8x, hqx\u27e9, \u27e8x, or.inr hqx\u27e9)\u27e9\n\n@[simp] theorem exists_and_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2203x, q \u2227 p x) \u2194 q \u2227 (\u2203x, p x) :=\n\u27e8\u03bb \u27e8x, hq, hp\u27e9, \u27e8hq, x, hp\u27e9, \u03bb \u27e8hq, x, hp\u27e9, \u27e8x, hq, hp\u27e9\u27e9\n\n@[simp] theorem exists_and_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2203x, p x \u2227 q) \u2194 (\u2203x, p x) \u2227 q :=\nby simp [and_comm]\n\n@[simp] theorem forall_eq {a' : \u03b1} : (\u2200a, a = a' \u2192 p a) \u2194 p a' :=\n\u27e8\u03bb h, h a' rfl, \u03bb h a e, e.symm \u25b8 h\u27e9\n\n@[simp] theorem forall_eq' {a' : \u03b1} : (\u2200a, a' = a \u2192 p a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\ntheorem and_forall_ne (a : \u03b1) : (p a \u2227 \u2200 b \u2260 a, p b) \u2194 \u2200 b, p b :=\nby simp only [\u2190 @forall_eq _ p a, \u2190 forall_and_distrib, \u2190 or_imp_distrib, classical.em,\n  forall_const]\n\n-- this lemma is needed to simplify the output of `list.mem_cons_iff`\n@[simp] theorem forall_eq_or_imp {a' : \u03b1} : (\u2200 a, a = a' \u2228 q a \u2192 p a) \u2194 p a' \u2227 \u2200 a, q a \u2192 p a :=\nby simp only [or_imp_distrib, forall_and_distrib, forall_eq]\n\ntheorem exists_eq {a' : \u03b1} : \u2203 a, a = a' := \u27e8_, rfl\u27e9\n\n@[simp] theorem exists_eq' {a' : \u03b1} : \u2203 a, a' = a := \u27e8_, rfl\u27e9\n\n@[simp] theorem exists_unique_eq {a' : \u03b1} : \u2203! a, a = a' :=\nby simp only [eq_comm, exists_unique, and_self, forall_eq', exists_eq']\n\n@[simp] theorem exists_unique_eq' {a' : \u03b1} : \u2203! a, a' = a :=\nby simp only [exists_unique, and_self, forall_eq', exists_eq']\n\n@[simp] theorem exists_eq_left {a' : \u03b1} : (\u2203 a, a = a' \u2227 p a) \u2194 p a' :=\n\u27e8\u03bb \u27e8a, e, h\u27e9, e \u25b8 h, \u03bb h, \u27e8_, rfl, h\u27e9\u27e9\n\n@[simp] theorem exists_eq_right {a' : \u03b1} : (\u2203 a, p a \u2227 a = a') \u2194 p a' :=\n(exists_congr $ by exact \u03bb a, and.comm).trans exists_eq_left\n\n@[simp] theorem exists_eq_right_right {a' : \u03b1} :\n  (\u2203 (a : \u03b1), p a \u2227 b \u2227 a = a') \u2194 p a' \u2227 b :=\n\u27e8\u03bb \u27e8_, hp, hq, rfl\u27e9, \u27e8hp, hq\u27e9, \u03bb \u27e8hp, hq\u27e9, \u27e8a', hp, hq, rfl\u27e9\u27e9\n\n@[simp] theorem exists_eq_right_right' {a' : \u03b1} :\n  (\u2203 (a : \u03b1), p a \u2227 b \u2227 a' = a) \u2194 p a' \u2227 b :=\n\u27e8\u03bb \u27e8_, hp, hq, rfl\u27e9, \u27e8hp, hq\u27e9, \u03bb \u27e8hp, hq\u27e9, \u27e8a', hp, hq, rfl\u27e9\u27e9\n\n@[simp] theorem exists_apply_eq_apply (f : \u03b1 \u2192 \u03b2) (a' : \u03b1) : \u2203 a, f a = f a' := \u27e8a', rfl\u27e9\n\n@[simp] theorem exists_apply_eq_apply' (f : \u03b1 \u2192 \u03b2) (a' : \u03b1) : \u2203 a, f a' = f a := \u27e8a', rfl\u27e9\n\n@[simp] theorem exists_exists_and_eq_and {f : \u03b1 \u2192 \u03b2} {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} :\n  (\u2203 b, (\u2203 a, p a \u2227 f a = b) \u2227 q b) \u2194 \u2203 a, p a \u2227 q (f a) :=\n\u27e8\u03bb \u27e8b, \u27e8a, ha, hab\u27e9, hb\u27e9, \u27e8a, ha, hab.symm \u25b8 hb\u27e9, \u03bb \u27e8a, hp, hq\u27e9, \u27e8f a, \u27e8a, hp, rfl\u27e9, hq\u27e9\u27e9\n\n@[simp] theorem exists_exists_eq_and {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2203 b, (\u2203 a, f a = b) \u2227 p b) \u2194 \u2203 a, p (f a) :=\n\u27e8\u03bb \u27e8b, \u27e8a, ha\u27e9, hb\u27e9, \u27e8a, ha.symm \u25b8 hb\u27e9, \u03bb \u27e8a, ha\u27e9, \u27e8f a, \u27e8a, rfl\u27e9, ha\u27e9\u27e9\n\n@[simp] lemma exists_or_eq_left (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), x = y \u2228 p x :=\n\u27e8y, or.inl rfl\u27e9\n\n@[simp] lemma exists_or_eq_right (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), p x \u2228 x = y :=\n\u27e8y, or.inr rfl\u27e9\n\n@[simp] lemma exists_or_eq_left' (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), y = x \u2228 p x :=\n\u27e8y, or.inl rfl\u27e9\n\n@[simp] lemma exists_or_eq_right' (y : \u03b1) (p : \u03b1 \u2192 Prop) : \u2203 (x : \u03b1), p x \u2228 y = x :=\n\u27e8y, or.inr rfl\u27e9\n\n@[simp] theorem forall_apply_eq_imp_iff {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 a, \u2200 b, f a = b \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\n\u27e8\u03bb h a, h a (f a) rfl, \u03bb h a b hab, hab \u25b8 h a\u27e9\n\n@[simp] theorem forall_apply_eq_imp_iff' {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, f a = b \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby { rw forall_swap, simp }\n\n@[simp] theorem forall_eq_apply_imp_iff {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 a, \u2200 b, b = f a \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby simp [@eq_comm _ _ (f _)]\n\n@[simp] theorem forall_eq_apply_imp_iff' {f : \u03b1 \u2192 \u03b2} {p : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, b = f a \u2192 p b) \u2194 (\u2200 a, p (f a)) :=\nby { rw forall_swap, simp }\n\n@[simp] theorem forall_apply_eq_imp_iff\u2082 {f : \u03b1 \u2192 \u03b2} {p : \u03b1 \u2192 Prop} {q : \u03b2 \u2192 Prop} :\n  (\u2200 b, \u2200 a, p a \u2192 f a = b \u2192 q b) \u2194 \u2200 a, p a \u2192 q (f a) :=\n\u27e8\u03bb h a ha, h (f a) a ha rfl, \u03bb h b a ha hb, hb \u25b8 h a ha\u27e9\n\n@[simp] theorem exists_eq_left' {a' : \u03b1} : (\u2203 a, a' = a \u2227 p a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\n@[simp] theorem exists_eq_right' {a' : \u03b1} : (\u2203 a, p a \u2227 a' = a) \u2194 p a' :=\nby simp [@eq_comm _ a']\n\ntheorem exists_comm {p : \u03b1 \u2192 \u03b2 \u2192 Prop} : (\u2203 a b, p a b) \u2194 \u2203 b a, p a b :=\n\u27e8\u03bb \u27e8a, b, h\u27e9, \u27e8b, a, h\u27e9, \u03bb \u27e8b, a, h\u27e9, \u27e8a, b, h\u27e9\u27e9\n\ntheorem and.exists {p q : Prop} {f : p \u2227 q \u2192 Prop} : (\u2203 h, f h) \u2194 \u2203 hp hq, f \u27e8hp, hq\u27e9 :=\n\u27e8\u03bb \u27e8h, H\u27e9, \u27e8h.1, h.2, H\u27e9, \u03bb \u27e8hp, hq, H\u27e9, \u27e8\u27e8hp, hq\u27e9, H\u27e9\u27e9\n\ntheorem forall_or_of_or_forall (h : b \u2228 \u2200x, p x) (x) : b \u2228 p x :=\nh.imp_right $ \u03bb h\u2082, h\u2082 x\n\n-- See Note [decidable namespace]\nprotected theorem decidable.forall_or_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} [decidable q] :\n  (\u2200x, q \u2228 p x) \u2194 q \u2228 (\u2200x, p x) :=\n\u27e8\u03bb h, if hq : q then or.inl hq else or.inr $ \u03bb x, (h x).resolve_left hq,\n  forall_or_of_or_forall\u27e9\n\ntheorem forall_or_distrib_left {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2200x, q \u2228 p x) \u2194 q \u2228 (\u2200x, p x) := decidable.forall_or_distrib_left\n\n-- See Note [decidable namespace]\nprotected theorem decidable.forall_or_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} [decidable q] :\n  (\u2200x, p x \u2228 q) \u2194 (\u2200x, p x) \u2228 q :=\nby simp [or_comm, decidable.forall_or_distrib_left]\n\ntheorem forall_or_distrib_right {q : Prop} {p : \u03b1 \u2192 Prop} :\n  (\u2200x, p x \u2228 q) \u2194 (\u2200x, p x) \u2228 q := decidable.forall_or_distrib_right\n\n@[simp] theorem exists_prop {p q : Prop} : (\u2203 h : p, q) \u2194 p \u2227 q :=\n\u27e8\u03bb \u27e8h\u2081, h\u2082\u27e9, \u27e8h\u2081, h\u2082\u27e9, \u03bb \u27e8h\u2081, h\u2082\u27e9, \u27e8h\u2081, h\u2082\u27e9\u27e9\n\ntheorem exists_unique_prop {p q : Prop} : (\u2203! h : p, q) \u2194 p \u2227 q :=\nby simp\n\n@[simp] theorem exists_false : \u00ac (\u2203a:\u03b1, false) := assume \u27e8a, h\u27e9, h\n\n@[simp] lemma exists_unique_false : \u00ac (\u2203! (a : \u03b1), false) := assume \u27e8a, h, h'\u27e9, h\n\ntheorem Exists.fst {p : b \u2192 Prop} : Exists p \u2192 b\n| \u27e8h, _\u27e9 := h\n\ntheorem Exists.snd {p : b \u2192 Prop} : \u2200 h : Exists p, p h.fst\n| \u27e8_, h\u27e9 := h\n\ntheorem forall_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2200 h' : p, q h') \u2194 q h :=\n@forall_const (q h) p \u27e8h\u27e9\n\ntheorem exists_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2203 h' : p, q h') \u2194 q h :=\n@exists_const (q h) p \u27e8h\u27e9\n\nlemma exists_iff_of_forall {p : Prop} {q : p \u2192 Prop} (h : \u2200 h, q h) : (\u2203 h, q h) \u2194 p :=\n\u27e8Exists.fst, \u03bb H, \u27e8H, h H\u27e9\u27e9\n\ntheorem exists_unique_prop_of_true {p : Prop} {q : p \u2192 Prop} (h : p) : (\u2203! h' : p, q h') \u2194 q h :=\n@exists_unique_const (q h) p \u27e8h\u27e9 _\n\ntheorem forall_prop_of_false {p : Prop} {q : p \u2192 Prop} (hn : \u00ac p) :\n  (\u2200 h' : p, q h') \u2194 true :=\niff_true_intro $ \u03bb h, hn.elim h\n\ntheorem exists_prop_of_false {p : Prop} {q : p \u2192 Prop} : \u00ac p \u2192 \u00ac (\u2203 h' : p, q h') :=\nmt Exists.fst\n\n@[congr] lemma exists_prop_congr {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : Exists q \u2194 \u2203 h : p', q' (hp.2 h) :=\n\u27e8\u03bb \u27e8_, _\u27e9, \u27e8hp.1 \u2039_\u203a, (hq _).1 \u2039_\u203a\u27e9, \u03bb \u27e8_, _\u27e9, \u27e8_, (hq _).2 \u2039_\u203a\u27e9\u27e9\n\n@[congr] lemma exists_prop_congr' {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : Exists q = \u2203 h : p', q' (hp.2 h) :=\npropext (exists_prop_congr hq _)\n\n@[simp] lemma exists_true_left (p : true \u2192 Prop) : (\u2203 x, p x) \u2194 p true.intro :=\nexists_prop_of_true _\n\n@[simp] lemma exists_false_left (p : false \u2192 Prop) : \u00ac \u2203 x, p x :=\nexists_prop_of_false not_false\n\nlemma exists_unique.unique {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} (h : \u2203! x, p x)\n  {y\u2081 y\u2082 : \u03b1} (py\u2081 : p y\u2081) (py\u2082 : p y\u2082) : y\u2081 = y\u2082 :=\nunique_of_exists_unique h py\u2081 py\u2082\n\n@[congr] lemma forall_prop_congr {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : (\u2200 h, q h) \u2194 \u2200 h : p', q' (hp.2 h) :=\n\u27e8\u03bb h1 h2, (hq _).1 (h1 (hp.2 _)), \u03bb h1 h2, (hq _).2 (h1 (hp.1 h2))\u27e9\n\n@[congr] lemma forall_prop_congr' {p p' : Prop} {q q' : p \u2192 Prop}\n  (hq : \u2200 h, q h \u2194 q' h) (hp : p \u2194 p') : (\u2200 h, q h) = \u2200 h : p', q' (hp.2 h) :=\npropext (forall_prop_congr hq _)\n\n@[simp] lemma forall_true_left (p : true \u2192 Prop) : (\u2200 x, p x) \u2194 p true.intro :=\nforall_prop_of_true _\n\n@[simp] lemma forall_false_left (p : false \u2192 Prop) : (\u2200 x, p x) \u2194 true :=\nforall_prop_of_false not_false\n\nlemma exists_unique.elim2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 x (h : p x), Prop} {b : Prop} (h\u2082 : \u2203! x (h : p x), q x h)\n  (h\u2081 : \u2200 x (h : p x), q x h \u2192 (\u2200 y (hy : p y), q y hy \u2192 y = x) \u2192 b) : b :=\nbegin\n  simp only [exists_unique_iff_exists] at h\u2082,\n  apply h\u2082.elim,\n  exact \u03bb x \u27e8hxp, hxq\u27e9 H, h\u2081 x hxp hxq (\u03bb y hyp hyq, H y \u27e8hyp, hyq\u27e9)\nend\n\nlemma exists_unique.intro2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 (x : \u03b1) (h : p x), Prop} (w : \u03b1) (hp : p w) (hq : q w hp)\n  (H : \u2200 y (hy : p y), q y hy \u2192 y = w) :\n  \u2203! x (hx : p x), q x hx :=\nbegin\n  simp only [exists_unique_iff_exists],\n  exact exists_unique.intro w \u27e8hp, hq\u27e9 (\u03bb y \u27e8hyp, hyq\u27e9, H y hyp hyq)\nend\n\nlemma exists_unique.exists2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} {q : \u03a0 (x : \u03b1) (h : p x), Prop}\n  (h : \u2203! x (hx : p x), q x hx) :\n  \u2203 x (hx : p x), q x hx :=\nh.exists.imp (\u03bb x hx, hx.exists)\n\nlemma exists_unique.unique2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Sort*} [\u2200 x, subsingleton (p x)]\n  {q : \u03a0 (x : \u03b1) (hx : p x), Prop} (h : \u2203! x (hx : p x), q x hx)\n  {y\u2081 y\u2082 : \u03b1} (hpy\u2081 : p y\u2081) (hqy\u2081 : q y\u2081 hpy\u2081)\n  (hpy\u2082 : p y\u2082) (hqy\u2082 : q y\u2082 hpy\u2082) : y\u2081 = y\u2082 :=\nbegin\n  simp only [exists_unique_iff_exists] at h,\n  exact h.unique \u27e8hpy\u2081, hqy\u2081\u27e9 \u27e8hpy\u2082, hqy\u2082\u27e9\nend\n\nend quantifiers\n\n/-! ### Classical lemmas -/\n\nnamespace classical\nvariables {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop}\n\ntheorem cases {p : Prop \u2192 Prop} (h1 : p true) (h2 : p false) : \u2200a, p a :=\nassume a, cases_on a h1 h2\n\n/- use shortened names to avoid conflict when classical namespace is open. -/\n/-- Any prop `p` is decidable classically. A shorthand for `classical.prop_decidable`. -/\nnoncomputable def dec (p : Prop) : decidable p :=\nby apply_instance\n/-- Any predicate `p` is decidable classically. -/\nnoncomputable def dec_pred (p : \u03b1 \u2192 Prop) : decidable_pred p :=\nby apply_instance\n/-- Any relation `p` is decidable classically. -/\nnoncomputable def dec_rel (p : \u03b1 \u2192 \u03b1 \u2192 Prop) : decidable_rel p :=\nby apply_instance\n/-- Any type `\u03b1` has decidable equality classically. -/\nnoncomputable def dec_eq (\u03b1 : Sort*) : decidable_eq \u03b1 :=\nby apply_instance\n\n/-- Construct a function from a default value `H0`, and a function to use if there exists a value\nsatisfying the predicate. -/\n@[elab_as_eliminator]\nnoncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : \u2200 a, p a \u2192 C) : C :=\nif h : \u2203 a, p a then H (classical.some h) (classical.some_spec h) else H0\n\nlemma some_spec2 {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} {h : \u2203a, p a}\n  (q : \u03b1 \u2192 Prop) (hpq : \u2200a, p a \u2192 q a) : q (some h) :=\nhpq _ $ some_spec _\n\n/-- A version of classical.indefinite_description which is definitionally equal to a pair -/\nnoncomputable def subtype_of_exists {\u03b1 : Type*} {P : \u03b1 \u2192 Prop} (h : \u2203 x, P x) : {x // P x} :=\n\u27e8classical.some h, classical.some_spec h\u27e9\n\n/-- A version of `by_contradiction` that uses types instead of propositions. -/\nprotected noncomputable def by_contradiction' {\u03b1 : Sort*} (H : \u00ac (\u03b1 \u2192 false)) : \u03b1 :=\nclassical.choice $ peirce _ false $ \u03bb h, (H $ \u03bb a, h \u27e8a\u27e9).elim\n\n/-- `classical.by_contradiction'` is equivalent to lean's axiom `classical.choice`. -/\ndef choice_of_by_contradiction' {\u03b1 : Sort*} (contra : \u00ac (\u03b1 \u2192 false) \u2192 \u03b1) : nonempty \u03b1 \u2192 \u03b1 :=\n\u03bb H, contra H.elim\n\nend classical\n\n/-- This function has the same type as `exists.rec_on`, and can be used to case on an equality,\nbut `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe\nusing the axiom of choice. -/\n@[elab_as_eliminator]\nnoncomputable def {u} exists.classical_rec_on\n {\u03b1} {p : \u03b1 \u2192 Prop} (h : \u2203 a, p a) {C : Sort u} (H : \u2200 a, p a \u2192 C) : C :=\nH (classical.some h) (classical.some_spec h)\n\n/-! ### Declarations about bounded quantifiers -/\n\nsection bounded_quantifiers\nvariables {\u03b1 : Sort*} {r p q : \u03b1 \u2192 Prop} {P Q : \u2200 x, p x \u2192 Prop} {b : Prop}\n\ntheorem bex_def : (\u2203 x (h : p x), q x) \u2194 \u2203 x, p x \u2227 q x :=\n\u27e8\u03bb \u27e8x, px, qx\u27e9, \u27e8x, px, qx\u27e9, \u03bb \u27e8x, px, qx\u27e9, \u27e8x, px, qx\u27e9\u27e9\n\ntheorem bex.elim {b : Prop} : (\u2203 x h, P x h) \u2192 (\u2200 a h, P a h \u2192 b) \u2192 b\n| \u27e8a, h\u2081, h\u2082\u27e9 h' := h' a h\u2081 h\u2082\n\ntheorem bex.intro (a : \u03b1) (h\u2081 : p a) (h\u2082 : P a h\u2081) : \u2203 x (h : p x), P x h :=\n\u27e8a, h\u2081, h\u2082\u27e9\n\ntheorem ball_congr (H : \u2200 x h, P x h \u2194 Q x h) :\n  (\u2200 x h, P x h) \u2194 (\u2200 x h, Q x h) :=\nforall_congr $ \u03bb x, forall_congr (H x)\n\ntheorem bex_congr (H : \u2200 x h, P x h \u2194 Q x h) :\n  (\u2203 x h, P x h) \u2194 (\u2203 x h, Q x h) :=\nexists_congr $ \u03bb x, exists_congr (H x)\n\ntheorem bex_eq_left {a : \u03b1} : (\u2203 x (_ : x = a), p x) \u2194 p a :=\nby simp only [exists_prop, exists_eq_left]\n\ntheorem ball.imp_right (H : \u2200 x h, (P x h \u2192 Q x h))\n  (h\u2081 : \u2200 x h, P x h) (x h) : Q x h :=\nH _ _ $ h\u2081 _ _\n\ntheorem bex.imp_right (H : \u2200 x h, (P x h \u2192 Q x h)) :\n  (\u2203 x h, P x h) \u2192 \u2203 x h, Q x h\n| \u27e8x, h, h'\u27e9 := \u27e8_, _, H _ _ h'\u27e9\n\ntheorem ball.imp_left (H : \u2200 x, p x \u2192 q x)\n  (h\u2081 : \u2200 x, q x \u2192 r x) (x) (h : p x) : r x :=\nh\u2081 _ $ H _ h\n\ntheorem bex.imp_left (H : \u2200 x, p x \u2192 q x) :\n  (\u2203 x (_ : p x), r x) \u2192 \u2203 x (_ : q x), r x\n| \u27e8x, hp, hr\u27e9 := \u27e8x, H _ hp, hr\u27e9\n\ntheorem ball_of_forall (h : \u2200 x, p x) (x) : p x :=\nh x\n\ntheorem forall_of_ball (H : \u2200 x, p x) (h : \u2200 x, p x \u2192 q x) (x) : q x :=\nh x $ H x\n\ntheorem bex_of_exists (H : \u2200 x, p x) : (\u2203 x, q x) \u2192 \u2203 x (_ : p x), q x\n| \u27e8x, hq\u27e9 := \u27e8x, H x, hq\u27e9\n\ntheorem exists_of_bex : (\u2203 x (_ : p x), q x) \u2192 \u2203 x, q x\n| \u27e8x, _, hq\u27e9 := \u27e8x, hq\u27e9\n\n@[simp] theorem bex_imp_distrib : ((\u2203 x h, P x h) \u2192 b) \u2194 (\u2200 x h, P x h \u2192 b) :=\nby simp\n\ntheorem not_bex : (\u00ac \u2203 x h, P x h) \u2194 \u2200 x h, \u00ac P x h :=\nbex_imp_distrib\n\ntheorem not_ball_of_bex_not : (\u2203 x h, \u00ac P x h) \u2192 \u00ac \u2200 x h, P x h\n| \u27e8x, h, hp\u27e9 al := hp $ al x h\n\n-- See Note [decidable namespace]\nprotected theorem decidable.not_ball [decidable (\u2203 x h, \u00ac P x h)] [\u2200 x h, decidable (P x h)] :\n  (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) :=\n\u27e8not.decidable_imp_symm $ \u03bb nx x h, nx.decidable_imp_symm $ \u03bb h', \u27e8x, h, h'\u27e9,\n not_ball_of_bex_not\u27e9\n\ntheorem not_ball : (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) := decidable.not_ball\n\ntheorem ball_true_iff (p : \u03b1 \u2192 Prop) : (\u2200 x, p x \u2192 true) \u2194 true :=\niff_true_intro (\u03bb h hrx, trivial)\n\ntheorem ball_and_distrib : (\u2200 x h, P x h \u2227 Q x h) \u2194 (\u2200 x h, P x h) \u2227 (\u2200 x h, Q x h) :=\niff.trans (forall_congr $ \u03bb x, forall_and_distrib) forall_and_distrib\n\ntheorem bex_or_distrib : (\u2203 x h, P x h \u2228 Q x h) \u2194 (\u2203 x h, P x h) \u2228 (\u2203 x h, Q x h) :=\niff.trans (exists_congr $ \u03bb x, exists_or_distrib) exists_or_distrib\n\ntheorem ball_or_left_distrib : (\u2200 x, p x \u2228 q x \u2192 r x) \u2194 (\u2200 x, p x \u2192 r x) \u2227 (\u2200 x, q x \u2192 r x) :=\niff.trans (forall_congr $ \u03bb x, or_imp_distrib) forall_and_distrib\n\ntheorem bex_or_left_distrib :\n  (\u2203 x (_ : p x \u2228 q x), r x) \u2194 (\u2203 x (_ : p x), r x) \u2228 (\u2203 x (_ : q x), r x) :=\nby simp only [exists_prop]; exact\niff.trans (exists_congr $ \u03bb x, or_and_distrib_right) exists_or_distrib\n\nend bounded_quantifiers\n\nnamespace classical\nlocal attribute [instance] prop_decidable\n\ntheorem not_ball {\u03b1 : Sort*} {p : \u03b1 \u2192 Prop} {P : \u03a0 (x : \u03b1), p x \u2192 Prop} :\n  (\u00ac \u2200 x h, P x h) \u2194 (\u2203 x h, \u00ac P x h) := _root_.not_ball\n\nend classical\n\nsection ite\nvariables {\u03b1 \u03b2 \u03b3 : Sort*} {\u03c3 : \u03b1 \u2192 Sort*} (f : \u03b1 \u2192 \u03b2) {P Q : Prop} [decidable P] [decidable Q]\n  {a b c : \u03b1} {A : P \u2192 \u03b1} {B : \u00ac P \u2192 \u03b1}\n\nlemma dite_eq_iff : dite P A B = c \u2194 (\u2203 h, A h = c) \u2228 \u2203 h, B h = c := by by_cases P; simp *\nlemma ite_eq_iff : ite P a b = c \u2194 P \u2227 a = c \u2228 \u00ac P \u2227 b = c :=\ndite_eq_iff.trans $ by rw [exists_prop, exists_prop]\n\n@[simp] lemma dite_eq_left_iff : dite P (\u03bb _, a) B = a \u2194 \u2200 h, B h = a := by by_cases P; simp *\n@[simp] lemma dite_eq_right_iff : dite P A (\u03bb _, b) = b \u2194 \u2200 h, A h = b := by by_cases P; simp *\n@[simp] lemma ite_eq_left_iff : ite P a b = a \u2194 (\u00ac P \u2192 b = a) := dite_eq_left_iff\n@[simp] lemma ite_eq_right_iff : ite P a b = b \u2194 (P \u2192 a = b) := dite_eq_right_iff\n\nlemma dite_ne_left_iff : dite P (\u03bb _, a) B \u2260 a \u2194 \u2203 h, a \u2260 B h :=\nby { rw [ne.def, dite_eq_left_iff, not_forall], exact exists_congr (\u03bb h, by rw ne_comm) }\n\nlemma dite_ne_right_iff : dite P A (\u03bb _, b) \u2260 b \u2194 \u2203 h, A h \u2260 b :=\nby simp only [ne.def, dite_eq_right_iff, not_forall]\n\nlemma ite_ne_left_iff : ite P a b \u2260 a \u2194 \u00ac P \u2227 a \u2260 b := dite_ne_left_iff.trans $ by rw exists_prop\nlemma ite_ne_right_iff : ite P a b \u2260 b \u2194 P \u2227 a \u2260 b := dite_ne_right_iff.trans $ by rw exists_prop\n\nprotected lemma ne.dite_eq_left_iff (h : \u2200 h, a \u2260 B h) : dite P (\u03bb _, a) B = a \u2194 P :=\ndite_eq_left_iff.trans $ \u27e8\u03bb H, of_not_not $ \u03bb h', h h' (H h').symm, \u03bb h H, (H h).elim\u27e9\n\nprotected lemma ne.dite_eq_right_iff (h : \u2200 h, A h \u2260 b) : dite P A (\u03bb _, b) = b \u2194 \u00ac P :=\ndite_eq_right_iff.trans $ \u27e8\u03bb H h', h h' (H h'), \u03bb h' H, (h' H).elim\u27e9\n\nprotected lemma ne.ite_eq_left_iff (h : a \u2260 b) : ite P a b = a \u2194 P := ne.dite_eq_left_iff $ \u03bb _, h\nprotected lemma ne.ite_eq_right_iff (h : a \u2260 b) : ite P a b = b \u2194 \u00ac P :=\nne.dite_eq_right_iff $ \u03bb _, h\n\nprotected lemma ne.dite_ne_left_iff (h : \u2200 h, a \u2260 B h) : dite P (\u03bb _, a) B \u2260 a \u2194 \u00ac P :=\ndite_ne_left_iff.trans $ exists_iff_of_forall h\n\nprotected lemma ne.dite_ne_right_iff (h : \u2200 h, A h \u2260 b) : dite P A (\u03bb _, b) \u2260 b \u2194 P :=\ndite_ne_right_iff.trans $ exists_iff_of_forall h\n\nprotected lemma ne.ite_ne_left_iff (h : a \u2260 b) : ite P a b \u2260 a \u2194 \u00ac P := ne.dite_ne_left_iff $ \u03bb _, h\n\nprotected lemma ne.ite_ne_right_iff (h : a \u2260 b) : ite P a b \u2260 b \u2194 P := ne.dite_ne_right_iff $ \u03bb _, h\n\nvariables (P Q) (a b)\n\n/-- A `dite` whose results do not actually depend on the condition may be reduced to an `ite`. -/\n@[simp] lemma dite_eq_ite : dite P (\u03bb h, a) (\u03bb h, b) = ite P a b := rfl\n\nlemma dite_eq_or_eq : (\u2203 h, dite P A B = A h) \u2228 \u2203 h, dite P A B = B h :=\ndecidable.by_cases (\u03bb h, or.inl \u27e8h, dif_pos h\u27e9) (\u03bb h, or.inr \u27e8h, dif_neg h\u27e9)\n\nlemma ite_eq_or_eq : ite P a b = a \u2228 ite P a b = b :=\ndecidable.by_cases (\u03bb h, or.inl (if_pos h)) (\u03bb h, or.inr (if_neg h))\n\n/-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/\nlemma apply_dite (x : P \u2192 \u03b1) (y : \u00acP \u2192 \u03b1) : f (dite P x y) = dite P (\u03bb h, f (x h)) (\u03bb h, f (y h)) :=\nby by_cases h : P; simp [h]\n\n/-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/\nlemma apply_ite : f (ite P a b) = ite P (f a) (f b) := apply_dite f P (\u03bb _, a) (\u03bb _, b)\n\n/-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function\napplied to each of the branches. -/\nlemma apply_dite2 (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) (P : Prop) [decidable P] (a : P \u2192 \u03b1) (b : \u00acP \u2192 \u03b1) (c : P \u2192 \u03b2)\n  (d : \u00acP \u2192 \u03b2) :\n  f (dite P a b) (dite P c d) = dite P (\u03bb h, f (a h) (c h)) (\u03bb h, f (b h) (d h)) :=\nby by_cases h : P; simp [h]\n\n/-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function\napplied to each of the branches. -/\nlemma apply_ite2 (f : \u03b1 \u2192 \u03b2 \u2192 \u03b3) (P : Prop) [decidable P] (a b : \u03b1) (c d : \u03b2) :\n  f (ite P a b) (ite P c d) = ite P (f a c) (f b d) :=\napply_dite2 f P (\u03bb _, a) (\u03bb _, b) (\u03bb _, c) (\u03bb _, d)\n\n/-- A 'dite' producing a `Pi` type `\u03a0 a, \u03c3 a`, applied to a value `a : \u03b1` is a `dite` that applies\neither branch to `a`. -/\nlemma dite_apply (f : P \u2192 \u03a0 a, \u03c3 a) (g : \u00ac P \u2192 \u03a0 a, \u03c3 a) (a : \u03b1) :\n  (dite P f g) a = dite P (\u03bb h, f h a) (\u03bb h, g h a) :=\nby by_cases h : P; simp [h]\n\n/-- A 'ite' producing a `Pi` type `\u03a0 a, \u03c3 a`, applied to a value `a : \u03b1` is a `ite` that applies\neither branch to `a`. -/\nlemma ite_apply (f g : \u03a0 a, \u03c3 a) (a : \u03b1) : (ite P f g) a = ite P (f a) (g a) :=\ndite_apply P (\u03bb _, f) (\u03bb _, g) a\n\n/-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/\n@[simp] lemma dite_not (x : \u00ac P \u2192 \u03b1) (y : \u00ac\u00ac P \u2192 \u03b1) :\n  dite (\u00ac P) x y = dite P (\u03bb h, y (not_not_intro h)) x :=\nby by_cases h : P; simp [h]\n\n/-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/\n@[simp] lemma ite_not : ite (\u00ac P) a b = ite P b a := dite_not P (\u03bb _, a) (\u03bb _, b)\n\nlemma ite_and : ite (P \u2227 Q) a b = ite P (ite Q a b) b :=\nby by_cases hp : P; by_cases hq : Q; simp [hp, hq]\n\nend ite\n", "meta": {"author": "Mel-TunaRoll", "repo": "Lean-Mordell-Weil-Mel-Branch", "sha": "4db36f86423976aacd2c2968c4e45787fcd86b97", "save_path": "github-repos/lean/Mel-TunaRoll-Lean-Mordell-Weil-Mel-Branch", "path": "github-repos/lean/Mel-TunaRoll-Lean-Mordell-Weil-Mel-Branch/Lean-Mordell-Weil-Mel-Branch-4db36f86423976aacd2c2968c4e45787fcd86b97/src/logic/basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.41489886026626094, "lm_q2_score": 0.12252320610998799, "lm_q1q2_score": 0.050834738571202194}}
{"text": "import MyNat.Definition\n/-!\n\n## Tactic : tauto\n\n## Summary\n\nThe `tauto` tactic (and its variant `tauto!`) will close various logic\ngoals.\n\n## Details\n\n`tauto` is an all-purpose logic tactic which will try to solve goals using pure\nlogical reasoning -- for example it will close the following goal:\n\n```\nP Q : Prop,\nhP : P,\nhQ : Q\n\u22a2 P \u2227 Q\n```\n\n`tauto` is supposed to only use constructive logic, but its big brother `tauto!` uses classical logic\nand hence closes more goals.\n\n-/", "meta": {"author": "leanprover", "repo": "lean4-samples", "sha": "5c0db5f1e952e7ebada506ba3a390040972293a9", "save_path": "github-repos/lean/leanprover-lean4-samples", "path": "github-repos/lean/leanprover-lean4-samples/lean4-samples-5c0db5f1e952e7ebada506ba3a390040972293a9/NaturalNumbers/Tactics/tauto.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO\n\n", "lm_q1_score": 0.480478678047907, "lm_q2_score": 0.10521052968901465, "lm_q1q2_score": 0.05055141622169783}}
{"text": "/-\nCopyright (c) 2020 Scott Morrison. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Scott Morrison\n-/\nimport tactic.solve_by_elim\nimport tactic.interactive\n\nnamespace tactic\n\nnamespace hint\n\n/-- An attribute marking a `tactic unit` or `tactic string` which should be used by the `hint`\ntactic. -/\n@[user_attribute] meta def hint_tactic_attribute : user_attribute := {\n  name := `hint_tactic,\n  descr := \"A tactic that should be tried by `hint`.\"\n}\n\nadd_tactic_doc\n{ name                     := \"hint_tactic\",\n  category                 := doc_category.attr,\n  decl_names               := [`tactic.hint.hint_tactic_attribute],\n  tags                     := [\"rewrite\", \"search\"] }\n\nopen lean lean.parser interactive\n\nprivate meta def add_tactic_hint (n : name) (t : expr) : tactic unit :=\ndo\n  add_decl $ declaration.defn n [] `(tactic string) t reducibility_hints.opaque ff,\n  hint_tactic_attribute.set n () tt\n\n/--\n`add_hint_tactic t` runs the tactic `t` whenever `hint` is invoked.\nThe typical use case is `add_hint_tactic \"foo\"` for some interactive tactic `foo`.\n-/\n@[user_command] meta def add_hint_tactic (_ : parse (tk \"add_hint_tactic\")) : parser unit :=\ndo n \u2190 parser.pexpr,\n   e \u2190 to_expr n,\n   s \u2190 eval_expr string e,\n   let t := \"`[\" ++ s ++ \"]\",\n   (t, _) \u2190 with_input parser.pexpr t,\n   of_tactic $ do\n   let h := s <.> \"_hint\",\n   t \u2190 to_expr ``(do %%t, pure %%n),\n   add_tactic_hint h t.\n\nadd_tactic_doc\n{ name                     := \"add_hint_tactic\",\n  category                 := doc_category.cmd,\n  decl_names               := [`tactic.hint.add_hint_tactic],\n  tags                     := [\"search\"] }\n\nadd_hint_tactic \"refl\"\nadd_hint_tactic \"exact dec_trivial\"\nadd_hint_tactic \"assumption\"\n-- tidy does something better here: it suggests the actual \"intros X Y f\" string.\n-- perhaps add a wrapper?\nadd_hint_tactic \"intro\"\nadd_hint_tactic \"apply_auto_param\"\nadd_hint_tactic \"dsimp at *\"\nadd_hint_tactic \"simp at *\" -- TODO hook up to squeeze_simp?\nadd_hint_tactic \"fconstructor\"\nadd_hint_tactic \"injections_and_clear\"\nadd_hint_tactic \"solve_by_elim\"\nadd_hint_tactic \"unfold_coes\"\nadd_hint_tactic \"unfold_aux\"\n\nend hint\n\n/--\nReport a list of tactics that can make progress against the current goal,\nand for each such tactic, the number of remaining goals afterwards.\n-/\nmeta def hint : tactic (list (string \u00d7 \u2115)) :=\ndo\n  names \u2190 attribute.get_instances `hint_tactic,\n  focus1 $ try_all_sorted (names.reverse.map name_to_tactic)\n\nnamespace interactive\n\n/--\nReport a list of tactics that can make progress against the current goal.\n-/\nmeta def hint : tactic unit :=\ndo\n  hints \u2190 tactic.hint,\n  if hints.length = 0 then\n    fail \"no hints available\"\n  else do\n    t \u2190 hints.nth 0,\n    if t.2 = 0 then do\n      trace \"the following tactics solve the goal:\\n----\",\n      (hints.filter (\u03bb p : string \u00d7 \u2115, p.2 = 0)).mmap' (\u03bb p, tactic.trace format!\"Try this: {p.1}\")\n    else do\n      trace \"the following tactics make progress:\\n----\",\n      hints.mmap' (\u03bb p, tactic.trace format!\"Try this: {p.1}\")\n\n/--\n`hint` lists possible tactics which will make progress (that is, not fail) against the current goal.\n\n```lean\nexample {P Q : Prop} (p : P) (h : P \u2192 Q) : Q :=\nbegin\n  hint,\n  /- the following tactics make progress:\n     ----\n     Try this: solve_by_elim\n     Try this: finish\n     Try this: tauto\n  -/\n  solve_by_elim,\nend\n```\n\nYou can add a tactic to the list that `hint` tries by either using\n1. `attribute [hint_tactic] my_tactic`, if `my_tactic` is already of type `tactic string`\n(`tactic unit` is allowed too, in which case the printed string will be the name of the\ntactic), or\n2. `add_hint_tactic \"my_tactic\"`, specifying a string which works as an interactive tactic.\n-/\nadd_tactic_doc\n{ name        := \"hint\",\n  category    := doc_category.tactic,\n  decl_names  := [`tactic.interactive.hint],\n  tags        := [\"search\", \"Try this\"] }\n\nend interactive\n\nend tactic\n", "meta": {"author": "JLimperg", "repo": "aesop3", "sha": "a4a116f650cc7403428e72bd2e2c4cda300fe03f", "save_path": "github-repos/lean/JLimperg-aesop3", "path": "github-repos/lean/JLimperg-aesop3/aesop3-a4a116f650cc7403428e72bd2e2c4cda300fe03f/src/tactic/hint.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.3593641451601019, "lm_q2_score": 0.1403362512877889, "lm_q1q2_score": 0.05043181697900951}}
{"text": "/-\nCopyright (c) 2018 Simon Hudon. All rights reserved.\nReleased under Apache 2.0 license as described in the file LICENSE.\nAuthors: Simon Hudon\n\n! This file was ported from Lean 3 source module data.lazy_list.basic\n! leanprover-community/mathlib commit 4d392a6c9c4539cbeca399b3ee0afea398fbd2eb\n! Please do not edit these lines, except to modify the commit id\n! if you have ported upstream changes.\n-/\nimport Mathbin.Control.Traversable.Equiv\nimport Mathbin.Control.Traversable.Instances\nimport Mathbin.Data.LazyList\n\n/-!\n## Definitions on lazy lists\n\n> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.\n> Any changes to this file require a corresponding PR to mathlib4.\n\nThis file contains various definitions and proofs on lazy lists.\n\nTODO: move the `lazy_list.lean` file from core to mathlib.\n-/\n\n\nuniverse u\n\nnamespace Thunk\n\n/- warning: thunk.mk -> Thunk.pure is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}}, \u03b1 -> (Thunk\u2093.{u1} \u03b1)\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}}, \u03b1 -> (Thunk.{u1} \u03b1)\nCase conversion may be inaccurate. Consider using '#align thunk.mk Thunk.pure\u2093'. -/\n/-- Creates a thunk with a (non-lazy) constant value. -/\ndef pure {\u03b1} (x : \u03b1) : Thunk \u03b1 := fun _ => x\n#align thunk.mk Thunk.pure\n\ninstance {\u03b1 : Type u} [DecidableEq \u03b1] : DecidableEq (Thunk \u03b1)\n  | a, b =>\n    by\n    have : a = b \u2194 a () = b () := \u27e8by cc, by intro <;> ext x <;> cases x <;> assumption\u27e9\n    rw [this] <;> infer_instance\n\nend Thunk\n\nnamespace LazyList\n\nopen Function\n\n#print LazyList.listEquivLazyList /-\n/-- Isomorphism between strict and lazy lists. -/\ndef listEquivLazyList (\u03b1 : Type _) : List \u03b1 \u2243 LazyList \u03b1\n    where\n  toFun := LazyList.ofList\n  invFun := LazyList.toList\n  right_inv := by\n    intro\n    induction x\n    rfl\n    simp! [*]\n    ext\n    cases x\n    rfl\n  left_inv := by\n    intro\n    induction x\n    rfl\n    simp! [*]\n#align lazy_list.list_equiv_lazy_list LazyList.listEquivLazyList\n-/\n\ninstance {\u03b1 : Type u} [DecidableEq \u03b1] : DecidableEq (LazyList \u03b1)\n  | nil, nil => isTrue rfl\n  | cons x xs, cons y ys =>\n    if h : x = y then\n      match DecidableEq (xs ()) (ys ()) with\n      | is_false h2 => isFalse (by intro <;> cc)\n      | is_true h2 =>\n        have : xs = ys := by ext u <;> cases u <;> assumption\n        isTrue (by cc)\n    else isFalse (by intro <;> cc)\n  | nil, cons _ _ => isFalse (by cc)\n  | cons _ _, nil => isFalse (by cc)\n\n#print LazyList.traverse /-\n/-- Traversal of lazy lists using an applicative effect. -/\nprotected def traverse {m : Type u \u2192 Type u} [Applicative m] {\u03b1 \u03b2 : Type u} (f : \u03b1 \u2192 m \u03b2) :\n    LazyList \u03b1 \u2192 m (LazyList \u03b2)\n  | LazyList.nil => pure LazyList.nil\n  | LazyList.cons x xs => LazyList.cons <$> f x <*> Thunk.pure <$> traverse (xs ())\n#align lazy_list.traverse LazyList.traverse\n-/\n\ninstance : Traversable LazyList\n    where\n  map := @LazyList.traverse id _\n  traverse := @LazyList.traverse\n\ninstance : IsLawfulTraversable LazyList :=\n  by\n  apply Equiv.isLawfulTraversable' list_equiv_lazy_list <;> intros <;> skip <;> ext\n  \u00b7 induction x\n    rfl\n    simp! [Equiv.map, Functor.map] at *\n    simp [*]\n    rfl\n  \u00b7 induction x\n    rfl\n    simp! [Equiv.map, Functor.mapConst] at *\n    simp [*]\n    rfl\n  \u00b7 induction x\n    \u00b7 simp! [Traversable.traverse, Equiv.traverse, functor_norm]\n      rfl\n    simp! [Equiv.map, Functor.mapConst, Traversable.traverse] at *\n    rw [x_ih]\n    dsimp [list_equiv_lazy_list, Equiv.traverse, to_list, Traversable.traverse, List.traverse]\n    simp! [functor_norm]\n    rfl\n\n#print LazyList.init /-\n/-- `init xs`, if `xs` non-empty, drops the last element of the list.\nOtherwise, return the empty list. -/\ndef init {\u03b1} : LazyList \u03b1 \u2192 LazyList \u03b1\n  | LazyList.nil => LazyList.nil\n  | LazyList.cons x xs =>\n    let xs' := xs ()\n    match xs' with\n    | LazyList.nil => LazyList.nil\n    | LazyList.cons _ _ => LazyList.cons x (init xs')\n#align lazy_list.init LazyList.init\n-/\n\n#print LazyList.find /-\n/-- Return the first object contained in the list that satisfies\npredicate `p` -/\ndef find {\u03b1} (p : \u03b1 \u2192 Prop) [DecidablePred p] : LazyList \u03b1 \u2192 Option \u03b1\n  | nil => none\n  | cons h t => if p h then some h else find (t ())\n#align lazy_list.find LazyList.find\n-/\n\n#print LazyList.interleave /-\n/-- `interleave xs ys` creates a list where elements of `xs` and `ys` alternate. -/\ndef interleave {\u03b1} : LazyList \u03b1 \u2192 LazyList \u03b1 \u2192 LazyList \u03b1\n  | LazyList.nil, xs => xs\n  | a@(LazyList.cons x xs), LazyList.nil => a\n  | LazyList.cons x xs, LazyList.cons y ys =>\n    LazyList.cons x (LazyList.cons y (interleave (xs ()) (ys ())))\n#align lazy_list.interleave LazyList.interleave\n-/\n\n#print LazyList.interleaveAll /-\n/-- `interleave_all (xs::ys::zs::xss)` creates a list where elements of `xs`, `ys`\nand `zs` and the rest alternate. Every other element of the resulting list is taken from\n`xs`, every fourth is taken from `ys`, every eighth is taken from `zs` and so on. -/\ndef interleaveAll {\u03b1} : List (LazyList \u03b1) \u2192 LazyList \u03b1\n  | [] => LazyList.nil\n  | x :: xs => interleave x (interleave_all xs)\n#align lazy_list.interleave_all LazyList.interleaveAll\n-/\n\n#print LazyList.bind /-\n/-- Monadic bind operation for `lazy_list`. -/\nprotected def bind {\u03b1 \u03b2} : LazyList \u03b1 \u2192 (\u03b1 \u2192 LazyList \u03b2) \u2192 LazyList \u03b2\n  | LazyList.nil, _ => LazyList.nil\n  | LazyList.cons x xs, f => LazyList.append (f x) (bind (xs ()) f)\n#align lazy_list.bind LazyList.bind\n-/\n\n#print LazyList.reverse /-\n/-- Reverse the order of a `lazy_list`.\nIt is done by converting to a `list` first because reversal involves evaluating all\nthe list and if the list is all evaluated, `list` is a better representation for\nit than a series of thunks. -/\ndef reverse {\u03b1} (xs : LazyList \u03b1) : LazyList \u03b1 :=\n  ofList xs.toList.reverse\n#align lazy_list.reverse LazyList.reverse\n-/\n\ninstance : Monad LazyList where\n  pure := @LazyList.singleton\n  bind := @LazyList.bind\n\n/- warning: lazy_list.append_nil -> LazyList.append_nil is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}} (xs : LazyList.{u1} \u03b1), Eq.{succ u1} (LazyList.{u1} \u03b1) (LazyList.append.{u1} \u03b1 xs (fun (_ : Unit) => LazyList.nil.{u1} \u03b1)) xs\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}} (xs : LazyList.{u1} \u03b1), Eq.{succ u1} (LazyList.{u1} \u03b1) (LazyList.append.{u1} \u03b1 xs (Thunk.pure.{u1} (LazyList.{u1} \u03b1) (LazyList.nil.{u1} \u03b1))) xs\nCase conversion may be inaccurate. Consider using '#align lazy_list.append_nil LazyList.append_nil\u2093'. -/\ntheorem append_nil {\u03b1} (xs : LazyList \u03b1) : xs.append LazyList.nil = xs :=\n  by\n  induction xs; rfl\n  simp [LazyList.append, xs_ih]\n  ext; congr\n#align lazy_list.append_nil LazyList.append_nil\n\n/- warning: lazy_list.append_assoc -> LazyList.append_assoc is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}} (xs : LazyList.{u1} \u03b1) (ys : LazyList.{u1} \u03b1) (zs : LazyList.{u1} \u03b1), Eq.{succ u1} (LazyList.{u1} \u03b1) (LazyList.append.{u1} \u03b1 (LazyList.append.{u1} \u03b1 xs (fun (_ : Unit) => ys)) (fun (_ : Unit) => zs)) (LazyList.append.{u1} \u03b1 xs (fun (_ : Unit) => LazyList.append.{u1} \u03b1 ys (fun (_ : Unit) => zs)))\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}} (xs : LazyList.{u1} \u03b1) (ys : LazyList.{u1} \u03b1) (zs : LazyList.{u1} \u03b1), Eq.{succ u1} (LazyList.{u1} \u03b1) (LazyList.append.{u1} \u03b1 (LazyList.append.{u1} \u03b1 xs (Thunk.mk.{u1} (LazyList.{u1} \u03b1) (fun (x._@.Init.Core._hyg.266 : Unit) => ys))) (Thunk.mk.{u1} (LazyList.{u1} \u03b1) (fun (x._@.Init.Core._hyg.266 : Unit) => zs))) (LazyList.append.{u1} \u03b1 xs (Thunk.mk.{u1} (LazyList.{u1} \u03b1) (fun (x._@.Init.Core._hyg.266 : Unit) => LazyList.append.{u1} \u03b1 ys (Thunk.mk.{u1} (LazyList.{u1} \u03b1) (fun (x._@.Init.Core._hyg.266 : Unit) => zs)))))\nCase conversion may be inaccurate. Consider using '#align lazy_list.append_assoc LazyList.append_assoc\u2093'. -/\ntheorem append_assoc {\u03b1} (xs ys zs : LazyList \u03b1) :\n    (xs.append ys).append zs = xs.append (ys.append zs) := by induction xs <;> simp [append, *]\n#align lazy_list.append_assoc LazyList.append_assoc\n\n/- warning: lazy_list.append_bind -> LazyList.append_bind is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}} {\u03b2 : Type.{u2}} (xs : LazyList.{u1} \u03b1) (ys : Thunk\u2093.{u1} (LazyList.{u1} \u03b1)) (f : \u03b1 -> (LazyList.{u2} \u03b2)), Eq.{succ u2} (LazyList.{u2} \u03b2) (LazyList.bind.{u1, u2} \u03b1 \u03b2 (LazyList.append.{u1} \u03b1 xs ys) f) (LazyList.append.{u2} \u03b2 (LazyList.bind.{u1, u2} \u03b1 \u03b2 xs f) (fun (_ : Unit) => LazyList.bind.{u1, u2} \u03b1 \u03b2 (ys Unit.unit) f))\nbut is expected to have type\n  forall {\u03b1 : Type.{u2}} {\u03b2 : Type.{u1}} (xs : LazyList.{u2} \u03b1) (ys : Thunk.{u2} (LazyList.{u2} \u03b1)) (f : \u03b1 -> (LazyList.{u1} \u03b2)), Eq.{succ u1} (LazyList.{u1} \u03b2) (LazyList.bind.{u2, u1} \u03b1 \u03b2 (LazyList.append.{u2} \u03b1 xs ys) f) (LazyList.append.{u1} \u03b2 (LazyList.bind.{u2, u1} \u03b1 \u03b2 xs f) (Thunk.mk.{u1} (LazyList.{u1} \u03b2) (fun (x._@.Init.Core._hyg.266 : Unit) => LazyList.bind.{u2, u1} \u03b1 \u03b2 (Thunk.get.{u2} (LazyList.{u2} \u03b1) ys) f)))\nCase conversion may be inaccurate. Consider using '#align lazy_list.append_bind LazyList.append_bind\u2093'. -/\ntheorem append_bind {\u03b1 \u03b2} (xs : LazyList \u03b1) (ys : Thunk (LazyList \u03b1)) (f : \u03b1 \u2192 LazyList \u03b2) :\n    (@LazyList.append _ xs ys).bind f = (xs.bind f).append ((ys ()).bind f) := by\n  induction xs <;> simp [LazyList.bind, append, *, append_assoc, append, LazyList.bind]\n#align lazy_list.append_bind LazyList.append_bind\n\ninstance : LawfulMonad LazyList\n    where\n  pure_bind := by\n    intros\n    apply append_nil\n  bind_assoc := by\n    intros\n    dsimp [(\u00b7 >>= \u00b7)]\n    induction x <;> simp [LazyList.bind, append_bind, *]\n  id_map := by\n    intros\n    simp [(\u00b7 <$> \u00b7)]\n    induction x <;> simp [LazyList.bind, *, singleton, append]\n    ext \u27e8\u27e9; rfl\n\n/- warning: lazy_list.mfirst -> LazyList.mfirst\u2093 is a dubious translation:\nlean 3 declaration is\n  forall {m : Type.{u1} -> Type.{u2}} [_inst_1 : Alternative.{u1, u2} m] {\u03b1 : Type.{u3}} {\u03b2 : Type.{u1}}, (\u03b1 -> (m \u03b2)) -> (LazyList.{u3} \u03b1) -> (m \u03b2)\nbut is expected to have type\n  forall {m : Type.{u3} -> Type.{u2}} [_inst_1 : Alternative.{u3, u2} m] {\u03b1 : Type.{u1}} {\u03b2 : Type.{u3}}, (\u03b1 -> (m \u03b2)) -> (LazyList.{u1} \u03b1) -> (m \u03b2)\nCase conversion may be inaccurate. Consider using '#align lazy_list.mfirst LazyList.mfirst\u2093\u2093'. -/\n/-- Try applying function `f` to every element of a `lazy_list` and\nreturn the result of the first attempt that succeeds. -/\ndef mfirst {m} [Alternative m] {\u03b1 \u03b2} (f : \u03b1 \u2192 m \u03b2) : LazyList \u03b1 \u2192 m \u03b2\n  | nil => failure\n  | cons x xs => f x <|> mfirst (xs ())\n#align lazy_list.mfirst LazyList.mfirst\u2093\n\n#print LazyList.Mem /-\n/-- Membership in lazy lists -/\nprotected def Mem {\u03b1} (x : \u03b1) : LazyList \u03b1 \u2192 Prop\n  | LazyList.nil => False\n  | LazyList.cons y ys => x = y \u2228 mem (ys ())\n#align lazy_list.mem LazyList.Mem\n-/\n\ninstance {\u03b1} : Membership \u03b1 (LazyList \u03b1) :=\n  \u27e8LazyList.Mem\u27e9\n\n#print LazyList.Mem.decidable /-\ninstance Mem.decidable {\u03b1} [DecidableEq \u03b1] (x : \u03b1) : \u2200 xs : LazyList \u03b1, Decidable (x \u2208 xs)\n  | LazyList.nil => Decidable.false\n  | LazyList.cons y ys =>\n    if h : x = y then Decidable.isTrue (Or.inl h)\n    else decidable_of_decidable_of_iff (mem.decidable (ys ())) (by simp [*, (\u00b7 \u2208 \u00b7), LazyList.Mem])\n#align lazy_list.mem.decidable LazyList.Mem.decidable\n-/\n\n#print LazyList.mem_nil /-\n@[simp]\ntheorem mem_nil {\u03b1} (x : \u03b1) : x \u2208 @LazyList.nil \u03b1 \u2194 False :=\n  Iff.rfl\n#align lazy_list.mem_nil LazyList.mem_nil\n-/\n\n/- warning: lazy_list.mem_cons -> LazyList.mem_cons is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}} (x : \u03b1) (y : \u03b1) (ys : Thunk\u2093.{u1} (LazyList.{u1} \u03b1)), Iff (Membership.Mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.hasMem.{u1} \u03b1) x (LazyList.cons.{u1} \u03b1 y ys)) (Or (Eq.{succ u1} \u03b1 x y) (Membership.Mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.hasMem.{u1} \u03b1) x (ys Unit.unit)))\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}} (x : \u03b1) (y : \u03b1) (ys : Thunk.{u1} (LazyList.{u1} \u03b1)), Iff (Membership.mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.instMembershipLazyList.{u1} \u03b1) x (LazyList.cons.{u1} \u03b1 y ys)) (Or (Eq.{succ u1} \u03b1 x y) (Membership.mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.instMembershipLazyList.{u1} \u03b1) x (Thunk.get.{u1} (LazyList.{u1} \u03b1) ys)))\nCase conversion may be inaccurate. Consider using '#align lazy_list.mem_cons LazyList.mem_cons\u2093'. -/\n@[simp]\ntheorem mem_cons {\u03b1} (x y : \u03b1) (ys : Thunk (LazyList \u03b1)) :\n    x \u2208 @LazyList.cons \u03b1 y ys \u2194 x = y \u2228 x \u2208 ys () :=\n  Iff.rfl\n#align lazy_list.mem_cons LazyList.mem_cons\n\n/- warning: lazy_list.forall_mem_cons -> LazyList.forall_mem_cons is a dubious translation:\nlean 3 declaration is\n  forall {\u03b1 : Type.{u1}} {p : \u03b1 -> Prop} {a : \u03b1} {l : Thunk\u2093.{u1} (LazyList.{u1} \u03b1)}, Iff (forall (x : \u03b1), (Membership.Mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.hasMem.{u1} \u03b1) x (LazyList.cons.{u1} \u03b1 a l)) -> (p x)) (And (p a) (forall (x : \u03b1), (Membership.Mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.hasMem.{u1} \u03b1) x (l Unit.unit)) -> (p x)))\nbut is expected to have type\n  forall {\u03b1 : Type.{u1}} {p : \u03b1 -> Prop} {a : \u03b1} {l : Thunk.{u1} (LazyList.{u1} \u03b1)}, Iff (forall (x : \u03b1), (Membership.mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.instMembershipLazyList.{u1} \u03b1) x (LazyList.cons.{u1} \u03b1 a l)) -> (p x)) (And (p a) (forall (x : \u03b1), (Membership.mem.{u1, u1} \u03b1 (LazyList.{u1} \u03b1) (LazyList.instMembershipLazyList.{u1} \u03b1) x (Thunk.get.{u1} (LazyList.{u1} \u03b1) l)) -> (p x)))\nCase conversion may be inaccurate. Consider using '#align lazy_list.forall_mem_cons LazyList.forall_mem_cons\u2093'. -/\ntheorem forall_mem_cons {\u03b1} {p : \u03b1 \u2192 Prop} {a : \u03b1} {l : Thunk (LazyList \u03b1)} :\n    (\u2200 x \u2208 @LazyList.cons _ a l, p x) \u2194 p a \u2227 \u2200 x \u2208 l (), p x := by\n  simp only [Membership.Mem, LazyList.Mem, or_imp, forall_and, forall_eq]\n#align lazy_list.forall_mem_cons LazyList.forall_mem_cons\n\n/-! ### map for partial functions -/\n\n\n#print LazyList.pmap /-\n/-- Partial map. If `f : \u03a0 a, p a \u2192 \u03b2` is a partial function defined on\n  `a : \u03b1` satisfying `p`, then `pmap f l h` is essentially the same as `map f l`\n  but is defined only when all members of `l` satisfy `p`, using the proof\n  to apply `f`. -/\n@[simp]\ndef pmap {\u03b1 \u03b2} {p : \u03b1 \u2192 Prop} (f : \u2200 a, p a \u2192 \u03b2) : \u2200 l : LazyList \u03b1, (\u2200 a \u2208 l, p a) \u2192 LazyList \u03b2\n  | LazyList.nil, H => LazyList.nil\n  | LazyList.cons x xs, H =>\n    LazyList.cons (f x (forall_mem_cons.1 H).1) (pmap (xs ()) (forall_mem_cons.1 H).2)\n#align lazy_list.pmap LazyList.pmap\n-/\n\n#print LazyList.attach /-\n/-- \"Attach\" the proof that the elements of `l` are in `l` to produce a new `lazy_list`\n  with the same elements but in the type `{x // x \u2208 l}`. -/\ndef attach {\u03b1} (l : LazyList \u03b1) : LazyList { x // x \u2208 l } :=\n  pmap Subtype.mk l fun a => id\n#align lazy_list.attach LazyList.attach\n-/\n\ninstance {\u03b1} [Repr \u03b1] : Repr (LazyList \u03b1) :=\n  \u27e8fun xs => repr xs.toList\u27e9\n\nend LazyList\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/Data/LazyList/Basic.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.46490158620112276, "lm_q2_score": 0.10818894737344457, "lm_q1q2_score": 0.05029721324334418}}
{"text": "import tactic.basic\n\nnamespace basics\n\n/-\nInductive day : Type :=\n  | monday\n  | tuesday\n  | wednesday\n  | thursday\n  | friday\n  | saturday\n  | sunday.\n-/\n\ninductive day : Type\n| monday\n| tuesday\n| wednesday\n| thursday\n| friday\n| saturday\n| sunday\n\n/- add open because coq puts constructors in scope -/\nopen day\n\n/-\nDefinition next_weekday (d:day) : day :=\n  match d with\n  | monday \u21d2 tuesday\n  | tuesday \u21d2 wednesday\n  | wednesday \u21d2 thursday\n  | thursday \u21d2 friday\n  | friday \u21d2 monday\n  | saturday \u21d2 monday\n  | sunday \u21d2 monday\n  end.\n-/\n\ndef next_weekday : day \u2192 day\n| monday := tuesday\n| tuesday := wednesday\n| wednesday := thursday\n| thursday := friday\n| _ := monday\n\n/-\nCompute (next_weekday friday).\n(* ==> monday : day *)\nCompute (next_weekday (next_weekday saturday)).\n(* ==> tuesday : day *)\n-/\n\n#reduce next_weekday friday\n#reduce next_weekday (next_weekday saturday)\n\n/-\nExample test_next_weekday:\n  (next_weekday (next_weekday saturday)) = tuesday.\nProof. simpl. reflexivity. Qed.\n-/\n\nexample : next_weekday (next_weekday saturday) = tuesday := rfl\n\n/-\nInductive bool : Type :=\n  | true\n  | false.\n-/\n\n/- tt/ff are always in scope and the compiler will pick them over your impl -/\nnamespace myBool\n\ninductive bool : Type\n| tt\n| ff\n\nend myBool\n\n/-\nDefinition negb (b:bool) : bool :=\n  match b with\n  | true \u21d2 false\n  | false \u21d2 true\n  end.\nDefinition andb (b1:bool) (b2:bool) : bool :=\n  match b1 with\n  | true \u21d2 b2\n  | false \u21d2 false\n  end.\nDefinition orb (b1:bool) (b2:bool) : bool :=\n  match b1 with\n  | true \u21d2 true\n  | false \u21d2 b2\n  end.\n-/\n\ndef bnot : bool \u2192 bool\n| tt := ff\n| _ := tt\n\ndef band : bool \u2192 bool \u2192 bool\n| tt b\u2082 := b\u2082\n| _ _ := ff\n\ndef bor : bool \u2192 bool \u2192 bool\n| ff b\u2082 := b\u2082\n| _ _ := tt\n\n/-\nExample test_orb1: (orb true false) = true.\nProof. simpl. reflexivity. Qed.\nExample test_orb2: (orb false false) = false.\nProof. simpl. reflexivity. Qed.\nExample test_orb3: (orb false true) = true.\nProof. simpl. reflexivity. Qed.\nExample test_orb4: (orb true true) = true.\nProof. simpl. reflexivity. Qed.\n-/\n\nexample : bor tt ff = tt := rfl\nexample : bor ff ff = ff := rfl\nexample : bor ff tt = tt := rfl\nexample : bor tt tt = tt := rfl\n\n/-\nNotation \"x && y\" := (andb x y).\nNotation \"x || y\" := (orb x y).\nExample test_orb5: false || false || true = true.\nProof. simpl. reflexivity. Qed.\n-/\n\nlocal infix && := band\nlocal infix || := bor\n\nexample : ff || ff || tt = tt := rfl\n\n/-\nDefinition nandb (b1:bool) (b2:bool) : bool\n  (* REPLACE THIS LINE WITH \":= _your_definition_ .\" *). Admitted.\nExample test_nandb1: (nandb true false) = true.\n(* FILL IN HERE *) Admitted.\nExample test_nandb2: (nandb false false) = true.\n(* FILL IN HERE *) Admitted.\nExample test_nandb3: (nandb false true) = true.\n(* FILL IN HERE *) Admitted.\nExample test_nandb4: (nandb true true) = false.\n(* FILL IN HERE *) Admitted.\n-/\n\ndef bnand : bool \u2192 bool \u2192 bool\n| tt tt := ff\n| _ _ := tt\n\nexample : bnand tt ff = tt := rfl\nexample : bnand ff ff = tt := rfl\nexample : bnand ff tt = tt := rfl\nexample : bnand tt tt = ff := rfl\n\n/-\nDefinition andb3 (b1:bool) (b2:bool) (b3:bool) : bool\n  (* REPLACE THIS LINE WITH \":= _your_definition_ .\" *). Admitted.\nExample test_andb31: (andb3 true true true) = true.\n(* FILL IN HERE *) Admitted.\nExample test_andb32: (andb3 false true true) = false.\n(* FILL IN HERE *) Admitted.\nExample test_andb33: (andb3 true false true) = false.\n(* FILL IN HERE *) Admitted.\nExample test_andb34: (andb3 true true false) = false.\n(* FILL IN HERE *) Admitted.\n-/\n\ndef band3 (b\u2081 : bool) (b\u2082 : bool) (b\u2083 : bool) : bool :=\n  band b\u2081 (band b\u2082 b\u2083)\n\nexample : band3 tt tt tt = tt := rfl\nexample : band3 ff tt tt = ff := rfl\nexample : band3 tt ff tt = ff := rfl\nexample : band3 tt tt ff = ff := rfl\n\n/-\nCheck true.\n(* ===> true : bool *)\nCheck (negb true).\n(* ===> negb true : bool *)\n-/\n\n#check tt\n#check bnot tt\n\n/-\nCheck negb.\n(* ===> negb : bool -> bool *)\n-/\n\n#check bnot\n\n/-\nInductive rgb : Type :=\n  | red\n  | green\n  | blue.\nInductive color : Type :=\n  | black\n  | white\n  | primary (p : rgb).\n-/\n\ninductive rgb : Type\n| red\n| green\n| blue\n\ninductive color : Type\n| black\n| white\n| primary (p : rgb)\n\nopen rgb\nopen color\n\n/-\nDefinition monochrome (c : color) : bool :=\n  match c with\n  | black \u21d2 true\n  | white \u21d2 true\n  | primary q \u21d2 false\n  end.\n-/\n\ndef monochrome : color \u2192 bool\n| black := tt\n| white := tt\n| _ := ff\n\n/-\nDefinition isred (c : color) : bool :=\n  match c with\n  | black \u21d2 false\n  | white \u21d2 false\n  | primary red \u21d2 true\n  | primary _ \u21d2 false\n  end.\n-/\n\ndef isred : color \u2192 bool\n| (primary red) := tt\n| _ := false\n\n/-\nInductive bit : Type :=\n  | B0\n  | B1.\nInductive nybble : Type :=\n  | bits (b0 b1 b2 b3 : bit).\nCheck (bits B1 B0 B1 B0).\n(* ==> bits B1 B0 B1 B0 : nybble *)\n-/\n\ninductive bit : Type\n| B\u2080\n| B\u2081\n\nopen bit\n\ninductive nybble : Type\n| bits (b\u2080 b\u2081 b\u2082 b\u2083 : bit)\n\nopen nybble\n\n#check bits B\u2081 B\u2080 B\u2081 B\u2080\n\n/-\nDefinition all_zero (nb : nybble) : bool :=\n  match nb with\n    | (bits B0 B0 B0 B0) \u21d2 true\n    | (bits _ _ _ _) \u21d2 false\n  end.\nCompute (all_zero (bits B1 B0 B1 B0)).\n(* ===> false : bool *)\nCompute (all_zero (bits B0 B0 B0 B0)).\n(* ===> true : bool *)\n-/\n\ndef all_zero : nybble \u2192 bool\n| (bits B\u2080 B\u2080 B\u2080 B\u2080) := tt\n| _ := ff\n\n/-\nModule NatPlayground.\n-/\n\nnamespace myNat\n\n/-\nInductive nat : Type :=\n  | O\n  | S (n : nat).\n-/\n\ninductive nat : Type\n| zero\n| succ (n : nat)\n\nend myNat\n\n/-\nInductive nat' : Type :=\n  | stop\n  | tick (foo : nat').\n-/\n\ninductive nat' : Type\n| stop\n| tick (foo : nat')\n\n/-\nDefinition pred (n : nat) : nat :=\n  match n with\n    | O \u21d2 O\n    | S n' \u21d2 n'\n  end.\n-/\n\nopen nat (succ zero)\n\ndef pred : \u2115 \u2192 \u2115\n| zero := zero\n| (succ n) := n\n\n/-\nCheck (S (S (S (S O)))).\n  (* ===> 4 : nat *)\n\nDefinition minustwo (n : nat) : nat :=\n  match n with\n    | O \u21d2 O\n    | S O \u21d2 O\n    | S (S n') \u21d2 n'\n  end.\n\nCompute (minustwo 4).\n  (* ===> 2 : nat *)\n-/\n\n#check succ (succ (succ (succ zero)))\n\ndef sub_two : \u2115 \u2192 \u2115\n| (succ (succ n)) := n\n| _ := zero\n\n#reduce sub_two 4\n\n/-\nCheck S.\nCheck pred.\nCheck minustwo.\n-/\n\n#check succ\n#check pred\n#check sub_two\n\n/-\nFixpoint evenb (n:nat) : bool :=\n  match n with\n  | O \u21d2 true\n  | S O \u21d2 false\n  | S (S n') \u21d2 evenb n'\n  end.\n-/\n\n/- recursion just works with this form -/\ndef evenb : \u2115 \u2192 bool\n| 0 := tt\n| 1 := ff\n| (succ (succ n)) := evenb n\n\n/-\nDefinition oddb (n:nat) : bool := negb (evenb n).\nExample test_oddb1: oddb 1 = true.\nProof. simpl. reflexivity. Qed.\nExample test_oddb2: oddb 4 = false.\nProof. simpl. reflexivity. Qed.\n-/\n\ndef oddb := bnot \u2218 evenb\n\nexample : oddb 1 = tt := rfl\nexample : oddb 4 = ff := rfl\n\n/-\nModule NatPlayground2.\nFixpoint plus (n : nat) (m : nat) : nat :=\n  match n with\n    | O \u21d2 m\n    | S n' \u21d2 S (plus n' m)\n  end.\n-/\n\ndef add : \u2115 \u2192 \u2115 \u2192 \u2115\n| 0 m := m\n| (n + 1) m := (add n m) + 1\n\n/-\nCompute (plus 3 2).\n-/\n\n#reduce add 3 2\n\n/-\nFixpoint mult (n m : nat) : nat :=\n  match n with\n    | O \u21d2 O\n    | S n' \u21d2 plus m (mult n' m)\n  end.\nExample test_mult1: (mult 3 3) = 9.\nProof. simpl. reflexivity. Qed.\n-/\n\n/- match doesn't work with recursion -/\ndef mul : \u2115 \u2192 \u2115 \u2192 \u2115\n| 0 m := 0\n| (n + 1) m := add m (mul n m)\n\n/-\nFixpoint minus (n m:nat) : nat :=\n  match n, m with\n  | O , _ \u21d2 O\n  | S _ , O \u21d2 n\n  | S n', S m' \u21d2 minus n' m'\n  end.\n\nEnd NatPlayground2.\n\nFixpoint exp (base power : nat) : nat :=\n  match power with\n    | O \u21d2 S O\n    | S p \u21d2 mult base (exp base p)\n  end.\n-/\n\ndef sub : \u2115 \u2192 \u2115 \u2192 \u2115\n| 0 _ := 0\n| n 0 := n\n| (n + 1) (m + 1) := sub n m\n\n/- left of the colon is fixed in recursive calls -/\ndef exp (base : \u2115) : \u2115 \u2192 \u2115\n| 0 := 1\n| (p + 1) := mul base (exp p)\n\n/-\nFixpoint factorial (n:nat) : nat\n  (* REPLACE THIS LINE WITH \":= _your_definition_ .\" *). Admitted.\n\nExample test_factorial1: (factorial 3) = 6.\n(* FILL IN HERE *) Admitted.\n\nExample test_factorial2: (factorial 5) = (mult 10 12).\n(* FILL IN HERE *) Admitted.\n-/\n\ndef factorial : \u2115 \u2192 \u2115\n| 0 := 1\n| (n + 1) := mul (n + 1) (factorial n)\n\nexample : factorial 3 = 6 := rfl\nexample : factorial 5 = mul 10 12 := rfl\n\n/-\nNotation \"x + y\" := (plus x y)\n                       (at level 50, left associativity)\n                       : nat_scope.\nNotation \"x - y\" := (minus x y)\n                       (at level 50, left associativity)\n                       : nat_scope.\nNotation \"x * y\" := (mult x y)\n                       (at level 40, left associativity)\n                       : nat_scope.\nCheck ((0 + 1) + 1).\n-/\n\n/- actual definitions use type classes -/\n/- this breaks the ability to pattern match on + 1 instead of succ -/\nlocal infixl + := add\nlocal infixl - := sub\nlocal infixl * := mul\n\n#check ((0 + 1) + 1)\n\n/-\nFixpoint eqb (n m : nat) : bool :=\n  match n with\n  | O \u21d2 match m with\n         | O \u21d2 true\n         | S m' \u21d2 false\n         end\n  | S n' \u21d2 match m with\n            | O \u21d2 false\n            | S m' \u21d2 eqb n' m'\n            end\n  end.\n-/\n\ndef eqb : \u2115 \u2192 \u2115 \u2192 bool\n| 0 0 := tt\n| 0 (succ m) := ff\n| _ 0 := ff\n| (succ n) (succ m) := eqb n m\n\n/-\nFixpoint leb (n m : nat) : bool :=\n  match n with\n  | O \u21d2 true\n  | S n' \u21d2\n      match m with\n      | O \u21d2 false\n      | S m' \u21d2 leb n' m'\n      end\n  end.\n\nExample test_leb1: (leb 2 2) = true.\nProof. simpl. reflexivity. Qed.\n\nExample test_leb2: (leb 2 4) = true.\nProof. simpl. reflexivity. Qed.\n\nExample test_leb3: (leb 4 2) = false.\nProof. simpl. reflexivity. Qed.\n-/\n\n/-\nTODO - examine why wildcard on left breaks things\n-/\ndef leb : \u2115 \u2192 \u2115 \u2192 bool\n| 0 _ := tt\n| (succ n) 0 := ff\n| (succ n) (succ m) := leb n m\n\nexample : leb 2 2 = tt := rfl\nexample : leb 0 2 = tt := rfl\nexample : leb 4 2 = ff := rfl\n\n/-\nNotation \"x =? y\" := (eqb x y) (at level 70) : nat_scope.\nNotation \"x <=? y\" := (leb x y) (at level 70) : nat_scope.\n\nExample test_leb3': (4 <=? 2) = false.\nProof. simpl. reflexivity. Qed.\n-/\n\ninfix ` =? `:50 := eqb\ninfix ` \u2264? `:50 := leb\n\nexample : (4 \u2264? 2) = ff := rfl\n\n/-\nDefinition ltb (n m : nat) : bool\n  (* REPLACE THIS LINE WITH \":= _your_definition_ .\" *). Admitted.\n\nNotation \"x <? y\" := (ltb x y) (at level 70) : nat_scope.\n\nExample test_ltb1: (ltb 2 2) = false.\n(* FILL IN HERE *) Admitted.\n\nExample test_ltb2: (ltb 2 4) = true.\n(* FILL IN HERE *) Admitted.\n\nExample test_ltb3: (ltb 4 2) = false.\n(* FILL IN HERE *) Admitted.\n-/\n\ndef ltb : \u2115 \u2192 \u2115 \u2192 bool\n| _ 0 := ff\n| 0 (succ m) := tt\n| (succ n) (succ m) := ltb n m\n\ninfix ` <? `:50 := ltb\n\nexample : ltb 2 2 = ff := rfl\nexample : ltb 2 4 = tt := rfl\nexample : ltb 4 2 = ff := rfl\n\n/-\nTheorem plus_O_n : \u2200n : nat, 0 + n = n.\nProof.\n  intros n. simpl. reflexivity. Qed.\n-/\n\ntheorem zero_add (n : \u2115) : 0 + n = n := by refl\n\n/-\nTheorem plus_O_n' : \u2200n : nat, 0 + n = n.\nProof.\n  intros n. reflexivity. Qed.\n-/\n\ntheorem zero_add' (n : \u2115) : 0 + n = n := rfl\n\n/-\nTheorem plus_1_l : \u2200n:nat, 1 + n = S n.\nProof.\n  intros n. reflexivity. Qed.\nTheorem mult_0_l : \u2200n:nat, 0 * n = 0.\nProof.\n  intros n. reflexivity. Qed.\n-/\n\ntheorem one_plus (n : \u2115) : 1 + n = succ n := rfl\ntheorem zero_mul (n : \u2115) : 0 * n = 0 := rfl\n\n/-\nTheorem plus_id_example : \u2200n m:nat,\n  n = m \u2192\n  n + n = m + m.\nProof.\n  (* move both quantifiers into the context: *)\n  intros n m.\n  (* move the hypothesis into the context: *)\n  intros H.\n  (* rewrite the goal using the hypothesis: *)\n  rewrite \u2192 H.\n  reflexivity. Qed.\n-/\n\nexample (n m : \u2115) (h : n = m) : n + n = m + m := by rw h\n\n/-\nTheorem plus_id_exercise : \u2200n m o : nat,\n  n = m \u2192 m = o \u2192 n + m = m + o.\nProof.\n  (* FILL IN HERE *) Admitted.\n-/\n\ntheorem add_id (n m o : \u2115) (hnm : n = m) (hmo : m = o) :\n  n + m = m + o := by rw [hnm, hmo]\n\n/-\nTheorem mult_0_plus : \u2200n m : nat,\n  (0 + n) * m = n * m.\nProof.\n  intros n m.\n  rewrite \u2192 plus_O_n.\n  reflexivity. Qed.\n-/\n\ntheorem zero_add_mul (n m : \u2115) : (0 + n) * m = n * m :=\n  by rw zero_add\n\n/-\nTheorem mult_S_1 : \u2200n m : nat,\n  m = S n \u2192\n  m * (1 + n) = m * m.\nProof.\n  (* FILL IN HERE *) Admitted.\n  (* (N.b. This proof can actually be completed with tactics other than\n     rewrite, but please do use rewrite for the sake of the exercise.) *)\n-/\n\ntheorem mul_one_add (n m : \u2115) (h : m = succ n)\n  : m * (1 + n) = m * m := by rw [one_plus, \u2190h]\n\n/-\nTheorem plus_1_neq_0_firsttry : \u2200n : nat,\n  (n + 1) =? 0 = false.\nProof.\n  intros n.\n  simpl. (* does nothing! *)\nAbort.\n-/\n\n/- sorry is reported when importing as well, so commenting this out -/\n/-\ntheorem plus_one_neq_zero_firsttry (n : \u2115) : (n + 1) =? 0 = ff :=\nbegin\n  try { simp },\n  sorry\nend\n-/\n\n/-\nTheorem plus_1_neq_0 : \u2200n : nat,\n  (n + 1) =? 0 = false.\nProof.\n  intros n. destruct n as [| n'] eqn:E.\n  - reflexivity.\n  - reflexivity. Qed.\n-/\n\ntheorem add_one_neq_zero (n : \u2115) : (n + 1) =? 0 = ff :=\nbegin\n  cases n,\n    refl,\n  refl,\nend\n\n/-\nTheorem negb_involutive : \u2200b : bool,\n  negb (negb b) = b.\nProof.\n  intros b. destruct b eqn:E.\n  - reflexivity.\n  - reflexivity. Qed.\n-/\n\ntheorem bnot_involutive (b : bool) : bnot (bnot b) = b :=\nbegin\n  cases b,\n    refl,\n  refl,\nend\n\n/-\nTheorem andb_commutative : \u2200b c, andb b c = andb c b.\nProof.\n  intros b c. destruct b eqn:Eb.\n  - destruct c eqn:Ec.\n    + reflexivity.\n    + reflexivity.\n  - destruct c eqn:Ec.\n    + reflexivity.\n    + reflexivity.\nQed.\n-/\n\ntheorem band_commutative (b c : bool) : band b c = band c b :=\nbegin\n  cases b,\n    cases c,\n      refl,\n    refl,\n  cases c,\n    refl,\n  refl,\nend\n\n/-\nTheorem andb_commutative' : \u2200b c, andb b c = andb c b.\nProof.\n  intros b c. destruct b eqn:Eb.\n  { destruct c eqn:Ec.\n    { reflexivity. }\n    { reflexivity. } }\n  { destruct c eqn:Ec.\n    { reflexivity. }\n    { reflexivity. } }\nQed.\n-/\n\ntheorem band_commutative' (b c : bool) : band b c = band c b :=\nbegin\ncases b,\ncase ff {\n  cases c,\n  case ff { refl, },\n  case tt { refl, }\n},\ncase tt {\n  cases c,\n  case ff { refl, },\n  case tt { refl, },\n},\nend\n\n/-\nTheorem andb3_exchange :\n  \u2200b c d, andb (andb b c) d = andb (andb b d) c.\nProof.\n  intros b c d. destruct b eqn:Eb.\n  - destruct c eqn:Ec.\n    { destruct d eqn:Ed.\n      - reflexivity.\n      - reflexivity. }\n    { destruct d eqn:Ed.\n      - reflexivity.\n      - reflexivity. }\n  - destruct c eqn:Ec.\n    { destruct d eqn:Ed.\n      - reflexivity.\n      - reflexivity. }\n    { destruct d eqn:Ed.\n      - reflexivity.\n      - reflexivity. }\nQed.\n-/\n\n/- this is horrible without combinators or automation -/\ntheorem band3_exchange (b c d : bool)\n  : band (band b c) d = band (band b d) c :=\nbegin\n  cases b,\n  case ff {\n    cases c,\n    case ff {\n      cases d,\n      case ff { refl, },\n      case tt { refl, },\n    },\n    case tt {\n      cases d,\n      case ff { refl, },\n      case tt { refl, },\n    },\n  },\n  case tt {\n    cases c,\n    case ff {\n      cases d,\n      case ff { refl, },\n      case tt { refl, },\n    },\n    case tt {\n      cases d,\n      case ff { refl, },\n      case tt { refl, },\n    },\n  }\nend\n\n/-\nTheorem plus_1_neq_0' : \u2200n : nat,\n  (n + 1) =? 0 = false.\nProof.\n  intros [|n].\n  - reflexivity.\n  - reflexivity. Qed.\n\nTheorem andb_commutative'' :\n  \u2200b c, andb b c = andb c b.\nProof.\n  intros [] [].\n  - reflexivity.\n  - reflexivity.\n  - reflexivity.\n  - reflexivity.\nQed.\n-/\n\n/-\nnb. requires mathlib\nTODO: rintro doesn't have case labels\n-/\ntheorem add_one_neq_zero' : \u2200n, n + 1 =? 0 = ff :=\nbegin\n  rintro \u27e8n\u27e9,\n    refl,\n  refl,\nend\n\ntheorem band_commutative'' : \u2200b c, band b c = band c b :=\nbegin\n  rintro \u27e8b\u27e9 \u27e8c\u27e9,\n        refl,\n      refl,\n    refl,\n  refl,\nend\n\n/-\nTheorem andb_true_elim2 : \u2200b c : bool,\n  andb b c = true \u2192 c = true.\nProof.\n  (* FILL IN HERE *) Admitted.\n-/\n\ntheorem band_true_elim2 (b c : bool) (h : band b c = tt)\n  : c = tt :=\nbegin\n  cases c,\n    rw \u2190h,\n    cases b,\n      refl,\n    refl,\n  refl,\nend\n\n/-\nTheorem zero_nbeq_plus_1 : \u2200n : nat,\n  0 =? (n + 1) = false.\nProof.\n  (* FILL IN HERE *) Admitted.\n-/\n\ntheorem zero_nbeq_plus_one (n : \u2115) : 0 =? (n + 1) = ff :=\nbegin\n  cases n,\n    refl,\n  refl,\nend\n\n/-\nTheorem identity_fn_applied_twice :\n  \u2200(f : bool \u2192 bool),\n  (\u2200(x : bool), f x = x) \u2192\n  \u2200(b : bool), f (f b) = b.\nProof.\n  (* FILL IN HERE *) Admitted.\n-/\n\ntheorem identity_fn_applied_twice\n  (f : bool \u2192 bool)\n  (h : \u2200 x : bool, f x = x)\n  (b : bool) : f (f b) = b := by rw [h, h]\n\ntheorem negation_fn_applied_twice\n  (f : bool \u2192 bool)\n  (h : \u2200 x : bool, f x = bnot x)\n  (b : bool) : f (f b) = b :=\nbegin\n  rw [h, h],\n  rw bnot_involutive,\nend\n\n/-\nTheorem andb_eq_orb :\n  \u2200(b c : bool),\n  (andb b c = orb b c) \u2192\n  b = c.\nProof.\n  (* FILL IN HERE *) Admitted.\n-/\n\n/-\nthis is so much worse wihout at\nTODO: revisit idea of intro in the type def\n-/\ntheorem band_eq_bor (b c : bool) : band b c = bor b c \u2192 b = c :=\nbegin\n  cases b,\n    cases c,\n      intro h,\n      refl,\n    rw [band, bor],\n    intro h,\n    rw h,\n  rw [band, bor],\n  intro h,\n  rw h,\nend\n\n/-\nInductive bin : Type :=\n  | Z\n  | A (n : bin)\n  | B (n : bin).\n-/\n\ninductive bin : Type\n| Z\n| A (n : bin)\n| B (n : bin)\n\nopen bin\n\n/-\nFixpoint incr (m:bin) : bin\n  (* REPLACE THIS LINE WITH \":= _your_definition_ .\" *). Admitted.\n\nFixpoint bin_to_nat (m:bin) : nat\n  (* REPLACE THIS LINE WITH \":= _your_definition_ .\" *). Admitted.\n-/\n\ndef incr : bin \u2192 bin\n| Z := B Z\n| (A b) := B b\n| (B b) := A (incr b)\n\ndef bin_to_nat : bin \u2192 \u2115\n| Z := 0\n| (A b) := 2 * bin_to_nat b\n| (B b) := 2 * bin_to_nat b + 1\n\ndef nat_to_bin : \u2115 \u2192 bin\n| 0 := Z\n| (succ n) := incr (nat_to_bin n)\n\nexample : bin_to_nat (nat_to_bin 127) = 127 := rfl\n\nend basics", "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/sf/v1/ch01_basics.lean", "lm_name": "Qwen/Qwen-72B", "lm_label": "1. NO\n2. NO", "lm_q1_score": 0.437823499114202, "lm_q2_score": 0.11436851561661297, "lm_q1q2_score": 0.050073223695762746}}
