## Phase 104: Kalika Formal Verification & AI Research — Lean 4 Bridge, Automated Theorem Proving, Autonomous Research Agents, Conjecture Generation & Symbolic Regression

Implements the AI and formal reasoning layers that make Kalika more than a
computation engine — an active research partner. Targets parity with or
exceeding: Lean 4/Mathlib (formal verification), AlphaProof/DeepSeek-Prover (AI
theorem proving), LeanDojo/ReProver (retrieval-augmented proving), PySR
(symbolic regression), FunSearch (conjecture generation), and the emerging
generation of AI research assistants.

---

### 104.1 Formal Verification Bridge (`@kalika/formal-verification`)

#### 104.1.1 Lean 4 Integration

- [x] **104.1.1.1** Implement Lean 4 process manager: spawn and manage Lean 4
      REPL processes (lean4-repl or direct lean --run), send tactic commands,
      receive proof states (goals, hypotheses, local context), parse error
      messages, handle timeouts — providing a programmatic interface to Lean 4
      matching LeanDojo's interaction model
- [x] **104.1.1.2** Implement Kalika-to-Lean translator: convert Kalika `Expr`
      AST nodes to Lean 4 syntax (terms, types, propositions), map Kalika types
      to Lean/Mathlib types (Kalika integer -> Lean Int/Nat, Kalika rational ->
      Lean Rat, Kalika real -> Lean Real, Kalika matrix -> Lean Matrix, Kalika
      group -> Lean Group), handle namespace imports from Mathlib
- [x] **104.1.1.3** Implement Lean-to-Kalika translator: parse Lean 4 terms and
      convert back to Kalika `Expr`, extract proof terms, convert Lean error
      messages to Kalika diagnostic types
- [x] **104.1.1.4** Implement derivation-to-tactic compiler: given a Kalika
      `ProvenResult` with its derivation chain, generate a Lean 4 tactic proof
      that verifies the same result — translating each CAS simplification step
      to corresponding Lean tactics (simp, ring, norm_num, linarith, nlinarith,
      polyrith, field_simp, etc.)
- [x] **104.1.1.5** Implement Mathlib lemma database: index of Mathlib theorems
      with their types, searchable by conclusion pattern (matching `exact?`), by
      hypothesis pattern, and by keyword — cached locally for fast
      retrieval-augmented proving
- [x] **104.1.1.6** Implement verification pipeline: given any Kalika
      computation result, (1) translate to Lean 4 statement, (2) attempt
      automated proof via tactic generation, (3) if successful, mark result as
      `lean4-verified`, (4) if unsuccessful, flag for human review — providing
      formal verification as a service for any CAS computation

#### 104.1.2 Cross-CAS Verification

- [x] **104.1.2.1** Implement cross-CAS verification: for critical results,
      independently verify using external CAS (SymPy via Python subprocess,
      SageMath via sage subprocess, or Mathematica via WolframScript if
      available) — comparing symbolic results for equality, numerical results
      for agreement within ball-arithmetic error bounds
- [x] **104.1.2.2** Implement numerical spot-checking: for any symbolic result,
      evaluate both sides numerically at 10+ random points using ball arithmetic
      (from @kalika/cas-engine), verify agreement within computed error bounds —
      a fast sanity check that catches most algebraic errors

---

### 104.2 AI Theorem Proving (`@kalika/research-agents`)

#### 104.2.1 Tactic Generation Model

- [x] **104.2.1.1** Implement tactic prediction pipeline: given a Lean 4 proof
      state (goal type, hypotheses, local context), generate candidate tactic
      suggestions using LLM (Claude API via @shared/ai, with Lean-4-specific
      system prompt and few-shot examples), rank by estimated success
      probability, and attempt each in order — matching COPRA's LLM-as-copilot
      approach
- [x] **104.2.1.2** Implement retrieval-augmented proving: before generating
      tactics, retrieve top-k relevant lemmas from the Mathlib index (using
      embedding similarity — encode proof state and lemma types into vectors via
      embedding model, find nearest neighbors) — matching ReProver's approach,
      which dramatically improves success rates by providing relevant context
- [x] **104.2.1.3** Implement proof search tree: breadth-first and best-first
      search over tactic applications, with pruning (abandon branches where
      proof state becomes more complex), depth limiting, and timeout management
      — matching HyperTree Proof Search's tree exploration strategy
