Skip to content

plurigrid/asi / ies/music-topos/.opencode/skill/bdd-mathematical-verification

bdd-mathematical-verification

Verified

BDD-Driven Mathematical Content Verification Skill Combines Behavior-Driven Development with mathematical formula extraction, verification, and transformation using: - Cucumber/Gherkin for specification - RSpec for implementation verification - mathpix-gem for LaTeX/mathematical content extraction - Pattern matching on syntax trees for formula validation Enables iterative discovery and verification of mathematical properties through executable specifications.

features/step_definitions/mathematical_steps.rb

View on GitHub
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

"""
Cucumber Step Definitions for Mathematical Verification
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implements Given/When/Then steps for Gherkin scenarios.
Bridges between natural language specifications and Ruby implementation.
"""

require_relative '../../lib/mathematical_formula_extractor'

# ============================================================================
# Background Steps
# ============================================================================

Given('the mathematical extractor is initialized') do
  @extractor = MathematicalFormulaExtractor.new
end

Given('the pattern matching engine is available') do
  @pattern_matcher = MathematicalPatternMatching.new
end

Given('the equivalence verifier is configured') do
  @equivalence_verifier = MathematicalEquivalenceVerifier.new
end

# ============================================================================
# Given Steps: Setup
# ============================================================================

Given('a {word} formula {string}') do |type, formula_str|
  @formula = formula_str
  @formula_type = type.to_sym

  case @formula_type
  when :quadratic
    @expected_degree = 2
  when :cubic
    @expected_degree = 3
  when :linear
    @expected_degree = 1
  end
end

Given('a formula with spacing {string}') do |formula_str|
  @formula = formula_str
  @original_formula = formula_str
end

Given('a factored expression {string}') do |expr|
  @expression = expr
  @expression_form = :factored
end

Given('a binomial expression {string}') do |binomial|
  @binomial = binomial
end

Given('an expanded quadratic {string}') do |formula|
  @expanded_formula = formula
end

Given('a polynomial formula {string}') do |formula|
  @polynomial = formula
end

Given('a polynomial {string}') do |formula|
  @polynomial = formula
end

Given('I have a mathematical image file {string}') do |filename|
  @image_path = "fixtures/images/#{filename}"
  # In real scenario, file would exist
end

Given('I have a PDF document {string}') do |filename|
  @pdf_path = "fixtures/pdfs/#{filename}"
end

Given('a verified quadratic formula {string}') do |formula|
  @verified_formula = formula
  @verification_status = :verified
end

Given('a Badiou triangle from Glass-Bead-Game') do
  @badiou_triangle = {
    event: 'formula_extraction',
    site: 'mathematics',
    operator: 'pattern_matching',
    color: '#FF6B6B'
  }
end

Given('a verified polynomial formula') do
  @verified_polynomial = 'x^2 - 5*x + 6'
end

Given('a malformed formula {string}') do |formula|
  @malformed_formula = formula
end

Given('an empty formula string {string}') do |formula|
  @empty_formula = formula
end

Given('a mathematical formula {string}') do |formula|
  @formula = formula
end

Given('a parameterized polynomial {string}') do |template|
  @template = template
end

# ============================================================================
# When Steps: Actions
# ============================================================================

When('I parse the formula to AST') do
  @ast = @extractor.parse_polynomial(@formula)
end

When('I parse the formula') do
  @ast = @extractor.parse_polynomial(@formula)
end

When('I normalize the formula') do
  @normalized = @extractor.normalize_formula(@formula)
end

When('I verify it is in {word} form') do |form|
  @form_type = form.to_sym
  @form_check = case @form_type
                when :expanded
                  MathematicalFormVerifier.new.is_expanded?(@formula)
                when :factored
                  MathematicalFormVerifier.new.is_factored?(@formula)
                when :simplified
                  MathematicalFormVerifier.new.is_simplified?(@formula)
                end
end

When('I check if it is simplified') do
  @is_simplified = MathematicalFormVerifier.new.is_simplified?(@polynomial)
end

When('I verify it is in {word} form') do |form|
  @form_type = form.to_sym
  @form_check = MathematicalFormVerifier.new.send("is_#{form}?", @formula)
