import DiffDog2.Experimental.NestedADFrontend open scoped F32Backend set_option synthInstance.maxSize 1024 namespace Examples.NestedDerivative abbrev C := LeanArray DDFloat32 /-- Reference expansion: the same carrier-polymorphic `f` is used directly and under AD. -/ private def explicitResult : C [] := ∂ (fun {R} [Carrier F32Sig R] [Interpretable F32Sig R] [R ⊒ C] (x : R []) => let f := fun {S} [Carrier F32Sig S] [Interpretable F32Sig S] [S ⊒ R] (y : S []) => (⇧x) * y (letI : R ⊒ R := ⟨id⟩; f x) + ∂ f x) (5 : C []) /-- A normal named polymorphic helper, defined outside the frontend and never rewritten. -/ private def genericSquare {R : Shape → Type} [Carrier F32Sig R] [Interpretable F32Sig R] (x : R []) : R [] := x * x private def genericBranch {R : Shape → Type} [Carrier F32Sig R] [Interpretable F32Sig R] (flag : Bool) (x : R []) : R [] := if flag then genericSquare x else x private structure ScalarRecord (A : Type) where value : A flag : Bool private abbrev Scalar (R : Shape → Type) := R [] private def explicitSquare {R : Shape → Type} [Carrier F32Sig R] [Interpretable F32Sig R] [R ⊒ C] (x : R []) : R [] := x * x private opaque opaqueGenericSquare {R : Shape → Type} [Carrier F32Sig R] [Interpretable F32Sig R] (x : R []) : R [] := x * x private opaque hostSquare (x : Float) : Float := x * x private opaque backendSquare (x : C []) : C [] := x * x -- Importing the frontend alone leaves ordinary notation and function lets unchanged. example : (∂ genericSquare (5 : C [])).unscalar = 10 := by native_decide example : (let increment n := n + 1; increment 2) = (3 : Nat) := rfl example : True := by fail_if_success have : C [] := ∂ (fun x => let f y := x * y; f x + ∂ f x) 5 trivial section Prototype -- Only this section opts into the frontend; ordinary `∂` is unchanged outside it. open scoped NestedAD -- One real shared let-bound f, used directly and under differentiation. def result : C [] := ∂ (fun x => let f y := x * y; f x + ∂ f x) 5 -- Numerical regressions are not analytic correctness proofs. #eval result.unscalar example : result.unscalar = 11 := by native_decide example : explicitResult.unscalar = 11 := by native_decide example : result = explicitResult := rfl -- With no expected type or typed argument, the frontend supplies its own Float32 carrier. private def defaultCarrier := ∂ (fun x => x * x) 5 example : defaultCarrier.unscalar = 10 := by native_decide -- Opening the scope still leaves lets outside AD callbacks to Lean. example : (let increment n := n + 1; increment 2) = (3 : Nat) := rfl -- Assert the elaborated result really retains a rank-2 let, rather than substituting f's source. run_cmd do let value := (← Lean.getConstInfo ``result).value! unless (value.find? fun e => match e with | .letE name type _ body _ => name == `f && type.isForall && body.hasLooseBVar 0 | _ => false).isSome do throwError "nested AD regression: missing shared polymorphic let f" -- Renaming has no effect; `g` remains one shared binding at another input. example : (∂ (fun a => let g b := a * b; g a + ∂ g a) 3).unscalar = 7 := by native_decide -- A parameter named like its captured parent shadows that parent, rather than being lifted. example : (∂ (fun x => let f x := x * x; f x + ∂ f x) 5).unscalar = 12 := by native_decide -- The scalar-let RHS sees the old x; its body sees the new x, not the enclosing parameter. example : (∂ (fun x => let f y := let x := x + y; x * x f x + ∂ f x) 5).unscalar = 44 := by native_decide -- A shared function closes over a scalar let. Its value is fixed at definition, not call, time. set_option linter.unusedVariables false in example : (∂ (fun x => let a := x + 1 let f := fun y => a * y let a := x * x f x + ∂ f a) 5).unscalar = 12 := by native_decide -- Inline AD automatically transports captures across multiple fresh carriers. example : (∂ (fun x => ∂ (fun y => ∂ (fun z => x * y * z) y) x) 5).unscalar = 1 := by native_decide -- A function from an ancestor is still shared when used two carriers deeper. example : (∂ (fun x => let f y := x * y * y ∂ (fun z => f z + ∂ f z) x) 5).unscalar = 22 := by native_decide -- User names must not collide with any generated carrier, eta, or capture binder. example : (∂ (fun R => let C capture := R * capture let argument := R C argument + ∂ C argument) 5).unscalar = 11 := by native_decide -- Functions can capture other functions, even when their source name is later shadowed. example : (∂ (fun x => let f y := x * y let g z := f z let f y := y * y g x + ∂ f x) 5).unscalar = 12 := by native_decide example : (∂ (fun x => -(x * x) / 2 - x) 3).unscalar = -4 := by native_decide -- Ordinary names, applications, type annotations, tuples, projections, pattern lets and locally -- constructed records are Lean syntax, not cases in the frontend. example : (∂ (fun x => let pair := (x, x * x) let (a, b) := pair let record := ScalarRecord.mk a true (fun (v : _) => if record.flag then (b + v : _) else v) record.value) 3).unscalar = 7 := by native_decide example : (∂ (fun x => genericSquare x + ∂ genericSquare x) 5).unscalar = 12 := by native_decide example : (∂ (fun x => opaqueGenericSquare x + ∂ opaqueGenericSquare x) 5).unscalar = 12 := by native_decide example : (∂ (fun x => let shared := @genericSquare; shared x + ∂ shared x) 5).unscalar = 12 := by native_decide example : (∂ (fun x => let f := (fun y => x * y); f x + ∂ f x) 5).unscalar = 11 := by native_decide private def outerCapture (a : C []) : C [] := ∂ (fun x => a * x) 5 example : (outerCapture 3).unscalar = 3 := by native_decide example : (∂ (genericBranch (flag := true)) (5 : C [])).unscalar = 10 := by native_decide -- The expected result, rather than a hard-coded C, determines an outer abstract carrier. private def genericOuter {R : Shape → Type} [Carrier F32Sig R] [Interpretable F32Sig R] (x : R []) : R [] := ∂ (fun y => y * y) x example : (genericOuter (5 : C [])).unscalar = 10 := by native_decide -- Here the derivative is inferred before its result has an expected type; its argument supplies R. private def inferredOuter {R : Shape → Type} [Carrier F32Sig R] [Interpretable F32Sig R] (x : R []) : R [] := let derivative := ∂ genericSquare x; derivative example : (inferredOuter (5 : C [])).unscalar = 10 := by native_decide -- Static host values (including a configuration tuple) are not lifted. An annotated Nat helper -- stays a Nat function, even though an untyped one-parameter AD function let is generalized. private def staticCaptures (count : Nat) (flag : Bool) (config : Nat × Bool) : C [] := ∂ (fun x => let bump (n : Nat) : Nat := n + config.1 let f y := if flag && config.2 then match bump count with | 0 => y | _ + 1 => x * y else y * y f x + ∂ f x) 5 example : (staticCaptures 2 true (1, true)).unscalar = 11 := by native_decide example : (staticCaptures 2 false (1, true)).unscalar = 12 := by native_decide -- Array aliases are recognized by WHNF type, not just the spelling R []. set_option linter.unusedVariables false in example : (∂ (fun x => let a : Scalar _ := x + 1; let f y := a * y; f x + ∂ f x) 5).unscalar = 12 := by native_decide -- A macro-introduced x and a source x have different binding identities despite the same print name. local macro "captureSource " value:term : term => `(let x : Nat := 100; ∂ (fun y => $value * y) 3) set_option linter.unusedVariables false in example : (∂ (fun x => captureSource x) 5).unscalar = 1 := by native_decide local macro "twoSourceCaptures " value:term : term => `(let x := $value + 1; ∂ (fun y => x * y + $value * y) 3) example : (∂ (fun x => twoSourceCaptures x) 5).unscalar = 2 := by native_decide -- Explicit rank-2 functions still use Lean's ordinary binder elaboration. example : (∂ (fun {R} [Carrier F32Sig R] [Interpretable F32Sig R] [R ⊒ C] (x : R []) => x * x) 5).unscalar = 10 := by native_decide example : (∂ (@explicitSquare) (5 : C [])).unscalar = 10 := by native_decide -- Explicit nested callbacks retain their own frame; explicit captures must not be double-lifted. example : (∂ (fun x => ∂ (fun {S} [Carrier F32Sig S] [Interpretable F32Sig S] [S ⊒ _] (y : S []) => let f := fun {T} [Carrier F32Sig T] [Interpretable F32Sig T] [T ⊒ S] (z : T []) => (⇧y) * z (⇧x) * y + f y + ∂ f y) x) 5).unscalar = 3 := by native_decide -- Bare/parenthesized/partially applied forms use the existing unary library prefix parser. -- Only the fully applied two-operand form provides this prototype's automatic boundaries. private def bareDerivative : C [] → C [] := ∂ genericSquare example : (bareDerivative 5).unscalar = 10 := by native_decide example : ((∂ genericSquare) (5 : C [])).unscalar = 10 := by native_decide example : ((List.map (∂ genericSquare) [(3 : C [])]).map LeanArray.unscalar) = [6] := by native_decide -- Partial application does not activate the outer automatic boundary in the nested sugar. example : True := by fail_if_success have : C [] := (∂ (fun x => let f y := x * y; f x + ∂ f x)) 5 trivial -- Real transport limits: outer containers are not reconstructed. Destructure array leaves before -- entering the next boundary. Records/tuples built *inside* a boundary work, as tested above. example : True := by fail_if_success have : C [] := ∂ (fun x => let pair := (x, true) ∂ (fun y => pair.1 * y) x) 5 trivial example : True := by fail_if_success have : C [] := ∂ (fun x => let record := ScalarRecord.mk x true ∂ (fun y => record.value * y) x) 5 trivial set_option linter.unusedVariables false in example : (∂ (fun x => let pair := (x, true) let (a, flag) := pair ∂ (fun y => if flag then a * y else y) x) 5).unscalar = 1 := by native_decide -- Opaque/first-class monomorphic functions cannot satisfy the generative carrier contract. Their -- bodies are never inspected to manufacture polymorphism, even for this backend's scalar arrays. example : True := by fail_if_success have : C [] := ∂ hostSquare 5 fail_if_success have : C [] := ∂ backendSquare 5 fail_if_success have : C [] := ∂ (fun (x : C []) => x) 5 fail_if_success have : C [] := ∂ (fun x y => x * y) 5 trivial end Prototype -- The automatic boundaries are unavailable again after the scoped section. example : True := by fail_if_success have : C [] := ∂ (fun x => let f y := x * y; f x + ∂ f x) 5 trivial -- This non-prototype rank-2 lambda verifies that the ordinary prefix notation is restored. example : (∂ (fun {R} [Carrier F32Sig R] [Interpretable F32Sig R] [R ⊒ C] (x : R []) => x * x) (5 : C [])).unscalar = 10 := by native_decide example : (∂ genericSquare (5 : C [])).unscalar = 10 := by native_decide -- Neither reflexive nor transitive instances escape the generated body scopes. example : True := by fail_if_success have : C ⊒ C := inferInstance trivial example {R S T : Shape → Type} [S ⊒ R] [T ⊒ S] : True := by fail_if_success have : T ⊒ R := inferInstance trivial -- The ordinary let elaborator is also restored. example : (let increment n := n + 1; increment 2) = (3 : Nat) := rfl end Examples.NestedDerivative