mathlib4 icon indicating copy to clipboard operation
mathlib4 copied to clipboard

feat: deriving optics

Open EdAyers opened this issue 3 years ago β€’ 2 comments

This file defines the derive_optics T command where T is an inductive datatype. For each constructor 𝑐 of T and each field π‘Ž : Ξ± of 𝑐, this will create the following definitions:

  1. T.𝑐.π‘Ž? : T β†’ Option Ξ±
  2. T.𝑐.π‘Ž! : T β†’ Ξ±
  3. T.𝑐.withπ‘Ž : Ξ± β†’ T β†’ T
  4. T.𝑐.modifyπ‘Ž : (Ξ± β†’ Ξ±) β†’ T β†’ T
  5. T.𝑐.modifyMπ‘Ž : (Ξ± β†’ M Ξ±) β†’ T β†’ M T

This eliminates some boilerplate when defining new inductive datatypes.

Additionally, if T has multiple constructors with the same field name π‘Ž, it will create shortened versions of these (T.π‘Ž?, T.π‘Ž! etc).

EdAyers avatar Jun 07 '22 02:06 EdAyers

Oops, @EdAyers, I see you removed the awaiting-review tag yourself. What is the status of this PR?

kim-em avatar Jul 27 '22 02:07 kim-em

Basically, I wasn't sure what the protocol was for mathlib4. I am also not sure whether this belongs in mathlib4.

EdAyers avatar Jul 27 '22 08:07 EdAyers

How does modify behave if other fields depend on this one?

A semi-offtopic idea. There is .copy function used many times in mathlib that updates data fields to propositionally (but probably not definitionally) equal terms. See, e.g., local_equiv.copy. How hard would it be for you to write a command that generates this copy command (plus copy_eq and something like coe_copy/copy_apply/...)? Ideally, it should use simp-normal forms for projections (e.g., coercion instead to toFun) based on simps machinery.

urkud avatar Nov 25 '22 03:11 urkud