use crate::core::Expression;
use crate::functions::properties::FunctionProperties;
use std::collections::HashMap;
pub trait FunctionIntelligence: Send + Sync {
fn family_name(&self) -> &'static str;
fn get_all_properties(&self) -> HashMap<String, FunctionProperties>;
fn has_function(&self, name: &str) -> bool;
fn function_count(&self) -> usize {
self.get_all_properties().len()
}
fn function_names(&self) -> Vec<String> {
self.get_all_properties().keys().cloned().collect()
}
}
pub trait FunctionEducator: Send + Sync {
fn explain_evaluation(&self, name: &str, args: &[Expression]) -> Vec<String>;
fn to_latex(&self, name: &str, args: &[Expression]) -> String;
fn get_background(&self, name: &str) -> Option<String>;
fn get_related_concepts(&self, name: &str) -> Vec<String>;
}
pub trait FunctionOptimizer: Send + Sync {
fn optimize_bulk_evaluation(&self, name: &str, values: &[f64]) -> Option<Vec<f64>>;
fn detect_special_values(&self, name: &str, args: &[Expression]) -> Option<Expression>;
fn optimal_strategy(&self, name: &str, input_size: usize) -> EvaluationStrategy;
fn complexity_estimate(&self, name: &str, input_size: usize) -> ComplexityEstimate;
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum EvaluationStrategy {
Direct,
SIMD,
Series,
Lookup,
Recursive,
}
#[derive(Debug, Clone)]
pub struct ComplexityEstimate {
pub time_complexity: f64,
pub space_complexity: usize,
pub accuracy: f64,
pub recommended_limit: usize,
}
pub trait PropertyValidator: Send + Sync {
fn validate_properties(&self, name: &str, properties: &FunctionProperties) -> ValidationResult;
fn validate_consistency(&self, functions: &[(&str, &FunctionProperties)]) -> ValidationResult;
fn validate_accuracy(&self, name: &str, test_cases: &[(Vec<f64>, f64)]) -> ValidationResult;
}
#[derive(Debug, Clone)]
pub struct ValidationResult {
pub is_valid: bool,
pub score: f64,
pub report: String,
pub issues: Vec<ValidationIssue>,
}
#[derive(Debug, Clone)]
pub struct ValidationIssue {
pub severity: IssueSeverity,
pub description: String,
pub suggested_fix: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IssueSeverity {
Info,
Warning,
Error,
Critical,
}
pub trait MetadataProvider: Send + Sync {
fn get_references(&self, name: &str) -> Vec<Reference>;
fn get_version(&self, name: &str) -> Option<Version>;
fn get_compatibility(&self, name: &str) -> CompatibilityInfo;
fn get_implementation_notes(&self, name: &str) -> Option<String>;
}
#[derive(Debug, Clone)]
pub struct Reference {
pub authors: Vec<String>,
pub title: String,
pub publication: String,
pub year: u32,
pub identifier: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
#[derive(Debug, Clone)]
pub struct CompatibilityInfo {
pub min_version: Version,
pub max_version: Option<Version>,
pub breaking_changes: Vec<String>,
pub deprecated_features: Vec<String>,
}
pub trait CompleteFunctionIntelligence:
FunctionIntelligence + FunctionEducator + FunctionOptimizer + PropertyValidator + MetadataProvider
{
fn generate_report(&self) -> IntelligenceReport {
IntelligenceReport {
family_name: self.family_name().to_owned(),
function_count: self.function_count(),
function_names: self.function_names(),
capabilities: vec![
"Intelligence".to_owned(),
"Evaluation".to_owned(),
"Education".to_owned(),
"Optimization".to_owned(),
"Validation".to_owned(),
"Metadata".to_owned(),
],
}
}
}
#[derive(Debug, Clone)]
pub struct IntelligenceReport {
pub family_name: String,
pub function_count: usize,
pub function_names: Vec<String>,
pub capabilities: Vec<String>,
}
pub trait IntelligenceFactory {
type Intelligence: FunctionIntelligence;
fn create_default() -> Self::Intelligence;
fn create_with_config(config: &IntelligenceConfig) -> Self::Intelligence;
fn default_config() -> IntelligenceConfig;
}
#[derive(Debug, Clone)]
pub struct IntelligenceConfig {
pub high_precision: bool,
pub enable_simd: bool,
pub max_cache_size: usize,
pub validation_level: ValidationLevel,
pub custom_params: HashMap<String, String>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ValidationLevel {
None,
Basic,
Standard,
Strict,
Research,
}
impl Default for IntelligenceConfig {
fn default() -> Self {
Self {
high_precision: false,
enable_simd: true,
max_cache_size: 1024,
validation_level: ValidationLevel::Standard,
custom_params: HashMap::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version_ordering() {
let v1 = Version {
major: 1,
minor: 0,
patch: 0,
};
let v2 = Version {
major: 1,
minor: 0,
patch: 1,
};
let v3 = Version {
major: 1,
minor: 1,
patch: 0,
};
assert!(v1 < v2);
assert!(v2 < v3);
assert!(v1 < v3);
}
#[test]
fn test_issue_severity_ordering() {
assert!(IssueSeverity::Info < IssueSeverity::Warning);
assert!(IssueSeverity::Warning < IssueSeverity::Error);
assert!(IssueSeverity::Error < IssueSeverity::Critical);
}
#[test]
fn test_default_config() {
let config = IntelligenceConfig::default();
assert!(!config.high_precision);
assert!(config.enable_simd);
assert_eq!(config.max_cache_size, 1024);
assert_eq!(config.validation_level, ValidationLevel::Standard);
}
}