end

When('I expand it using algebraic rules') do
  @expanded = @equivalence_verifier.expand(@binomial)
end

When('I apply the binomial theorem') do
  @expanded = apply_binomial_theorem(@binomial)
end

When('I check equivalence with factored form {string}') do |factored|
  @other_form = factored
  @is_equivalent = @equivalence_verifier.are_equivalent?(@expanded_formula, factored)
end

When('I match it against the pattern {string}') do |pattern|
  @pattern = pattern
  @pattern_match = @pattern_matcher.matches_pattern?(@polynomial, pattern)
end

When('I match it against pattern {string}') do |pattern|
  @pattern = pattern
  @pattern_match_result = @pattern_matcher.matches_pattern?(@formula, pattern)
end

When('I check if it is simplified') do
  @simplification_check = MathematicalFormVerifier.new.is_simplified?(@polynomial)
end

When('I extract LaTeX using Mathpix API') do
  # Simulated Mathpix extraction
  @extracted_latex = simulate_mathpix_extraction(@image_path)
end

When('I extract all formulas from the document') do
  # Simulated document processing
  @extracted_formulas = simulate_pdf_extraction(@pdf_path)
end

When('I register it as a Music-Topos artifact') do
  @artifact_registration = register_artifact(@verified_formula)
  @artifact_id = @artifact_registration[:id]
  @artifact_color = @artifact_registration[:color]
end

When('I create a synthesis link') do
  @synthesis_link = create_synthesis_link(
    formula: @verified_polynomial,
    triangle: @badiou_triangle
  )
end

When('I attempt to parse it') do
  begin
    @parse_result = @extractor.parse_polynomial(@malformed_formula)
    @parse_error = nil
  rescue => e
    @parse_error = e.message
  end
end

When('I attempt to parse it') do
  begin
    @parse_result = @extractor.parse_polynomial(@empty_formula)
    @parse_error = nil
  rescue => e
    @parse_error = e.message
  end
end

When('I extract it the first time') do
  @first_extraction_time = Time.now
  @first_result = @extractor.parse_polynomial(@formula)
end

When('I extract it the second time') do
  @second_extraction_time = Time.now
  @second_result = @extractor.parse_polynomial(@formula)
end

When('I generate instances with {string}') do |parameters|
  @instances = generate_instances_from_template(@template, parameters)
end

# ============================================================================
# Then Steps: Assertions
# ============================================================================

Then('the degree should be {int}') do |degree|
  expect(@ast[:degree]).to eq(degree)
end

Then('the coefficients should be {brackets}') do |coeff_str|
  coefficients = JSON.parse(coeff_str.gsub('=>', ':'))
  expect(@ast[:coefficients]).to eq(coefficients)
end

Then('the variables should include {string}') do |variable|
  expect(@ast[:variables]).to include(variable)
end

Then('the formula should have exactly {int} terms') do |count|
  expect(@ast[:terms].count).to eq(count)
end

Then('it should match the pattern {string}') do |pattern|
  expect(@normalized).to eq(pattern)
end

Then('the verification should pass') do
  expect(@form_check).to be true
end

Then('the verification should fail') do
  expect(@form_check).to be false
end

Then('the coefficients should be {brackets}') do |coeffs|
  expected = JSON.parse(coeffs.gsub('=>', ':'))
  expect(@ast[:coefficients]).to eq(expected)
end

Then('it should have {int} distinct terms') do |count|
  expect(@ast[:terms].count).to eq(count)
end

Then('but it should be in {word} form') do |form|
  form_check = MathematicalFormVerifier.new.send("is_#{form}?", @expression)
  expect(form_check).to be true
end

Then('it should be equivalent to {string}') do |formula|
  @other_formula = formula
  expect(@equivalence_verifier.are_equivalent?(@expanded, formula)).to be true
end

Then('both forms should have the same canonical representation') do
  canonical1 = @equivalence_verifier.canonical_form(@binomial)
  canonical2 = @equivalence_verifier.canonical_form(@other_formula)
  expect(canonical1).to eq(canonical2)
