Into to Lean 4 and

its use in scientific computing

Tomáš Skřivan

Carnegie Mellon University

Hoskinson Center for Formal Mathematics

15.5.2024

Natural numbers

define natural numbers and

prove commutativity of addition

Symbolic and automatic

differentiation

def foo (x : R) : R := x^2
def foo_deriv (x : R) : R := 2*x

AD

  • automatic differentiation (AD) synthesizes derivatives of a program
  • application in machine learning, optimization, and scientific computing

Symbolic and automatic

differentiation

def foo (x : R) : R := x^2
noncomputable
def foo_deriv (x : R) : R := fderiv R (fun x' => foo x') x 1
noncomputable
def fderiv (f : X → Y) (x : X) : X →L[𝕜] Y :=
  if h : ∃ (f' : X →L[𝕜] Y), (fun x' => f x' - f x - f' (x' - x)) =o[𝓝 x] fun x' => x' - x then
    choose h
  else 
  	0
  • definition of derivative in mathlib
  • using mathlib derivative
def foo_deriv (x : R) : R := (fderiv R (fun x' => foo x') x 1) rewrite_by fun_trans
  • fun_trans is a general tactic for function transformation

General function transformation

  • setting up fderiv as function transformation
  • definition:
@[fun_trans]
noncomputable
def fderiv (f : X → Y) (x : X) : X →L[𝕜] Y := ...
@[fun_trans]
theorem fderiv_add :
    (fderiv K fun x => f x + g x)
    =
    fun x => fderiv K f x + fderiv K g x := ...
@[fun_trans]
theorem fderiv_comp :
    fderiv 𝕜 (fun x => (f (g x))) 
    = 
    fun x => fun dx =>L[K] fderiv K f (g x) (fderiv K g x dx) := ...
@[fun_trans]
theorem fderiv_id :
    (fderiv K fun x : X => x) = fun _ => fun dx =>L[K] dx := ...
  • transformation rules:
  • code demo

Array expression optimization

  • custom interactive optimizations
  • example on basic linear algebra operation saxpy
def saxpy (a : Float) (x y : Float^[n]) := a•x+y
def saxpy_optimized (a : Float) (x y : Float^[n]) := x.mapIdx (fun i xi => a*xi + y[i])

?

def_optimize saxpy by
  simp only [HAdd.hAdd,Add.add,HSMul.hSMul,SMul.smul]
  simp only [mapMono_mapIdxMono]
  • semi-automatic with def_optimize macro
  • code demo

Lean 4

  • purely functional programming language with dependent types
    • interactive theorem prover
    • general purpose programming language
  • mathlib
    •  large library of formalized mathematics
    • active community
  • easily extensible - Lean is written in Leanb
  • support for imperative code with mutable variables
  • support for FFI through C