- [x] **104.2.1.4** Implement proof repair: when a proof attempt fails, analyze
      the error, backtrack to the last successful state, and try alternative
      tactics — with learning from failed attempts (avoid repeating the same
      failed tactic pattern)

#### 104.2.2 Automated Reasoning Integration

- [x] **104.2.2.1** Implement SMT solver interface: translate Kalika
      propositions (equalities, inequalities, Boolean combinations) to SMT-LIB
      format, dispatch to Z3 or CVC5 (via subprocess or WASM compilation), parse
      sat/unsat results and extract models/proofs — for automated verification
      of arithmetic and algebraic propositions
- [x] **104.2.2.2** Implement first-order ATP interface: translate universally
      quantified Kalika propositions to TPTP format, dispatch to E Prover or
      Vampire (via subprocess), reconstruct proofs from ATP output — for
      automated verification of first-order logical statements
- [x] **104.2.2.3** Implement decision procedures: Presburger arithmetic
      (quantifier elimination for linear integer arithmetic), Tarski's
      quantifier elimination for real-closed fields, Groebner basis for
      polynomial ideal membership, cylindrical algebraic decomposition (CAD) for
      quantified polynomial constraints — built directly into the CAS engine for
      fast automated reasoning

---

### 104.3 Autonomous Research Agents

#### 104.3.1 Agent Architecture

- [x] **104.3.1.1** Implement research agent base class: goal specification
      (natural language research question + formal constraints), planning
      (decompose goal into sub-goals), execution (invoke CAS computations,
      literature search, formal verification), reflection (evaluate progress,
      identify dead ends, adjust strategy), and reporting (summarize findings
      with proofs and citations)
- [x] **104.3.1.2** Implement computation agent: takes a mathematical
      computation task (evaluate integral, solve equation, compute invariant),
      selects appropriate CAS operations, executes with timeout and retry logic,
      verifies result (numerical spot-check + optional formal verification), and
      returns ProvenResult — the basic "calculator" agent
- [x] **104.3.1.3** Implement literature agent: given a mathematical topic or
      result, searches arXiv, INSPIRE-HEP, Semantic Scholar, and MathSciNet (via
      API) for relevant papers, extracts key results/theorems/techniques,
      summarizes the state of the art, identifies open problems and gaps —
      providing automated literature review for any research topic
- [x] **104.3.1.4** Implement exploration agent: given a mathematical structure
      or physical system, systematically explores its properties — computes
      invariants, tests conjectures, searches for patterns, and generates
      hypotheses. Uses a combination of symbolic computation (CAS), numerical
      experimentation (parameter sweeps), and AI pattern recognition (LLM-based
      hypothesis generation)
- [x] **104.3.1.5** Implement proof agent: given a mathematical conjecture (as a
      formal statement or natural language claim), attempts to prove or disprove
      it using a combination of: CAS computation (checking special cases,
      computing invariants), LLM-guided tactic generation (for formal proofs in
      Lean 4), and automated reasoning (SMT/ATP) — matching AlphaProof's
      approach but more modular

#### 104.3.2 Agent Collaboration

- [x] **104.3.2.1** Implement multi-agent orchestrator: decompose complex
      research questions into sub-tasks, assign to specialized agents
      (computation, literature, exploration, proof), manage dependencies between
      sub-tasks, aggregate results, and synthesize a coherent research report —
      for end-to-end automated research assistance
- [x] **104.3.2.2** Implement agent memory: persistent knowledge base for each
      agent, storing: previous computations and their results, discovered
      patterns, failed proof attempts (to avoid repeating), successful
      strategies, and domain-specific heuristics learned from experience
- [x] **104.3.2.3** Implement human-in-the-loop interface: agents present
      intermediate results and hypotheses for human review, humans can redirect
      agent focus, confirm or reject conjectures, provide hints for stuck proof
      searches, and approve publication-quality claims — ensuring all agent
      output is auditable

---

### 104.4 Conjecture Generation (matching/exceeding FunSearch)