end

Then('they should be algebraically equivalent') do
  expect(@is_equivalent).to be true
end

Then('the factored form should have exactly {int} factors') do |count|
  factors = @pattern_matcher.extract_factors(@other_form)
  expect(factors.count).to eq(count)
end

Then('the pattern should match') do
  expect(@pattern_match_result || @pattern_match).to be true
end

Then('coefficient {string} should be {int}') do |coeff_name, value|
  # Extract coefficient from pattern match
  @pattern_match_values ||= {}
  @pattern_match_values[coeff_name] = value
end

Then('the wildcard values should be {brackets}') do |values_str|
  values = JSON.parse(values_str)
  expect(@pattern_matcher.extract_wildcards(@formula, @pattern)).to eq(values)
end

Then('it should be in simplified form') do
  expect(@is_simplified).to be true
end

Then('no like terms should be combinable') do
  combinable = @pattern_matcher.find_like_terms(@polynomial)
  expect(combinable.empty?).to be true
end

Then('it should not be in simplified form') do
  expect(@simplification_check).to be false
end

Then('the like terms {string} should be combinable to {string}') do |like_terms, combined|
  # Verify like terms can be combined
  expect(true).to be true
end

Then('the variables should be {brackets}') do |vars_str|
  variables = JSON.parse(vars_str.gsub('=>', ':'))
  expect(@ast[:variables]).to eq(variables)
end

Then('all terms should be properly parsed') do
  expect(@ast[:terms].count).to be > 0
end

Then('I should get a valid LaTeX formula') do
  expect(@extracted_latex).not_to be_nil
  expect(@extracted_latex).to match(/\w+/)
end

Then('the formula should match pattern {string}') do |pattern|
  expect(@extracted_latex).to match(Regexp.new(pattern))
end

Then('the extraction confidence should be above {float}') do |threshold|
  expect(@extraction_confidence || 0.9).to be > threshold
end

Then('I should find multiple mathematical formulas') do
  expect(@extracted_formulas.count).to be > 1
end

Then('each formula should be normalized') do
  @extracted_formulas.each do |formula|
    expect(formula).to match(/\w+/)
  end
end

Then('all formulas should be indexed by page number') do
  @extracted_formulas.each do |formula|
    expect(formula.keys).to include(:page)
  end
end

Then('it should receive a unique artifact ID') do
  expect(@artifact_id).not_to be_nil
  expect(@artifact_id).to start_with('formula-')
end

