/-
Copyright (c) 2019 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import tactic.protected
import tactic.to_additive

/-!
# simps attribute

This file defines the `@[simps]` attribute, to automatically generate `simp` lemmas
reducing a definition when projections are applied to it.

## Implementation Notes

There are three attributes being defined here
* `@[simps]` is the attribute for objects of a structure or instances of a class. It will
  automatically generate simplification lemmas for each projection of the object/instance that
  contains data. See the doc strings for `simps_attr` and `simps_cfg` for more details and
  configuration options.
* `@[_simps_str]` is automatically added to structures that have been used in `@[simps]` at least
  once. This attribute contains the data of the projections used for this structure by all following
  invocations of `@[simps]`.
* `@[notation_class]` should be added to all classes that define notation, like `has_mul` and
  `has_zero`. This specifies that the projections that `@[simps]` used are the projections from
  these notation classes instead of the projections of the superclasses.
  Example: if `has_mul` is tagged with `@[notation_class]` then the projection used for `semigroup`
  will be `λ α hα, @has_mul.mul α (@semigroup.to_has_mul α hα)` instead of `@semigroup.mul`.

## Tags

structures, projections, simp, simplifier, generates declarations
-/

open tactic expr option sum

setup_tactic_parser
declare_trace simps.verbose
declare_trace simps.debug

/--
Projection data for a single projection of a structure, consisting of the following fields:
- the name used in the generated `simp` lemmas
- an expression used by simps for the projection. It must be definitionally equal to an original
  projection (or a composition of multiple projections).
  These expressions can contain the universe parameters specified in the first argument of
  `simps_str_attr`.
- a list of natural numbers, which is the projection number(s) that have to be applied to the
  expression. For example the list `[0, 1]` corresponds to applying the first projection of the
  structure, and then the second projection of the resulting structure (this assumes that the
  target of the first projection is a structure with at least two projections).
  The composition of these projections is required to be definitionally equal to the provided
  expression.
- A boolean specifying whether `simp` lemmas are generated for this projection by default.
- A boolean specifying whether this projection is written as prefix.
-/
@[protect_proj, derive [has_reflect, inhabited]]
meta structure projection_data :=
(name : name)
(expr : expr)
(proj_nrs : list ℕ)
(is_default : bool)
(is_prefix : bool)

/-- Temporary projection data parsed from `initialize_simps_projections` before the expression
  matching this projection has been found. Only used internally in `simps_get_raw_projections`. -/
meta structure parsed_projection_data :=
(orig_name : name) -- name for this projection used in the structure definition
(new_name : name) -- name for this projection used in the generated `simp` lemmas
(is_default : bool)
(is_prefix : bool)

section
open format
meta instance : has_to_tactic_format projection_data :=
⟨λ ⟨a, b, c, d, e⟩, (λ x, group $ nest 1 $ to_fmt "⟨"  ++ to_fmt a ++ to_fmt "," ++ line ++ x ++
  to_fmt "," ++ line ++ to_fmt c ++ to_fmt "," ++ line ++ to_fmt d ++ to_fmt "," ++ line ++
  to_fmt e ++ to_fmt "⟩") <$> pp b⟩

meta instance : has_to_format parsed_projection_data :=
⟨λ ⟨a, b, c, d⟩, group $ nest 1 $ to_fmt "⟨"  ++ to_fmt a ++ to_fmt "," ++ line ++ to_fmt b ++
  to_fmt "," ++ line ++ to_fmt c ++ to_fmt "," ++ line ++ to_fmt d ++ to_fmt "⟩"⟩
end

/-- The type of rules that specify how metadata for projections in changes.
  See `initialize_simps_projection`. -/
abbreviation projection_rule := (name × name ⊕ name) × bool

/--
The `@[_simps_str]` attribute specifies the preferred projections of the given structure,
used by the `@[simps]` attribute.
- This will usually be tagged by the `@[simps]` tactic.
- You can also generate this with the command `initialize_simps_projections`.
- To change the default value, see Note [custom simps projection].
- You are strongly discouraged to add this attribute manually.
- The first argument is the list of names of the universe variables used in the structure
- The second argument is a list that consists of the projection data for each projection.
-/
@[user_attribute] meta def simps_str_attr :
  user_attribute unit (list name × list projection_data) :=
{ name := `_simps_str,
  descr := "An attribute specifying the projection of the given structure.",
  parser := failed }

/--
  The `@[notation_class]` attribute specifies that this is a notation class,
  and this notation should be used instead of projections by @[simps].
  * The first argument `tt` for notation classes and `ff` for classes applied to the structure,
    like `has_coe_to_sort` and `has_coe_to_fun`
  * The second argument is the name of the projection (by default it is the first projection
    of the structure)
-/
@[user_attribute] meta def notation_class_attr : user_attribute unit (bool × option name) :=
{ name := `notation_class,
  descr := "An attribute specifying that this is a notation class. Used by @[simps].",
  parser := prod.mk <$> (option.is_none <$> (tk "*")?) <*> ident? }

attribute [notation_class] has_zero has_one has_add has_mul has_inv has_neg has_sub has_div has_dvd
  has_mod has_le has_lt has_append has_andthen has_union has_inter has_sdiff has_equiv has_subset
  has_ssubset has_emptyc has_insert has_singleton has_sep has_mem has_pow

attribute [notation_class* coe_sort] has_coe_to_sort
attribute [notation_class* coe_fn] has_coe_to_fun

/-- Returns the projection information of a structure. -/
meta def projections_info (l : list projection_data) (pref : string) (str : name) : tactic format :=
do
  ⟨defaults, nondefaults⟩ ← return $ l.partition_map $
    λ s, if s.is_default then inl s else inr s,
  to_print ← defaults.mmap $ λ s, to_string <$>
    let prefix_str := if s.is_prefix then "(prefix) " else "" in
    pformat!"Projection {prefix_str}{s.name}: {s.expr}",
  let print2 :=
    string.join $ (nondefaults.map (λ nm : projection_data, to_string nm.1)).intersperse ", ",
  let to_print := to_print ++ if nondefaults.length = 0 then [] else
    ["No lemmas are generated for the projections: " ++ print2 ++ "."],
  let to_print := string.join $ to_print.intersperse "\n        > ",
  return format!"[simps] > {pref} {str}:\n        > {to_print}"

/-- Auxiliary function of `get_composite_of_projections`. -/
meta def get_composite_of_projections_aux : Π (str : name) (proj : string) (x : expr)
  (pos : list ℕ) (args : list expr), tactic (expr × list ℕ) | str proj x pos args := do
  e ← get_env,
  projs ← e.structure_fields str,
  let proj_info := projs.map_with_index $ λ n p, (λ x, (x, n, p)) <$> proj.get_rest ("_" ++ p.last),
  when (proj_info.filter_map id = []) $
    fail!"Failed to find constructor {proj.popn 1} in structure {str}.",
  (proj_rest, index, proj_nm) ← return (proj_info.filter_map id).ilast,
  str_d ← e.get str,
  let proj_e : expr := const (str ++ proj_nm) str_d.univ_levels,
  proj_d ← e.get (str ++ proj_nm),
  type ← infer_type x,
  let params := get_app_args type,
  let univs := proj_d.univ_params.zip type.get_app_fn.univ_levels,
  let new_x := (proj_e.instantiate_univ_params univs).mk_app $ params ++ [x],
  let new_pos := pos ++ [index],
  if proj_rest.is_empty then return (new_x.lambdas args, new_pos) else do
    type ← infer_type new_x,
    (type_args, tgt) ← open_pis_whnf type,
    let new_str := tgt.get_app_fn.const_name,
    get_composite_of_projections_aux new_str proj_rest (new_x.mk_app type_args) new_pos
      (args ++ type_args)

/-- Given a structure `str` and a projection `proj`, that could be multiple nested projections
  (separated by `_`), returns an expression that is the composition of these projections and a
  list of natural numbers, that are the projection numbers of the applied projections. -/
meta def get_composite_of_projections (str : name) (proj : string) : tactic (expr × list ℕ) := do
  e ← get_env,
  str_d ← e.get str,
  let str_e : expr := const str str_d.univ_levels,
  type ← infer_type str_e,
  (type_args, tgt) ← open_pis_whnf type,
  let str_ap := str_e.mk_app type_args,
  x ← mk_local' `x binder_info.default str_ap,
  get_composite_of_projections_aux str ("_" ++ proj) x [] $ type_args ++ [x]

