use crate::divisibility::Domain;
use crate::ring::*;
use crate::pid::*;

///
/// Trait for rings that are fields, i.e. where every
/// nonzero element has an inverse.
/// 
/// Note that fields must be commutative.
/// 
pub trait Field: Domain + EuclideanRing {

    ///
    /// Computes the division `lhs / rhs`, where `rhs != 0`.
    /// 
    /// Panics if `rhs = 0`.
    /// 
    fn div(&self, lhs: &Self::Element, rhs: &Self::Element) -> Self::Element {
        assert!(!self.is_zero(rhs));
        assert!(self.is_commutative());
        return self.checked_left_div(lhs, rhs).unwrap();
    }
}

///
/// Trait for [`RingStore`]s that store [`Field`]s. Mainly used
/// to provide a convenient interface to the `Field`-functions.
/// 
pub trait FieldStore: RingStore + EuclideanRingStore
    where Self::Type: Field
{
    delegate!{ Field, fn div(&self, lhs: &El<Self>, rhs: &El<Self>) -> El<Self> }
}

impl<R> FieldStore for R
    where R: RingStore, R::Type: Field
{}

///
/// Field such that every finite degree extension field is separable.
/// 
/// This is equivalent to the following:
///  - Every irreducible polynomial is square-free (over the algebraic closure)
///  - Every finite-degree field extension is simple, i.e. generated by a single element
/// 
/// Note that currently, I sometimes make the assumption that a `PerfectField` is either
/// finite, or has characteristic 0. This is clearly not the case in general, but all perfect
/// fields that currently exist are one of those. I would even say that infinite perfect fields
/// of positive characteristic are quite an exotic case in computer algebra, and I do not expect
/// to implement them in the forseeable future. The simplest example of such a field would be
/// the algebraic closure of the function field over a finite field, i.e. `AlgClosure(Fp(X))`.
/// 
pub trait PerfectField: Field {}

#[cfg(any(test, feature = "generic_tests"))]
pub mod generic_tests {
    use super::*;

    pub fn test_field_axioms<R, I>(R: R, edge_case_elements: I)
        where R: FieldStore, R::Type: Field, I: Iterator<Item = El<R>>
    {
        let edge_case_elements = edge_case_elements.collect::<Vec<_>>();
        for (i, a) in edge_case_elements.iter().enumerate() {
            for (j, b) in edge_case_elements.iter().enumerate() {
                assert!(i == j || !R.eq_el(&a, &b));
                if !R.is_zero(&b) {
                    assert_el_eq!(R, a, R.mul_ref_fst(&b, R.div(&a, &b)));
                }
            }
        }
    }
}