Research-Stack/0-Core-Formalism/lean/Semantics/Semantics/LeanBridge.lean

125 lines
2.9 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Mathlib.Data.Rat.Defs
/-!
Lean Bridge Utilities
ID: LEAN-BRIDGE-1
This module provides utility functions and type class instances to smooth over
Lean 4 idiosyncrasies encountered during development.
Common issues addressed:
- Structure field notation complexity in theorem hypotheses
- BEq instance not found for functions returning Bool
- OfNat instance resolution for numerals in
- Structure constructor syntax differences
- List operations requiring BEq instances
STATUS: UTILITY
WARNING:
- This is a pragmatic engineering solution to Lean 4 friction points
- Use these utilities to avoid common type inference pitfalls
- Prefer direct Lean idioms when they work cleanly
-/
namespace Semantics
/--
Safe list count with predicate function.
Avoids BEq instance issues by using a manual recursive implementation
instead of List.count which requires BEq for the predicate result type.
-/
def safeCount {α : Type} (pred : α → Bool) (xs : List α) : Nat :=
let rec go (ys : List α) (acc : Nat) : Nat :=
match ys with
| [] => acc
| y :: rest =>
if pred y then
go rest (acc + 1)
else
go rest acc
go xs 0
/--
Structure type marker for typeclass dispatch.
Use this to mark types that are structures (not inductives) for
special handling in utility functions.
-/
class StructType (α : Type) where
/--
Safe rational numeral conversion.
Provides explicit type annotations to avoid OfNat instance resolution issues.
-/
def q0 : := (0 : )
def q1 : := (1 : )
/--
Safe structure construction helper.
Avoids ⟨⟩ notation issues by using explicit field assignment syntax
that works reliably across Lean versions.
-/
def mkStruct {α : Type} [StructType α] (fields : α) : α :=
fields
/--
Pattern matching helper for structures.
Provides a cases-like interface that works better with structure types
in theorem proofs.
-/
def structCases {α : Type} [StructType α] (x : α) (onFields : α → β) : β :=
onFields x
/--
List filter with explicit predicate.
Alternative to List.filter that avoids BEq requirements.
-/
def safeFilter {α : Type} (pred : α → Bool) (xs : List α) : List α :=
xs.filter pred
/--
List find with predicate.
Returns the first element satisfying the predicate, or none if not found.
-/
def safeFind {α : Type} (pred : α → Bool) (xs : List α) : Option α :=
xs.find? pred
/--
List any with predicate.
Returns true if any element satisfies the predicate.
-/
def safeAny {α : Type} (pred : α → Bool) (xs : List α) : Bool :=
xs.any pred
/--
List all with predicate.
Returns true if all elements satisfy the predicate.
-/
def safeAll {α : Type} (pred : α → Bool) (xs : List α) : Bool :=
xs.all pred
/--
Nat to conversion helper.
Avoids type inference issues in arithmetic contexts.
-/
def natToQ (n : Nat) : :=
(n : )
/--
Int to conversion helper.
Avoids type inference issues in arithmetic contexts.
-/
def intToQ (n : Int) : :=
(n : )
end Semantics