-- | 'substitute' @sigma@ takes a polynomial and a map @sigma@ which contains the substitutions.
-- every variable gets substituted by the corresponding polynomial found in the map
substitute :: Polynomial -> [(Var, Polynomial)] -> Polynomial
substitute (Polynomial p) sigma= foldl1 (addPolynomials) (map substitueMonomial p) where
    substitueMonomial (Monomial c vs) = foldl (multiplyPolynomials) (Polynomial [Monomial c []]) (map (var2Poly) vs) where
        var2Poly var = maybe (Polynomial [Monomial (ident (dimension (Polynomial p))) [var]]) (snd) (findTuple var)
        findTuple var = Data.List.find ((var == ) . fst) sigma

-- | 'compatible' @p@ takes a polynomial and checks if it is compatible, which means there are no negative coefficients
-- in the polynomial and the constant factor is positive
substitute :: Polynomial -> [(Var, Polynomial)] -> Polynomial
substitute (Polynomial p) sigma= foldl1 (addPolynomials) (map substitueMonomial p) where
    substitueMonomial (Monomial c vs) = foldl (multiplyPolynomials) (Polynomial [Monomial c []]) (map (var2Poly) vs) where
        var2Poly var = maybe (Polynomial [Monomial (ident (dimension (Polynomial p))) [var]]) (snd) (findTuple var)
        findTuple var = Data.List.find ((var == ) . fst) sigma

-- | 'compatible' @p@ takes a polynomial and checks if it is compatible, which means there are no negative coefficients
-- in the polynomial and the constant factor is positive
substitute (Polynomial p) sigma= foldl1 (addPolynomials) (map substitueMonomial p) where
    substitueMonomial (Monomial c vs) = foldl (multiplyPolynomials) (Polynomial [Monomial c []]) (map (var2Poly) vs) where
        var2Poly var = maybe (Polynomial [Monomial (ident (dimension (Polynomial p))) [var]]) (snd) (findTuple var)
        findTuple var = Data.List.find ((var == ) . fst) sigma

-- | 'compatible' @p@ takes a polynomial and checks if it is compatible, which means there are no negative coefficients
-- in the polynomial and the constant factor is positive