Init.ShareCommon unchecked StateFactoryBuilder breaks runtime/spec consistency

Abierto
#16,779 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Área
compilers

Línea de trabajo

Start with src/Init/ShareCommon.lean lines 47–86 and the minimal reproduction in the issue, then trace how StateFactoryBuilder callbacks are used by StateFactory.mk and State.shareCommon at runtime. Determine how to preserve the logical result when callbacks are non-lawful and verify that the reproduction no longer makes native_decide derive a contradiction.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    Paranoid catches it
Description

ShareCommon.StateFactoryBuilder is a public structure with no lawfulness constraints on mapFind? / setFind?, and ShareCommon.StateFactory.mk is a public opaque function implemented via unsafeCast (@[implemented_by Internal.StateFactory.mkImpl]). Meanwhile, ShareCommon.State.shareCommon has the pure logical definition (a, s) while executing @[extern "lean_state_sharecommon"] at runtime, returning whatever setFind? / mapFind? returns.

Providing a non-lawful setFind? (e.g., returning the previously inserted element regardless of the lookup key) makes (s.shareCommon a).1 disagree with its logical definition a—and across distinct types α ≠ β, returns an α value at type β in safe Lean (no unsafe, @[extern], or @[implemented_by]). This breaks runtime type safety and lets native_decide prove 0 = 1.

Scope: Does not affect pure-kernel proofs (the proof of 0 = 1 depends on .native_decide.ax_ and is rejected by standard axiom checks). Still present on master after #16715.

Where issue is

src/Init/ShareCommon.lean lines 47–86 (e6e9431b15):
public structure StateFactoryBuilder (lines 47–55)
@[implemented_by Internal.StateFactory.mkImpl] public opaque StateFactory.mk (lines 71–72)
@[extern "lean_state_sharecommon"] public def State.shareCommon (lines 85–86)

Minimum Reproducible Issue

import Init.ShareCommon

def evilFactory : ShareCommon.StateFactory :=
  ShareCommon.StateFactory.mk {
    Map := fun _ β _ _ => Option β
    mkMap := fun _ => none
    mapFind? := fun m _ => m
    mapInsert := fun _ _ v => some v
    Set := fun α _ _ => Option α
    mkSet := fun _ => none
    setFind? := fun s _ => s
    setInsert := fun _ x => some x
  }

def evilVal : List Nat :=
  let s0 : ShareCommon.State.{0} evilFactory := ShareCommon.State.mk evilFactory
  let (_, s1) := s0.shareCommon [0]
  (s1.shareCommon [1]).1

theorem evil_spec : evilVal = [1] := rfl
theorem evil_native : evilVal = [0] := by native_decide

theorem zero_eq_one : 0 = 1 := by
  have h : ([0] : List Nat) = [1] := evil_native.symm.trans evil_spec
  injection h

#print axioms zero_eq_one

Output

'zero_eq_one' depends on axioms: [Classical.choice, evil_native._native.native_decide.ax_1_1]

Versions

v4.36.0 , v4.37.0-rc1

Lenguaje dominante
Lean
Estrellas
10.3k
Forks
1.1k
Merge medio
16 h 45 min
PR fusionados (30 d)
361

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de leanprover/lean4

Todos los issues de leanprover/lean4

Issues similares

Más issues de Compilers

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.