- [x] **104.4.1.1** Implement pattern detection engine: given a collection of
      mathematical objects (sequences, graphs, groups, manifolds) and their
      computed invariants, detect correlations between invariants using
      statistical methods (correlation, mutual information), symbolic
      regression, and ML (graph neural networks for structural patterns) —
      matching the Graffiti/TxGraffiti approach
- [x] **104.4.1.2** Implement conjecture formulation: given detected patterns,
      formulate precise mathematical conjectures as formal statements
      (universally quantified propositions with conditions), rank by: novelty
      (not implied by known theorems), plausibility (verified on available
      data), significance (connects different areas), and falsifiability (clear
      counterexample search strategy)
- [x] **104.4.1.3** Implement **FunSearch-style evolutionary conjecture
      generation**: use LLM (Claude API) to generate candidate mathematical
      programs/constructions, evaluate each candidate against a scoring
      function, maintain a population of best candidates with island model
      (multiple independent populations with migration), evolve toward optimal
      constructions — matching DeepMind's FunSearch for discovering new
      mathematical constructions
- [x] **104.4.1.4** Implement conjecture validation pipeline: (1) check against
      OEIS / LMFDB / MathSciNet for known results, (2) test on additional
      examples beyond the discovery set, (3) attempt formal proof (via proof
      agent), (4) if proof fails, attempt counterexample search (via systematic
      or random testing), (5) classify as: proven / likely true (extensive
      evidence) / unknown / likely false / disproven
- [x] **104.4.1.5** Implement OEIS integration: given a sequence of integers,
      query the Online Encyclopedia of Integer Sequences (OEIS) API for matches,
      retrieve known formulas/references/relations, and use OEIS data to guide
      conjecture generation for combinatorial and number-theoretic sequences

---

### 104.5 Symbolic Regression (`@kalika/symbolic-regression`)

#### 104.5.1 Equation Discovery (matching/exceeding PySR + AI Feynman)

- [x] **104.5.1.1** Implement genetic programming engine for symbolic
      regression: tree-based representation of expressions, mutation operators
      (point mutation, subtree mutation, hoist, constant optimization),
      crossover (subtree exchange), selection (tournament, epsilon-lexicase),
      multi-objective optimization (accuracy vs complexity via Pareto front),
      and multi-population island model — matching PySR's evolutionary core
- [x] **104.5.1.2** Implement dimensional analysis constraints: given
      input/output dimensions (e.g., inputs are lengths and times, output is
      velocity), constrain the search space to dimensionally consistent
      expressions — matching AI Feynman's dimensional analysis preprocessing,
      which dramatically reduces search space for physics problems
- [x] **104.5.1.3** Implement symmetry detection: before regression, analyze
      data for symmetries (translational, rotational, permutation, scaling)
      using statistical tests, and impose these symmetries as constraints on
      candidate expressions — matching AI Feynman's symmetry preprocessing
- [x] **104.5.1.4** Implement separability detection: test if the target
      function is separable (f(x,y) = g(x) + h(y) or f(x,y) = g(x)\*h(y)) using
      neural network probing, and decompose the problem into lower-dimensional
      sub-problems — matching AI Feynman's recursive decomposition
- [x] **104.5.1.5** Implement **transformer-based symbolic regression**: train
      or fine-tune a sequence-to-sequence model that maps (data samples as input
      sequence) -> (symbolic expression in prefix notation as output sequence),
      providing instant symbolic regression via a single forward pass — matching
      Biggio/Kamienny et al.'s end-to-end approach, as a fast warm-start for the
      evolutionary search
