|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Leonardo de Moura |
| 5 | +-/ |
| 6 | +module |
| 7 | +prelude |
| 8 | +public import Lean.Elab.Command |
| 9 | +meta import Lean.Elab.Command |
| 10 | +public import Lean.Data.KVMap |
| 11 | +public section |
| 12 | +namespace Lean.Elab |
| 13 | +open Command Meta |
| 14 | + |
| 15 | +/-- |
| 16 | +Generates a function `setterName` for updating the `Bool` and `Nat` fields |
| 17 | +of the structure `struct`. |
| 18 | +This is a very simple implementation. There is no support for subobjects. |
| 19 | +-/ |
| 20 | +meta def mkConfigSetter (doc? : Option (TSyntax ``Parser.Command.docComment)) |
| 21 | + (setterName struct : Ident) : CommandElabM Unit := do |
| 22 | + let structName ← resolveGlobalConstNoOverload struct |
| 23 | + let .inductInfo val ← getConstInfo structName |
| 24 | + | throwErrorAt struct "`{structName}` is not s structure" |
| 25 | + unless val.levelParams.isEmpty do |
| 26 | + throwErrorAt struct "`{structName}` is universe polymorphic" |
| 27 | + unless val.numIndices == 0 && val.numParams == 0 do |
| 28 | + throwErrorAt struct "`{structName}` must not be parametric" |
| 29 | + let env ← getEnv |
| 30 | + let some structInfo := getStructureInfo? env structName |
| 31 | + | throwErrorAt struct "`{structName}` is not a structure" |
| 32 | + let code : Term ← liftTermElabM do |
| 33 | + let mut code : Term ← `(throwError "invalid configuration option `{fieldName}`") |
| 34 | + for fieldInfo in structInfo.fieldInfo do |
| 35 | + if fieldInfo.subobject?.isSome then continue -- ignore subobject's |
| 36 | + let projInfo ← getConstInfo fieldInfo.projFn |
| 37 | + let fieldType ← forallTelescope projInfo.type fun _ body => pure body |
| 38 | + -- **Note**: We only support `Nat` and `Bool` fields |
| 39 | + let fieldIdent : Ident := mkCIdent fieldInfo.fieldName |
| 40 | + if fieldType.isConstOf ``Nat then |
| 41 | + code ← `(if fieldName == $(quote fieldInfo.fieldName) then |
| 42 | + return { s with $fieldIdent:ident := (← getNatField) } |
| 43 | + else $code) |
| 44 | + else if fieldType.isConstOf ``Bool then |
| 45 | + code ← `(if fieldName == $(quote fieldInfo.fieldName) then |
| 46 | + return { s with $fieldIdent:ident := (← getBoolField) } |
| 47 | + else $code) |
| 48 | + return code |
| 49 | + let cmd ← `(command| |
| 50 | + $[$doc?:docComment]? |
| 51 | + def $setterName (s : $struct) (fieldName : Name) (val : DataValue) : CoreM $struct := |
| 52 | + let getBoolField : CoreM Bool := do |
| 53 | + let .ofBool b := val | throwError "`{fieldName}` is a Boolean" |
| 54 | + return b |
| 55 | + let getNatField : CoreM Nat := do |
| 56 | + let .ofNat n := val | throwError "`{fieldName}` is a natural number" |
| 57 | + return n |
| 58 | + $code |
| 59 | + ) |
| 60 | + elabCommand cmd |
| 61 | + |
| 62 | +elab (name := elabConfigGetter) doc?:(docComment)? "declare_config_getter" setterName:ident type:ident : command => do |
| 63 | + mkConfigSetter doc? setterName type |
| 64 | + |
| 65 | +end Lean.Elab |
0 commit comments