|
| 1 | +/- |
| 2 | +Copyright (c) 2019 Robert Y. Lewis. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Mario Carneiro, Simon Hudon, Scott Morrison, Keeley Hoek, Robert Y. Lewis, Floris van Doorn, E.W.Ayers |
| 5 | +-/ |
| 6 | +import Lean |
| 7 | + |
| 8 | +/-! |
| 9 | +# Additional operations on Expr and related types |
| 10 | +
|
| 11 | +This file defines basic operations on the types expr, name, declaration, level, environment. |
| 12 | +
|
| 13 | +This file is mostly for non-tactics. |
| 14 | +-/ |
| 15 | + |
| 16 | +namespace Lean |
| 17 | + |
| 18 | +namespace BinderInfo |
| 19 | + |
| 20 | +/-! ### Declarations about `BinderInfo` -/ |
| 21 | + |
| 22 | +/-- The brackets corresponding to a given `BinderInfo`. -/ |
| 23 | +def brackets : BinderInfo → String × String |
| 24 | +| BinderInfo.implicit => ("{", "}") |
| 25 | +| BinderInfo.strictImplicit => ("{{", "}}") |
| 26 | +| BinderInfo.instImplicit => ("[", "]") |
| 27 | +| _ => ("(", ")") |
| 28 | + |
| 29 | +end BinderInfo |
| 30 | + |
| 31 | +namespace Name |
| 32 | + |
| 33 | +/-! ### Declarations about `name` -/ |
| 34 | + |
| 35 | +/-- Find the largest prefix `n` of a `Name` such that `f n != none`, then replace this prefix |
| 36 | +with the value of `f n`. -/ |
| 37 | +def mapPrefix (f : Name → Option Name) (n : Name) : Name := Id.run do |
| 38 | + if let some n' := f n then return n' |
| 39 | + match n with |
| 40 | + | anonymous => anonymous |
| 41 | + | str n' s _ => mkStr (mapPrefix f n') s |
| 42 | + | num n' i _ => mkNum (mapPrefix f n') i |
| 43 | + |
| 44 | +end Name |
| 45 | + |
| 46 | + |
| 47 | +namespace ConstantInfo |
| 48 | + |
| 49 | +def isDef : ConstantInfo → Bool |
| 50 | + | defnInfo _ => true |
| 51 | + | _ => false |
| 52 | + |
| 53 | +def isThm : ConstantInfo → Bool |
| 54 | + | thmInfo _ => true |
| 55 | + | _ => false |
| 56 | + |
| 57 | +def updateName : ConstantInfo → Name → ConstantInfo |
| 58 | + | defnInfo info, n => defnInfo {info with name := n} |
| 59 | + | axiomInfo info, n => axiomInfo {info with name := n} |
| 60 | + | thmInfo info, n => thmInfo {info with name := n} |
| 61 | + | opaqueInfo info, n => opaqueInfo {info with name := n} |
| 62 | + | quotInfo info, n => quotInfo {info with name := n} |
| 63 | + | inductInfo info, n => inductInfo {info with name := n} |
| 64 | + | ctorInfo info, n => ctorInfo {info with name := n} |
| 65 | + | recInfo info, n => recInfo {info with name := n} |
| 66 | + |
| 67 | +def updateType : ConstantInfo → Expr → ConstantInfo |
| 68 | + | defnInfo info, y => defnInfo {info with type := y} |
| 69 | + | axiomInfo info, y => axiomInfo {info with type := y} |
| 70 | + | thmInfo info, y => thmInfo {info with type := y} |
| 71 | + | opaqueInfo info, y => opaqueInfo {info with type := y} |
| 72 | + | quotInfo info, y => quotInfo {info with type := y} |
| 73 | + | inductInfo info, y => inductInfo {info with type := y} |
| 74 | + | ctorInfo info, y => ctorInfo {info with type := y} |
| 75 | + | recInfo info, y => recInfo {info with type := y} |
| 76 | + |
| 77 | +def updateValue : ConstantInfo → Expr → ConstantInfo |
| 78 | + | defnInfo info, v => defnInfo {info with value := v} |
| 79 | + | thmInfo info, v => thmInfo {info with value := v} |
| 80 | + | opaqueInfo info, v => opaqueInfo {info with value := v} |
| 81 | + | d, v => d |
| 82 | + |
| 83 | +def toDeclaration! : ConstantInfo → Declaration |
| 84 | + | defnInfo info => Declaration.defnDecl info |
| 85 | + | thmInfo info => Declaration.thmDecl info |
| 86 | + | axiomInfo info => Declaration.axiomDecl info |
| 87 | + | opaqueInfo info => Declaration.opaqueDecl info |
| 88 | + | quotInfo info => panic! "toDeclaration for quotInfo not implemented" |
| 89 | + | inductInfo info => panic! "toDeclaration for inductInfo not implemented" |
| 90 | + | ctorInfo info => panic! "toDeclaration for ctorInfo not implemented" |
| 91 | + | recInfo info => panic! "toDeclaration for recInfo not implemented" |
| 92 | + |
| 93 | +end ConstantInfo |
| 94 | + |
| 95 | +namespace Expr |
| 96 | + |
| 97 | +/-! ### Declarations about `Expr` -/ |
| 98 | + |
| 99 | +/-- If the expression is a constant, return that name. Otherwise return `Name.anonymous`. -/ |
| 100 | +def constName (e : Expr) : Name := |
| 101 | + e.constName?.getD Name.anonymous |
| 102 | + |
| 103 | +def bvarIdx? : Expr → Option Nat |
| 104 | + | bvar idx _ => some idx |
| 105 | + | _ => none |
| 106 | + |
| 107 | +/-- Return the function (name) and arguments of an application. -/ |
| 108 | +def getAppFnArgs (e : Expr) : Name × Array Expr := |
| 109 | + withApp e λ e a => (e.constName, a) |
| 110 | + |
| 111 | +/-- Turn an expression that is a natural number literal into a natural number. -/ |
| 112 | +def natLit! : Expr → Nat |
| 113 | + | lit (Literal.natVal v) _ => v |
| 114 | + | _ => panic! "nat literal expected" |
| 115 | + |
| 116 | +/-- Returns a `NameSet` of all constants in an expression starting with a certain prefix. -/ |
| 117 | +def listNamesWithPrefix (pre : Name) (e : Expr) : NameSet := |
| 118 | + e.foldConsts ∅ fun n l => if n.getPrefix == pre then l.insert n else l |
| 119 | + |
| 120 | +def modifyAppArgM [Functor M] [Pure M] (modifier : Expr → M Expr) : Expr → M Expr |
| 121 | + | app f a _ => mkApp f <$> modifier a |
| 122 | + | e => pure e |
| 123 | + |
| 124 | +def modifyAppArg (modifier : Expr → Expr) : Expr → Expr := |
| 125 | + modifyAppArgM (M := Id) modifier |
| 126 | + |
| 127 | +def modifyRevArg (modifier : Expr → Expr): Nat → Expr → Expr |
| 128 | + | 0 => modifyAppArg modifier |
| 129 | + | (i+1) => modifyAppArg (modifyRevArg modifier i) |
| 130 | + |
| 131 | +/-- Given `f a₀ a₁ ... aₙ₋₁`, runs `modifier` on the `i`th argument or returns the original expression if out of bounds. -/ |
| 132 | +def modifyArg (modifier : Expr → Expr) (e : Expr) (i : Nat) (n := e.getAppNumArgs) : Expr := |
| 133 | + modifyRevArg modifier (n - i - 1) e |
| 134 | + |
| 135 | +def getRevArg? : Expr → Nat → Option Expr |
| 136 | + | app f a _, 0 => a |
| 137 | + | app f _ _, i+1 => getRevArg! f i |
| 138 | + | _, _ => none |
| 139 | + |
| 140 | +/-- Given `f a₀ a₁ ... aₙ₋₁`, returns the `i`th argument or none if out of bounds. -/ |
| 141 | +def getArg? (e : Expr) (i : Nat) (n := e.getAppNumArgs): Option Expr := |
| 142 | + getRevArg? e (n - i - 1) |
| 143 | + |
| 144 | +/-- Given `f a₀ a₁ ... aₙ₋₁`, runs `modifier` on the `i`th argument. |
| 145 | +An argument `n` may be provided which says how many arguments we are expecting `e` to have. -/ |
| 146 | +def modifyArgM [Monad M] (modifier : Expr → M Expr) (e : Expr) (i : Nat) (n := e.getAppNumArgs) : M Expr := do |
| 147 | + let some a := getArg? e i | return e |
| 148 | + let a ← modifier a |
| 149 | + return modifyArg (fun _ => a) e i n |
| 150 | + |
| 151 | +end Expr |
| 152 | + |
| 153 | +end Lean |
0 commit comments