|
| 1 | +/- |
| 2 | +Copyright (c) 2019 Floris van Doorn. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Floris van Doorn |
| 5 | +-/ |
| 6 | +import tactic.core |
| 7 | +/-! |
| 8 | + # simps attribute |
| 9 | + This file defines the `@[simps]` attribute, to automatically generate simp-lemmas |
| 10 | + reducing a definition when projections are applied to it. |
| 11 | +
|
| 12 | + ## Tags |
| 13 | + structures, projections, simp, simplifier, generates declarations |
| 14 | +-/ |
| 15 | + |
| 16 | +open tactic expr |
| 17 | + |
| 18 | +/-- Add a lemma with `nm` stating that `lhs = rhs`. `type` is the type of both `lhs` and `rhs`, |
| 19 | + `args` is the list of local constants occurring, and `univs` is the list of universe variables. |
| 20 | + If `add_simp` then we make the resulting lemma a simp-lemma. -/ |
| 21 | +meta def add_projection (nm : name) (type lhs rhs : expr) (args : list expr) |
| 22 | + (univs : list name) (add_simp : bool) : tactic unit := do |
| 23 | + eq_ap ← mk_mapp `eq $ [type, lhs, rhs].map some, |
| 24 | + refl_ap ← mk_app `eq.refl [type, lhs], |
| 25 | + decl_name ← get_unused_decl_name nm, |
| 26 | + let decl_type := eq_ap.pis args, |
| 27 | + let decl_value := refl_ap.lambdas args, |
| 28 | + let decl := declaration.thm decl_name univs decl_type (pure decl_value), |
| 29 | + add_decl decl <|> fail format!"failed to add projection lemma {decl_name}.", |
| 30 | + when add_simp $ |
| 31 | + set_basic_attribute `simp decl_name tt >> set_basic_attribute `_refl_lemma decl_name tt |
| 32 | + |
| 33 | +/-- Derive lemmas specifying the projections of the declaration. -/ |
| 34 | +meta def add_projections : ∀(e : environment) (nm : name) (type lhs rhs : expr) |
| 35 | + (args : list expr) (univs : list name) (add_simp must_be_str : bool), tactic unit |
| 36 | + | e nm type lhs rhs args univs add_simp must_be_str := do |
| 37 | + (type_args, tgt) ← mk_local_pis_whnf type, |
| 38 | + tgt ← whnf tgt, |
| 39 | + let new_args := args ++ type_args, |
| 40 | + let lhs_ap := lhs.mk_app type_args, |
| 41 | + let rhs_ap := rhs.instantiate_lambdas_or_apps type_args, |
| 42 | + let str := tgt.get_app_fn.const_name, |
| 43 | + if e.is_structure str then do |
| 44 | + projs ← e.get_projections str, |
| 45 | + [intro] ← return $ e.constructors_of str | fail "unreachable code (3)", |
| 46 | + let params := get_app_args tgt, -- the parameters of the structure |
| 47 | + if is_constant_of (get_app_fn rhs_ap) intro then do -- if the value is a constructor application |
| 48 | + guard ((get_app_args rhs_ap).take params.length = params) <|> fail "unreachable code (1)", |
| 49 | + let rhs_args := (get_app_args rhs_ap).drop params.length, -- the fields of the structure |
| 50 | + guard (rhs_args.length = projs.length) <|> fail "unreachable code (2)", |
| 51 | + let pairs := projs.zip rhs_args, |
| 52 | + eta ← expr.is_eta_expansion_aux rhs_ap pairs, |
| 53 | + match eta with |
| 54 | + | none := |
| 55 | + pairs.mmap' $ λ ⟨proj, new_rhs⟩, do |
| 56 | + new_type ← infer_type new_rhs, |
| 57 | + b ← is_prop new_type, |
| 58 | + when ¬ b $ do -- if this field is a proposition, we skip it |
| 59 | + -- cannot use `mk_app` here, since the resulting application might still be a function. |
| 60 | + new_lhs ← mk_mapp proj $ (params ++ [lhs_ap]).map some, |
| 61 | + let new_nm := nm.append_suffix $ "_" ++ proj.last, |
| 62 | + add_projections e new_nm new_type new_lhs new_rhs new_args univs add_simp ff |
| 63 | + | (some eta_reduction) := add_projection nm tgt lhs_ap eta_reduction new_args univs add_simp |
| 64 | + end |
| 65 | + else (do |
| 66 | + when must_be_str $ |
| 67 | + fail "Invalid `simps` attribute. Body is not a constructor application", |
| 68 | + add_projection nm tgt lhs_ap rhs_ap new_args univs add_simp) |
| 69 | + else |
| 70 | + (do when must_be_str $ |
| 71 | + fail "Invalid `simps` attribute. Target is not a structure", |
| 72 | + add_projection nm tgt lhs_ap rhs_ap new_args univs add_simp) |
| 73 | + |
| 74 | +/-- Derive lemmas specifying the projections of the declaration. -/ |
| 75 | +meta def simps_tac (nm : name) (add_simp : bool) : tactic unit := do |
| 76 | + e ← get_env, |
| 77 | + d ← e.get nm, |
| 78 | + let lhs : expr := const d.to_name (d.univ_params.map level.param), |
| 79 | + add_projections e nm d.type lhs d.value [] d.univ_params add_simp tt |
| 80 | + |
| 81 | +reserve notation `lemmas_only` |
| 82 | +setup_tactic_parser |
| 83 | + |
| 84 | +/-- Automatically derive lemmas specifying the projections of this declaration. |
| 85 | + Example: (note that the forward and reverse functions are specified differently!) |
| 86 | + ``` |
| 87 | + @[simps] def refl (α) : α ≃ α := ⟨id, λ x, x, λ x, rfl, λ x, rfl⟩ |
| 88 | + ``` |
| 89 | + derives two simp-lemmas: |
| 90 | + ``` |
| 91 | + @[simp] lemma refl_to_fun (α) (x : α) : (refl α).to_fun x = id x |
| 92 | + @[simp] lemma refl_inv_fun (α) (x : α) : (refl α).inv_fun x = x |
| 93 | + ``` |
| 94 | + * It does not derive simp-lemmas for the prop-valued projections. |
| 95 | + * It will automatically reduce newly created beta-redexes, but not unfold any definitions. |
| 96 | + * If one of the fields itself is a structure, this command will recursively create |
| 97 | + simp-lemmas for all fields in that structure. |
| 98 | + * If one of the values is an eta-expanded structure, we will eta-reduce this structure. |
| 99 | + * You can use `@[simps lemmas_only]` to derive the lemmas, but not mark them |
| 100 | + as simp-lemmas. |
| 101 | + * If one of the projections is marked as a coercion, the generated lemmas do *not* use this |
| 102 | + coercion. |
| 103 | + * If one of the fields is a partially applied constructor, we will eta-expand it |
| 104 | + (this likely never happens). |
| 105 | + -/ |
| 106 | +@[user_attribute] meta def simps_attr : user_attribute unit (option unit) := |
| 107 | +{ name := `simps, |
| 108 | + descr := "Automatically derive lemmas specifying the projections of this declaration.", |
| 109 | + parser := optional (tk "lemmas_only"), |
| 110 | + after_set := some $ |
| 111 | + λ n _ _, option.is_none <$> simps_attr.get_param n >>= simps_tac n } |
0 commit comments