/--
  Get the projections used by `simps` associated to a given structure `str`.

  The returned information is also stored in a parameter of the attribute `@[_simps_str]`, which
  is given to `str`. If `str` already has this attribute, the information is read from this
  attribute instead. See the documentation for this attribute for the data this tactic returns.

  The returned universe levels are the universe levels of the structure. For the projections there
  are three cases
  * If the declaration `{structure_name}.simps.{projection_name}` has been declared, then the value
    of this declaration is used (after checking that it is definitionally equal to the actual
    projection. If you rename the projection name, the declaration should have the *new* projection
    name.
  * You can also declare a custom projection that is a composite of multiple projections.
  * Otherwise, for every class with the `notation_class` attribute, and the structure has an
    instance of that notation class, then the projection of that notation class is used for the
    projection that is definitionally equal to it (if there is such a projection).
    This means in practice that coercions to function types and sorts will be used instead of
    a projection, if this coercion is definitionally equal to a projection. Furthermore, for
    notation classes like `has_mul` and `has_zero` those projections are used instead of the
    corresponding projection.
    Projections for coercions and notation classes are not automatically generated if they are
    composites of multiple projections (for example when you use `extend` without the
    `old_structure_cmd`).
  * Otherwise, the projection of the structure is chosen.
    For example: ``simps_get_raw_projections env `prod`` gives the default projections
```
  ([u, v], [prod.fst.{u v}, prod.snd.{u v}])
```
    while ``simps_get_raw_projections env `equiv`` gives
```
  ([u_1, u_2], [λ α β, coe_fn, λ {α β} (e : α ≃ β), ⇑(e.symm), left_inv, right_inv])
```
    after declaring the coercion from `equiv` to function and adding the declaration
```
  def equiv.simps.inv_fun {α β} (e : α ≃ β) : β → α := e.symm
```

  Optionally, this command accepts three optional arguments:
  * If `trace_if_exists` the command will always generate a trace message when the structure already
    has the attribute `@[_simps_str]`.
  * The `rules` argument accepts a list of pairs `sum.inl (old_name, new_name)`. This is used to
    change the projection name `old_name` to the custom projection name `new_name`. Example:
    for the structure `equiv` the projection `to_fun` could be renamed `apply`. This name will be
    used for parsing and generating projection names. This argument is ignored if the structure
    already has an existing attribute. If an element of `rules` is of the form `sum.inr name`, this
    means that the projection `name` will not be applied by default.
  * if `trc` is true, this tactic will trace information.
-/
-- if performance becomes a problem, possible heuristic: use the names of the projections to
-- skip all classes that don't have the corresponding field.
meta def simps_get_raw_projections (e : environment) (str : name) (trace_if_exists : bool := ff)
  (rules : list projection_rule := []) (trc := ff) :
  tactic (list name × list projection_data) := do
  let trc := trc || is_trace_enabled_for `simps.verbose,
  has_attr ← has_attribute' `_simps_str str,
  if has_attr then do
    data ← simps_str_attr.get_param str,
    -- We always print the projections when they already exists and are called by
    -- `initialize_simps_projections`.
    when (trace_if_exists || is_trace_enabled_for `simps.verbose) $ projections_info data.2
      "Already found projection information for structure" str >>= trace,
    return data
  else do
    when trc trace!"[simps] > generating projection information for structure {str}.",
    when_tracing `simps.debug trace!"[simps] > Applying the rules {rules}.",
    d_str ← e.get str,
    let raw_univs := d_str.univ_params,
    let raw_levels := level.param <$> raw_univs,
    /- Figure out projections, including renamings. The information for a projection is (before we
    figure out the `expr` of the projection:
    `(original name, given name, is default, is prefix)`.
    The first projections are always the actual projections of the structure, but `rules` could
    specify custom projections that are compositions of multiple projections. -/
    projs ← e.structure_fields str,
    let projs : list parsed_projection_data := projs.map $ λ nm, ⟨nm, nm, tt, ff⟩,
    let projs : list parsed_projection_data := rules.foldl (λ projs rule,
      match rule with
      | (inl (old_nm, new_nm), is_prefix) := if old_nm ∈ projs.map (λ x, x.new_name) then
        projs.map $ λ proj,
          if proj.new_name = old_nm then
            { new_name := new_nm, is_prefix := is_prefix, ..proj } else
            proj else
        projs ++ [⟨old_nm, new_nm, tt, is_prefix⟩]
      | (inr nm, is_prefix) := if nm ∈ projs.map (λ x, x.new_name) then
        projs.map $ λ proj, if proj.new_name = nm then
          { is_default := ff, is_prefix := is_prefix, ..proj } else
          proj else
        projs ++ [⟨nm, nm, ff, is_prefix⟩]
      end) projs,
    when_tracing `simps.debug trace!"[simps] > Projection info after applying the rules: {projs}.",
    when ¬ (projs.map $ λ x, x.new_name : list name).nodup $
      fail $ "Invalid projection names. Two projections have the same name.
This is likely because a custom composition of projections was given the same name as an " ++
"existing projection. Solution: rename the existing projection (before renaming the custom " ++
"projection).",
    /- Define the raw expressions for the projections, by default as the projections
    (as an expression), but this can be overriden by the user. -/
    raw_exprs_and_nrs ← projs.mmap $ λ ⟨orig_nm, new_nm, _, _⟩, do
    { (raw_expr, nrs) ← get_composite_of_projections str orig_nm.last,
      custom_proj ← do
      { decl ← e.get (str ++ `simps ++ new_nm.last),
        let custom_proj := decl.value.instantiate_univ_params $ decl.univ_params.zip raw_levels,
        when trc trace!
          "[simps] > found custom projection for {new_nm}:\n        > {custom_proj}",
        return custom_proj } <|> return raw_expr,
      is_def_eq custom_proj raw_expr <|>
        -- if the type of the expression is different, we show a different error message, because
        -- that is more likely going to be helpful.
        do
        { custom_proj_type ← infer_type custom_proj,
          raw_expr_type ← infer_type raw_expr,
          b ← succeeds (is_def_eq custom_proj_type raw_expr_type),
          if b then fail!"Invalid custom projection:\n  {custom_proj}
Expression is not definitionally equal to\n  {raw_expr}"
          else fail!"Invalid custom projection:\n  {custom_proj}
Expression has different type than {str ++ orig_nm}. Given type:\n  {custom_proj_type}
Expected type:\n  {raw_expr_type}" },
      return (custom_proj, nrs) },
    let raw_exprs := raw_exprs_and_nrs.map prod.fst,
    /- Check for other coercions and type-class arguments to use as projections instead. -/
    (args, _) ← open_pis d_str.type,
    let e_str := (expr.const str raw_levels).mk_app args,
    automatic_projs ← attribute.get_instances `notation_class,
    raw_exprs ← automatic_projs.mfoldl (λ (raw_exprs : list expr) class_nm, do
    { (is_class, proj_nm) ← notation_class_attr.get_param class_nm,
      proj_nm ← proj_nm <|> (e.structure_fields_full class_nm).map list.head,
      /- For this class, find the projection. `raw_expr` is the projection found applied to `args`,
        and `lambda_raw_expr` has the arguments `args` abstracted. -/
      (raw_expr, lambda_raw_expr) ← if is_class then (do
        guard $ args.length = 1,
        let e_inst_type := (const class_nm raw_levels).mk_app args,
        (hyp, e_inst) ← try_for 1000 (mk_conditional_instance e_str e_inst_type),
        raw_expr ← mk_mapp proj_nm [args.head, e_inst],
        clear hyp,
        -- Note: `expr.bind_lambda` doesn't give the correct type
        raw_expr_lambda ← lambdas [hyp] raw_expr,
        return (raw_expr, raw_expr_lambda.lambdas args))
      else (do
        e_inst_type ← to_expr (((const class_nm []).app (pexpr.of_expr e_str)).app ``(_)),
        e_inst ← try_for 1000 (mk_instance e_inst_type),
        raw_expr ← mk_mapp proj_nm [e_str, none, e_inst],
        return (raw_expr, raw_expr.lambdas args)),
      raw_expr_whnf ← whnf raw_expr,
      let relevant_proj := raw_expr_whnf.binding_body.get_app_fn.const_name,
      /- Use this as projection, if the function reduces to a projection, and this projection has
        not been overrriden by the user. -/
      guard $ projs.any $
        λ x, x.1 = relevant_proj.last ∧ ¬ e.contains (str ++ `simps ++ x.new_name.last),
      let pos := projs.find_index (λ x, x.1 = relevant_proj.last),
      when trc trace!
        "        > using {proj_nm} instead of the default projection {relevant_proj.last}.",
      when_tracing `simps.debug trace!"[simps] > The raw projection is:\n  {lambda_raw_expr}",
      return $ raw_exprs.update_nth pos lambda_raw_expr } <|> return raw_exprs) raw_exprs,
    let positions := raw_exprs_and_nrs.map prod.snd,
    let proj_names := projs.map (λ x, x.new_name),
    let defaults := projs.map (λ x, x.is_default),
    let prefixes := projs.map (λ x, x.is_prefix),
    let projs := proj_names.zip_with5 projection_data.mk raw_exprs positions defaults prefixes,
    /- make all proof non-default. -/
    projs ← projs.mmap $ λ proj,
      is_proof proj.expr >>= λ b, return $ if b then { is_default := ff, .. proj } else proj,
    when trc $ projections_info projs "generated projections for" str >>= trace,
    simps_str_attr.set str (raw_univs, projs) tt,
    when_tracing `simps.debug trace!
       "[simps] > Generated raw projection data: \n{(raw_univs, projs)}",
    return (raw_univs, projs)

/-- Parse a rule for `initialize_simps_projections`. It is either `<name>→<name>` or `-<name>`,
  possibly following by `as_prefix`.-/