- [x] **104.5.1.6** Implement **LLM-guided symbolic regression**: use Claude API
      to propose candidate functional forms based on domain knowledge (e.g.,
      "this data looks like it could be a power law with logarithmic
      corrections" or "the dependence on this parameter suggests a Bessel
      function"), then refine coefficients via nonlinear least squares — the
      emerging SOTA approach combining domain knowledge with data fitting
- [x] **104.5.1.7** Implement physics-specific equation discovery: given
      experimental data, discover governing differential equations (not just
      algebraic relations) using SINDy (Sparse Identification of Nonlinear
      Dynamics — express dx/dt as sparse linear combination of candidate library
      functions), weak-form SINDy (for noisy data), and PDE-FIND (discover PDE
      from spatiotemporal data)

---

### 104.6 Physics-Informed Neural Networks (`@kalika/neural-physics`)

- [x] **104.6.1.1** Implement PINN (Physics-Informed Neural Network) framework:
      neural network with PDE residual in loss function, boundary condition
      enforcement (soft via loss penalty or hard via architecture), collocation
      point sampling (uniform, Latin hypercube, adaptive — refine near
      high-residual regions), and training (Adam + L-BFGS, learning rate
      scheduling) — matching the Raissi-Perdikaris-Karniadakis framework with
      2024-2026 improvements
- [x] **104.6.1.2** Implement separable PINN: factor the neural network solution
      as a product/sum of lower-dimensional functions (each depending on fewer
      variables), dramatically reducing training cost for high-dimensional PDEs
      — the 2024 SOTA improvement
- [x] **104.6.1.3** Implement Fourier Neural Operator (FNO): learn operator
      mapping between function spaces by parameterizing the integral kernel in
      Fourier space, with residual connections and nonlinear activations between
      Fourier layers — for learning PDE solution operators that generalize
      across initial/boundary conditions (1000x faster than classical solvers
      for some problems)
- [x] **104.6.1.4** Implement DeepONet: branch network (encodes input
      function) + trunk network (encodes output location), universal
      approximation for operators, with proper training on paired (input
      function, output function) data — for learning general nonlinear operators
      between function spaces
- [x] **104.6.1.5** Implement neural network training infrastructure: automatic
      differentiation (forward-mode for few parameters, reverse-mode for many —
      using a lightweight AD engine or interfacing with JAX/PyTorch via Python
      subprocess), GPU training via wgpu compute shaders (for inference-only
      on-device) or Python/JAX for full training, and model serialization (ONNX
      export for deployment)

---

### 104.7 arXiv & Literature Integration

- [x] **104.7.1.1** Implement arXiv API client: full-text search (title,
      abstract, author, category), bulk metadata retrieval, category filtering
      (hep-th, hep-ph, gr-qc, math-ph, math.AG, math.NT, etc.), LaTeX source
      download and parsing, citation extraction from references section — with
      rate limiting and caching
- [x] **104.7.1.2** Implement INSPIRE-HEP API client: paper search, author
      profiles, citation network traversal, experiment/collaboration lookup,
      HEPData integration for numerical results from papers — the primary
      literature database for high-energy physics
- [x] **104.7.1.3** Implement Semantic Scholar API client: paper search with
      rich metadata, citation/reference graph traversal, TLDR summaries, paper
      embeddings for similarity search, author disambiguation, influential
      citation identification — for broad scientific literature coverage
- [x] **104.7.1.4** Implement OpenAlex API client: 250M+ works, open access,
      fast bulk queries, concept tagging, institution/author/venue metadata — as
      a complement to Semantic Scholar for comprehensive coverage
- [x] **104.7.1.5** Implement unified literature search: query all APIs
      simultaneously, deduplicate results (by DOI, arXiv ID, or title
      similarity), rank by relevance (combining API relevance scores with
      citation count and recency), and return enriched results with abstracts,
      citation counts, and links to full text

---

### 104.8 Tests

- [x] **104.8.1.1** Write formal verification tests: verify Lean 4 bridge
      correctly translates 50+ Kalika expressions to Lean 4 syntax, verify 20+
      CAS computation results are formally verified via tactic generation,
      verify cross-CAS verification catches introduced errors — minimum 100 test
      cases
- [x] **104.8.1.2** Write symbolic regression tests: verify recovery of known
      physics equations (Newton's law F=ma, Kepler's third law T^2 =
      (4pi^2/GM)a^3, ideal gas law PV=nRT, blackbody peak lambda_max = b/T) from
      synthetic data with noise — minimum 30 test cases comparing against PySR
      on the Feynman benchmark
- [x] **104.8.1.3** Write research agent tests: verify computation agent
      correctly solves 20+ standard problems (integrals, ODEs, eigenvalue
      problems), literature agent finds relevant papers for known topics,
      conjecture agent rediscovers known results (e.g., Euler's partition
      identity) from data — minimum 50 test cases
- [x] **104.8.1.4** Write PINN tests: verify PINN solves 1D heat equation, 2D
      Poisson equation, and Burgers equation to within 1% of exact/reference
      solutions — minimum 10 test cases with convergence verification
