|
| 1 | +/- |
| 2 | +Copyright (c) 2017 Mario Carneiro. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Mario Carneiro, David Renshaw |
| 5 | +-/ |
| 6 | +import Lean.Elab.Command |
| 7 | +import Lean.Elab.Term |
| 8 | +import Lean |
| 9 | + |
| 10 | +/-! |
| 11 | +# The `alias` command |
| 12 | +
|
| 13 | +This file defines an `alias` command, which can be used to create copies |
| 14 | +of a theorem or definition with different names. |
| 15 | +
|
| 16 | +Syntax: |
| 17 | +
|
| 18 | +```lean |
| 19 | +/-- doc string -/ |
| 20 | +alias my_theorem ← alias1 alias2 ... |
| 21 | +``` |
| 22 | +
|
| 23 | +This produces defs or theorems of the form: |
| 24 | +
|
| 25 | +```lean |
| 26 | +/-- doc string -/ |
| 27 | +theorem alias1 : <type of my_theorem> := my_theorem |
| 28 | +
|
| 29 | +/-- doc string -/ |
| 30 | +theorem alias2 : <type of my_theorem> := my_theorem |
| 31 | +``` |
| 32 | +
|
| 33 | +Iff alias syntax: |
| 34 | +
|
| 35 | +```lean |
| 36 | +alias A_iff_B ↔ B_of_A A_of_B |
| 37 | +alias A_iff_B ↔ .. |
| 38 | +``` |
| 39 | +
|
| 40 | +This gets an existing biconditional theorem `A_iff_B` and produces |
| 41 | +the one-way implications `B_of_A` and `A_of_B` (with no change in |
| 42 | +implicit arguments). A blank `_` can be used to avoid generating one direction. |
| 43 | +The `..` notation attempts to generate the 'of'-names automatically when the |
| 44 | +input theorem has the form `A_iff_B` or `A_iff_B_left` etc. |
| 45 | +-/ |
| 46 | + |
| 47 | +namespace Tactic |
| 48 | +namespace Alias |
| 49 | + |
| 50 | +open Lean |
| 51 | + |
| 52 | +/-- Adds some copies of a theorem or definition. -/ |
| 53 | +syntax (name := alias) "alias " ident " ← " ident* : command |
| 54 | + |
| 55 | +/-- Adds one-way implication declarations. -/ |
| 56 | +syntax (name := aliasLR) "alias " ident " ↔ " binderIdent binderIdent : command |
| 57 | + |
| 58 | +/-- Adds one-way implication declarations, inferring names for them. -/ |
| 59 | +syntax (name := aliasLRDots) "alias " ident " ↔ " ".." : command |
| 60 | + |
| 61 | +/-- Like `++`, except that if the right argument starts with `_root_` the namespace will be |
| 62 | +ignored. |
| 63 | +``` |
| 64 | +appendNamespace `a.b `c.d = `a.b.c.d |
| 65 | +appendNamespace `a.b `_root_.c.d = `c.d |
| 66 | +``` |
| 67 | +
|
| 68 | +TODO: Move this declaration to a more central location. |
| 69 | +-/ |
| 70 | +def appendNamespace (ns : Name) : Name → Name |
| 71 | +| Name.str Name.anonymous s _ => if s = "_root_" then Name.anonymous else Name.mkStr ns s |
| 72 | +| Name.str p s _ => Name.mkStr (appendNamespace ns p) s |
| 73 | +| Name.num p n _ => Name.mkNum (appendNamespace ns p) n |
| 74 | +| Name.anonymous => ns |
| 75 | + |
| 76 | +/-- Elaborates an `alias ←` command. -/ |
| 77 | +@[commandElab «alias»] def elabAlias : Elab.Command.CommandElab |
| 78 | +| `(alias $name:ident ← $aliases:ident*) => do |
| 79 | + let resolved ← resolveGlobalConstNoOverload name |
| 80 | + let constant ← getConstInfo resolved |
| 81 | + let ns ← getCurrNamespace |
| 82 | + |
| 83 | + for a in aliases do |
| 84 | + let decl ← match constant with |
| 85 | + | Lean.ConstantInfo.defnInfo d => |
| 86 | + pure $ .defnDecl { |
| 87 | + d with name := (appendNamespace ns a.getId) |
| 88 | + value := mkConst resolved (d.levelParams.map mkLevelParam) |
| 89 | + } |
| 90 | + | Lean.ConstantInfo.thmInfo t => |
| 91 | + pure $ .thmDecl { |
| 92 | + t with name := (appendNamespace ns a.getId) |
| 93 | + value := mkConst resolved (t.levelParams.map mkLevelParam) |
| 94 | + } |
| 95 | + | _ => throwError "alias only works with def or theorem" |
| 96 | + |
| 97 | + -- TODO add @alias attribute |
| 98 | + Lean.addDecl decl |
| 99 | + |
| 100 | +| _ => Lean.Elab.throwUnsupportedSyntax |
| 101 | + |
| 102 | + |
| 103 | +/-- |
| 104 | + Given a possibly forall-quantified iff expression `prf`, produce a value for one |
| 105 | + of the implication directions (determined by `mp`). |
| 106 | +-/ |
| 107 | +def mkIffMpApp (mp : Bool) (prf : Expr) : MetaM Expr := do |
| 108 | + Meta.forallTelescope (← Meta.inferType prf) fun xs ty => do |
| 109 | + let some (lhs, rhs) := ty.iff? |
| 110 | + | throwError "Target theorem must have the form `∀ x y z, a ↔ b`" |
| 111 | + Meta.mkLambdaFVars xs <| |
| 112 | + mkApp3 (mkConst (if mp then ``Iff.mp else ``Iff.mpr)) lhs rhs (mkAppN prf xs) |
| 113 | + |
| 114 | +/-- |
| 115 | + Given a constant representing an iff decl, adds a decl for one of the implication |
| 116 | + directions. |
| 117 | +-/ |
| 118 | +def aliasIff (ci : ConstantInfo) (al : Name) (isForward : Bool) : MetaM Unit := do |
| 119 | + let ls := ci.levelParams |
| 120 | + let v ← mkIffMpApp isForward ci.value! |
| 121 | + let t' ← Meta.inferType v |
| 122 | + -- TODO add @alias attribute |
| 123 | + addDecl $ .thmDecl { |
| 124 | + name := al |
| 125 | + value := v |
| 126 | + type := t' |
| 127 | + levelParams := ls |
| 128 | + } |
| 129 | + |
| 130 | +/-- Elaborates an `alias ↔` command. -/ |
| 131 | +@[commandElab aliasLR] def elabAliasLR : Lean.Elab.Command.CommandElab |
| 132 | +| `(alias $name:ident ↔ $left:binderIdent $right:binderIdent ) => do |
| 133 | + let resolved ← resolveGlobalConstNoOverload name |
| 134 | + let constant ← getConstInfo resolved |
| 135 | + let ns ← getCurrNamespace |
| 136 | + |
| 137 | + Lean.Elab.Command.liftTermElabM none do |
| 138 | + if let `(binderIdent| $x:ident) := left |
| 139 | + then aliasIff constant (appendNamespace ns x.getId) true |
| 140 | + |
| 141 | + if let `(binderIdent| $x:ident) := right |
| 142 | + then aliasIff constant (appendNamespace ns x.getId) false |
| 143 | + |
| 144 | +| _ => Lean.Elab.throwUnsupportedSyntax |
| 145 | + |
| 146 | +/-- Elaborates an `alias ↔ ..` command. -/ |
| 147 | +@[commandElab aliasLRDots] def elabAliasLRDots : Lean.Elab.Command.CommandElab |
| 148 | +| `(alias $name:ident ↔ ..) => do |
| 149 | + let resolved ← resolveGlobalConstNoOverload name |
| 150 | + let constant ← getConstInfo resolved |
| 151 | + |
| 152 | + let (parent, base) ← match resolved with |
| 153 | + | Name.str n s _ => pure (n,s) |
| 154 | + | _ => throwError "alias only works for string names" |
| 155 | + |
| 156 | + let components := base.splitOn "_iff_" |
| 157 | + if components.length != 2 then throwError "LHS must be of the form *_iff_*" |
| 158 | + let forward := String.intercalate "_of_" components.reverse |
| 159 | + let backward := String.intercalate "_of_" components |
| 160 | + let forwardName := Name.mkStr parent forward |
| 161 | + let backwardName := Name.mkStr parent backward |
| 162 | + |
| 163 | + Lean.Elab.Command.liftTermElabM none do |
| 164 | + aliasIff constant forwardName true |
| 165 | + aliasIff constant backwardName false |
| 166 | + |
| 167 | +| _ => Lean.Elab.throwUnsupportedSyntax |
| 168 | + |
| 169 | +end Alias |
| 170 | +end Tactic |
0 commit comments