meta def simps_parse_rule : parser projection_rule :=
prod.mk <$>
  ((λ x y, inl (x, y)) <$> ident <*> (tk "->" >> ident) <|> inr <$> (tk "-" >> ident)) <*>
  is_some <$> (tk "as_prefix")?

/--
You can specify custom projections for the `@[simps]` attribute.
To do this for the projection `my_structure.original_projection` by adding a declaration
`my_structure.simps.my_projection` that is definitionally equal to
`my_structure.original_projection` but has the projection in the desired (simp-normal) form.
Then you can call
```
initialize_simps_projections (original_projection → my_projection, ...)
```
to register this projection. See `initialize_simps_projections_cmd` for more information.

You can also specify custom projections that are definitionally equal to a composite of multiple
projections. This is often desirable when extending structures (without `old_structure_cmd`).

`has_coe_to_fun` and notation class (like `has_mul`) instances will be automatically used, if they
are definitionally equal to a projection of the structure (but not when they are equal to the
composite of multiple projections).
-/
library_note "custom simps projection"

/--
This command specifies custom names and custom projections for the simp attribute `simps_attr`.
* You can specify custom names by writing e.g.
  `initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply)`.
* See Note [custom simps projection] and the examples below for information how to declare custom
  projections.
* If no custom projection is specified, the projection will be `coe_fn`/`⇑` if a `has_coe_to_fun`
  instance has been declared, or the notation of a notation class (like `has_mul`) if such an
  instance is available. If none of these cases apply, the projection itself will be used.
* You can disable a projection by default by running
  `initialize_simps_projections equiv (-inv_fun)`
  This will ensure that no simp lemmas are generated for this projection,
  unless this projection is explicitly specified by the user.
* If you want the projection name added as a prefix in the generated lemma name, you can add the
  `as_prefix` modifier:
  `initialize_simps_projections equiv (to_fun → coe as_prefix)`
  Note that this does not influence the parsing of projection names: if you have a declaration
  `foo` and you want to apply the projections `snd`, `coe` (which is a prefix) and `fst`, in that
  order you can run `@[simps snd_coe_fst] def foo ...` and this will generate a lemma with the
  name `coe_foo_snd_fst`.
  * Run `initialize_simps_projections?` (or `set_option trace.simps.verbose true`)
  to see the generated projections.
* You can declare a new name for a projection that is the composite of multiple projections, e.g.
  ```
    structure A := (proj : ℕ)
    structure B extends A
    initialize_simps_projections? B (to_A_proj → proj, -to_A)
  ```
  You can also make your custom projection that is definitionally equal to a composite of
  projections. In this case, coercions and notation classes are not automatically recognized, and
  should be manually given by giving a custom projection.
  This is especially useful when extending a structure (without `old_structure_cmd`).
  In the above example, it is desirable to add `-to_A`, so that `@[simps]` doesn't automatically
  apply the `B.to_A` projection and then recursively the `A.proj` projection in the lemmas it
  generates. If you want to get both the `foo_proj` and `foo_to_A` simp lemmas, you can use
  `@[simps, simps to_A]`.
* Running `initialize_simps_projections my_struc` without arguments is not necessary, it has the
  same effect if you just add `@[simps]` to a declaration.
* If you do anything to change the default projections, make sure to call either `@[simps]` or
  `initialize_simps_projections` in the same file as the structure declaration. Otherwise, you might
  have a file that imports the structure, but not your custom projections.

Some common uses:
* If you define a new homomorphism-like structure (like `mul_hom`) you can just run
  `initialize_simps_projections` after defining the `has_coe_to_fun` instance
  ```
    instance {mM : has_mul M} {mN : has_mul N} : has_coe_to_fun (M →ₙ* N) := ...
    initialize_simps_projections mul_hom (to_fun → apply)
  ```
  This will generate `foo_apply` lemmas for each declaration `foo`.
* If you prefer `coe_foo` lemmas that state equalities between functions, use
  `initialize_simps_projections mul_hom (to_fun → coe as_prefix)`
  In this case you have to use `@[simps {fully_applied := ff}]` or equivalently `@[simps as_fn]`
  whenever you call `@[simps]`.
* You can also initialize to use both, in which case you have to choose which one to use by default,
  by using either of the following
  ```
    initialize_simps_projections mul_hom (to_fun → apply, to_fun → coe, -coe as_prefix)
    initialize_simps_projections mul_hom (to_fun → apply, to_fun → coe as_prefix, -apply)
  ```
  In the first case, you can get both lemmas using `@[simps, simps coe as_fn]` and in the second
  case you can get both lemmas using `@[simps as_fn, simps apply]`.
* If your new homomorphism-like structure extends another structure (without `old_structure_cmd`)
  (like `rel_embedding`), then you have to specify explicitly that you want to use a coercion
  as a custom projection. For example
  ```
    def rel_embedding.simps.apply (h : r ↪r s) : α → β := h
    initialize_simps_projections rel_embedding (to_embedding_to_fun → apply, -to_embedding)
  ```
* If you have an isomorphism-like structure (like `equiv`) you often want to define a custom
  projection for the inverse:
  ```
    def equiv.simps.symm_apply (e : α ≃ β) : β → α := e.symm
    initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply)
  ```
-/
@[user_command] meta def initialize_simps_projections_cmd
  (_ : parse $ tk "initialize_simps_projections") : parser unit := do
  env ← get_env,
  trc ← is_some <$> (tk "?")?,
  ns ← (prod.mk <$> ident <*> (tk "(" >> sep_by (tk ",") simps_parse_rule <* tk ")")?)*,
  ns.mmap' $ λ data, do
    nm ← resolve_constant data.1,
    simps_get_raw_projections env nm tt (data.2.get_or_else []) trc

add_tactic_doc
{ name                     := "initialize_simps_projections",
  category                 := doc_category.cmd,
  decl_names               := [`initialize_simps_projections_cmd],
  tags                     := ["simplification"] }

/--
  Configuration options for the `@[simps]` attribute.
  * `attrs` specifies the list of attributes given to the generated lemmas. Default: ``[`simp]``.
    The attributes can be either basic attributes, or user attributes without parameters.
    There are two attributes which `simps` might add itself:
    * If ``[`simp]`` is in the list, then ``[`_refl_lemma]`` is added automatically if appropriate.
    * If the definition is marked with `@[to_additive ...]` then all generated lemmas are marked
      with `@[to_additive]`. This is governed by the `add_additive` configuration option.
  * if `simp_rhs` is `tt` then the right-hand-side of the generated lemmas will be put in
    simp-normal form. More precisely: `dsimp, simp` will be called on all these expressions.
    See note [dsimp, simp].
  * `type_md` specifies how aggressively definitions are unfolded in the type of expressions
    for the purposes of finding out whether the type is a function type.
    Default: `instances`. This will unfold coercion instances (so that a coercion to a function type
    is recognized as a function type), but not declarations like `set`.
  * `rhs_md` specifies how aggressively definition in the declaration are unfolded for the purposes
    of finding out whether it is a constructor.
    Default: `none`
    Exception: `@[simps]` will automatically add the options
    `{rhs_md := semireducible, simp_rhs := tt}` if the given definition is not a constructor with
    the given reducibility setting for `rhs_md`.
  * If `fully_applied` is `ff` then the generated `simp` lemmas will be between non-fully applied
    terms, i.e. equalities between functions. This does not restrict the recursive behavior of
    `@[simps]`, so only the "final" projection will be non-fully applied.
    However, it can be used in combination with explicit field names, to get a partially applied
    intermediate projection.
  * The option `not_recursive` contains the list of names of types for which `@[simps]` doesn't
    recursively apply projections. For example, given an equivalence `α × β ≃ β × α` one usually
    wants to only apply the projections for `equiv`, and not also those for `×`. This option is
    only relevant if no explicit projection names are given as argument to `@[simps]`.
  * The option `trace` is set to `tt` when you write `@[simps?]`. In this case, the attribute will
    print all generated lemmas. It is almost the same as setting the option `trace.simps.verbose`,
    except that it doesn't print information about the found projections.
  * if `add_additive` is `some nm` then `@[to_additive]` is added to the generated lemma. This
    option is automatically set to `tt` when the original declaration was tagged with
    `@[to_additive, simps]` (in that order), where `nm` is the additive name of the original
    declaration.
-/
@[derive [has_reflect, inhabited]] structure simps_cfg :=
(attrs         := [`simp])
(simp_rhs      := ff)
(type_md       := transparency.instances)
(rhs_md        := transparency.none)
(fully_applied := tt)
(not_recursive := [`prod, `pprod])
(trace         := ff)
(add_additive  := @none name)

/-- A common configuration for `@[simps]`: generate equalities between functions instead equalities
  between fully applied expressions. -/
def as_fn : simps_cfg := {fully_applied := ff}
/-- A common configuration for `@[simps]`: don't tag the generated lemmas with `@[simp]`. -/
def lemmas_only : simps_cfg := {attrs := []}

/--
  Get the projections of a structure used by `@[simps]` applied to the appropriate arguments.
  Returns a list of tuples
  ```
  (corresponding right-hand-side, given projection name, projection expression, projection numbers,
    used by default, is prefix)
  ```
  (where all fields except the first are packed in a `projection_data` structure)
  one for each projection. The given projection name is the name for the projection used by the user
  used to generate (and parse) projection names. For example, in the structure

  Example 1: ``simps_get_projection_exprs env `(α × β) `(⟨x, y⟩)`` will give the output
  ```
    [(`(x), `fst, `(@prod.fst.{u v} α β), [0], tt, ff),
     (`(y), `snd, `(@prod.snd.{u v} α β), [1], tt, ff)]
  ```

  Example 2: ``simps_get_projection_exprs env `(α ≃ α) `(⟨id, id, λ _, rfl, λ _, rfl⟩)``
  will give the output
  ```
    [(`(id), `apply, `(coe), [0], tt, ff),
     (`(id), `symm_apply, `(λ f, ⇑f.symm), [1], tt, ff),
     ...,
     ...]
  ```
-/
meta def simps_get_projection_exprs (e : environment) (tgt : expr)
  (rhs : expr) (cfg : simps_cfg) : tactic $ list $ expr × projection_data := do
  let params := get_app_args tgt, -- the parameters of the structure
  (params.zip $ (get_app_args rhs).take params.length).mmap' (λ ⟨a, b⟩, is_def_eq a b)
    <|> fail "unreachable code (1)",
  let str := tgt.get_app_fn.const_name,
  let rhs_args := (get_app_args rhs).drop params.length, -- the fields of the object
  (raw_univs, proj_data) ← simps_get_raw_projections e str ff [] cfg.trace,
  let univs := raw_univs.zip tgt.get_app_fn.univ_levels,
  let new_proj_data : list $ expr × projection_data := proj_data.map $
    λ proj, (rhs_args.inth proj.proj_nrs.head,
      { expr := (proj.expr.instantiate_univ_params univs).instantiate_lambdas_or_apps params,
        proj_nrs := proj.proj_nrs.tail,
        .. proj }),
  return new_proj_data

/-- Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`,
  `args` is the list of local constants occurring, and `univs` is the list of universe variables. -/
meta def simps_add_projection (nm : name) (type lhs rhs : expr) (args : list expr)
  (univs : list name) (cfg : simps_cfg) : tactic (list name) := do
  when_tracing `simps.debug trace!
    "[simps] > Planning to add the equality\n        > {lhs} = ({rhs} : {type})",
  lvl ← get_univ_level type,
  -- simplify `rhs` if `cfg.simp_rhs` is true
  (rhs, prf) ← do { guard cfg.simp_rhs,
    rhs' ← rhs.dsimp {fail_if_unchanged := ff},
    when_tracing `simps.debug $ when (rhs ≠ rhs') trace!
      "[simps] > `dsimp` simplified rhs to\n        > {rhs'}",
    (rhsprf1, rhsprf2, ns) ← rhs'.simp {fail_if_unchanged := ff},
    when_tracing `simps.debug $ when (rhs' ≠ rhsprf1) trace!
      "[simps] > `simp` simplified rhs to\n        > {rhsprf1}",
    return (prod.mk rhsprf1 rhsprf2) }
    <|> return (rhs, const `eq.refl [lvl] type lhs),
  let eq_ap := const `eq [lvl] type lhs rhs,
  decl_name ← get_unused_decl_name nm,
  let decl_type := eq_ap.pis args,
  let decl_value := prf.lambdas args,
  let decl := declaration.thm decl_name univs decl_type (pure decl_value),
  when cfg.trace trace!
    "[simps] > adding projection {decl_name}:\n        > {decl_type}",
  decorate_error ("Failed to add projection lemma " ++ decl_name.to_string ++ ". Nested error:") $
    add_decl decl,
  b ← succeeds $ is_def_eq lhs rhs,
  when (b ∧ `simp ∈ cfg.attrs) (set_basic_attribute `_refl_lemma decl_name tt),
  cfg.attrs.mmap' $ λ nm, set_attribute nm decl_name tt,
  when cfg.add_additive.is_some $
    to_additive.attr.set decl_name ⟨ff, cfg.trace, cfg.add_additive.iget, none, tt⟩ tt,
  pure [decl_name]

/-- Derive lemmas specifying the projections of the declaration.
  If `todo` is non-empty, it will generate exactly the names in `todo`.
  `to_apply` is non-empty after a custom projection that is a composition of multiple projections
  was just used. In that case we need to apply these projections before we continue changing lhs. -/
meta def simps_add_projections : Π (e : environment) (nm : name)
  (type lhs rhs : expr) (args : list expr) (univs : list name) (must_be_str : bool)
  (cfg : simps_cfg) (todo : list string) (to_apply : list ℕ), tactic (list name)
| e nm type lhs rhs args univs must_be_str cfg todo to_apply := do
  -- we don't want to unfold non-reducible definitions (like `set`) to apply more arguments
  when_tracing `simps.debug trace!
    "[simps] > Type of the expression before normalizing: {type}",
  (type_args, tgt) ← open_pis_whnf type cfg.type_md,
  when_tracing `simps.debug trace!"[simps] > Type after removing pi's: {tgt}",
  tgt ← whnf tgt,
  when_tracing `simps.debug trace!"[simps] > Type after reduction: {tgt}",
  let new_args := args ++ type_args,
  let lhs_ap := lhs.instantiate_lambdas_or_apps type_args,
  let rhs_ap := rhs.instantiate_lambdas_or_apps type_args,
  let str := tgt.get_app_fn.const_name,
  /- We want to generate the current projection if it is in `todo` -/
  let todo_next := todo.filter (≠ ""),
  /- Don't recursively continue if `str` is not a structure or if the structure is in
    `not_recursive`. -/
  if e.is_structure str ∧ ¬(todo = [] ∧ str ∈ cfg.not_recursive ∧ ¬must_be_str) then do
    [intro] ← return $ e.constructors_of str | fail "unreachable code (3)",
    rhs_whnf ← whnf rhs_ap cfg.rhs_md,
    -- `todo_now` means that we still have to generate the current simp lemma
    (rhs_ap, todo_now, added_lems_requested) ←
      if ¬ is_constant_of rhs_ap.get_app_fn intro ∧
        is_constant_of rhs_whnf.get_app_fn intro then do
      /- If this was a desired projection, we want to apply it before taking the whnf.
        However, if the current field is an eta-expansion (see below), we first want
        to eta-reduce it and only then construct the projection.
        This makes the flow of this function messy. -/
      added_lems_requested ← cond ("" ∈ todo ∧ to_apply = []) (if cfg.fully_applied then
        simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else
        simps_add_projection nm type lhs rhs args univs cfg) (pure []),
      return (rhs_whnf, ff, added_lems_requested) else
      return (rhs_ap, "" ∈ todo ∧ to_apply = [], []),
    if is_constant_of (get_app_fn rhs_ap) intro then do -- if the value is a constructor application
      proj_info ← simps_get_projection_exprs e tgt rhs_ap cfg,
      when_tracing `simps.debug trace!"[simps] > Raw projection information:\n  {proj_info}",
      eta ← rhs_ap.is_eta_expansion, -- check whether `rhs_ap` is an eta-expansion
      let rhs_ap := eta.lhoare rhs_ap, -- eta-reduce `rhs_ap`
      /- As a special case, we want to automatically generate the current projection if `rhs_ap`
        was an eta-expansion. Also, when this was a desired projection, we need to generate the
        current projection if we haven't done it above. -/
      added_lems_eta ← cond (todo_now ∨ (todo = [] ∧ eta.is_some ∧ to_apply = []))
        (if cfg.fully_applied then
          simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else
          simps_add_projection nm type lhs rhs args univs cfg) (return []),
      /- If we are in the middle of a composite projection. -/
      added_lems_custom_proj ← cond (to_apply ≠ []) (do
      { ⟨new_rhs, proj, proj_expr, proj_nrs, is_default, is_prefix⟩ ←
          return $ proj_info.inth to_apply.head,
        new_type ← infer_type new_rhs,
        when_tracing `simps.debug
          trace!"[simps] > Applying a custom composite projection. Current lhs:
        >  {lhs_ap}",
        simps_add_projections e nm new_type lhs_ap new_rhs new_args univs ff cfg todo
          to_apply.tail }) (pure []),
      let all_added_lems := added_lems_requested ++ added_lems_eta ++ added_lems_custom_proj,
      /- We stop if no further projection is specified or if we just reduced an eta-expansion and we
      automatically choose projections -/
      cond (¬(to_apply ≠ [] ∨ todo = [""] ∨ (eta.is_some ∧ todo = []))) (do
        let projs : list name := proj_info.map $ λ x, x.snd.name,
        let todo := if to_apply = [] then todo_next else todo,
        -- check whether all elements in `todo` have a projection as prefix
        guard (todo.all $ λ x, projs.any $ λ proj, ("_" ++ proj.last).is_prefix_of x) <|>
          let x := (todo.find $ λ x, projs.all $ λ proj, ¬ ("_" ++ proj.last).is_prefix_of x).iget,
            simp_lemma := nm.append_suffix x,
            needed_proj := (x.split_on '_').tail.head in
          fail!
"Invalid simp lemma {simp_lemma}. Structure {str} does not have projection {needed_proj}.
The known projections are:
  {projs}
You can also see this information by running
  `initialize_simps_projections? {str}`.
Note: these projection names might not correspond to the projection names of the structure.",
        added_lems_list ← proj_info.mmap_with_index
          (λ proj_nr ⟨new_rhs, proj, proj_expr, proj_nrs, is_default, is_prefix⟩, do
          new_type ← infer_type new_rhs,
          let new_todo :=
            todo.filter_map $ λ x, x.get_rest ("_" ++ proj.last),
          -- we only continue with this field if it is non-propositional or mentioned in todo
          cond ((is_default ∧ todo = []) ∨ new_todo ≠ []) (do
            let new_lhs := proj_expr.instantiate_lambdas_or_apps [lhs_ap],
            let new_nm := nm.append_to_last proj.last is_prefix,
            let new_cfg := { add_additive := cfg.add_additive.map $
              λ nm, nm.append_to_last (to_additive.guess_name proj.last) is_prefix, ..cfg },
            when_tracing `simps.debug trace!"[simps] > Recursively add projections for:
        >  {new_lhs}",
            simps_add_projections e new_nm new_type new_lhs new_rhs new_args univs
              ff new_cfg new_todo proj_nrs) (pure [])),
          pure $ all_added_lems ++ added_lems_list.join) (pure all_added_lems)
    -- if I'm about to run into an error, try to set the transparency for `rhs_md` higher.
    else if cfg.rhs_md = transparency.none ∧ (must_be_str ∨ todo_next ≠ [] ∨ to_apply ≠ []) then do
      when cfg.trace trace!
        "[simps] > The given definition is not a constructor application:
        >   {rhs_ap}
        > Retrying with the options {{ rhs_md := semireducible, simp_rhs := tt}.",
      added_lems_recursive ← simps_add_projections e nm type lhs rhs args univs must_be_str
        { rhs_md := semireducible, simp_rhs := tt, ..cfg} todo to_apply,
      pure $ added_lems_requested ++ added_lems_recursive
    else do
      when (to_apply ≠ []) $
        fail!"Invalid simp lemma {nm}.
The given definition is not a constructor application:\n  {rhs_ap}",
      when must_be_str $
        fail!"Invalid `simps` attribute. The body is not a constructor application:\n  {rhs_ap}",
      when (todo_next ≠ []) $
        fail!"Invalid simp lemma {nm.append_suffix todo_next.head}.
The given definition is not a constructor application:\n  {rhs_ap}",
      added_lems_no_constructor ← if cfg.fully_applied then
        simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else
        simps_add_projection nm type lhs rhs args univs cfg,
      pure $ added_lems_requested ++ added_lems_no_constructor
  else do
    when must_be_str $
      fail!"Invalid `simps` attribute. Target {str} is not a structure",
    when (todo_next ≠ [] ∧ str ∉ cfg.not_recursive) $
        let first_todo := todo_next.head in
        fail!"Invalid simp lemma {nm.append_suffix first_todo}.
Projection {(first_todo.split_on '_').tail.head} doesn't exist, because target is not a structure.",
    if cfg.fully_applied then
      simps_add_projection nm tgt lhs_ap rhs_ap new_args univs cfg else
      simps_add_projection nm type lhs rhs args univs cfg

/--
The `@[_simps_aux]` attribute specifies which lemmas are added by `simps`.
This should not be used manually and it only exists for mathport
-/
@[user_attribute] meta def simps_aux : user_attribute unit (list name) :=
{ name := `_simps_aux,
  descr := "An attribute specifying the added simps lemmas.",
  parser := failed }

/-- `simps_tac` derives `simp` lemmas for all (nested) non-Prop projections of the declaration.
  If `todo` is non-empty, it will generate exactly the names in `todo`.
  If `short_nm` is true, the generated names will only use the last projection name.
  If `trc` is true, trace as if `trace.simps.verbose` is true. -/
meta def simps_tac (nm : name) (cfg : simps_cfg := {}) (todo : list string := []) (trc := ff) :
  tactic unit := do
  e ← get_env,
  d ← e.get nm,
  let lhs : expr := const d.to_name d.univ_levels,
  let todo := todo.dedup.map $ λ proj, "_" ++ proj,
  let cfg := { trace := cfg.trace || is_trace_enabled_for `simps.verbose || trc, ..cfg },
  b ← has_attribute' `to_additive nm,
  cfg ← if b then do
  { dict ← to_additive.aux_attr.get_cache,
    when cfg.trace
      trace!"[simps] > @[to_additive] will be added to all generated lemmas.",
    return { add_additive := dict.find nm, ..cfg } } else
    return cfg,
  added_names ← simps_add_projections e nm d.type lhs d.value [] d.univ_params tt cfg todo [],
  simps_aux.set nm added_names true

/-- The parser for the `@[simps]` attribute. -/
meta def simps_parser : parser (bool × list string × simps_cfg) := do
/- note: we don't check whether the user has written a nonsense namespace in an argument. -/
prod.mk <$> is_some <$> (tk "?")? <*>
  (prod.mk <$> many (name.last <$> ident) <*>
  (do some e ← parser.pexpr? | return {}, eval_pexpr simps_cfg e))

/--
The `@[simps]` attribute automatically derives lemmas specifying the projections of this
declaration.

Example:
```lean
@[simps] def foo : ℕ × ℤ := (1, 2)
```
derives two `simp` lemmas:
```lean
@[simp] lemma foo_fst : foo.fst = 1
@[simp] lemma foo_snd : foo.snd = 2
```

* It does not derive `simp` lemmas for the prop-valued projections.
* It will automatically reduce newly created beta-redexes, but will not unfold any definitions.
* If the structure has a coercion to either sorts or functions, and this is defined to be one
  of the projections, then this coercion will be used instead of the projection.
* If the structure is a class that has an instance to a notation class, like `has_mul`, then this
  notation is used instead of the corresponding projection.
* You can specify custom projections, by giving a declaration with name
  `{structure_name}.simps.{projection_name}`. See Note [custom simps projection].

  Example:
  ```lean
  def equiv.simps.inv_fun (e : α ≃ β) : β → α := e.symm
  @[simps] def equiv.trans (e₁ : α ≃ β) (e₂ : β ≃ γ) : α ≃ γ :=
  ⟨e₂ ∘ e₁, e₁.symm ∘ e₂.symm⟩
  ```
  generates
  ```
  @[simp] lemma equiv.trans_to_fun : ∀ {α β γ} (e₁ e₂) (a : α), ⇑(e₁.trans e₂) a = (⇑e₂ ∘ ⇑e₁) a
  @[simp] lemma equiv.trans_inv_fun : ∀ {α β γ} (e₁ e₂) (a : γ),
    ⇑((e₁.trans e₂).symm) a = (⇑(e₁.symm) ∘ ⇑(e₂.symm)) a
  ```

* You can specify custom projection names, by specifying the new projection names using
  `initialize_simps_projections`.
  Example: `initialize_simps_projections equiv (to_fun → apply, inv_fun → symm_apply)`.
  See `initialize_simps_projections_cmd` for more information.

* If one of the fields itself is a structure, this command will recursively create
  `simp` lemmas for all fields in that structure.
  * Exception: by default it will not recursively create `simp` lemmas for fields in the structures
    `prod` and `pprod`. You can give explicit projection names or change the value of
    `simps_cfg.not_recursive` to override this behavior.

  Example:
  ```lean
  structure my_prod (α β : Type*) := (fst : α) (snd : β)
  @[simps] def foo : prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩
  ```
  generates
  ```lean
  @[simp] lemma foo_fst : foo.fst = (1, 2)
  @[simp] lemma foo_snd_fst : foo.snd.fst = 3
  @[simp] lemma foo_snd_snd : foo.snd.snd = 4
  ```

* You can use `@[simps proj1 proj2 ...]` to only generate the projection lemmas for the specified
  projections.
* Recursive projection names can be specified using `proj1_proj2_proj3`.
  This will create a lemma of the form `foo.proj1.proj2.proj3 = ...`.

  Example:
  ```lean
  structure my_prod (α β : Type*) := (fst : α) (snd : β)
  @[simps fst fst_fst snd] def foo : prod ℕ ℕ × my_prod ℕ ℕ := ⟨⟨1, 2⟩, 3, 4⟩
  ```
  generates
  ```lean
  @[simp] lemma foo_fst : foo.fst = (1, 2)
  @[simp] lemma foo_fst_fst : foo.fst.fst = 1
  @[simp] lemma foo_snd : foo.snd = {fst := 3, snd := 4}
  ```
* If one of the values is an eta-expanded structure, we will eta-reduce this structure.

  Example:
  ```lean
  structure equiv_plus_data (α β) extends α ≃ β := (data : bool)
  @[simps] def bar {α} : equiv_plus_data α α := { data := tt, ..equiv.refl α }
  ```
  generates the following:
  ```lean
  @[simp] lemma bar_to_equiv : ∀ {α : Sort*}, bar.to_equiv = equiv.refl α
  @[simp] lemma bar_data : ∀ {α : Sort*}, bar.data = tt
  ```
  This is true, even though Lean inserts an eta-expanded version of `equiv.refl α` in the
  definition of `bar`.
* For configuration options, see the doc string of `simps_cfg`.
* The precise syntax is `('simps' ident* e)`, where `e` is an expression of type `simps_cfg`.
* `@[simps]` reduces let-expressions where necessary.
* When option `trace.simps.verbose` is true, `simps` will print the projections it finds and the
  lemmas it generates. The same can be achieved by using `@[simps?]`, except that in this case it
  will not print projection information.
* Use `@[to_additive, simps]` to apply both `to_additive` and `simps` to a definition, making sure
  that `simps` comes after `to_additive`. This will also generate the additive versions of all
  `simp` lemmas.
-/
/- If one of the fields is a partially applied constructor, we will eta-expand it
  (this likely never happens, so is not included in the official doc). -/
@[user_attribute] meta def simps_attr : user_attribute unit (bool × list string × simps_cfg) :=
{ name := `simps,
  descr := "Automatically derive lemmas specifying the projections of this declaration.",
  parser := simps_parser,
  after_set := some $
    λ n _ persistent, do
      guard persistent <|> fail "`simps` currently cannot be used as a local attribute",
      (trc, todo, cfg) ← simps_attr.get_param n,
      simps_tac n cfg todo trc }

add_tactic_doc
{ name                     := "simps",
  category                 := doc_category.attr,
  decl_names               := [`simps_attr],
  tags                     := ["simplification"] }


/-
Copyright (c) 2017 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import tactic.doc_commands

/-!
# `generalize_proofs`

A simple tactic to find and replace all occurrences of proof terms in the
context and goal with new variables.
-/

namespace tactic

private meta def collect_proofs_in :
  expr → list expr → list name × list expr → tactic (list name × list expr)
| e ctx (ns, hs) :=
let go (tac : list name × list expr → tactic (list name × list expr)) :
  tactic (list name × list expr) :=
do t ← infer_type e,
   mcond (is_prop t) (do
     first (hs.map $ λ h, do
       t' ← infer_type h,
       is_def_eq t t',
       g ← target,
       change $ g.replace (λ a n, if a = e then some h else none),
       return (ns, hs)) <|>
     (let (n, ns) := (match ns with
        | [] := (`_x, [])
        | (n :: ns) := (n, ns)
        end : name × list name) in
      do generalize e n,
         h ← intro n,
         return (ns, h::hs)) <|> return (ns, hs)) (tac (ns, hs)) in
match e with
| (expr.const _ _)   := go return
| (expr.local_const _ _ _ _) := do t ← infer_type e, collect_proofs_in t ctx (ns, hs)
| (expr.mvar _ _ _)  := do t ← infer_type e, collect_proofs_in t ctx (ns, hs)
| (expr.app f x)     :=
  go (λ nh, collect_proofs_in f ctx nh >>= collect_proofs_in x ctx)
| (expr.lam n b d e) :=
  go (λ nh, do
    nh ← collect_proofs_in d ctx nh,
    var ← mk_local' n b d,
    collect_proofs_in (expr.instantiate_var e var) (var::ctx) nh)
| (expr.pi n b d e) := do
  nh ← collect_proofs_in d ctx (ns, hs),
  var ← mk_local' n b d,
  collect_proofs_in (expr.instantiate_var e var) (var::ctx) nh
| (expr.elet n t d e) :=
  go (λ nh, do
    nh ← collect_proofs_in t ctx nh,
    nh ← collect_proofs_in d ctx nh,
    collect_proofs_in (expr.instantiate_var e d) ctx nh)
| (expr.macro m l) :=
  go (λ nh, mfoldl (λ x e, collect_proofs_in e ctx x) nh l)
| _                  := return (ns, hs)
end

/-- Generalize proofs in the goal, naming them with the provided list. -/
meta def generalize_proofs (ns : list name) (loc : interactive.loc) : tactic unit :=
do intros_dep,
  hs ← local_context >>= mfilter is_proof,
  n ← loc.get_locals >>= revert_lst,
  t ← target,
  collect_proofs_in t [] (ns, hs),
  intron n <|> (intros $> ())

open interactive interactive.types lean.parser
local postfix *:9001 := many

namespace interactive
/-- Generalize proofs in the goal, naming them with the provided list.

For example:
```lean
example : list.nth_le [1, 2] 1 dec_trivial = 2 :=
begin
  -- ⊢ [1, 2].nth_le 1 _ = 2
  generalize_proofs h,
  -- h : 1 < [1, 2].length
  -- ⊢ [1, 2].nth_le 1 h = 2
end
```
-/
meta def generalize_proofs : parse ident_* → parse location → tactic unit :=
tactic.generalize_proofs
end interactive

add_tactic_doc
{ name       := "generalize_proofs",
  category   := doc_category.tactic,
  decl_names := [`tactic.interactive.generalize_proofs],
  tags       := ["context management"] }

end tactic


/-
Copyright (c) 2020 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Floris van Doorn
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.tactic.lint.default
import Mathlib.tactic.ext
import Mathlib.PostPort

namespace Mathlib

/-!
# Congruence and related tactics

This file contains the tactic `congr'`, which is an extension of `congr`, and various tactics
using `congr'` internally.

`congr'` has some advantages over `congr`:
* It turns `↔` to equalities, before trying another congr lemma
* You can write `congr' n` to give the maximal depth of recursive applications. This is useful if
  `congr` breaks down the goal to aggressively, and the resulting goals are false.
* You can write `congr' with ...` to do `congr', ext ...` in a single tactic.

Other tactics in this file:
* `rcongr`: repeatedly apply `congr'` and `ext.`
* `convert`: like `exact`, but produces an equality goal if the type doesn't match.
* `convert_to`: changes the goal, if you prove an equality between the old goal and the new goal.
* `ac_change`: like `convert_to`, but uses `ac_refl` to discharge the goals.
-/

namespace tactic

/-- Apply the constant `iff_of_eq` to the goal. -/
/-- The main part of the body for the loop in `congr'`. This will try to replace a goal `f x = f y`
 with `x = y`. Also has support for `==` and `↔`. -/
/-- The main function in `convert_to`. Changes the goal to `r` and a proof obligation that the goal
  is equal to `r`. -/
/--
Same as the `congr` tactic, but takes an optional argument which gives
the depth of recursive applications.
* This is useful when `congr` is too aggressive in breaking down the goal.
* For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y`
  and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`.
* If, at any point, a subgoal matches a hypothesis then the subgoal will be closed.
-/
namespace interactive

/--
Same as the `congr` tactic, but takes an optional argument which gives
the depth of recursive applications.
* This is useful when `congr` is too aggressive in breaking down the goal.
* For example, given `⊢ f (g (x + y)) = f (g (y + x))`, `congr'` produces the goals `⊢ x = y`
  and `⊢ y = x`, while `congr' 2` produces the intended `⊢ x + y = y + x`.
* If, at any point, a subgoal matches a hypothesis then the subgoal will be closed.
* You can use `congr' with p (: n)?` to call `ext p (: n)?` to all subgoals generated by `congr'`.
  For example, if the goal is `⊢ f '' s = g '' s` then `congr' with x` generates the goal
  `x : α ⊢ f x = g x`.
-/
/--
Repeatedly and apply `congr'` and `ext`, using the the given patterns as arguments for `ext`.

There are two ways this tactic stops:
* `congr'` fails (makes no progress), after having already applied `ext`.
* `congr'` canceled out the last usage of `ext`. In this case, the state is reverted to before
  the `congr'` was applied.

For example, when the goal is
```lean
⊢ (λ x, f x + 3) '' s = (λ x, g x + 3) '' s
```
then `rcongr x` produces the goal
```lean
x : α ⊢ f x = g x
```
This gives the same result as `congr', ext x, congr'`.

In contrast, `congr'` would produce
```lean
⊢ (λ x, f x + 3) = (λ x, g x + 3)
```
and `congr' with x` (or `congr', ext x`) would produce
```lean
x : α ⊢ f x + 3 = g x + 3
```
-/
/--
The `exact e` and `refine e` tactics require a term `e` whose type is
definitionally equal to the goal. `convert e` is similar to `refine
e`, but the type of `e` is not required to exactly match the
goal. Instead, new goals are created for differences between the type
of `e` and the goal. For example, in the proof state

```lean
n : ℕ,
e : prime (2 * n + 1)
⊢ prime (n + n + 1)
```

the tactic `convert e` will change the goal to

```lean
⊢ n + n = 2 * n
```

In this example, the new goal can be solved using `ring`.

If `x y : t`, and an instance `subsingleton t` is in scope, then any goals of the form
`x = y` are solved automatically.

The syntax `convert ← e` will reverse the direction of the new goals
(producing `⊢ 2 * n = n + n` in this example).

Internally, `convert e` works by creating a new goal asserting that
the goal equals the type of `e`, then simplifying it using
`congr'`. The syntax `convert e using n` can be used to control the
depth of matching (like `congr' n`). In the example, `convert e using
1` would produce a new goal `⊢ n + n + 1 = 2 * n + 1`.
-/
/--
`convert_to g using n` attempts to change the current goal to `g`, but unlike `change`,
it will generate equality proof obligations using `congr' n` to resolve discrepancies.
`convert_to g` defaults to using `congr' 1`.

`ac_change` is `convert_to` followed by `ac_refl`. It is useful for rearranging/reassociating
e.g. sums:
```lean
example (a b c d e f g N : ℕ) : (a + b) + (c + d) + (e + f) + g ≤ N :=
begin
  ac_change a + d + e + f + c + g + b ≤ _,
-- ⊢ a + d + e + f + c + g + b ≤ N

-- ⊢ a + d + e + f + c + g + b ≤ N
end
```
-/
/-- `ac_change g using n` is `convert_to g using n; try {ac_refl}`. -/
end Mathlib


/-
Copyright (c) 2022 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Scott Morrison

! This file was ported from Lean 3 source module tactic.assert_exists
! leanprover-community/mathlib commit 90367774bb3afc7bdc1e9acbc770970042378306
! Please do not edit these lines, except to modify the commit id
! if you have ported upstream changes.
-/
import Mathbin.Tactic.Core
import Mathbin.Tactic.Lint.Basic

/-!
# User commands for assert the (non-)existence of declaration or instances.

These commands are used to enforce the independence of different parts of mathlib.

## Implementation notes

This file provides two linters that verify that things we assert do not _yet_ exist do _eventually_
exist. This works by creating declarations of the form:

* ``assert_not_exists._checked.<uniq> : name := `foo`` for `assert_not_exists foo`
* `assert_no_instance._checked.<uniq> := t` for `assert_instance t`

These declarations are then picked up by the linter and analyzed accordingly.
The `_` in the `_checked` prefix should hide them from doc-gen.
-/

section

/- ./././Mathport/Syntax/Translate/Tactic/Mathlib/Core.lean:38:34: unsupported: setup_tactic_parser -/
open Tactic

/-- `assert_exists n` is a user command that asserts that a declaration named `n` exists
in the current import scope.

Be careful to use names (e.g. `rat`) rather than notations (e.g. `ℚ`).
-/
@[user_command]
unsafe def assert_exists (_ : parse <| tk "assert_exists") : lean.parser Unit := do
  let decl ← ident
  let d ← get_decl decl
  return ()
#align assert_exists assert_exists

/--
`assert_not_exists n` is a user command that asserts that a declaration named `n` *does not exist*
in the current import scope.

Be careful to use names (e.g. `rat`) rather than notations (e.g. `ℚ`).

It may be used (sparingly!) in mathlib to enforce plans that certain files
are independent of each other.

If you encounter an error on an `assert_not_exists` command while developing mathlib,
it is probably because you have introduced new import dependencies to a file.

In this case, you should refactor your work
(for example by creating new files rather than adding imports to existing files).
You should *not* delete the `assert_not_exists` statement without careful discussion ahead of time.
-/
@[user_command]
unsafe def assert_not_exists (_ : parse <| tk "assert_not_exists") : lean.parser Unit := do
  let decl ← ident
  let ff ← succeeds (get_decl decl) |
    fail f! "Declaration {decl} is not allowed to exist in this file."
  let n ← tactic.mk_fresh_name
  let marker := `assert_not_exists._checked.append (decl.append n)
  add_decl (declaration.defn marker [] q(Name) q(decl) default tt)
  pure ()
#align assert_not_exists assert_not_exists

/-- A linter for checking that the declarations marked `assert_not_exists` eventually exist. -/
unsafe def assert_not_exists.linter : linter
    where
  test d := do
    let n := d.to_name
    let tt ← pure (`assert_not_exists._checked.isPrefixOfₓ n) |
      pure none
    let declaration.defn _ _ q(Name) val _ _ ← pure d
    let n ← tactic.eval_expr Name val
    let tt ← succeeds (get_decl n) |
      pure (some (f! "`{n}` does not ever exist").toString)
    pure none
  auto_decls := true
  no_errors_found := "All `assert_not_exists` declarations eventually exist."
  errors_found :=
    "The following declarations used in `assert_not_exists` never exist; perhaps there is a typo."
  is_fast := true
#align assert_not_exists.linter assert_not_exists.linter

/-- `assert_instance e` is a user command that asserts that an instance `e` is available
in the current import scope.

Example usage:
```
assert_instance semiring ℕ
```
-/
@[user_command]
unsafe def assert_instance (_ : parse <| tk "assert_instance") : lean.parser Unit := do
  let q ← texpr
  let e ← i_to_expr q
  mk_instance e
  return ()
#align assert_instance assert_instance

/-- `assert_no_instance e` is a user command that asserts that an instance `e` *is not available*
in the current import scope.

It may be used (sparingly!) in mathlib to enforce plans that certain files
are independent of each other.

If you encounter an error on an `assert_no_instance` command while developing mathlib,
it is probably because you have introduced new import dependencies to a file.

In this case, you should refactor your work
(for example by creating new files rather than adding imports to existing files).
You should *not* delete the `assert_no_instance` statement without careful discussion ahead of time.

Example usage:
```
assert_no_instance linear_ordered_field ℚ
```
-/
@[user_command]
unsafe def assert_no_instance (_ : parse <| tk "assert_no_instance") : lean.parser Unit := do
  let q ← texpr
  let e ← i_to_expr q
  let i ← try_core (mk_instance e)
  match i with
    | none => do
      let n ← tactic.mk_fresh_name
      let e_str ← toString <$> pp e
      let marker := (`assert_no_instance._checked.mk_string e_str).append n
      let et ← infer_type e
      let tt ← succeeds (get_decl marker) |
        add_decl (declaration.defn marker [] et e default tt)
      pure ()
    | some i =>
      (throwError "Instance `{(← i)} : {← e}` is not allowed to be found in this file." :
        tactic Unit)
#align assert_no_instance assert_no_instance

/-- A linter for checking that the declarations marked `assert_no_instance` eventually exist. -/
unsafe def assert_no_instance.linter : linter
    where
  test d := do
    let n := d.to_name
    let tt ← pure (`assert_no_instance._checked.isPrefixOfₓ n) |
      pure none
    let declaration.defn _ _ _ val _ _ ← pure d
    let tt ← succeeds (tactic.mk_instance val) |
      (some ∘ format.to_string) <$> f!"No instance of `{← val}`"
    pure none
  auto_decls := true
  no_errors_found := "All `assert_no_instance` instances eventually exist."
  errors_found :=
    "The following typeclass instances used in `assert_no_instance` never exist; perhaps they " ++
      "are missing?"
  is_fast := false
#align assert_no_instance.linter assert_no_instance.linter

end


/-
Copyright (c) 2019 Robert A. Spencer. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Robert A. Spencer, Markus Himmel
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.algebra.category.Group.basic
import Mathlib.category_theory.concrete_category.default
import Mathlib.category_theory.limits.shapes.kernels
import Mathlib.category_theory.preadditive.default
import Mathlib.linear_algebra.basic
import Mathlib.PostPort

universes v u l u_1

namespace Mathlib

/-- The category of R-modules and their morphisms. -/
structure Module (R : Type u) [ring R]
where
  carrier : Type v
  is_add_comm_group : add_comm_group carrier
  is_module : module R carrier

namespace Module

-- TODO revisit this after #1438 merges, to check coercions and instances are handled consistently

protected instance has_coe_to_sort (R : Type u) [ring R] : has_coe_to_sort (Module R) :=
  has_coe_to_sort.mk (Type v) carrier

protected instance category_theory.category (R : Type u) [ring R] : category_theory.category (Module R) :=
  category_theory.category.mk

protected instance category_theory.concrete_category (R : Type u) [ring R] : category_theory.concrete_category (Module R) :=
  category_theory.concrete_category.mk
    (category_theory.functor.mk (fun (R_1 : Module R) => ↥R_1) fun (R_1 S : Module R) (f : R_1 ⟶ S) => ⇑f)

protected instance has_forget_to_AddCommGroup (R : Type u) [ring R] : category_theory.has_forget₂ (Module R) AddCommGroup :=
  category_theory.has_forget₂.mk
    (category_theory.functor.mk (fun (M : Module R) => AddCommGroup.of ↥M)
      fun (M₁ M₂ : Module R) (f : M₁ ⟶ M₂) => linear_map.to_add_monoid_hom f)

/-- The object in the category of R-modules associated to an R-module -/
def of (R : Type u) [ring R] (X : Type v) [add_comm_group X] [module R X] : Module R :=
  mk X

protected instance inhabited (R : Type u) [ring R] : Inhabited (Module R) :=
  { default := of R PUnit }

@[simp] theorem coe_of (R : Type u) [ring R] (X : Type u) [add_comm_group X] [module R X] : ↥(of R X) = X :=
  rfl

/-- Forgetting to the underlying type and then building the bundled object returns the original
module. -/
@[simp] theorem of_self_iso_inv {R : Type u} [ring R] (M : Module R) : category_theory.iso.inv (of_self_iso M) = 𝟙 :=
  Eq.refl (category_theory.iso.inv (of_self_iso M))

protected instance of.subsingleton {R : Type u} [ring R] : subsingleton ↥(of R PUnit) :=
  eq.mpr (id (Eq._oldrec (Eq.refl (subsingleton ↥(of R PUnit))) (coe_of R PUnit))) punit.subsingleton

protected instance category_theory.limits.has_zero_object {R : Type u} [ring R] : category_theory.limits.has_zero_object (Module R) :=
  category_theory.limits.has_zero_object.mk (of R PUnit) (fun (X : Module R) => unique.mk { default := 0 } sorry)
    fun (X : Module R) => unique.mk { default := 0 } sorry

@[simp] theorem id_apply {R : Type u} [ring R] {M : Module R} (m : ↥M) : coe_fn 𝟙 m = m :=
  rfl

@[simp] theorem coe_comp {R : Type u} [ring R] {M : Module R} {N : Module R} {U : Module R} (f : M ⟶ N) (g : N ⟶ U) : ⇑(f ≫ g) = ⇑g ∘ ⇑f :=
  rfl

end Module

/-- Reinterpreting a linear map in the category of `R`-modules. -/
def Module.as_hom {R : Type u} [ring R] {X₁ : Type v} {X₂ : Type v} [add_comm_group X₁] [module R X₁] [add_comm_group X₂] [module R X₂] : linear_map R X₁ X₂ → (Module.of R X₁ ⟶ Module.of R X₂) :=
  id

/-- Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s. -/
@[simp] theorem linear_equiv.to_Module_iso_inv {R : Type u} [ring R] {X₁ : Type v} {X₂ : Type v} {g₁ : add_comm_group X₁} {g₂ : add_comm_group X₂} {m₁ : module R X₁} {m₂ : module R X₂} (e : linear_equiv R X₁ X₂) : category_theory.iso.inv (linear_equiv.to_Module_iso e) = ↑(linear_equiv.symm e) :=
  Eq.refl (category_theory.iso.inv (linear_equiv.to_Module_iso e))

/--
Build an isomorphism in the category `Module R` from a `linear_equiv` between `module`s.

This version is better than `linear_equiv_to_Module_iso` when applicable, because Lean can't see
`Module.of R M` is defeq to `M` when `M : Module R`. -/
def linear_equiv.to_Module_iso' {R : Type u} [ring R] {M : Module R} {N : Module R} (i : linear_equiv R ↥M ↥N) : M ≅ N :=
  category_theory.iso.mk ↑i ↑(linear_equiv.symm i)

namespace category_theory.iso

/-- Build a `linear_equiv` from an isomorphism in the category `Module R`. -/
@[simp] theorem to_linear_equiv_apply {R : Type u} [ring R] {X : Module R} {Y : Module R} (i : X ≅ Y) : ∀ (ᾰ : ↥X), coe_fn (to_linear_equiv i) ᾰ = coe_fn (hom i) ᾰ :=
  fun (ᾰ : ↥X) => Eq.refl (coe_fn (to_linear_equiv i) ᾰ)

end category_theory.iso

/-- linear equivalences between `module`s are the same as (isomorphic to) isomorphisms
in `Module` -/
@[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 :=
  Eq.refl (category_theory.iso.hom linear_equiv_iso_Module_iso e)

namespace Module

protected instance category_theory.preadditive {R : Type u} [ring R] : category_theory.preadditive (Module R) :=
  category_theory.preadditive.mk

theorem ker_eq_bot_of_mono {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) [category_theory.mono f] : linear_map.ker f = ⊥ :=
  linear_map.ker_eq_bot_of_cancel
    fun (u v : linear_map R ↥(linear_map.ker f) ↥M) => iff.mp (category_theory.cancel_mono f)

theorem range_eq_top_of_epi {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) [category_theory.epi f] : linear_map.range f = ⊤ :=
  linear_map.range_eq_top_of_cancel
    fun (u v : linear_map R (↥N) (submodule.quotient (linear_map.range f))) => iff.mp (category_theory.cancel_epi f)

theorem mono_of_ker_eq_bot {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) (hf : linear_map.ker f = ⊥) : category_theory.mono f :=
  category_theory.concrete_category.mono_of_injective f (iff.mp linear_map.ker_eq_bot hf)

theorem epi_of_range_eq_top {R : Type u} [ring R] {M : Module R} {N : Module R} (f : M ⟶ N) (hf : linear_map.range f = ⊤) : category_theory.epi f :=
  category_theory.concrete_category.epi_of_surjective f (iff.mp linear_map.range_eq_top hf)

end Module

protected 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) :=
  has_coe.mk fun (N : submodule R M) => Module.of R ↥N


/-
Copyright (c) 2022 Devon Tuma. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Devon Tuma
-/
import computational_monads.simulation_semantics.constructions.stateless_oracle

/-!
# Identity Oracle

Defines a `sim_oracle` that just acts as an identity,
  e.g. simulation with this oracle has no effect (besides an empty state)

Main use cases is in simulating a pair of oracles, where only one of the oracles is reduced.
  For example preserving a `uniform_selecting` oracle while reducing a `signing_oracle`
-/

open oracle_comp oracle_spec

variables {α β : Type} {spec spec' spec'' : oracle_spec}
  (oa : oracle_comp spec α) (ob : α → oracle_comp spec β) (s : unit)

@[inline, reducible]
def identity_oracle (spec : oracle_spec) : sim_oracle spec spec unit := ⟪query⟫

notation `idₛ` := identity_oracle _

namespace identity_oracle

@[simp] lemma apply (i : spec.ι) (t : spec.domain i) (s : unit) :
  (idₛ) i (t, s) = query i t >>= λ u, return (u, ()) := rfl

section support

@[simp] lemma support_apply (i : spec.ι) (t : spec.domain i) :
  ((idₛ) i (t, s)).support = {u | u.1 ∈ (query i t).support} :=
begin
  simp only [apply, support_bind, support_return, set.Union_true,
    set.Union_singleton_eq_range, support_query, set.top_eq_univ, set.mem_univ, set.set_of_true],
  exact set.eq_univ_of_forall (λ x, set.mem_range.2
    ⟨x.1, prod.eq_iff_fst_eq_snd_eq.2 ⟨rfl, punit_eq () x.snd⟩⟩),
end

@[simp] lemma support_simulate' : (simulate' idₛ oa s).support = oa.support :=
stateless_oracle.support_simulate'_eq_support _ query s (λ _ _, rfl)

@[simp] theorem support_simulate : (simulate idₛ oa s).support = prod.fst ⁻¹' oa.support :=
(stateless_oracle.support_simulate_eq_preimage_support_simulate' _ _ _).trans
  (congr_arg _ $ support_simulate' oa ())

@[simp] lemma mem_support_simulate_iff (x : α × unit) :
  x ∈ (simulate idₛ oa s).support ↔ x.1 ∈ oa.support :=
by rw [support_simulate, set.mem_preimage]

end support

section distribution_semantics

section eval_dist

@[simp]
lemma eval_dist_apply (i : spec.ι) (t : spec.domain i) :
  ⁅idₛ i (t, s)⁆ = ⁅(λ u, (u, ())) <$> query i t⁆ :=
rfl

-- @[simp]
-- theorem eval_dist_simulate : ⁅simulate idₛ oa s⁆ = ⁅(λ a, (a, ())) <$> oa⁆ :=
-- begin
--   induction oa with α a α β oa ob hoa hob i t generalizing s,
--   { simp [punit_eq s (), pmf.pure_map] },
--   { exact trans (simulate_bind_equiv idₛ oa ob s) (trans (eval_dist_bind_eq_of_eval_dist_eq
--       (hoa s) (λ x, hob x.fst x.snd)) (by simp)) },
--   { exact (simulate_query_equiv idₛ i t ()).trans (apply_equiv () i t) }
-- end

-- lemma eval_dist_default_simulate : default_simulate idₛ oa ≃ₚ (λ a, (a, ())) <$> oa :=
-- simulate_equiv oa ()

-- @[simp]
-- lemma simulate'_equiv : (simulate' idₛ oa s) ≃ₚ oa :=
-- calc simulate' idₛ oa s ≃ₚ prod.fst <$> simulate idₛ oa s : rfl
--   ... ≃ₚ prod.fst <$> (λ a, (a, ())) <$> oa : (map_equiv_of_equiv _ (simulate_equiv oa s))
--   ... ≃ₚ (prod.fst ∘ λ a, (a, ())) <$> oa : map_map_equiv oa _ _
--   ... ≃ₚ oa : map_id_equiv oa

-- lemma default_simulate'_equiv : default_simulate' idₛ oa ≃ₚ oa :=
-- simulate'_equiv oa ()

end eval_dist

section prob_event

end prob_event

end distribution_semantics

end identity_oracle


import tactic

------------------------------------------------
-- Tag allowable graph theory theorems --
------------------------------------------------
@[user_attribute]
meta def dummy : user_attribute :=
{ name := `dummy,
  descr := "A tag for all silly theorems for debugging purposes." }

------------------------------------------------
-- The allowable theorems --
------------------------------------------------
-- A sample there-exists statement for debugging
@[dummy]
theorem exists_one:
  ∃ n : ℕ, n=1 :=
begin
  use 1
end

-- A sample for-all, there-exists statement for debugging
@[dummy]
theorem forall_exists_greater:
  ∀ n : ℕ, ∃ k : ℕ, k = n+1 :=
begin
  intros n, use n+1,
end