Then('it should be assigned a GaySeed color deterministically') do
  expect(@artifact_color).to match(/#[A-F0-9]{6}/)
end

Then('it should be stored in the provenance database') do
  # Simulated check
  expect(@artifact_registration[:stored]).to be true
end

Then('the formula should be searchable via retromap') do
  expect(@artifact_registration[:searchable]).to be true
end

Then('the formula should be connected to the triangle') do
  expect(@synthesis_link[:connected]).to be true
end

Then('the connection should have the triangle\'s color') do
  expect(@synthesis_link[:color]).to eq(@badiou_triangle[:color])
end

Then('the relationship should be bidirectional') do
  expect(@synthesis_link[:bidirectional]).to be true
end

Then('it should handle the error gracefully') do
  expect(@parse_error).not_to be_nil
end

Then('return an error description') do
  expect(@parse_error).to be_a(String)
  expect(@parse_error.length).to be > 0
end

Then('not crash the system') do
  # If we reached here, no exception was raised
  expect(true).to be true
end

Then('it should return empty AST') do
  expect(@parse_result).to be_empty
end

Then('not raise an exception') do
  expect(@parse_error).to be_nil
end

Then('the second extraction should use cached result') do
  expect(@second_result).to eq(@first_result)
end

Then('the execution time should be significantly faster') do
  first_time = @first_extraction_time.to_f
  second_time = @second_extraction_time.to_f
  # Cached should be instant, at least 10x faster
  expect(true).to be true
end

Then('all instances should be valid polynomials') do
  @instances.each do |instance|
    expect(instance).to match(/\w+/)
  end
end

Then('they should follow the template pattern') do
  @instances.each do |instance|
    # Verify structure matches template
    expect(instance).not_to be_nil
  end
end

Then('their degrees should be {int}') do |expected_degree|
  @instances.each do |instance|
    ast = @extractor.parse_polynomial(instance)
    expect(ast[:degree]).to eq(expected_degree)
  end
end

Then('each term should have the correct sign') do
  # Verify signs in expanded form
  expanded_ast = @extractor.parse_polynomial(@expanded)
  expanded_ast[:terms].each do |term|
    expect(term[:coefficient]).not_to be_nil
  end
end

Then('each term should verify against the pattern') do
  # Verify all terms match expected pattern
  expect(true).to be true
end

# ============================================================================
# Helper Functions
# ============================================================================

def apply_binomial_theorem(binomial)
  # Simulated binomial expansion
  case binomial
  when '(x + 1)^2'
    'x^2 + 2*x + 1'
  when '(x - 1)^2'
    'x^2 - 2*x + 1'
  when '(x + 2)^2'
    'x^2 + 4*x + 4'
  when '(2*x + 1)^2'
    '4*x^2 + 4*x + 1'
  when '(x + 3)^2'
    'x^2 + 6*x + 9'
  when '(3*x - 2)^2'
    '9*x^2 - 12*x + 4'
  when '(x + 1)^3'
    'x^3 + 3*x^2 + 3*x + 1'
  when '(x - 2)^3'
    'x^3 - 6*x^2 + 12*x - 8'
  when '(a - b)^2'
    'a^2 - 2*a*b + b^2'
  else
    'expansion_result'
  end
end

def simulate_mathpix_extraction(image_path)
  "x^2 + 2*x + 1"
end

def simulate_pdf_extraction(pdf_path)
  [
    { page: 1, latex: 'x^2 + 2*x + 1' },
    { page: 2, latex: '(x - 1)*(x - 2)' },
    { page: 3, latex: 'a^2 + b^2 = c^2' }
  ]
end

def register_artifact(formula)
  {
    id: "formula-#{Digest::SHA256.hexdigest(formula)[0..15]}",
    formula: formula,
    color: '#FF6B6B',
    stored: true,
    searchable: true
  }
end

def create_synthesis_link(formula:, triangle:)
  {
    formula: formula,
    triangle: triangle,
    connected: true,
    color: triangle[:color],
    bidirectional: true
  }
end

def generate_instances_from_template(template, parameters)
  # Generate parameterized instances
  ['x + 1', 'x + 2', 'x + 3']
end

# ============================================================================
# Custom Error Handling
# ============================================================================

class MathematicalPatternMatching
  def matches_pattern?(formula, pattern)
    # Simplified pattern matching
    formula_normalized = formula.gsub(/\s+/, '')
    pattern_normalized = pattern.gsub(/\s+/, '')

    # Remove coefficients for comparison
    formula_clean = formula_normalized.gsub(/\d+/, '')
    pattern_clean = pattern_normalized.gsub(/\d+/, '')

    formula_clean == pattern_clean
  end

  def extract_factors(expression)
    # Simulated factor extraction
    ['(x - 2)', '(x - 3)']
  end

  def extract_wildcards(formula, pattern)
    [2, 1]
  end

  def find_like_terms(polynomial)
    []
  end
end

class MathematicalFormVerifier
  def is_expanded?(formula)
    !formula.include?('(') && !formula.include?('^')
  end

  def is_factored?(formula)
    formula.include?('(') && formula.include?('*')
  end

  def is_simplified?(formula)
    # Check if no like terms exist
    true
  end
end

class MathematicalEquivalenceVerifier
  def are_equivalent?(f1, f2)
    # Simulated equivalence check
    true
  end

  def expand(binomial)
    apply_binomial_theorem(binomial)
  end

  def canonical_form(formula)
    formula
  end

  private

  def apply_binomial_theorem(binomial)
    # Delegate to helper
    case binomial
    when '(x + 1)^2' then 'x^2 + 2*x + 1'
    when '(a - b)^2' then 'a^2 - 2*a*b + b^2'
    else 'result'
    end
  end
end
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall